blob: 311f4f0e916be0df949278e6cebf5b0b8f87674c [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
1531#ifndef FEAT_EVAL
1532 /*
1533 * Reset if_level, in case a sourced script file contains more ":if" than
1534 * ":endif" (could be ":if x | foo | endif").
1535 */
1536 if_level = 0;
1537#endif
1538
1539 --call_depth;
1540 return retval;
1541}
1542
1543#ifdef FEAT_EVAL
1544/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001545 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001546 */
1547 static char_u *
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001548get_loop_line(c, cookie, indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549 int c;
1550 void *cookie;
1551 int indent;
1552{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001553 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001554 wcmd_T *wp;
1555 char_u *line;
1556
1557 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1558 {
1559 if (cp->repeating)
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001560 return NULL; /* trying to read past ":endwhile"/":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001562 /* First time inside the ":while"/":for": get line normally. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563 if (cp->getline == NULL)
1564 line = getcmdline(c, 0L, indent);
1565 else
1566 line = cp->getline(c, cp->cookie, indent);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001567 if (line != NULL && store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001568 ++cp->current_line;
1569
1570 return line;
1571 }
1572
1573 KeyTyped = FALSE;
1574 ++cp->current_line;
1575 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
1576 sourcing_lnum = wp->lnum;
1577 return vim_strsave(wp->line);
1578}
1579
1580/*
1581 * Store a line in "gap" so that a ":while" loop can execute it again.
1582 */
1583 static int
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001584store_loop_line(gap, line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585 garray_T *gap;
1586 char_u *line;
1587{
1588 if (ga_grow(gap, 1) == FAIL)
1589 return FAIL;
1590 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
1591 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = sourcing_lnum;
1592 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593 return OK;
1594}
1595
1596/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001597 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598 */
1599 static void
1600free_cmdlines(gap)
1601 garray_T *gap;
1602{
1603 while (gap->ga_len > 0)
1604 {
1605 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1606 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607 }
1608}
1609#endif
1610
1611/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001612 * If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals
1613 * "func". * Otherwise return TRUE when "fgetline" equals "func".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001614 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615 int
Bram Moolenaar89d40322006-08-29 15:30:07 +00001616getline_equal(fgetline, cookie, func)
1617 char_u *(*fgetline) __ARGS((int, void *, int));
Bram Moolenaar78a15312009-05-15 19:33:18 +00001618 void *cookie UNUSED; /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619 char_u *(*func) __ARGS((int, void *, int));
1620{
1621#ifdef FEAT_EVAL
1622 char_u *(*gp) __ARGS((int, void *, int));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001623 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001624
Bram Moolenaar89d40322006-08-29 15:30:07 +00001625 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001626 * function that's originally used to obtain the lines. This may be
1627 * nested several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001628 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001629 cp = (struct loop_cookie *)cookie;
1630 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631 {
1632 gp = cp->getline;
1633 cp = cp->cookie;
1634 }
1635 return gp == func;
1636#else
Bram Moolenaar89d40322006-08-29 15:30:07 +00001637 return fgetline == func;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638#endif
1639}
1640
1641#if defined(FEAT_EVAL) || defined(FEAT_MBYTE) || defined(PROTO)
1642/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001643 * If "fgetline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644 * getline function. Otherwise return "cookie".
1645 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001646 void *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001647getline_cookie(fgetline, cookie)
Bram Moolenaar78a15312009-05-15 19:33:18 +00001648 char_u *(*fgetline) __ARGS((int, void *, int)) UNUSED;
Bram Moolenaar89d40322006-08-29 15:30:07 +00001649 void *cookie; /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001650{
1651# ifdef FEAT_EVAL
1652 char_u *(*gp) __ARGS((int, void *, int));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001653 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001654
Bram Moolenaar89d40322006-08-29 15:30:07 +00001655 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001656 * cookie that's originally used to obtain the lines. This may be nested
Bram Moolenaar071d4272004-06-13 20:20:40 +00001657 * several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001658 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001659 cp = (struct loop_cookie *)cookie;
1660 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661 {
1662 gp = cp->getline;
1663 cp = cp->cookie;
1664 }
1665 return cp;
1666# else
1667 return cookie;
1668# endif
1669}
1670#endif
1671
1672/*
1673 * Execute one Ex command.
1674 *
1675 * If 'sourcing' is TRUE, the command will be included in the error message.
1676 *
1677 * 1. skip comment lines and leading space
1678 * 2. handle command modifiers
1679 * 3. parse range
1680 * 4. parse command
1681 * 5. parse arguments
1682 * 6. switch on command name
1683 *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001684 * Note: "fgetline" can be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001685 *
1686 * This function may be called recursively!
1687 */
1688#if (_MSC_VER == 1200)
1689/*
Bram Moolenaared203462004-06-16 11:19:22 +00001690 * Avoid optimisation bug in VC++ version 6.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001691 */
Bram Moolenaar281bdce2005-01-25 21:53:18 +00001692 #pragma optimize( "g", off )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693#endif
1694 static char_u *
1695do_one_cmd(cmdlinep, sourcing,
1696#ifdef FEAT_EVAL
1697 cstack,
1698#endif
Bram Moolenaar89d40322006-08-29 15:30:07 +00001699 fgetline, cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001700 char_u **cmdlinep;
1701 int sourcing;
1702#ifdef FEAT_EVAL
1703 struct condstack *cstack;
1704#endif
Bram Moolenaar89d40322006-08-29 15:30:07 +00001705 char_u *(*fgetline) __ARGS((int, void *, int));
1706 void *cookie; /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001707{
1708 char_u *p;
1709 linenr_T lnum;
1710 long n;
1711 char_u *errormsg = NULL; /* error message */
1712 exarg_T ea; /* Ex command arguments */
1713 long verbose_save = -1;
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001714 int save_msg_scroll = msg_scroll;
1715 int save_msg_silent = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716 int did_esilent = 0;
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001717#ifdef HAVE_SANDBOX
1718 int did_sandbox = FALSE;
1719#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720 cmdmod_T save_cmdmod;
1721 int ni; /* set when Not Implemented */
1722
1723 vim_memset(&ea, 0, sizeof(ea));
1724 ea.line1 = 1;
1725 ea.line2 = 1;
1726#ifdef FEAT_EVAL
1727 ++ex_nesting_level;
1728#endif
1729
1730 /* when not editing the last file :q has to be typed twice */
1731 if (quitmore
1732#ifdef FEAT_EVAL
1733 /* avoid that a function call in 'statusline' does this */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001734 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001735#endif
1736 )
1737 --quitmore;
1738
1739 /*
1740 * Reset browse, confirm, etc.. They are restored when returning, for
1741 * recursive calls.
1742 */
1743 save_cmdmod = cmdmod;
1744 vim_memset(&cmdmod, 0, sizeof(cmdmod));
1745
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001746 /* "#!anything" is handled like a comment. */
1747 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1748 goto doend;
1749
Bram Moolenaar071d4272004-06-13 20:20:40 +00001750 /*
1751 * Repeat until no more command modifiers are found.
1752 */
1753 ea.cmd = *cmdlinep;
1754 for (;;)
1755 {
1756/*
1757 * 1. skip comment lines and leading white space and colons
1758 */
1759 while (*ea.cmd == ' ' || *ea.cmd == '\t' || *ea.cmd == ':')
1760 ++ea.cmd;
1761
1762 /* in ex mode, an empty line works like :+ */
1763 if (*ea.cmd == NUL && exmode_active
Bram Moolenaar89d40322006-08-29 15:30:07 +00001764 && (getline_equal(fgetline, cookie, getexmodeline)
1765 || getline_equal(fgetline, cookie, getexline))
Bram Moolenaardf177f62005-02-22 08:39:57 +00001766 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767 {
1768 ea.cmd = (char_u *)"+";
1769 ex_pressedreturn = TRUE;
1770 }
1771
1772 /* ignore comment and empty lines */
Bram Moolenaarf998c042007-11-20 11:31:26 +00001773 if (*ea.cmd == '"')
1774 goto doend;
1775 if (*ea.cmd == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001776 {
1777 ex_pressedreturn = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001779 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780
1781/*
1782 * 2. handle command modifiers.
1783 */
1784 p = ea.cmd;
1785 if (VIM_ISDIGIT(*ea.cmd))
1786 p = skipwhite(skipdigits(ea.cmd));
1787 switch (*p)
1788 {
1789 /* When adding an entry, also modify cmd_exists(). */
1790 case 'a': if (!checkforcmd(&ea.cmd, "aboveleft", 3))
1791 break;
1792#ifdef FEAT_WINDOWS
1793 cmdmod.split |= WSP_ABOVE;
1794#endif
1795 continue;
1796
1797 case 'b': if (checkforcmd(&ea.cmd, "belowright", 3))
1798 {
1799#ifdef FEAT_WINDOWS
1800 cmdmod.split |= WSP_BELOW;
1801#endif
1802 continue;
1803 }
1804 if (checkforcmd(&ea.cmd, "browse", 3))
1805 {
Bram Moolenaard812df62008-11-09 12:46:09 +00001806#ifdef FEAT_BROWSE_CMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807 cmdmod.browse = TRUE;
1808#endif
1809 continue;
1810 }
1811 if (!checkforcmd(&ea.cmd, "botright", 2))
1812 break;
1813#ifdef FEAT_WINDOWS
1814 cmdmod.split |= WSP_BOT;
1815#endif
1816 continue;
1817
1818 case 'c': if (!checkforcmd(&ea.cmd, "confirm", 4))
1819 break;
1820#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1821 cmdmod.confirm = TRUE;
1822#endif
1823 continue;
1824
1825 case 'k': if (checkforcmd(&ea.cmd, "keepmarks", 3))
1826 {
1827 cmdmod.keepmarks = TRUE;
1828 continue;
1829 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001830 if (checkforcmd(&ea.cmd, "keepalt", 5))
1831 {
1832 cmdmod.keepalt = TRUE;
1833 continue;
1834 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001835 if (!checkforcmd(&ea.cmd, "keepjumps", 5))
1836 break;
1837 cmdmod.keepjumps = TRUE;
1838 continue;
1839
1840 /* ":hide" and ":hide | cmd" are not modifiers */
1841 case 'h': if (p != ea.cmd || !checkforcmd(&p, "hide", 3)
1842 || *p == NUL || ends_excmd(*p))
1843 break;
1844 ea.cmd = p;
1845 cmdmod.hide = TRUE;
1846 continue;
1847
1848 case 'l': if (checkforcmd(&ea.cmd, "lockmarks", 3))
1849 {
1850 cmdmod.lockmarks = TRUE;
1851 continue;
1852 }
1853
1854 if (!checkforcmd(&ea.cmd, "leftabove", 5))
1855 break;
1856#ifdef FEAT_WINDOWS
1857 cmdmod.split |= WSP_ABOVE;
1858#endif
1859 continue;
1860
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001861 case 'n': if (!checkforcmd(&ea.cmd, "noautocmd", 3))
1862 break;
1863#ifdef FEAT_AUTOCMD
1864 if (cmdmod.save_ei == NULL)
1865 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001866 /* Set 'eventignore' to "all". Restore the
1867 * existing option value later. */
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001868 cmdmod.save_ei = vim_strsave(p_ei);
1869 set_string_option_direct((char_u *)"ei", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001870 (char_u *)"all", OPT_FREE, SID_NONE);
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001871 }
1872#endif
1873 continue;
1874
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875 case 'r': if (!checkforcmd(&ea.cmd, "rightbelow", 6))
1876 break;
1877#ifdef FEAT_WINDOWS
1878 cmdmod.split |= WSP_BELOW;
1879#endif
1880 continue;
1881
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001882 case 's': if (checkforcmd(&ea.cmd, "sandbox", 3))
1883 {
1884#ifdef HAVE_SANDBOX
1885 if (!did_sandbox)
1886 ++sandbox;
1887 did_sandbox = TRUE;
1888#endif
1889 continue;
1890 }
1891 if (!checkforcmd(&ea.cmd, "silent", 3))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001892 break;
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001893 if (save_msg_silent == -1)
1894 save_msg_silent = msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895 ++msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896 if (*ea.cmd == '!' && !vim_iswhite(ea.cmd[-1]))
1897 {
1898 /* ":silent!", but not "silent !cmd" */
1899 ea.cmd = skipwhite(ea.cmd + 1);
1900 ++emsg_silent;
1901 ++did_esilent;
1902 }
1903 continue;
1904
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001905 case 't': if (checkforcmd(&p, "tab", 3))
1906 {
1907#ifdef FEAT_WINDOWS
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001908 if (vim_isdigit(*ea.cmd))
1909 cmdmod.tab = atoi((char *)ea.cmd) + 1;
1910 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001911 cmdmod.tab = tabpage_index(curtab) + 1;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001912 ea.cmd = p;
1913#endif
1914 continue;
1915 }
1916 if (!checkforcmd(&ea.cmd, "topleft", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917 break;
1918#ifdef FEAT_WINDOWS
1919 cmdmod.split |= WSP_TOP;
1920#endif
1921 continue;
1922
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001923 case 'u': if (!checkforcmd(&ea.cmd, "unsilent", 3))
1924 break;
1925 if (save_msg_silent == -1)
1926 save_msg_silent = msg_silent;
1927 msg_silent = 0;
1928 continue;
1929
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930 case 'v': if (checkforcmd(&ea.cmd, "vertical", 4))
1931 {
1932#ifdef FEAT_VERTSPLIT
1933 cmdmod.split |= WSP_VERT;
1934#endif
1935 continue;
1936 }
1937 if (!checkforcmd(&p, "verbose", 4))
1938 break;
1939 if (verbose_save < 0)
1940 verbose_save = p_verbose;
Bram Moolenaared203462004-06-16 11:19:22 +00001941 if (vim_isdigit(*ea.cmd))
1942 p_verbose = atoi((char *)ea.cmd);
1943 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001944 p_verbose = 1;
1945 ea.cmd = p;
1946 continue;
1947 }
1948 break;
1949 }
1950
1951#ifdef FEAT_EVAL
1952 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
1953 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
1954#else
1955 ea.skip = (if_level > 0);
1956#endif
1957
1958#ifdef FEAT_EVAL
Bram Moolenaar05159a02005-02-26 23:04:13 +00001959# ifdef FEAT_PROFILE
1960 /* Count this line for profiling if ea.skip is FALSE. */
Bram Moolenaar371d5402006-03-20 21:47:49 +00001961 if (do_profiling == PROF_YES && !ea.skip)
Bram Moolenaar05159a02005-02-26 23:04:13 +00001962 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001963 if (getline_equal(fgetline, cookie, get_func_line))
1964 func_line_exec(getline_cookie(fgetline, cookie));
1965 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +00001966 script_line_exec();
1967 }
1968#endif
1969
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970 /* May go to debug mode. If this happens and the ">quit" debug command is
1971 * used, throw an interrupt exception and skip the next command. */
1972 dbg_check_breakpoint(&ea);
1973 if (!ea.skip && got_int)
1974 {
1975 ea.skip = TRUE;
1976 (void)do_intthrow(cstack);
1977 }
1978#endif
1979
1980/*
1981 * 3. parse a range specifier of the form: addr [,addr] [;addr] ..
1982 *
1983 * where 'addr' is:
1984 *
1985 * % (entire file)
1986 * $ [+-NUM]
1987 * 'x [+-NUM] (where x denotes a currently defined mark)
1988 * . [+-NUM]
1989 * [+-NUM]..
1990 * NUM
1991 *
1992 * The ea.cmd pointer is updated to point to the first character following the
1993 * range spec. If an initial address is found, but no second, the upper bound
1994 * is equal to the lower.
1995 */
1996
1997 /* repeat for all ',' or ';' separated addresses */
1998 for (;;)
1999 {
2000 ea.line1 = ea.line2;
2001 ea.line2 = curwin->w_cursor.lnum; /* default is current line number */
2002 ea.cmd = skipwhite(ea.cmd);
2003 lnum = get_address(&ea.cmd, ea.skip, ea.addr_count == 0);
2004 if (ea.cmd == NULL) /* error detected */
2005 goto doend;
2006 if (lnum == MAXLNUM)
2007 {
2008 if (*ea.cmd == '%') /* '%' - all lines */
2009 {
2010 ++ea.cmd;
2011 ea.line1 = 1;
2012 ea.line2 = curbuf->b_ml.ml_line_count;
2013 ++ea.addr_count;
2014 }
2015 /* '*' - visual area */
2016 else if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
2017 {
2018 pos_T *fp;
2019
2020 ++ea.cmd;
2021 if (!ea.skip)
2022 {
2023 fp = getmark('<', FALSE);
2024 if (check_mark(fp) == FAIL)
2025 goto doend;
2026 ea.line1 = fp->lnum;
2027 fp = getmark('>', FALSE);
2028 if (check_mark(fp) == FAIL)
2029 goto doend;
2030 ea.line2 = fp->lnum;
2031 ++ea.addr_count;
2032 }
2033 }
2034 }
2035 else
2036 ea.line2 = lnum;
2037 ea.addr_count++;
2038
2039 if (*ea.cmd == ';')
2040 {
2041 if (!ea.skip)
2042 curwin->w_cursor.lnum = ea.line2;
2043 }
2044 else if (*ea.cmd != ',')
2045 break;
2046 ++ea.cmd;
2047 }
2048
2049 /* One address given: set start and end lines */
2050 if (ea.addr_count == 1)
2051 {
2052 ea.line1 = ea.line2;
2053 /* ... but only implicit: really no address given */
2054 if (lnum == MAXLNUM)
2055 ea.addr_count = 0;
2056 }
2057
2058 /* Don't leave the cursor on an illegal line (caused by ';') */
2059 check_cursor_lnum();
2060
2061/*
2062 * 4. parse command
2063 */
2064
2065 /*
2066 * Skip ':' and any white space
2067 */
2068 ea.cmd = skipwhite(ea.cmd);
2069 while (*ea.cmd == ':')
2070 ea.cmd = skipwhite(ea.cmd + 1);
2071
2072 /*
2073 * If we got a line, but no command, then go to the line.
2074 * If we find a '|' or '\n' we set ea.nextcmd.
2075 */
2076 if (*ea.cmd == NUL || *ea.cmd == '"' ||
2077 (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
2078 {
2079 /*
2080 * strange vi behaviour:
2081 * ":3" jumps to line 3
2082 * ":3|..." prints line 3
2083 * ":|" prints current line
2084 */
2085 if (ea.skip) /* skip this if inside :if */
2086 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00002087 if (*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002088 {
2089 ea.cmdidx = CMD_print;
2090 ea.argt = RANGE+COUNT+TRLBAR;
2091 if ((errormsg = invalid_range(&ea)) == NULL)
2092 {
2093 correct_range(&ea);
2094 ex_print(&ea);
2095 }
2096 }
2097 else if (ea.addr_count != 0)
2098 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002099 if (ea.line2 > curbuf->b_ml.ml_line_count)
2100 {
2101 /* With '-' in 'cpoptions' a line number past the file is an
2102 * error, otherwise put it at the end of the file. */
2103 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
2104 ea.line2 = -1;
2105 else
2106 ea.line2 = curbuf->b_ml.ml_line_count;
2107 }
2108
2109 if (ea.line2 < 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002110 errormsg = (char_u *)_(e_invrange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002111 else
2112 {
2113 if (ea.line2 == 0)
2114 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002115 else
2116 curwin->w_cursor.lnum = ea.line2;
2117 beginline(BL_SOL | BL_FIX);
2118 }
2119 }
2120 goto doend;
2121 }
2122
2123 /* Find the command and let "p" point to after it. */
2124 p = find_command(&ea, NULL);
2125
2126#ifdef FEAT_USR_CMDS
2127 if (p == NULL)
2128 {
2129 if (!ea.skip)
2130 errormsg = (char_u *)_("E464: Ambiguous use of user-defined command");
2131 goto doend;
2132 }
2133 /* Check for wrong commands. */
2134 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78)
2135 {
2136 errormsg = uc_fun_cmd();
2137 goto doend;
2138 }
2139#endif
2140 if (ea.cmdidx == CMD_SIZE)
2141 {
2142 if (!ea.skip)
2143 {
2144 STRCPY(IObuff, _("E492: Not an editor command"));
2145 if (!sourcing)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002146 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002147 errormsg = IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02002148 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002149 }
2150 goto doend;
2151 }
2152
2153 ni = (
2154#ifdef FEAT_USR_CMDS
2155 !USER_CMDIDX(ea.cmdidx) &&
2156#endif
Bram Moolenaar3ebc1e52007-07-05 07:54:17 +00002157 (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00002158#ifdef HAVE_EX_SCRIPT_NI
2159 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
2160#endif
2161 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002162
2163#ifndef FEAT_EVAL
2164 /*
2165 * When the expression evaluation is disabled, recognize the ":if" and
2166 * ":endif" commands and ignore everything in between it.
2167 */
2168 if (ea.cmdidx == CMD_if)
2169 ++if_level;
2170 if (if_level)
2171 {
2172 if (ea.cmdidx == CMD_endif)
2173 --if_level;
2174 goto doend;
2175 }
2176
2177#endif
2178
Bram Moolenaar196b3b02008-06-20 16:51:41 +00002179 /* forced commands */
2180 if (*p == '!' && ea.cmdidx != CMD_substitute
2181 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002182 {
2183 ++p;
2184 ea.forceit = TRUE;
2185 }
2186 else
2187 ea.forceit = FALSE;
2188
2189/*
2190 * 5. parse arguments
2191 */
2192#ifdef FEAT_USR_CMDS
2193 if (!USER_CMDIDX(ea.cmdidx))
2194#endif
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002195 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002196
2197 if (!ea.skip)
2198 {
2199#ifdef HAVE_SANDBOX
2200 if (sandbox != 0 && !(ea.argt & SBOXOK))
2201 {
2202 /* Command not allowed in sandbox. */
2203 errormsg = (char_u *)_(e_sandbox);
2204 goto doend;
2205 }
2206#endif
2207 if (!curbuf->b_p_ma && (ea.argt & MODIFY))
2208 {
2209 /* Command not allowed in non-'modifiable' buffer */
2210 errormsg = (char_u *)_(e_modifiable);
2211 goto doend;
2212 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002213
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002214 if (text_locked() && !(ea.argt & CMDWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002215# ifdef FEAT_USR_CMDS
2216 && !USER_CMDIDX(ea.cmdidx)
2217# endif
2218 )
2219 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002220 /* Command not allowed when editing the command line. */
2221#ifdef FEAT_CMDWIN
2222 if (cmdwin_type != 0)
2223 errormsg = (char_u *)_(e_cmdwin);
2224 else
2225#endif
2226 errormsg = (char_u *)_(e_secure);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002227 goto doend;
2228 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002229#ifdef FEAT_AUTOCMD
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002230 /* Disallow editing another buffer when "curbuf_lock" is set.
2231 * Do allow ":edit" (check for argument later).
2232 * Do allow ":checktime" (it's postponed). */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002233 if (!(ea.argt & CMDWIN)
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002234 && ea.cmdidx != CMD_edit
2235 && ea.cmdidx != CMD_checktime
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002236# ifdef FEAT_USR_CMDS
2237 && !USER_CMDIDX(ea.cmdidx)
2238# endif
2239 && curbuf_locked())
2240 goto doend;
2241#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002242
2243 if (!ni && !(ea.argt & RANGE) && ea.addr_count > 0)
2244 {
2245 /* no range allowed */
2246 errormsg = (char_u *)_(e_norange);
2247 goto doend;
2248 }
2249 }
2250
2251 if (!ni && !(ea.argt & BANG) && ea.forceit) /* no <!> allowed */
2252 {
2253 errormsg = (char_u *)_(e_nobang);
2254 goto doend;
2255 }
2256
2257 /*
2258 * Don't complain about the range if it is not used
2259 * (could happen if line_count is accidentally set to 0).
2260 */
2261 if (!ea.skip && !ni)
2262 {
2263 /*
2264 * If the range is backwards, ask for confirmation and, if given, swap
2265 * ea.line1 & ea.line2 so it's forwards again.
2266 * When global command is busy, don't ask, will fail below.
2267 */
2268 if (!global_busy && ea.line1 > ea.line2)
2269 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002270 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002272 if (sourcing || exmode_active)
2273 {
2274 errormsg = (char_u *)_("E493: Backwards range given");
2275 goto doend;
2276 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277 if (ask_yesno((char_u *)
2278 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002279 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002280 }
2281 lnum = ea.line1;
2282 ea.line1 = ea.line2;
2283 ea.line2 = lnum;
2284 }
2285 if ((errormsg = invalid_range(&ea)) != NULL)
2286 goto doend;
2287 }
2288
2289 if ((ea.argt & NOTADR) && ea.addr_count == 0) /* default is 1, not cursor */
2290 ea.line2 = 1;
2291
2292 correct_range(&ea);
2293
2294#ifdef FEAT_FOLDING
2295 if (((ea.argt & WHOLEFOLD) || ea.addr_count >= 2) && !global_busy)
2296 {
2297 /* Put the first line at the start of a closed fold, put the last line
2298 * at the end of a closed fold. */
2299 (void)hasFolding(ea.line1, &ea.line1, NULL);
2300 (void)hasFolding(ea.line2, NULL, &ea.line2);
2301 }
2302#endif
2303
2304#ifdef FEAT_QUICKFIX
2305 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002306 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307 * option here, so things like % get expanded.
2308 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002309 p = replace_makeprg(&ea, p, cmdlinep);
2310 if (p == NULL)
2311 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312#endif
2313
2314 /*
2315 * Skip to start of argument.
2316 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2317 */
2318 if (ea.cmdidx == CMD_bang)
2319 ea.arg = p;
2320 else
2321 ea.arg = skipwhite(p);
2322
2323 /*
2324 * Check for "++opt=val" argument.
2325 * Must be first, allow ":w ++enc=utf8 !cmd"
2326 */
2327 if (ea.argt & ARGOPT)
2328 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2329 if (getargopt(&ea) == FAIL && !ni)
2330 {
2331 errormsg = (char_u *)_(e_invarg);
2332 goto doend;
2333 }
2334
2335 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2336 {
2337 if (*ea.arg == '>') /* append */
2338 {
2339 if (*++ea.arg != '>') /* typed wrong */
2340 {
2341 errormsg = (char_u *)_("E494: Use w or w>>");
2342 goto doend;
2343 }
2344 ea.arg = skipwhite(ea.arg + 1);
2345 ea.append = TRUE;
2346 }
2347 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
2348 {
2349 ++ea.arg;
2350 ea.usefilter = TRUE;
2351 }
2352 }
2353
2354 if (ea.cmdidx == CMD_read)
2355 {
2356 if (ea.forceit)
2357 {
2358 ea.usefilter = TRUE; /* :r! filter if ea.forceit */
2359 ea.forceit = FALSE;
2360 }
2361 else if (*ea.arg == '!') /* :r !filter */
2362 {
2363 ++ea.arg;
2364 ea.usefilter = TRUE;
2365 }
2366 }
2367
2368 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2369 {
2370 ea.amount = 1;
2371 while (*ea.arg == *ea.cmd) /* count number of '>' or '<' */
2372 {
2373 ++ea.arg;
2374 ++ea.amount;
2375 }
2376 ea.arg = skipwhite(ea.arg);
2377 }
2378
2379 /*
2380 * Check for "+command" argument, before checking for next command.
2381 * Don't do this for ":read !cmd" and ":write !cmd".
2382 */
2383 if ((ea.argt & EDITCMD) && !ea.usefilter)
2384 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2385
2386 /*
2387 * Check for '|' to separate commands and '"' to start comments.
2388 * Don't do this for ":read !cmd" and ":write !cmd".
2389 */
2390 if ((ea.argt & TRLBAR) && !ea.usefilter)
2391 separate_nextcmd(&ea);
2392
2393 /*
2394 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002395 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2396 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002398 else if (ea.cmdidx == CMD_bang
2399 || ea.cmdidx == CMD_global
2400 || ea.cmdidx == CMD_vglobal
2401 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402 {
2403 for (p = ea.arg; *p; ++p)
2404 {
2405 /* Remove one backslash before a newline, so that it's possible to
2406 * pass a newline to the shell and also a newline that is preceded
2407 * with a backslash. This makes it impossible to end a shell
2408 * command in a backslash, but that doesn't appear useful.
2409 * Halving the number of backslashes is incompatible with previous
2410 * versions. */
2411 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002412 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413 else if (*p == '\n')
2414 {
2415 ea.nextcmd = p + 1;
2416 *p = NUL;
2417 break;
2418 }
2419 }
2420 }
2421
2422 if ((ea.argt & DFLALL) && ea.addr_count == 0)
2423 {
2424 ea.line1 = 1;
2425 ea.line2 = curbuf->b_ml.ml_line_count;
2426 }
2427
2428 /* accept numbered register only when no count allowed (:put) */
2429 if ( (ea.argt & REGSTR)
2430 && *ea.arg != NUL
2431#ifdef FEAT_USR_CMDS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002432 /* Do not allow register = for user commands */
2433 && (!USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434#endif
2435 && !((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)))
2436 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002437#ifndef FEAT_CLIPBOARD
2438 /* check these explicitly for a more specific error message */
2439 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002440 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002441 errormsg = (char_u *)_(e_invalidreg);
2442 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443 }
2444#endif
Bram Moolenaar85de2062011-05-05 14:26:41 +02002445 if (
2446#ifdef FEAT_USR_CMDS
2447 valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2448 && USER_CMDIDX(ea.cmdidx)))
2449#else
2450 valid_yank_reg(*ea.arg, ea.cmdidx != CMD_put)
2451#endif
2452 )
2453 {
2454 ea.regname = *ea.arg++;
2455#ifdef FEAT_EVAL
2456 /* for '=' register: accept the rest of the line as an expression */
2457 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2458 {
2459 set_expr_line(vim_strsave(ea.arg));
2460 ea.arg += STRLEN(ea.arg);
2461 }
2462#endif
2463 ea.arg = skipwhite(ea.arg);
2464 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465 }
2466
2467 /*
2468 * Check for a count. When accepting a BUFNAME, don't use "123foo" as a
2469 * count, it's a buffer name.
2470 */
2471 if ((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)
2472 && (!(ea.argt & BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
2473 || vim_iswhite(*p)))
2474 {
2475 n = getdigits(&ea.arg);
2476 ea.arg = skipwhite(ea.arg);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002477 if (n <= 0 && !ni && (ea.argt & ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002478 {
2479 errormsg = (char_u *)_(e_zerocount);
2480 goto doend;
2481 }
2482 if (ea.argt & NOTADR) /* e.g. :buffer 2, :sleep 3 */
2483 {
2484 ea.line2 = n;
2485 if (ea.addr_count == 0)
2486 ea.addr_count = 1;
2487 }
2488 else
2489 {
2490 ea.line1 = ea.line2;
2491 ea.line2 += n - 1;
2492 ++ea.addr_count;
2493 /*
2494 * Be vi compatible: no error message for out of range.
2495 */
2496 if (ea.line2 > curbuf->b_ml.ml_line_count)
2497 ea.line2 = curbuf->b_ml.ml_line_count;
2498 }
2499 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002500
2501 /*
2502 * Check for flags: 'l', 'p' and '#'.
2503 */
2504 if (ea.argt & EXFLAGS)
2505 get_flags(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002506 /* no arguments allowed */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002507 if (!ni && !(ea.argt & EXTRA) && *ea.arg != NUL
Bram Moolenaara2031822006-03-07 22:29:51 +00002508 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509 {
2510 errormsg = (char_u *)_(e_trailing);
2511 goto doend;
2512 }
2513
2514 if (!ni && (ea.argt & NEEDARG) && *ea.arg == NUL)
2515 {
2516 errormsg = (char_u *)_(e_argreq);
2517 goto doend;
2518 }
2519
2520#ifdef FEAT_EVAL
2521 /*
2522 * Skip the command when it's not going to be executed.
2523 * The commands like :if, :endif, etc. always need to be executed.
2524 * Also make an exception for commands that handle a trailing command
2525 * themselves.
2526 */
2527 if (ea.skip)
2528 {
2529 switch (ea.cmdidx)
2530 {
2531 /* commands that need evaluation */
2532 case CMD_while:
2533 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002534 case CMD_for:
2535 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002536 case CMD_if:
2537 case CMD_elseif:
2538 case CMD_else:
2539 case CMD_endif:
2540 case CMD_try:
2541 case CMD_catch:
2542 case CMD_finally:
2543 case CMD_endtry:
2544 case CMD_function:
2545 break;
2546
2547 /* Commands that handle '|' themselves. Check: A command should
2548 * either have the TRLBAR flag, appear in this list or appear in
2549 * the list at ":help :bar". */
2550 case CMD_aboveleft:
2551 case CMD_and:
2552 case CMD_belowright:
2553 case CMD_botright:
2554 case CMD_browse:
2555 case CMD_call:
2556 case CMD_confirm:
2557 case CMD_delfunction:
2558 case CMD_djump:
2559 case CMD_dlist:
2560 case CMD_dsearch:
2561 case CMD_dsplit:
2562 case CMD_echo:
2563 case CMD_echoerr:
2564 case CMD_echomsg:
2565 case CMD_echon:
2566 case CMD_execute:
2567 case CMD_help:
2568 case CMD_hide:
2569 case CMD_ijump:
2570 case CMD_ilist:
2571 case CMD_isearch:
2572 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002573 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002574 case CMD_keepjumps:
2575 case CMD_keepmarks:
2576 case CMD_leftabove:
2577 case CMD_let:
2578 case CMD_lockmarks:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002579 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002581 case CMD_mzscheme:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582 case CMD_perl:
2583 case CMD_psearch:
2584 case CMD_python:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002585 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002586 case CMD_python3:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002587 case CMD_return:
2588 case CMD_rightbelow:
2589 case CMD_ruby:
2590 case CMD_silent:
2591 case CMD_smagic:
2592 case CMD_snomagic:
2593 case CMD_substitute:
2594 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002595 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002596 case CMD_tcl:
2597 case CMD_throw:
2598 case CMD_tilde:
2599 case CMD_topleft:
2600 case CMD_unlet:
2601 case CMD_verbose:
2602 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002603 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604 break;
2605
2606 default: goto doend;
2607 }
2608 }
2609#endif
2610
2611 if (ea.argt & XFILE)
2612 {
2613 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2614 goto doend;
2615 }
2616
2617#ifdef FEAT_LISTCMDS
2618 /*
2619 * Accept buffer name. Cannot be used at the same time with a buffer
2620 * number. Don't do this for a user command.
2621 */
2622 if ((ea.argt & BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
2623# ifdef FEAT_USR_CMDS
2624 && !USER_CMDIDX(ea.cmdidx)
2625# endif
2626 )
2627 {
2628 /*
2629 * :bdelete, :bwipeout and :bunload take several arguments, separated
2630 * by spaces: find next space (skipping over escaped characters).
2631 * The others take one argument: ignore trailing spaces.
2632 */
2633 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2634 || ea.cmdidx == CMD_bunload)
2635 p = skiptowhite_esc(ea.arg);
2636 else
2637 {
2638 p = ea.arg + STRLEN(ea.arg);
2639 while (p > ea.arg && vim_iswhite(p[-1]))
2640 --p;
2641 }
2642 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & BUFUNL) != 0, FALSE);
2643 if (ea.line2 < 0) /* failed */
2644 goto doend;
2645 ea.addr_count = 1;
2646 ea.arg = skipwhite(p);
2647 }
2648#endif
2649
2650/*
2651 * 6. switch on command name
2652 *
2653 * The "ea" structure holds the arguments that can be used.
2654 */
2655 ea.cmdlinep = cmdlinep;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002656 ea.getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657 ea.cookie = cookie;
2658#ifdef FEAT_EVAL
2659 ea.cstack = cstack;
2660#endif
2661
2662#ifdef FEAT_USR_CMDS
2663 if (USER_CMDIDX(ea.cmdidx))
2664 {
2665 /*
2666 * Execute a user-defined command.
2667 */
2668 do_ucmd(&ea);
2669 }
2670 else
2671#endif
2672 {
2673 /*
2674 * Call the function to execute the command.
2675 */
2676 ea.errmsg = NULL;
2677 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2678 if (ea.errmsg != NULL)
2679 errormsg = (char_u *)_(ea.errmsg);
2680 }
2681
2682#ifdef FEAT_EVAL
2683 /*
2684 * If the command just executed called do_cmdline(), any throw or ":return"
2685 * or ":finish" encountered there must also check the cstack of the still
2686 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2687 * exception, or reanimate a returned function or finished script file and
2688 * return or finish it again.
2689 */
2690 if (need_rethrow)
2691 do_throw(cstack);
2692 else if (check_cstack)
2693 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002694 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002696 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002697 && current_func_returned())
2698 do_return(&ea, TRUE, FALSE, NULL);
2699 }
2700 need_rethrow = check_cstack = FALSE;
2701#endif
2702
2703doend:
2704 if (curwin->w_cursor.lnum == 0) /* can happen with zero line number */
2705 curwin->w_cursor.lnum = 1;
2706
2707 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2708 {
2709 if (sourcing)
2710 {
2711 if (errormsg != IObuff)
2712 {
2713 STRCPY(IObuff, errormsg);
2714 errormsg = IObuff;
2715 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002716 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002717 }
2718 emsg(errormsg);
2719 }
2720#ifdef FEAT_EVAL
2721 do_errthrow(cstack,
2722 (ea.cmdidx != CMD_SIZE
2723# ifdef FEAT_USR_CMDS
2724 && !USER_CMDIDX(ea.cmdidx)
2725# endif
2726 ) ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
2727#endif
2728
2729 if (verbose_save >= 0)
2730 p_verbose = verbose_save;
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00002731#ifdef FEAT_AUTOCMD
2732 if (cmdmod.save_ei != NULL)
2733 {
2734 /* Restore 'eventignore' to the value before ":noautocmd". */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002735 set_string_option_direct((char_u *)"ei", -1, cmdmod.save_ei,
2736 OPT_FREE, SID_NONE);
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00002737 free_string_option(cmdmod.save_ei);
2738 }
2739#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002740
2741 cmdmod = save_cmdmod;
2742
Bram Moolenaar8e258a42009-07-09 13:55:43 +00002743 if (save_msg_silent != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002744 {
2745 /* messages could be enabled for a serious error, need to check if the
2746 * counters don't become negative */
Bram Moolenaarbecf4282009-09-30 11:24:36 +00002747 if (!did_emsg || msg_silent > save_msg_silent)
Bram Moolenaar8e258a42009-07-09 13:55:43 +00002748 msg_silent = save_msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002749 emsg_silent -= did_esilent;
2750 if (emsg_silent < 0)
2751 emsg_silent = 0;
2752 /* Restore msg_scroll, it's set by file I/O commands, even when no
2753 * message is actually displayed. */
2754 msg_scroll = save_msg_scroll;
Bram Moolenaar77ab2802009-04-22 12:44:48 +00002755
2756 /* "silent reg" or "silent echo x" inside "redir" leaves msg_col
2757 * somewhere in the line. Put it back in the first column. */
2758 if (redirecting())
2759 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760 }
2761
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002762#ifdef HAVE_SANDBOX
2763 if (did_sandbox)
2764 --sandbox;
2765#endif
2766
Bram Moolenaar071d4272004-06-13 20:20:40 +00002767 if (ea.nextcmd && *ea.nextcmd == NUL) /* not really a next command */
2768 ea.nextcmd = NULL;
2769
2770#ifdef FEAT_EVAL
2771 --ex_nesting_level;
2772#endif
2773
2774 return ea.nextcmd;
2775}
2776#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00002777 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002778#endif
2779
2780/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002781 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002782 * If there is a match advance "pp" to the argument and return TRUE.
2783 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002784 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00002785checkforcmd(pp, cmd, len)
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002786 char_u **pp; /* start of command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002787 char *cmd; /* name of command */
2788 int len; /* required length */
2789{
2790 int i;
2791
2792 for (i = 0; cmd[i] != NUL; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00002793 if (((char_u *)cmd)[i] != (*pp)[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00002794 break;
2795 if (i >= len && !isalpha((*pp)[i]))
2796 {
2797 *pp = skipwhite(*pp + i);
2798 return TRUE;
2799 }
2800 return FALSE;
2801}
2802
2803/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002804 * Append "cmd" to the error message in IObuff.
2805 * Takes care of limiting the length and handling 0xa0, which would be
2806 * invisible otherwise.
2807 */
2808 static void
2809append_command(cmd)
2810 char_u *cmd;
2811{
2812 char_u *s = cmd;
2813 char_u *d;
2814
2815 STRCAT(IObuff, ": ");
2816 d = IObuff + STRLEN(IObuff);
2817 while (*s != NUL && d - IObuff < IOSIZE - 7)
2818 {
2819 if (
2820#ifdef FEAT_MBYTE
2821 enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) :
2822#endif
2823 *s == 0xa0)
2824 {
2825 s +=
2826#ifdef FEAT_MBYTE
2827 enc_utf8 ? 2 :
2828#endif
2829 1;
2830 STRCPY(d, "<a0>");
2831 d += 4;
2832 }
2833 else
2834 MB_COPY_CHAR(s, d);
2835 }
2836 *d = NUL;
2837}
2838
2839/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002840 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002841 * Start of the name can be found at eap->cmd.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002842 * Returns pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002843 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002844 * Returns NULL for an ambiguous user command.
2845 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002846 static char_u *
2847find_command(eap, full)
2848 exarg_T *eap;
Bram Moolenaar78a15312009-05-15 19:33:18 +00002849 int *full UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002850{
2851 int len;
2852 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00002853 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002854
2855 /*
2856 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00002857 * Exceptions:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858 * - the 'k' command can directly be followed by any character.
2859 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
2860 * but :sre[wind] is another command, as are :scrip[tnames],
2861 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00002862 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002863 */
2864 p = eap->cmd;
2865 if (*p == 'k')
2866 {
2867 eap->cmdidx = CMD_k;
2868 ++p;
2869 }
2870 else if (p[0] == 's'
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002871 && ((p[1] == 'c' && p[2] != 's' && p[2] != 'r'
2872 && p[3] != 'i' && p[4] != 'p')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002873 || p[1] == 'g'
2874 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
2875 || p[1] == 'I'
2876 || (p[1] == 'r' && p[2] != 'e')))
2877 {
2878 eap->cmdidx = CMD_substitute;
2879 ++p;
2880 }
2881 else
2882 {
2883 while (ASCII_ISALPHA(*p))
2884 ++p;
Bram Moolenaarb6590522010-07-21 16:00:43 +02002885 /* for python 3.x support ":py3", ":python3", ":py3file", etc. */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002886 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaarb6590522010-07-21 16:00:43 +02002887 while (ASCII_ISALNUM(*p))
2888 ++p;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02002889
Bram Moolenaar071d4272004-06-13 20:20:40 +00002890 /* check for non-alpha command */
2891 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
2892 ++p;
2893 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00002894 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
2895 {
2896 /* Check for ":dl", ":dell", etc. to ":deletel": that's
2897 * :delete with the 'l' flag. Same for 'p'. */
2898 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00002899 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00002900 break;
2901 if (i == len - 1)
2902 {
2903 --len;
2904 if (p[-1] == 'l')
2905 eap->flags |= EXFLAG_LIST;
2906 else
2907 eap->flags |= EXFLAG_PRINT;
2908 }
2909 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002910
2911 if (ASCII_ISLOWER(*eap->cmd))
2912 eap->cmdidx = cmdidxs[CharOrdLow(*eap->cmd)];
2913 else
2914 eap->cmdidx = cmdidxs[26];
2915
2916 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
2917 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
2918 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
2919 (size_t)len) == 0)
2920 {
2921#ifdef FEAT_EVAL
2922 if (full != NULL
2923 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
2924 *full = TRUE;
2925#endif
2926 break;
2927 }
2928
2929#ifdef FEAT_USR_CMDS
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01002930 /* Look for a user defined command as a last resort. Let ":Print" be
2931 * overruled by a user defined command. */
2932 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
2933 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002934 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002935 /* User defined commands may contain digits. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002936 while (ASCII_ISALNUM(*p))
2937 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002938 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002939 }
2940#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002941 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002942 eap->cmdidx = CMD_SIZE;
2943 }
2944
2945 return p;
2946}
2947
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002948#ifdef FEAT_USR_CMDS
2949/*
2950 * Search for a user command that matches "eap->cmd".
2951 * Return cmdidx in "eap->cmdidx", flags in "eap->argt", idx in "eap->useridx".
2952 * Return a pointer to just after the command.
2953 * Return NULL if there is no matching command.
2954 */
2955 static char_u *
2956find_ucmd(eap, p, full, xp, compl)
2957 exarg_T *eap;
2958 char_u *p; /* end of the command (possibly including count) */
2959 int *full; /* set to TRUE for a full match */
2960 expand_T *xp; /* used for completion, NULL otherwise */
2961 int *compl; /* completion flags or NULL */
2962{
2963 int len = (int)(p - eap->cmd);
2964 int j, k, matchlen = 0;
2965 ucmd_T *uc;
2966 int found = FALSE;
2967 int possible = FALSE;
2968 char_u *cp, *np; /* Point into typed cmd and test name */
2969 garray_T *gap;
2970 int amb_local = FALSE; /* Found ambiguous buffer-local command,
2971 only full match global is accepted. */
2972
2973 /*
2974 * Look for buffer-local user commands first, then global ones.
2975 */
2976 gap = &curbuf->b_ucmds;
2977 for (;;)
2978 {
2979 for (j = 0; j < gap->ga_len; ++j)
2980 {
2981 uc = USER_CMD_GA(gap, j);
2982 cp = eap->cmd;
2983 np = uc->uc_name;
2984 k = 0;
2985 while (k < len && *np != NUL && *cp++ == *np++)
2986 k++;
2987 if (k == len || (*np == NUL && vim_isdigit(eap->cmd[k])))
2988 {
2989 /* If finding a second match, the command is ambiguous. But
2990 * not if a buffer-local command wasn't a full match and a
2991 * global command is a full match. */
2992 if (k == len && found && *np != NUL)
2993 {
2994 if (gap == &ucmds)
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002995 return NULL;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002996 amb_local = TRUE;
2997 }
2998
2999 if (!found || (k == len && *np == NUL))
3000 {
3001 /* If we matched up to a digit, then there could
3002 * be another command including the digit that we
3003 * should use instead.
3004 */
3005 if (k == len)
3006 found = TRUE;
3007 else
3008 possible = TRUE;
3009
3010 if (gap == &ucmds)
3011 eap->cmdidx = CMD_USER;
3012 else
3013 eap->cmdidx = CMD_USER_BUF;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003014 eap->argt = (long)uc->uc_argt;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003015 eap->useridx = j;
3016
3017# ifdef FEAT_CMDL_COMPL
3018 if (compl != NULL)
3019 *compl = uc->uc_compl;
3020# ifdef FEAT_EVAL
3021 if (xp != NULL)
3022 {
3023 xp->xp_arg = uc->uc_compl_arg;
3024 xp->xp_scriptID = uc->uc_scriptID;
3025 }
3026# endif
3027# endif
3028 /* Do not search for further abbreviations
3029 * if this is an exact match. */
3030 matchlen = k;
3031 if (k == len && *np == NUL)
3032 {
3033 if (full != NULL)
3034 *full = TRUE;
3035 amb_local = FALSE;
3036 break;
3037 }
3038 }
3039 }
3040 }
3041
3042 /* Stop if we found a full match or searched all. */
3043 if (j < gap->ga_len || gap == &ucmds)
3044 break;
3045 gap = &ucmds;
3046 }
3047
3048 /* Only found ambiguous matches. */
3049 if (amb_local)
3050 {
3051 if (xp != NULL)
3052 xp->xp_context = EXPAND_UNSUCCESSFUL;
3053 return NULL;
3054 }
3055
3056 /* The match we found may be followed immediately by a number. Move "p"
3057 * back to point to it. */
3058 if (found || possible)
3059 return p + (matchlen - len);
3060 return p;
3061}
3062#endif
3063
Bram Moolenaar071d4272004-06-13 20:20:40 +00003064#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003065static struct cmdmod
3066{
3067 char *name;
3068 int minlen;
3069 int has_count; /* :123verbose :3tab */
3070} cmdmods[] = {
3071 {"aboveleft", 3, FALSE},
3072 {"belowright", 3, FALSE},
3073 {"botright", 2, FALSE},
3074 {"browse", 3, FALSE},
3075 {"confirm", 4, FALSE},
3076 {"hide", 3, FALSE},
3077 {"keepalt", 5, FALSE},
3078 {"keepjumps", 5, FALSE},
3079 {"keepmarks", 3, FALSE},
3080 {"leftabove", 5, FALSE},
3081 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003082 {"noautocmd", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003083 {"rightbelow", 6, FALSE},
3084 {"sandbox", 3, FALSE},
3085 {"silent", 3, FALSE},
3086 {"tab", 3, TRUE},
3087 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003088 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003089 {"verbose", 4, TRUE},
3090 {"vertical", 4, FALSE},
3091};
3092
3093/*
3094 * Return length of a command modifier (including optional count).
3095 * Return zero when it's not a modifier.
3096 */
3097 int
3098modifier_len(cmd)
3099 char_u *cmd;
3100{
3101 int i, j;
3102 char_u *p = cmd;
3103
3104 if (VIM_ISDIGIT(*cmd))
3105 p = skipwhite(skipdigits(cmd));
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003106 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003107 {
3108 for (j = 0; p[j] != NUL; ++j)
3109 if (p[j] != cmdmods[i].name[j])
3110 break;
3111 if (!isalpha(p[j]) && j >= cmdmods[i].minlen
3112 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003113 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003114 }
3115 return 0;
3116}
3117
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118/*
3119 * Return > 0 if an Ex command "name" exists.
3120 * Return 2 if there is an exact match.
3121 * Return 3 if there is an ambiguous match.
3122 */
3123 int
3124cmd_exists(name)
3125 char_u *name;
3126{
3127 exarg_T ea;
3128 int full = FALSE;
3129 int i;
3130 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003131 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003132
3133 /* Check command modifiers. */
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003134 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003135 {
3136 for (j = 0; name[j] != NUL; ++j)
3137 if (name[j] != cmdmods[i].name[j])
3138 break;
3139 if (name[j] == NUL && j >= cmdmods[i].minlen)
3140 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3141 }
3142
Bram Moolenaara9587612006-05-04 21:47:50 +00003143 /* Check built-in commands and user defined commands.
3144 * For ":2match" and ":3match" we need to skip the number. */
3145 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003147 p = find_command(&ea, &full);
3148 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003149 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003150 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3151 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003152 if (*skipwhite(p) != NUL)
3153 return 0; /* trailing garbage */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3155}
3156#endif
3157
3158/*
3159 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
3160 * we don't need/want deleted. Maybe this could be done better if we didn't
3161 * repeat all this stuff. The only problem is that they may not stay
3162 * perfectly compatible with each other, but then the command line syntax
3163 * probably won't change that much -- webb.
3164 */
3165 char_u *
3166set_one_cmd_context(xp, buff)
3167 expand_T *xp;
3168 char_u *buff; /* buffer for command string */
3169{
3170 char_u *p;
3171 char_u *cmd, *arg;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003172 int len = 0;
3173 exarg_T ea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003174#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3175 int compl = EXPAND_NOTHING;
3176#endif
3177#ifdef FEAT_CMDL_COMPL
3178 int delim;
3179#endif
3180 int forceit = FALSE;
3181 int usefilter = FALSE; /* filter instead of file name */
3182
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003183 ExpandInit(xp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003184 xp->xp_pattern = buff;
3185 xp->xp_context = EXPAND_COMMANDS; /* Default until we get past command */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003186 ea.argt = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187
3188/*
3189 * 2. skip comment lines and leading space, colons or bars
3190 */
3191 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
3192 ;
3193 xp->xp_pattern = cmd;
3194
3195 if (*cmd == NUL)
3196 return NULL;
3197 if (*cmd == '"') /* ignore comment lines */
3198 {
3199 xp->xp_context = EXPAND_NOTHING;
3200 return NULL;
3201 }
3202
3203/*
3204 * 3. parse a range specifier of the form: addr [,addr] [;addr] ..
3205 */
3206 cmd = skip_range(cmd, &xp->xp_context);
3207
3208/*
3209 * 4. parse command
3210 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211 xp->xp_pattern = cmd;
3212 if (*cmd == NUL)
3213 return NULL;
3214 if (*cmd == '"')
3215 {
3216 xp->xp_context = EXPAND_NOTHING;
3217 return NULL;
3218 }
3219
3220 if (*cmd == '|' || *cmd == '\n')
3221 return cmd + 1; /* There's another command */
3222
3223 /*
3224 * Isolate the command and search for it in the command table.
3225 * Exceptions:
3226 * - the 'k' command can directly be followed by any character, but
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003227 * do accept "keepmarks", "keepalt" and "keepjumps".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003228 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
3229 */
3230 if (*cmd == 'k' && cmd[1] != 'e')
3231 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003232 ea.cmdidx = CMD_k;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003233 p = cmd + 1;
3234 }
3235 else
3236 {
3237 p = cmd;
3238 while (ASCII_ISALPHA(*p) || *p == '*') /* Allow * wild card */
3239 ++p;
3240 /* check for non-alpha command */
3241 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3242 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003243 len = (int)(p - cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003244
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003245 if (len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246 {
3247 xp->xp_context = EXPAND_UNSUCCESSFUL;
3248 return NULL;
3249 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003250 for (ea.cmdidx = (cmdidx_T)0; (int)ea.cmdidx < (int)CMD_SIZE;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01003251 ea.cmdidx = (cmdidx_T)((int)ea.cmdidx + 1))
3252 if (STRNCMP(cmdnames[(int)ea.cmdidx].cmd_name, cmd,
3253 (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003254 break;
3255
3256#ifdef FEAT_USR_CMDS
3257 if (cmd[0] >= 'A' && cmd[0] <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003258 while (ASCII_ISALNUM(*p) || *p == '*') /* Allow * wild card */
3259 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003260#endif
3261 }
3262
3263 /*
3264 * If the cursor is touching the command, and it ends in an alpha-numeric
3265 * character, complete the command name.
3266 */
3267 if (*p == NUL && ASCII_ISALNUM(p[-1]))
3268 return NULL;
3269
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003270 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003271 {
3272 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
3273 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003274 ea.cmdidx = CMD_substitute;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275 p = cmd + 1;
3276 }
3277#ifdef FEAT_USR_CMDS
3278 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
3279 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003280 ea.cmd = cmd;
3281 p = find_ucmd(&ea, p, NULL, xp,
3282# if defined(FEAT_CMDL_COMPL)
3283 &compl
3284# else
3285 NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003286# endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003287 );
Bram Moolenaarebefac62005-12-28 22:39:57 +00003288 if (p == NULL)
3289 ea.cmdidx = CMD_SIZE; /* ambiguous user command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290 }
3291#endif
3292 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003293 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003294 {
3295 /* Not still touching the command and it was an illegal one */
3296 xp->xp_context = EXPAND_UNSUCCESSFUL;
3297 return NULL;
3298 }
3299
3300 xp->xp_context = EXPAND_NOTHING; /* Default now that we're past command */
3301
3302 if (*p == '!') /* forced commands */
3303 {
3304 forceit = TRUE;
3305 ++p;
3306 }
3307
3308/*
3309 * 5. parse arguments
3310 */
3311#ifdef FEAT_USR_CMDS
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003312 if (!USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313#endif
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003314 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315
3316 arg = skipwhite(p);
3317
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003318 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003319 {
3320 if (*arg == '>') /* append */
3321 {
3322 if (*++arg == '>')
3323 ++arg;
3324 arg = skipwhite(arg);
3325 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003326 else if (*arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327 {
3328 ++arg;
3329 usefilter = TRUE;
3330 }
3331 }
3332
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003333 if (ea.cmdidx == CMD_read)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334 {
3335 usefilter = forceit; /* :r! filter if forced */
3336 if (*arg == '!') /* :r !filter */
3337 {
3338 ++arg;
3339 usefilter = TRUE;
3340 }
3341 }
3342
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003343 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344 {
3345 while (*arg == *cmd) /* allow any number of '>' or '<' */
3346 ++arg;
3347 arg = skipwhite(arg);
3348 }
3349
3350 /* Does command allow "+command"? */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003351 if ((ea.argt & EDITCMD) && !usefilter && *arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352 {
3353 /* Check if we're in the +command */
3354 p = arg + 1;
3355 arg = skip_cmd_arg(arg, FALSE);
3356
3357 /* Still touching the command after '+'? */
3358 if (*arg == NUL)
3359 return p;
3360
3361 /* Skip space(s) after +command to get to the real argument */
3362 arg = skipwhite(arg);
3363 }
3364
3365 /*
3366 * Check for '|' to separate commands and '"' to start comments.
3367 * Don't do this for ":read !cmd" and ":write !cmd".
3368 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003369 if ((ea.argt & TRLBAR) && !usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370 {
3371 p = arg;
3372 /* ":redir @" is not the start of a comment */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003373 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374 p += 2;
3375 while (*p)
3376 {
3377 if (*p == Ctrl_V)
3378 {
3379 if (p[1] != NUL)
3380 ++p;
3381 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003382 else if ( (*p == '"' && !(ea.argt & NOTRLCOM))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383 || *p == '|' || *p == '\n')
3384 {
3385 if (*(p - 1) != '\\')
3386 {
3387 if (*p == '|' || *p == '\n')
3388 return p + 1;
3389 return NULL; /* It's a comment */
3390 }
3391 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003392 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393 }
3394 }
3395
3396 /* no arguments allowed */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003397 if (!(ea.argt & EXTRA) && *arg != NUL &&
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398 vim_strchr((char_u *)"|\"", *arg) == NULL)
3399 return NULL;
3400
3401 /* Find start of last argument (argument just before cursor): */
Bram Moolenaar848f8762012-07-25 17:22:23 +02003402 p = buff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003403 xp->xp_pattern = p;
Bram Moolenaar09168a72012-08-02 21:24:42 +02003404 len = (int)STRLEN(buff);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003405 while (*p && p < buff + len)
3406 {
3407 if (*p == ' ' || *p == TAB)
3408 {
3409 /* argument starts after a space */
3410 xp->xp_pattern = ++p;
3411 }
3412 else
3413 {
3414 if (*p == '\\' && *(p + 1) != NUL)
3415 ++p; /* skip over escaped character */
3416 mb_ptr_adv(p);
3417 }
3418 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003420 if (ea.argt & XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003422 int c;
3423 int in_quote = FALSE;
3424 char_u *bow = NULL; /* Beginning of word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003425
3426 /*
3427 * Allow spaces within back-quotes to count as part of the argument
3428 * being expanded.
3429 */
3430 xp->xp_pattern = skipwhite(arg);
Bram Moolenaar6529c102007-08-18 15:47:34 +00003431 p = xp->xp_pattern;
3432 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003434#ifdef FEAT_MBYTE
3435 if (has_mbyte)
3436 c = mb_ptr2char(p);
3437 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438#endif
Bram Moolenaar6529c102007-08-18 15:47:34 +00003439 c = *p;
3440 if (c == '\\' && p[1] != NUL)
3441 ++p;
3442 else if (c == '`')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443 {
3444 if (!in_quote)
3445 {
3446 xp->xp_pattern = p;
3447 bow = p + 1;
3448 }
3449 in_quote = !in_quote;
3450 }
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003451 /* An argument can contain just about everything, except
3452 * characters that end the command and white space. */
3453 else if (c == '|' || c == '\n' || c == '"' || (vim_iswhite(c)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003454#ifdef SPACE_IN_FILENAME
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003455 && (!(ea.argt & NOSPC) || usefilter)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003456#endif
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003457 ))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003458 {
Bram Moolenaarcf5a5b82008-02-26 20:30:12 +00003459 len = 0; /* avoid getting stuck when space is in 'isfname' */
Bram Moolenaar6529c102007-08-18 15:47:34 +00003460 while (*p != NUL)
3461 {
3462#ifdef FEAT_MBYTE
3463 if (has_mbyte)
3464 c = mb_ptr2char(p);
3465 else
3466#endif
3467 c = *p;
Bram Moolenaardd87969c2007-08-21 13:07:12 +00003468 if (c == '`' || vim_isfilec_or_wc(c))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003469 break;
3470#ifdef FEAT_MBYTE
3471 if (has_mbyte)
3472 len = (*mb_ptr2len)(p);
3473 else
3474#endif
3475 len = 1;
3476 mb_ptr_adv(p);
3477 }
3478 if (in_quote)
3479 bow = p;
3480 else
3481 xp->xp_pattern = p;
3482 p -= len;
3483 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003484 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485 }
3486
3487 /*
3488 * If we are still inside the quotes, and we passed a space, just
3489 * expand from there.
3490 */
3491 if (bow != NULL && in_quote)
3492 xp->xp_pattern = bow;
3493 xp->xp_context = EXPAND_FILES;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003494
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003495 /* For a shell command more chars need to be escaped. */
3496 if (usefilter || ea.cmdidx == CMD_bang)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003497 {
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003498#ifndef BACKSLASH_IN_FILENAME
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003499 xp->xp_shell = TRUE;
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003500#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003501 /* When still after the command name expand executables. */
3502 if (xp->xp_pattern == skipwhite(arg))
Bram Moolenaar5c5b0942007-05-06 12:07:59 +00003503 xp->xp_context = EXPAND_SHELLCMD;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003504 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505
3506 /* Check for environment variable */
3507 if (*xp->xp_pattern == '$'
3508#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
3509 || *xp->xp_pattern == '%'
3510#endif
3511 )
3512 {
3513 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
3514 if (!vim_isIDc(*p))
3515 break;
3516 if (*p == NUL)
3517 {
3518 xp->xp_context = EXPAND_ENV_VARS;
3519 ++xp->xp_pattern;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003520#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3521 /* Avoid that the assignment uses EXPAND_FILES again. */
Bram Moolenaara466c992005-07-09 21:03:22 +00003522 if (compl != EXPAND_USER_DEFINED && compl != EXPAND_USER_LIST)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003523 compl = EXPAND_ENV_VARS;
3524#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525 }
3526 }
Bram Moolenaar24305862012-08-15 14:05:05 +02003527#if defined(FEAT_CMDL_COMPL)
3528 /* Check for user names */
3529 if (*xp->xp_pattern == '~')
3530 {
3531 for (p = xp->xp_pattern + 1; *p != NUL && *p != '/'; ++p)
3532 ;
3533 /* Complete ~user only if it partially matches a user name.
3534 * A full match ~user<Tab> will be replaced by user's home
3535 * directory i.e. something like ~user<Tab> -> /home/user/ */
3536 if (*p == NUL && p > xp->xp_pattern + 1
3537 && match_user(xp->xp_pattern + 1) == 1)
3538 {
3539 xp->xp_context = EXPAND_USER;
3540 ++xp->xp_pattern;
3541 }
3542 }
3543#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544 }
3545
3546/*
3547 * 6. switch on command name
3548 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003549 switch (ea.cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003551 case CMD_find:
3552 case CMD_sfind:
3553 case CMD_tabfind:
Bram Moolenaarc24b6972010-08-16 22:34:29 +02003554 if (xp->xp_context == EXPAND_FILES)
3555 xp->xp_context = EXPAND_FILES_IN_PATH;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003556 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557 case CMD_cd:
3558 case CMD_chdir:
3559 case CMD_lcd:
3560 case CMD_lchdir:
3561 if (xp->xp_context == EXPAND_FILES)
3562 xp->xp_context = EXPAND_DIRECTORIES;
3563 break;
3564 case CMD_help:
3565 xp->xp_context = EXPAND_HELP;
3566 xp->xp_pattern = arg;
3567 break;
3568
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003569 /* Command modifiers: return the argument.
3570 * Also for commands with an argument that is a command. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571 case CMD_aboveleft:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003572 case CMD_argdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003573 case CMD_belowright:
3574 case CMD_botright:
3575 case CMD_browse:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003576 case CMD_bufdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003577 case CMD_confirm:
Bram Moolenaardf177f62005-02-22 08:39:57 +00003578 case CMD_debug:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579 case CMD_folddoclosed:
3580 case CMD_folddoopen:
3581 case CMD_hide:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003582 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583 case CMD_keepjumps:
3584 case CMD_keepmarks:
3585 case CMD_leftabove:
3586 case CMD_lockmarks:
3587 case CMD_rightbelow:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003588 case CMD_sandbox:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589 case CMD_silent:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003590 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591 case CMD_topleft:
3592 case CMD_verbose:
3593 case CMD_vertical:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003594 case CMD_windo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003595 return arg;
3596
Bram Moolenaar4f688582007-07-24 12:34:30 +00003597#ifdef FEAT_CMDL_COMPL
3598# ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599 case CMD_match:
3600 if (*arg == NUL || !ends_excmd(*arg))
3601 {
Bram Moolenaar4f688582007-07-24 12:34:30 +00003602 /* also complete "None" */
3603 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604 arg = skipwhite(skiptowhite(arg));
3605 if (*arg != NUL)
3606 {
3607 xp->xp_context = EXPAND_NOTHING;
3608 arg = skip_regexp(arg + 1, *arg, p_magic, NULL);
3609 }
3610 }
3611 return find_nextcmd(arg);
Bram Moolenaar4f688582007-07-24 12:34:30 +00003612# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614/*
3615 * All completion for the +cmdline_compl feature goes here.
3616 */
3617
3618# ifdef FEAT_USR_CMDS
3619 case CMD_command:
3620 /* Check for attributes */
3621 while (*arg == '-')
3622 {
3623 arg++; /* Skip "-" */
3624 p = skiptowhite(arg);
3625 if (*p == NUL)
3626 {
3627 /* Cursor is still in the attribute */
3628 p = vim_strchr(arg, '=');
3629 if (p == NULL)
3630 {
3631 /* No "=", so complete attribute names */
3632 xp->xp_context = EXPAND_USER_CMD_FLAGS;
3633 xp->xp_pattern = arg;
3634 return NULL;
3635 }
3636
3637 /* For the -complete and -nargs attributes, we complete
3638 * their arguments as well.
3639 */
3640 if (STRNICMP(arg, "complete", p - arg) == 0)
3641 {
3642 xp->xp_context = EXPAND_USER_COMPLETE;
3643 xp->xp_pattern = p + 1;
3644 return NULL;
3645 }
3646 else if (STRNICMP(arg, "nargs", p - arg) == 0)
3647 {
3648 xp->xp_context = EXPAND_USER_NARGS;
3649 xp->xp_pattern = p + 1;
3650 return NULL;
3651 }
3652 return NULL;
3653 }
3654 arg = skipwhite(p);
3655 }
3656
3657 /* After the attributes comes the new command name */
3658 p = skiptowhite(arg);
3659 if (*p == NUL)
3660 {
3661 xp->xp_context = EXPAND_USER_COMMANDS;
3662 xp->xp_pattern = arg;
3663 break;
3664 }
3665
3666 /* And finally comes a normal command */
3667 return skipwhite(p);
3668
3669 case CMD_delcommand:
3670 xp->xp_context = EXPAND_USER_COMMANDS;
3671 xp->xp_pattern = arg;
3672 break;
3673# endif
3674
3675 case CMD_global:
3676 case CMD_vglobal:
3677 delim = *arg; /* get the delimiter */
3678 if (delim)
3679 ++arg; /* skip delimiter if there is one */
3680
3681 while (arg[0] != NUL && arg[0] != delim)
3682 {
3683 if (arg[0] == '\\' && arg[1] != NUL)
3684 ++arg;
3685 ++arg;
3686 }
3687 if (arg[0] != NUL)
3688 return arg + 1;
3689 break;
3690 case CMD_and:
3691 case CMD_substitute:
3692 delim = *arg;
3693 if (delim)
3694 {
3695 /* skip "from" part */
3696 ++arg;
3697 arg = skip_regexp(arg, delim, p_magic, NULL);
3698 }
3699 /* skip "to" part */
3700 while (arg[0] != NUL && arg[0] != delim)
3701 {
3702 if (arg[0] == '\\' && arg[1] != NUL)
3703 ++arg;
3704 ++arg;
3705 }
3706 if (arg[0] != NUL) /* skip delimiter */
3707 ++arg;
3708 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
3709 ++arg;
3710 if (arg[0] != NUL)
3711 return arg;
3712 break;
3713 case CMD_isearch:
3714 case CMD_dsearch:
3715 case CMD_ilist:
3716 case CMD_dlist:
3717 case CMD_ijump:
3718 case CMD_psearch:
3719 case CMD_djump:
3720 case CMD_isplit:
3721 case CMD_dsplit:
3722 arg = skipwhite(skipdigits(arg)); /* skip count */
3723 if (*arg == '/') /* Match regexp, not just whole words */
3724 {
3725 for (++arg; *arg && *arg != '/'; arg++)
3726 if (*arg == '\\' && arg[1] != NUL)
3727 arg++;
3728 if (*arg)
3729 {
3730 arg = skipwhite(arg + 1);
3731
3732 /* Check for trailing illegal characters */
3733 if (*arg && vim_strchr((char_u *)"|\"\n", *arg) == NULL)
3734 xp->xp_context = EXPAND_NOTHING;
3735 else
3736 return arg;
3737 }
3738 }
3739 break;
3740#ifdef FEAT_AUTOCMD
3741 case CMD_autocmd:
3742 return set_context_in_autocmd(xp, arg, FALSE);
3743
3744 case CMD_doautocmd:
Bram Moolenaar73a9d7b2008-11-06 16:16:44 +00003745 case CMD_doautoall:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746 return set_context_in_autocmd(xp, arg, TRUE);
3747#endif
3748 case CMD_set:
3749 set_context_in_set_cmd(xp, arg, 0);
3750 break;
3751 case CMD_setglobal:
3752 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
3753 break;
3754 case CMD_setlocal:
3755 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
3756 break;
3757 case CMD_tag:
3758 case CMD_stag:
3759 case CMD_ptag:
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00003760 case CMD_ltag:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761 case CMD_tselect:
3762 case CMD_stselect:
3763 case CMD_ptselect:
3764 case CMD_tjump:
3765 case CMD_stjump:
3766 case CMD_ptjump:
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003767 if (*p_wop != NUL)
3768 xp->xp_context = EXPAND_TAGS_LISTFILES;
3769 else
3770 xp->xp_context = EXPAND_TAGS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003771 xp->xp_pattern = arg;
3772 break;
3773 case CMD_augroup:
3774 xp->xp_context = EXPAND_AUGROUP;
3775 xp->xp_pattern = arg;
3776 break;
3777#ifdef FEAT_SYN_HL
3778 case CMD_syntax:
3779 set_context_in_syntax_cmd(xp, arg);
3780 break;
3781#endif
3782#ifdef FEAT_EVAL
3783 case CMD_let:
3784 case CMD_if:
3785 case CMD_elseif:
3786 case CMD_while:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00003787 case CMD_for:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003788 case CMD_echo:
3789 case CMD_echon:
3790 case CMD_execute:
3791 case CMD_echomsg:
3792 case CMD_echoerr:
3793 case CMD_call:
3794 case CMD_return:
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003795 set_context_for_expression(xp, arg, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796 break;
3797
3798 case CMD_unlet:
3799 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3800 arg = xp->xp_pattern + 1;
3801 xp->xp_context = EXPAND_USER_VARS;
3802 xp->xp_pattern = arg;
3803 break;
3804
3805 case CMD_function:
3806 case CMD_delfunction:
3807 xp->xp_context = EXPAND_USER_FUNC;
3808 xp->xp_pattern = arg;
3809 break;
3810
3811 case CMD_echohl:
Bram Moolenaar4f688582007-07-24 12:34:30 +00003812 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003813 break;
3814#endif
3815 case CMD_highlight:
3816 set_context_in_highlight_cmd(xp, arg);
3817 break;
Bram Moolenaarf4580d82009-03-18 11:52:53 +00003818#ifdef FEAT_CSCOPE
3819 case CMD_cscope:
Bram Moolenaar7bfef802009-04-22 14:25:01 +00003820 case CMD_lcscope:
3821 case CMD_scscope:
3822 set_context_in_cscope_cmd(xp, arg, ea.cmdidx);
Bram Moolenaarf4580d82009-03-18 11:52:53 +00003823 break;
3824#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00003825#ifdef FEAT_SIGNS
3826 case CMD_sign:
3827 set_context_in_sign_cmd(xp, arg);
3828 break;
3829#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830#ifdef FEAT_LISTCMDS
3831 case CMD_bdelete:
3832 case CMD_bwipeout:
3833 case CMD_bunload:
3834 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3835 arg = xp->xp_pattern + 1;
3836 /*FALLTHROUGH*/
3837 case CMD_buffer:
3838 case CMD_sbuffer:
3839 case CMD_checktime:
3840 xp->xp_context = EXPAND_BUFFERS;
3841 xp->xp_pattern = arg;
3842 break;
3843#endif
3844#ifdef FEAT_USR_CMDS
3845 case CMD_USER:
3846 case CMD_USER_BUF:
3847 if (compl != EXPAND_NOTHING)
3848 {
3849 /* XFILE: file names are handled above */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003850 if (!(ea.argt & XFILE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851 {
3852# ifdef FEAT_MENU
3853 if (compl == EXPAND_MENUS)
3854 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
3855# endif
3856 if (compl == EXPAND_COMMANDS)
3857 return arg;
3858 if (compl == EXPAND_MAPPINGS)
3859 return set_context_in_map_cmd(xp, (char_u *)"map",
3860 arg, forceit, FALSE, FALSE, CMD_map);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003861 /* Find start of last argument. */
3862 p = arg;
3863 while (*p)
3864 {
3865 if (*p == ' ')
Bram Moolenaar848f8762012-07-25 17:22:23 +02003866 /* argument starts after a space */
3867 arg = p + 1;
Bram Moolenaara07c8312012-07-27 21:12:07 +02003868 else if (*p == '\\' && *(p + 1) != NUL)
3869 ++p; /* skip over escaped character */
3870 mb_ptr_adv(p);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003871 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003872 xp->xp_pattern = arg;
3873 }
3874 xp->xp_context = compl;
3875 }
3876 break;
3877#endif
3878 case CMD_map: case CMD_noremap:
3879 case CMD_nmap: case CMD_nnoremap:
3880 case CMD_vmap: case CMD_vnoremap:
3881 case CMD_omap: case CMD_onoremap:
3882 case CMD_imap: case CMD_inoremap:
3883 case CMD_cmap: case CMD_cnoremap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003884 case CMD_lmap: case CMD_lnoremap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003885 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003886 FALSE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887 case CMD_unmap:
3888 case CMD_nunmap:
3889 case CMD_vunmap:
3890 case CMD_ounmap:
3891 case CMD_iunmap:
3892 case CMD_cunmap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003893 case CMD_lunmap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003895 FALSE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896 case CMD_abbreviate: case CMD_noreabbrev:
3897 case CMD_cabbrev: case CMD_cnoreabbrev:
3898 case CMD_iabbrev: case CMD_inoreabbrev:
3899 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003900 TRUE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003901 case CMD_unabbreviate:
3902 case CMD_cunabbrev:
3903 case CMD_iunabbrev:
3904 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003905 TRUE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003906#ifdef FEAT_MENU
3907 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
3908 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
3909 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
3910 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
3911 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
3912 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
3913 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
3914 case CMD_tmenu: case CMD_tunmenu:
3915 case CMD_popup: case CMD_tearoff: case CMD_emenu:
3916 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
3917#endif
3918
3919 case CMD_colorscheme:
3920 xp->xp_context = EXPAND_COLORS;
3921 xp->xp_pattern = arg;
3922 break;
3923
3924 case CMD_compiler:
3925 xp->xp_context = EXPAND_COMPILER;
3926 xp->xp_pattern = arg;
3927 break;
3928
Bram Moolenaar883f5d02010-06-21 06:24:34 +02003929 case CMD_ownsyntax:
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02003930 xp->xp_context = EXPAND_OWNSYNTAX;
3931 xp->xp_pattern = arg;
3932 break;
3933
3934 case CMD_setfiletype:
Bram Moolenaar883f5d02010-06-21 06:24:34 +02003935 xp->xp_context = EXPAND_FILETYPE;
3936 xp->xp_pattern = arg;
3937 break;
3938
Bram Moolenaar071d4272004-06-13 20:20:40 +00003939#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
3940 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
3941 case CMD_language:
Bram Moolenaara660dc82011-05-25 12:51:22 +02003942 p = skiptowhite(arg);
3943 if (*p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003944 {
3945 xp->xp_context = EXPAND_LANGUAGE;
3946 xp->xp_pattern = arg;
3947 }
3948 else
Bram Moolenaara660dc82011-05-25 12:51:22 +02003949 {
3950 if ( STRNCMP(arg, "messages", p - arg) == 0
3951 || STRNCMP(arg, "ctype", p - arg) == 0
3952 || STRNCMP(arg, "time", p - arg) == 0)
3953 {
3954 xp->xp_context = EXPAND_LOCALES;
3955 xp->xp_pattern = skipwhite(p);
3956 }
3957 else
3958 xp->xp_context = EXPAND_NOTHING;
3959 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003960 break;
3961#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01003962#if defined(FEAT_PROFILE)
3963 case CMD_profile:
3964 set_context_in_profile_cmd(xp, arg);
3965 break;
3966#endif
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01003967 case CMD_behave:
3968 xp->xp_context = EXPAND_BEHAVE;
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02003969 xp->xp_pattern = arg;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01003970 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003971
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02003972#if defined(FEAT_CMDHIST)
3973 case CMD_history:
3974 xp->xp_context = EXPAND_HISTORY;
3975 xp->xp_pattern = arg;
3976 break;
3977#endif
3978
Bram Moolenaar071d4272004-06-13 20:20:40 +00003979#endif /* FEAT_CMDL_COMPL */
3980
3981 default:
3982 break;
3983 }
3984 return NULL;
3985}
3986
3987/*
3988 * skip a range specifier of the form: addr [,addr] [;addr] ..
3989 *
3990 * Backslashed delimiters after / or ? will be skipped, and commands will
3991 * not be expanded between /'s and ?'s or after "'".
3992 *
Bram Moolenaardf177f62005-02-22 08:39:57 +00003993 * Also skip white space and ":" characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994 * Returns the "cmd" pointer advanced to beyond the range.
3995 */
3996 char_u *
3997skip_range(cmd, ctx)
3998 char_u *cmd;
3999 int *ctx; /* pointer to xp_context or NULL */
4000{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004001 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002
Bram Moolenaardf177f62005-02-22 08:39:57 +00004003 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004 {
4005 if (*cmd == '\'')
4006 {
4007 if (*++cmd == NUL && ctx != NULL)
4008 *ctx = EXPAND_NOTHING;
4009 }
4010 else if (*cmd == '/' || *cmd == '?')
4011 {
4012 delim = *cmd++;
4013 while (*cmd != NUL && *cmd != delim)
4014 if (*cmd++ == '\\' && *cmd != NUL)
4015 ++cmd;
4016 if (*cmd == NUL && ctx != NULL)
4017 *ctx = EXPAND_NOTHING;
4018 }
4019 if (*cmd != NUL)
4020 ++cmd;
4021 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004022
4023 /* Skip ":" and white space. */
4024 while (*cmd == ':')
4025 cmd = skipwhite(cmd + 1);
4026
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027 return cmd;
4028}
4029
4030/*
4031 * get a single EX address
4032 *
4033 * Set ptr to the next character after the part that was interpreted.
4034 * Set ptr to NULL when an error is encountered.
4035 *
4036 * Return MAXLNUM when no Ex address was found.
4037 */
4038 static linenr_T
4039get_address(ptr, skip, to_other_file)
4040 char_u **ptr;
4041 int skip; /* only skip the address, don't use it */
4042 int to_other_file; /* flag: may jump to other file */
4043{
4044 int c;
4045 int i;
4046 long n;
4047 char_u *cmd;
4048 pos_T pos;
4049 pos_T *fp;
4050 linenr_T lnum;
4051
4052 cmd = skipwhite(*ptr);
4053 lnum = MAXLNUM;
4054 do
4055 {
4056 switch (*cmd)
4057 {
4058 case '.': /* '.' - Cursor position */
4059 ++cmd;
4060 lnum = curwin->w_cursor.lnum;
4061 break;
4062
4063 case '$': /* '$' - last line */
4064 ++cmd;
4065 lnum = curbuf->b_ml.ml_line_count;
4066 break;
4067
4068 case '\'': /* ''' - mark */
4069 if (*++cmd == NUL)
4070 {
4071 cmd = NULL;
4072 goto error;
4073 }
4074 if (skip)
4075 ++cmd;
4076 else
4077 {
4078 /* Only accept a mark in another file when it is
4079 * used by itself: ":'M". */
4080 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
4081 ++cmd;
4082 if (fp == (pos_T *)-1)
4083 /* Jumped to another file. */
4084 lnum = curwin->w_cursor.lnum;
4085 else
4086 {
4087 if (check_mark(fp) == FAIL)
4088 {
4089 cmd = NULL;
4090 goto error;
4091 }
4092 lnum = fp->lnum;
4093 }
4094 }
4095 break;
4096
4097 case '/':
4098 case '?': /* '/' or '?' - search */
4099 c = *cmd++;
4100 if (skip) /* skip "/pat/" */
4101 {
4102 cmd = skip_regexp(cmd, c, (int)p_magic, NULL);
4103 if (*cmd == c)
4104 ++cmd;
4105 }
4106 else
4107 {
4108 pos = curwin->w_cursor; /* save curwin->w_cursor */
4109 /*
4110 * When '/' or '?' follows another address, start
4111 * from there.
4112 */
4113 if (lnum != MAXLNUM)
4114 curwin->w_cursor.lnum = lnum;
4115 /*
4116 * Start a forward search at the end of the line.
4117 * Start a backward search at the start of the line.
4118 * This makes sure we never match in the current
4119 * line, and can match anywhere in the
4120 * next/previous line.
4121 */
4122 if (c == '/')
4123 curwin->w_cursor.col = MAXCOL;
4124 else
4125 curwin->w_cursor.col = 0;
4126 searchcmdlen = 0;
4127 if (!do_search(NULL, c, cmd, 1L,
Bram Moolenaaraad86642008-03-10 20:34:59 +00004128 SEARCH_HIS | SEARCH_MSG, NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129 {
4130 curwin->w_cursor = pos;
4131 cmd = NULL;
4132 goto error;
4133 }
4134 lnum = curwin->w_cursor.lnum;
4135 curwin->w_cursor = pos;
4136 /* adjust command string pointer */
4137 cmd += searchcmdlen;
4138 }
4139 break;
4140
4141 case '\\': /* "\?", "\/" or "\&", repeat search */
4142 ++cmd;
4143 if (*cmd == '&')
4144 i = RE_SUBST;
4145 else if (*cmd == '?' || *cmd == '/')
4146 i = RE_SEARCH;
4147 else
4148 {
4149 EMSG(_(e_backslash));
4150 cmd = NULL;
4151 goto error;
4152 }
4153
4154 if (!skip)
4155 {
4156 /*
4157 * When search follows another address, start from
4158 * there.
4159 */
4160 if (lnum != MAXLNUM)
4161 pos.lnum = lnum;
4162 else
4163 pos.lnum = curwin->w_cursor.lnum;
4164
4165 /*
4166 * Start the search just like for the above
4167 * do_search().
4168 */
4169 if (*cmd != '?')
4170 pos.col = MAXCOL;
4171 else
4172 pos.col = 0;
4173 if (searchit(curwin, curbuf, &pos,
4174 *cmd == '?' ? BACKWARD : FORWARD,
Bram Moolenaaraad86642008-03-10 20:34:59 +00004175 (char_u *)"", 1L, SEARCH_MSG,
Bram Moolenaar76929292008-01-06 19:07:36 +00004176 i, (linenr_T)0, NULL) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177 lnum = pos.lnum;
4178 else
4179 {
4180 cmd = NULL;
4181 goto error;
4182 }
4183 }
4184 ++cmd;
4185 break;
4186
4187 default:
4188 if (VIM_ISDIGIT(*cmd)) /* absolute line number */
4189 lnum = getdigits(&cmd);
4190 }
4191
4192 for (;;)
4193 {
4194 cmd = skipwhite(cmd);
4195 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4196 break;
4197
4198 if (lnum == MAXLNUM)
4199 lnum = curwin->w_cursor.lnum; /* "+1" is same as ".+1" */
4200 if (VIM_ISDIGIT(*cmd))
4201 i = '+'; /* "number" is same as "+number" */
4202 else
4203 i = *cmd++;
4204 if (!VIM_ISDIGIT(*cmd)) /* '+' is '+1', but '+0' is not '+1' */
4205 n = 1;
4206 else
4207 n = getdigits(&cmd);
4208 if (i == '-')
4209 lnum -= n;
4210 else
4211 lnum += n;
4212 }
4213 } while (*cmd == '/' || *cmd == '?');
4214
4215error:
4216 *ptr = cmd;
4217 return lnum;
4218}
4219
4220/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004221 * Get flags from an Ex command argument.
4222 */
4223 static void
4224get_flags(eap)
4225 exarg_T *eap;
4226{
4227 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4228 {
4229 if (*eap->arg == 'l')
4230 eap->flags |= EXFLAG_LIST;
4231 else if (*eap->arg == 'p')
4232 eap->flags |= EXFLAG_PRINT;
4233 else
4234 eap->flags |= EXFLAG_NR;
4235 eap->arg = skipwhite(eap->arg + 1);
4236 }
4237}
4238
4239/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240 * Function called for command which is Not Implemented. NI!
4241 */
4242 void
4243ex_ni(eap)
4244 exarg_T *eap;
4245{
4246 if (!eap->skip)
4247 eap->errmsg = (char_u *)N_("E319: Sorry, the command is not available in this version");
4248}
4249
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004250#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004251/*
4252 * Function called for script command which is Not Implemented. NI!
4253 * Skips over ":perl <<EOF" constructs.
4254 */
4255 static void
4256ex_script_ni(eap)
4257 exarg_T *eap;
4258{
4259 if (!eap->skip)
4260 ex_ni(eap);
4261 else
4262 vim_free(script_get(eap, eap->arg));
4263}
4264#endif
4265
4266/*
4267 * Check range in Ex command for validity.
4268 * Return NULL when valid, error message when invalid.
4269 */
4270 static char_u *
4271invalid_range(eap)
4272 exarg_T *eap;
4273{
4274 if ( eap->line1 < 0
4275 || eap->line2 < 0
4276 || eap->line1 > eap->line2
4277 || ((eap->argt & RANGE)
4278 && !(eap->argt & NOTADR)
4279 && eap->line2 > curbuf->b_ml.ml_line_count
4280#ifdef FEAT_DIFF
4281 + (eap->cmdidx == CMD_diffget)
4282#endif
4283 ))
4284 return (char_u *)_(e_invrange);
4285 return NULL;
4286}
4287
4288/*
4289 * Correct the range for zero line number, if required.
4290 */
4291 static void
4292correct_range(eap)
4293 exarg_T *eap;
4294{
4295 if (!(eap->argt & ZEROR)) /* zero in range not allowed */
4296 {
4297 if (eap->line1 == 0)
4298 eap->line1 = 1;
4299 if (eap->line2 == 0)
4300 eap->line2 = 1;
4301 }
4302}
4303
Bram Moolenaar748bf032005-02-02 23:04:36 +00004304#ifdef FEAT_QUICKFIX
4305static char_u *skip_grep_pat __ARGS((exarg_T *eap));
4306
4307/*
4308 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4309 * pattern. Otherwise return eap->arg.
4310 */
4311 static char_u *
4312skip_grep_pat(eap)
4313 exarg_T *eap;
4314{
4315 char_u *p = eap->arg;
4316
Bram Moolenaara37420f2006-02-04 22:37:47 +00004317 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4318 || eap->cmdidx == CMD_vimgrepadd
4319 || eap->cmdidx == CMD_lvimgrepadd
4320 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004321 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004322 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004323 if (p == NULL)
4324 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004325 }
4326 return p;
4327}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004328
4329/*
4330 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4331 * in the command line, so that things like % get expanded.
4332 */
4333 static char_u *
4334replace_makeprg(eap, p, cmdlinep)
4335 exarg_T *eap;
4336 char_u *p;
4337 char_u **cmdlinep;
4338{
4339 char_u *new_cmdline;
4340 char_u *program;
4341 char_u *pos;
4342 char_u *ptr;
4343 int len;
4344 int i;
4345
4346 /*
4347 * Don't do it when ":vimgrep" is used for ":grep".
4348 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004349 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4350 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4351 || eap->cmdidx == CMD_grepadd
4352 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004353 && !grep_internal(eap->cmdidx))
4354 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004355 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4356 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004357 {
4358 if (*curbuf->b_p_gp == NUL)
4359 program = p_gp;
4360 else
4361 program = curbuf->b_p_gp;
4362 }
4363 else
4364 {
4365 if (*curbuf->b_p_mp == NUL)
4366 program = p_mp;
4367 else
4368 program = curbuf->b_p_mp;
4369 }
4370
4371 p = skipwhite(p);
4372
4373 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4374 {
4375 /* replace $* by given arguments */
4376 i = 1;
4377 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4378 ++i;
4379 len = (int)STRLEN(p);
4380 new_cmdline = alloc((int)(STRLEN(program) + i * (len - 2) + 1));
4381 if (new_cmdline == NULL)
4382 return NULL; /* out of memory */
4383 ptr = new_cmdline;
4384 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4385 {
4386 i = (int)(pos - program);
4387 STRNCPY(ptr, program, i);
4388 STRCPY(ptr += i, p);
4389 ptr += len;
4390 program = pos + 2;
4391 }
4392 STRCPY(ptr, program);
4393 }
4394 else
4395 {
4396 new_cmdline = alloc((int)(STRLEN(program) + STRLEN(p) + 2));
4397 if (new_cmdline == NULL)
4398 return NULL; /* out of memory */
4399 STRCPY(new_cmdline, program);
4400 STRCAT(new_cmdline, " ");
4401 STRCAT(new_cmdline, p);
4402 }
4403 msg_make(p);
4404
4405 /* 'eap->cmd' is not set here, because it is not used at CMD_make */
4406 vim_free(*cmdlinep);
4407 *cmdlinep = new_cmdline;
4408 p = new_cmdline;
4409 }
4410 return p;
4411}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004412#endif
4413
Bram Moolenaar071d4272004-06-13 20:20:40 +00004414/*
4415 * Expand file name in Ex command argument.
4416 * Return FAIL for failure, OK otherwise.
4417 */
4418 int
4419expand_filename(eap, cmdlinep, errormsgp)
4420 exarg_T *eap;
4421 char_u **cmdlinep;
4422 char_u **errormsgp;
4423{
4424 int has_wildcards; /* need to expand wildcards */
4425 char_u *repl;
4426 int srclen;
4427 char_u *p;
4428 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004429 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004430
Bram Moolenaar748bf032005-02-02 23:04:36 +00004431#ifdef FEAT_QUICKFIX
4432 /* Skip a regexp pattern for ":vimgrep[add] pat file..." */
4433 p = skip_grep_pat(eap);
4434#else
4435 p = eap->arg;
4436#endif
4437
Bram Moolenaar071d4272004-06-13 20:20:40 +00004438 /*
4439 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4440 * the file name contains a wildcard it should not cause expanding.
4441 * (it will be expanded anyway if there is a wildcard before replacing).
4442 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004443 has_wildcards = mch_has_wildcard(p);
4444 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004445 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004446#ifdef FEAT_EVAL
4447 /* Skip over `=expr`, wildcards in it are not expanded. */
4448 if (p[0] == '`' && p[1] == '=')
4449 {
4450 p += 2;
4451 (void)skip_expr(&p);
4452 if (*p == '`')
4453 ++p;
4454 continue;
4455 }
4456#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004457 /*
4458 * Quick check if this cannot be the start of a special string.
4459 * Also removes backslash before '%', '#' and '<'.
4460 */
4461 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4462 {
4463 ++p;
4464 continue;
4465 }
4466
4467 /*
4468 * Try to find a match at this position.
4469 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004470 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
4471 errormsgp, &escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004472 if (*errormsgp != NULL) /* error detected */
4473 return FAIL;
4474 if (repl == NULL) /* no match found */
4475 {
4476 p += srclen;
4477 continue;
4478 }
4479
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004480 /* Wildcards won't be expanded below, the replacement is taken
4481 * literally. But do expand "~/file", "~user/file" and "$HOME/file". */
4482 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4483 {
4484 char_u *l = repl;
4485
4486 repl = expand_env_save(repl);
4487 vim_free(l);
4488 }
4489
Bram Moolenaar63b92542007-03-27 14:57:09 +00004490 /* Need to escape white space et al. with a backslash.
4491 * Don't do this for:
4492 * - replacement that already has been escaped: "##"
4493 * - shell commands (may have to use quotes instead).
4494 * - non-unix systems when there is a single argument (spaces don't
4495 * separate arguments then).
4496 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004497 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00004498 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00004499 && eap->cmdidx != CMD_bang
4500 && eap->cmdidx != CMD_make
Bram Moolenaara37420f2006-02-04 22:37:47 +00004501 && eap->cmdidx != CMD_lmake
Bram Moolenaar071d4272004-06-13 20:20:40 +00004502 && eap->cmdidx != CMD_grep
Bram Moolenaara37420f2006-02-04 22:37:47 +00004503 && eap->cmdidx != CMD_lgrep
Bram Moolenaar071d4272004-06-13 20:20:40 +00004504 && eap->cmdidx != CMD_grepadd
Bram Moolenaara37420f2006-02-04 22:37:47 +00004505 && eap->cmdidx != CMD_lgrepadd
Bram Moolenaar071d4272004-06-13 20:20:40 +00004506#ifndef UNIX
4507 && !(eap->argt & NOSPC)
4508#endif
4509 )
4510 {
4511 char_u *l;
4512#ifdef BACKSLASH_IN_FILENAME
4513 /* Don't escape a backslash here, because rem_backslash() doesn't
4514 * remove it later. */
4515 static char_u *nobslash = (char_u *)" \t\"|";
4516# define ESCAPE_CHARS nobslash
4517#else
4518# define ESCAPE_CHARS escape_chars
4519#endif
4520
4521 for (l = repl; *l; ++l)
4522 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
4523 {
4524 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
4525 if (l != NULL)
4526 {
4527 vim_free(repl);
4528 repl = l;
4529 }
4530 break;
4531 }
4532 }
4533
4534 /* For a shell command a '!' must be escaped. */
4535 if ((eap->usefilter || eap->cmdidx == CMD_bang)
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004536 && vim_strpbrk(repl, (char_u *)"!&;()<>") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004537 {
4538 char_u *l;
4539
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004540 l = vim_strsave_escaped(repl, (char_u *)"!&;()<>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004541 if (l != NULL)
4542 {
4543 vim_free(repl);
4544 repl = l;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004545 /* For a sh-like shell escape "!" another time. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004546 if (strstr((char *)p_sh, "sh") != NULL)
4547 {
4548 l = vim_strsave_escaped(repl, (char_u *)"!");
4549 if (l != NULL)
4550 {
4551 vim_free(repl);
4552 repl = l;
4553 }
4554 }
4555 }
4556 }
4557
4558 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
4559 vim_free(repl);
4560 if (p == NULL)
4561 return FAIL;
4562 }
4563
4564 /*
4565 * One file argument: Expand wildcards.
4566 * Don't do this with ":r !command" or ":w !command".
4567 */
4568 if ((eap->argt & NOSPC) && !eap->usefilter)
4569 {
4570 /*
4571 * May do this twice:
4572 * 1. Replace environment variables.
4573 * 2. Replace any other wildcards, remove backslashes.
4574 */
4575 for (n = 1; n <= 2; ++n)
4576 {
4577 if (n == 2)
4578 {
4579#ifdef UNIX
4580 /*
4581 * Only for Unix we check for more than one file name.
4582 * For other systems spaces are considered to be part
4583 * of the file name.
4584 * Only check here if there is no wildcard, otherwise
4585 * ExpandOne() will check for errors. This allows
4586 * ":e `ls ve*.c`" on Unix.
4587 */
4588 if (!has_wildcards)
4589 for (p = eap->arg; *p; ++p)
4590 {
4591 /* skip escaped characters */
4592 if (p[1] && (*p == '\\' || *p == Ctrl_V))
4593 ++p;
4594 else if (vim_iswhite(*p))
4595 {
4596 *errormsgp = (char_u *)_("E172: Only one file name allowed");
4597 return FAIL;
4598 }
4599 }
4600#endif
4601
4602 /*
4603 * Halve the number of backslashes (this is Vi compatible).
4604 * For Unix and OS/2, when wildcards are expanded, this is
4605 * done by ExpandOne() below.
4606 */
4607#if defined(UNIX) || defined(OS2)
4608 if (!has_wildcards)
4609#endif
4610 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004611 }
4612
4613 if (has_wildcards)
4614 {
4615 if (n == 1)
4616 {
4617 /*
4618 * First loop: May expand environment variables. This
4619 * can be done much faster with expand_env() than with
4620 * something else (e.g., calling a shell).
4621 * After expanding environment variables, check again
4622 * if there are still wildcards present.
4623 */
4624 if (vim_strchr(eap->arg, '$') != NULL
4625 || vim_strchr(eap->arg, '~') != NULL)
4626 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00004627 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004628 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004629 has_wildcards = mch_has_wildcard(NameBuff);
4630 p = NameBuff;
4631 }
4632 else
4633 p = NULL;
4634 }
4635 else /* n == 2 */
4636 {
4637 expand_T xpc;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004638 int options = WILD_LIST_NOTFOUND|WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004639
4640 ExpandInit(&xpc);
4641 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004642 if (p_wic)
4643 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004644 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01004645 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004646 if (p == NULL)
4647 return FAIL;
4648 }
4649 if (p != NULL)
4650 {
4651 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
4652 p, cmdlinep);
4653 if (n == 2) /* p came from ExpandOne() */
4654 vim_free(p);
4655 }
4656 }
4657 }
4658 }
4659 return OK;
4660}
4661
4662/*
4663 * Replace part of the command line, keeping eap->cmd, eap->arg and
4664 * eap->nextcmd correct.
4665 * "src" points to the part that is to be replaced, of length "srclen".
4666 * "repl" is the replacement string.
4667 * Returns a pointer to the character after the replaced string.
4668 * Returns NULL for failure.
4669 */
4670 static char_u *
4671repl_cmdline(eap, src, srclen, repl, cmdlinep)
4672 exarg_T *eap;
4673 char_u *src;
4674 int srclen;
4675 char_u *repl;
4676 char_u **cmdlinep;
4677{
4678 int len;
4679 int i;
4680 char_u *new_cmdline;
4681
4682 /*
4683 * The new command line is build in new_cmdline[].
4684 * First allocate it.
4685 * Careful: a "+cmd" argument may have been NUL terminated.
4686 */
4687 len = (int)STRLEN(repl);
4688 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004689 if (eap->nextcmd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004690 i += (int)STRLEN(eap->nextcmd);/* add space for next command */
4691 if ((new_cmdline = alloc((unsigned)i)) == NULL)
4692 return NULL; /* out of memory! */
4693
4694 /*
4695 * Copy the stuff before the expanded part.
4696 * Copy the expanded stuff.
4697 * Copy what came after the expanded part.
4698 * Copy the next commands, if there are any.
4699 */
4700 i = (int)(src - *cmdlinep); /* length of part before match */
4701 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00004702
Bram Moolenaar071d4272004-06-13 20:20:40 +00004703 mch_memmove(new_cmdline + i, repl, (size_t)len);
4704 i += len; /* remember the end of the string */
4705 STRCPY(new_cmdline + i, src + srclen);
4706 src = new_cmdline + i; /* remember where to continue */
4707
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004708 if (eap->nextcmd != NULL) /* append next command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004709 {
4710 i = (int)STRLEN(new_cmdline) + 1;
4711 STRCPY(new_cmdline + i, eap->nextcmd);
4712 eap->nextcmd = new_cmdline + i;
4713 }
4714 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
4715 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
4716 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
4717 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
4718 vim_free(*cmdlinep);
4719 *cmdlinep = new_cmdline;
4720
4721 return src;
4722}
4723
4724/*
4725 * Check for '|' to separate commands and '"' to start comments.
4726 */
4727 void
4728separate_nextcmd(eap)
4729 exarg_T *eap;
4730{
4731 char_u *p;
4732
Bram Moolenaar86b68352004-12-27 21:59:20 +00004733#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004734 p = skip_grep_pat(eap);
4735#else
4736 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004737#endif
4738
4739 for ( ; *p; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004740 {
4741 if (*p == Ctrl_V)
4742 {
4743 if (eap->argt & (USECTRLV | XFILE))
4744 ++p; /* skip CTRL-V and next char */
4745 else
Bram Moolenaarb0db5692007-08-14 20:54:49 +00004746 /* remove CTRL-V and skip next char */
Bram Moolenaara7241f52008-06-24 20:39:31 +00004747 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004748 if (*p == NUL) /* stop at NUL after CTRL-V */
4749 break;
4750 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004751
4752#ifdef FEAT_EVAL
4753 /* Skip over `=expr` when wildcards are expanded. */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004754 else if (p[0] == '`' && p[1] == '=' && (eap->argt & XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004755 {
4756 p += 2;
4757 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004758 }
4759#endif
4760
Bram Moolenaar071d4272004-06-13 20:20:40 +00004761 /* Check for '"': start of comment or '|': next command */
4762 /* :@" and :*" do not start a comment!
4763 * :redir @" doesn't either. */
4764 else if ((*p == '"' && !(eap->argt & NOTRLCOM)
4765 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
4766 || p != eap->arg)
4767 && (eap->cmdidx != CMD_redir
4768 || p != eap->arg + 1 || p[-1] != '@'))
4769 || *p == '|' || *p == '\n')
4770 {
4771 /*
4772 * We remove the '\' before the '|', unless USECTRLV is used
4773 * AND 'b' is present in 'cpoptions'.
4774 */
4775 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
4776 || !(eap->argt & USECTRLV)) && *(p - 1) == '\\')
4777 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00004778 STRMOVE(p - 1, p); /* remove the '\' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779 --p;
4780 }
4781 else
4782 {
4783 eap->nextcmd = check_nextcmd(p);
4784 *p = NUL;
4785 break;
4786 }
4787 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004788 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004789
Bram Moolenaar071d4272004-06-13 20:20:40 +00004790 if (!(eap->argt & NOTRLCOM)) /* remove trailing spaces */
4791 del_trailing_spaces(eap->arg);
4792}
4793
4794/*
4795 * get + command from ex argument
4796 */
4797 static char_u *
4798getargcmd(argp)
4799 char_u **argp;
4800{
4801 char_u *arg = *argp;
4802 char_u *command = NULL;
4803
4804 if (*arg == '+') /* +[command] */
4805 {
4806 ++arg;
4807 if (vim_isspace(*arg))
4808 command = dollar_command;
4809 else
4810 {
4811 command = arg;
4812 arg = skip_cmd_arg(command, TRUE);
4813 if (*arg != NUL)
4814 *arg++ = NUL; /* terminate command with NUL */
4815 }
4816
4817 arg = skipwhite(arg); /* skip over spaces */
4818 *argp = arg;
4819 }
4820 return command;
4821}
4822
4823/*
4824 * Find end of "+command" argument. Skip over "\ " and "\\".
4825 */
4826 static char_u *
4827skip_cmd_arg(p, rembs)
4828 char_u *p;
4829 int rembs; /* TRUE to halve the number of backslashes */
4830{
4831 while (*p && !vim_isspace(*p))
4832 {
4833 if (*p == '\\' && p[1] != NUL)
4834 {
4835 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00004836 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004837 else
4838 ++p;
4839 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004840 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004841 }
4842 return p;
4843}
4844
4845/*
4846 * Get "++opt=arg" argument.
4847 * Return FAIL or OK.
4848 */
4849 static int
4850getargopt(eap)
4851 exarg_T *eap;
4852{
4853 char_u *arg = eap->arg + 2;
4854 int *pp = NULL;
4855#ifdef FEAT_MBYTE
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004856 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004857 char_u *p;
4858#endif
4859
4860 /* ":edit ++[no]bin[ary] file" */
4861 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
4862 {
4863 if (*arg == 'n')
4864 {
4865 arg += 2;
4866 eap->force_bin = FORCE_NOBIN;
4867 }
4868 else
4869 eap->force_bin = FORCE_BIN;
4870 if (!checkforcmd(&arg, "binary", 3))
4871 return FAIL;
4872 eap->arg = skipwhite(arg);
4873 return OK;
4874 }
4875
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004876 /* ":read ++edit file" */
4877 if (STRNCMP(arg, "edit", 4) == 0)
4878 {
4879 eap->read_edit = TRUE;
4880 eap->arg = skipwhite(arg + 4);
4881 return OK;
4882 }
4883
Bram Moolenaar071d4272004-06-13 20:20:40 +00004884 if (STRNCMP(arg, "ff", 2) == 0)
4885 {
4886 arg += 2;
4887 pp = &eap->force_ff;
4888 }
4889 else if (STRNCMP(arg, "fileformat", 10) == 0)
4890 {
4891 arg += 10;
4892 pp = &eap->force_ff;
4893 }
4894#ifdef FEAT_MBYTE
4895 else if (STRNCMP(arg, "enc", 3) == 0)
4896 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01004897 if (STRNCMP(arg, "encoding", 8) == 0)
4898 arg += 8;
4899 else
4900 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004901 pp = &eap->force_enc;
4902 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004903 else if (STRNCMP(arg, "bad", 3) == 0)
4904 {
4905 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004906 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004907 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004908#endif
4909
4910 if (pp == NULL || *arg != '=')
4911 return FAIL;
4912
4913 ++arg;
4914 *pp = (int)(arg - eap->cmd);
4915 arg = skip_cmd_arg(arg, FALSE);
4916 eap->arg = skipwhite(arg);
4917 *arg = NUL;
4918
4919#ifdef FEAT_MBYTE
4920 if (pp == &eap->force_ff)
4921 {
4922#endif
4923 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
4924 return FAIL;
4925#ifdef FEAT_MBYTE
4926 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004927 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004928 {
4929 /* Make 'fileencoding' lower case. */
4930 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
4931 *p = TOLOWER_ASC(*p);
4932 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004933 else
4934 {
4935 /* Check ++bad= argument. Must be a single-byte character, "keep" or
4936 * "drop". */
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004937 p = eap->cmd + bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004938 if (STRICMP(p, "keep") == 0)
4939 eap->bad_char = BAD_KEEP;
4940 else if (STRICMP(p, "drop") == 0)
4941 eap->bad_char = BAD_DROP;
4942 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
4943 eap->bad_char = *p;
4944 else
4945 return FAIL;
4946 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004947#endif
4948
4949 return OK;
4950}
4951
4952/*
4953 * ":abbreviate" and friends.
4954 */
4955 static void
4956ex_abbreviate(eap)
4957 exarg_T *eap;
4958{
4959 do_exmap(eap, TRUE); /* almost the same as mapping */
4960}
4961
4962/*
4963 * ":map" and friends.
4964 */
4965 static void
4966ex_map(eap)
4967 exarg_T *eap;
4968{
4969 /*
4970 * If we are sourcing .exrc or .vimrc in current directory we
4971 * print the mappings for security reasons.
4972 */
4973 if (secure)
4974 {
4975 secure = 2;
4976 msg_outtrans(eap->cmd);
4977 msg_putchar('\n');
4978 }
4979 do_exmap(eap, FALSE);
4980}
4981
4982/*
4983 * ":unmap" and friends.
4984 */
4985 static void
4986ex_unmap(eap)
4987 exarg_T *eap;
4988{
4989 do_exmap(eap, FALSE);
4990}
4991
4992/*
4993 * ":mapclear" and friends.
4994 */
4995 static void
4996ex_mapclear(eap)
4997 exarg_T *eap;
4998{
4999 map_clear(eap->cmd, eap->arg, eap->forceit, FALSE);
5000}
5001
5002/*
5003 * ":abclear" and friends.
5004 */
5005 static void
5006ex_abclear(eap)
5007 exarg_T *eap;
5008{
5009 map_clear(eap->cmd, eap->arg, TRUE, TRUE);
5010}
5011
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005012#if defined(FEAT_AUTOCMD) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005013 static void
5014ex_autocmd(eap)
5015 exarg_T *eap;
5016{
5017 /*
5018 * Disallow auto commands from .exrc and .vimrc in current
5019 * directory for security reasons.
5020 */
5021 if (secure)
5022 {
5023 secure = 2;
5024 eap->errmsg = e_curdir;
5025 }
5026 else if (eap->cmdidx == CMD_autocmd)
5027 do_autocmd(eap->arg, eap->forceit);
5028 else
5029 do_augroup(eap->arg, eap->forceit);
5030}
5031
5032/*
5033 * ":doautocmd": Apply the automatic commands to the current buffer.
5034 */
5035 static void
5036ex_doautocmd(eap)
5037 exarg_T *eap;
5038{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005039 char_u *arg = eap->arg;
5040 int call_do_modelines = check_nomodeline(&arg);
5041
5042 (void)do_doautocmd(arg, TRUE);
5043 if (call_do_modelines) /* Only when there is no <nomodeline>. */
5044 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005045}
5046#endif
5047
5048#ifdef FEAT_LISTCMDS
5049/*
5050 * :[N]bunload[!] [N] [bufname] unload buffer
5051 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
5052 * :[N]bwipeout[!] [N] [bufname] delete buffer really
5053 */
5054 static void
5055ex_bunload(eap)
5056 exarg_T *eap;
5057{
5058 eap->errmsg = do_bufdel(
5059 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
5060 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
5061 : DOBUF_UNLOAD, eap->arg,
5062 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
5063}
5064
5065/*
5066 * :[N]buffer [N] to buffer N
5067 * :[N]sbuffer [N] to buffer N
5068 */
5069 static void
5070ex_buffer(eap)
5071 exarg_T *eap;
5072{
5073 if (*eap->arg)
5074 eap->errmsg = e_trailing;
5075 else
5076 {
5077 if (eap->addr_count == 0) /* default is current buffer */
5078 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
5079 else
5080 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
5081 }
5082}
5083
5084/*
5085 * :[N]bmodified [N] to next mod. buffer
5086 * :[N]sbmodified [N] to next mod. buffer
5087 */
5088 static void
5089ex_bmodified(eap)
5090 exarg_T *eap;
5091{
5092 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
5093}
5094
5095/*
5096 * :[N]bnext [N] to next buffer
5097 * :[N]sbnext [N] split and to next buffer
5098 */
5099 static void
5100ex_bnext(eap)
5101 exarg_T *eap;
5102{
5103 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
5104}
5105
5106/*
5107 * :[N]bNext [N] to previous buffer
5108 * :[N]bprevious [N] to previous buffer
5109 * :[N]sbNext [N] split and to previous buffer
5110 * :[N]sbprevious [N] split and to previous buffer
5111 */
5112 static void
5113ex_bprevious(eap)
5114 exarg_T *eap;
5115{
5116 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
5117}
5118
5119/*
5120 * :brewind to first buffer
5121 * :bfirst to first buffer
5122 * :sbrewind split and to first buffer
5123 * :sbfirst split and to first buffer
5124 */
5125 static void
5126ex_brewind(eap)
5127 exarg_T *eap;
5128{
5129 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
5130}
5131
5132/*
5133 * :blast to last buffer
5134 * :sblast split and to last buffer
5135 */
5136 static void
5137ex_blast(eap)
5138 exarg_T *eap;
5139{
5140 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
5141}
5142#endif
5143
5144 int
5145ends_excmd(c)
5146 int c;
5147{
5148 return (c == NUL || c == '|' || c == '"' || c == '\n');
5149}
5150
5151#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5152 || defined(PROTO)
5153/*
5154 * Return the next command, after the first '|' or '\n'.
5155 * Return NULL if not found.
5156 */
5157 char_u *
5158find_nextcmd(p)
5159 char_u *p;
5160{
5161 while (*p != '|' && *p != '\n')
5162 {
5163 if (*p == NUL)
5164 return NULL;
5165 ++p;
5166 }
5167 return (p + 1);
5168}
5169#endif
5170
5171/*
5172 * Check if *p is a separator between Ex commands.
5173 * Return NULL if it isn't, (p + 1) if it is.
5174 */
5175 char_u *
5176check_nextcmd(p)
5177 char_u *p;
5178{
5179 p = skipwhite(p);
5180 if (*p == '|' || *p == '\n')
5181 return (p + 1);
5182 else
5183 return NULL;
5184}
5185
5186/*
5187 * - if there are more files to edit
5188 * - and this is the last window
5189 * - and forceit not used
5190 * - and not repeated twice on a row
5191 * return FAIL and give error message if 'message' TRUE
5192 * return OK otherwise
5193 */
5194 static int
5195check_more(message, forceit)
5196 int message; /* when FALSE check only, no messages */
5197 int forceit;
5198{
5199 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5200
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005201 if (!forceit && only_one_window()
5202 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203 {
5204 if (message)
5205 {
5206#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
5207 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
5208 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005209 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005210
5211 if (n == 1)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02005212 vim_strncpy(buff,
5213 (char_u *)_("1 more file to edit. Quit anyway?"),
Bram Moolenaard9462e32011-04-11 21:35:11 +02005214 DIALOG_MSG_SIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005215 else
Bram Moolenaard9462e32011-04-11 21:35:11 +02005216 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005217 _("%d more files to edit. Quit anyway?"), n);
5218 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5219 return OK;
5220 return FAIL;
5221 }
5222#endif
5223 if (n == 1)
5224 EMSG(_("E173: 1 more file to edit"));
5225 else
5226 EMSGN(_("E173: %ld more files to edit"), n);
5227 quitmore = 2; /* next try to quit is allowed */
5228 }
5229 return FAIL;
5230 }
5231 return OK;
5232}
5233
5234#ifdef FEAT_CMDL_COMPL
5235/*
5236 * Function given to ExpandGeneric() to obtain the list of command names.
5237 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005238 char_u *
5239get_command_name(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00005240 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005241 int idx;
5242{
5243 if (idx >= (int)CMD_SIZE)
5244# ifdef FEAT_USR_CMDS
5245 return get_user_command_name(idx);
5246# else
5247 return NULL;
5248# endif
5249 return cmdnames[idx].cmd_name;
5250}
5251#endif
5252
5253#if defined(FEAT_USR_CMDS) || defined(PROTO)
5254static 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));
5255static void uc_list __ARGS((char_u *name, size_t name_len));
5256static int uc_scan_attr __ARGS((char_u *attr, size_t len, long *argt, long *def, int *flags, int *compl, char_u **compl_arg));
5257static char_u *uc_split_args __ARGS((char_u *arg, size_t *lenp));
5258static 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));
5259
5260 static int
5261uc_add_command(name, name_len, rep, argt, def, flags, compl, compl_arg, force)
5262 char_u *name;
5263 size_t name_len;
5264 char_u *rep;
5265 long argt;
5266 long def;
5267 int flags;
5268 int compl;
5269 char_u *compl_arg;
5270 int force;
5271{
5272 ucmd_T *cmd = NULL;
5273 char_u *p;
5274 int i;
5275 int cmp = 1;
5276 char_u *rep_buf = NULL;
5277 garray_T *gap;
5278
Bram Moolenaar9c102382006-05-03 21:26:49 +00005279 replace_termcodes(rep, &rep_buf, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005280 if (rep_buf == NULL)
5281 {
5282 /* Can't replace termcodes - try using the string as is */
5283 rep_buf = vim_strsave(rep);
5284
5285 /* Give up if out of memory */
5286 if (rep_buf == NULL)
5287 return FAIL;
5288 }
5289
5290 /* get address of growarray: global or in curbuf */
5291 if (flags & UC_BUFFER)
5292 {
5293 gap = &curbuf->b_ucmds;
5294 if (gap->ga_itemsize == 0)
5295 ga_init2(gap, (int)sizeof(ucmd_T), 4);
5296 }
5297 else
5298 gap = &ucmds;
5299
5300 /* Search for the command in the already defined commands. */
5301 for (i = 0; i < gap->ga_len; ++i)
5302 {
5303 size_t len;
5304
5305 cmd = USER_CMD_GA(gap, i);
5306 len = STRLEN(cmd->uc_name);
5307 cmp = STRNCMP(name, cmd->uc_name, name_len);
5308 if (cmp == 0)
5309 {
5310 if (name_len < len)
5311 cmp = -1;
5312 else if (name_len > len)
5313 cmp = 1;
5314 }
5315
5316 if (cmp == 0)
5317 {
5318 if (!force)
5319 {
5320 EMSG(_("E174: Command already exists: add ! to replace it"));
5321 goto fail;
5322 }
5323
5324 vim_free(cmd->uc_rep);
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00005325 cmd->uc_rep = NULL;
5326#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5327 vim_free(cmd->uc_compl_arg);
5328 cmd->uc_compl_arg = NULL;
5329#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005330 break;
5331 }
5332
5333 /* Stop as soon as we pass the name to add */
5334 if (cmp < 0)
5335 break;
5336 }
5337
5338 /* Extend the array unless we're replacing an existing command */
5339 if (cmp != 0)
5340 {
5341 if (ga_grow(gap, 1) != OK)
5342 goto fail;
5343 if ((p = vim_strnsave(name, (int)name_len)) == NULL)
5344 goto fail;
5345
5346 cmd = USER_CMD_GA(gap, i);
5347 mch_memmove(cmd + 1, cmd, (gap->ga_len - i) * sizeof(ucmd_T));
5348
5349 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005350
5351 cmd->uc_name = p;
5352 }
5353
5354 cmd->uc_rep = rep_buf;
5355 cmd->uc_argt = argt;
5356 cmd->uc_def = def;
5357 cmd->uc_compl = compl;
5358#ifdef FEAT_EVAL
5359 cmd->uc_scriptID = current_SID;
5360# ifdef FEAT_CMDL_COMPL
5361 cmd->uc_compl_arg = compl_arg;
5362# endif
5363#endif
5364
5365 return OK;
5366
5367fail:
5368 vim_free(rep_buf);
5369#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5370 vim_free(compl_arg);
5371#endif
5372 return FAIL;
5373}
5374
5375/*
5376 * List of names for completion for ":command" with the EXPAND_ flag.
5377 * Must be alphabetical for completion.
5378 */
5379static struct
5380{
5381 int expand;
5382 char *name;
5383} command_complete[] =
5384{
5385 {EXPAND_AUGROUP, "augroup"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005386 {EXPAND_BEHAVE, "behave"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005387 {EXPAND_BUFFERS, "buffer"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005388 {EXPAND_COLORS, "color"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005389 {EXPAND_COMMANDS, "command"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005390 {EXPAND_COMPILER, "compiler"},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005391#if defined(FEAT_CSCOPE)
5392 {EXPAND_CSCOPE, "cscope"},
5393#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005394#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5395 {EXPAND_USER_DEFINED, "custom"},
Bram Moolenaara466c992005-07-09 21:03:22 +00005396 {EXPAND_USER_LIST, "customlist"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005397#endif
5398 {EXPAND_DIRECTORIES, "dir"},
5399 {EXPAND_ENV_VARS, "environment"},
5400 {EXPAND_EVENTS, "event"},
5401 {EXPAND_EXPRESSION, "expression"},
5402 {EXPAND_FILES, "file"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005403 {EXPAND_FILES_IN_PATH, "file_in_path"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005404 {EXPAND_FILETYPE, "filetype"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005405 {EXPAND_FUNCTIONS, "function"},
5406 {EXPAND_HELP, "help"},
5407 {EXPAND_HIGHLIGHT, "highlight"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005408#if defined(FEAT_CMDHIST)
5409 {EXPAND_HISTORY, "history"},
5410#endif
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005411#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005412 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005413 {EXPAND_LOCALES, "locale"},
5414#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005415 {EXPAND_MAPPINGS, "mapping"},
5416 {EXPAND_MENUS, "menu"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005417 {EXPAND_OWNSYNTAX, "syntax"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005418 {EXPAND_SETTINGS, "option"},
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005419 {EXPAND_SHELLCMD, "shellcmd"},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005420#if defined(FEAT_SIGNS)
5421 {EXPAND_SIGN, "sign"},
5422#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005423 {EXPAND_TAGS, "tag"},
5424 {EXPAND_TAGS_LISTFILES, "tag_listfiles"},
Bram Moolenaar24305862012-08-15 14:05:05 +02005425 {EXPAND_USER, "user"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005426 {EXPAND_USER_VARS, "var"},
5427 {0, NULL}
5428};
5429
5430 static void
5431uc_list(name, name_len)
5432 char_u *name;
5433 size_t name_len;
5434{
5435 int i, j;
5436 int found = FALSE;
5437 ucmd_T *cmd;
5438 int len;
5439 long a;
5440 garray_T *gap;
5441
5442 gap = &curbuf->b_ucmds;
5443 for (;;)
5444 {
5445 for (i = 0; i < gap->ga_len; ++i)
5446 {
5447 cmd = USER_CMD_GA(gap, i);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005448 a = (long)cmd->uc_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005449
5450 /* Skip commands which don't match the requested prefix */
5451 if (STRNCMP(name, cmd->uc_name, name_len) != 0)
5452 continue;
5453
5454 /* Put out the title first time */
5455 if (!found)
5456 MSG_PUTS_TITLE(_("\n Name Args Range Complete Definition"));
5457 found = TRUE;
5458 msg_putchar('\n');
5459 if (got_int)
5460 break;
5461
5462 /* Special cases */
5463 msg_putchar(a & BANG ? '!' : ' ');
5464 msg_putchar(a & REGSTR ? '"' : ' ');
5465 msg_putchar(gap != &ucmds ? 'b' : ' ');
5466 msg_putchar(' ');
5467
5468 msg_outtrans_attr(cmd->uc_name, hl_attr(HLF_D));
5469 len = (int)STRLEN(cmd->uc_name) + 4;
5470
5471 do {
5472 msg_putchar(' ');
5473 ++len;
5474 } while (len < 16);
5475
5476 len = 0;
5477
5478 /* Arguments */
5479 switch ((int)(a & (EXTRA|NOSPC|NEEDARG)))
5480 {
5481 case 0: IObuff[len++] = '0'; break;
5482 case (EXTRA): IObuff[len++] = '*'; break;
5483 case (EXTRA|NOSPC): IObuff[len++] = '?'; break;
5484 case (EXTRA|NEEDARG): IObuff[len++] = '+'; break;
5485 case (EXTRA|NOSPC|NEEDARG): IObuff[len++] = '1'; break;
5486 }
5487
5488 do {
5489 IObuff[len++] = ' ';
5490 } while (len < 5);
5491
5492 /* Range */
5493 if (a & (RANGE|COUNT))
5494 {
5495 if (a & COUNT)
5496 {
5497 /* -count=N */
5498 sprintf((char *)IObuff + len, "%ldc", cmd->uc_def);
5499 len += (int)STRLEN(IObuff + len);
5500 }
5501 else if (a & DFLALL)
5502 IObuff[len++] = '%';
5503 else if (cmd->uc_def >= 0)
5504 {
5505 /* -range=N */
5506 sprintf((char *)IObuff + len, "%ld", cmd->uc_def);
5507 len += (int)STRLEN(IObuff + len);
5508 }
5509 else
5510 IObuff[len++] = '.';
5511 }
5512
5513 do {
5514 IObuff[len++] = ' ';
5515 } while (len < 11);
5516
5517 /* Completion */
5518 for (j = 0; command_complete[j].expand != 0; ++j)
5519 if (command_complete[j].expand == cmd->uc_compl)
5520 {
5521 STRCPY(IObuff + len, command_complete[j].name);
5522 len += (int)STRLEN(IObuff + len);
5523 break;
5524 }
5525
5526 do {
5527 IObuff[len++] = ' ';
5528 } while (len < 21);
5529
5530 IObuff[len] = '\0';
5531 msg_outtrans(IObuff);
5532
5533 msg_outtrans_special(cmd->uc_rep, FALSE);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005534#ifdef FEAT_EVAL
5535 if (p_verbose > 0)
5536 last_set_msg(cmd->uc_scriptID);
5537#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005538 out_flush();
5539 ui_breakcheck();
5540 if (got_int)
5541 break;
5542 }
5543 if (gap == &ucmds || i < gap->ga_len)
5544 break;
5545 gap = &ucmds;
5546 }
5547
5548 if (!found)
5549 MSG(_("No user-defined commands found"));
5550}
5551
5552 static char_u *
5553uc_fun_cmd()
5554{
5555 static char_u fcmd[] = {0x84, 0xaf, 0x60, 0xb9, 0xaf, 0xb5, 0x60, 0xa4,
5556 0xa5, 0xad, 0xa1, 0xae, 0xa4, 0x60, 0xa1, 0x60,
5557 0xb3, 0xa8, 0xb2, 0xb5, 0xa2, 0xa2, 0xa5, 0xb2,
5558 0xb9, 0x7f, 0};
5559 int i;
5560
5561 for (i = 0; fcmd[i]; ++i)
5562 IObuff[i] = fcmd[i] - 0x40;
5563 IObuff[i] = 0;
5564 return IObuff;
5565}
5566
5567 static int
5568uc_scan_attr(attr, len, argt, def, flags, compl, compl_arg)
5569 char_u *attr;
5570 size_t len;
5571 long *argt;
5572 long *def;
5573 int *flags;
5574 int *compl;
5575 char_u **compl_arg;
5576{
5577 char_u *p;
5578
5579 if (len == 0)
5580 {
5581 EMSG(_("E175: No attribute specified"));
5582 return FAIL;
5583 }
5584
5585 /* First, try the simple attributes (no arguments) */
5586 if (STRNICMP(attr, "bang", len) == 0)
5587 *argt |= BANG;
5588 else if (STRNICMP(attr, "buffer", len) == 0)
5589 *flags |= UC_BUFFER;
5590 else if (STRNICMP(attr, "register", len) == 0)
5591 *argt |= REGSTR;
5592 else if (STRNICMP(attr, "bar", len) == 0)
5593 *argt |= TRLBAR;
5594 else
5595 {
5596 int i;
5597 char_u *val = NULL;
5598 size_t vallen = 0;
5599 size_t attrlen = len;
5600
5601 /* Look for the attribute name - which is the part before any '=' */
5602 for (i = 0; i < (int)len; ++i)
5603 {
5604 if (attr[i] == '=')
5605 {
5606 val = &attr[i + 1];
5607 vallen = len - i - 1;
5608 attrlen = i;
5609 break;
5610 }
5611 }
5612
5613 if (STRNICMP(attr, "nargs", attrlen) == 0)
5614 {
5615 if (vallen == 1)
5616 {
5617 if (*val == '0')
5618 /* Do nothing - this is the default */;
5619 else if (*val == '1')
5620 *argt |= (EXTRA | NOSPC | NEEDARG);
5621 else if (*val == '*')
5622 *argt |= EXTRA;
5623 else if (*val == '?')
5624 *argt |= (EXTRA | NOSPC);
5625 else if (*val == '+')
5626 *argt |= (EXTRA | NEEDARG);
5627 else
5628 goto wrong_nargs;
5629 }
5630 else
5631 {
5632wrong_nargs:
5633 EMSG(_("E176: Invalid number of arguments"));
5634 return FAIL;
5635 }
5636 }
5637 else if (STRNICMP(attr, "range", attrlen) == 0)
5638 {
5639 *argt |= RANGE;
5640 if (vallen == 1 && *val == '%')
5641 *argt |= DFLALL;
5642 else if (val != NULL)
5643 {
5644 p = val;
5645 if (*def >= 0)
5646 {
5647two_count:
5648 EMSG(_("E177: Count cannot be specified twice"));
5649 return FAIL;
5650 }
5651
5652 *def = getdigits(&p);
5653 *argt |= (ZEROR | NOTADR);
5654
5655 if (p != val + vallen || vallen == 0)
5656 {
5657invalid_count:
5658 EMSG(_("E178: Invalid default value for count"));
5659 return FAIL;
5660 }
5661 }
5662 }
5663 else if (STRNICMP(attr, "count", attrlen) == 0)
5664 {
Bram Moolenaar32e7b2d2005-02-27 22:36:47 +00005665 *argt |= (COUNT | ZEROR | RANGE | NOTADR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005666
5667 if (val != NULL)
5668 {
5669 p = val;
5670 if (*def >= 0)
5671 goto two_count;
5672
5673 *def = getdigits(&p);
5674
5675 if (p != val + vallen)
5676 goto invalid_count;
5677 }
5678
5679 if (*def < 0)
5680 *def = 0;
5681 }
5682 else if (STRNICMP(attr, "complete", attrlen) == 0)
5683 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005684 if (val == NULL)
5685 {
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005686 EMSG(_("E179: argument required for -complete"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005687 return FAIL;
5688 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005689
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005690 if (parse_compl_arg(val, (int)vallen, compl, argt, compl_arg)
5691 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005692 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005693 }
5694 else
5695 {
5696 char_u ch = attr[len];
5697 attr[len] = '\0';
5698 EMSG2(_("E181: Invalid attribute: %s"), attr);
5699 attr[len] = ch;
5700 return FAIL;
5701 }
5702 }
5703
5704 return OK;
5705}
5706
Bram Moolenaara850a712009-01-28 14:42:59 +00005707/*
5708 * ":command ..."
5709 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005710 static void
5711ex_command(eap)
5712 exarg_T *eap;
5713{
5714 char_u *name;
5715 char_u *end;
5716 char_u *p;
5717 long argt = 0;
5718 long def = -1;
5719 int flags = 0;
5720 int compl = EXPAND_NOTHING;
5721 char_u *compl_arg = NULL;
5722 int has_attr = (eap->arg[0] == '-');
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01005723 int name_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005724
5725 p = eap->arg;
5726
5727 /* Check for attributes */
5728 while (*p == '-')
5729 {
5730 ++p;
5731 end = skiptowhite(p);
5732 if (uc_scan_attr(p, end - p, &argt, &def, &flags, &compl, &compl_arg)
5733 == FAIL)
5734 return;
5735 p = skipwhite(end);
5736 }
5737
5738 /* Get the name (if any) and skip to the following argument */
5739 name = p;
5740 if (ASCII_ISALPHA(*p))
5741 while (ASCII_ISALNUM(*p))
5742 ++p;
5743 if (!ends_excmd(*p) && !vim_iswhite(*p))
5744 {
5745 EMSG(_("E182: Invalid command name"));
5746 return;
5747 }
5748 end = p;
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01005749 name_len = (int)(end - name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005750
5751 /* If there is nothing after the name, and no attributes were specified,
5752 * we are listing commands
5753 */
5754 p = skipwhite(end);
5755 if (!has_attr && ends_excmd(*p))
5756 {
5757 uc_list(name, end - name);
5758 }
5759 else if (!ASCII_ISUPPER(*name))
5760 {
5761 EMSG(_("E183: User defined commands must start with an uppercase letter"));
5762 return;
5763 }
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01005764 else if ((name_len == 1 && *name == 'X')
5765 || (name_len <= 4
5766 && STRNCMP(name, "Next", name_len > 4 ? 4 : name_len) == 0))
5767 {
5768 EMSG(_("E841: Reserved name, cannot be used for user defined command"));
5769 return;
5770 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005771 else
5772 uc_add_command(name, end - name, p, argt, def, flags, compl, compl_arg,
5773 eap->forceit);
5774}
5775
5776/*
5777 * ":comclear"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005778 * Clear all user commands, global and for current buffer.
5779 */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00005780 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005781ex_comclear(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00005782 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005783{
5784 uc_clear(&ucmds);
5785 uc_clear(&curbuf->b_ucmds);
5786}
5787
5788/*
5789 * Clear all user commands for "gap".
5790 */
5791 void
5792uc_clear(gap)
5793 garray_T *gap;
5794{
5795 int i;
5796 ucmd_T *cmd;
5797
5798 for (i = 0; i < gap->ga_len; ++i)
5799 {
5800 cmd = USER_CMD_GA(gap, i);
5801 vim_free(cmd->uc_name);
5802 vim_free(cmd->uc_rep);
5803# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5804 vim_free(cmd->uc_compl_arg);
5805# endif
5806 }
5807 ga_clear(gap);
5808}
5809
5810 static void
5811ex_delcommand(eap)
5812 exarg_T *eap;
5813{
5814 int i = 0;
5815 ucmd_T *cmd = NULL;
5816 int cmp = -1;
5817 garray_T *gap;
5818
5819 gap = &curbuf->b_ucmds;
5820 for (;;)
5821 {
5822 for (i = 0; i < gap->ga_len; ++i)
5823 {
5824 cmd = USER_CMD_GA(gap, i);
5825 cmp = STRCMP(eap->arg, cmd->uc_name);
5826 if (cmp <= 0)
5827 break;
5828 }
5829 if (gap == &ucmds || cmp == 0)
5830 break;
5831 gap = &ucmds;
5832 }
5833
5834 if (cmp != 0)
5835 {
5836 EMSG2(_("E184: No such user-defined command: %s"), eap->arg);
5837 return;
5838 }
5839
5840 vim_free(cmd->uc_name);
5841 vim_free(cmd->uc_rep);
5842# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5843 vim_free(cmd->uc_compl_arg);
5844# endif
5845
5846 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005847
5848 if (i < gap->ga_len)
5849 mch_memmove(cmd, cmd + 1, (gap->ga_len - i) * sizeof(ucmd_T));
5850}
5851
Bram Moolenaar552f8a12007-03-08 17:12:08 +00005852/*
5853 * split and quote args for <f-args>
5854 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005855 static char_u *
5856uc_split_args(arg, lenp)
5857 char_u *arg;
5858 size_t *lenp;
5859{
5860 char_u *buf;
5861 char_u *p;
5862 char_u *q;
5863 int len;
5864
5865 /* Precalculate length */
5866 p = arg;
5867 len = 2; /* Initial and final quotes */
5868
5869 while (*p)
5870 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00005871 if (p[0] == '\\' && p[1] == '\\')
5872 {
5873 len += 2;
5874 p += 2;
5875 }
5876 else if (p[0] == '\\' && vim_iswhite(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005877 {
5878 len += 1;
5879 p += 2;
5880 }
5881 else if (*p == '\\' || *p == '"')
5882 {
5883 len += 2;
5884 p += 1;
5885 }
5886 else if (vim_iswhite(*p))
5887 {
5888 p = skipwhite(p);
5889 if (*p == NUL)
5890 break;
5891 len += 3; /* "," */
5892 }
5893 else
5894 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02005895#ifdef FEAT_MBYTE
5896 int charlen = (*mb_ptr2len)(p);
5897 len += charlen;
5898 p += charlen;
5899#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005900 ++len;
5901 ++p;
Bram Moolenaardfef1542012-07-10 19:25:10 +02005902#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005903 }
5904 }
5905
5906 buf = alloc(len + 1);
5907 if (buf == NULL)
5908 {
5909 *lenp = 0;
5910 return buf;
5911 }
5912
5913 p = arg;
5914 q = buf;
5915 *q++ = '"';
5916 while (*p)
5917 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00005918 if (p[0] == '\\' && p[1] == '\\')
5919 {
5920 *q++ = '\\';
5921 *q++ = '\\';
5922 p += 2;
5923 }
5924 else if (p[0] == '\\' && vim_iswhite(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005925 {
5926 *q++ = p[1];
5927 p += 2;
5928 }
5929 else if (*p == '\\' || *p == '"')
5930 {
5931 *q++ = '\\';
5932 *q++ = *p++;
5933 }
5934 else if (vim_iswhite(*p))
5935 {
5936 p = skipwhite(p);
5937 if (*p == NUL)
5938 break;
5939 *q++ = '"';
5940 *q++ = ',';
5941 *q++ = '"';
5942 }
5943 else
5944 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02005945 MB_COPY_CHAR(p, q);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005946 }
5947 }
5948 *q++ = '"';
5949 *q = 0;
5950
5951 *lenp = len;
5952 return buf;
5953}
5954
5955/*
5956 * Check for a <> code in a user command.
5957 * "code" points to the '<'. "len" the length of the <> (inclusive).
5958 * "buf" is where the result is to be added.
5959 * "split_buf" points to a buffer used for splitting, caller should free it.
5960 * "split_len" is the length of what "split_buf" contains.
5961 * Returns the length of the replacement, which has been added to "buf".
5962 * Returns -1 if there was no match, and only the "<" has been copied.
5963 */
5964 static size_t
5965uc_check_code(code, len, buf, cmd, eap, split_buf, split_len)
5966 char_u *code;
5967 size_t len;
5968 char_u *buf;
5969 ucmd_T *cmd; /* the user command we're expanding */
5970 exarg_T *eap; /* ex arguments */
5971 char_u **split_buf;
5972 size_t *split_len;
5973{
5974 size_t result = 0;
5975 char_u *p = code + 1;
5976 size_t l = len - 2;
5977 int quote = 0;
5978 enum { ct_ARGS, ct_BANG, ct_COUNT, ct_LINE1, ct_LINE2, ct_REGISTER,
5979 ct_LT, ct_NONE } type = ct_NONE;
5980
5981 if ((vim_strchr((char_u *)"qQfF", *p) != NULL) && p[1] == '-')
5982 {
5983 quote = (*p == 'q' || *p == 'Q') ? 1 : 2;
5984 p += 2;
5985 l -= 2;
5986 }
5987
Bram Moolenaar371d5402006-03-20 21:47:49 +00005988 ++l;
5989 if (l <= 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005990 type = ct_NONE;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005991 else if (STRNICMP(p, "args>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005992 type = ct_ARGS;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005993 else if (STRNICMP(p, "bang>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005994 type = ct_BANG;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005995 else if (STRNICMP(p, "count>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005996 type = ct_COUNT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005997 else if (STRNICMP(p, "line1>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005998 type = ct_LINE1;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005999 else if (STRNICMP(p, "line2>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006000 type = ct_LINE2;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006001 else if (STRNICMP(p, "lt>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006002 type = ct_LT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006003 else if (STRNICMP(p, "reg>", l) == 0 || STRNICMP(p, "register>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006004 type = ct_REGISTER;
6005
6006 switch (type)
6007 {
6008 case ct_ARGS:
6009 /* Simple case first */
6010 if (*eap->arg == NUL)
6011 {
6012 if (quote == 1)
6013 {
6014 result = 2;
6015 if (buf != NULL)
6016 STRCPY(buf, "''");
6017 }
6018 else
6019 result = 0;
6020 break;
6021 }
6022
6023 /* When specified there is a single argument don't split it.
6024 * Works for ":Cmd %" when % is "a b c". */
6025 if ((eap->argt & NOSPC) && quote == 2)
6026 quote = 1;
6027
6028 switch (quote)
6029 {
6030 case 0: /* No quoting, no splitting */
6031 result = STRLEN(eap->arg);
6032 if (buf != NULL)
6033 STRCPY(buf, eap->arg);
6034 break;
6035 case 1: /* Quote, but don't split */
6036 result = STRLEN(eap->arg) + 2;
6037 for (p = eap->arg; *p; ++p)
6038 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006039#ifdef FEAT_MBYTE
6040 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6041 /* DBCS can contain \ in a trail byte, skip the
6042 * double-byte character. */
6043 ++p;
6044 else
6045#endif
6046 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006047 ++result;
6048 }
6049
6050 if (buf != NULL)
6051 {
6052 *buf++ = '"';
6053 for (p = eap->arg; *p; ++p)
6054 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006055#ifdef FEAT_MBYTE
6056 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6057 /* DBCS can contain \ in a trail byte, copy the
6058 * double-byte character to avoid escaping. */
6059 *buf++ = *p++;
6060 else
6061#endif
6062 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006063 *buf++ = '\\';
6064 *buf++ = *p;
6065 }
6066 *buf = '"';
6067 }
6068
6069 break;
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006070 case 2: /* Quote and split (<f-args>) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006071 /* This is hard, so only do it once, and cache the result */
6072 if (*split_buf == NULL)
6073 *split_buf = uc_split_args(eap->arg, split_len);
6074
6075 result = *split_len;
6076 if (buf != NULL && result != 0)
6077 STRCPY(buf, *split_buf);
6078
6079 break;
6080 }
6081 break;
6082
6083 case ct_BANG:
6084 result = eap->forceit ? 1 : 0;
6085 if (quote)
6086 result += 2;
6087 if (buf != NULL)
6088 {
6089 if (quote)
6090 *buf++ = '"';
6091 if (eap->forceit)
6092 *buf++ = '!';
6093 if (quote)
6094 *buf = '"';
6095 }
6096 break;
6097
6098 case ct_LINE1:
6099 case ct_LINE2:
6100 case ct_COUNT:
6101 {
6102 char num_buf[20];
6103 long num = (type == ct_LINE1) ? eap->line1 :
6104 (type == ct_LINE2) ? eap->line2 :
6105 (eap->addr_count > 0) ? eap->line2 : cmd->uc_def;
6106 size_t num_len;
6107
6108 sprintf(num_buf, "%ld", num);
6109 num_len = STRLEN(num_buf);
6110 result = num_len;
6111
6112 if (quote)
6113 result += 2;
6114
6115 if (buf != NULL)
6116 {
6117 if (quote)
6118 *buf++ = '"';
6119 STRCPY(buf, num_buf);
6120 buf += num_len;
6121 if (quote)
6122 *buf = '"';
6123 }
6124
6125 break;
6126 }
6127
6128 case ct_REGISTER:
6129 result = eap->regname ? 1 : 0;
6130 if (quote)
6131 result += 2;
6132 if (buf != NULL)
6133 {
6134 if (quote)
6135 *buf++ = '\'';
6136 if (eap->regname)
6137 *buf++ = eap->regname;
6138 if (quote)
6139 *buf = '\'';
6140 }
6141 break;
6142
6143 case ct_LT:
6144 result = 1;
6145 if (buf != NULL)
6146 *buf = '<';
6147 break;
6148
6149 default:
6150 /* Not recognized: just copy the '<' and return -1. */
6151 result = (size_t)-1;
6152 if (buf != NULL)
6153 *buf = '<';
6154 break;
6155 }
6156
6157 return result;
6158}
6159
6160 static void
6161do_ucmd(eap)
6162 exarg_T *eap;
6163{
6164 char_u *buf;
6165 char_u *p;
6166 char_u *q;
6167
6168 char_u *start;
Bram Moolenaar25648a52009-02-21 19:37:46 +00006169 char_u *end = NULL;
Bram Moolenaara850a712009-01-28 14:42:59 +00006170 char_u *ksp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006171 size_t len, totlen;
6172
6173 size_t split_len = 0;
6174 char_u *split_buf = NULL;
6175 ucmd_T *cmd;
6176#ifdef FEAT_EVAL
6177 scid_T save_current_SID = current_SID;
6178#endif
6179
6180 if (eap->cmdidx == CMD_USER)
6181 cmd = USER_CMD(eap->useridx);
6182 else
6183 cmd = USER_CMD_GA(&curbuf->b_ucmds, eap->useridx);
6184
6185 /*
6186 * Replace <> in the command by the arguments.
Bram Moolenaara850a712009-01-28 14:42:59 +00006187 * First round: "buf" is NULL, compute length, allocate "buf".
6188 * Second round: copy result into "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006189 */
6190 buf = NULL;
6191 for (;;)
6192 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006193 p = cmd->uc_rep; /* source */
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00006194 q = buf; /* destination */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006195 totlen = 0;
Bram Moolenaara850a712009-01-28 14:42:59 +00006196
6197 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006198 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006199 start = vim_strchr(p, '<');
6200 if (start != NULL)
6201 end = vim_strchr(start + 1, '>');
6202 if (buf != NULL)
6203 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006204 for (ksp = p; *ksp != NUL && *ksp != K_SPECIAL; ++ksp)
6205 ;
6206 if (*ksp == K_SPECIAL
6207 && (start == NULL || ksp < start || end == NULL)
Bram Moolenaara850a712009-01-28 14:42:59 +00006208 && ((ksp[1] == KS_SPECIAL && ksp[2] == KE_FILLER)
6209# ifdef FEAT_GUI
6210 || (ksp[1] == KS_EXTRA && ksp[2] == (int)KE_CSI)
6211# endif
6212 ))
6213 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006214 /* K_SPECIAL has been put in the buffer as K_SPECIAL
Bram Moolenaara850a712009-01-28 14:42:59 +00006215 * KS_SPECIAL KE_FILLER, like for mappings, but
6216 * do_cmdline() doesn't handle that, so convert it back.
6217 * Also change K_SPECIAL KS_EXTRA KE_CSI into CSI. */
6218 len = ksp - p;
6219 if (len > 0)
6220 {
6221 mch_memmove(q, p, len);
6222 q += len;
6223 }
6224 *q++ = ksp[1] == KS_SPECIAL ? K_SPECIAL : CSI;
6225 p = ksp + 3;
6226 continue;
6227 }
6228 }
6229
6230 /* break if there no <item> is found */
6231 if (start == NULL || end == NULL)
6232 break;
6233
Bram Moolenaar071d4272004-06-13 20:20:40 +00006234 /* Include the '>' */
6235 ++end;
6236
6237 /* Take everything up to the '<' */
6238 len = start - p;
6239 if (buf == NULL)
6240 totlen += len;
6241 else
6242 {
6243 mch_memmove(q, p, len);
6244 q += len;
6245 }
6246
6247 len = uc_check_code(start, end - start, q, cmd, eap,
6248 &split_buf, &split_len);
6249 if (len == (size_t)-1)
6250 {
6251 /* no match, continue after '<' */
6252 p = start + 1;
6253 len = 1;
6254 }
6255 else
6256 p = end;
6257 if (buf == NULL)
6258 totlen += len;
6259 else
6260 q += len;
6261 }
6262 if (buf != NULL) /* second time here, finished */
6263 {
6264 STRCPY(q, p);
6265 break;
6266 }
6267
6268 totlen += STRLEN(p); /* Add on the trailing characters */
6269 buf = alloc((unsigned)(totlen + 1));
6270 if (buf == NULL)
6271 {
6272 vim_free(split_buf);
6273 return;
6274 }
6275 }
6276
6277#ifdef FEAT_EVAL
6278 current_SID = cmd->uc_scriptID;
6279#endif
6280 (void)do_cmdline(buf, eap->getline, eap->cookie,
6281 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
6282#ifdef FEAT_EVAL
6283 current_SID = save_current_SID;
6284#endif
6285 vim_free(buf);
6286 vim_free(split_buf);
6287}
6288
6289# if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6290 static char_u *
6291get_user_command_name(idx)
6292 int idx;
6293{
6294 return get_user_commands(NULL, idx - (int)CMD_SIZE);
6295}
6296
6297/*
6298 * Function given to ExpandGeneric() to obtain the list of user command names.
6299 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006300 char_u *
6301get_user_commands(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006302 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006303 int idx;
6304{
6305 if (idx < curbuf->b_ucmds.ga_len)
6306 return USER_CMD_GA(&curbuf->b_ucmds, idx)->uc_name;
6307 idx -= curbuf->b_ucmds.ga_len;
6308 if (idx < ucmds.ga_len)
6309 return USER_CMD(idx)->uc_name;
6310 return NULL;
6311}
6312
6313/*
6314 * Function given to ExpandGeneric() to obtain the list of user command
6315 * attributes.
6316 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006317 char_u *
6318get_user_cmd_flags(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006319 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006320 int idx;
6321{
6322 static char *user_cmd_flags[] =
6323 {"bang", "bar", "buffer", "complete", "count",
6324 "nargs", "range", "register"};
6325
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00006326 if (idx >= (int)(sizeof(user_cmd_flags) / sizeof(user_cmd_flags[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006327 return NULL;
6328 return (char_u *)user_cmd_flags[idx];
6329}
6330
6331/*
6332 * Function given to ExpandGeneric() to obtain the list of values for -nargs.
6333 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006334 char_u *
6335get_user_cmd_nargs(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006336 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006337 int idx;
6338{
6339 static char *user_cmd_nargs[] = {"0", "1", "*", "?", "+"};
6340
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00006341 if (idx >= (int)(sizeof(user_cmd_nargs) / sizeof(user_cmd_nargs[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006342 return NULL;
6343 return (char_u *)user_cmd_nargs[idx];
6344}
6345
6346/*
6347 * Function given to ExpandGeneric() to obtain the list of values for -complete.
6348 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006349 char_u *
6350get_user_cmd_complete(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006351 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006352 int idx;
6353{
6354 return (char_u *)command_complete[idx].name;
6355}
6356# endif /* FEAT_CMDL_COMPL */
6357
6358#endif /* FEAT_USR_CMDS */
6359
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006360#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
6361/*
6362 * Parse a completion argument "value[vallen]".
6363 * The detected completion goes in "*complp", argument type in "*argt".
6364 * When there is an argument, for function and user defined completion, it's
6365 * copied to allocated memory and stored in "*compl_arg".
6366 * Returns FAIL if something is wrong.
6367 */
6368 int
6369parse_compl_arg(value, vallen, complp, argt, compl_arg)
6370 char_u *value;
6371 int vallen;
6372 int *complp;
6373 long *argt;
6374 char_u **compl_arg;
6375{
6376 char_u *arg = NULL;
6377 size_t arglen = 0;
6378 int i;
6379 int valend = vallen;
6380
6381 /* Look for any argument part - which is the part after any ',' */
6382 for (i = 0; i < vallen; ++i)
6383 {
6384 if (value[i] == ',')
6385 {
6386 arg = &value[i + 1];
6387 arglen = vallen - i - 1;
6388 valend = i;
6389 break;
6390 }
6391 }
6392
6393 for (i = 0; command_complete[i].expand != 0; ++i)
6394 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006395 if ((int)STRLEN(command_complete[i].name) == valend
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006396 && STRNCMP(value, command_complete[i].name, valend) == 0)
6397 {
6398 *complp = command_complete[i].expand;
6399 if (command_complete[i].expand == EXPAND_BUFFERS)
6400 *argt |= BUFNAME;
6401 else if (command_complete[i].expand == EXPAND_DIRECTORIES
6402 || command_complete[i].expand == EXPAND_FILES)
6403 *argt |= XFILE;
6404 break;
6405 }
6406 }
6407
6408 if (command_complete[i].expand == 0)
6409 {
6410 EMSG2(_("E180: Invalid complete value: %s"), value);
6411 return FAIL;
6412 }
6413
6414# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6415 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST
6416 && arg != NULL)
6417# else
6418 if (arg != NULL)
6419# endif
6420 {
6421 EMSG(_("E468: Completion argument only allowed for custom completion"));
6422 return FAIL;
6423 }
6424
6425# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6426 if ((*complp == EXPAND_USER_DEFINED || *complp == EXPAND_USER_LIST)
6427 && arg == NULL)
6428 {
6429 EMSG(_("E467: Custom completion requires a function argument"));
6430 return FAIL;
6431 }
6432
6433 if (arg != NULL)
6434 *compl_arg = vim_strnsave(arg, (int)arglen);
6435# endif
6436 return OK;
6437}
6438#endif
6439
Bram Moolenaar071d4272004-06-13 20:20:40 +00006440 static void
6441ex_colorscheme(eap)
6442 exarg_T *eap;
6443{
Bram Moolenaare6850792010-05-14 15:28:44 +02006444 if (*eap->arg == NUL)
6445 {
6446#ifdef FEAT_EVAL
6447 char_u *expr = vim_strsave((char_u *)"g:colors_name");
6448 char_u *p = NULL;
6449
6450 if (expr != NULL)
6451 {
6452 ++emsg_off;
6453 p = eval_to_string(expr, NULL, FALSE);
6454 --emsg_off;
6455 vim_free(expr);
6456 }
6457 if (p != NULL)
6458 {
6459 MSG(p);
6460 vim_free(p);
6461 }
6462 else
6463 MSG("default");
6464#else
6465 MSG(_("unknown"));
6466#endif
6467 }
6468 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarbe1e9e92012-09-18 16:47:07 +02006469 EMSG2(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006470}
6471
6472 static void
6473ex_highlight(eap)
6474 exarg_T *eap;
6475{
6476 if (*eap->arg == NUL && eap->cmd[2] == '!')
6477 MSG(_("Greetings, Vim user!"));
6478 do_highlight(eap->arg, eap->forceit, FALSE);
6479}
6480
6481
6482/*
6483 * Call this function if we thought we were going to exit, but we won't
6484 * (because of an error). May need to restore the terminal mode.
6485 */
6486 void
6487not_exiting()
6488{
6489 exiting = FALSE;
6490 settmode(TMODE_RAW);
6491}
6492
6493/*
6494 * ":quit": quit current window, quit Vim if closed the last window.
6495 */
6496 static void
6497ex_quit(eap)
6498 exarg_T *eap;
6499{
6500#ifdef FEAT_CMDWIN
6501 if (cmdwin_type != 0)
6502 {
6503 cmdwin_result = Ctrl_C;
6504 return;
6505 }
6506#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006507 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006508 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006509 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006510 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006511 return;
6512 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006513#ifdef FEAT_AUTOCMD
Bram Moolenaar3b53dfb2012-06-06 18:03:07 +02006514 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar362ce482012-06-06 19:02:45 +02006515 /* Refuse to quick when locked or when the buffer in the last window is
6516 * being closed (can only happen in autocommands). */
6517 if (curbuf_locked() || (curbuf->b_nwindows == 1 && curbuf->b_closing))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006518 return;
6519#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006520
6521#ifdef FEAT_NETBEANS_INTG
6522 netbeansForcedQuit = eap->forceit;
6523#endif
6524
6525 /*
6526 * If there are more files or windows we won't exit.
6527 */
6528 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6529 exiting = TRUE;
6530 if ((!P_HID(curbuf)
6531 && check_changed(curbuf, p_awa, FALSE, eap->forceit, FALSE))
6532 || check_more(TRUE, eap->forceit) == FAIL
6533 || (only_one_window() && check_changed_any(eap->forceit)))
6534 {
6535 not_exiting();
6536 }
6537 else
6538 {
6539#ifdef FEAT_WINDOWS
6540 if (only_one_window()) /* quit last window */
6541#endif
6542 getout(0);
6543#ifdef FEAT_WINDOWS
6544# ifdef FEAT_GUI
6545 need_mouse_correct = TRUE;
6546# endif
6547 /* close window; may free buffer */
6548 win_close(curwin, !P_HID(curwin->w_buffer) || eap->forceit);
6549#endif
6550 }
6551}
6552
6553/*
6554 * ":cquit".
6555 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006556 static void
6557ex_cquit(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006558 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006559{
6560 getout(1); /* this does not always pass on the exit code to the Manx
6561 compiler. why? */
6562}
6563
6564/*
6565 * ":qall": try to quit all windows
6566 */
6567 static void
6568ex_quit_all(eap)
6569 exarg_T *eap;
6570{
6571# ifdef FEAT_CMDWIN
6572 if (cmdwin_type != 0)
6573 {
6574 if (eap->forceit)
6575 cmdwin_result = K_XF1; /* ex_window() takes care of this */
6576 else
6577 cmdwin_result = K_XF2;
6578 return;
6579 }
6580# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006581
6582 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006583 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006584 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006585 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006586 return;
6587 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006588#ifdef FEAT_AUTOCMD
6589 if (curbuf_locked())
6590 return;
6591#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006592
Bram Moolenaar071d4272004-06-13 20:20:40 +00006593 exiting = TRUE;
6594 if (eap->forceit || !check_changed_any(FALSE))
6595 getout(0);
6596 not_exiting();
6597}
6598
Bram Moolenaard9967712006-03-11 21:18:15 +00006599#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006600/*
6601 * ":close": close current window, unless it is the last one
6602 */
6603 static void
6604ex_close(eap)
6605 exarg_T *eap;
6606{
6607# ifdef FEAT_CMDWIN
6608 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02006609 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006610 else
6611# endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006612 if (!text_locked()
6613#ifdef FEAT_AUTOCMD
6614 && !curbuf_locked()
6615#endif
6616 )
Bram Moolenaarf740b292006-02-16 22:11:02 +00006617 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006618}
6619
Bram Moolenaard9967712006-03-11 21:18:15 +00006620# ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006621/*
6622 * ":pclose": Close any preview window.
6623 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006624 static void
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006625ex_pclose(eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006626 exarg_T *eap;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006627{
6628 win_T *win;
6629
6630 for (win = firstwin; win != NULL; win = win->w_next)
6631 if (win->w_p_pvw)
6632 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006633 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006634 break;
6635 }
6636}
Bram Moolenaard9967712006-03-11 21:18:15 +00006637# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006638
Bram Moolenaarf740b292006-02-16 22:11:02 +00006639/*
6640 * Close window "win" and take care of handling closing the last window for a
6641 * modified buffer.
6642 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006643 static void
Bram Moolenaarf740b292006-02-16 22:11:02 +00006644ex_win_close(forceit, win, tp)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006645 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006646 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006647 tabpage_T *tp; /* NULL or the tab page "win" is in */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006648{
6649 int need_hide;
6650 buf_T *buf = win->w_buffer;
6651
6652 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006653 if (need_hide && !P_HID(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006654 {
Bram Moolenaard9967712006-03-11 21:18:15 +00006655# if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006656 if ((p_confirm || cmdmod.confirm) && p_write)
6657 {
6658 dialog_changed(buf, FALSE);
6659 if (buf_valid(buf) && bufIsChanged(buf))
6660 return;
6661 need_hide = FALSE;
6662 }
6663 else
Bram Moolenaard9967712006-03-11 21:18:15 +00006664# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006665 {
6666 EMSG(_(e_nowrtmsg));
6667 return;
6668 }
6669 }
6670
Bram Moolenaard9967712006-03-11 21:18:15 +00006671# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006672 need_mouse_correct = TRUE;
Bram Moolenaard9967712006-03-11 21:18:15 +00006673# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006674
Bram Moolenaar071d4272004-06-13 20:20:40 +00006675 /* free buffer when not hiding it or when it's a scratch buffer */
Bram Moolenaarf740b292006-02-16 22:11:02 +00006676 if (tp == NULL)
6677 win_close(win, !need_hide && !P_HID(buf));
6678 else
6679 win_close_othertab(win, !need_hide && !P_HID(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006680}
6681
Bram Moolenaar071d4272004-06-13 20:20:40 +00006682/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006683 * ":tabclose": close current tab page, unless it is the last one.
6684 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006685 */
6686 static void
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006687ex_tabclose(eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006688 exarg_T *eap;
6689{
Bram Moolenaarf740b292006-02-16 22:11:02 +00006690 tabpage_T *tp;
6691
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006692# ifdef FEAT_CMDWIN
6693 if (cmdwin_type != 0)
6694 cmdwin_result = K_IGNORE;
6695 else
6696# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006697 if (first_tabpage->tp_next == NULL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00006698 EMSG(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00006699 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006700 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006701 if (eap->addr_count > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006702 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006703 tp = find_tabpage((int)eap->line2);
6704 if (tp == NULL)
6705 {
6706 beep_flush();
6707 return;
6708 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00006709 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006710 {
6711 tabpage_close_other(tp, eap->forceit);
6712 return;
6713 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006714 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006715 if (!text_locked()
6716#ifdef FEAT_AUTOCMD
6717 && !curbuf_locked()
6718#endif
6719 )
Bram Moolenaarf740b292006-02-16 22:11:02 +00006720 tabpage_close(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006721 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006722}
6723
6724/*
6725 * ":tabonly": close all tab pages except the current one
6726 */
6727 static void
6728ex_tabonly(eap)
6729 exarg_T *eap;
6730{
6731 tabpage_T *tp;
6732 int done;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006733
6734# ifdef FEAT_CMDWIN
6735 if (cmdwin_type != 0)
6736 cmdwin_result = K_IGNORE;
6737 else
6738# endif
6739 if (first_tabpage->tp_next == NULL)
6740 MSG(_("Already only one tab page"));
6741 else
6742 {
6743 /* Repeat this up to a 1000 times, because autocommands may mess
6744 * up the lists. */
6745 for (done = 0; done < 1000; ++done)
6746 {
6747 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6748 if (tp->tp_topframe != topframe)
6749 {
6750 tabpage_close_other(tp, eap->forceit);
6751 /* if we failed to close it quit */
6752 if (valid_tabpage(tp))
6753 done = 1000;
6754 /* start over, "tp" is now invalid */
6755 break;
6756 }
6757 if (first_tabpage->tp_next == NULL)
6758 break;
6759 }
6760 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006761}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006762
6763/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006764 * Close the current tab page.
6765 */
6766 void
6767tabpage_close(forceit)
6768 int forceit;
6769{
6770 /* First close all the windows but the current one. If that worked then
6771 * close the last window in this tab, that will close it. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006772 if (lastwin != firstwin)
6773 close_others(TRUE, forceit);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006774 if (lastwin == firstwin)
6775 ex_win_close(forceit, curwin, NULL);
6776# ifdef FEAT_GUI
6777 need_mouse_correct = TRUE;
6778# endif
6779}
6780
6781/*
6782 * Close tab page "tp", which is not the current tab page.
6783 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006784 * Also takes care of the tab pages line disappearing when closing the
6785 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00006786 */
6787 void
6788tabpage_close_other(tp, forceit)
6789 tabpage_T *tp;
6790 int forceit;
6791{
6792 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006793 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006794 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006795
6796 /* Limit to 1000 windows, autocommands may add a window while we close
6797 * one. OK, so I'm paranoid... */
6798 while (++done < 1000)
6799 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006800 wp = tp->tp_firstwin;
6801 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006802
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006803 /* Autocommands may delete the tab page under our fingers and we may
6804 * fail to close a window with a modified buffer. */
6805 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006806 break;
6807 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006808
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006809 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006810 if (h != tabline_height())
6811 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006812}
6813
6814/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006815 * ":only".
6816 */
6817 static void
6818ex_only(eap)
6819 exarg_T *eap;
6820{
6821# ifdef FEAT_GUI
6822 need_mouse_correct = TRUE;
6823# endif
6824 close_others(TRUE, eap->forceit);
6825}
6826
6827/*
6828 * ":all" and ":sall".
Bram Moolenaard9967712006-03-11 21:18:15 +00006829 * Also used for ":tab drop file ..." after setting the argument list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006830 */
Bram Moolenaard9967712006-03-11 21:18:15 +00006831 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00006832ex_all(eap)
6833 exarg_T *eap;
6834{
6835 if (eap->addr_count == 0)
6836 eap->line2 = 9999;
Bram Moolenaard9967712006-03-11 21:18:15 +00006837 do_arg_all((int)eap->line2, eap->forceit, eap->cmdidx == CMD_drop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006838}
6839#endif /* FEAT_WINDOWS */
6840
6841 static void
6842ex_hide(eap)
6843 exarg_T *eap;
6844{
6845 if (*eap->arg != NUL && check_nextcmd(eap->arg) == NULL)
6846 eap->errmsg = e_invarg;
6847 else
6848 {
6849 /* ":hide" or ":hide | cmd": hide current window */
6850 eap->nextcmd = check_nextcmd(eap->arg);
6851#ifdef FEAT_WINDOWS
6852 if (!eap->skip)
6853 {
6854# ifdef FEAT_GUI
6855 need_mouse_correct = TRUE;
6856# endif
6857 win_close(curwin, FALSE); /* don't free buffer */
6858 }
6859#endif
6860 }
6861}
6862
6863/*
6864 * ":stop" and ":suspend": Suspend Vim.
6865 */
6866 static void
6867ex_stop(eap)
6868 exarg_T *eap;
6869{
6870 /*
6871 * Disallow suspending for "rvim".
6872 */
6873 if (!check_restricted()
6874#ifdef WIN3264
6875 /*
6876 * Check if external commands are allowed now.
6877 */
6878 && can_end_termcap_mode(TRUE)
6879#endif
6880 )
6881 {
6882 if (!eap->forceit)
6883 autowrite_all();
6884 windgoto((int)Rows - 1, 0);
6885 out_char('\n');
6886 out_flush();
6887 stoptermcap();
6888 out_flush(); /* needed for SUN to restore xterm buffer */
6889#ifdef FEAT_TITLE
6890 mch_restore_title(3); /* restore window titles */
6891#endif
6892 ui_suspend(); /* call machine specific function */
6893#ifdef FEAT_TITLE
6894 maketitle();
6895 resettitle(); /* force updating the title */
6896#endif
6897 starttermcap();
6898 scroll_start(); /* scroll screen before redrawing */
6899 redraw_later_clear();
6900 shell_resized(); /* may have resized window */
6901 }
6902}
6903
6904/*
6905 * ":exit", ":xit" and ":wq": Write file and exit Vim.
6906 */
6907 static void
6908ex_exit(eap)
6909 exarg_T *eap;
6910{
6911#ifdef FEAT_CMDWIN
6912 if (cmdwin_type != 0)
6913 {
6914 cmdwin_result = Ctrl_C;
6915 return;
6916 }
6917#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006918 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006919 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006920 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006921 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006922 return;
6923 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006924#ifdef FEAT_AUTOCMD
6925 if (curbuf_locked())
6926 return;
6927#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006928
6929 /*
6930 * if more files or windows we won't exit
6931 */
6932 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6933 exiting = TRUE;
6934 if ( ((eap->cmdidx == CMD_wq
6935 || curbufIsChanged())
6936 && do_write(eap) == FAIL)
6937 || check_more(TRUE, eap->forceit) == FAIL
6938 || (only_one_window() && check_changed_any(eap->forceit)))
6939 {
6940 not_exiting();
6941 }
6942 else
6943 {
6944#ifdef FEAT_WINDOWS
6945 if (only_one_window()) /* quit last window, exit Vim */
6946#endif
6947 getout(0);
6948#ifdef FEAT_WINDOWS
6949# ifdef FEAT_GUI
6950 need_mouse_correct = TRUE;
6951# endif
6952 /* quit current window, may free buffer */
6953 win_close(curwin, !P_HID(curwin->w_buffer));
6954#endif
6955 }
6956}
6957
6958/*
6959 * ":print", ":list", ":number".
6960 */
6961 static void
6962ex_print(eap)
6963 exarg_T *eap;
6964{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006965 if (curbuf->b_ml.ml_flags & ML_EMPTY)
6966 EMSG(_(e_emptybuf));
6967 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006968 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00006969 for ( ;!got_int; ui_breakcheck())
6970 {
6971 print_line(eap->line1,
6972 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
6973 || (eap->flags & EXFLAG_NR)),
6974 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
6975 if (++eap->line1 > eap->line2)
6976 break;
6977 out_flush(); /* show one line at a time */
6978 }
6979 setpcmark();
6980 /* put cursor at last line */
6981 curwin->w_cursor.lnum = eap->line2;
6982 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006983 }
6984
Bram Moolenaar071d4272004-06-13 20:20:40 +00006985 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006986}
6987
6988#ifdef FEAT_BYTEOFF
6989 static void
6990ex_goto(eap)
6991 exarg_T *eap;
6992{
6993 goto_byte(eap->line2);
6994}
6995#endif
6996
6997/*
6998 * ":shell".
6999 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007000 static void
7001ex_shell(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007002 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007003{
7004 do_shell(NULL, 0);
7005}
7006
7007#if (defined(FEAT_WINDOWS) && defined(HAVE_DROP_FILE)) \
7008 || (defined(FEAT_GUI_GTK) && defined(FEAT_DND)) \
Bram Moolenaar371baa92005-12-29 22:43:53 +00007009 || defined(FEAT_GUI_MSWIN) \
7010 || defined(FEAT_GUI_MAC) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00007011 || defined(PROTO)
7012
7013/*
7014 * Handle a file drop. The code is here because a drop is *nearly* like an
7015 * :args command, but not quite (we have a list of exact filenames, so we
7016 * don't want to (a) parse a command line, or (b) expand wildcards. So the
7017 * code is very similar to :args and hence needs access to a lot of the static
7018 * functions in this file.
7019 *
7020 * The list should be allocated using alloc(), as should each item in the
7021 * list. This function takes over responsibility for freeing the list.
7022 *
Bram Moolenaar81870892007-11-11 18:17:28 +00007023 * XXX The list is made into the argument list. This is freed using
Bram Moolenaar071d4272004-06-13 20:20:40 +00007024 * FreeWild(), which does a series of vim_free() calls, unless the two defines
7025 * __EMX__ and __ALWAYS_HAS_TRAILING_NUL_POINTER are set. In this case, a
7026 * routine _fnexplodefree() is used. This may cause problems, but as the drop
7027 * file functionality is (currently) not in EMX this is not presently a
7028 * problem.
7029 */
7030 void
7031handle_drop(filec, filev, split)
7032 int filec; /* the number of files dropped */
7033 char_u **filev; /* the list of files dropped */
7034 int split; /* force splitting the window */
7035{
7036 exarg_T ea;
7037 int save_msg_scroll = msg_scroll;
7038
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007039 /* Postpone this while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007040 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007041 return;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007042#ifdef FEAT_AUTOCMD
7043 if (curbuf_locked())
7044 return;
7045#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00007046 /* When the screen is being updated we should not change buffers and
7047 * windows structures, it may cause freed memory to be used. */
7048 if (updating_screen)
7049 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007050
7051 /* Check whether the current buffer is changed. If so, we will need
7052 * to split the current window or data could be lost.
7053 * We don't need to check if the 'hidden' option is set, as in this
7054 * case the buffer won't be lost.
7055 */
7056 if (!P_HID(curbuf) && !split)
7057 {
7058 ++emsg_off;
7059 split = check_changed(curbuf, TRUE, FALSE, FALSE, FALSE);
7060 --emsg_off;
7061 }
7062 if (split)
7063 {
7064# ifdef FEAT_WINDOWS
7065 if (win_split(0, 0) == FAIL)
7066 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007067 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007068
7069 /* When splitting the window, create a new alist. Otherwise the
7070 * existing one is overwritten. */
7071 alist_unlink(curwin->w_alist);
7072 alist_new();
7073# else
7074 return; /* can't split, always fail */
7075# endif
7076 }
7077
7078 /*
7079 * Set up the new argument list.
7080 */
Bram Moolenaar86b68352004-12-27 21:59:20 +00007081 alist_set(ALIST(curwin), filec, filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007082
7083 /*
7084 * Move to the first file.
7085 */
7086 /* Fake up a minimal "next" command for do_argfile() */
7087 vim_memset(&ea, 0, sizeof(ea));
7088 ea.cmd = (char_u *)"next";
7089 do_argfile(&ea, 0);
7090
7091 /* do_ecmd() may set need_start_insertmode, but since we never left Insert
7092 * mode that is not needed here. */
7093 need_start_insertmode = FALSE;
7094
7095 /* Restore msg_scroll, otherwise a following command may cause scrolling
7096 * unexpectedly. The screen will be redrawn by the caller, thus
7097 * msg_scroll being set by displaying a message is irrelevant. */
7098 msg_scroll = save_msg_scroll;
7099}
7100#endif
7101
Bram Moolenaar071d4272004-06-13 20:20:40 +00007102/*
7103 * Clear an argument list: free all file names and reset it to zero entries.
7104 */
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007105 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00007106alist_clear(al)
7107 alist_T *al;
7108{
7109 while (--al->al_ga.ga_len >= 0)
7110 vim_free(AARGLIST(al)[al->al_ga.ga_len].ae_fname);
7111 ga_clear(&al->al_ga);
7112}
7113
7114/*
7115 * Init an argument list.
7116 */
7117 void
7118alist_init(al)
7119 alist_T *al;
7120{
7121 ga_init2(&al->al_ga, (int)sizeof(aentry_T), 5);
7122}
7123
7124#if defined(FEAT_WINDOWS) || defined(PROTO)
7125
7126/*
7127 * Remove a reference from an argument list.
7128 * Ignored when the argument list is the global one.
7129 * If the argument list is no longer used by any window, free it.
7130 */
7131 void
7132alist_unlink(al)
7133 alist_T *al;
7134{
7135 if (al != &global_alist && --al->al_refcount <= 0)
7136 {
7137 alist_clear(al);
7138 vim_free(al);
7139 }
7140}
7141
7142# if defined(FEAT_LISTCMDS) || defined(HAVE_DROP_FILE) || defined(PROTO)
7143/*
7144 * Create a new argument list and use it for the current window.
7145 */
7146 void
7147alist_new()
7148{
7149 curwin->w_alist = (alist_T *)alloc((unsigned)sizeof(alist_T));
7150 if (curwin->w_alist == NULL)
7151 {
7152 curwin->w_alist = &global_alist;
7153 ++global_alist.al_refcount;
7154 }
7155 else
7156 {
7157 curwin->w_alist->al_refcount = 1;
7158 alist_init(curwin->w_alist);
7159 }
7160}
7161# endif
7162#endif
7163
7164#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE) || defined(PROTO)
7165/*
7166 * Expand the file names in the global argument list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00007167 * If "fnum_list" is not NULL, use "fnum_list[fnum_len]" as a list of buffer
7168 * numbers to be re-used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007169 */
7170 void
Bram Moolenaar86b68352004-12-27 21:59:20 +00007171alist_expand(fnum_list, fnum_len)
7172 int *fnum_list;
7173 int fnum_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007174{
7175 char_u **old_arg_files;
Bram Moolenaar86b68352004-12-27 21:59:20 +00007176 int old_arg_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007177 char_u **new_arg_files;
7178 int new_arg_file_count;
7179 char_u *save_p_su = p_su;
7180 int i;
7181
7182 /* Don't use 'suffixes' here. This should work like the shell did the
7183 * expansion. Also, the vimrc file isn't read yet, thus the user
7184 * can't set the options. */
7185 p_su = empty_option;
7186 old_arg_files = (char_u **)alloc((unsigned)(sizeof(char_u *) * GARGCOUNT));
7187 if (old_arg_files != NULL)
7188 {
7189 for (i = 0; i < GARGCOUNT; ++i)
Bram Moolenaar86b68352004-12-27 21:59:20 +00007190 old_arg_files[i] = vim_strsave(GARGLIST[i].ae_fname);
7191 old_arg_count = GARGCOUNT;
7192 if (expand_wildcards(old_arg_count, old_arg_files,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007193 &new_arg_file_count, &new_arg_files,
Bram Moolenaarb5609832011-07-20 15:04:58 +02007194 EW_FILE|EW_NOTFOUND|EW_ADDSLASH|EW_NOERROR) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007195 && new_arg_file_count > 0)
7196 {
Bram Moolenaar86b68352004-12-27 21:59:20 +00007197 alist_set(&global_alist, new_arg_file_count, new_arg_files,
7198 TRUE, fnum_list, fnum_len);
7199 FreeWild(old_arg_count, old_arg_files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007200 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007201 }
7202 p_su = save_p_su;
7203}
7204#endif
7205
7206/*
7207 * Set the argument list for the current window.
7208 * Takes over the allocated files[] and the allocated fnames in it.
7209 */
7210 void
Bram Moolenaar86b68352004-12-27 21:59:20 +00007211alist_set(al, count, files, use_curbuf, fnum_list, fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007212 alist_T *al;
7213 int count;
7214 char_u **files;
7215 int use_curbuf;
Bram Moolenaar86b68352004-12-27 21:59:20 +00007216 int *fnum_list;
7217 int fnum_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007218{
7219 int i;
7220
7221 alist_clear(al);
7222 if (ga_grow(&al->al_ga, count) == OK)
7223 {
7224 for (i = 0; i < count; ++i)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007225 {
7226 if (got_int)
7227 {
7228 /* When adding many buffers this can take a long time. Allow
7229 * interrupting here. */
7230 while (i < count)
7231 vim_free(files[i++]);
7232 break;
7233 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00007234
7235 /* May set buffer name of a buffer previously used for the
7236 * argument list, so that it's re-used by alist_add. */
7237 if (fnum_list != NULL && i < fnum_len)
7238 buf_set_name(fnum_list[i], files[i]);
7239
Bram Moolenaar071d4272004-06-13 20:20:40 +00007240 alist_add(al, files[i], use_curbuf ? 2 : 1);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007241 ui_breakcheck();
7242 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007243 vim_free(files);
7244 }
7245 else
7246 FreeWild(count, files);
7247#ifdef FEAT_WINDOWS
7248 if (al == &global_alist)
7249#endif
7250 arg_had_last = FALSE;
7251}
7252
7253/*
7254 * Add file "fname" to argument list "al".
7255 * "fname" must have been allocated and "al" must have been checked for room.
7256 */
7257 void
7258alist_add(al, fname, set_fnum)
7259 alist_T *al;
7260 char_u *fname;
7261 int set_fnum; /* 1: set buffer number; 2: re-use curbuf */
7262{
7263 if (fname == NULL) /* don't add NULL file names */
7264 return;
7265#ifdef BACKSLASH_IN_FILENAME
7266 slash_adjust(fname);
7267#endif
7268 AARGLIST(al)[al->al_ga.ga_len].ae_fname = fname;
7269 if (set_fnum > 0)
7270 AARGLIST(al)[al->al_ga.ga_len].ae_fnum =
7271 buflist_add(fname, BLN_LISTED | (set_fnum == 2 ? BLN_CURBUF : 0));
7272 ++al->al_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007273}
7274
7275#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
7276/*
7277 * Adjust slashes in file names. Called after 'shellslash' was set.
7278 */
7279 void
7280alist_slash_adjust()
7281{
7282 int i;
7283# ifdef FEAT_WINDOWS
7284 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007285 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007286# endif
7287
7288 for (i = 0; i < GARGCOUNT; ++i)
7289 if (GARGLIST[i].ae_fname != NULL)
7290 slash_adjust(GARGLIST[i].ae_fname);
7291# ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00007292 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007293 if (wp->w_alist != &global_alist)
7294 for (i = 0; i < WARGCOUNT(wp); ++i)
7295 if (WARGLIST(wp)[i].ae_fname != NULL)
7296 slash_adjust(WARGLIST(wp)[i].ae_fname);
7297# endif
7298}
7299#endif
7300
7301/*
7302 * ":preserve".
7303 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007304 static void
7305ex_preserve(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007306 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007307{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007308 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007309 ml_preserve(curbuf, TRUE);
7310}
7311
7312/*
7313 * ":recover".
7314 */
7315 static void
7316ex_recover(eap)
7317 exarg_T *eap;
7318{
7319 /* Set recoverymode right away to avoid the ATTENTION prompt. */
7320 recoverymode = TRUE;
7321 if (!check_changed(curbuf, p_awa, TRUE, eap->forceit, FALSE)
7322 && (*eap->arg == NUL
7323 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
7324 ml_recover();
7325 recoverymode = FALSE;
7326}
7327
7328/*
7329 * Command modifier used in a wrong way.
7330 */
7331 static void
7332ex_wrongmodifier(eap)
7333 exarg_T *eap;
7334{
7335 eap->errmsg = e_invcmd;
7336}
7337
7338#ifdef FEAT_WINDOWS
7339/*
7340 * :sview [+command] file split window with new file, read-only
7341 * :split [[+command] file] split window with current or new file
7342 * :vsplit [[+command] file] split window vertically with current or new file
7343 * :new [[+command] file] split window with no or new file
7344 * :vnew [[+command] file] split vertically window with no or new file
7345 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007346 *
7347 * :tabedit open new Tab page with empty window
7348 * :tabedit [+command] file open new Tab page and edit "file"
7349 * :tabnew [[+command] file] just like :tabedit
7350 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007351 */
7352 void
7353ex_splitview(eap)
7354 exarg_T *eap;
7355{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007356 win_T *old_curwin = curwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007357# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007358 char_u *fname = NULL;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007359# endif
7360# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007361 int browse_flag = cmdmod.browse;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007362# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007363
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007364# ifndef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007365 if (eap->cmdidx == CMD_vsplit || eap->cmdidx == CMD_vnew)
7366 {
7367 ex_ni(eap);
7368 return;
7369 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007370# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007371
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007372# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007373 need_mouse_correct = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007374# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007375
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007376# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00007377 /* A ":split" in the quickfix window works like ":new". Don't want two
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007378 * quickfix windows. But it's OK when doing ":tab split". */
7379 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007380 {
7381 if (eap->cmdidx == CMD_split)
7382 eap->cmdidx = CMD_new;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007383# ifdef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007384 if (eap->cmdidx == CMD_vsplit)
7385 eap->cmdidx = CMD_vnew;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007386# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007387 }
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_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007391 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007392 {
7393 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
7394 FNAME_MESS, TRUE, curbuf->b_ffname);
7395 if (fname == NULL)
7396 goto theend;
7397 eap->arg = fname;
7398 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007399# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007400 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007401# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007402# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007403# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007404 if (cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007405# ifdef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007406 && eap->cmdidx != CMD_vnew
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007407# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007408 && eap->cmdidx != CMD_new)
7409 {
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007410# ifdef FEAT_AUTOCMD
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007411 if (
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007412# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007413 !gui.in_use &&
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007414# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007415 au_has_group((char_u *)"FileExplorer"))
7416 {
7417 /* No browsing supported but we do have the file explorer:
7418 * Edit the directory. */
7419 if (*eap->arg == NUL || !mch_isdir(eap->arg))
7420 eap->arg = (char_u *)".";
7421 }
7422 else
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007423# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007424 {
7425 fname = do_browse(0, (char_u *)_("Edit File in new window"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007426 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007427 if (fname == NULL)
7428 goto theend;
7429 eap->arg = fname;
7430 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007431 }
7432 cmdmod.browse = FALSE; /* Don't browse again in do_ecmd(). */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007433# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007434
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007435 /*
7436 * Either open new tab page or split the window.
7437 */
7438 if (eap->cmdidx == CMD_tabedit
7439 || eap->cmdidx == CMD_tabfind
7440 || eap->cmdidx == CMD_tabnew)
7441 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00007442 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
7443 : eap->addr_count == 0 ? 0
7444 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007445 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00007446 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007447
7448 /* set the alternate buffer for the window we came from */
7449 if (curwin != old_curwin
7450 && win_valid(old_curwin)
7451 && old_curwin->w_buffer != curbuf
7452 && !cmdmod.keepalt)
7453 old_curwin->w_alt_fnum = curbuf->b_fnum;
7454 }
7455 }
7456 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007457 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
7458 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007459# ifdef FEAT_SCROLLBIND
Bram Moolenaar071d4272004-06-13 20:20:40 +00007460 /* Reset 'scrollbind' when editing another file, but keep it when
7461 * doing ":split" without arguments. */
7462 if (*eap->arg != NUL
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007463# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007464 || cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007465# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007466 )
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007467 {
7468 RESET_BINDING(curwin);
7469 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007470 else
7471 do_check_scrollbind(FALSE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007472# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007473 do_exedit(eap, old_curwin);
7474 }
7475
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007476# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007477 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007478# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007479
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007480# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007481theend:
7482 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007483# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007484}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007485
7486/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007487 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007488 */
7489 void
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007490tabpage_new()
7491{
7492 exarg_T ea;
7493
7494 vim_memset(&ea, 0, sizeof(ea));
7495 ea.cmdidx = CMD_tabnew;
7496 ea.cmd = (char_u *)"tabn";
7497 ea.arg = (char_u *)"";
7498 ex_splitview(&ea);
7499}
7500
7501/*
7502 * :tabnext command
7503 */
7504 static void
7505ex_tabnext(eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007506 exarg_T *eap;
7507{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007508 switch (eap->cmdidx)
7509 {
7510 case CMD_tabfirst:
7511 case CMD_tabrewind:
7512 goto_tabpage(1);
7513 break;
7514 case CMD_tablast:
7515 goto_tabpage(9999);
7516 break;
7517 case CMD_tabprevious:
7518 case CMD_tabNext:
7519 goto_tabpage(eap->addr_count == 0 ? -1 : -(int)eap->line2);
7520 break;
7521 default: /* CMD_tabnext */
7522 goto_tabpage(eap->addr_count == 0 ? 0 : (int)eap->line2);
7523 break;
7524 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007525}
7526
7527/*
7528 * :tabmove command
7529 */
7530 static void
7531ex_tabmove(eap)
7532 exarg_T *eap;
7533{
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02007534 int tab_number = 9999;
7535
7536 if (eap->arg && *eap->arg != NUL)
7537 {
7538 char_u *p = eap->arg;
7539 int relative = 0; /* argument +N/-N means: move N places to the
7540 * right/left relative to the current position. */
7541
7542 if (*eap->arg == '-')
7543 {
7544 relative = -1;
7545 p = eap->arg + 1;
7546 }
7547 else if (*eap->arg == '+')
7548 {
7549 relative = 1;
7550 p = eap->arg + 1;
7551 }
7552 else
7553 p = eap->arg;
7554
7555 if (p == skipdigits(p))
7556 {
7557 /* No numbers as argument. */
7558 eap->errmsg = e_invarg;
7559 return;
7560 }
7561
7562 tab_number = getdigits(&p);
7563 if (relative != 0)
7564 tab_number = tab_number * relative + tabpage_index(curtab) - 1;;
7565 }
7566 else if (eap->addr_count != 0)
7567 tab_number = eap->line2;
7568
7569 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007570}
7571
7572/*
7573 * :tabs command: List tabs and their contents.
7574 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007575 static void
7576ex_tabs(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007577 exarg_T *eap UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007578{
7579 tabpage_T *tp;
7580 win_T *wp;
7581 int tabcount = 1;
7582
7583 msg_start();
7584 msg_scroll = TRUE;
7585 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
7586 {
7587 msg_putchar('\n');
7588 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
7589 msg_outtrans_attr(IObuff, hl_attr(HLF_T));
7590 out_flush(); /* output one line at a time */
7591 ui_breakcheck();
7592
Bram Moolenaar030f0df2006-02-21 22:02:53 +00007593 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007594 wp = firstwin;
7595 else
7596 wp = tp->tp_firstwin;
7597 for ( ; wp != NULL && !got_int; wp = wp->w_next)
7598 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007599 msg_putchar('\n');
7600 msg_putchar(wp == curwin ? '>' : ' ');
7601 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007602 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
7603 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007604 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02007605 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007606 else
7607 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
7608 IObuff, IOSIZE, TRUE);
7609 msg_outtrans(IObuff);
7610 out_flush(); /* output one line at a time */
7611 ui_breakcheck();
7612 }
7613 }
7614}
7615
7616#endif /* FEAT_WINDOWS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007617
7618/*
7619 * ":mode": Set screen mode.
7620 * If no argument given, just get the screen size and redraw.
7621 */
7622 static void
7623ex_mode(eap)
7624 exarg_T *eap;
7625{
7626 if (*eap->arg == NUL)
7627 shell_resized();
7628 else
7629 mch_screenmode(eap->arg);
7630}
7631
7632#ifdef FEAT_WINDOWS
7633/*
7634 * ":resize".
7635 * set, increment or decrement current window height
7636 */
7637 static void
7638ex_resize(eap)
7639 exarg_T *eap;
7640{
7641 int n;
7642 win_T *wp = curwin;
7643
7644 if (eap->addr_count > 0)
7645 {
7646 n = eap->line2;
7647 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
7648 ;
7649 }
7650
7651#ifdef FEAT_GUI
7652 need_mouse_correct = TRUE;
7653#endif
7654 n = atol((char *)eap->arg);
7655#ifdef FEAT_VERTSPLIT
7656 if (cmdmod.split & WSP_VERT)
7657 {
7658 if (*eap->arg == '-' || *eap->arg == '+')
7659 n += W_WIDTH(curwin);
7660 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
7661 n = 9999;
7662 win_setwidth_win((int)n, wp);
7663 }
7664 else
7665#endif
7666 {
7667 if (*eap->arg == '-' || *eap->arg == '+')
7668 n += curwin->w_height;
7669 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
7670 n = 9999;
7671 win_setheight_win((int)n, wp);
7672 }
7673}
7674#endif
7675
7676/*
7677 * ":find [+command] <file>" command.
7678 */
7679 static void
7680ex_find(eap)
7681 exarg_T *eap;
7682{
7683#ifdef FEAT_SEARCHPATH
7684 char_u *fname;
7685 int count;
7686
7687 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
7688 TRUE, curbuf->b_ffname);
7689 if (eap->addr_count > 0)
7690 {
7691 /* Repeat finding the file "count" times. This matters when it
7692 * appears several times in the path. */
7693 count = eap->line2;
7694 while (fname != NULL && --count > 0)
7695 {
7696 vim_free(fname);
7697 fname = find_file_in_path(NULL, 0, FNAME_MESS,
7698 FALSE, curbuf->b_ffname);
7699 }
7700 }
7701
7702 if (fname != NULL)
7703 {
7704 eap->arg = fname;
7705#endif
7706 do_exedit(eap, NULL);
7707#ifdef FEAT_SEARCHPATH
7708 vim_free(fname);
7709 }
7710#endif
7711}
7712
7713/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00007714 * ":open" simulation: for now just work like ":visual".
7715 */
7716 static void
7717ex_open(eap)
7718 exarg_T *eap;
7719{
7720 regmatch_T regmatch;
7721 char_u *p;
7722
7723 curwin->w_cursor.lnum = eap->line2;
7724 beginline(BL_SOL | BL_FIX);
7725 if (*eap->arg == '/')
7726 {
7727 /* ":open /pattern/": put cursor in column found with pattern */
7728 ++eap->arg;
7729 p = skip_regexp(eap->arg, '/', p_magic, NULL);
7730 *p = NUL;
7731 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
7732 if (regmatch.regprog != NULL)
7733 {
7734 regmatch.rm_ic = p_ic;
7735 p = ml_get_curline();
7736 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007737 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007738 else
7739 EMSG(_(e_nomatch));
7740 vim_free(regmatch.regprog);
7741 }
7742 /* Move to the NUL, ignore any other arguments. */
7743 eap->arg += STRLEN(eap->arg);
7744 }
7745 check_cursor();
7746
7747 eap->cmdidx = CMD_visual;
7748 do_exedit(eap, NULL);
7749}
7750
7751/*
7752 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007753 */
7754 static void
7755ex_edit(eap)
7756 exarg_T *eap;
7757{
7758 do_exedit(eap, NULL);
7759}
7760
7761/*
7762 * ":edit <file>" command and alikes.
7763 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007764 void
7765do_exedit(eap, old_curwin)
7766 exarg_T *eap;
7767 win_T *old_curwin; /* curwin before doing a split or NULL */
7768{
7769 int n;
7770#ifdef FEAT_WINDOWS
7771 int need_hide;
7772#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00007773 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007774
7775 /*
7776 * ":vi" command ends Ex mode.
7777 */
7778 if (exmode_active && (eap->cmdidx == CMD_visual
7779 || eap->cmdidx == CMD_view))
7780 {
7781 exmode_active = FALSE;
7782 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007783 {
7784 /* Special case: ":global/pat/visual\NLvi-commands" */
7785 if (global_busy)
7786 {
7787 int rd = RedrawingDisabled;
7788 int nwr = no_wait_return;
7789 int ms = msg_scroll;
7790#ifdef FEAT_GUI
7791 int he = hold_gui_events;
7792#endif
7793
7794 if (eap->nextcmd != NULL)
7795 {
7796 stuffReadbuff(eap->nextcmd);
7797 eap->nextcmd = NULL;
7798 }
7799
7800 if (exmode_was != EXMODE_VIM)
7801 settmode(TMODE_RAW);
7802 RedrawingDisabled = 0;
7803 no_wait_return = 0;
7804 need_wait_return = FALSE;
7805 msg_scroll = 0;
7806#ifdef FEAT_GUI
7807 hold_gui_events = 0;
7808#endif
7809 must_redraw = CLEAR;
7810
7811 main_loop(FALSE, TRUE);
7812
7813 RedrawingDisabled = rd;
7814 no_wait_return = nwr;
7815 msg_scroll = ms;
7816#ifdef FEAT_GUI
7817 hold_gui_events = he;
7818#endif
7819 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007820 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007821 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007822 }
7823
7824 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007825 || eap->cmdidx == CMD_tabnew
7826 || eap->cmdidx == CMD_tabedit
Bram Moolenaar071d4272004-06-13 20:20:40 +00007827#ifdef FEAT_VERTSPLIT
7828 || eap->cmdidx == CMD_vnew
7829#endif
7830 ) && *eap->arg == NUL)
7831 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007832 /* ":new" or ":tabnew" without argument: edit an new empty buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007833 setpcmark();
7834 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007835 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
7836 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007837 }
7838 else if ((eap->cmdidx != CMD_split
7839#ifdef FEAT_VERTSPLIT
7840 && eap->cmdidx != CMD_vsplit
7841#endif
7842 )
7843 || *eap->arg != NUL
7844#ifdef FEAT_BROWSE
7845 || cmdmod.browse
7846#endif
7847 )
7848 {
Bram Moolenaar5555acc2006-04-07 21:33:12 +00007849#ifdef FEAT_AUTOCMD
7850 /* Can't edit another file when "curbuf_lock" is set. Only ":edit"
7851 * can bring us here, others are stopped earlier. */
7852 if (*eap->arg != NUL && curbuf_locked())
7853 return;
7854#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007855 n = readonlymode;
7856 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
7857 readonlymode = TRUE;
7858 else if (eap->cmdidx == CMD_enew)
7859 readonlymode = FALSE; /* 'readonly' doesn't make sense in an
7860 empty buffer */
7861 setpcmark();
7862 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
7863 NULL, eap,
7864 /* ":edit" goes to first line if Vi compatible */
7865 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
7866 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
7867 ? ECMD_ONE : eap->do_ecmd_lnum,
7868 (P_HID(curbuf) ? ECMD_HIDE : 0)
7869 + (eap->forceit ? ECMD_FORCEIT : 0)
7870#ifdef FEAT_LISTCMDS
7871 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
7872#endif
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007873 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007874 {
7875 /* Editing the file failed. If the window was split, close it. */
7876#ifdef FEAT_WINDOWS
7877 if (old_curwin != NULL)
7878 {
7879 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
7880 if (!need_hide || P_HID(curbuf))
7881 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007882# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7883 cleanup_T cs;
7884
7885 /* Reset the error/interrupt/exception state here so that
7886 * aborting() returns FALSE when closing a window. */
7887 enter_cleanup(&cs);
7888# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007889# ifdef FEAT_GUI
7890 need_mouse_correct = TRUE;
7891# endif
7892 win_close(curwin, !need_hide && !P_HID(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007893
7894# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7895 /* Restore the error/interrupt/exception state if not
7896 * discarded by a new aborting error, interrupt, or
7897 * uncaught exception. */
7898 leave_cleanup(&cs);
7899# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007900 }
7901 }
7902#endif
7903 }
7904 else if (readonlymode && curbuf->b_nwindows == 1)
7905 {
7906 /* When editing an already visited buffer, 'readonly' won't be set
7907 * but the previous value is kept. With ":view" and ":sview" we
7908 * want the file to be readonly, except when another window is
7909 * editing the same buffer. */
7910 curbuf->b_p_ro = TRUE;
7911 }
7912 readonlymode = n;
7913 }
7914 else
7915 {
7916 if (eap->do_ecmd_cmd != NULL)
7917 do_cmdline_cmd(eap->do_ecmd_cmd);
7918#ifdef FEAT_TITLE
7919 n = curwin->w_arg_idx_invalid;
7920#endif
7921 check_arg_idx(curwin);
7922#ifdef FEAT_TITLE
7923 if (n != curwin->w_arg_idx_invalid)
7924 maketitle();
7925#endif
7926 }
7927
7928#ifdef FEAT_WINDOWS
7929 /*
7930 * if ":split file" worked, set alternate file name in old window to new
7931 * file
7932 */
7933 if (old_curwin != NULL
7934 && *eap->arg != NUL
7935 && curwin != old_curwin
7936 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00007937 && old_curwin->w_buffer != curbuf
7938 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007939 old_curwin->w_alt_fnum = curbuf->b_fnum;
7940#endif
7941
7942 ex_no_reprint = TRUE;
7943}
7944
7945#ifndef FEAT_GUI
7946/*
7947 * ":gui" and ":gvim" when there is no GUI.
7948 */
7949 static void
7950ex_nogui(eap)
7951 exarg_T *eap;
7952{
7953 eap->errmsg = e_nogvim;
7954}
7955#endif
7956
7957#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
7958 static void
7959ex_tearoff(eap)
7960 exarg_T *eap;
7961{
7962 gui_make_tearoff(eap->arg);
7963}
7964#endif
7965
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007966#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007967 static void
7968ex_popup(eap)
7969 exarg_T *eap;
7970{
Bram Moolenaar97409f12005-07-08 22:17:29 +00007971 gui_make_popup(eap->arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007972}
7973#endif
7974
Bram Moolenaar071d4272004-06-13 20:20:40 +00007975 static void
7976ex_swapname(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007977 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007978{
7979 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
7980 MSG(_("No swap file"));
7981 else
7982 msg(curbuf->b_ml.ml_mfp->mf_fname);
7983}
7984
7985/*
7986 * ":syncbind" forces all 'scrollbind' windows to have the same relative
7987 * offset.
7988 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
7989 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007990 static void
7991ex_syncbind(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007992 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007993{
7994#ifdef FEAT_SCROLLBIND
7995 win_T *wp;
7996 long topline;
7997 long y;
7998 linenr_T old_linenr = curwin->w_cursor.lnum;
7999
8000 setpcmark();
8001
8002 /*
8003 * determine max topline
8004 */
8005 if (curwin->w_p_scb)
8006 {
8007 topline = curwin->w_topline;
8008 for (wp = firstwin; wp; wp = wp->w_next)
8009 {
8010 if (wp->w_p_scb && wp->w_buffer)
8011 {
8012 y = wp->w_buffer->b_ml.ml_line_count - p_so;
8013 if (topline > y)
8014 topline = y;
8015 }
8016 }
8017 if (topline < 1)
8018 topline = 1;
8019 }
8020 else
8021 {
8022 topline = 1;
8023 }
8024
8025
8026 /*
8027 * set all scrollbind windows to the same topline
8028 */
8029 wp = curwin;
8030 for (curwin = firstwin; curwin; curwin = curwin->w_next)
8031 {
8032 if (curwin->w_p_scb)
8033 {
8034 y = topline - curwin->w_topline;
8035 if (y > 0)
8036 scrollup(y, TRUE);
8037 else
8038 scrolldown(-y, TRUE);
8039 curwin->w_scbind_pos = topline;
8040 redraw_later(VALID);
8041 cursor_correct();
8042#ifdef FEAT_WINDOWS
8043 curwin->w_redr_status = TRUE;
8044#endif
8045 }
8046 }
8047 curwin = wp;
8048 if (curwin->w_p_scb)
8049 {
8050 did_syncbind = TRUE;
8051 checkpcmark();
8052 if (old_linenr != curwin->w_cursor.lnum)
8053 {
8054 char_u ctrl_o[2];
8055
8056 ctrl_o[0] = Ctrl_O;
8057 ctrl_o[1] = 0;
8058 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
8059 }
8060 }
8061#endif
8062}
8063
8064
8065 static void
8066ex_read(eap)
8067 exarg_T *eap;
8068{
Bram Moolenaardf177f62005-02-22 08:39:57 +00008069 int i;
8070 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
8071 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008072
8073 if (eap->usefilter) /* :r!cmd */
8074 do_bang(1, eap, FALSE, FALSE, TRUE);
8075 else
8076 {
8077 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
8078 return;
8079
8080#ifdef FEAT_BROWSE
8081 if (cmdmod.browse)
8082 {
8083 char_u *browseFile;
8084
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008085 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008086 NULL, NULL, NULL, curbuf);
8087 if (browseFile != NULL)
8088 {
8089 i = readfile(browseFile, NULL,
8090 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8091 vim_free(browseFile);
8092 }
8093 else
8094 i = OK;
8095 }
8096 else
8097#endif
8098 if (*eap->arg == NUL)
8099 {
8100 if (check_fname() == FAIL) /* check for no file name */
8101 return;
8102 i = readfile(curbuf->b_ffname, curbuf->b_fname,
8103 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8104 }
8105 else
8106 {
8107 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
8108 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
8109 i = readfile(eap->arg, NULL,
8110 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8111
8112 }
8113 if (i == FAIL)
8114 {
8115#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
8116 if (!aborting())
8117#endif
8118 EMSG2(_(e_notopen), eap->arg);
8119 }
8120 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008121 {
8122 if (empty && exmode_active)
8123 {
8124 /* Delete the empty line that remains. Historically ex does
8125 * this but vi doesn't. */
8126 if (eap->line2 == 0)
8127 lnum = curbuf->b_ml.ml_line_count;
8128 else
8129 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008130 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008131 {
8132 ml_delete(lnum, FALSE);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008133 if (curwin->w_cursor.lnum > 1
8134 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008135 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00008136 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008137 }
8138 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008139 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008140 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008141 }
8142}
8143
Bram Moolenaarea408852005-06-25 22:49:46 +00008144static char_u *prev_dir = NULL;
8145
8146#if defined(EXITFREE) || defined(PROTO)
8147 void
8148free_cd_dir()
8149{
8150 vim_free(prev_dir);
Bram Moolenaara0174af2008-01-02 20:08:25 +00008151 prev_dir = NULL;
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00008152
8153 vim_free(globaldir);
8154 globaldir = NULL;
Bram Moolenaarea408852005-06-25 22:49:46 +00008155}
8156#endif
8157
8158
Bram Moolenaar071d4272004-06-13 20:20:40 +00008159/*
8160 * ":cd", ":lcd", ":chdir" and ":lchdir".
8161 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00008162 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00008163ex_cd(eap)
8164 exarg_T *eap;
8165{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008166 char_u *new_dir;
8167 char_u *tofree;
8168
8169 new_dir = eap->arg;
8170#if !defined(UNIX) && !defined(VMS)
8171 /* for non-UNIX ":cd" means: print current directory */
8172 if (*new_dir == NUL)
8173 ex_pwd(NULL);
8174 else
8175#endif
8176 {
Bram Moolenaar8e8fe9b2009-03-11 14:37:32 +00008177#ifdef FEAT_AUTOCMD
8178 if (allbuf_locked())
8179 return;
8180#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00008181 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged()
8182 && !eap->forceit)
8183 {
Bram Moolenaar81870892007-11-11 18:17:28 +00008184 EMSG(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00008185 return;
8186 }
8187
Bram Moolenaar071d4272004-06-13 20:20:40 +00008188 /* ":cd -": Change to previous directory */
8189 if (STRCMP(new_dir, "-") == 0)
8190 {
8191 if (prev_dir == NULL)
8192 {
8193 EMSG(_("E186: No previous directory"));
8194 return;
8195 }
8196 new_dir = prev_dir;
8197 }
8198
8199 /* Save current directory for next ":cd -" */
8200 tofree = prev_dir;
8201 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8202 prev_dir = vim_strsave(NameBuff);
8203 else
8204 prev_dir = NULL;
8205
8206#if defined(UNIX) || defined(VMS)
8207 /* for UNIX ":cd" means: go to home directory */
8208 if (*new_dir == NUL)
8209 {
8210 /* use NameBuff for home directory name */
8211# ifdef VMS
8212 char_u *p;
8213
8214 p = mch_getenv((char_u *)"SYS$LOGIN");
8215 if (p == NULL || *p == NUL) /* empty is the same as not set */
8216 NameBuff[0] = NUL;
8217 else
Bram Moolenaarb6356332005-07-18 21:40:44 +00008218 vim_strncpy(NameBuff, p, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008219# else
8220 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
8221# endif
8222 new_dir = NameBuff;
8223 }
8224#endif
8225 if (new_dir == NULL || vim_chdir(new_dir))
8226 EMSG(_(e_failed));
8227 else
8228 {
8229 vim_free(curwin->w_localdir);
8230 if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir)
8231 {
8232 /* If still in global directory, need to remember current
8233 * directory as global directory. */
8234 if (globaldir == NULL && prev_dir != NULL)
8235 globaldir = vim_strsave(prev_dir);
8236 /* Remember this local directory for the window. */
8237 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8238 curwin->w_localdir = vim_strsave(NameBuff);
8239 }
8240 else
8241 {
8242 /* We are now in the global directory, no need to remember its
8243 * name. */
8244 vim_free(globaldir);
8245 globaldir = NULL;
8246 curwin->w_localdir = NULL;
8247 }
8248
8249 shorten_fnames(TRUE);
8250
8251 /* Echo the new current directory if the command was typed. */
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00008252 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008253 ex_pwd(eap);
8254 }
8255 vim_free(tofree);
8256 }
8257}
8258
8259/*
8260 * ":pwd".
8261 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008262 static void
8263ex_pwd(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008264 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008265{
8266 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8267 {
8268#ifdef BACKSLASH_IN_FILENAME
8269 slash_adjust(NameBuff);
8270#endif
8271 msg(NameBuff);
8272 }
8273 else
8274 EMSG(_("E187: Unknown"));
8275}
8276
8277/*
8278 * ":=".
8279 */
8280 static void
8281ex_equal(eap)
8282 exarg_T *eap;
8283{
8284 smsg((char_u *)"%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008285 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008286}
8287
8288 static void
8289ex_sleep(eap)
8290 exarg_T *eap;
8291{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008292 int n;
8293 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008294
8295 if (cursor_valid())
8296 {
8297 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
8298 if (n >= 0)
8299 windgoto((int)n, curwin->w_wcol);
8300 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008301
8302 len = eap->line2;
8303 switch (*eap->arg)
8304 {
8305 case 'm': break;
8306 case NUL: len *= 1000L; break;
8307 default: EMSG2(_(e_invarg2), eap->arg); return;
8308 }
8309 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008310}
8311
8312/*
8313 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
8314 */
8315 void
8316do_sleep(msec)
8317 long msec;
8318{
8319 long done;
8320
8321 cursor_on();
8322 out_flush();
8323 for (done = 0; !got_int && done < msec; done += 1000L)
8324 {
8325 ui_delay(msec - done > 1000L ? 1000L : msec - done, TRUE);
8326 ui_breakcheck();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02008327#ifdef FEAT_NETBEANS_INTG
8328 /* Process the netbeans messages that may have been received in the
8329 * call to ui_breakcheck() when the GUI is in use. This may occur when
8330 * running a test case. */
8331 netbeans_parse_messages();
8332#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008333 }
8334}
8335
8336 static void
8337do_exmap(eap, isabbrev)
8338 exarg_T *eap;
8339 int isabbrev;
8340{
8341 int mode;
8342 char_u *cmdp;
8343
8344 cmdp = eap->cmd;
8345 mode = get_map_mode(&cmdp, eap->forceit || isabbrev);
8346
8347 switch (do_map((*cmdp == 'n') ? 2 : (*cmdp == 'u'),
8348 eap->arg, mode, isabbrev))
8349 {
8350 case 1: EMSG(_(e_invarg));
8351 break;
8352 case 2: EMSG(isabbrev ? _(e_noabbr) : _(e_nomap));
8353 break;
8354 }
8355}
8356
8357/*
8358 * ":winsize" command (obsolete).
8359 */
8360 static void
8361ex_winsize(eap)
8362 exarg_T *eap;
8363{
8364 int w, h;
8365 char_u *arg = eap->arg;
8366 char_u *p;
8367
8368 w = getdigits(&arg);
8369 arg = skipwhite(arg);
8370 p = arg;
8371 h = getdigits(&arg);
8372 if (*p != NUL && *arg == NUL)
8373 set_shellsize(w, h, TRUE);
8374 else
8375 EMSG(_("E465: :winsize requires two number arguments"));
8376}
8377
8378#ifdef FEAT_WINDOWS
8379 static void
8380ex_wincmd(eap)
8381 exarg_T *eap;
8382{
8383 int xchar = NUL;
8384 char_u *p;
8385
8386 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
8387 {
8388 /* CTRL-W g and CTRL-W CTRL-G have an extra command character */
8389 if (eap->arg[1] == NUL)
8390 {
8391 EMSG(_(e_invarg));
8392 return;
8393 }
8394 xchar = eap->arg[1];
8395 p = eap->arg + 2;
8396 }
8397 else
8398 p = eap->arg + 1;
8399
8400 eap->nextcmd = check_nextcmd(p);
8401 p = skipwhite(p);
8402 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
8403 EMSG(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02008404 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008405 {
8406 /* Pass flags on for ":vertical wincmd ]". */
8407 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008408 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008409 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
8410 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008411 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008412 }
8413}
8414#endif
8415
Bram Moolenaar843ee412004-06-30 16:16:41 +00008416#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008417/*
8418 * ":winpos".
8419 */
8420 static void
8421ex_winpos(eap)
8422 exarg_T *eap;
8423{
8424 int x, y;
8425 char_u *arg = eap->arg;
8426 char_u *p;
8427
8428 if (*arg == NUL)
8429 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00008430# if defined(FEAT_GUI) || defined(MSWIN)
8431# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008432 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00008433# else
8434 if (mch_get_winpos(&x, &y) != FAIL)
8435# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008436 {
8437 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
8438 msg(IObuff);
8439 }
8440 else
8441# endif
8442 EMSG(_("E188: Obtaining window position not implemented for this platform"));
8443 }
8444 else
8445 {
8446 x = getdigits(&arg);
8447 arg = skipwhite(arg);
8448 p = arg;
8449 y = getdigits(&arg);
8450 if (*p == NUL || *arg != NUL)
8451 {
8452 EMSG(_("E466: :winpos requires two number arguments"));
8453 return;
8454 }
8455# ifdef FEAT_GUI
8456 if (gui.in_use)
8457 gui_mch_set_winpos(x, y);
8458 else if (gui.starting)
8459 {
8460 /* Remember the coordinates for when the window is opened. */
8461 gui_win_x = x;
8462 gui_win_y = y;
8463 }
8464# ifdef HAVE_TGETENT
8465 else
8466# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00008467# else
8468# ifdef MSWIN
8469 mch_set_winpos(x, y);
8470# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008471# endif
8472# ifdef HAVE_TGETENT
8473 if (*T_CWP)
8474 term_set_winpos(x, y);
8475# endif
8476 }
8477}
8478#endif
8479
8480/*
8481 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
8482 */
8483 static void
8484ex_operators(eap)
8485 exarg_T *eap;
8486{
8487 oparg_T oa;
8488
8489 clear_oparg(&oa);
8490 oa.regname = eap->regname;
8491 oa.start.lnum = eap->line1;
8492 oa.end.lnum = eap->line2;
8493 oa.line_count = eap->line2 - eap->line1 + 1;
8494 oa.motion_type = MLINE;
8495#ifdef FEAT_VIRTUALEDIT
8496 virtual_op = FALSE;
8497#endif
8498 if (eap->cmdidx != CMD_yank) /* position cursor for undo */
8499 {
8500 setpcmark();
8501 curwin->w_cursor.lnum = eap->line1;
8502 beginline(BL_SOL | BL_FIX);
8503 }
8504
8505 switch (eap->cmdidx)
8506 {
8507 case CMD_delete:
8508 oa.op_type = OP_DELETE;
8509 op_delete(&oa);
8510 break;
8511
8512 case CMD_yank:
8513 oa.op_type = OP_YANK;
8514 (void)op_yank(&oa, FALSE, TRUE);
8515 break;
8516
8517 default: /* CMD_rshift or CMD_lshift */
8518 if ((eap->cmdidx == CMD_rshift)
8519#ifdef FEAT_RIGHTLEFT
8520 ^ curwin->w_p_rl
8521#endif
8522 )
8523 oa.op_type = OP_RSHIFT;
8524 else
8525 oa.op_type = OP_LSHIFT;
8526 op_shift(&oa, FALSE, eap->amount);
8527 break;
8528 }
8529#ifdef FEAT_VIRTUALEDIT
8530 virtual_op = MAYBE;
8531#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00008532 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008533}
8534
8535/*
8536 * ":put".
8537 */
8538 static void
8539ex_put(eap)
8540 exarg_T *eap;
8541{
8542 /* ":0put" works like ":1put!". */
8543 if (eap->line2 == 0)
8544 {
8545 eap->line2 = 1;
8546 eap->forceit = TRUE;
8547 }
8548 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008549 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
8550 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008551}
8552
8553/*
8554 * Handle ":copy" and ":move".
8555 */
8556 static void
8557ex_copymove(eap)
8558 exarg_T *eap;
8559{
8560 long n;
8561
8562 n = get_address(&eap->arg, FALSE, FALSE);
8563 if (eap->arg == NULL) /* error detected */
8564 {
8565 eap->nextcmd = NULL;
8566 return;
8567 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008568 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008569
8570 /*
8571 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
8572 */
8573 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
8574 {
8575 EMSG(_(e_invaddr));
8576 return;
8577 }
8578
8579 if (eap->cmdidx == CMD_move)
8580 {
8581 if (do_move(eap->line1, eap->line2, n) == FAIL)
8582 return;
8583 }
8584 else
8585 ex_copy(eap->line1, eap->line2, n);
8586 u_clearline();
8587 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008588 ex_may_print(eap);
8589}
8590
8591/*
8592 * Print the current line if flags were given to the Ex command.
8593 */
8594 static void
8595ex_may_print(eap)
8596 exarg_T *eap;
8597{
8598 if (eap->flags != 0)
8599 {
8600 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
8601 (eap->flags & EXFLAG_LIST));
8602 ex_no_reprint = TRUE;
8603 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008604}
8605
8606/*
8607 * ":smagic" and ":snomagic".
8608 */
8609 static void
8610ex_submagic(eap)
8611 exarg_T *eap;
8612{
8613 int magic_save = p_magic;
8614
8615 p_magic = (eap->cmdidx == CMD_smagic);
8616 do_sub(eap);
8617 p_magic = magic_save;
8618}
8619
8620/*
8621 * ":join".
8622 */
8623 static void
8624ex_join(eap)
8625 exarg_T *eap;
8626{
8627 curwin->w_cursor.lnum = eap->line1;
8628 if (eap->line1 == eap->line2)
8629 {
8630 if (eap->addr_count >= 2) /* :2,2join does nothing */
8631 return;
8632 if (eap->line2 == curbuf->b_ml.ml_line_count)
8633 {
8634 beep_flush();
8635 return;
8636 }
8637 ++eap->line2;
8638 }
Bram Moolenaar81340392012-06-06 16:12:59 +02008639 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008640 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008641 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008642}
8643
8644/*
8645 * ":[addr]@r" or ":[addr]*r": execute register
8646 */
8647 static void
8648ex_at(eap)
8649 exarg_T *eap;
8650{
8651 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00008652 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008653
8654 curwin->w_cursor.lnum = eap->line2;
8655
8656#ifdef USE_ON_FLY_SCROLL
8657 dont_scroll = TRUE; /* disallow scrolling here */
8658#endif
8659
8660 /* get the register name. No name means to use the previous one */
8661 c = *eap->arg;
8662 if (c == NUL || (c == '*' && *eap->cmd == '*'))
8663 c = '@';
Bram Moolenaard333d1e2006-11-07 17:43:47 +00008664 /* Put the register in the typeahead buffer with the "silent" flag. */
8665 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
8666 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008667 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008668 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00008669 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008670 else
8671 {
8672 int save_efr = exec_from_reg;
8673
8674 exec_from_reg = TRUE;
8675
8676 /*
8677 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00008678 * Continue until the stuff buffer is empty and all added characters
8679 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008680 */
Bram Moolenaar60462872009-11-03 11:40:19 +00008681 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008682 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
8683
8684 exec_from_reg = save_efr;
8685 }
8686}
8687
8688/*
8689 * ":!".
8690 */
8691 static void
8692ex_bang(eap)
8693 exarg_T *eap;
8694{
8695 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
8696}
8697
8698/*
8699 * ":undo".
8700 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008701 static void
8702ex_undo(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008703 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008704{
Bram Moolenaard3667a22006-03-16 21:35:52 +00008705 if (eap->addr_count == 1) /* :undo 123 */
Bram Moolenaar730cde92010-06-27 05:18:54 +02008706 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00008707 else
8708 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008709}
8710
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008711#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008712 static void
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008713ex_wundo(eap)
8714 exarg_T *eap;
8715{
8716 char_u hash[UNDO_HASH_SIZE];
8717
8718 u_compute_hash(hash);
8719 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
8720}
8721
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008722 static void
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008723ex_rundo(eap)
8724 exarg_T *eap;
8725{
8726 char_u hash[UNDO_HASH_SIZE];
8727
8728 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02008729 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008730}
8731#endif
8732
Bram Moolenaar071d4272004-06-13 20:20:40 +00008733/*
8734 * ":redo".
8735 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008736 static void
8737ex_redo(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008738 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008739{
8740 u_redo(1);
8741}
8742
8743/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008744 * ":earlier" and ":later".
8745 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008746 static void
8747ex_later(eap)
8748 exarg_T *eap;
8749{
8750 long count = 0;
8751 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008752 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008753 char_u *p = eap->arg;
8754
8755 if (*p == NUL)
8756 count = 1;
8757 else if (isdigit(*p))
8758 {
8759 count = getdigits(&p);
8760 switch (*p)
8761 {
8762 case 's': ++p; sec = TRUE; break;
8763 case 'm': ++p; sec = TRUE; count *= 60; break;
8764 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008765 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
8766 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008767 }
8768 }
8769
8770 if (*p != NUL)
8771 EMSG2(_(e_invarg2), eap->arg);
8772 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02008773 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
8774 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008775}
8776
8777/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008778 * ":redir": start/stop redirection.
8779 */
8780 static void
8781ex_redir(eap)
8782 exarg_T *eap;
8783{
8784 char *mode;
8785 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00008786 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008787
8788 if (STRICMP(eap->arg, "END") == 0)
8789 close_redir();
8790 else
8791 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008792 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008793 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008794 ++arg;
8795 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008796 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008797 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008798 mode = "a";
8799 }
8800 else
8801 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00008802 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008803
8804 close_redir();
8805
8806 /* Expand environment variables and "~/". */
Bram Moolenaarca472992005-01-21 11:46:23 +00008807 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008808 if (fname == NULL)
8809 return;
8810#ifdef FEAT_BROWSE
8811 if (cmdmod.browse)
8812 {
8813 char_u *browseFile;
8814
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008815 browseFile = do_browse(BROWSE_SAVE,
8816 (char_u *)_("Save Redirection"),
8817 fname, NULL, NULL, BROWSE_FILTER_ALL_FILES, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008818 if (browseFile == NULL)
8819 return; /* operation cancelled */
8820 vim_free(fname);
8821 fname = browseFile;
8822 eap->forceit = TRUE; /* since dialog already asked */
8823 }
8824#endif
8825
8826 redir_fd = open_exfile(fname, eap->forceit, mode);
8827 vim_free(fname);
8828 }
8829#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00008830 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008831 {
8832 /* redirect to a register a-z (resp. A-Z for appending) */
8833 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00008834 ++arg;
8835 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008836# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00008837 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00008838 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008839# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00008840 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008841 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008842 redir_reg = *arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008843 if (*arg == '>' && arg[1] == '>') /* append */
Bram Moolenaard9d30582005-05-18 22:10:28 +00008844 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008845 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008846 {
Bram Moolenaarc188b882007-10-19 14:20:54 +00008847 /* Can use both "@a" and "@a>". */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00008848 if (*arg == '>')
8849 arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008850 /* Make register empty when not using @A-@Z and the
8851 * command is valid. */
8852 if (*arg == NUL && !isupper(redir_reg))
8853 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008854 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008855 }
8856 if (*arg != NUL)
8857 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00008858 redir_reg = 0;
Bram Moolenaard9d30582005-05-18 22:10:28 +00008859 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008860 }
8861 }
8862 else if (*arg == '=' && arg[1] == '>')
8863 {
8864 int append;
8865
8866 /* redirect to a variable */
8867 close_redir();
8868 arg += 2;
8869
8870 if (*arg == '>')
8871 {
8872 ++arg;
8873 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008874 }
8875 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008876 append = FALSE;
8877
8878 if (var_redir_start(skipwhite(arg), append) == OK)
8879 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008880 }
8881#endif
8882
8883 /* TODO: redirect to a buffer */
8884
Bram Moolenaar071d4272004-06-13 20:20:40 +00008885 else
Bram Moolenaarca472992005-01-21 11:46:23 +00008886 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008887 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00008888
8889 /* Make sure redirection is not off. Can happen for cmdline completion
8890 * that indirectly invokes a command to catch its output. */
8891 if (redir_fd != NULL
8892#ifdef FEAT_EVAL
8893 || redir_reg || redir_vname
8894#endif
8895 )
8896 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008897}
8898
8899/*
8900 * ":redraw": force redraw
8901 */
8902 static void
8903ex_redraw(eap)
8904 exarg_T *eap;
8905{
8906 int r = RedrawingDisabled;
8907 int p = p_lz;
8908
8909 RedrawingDisabled = 0;
8910 p_lz = FALSE;
8911 update_topline();
8912 update_screen(eap->forceit ? CLEAR :
8913#ifdef FEAT_VISUAL
8914 VIsual_active ? INVERTED :
8915#endif
8916 0);
8917#ifdef FEAT_TITLE
8918 if (need_maketitle)
8919 maketitle();
8920#endif
8921 RedrawingDisabled = r;
8922 p_lz = p;
8923
8924 /* Reset msg_didout, so that a message that's there is overwritten. */
8925 msg_didout = FALSE;
8926 msg_col = 0;
8927
8928 out_flush();
8929}
8930
8931/*
8932 * ":redrawstatus": force redraw of status line(s)
8933 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008934 static void
8935ex_redrawstatus(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008936 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008937{
8938#if defined(FEAT_WINDOWS)
8939 int r = RedrawingDisabled;
8940 int p = p_lz;
8941
8942 RedrawingDisabled = 0;
8943 p_lz = FALSE;
8944 if (eap->forceit)
8945 status_redraw_all();
8946 else
8947 status_redraw_curbuf();
8948 update_screen(
8949# ifdef FEAT_VISUAL
8950 VIsual_active ? INVERTED :
8951# endif
8952 0);
8953 RedrawingDisabled = r;
8954 p_lz = p;
8955 out_flush();
8956#endif
8957}
8958
8959 static void
8960close_redir()
8961{
8962 if (redir_fd != NULL)
8963 {
8964 fclose(redir_fd);
8965 redir_fd = NULL;
8966 }
8967#ifdef FEAT_EVAL
8968 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008969 if (redir_vname)
8970 {
8971 var_redir_stop();
8972 redir_vname = 0;
8973 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008974#endif
8975}
8976
8977#if defined(FEAT_SESSION) && defined(USE_CRNL)
8978# define MKSESSION_NL
8979static int mksession_nl = FALSE; /* use NL only in put_eol() */
8980#endif
8981
8982/*
8983 * ":mkexrc", ":mkvimrc", ":mkview" and ":mksession".
8984 */
8985 static void
8986ex_mkrc(eap)
8987 exarg_T *eap;
8988{
8989 FILE *fd;
8990 int failed = FALSE;
8991 char_u *fname;
8992#ifdef FEAT_BROWSE
8993 char_u *browseFile = NULL;
8994#endif
8995#ifdef FEAT_SESSION
8996 int view_session = FALSE;
8997 int using_vdir = FALSE; /* using 'viewdir'? */
8998 char_u *viewFile = NULL;
8999 unsigned *flagp;
9000#endif
9001
9002 if (eap->cmdidx == CMD_mksession || eap->cmdidx == CMD_mkview)
9003 {
9004#ifdef FEAT_SESSION
9005 view_session = TRUE;
9006#else
9007 ex_ni(eap);
9008 return;
9009#endif
9010 }
9011
9012#ifdef FEAT_SESSION
Bram Moolenaar8d594672009-07-01 18:18:57 +00009013 /* Use the short file name until ":lcd" is used. We also don't use the
9014 * short file name when 'acd' is set, that is checked later. */
Bram Moolenaareeefcc72007-05-01 21:21:21 +00009015 did_lcd = FALSE;
9016
Bram Moolenaar071d4272004-06-13 20:20:40 +00009017 /* ":mkview" or ":mkview 9": generate file name with 'viewdir' */
9018 if (eap->cmdidx == CMD_mkview
9019 && (*eap->arg == NUL
9020 || (vim_isdigit(*eap->arg) && eap->arg[1] == NUL)))
9021 {
9022 eap->forceit = TRUE;
9023 fname = get_view_file(*eap->arg);
9024 if (fname == NULL)
9025 return;
9026 viewFile = fname;
9027 using_vdir = TRUE;
9028 }
9029 else
9030#endif
9031 if (*eap->arg != NUL)
9032 fname = eap->arg;
9033 else if (eap->cmdidx == CMD_mkvimrc)
9034 fname = (char_u *)VIMRC_FILE;
9035#ifdef FEAT_SESSION
9036 else if (eap->cmdidx == CMD_mksession)
9037 fname = (char_u *)SESSION_FILE;
9038#endif
9039 else
9040 fname = (char_u *)EXRC_FILE;
9041
9042#ifdef FEAT_BROWSE
9043 if (cmdmod.browse)
9044 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00009045 browseFile = do_browse(BROWSE_SAVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009046# ifdef FEAT_SESSION
9047 eap->cmdidx == CMD_mkview ? (char_u *)_("Save View") :
9048 eap->cmdidx == CMD_mksession ? (char_u *)_("Save Session") :
9049# endif
9050 (char_u *)_("Save Setup"),
9051 fname, (char_u *)"vim", NULL, BROWSE_FILTER_MACROS, NULL);
9052 if (browseFile == NULL)
9053 goto theend;
9054 fname = browseFile;
9055 eap->forceit = TRUE; /* since dialog already asked */
9056 }
9057#endif
9058
9059#if defined(FEAT_SESSION) && defined(vim_mkdir)
9060 /* When using 'viewdir' may have to create the directory. */
9061 if (using_vdir && !mch_isdir(p_vdir))
Bram Moolenaardf177f62005-02-22 08:39:57 +00009062 vim_mkdir_emsg(p_vdir, 0755);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009063#endif
9064
9065 fd = open_exfile(fname, eap->forceit, WRITEBIN);
9066 if (fd != NULL)
9067 {
9068#ifdef FEAT_SESSION
9069 if (eap->cmdidx == CMD_mkview)
9070 flagp = &vop_flags;
9071 else
9072 flagp = &ssop_flags;
9073#endif
9074
9075#ifdef MKSESSION_NL
9076 /* "unix" in 'sessionoptions': use NL line separator */
9077 if (view_session && (*flagp & SSOP_UNIX))
9078 mksession_nl = TRUE;
9079#endif
9080
9081 /* Write the version command for :mkvimrc */
9082 if (eap->cmdidx == CMD_mkvimrc)
9083 (void)put_line(fd, "version 6.0");
9084
9085#ifdef FEAT_SESSION
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009086 if (eap->cmdidx == CMD_mksession)
9087 {
9088 if (put_line(fd, "let SessionLoad = 1") == FAIL)
9089 failed = TRUE;
9090 }
9091
Bram Moolenaar071d4272004-06-13 20:20:40 +00009092 if (eap->cmdidx != CMD_mkview)
9093#endif
9094 {
9095 /* Write setting 'compatible' first, because it has side effects.
9096 * For that same reason only do it when needed. */
9097 if (p_cp)
9098 (void)put_line(fd, "if !&cp | set cp | endif");
9099 else
9100 (void)put_line(fd, "if &cp | set nocp | endif");
9101 }
9102
9103#ifdef FEAT_SESSION
9104 if (!view_session
9105 || (eap->cmdidx == CMD_mksession
9106 && (*flagp & SSOP_OPTIONS)))
9107#endif
9108 failed |= (makemap(fd, NULL) == FAIL
9109 || makeset(fd, OPT_GLOBAL, FALSE) == FAIL);
9110
9111#ifdef FEAT_SESSION
9112 if (!failed && view_session)
9113 {
9114 if (put_line(fd, "let s:so_save = &so | let s:siso_save = &siso | set so=0 siso=0") == FAIL)
9115 failed = TRUE;
9116 if (eap->cmdidx == CMD_mksession)
9117 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02009118 char_u *dirnow; /* current directory */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009119
Bram Moolenaard9462e32011-04-11 21:35:11 +02009120 dirnow = alloc(MAXPATHL);
9121 if (dirnow == NULL)
9122 failed = TRUE;
9123 else
9124 {
9125 /*
9126 * Change to session file's dir.
9127 */
9128 if (mch_dirname(dirnow, MAXPATHL) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00009129 || mch_chdir((char *)dirnow) != 0)
Bram Moolenaard9462e32011-04-11 21:35:11 +02009130 *dirnow = NUL;
9131 if (*dirnow != NUL && (ssop_flags & SSOP_SESDIR))
9132 {
9133 if (vim_chdirfile(fname) == OK)
9134 shorten_fnames(TRUE);
9135 }
9136 else if (*dirnow != NUL
9137 && (ssop_flags & SSOP_CURDIR) && globaldir != NULL)
9138 {
9139 if (mch_chdir((char *)globaldir) == 0)
9140 shorten_fnames(TRUE);
9141 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009142
Bram Moolenaard9462e32011-04-11 21:35:11 +02009143 failed |= (makeopens(fd, dirnow) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009144
Bram Moolenaard9462e32011-04-11 21:35:11 +02009145 /* restore original dir */
9146 if (*dirnow != NUL && ((ssop_flags & SSOP_SESDIR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009147 || ((ssop_flags & SSOP_CURDIR) && globaldir != NULL)))
Bram Moolenaard9462e32011-04-11 21:35:11 +02009148 {
9149 if (mch_chdir((char *)dirnow) != 0)
9150 EMSG(_(e_prev_dir));
9151 shorten_fnames(TRUE);
9152 }
9153 vim_free(dirnow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009154 }
9155 }
9156 else
9157 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +00009158 failed |= (put_view(fd, curwin, !using_vdir, flagp,
9159 -1) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009160 }
9161 if (put_line(fd, "let &so = s:so_save | let &siso = s:siso_save")
9162 == FAIL)
9163 failed = TRUE;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009164 if (put_line(fd, "doautoall SessionLoadPost") == FAIL)
9165 failed = TRUE;
Bram Moolenaar4770d092006-01-12 23:22:24 +00009166 if (eap->cmdidx == CMD_mksession)
9167 {
9168 if (put_line(fd, "unlet SessionLoad") == FAIL)
9169 failed = TRUE;
9170 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009171 }
9172#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009173 if (put_line(fd, "\" vim: set ft=vim :") == FAIL)
9174 failed = TRUE;
9175
Bram Moolenaar071d4272004-06-13 20:20:40 +00009176 failed |= fclose(fd);
9177
9178 if (failed)
9179 EMSG(_(e_write));
9180#if defined(FEAT_EVAL) && defined(FEAT_SESSION)
9181 else if (eap->cmdidx == CMD_mksession)
9182 {
9183 /* successful session write - set this_session var */
Bram Moolenaard9462e32011-04-11 21:35:11 +02009184 char_u *tbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009185
Bram Moolenaard9462e32011-04-11 21:35:11 +02009186 tbuf = alloc(MAXPATHL);
9187 if (tbuf != NULL)
9188 {
9189 if (vim_FullName(fname, tbuf, MAXPATHL, FALSE) == OK)
9190 set_vim_var_string(VV_THIS_SESSION, tbuf, -1);
9191 vim_free(tbuf);
9192 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009193 }
9194#endif
9195#ifdef MKSESSION_NL
9196 mksession_nl = FALSE;
9197#endif
9198 }
9199
9200#ifdef FEAT_BROWSE
9201theend:
9202 vim_free(browseFile);
9203#endif
9204#ifdef FEAT_SESSION
9205 vim_free(viewFile);
9206#endif
9207}
9208
Bram Moolenaardf177f62005-02-22 08:39:57 +00009209#if ((defined(FEAT_SESSION) || defined(FEAT_EVAL)) && defined(vim_mkdir)) \
9210 || defined(PROTO)
9211 int
9212vim_mkdir_emsg(name, prot)
9213 char_u *name;
Bram Moolenaar78a15312009-05-15 19:33:18 +00009214 int prot UNUSED;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009215{
9216 if (vim_mkdir(name, prot) != 0)
9217 {
9218 EMSG2(_("E739: Cannot create directory: %s"), name);
9219 return FAIL;
9220 }
9221 return OK;
9222}
9223#endif
9224
Bram Moolenaar071d4272004-06-13 20:20:40 +00009225/*
9226 * Open a file for writing for an Ex command, with some checks.
9227 * Return file descriptor, or NULL on failure.
9228 */
9229 FILE *
9230open_exfile(fname, forceit, mode)
9231 char_u *fname;
9232 int forceit;
9233 char *mode; /* "w" for create new file or "a" for append */
9234{
9235 FILE *fd;
9236
9237#ifdef UNIX
9238 /* with Unix it is possible to open a directory */
9239 if (mch_isdir(fname))
9240 {
9241 EMSG2(_(e_isadir2), fname);
9242 return NULL;
9243 }
9244#endif
9245 if (!forceit && *mode != 'a' && vim_fexists(fname))
9246 {
9247 EMSG2(_("E189: \"%s\" exists (add ! to override)"), fname);
9248 return NULL;
9249 }
9250
9251 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
9252 EMSG2(_("E190: Cannot open \"%s\" for writing"), fname);
9253
9254 return fd;
9255}
9256
9257/*
9258 * ":mark" and ":k".
9259 */
9260 static void
9261ex_mark(eap)
9262 exarg_T *eap;
9263{
9264 pos_T pos;
9265
9266 if (*eap->arg == NUL) /* No argument? */
9267 EMSG(_(e_argreq));
9268 else if (eap->arg[1] != NUL) /* more than one character? */
9269 EMSG(_(e_trailing));
9270 else
9271 {
9272 pos = curwin->w_cursor; /* save curwin->w_cursor */
9273 curwin->w_cursor.lnum = eap->line2;
9274 beginline(BL_WHITE | BL_FIX);
9275 if (setmark(*eap->arg) == FAIL) /* set mark */
9276 EMSG(_("E191: Argument must be a letter or forward/backward quote"));
9277 curwin->w_cursor = pos; /* restore curwin->w_cursor */
9278 }
9279}
9280
9281/*
9282 * Update w_topline, w_leftcol and the cursor position.
9283 */
9284 void
9285update_topline_cursor()
9286{
9287 check_cursor(); /* put cursor on valid line */
9288 update_topline();
9289 if (!curwin->w_p_wrap)
9290 validate_cursor();
9291 update_curswant();
9292}
9293
9294#ifdef FEAT_EX_EXTRA
9295/*
9296 * ":normal[!] {commands}": Execute normal mode commands.
9297 */
9298 static void
9299ex_normal(eap)
9300 exarg_T *eap;
9301{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009302 int save_msg_scroll = msg_scroll;
9303 int save_restart_edit = restart_edit;
9304 int save_msg_didout = msg_didout;
9305 int save_State = State;
9306 tasave_T tabuf;
9307 int save_insertmode = p_im;
9308 int save_finish_op = finish_op;
Bram Moolenaar38084112008-07-26 14:05:07 +00009309 int save_opcount = opcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009310#ifdef FEAT_MBYTE
9311 char_u *arg = NULL;
9312 int l;
9313 char_u *p;
9314#endif
9315
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00009316 if (ex_normal_lock > 0)
9317 {
9318 EMSG(_(e_secure));
9319 return;
9320 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009321 if (ex_normal_busy >= p_mmd)
9322 {
9323 EMSG(_("E192: Recursive use of :normal too deep"));
9324 return;
9325 }
9326 ++ex_normal_busy;
9327
9328 msg_scroll = FALSE; /* no msg scrolling in Normal mode */
9329 restart_edit = 0; /* don't go to Insert mode */
9330 p_im = FALSE; /* don't use 'insertmode' */
9331
9332#ifdef FEAT_MBYTE
9333 /*
9334 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
9335 * this for the K_SPECIAL leading byte, otherwise special keys will not
9336 * work.
9337 */
9338 if (has_mbyte)
9339 {
9340 int len = 0;
9341
9342 /* Count the number of characters to be escaped. */
9343 for (p = eap->arg; *p != NUL; ++p)
9344 {
9345# ifdef FEAT_GUI
9346 if (*p == CSI) /* leadbyte CSI */
9347 len += 2;
9348# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009349 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009350 if (*++p == K_SPECIAL /* trailbyte K_SPECIAL or CSI */
9351# ifdef FEAT_GUI
9352 || *p == CSI
9353# endif
9354 )
9355 len += 2;
9356 }
9357 if (len > 0)
9358 {
9359 arg = alloc((unsigned)(STRLEN(eap->arg) + len + 1));
9360 if (arg != NULL)
9361 {
9362 len = 0;
9363 for (p = eap->arg; *p != NUL; ++p)
9364 {
9365 arg[len++] = *p;
9366# ifdef FEAT_GUI
9367 if (*p == CSI)
9368 {
9369 arg[len++] = KS_EXTRA;
9370 arg[len++] = (int)KE_CSI;
9371 }
9372# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009373 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009374 {
9375 arg[len++] = *++p;
9376 if (*p == K_SPECIAL)
9377 {
9378 arg[len++] = KS_SPECIAL;
9379 arg[len++] = KE_FILLER;
9380 }
9381# ifdef FEAT_GUI
9382 else if (*p == CSI)
9383 {
9384 arg[len++] = KS_EXTRA;
9385 arg[len++] = (int)KE_CSI;
9386 }
9387# endif
9388 }
9389 arg[len] = NUL;
9390 }
9391 }
9392 }
9393 }
9394#endif
9395
9396 /*
9397 * Save the current typeahead. This is required to allow using ":normal"
9398 * from an event handler and makes sure we don't hang when the argument
9399 * ends with half a command.
9400 */
9401 save_typeahead(&tabuf);
9402 if (tabuf.typebuf_valid)
9403 {
9404 /*
9405 * Repeat the :normal command for each line in the range. When no
9406 * range given, execute it just once, without positioning the cursor
9407 * first.
9408 */
9409 do
9410 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009411 if (eap->addr_count != 0)
9412 {
9413 curwin->w_cursor.lnum = eap->line1++;
9414 curwin->w_cursor.col = 0;
9415 }
9416
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009417 exec_normal_cmd(
Bram Moolenaar071d4272004-06-13 20:20:40 +00009418#ifdef FEAT_MBYTE
9419 arg != NULL ? arg :
9420#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009421 eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009422 }
9423 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
9424 }
9425
9426 /* Might not return to the main loop when in an event handler. */
9427 update_topline_cursor();
9428
9429 /* Restore the previous typeahead. */
9430 restore_typeahead(&tabuf);
9431
9432 --ex_normal_busy;
9433 msg_scroll = save_msg_scroll;
9434 restart_edit = save_restart_edit;
9435 p_im = save_insertmode;
9436 finish_op = save_finish_op;
Bram Moolenaar38084112008-07-26 14:05:07 +00009437 opcount = save_opcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009438 msg_didout |= save_msg_didout; /* don't reset msg_didout now */
9439
9440 /* Restore the state (needed when called from a function executed for
9441 * 'indentexpr'). */
9442 State = save_State;
9443#ifdef FEAT_MBYTE
9444 vim_free(arg);
9445#endif
9446}
9447
9448/*
Bram Moolenaar64969662005-12-14 21:59:55 +00009449 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009450 */
9451 static void
9452ex_startinsert(eap)
9453 exarg_T *eap;
9454{
Bram Moolenaarfd371682005-01-14 21:42:54 +00009455 if (eap->forceit)
9456 {
9457 coladvance((colnr_T)MAXCOL);
9458 curwin->w_curswant = MAXCOL;
9459 curwin->w_set_curswant = FALSE;
9460 }
9461
Bram Moolenaara40c5002005-01-09 21:16:21 +00009462 /* Ignore the command when already in Insert mode. Inserting an
9463 * expression register that invokes a function can do this. */
9464 if (State & INSERT)
9465 return;
9466
Bram Moolenaar64969662005-12-14 21:59:55 +00009467 if (eap->cmdidx == CMD_startinsert)
9468 restart_edit = 'a';
9469 else if (eap->cmdidx == CMD_startreplace)
9470 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00009471 else
Bram Moolenaar64969662005-12-14 21:59:55 +00009472 restart_edit = 'V';
9473
9474 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009475 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009476 if (eap->cmdidx == CMD_startinsert)
9477 restart_edit = 'i';
Bram Moolenaar071d4272004-06-13 20:20:40 +00009478 curwin->w_curswant = 0; /* avoid MAXCOL */
9479 }
9480}
9481
9482/*
9483 * ":stopinsert"
9484 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009485 static void
9486ex_stopinsert(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009487 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009488{
9489 restart_edit = 0;
9490 stop_insert_mode = TRUE;
9491}
9492#endif
9493
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009494#if defined(FEAT_EX_EXTRA) || defined(FEAT_MENU) || defined(PROTO)
9495/*
9496 * Execute normal mode command "cmd".
9497 * "remap" can be REMAP_NONE or REMAP_YES.
9498 */
9499 void
9500exec_normal_cmd(cmd, remap, silent)
9501 char_u *cmd;
9502 int remap;
9503 int silent;
9504{
9505 oparg_T oa;
9506
9507 /*
9508 * Stuff the argument into the typeahead buffer.
9509 * Execute normal_cmd() until there is no typeahead left.
9510 */
9511 clear_oparg(&oa);
9512 finish_op = FALSE;
9513 ins_typebuf(cmd, remap, 0, TRUE, silent);
9514 while ((!stuff_empty() || (!typebuf_typed() && typebuf.tb_len > 0))
9515 && !got_int)
9516 {
9517 update_topline_cursor();
Bram Moolenaar48ac02c2011-01-17 19:50:06 +01009518 normal_cmd(&oa, TRUE); /* execute a Normal mode cmd */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009519 }
9520}
9521#endif
9522
Bram Moolenaar071d4272004-06-13 20:20:40 +00009523#ifdef FEAT_FIND_ID
9524 static void
9525ex_checkpath(eap)
9526 exarg_T *eap;
9527{
9528 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
9529 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
9530 (linenr_T)1, (linenr_T)MAXLNUM);
9531}
9532
9533#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
9534/*
9535 * ":psearch"
9536 */
9537 static void
9538ex_psearch(eap)
9539 exarg_T *eap;
9540{
9541 g_do_tagpreview = p_pvh;
9542 ex_findpat(eap);
9543 g_do_tagpreview = 0;
9544}
9545#endif
9546
9547 static void
9548ex_findpat(eap)
9549 exarg_T *eap;
9550{
9551 int whole = TRUE;
9552 long n;
9553 char_u *p;
9554 int action;
9555
9556 switch (cmdnames[eap->cmdidx].cmd_name[2])
9557 {
9558 case 'e': /* ":psearch", ":isearch" and ":dsearch" */
9559 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
9560 action = ACTION_GOTO;
9561 else
9562 action = ACTION_SHOW;
9563 break;
9564 case 'i': /* ":ilist" and ":dlist" */
9565 action = ACTION_SHOW_ALL;
9566 break;
9567 case 'u': /* ":ijump" and ":djump" */
9568 action = ACTION_GOTO;
9569 break;
9570 default: /* ":isplit" and ":dsplit" */
9571 action = ACTION_SPLIT;
9572 break;
9573 }
9574
9575 n = 1;
9576 if (vim_isdigit(*eap->arg)) /* get count */
9577 {
9578 n = getdigits(&eap->arg);
9579 eap->arg = skipwhite(eap->arg);
9580 }
9581 if (*eap->arg == '/') /* Match regexp, not just whole words */
9582 {
9583 whole = FALSE;
9584 ++eap->arg;
9585 p = skip_regexp(eap->arg, '/', p_magic, NULL);
9586 if (*p)
9587 {
9588 *p++ = NUL;
9589 p = skipwhite(p);
9590
9591 /* Check for trailing illegal characters */
9592 if (!ends_excmd(*p))
9593 eap->errmsg = e_trailing;
9594 else
9595 eap->nextcmd = check_nextcmd(p);
9596 }
9597 }
9598 if (!eap->skip)
9599 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
9600 whole, !eap->forceit,
9601 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
9602 n, action, eap->line1, eap->line2);
9603}
9604#endif
9605
9606#ifdef FEAT_WINDOWS
9607
9608# ifdef FEAT_QUICKFIX
9609/*
9610 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
9611 */
9612 static void
9613ex_ptag(eap)
9614 exarg_T *eap;
9615{
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01009616 g_do_tagpreview = p_pvh; /* will be reset to 0 in ex_tag_cmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009617 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9618}
9619
9620/*
9621 * ":pedit"
9622 */
9623 static void
9624ex_pedit(eap)
9625 exarg_T *eap;
9626{
9627 win_T *curwin_save = curwin;
9628
9629 g_do_tagpreview = p_pvh;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00009630 prepare_tagpreview(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009631 keep_help_flag = curwin_save->w_buffer->b_help;
9632 do_exedit(eap, NULL);
9633 keep_help_flag = FALSE;
9634 if (curwin != curwin_save && win_valid(curwin_save))
9635 {
9636 /* Return cursor to where we were */
9637 validate_cursor();
9638 redraw_later(VALID);
9639 win_enter(curwin_save, TRUE);
9640 }
9641 g_do_tagpreview = 0;
9642}
9643# endif
9644
9645/*
9646 * ":stag", ":stselect" and ":stjump".
9647 */
9648 static void
9649ex_stag(eap)
9650 exarg_T *eap;
9651{
9652 postponed_split = -1;
9653 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009654 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009655 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9656 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009657 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009658}
9659#endif
9660
9661/*
9662 * ":tag", ":tselect", ":tjump", ":tnext", etc.
9663 */
9664 static void
9665ex_tag(eap)
9666 exarg_T *eap;
9667{
9668 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
9669}
9670
9671 static void
9672ex_tag_cmd(eap, name)
9673 exarg_T *eap;
9674 char_u *name;
9675{
9676 int cmd;
9677
9678 switch (name[1])
9679 {
9680 case 'j': cmd = DT_JUMP; /* ":tjump" */
9681 break;
9682 case 's': cmd = DT_SELECT; /* ":tselect" */
9683 break;
9684 case 'p': cmd = DT_PREV; /* ":tprevious" */
9685 break;
9686 case 'N': cmd = DT_PREV; /* ":tNext" */
9687 break;
9688 case 'n': cmd = DT_NEXT; /* ":tnext" */
9689 break;
9690 case 'o': cmd = DT_POP; /* ":pop" */
9691 break;
9692 case 'f': /* ":tfirst" */
9693 case 'r': cmd = DT_FIRST; /* ":trewind" */
9694 break;
9695 case 'l': cmd = DT_LAST; /* ":tlast" */
9696 break;
9697 default: /* ":tag" */
9698#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +00009699 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009700 {
9701 do_cstag(eap);
9702 return;
9703 }
9704#endif
9705 cmd = DT_TAG;
9706 break;
9707 }
9708
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00009709 if (name[0] == 'l')
9710 {
9711#ifndef FEAT_QUICKFIX
9712 ex_ni(eap);
9713 return;
9714#else
9715 cmd = DT_LTAG;
9716#endif
9717 }
9718
Bram Moolenaar071d4272004-06-13 20:20:40 +00009719 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
9720 eap->forceit, TRUE);
9721}
9722
9723/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009724 * Check "str" for starting with a special cmdline variable.
9725 * If found return one of the SPEC_ values and set "*usedlen" to the length of
9726 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
9727 */
9728 int
9729find_cmdline_var(src, usedlen)
9730 char_u *src;
9731 int *usedlen;
9732{
9733 int len;
9734 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +00009735 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009736 "%",
9737#define SPEC_PERC 0
9738 "#",
9739#define SPEC_HASH 1
9740 "<cword>", /* cursor word */
9741#define SPEC_CWORD 2
9742 "<cWORD>", /* cursor WORD */
9743#define SPEC_CCWORD 3
9744 "<cfile>", /* cursor path name */
9745#define SPEC_CFILE 4
9746 "<sfile>", /* ":so" file name */
9747#define SPEC_SFILE 5
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009748 "<slnum>", /* ":so" file line number */
9749#define SPEC_SLNUM 6
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009750#ifdef FEAT_AUTOCMD
9751 "<afile>", /* autocommand file name */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009752# define SPEC_AFILE 7
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009753 "<abuf>", /* autocommand buffer number */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009754# define SPEC_ABUF 8
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009755 "<amatch>", /* autocommand match name */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009756# define SPEC_AMATCH 9
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009757#endif
9758#ifdef FEAT_CLIENTSERVER
9759 "<client>"
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009760# ifdef FEAT_AUTOCMD
9761# define SPEC_CLIENT 10
9762# else
9763# define SPEC_CLIENT 7
9764# endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009765#endif
9766 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009767
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009768 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009769 {
9770 len = (int)STRLEN(spec_str[i]);
9771 if (STRNCMP(src, spec_str[i], len) == 0)
9772 {
9773 *usedlen = len;
9774 return i;
9775 }
9776 }
9777 return -1;
9778}
9779
9780/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009781 * Evaluate cmdline variables.
9782 *
9783 * change '%' to curbuf->b_ffname
9784 * '#' to curwin->w_altfile
9785 * '<cword>' to word under the cursor
9786 * '<cWORD>' to WORD under the cursor
9787 * '<cfile>' to path name under the cursor
9788 * '<sfile>' to sourced file name
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009789 * '<slnum>' to sourced file line number
Bram Moolenaar071d4272004-06-13 20:20:40 +00009790 * '<afile>' to file name for autocommand
9791 * '<abuf>' to buffer number for autocommand
9792 * '<amatch>' to matching name for autocommand
9793 *
9794 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
9795 * "" for error without a message) and NULL is returned.
9796 * Returns an allocated string if a valid match was found.
9797 * Returns NULL if no match was found. "usedlen" then still contains the
9798 * number of characters to skip.
9799 */
9800 char_u *
Bram Moolenaar63b92542007-03-27 14:57:09 +00009801eval_vars(src, srcstart, usedlen, lnump, errormsg, escaped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009802 char_u *src; /* pointer into commandline */
Bram Moolenaar63b92542007-03-27 14:57:09 +00009803 char_u *srcstart; /* beginning of valid memory for src */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009804 int *usedlen; /* characters after src that are used */
9805 linenr_T *lnump; /* line number for :e command, or NULL */
9806 char_u **errormsg; /* pointer to error message */
Bram Moolenaar63b92542007-03-27 14:57:09 +00009807 int *escaped; /* return value has escaped white space (can
9808 * be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009809{
9810 int i;
9811 char_u *s;
9812 char_u *result;
9813 char_u *resultbuf = NULL;
9814 int resultlen;
9815 buf_T *buf;
9816 int valid = VALID_HEAD + VALID_PATH; /* assume valid result */
9817 int spec_idx;
9818#ifdef FEAT_MODIFY_FNAME
9819 int skip_mod = FALSE;
9820#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009821 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009822
9823 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009824 if (escaped != NULL)
9825 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009826
9827 /*
9828 * Check if there is something to do.
9829 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009830 spec_idx = find_cmdline_var(src, usedlen);
9831 if (spec_idx < 0) /* no match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009832 {
9833 *usedlen = 1;
9834 return NULL;
9835 }
9836
9837 /*
9838 * Skip when preceded with a backslash "\%" and "\#".
9839 * Note: In "\\%" the % is also not recognized!
9840 */
9841 if (src > srcstart && src[-1] == '\\')
9842 {
9843 *usedlen = 0;
Bram Moolenaara7241f52008-06-24 20:39:31 +00009844 STRMOVE(src - 1, src); /* remove backslash */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009845 return NULL;
9846 }
9847
9848 /*
9849 * word or WORD under cursor
9850 */
9851 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD)
9852 {
9853 resultlen = find_ident_under_cursor(&result, spec_idx == SPEC_CWORD ?
9854 (FIND_IDENT|FIND_STRING) : FIND_STRING);
9855 if (resultlen == 0)
9856 {
9857 *errormsg = (char_u *)"";
9858 return NULL;
9859 }
9860 }
9861
9862 /*
9863 * '#': Alternate file name
9864 * '%': Current file name
9865 * File name under the cursor
9866 * File name for autocommand
9867 * and following modifiers
9868 */
9869 else
9870 {
9871 switch (spec_idx)
9872 {
9873 case SPEC_PERC: /* '%': current file */
9874 if (curbuf->b_fname == NULL)
9875 {
9876 result = (char_u *)"";
9877 valid = 0; /* Must have ":p:h" to be valid */
9878 }
9879 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009880 result = curbuf->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009881 break;
9882
9883 case SPEC_HASH: /* '#' or "#99": alternate file */
9884 if (src[1] == '#') /* "##": the argument list */
9885 {
9886 result = arg_all();
9887 resultbuf = result;
9888 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009889 if (escaped != NULL)
9890 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009891#ifdef FEAT_MODIFY_FNAME
9892 skip_mod = TRUE;
9893#endif
9894 break;
9895 }
9896 s = src + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00009897 if (*s == '<') /* "#<99" uses v:oldfiles */
9898 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009899 i = (int)getdigits(&s);
9900 *usedlen = (int)(s - src); /* length of what we expand */
9901
Bram Moolenaard812df62008-11-09 12:46:09 +00009902 if (src[1] == '<')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009903 {
Bram Moolenaard812df62008-11-09 12:46:09 +00009904 if (*usedlen < 2)
9905 {
9906 /* Should we give an error message for #<text? */
9907 *usedlen = 1;
9908 return NULL;
9909 }
9910#ifdef FEAT_EVAL
9911 result = list_find_str(get_vim_var_list(VV_OLDFILES),
9912 (long)i);
9913 if (result == NULL)
9914 {
9915 *errormsg = (char_u *)"";
9916 return NULL;
9917 }
9918#else
9919 *errormsg = (char_u *)_("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009920 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00009921#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009922 }
9923 else
Bram Moolenaard812df62008-11-09 12:46:09 +00009924 {
9925 buf = buflist_findnr(i);
9926 if (buf == NULL)
9927 {
9928 *errormsg = (char_u *)_("E194: No alternate file name to substitute for '#'");
9929 return NULL;
9930 }
9931 if (lnump != NULL)
9932 *lnump = ECMD_LAST;
9933 if (buf->b_fname == NULL)
9934 {
9935 result = (char_u *)"";
9936 valid = 0; /* Must have ":p:h" to be valid */
9937 }
9938 else
9939 result = buf->b_fname;
9940 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009941 break;
9942
9943#ifdef FEAT_SEARCHPATH
9944 case SPEC_CFILE: /* file name under cursor */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009945 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009946 if (result == NULL)
9947 {
9948 *errormsg = (char_u *)"";
9949 return NULL;
9950 }
9951 resultbuf = result; /* remember allocated string */
9952 break;
9953#endif
9954
9955#ifdef FEAT_AUTOCMD
9956 case SPEC_AFILE: /* file name for autocommand */
9957 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +00009958 if (result != NULL && !autocmd_fname_full)
9959 {
9960 /* Still need to turn the fname into a full path. It is
9961 * postponed to avoid a delay when <afile> is not used. */
9962 autocmd_fname_full = TRUE;
9963 result = FullName_save(autocmd_fname, FALSE);
9964 vim_free(autocmd_fname);
9965 autocmd_fname = result;
9966 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009967 if (result == NULL)
9968 {
9969 *errormsg = (char_u *)_("E495: no autocommand file name to substitute for \"<afile>\"");
9970 return NULL;
9971 }
Bram Moolenaara0174af2008-01-02 20:08:25 +00009972 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009973 break;
9974
9975 case SPEC_ABUF: /* buffer number for autocommand */
9976 if (autocmd_bufnr <= 0)
9977 {
9978 *errormsg = (char_u *)_("E496: no autocommand buffer number to substitute for \"<abuf>\"");
9979 return NULL;
9980 }
9981 sprintf((char *)strbuf, "%d", autocmd_bufnr);
9982 result = strbuf;
9983 break;
9984
9985 case SPEC_AMATCH: /* match name for autocommand */
9986 result = autocmd_match;
9987 if (result == NULL)
9988 {
9989 *errormsg = (char_u *)_("E497: no autocommand match name to substitute for \"<amatch>\"");
9990 return NULL;
9991 }
9992 break;
9993
9994#endif
9995 case SPEC_SFILE: /* file name for ":so" command */
9996 result = sourcing_name;
9997 if (result == NULL)
9998 {
9999 *errormsg = (char_u *)_("E498: no :source file name to substitute for \"<sfile>\"");
10000 return NULL;
10001 }
10002 break;
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010003 case SPEC_SLNUM: /* line in file for ":so" command */
10004 if (sourcing_name == NULL || sourcing_lnum == 0)
10005 {
10006 *errormsg = (char_u *)_("E842: no line number to use for \"<slnum>\"");
10007 return NULL;
10008 }
10009 sprintf((char *)strbuf, "%ld", (long)sourcing_lnum);
10010 result = strbuf;
10011 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010012#if defined(FEAT_CLIENTSERVER)
10013 case SPEC_CLIENT: /* Source of last submitted input */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010014 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
10015 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010016 result = strbuf;
10017 break;
10018#endif
10019 }
10020
10021 resultlen = (int)STRLEN(result); /* length of new string */
10022 if (src[*usedlen] == '<') /* remove the file name extension */
10023 {
10024 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010025 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010026 resultlen = (int)(s - result);
10027 }
10028#ifdef FEAT_MODIFY_FNAME
10029 else if (!skip_mod)
10030 {
10031 valid |= modify_fname(src, usedlen, &result, &resultbuf,
10032 &resultlen);
10033 if (result == NULL)
10034 {
10035 *errormsg = (char_u *)"";
10036 return NULL;
10037 }
10038 }
10039#endif
10040 }
10041
10042 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
10043 {
10044 if (valid != VALID_HEAD + VALID_PATH)
10045 /* xgettext:no-c-format */
10046 *errormsg = (char_u *)_("E499: Empty file name for '%' or '#', only works with \":p:h\"");
10047 else
10048 *errormsg = (char_u *)_("E500: Evaluates to an empty string");
10049 result = NULL;
10050 }
10051 else
10052 result = vim_strnsave(result, resultlen);
10053 vim_free(resultbuf);
10054 return result;
10055}
10056
10057/*
10058 * Concatenate all files in the argument list, separated by spaces, and return
10059 * it in one allocated string.
10060 * Spaces and backslashes in the file names are escaped with a backslash.
10061 * Returns NULL when out of memory.
10062 */
10063 static char_u *
10064arg_all()
10065{
10066 int len;
10067 int idx;
10068 char_u *retval = NULL;
10069 char_u *p;
10070
10071 /*
10072 * Do this loop two times:
10073 * first time: compute the total length
10074 * second time: concatenate the names
10075 */
10076 for (;;)
10077 {
10078 len = 0;
10079 for (idx = 0; idx < ARGCOUNT; ++idx)
10080 {
10081 p = alist_name(&ARGLIST[idx]);
10082 if (p != NULL)
10083 {
10084 if (len > 0)
10085 {
10086 /* insert a space in between names */
10087 if (retval != NULL)
10088 retval[len] = ' ';
10089 ++len;
10090 }
10091 for ( ; *p != NUL; ++p)
10092 {
10093 if (*p == ' ' || *p == '\\')
10094 {
10095 /* insert a backslash */
10096 if (retval != NULL)
10097 retval[len] = '\\';
10098 ++len;
10099 }
10100 if (retval != NULL)
10101 retval[len] = *p;
10102 ++len;
10103 }
10104 }
10105 }
10106
10107 /* second time: break here */
10108 if (retval != NULL)
10109 {
10110 retval[len] = NUL;
10111 break;
10112 }
10113
10114 /* allocate memory */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000010115 retval = alloc((unsigned)len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010116 if (retval == NULL)
10117 break;
10118 }
10119
10120 return retval;
10121}
10122
10123#if defined(FEAT_AUTOCMD) || defined(PROTO)
10124/*
10125 * Expand the <sfile> string in "arg".
10126 *
10127 * Returns an allocated string, or NULL for any error.
10128 */
10129 char_u *
10130expand_sfile(arg)
10131 char_u *arg;
10132{
10133 char_u *errormsg;
10134 int len;
10135 char_u *result;
10136 char_u *newres;
10137 char_u *repl;
10138 int srclen;
10139 char_u *p;
10140
10141 result = vim_strsave(arg);
10142 if (result == NULL)
10143 return NULL;
10144
10145 for (p = result; *p; )
10146 {
10147 if (STRNCMP(p, "<sfile>", 7) != 0)
10148 ++p;
10149 else
10150 {
10151 /* replace "<sfile>" with the sourced file name, and do ":" stuff */
Bram Moolenaar63b92542007-03-27 14:57:09 +000010152 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010153 if (errormsg != NULL)
10154 {
10155 if (*errormsg)
10156 emsg(errormsg);
10157 vim_free(result);
10158 return NULL;
10159 }
10160 if (repl == NULL) /* no match (cannot happen) */
10161 {
10162 p += srclen;
10163 continue;
10164 }
10165 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
10166 newres = alloc(len);
10167 if (newres == NULL)
10168 {
10169 vim_free(repl);
10170 vim_free(result);
10171 return NULL;
10172 }
10173 mch_memmove(newres, result, (size_t)(p - result));
10174 STRCPY(newres + (p - result), repl);
10175 len = (int)STRLEN(newres);
10176 STRCAT(newres, p + srclen);
10177 vim_free(repl);
10178 vim_free(result);
10179 result = newres;
10180 p = newres + len; /* continue after the match */
10181 }
10182 }
10183
10184 return result;
10185}
10186#endif
10187
10188#ifdef FEAT_SESSION
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010189static int ses_winsizes __ARGS((FILE *fd, int restore_size,
10190 win_T *tab_firstwin));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010191static int ses_win_rec __ARGS((FILE *fd, frame_T *fr));
10192static frame_T *ses_skipframe __ARGS((frame_T *fr));
10193static int ses_do_frame __ARGS((frame_T *fr));
10194static int ses_do_win __ARGS((win_T *wp));
10195static int ses_arglist __ARGS((FILE *fd, char *cmd, garray_T *gap, int fullname, unsigned *flagp));
10196static int ses_put_fname __ARGS((FILE *fd, char_u *name, unsigned *flagp));
10197static int ses_fname __ARGS((FILE *fd, buf_T *buf, unsigned *flagp));
10198
10199/*
10200 * Write openfile commands for the current buffers to an .exrc file.
10201 * Return FAIL on error, OK otherwise.
10202 */
10203 static int
10204makeopens(fd, dirnow)
10205 FILE *fd;
10206 char_u *dirnow; /* Current directory name */
10207{
10208 buf_T *buf;
10209 int only_save_windows = TRUE;
10210 int nr;
10211 int cnr = 1;
10212 int restore_size = TRUE;
10213 win_T *wp;
10214 char_u *sname;
10215 win_T *edited_win = NULL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010216 int tabnr;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010217 win_T *tab_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010218 frame_T *tab_topframe;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010219 int cur_arg_idx = 0;
Bram Moolenaar383c6f52008-01-04 15:01:07 +000010220 int next_arg_idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010221
10222 if (ssop_flags & SSOP_BUFFERS)
10223 only_save_windows = FALSE; /* Save ALL buffers */
10224
10225 /*
10226 * Begin by setting the this_session variable, and then other
10227 * sessionable variables.
10228 */
10229#ifdef FEAT_EVAL
10230 if (put_line(fd, "let v:this_session=expand(\"<sfile>:p\")") == FAIL)
10231 return FAIL;
10232 if (ssop_flags & SSOP_GLOBALS)
10233 if (store_session_globals(fd) == FAIL)
10234 return FAIL;
10235#endif
10236
10237 /*
10238 * Close all windows but one.
10239 */
10240 if (put_line(fd, "silent only") == FAIL)
10241 return FAIL;
10242
10243 /*
10244 * Now a :cd command to the session directory or the current directory
10245 */
10246 if (ssop_flags & SSOP_SESDIR)
10247 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010248 if (put_line(fd, "exe \"cd \" . escape(expand(\"<sfile>:p:h\"), ' ')")
10249 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010250 return FAIL;
10251 }
10252 else if (ssop_flags & SSOP_CURDIR)
10253 {
10254 sname = home_replace_save(NULL, globaldir != NULL ? globaldir : dirnow);
10255 if (sname == NULL
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010256 || fputs("cd ", fd) < 0
10257 || ses_put_fname(fd, sname, &ssop_flags) == FAIL
10258 || put_eol(fd) == FAIL)
10259 {
10260 vim_free(sname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010261 return FAIL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010262 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010263 vim_free(sname);
10264 }
10265
10266 /*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010267 * If there is an empty, unnamed buffer we will wipe it out later.
10268 * Remember the buffer number.
10269 */
10270 if (put_line(fd, "if expand('%') == '' && !&modified && line('$') <= 1 && getline(1) == ''") == FAIL)
10271 return FAIL;
10272 if (put_line(fd, " let s:wipebuf = bufnr('%')") == FAIL)
10273 return FAIL;
10274 if (put_line(fd, "endif") == FAIL)
10275 return FAIL;
10276
10277 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010278 * Now save the current files, current buffer first.
10279 */
10280 if (put_line(fd, "set shortmess=aoO") == FAIL)
10281 return FAIL;
10282
10283 /* Now put the other buffers into the buffer list */
10284 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
10285 {
10286 if (!(only_save_windows && buf->b_nwindows == 0)
10287 && !(buf->b_help && !(ssop_flags & SSOP_HELP))
10288 && buf->b_fname != NULL
10289 && buf->b_p_bl)
10290 {
10291 if (fprintf(fd, "badd +%ld ", buf->b_wininfo == NULL ? 1L
10292 : buf->b_wininfo->wi_fpos.lnum) < 0
10293 || ses_fname(fd, buf, &ssop_flags) == FAIL)
10294 return FAIL;
10295 }
10296 }
10297
10298 /* the global argument list */
10299 if (ses_arglist(fd, "args", &global_alist.al_ga,
10300 !(ssop_flags & SSOP_CURDIR), &ssop_flags) == FAIL)
10301 return FAIL;
10302
10303 if (ssop_flags & SSOP_RESIZE)
10304 {
10305 /* Note: after the restore we still check it worked!*/
10306 if (fprintf(fd, "set lines=%ld columns=%ld" , Rows, Columns) < 0
10307 || put_eol(fd) == FAIL)
10308 return FAIL;
10309 }
10310
10311#ifdef FEAT_GUI
10312 if (gui.in_use && (ssop_flags & SSOP_WINPOS))
10313 {
10314 int x, y;
10315
10316 if (gui_mch_get_winpos(&x, &y) == OK)
10317 {
10318 /* Note: after the restore we still check it worked!*/
10319 if (fprintf(fd, "winpos %d %d", x, y) < 0 || put_eol(fd) == FAIL)
10320 return FAIL;
10321 }
10322 }
10323#endif
10324
10325 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000010326 * May repeat putting Windows for each tab, when "tabpages" is in
10327 * 'sessionoptions'.
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010328 * Don't use goto_tabpage(), it may change directory and trigger
10329 * autocommands.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010330 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010331 tab_firstwin = firstwin; /* first window in tab page "tabnr" */
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010332 tab_topframe = topframe;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010333 for (tabnr = 1; ; ++tabnr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010334 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010335 int need_tabnew = FALSE;
10336
Bram Moolenaar18144c82006-04-12 21:52:12 +000010337 if ((ssop_flags & SSOP_TABPAGES))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010338 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010339 tabpage_T *tp = find_tabpage(tabnr);
10340
10341 if (tp == NULL)
10342 break; /* done all tab pages */
10343 if (tp == curtab)
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010344 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010345 tab_firstwin = firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010346 tab_topframe = topframe;
10347 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010348 else
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010349 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010350 tab_firstwin = tp->tp_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010351 tab_topframe = tp->tp_topframe;
10352 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010353 if (tabnr > 1)
10354 need_tabnew = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010355 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010356
Bram Moolenaar18144c82006-04-12 21:52:12 +000010357 /*
10358 * Before creating the window layout, try loading one file. If this
10359 * is aborted we don't end up with a number of useless windows.
10360 * This may have side effects! (e.g., compressed or network file).
10361 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010362 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010363 {
10364 if (ses_do_win(wp)
10365 && wp->w_buffer->b_ffname != NULL
10366 && !wp->w_buffer->b_help
10367#ifdef FEAT_QUICKFIX
10368 && !bt_nofile(wp->w_buffer)
10369#endif
10370 )
10371 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010372 if (fputs(need_tabnew ? "tabedit " : "edit ", fd) < 0
Bram Moolenaar18144c82006-04-12 21:52:12 +000010373 || ses_fname(fd, wp->w_buffer, &ssop_flags) == FAIL)
10374 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010375 need_tabnew = FALSE;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010376 if (!wp->w_arg_idx_invalid)
10377 edited_win = wp;
10378 break;
10379 }
10380 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010381
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010382 /* If no file got edited create an empty tab page. */
10383 if (need_tabnew && put_line(fd, "tabnew") == FAIL)
10384 return FAIL;
10385
Bram Moolenaar18144c82006-04-12 21:52:12 +000010386 /*
10387 * Save current window layout.
10388 */
10389 if (put_line(fd, "set splitbelow splitright") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010390 return FAIL;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010391 if (ses_win_rec(fd, tab_topframe) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010392 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010393 if (!p_sb && put_line(fd, "set nosplitbelow") == FAIL)
10394 return FAIL;
10395 if (!p_spr && put_line(fd, "set nosplitright") == FAIL)
10396 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010397
Bram Moolenaar18144c82006-04-12 21:52:12 +000010398 /*
10399 * Check if window sizes can be restored (no windows omitted).
10400 * Remember the window number of the current window after restoring.
10401 */
10402 nr = 0;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010403 for (wp = tab_firstwin; wp != NULL; wp = W_NEXT(wp))
Bram Moolenaar18144c82006-04-12 21:52:12 +000010404 {
10405 if (ses_do_win(wp))
10406 ++nr;
10407 else
10408 restore_size = FALSE;
10409 if (curwin == wp)
10410 cnr = nr;
10411 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010412
Bram Moolenaar18144c82006-04-12 21:52:12 +000010413 /* Go to the first window. */
10414 if (put_line(fd, "wincmd t") == FAIL)
10415 return FAIL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010416
Bram Moolenaar18144c82006-04-12 21:52:12 +000010417 /*
10418 * If more than one window, see if sizes can be restored.
10419 * First set 'winheight' and 'winwidth' to 1 to avoid the windows being
10420 * resized when moving between windows.
10421 * Do this before restoring the view, so that the topline and the
10422 * cursor can be set. This is done again below.
10423 */
10424 if (put_line(fd, "set winheight=1 winwidth=1") == FAIL)
10425 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010426 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010427 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010428
Bram Moolenaar18144c82006-04-12 21:52:12 +000010429 /*
10430 * Restore the view of the window (options, file, cursor, etc.).
10431 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010432 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010433 {
10434 if (!ses_do_win(wp))
10435 continue;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010436 if (put_view(fd, wp, wp != edited_win, &ssop_flags,
10437 cur_arg_idx) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010438 return FAIL;
10439 if (nr > 1 && put_line(fd, "wincmd w") == FAIL)
10440 return FAIL;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010441 next_arg_idx = wp->w_arg_idx;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010442 }
10443
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010444 /* The argument index in the first tab page is zero, need to set it in
10445 * each window. For further tab pages it's the window where we do
10446 * "tabedit". */
10447 cur_arg_idx = next_arg_idx;
10448
Bram Moolenaar18144c82006-04-12 21:52:12 +000010449 /*
10450 * Restore cursor to the current window if it's not the first one.
10451 */
10452 if (cnr > 1 && (fprintf(fd, "%dwincmd w", cnr) < 0
10453 || put_eol(fd) == FAIL))
10454 return FAIL;
10455
10456 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000010457 * Restore window sizes again after jumping around in windows, because
10458 * the current window has a minimum size while others may not.
10459 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010460 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010461 return FAIL;
10462
Bram Moolenaar18144c82006-04-12 21:52:12 +000010463 /* Don't continue in another tab page when doing only the current one
10464 * or when at the last tab page. */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010465 if (!(ssop_flags & SSOP_TABPAGES))
Bram Moolenaar18144c82006-04-12 21:52:12 +000010466 break;
10467 }
10468
10469 if (ssop_flags & SSOP_TABPAGES)
10470 {
Bram Moolenaar18144c82006-04-12 21:52:12 +000010471 if (fprintf(fd, "tabnext %d", tabpage_index(curtab)) < 0
10472 || put_eol(fd) == FAIL)
10473 return FAIL;
10474 }
10475
Bram Moolenaar9c102382006-05-03 21:26:49 +000010476 /*
10477 * Wipe out an empty unnamed buffer we started in.
10478 */
10479 if (put_line(fd, "if exists('s:wipebuf')") == FAIL)
10480 return FAIL;
Bram Moolenaarf3a67882006-05-05 21:09:41 +000010481 if (put_line(fd, " silent exe 'bwipe ' . s:wipebuf") == FAIL)
Bram Moolenaar9c102382006-05-03 21:26:49 +000010482 return FAIL;
10483 if (put_line(fd, "endif") == FAIL)
10484 return FAIL;
10485 if (put_line(fd, "unlet! s:wipebuf") == FAIL)
10486 return FAIL;
10487
10488 /* Re-apply 'winheight', 'winwidth' and 'shortmess'. */
10489 if (fprintf(fd, "set winheight=%ld winwidth=%ld shortmess=%s",
10490 p_wh, p_wiw, p_shm) < 0 || put_eol(fd) == FAIL)
10491 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010492
10493 /*
10494 * Lastly, execute the x.vim file if it exists.
10495 */
10496 if (put_line(fd, "let s:sx = expand(\"<sfile>:p:r\").\"x.vim\"") == FAIL
10497 || put_line(fd, "if file_readable(s:sx)") == FAIL
Bram Moolenaar42ba1262008-12-09 10:18:03 +000010498 || put_line(fd, " exe \"source \" . fnameescape(s:sx)") == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000010499 || put_line(fd, "endif") == FAIL)
10500 return FAIL;
10501
10502 return OK;
10503}
10504
10505 static int
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010506ses_winsizes(fd, restore_size, tab_firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010507 FILE *fd;
10508 int restore_size;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010509 win_T *tab_firstwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010510{
10511 int n = 0;
10512 win_T *wp;
10513
10514 if (restore_size && (ssop_flags & SSOP_WINSIZE))
10515 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010516 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010517 {
10518 if (!ses_do_win(wp))
10519 continue;
10520 ++n;
10521
10522 /* restore height when not full height */
10523 if (wp->w_height + wp->w_status_height < topframe->fr_height
10524 && (fprintf(fd,
10525 "exe '%dresize ' . ((&lines * %ld + %ld) / %ld)",
10526 n, (long)wp->w_height, Rows / 2, Rows) < 0
10527 || put_eol(fd) == FAIL))
10528 return FAIL;
10529
10530 /* restore width when not full width */
10531 if (wp->w_width < Columns && (fprintf(fd,
10532 "exe 'vert %dresize ' . ((&columns * %ld + %ld) / %ld)",
10533 n, (long)wp->w_width, Columns / 2, Columns) < 0
10534 || put_eol(fd) == FAIL))
10535 return FAIL;
10536 }
10537 }
10538 else
10539 {
10540 /* Just equalise window sizes */
10541 if (put_line(fd, "wincmd =") == FAIL)
10542 return FAIL;
10543 }
10544 return OK;
10545}
10546
10547/*
10548 * Write commands to "fd" to recursively create windows for frame "fr",
10549 * horizontally and vertically split.
10550 * After the commands the last window in the frame is the current window.
10551 * Returns FAIL when writing the commands to "fd" fails.
10552 */
10553 static int
10554ses_win_rec(fd, fr)
10555 FILE *fd;
10556 frame_T *fr;
10557{
10558 frame_T *frc;
10559 int count = 0;
10560
10561 if (fr->fr_layout != FR_LEAF)
10562 {
10563 /* Find first frame that's not skipped and then create a window for
10564 * each following one (first frame is already there). */
10565 frc = ses_skipframe(fr->fr_child);
10566 if (frc != NULL)
10567 while ((frc = ses_skipframe(frc->fr_next)) != NULL)
10568 {
10569 /* Make window as big as possible so that we have lots of room
10570 * to split. */
10571 if (put_line(fd, "wincmd _ | wincmd |") == FAIL
10572 || put_line(fd, fr->fr_layout == FR_COL
10573 ? "split" : "vsplit") == FAIL)
10574 return FAIL;
10575 ++count;
10576 }
10577
10578 /* Go back to the first window. */
10579 if (count > 0 && (fprintf(fd, fr->fr_layout == FR_COL
10580 ? "%dwincmd k" : "%dwincmd h", count) < 0
10581 || put_eol(fd) == FAIL))
10582 return FAIL;
10583
10584 /* Recursively create frames/windows in each window of this column or
10585 * row. */
10586 frc = ses_skipframe(fr->fr_child);
10587 while (frc != NULL)
10588 {
10589 ses_win_rec(fd, frc);
10590 frc = ses_skipframe(frc->fr_next);
10591 /* Go to next window. */
10592 if (frc != NULL && put_line(fd, "wincmd w") == FAIL)
10593 return FAIL;
10594 }
10595 }
10596 return OK;
10597}
10598
10599/*
10600 * Skip frames that don't contain windows we want to save in the Session.
10601 * Returns NULL when there none.
10602 */
10603 static frame_T *
10604ses_skipframe(fr)
10605 frame_T *fr;
10606{
10607 frame_T *frc;
10608
10609 for (frc = fr; frc != NULL; frc = frc->fr_next)
10610 if (ses_do_frame(frc))
10611 break;
10612 return frc;
10613}
10614
10615/*
10616 * Return TRUE if frame "fr" has a window somewhere that we want to save in
10617 * the Session.
10618 */
10619 static int
10620ses_do_frame(fr)
10621 frame_T *fr;
10622{
10623 frame_T *frc;
10624
10625 if (fr->fr_layout == FR_LEAF)
10626 return ses_do_win(fr->fr_win);
10627 for (frc = fr->fr_child; frc != NULL; frc = frc->fr_next)
10628 if (ses_do_frame(frc))
10629 return TRUE;
10630 return FALSE;
10631}
10632
10633/*
10634 * Return non-zero if window "wp" is to be stored in the Session.
10635 */
10636 static int
10637ses_do_win(wp)
10638 win_T *wp;
10639{
10640 if (wp->w_buffer->b_fname == NULL
10641#ifdef FEAT_QUICKFIX
10642 /* When 'buftype' is "nofile" can't restore the window contents. */
10643 || bt_nofile(wp->w_buffer)
10644#endif
10645 )
10646 return (ssop_flags & SSOP_BLANK);
10647 if (wp->w_buffer->b_help)
10648 return (ssop_flags & SSOP_HELP);
10649 return TRUE;
10650}
10651
10652/*
10653 * Write commands to "fd" to restore the view of a window.
10654 * Caller must make sure 'scrolloff' is zero.
10655 */
10656 static int
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010657put_view(fd, wp, add_edit, flagp, current_arg_idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010658 FILE *fd;
10659 win_T *wp;
10660 int add_edit; /* add ":edit" command to view */
10661 unsigned *flagp; /* vop_flags or ssop_flags */
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010662 int current_arg_idx; /* current argument index of the window, use
10663 * -1 if unknown */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010664{
10665 win_T *save_curwin;
10666 int f;
10667 int do_cursor;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010668 int did_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010669
10670 /* Always restore cursor position for ":mksession". For ":mkview" only
10671 * when 'viewoptions' contains "cursor". */
10672 do_cursor = (flagp == &ssop_flags || *flagp & SSOP_CURSOR);
10673
10674 /*
10675 * Local argument list.
10676 */
10677 if (wp->w_alist == &global_alist)
10678 {
10679 if (put_line(fd, "argglobal") == FAIL)
10680 return FAIL;
10681 }
10682 else
10683 {
10684 if (ses_arglist(fd, "arglocal", &wp->w_alist->al_ga,
10685 flagp == &vop_flags
10686 || !(*flagp & SSOP_CURDIR)
10687 || wp->w_localdir != NULL, flagp) == FAIL)
10688 return FAIL;
10689 }
10690
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010691 /* Only when part of a session: restore the argument index. Some
10692 * arguments may have been deleted, check if the index is valid. */
Bram Moolenaar51f53df2010-06-26 05:25:54 +020010693 if (wp->w_arg_idx != current_arg_idx && wp->w_arg_idx < WARGCOUNT(wp)
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010694 && flagp == &ssop_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010695 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010696 if (fprintf(fd, "%ldargu", (long)wp->w_arg_idx + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010697 || put_eol(fd) == FAIL)
10698 return FAIL;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010699 did_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010700 }
10701
10702 /* Edit the file. Skip this when ":next" already did it. */
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010703 if (add_edit && (!did_next || wp->w_arg_idx_invalid))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010704 {
10705 /*
10706 * Load the file.
10707 */
10708 if (wp->w_buffer->b_ffname != NULL
10709#ifdef FEAT_QUICKFIX
10710 && !bt_nofile(wp->w_buffer)
10711#endif
10712 )
10713 {
10714 /*
10715 * Editing a file in this buffer: use ":edit file".
10716 * This may have side effects! (e.g., compressed or network file).
10717 */
10718 if (fputs("edit ", fd) < 0
10719 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
10720 return FAIL;
10721 }
10722 else
10723 {
10724 /* No file in this buffer, just make it empty. */
10725 if (put_line(fd, "enew") == FAIL)
10726 return FAIL;
10727#ifdef FEAT_QUICKFIX
10728 if (wp->w_buffer->b_ffname != NULL)
10729 {
10730 /* The buffer does have a name, but it's not a file name. */
10731 if (fputs("file ", fd) < 0
10732 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
10733 return FAIL;
10734 }
10735#endif
10736 do_cursor = FALSE;
10737 }
10738 }
10739
10740 /*
10741 * Local mappings and abbreviations.
10742 */
10743 if ((*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
10744 && makemap(fd, wp->w_buffer) == FAIL)
10745 return FAIL;
10746
10747 /*
10748 * Local options. Need to go to the window temporarily.
10749 * Store only local values when using ":mkview" and when ":mksession" is
10750 * used and 'sessionoptions' doesn't include "options".
10751 * Some folding options are always stored when "folds" is included,
10752 * otherwise the folds would not be restored correctly.
10753 */
10754 save_curwin = curwin;
10755 curwin = wp;
10756 curbuf = curwin->w_buffer;
10757 if (*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
10758 f = makeset(fd, OPT_LOCAL,
10759 flagp == &vop_flags || !(*flagp & SSOP_OPTIONS));
10760#ifdef FEAT_FOLDING
10761 else if (*flagp & SSOP_FOLDS)
10762 f = makefoldset(fd);
10763#endif
10764 else
10765 f = OK;
10766 curwin = save_curwin;
10767 curbuf = curwin->w_buffer;
10768 if (f == FAIL)
10769 return FAIL;
10770
10771#ifdef FEAT_FOLDING
10772 /*
10773 * Save Folds when 'buftype' is empty and for help files.
10774 */
10775 if ((*flagp & SSOP_FOLDS)
10776 && wp->w_buffer->b_ffname != NULL
Bram Moolenaarb1b715d2006-01-21 22:09:43 +000010777# ifdef FEAT_QUICKFIX
10778 && (*wp->w_buffer->b_p_bt == NUL || wp->w_buffer->b_help)
10779# endif
10780 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000010781 {
10782 if (put_folds(fd, wp) == FAIL)
10783 return FAIL;
10784 }
10785#endif
10786
10787 /*
10788 * Set the cursor after creating folds, since that moves the cursor.
10789 */
10790 if (do_cursor)
10791 {
10792
10793 /* Restore the cursor line in the file and relatively in the
10794 * window. Don't use "G", it changes the jumplist. */
10795 if (fprintf(fd, "let s:l = %ld - ((%ld * winheight(0) + %ld) / %ld)",
10796 (long)wp->w_cursor.lnum,
10797 (long)(wp->w_cursor.lnum - wp->w_topline),
10798 (long)wp->w_height / 2, (long)wp->w_height) < 0
10799 || put_eol(fd) == FAIL
10800 || put_line(fd, "if s:l < 1 | let s:l = 1 | endif") == FAIL
10801 || put_line(fd, "exe s:l") == FAIL
10802 || put_line(fd, "normal! zt") == FAIL
10803 || fprintf(fd, "%ld", (long)wp->w_cursor.lnum) < 0
10804 || put_eol(fd) == FAIL)
10805 return FAIL;
10806 /* Restore the cursor column and left offset when not wrapping. */
10807 if (wp->w_cursor.col == 0)
10808 {
10809 if (put_line(fd, "normal! 0") == FAIL)
10810 return FAIL;
10811 }
10812 else
10813 {
10814 if (!wp->w_p_wrap && wp->w_leftcol > 0 && wp->w_width > 0)
10815 {
10816 if (fprintf(fd,
10817 "let s:c = %ld - ((%ld * winwidth(0) + %ld) / %ld)",
10818 (long)wp->w_cursor.col,
10819 (long)(wp->w_cursor.col - wp->w_leftcol),
10820 (long)wp->w_width / 2, (long)wp->w_width) < 0
10821 || put_eol(fd) == FAIL
10822 || put_line(fd, "if s:c > 0") == FAIL
10823 || fprintf(fd,
10824 " exe 'normal! 0' . s:c . 'lzs' . (%ld - s:c) . 'l'",
10825 (long)wp->w_cursor.col) < 0
10826 || put_eol(fd) == FAIL
10827 || put_line(fd, "else") == FAIL
10828 || fprintf(fd, " normal! 0%dl", wp->w_cursor.col) < 0
10829 || put_eol(fd) == FAIL
10830 || put_line(fd, "endif") == FAIL)
10831 return FAIL;
10832 }
10833 else
10834 {
10835 if (fprintf(fd, "normal! 0%dl", wp->w_cursor.col) < 0
10836 || put_eol(fd) == FAIL)
10837 return FAIL;
10838 }
10839 }
10840 }
10841
10842 /*
10843 * Local directory.
10844 */
10845 if (wp->w_localdir != NULL)
10846 {
10847 if (fputs("lcd ", fd) < 0
10848 || ses_put_fname(fd, wp->w_localdir, flagp) == FAIL
10849 || put_eol(fd) == FAIL)
10850 return FAIL;
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010851 did_lcd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010852 }
10853
10854 return OK;
10855}
10856
10857/*
10858 * Write an argument list to the session file.
10859 * Returns FAIL if writing fails.
10860 */
10861 static int
10862ses_arglist(fd, cmd, gap, fullname, flagp)
10863 FILE *fd;
10864 char *cmd;
10865 garray_T *gap;
10866 int fullname; /* TRUE: use full path name */
10867 unsigned *flagp;
10868{
10869 int i;
Bram Moolenaard9462e32011-04-11 21:35:11 +020010870 char_u *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010871 char_u *s;
10872
10873 if (gap->ga_len == 0)
10874 return put_line(fd, "silent! argdel *");
10875 if (fputs(cmd, fd) < 0)
10876 return FAIL;
10877 for (i = 0; i < gap->ga_len; ++i)
10878 {
10879 /* NULL file names are skipped (only happens when out of memory). */
10880 s = alist_name(&((aentry_T *)gap->ga_data)[i]);
10881 if (s != NULL)
10882 {
10883 if (fullname)
10884 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020010885 buf = alloc(MAXPATHL);
10886 if (buf != NULL)
10887 {
10888 (void)vim_FullName(s, buf, MAXPATHL, FALSE);
10889 s = buf;
10890 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010891 }
10892 if (fputs(" ", fd) < 0 || ses_put_fname(fd, s, flagp) == FAIL)
Bram Moolenaard9462e32011-04-11 21:35:11 +020010893 {
10894 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010895 return FAIL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020010896 }
10897 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010898 }
10899 }
10900 return put_eol(fd);
10901}
10902
10903/*
10904 * Write a buffer name to the session file.
10905 * Also ends the line.
10906 * Returns FAIL if writing fails.
10907 */
10908 static int
10909ses_fname(fd, buf, flagp)
10910 FILE *fd;
10911 buf_T *buf;
10912 unsigned *flagp;
10913{
10914 char_u *name;
10915
10916 /* Use the short file name if the current directory is known at the time
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010917 * the session file will be sourced.
10918 * Don't do this for ":mkview", we don't know the current directory.
10919 * Don't do this after ":lcd", we don't keep track of what the current
10920 * directory is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010921 if (buf->b_sfname != NULL
10922 && flagp == &ssop_flags
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010923 && (ssop_flags & (SSOP_CURDIR | SSOP_SESDIR))
Bram Moolenaar8d594672009-07-01 18:18:57 +000010924#ifdef FEAT_AUTOCHDIR
10925 && !p_acd
10926#endif
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010927 && !did_lcd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010928 name = buf->b_sfname;
10929 else
10930 name = buf->b_ffname;
10931 if (ses_put_fname(fd, name, flagp) == FAIL || put_eol(fd) == FAIL)
10932 return FAIL;
10933 return OK;
10934}
10935
10936/*
10937 * Write a file name to the session file.
10938 * Takes care of the "slash" option in 'sessionoptions' and escapes special
10939 * characters.
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010940 * Returns FAIL if writing fails or out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010941 */
10942 static int
10943ses_put_fname(fd, name, flagp)
10944 FILE *fd;
10945 char_u *name;
10946 unsigned *flagp;
10947{
10948 char_u *sname;
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010949 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010950 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010951
10952 sname = home_replace_save(NULL, name);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010953 if (sname == NULL)
10954 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010955
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010956 if (*flagp & SSOP_SLASH)
10957 {
10958 /* change all backslashes to forward slashes */
10959 for (p = sname; *p != NUL; mb_ptr_adv(p))
10960 if (*p == '\\')
10961 *p = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010962 }
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010963
10964 /* escapse special characters */
10965 p = vim_strsave_fnameescape(sname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010966 vim_free(sname);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010967 if (p == NULL)
10968 return FAIL;
10969
10970 /* write the result */
10971 if (fputs((char *)p, fd) < 0)
10972 retval = FAIL;
10973
10974 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010975 return retval;
10976}
10977
10978/*
10979 * ":loadview [nr]"
10980 */
10981 static void
10982ex_loadview(eap)
10983 exarg_T *eap;
10984{
10985 char_u *fname;
10986
10987 fname = get_view_file(*eap->arg);
10988 if (fname != NULL)
10989 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010990 do_source(fname, FALSE, DOSO_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010991 vim_free(fname);
10992 }
10993}
10994
10995/*
10996 * Get the name of the view file for the current buffer.
10997 */
10998 static char_u *
10999get_view_file(c)
11000 int c;
11001{
11002 int len = 0;
11003 char_u *p, *s;
11004 char_u *retval;
11005 char_u *sname;
11006
11007 if (curbuf->b_ffname == NULL)
11008 {
11009 EMSG(_(e_noname));
11010 return NULL;
11011 }
11012 sname = home_replace_save(NULL, curbuf->b_ffname);
11013 if (sname == NULL)
11014 return NULL;
11015
11016 /*
11017 * We want a file name without separators, because we're not going to make
11018 * a directory.
11019 * "normal" path separator -> "=+"
11020 * "=" -> "=="
11021 * ":" path separator -> "=-"
11022 */
11023 for (p = sname; *p; ++p)
11024 if (*p == '=' || vim_ispathsep(*p))
11025 ++len;
11026 retval = alloc((unsigned)(STRLEN(sname) + len + STRLEN(p_vdir) + 9));
11027 if (retval != NULL)
11028 {
11029 STRCPY(retval, p_vdir);
11030 add_pathsep(retval);
11031 s = retval + STRLEN(retval);
11032 for (p = sname; *p; ++p)
11033 {
11034 if (*p == '=')
11035 {
11036 *s++ = '=';
11037 *s++ = '=';
11038 }
11039 else if (vim_ispathsep(*p))
11040 {
11041 *s++ = '=';
Bram Moolenaare60acc12011-05-10 16:41:25 +020011042#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011043 if (*p == ':')
11044 *s++ = '-';
11045 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000011046#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000011047 *s++ = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +000011048 }
11049 else
11050 *s++ = *p;
11051 }
11052 *s++ = '=';
11053 *s++ = c;
11054 STRCPY(s, ".vim");
11055 }
11056
11057 vim_free(sname);
11058 return retval;
11059}
11060
11061#endif /* FEAT_SESSION */
11062
11063/*
11064 * Write end-of-line character(s) for ":mkexrc", ":mkvimrc" and ":mksession".
11065 * Return FAIL for a write error.
11066 */
11067 int
11068put_eol(fd)
11069 FILE *fd;
11070{
11071 if (
11072#ifdef USE_CRNL
11073 (
11074# ifdef MKSESSION_NL
11075 !mksession_nl &&
11076# endif
11077 (putc('\r', fd) < 0)) ||
11078#endif
11079 (putc('\n', fd) < 0))
11080 return FAIL;
11081 return OK;
11082}
11083
11084/*
11085 * Write a line to "fd".
11086 * Return FAIL for a write error.
11087 */
11088 int
11089put_line(fd, s)
11090 FILE *fd;
11091 char *s;
11092{
11093 if (fputs(s, fd) < 0 || put_eol(fd) == FAIL)
11094 return FAIL;
11095 return OK;
11096}
11097
11098#ifdef FEAT_VIMINFO
11099/*
11100 * ":rviminfo" and ":wviminfo".
11101 */
11102 static void
11103ex_viminfo(eap)
11104 exarg_T *eap;
11105{
11106 char_u *save_viminfo;
11107
11108 save_viminfo = p_viminfo;
11109 if (*p_viminfo == NUL)
11110 p_viminfo = (char_u *)"'100";
11111 if (eap->cmdidx == CMD_rviminfo)
11112 {
Bram Moolenaard812df62008-11-09 12:46:09 +000011113 if (read_viminfo(eap->arg, VIF_WANT_INFO | VIF_WANT_MARKS
11114 | (eap->forceit ? VIF_FORCEIT : 0)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011115 EMSG(_("E195: Cannot open viminfo file for reading"));
11116 }
11117 else
11118 write_viminfo(eap->arg, eap->forceit);
11119 p_viminfo = save_viminfo;
11120}
11121#endif
11122
11123#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +000011124/*
Bram Moolenaard9462e32011-04-11 21:35:11 +020011125 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +000011126 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +000011127 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011128 void
11129dialog_msg(buff, format, fname)
11130 char_u *buff;
11131 char *format;
11132 char_u *fname;
11133{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011134 if (fname == NULL)
11135 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +020011136 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011137}
11138#endif
11139
11140/*
11141 * ":behave {mswin,xterm}"
11142 */
11143 static void
11144ex_behave(eap)
11145 exarg_T *eap;
11146{
11147 if (STRCMP(eap->arg, "mswin") == 0)
11148 {
11149 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
11150 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
11151 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
11152 set_option_value((char_u *)"keymodel", 0L,
11153 (char_u *)"startsel,stopsel", 0);
11154 }
11155 else if (STRCMP(eap->arg, "xterm") == 0)
11156 {
11157 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
11158 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
11159 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
11160 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
11161 }
11162 else
11163 EMSG2(_(e_invarg2), eap->arg);
11164}
11165
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010011166#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
11167/*
11168 * Function given to ExpandGeneric() to obtain the possible arguments of the
11169 * ":behave {mswin,xterm}" command.
11170 */
11171 char_u *
11172get_behave_arg(xp, idx)
11173 expand_T *xp UNUSED;
11174 int idx;
11175{
11176 if (idx == 0)
11177 return (char_u *)"mswin";
11178 if (idx == 1)
11179 return (char_u *)"xterm";
11180 return NULL;
11181}
11182#endif
11183
Bram Moolenaar071d4272004-06-13 20:20:40 +000011184#ifdef FEAT_AUTOCMD
11185static int filetype_detect = FALSE;
11186static int filetype_plugin = FALSE;
11187static int filetype_indent = FALSE;
11188
11189/*
11190 * ":filetype [plugin] [indent] {on,off,detect}"
11191 * on: Load the filetype.vim file to install autocommands for file types.
11192 * off: Load the ftoff.vim file to remove all autocommands for file types.
11193 * plugin on: load filetype.vim and ftplugin.vim
11194 * plugin off: load ftplugof.vim
11195 * indent on: load filetype.vim and indent.vim
11196 * indent off: load indoff.vim
11197 */
11198 static void
11199ex_filetype(eap)
11200 exarg_T *eap;
11201{
11202 char_u *arg = eap->arg;
11203 int plugin = FALSE;
11204 int indent = FALSE;
11205
11206 if (*eap->arg == NUL)
11207 {
11208 /* Print current status. */
11209 smsg((char_u *)"filetype detection:%s plugin:%s indent:%s",
11210 filetype_detect ? "ON" : "OFF",
11211 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
11212 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
11213 return;
11214 }
11215
11216 /* Accept "plugin" and "indent" in any order. */
11217 for (;;)
11218 {
11219 if (STRNCMP(arg, "plugin", 6) == 0)
11220 {
11221 plugin = TRUE;
11222 arg = skipwhite(arg + 6);
11223 continue;
11224 }
11225 if (STRNCMP(arg, "indent", 6) == 0)
11226 {
11227 indent = TRUE;
11228 arg = skipwhite(arg + 6);
11229 continue;
11230 }
11231 break;
11232 }
11233 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
11234 {
11235 if (*arg == 'o' || !filetype_detect)
11236 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011237 source_runtime((char_u *)FILETYPE_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011238 filetype_detect = TRUE;
11239 if (plugin)
11240 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011241 source_runtime((char_u *)FTPLUGIN_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011242 filetype_plugin = TRUE;
11243 }
11244 if (indent)
11245 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011246 source_runtime((char_u *)INDENT_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011247 filetype_indent = TRUE;
11248 }
11249 }
11250 if (*arg == 'd')
11251 {
11252 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE);
Bram Moolenaara3227e22006-03-08 21:32:40 +000011253 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011254 }
11255 }
11256 else if (STRCMP(arg, "off") == 0)
11257 {
11258 if (plugin || indent)
11259 {
11260 if (plugin)
11261 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011262 source_runtime((char_u *)FTPLUGOF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011263 filetype_plugin = FALSE;
11264 }
11265 if (indent)
11266 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011267 source_runtime((char_u *)INDOFF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011268 filetype_indent = FALSE;
11269 }
11270 }
11271 else
11272 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011273 source_runtime((char_u *)FTOFF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011274 filetype_detect = FALSE;
11275 }
11276 }
11277 else
11278 EMSG2(_(e_invarg2), arg);
11279}
11280
11281/*
11282 * ":setfiletype {name}"
11283 */
11284 static void
11285ex_setfiletype(eap)
11286 exarg_T *eap;
11287{
11288 if (!did_filetype)
11289 set_option_value((char_u *)"filetype", 0L, eap->arg, OPT_LOCAL);
11290}
11291#endif
11292
Bram Moolenaar071d4272004-06-13 20:20:40 +000011293 static void
11294ex_digraphs(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +000011295 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011296{
11297#ifdef FEAT_DIGRAPHS
11298 if (*eap->arg != NUL)
11299 putdigraph(eap->arg);
11300 else
11301 listdigraphs();
11302#else
11303 EMSG(_("E196: No digraphs in this version"));
11304#endif
11305}
11306
11307 static void
11308ex_set(eap)
11309 exarg_T *eap;
11310{
11311 int flags = 0;
11312
11313 if (eap->cmdidx == CMD_setlocal)
11314 flags = OPT_LOCAL;
11315 else if (eap->cmdidx == CMD_setglobal)
11316 flags = OPT_GLOBAL;
11317#if defined(FEAT_EVAL) && defined(FEAT_AUTOCMD) && defined(FEAT_BROWSE)
11318 if (cmdmod.browse && flags == 0)
11319 ex_options(eap);
11320 else
11321#endif
11322 (void)do_set(eap->arg, flags);
11323}
11324
11325#ifdef FEAT_SEARCH_EXTRA
11326/*
11327 * ":nohlsearch"
11328 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011329 static void
11330ex_nohlsearch(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +000011331 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011332{
11333 no_hlsearch = TRUE;
Bram Moolenaarf71a3db2006-03-12 21:50:18 +000011334 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011335}
11336
11337/*
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011338 * ":[N]match {group} {pattern}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000011339 * Sets nextcmd to the start of the next command, if any. Also called when
11340 * skipping commands to find the next command.
11341 */
11342 static void
11343ex_match(eap)
11344 exarg_T *eap;
11345{
11346 char_u *p;
Bram Moolenaar52d36c82007-08-11 14:00:30 +000011347 char_u *g = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011348 char_u *end;
11349 int c;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011350 int id;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011351
11352 if (eap->line2 <= 3)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011353 id = eap->line2;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011354 else
11355 {
11356 EMSG(e_invcmd);
11357 return;
11358 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011359
11360 /* First clear any old pattern. */
11361 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011362 match_delete(curwin, id, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011363
11364 if (ends_excmd(*eap->arg))
11365 end = eap->arg;
11366 else if ((STRNICMP(eap->arg, "none", 4) == 0
11367 && (vim_iswhite(eap->arg[4]) || ends_excmd(eap->arg[4]))))
11368 end = eap->arg + 4;
11369 else
11370 {
11371 p = skiptowhite(eap->arg);
11372 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011373 g = vim_strnsave(eap->arg, (int)(p - eap->arg));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011374 p = skipwhite(p);
11375 if (*p == NUL)
11376 {
11377 /* There must be two arguments. */
11378 EMSG2(_(e_invarg2), eap->arg);
11379 return;
11380 }
11381 end = skip_regexp(p + 1, *p, TRUE, NULL);
11382 if (!eap->skip)
11383 {
11384 if (*end != NUL && !ends_excmd(*skipwhite(end + 1)))
11385 {
11386 eap->errmsg = e_trailing;
11387 return;
11388 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000011389 if (*end != *p)
11390 {
11391 EMSG2(_(e_invarg2), p);
11392 return;
11393 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011394
11395 c = *end;
11396 *end = NUL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011397 match_add(curwin, g, p + 1, 10, id);
11398 vim_free(g);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000011399 *end = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011400 }
11401 }
11402 eap->nextcmd = find_nextcmd(end);
11403}
11404#endif
11405
11406#ifdef FEAT_CRYPT
11407/*
11408 * ":X": Get crypt key
11409 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011410 static void
11411ex_X(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +000011412 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011413{
Bram Moolenaar49771f42010-07-20 17:32:38 +020011414 if (get_crypt_method(curbuf) == 0 || blowfish_self_test() == OK)
Bram Moolenaar40e6a712010-05-16 22:32:54 +020011415 (void)get_crypt_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011416}
11417#endif
11418
11419#ifdef FEAT_FOLDING
11420 static void
11421ex_fold(eap)
11422 exarg_T *eap;
11423{
11424 if (foldManualAllowed(TRUE))
11425 foldCreate(eap->line1, eap->line2);
11426}
11427
11428 static void
11429ex_foldopen(eap)
11430 exarg_T *eap;
11431{
11432 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
11433 eap->forceit, FALSE);
11434}
11435
11436 static void
11437ex_folddo(eap)
11438 exarg_T *eap;
11439{
11440 linenr_T lnum;
11441
11442 /* First set the marks for all lines closed/open. */
11443 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
11444 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
11445 ml_setmarked(lnum);
11446
11447 /* Execute the command on the marked lines. */
11448 global_exe(eap->arg);
11449 ml_clearmarked(); /* clear rest of the marks */
11450}
11451#endif