blob: 686ce3c9f8369d85a23b0b7d8d289416d9abcfed [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
Bram Moolenaard620aa92013-05-17 16:40:06 +0200271# define ex_pydo ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000272# define ex_pyfile ex_ni
273#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200274#ifndef FEAT_PYTHON3
Bram Moolenaar368373e2010-07-19 20:46:22 +0200275# define ex_py3 ex_script_ni
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200276# define ex_py3do ex_ni
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200277# define ex_py3file ex_ni
278#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000279#ifndef FEAT_TCL
280# define ex_tcl ex_script_ni
281# define ex_tcldo ex_ni
282# define ex_tclfile ex_ni
283#endif
284#ifndef FEAT_RUBY
285# define ex_ruby ex_script_ni
286# define ex_rubydo ex_ni
287# define ex_rubyfile ex_ni
288#endif
289#ifndef FEAT_SNIFF
290# define ex_sniff ex_ni
291#endif
292#ifndef FEAT_KEYMAP
293# define ex_loadkeymap ex_ni
294#endif
295static void ex_swapname __ARGS((exarg_T *eap));
296static void ex_syncbind __ARGS((exarg_T *eap));
297static void ex_read __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000298static void ex_pwd __ARGS((exarg_T *eap));
299static void ex_equal __ARGS((exarg_T *eap));
300static void ex_sleep __ARGS((exarg_T *eap));
301static void do_exmap __ARGS((exarg_T *eap, int isabbrev));
302static void ex_winsize __ARGS((exarg_T *eap));
303#ifdef FEAT_WINDOWS
304static void ex_wincmd __ARGS((exarg_T *eap));
305#else
306# define ex_wincmd ex_ni
307#endif
Bram Moolenaar843ee412004-06-30 16:16:41 +0000308#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000309static void ex_winpos __ARGS((exarg_T *eap));
310#else
311# define ex_winpos ex_ni
312#endif
313static void ex_operators __ARGS((exarg_T *eap));
314static void ex_put __ARGS((exarg_T *eap));
315static void ex_copymove __ARGS((exarg_T *eap));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000316static void ex_may_print __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000317static void ex_submagic __ARGS((exarg_T *eap));
318static void ex_join __ARGS((exarg_T *eap));
319static void ex_at __ARGS((exarg_T *eap));
320static void ex_bang __ARGS((exarg_T *eap));
321static void ex_undo __ARGS((exarg_T *eap));
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200322#ifdef FEAT_PERSISTENT_UNDO
323static void ex_wundo __ARGS((exarg_T *eap));
324static void ex_rundo __ARGS((exarg_T *eap));
325#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000326static void ex_redo __ARGS((exarg_T *eap));
Bram Moolenaarc7d89352006-03-14 22:53:34 +0000327static void ex_later __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000328static void ex_redir __ARGS((exarg_T *eap));
329static void ex_redraw __ARGS((exarg_T *eap));
330static void ex_redrawstatus __ARGS((exarg_T *eap));
331static void close_redir __ARGS((void));
332static void ex_mkrc __ARGS((exarg_T *eap));
333static void ex_mark __ARGS((exarg_T *eap));
334#ifdef FEAT_USR_CMDS
335static char_u *uc_fun_cmd __ARGS((void));
Bram Moolenaar52b4b552005-03-07 23:00:57 +0000336static 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 +0000337#endif
338#ifdef FEAT_EX_EXTRA
339static void ex_normal __ARGS((exarg_T *eap));
340static void ex_startinsert __ARGS((exarg_T *eap));
341static void ex_stopinsert __ARGS((exarg_T *eap));
342#else
343# define ex_normal ex_ni
344# define ex_align ex_ni
345# define ex_retab ex_ni
346# define ex_startinsert ex_ni
347# define ex_stopinsert ex_ni
348# define ex_helptags ex_ni
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +0000349# define ex_sort ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000350#endif
351#ifdef FEAT_FIND_ID
352static void ex_checkpath __ARGS((exarg_T *eap));
353static void ex_findpat __ARGS((exarg_T *eap));
354#else
355# define ex_findpat ex_ni
356# define ex_checkpath ex_ni
357#endif
358#if defined(FEAT_FIND_ID) && defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
359static void ex_psearch __ARGS((exarg_T *eap));
360#else
361# define ex_psearch ex_ni
362#endif
363static void ex_tag __ARGS((exarg_T *eap));
364static void ex_tag_cmd __ARGS((exarg_T *eap, char_u *name));
365#ifndef FEAT_EVAL
366# define ex_scriptnames ex_ni
367# define ex_finish ex_ni
368# define ex_echo ex_ni
369# define ex_echohl ex_ni
370# define ex_execute ex_ni
371# define ex_call ex_ni
372# define ex_if ex_ni
373# define ex_endif ex_ni
374# define ex_else ex_ni
375# define ex_while ex_ni
376# define ex_continue ex_ni
377# define ex_break ex_ni
378# define ex_endwhile ex_ni
379# define ex_throw ex_ni
380# define ex_try ex_ni
381# define ex_catch ex_ni
382# define ex_finally ex_ni
383# define ex_endtry ex_ni
384# define ex_endfunction ex_ni
385# define ex_let ex_ni
386# define ex_unlet ex_ni
Bram Moolenaar65c1b012005-01-31 19:02:28 +0000387# define ex_lockvar ex_ni
388# define ex_unlockvar ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000389# define ex_function ex_ni
390# define ex_delfunction ex_ni
391# define ex_return ex_ni
Bram Moolenaard812df62008-11-09 12:46:09 +0000392# define ex_oldfiles ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000393#endif
394static char_u *arg_all __ARGS((void));
395#ifdef FEAT_SESSION
396static int makeopens __ARGS((FILE *fd, char_u *dirnow));
Bram Moolenaarf13be0d2008-01-02 14:13:10 +0000397static 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 +0000398static void ex_loadview __ARGS((exarg_T *eap));
399static char_u *get_view_file __ARGS((int c));
Bram Moolenaareeefcc72007-05-01 21:21:21 +0000400static int did_lcd; /* whether ":lcd" was produced for a session */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000401#else
402# define ex_loadview ex_ni
403#endif
404#ifndef FEAT_EVAL
405# define ex_compiler ex_ni
406#endif
407#ifdef FEAT_VIMINFO
408static void ex_viminfo __ARGS((exarg_T *eap));
409#else
410# define ex_viminfo ex_ni
411#endif
412static void ex_behave __ARGS((exarg_T *eap));
413#ifdef FEAT_AUTOCMD
414static void ex_filetype __ARGS((exarg_T *eap));
415static void ex_setfiletype __ARGS((exarg_T *eap));
416#else
417# define ex_filetype ex_ni
418# define ex_setfiletype ex_ni
419#endif
420#ifndef FEAT_DIFF
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000421# define ex_diffoff ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000422# define ex_diffpatch ex_ni
423# define ex_diffgetput ex_ni
424# define ex_diffsplit ex_ni
425# define ex_diffthis ex_ni
426# define ex_diffupdate ex_ni
427#endif
428static void ex_digraphs __ARGS((exarg_T *eap));
429static void ex_set __ARGS((exarg_T *eap));
430#if !defined(FEAT_EVAL) || !defined(FEAT_AUTOCMD)
431# define ex_options ex_ni
432#endif
433#ifdef FEAT_SEARCH_EXTRA
434static void ex_nohlsearch __ARGS((exarg_T *eap));
435static void ex_match __ARGS((exarg_T *eap));
436#else
437# define ex_nohlsearch ex_ni
438# define ex_match ex_ni
439#endif
440#ifdef FEAT_CRYPT
441static void ex_X __ARGS((exarg_T *eap));
442#else
443# define ex_X ex_ni
444#endif
445#ifdef FEAT_FOLDING
446static void ex_fold __ARGS((exarg_T *eap));
447static void ex_foldopen __ARGS((exarg_T *eap));
448static void ex_folddo __ARGS((exarg_T *eap));
449#else
450# define ex_fold ex_ni
451# define ex_foldopen ex_ni
452# define ex_folddo ex_ni
453#endif
454#if !((defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
455 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE)))
456# define ex_language ex_ni
457#endif
458#ifndef FEAT_SIGNS
459# define ex_sign ex_ni
460#endif
461#ifndef FEAT_SUN_WORKSHOP
462# define ex_wsverb ex_ni
463#endif
Bram Moolenaar009b2592004-10-24 19:18:58 +0000464#ifndef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200465# define ex_nbclose ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000466# define ex_nbkey ex_ni
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200467# define ex_nbstart ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000468#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000469
470#ifndef FEAT_EVAL
471# define ex_debug ex_ni
472# define ex_breakadd ex_ni
473# define ex_debuggreedy ex_ni
474# define ex_breakdel ex_ni
475# define ex_breaklist ex_ni
476#endif
477
478#ifndef FEAT_CMDHIST
479# define ex_history ex_ni
480#endif
481#ifndef FEAT_JUMPLIST
482# define ex_jumps ex_ni
483# define ex_changes ex_ni
484#endif
485
Bram Moolenaar05159a02005-02-26 23:04:13 +0000486#ifndef FEAT_PROFILE
487# define ex_profile ex_ni
488#endif
489
Bram Moolenaar071d4272004-06-13 20:20:40 +0000490/*
491 * Declare cmdnames[].
492 */
493#define DO_DECLARE_EXCMD
494#include "ex_cmds.h"
495
496/*
497 * Table used to quickly search for a command, based on its first character.
498 */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +0000499static cmdidx_T cmdidxs[27] =
Bram Moolenaar071d4272004-06-13 20:20:40 +0000500{
501 CMD_append,
502 CMD_buffer,
503 CMD_change,
504 CMD_delete,
505 CMD_edit,
506 CMD_file,
507 CMD_global,
508 CMD_help,
509 CMD_insert,
510 CMD_join,
511 CMD_k,
512 CMD_list,
513 CMD_move,
514 CMD_next,
515 CMD_open,
516 CMD_print,
517 CMD_quit,
518 CMD_read,
519 CMD_substitute,
520 CMD_t,
521 CMD_undo,
522 CMD_vglobal,
523 CMD_write,
524 CMD_xit,
525 CMD_yank,
526 CMD_z,
527 CMD_bang
528};
529
530static char_u dollar_command[2] = {'$', 0};
531
532
533#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000534/* Struct for storing a line inside a while/for loop */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000535typedef struct
536{
537 char_u *line; /* command line */
538 linenr_T lnum; /* sourcing_lnum of the line */
539} wcmd_T;
540
541/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000542 * Structure used to store info for line position in a while or for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000543 * This is required, because do_one_cmd() may invoke ex_function(), which
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000544 * reads more lines that may come from the while/for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000546struct loop_cookie
Bram Moolenaar071d4272004-06-13 20:20:40 +0000547{
548 garray_T *lines_gap; /* growarray with line info */
549 int current_line; /* last read line from growarray */
550 int repeating; /* TRUE when looping a second time */
551 /* When "repeating" is FALSE use "getline" and "cookie" to get lines */
552 char_u *(*getline) __ARGS((int, void *, int));
553 void *cookie;
554};
555
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000556static char_u *get_loop_line __ARGS((int c, void *cookie, int indent));
557static int store_loop_line __ARGS((garray_T *gap, char_u *line));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000558static void free_cmdlines __ARGS((garray_T *gap));
Bram Moolenaared203462004-06-16 11:19:22 +0000559
560/* Struct to save a few things while debugging. Used in do_cmdline() only. */
561struct dbg_stuff
562{
563 int trylevel;
564 int force_abort;
565 except_T *caught_stack;
566 char_u *vv_exception;
567 char_u *vv_throwpoint;
568 int did_emsg;
569 int got_int;
570 int did_throw;
571 int need_rethrow;
572 int check_cstack;
573 except_T *current_exception;
574};
575
576static void save_dbg_stuff __ARGS((struct dbg_stuff *dsp));
577static void restore_dbg_stuff __ARGS((struct dbg_stuff *dsp));
578
579 static void
580save_dbg_stuff(dsp)
581 struct dbg_stuff *dsp;
582{
583 dsp->trylevel = trylevel; trylevel = 0;
584 dsp->force_abort = force_abort; force_abort = FALSE;
585 dsp->caught_stack = caught_stack; caught_stack = NULL;
586 dsp->vv_exception = v_exception(NULL);
587 dsp->vv_throwpoint = v_throwpoint(NULL);
588
589 /* Necessary for debugging an inactive ":catch", ":finally", ":endtry" */
590 dsp->did_emsg = did_emsg; did_emsg = FALSE;
591 dsp->got_int = got_int; got_int = FALSE;
592 dsp->did_throw = did_throw; did_throw = FALSE;
593 dsp->need_rethrow = need_rethrow; need_rethrow = FALSE;
594 dsp->check_cstack = check_cstack; check_cstack = FALSE;
595 dsp->current_exception = current_exception; current_exception = NULL;
596}
597
598 static void
599restore_dbg_stuff(dsp)
600 struct dbg_stuff *dsp;
601{
602 suppress_errthrow = FALSE;
603 trylevel = dsp->trylevel;
604 force_abort = dsp->force_abort;
605 caught_stack = dsp->caught_stack;
606 (void)v_exception(dsp->vv_exception);
607 (void)v_throwpoint(dsp->vv_throwpoint);
608 did_emsg = dsp->did_emsg;
609 got_int = dsp->got_int;
610 did_throw = dsp->did_throw;
611 need_rethrow = dsp->need_rethrow;
612 check_cstack = dsp->check_cstack;
613 current_exception = dsp->current_exception;
614}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000615#endif
616
617
618/*
619 * do_exmode(): Repeatedly get commands for the "Ex" mode, until the ":vi"
620 * command is given.
621 */
622 void
623do_exmode(improved)
624 int improved; /* TRUE for "improved Ex" mode */
625{
626 int save_msg_scroll;
627 int prev_msg_row;
628 linenr_T prev_line;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000629 int changedtick;
630
631 if (improved)
632 exmode_active = EXMODE_VIM;
633 else
634 exmode_active = EXMODE_NORMAL;
635 State = NORMAL;
636
637 /* When using ":global /pat/ visual" and then "Q" we return to continue
638 * the :global command. */
639 if (global_busy)
640 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641
642 save_msg_scroll = msg_scroll;
643 ++RedrawingDisabled; /* don't redisplay the window */
644 ++no_wait_return; /* don't wait for return */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645#ifdef FEAT_GUI
646 /* Ignore scrollbar and mouse events in Ex mode */
647 ++hold_gui_events;
648#endif
649#ifdef FEAT_SNIFF
650 want_sniff_request = 0; /* No K_SNIFF wanted */
651#endif
652
653 MSG(_("Entering Ex mode. Type \"visual\" to go to Normal mode."));
654 while (exmode_active)
655 {
Bram Moolenaar7c626922005-02-07 22:01:03 +0000656#ifdef FEAT_EX_EXTRA
657 /* Check for a ":normal" command and no more characters left. */
658 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
659 {
660 exmode_active = FALSE;
661 break;
662 }
663#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000664 msg_scroll = TRUE;
665 need_wait_return = FALSE;
666 ex_pressedreturn = FALSE;
667 ex_no_reprint = FALSE;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000668 changedtick = curbuf->b_changedtick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000669 prev_msg_row = msg_row;
670 prev_line = curwin->w_cursor.lnum;
671#ifdef FEAT_SNIFF
672 ProcessSniffRequests();
673#endif
674 if (improved)
675 {
676 cmdline_row = msg_row;
677 do_cmdline(NULL, getexline, NULL, 0);
678 }
679 else
680 do_cmdline(NULL, getexmodeline, NULL, DOCMD_NOWAIT);
681 lines_left = Rows - 1;
682
Bram Moolenaardf177f62005-02-22 08:39:57 +0000683 if ((prev_line != curwin->w_cursor.lnum
684 || changedtick != curbuf->b_changedtick) && !ex_no_reprint)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000685 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000686 if (curbuf->b_ml.ml_flags & ML_EMPTY)
687 EMSG(_(e_emptybuf));
688 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000689 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000690 if (ex_pressedreturn)
691 {
692 /* go up one line, to overwrite the ":<CR>" line, so the
Bram Moolenaar81870892007-11-11 18:17:28 +0000693 * output doesn't contain empty lines. */
Bram Moolenaardf177f62005-02-22 08:39:57 +0000694 msg_row = prev_msg_row;
695 if (prev_msg_row == Rows - 1)
696 msg_row--;
697 }
698 msg_col = 0;
699 print_line_no_prefix(curwin->w_cursor.lnum, FALSE, FALSE);
700 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000701 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000702 }
Bram Moolenaardf177f62005-02-22 08:39:57 +0000703 else if (ex_pressedreturn && !ex_no_reprint) /* must be at EOF */
704 {
705 if (curbuf->b_ml.ml_flags & ML_EMPTY)
706 EMSG(_(e_emptybuf));
707 else
708 EMSG(_("E501: At end-of-file"));
709 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000710 }
711
712#ifdef FEAT_GUI
713 --hold_gui_events;
714#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000715 --RedrawingDisabled;
716 --no_wait_return;
717 update_screen(CLEAR);
718 need_wait_return = FALSE;
719 msg_scroll = save_msg_scroll;
720}
721
722/*
723 * Execute a simple command line. Used for translated commands like "*".
724 */
725 int
726do_cmdline_cmd(cmd)
727 char_u *cmd;
728{
729 return do_cmdline(cmd, NULL, NULL,
730 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
731}
732
733/*
734 * do_cmdline(): execute one Ex command line
735 *
736 * 1. Execute "cmdline" when it is not NULL.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100737 * If "cmdline" is NULL, or more lines are needed, fgetline() is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000738 * 2. Split up in parts separated with '|'.
739 *
740 * This function can be called recursively!
741 *
742 * flags:
743 * DOCMD_VERBOSE - The command will be included in the error message.
744 * DOCMD_NOWAIT - Don't call wait_return() and friends.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100745 * DOCMD_REPEAT - Repeat execution until fgetline() returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000746 * DOCMD_KEYTYPED - Don't reset KeyTyped.
747 * DOCMD_EXCRESET - Reset the exception environment (used for debugging).
748 * DOCMD_KEEPLINE - Store first typed line (for repeating with ".").
749 *
750 * return FAIL if cmdline could not be executed, OK otherwise
751 */
752 int
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100753do_cmdline(cmdline, fgetline, cookie, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754 char_u *cmdline;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100755 char_u *(*fgetline) __ARGS((int, void *, int));
756 void *cookie; /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000757 int flags;
758{
759 char_u *next_cmdline; /* next cmd to execute */
760 char_u *cmdline_copy = NULL; /* copy of cmd line */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100761 int used_getline = FALSE; /* used "fgetline" to obtain command */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000762 static int recursive = 0; /* recursive depth */
763 int msg_didout_before_start = 0;
764 int count = 0; /* line number count */
765 int did_inc = FALSE; /* incremented RedrawingDisabled */
766 int retval = OK;
767#ifdef FEAT_EVAL
768 struct condstack cstack; /* conditional stack */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000769 garray_T lines_ga; /* keep lines for ":while"/":for" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770 int current_line = 0; /* active line in lines_ga */
771 char_u *fname = NULL; /* function or script name */
772 linenr_T *breakpoint = NULL; /* ptr to breakpoint field in cookie */
773 int *dbg_tick = NULL; /* ptr to dbg_tick field in cookie */
Bram Moolenaared203462004-06-16 11:19:22 +0000774 struct dbg_stuff debug_saved; /* saved things for debug mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775 int initial_trylevel;
776 struct msglist **saved_msg_list = NULL;
777 struct msglist *private_msg_list;
778
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100779 /* "fgetline" and "cookie" passed to do_one_cmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000780 char_u *(*cmd_getline) __ARGS((int, void *, int));
781 void *cmd_cookie;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000782 struct loop_cookie cmd_loop_cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783 void *real_cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000784 int getline_is_func;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785#else
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100786# define cmd_getline fgetline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787# define cmd_cookie cookie
788#endif
789 static int call_depth = 0; /* recursiveness */
790
791#ifdef FEAT_EVAL
792 /* For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory
793 * location for storing error messages to be converted to an exception.
Bram Moolenaarcf3630f2005-01-08 16:04:29 +0000794 * This ensures that the do_errthrow() call in do_one_cmd() does not
795 * combine the messages stored by an earlier invocation of do_one_cmd()
796 * with the command name of the later one. This would happen when
797 * BufWritePost autocommands are executed after a write error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798 saved_msg_list = msg_list;
799 msg_list = &private_msg_list;
800 private_msg_list = NULL;
801#endif
802
803 /* It's possible to create an endless loop with ":execute", catch that
804 * here. The value of 200 allows nested function calls, ":source", etc. */
805 if (call_depth == 200)
806 {
807 EMSG(_("E169: Command too recursive"));
808#ifdef FEAT_EVAL
809 /* When converting to an exception, we do not include the command name
810 * since this is not an error of the specific command. */
811 do_errthrow((struct condstack *)NULL, (char_u *)NULL);
812 msg_list = saved_msg_list;
813#endif
814 return FAIL;
815 }
816 ++call_depth;
817
818#ifdef FEAT_EVAL
819 cstack.cs_idx = -1;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000820 cstack.cs_looplevel = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821 cstack.cs_trylevel = 0;
822 cstack.cs_emsg_silent_list = NULL;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000823 cstack.cs_lflags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824 ga_init2(&lines_ga, (int)sizeof(wcmd_T), 10);
825
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100826 real_cookie = getline_cookie(fgetline, cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827
828 /* Inside a function use a higher nesting level. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100829 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000830 if (getline_is_func && ex_nesting_level == func_level(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000831 ++ex_nesting_level;
832
833 /* Get the function or script name and the address where the next breakpoint
834 * line and the debug tick for a function or script are stored. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000835 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000836 {
837 fname = func_name(real_cookie);
838 breakpoint = func_breakpoint(real_cookie);
839 dbg_tick = func_dbg_tick(real_cookie);
840 }
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100841 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842 {
843 fname = sourcing_name;
844 breakpoint = source_breakpoint(real_cookie);
845 dbg_tick = source_dbg_tick(real_cookie);
846 }
847
848 /*
849 * Initialize "force_abort" and "suppress_errthrow" at the top level.
850 */
851 if (!recursive)
852 {
853 force_abort = FALSE;
854 suppress_errthrow = FALSE;
855 }
856
857 /*
858 * If requested, store and reset the global values controlling the
Bram Moolenaar89d40322006-08-29 15:30:07 +0000859 * exception handling (used when debugging). Otherwise clear it to avoid
860 * a bogus compiler warning when the optimizer uses inline functions...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000861 */
Bram Moolenaarb4872942006-05-13 10:32:52 +0000862 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +0000863 save_dbg_stuff(&debug_saved);
Bram Moolenaar89d40322006-08-29 15:30:07 +0000864 else
Bram Moolenaar7db5fc82010-05-24 11:59:29 +0200865 vim_memset(&debug_saved, 0, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000866
867 initial_trylevel = trylevel;
868
869 /*
870 * "did_throw" will be set to TRUE when an exception is being thrown.
871 */
872 did_throw = FALSE;
873#endif
874 /*
875 * "did_emsg" will be set to TRUE when emsg() is used, in which case we
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000876 * cancel the whole command line, and any if/endif or loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000877 * If force_abort is set, we cancel everything.
878 */
879 did_emsg = FALSE;
880
881 /*
882 * KeyTyped is only set when calling vgetc(). Reset it here when not
883 * calling vgetc() (sourced command lines).
884 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100885 if (!(flags & DOCMD_KEYTYPED)
886 && !getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887 KeyTyped = FALSE;
888
889 /*
890 * Continue executing command lines:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000891 * - when inside an ":if", ":while" or ":for"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892 * - for multiple commands on one line, separated with '|'
893 * - when repeating until there are no more lines (for ":source")
894 */
895 next_cmdline = cmdline;
896 do
897 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000898#ifdef FEAT_EVAL
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100899 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000900#endif
901
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000902 /* stop skipping cmds for an error msg after all endif/while/for */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000903 if (next_cmdline == NULL
904#ifdef FEAT_EVAL
905 && !force_abort
906 && cstack.cs_idx < 0
Bram Moolenaar05159a02005-02-26 23:04:13 +0000907 && !(getline_is_func && func_has_abort(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000908#endif
909 )
910 did_emsg = FALSE;
911
912 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000913 * 1. If repeating a line in a loop, get a line from lines_ga.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100914 * 2. If no line given: Get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000915 * 3. If a line is given: Make a copy, so we can mess with it.
916 */
917
918#ifdef FEAT_EVAL
919 /* 1. If repeating, get a previous line from lines_ga. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000920 if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000921 {
922 /* Each '|' separated command is stored separately in lines_ga, to
923 * be able to jump to it. Don't use next_cmdline now. */
924 vim_free(cmdline_copy);
925 cmdline_copy = NULL;
926
927 /* Check if a function has returned or, unless it has an unclosed
928 * try conditional, aborted. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000929 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000930 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000931# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000932 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000933 func_line_end(real_cookie);
934# endif
935 if (func_has_ended(real_cookie))
936 {
937 retval = FAIL;
938 break;
939 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000940 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000941#ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000942 else if (do_profiling == PROF_YES
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100943 && getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000944 script_line_end();
945#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000946
947 /* Check if a sourced file hit a ":finish" command. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100948 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949 {
950 retval = FAIL;
951 break;
952 }
953
954 /* If breakpoints have been added/deleted need to check for it. */
955 if (breakpoint != NULL && dbg_tick != NULL
956 && *dbg_tick != debug_tick)
957 {
958 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100959 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960 fname, sourcing_lnum);
961 *dbg_tick = debug_tick;
962 }
963
964 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
965 sourcing_lnum = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
966
967 /* Did we encounter a breakpoint? */
968 if (breakpoint != NULL && *breakpoint != 0
969 && *breakpoint <= sourcing_lnum)
970 {
971 dbg_breakpoint(fname, sourcing_lnum);
972 /* Find next breakpoint. */
973 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100974 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000975 fname, sourcing_lnum);
976 *dbg_tick = debug_tick;
977 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000978# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000979 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000980 {
981 if (getline_is_func)
982 func_line_start(real_cookie);
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100983 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000984 script_line_start();
985 }
986# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987 }
988
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000989 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000991 /* Inside a while/for loop we need to store the lines and use them
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100992 * again. Pass a different "fgetline" function to do_one_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993 * below, so that it stores lines in or reads them from
994 * "lines_ga". Makes it possible to define a function inside a
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000995 * while/for loop. */
996 cmd_getline = get_loop_line;
997 cmd_cookie = (void *)&cmd_loop_cookie;
998 cmd_loop_cookie.lines_gap = &lines_ga;
999 cmd_loop_cookie.current_line = current_line;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001000 cmd_loop_cookie.getline = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001001 cmd_loop_cookie.cookie = cookie;
1002 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003 }
1004 else
1005 {
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001006 cmd_getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007 cmd_cookie = cookie;
1008 }
1009#endif
1010
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001011 /* 2. If no line given, get an allocated line with fgetline(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012 if (next_cmdline == NULL)
1013 {
1014 /*
1015 * Need to set msg_didout for the first line after an ":if",
1016 * otherwise the ":if" will be overwritten.
1017 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001018 if (count == 1 && getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019 msg_didout = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001020 if (fgetline == NULL || (next_cmdline = fgetline(':', cookie,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021#ifdef FEAT_EVAL
1022 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
1023#else
1024 0
1025#endif
1026 )) == NULL)
1027 {
1028 /* Don't call wait_return for aborted command line. The NULL
1029 * returned for the end of a sourced file or executed function
1030 * doesn't do this. */
1031 if (KeyTyped && !(flags & DOCMD_REPEAT))
1032 need_wait_return = FALSE;
1033 retval = FAIL;
1034 break;
1035 }
1036 used_getline = TRUE;
1037
1038 /*
1039 * Keep the first typed line. Clear it when more lines are typed.
1040 */
1041 if (flags & DOCMD_KEEPLINE)
1042 {
1043 vim_free(repeat_cmdline);
1044 if (count == 0)
1045 repeat_cmdline = vim_strsave(next_cmdline);
1046 else
1047 repeat_cmdline = NULL;
1048 }
1049 }
1050
1051 /* 3. Make a copy of the command so we can mess with it. */
1052 else if (cmdline_copy == NULL)
1053 {
1054 next_cmdline = vim_strsave(next_cmdline);
1055 if (next_cmdline == NULL)
1056 {
1057 EMSG(_(e_outofmem));
1058 retval = FAIL;
1059 break;
1060 }
1061 }
1062 cmdline_copy = next_cmdline;
1063
1064#ifdef FEAT_EVAL
1065 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001066 * Save the current line when inside a ":while" or ":for", and when
1067 * the command looks like a ":while" or ":for", because we may need it
1068 * later. When there is a '|' and another command, it is stored
1069 * separately, because we need to be able to jump back to it from an
1070 * :endwhile/:endfor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001072 if (current_line == lines_ga.ga_len
1073 && (cstack.cs_looplevel || has_loop_cmd(next_cmdline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001075 if (store_loop_line(&lines_ga, next_cmdline) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001076 {
1077 retval = FAIL;
1078 break;
1079 }
1080 }
1081 did_endif = FALSE;
1082#endif
1083
1084 if (count++ == 0)
1085 {
1086 /*
1087 * All output from the commands is put below each other, without
1088 * waiting for a return. Don't do this when executing commands
1089 * from a script or when being called recursive (e.g. for ":e
1090 * +command file").
1091 */
1092 if (!(flags & DOCMD_NOWAIT) && !recursive)
1093 {
1094 msg_didout_before_start = msg_didout;
1095 msg_didany = FALSE; /* no output yet */
1096 msg_start();
1097 msg_scroll = TRUE; /* put messages below each other */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001098 ++no_wait_return; /* don't wait for return until finished */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099 ++RedrawingDisabled;
1100 did_inc = TRUE;
1101 }
1102 }
1103
1104 if (p_verbose >= 15 && sourcing_name != NULL)
1105 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001106 ++no_wait_return;
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001107 verbose_enter_scroll();
1108
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109 smsg((char_u *)_("line %ld: %s"),
1110 (long)sourcing_lnum, cmdline_copy);
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001111 if (msg_silent == 0)
1112 msg_puts((char_u *)"\n"); /* don't overwrite this */
1113
1114 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115 --no_wait_return;
1116 }
1117
1118 /*
1119 * 2. Execute one '|' separated command.
1120 * do_one_cmd() will return NULL if there is no trailing '|'.
1121 * "cmdline_copy" can change, e.g. for '%' and '#' expansion.
1122 */
1123 ++recursive;
1124 next_cmdline = do_one_cmd(&cmdline_copy, flags & DOCMD_VERBOSE,
1125#ifdef FEAT_EVAL
1126 &cstack,
1127#endif
1128 cmd_getline, cmd_cookie);
1129 --recursive;
1130
1131#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001132 if (cmd_cookie == (void *)&cmd_loop_cookie)
1133 /* Use "current_line" from "cmd_loop_cookie", it may have been
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134 * incremented when defining a function. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001135 current_line = cmd_loop_cookie.current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136#endif
1137
1138 if (next_cmdline == NULL)
1139 {
1140 vim_free(cmdline_copy);
1141 cmdline_copy = NULL;
1142#ifdef FEAT_CMDHIST
1143 /*
1144 * If the command was typed, remember it for the ':' register.
1145 * Do this AFTER executing the command to make :@: work.
1146 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001147 if (getline_equal(fgetline, cookie, getexline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001148 && new_last_cmdline != NULL)
1149 {
1150 vim_free(last_cmdline);
1151 last_cmdline = new_last_cmdline;
1152 new_last_cmdline = NULL;
1153 }
1154#endif
1155 }
1156 else
1157 {
1158 /* need to copy the command after the '|' to cmdline_copy, for the
1159 * next do_one_cmd() */
Bram Moolenaara7241f52008-06-24 20:39:31 +00001160 STRMOVE(cmdline_copy, next_cmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001161 next_cmdline = cmdline_copy;
1162 }
1163
1164
1165#ifdef FEAT_EVAL
1166 /* reset did_emsg for a function that is not aborted by an error */
1167 if (did_emsg && !force_abort
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001168 && getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169 && !func_has_abort(real_cookie))
1170 did_emsg = FALSE;
1171
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001172 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173 {
1174 ++current_line;
1175
1176 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001177 * An ":endwhile", ":endfor" and ":continue" is handled here.
1178 * If we were executing commands, jump back to the ":while" or
1179 * ":for".
1180 * If we were not executing commands, decrement cs_looplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001182 if (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 cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001186 /* Jump back to the matching ":while" or ":for". Be careful
1187 * not to use a cs_line[] from an entry that isn't a ":while"
1188 * or ":for": It would make "current_line" invalid and can
1189 * cause a crash. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190 if (!did_emsg && !got_int && !did_throw
1191 && cstack.cs_idx >= 0
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001192 && (cstack.cs_flags[cstack.cs_idx]
1193 & (CSF_WHILE | CSF_FOR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194 && cstack.cs_line[cstack.cs_idx] >= 0
1195 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
1196 {
1197 current_line = cstack.cs_line[cstack.cs_idx];
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001198 /* remember we jumped there */
1199 cstack.cs_lflags |= CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001200 line_breakcheck(); /* check if CTRL-C typed */
1201
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001202 /* Check for the next breakpoint at or after the ":while"
1203 * or ":for". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204 if (breakpoint != NULL)
1205 {
1206 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001207 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001208 fname,
1209 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
1210 *dbg_tick = debug_tick;
1211 }
1212 }
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001213 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001215 /* can only get here with ":endwhile" or ":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216 if (cstack.cs_idx >= 0)
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001217 rewind_conditionals(&cstack, cstack.cs_idx - 1,
1218 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219 }
1220 }
1221
1222 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001223 * For a ":while" or ":for" we need to remember the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001225 else if (cstack.cs_lflags & CSL_HAD_LOOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001227 cstack.cs_lflags &= ~CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001228 cstack.cs_line[cstack.cs_idx] = current_line - 1;
1229 }
1230 }
1231
1232 /*
1233 * When not inside any ":while" loop, clear remembered lines.
1234 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001235 if (cstack.cs_looplevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001236 {
1237 if (lines_ga.ga_len > 0)
1238 {
1239 sourcing_lnum =
1240 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
1241 free_cmdlines(&lines_ga);
1242 }
1243 current_line = 0;
1244 }
1245
1246 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001247 * A ":finally" makes did_emsg, got_int, and did_throw pending for
1248 * being restored at the ":endtry". Reset them here and set the
1249 * ACTIVE and FINALLY flags, so that the finally clause gets executed.
1250 * This includes the case where a missing ":endif", ":endwhile" or
1251 * ":endfor" was detected by the ":finally" itself.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001253 if (cstack.cs_lflags & CSL_HAD_FINA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001255 cstack.cs_lflags &= ~CSL_HAD_FINA;
1256 report_make_pending(cstack.cs_pending[cstack.cs_idx]
1257 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001258 did_throw ? (void *)current_exception : NULL);
1259 did_emsg = got_int = did_throw = FALSE;
1260 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
1261 }
1262
1263 /* Update global "trylevel" for recursive calls to do_cmdline() from
1264 * within this loop. */
1265 trylevel = initial_trylevel + cstack.cs_trylevel;
1266
1267 /*
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001268 * If the outermost try conditional (across function calls and sourced
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269 * files) is aborted because of an error, an interrupt, or an uncaught
1270 * exception, cancel everything. If it is left normally, reset
1271 * force_abort to get the non-EH compatible abortion behavior for
1272 * the rest of the script.
1273 */
1274 if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
1275 force_abort = FALSE;
1276
1277 /* Convert an interrupt to an exception if appropriate. */
1278 (void)do_intthrow(&cstack);
1279#endif /* FEAT_EVAL */
1280
1281 }
1282 /*
1283 * Continue executing command lines when:
1284 * - no CTRL-C typed, no aborting error, no exception thrown or try
1285 * conditionals need to be checked for executing finally clauses or
1286 * catching an interrupt exception
1287 * - didn't get an error message or lines are not typed
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001288 * - there is a command after '|', inside a :if, :while, :for or :try, or
Bram Moolenaar071d4272004-06-13 20:20:40 +00001289 * looping for ":source" command or function call.
1290 */
1291 while (!((got_int
1292#ifdef FEAT_EVAL
1293 || (did_emsg && force_abort) || did_throw
1294#endif
1295 )
1296#ifdef FEAT_EVAL
1297 && cstack.cs_trylevel == 0
1298#endif
1299 )
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001300 && !(did_emsg
1301#ifdef FEAT_EVAL
1302 /* Keep going when inside try/catch, so that the error can be
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001303 * deal with, except when it is a syntax error, it may cause
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001304 * the :endtry to be missed. */
1305 && (cstack.cs_trylevel == 0 || did_emsg_syntax)
1306#endif
1307 && used_getline
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001308 && (getline_equal(fgetline, cookie, getexmodeline)
1309 || getline_equal(fgetline, cookie, getexline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310 && (next_cmdline != NULL
1311#ifdef FEAT_EVAL
1312 || cstack.cs_idx >= 0
1313#endif
1314 || (flags & DOCMD_REPEAT)));
1315
1316 vim_free(cmdline_copy);
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001317 did_emsg_syntax = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318#ifdef FEAT_EVAL
1319 free_cmdlines(&lines_ga);
1320 ga_clear(&lines_ga);
1321
1322 if (cstack.cs_idx >= 0)
1323 {
1324 /*
1325 * If a sourced file or executed function ran to its end, report the
1326 * unclosed conditional.
1327 */
1328 if (!got_int && !did_throw
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001329 && ((getline_equal(fgetline, cookie, getsourceline)
1330 && !source_finished(fgetline, cookie))
1331 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332 && !func_has_ended(real_cookie))))
1333 {
1334 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
1335 EMSG(_(e_endtry));
1336 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
1337 EMSG(_(e_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001338 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
1339 EMSG(_(e_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340 else
1341 EMSG(_(e_endif));
1342 }
1343
1344 /*
1345 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1346 * ":endtry" in a sourced file or executed function. If the try
1347 * conditional is in its finally clause, ignore anything pending.
1348 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001349 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350 */
1351 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001352 {
1353 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1354
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001355 if (idx >= 0)
1356 --idx; /* remove try block not in its finally clause */
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001357 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1358 &cstack.cs_looplevel);
1359 }
1360 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361 trylevel = initial_trylevel;
1362 }
1363
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001364 /* If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1365 * lack was reported above and the error message is to be converted to an
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366 * exception, do this now after rewinding the cstack. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001367 do_errthrow(&cstack, getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001368 ? (char_u *)"endfunction" : (char_u *)NULL);
1369
1370 if (trylevel == 0)
1371 {
1372 /*
1373 * When an exception is being thrown out of the outermost try
1374 * conditional, discard the uncaught exception, disable the conversion
1375 * of interrupts or errors to exceptions, and ensure that no more
1376 * commands are executed.
1377 */
1378 if (did_throw)
1379 {
1380 void *p = NULL;
1381 char_u *saved_sourcing_name;
1382 int saved_sourcing_lnum;
1383 struct msglist *messages = NULL, *next;
1384
1385 /*
1386 * If the uncaught exception is a user exception, report it as an
1387 * error. If it is an error exception, display the saved error
1388 * message now. For an interrupt exception, do nothing; the
1389 * interrupt message is given elsewhere.
1390 */
1391 switch (current_exception->type)
1392 {
1393 case ET_USER:
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001394 vim_snprintf((char *)IObuff, IOSIZE,
1395 _("E605: Exception not caught: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001396 current_exception->value);
1397 p = vim_strsave(IObuff);
1398 break;
1399 case ET_ERROR:
1400 messages = current_exception->messages;
1401 current_exception->messages = NULL;
1402 break;
1403 case ET_INTERRUPT:
1404 break;
1405 default:
1406 p = vim_strsave((char_u *)_(e_internal));
1407 }
1408
1409 saved_sourcing_name = sourcing_name;
1410 saved_sourcing_lnum = sourcing_lnum;
1411 sourcing_name = current_exception->throw_name;
1412 sourcing_lnum = current_exception->throw_lnum;
1413 current_exception->throw_name = NULL;
1414
1415 discard_current_exception(); /* uses IObuff if 'verbose' */
1416 suppress_errthrow = TRUE;
1417 force_abort = TRUE;
1418
1419 if (messages != NULL)
1420 {
1421 do
1422 {
1423 next = messages->next;
1424 emsg(messages->msg);
1425 vim_free(messages->msg);
1426 vim_free(messages);
1427 messages = next;
1428 }
1429 while (messages != NULL);
1430 }
1431 else if (p != NULL)
1432 {
1433 emsg(p);
1434 vim_free(p);
1435 }
1436 vim_free(sourcing_name);
1437 sourcing_name = saved_sourcing_name;
1438 sourcing_lnum = saved_sourcing_lnum;
1439 }
1440
1441 /*
1442 * On an interrupt or an aborting error not converted to an exception,
1443 * disable the conversion of errors to exceptions. (Interrupts are not
1444 * converted any more, here.) This enables also the interrupt message
1445 * when force_abort is set and did_emsg unset in case of an interrupt
1446 * from a finally clause after an error.
1447 */
1448 else if (got_int || (did_emsg && force_abort))
1449 suppress_errthrow = TRUE;
1450 }
1451
1452 /*
1453 * The current cstack will be freed when do_cmdline() returns. An uncaught
1454 * exception will have to be rethrown in the previous cstack. If a function
1455 * has just returned or a script file was just finished and the previous
1456 * cstack belongs to the same function or, respectively, script file, it
1457 * will have to be checked for finally clauses to be executed due to the
1458 * ":return" or ":finish". This is done in do_one_cmd().
1459 */
1460 if (did_throw)
1461 need_rethrow = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001462 if ((getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001463 && ex_nesting_level > source_level(real_cookie))
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001464 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001465 && ex_nesting_level > func_level(real_cookie) + 1))
1466 {
1467 if (!did_throw)
1468 check_cstack = TRUE;
1469 }
1470 else
1471 {
1472 /* When leaving a function, reduce nesting level. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001473 if (getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001474 --ex_nesting_level;
1475 /*
1476 * Go to debug mode when returning from a function in which we are
1477 * single-stepping.
1478 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001479 if ((getline_equal(fgetline, cookie, getsourceline)
1480 || getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001481 && ex_nesting_level + 1 <= debug_break_level)
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001482 do_debug(getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483 ? (char_u *)_("End of sourced file")
1484 : (char_u *)_("End of function"));
1485 }
1486
1487 /*
1488 * Restore the exception environment (done after returning from the
1489 * debugger).
1490 */
1491 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001492 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001493
1494 msg_list = saved_msg_list;
1495#endif /* FEAT_EVAL */
1496
1497 /*
1498 * If there was too much output to fit on the command line, ask the user to
1499 * hit return before redrawing the screen. With the ":global" command we do
1500 * this only once after the command is finished.
1501 */
1502 if (did_inc)
1503 {
1504 --RedrawingDisabled;
1505 --no_wait_return;
1506 msg_scroll = FALSE;
1507
1508 /*
1509 * When just finished an ":if"-":else" which was typed, no need to
1510 * wait for hit-return. Also for an error situation.
1511 */
1512 if (retval == FAIL
1513#ifdef FEAT_EVAL
1514 || (did_endif && KeyTyped && !did_emsg)
1515#endif
1516 )
1517 {
1518 need_wait_return = FALSE;
1519 msg_didany = FALSE; /* don't wait when restarting edit */
1520 }
1521 else if (need_wait_return)
1522 {
1523 /*
1524 * The msg_start() above clears msg_didout. The wait_return we do
1525 * here should not overwrite the command that may be shown before
1526 * doing that.
1527 */
1528 msg_didout |= msg_didout_before_start;
1529 wait_return(FALSE);
1530 }
1531 }
1532
Bram Moolenaar2a942252012-11-28 23:03:07 +01001533#ifdef FEAT_EVAL
1534 did_endif = FALSE; /* in case do_cmdline used recursively */
1535#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001536 /*
1537 * Reset if_level, in case a sourced script file contains more ":if" than
1538 * ":endif" (could be ":if x | foo | endif").
1539 */
1540 if_level = 0;
Bram Moolenaar2e18a122012-11-28 19:10:54 +01001541#endif
Bram Moolenaard4ad0d42012-11-28 17:34:48 +01001542
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543 --call_depth;
1544 return retval;
1545}
1546
1547#ifdef FEAT_EVAL
1548/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001549 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001550 */
1551 static char_u *
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001552get_loop_line(c, cookie, indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001553 int c;
1554 void *cookie;
1555 int indent;
1556{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001557 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001558 wcmd_T *wp;
1559 char_u *line;
1560
1561 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1562 {
1563 if (cp->repeating)
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001564 return NULL; /* trying to read past ":endwhile"/":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001565
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001566 /* First time inside the ":while"/":for": get line normally. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567 if (cp->getline == NULL)
1568 line = getcmdline(c, 0L, indent);
1569 else
1570 line = cp->getline(c, cp->cookie, indent);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001571 if (line != NULL && store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001572 ++cp->current_line;
1573
1574 return line;
1575 }
1576
1577 KeyTyped = FALSE;
1578 ++cp->current_line;
1579 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
1580 sourcing_lnum = wp->lnum;
1581 return vim_strsave(wp->line);
1582}
1583
1584/*
1585 * Store a line in "gap" so that a ":while" loop can execute it again.
1586 */
1587 static int
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001588store_loop_line(gap, line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589 garray_T *gap;
1590 char_u *line;
1591{
1592 if (ga_grow(gap, 1) == FAIL)
1593 return FAIL;
1594 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
1595 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = sourcing_lnum;
1596 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597 return OK;
1598}
1599
1600/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001601 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602 */
1603 static void
1604free_cmdlines(gap)
1605 garray_T *gap;
1606{
1607 while (gap->ga_len > 0)
1608 {
1609 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1610 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611 }
1612}
1613#endif
1614
1615/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001616 * If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals
1617 * "func". * Otherwise return TRUE when "fgetline" equals "func".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619 int
Bram Moolenaar89d40322006-08-29 15:30:07 +00001620getline_equal(fgetline, cookie, func)
1621 char_u *(*fgetline) __ARGS((int, void *, int));
Bram Moolenaar78a15312009-05-15 19:33:18 +00001622 void *cookie UNUSED; /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623 char_u *(*func) __ARGS((int, void *, int));
1624{
1625#ifdef FEAT_EVAL
1626 char_u *(*gp) __ARGS((int, void *, int));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001627 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628
Bram Moolenaar89d40322006-08-29 15:30:07 +00001629 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001630 * function that's originally used to obtain the lines. This may be
1631 * nested several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001632 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001633 cp = (struct loop_cookie *)cookie;
1634 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001635 {
1636 gp = cp->getline;
1637 cp = cp->cookie;
1638 }
1639 return gp == func;
1640#else
Bram Moolenaar89d40322006-08-29 15:30:07 +00001641 return fgetline == func;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001642#endif
1643}
1644
1645#if defined(FEAT_EVAL) || defined(FEAT_MBYTE) || defined(PROTO)
1646/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001647 * If "fgetline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001648 * getline function. Otherwise return "cookie".
1649 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001650 void *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001651getline_cookie(fgetline, cookie)
Bram Moolenaar78a15312009-05-15 19:33:18 +00001652 char_u *(*fgetline) __ARGS((int, void *, int)) UNUSED;
Bram Moolenaar89d40322006-08-29 15:30:07 +00001653 void *cookie; /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001654{
1655# ifdef FEAT_EVAL
1656 char_u *(*gp) __ARGS((int, void *, int));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001657 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658
Bram Moolenaar89d40322006-08-29 15:30:07 +00001659 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001660 * cookie that's originally used to obtain the lines. This may be nested
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661 * several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001662 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001663 cp = (struct loop_cookie *)cookie;
1664 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001665 {
1666 gp = cp->getline;
1667 cp = cp->cookie;
1668 }
1669 return cp;
1670# else
1671 return cookie;
1672# endif
1673}
1674#endif
1675
1676/*
1677 * Execute one Ex command.
1678 *
1679 * If 'sourcing' is TRUE, the command will be included in the error message.
1680 *
1681 * 1. skip comment lines and leading space
1682 * 2. handle command modifiers
1683 * 3. parse range
1684 * 4. parse command
1685 * 5. parse arguments
1686 * 6. switch on command name
1687 *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001688 * Note: "fgetline" can be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689 *
1690 * This function may be called recursively!
1691 */
1692#if (_MSC_VER == 1200)
1693/*
Bram Moolenaared203462004-06-16 11:19:22 +00001694 * Avoid optimisation bug in VC++ version 6.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001695 */
Bram Moolenaar281bdce2005-01-25 21:53:18 +00001696 #pragma optimize( "g", off )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001697#endif
1698 static char_u *
1699do_one_cmd(cmdlinep, sourcing,
1700#ifdef FEAT_EVAL
1701 cstack,
1702#endif
Bram Moolenaar89d40322006-08-29 15:30:07 +00001703 fgetline, cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001704 char_u **cmdlinep;
1705 int sourcing;
1706#ifdef FEAT_EVAL
1707 struct condstack *cstack;
1708#endif
Bram Moolenaar89d40322006-08-29 15:30:07 +00001709 char_u *(*fgetline) __ARGS((int, void *, int));
1710 void *cookie; /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001711{
1712 char_u *p;
1713 linenr_T lnum;
1714 long n;
1715 char_u *errormsg = NULL; /* error message */
1716 exarg_T ea; /* Ex command arguments */
1717 long verbose_save = -1;
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001718 int save_msg_scroll = msg_scroll;
1719 int save_msg_silent = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720 int did_esilent = 0;
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001721#ifdef HAVE_SANDBOX
1722 int did_sandbox = FALSE;
1723#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001724 cmdmod_T save_cmdmod;
1725 int ni; /* set when Not Implemented */
1726
1727 vim_memset(&ea, 0, sizeof(ea));
1728 ea.line1 = 1;
1729 ea.line2 = 1;
1730#ifdef FEAT_EVAL
1731 ++ex_nesting_level;
1732#endif
1733
Bram Moolenaar21691f82012-12-05 19:13:18 +01001734 /* When the last file has not been edited :q has to be typed twice. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001735 if (quitmore
1736#ifdef FEAT_EVAL
1737 /* avoid that a function call in 'statusline' does this */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001738 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001739#endif
1740#ifdef FEAT_AUTOCMD
Bram Moolenaar21691f82012-12-05 19:13:18 +01001741 /* avoid that an autocommand, e.g. QuitPre, does this */
1742 && !getline_equal(fgetline, cookie, getnextac)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001743#endif
1744 )
1745 --quitmore;
1746
1747 /*
1748 * Reset browse, confirm, etc.. They are restored when returning, for
1749 * recursive calls.
1750 */
1751 save_cmdmod = cmdmod;
1752 vim_memset(&cmdmod, 0, sizeof(cmdmod));
1753
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001754 /* "#!anything" is handled like a comment. */
1755 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1756 goto doend;
1757
Bram Moolenaar071d4272004-06-13 20:20:40 +00001758 /*
1759 * Repeat until no more command modifiers are found.
1760 */
1761 ea.cmd = *cmdlinep;
1762 for (;;)
1763 {
1764/*
1765 * 1. skip comment lines and leading white space and colons
1766 */
1767 while (*ea.cmd == ' ' || *ea.cmd == '\t' || *ea.cmd == ':')
1768 ++ea.cmd;
1769
1770 /* in ex mode, an empty line works like :+ */
1771 if (*ea.cmd == NUL && exmode_active
Bram Moolenaar89d40322006-08-29 15:30:07 +00001772 && (getline_equal(fgetline, cookie, getexmodeline)
1773 || getline_equal(fgetline, cookie, getexline))
Bram Moolenaardf177f62005-02-22 08:39:57 +00001774 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001775 {
1776 ea.cmd = (char_u *)"+";
1777 ex_pressedreturn = TRUE;
1778 }
1779
1780 /* ignore comment and empty lines */
Bram Moolenaarf998c042007-11-20 11:31:26 +00001781 if (*ea.cmd == '"')
1782 goto doend;
1783 if (*ea.cmd == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001784 {
1785 ex_pressedreturn = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001787 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788
1789/*
1790 * 2. handle command modifiers.
1791 */
1792 p = ea.cmd;
1793 if (VIM_ISDIGIT(*ea.cmd))
1794 p = skipwhite(skipdigits(ea.cmd));
1795 switch (*p)
1796 {
1797 /* When adding an entry, also modify cmd_exists(). */
1798 case 'a': if (!checkforcmd(&ea.cmd, "aboveleft", 3))
1799 break;
1800#ifdef FEAT_WINDOWS
1801 cmdmod.split |= WSP_ABOVE;
1802#endif
1803 continue;
1804
1805 case 'b': if (checkforcmd(&ea.cmd, "belowright", 3))
1806 {
1807#ifdef FEAT_WINDOWS
1808 cmdmod.split |= WSP_BELOW;
1809#endif
1810 continue;
1811 }
1812 if (checkforcmd(&ea.cmd, "browse", 3))
1813 {
Bram Moolenaard812df62008-11-09 12:46:09 +00001814#ifdef FEAT_BROWSE_CMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815 cmdmod.browse = TRUE;
1816#endif
1817 continue;
1818 }
1819 if (!checkforcmd(&ea.cmd, "botright", 2))
1820 break;
1821#ifdef FEAT_WINDOWS
1822 cmdmod.split |= WSP_BOT;
1823#endif
1824 continue;
1825
1826 case 'c': if (!checkforcmd(&ea.cmd, "confirm", 4))
1827 break;
1828#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1829 cmdmod.confirm = TRUE;
1830#endif
1831 continue;
1832
1833 case 'k': if (checkforcmd(&ea.cmd, "keepmarks", 3))
1834 {
1835 cmdmod.keepmarks = TRUE;
1836 continue;
1837 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001838 if (checkforcmd(&ea.cmd, "keepalt", 5))
1839 {
1840 cmdmod.keepalt = TRUE;
1841 continue;
1842 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843 if (!checkforcmd(&ea.cmd, "keepjumps", 5))
1844 break;
1845 cmdmod.keepjumps = TRUE;
1846 continue;
1847
1848 /* ":hide" and ":hide | cmd" are not modifiers */
1849 case 'h': if (p != ea.cmd || !checkforcmd(&p, "hide", 3)
1850 || *p == NUL || ends_excmd(*p))
1851 break;
1852 ea.cmd = p;
1853 cmdmod.hide = TRUE;
1854 continue;
1855
1856 case 'l': if (checkforcmd(&ea.cmd, "lockmarks", 3))
1857 {
1858 cmdmod.lockmarks = TRUE;
1859 continue;
1860 }
1861
1862 if (!checkforcmd(&ea.cmd, "leftabove", 5))
1863 break;
1864#ifdef FEAT_WINDOWS
1865 cmdmod.split |= WSP_ABOVE;
1866#endif
1867 continue;
1868
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001869 case 'n': if (!checkforcmd(&ea.cmd, "noautocmd", 3))
1870 break;
1871#ifdef FEAT_AUTOCMD
1872 if (cmdmod.save_ei == NULL)
1873 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001874 /* Set 'eventignore' to "all". Restore the
1875 * existing option value later. */
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001876 cmdmod.save_ei = vim_strsave(p_ei);
1877 set_string_option_direct((char_u *)"ei", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001878 (char_u *)"all", OPT_FREE, SID_NONE);
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001879 }
1880#endif
1881 continue;
1882
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883 case 'r': if (!checkforcmd(&ea.cmd, "rightbelow", 6))
1884 break;
1885#ifdef FEAT_WINDOWS
1886 cmdmod.split |= WSP_BELOW;
1887#endif
1888 continue;
1889
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001890 case 's': if (checkforcmd(&ea.cmd, "sandbox", 3))
1891 {
1892#ifdef HAVE_SANDBOX
1893 if (!did_sandbox)
1894 ++sandbox;
1895 did_sandbox = TRUE;
1896#endif
1897 continue;
1898 }
1899 if (!checkforcmd(&ea.cmd, "silent", 3))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900 break;
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001901 if (save_msg_silent == -1)
1902 save_msg_silent = msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903 ++msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001904 if (*ea.cmd == '!' && !vim_iswhite(ea.cmd[-1]))
1905 {
1906 /* ":silent!", but not "silent !cmd" */
1907 ea.cmd = skipwhite(ea.cmd + 1);
1908 ++emsg_silent;
1909 ++did_esilent;
1910 }
1911 continue;
1912
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001913 case 't': if (checkforcmd(&p, "tab", 3))
1914 {
1915#ifdef FEAT_WINDOWS
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001916 if (vim_isdigit(*ea.cmd))
1917 cmdmod.tab = atoi((char *)ea.cmd) + 1;
1918 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001919 cmdmod.tab = tabpage_index(curtab) + 1;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001920 ea.cmd = p;
1921#endif
1922 continue;
1923 }
1924 if (!checkforcmd(&ea.cmd, "topleft", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001925 break;
1926#ifdef FEAT_WINDOWS
1927 cmdmod.split |= WSP_TOP;
1928#endif
1929 continue;
1930
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001931 case 'u': if (!checkforcmd(&ea.cmd, "unsilent", 3))
1932 break;
1933 if (save_msg_silent == -1)
1934 save_msg_silent = msg_silent;
1935 msg_silent = 0;
1936 continue;
1937
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938 case 'v': if (checkforcmd(&ea.cmd, "vertical", 4))
1939 {
1940#ifdef FEAT_VERTSPLIT
1941 cmdmod.split |= WSP_VERT;
1942#endif
1943 continue;
1944 }
1945 if (!checkforcmd(&p, "verbose", 4))
1946 break;
1947 if (verbose_save < 0)
1948 verbose_save = p_verbose;
Bram Moolenaared203462004-06-16 11:19:22 +00001949 if (vim_isdigit(*ea.cmd))
1950 p_verbose = atoi((char *)ea.cmd);
1951 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001952 p_verbose = 1;
1953 ea.cmd = p;
1954 continue;
1955 }
1956 break;
1957 }
1958
1959#ifdef FEAT_EVAL
1960 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
1961 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
1962#else
1963 ea.skip = (if_level > 0);
1964#endif
1965
1966#ifdef FEAT_EVAL
Bram Moolenaar05159a02005-02-26 23:04:13 +00001967# ifdef FEAT_PROFILE
1968 /* Count this line for profiling if ea.skip is FALSE. */
Bram Moolenaar371d5402006-03-20 21:47:49 +00001969 if (do_profiling == PROF_YES && !ea.skip)
Bram Moolenaar05159a02005-02-26 23:04:13 +00001970 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001971 if (getline_equal(fgetline, cookie, get_func_line))
1972 func_line_exec(getline_cookie(fgetline, cookie));
1973 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +00001974 script_line_exec();
1975 }
1976#endif
1977
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978 /* May go to debug mode. If this happens and the ">quit" debug command is
1979 * used, throw an interrupt exception and skip the next command. */
1980 dbg_check_breakpoint(&ea);
1981 if (!ea.skip && got_int)
1982 {
1983 ea.skip = TRUE;
1984 (void)do_intthrow(cstack);
1985 }
1986#endif
1987
1988/*
1989 * 3. parse a range specifier of the form: addr [,addr] [;addr] ..
1990 *
1991 * where 'addr' is:
1992 *
1993 * % (entire file)
1994 * $ [+-NUM]
1995 * 'x [+-NUM] (where x denotes a currently defined mark)
1996 * . [+-NUM]
1997 * [+-NUM]..
1998 * NUM
1999 *
2000 * The ea.cmd pointer is updated to point to the first character following the
2001 * range spec. If an initial address is found, but no second, the upper bound
2002 * is equal to the lower.
2003 */
2004
2005 /* repeat for all ',' or ';' separated addresses */
2006 for (;;)
2007 {
2008 ea.line1 = ea.line2;
2009 ea.line2 = curwin->w_cursor.lnum; /* default is current line number */
2010 ea.cmd = skipwhite(ea.cmd);
2011 lnum = get_address(&ea.cmd, ea.skip, ea.addr_count == 0);
2012 if (ea.cmd == NULL) /* error detected */
2013 goto doend;
2014 if (lnum == MAXLNUM)
2015 {
2016 if (*ea.cmd == '%') /* '%' - all lines */
2017 {
2018 ++ea.cmd;
2019 ea.line1 = 1;
2020 ea.line2 = curbuf->b_ml.ml_line_count;
2021 ++ea.addr_count;
2022 }
2023 /* '*' - visual area */
2024 else if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
2025 {
2026 pos_T *fp;
2027
2028 ++ea.cmd;
2029 if (!ea.skip)
2030 {
2031 fp = getmark('<', FALSE);
2032 if (check_mark(fp) == FAIL)
2033 goto doend;
2034 ea.line1 = fp->lnum;
2035 fp = getmark('>', FALSE);
2036 if (check_mark(fp) == FAIL)
2037 goto doend;
2038 ea.line2 = fp->lnum;
2039 ++ea.addr_count;
2040 }
2041 }
2042 }
2043 else
2044 ea.line2 = lnum;
2045 ea.addr_count++;
2046
2047 if (*ea.cmd == ';')
2048 {
2049 if (!ea.skip)
2050 curwin->w_cursor.lnum = ea.line2;
2051 }
2052 else if (*ea.cmd != ',')
2053 break;
2054 ++ea.cmd;
2055 }
2056
2057 /* One address given: set start and end lines */
2058 if (ea.addr_count == 1)
2059 {
2060 ea.line1 = ea.line2;
2061 /* ... but only implicit: really no address given */
2062 if (lnum == MAXLNUM)
2063 ea.addr_count = 0;
2064 }
2065
2066 /* Don't leave the cursor on an illegal line (caused by ';') */
2067 check_cursor_lnum();
2068
2069/*
2070 * 4. parse command
2071 */
2072
2073 /*
2074 * Skip ':' and any white space
2075 */
2076 ea.cmd = skipwhite(ea.cmd);
2077 while (*ea.cmd == ':')
2078 ea.cmd = skipwhite(ea.cmd + 1);
2079
2080 /*
2081 * If we got a line, but no command, then go to the line.
2082 * If we find a '|' or '\n' we set ea.nextcmd.
2083 */
2084 if (*ea.cmd == NUL || *ea.cmd == '"' ||
2085 (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
2086 {
2087 /*
2088 * strange vi behaviour:
2089 * ":3" jumps to line 3
2090 * ":3|..." prints line 3
2091 * ":|" prints current line
2092 */
2093 if (ea.skip) /* skip this if inside :if */
2094 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00002095 if (*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002096 {
2097 ea.cmdidx = CMD_print;
2098 ea.argt = RANGE+COUNT+TRLBAR;
2099 if ((errormsg = invalid_range(&ea)) == NULL)
2100 {
2101 correct_range(&ea);
2102 ex_print(&ea);
2103 }
2104 }
2105 else if (ea.addr_count != 0)
2106 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002107 if (ea.line2 > curbuf->b_ml.ml_line_count)
2108 {
2109 /* With '-' in 'cpoptions' a line number past the file is an
2110 * error, otherwise put it at the end of the file. */
2111 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
2112 ea.line2 = -1;
2113 else
2114 ea.line2 = curbuf->b_ml.ml_line_count;
2115 }
2116
2117 if (ea.line2 < 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002118 errormsg = (char_u *)_(e_invrange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119 else
2120 {
2121 if (ea.line2 == 0)
2122 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002123 else
2124 curwin->w_cursor.lnum = ea.line2;
2125 beginline(BL_SOL | BL_FIX);
2126 }
2127 }
2128 goto doend;
2129 }
2130
2131 /* Find the command and let "p" point to after it. */
2132 p = find_command(&ea, NULL);
2133
2134#ifdef FEAT_USR_CMDS
2135 if (p == NULL)
2136 {
2137 if (!ea.skip)
2138 errormsg = (char_u *)_("E464: Ambiguous use of user-defined command");
2139 goto doend;
2140 }
2141 /* Check for wrong commands. */
2142 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78)
2143 {
2144 errormsg = uc_fun_cmd();
2145 goto doend;
2146 }
2147#endif
2148 if (ea.cmdidx == CMD_SIZE)
2149 {
2150 if (!ea.skip)
2151 {
2152 STRCPY(IObuff, _("E492: Not an editor command"));
2153 if (!sourcing)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002154 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155 errormsg = IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02002156 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002157 }
2158 goto doend;
2159 }
2160
2161 ni = (
2162#ifdef FEAT_USR_CMDS
2163 !USER_CMDIDX(ea.cmdidx) &&
2164#endif
Bram Moolenaar3ebc1e52007-07-05 07:54:17 +00002165 (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00002166#ifdef HAVE_EX_SCRIPT_NI
2167 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
2168#endif
2169 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002170
2171#ifndef FEAT_EVAL
2172 /*
2173 * When the expression evaluation is disabled, recognize the ":if" and
2174 * ":endif" commands and ignore everything in between it.
2175 */
2176 if (ea.cmdidx == CMD_if)
2177 ++if_level;
2178 if (if_level)
2179 {
2180 if (ea.cmdidx == CMD_endif)
2181 --if_level;
2182 goto doend;
2183 }
2184
2185#endif
2186
Bram Moolenaar196b3b02008-06-20 16:51:41 +00002187 /* forced commands */
2188 if (*p == '!' && ea.cmdidx != CMD_substitute
2189 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002190 {
2191 ++p;
2192 ea.forceit = TRUE;
2193 }
2194 else
2195 ea.forceit = FALSE;
2196
2197/*
2198 * 5. parse arguments
2199 */
2200#ifdef FEAT_USR_CMDS
2201 if (!USER_CMDIDX(ea.cmdidx))
2202#endif
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002203 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002204
2205 if (!ea.skip)
2206 {
2207#ifdef HAVE_SANDBOX
2208 if (sandbox != 0 && !(ea.argt & SBOXOK))
2209 {
2210 /* Command not allowed in sandbox. */
2211 errormsg = (char_u *)_(e_sandbox);
2212 goto doend;
2213 }
2214#endif
2215 if (!curbuf->b_p_ma && (ea.argt & MODIFY))
2216 {
2217 /* Command not allowed in non-'modifiable' buffer */
2218 errormsg = (char_u *)_(e_modifiable);
2219 goto doend;
2220 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002221
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002222 if (text_locked() && !(ea.argt & CMDWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223# ifdef FEAT_USR_CMDS
2224 && !USER_CMDIDX(ea.cmdidx)
2225# endif
2226 )
2227 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002228 /* Command not allowed when editing the command line. */
2229#ifdef FEAT_CMDWIN
2230 if (cmdwin_type != 0)
2231 errormsg = (char_u *)_(e_cmdwin);
2232 else
2233#endif
2234 errormsg = (char_u *)_(e_secure);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002235 goto doend;
2236 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002237#ifdef FEAT_AUTOCMD
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002238 /* Disallow editing another buffer when "curbuf_lock" is set.
2239 * Do allow ":edit" (check for argument later).
2240 * Do allow ":checktime" (it's postponed). */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002241 if (!(ea.argt & CMDWIN)
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002242 && ea.cmdidx != CMD_edit
2243 && ea.cmdidx != CMD_checktime
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002244# ifdef FEAT_USR_CMDS
2245 && !USER_CMDIDX(ea.cmdidx)
2246# endif
2247 && curbuf_locked())
2248 goto doend;
2249#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002250
2251 if (!ni && !(ea.argt & RANGE) && ea.addr_count > 0)
2252 {
2253 /* no range allowed */
2254 errormsg = (char_u *)_(e_norange);
2255 goto doend;
2256 }
2257 }
2258
2259 if (!ni && !(ea.argt & BANG) && ea.forceit) /* no <!> allowed */
2260 {
2261 errormsg = (char_u *)_(e_nobang);
2262 goto doend;
2263 }
2264
2265 /*
2266 * Don't complain about the range if it is not used
2267 * (could happen if line_count is accidentally set to 0).
2268 */
2269 if (!ea.skip && !ni)
2270 {
2271 /*
2272 * If the range is backwards, ask for confirmation and, if given, swap
2273 * ea.line1 & ea.line2 so it's forwards again.
2274 * When global command is busy, don't ask, will fail below.
2275 */
2276 if (!global_busy && ea.line1 > ea.line2)
2277 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002278 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002279 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002280 if (sourcing || exmode_active)
2281 {
2282 errormsg = (char_u *)_("E493: Backwards range given");
2283 goto doend;
2284 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285 if (ask_yesno((char_u *)
2286 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002287 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288 }
2289 lnum = ea.line1;
2290 ea.line1 = ea.line2;
2291 ea.line2 = lnum;
2292 }
2293 if ((errormsg = invalid_range(&ea)) != NULL)
2294 goto doend;
2295 }
2296
2297 if ((ea.argt & NOTADR) && ea.addr_count == 0) /* default is 1, not cursor */
2298 ea.line2 = 1;
2299
2300 correct_range(&ea);
2301
2302#ifdef FEAT_FOLDING
2303 if (((ea.argt & WHOLEFOLD) || ea.addr_count >= 2) && !global_busy)
2304 {
2305 /* Put the first line at the start of a closed fold, put the last line
2306 * at the end of a closed fold. */
2307 (void)hasFolding(ea.line1, &ea.line1, NULL);
2308 (void)hasFolding(ea.line2, NULL, &ea.line2);
2309 }
2310#endif
2311
2312#ifdef FEAT_QUICKFIX
2313 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002314 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315 * option here, so things like % get expanded.
2316 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002317 p = replace_makeprg(&ea, p, cmdlinep);
2318 if (p == NULL)
2319 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002320#endif
2321
2322 /*
2323 * Skip to start of argument.
2324 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2325 */
2326 if (ea.cmdidx == CMD_bang)
2327 ea.arg = p;
2328 else
2329 ea.arg = skipwhite(p);
2330
2331 /*
2332 * Check for "++opt=val" argument.
2333 * Must be first, allow ":w ++enc=utf8 !cmd"
2334 */
2335 if (ea.argt & ARGOPT)
2336 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2337 if (getargopt(&ea) == FAIL && !ni)
2338 {
2339 errormsg = (char_u *)_(e_invarg);
2340 goto doend;
2341 }
2342
2343 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2344 {
2345 if (*ea.arg == '>') /* append */
2346 {
2347 if (*++ea.arg != '>') /* typed wrong */
2348 {
2349 errormsg = (char_u *)_("E494: Use w or w>>");
2350 goto doend;
2351 }
2352 ea.arg = skipwhite(ea.arg + 1);
2353 ea.append = TRUE;
2354 }
2355 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
2356 {
2357 ++ea.arg;
2358 ea.usefilter = TRUE;
2359 }
2360 }
2361
2362 if (ea.cmdidx == CMD_read)
2363 {
2364 if (ea.forceit)
2365 {
2366 ea.usefilter = TRUE; /* :r! filter if ea.forceit */
2367 ea.forceit = FALSE;
2368 }
2369 else if (*ea.arg == '!') /* :r !filter */
2370 {
2371 ++ea.arg;
2372 ea.usefilter = TRUE;
2373 }
2374 }
2375
2376 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2377 {
2378 ea.amount = 1;
2379 while (*ea.arg == *ea.cmd) /* count number of '>' or '<' */
2380 {
2381 ++ea.arg;
2382 ++ea.amount;
2383 }
2384 ea.arg = skipwhite(ea.arg);
2385 }
2386
2387 /*
2388 * Check for "+command" argument, before checking for next command.
2389 * Don't do this for ":read !cmd" and ":write !cmd".
2390 */
2391 if ((ea.argt & EDITCMD) && !ea.usefilter)
2392 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2393
2394 /*
2395 * Check for '|' to separate commands and '"' to start comments.
2396 * Don't do this for ":read !cmd" and ":write !cmd".
2397 */
2398 if ((ea.argt & TRLBAR) && !ea.usefilter)
2399 separate_nextcmd(&ea);
2400
2401 /*
2402 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002403 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2404 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002406 else if (ea.cmdidx == CMD_bang
2407 || ea.cmdidx == CMD_global
2408 || ea.cmdidx == CMD_vglobal
2409 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410 {
2411 for (p = ea.arg; *p; ++p)
2412 {
2413 /* Remove one backslash before a newline, so that it's possible to
2414 * pass a newline to the shell and also a newline that is preceded
2415 * with a backslash. This makes it impossible to end a shell
2416 * command in a backslash, but that doesn't appear useful.
2417 * Halving the number of backslashes is incompatible with previous
2418 * versions. */
2419 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002420 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421 else if (*p == '\n')
2422 {
2423 ea.nextcmd = p + 1;
2424 *p = NUL;
2425 break;
2426 }
2427 }
2428 }
2429
2430 if ((ea.argt & DFLALL) && ea.addr_count == 0)
2431 {
2432 ea.line1 = 1;
2433 ea.line2 = curbuf->b_ml.ml_line_count;
2434 }
2435
2436 /* accept numbered register only when no count allowed (:put) */
2437 if ( (ea.argt & REGSTR)
2438 && *ea.arg != NUL
2439#ifdef FEAT_USR_CMDS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002440 /* Do not allow register = for user commands */
2441 && (!USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002442#endif
2443 && !((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)))
2444 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002445#ifndef FEAT_CLIPBOARD
2446 /* check these explicitly for a more specific error message */
2447 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002449 errormsg = (char_u *)_(e_invalidreg);
2450 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451 }
2452#endif
Bram Moolenaar85de2062011-05-05 14:26:41 +02002453 if (
2454#ifdef FEAT_USR_CMDS
2455 valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2456 && USER_CMDIDX(ea.cmdidx)))
2457#else
2458 valid_yank_reg(*ea.arg, ea.cmdidx != CMD_put)
2459#endif
2460 )
2461 {
2462 ea.regname = *ea.arg++;
2463#ifdef FEAT_EVAL
2464 /* for '=' register: accept the rest of the line as an expression */
2465 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2466 {
2467 set_expr_line(vim_strsave(ea.arg));
2468 ea.arg += STRLEN(ea.arg);
2469 }
2470#endif
2471 ea.arg = skipwhite(ea.arg);
2472 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473 }
2474
2475 /*
2476 * Check for a count. When accepting a BUFNAME, don't use "123foo" as a
2477 * count, it's a buffer name.
2478 */
2479 if ((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)
2480 && (!(ea.argt & BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
2481 || vim_iswhite(*p)))
2482 {
2483 n = getdigits(&ea.arg);
2484 ea.arg = skipwhite(ea.arg);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002485 if (n <= 0 && !ni && (ea.argt & ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486 {
2487 errormsg = (char_u *)_(e_zerocount);
2488 goto doend;
2489 }
2490 if (ea.argt & NOTADR) /* e.g. :buffer 2, :sleep 3 */
2491 {
2492 ea.line2 = n;
2493 if (ea.addr_count == 0)
2494 ea.addr_count = 1;
2495 }
2496 else
2497 {
2498 ea.line1 = ea.line2;
2499 ea.line2 += n - 1;
2500 ++ea.addr_count;
2501 /*
2502 * Be vi compatible: no error message for out of range.
2503 */
2504 if (ea.line2 > curbuf->b_ml.ml_line_count)
2505 ea.line2 = curbuf->b_ml.ml_line_count;
2506 }
2507 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002508
2509 /*
2510 * Check for flags: 'l', 'p' and '#'.
2511 */
2512 if (ea.argt & EXFLAGS)
2513 get_flags(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514 /* no arguments allowed */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002515 if (!ni && !(ea.argt & EXTRA) && *ea.arg != NUL
Bram Moolenaara2031822006-03-07 22:29:51 +00002516 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002517 {
2518 errormsg = (char_u *)_(e_trailing);
2519 goto doend;
2520 }
2521
2522 if (!ni && (ea.argt & NEEDARG) && *ea.arg == NUL)
2523 {
2524 errormsg = (char_u *)_(e_argreq);
2525 goto doend;
2526 }
2527
2528#ifdef FEAT_EVAL
2529 /*
2530 * Skip the command when it's not going to be executed.
2531 * The commands like :if, :endif, etc. always need to be executed.
2532 * Also make an exception for commands that handle a trailing command
2533 * themselves.
2534 */
2535 if (ea.skip)
2536 {
2537 switch (ea.cmdidx)
2538 {
2539 /* commands that need evaluation */
2540 case CMD_while:
2541 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002542 case CMD_for:
2543 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544 case CMD_if:
2545 case CMD_elseif:
2546 case CMD_else:
2547 case CMD_endif:
2548 case CMD_try:
2549 case CMD_catch:
2550 case CMD_finally:
2551 case CMD_endtry:
2552 case CMD_function:
2553 break;
2554
2555 /* Commands that handle '|' themselves. Check: A command should
2556 * either have the TRLBAR flag, appear in this list or appear in
2557 * the list at ":help :bar". */
2558 case CMD_aboveleft:
2559 case CMD_and:
2560 case CMD_belowright:
2561 case CMD_botright:
2562 case CMD_browse:
2563 case CMD_call:
2564 case CMD_confirm:
2565 case CMD_delfunction:
2566 case CMD_djump:
2567 case CMD_dlist:
2568 case CMD_dsearch:
2569 case CMD_dsplit:
2570 case CMD_echo:
2571 case CMD_echoerr:
2572 case CMD_echomsg:
2573 case CMD_echon:
2574 case CMD_execute:
2575 case CMD_help:
2576 case CMD_hide:
2577 case CMD_ijump:
2578 case CMD_ilist:
2579 case CMD_isearch:
2580 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002581 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582 case CMD_keepjumps:
2583 case CMD_keepmarks:
2584 case CMD_leftabove:
2585 case CMD_let:
2586 case CMD_lockmarks:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002587 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002588 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002589 case CMD_mzscheme:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002590 case CMD_perl:
2591 case CMD_psearch:
2592 case CMD_python:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002593 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002594 case CMD_python3:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595 case CMD_return:
2596 case CMD_rightbelow:
2597 case CMD_ruby:
2598 case CMD_silent:
2599 case CMD_smagic:
2600 case CMD_snomagic:
2601 case CMD_substitute:
2602 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002603 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604 case CMD_tcl:
2605 case CMD_throw:
2606 case CMD_tilde:
2607 case CMD_topleft:
2608 case CMD_unlet:
2609 case CMD_verbose:
2610 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002611 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002612 break;
2613
2614 default: goto doend;
2615 }
2616 }
2617#endif
2618
2619 if (ea.argt & XFILE)
2620 {
2621 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2622 goto doend;
2623 }
2624
2625#ifdef FEAT_LISTCMDS
2626 /*
2627 * Accept buffer name. Cannot be used at the same time with a buffer
2628 * number. Don't do this for a user command.
2629 */
2630 if ((ea.argt & BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
2631# ifdef FEAT_USR_CMDS
2632 && !USER_CMDIDX(ea.cmdidx)
2633# endif
2634 )
2635 {
2636 /*
2637 * :bdelete, :bwipeout and :bunload take several arguments, separated
2638 * by spaces: find next space (skipping over escaped characters).
2639 * The others take one argument: ignore trailing spaces.
2640 */
2641 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2642 || ea.cmdidx == CMD_bunload)
2643 p = skiptowhite_esc(ea.arg);
2644 else
2645 {
2646 p = ea.arg + STRLEN(ea.arg);
2647 while (p > ea.arg && vim_iswhite(p[-1]))
2648 --p;
2649 }
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002650 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & BUFUNL) != 0,
2651 FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652 if (ea.line2 < 0) /* failed */
2653 goto doend;
2654 ea.addr_count = 1;
2655 ea.arg = skipwhite(p);
2656 }
2657#endif
2658
2659/*
2660 * 6. switch on command name
2661 *
2662 * The "ea" structure holds the arguments that can be used.
2663 */
2664 ea.cmdlinep = cmdlinep;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002665 ea.getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002666 ea.cookie = cookie;
2667#ifdef FEAT_EVAL
2668 ea.cstack = cstack;
2669#endif
2670
2671#ifdef FEAT_USR_CMDS
2672 if (USER_CMDIDX(ea.cmdidx))
2673 {
2674 /*
2675 * Execute a user-defined command.
2676 */
2677 do_ucmd(&ea);
2678 }
2679 else
2680#endif
2681 {
2682 /*
2683 * Call the function to execute the command.
2684 */
2685 ea.errmsg = NULL;
2686 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2687 if (ea.errmsg != NULL)
2688 errormsg = (char_u *)_(ea.errmsg);
2689 }
2690
2691#ifdef FEAT_EVAL
2692 /*
2693 * If the command just executed called do_cmdline(), any throw or ":return"
2694 * or ":finish" encountered there must also check the cstack of the still
2695 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2696 * exception, or reanimate a returned function or finished script file and
2697 * return or finish it again.
2698 */
2699 if (need_rethrow)
2700 do_throw(cstack);
2701 else if (check_cstack)
2702 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002703 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002705 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706 && current_func_returned())
2707 do_return(&ea, TRUE, FALSE, NULL);
2708 }
2709 need_rethrow = check_cstack = FALSE;
2710#endif
2711
2712doend:
2713 if (curwin->w_cursor.lnum == 0) /* can happen with zero line number */
2714 curwin->w_cursor.lnum = 1;
2715
2716 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2717 {
2718 if (sourcing)
2719 {
2720 if (errormsg != IObuff)
2721 {
2722 STRCPY(IObuff, errormsg);
2723 errormsg = IObuff;
2724 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002725 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002726 }
2727 emsg(errormsg);
2728 }
2729#ifdef FEAT_EVAL
2730 do_errthrow(cstack,
2731 (ea.cmdidx != CMD_SIZE
2732# ifdef FEAT_USR_CMDS
2733 && !USER_CMDIDX(ea.cmdidx)
2734# endif
2735 ) ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
2736#endif
2737
2738 if (verbose_save >= 0)
2739 p_verbose = verbose_save;
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00002740#ifdef FEAT_AUTOCMD
2741 if (cmdmod.save_ei != NULL)
2742 {
2743 /* Restore 'eventignore' to the value before ":noautocmd". */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002744 set_string_option_direct((char_u *)"ei", -1, cmdmod.save_ei,
2745 OPT_FREE, SID_NONE);
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00002746 free_string_option(cmdmod.save_ei);
2747 }
2748#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002749
2750 cmdmod = save_cmdmod;
2751
Bram Moolenaar8e258a42009-07-09 13:55:43 +00002752 if (save_msg_silent != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002753 {
2754 /* messages could be enabled for a serious error, need to check if the
2755 * counters don't become negative */
Bram Moolenaarbecf4282009-09-30 11:24:36 +00002756 if (!did_emsg || msg_silent > save_msg_silent)
Bram Moolenaar8e258a42009-07-09 13:55:43 +00002757 msg_silent = save_msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002758 emsg_silent -= did_esilent;
2759 if (emsg_silent < 0)
2760 emsg_silent = 0;
2761 /* Restore msg_scroll, it's set by file I/O commands, even when no
2762 * message is actually displayed. */
2763 msg_scroll = save_msg_scroll;
Bram Moolenaar77ab2802009-04-22 12:44:48 +00002764
2765 /* "silent reg" or "silent echo x" inside "redir" leaves msg_col
2766 * somewhere in the line. Put it back in the first column. */
2767 if (redirecting())
2768 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002769 }
2770
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002771#ifdef HAVE_SANDBOX
2772 if (did_sandbox)
2773 --sandbox;
2774#endif
2775
Bram Moolenaar071d4272004-06-13 20:20:40 +00002776 if (ea.nextcmd && *ea.nextcmd == NUL) /* not really a next command */
2777 ea.nextcmd = NULL;
2778
2779#ifdef FEAT_EVAL
2780 --ex_nesting_level;
2781#endif
2782
2783 return ea.nextcmd;
2784}
2785#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00002786 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002787#endif
2788
2789/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002790 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002791 * If there is a match advance "pp" to the argument and return TRUE.
2792 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002793 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00002794checkforcmd(pp, cmd, len)
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002795 char_u **pp; /* start of command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002796 char *cmd; /* name of command */
2797 int len; /* required length */
2798{
2799 int i;
2800
2801 for (i = 0; cmd[i] != NUL; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00002802 if (((char_u *)cmd)[i] != (*pp)[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00002803 break;
2804 if (i >= len && !isalpha((*pp)[i]))
2805 {
2806 *pp = skipwhite(*pp + i);
2807 return TRUE;
2808 }
2809 return FALSE;
2810}
2811
2812/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002813 * Append "cmd" to the error message in IObuff.
2814 * Takes care of limiting the length and handling 0xa0, which would be
2815 * invisible otherwise.
2816 */
2817 static void
2818append_command(cmd)
2819 char_u *cmd;
2820{
2821 char_u *s = cmd;
2822 char_u *d;
2823
2824 STRCAT(IObuff, ": ");
2825 d = IObuff + STRLEN(IObuff);
2826 while (*s != NUL && d - IObuff < IOSIZE - 7)
2827 {
2828 if (
2829#ifdef FEAT_MBYTE
2830 enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) :
2831#endif
2832 *s == 0xa0)
2833 {
2834 s +=
2835#ifdef FEAT_MBYTE
2836 enc_utf8 ? 2 :
2837#endif
2838 1;
2839 STRCPY(d, "<a0>");
2840 d += 4;
2841 }
2842 else
2843 MB_COPY_CHAR(s, d);
2844 }
2845 *d = NUL;
2846}
2847
2848/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002849 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002850 * Start of the name can be found at eap->cmd.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002851 * Returns pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002852 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002853 * Returns NULL for an ambiguous user command.
2854 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002855 static char_u *
2856find_command(eap, full)
2857 exarg_T *eap;
Bram Moolenaar78a15312009-05-15 19:33:18 +00002858 int *full UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002859{
2860 int len;
2861 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00002862 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002863
2864 /*
2865 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00002866 * Exceptions:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002867 * - the 'k' command can directly be followed by any character.
2868 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
2869 * but :sre[wind] is another command, as are :scrip[tnames],
2870 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00002871 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002872 */
2873 p = eap->cmd;
2874 if (*p == 'k')
2875 {
2876 eap->cmdidx = CMD_k;
2877 ++p;
2878 }
2879 else if (p[0] == 's'
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002880 && ((p[1] == 'c' && p[2] != 's' && p[2] != 'r'
2881 && p[3] != 'i' && p[4] != 'p')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002882 || p[1] == 'g'
2883 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
2884 || p[1] == 'I'
2885 || (p[1] == 'r' && p[2] != 'e')))
2886 {
2887 eap->cmdidx = CMD_substitute;
2888 ++p;
2889 }
2890 else
2891 {
2892 while (ASCII_ISALPHA(*p))
2893 ++p;
Bram Moolenaarb6590522010-07-21 16:00:43 +02002894 /* for python 3.x support ":py3", ":python3", ":py3file", etc. */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002895 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaarb6590522010-07-21 16:00:43 +02002896 while (ASCII_ISALNUM(*p))
2897 ++p;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02002898
Bram Moolenaar071d4272004-06-13 20:20:40 +00002899 /* check for non-alpha command */
2900 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
2901 ++p;
2902 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00002903 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
2904 {
2905 /* Check for ":dl", ":dell", etc. to ":deletel": that's
2906 * :delete with the 'l' flag. Same for 'p'. */
2907 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00002908 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00002909 break;
2910 if (i == len - 1)
2911 {
2912 --len;
2913 if (p[-1] == 'l')
2914 eap->flags |= EXFLAG_LIST;
2915 else
2916 eap->flags |= EXFLAG_PRINT;
2917 }
2918 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002919
2920 if (ASCII_ISLOWER(*eap->cmd))
2921 eap->cmdidx = cmdidxs[CharOrdLow(*eap->cmd)];
2922 else
2923 eap->cmdidx = cmdidxs[26];
2924
2925 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
2926 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
2927 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
2928 (size_t)len) == 0)
2929 {
2930#ifdef FEAT_EVAL
2931 if (full != NULL
2932 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
2933 *full = TRUE;
2934#endif
2935 break;
2936 }
2937
2938#ifdef FEAT_USR_CMDS
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01002939 /* Look for a user defined command as a last resort. Let ":Print" be
2940 * overruled by a user defined command. */
2941 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
2942 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002943 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002944 /* User defined commands may contain digits. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002945 while (ASCII_ISALNUM(*p))
2946 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002947 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002948 }
2949#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002950 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002951 eap->cmdidx = CMD_SIZE;
2952 }
2953
2954 return p;
2955}
2956
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002957#ifdef FEAT_USR_CMDS
2958/*
2959 * Search for a user command that matches "eap->cmd".
2960 * Return cmdidx in "eap->cmdidx", flags in "eap->argt", idx in "eap->useridx".
2961 * Return a pointer to just after the command.
2962 * Return NULL if there is no matching command.
2963 */
2964 static char_u *
2965find_ucmd(eap, p, full, xp, compl)
2966 exarg_T *eap;
2967 char_u *p; /* end of the command (possibly including count) */
2968 int *full; /* set to TRUE for a full match */
2969 expand_T *xp; /* used for completion, NULL otherwise */
2970 int *compl; /* completion flags or NULL */
2971{
2972 int len = (int)(p - eap->cmd);
2973 int j, k, matchlen = 0;
2974 ucmd_T *uc;
2975 int found = FALSE;
2976 int possible = FALSE;
2977 char_u *cp, *np; /* Point into typed cmd and test name */
2978 garray_T *gap;
2979 int amb_local = FALSE; /* Found ambiguous buffer-local command,
2980 only full match global is accepted. */
2981
2982 /*
2983 * Look for buffer-local user commands first, then global ones.
2984 */
2985 gap = &curbuf->b_ucmds;
2986 for (;;)
2987 {
2988 for (j = 0; j < gap->ga_len; ++j)
2989 {
2990 uc = USER_CMD_GA(gap, j);
2991 cp = eap->cmd;
2992 np = uc->uc_name;
2993 k = 0;
2994 while (k < len && *np != NUL && *cp++ == *np++)
2995 k++;
2996 if (k == len || (*np == NUL && vim_isdigit(eap->cmd[k])))
2997 {
2998 /* If finding a second match, the command is ambiguous. But
2999 * not if a buffer-local command wasn't a full match and a
3000 * global command is a full match. */
3001 if (k == len && found && *np != NUL)
3002 {
3003 if (gap == &ucmds)
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003004 return NULL;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003005 amb_local = TRUE;
3006 }
3007
3008 if (!found || (k == len && *np == NUL))
3009 {
3010 /* If we matched up to a digit, then there could
3011 * be another command including the digit that we
3012 * should use instead.
3013 */
3014 if (k == len)
3015 found = TRUE;
3016 else
3017 possible = TRUE;
3018
3019 if (gap == &ucmds)
3020 eap->cmdidx = CMD_USER;
3021 else
3022 eap->cmdidx = CMD_USER_BUF;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003023 eap->argt = (long)uc->uc_argt;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003024 eap->useridx = j;
3025
3026# ifdef FEAT_CMDL_COMPL
3027 if (compl != NULL)
3028 *compl = uc->uc_compl;
3029# ifdef FEAT_EVAL
3030 if (xp != NULL)
3031 {
3032 xp->xp_arg = uc->uc_compl_arg;
3033 xp->xp_scriptID = uc->uc_scriptID;
3034 }
3035# endif
3036# endif
3037 /* Do not search for further abbreviations
3038 * if this is an exact match. */
3039 matchlen = k;
3040 if (k == len && *np == NUL)
3041 {
3042 if (full != NULL)
3043 *full = TRUE;
3044 amb_local = FALSE;
3045 break;
3046 }
3047 }
3048 }
3049 }
3050
3051 /* Stop if we found a full match or searched all. */
3052 if (j < gap->ga_len || gap == &ucmds)
3053 break;
3054 gap = &ucmds;
3055 }
3056
3057 /* Only found ambiguous matches. */
3058 if (amb_local)
3059 {
3060 if (xp != NULL)
3061 xp->xp_context = EXPAND_UNSUCCESSFUL;
3062 return NULL;
3063 }
3064
3065 /* The match we found may be followed immediately by a number. Move "p"
3066 * back to point to it. */
3067 if (found || possible)
3068 return p + (matchlen - len);
3069 return p;
3070}
3071#endif
3072
Bram Moolenaar071d4272004-06-13 20:20:40 +00003073#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003074static struct cmdmod
3075{
3076 char *name;
3077 int minlen;
3078 int has_count; /* :123verbose :3tab */
3079} cmdmods[] = {
3080 {"aboveleft", 3, FALSE},
3081 {"belowright", 3, FALSE},
3082 {"botright", 2, FALSE},
3083 {"browse", 3, FALSE},
3084 {"confirm", 4, FALSE},
3085 {"hide", 3, FALSE},
3086 {"keepalt", 5, FALSE},
3087 {"keepjumps", 5, FALSE},
3088 {"keepmarks", 3, FALSE},
3089 {"leftabove", 5, FALSE},
3090 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003091 {"noautocmd", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003092 {"rightbelow", 6, FALSE},
3093 {"sandbox", 3, FALSE},
3094 {"silent", 3, FALSE},
3095 {"tab", 3, TRUE},
3096 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003097 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003098 {"verbose", 4, TRUE},
3099 {"vertical", 4, FALSE},
3100};
3101
3102/*
3103 * Return length of a command modifier (including optional count).
3104 * Return zero when it's not a modifier.
3105 */
3106 int
3107modifier_len(cmd)
3108 char_u *cmd;
3109{
3110 int i, j;
3111 char_u *p = cmd;
3112
3113 if (VIM_ISDIGIT(*cmd))
3114 p = skipwhite(skipdigits(cmd));
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003115 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003116 {
3117 for (j = 0; p[j] != NUL; ++j)
3118 if (p[j] != cmdmods[i].name[j])
3119 break;
3120 if (!isalpha(p[j]) && j >= cmdmods[i].minlen
3121 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003122 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003123 }
3124 return 0;
3125}
3126
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127/*
3128 * Return > 0 if an Ex command "name" exists.
3129 * Return 2 if there is an exact match.
3130 * Return 3 if there is an ambiguous match.
3131 */
3132 int
3133cmd_exists(name)
3134 char_u *name;
3135{
3136 exarg_T ea;
3137 int full = FALSE;
3138 int i;
3139 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003140 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003141
3142 /* Check command modifiers. */
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003143 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003144 {
3145 for (j = 0; name[j] != NUL; ++j)
3146 if (name[j] != cmdmods[i].name[j])
3147 break;
3148 if (name[j] == NUL && j >= cmdmods[i].minlen)
3149 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3150 }
3151
Bram Moolenaara9587612006-05-04 21:47:50 +00003152 /* Check built-in commands and user defined commands.
3153 * For ":2match" and ":3match" we need to skip the number. */
3154 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003155 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003156 p = find_command(&ea, &full);
3157 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003158 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003159 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3160 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003161 if (*skipwhite(p) != NUL)
3162 return 0; /* trailing garbage */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003163 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3164}
3165#endif
3166
3167/*
3168 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
3169 * we don't need/want deleted. Maybe this could be done better if we didn't
3170 * repeat all this stuff. The only problem is that they may not stay
3171 * perfectly compatible with each other, but then the command line syntax
3172 * probably won't change that much -- webb.
3173 */
3174 char_u *
3175set_one_cmd_context(xp, buff)
3176 expand_T *xp;
3177 char_u *buff; /* buffer for command string */
3178{
3179 char_u *p;
3180 char_u *cmd, *arg;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003181 int len = 0;
3182 exarg_T ea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003183#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3184 int compl = EXPAND_NOTHING;
3185#endif
3186#ifdef FEAT_CMDL_COMPL
3187 int delim;
3188#endif
3189 int forceit = FALSE;
3190 int usefilter = FALSE; /* filter instead of file name */
3191
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003192 ExpandInit(xp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193 xp->xp_pattern = buff;
3194 xp->xp_context = EXPAND_COMMANDS; /* Default until we get past command */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003195 ea.argt = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003196
3197/*
3198 * 2. skip comment lines and leading space, colons or bars
3199 */
3200 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
3201 ;
3202 xp->xp_pattern = cmd;
3203
3204 if (*cmd == NUL)
3205 return NULL;
3206 if (*cmd == '"') /* ignore comment lines */
3207 {
3208 xp->xp_context = EXPAND_NOTHING;
3209 return NULL;
3210 }
3211
3212/*
3213 * 3. parse a range specifier of the form: addr [,addr] [;addr] ..
3214 */
3215 cmd = skip_range(cmd, &xp->xp_context);
3216
3217/*
3218 * 4. parse command
3219 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003220 xp->xp_pattern = cmd;
3221 if (*cmd == NUL)
3222 return NULL;
3223 if (*cmd == '"')
3224 {
3225 xp->xp_context = EXPAND_NOTHING;
3226 return NULL;
3227 }
3228
3229 if (*cmd == '|' || *cmd == '\n')
3230 return cmd + 1; /* There's another command */
3231
3232 /*
3233 * Isolate the command and search for it in the command table.
3234 * Exceptions:
3235 * - the 'k' command can directly be followed by any character, but
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003236 * do accept "keepmarks", "keepalt" and "keepjumps".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003237 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
3238 */
3239 if (*cmd == 'k' && cmd[1] != 'e')
3240 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003241 ea.cmdidx = CMD_k;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003242 p = cmd + 1;
3243 }
3244 else
3245 {
3246 p = cmd;
3247 while (ASCII_ISALPHA(*p) || *p == '*') /* Allow * wild card */
3248 ++p;
3249 /* check for non-alpha command */
3250 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3251 ++p;
Bram Moolenaar9f5d6002013-06-02 19:22:13 +02003252 /* for python 3.x: ":py3*" commands completion */
3253 if (cmd[0] == 'p' && cmd[1] == 'y' && p == cmd + 2 && *p == '3')
3254 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003255 len = (int)(p - cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003256
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003257 if (len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003258 {
3259 xp->xp_context = EXPAND_UNSUCCESSFUL;
3260 return NULL;
3261 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003262 for (ea.cmdidx = (cmdidx_T)0; (int)ea.cmdidx < (int)CMD_SIZE;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01003263 ea.cmdidx = (cmdidx_T)((int)ea.cmdidx + 1))
3264 if (STRNCMP(cmdnames[(int)ea.cmdidx].cmd_name, cmd,
3265 (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003266 break;
3267
3268#ifdef FEAT_USR_CMDS
3269 if (cmd[0] >= 'A' && cmd[0] <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270 while (ASCII_ISALNUM(*p) || *p == '*') /* Allow * wild card */
3271 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272#endif
3273 }
3274
3275 /*
3276 * If the cursor is touching the command, and it ends in an alpha-numeric
3277 * character, complete the command name.
3278 */
3279 if (*p == NUL && ASCII_ISALNUM(p[-1]))
3280 return NULL;
3281
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003282 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283 {
3284 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
3285 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003286 ea.cmdidx = CMD_substitute;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003287 p = cmd + 1;
3288 }
3289#ifdef FEAT_USR_CMDS
3290 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
3291 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003292 ea.cmd = cmd;
3293 p = find_ucmd(&ea, p, NULL, xp,
3294# if defined(FEAT_CMDL_COMPL)
3295 &compl
3296# else
3297 NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003298# endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003299 );
Bram Moolenaarebefac62005-12-28 22:39:57 +00003300 if (p == NULL)
3301 ea.cmdidx = CMD_SIZE; /* ambiguous user command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003302 }
3303#endif
3304 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003305 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306 {
3307 /* Not still touching the command and it was an illegal one */
3308 xp->xp_context = EXPAND_UNSUCCESSFUL;
3309 return NULL;
3310 }
3311
3312 xp->xp_context = EXPAND_NOTHING; /* Default now that we're past command */
3313
3314 if (*p == '!') /* forced commands */
3315 {
3316 forceit = TRUE;
3317 ++p;
3318 }
3319
3320/*
3321 * 5. parse arguments
3322 */
3323#ifdef FEAT_USR_CMDS
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003324 if (!USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325#endif
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003326 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327
3328 arg = skipwhite(p);
3329
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003330 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331 {
3332 if (*arg == '>') /* append */
3333 {
3334 if (*++arg == '>')
3335 ++arg;
3336 arg = skipwhite(arg);
3337 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003338 else if (*arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003339 {
3340 ++arg;
3341 usefilter = TRUE;
3342 }
3343 }
3344
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003345 if (ea.cmdidx == CMD_read)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346 {
3347 usefilter = forceit; /* :r! filter if forced */
3348 if (*arg == '!') /* :r !filter */
3349 {
3350 ++arg;
3351 usefilter = TRUE;
3352 }
3353 }
3354
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003355 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003356 {
3357 while (*arg == *cmd) /* allow any number of '>' or '<' */
3358 ++arg;
3359 arg = skipwhite(arg);
3360 }
3361
3362 /* Does command allow "+command"? */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003363 if ((ea.argt & EDITCMD) && !usefilter && *arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364 {
3365 /* Check if we're in the +command */
3366 p = arg + 1;
3367 arg = skip_cmd_arg(arg, FALSE);
3368
3369 /* Still touching the command after '+'? */
3370 if (*arg == NUL)
3371 return p;
3372
3373 /* Skip space(s) after +command to get to the real argument */
3374 arg = skipwhite(arg);
3375 }
3376
3377 /*
3378 * Check for '|' to separate commands and '"' to start comments.
3379 * Don't do this for ":read !cmd" and ":write !cmd".
3380 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003381 if ((ea.argt & TRLBAR) && !usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382 {
3383 p = arg;
3384 /* ":redir @" is not the start of a comment */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003385 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386 p += 2;
3387 while (*p)
3388 {
3389 if (*p == Ctrl_V)
3390 {
3391 if (p[1] != NUL)
3392 ++p;
3393 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003394 else if ( (*p == '"' && !(ea.argt & NOTRLCOM))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395 || *p == '|' || *p == '\n')
3396 {
3397 if (*(p - 1) != '\\')
3398 {
3399 if (*p == '|' || *p == '\n')
3400 return p + 1;
3401 return NULL; /* It's a comment */
3402 }
3403 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003404 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405 }
3406 }
3407
3408 /* no arguments allowed */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003409 if (!(ea.argt & EXTRA) && *arg != NUL &&
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410 vim_strchr((char_u *)"|\"", *arg) == NULL)
3411 return NULL;
3412
3413 /* Find start of last argument (argument just before cursor): */
Bram Moolenaar848f8762012-07-25 17:22:23 +02003414 p = buff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415 xp->xp_pattern = p;
Bram Moolenaar09168a72012-08-02 21:24:42 +02003416 len = (int)STRLEN(buff);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003417 while (*p && p < buff + len)
3418 {
3419 if (*p == ' ' || *p == TAB)
3420 {
3421 /* argument starts after a space */
3422 xp->xp_pattern = ++p;
3423 }
3424 else
3425 {
3426 if (*p == '\\' && *(p + 1) != NUL)
3427 ++p; /* skip over escaped character */
3428 mb_ptr_adv(p);
3429 }
3430 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003432 if (ea.argt & XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003434 int c;
3435 int in_quote = FALSE;
3436 char_u *bow = NULL; /* Beginning of word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437
3438 /*
3439 * Allow spaces within back-quotes to count as part of the argument
3440 * being expanded.
3441 */
3442 xp->xp_pattern = skipwhite(arg);
Bram Moolenaar6529c102007-08-18 15:47:34 +00003443 p = xp->xp_pattern;
3444 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003446#ifdef FEAT_MBYTE
3447 if (has_mbyte)
3448 c = mb_ptr2char(p);
3449 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450#endif
Bram Moolenaar6529c102007-08-18 15:47:34 +00003451 c = *p;
3452 if (c == '\\' && p[1] != NUL)
3453 ++p;
3454 else if (c == '`')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455 {
3456 if (!in_quote)
3457 {
3458 xp->xp_pattern = p;
3459 bow = p + 1;
3460 }
3461 in_quote = !in_quote;
3462 }
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003463 /* An argument can contain just about everything, except
3464 * characters that end the command and white space. */
3465 else if (c == '|' || c == '\n' || c == '"' || (vim_iswhite(c)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003466#ifdef SPACE_IN_FILENAME
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003467 && (!(ea.argt & NOSPC) || usefilter)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003468#endif
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003469 ))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003470 {
Bram Moolenaarcf5a5b82008-02-26 20:30:12 +00003471 len = 0; /* avoid getting stuck when space is in 'isfname' */
Bram Moolenaar6529c102007-08-18 15:47:34 +00003472 while (*p != NUL)
3473 {
3474#ifdef FEAT_MBYTE
3475 if (has_mbyte)
3476 c = mb_ptr2char(p);
3477 else
3478#endif
3479 c = *p;
Bram Moolenaardd87969c2007-08-21 13:07:12 +00003480 if (c == '`' || vim_isfilec_or_wc(c))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003481 break;
3482#ifdef FEAT_MBYTE
3483 if (has_mbyte)
3484 len = (*mb_ptr2len)(p);
3485 else
3486#endif
3487 len = 1;
3488 mb_ptr_adv(p);
3489 }
3490 if (in_quote)
3491 bow = p;
3492 else
3493 xp->xp_pattern = p;
3494 p -= len;
3495 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003496 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497 }
3498
3499 /*
3500 * If we are still inside the quotes, and we passed a space, just
3501 * expand from there.
3502 */
3503 if (bow != NULL && in_quote)
3504 xp->xp_pattern = bow;
3505 xp->xp_context = EXPAND_FILES;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003506
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003507 /* For a shell command more chars need to be escaped. */
3508 if (usefilter || ea.cmdidx == CMD_bang)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003509 {
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003510#ifndef BACKSLASH_IN_FILENAME
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003511 xp->xp_shell = TRUE;
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003512#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003513 /* When still after the command name expand executables. */
3514 if (xp->xp_pattern == skipwhite(arg))
Bram Moolenaar5c5b0942007-05-06 12:07:59 +00003515 xp->xp_context = EXPAND_SHELLCMD;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003516 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517
3518 /* Check for environment variable */
3519 if (*xp->xp_pattern == '$'
3520#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
3521 || *xp->xp_pattern == '%'
3522#endif
3523 )
3524 {
3525 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
3526 if (!vim_isIDc(*p))
3527 break;
3528 if (*p == NUL)
3529 {
3530 xp->xp_context = EXPAND_ENV_VARS;
3531 ++xp->xp_pattern;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003532#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3533 /* Avoid that the assignment uses EXPAND_FILES again. */
Bram Moolenaara466c992005-07-09 21:03:22 +00003534 if (compl != EXPAND_USER_DEFINED && compl != EXPAND_USER_LIST)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003535 compl = EXPAND_ENV_VARS;
3536#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537 }
3538 }
Bram Moolenaar24305862012-08-15 14:05:05 +02003539#if defined(FEAT_CMDL_COMPL)
3540 /* Check for user names */
3541 if (*xp->xp_pattern == '~')
3542 {
3543 for (p = xp->xp_pattern + 1; *p != NUL && *p != '/'; ++p)
3544 ;
3545 /* Complete ~user only if it partially matches a user name.
3546 * A full match ~user<Tab> will be replaced by user's home
3547 * directory i.e. something like ~user<Tab> -> /home/user/ */
3548 if (*p == NUL && p > xp->xp_pattern + 1
3549 && match_user(xp->xp_pattern + 1) == 1)
3550 {
3551 xp->xp_context = EXPAND_USER;
3552 ++xp->xp_pattern;
3553 }
3554 }
3555#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556 }
3557
3558/*
3559 * 6. switch on command name
3560 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003561 switch (ea.cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003563 case CMD_find:
3564 case CMD_sfind:
3565 case CMD_tabfind:
Bram Moolenaarc24b6972010-08-16 22:34:29 +02003566 if (xp->xp_context == EXPAND_FILES)
3567 xp->xp_context = EXPAND_FILES_IN_PATH;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003568 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569 case CMD_cd:
3570 case CMD_chdir:
3571 case CMD_lcd:
3572 case CMD_lchdir:
3573 if (xp->xp_context == EXPAND_FILES)
3574 xp->xp_context = EXPAND_DIRECTORIES;
3575 break;
3576 case CMD_help:
3577 xp->xp_context = EXPAND_HELP;
3578 xp->xp_pattern = arg;
3579 break;
3580
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003581 /* Command modifiers: return the argument.
3582 * Also for commands with an argument that is a command. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583 case CMD_aboveleft:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003584 case CMD_argdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585 case CMD_belowright:
3586 case CMD_botright:
3587 case CMD_browse:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003588 case CMD_bufdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589 case CMD_confirm:
Bram Moolenaardf177f62005-02-22 08:39:57 +00003590 case CMD_debug:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591 case CMD_folddoclosed:
3592 case CMD_folddoopen:
3593 case CMD_hide:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003594 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003595 case CMD_keepjumps:
3596 case CMD_keepmarks:
3597 case CMD_leftabove:
3598 case CMD_lockmarks:
3599 case CMD_rightbelow:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003600 case CMD_sandbox:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003601 case CMD_silent:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003602 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603 case CMD_topleft:
3604 case CMD_verbose:
3605 case CMD_vertical:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003606 case CMD_windo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607 return arg;
3608
Bram Moolenaar4f688582007-07-24 12:34:30 +00003609#ifdef FEAT_CMDL_COMPL
3610# ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611 case CMD_match:
3612 if (*arg == NUL || !ends_excmd(*arg))
3613 {
Bram Moolenaar4f688582007-07-24 12:34:30 +00003614 /* also complete "None" */
3615 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616 arg = skipwhite(skiptowhite(arg));
3617 if (*arg != NUL)
3618 {
3619 xp->xp_context = EXPAND_NOTHING;
3620 arg = skip_regexp(arg + 1, *arg, p_magic, NULL);
3621 }
3622 }
3623 return find_nextcmd(arg);
Bram Moolenaar4f688582007-07-24 12:34:30 +00003624# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003625
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626/*
3627 * All completion for the +cmdline_compl feature goes here.
3628 */
3629
3630# ifdef FEAT_USR_CMDS
3631 case CMD_command:
3632 /* Check for attributes */
3633 while (*arg == '-')
3634 {
3635 arg++; /* Skip "-" */
3636 p = skiptowhite(arg);
3637 if (*p == NUL)
3638 {
3639 /* Cursor is still in the attribute */
3640 p = vim_strchr(arg, '=');
3641 if (p == NULL)
3642 {
3643 /* No "=", so complete attribute names */
3644 xp->xp_context = EXPAND_USER_CMD_FLAGS;
3645 xp->xp_pattern = arg;
3646 return NULL;
3647 }
3648
3649 /* For the -complete and -nargs attributes, we complete
3650 * their arguments as well.
3651 */
3652 if (STRNICMP(arg, "complete", p - arg) == 0)
3653 {
3654 xp->xp_context = EXPAND_USER_COMPLETE;
3655 xp->xp_pattern = p + 1;
3656 return NULL;
3657 }
3658 else if (STRNICMP(arg, "nargs", p - arg) == 0)
3659 {
3660 xp->xp_context = EXPAND_USER_NARGS;
3661 xp->xp_pattern = p + 1;
3662 return NULL;
3663 }
3664 return NULL;
3665 }
3666 arg = skipwhite(p);
3667 }
3668
3669 /* After the attributes comes the new command name */
3670 p = skiptowhite(arg);
3671 if (*p == NUL)
3672 {
3673 xp->xp_context = EXPAND_USER_COMMANDS;
3674 xp->xp_pattern = arg;
3675 break;
3676 }
3677
3678 /* And finally comes a normal command */
3679 return skipwhite(p);
3680
3681 case CMD_delcommand:
3682 xp->xp_context = EXPAND_USER_COMMANDS;
3683 xp->xp_pattern = arg;
3684 break;
3685# endif
3686
3687 case CMD_global:
3688 case CMD_vglobal:
3689 delim = *arg; /* get the delimiter */
3690 if (delim)
3691 ++arg; /* skip delimiter if there is one */
3692
3693 while (arg[0] != NUL && arg[0] != delim)
3694 {
3695 if (arg[0] == '\\' && arg[1] != NUL)
3696 ++arg;
3697 ++arg;
3698 }
3699 if (arg[0] != NUL)
3700 return arg + 1;
3701 break;
3702 case CMD_and:
3703 case CMD_substitute:
3704 delim = *arg;
3705 if (delim)
3706 {
3707 /* skip "from" part */
3708 ++arg;
3709 arg = skip_regexp(arg, delim, p_magic, NULL);
3710 }
3711 /* skip "to" part */
3712 while (arg[0] != NUL && arg[0] != delim)
3713 {
3714 if (arg[0] == '\\' && arg[1] != NUL)
3715 ++arg;
3716 ++arg;
3717 }
3718 if (arg[0] != NUL) /* skip delimiter */
3719 ++arg;
3720 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
3721 ++arg;
3722 if (arg[0] != NUL)
3723 return arg;
3724 break;
3725 case CMD_isearch:
3726 case CMD_dsearch:
3727 case CMD_ilist:
3728 case CMD_dlist:
3729 case CMD_ijump:
3730 case CMD_psearch:
3731 case CMD_djump:
3732 case CMD_isplit:
3733 case CMD_dsplit:
3734 arg = skipwhite(skipdigits(arg)); /* skip count */
3735 if (*arg == '/') /* Match regexp, not just whole words */
3736 {
3737 for (++arg; *arg && *arg != '/'; arg++)
3738 if (*arg == '\\' && arg[1] != NUL)
3739 arg++;
3740 if (*arg)
3741 {
3742 arg = skipwhite(arg + 1);
3743
3744 /* Check for trailing illegal characters */
3745 if (*arg && vim_strchr((char_u *)"|\"\n", *arg) == NULL)
3746 xp->xp_context = EXPAND_NOTHING;
3747 else
3748 return arg;
3749 }
3750 }
3751 break;
3752#ifdef FEAT_AUTOCMD
3753 case CMD_autocmd:
3754 return set_context_in_autocmd(xp, arg, FALSE);
3755
3756 case CMD_doautocmd:
Bram Moolenaar73a9d7b2008-11-06 16:16:44 +00003757 case CMD_doautoall:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003758 return set_context_in_autocmd(xp, arg, TRUE);
3759#endif
3760 case CMD_set:
3761 set_context_in_set_cmd(xp, arg, 0);
3762 break;
3763 case CMD_setglobal:
3764 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
3765 break;
3766 case CMD_setlocal:
3767 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
3768 break;
3769 case CMD_tag:
3770 case CMD_stag:
3771 case CMD_ptag:
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00003772 case CMD_ltag:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003773 case CMD_tselect:
3774 case CMD_stselect:
3775 case CMD_ptselect:
3776 case CMD_tjump:
3777 case CMD_stjump:
3778 case CMD_ptjump:
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003779 if (*p_wop != NUL)
3780 xp->xp_context = EXPAND_TAGS_LISTFILES;
3781 else
3782 xp->xp_context = EXPAND_TAGS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003783 xp->xp_pattern = arg;
3784 break;
3785 case CMD_augroup:
3786 xp->xp_context = EXPAND_AUGROUP;
3787 xp->xp_pattern = arg;
3788 break;
3789#ifdef FEAT_SYN_HL
3790 case CMD_syntax:
3791 set_context_in_syntax_cmd(xp, arg);
3792 break;
3793#endif
3794#ifdef FEAT_EVAL
3795 case CMD_let:
3796 case CMD_if:
3797 case CMD_elseif:
3798 case CMD_while:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00003799 case CMD_for:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003800 case CMD_echo:
3801 case CMD_echon:
3802 case CMD_execute:
3803 case CMD_echomsg:
3804 case CMD_echoerr:
3805 case CMD_call:
3806 case CMD_return:
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003807 set_context_for_expression(xp, arg, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003808 break;
3809
3810 case CMD_unlet:
3811 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3812 arg = xp->xp_pattern + 1;
3813 xp->xp_context = EXPAND_USER_VARS;
3814 xp->xp_pattern = arg;
3815 break;
3816
3817 case CMD_function:
3818 case CMD_delfunction:
3819 xp->xp_context = EXPAND_USER_FUNC;
3820 xp->xp_pattern = arg;
3821 break;
3822
3823 case CMD_echohl:
Bram Moolenaar4f688582007-07-24 12:34:30 +00003824 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825 break;
3826#endif
3827 case CMD_highlight:
3828 set_context_in_highlight_cmd(xp, arg);
3829 break;
Bram Moolenaarf4580d82009-03-18 11:52:53 +00003830#ifdef FEAT_CSCOPE
3831 case CMD_cscope:
Bram Moolenaar7bfef802009-04-22 14:25:01 +00003832 case CMD_lcscope:
3833 case CMD_scscope:
3834 set_context_in_cscope_cmd(xp, arg, ea.cmdidx);
Bram Moolenaarf4580d82009-03-18 11:52:53 +00003835 break;
3836#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00003837#ifdef FEAT_SIGNS
3838 case CMD_sign:
3839 set_context_in_sign_cmd(xp, arg);
3840 break;
3841#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003842#ifdef FEAT_LISTCMDS
3843 case CMD_bdelete:
3844 case CMD_bwipeout:
3845 case CMD_bunload:
3846 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3847 arg = xp->xp_pattern + 1;
3848 /*FALLTHROUGH*/
3849 case CMD_buffer:
3850 case CMD_sbuffer:
3851 case CMD_checktime:
3852 xp->xp_context = EXPAND_BUFFERS;
3853 xp->xp_pattern = arg;
3854 break;
3855#endif
3856#ifdef FEAT_USR_CMDS
3857 case CMD_USER:
3858 case CMD_USER_BUF:
3859 if (compl != EXPAND_NOTHING)
3860 {
3861 /* XFILE: file names are handled above */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003862 if (!(ea.argt & XFILE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003863 {
3864# ifdef FEAT_MENU
3865 if (compl == EXPAND_MENUS)
3866 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
3867# endif
3868 if (compl == EXPAND_COMMANDS)
3869 return arg;
3870 if (compl == EXPAND_MAPPINGS)
3871 return set_context_in_map_cmd(xp, (char_u *)"map",
3872 arg, forceit, FALSE, FALSE, CMD_map);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003873 /* Find start of last argument. */
3874 p = arg;
3875 while (*p)
3876 {
3877 if (*p == ' ')
Bram Moolenaar848f8762012-07-25 17:22:23 +02003878 /* argument starts after a space */
3879 arg = p + 1;
Bram Moolenaara07c8312012-07-27 21:12:07 +02003880 else if (*p == '\\' && *(p + 1) != NUL)
3881 ++p; /* skip over escaped character */
3882 mb_ptr_adv(p);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003883 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884 xp->xp_pattern = arg;
3885 }
3886 xp->xp_context = compl;
3887 }
3888 break;
3889#endif
3890 case CMD_map: case CMD_noremap:
3891 case CMD_nmap: case CMD_nnoremap:
3892 case CMD_vmap: case CMD_vnoremap:
3893 case CMD_omap: case CMD_onoremap:
3894 case CMD_imap: case CMD_inoremap:
3895 case CMD_cmap: case CMD_cnoremap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003896 case CMD_lmap: case CMD_lnoremap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02003897 case CMD_smap: case CMD_snoremap:
3898 case CMD_xmap: case CMD_xnoremap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003899 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003900 FALSE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003901 case CMD_unmap:
3902 case CMD_nunmap:
3903 case CMD_vunmap:
3904 case CMD_ounmap:
3905 case CMD_iunmap:
3906 case CMD_cunmap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003907 case CMD_lunmap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02003908 case CMD_sunmap:
3909 case CMD_xunmap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003910 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003911 FALSE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003912 case CMD_abbreviate: case CMD_noreabbrev:
3913 case CMD_cabbrev: case CMD_cnoreabbrev:
3914 case CMD_iabbrev: case CMD_inoreabbrev:
3915 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003916 TRUE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003917 case CMD_unabbreviate:
3918 case CMD_cunabbrev:
3919 case CMD_iunabbrev:
3920 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003921 TRUE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003922#ifdef FEAT_MENU
3923 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
3924 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
3925 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
3926 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
3927 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
3928 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
3929 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
3930 case CMD_tmenu: case CMD_tunmenu:
3931 case CMD_popup: case CMD_tearoff: case CMD_emenu:
3932 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
3933#endif
3934
3935 case CMD_colorscheme:
3936 xp->xp_context = EXPAND_COLORS;
3937 xp->xp_pattern = arg;
3938 break;
3939
3940 case CMD_compiler:
3941 xp->xp_context = EXPAND_COMPILER;
3942 xp->xp_pattern = arg;
3943 break;
3944
Bram Moolenaar883f5d02010-06-21 06:24:34 +02003945 case CMD_ownsyntax:
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02003946 xp->xp_context = EXPAND_OWNSYNTAX;
3947 xp->xp_pattern = arg;
3948 break;
3949
3950 case CMD_setfiletype:
Bram Moolenaar883f5d02010-06-21 06:24:34 +02003951 xp->xp_context = EXPAND_FILETYPE;
3952 xp->xp_pattern = arg;
3953 break;
3954
Bram Moolenaar071d4272004-06-13 20:20:40 +00003955#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
3956 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
3957 case CMD_language:
Bram Moolenaara660dc82011-05-25 12:51:22 +02003958 p = skiptowhite(arg);
3959 if (*p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003960 {
3961 xp->xp_context = EXPAND_LANGUAGE;
3962 xp->xp_pattern = arg;
3963 }
3964 else
Bram Moolenaara660dc82011-05-25 12:51:22 +02003965 {
3966 if ( STRNCMP(arg, "messages", p - arg) == 0
3967 || STRNCMP(arg, "ctype", p - arg) == 0
3968 || STRNCMP(arg, "time", p - arg) == 0)
3969 {
3970 xp->xp_context = EXPAND_LOCALES;
3971 xp->xp_pattern = skipwhite(p);
3972 }
3973 else
3974 xp->xp_context = EXPAND_NOTHING;
3975 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003976 break;
3977#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01003978#if defined(FEAT_PROFILE)
3979 case CMD_profile:
3980 set_context_in_profile_cmd(xp, arg);
3981 break;
3982#endif
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01003983 case CMD_behave:
3984 xp->xp_context = EXPAND_BEHAVE;
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02003985 xp->xp_pattern = arg;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01003986 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003987
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02003988#if defined(FEAT_CMDHIST)
3989 case CMD_history:
3990 xp->xp_context = EXPAND_HISTORY;
3991 xp->xp_pattern = arg;
3992 break;
3993#endif
3994
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995#endif /* FEAT_CMDL_COMPL */
3996
3997 default:
3998 break;
3999 }
4000 return NULL;
4001}
4002
4003/*
4004 * skip a range specifier of the form: addr [,addr] [;addr] ..
4005 *
4006 * Backslashed delimiters after / or ? will be skipped, and commands will
4007 * not be expanded between /'s and ?'s or after "'".
4008 *
Bram Moolenaardf177f62005-02-22 08:39:57 +00004009 * Also skip white space and ":" characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004010 * Returns the "cmd" pointer advanced to beyond the range.
4011 */
4012 char_u *
4013skip_range(cmd, ctx)
4014 char_u *cmd;
4015 int *ctx; /* pointer to xp_context or NULL */
4016{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004017 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004018
Bram Moolenaardf177f62005-02-22 08:39:57 +00004019 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004020 {
4021 if (*cmd == '\'')
4022 {
4023 if (*++cmd == NUL && ctx != NULL)
4024 *ctx = EXPAND_NOTHING;
4025 }
4026 else if (*cmd == '/' || *cmd == '?')
4027 {
4028 delim = *cmd++;
4029 while (*cmd != NUL && *cmd != delim)
4030 if (*cmd++ == '\\' && *cmd != NUL)
4031 ++cmd;
4032 if (*cmd == NUL && ctx != NULL)
4033 *ctx = EXPAND_NOTHING;
4034 }
4035 if (*cmd != NUL)
4036 ++cmd;
4037 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004038
4039 /* Skip ":" and white space. */
4040 while (*cmd == ':')
4041 cmd = skipwhite(cmd + 1);
4042
Bram Moolenaar071d4272004-06-13 20:20:40 +00004043 return cmd;
4044}
4045
4046/*
4047 * get a single EX address
4048 *
4049 * Set ptr to the next character after the part that was interpreted.
4050 * Set ptr to NULL when an error is encountered.
4051 *
4052 * Return MAXLNUM when no Ex address was found.
4053 */
4054 static linenr_T
4055get_address(ptr, skip, to_other_file)
4056 char_u **ptr;
4057 int skip; /* only skip the address, don't use it */
4058 int to_other_file; /* flag: may jump to other file */
4059{
4060 int c;
4061 int i;
4062 long n;
4063 char_u *cmd;
4064 pos_T pos;
4065 pos_T *fp;
4066 linenr_T lnum;
4067
4068 cmd = skipwhite(*ptr);
4069 lnum = MAXLNUM;
4070 do
4071 {
4072 switch (*cmd)
4073 {
4074 case '.': /* '.' - Cursor position */
4075 ++cmd;
4076 lnum = curwin->w_cursor.lnum;
4077 break;
4078
4079 case '$': /* '$' - last line */
4080 ++cmd;
4081 lnum = curbuf->b_ml.ml_line_count;
4082 break;
4083
4084 case '\'': /* ''' - mark */
4085 if (*++cmd == NUL)
4086 {
4087 cmd = NULL;
4088 goto error;
4089 }
4090 if (skip)
4091 ++cmd;
4092 else
4093 {
4094 /* Only accept a mark in another file when it is
4095 * used by itself: ":'M". */
4096 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
4097 ++cmd;
4098 if (fp == (pos_T *)-1)
4099 /* Jumped to another file. */
4100 lnum = curwin->w_cursor.lnum;
4101 else
4102 {
4103 if (check_mark(fp) == FAIL)
4104 {
4105 cmd = NULL;
4106 goto error;
4107 }
4108 lnum = fp->lnum;
4109 }
4110 }
4111 break;
4112
4113 case '/':
4114 case '?': /* '/' or '?' - search */
4115 c = *cmd++;
4116 if (skip) /* skip "/pat/" */
4117 {
4118 cmd = skip_regexp(cmd, c, (int)p_magic, NULL);
4119 if (*cmd == c)
4120 ++cmd;
4121 }
4122 else
4123 {
4124 pos = curwin->w_cursor; /* save curwin->w_cursor */
4125 /*
4126 * When '/' or '?' follows another address, start
4127 * from there.
4128 */
4129 if (lnum != MAXLNUM)
4130 curwin->w_cursor.lnum = lnum;
4131 /*
4132 * Start a forward search at the end of the line.
4133 * Start a backward search at the start of the line.
4134 * This makes sure we never match in the current
4135 * line, and can match anywhere in the
4136 * next/previous line.
4137 */
4138 if (c == '/')
4139 curwin->w_cursor.col = MAXCOL;
4140 else
4141 curwin->w_cursor.col = 0;
4142 searchcmdlen = 0;
4143 if (!do_search(NULL, c, cmd, 1L,
Bram Moolenaaraad86642008-03-10 20:34:59 +00004144 SEARCH_HIS | SEARCH_MSG, NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145 {
4146 curwin->w_cursor = pos;
4147 cmd = NULL;
4148 goto error;
4149 }
4150 lnum = curwin->w_cursor.lnum;
4151 curwin->w_cursor = pos;
4152 /* adjust command string pointer */
4153 cmd += searchcmdlen;
4154 }
4155 break;
4156
4157 case '\\': /* "\?", "\/" or "\&", repeat search */
4158 ++cmd;
4159 if (*cmd == '&')
4160 i = RE_SUBST;
4161 else if (*cmd == '?' || *cmd == '/')
4162 i = RE_SEARCH;
4163 else
4164 {
4165 EMSG(_(e_backslash));
4166 cmd = NULL;
4167 goto error;
4168 }
4169
4170 if (!skip)
4171 {
4172 /*
4173 * When search follows another address, start from
4174 * there.
4175 */
4176 if (lnum != MAXLNUM)
4177 pos.lnum = lnum;
4178 else
4179 pos.lnum = curwin->w_cursor.lnum;
4180
4181 /*
4182 * Start the search just like for the above
4183 * do_search().
4184 */
4185 if (*cmd != '?')
4186 pos.col = MAXCOL;
4187 else
4188 pos.col = 0;
4189 if (searchit(curwin, curbuf, &pos,
4190 *cmd == '?' ? BACKWARD : FORWARD,
Bram Moolenaaraad86642008-03-10 20:34:59 +00004191 (char_u *)"", 1L, SEARCH_MSG,
Bram Moolenaar76929292008-01-06 19:07:36 +00004192 i, (linenr_T)0, NULL) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193 lnum = pos.lnum;
4194 else
4195 {
4196 cmd = NULL;
4197 goto error;
4198 }
4199 }
4200 ++cmd;
4201 break;
4202
4203 default:
4204 if (VIM_ISDIGIT(*cmd)) /* absolute line number */
4205 lnum = getdigits(&cmd);
4206 }
4207
4208 for (;;)
4209 {
4210 cmd = skipwhite(cmd);
4211 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4212 break;
4213
4214 if (lnum == MAXLNUM)
4215 lnum = curwin->w_cursor.lnum; /* "+1" is same as ".+1" */
4216 if (VIM_ISDIGIT(*cmd))
4217 i = '+'; /* "number" is same as "+number" */
4218 else
4219 i = *cmd++;
4220 if (!VIM_ISDIGIT(*cmd)) /* '+' is '+1', but '+0' is not '+1' */
4221 n = 1;
4222 else
4223 n = getdigits(&cmd);
4224 if (i == '-')
4225 lnum -= n;
4226 else
4227 lnum += n;
4228 }
4229 } while (*cmd == '/' || *cmd == '?');
4230
4231error:
4232 *ptr = cmd;
4233 return lnum;
4234}
4235
4236/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004237 * Get flags from an Ex command argument.
4238 */
4239 static void
4240get_flags(eap)
4241 exarg_T *eap;
4242{
4243 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4244 {
4245 if (*eap->arg == 'l')
4246 eap->flags |= EXFLAG_LIST;
4247 else if (*eap->arg == 'p')
4248 eap->flags |= EXFLAG_PRINT;
4249 else
4250 eap->flags |= EXFLAG_NR;
4251 eap->arg = skipwhite(eap->arg + 1);
4252 }
4253}
4254
4255/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256 * Function called for command which is Not Implemented. NI!
4257 */
4258 void
4259ex_ni(eap)
4260 exarg_T *eap;
4261{
4262 if (!eap->skip)
4263 eap->errmsg = (char_u *)N_("E319: Sorry, the command is not available in this version");
4264}
4265
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004266#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004267/*
4268 * Function called for script command which is Not Implemented. NI!
4269 * Skips over ":perl <<EOF" constructs.
4270 */
4271 static void
4272ex_script_ni(eap)
4273 exarg_T *eap;
4274{
4275 if (!eap->skip)
4276 ex_ni(eap);
4277 else
4278 vim_free(script_get(eap, eap->arg));
4279}
4280#endif
4281
4282/*
4283 * Check range in Ex command for validity.
4284 * Return NULL when valid, error message when invalid.
4285 */
4286 static char_u *
4287invalid_range(eap)
4288 exarg_T *eap;
4289{
4290 if ( eap->line1 < 0
4291 || eap->line2 < 0
4292 || eap->line1 > eap->line2
4293 || ((eap->argt & RANGE)
4294 && !(eap->argt & NOTADR)
4295 && eap->line2 > curbuf->b_ml.ml_line_count
4296#ifdef FEAT_DIFF
4297 + (eap->cmdidx == CMD_diffget)
4298#endif
4299 ))
4300 return (char_u *)_(e_invrange);
4301 return NULL;
4302}
4303
4304/*
4305 * Correct the range for zero line number, if required.
4306 */
4307 static void
4308correct_range(eap)
4309 exarg_T *eap;
4310{
4311 if (!(eap->argt & ZEROR)) /* zero in range not allowed */
4312 {
4313 if (eap->line1 == 0)
4314 eap->line1 = 1;
4315 if (eap->line2 == 0)
4316 eap->line2 = 1;
4317 }
4318}
4319
Bram Moolenaar748bf032005-02-02 23:04:36 +00004320#ifdef FEAT_QUICKFIX
4321static char_u *skip_grep_pat __ARGS((exarg_T *eap));
4322
4323/*
4324 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4325 * pattern. Otherwise return eap->arg.
4326 */
4327 static char_u *
4328skip_grep_pat(eap)
4329 exarg_T *eap;
4330{
4331 char_u *p = eap->arg;
4332
Bram Moolenaara37420f2006-02-04 22:37:47 +00004333 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4334 || eap->cmdidx == CMD_vimgrepadd
4335 || eap->cmdidx == CMD_lvimgrepadd
4336 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004337 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004338 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004339 if (p == NULL)
4340 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004341 }
4342 return p;
4343}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004344
4345/*
4346 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4347 * in the command line, so that things like % get expanded.
4348 */
4349 static char_u *
4350replace_makeprg(eap, p, cmdlinep)
4351 exarg_T *eap;
4352 char_u *p;
4353 char_u **cmdlinep;
4354{
4355 char_u *new_cmdline;
4356 char_u *program;
4357 char_u *pos;
4358 char_u *ptr;
4359 int len;
4360 int i;
4361
4362 /*
4363 * Don't do it when ":vimgrep" is used for ":grep".
4364 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004365 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4366 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4367 || eap->cmdidx == CMD_grepadd
4368 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004369 && !grep_internal(eap->cmdidx))
4370 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004371 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4372 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004373 {
4374 if (*curbuf->b_p_gp == NUL)
4375 program = p_gp;
4376 else
4377 program = curbuf->b_p_gp;
4378 }
4379 else
4380 {
4381 if (*curbuf->b_p_mp == NUL)
4382 program = p_mp;
4383 else
4384 program = curbuf->b_p_mp;
4385 }
4386
4387 p = skipwhite(p);
4388
4389 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4390 {
4391 /* replace $* by given arguments */
4392 i = 1;
4393 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4394 ++i;
4395 len = (int)STRLEN(p);
4396 new_cmdline = alloc((int)(STRLEN(program) + i * (len - 2) + 1));
4397 if (new_cmdline == NULL)
4398 return NULL; /* out of memory */
4399 ptr = new_cmdline;
4400 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4401 {
4402 i = (int)(pos - program);
4403 STRNCPY(ptr, program, i);
4404 STRCPY(ptr += i, p);
4405 ptr += len;
4406 program = pos + 2;
4407 }
4408 STRCPY(ptr, program);
4409 }
4410 else
4411 {
4412 new_cmdline = alloc((int)(STRLEN(program) + STRLEN(p) + 2));
4413 if (new_cmdline == NULL)
4414 return NULL; /* out of memory */
4415 STRCPY(new_cmdline, program);
4416 STRCAT(new_cmdline, " ");
4417 STRCAT(new_cmdline, p);
4418 }
4419 msg_make(p);
4420
4421 /* 'eap->cmd' is not set here, because it is not used at CMD_make */
4422 vim_free(*cmdlinep);
4423 *cmdlinep = new_cmdline;
4424 p = new_cmdline;
4425 }
4426 return p;
4427}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004428#endif
4429
Bram Moolenaar071d4272004-06-13 20:20:40 +00004430/*
4431 * Expand file name in Ex command argument.
4432 * Return FAIL for failure, OK otherwise.
4433 */
4434 int
4435expand_filename(eap, cmdlinep, errormsgp)
4436 exarg_T *eap;
4437 char_u **cmdlinep;
4438 char_u **errormsgp;
4439{
4440 int has_wildcards; /* need to expand wildcards */
4441 char_u *repl;
4442 int srclen;
4443 char_u *p;
4444 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004445 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004446
Bram Moolenaar748bf032005-02-02 23:04:36 +00004447#ifdef FEAT_QUICKFIX
4448 /* Skip a regexp pattern for ":vimgrep[add] pat file..." */
4449 p = skip_grep_pat(eap);
4450#else
4451 p = eap->arg;
4452#endif
4453
Bram Moolenaar071d4272004-06-13 20:20:40 +00004454 /*
4455 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4456 * the file name contains a wildcard it should not cause expanding.
4457 * (it will be expanded anyway if there is a wildcard before replacing).
4458 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004459 has_wildcards = mch_has_wildcard(p);
4460 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004461 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004462#ifdef FEAT_EVAL
4463 /* Skip over `=expr`, wildcards in it are not expanded. */
4464 if (p[0] == '`' && p[1] == '=')
4465 {
4466 p += 2;
4467 (void)skip_expr(&p);
4468 if (*p == '`')
4469 ++p;
4470 continue;
4471 }
4472#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004473 /*
4474 * Quick check if this cannot be the start of a special string.
4475 * Also removes backslash before '%', '#' and '<'.
4476 */
4477 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4478 {
4479 ++p;
4480 continue;
4481 }
4482
4483 /*
4484 * Try to find a match at this position.
4485 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004486 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
4487 errormsgp, &escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004488 if (*errormsgp != NULL) /* error detected */
4489 return FAIL;
4490 if (repl == NULL) /* no match found */
4491 {
4492 p += srclen;
4493 continue;
4494 }
4495
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004496 /* Wildcards won't be expanded below, the replacement is taken
4497 * literally. But do expand "~/file", "~user/file" and "$HOME/file". */
4498 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4499 {
4500 char_u *l = repl;
4501
4502 repl = expand_env_save(repl);
4503 vim_free(l);
4504 }
4505
Bram Moolenaar63b92542007-03-27 14:57:09 +00004506 /* Need to escape white space et al. with a backslash.
4507 * Don't do this for:
4508 * - replacement that already has been escaped: "##"
4509 * - shell commands (may have to use quotes instead).
4510 * - non-unix systems when there is a single argument (spaces don't
4511 * separate arguments then).
4512 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004513 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00004514 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00004515 && eap->cmdidx != CMD_bang
4516 && eap->cmdidx != CMD_make
Bram Moolenaara37420f2006-02-04 22:37:47 +00004517 && eap->cmdidx != CMD_lmake
Bram Moolenaar071d4272004-06-13 20:20:40 +00004518 && eap->cmdidx != CMD_grep
Bram Moolenaara37420f2006-02-04 22:37:47 +00004519 && eap->cmdidx != CMD_lgrep
Bram Moolenaar071d4272004-06-13 20:20:40 +00004520 && eap->cmdidx != CMD_grepadd
Bram Moolenaara37420f2006-02-04 22:37:47 +00004521 && eap->cmdidx != CMD_lgrepadd
Bram Moolenaar071d4272004-06-13 20:20:40 +00004522#ifndef UNIX
4523 && !(eap->argt & NOSPC)
4524#endif
4525 )
4526 {
4527 char_u *l;
4528#ifdef BACKSLASH_IN_FILENAME
4529 /* Don't escape a backslash here, because rem_backslash() doesn't
4530 * remove it later. */
4531 static char_u *nobslash = (char_u *)" \t\"|";
4532# define ESCAPE_CHARS nobslash
4533#else
4534# define ESCAPE_CHARS escape_chars
4535#endif
4536
4537 for (l = repl; *l; ++l)
4538 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
4539 {
4540 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
4541 if (l != NULL)
4542 {
4543 vim_free(repl);
4544 repl = l;
4545 }
4546 break;
4547 }
4548 }
4549
4550 /* For a shell command a '!' must be escaped. */
4551 if ((eap->usefilter || eap->cmdidx == CMD_bang)
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004552 && vim_strpbrk(repl, (char_u *)"!&;()<>") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004553 {
4554 char_u *l;
4555
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004556 l = vim_strsave_escaped(repl, (char_u *)"!&;()<>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557 if (l != NULL)
4558 {
4559 vim_free(repl);
4560 repl = l;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004561 /* For a sh-like shell escape "!" another time. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004562 if (strstr((char *)p_sh, "sh") != NULL)
4563 {
4564 l = vim_strsave_escaped(repl, (char_u *)"!");
4565 if (l != NULL)
4566 {
4567 vim_free(repl);
4568 repl = l;
4569 }
4570 }
4571 }
4572 }
4573
4574 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
4575 vim_free(repl);
4576 if (p == NULL)
4577 return FAIL;
4578 }
4579
4580 /*
4581 * One file argument: Expand wildcards.
4582 * Don't do this with ":r !command" or ":w !command".
4583 */
4584 if ((eap->argt & NOSPC) && !eap->usefilter)
4585 {
4586 /*
4587 * May do this twice:
4588 * 1. Replace environment variables.
4589 * 2. Replace any other wildcards, remove backslashes.
4590 */
4591 for (n = 1; n <= 2; ++n)
4592 {
4593 if (n == 2)
4594 {
4595#ifdef UNIX
4596 /*
4597 * Only for Unix we check for more than one file name.
4598 * For other systems spaces are considered to be part
4599 * of the file name.
4600 * Only check here if there is no wildcard, otherwise
4601 * ExpandOne() will check for errors. This allows
4602 * ":e `ls ve*.c`" on Unix.
4603 */
4604 if (!has_wildcards)
4605 for (p = eap->arg; *p; ++p)
4606 {
4607 /* skip escaped characters */
4608 if (p[1] && (*p == '\\' || *p == Ctrl_V))
4609 ++p;
4610 else if (vim_iswhite(*p))
4611 {
4612 *errormsgp = (char_u *)_("E172: Only one file name allowed");
4613 return FAIL;
4614 }
4615 }
4616#endif
4617
4618 /*
4619 * Halve the number of backslashes (this is Vi compatible).
4620 * For Unix and OS/2, when wildcards are expanded, this is
4621 * done by ExpandOne() below.
4622 */
4623#if defined(UNIX) || defined(OS2)
4624 if (!has_wildcards)
4625#endif
4626 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004627 }
4628
4629 if (has_wildcards)
4630 {
4631 if (n == 1)
4632 {
4633 /*
4634 * First loop: May expand environment variables. This
4635 * can be done much faster with expand_env() than with
4636 * something else (e.g., calling a shell).
4637 * After expanding environment variables, check again
4638 * if there are still wildcards present.
4639 */
4640 if (vim_strchr(eap->arg, '$') != NULL
4641 || vim_strchr(eap->arg, '~') != NULL)
4642 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00004643 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004644 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004645 has_wildcards = mch_has_wildcard(NameBuff);
4646 p = NameBuff;
4647 }
4648 else
4649 p = NULL;
4650 }
4651 else /* n == 2 */
4652 {
4653 expand_T xpc;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004654 int options = WILD_LIST_NOTFOUND|WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004655
4656 ExpandInit(&xpc);
4657 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004658 if (p_wic)
4659 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004660 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01004661 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004662 if (p == NULL)
4663 return FAIL;
4664 }
4665 if (p != NULL)
4666 {
4667 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
4668 p, cmdlinep);
4669 if (n == 2) /* p came from ExpandOne() */
4670 vim_free(p);
4671 }
4672 }
4673 }
4674 }
4675 return OK;
4676}
4677
4678/*
4679 * Replace part of the command line, keeping eap->cmd, eap->arg and
4680 * eap->nextcmd correct.
4681 * "src" points to the part that is to be replaced, of length "srclen".
4682 * "repl" is the replacement string.
4683 * Returns a pointer to the character after the replaced string.
4684 * Returns NULL for failure.
4685 */
4686 static char_u *
4687repl_cmdline(eap, src, srclen, repl, cmdlinep)
4688 exarg_T *eap;
4689 char_u *src;
4690 int srclen;
4691 char_u *repl;
4692 char_u **cmdlinep;
4693{
4694 int len;
4695 int i;
4696 char_u *new_cmdline;
4697
4698 /*
4699 * The new command line is build in new_cmdline[].
4700 * First allocate it.
4701 * Careful: a "+cmd" argument may have been NUL terminated.
4702 */
4703 len = (int)STRLEN(repl);
4704 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004705 if (eap->nextcmd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004706 i += (int)STRLEN(eap->nextcmd);/* add space for next command */
4707 if ((new_cmdline = alloc((unsigned)i)) == NULL)
4708 return NULL; /* out of memory! */
4709
4710 /*
4711 * Copy the stuff before the expanded part.
4712 * Copy the expanded stuff.
4713 * Copy what came after the expanded part.
4714 * Copy the next commands, if there are any.
4715 */
4716 i = (int)(src - *cmdlinep); /* length of part before match */
4717 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00004718
Bram Moolenaar071d4272004-06-13 20:20:40 +00004719 mch_memmove(new_cmdline + i, repl, (size_t)len);
4720 i += len; /* remember the end of the string */
4721 STRCPY(new_cmdline + i, src + srclen);
4722 src = new_cmdline + i; /* remember where to continue */
4723
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004724 if (eap->nextcmd != NULL) /* append next command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004725 {
4726 i = (int)STRLEN(new_cmdline) + 1;
4727 STRCPY(new_cmdline + i, eap->nextcmd);
4728 eap->nextcmd = new_cmdline + i;
4729 }
4730 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
4731 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
4732 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
4733 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
4734 vim_free(*cmdlinep);
4735 *cmdlinep = new_cmdline;
4736
4737 return src;
4738}
4739
4740/*
4741 * Check for '|' to separate commands and '"' to start comments.
4742 */
4743 void
4744separate_nextcmd(eap)
4745 exarg_T *eap;
4746{
4747 char_u *p;
4748
Bram Moolenaar86b68352004-12-27 21:59:20 +00004749#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004750 p = skip_grep_pat(eap);
4751#else
4752 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004753#endif
4754
4755 for ( ; *p; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004756 {
4757 if (*p == Ctrl_V)
4758 {
4759 if (eap->argt & (USECTRLV | XFILE))
4760 ++p; /* skip CTRL-V and next char */
4761 else
Bram Moolenaarb0db5692007-08-14 20:54:49 +00004762 /* remove CTRL-V and skip next char */
Bram Moolenaara7241f52008-06-24 20:39:31 +00004763 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004764 if (*p == NUL) /* stop at NUL after CTRL-V */
4765 break;
4766 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004767
4768#ifdef FEAT_EVAL
4769 /* Skip over `=expr` when wildcards are expanded. */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004770 else if (p[0] == '`' && p[1] == '=' && (eap->argt & XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004771 {
4772 p += 2;
4773 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004774 }
4775#endif
4776
Bram Moolenaar071d4272004-06-13 20:20:40 +00004777 /* Check for '"': start of comment or '|': next command */
4778 /* :@" and :*" do not start a comment!
4779 * :redir @" doesn't either. */
4780 else if ((*p == '"' && !(eap->argt & NOTRLCOM)
4781 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
4782 || p != eap->arg)
4783 && (eap->cmdidx != CMD_redir
4784 || p != eap->arg + 1 || p[-1] != '@'))
4785 || *p == '|' || *p == '\n')
4786 {
4787 /*
4788 * We remove the '\' before the '|', unless USECTRLV is used
4789 * AND 'b' is present in 'cpoptions'.
4790 */
4791 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
4792 || !(eap->argt & USECTRLV)) && *(p - 1) == '\\')
4793 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00004794 STRMOVE(p - 1, p); /* remove the '\' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004795 --p;
4796 }
4797 else
4798 {
4799 eap->nextcmd = check_nextcmd(p);
4800 *p = NUL;
4801 break;
4802 }
4803 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004804 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004805
Bram Moolenaar071d4272004-06-13 20:20:40 +00004806 if (!(eap->argt & NOTRLCOM)) /* remove trailing spaces */
4807 del_trailing_spaces(eap->arg);
4808}
4809
4810/*
4811 * get + command from ex argument
4812 */
4813 static char_u *
4814getargcmd(argp)
4815 char_u **argp;
4816{
4817 char_u *arg = *argp;
4818 char_u *command = NULL;
4819
4820 if (*arg == '+') /* +[command] */
4821 {
4822 ++arg;
4823 if (vim_isspace(*arg))
4824 command = dollar_command;
4825 else
4826 {
4827 command = arg;
4828 arg = skip_cmd_arg(command, TRUE);
4829 if (*arg != NUL)
4830 *arg++ = NUL; /* terminate command with NUL */
4831 }
4832
4833 arg = skipwhite(arg); /* skip over spaces */
4834 *argp = arg;
4835 }
4836 return command;
4837}
4838
4839/*
4840 * Find end of "+command" argument. Skip over "\ " and "\\".
4841 */
4842 static char_u *
4843skip_cmd_arg(p, rembs)
4844 char_u *p;
4845 int rembs; /* TRUE to halve the number of backslashes */
4846{
4847 while (*p && !vim_isspace(*p))
4848 {
4849 if (*p == '\\' && p[1] != NUL)
4850 {
4851 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00004852 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853 else
4854 ++p;
4855 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004856 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004857 }
4858 return p;
4859}
4860
4861/*
4862 * Get "++opt=arg" argument.
4863 * Return FAIL or OK.
4864 */
4865 static int
4866getargopt(eap)
4867 exarg_T *eap;
4868{
4869 char_u *arg = eap->arg + 2;
4870 int *pp = NULL;
4871#ifdef FEAT_MBYTE
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004872 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004873 char_u *p;
4874#endif
4875
4876 /* ":edit ++[no]bin[ary] file" */
4877 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
4878 {
4879 if (*arg == 'n')
4880 {
4881 arg += 2;
4882 eap->force_bin = FORCE_NOBIN;
4883 }
4884 else
4885 eap->force_bin = FORCE_BIN;
4886 if (!checkforcmd(&arg, "binary", 3))
4887 return FAIL;
4888 eap->arg = skipwhite(arg);
4889 return OK;
4890 }
4891
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004892 /* ":read ++edit file" */
4893 if (STRNCMP(arg, "edit", 4) == 0)
4894 {
4895 eap->read_edit = TRUE;
4896 eap->arg = skipwhite(arg + 4);
4897 return OK;
4898 }
4899
Bram Moolenaar071d4272004-06-13 20:20:40 +00004900 if (STRNCMP(arg, "ff", 2) == 0)
4901 {
4902 arg += 2;
4903 pp = &eap->force_ff;
4904 }
4905 else if (STRNCMP(arg, "fileformat", 10) == 0)
4906 {
4907 arg += 10;
4908 pp = &eap->force_ff;
4909 }
4910#ifdef FEAT_MBYTE
4911 else if (STRNCMP(arg, "enc", 3) == 0)
4912 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01004913 if (STRNCMP(arg, "encoding", 8) == 0)
4914 arg += 8;
4915 else
4916 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917 pp = &eap->force_enc;
4918 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004919 else if (STRNCMP(arg, "bad", 3) == 0)
4920 {
4921 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004922 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004923 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004924#endif
4925
4926 if (pp == NULL || *arg != '=')
4927 return FAIL;
4928
4929 ++arg;
4930 *pp = (int)(arg - eap->cmd);
4931 arg = skip_cmd_arg(arg, FALSE);
4932 eap->arg = skipwhite(arg);
4933 *arg = NUL;
4934
4935#ifdef FEAT_MBYTE
4936 if (pp == &eap->force_ff)
4937 {
4938#endif
4939 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
4940 return FAIL;
4941#ifdef FEAT_MBYTE
4942 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004943 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944 {
4945 /* Make 'fileencoding' lower case. */
4946 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
4947 *p = TOLOWER_ASC(*p);
4948 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004949 else
4950 {
4951 /* Check ++bad= argument. Must be a single-byte character, "keep" or
4952 * "drop". */
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004953 p = eap->cmd + bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004954 if (STRICMP(p, "keep") == 0)
4955 eap->bad_char = BAD_KEEP;
4956 else if (STRICMP(p, "drop") == 0)
4957 eap->bad_char = BAD_DROP;
4958 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
4959 eap->bad_char = *p;
4960 else
4961 return FAIL;
4962 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004963#endif
4964
4965 return OK;
4966}
4967
4968/*
4969 * ":abbreviate" and friends.
4970 */
4971 static void
4972ex_abbreviate(eap)
4973 exarg_T *eap;
4974{
4975 do_exmap(eap, TRUE); /* almost the same as mapping */
4976}
4977
4978/*
4979 * ":map" and friends.
4980 */
4981 static void
4982ex_map(eap)
4983 exarg_T *eap;
4984{
4985 /*
4986 * If we are sourcing .exrc or .vimrc in current directory we
4987 * print the mappings for security reasons.
4988 */
4989 if (secure)
4990 {
4991 secure = 2;
4992 msg_outtrans(eap->cmd);
4993 msg_putchar('\n');
4994 }
4995 do_exmap(eap, FALSE);
4996}
4997
4998/*
4999 * ":unmap" and friends.
5000 */
5001 static void
5002ex_unmap(eap)
5003 exarg_T *eap;
5004{
5005 do_exmap(eap, FALSE);
5006}
5007
5008/*
5009 * ":mapclear" and friends.
5010 */
5011 static void
5012ex_mapclear(eap)
5013 exarg_T *eap;
5014{
5015 map_clear(eap->cmd, eap->arg, eap->forceit, FALSE);
5016}
5017
5018/*
5019 * ":abclear" and friends.
5020 */
5021 static void
5022ex_abclear(eap)
5023 exarg_T *eap;
5024{
5025 map_clear(eap->cmd, eap->arg, TRUE, TRUE);
5026}
5027
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005028#if defined(FEAT_AUTOCMD) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005029 static void
5030ex_autocmd(eap)
5031 exarg_T *eap;
5032{
5033 /*
5034 * Disallow auto commands from .exrc and .vimrc in current
5035 * directory for security reasons.
5036 */
5037 if (secure)
5038 {
5039 secure = 2;
5040 eap->errmsg = e_curdir;
5041 }
5042 else if (eap->cmdidx == CMD_autocmd)
5043 do_autocmd(eap->arg, eap->forceit);
5044 else
5045 do_augroup(eap->arg, eap->forceit);
5046}
5047
5048/*
5049 * ":doautocmd": Apply the automatic commands to the current buffer.
5050 */
5051 static void
5052ex_doautocmd(eap)
5053 exarg_T *eap;
5054{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005055 char_u *arg = eap->arg;
5056 int call_do_modelines = check_nomodeline(&arg);
5057
5058 (void)do_doautocmd(arg, TRUE);
5059 if (call_do_modelines) /* Only when there is no <nomodeline>. */
5060 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005061}
5062#endif
5063
5064#ifdef FEAT_LISTCMDS
5065/*
5066 * :[N]bunload[!] [N] [bufname] unload buffer
5067 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
5068 * :[N]bwipeout[!] [N] [bufname] delete buffer really
5069 */
5070 static void
5071ex_bunload(eap)
5072 exarg_T *eap;
5073{
5074 eap->errmsg = do_bufdel(
5075 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
5076 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
5077 : DOBUF_UNLOAD, eap->arg,
5078 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
5079}
5080
5081/*
5082 * :[N]buffer [N] to buffer N
5083 * :[N]sbuffer [N] to buffer N
5084 */
5085 static void
5086ex_buffer(eap)
5087 exarg_T *eap;
5088{
5089 if (*eap->arg)
5090 eap->errmsg = e_trailing;
5091 else
5092 {
5093 if (eap->addr_count == 0) /* default is current buffer */
5094 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
5095 else
5096 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
5097 }
5098}
5099
5100/*
5101 * :[N]bmodified [N] to next mod. buffer
5102 * :[N]sbmodified [N] to next mod. buffer
5103 */
5104 static void
5105ex_bmodified(eap)
5106 exarg_T *eap;
5107{
5108 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
5109}
5110
5111/*
5112 * :[N]bnext [N] to next buffer
5113 * :[N]sbnext [N] split and to next buffer
5114 */
5115 static void
5116ex_bnext(eap)
5117 exarg_T *eap;
5118{
5119 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
5120}
5121
5122/*
5123 * :[N]bNext [N] to previous buffer
5124 * :[N]bprevious [N] to previous buffer
5125 * :[N]sbNext [N] split and to previous buffer
5126 * :[N]sbprevious [N] split and to previous buffer
5127 */
5128 static void
5129ex_bprevious(eap)
5130 exarg_T *eap;
5131{
5132 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
5133}
5134
5135/*
5136 * :brewind to first buffer
5137 * :bfirst to first buffer
5138 * :sbrewind split and to first buffer
5139 * :sbfirst split and to first buffer
5140 */
5141 static void
5142ex_brewind(eap)
5143 exarg_T *eap;
5144{
5145 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
5146}
5147
5148/*
5149 * :blast to last buffer
5150 * :sblast split and to last buffer
5151 */
5152 static void
5153ex_blast(eap)
5154 exarg_T *eap;
5155{
5156 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
5157}
5158#endif
5159
5160 int
5161ends_excmd(c)
5162 int c;
5163{
5164 return (c == NUL || c == '|' || c == '"' || c == '\n');
5165}
5166
5167#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5168 || defined(PROTO)
5169/*
5170 * Return the next command, after the first '|' or '\n'.
5171 * Return NULL if not found.
5172 */
5173 char_u *
5174find_nextcmd(p)
5175 char_u *p;
5176{
5177 while (*p != '|' && *p != '\n')
5178 {
5179 if (*p == NUL)
5180 return NULL;
5181 ++p;
5182 }
5183 return (p + 1);
5184}
5185#endif
5186
5187/*
5188 * Check if *p is a separator between Ex commands.
5189 * Return NULL if it isn't, (p + 1) if it is.
5190 */
5191 char_u *
5192check_nextcmd(p)
5193 char_u *p;
5194{
5195 p = skipwhite(p);
5196 if (*p == '|' || *p == '\n')
5197 return (p + 1);
5198 else
5199 return NULL;
5200}
5201
5202/*
5203 * - if there are more files to edit
5204 * - and this is the last window
5205 * - and forceit not used
5206 * - and not repeated twice on a row
5207 * return FAIL and give error message if 'message' TRUE
5208 * return OK otherwise
5209 */
5210 static int
5211check_more(message, forceit)
5212 int message; /* when FALSE check only, no messages */
5213 int forceit;
5214{
5215 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5216
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005217 if (!forceit && only_one_window()
5218 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005219 {
5220 if (message)
5221 {
5222#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
5223 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
5224 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005225 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005226
5227 if (n == 1)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02005228 vim_strncpy(buff,
5229 (char_u *)_("1 more file to edit. Quit anyway?"),
Bram Moolenaard9462e32011-04-11 21:35:11 +02005230 DIALOG_MSG_SIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005231 else
Bram Moolenaard9462e32011-04-11 21:35:11 +02005232 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005233 _("%d more files to edit. Quit anyway?"), n);
5234 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5235 return OK;
5236 return FAIL;
5237 }
5238#endif
5239 if (n == 1)
5240 EMSG(_("E173: 1 more file to edit"));
5241 else
5242 EMSGN(_("E173: %ld more files to edit"), n);
5243 quitmore = 2; /* next try to quit is allowed */
5244 }
5245 return FAIL;
5246 }
5247 return OK;
5248}
5249
5250#ifdef FEAT_CMDL_COMPL
5251/*
5252 * Function given to ExpandGeneric() to obtain the list of command names.
5253 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005254 char_u *
5255get_command_name(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00005256 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005257 int idx;
5258{
5259 if (idx >= (int)CMD_SIZE)
5260# ifdef FEAT_USR_CMDS
5261 return get_user_command_name(idx);
5262# else
5263 return NULL;
5264# endif
5265 return cmdnames[idx].cmd_name;
5266}
5267#endif
5268
5269#if defined(FEAT_USR_CMDS) || defined(PROTO)
5270static 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));
5271static void uc_list __ARGS((char_u *name, size_t name_len));
5272static int uc_scan_attr __ARGS((char_u *attr, size_t len, long *argt, long *def, int *flags, int *compl, char_u **compl_arg));
5273static char_u *uc_split_args __ARGS((char_u *arg, size_t *lenp));
5274static 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));
5275
5276 static int
5277uc_add_command(name, name_len, rep, argt, def, flags, compl, compl_arg, force)
5278 char_u *name;
5279 size_t name_len;
5280 char_u *rep;
5281 long argt;
5282 long def;
5283 int flags;
5284 int compl;
5285 char_u *compl_arg;
5286 int force;
5287{
5288 ucmd_T *cmd = NULL;
5289 char_u *p;
5290 int i;
5291 int cmp = 1;
5292 char_u *rep_buf = NULL;
5293 garray_T *gap;
5294
Bram Moolenaar9c102382006-05-03 21:26:49 +00005295 replace_termcodes(rep, &rep_buf, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005296 if (rep_buf == NULL)
5297 {
5298 /* Can't replace termcodes - try using the string as is */
5299 rep_buf = vim_strsave(rep);
5300
5301 /* Give up if out of memory */
5302 if (rep_buf == NULL)
5303 return FAIL;
5304 }
5305
5306 /* get address of growarray: global or in curbuf */
5307 if (flags & UC_BUFFER)
5308 {
5309 gap = &curbuf->b_ucmds;
5310 if (gap->ga_itemsize == 0)
5311 ga_init2(gap, (int)sizeof(ucmd_T), 4);
5312 }
5313 else
5314 gap = &ucmds;
5315
5316 /* Search for the command in the already defined commands. */
5317 for (i = 0; i < gap->ga_len; ++i)
5318 {
5319 size_t len;
5320
5321 cmd = USER_CMD_GA(gap, i);
5322 len = STRLEN(cmd->uc_name);
5323 cmp = STRNCMP(name, cmd->uc_name, name_len);
5324 if (cmp == 0)
5325 {
5326 if (name_len < len)
5327 cmp = -1;
5328 else if (name_len > len)
5329 cmp = 1;
5330 }
5331
5332 if (cmp == 0)
5333 {
5334 if (!force)
5335 {
5336 EMSG(_("E174: Command already exists: add ! to replace it"));
5337 goto fail;
5338 }
5339
5340 vim_free(cmd->uc_rep);
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00005341 cmd->uc_rep = NULL;
5342#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5343 vim_free(cmd->uc_compl_arg);
5344 cmd->uc_compl_arg = NULL;
5345#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005346 break;
5347 }
5348
5349 /* Stop as soon as we pass the name to add */
5350 if (cmp < 0)
5351 break;
5352 }
5353
5354 /* Extend the array unless we're replacing an existing command */
5355 if (cmp != 0)
5356 {
5357 if (ga_grow(gap, 1) != OK)
5358 goto fail;
5359 if ((p = vim_strnsave(name, (int)name_len)) == NULL)
5360 goto fail;
5361
5362 cmd = USER_CMD_GA(gap, i);
5363 mch_memmove(cmd + 1, cmd, (gap->ga_len - i) * sizeof(ucmd_T));
5364
5365 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005366
5367 cmd->uc_name = p;
5368 }
5369
5370 cmd->uc_rep = rep_buf;
5371 cmd->uc_argt = argt;
5372 cmd->uc_def = def;
5373 cmd->uc_compl = compl;
5374#ifdef FEAT_EVAL
5375 cmd->uc_scriptID = current_SID;
5376# ifdef FEAT_CMDL_COMPL
5377 cmd->uc_compl_arg = compl_arg;
5378# endif
5379#endif
5380
5381 return OK;
5382
5383fail:
5384 vim_free(rep_buf);
5385#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5386 vim_free(compl_arg);
5387#endif
5388 return FAIL;
5389}
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005390#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005391
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005392#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005393/*
5394 * List of names for completion for ":command" with the EXPAND_ flag.
5395 * Must be alphabetical for completion.
5396 */
5397static struct
5398{
5399 int expand;
5400 char *name;
5401} command_complete[] =
5402{
5403 {EXPAND_AUGROUP, "augroup"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005404 {EXPAND_BEHAVE, "behave"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005405 {EXPAND_BUFFERS, "buffer"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005406 {EXPAND_COLORS, "color"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005407 {EXPAND_COMMANDS, "command"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005408 {EXPAND_COMPILER, "compiler"},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005409#if defined(FEAT_CSCOPE)
5410 {EXPAND_CSCOPE, "cscope"},
5411#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005412#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5413 {EXPAND_USER_DEFINED, "custom"},
Bram Moolenaara466c992005-07-09 21:03:22 +00005414 {EXPAND_USER_LIST, "customlist"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005415#endif
5416 {EXPAND_DIRECTORIES, "dir"},
5417 {EXPAND_ENV_VARS, "environment"},
5418 {EXPAND_EVENTS, "event"},
5419 {EXPAND_EXPRESSION, "expression"},
5420 {EXPAND_FILES, "file"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005421 {EXPAND_FILES_IN_PATH, "file_in_path"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005422 {EXPAND_FILETYPE, "filetype"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005423 {EXPAND_FUNCTIONS, "function"},
5424 {EXPAND_HELP, "help"},
5425 {EXPAND_HIGHLIGHT, "highlight"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005426#if defined(FEAT_CMDHIST)
5427 {EXPAND_HISTORY, "history"},
5428#endif
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005429#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005430 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005431 {EXPAND_LOCALES, "locale"},
5432#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005433 {EXPAND_MAPPINGS, "mapping"},
5434 {EXPAND_MENUS, "menu"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005435 {EXPAND_OWNSYNTAX, "syntax"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005436 {EXPAND_SETTINGS, "option"},
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005437 {EXPAND_SHELLCMD, "shellcmd"},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005438#if defined(FEAT_SIGNS)
5439 {EXPAND_SIGN, "sign"},
5440#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005441 {EXPAND_TAGS, "tag"},
5442 {EXPAND_TAGS_LISTFILES, "tag_listfiles"},
Bram Moolenaar24305862012-08-15 14:05:05 +02005443 {EXPAND_USER, "user"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005444 {EXPAND_USER_VARS, "var"},
5445 {0, NULL}
5446};
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005447#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005448
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005449#if defined(FEAT_USR_CMDS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005450 static void
5451uc_list(name, name_len)
5452 char_u *name;
5453 size_t name_len;
5454{
5455 int i, j;
5456 int found = FALSE;
5457 ucmd_T *cmd;
5458 int len;
5459 long a;
5460 garray_T *gap;
5461
5462 gap = &curbuf->b_ucmds;
5463 for (;;)
5464 {
5465 for (i = 0; i < gap->ga_len; ++i)
5466 {
5467 cmd = USER_CMD_GA(gap, i);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005468 a = (long)cmd->uc_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005469
5470 /* Skip commands which don't match the requested prefix */
5471 if (STRNCMP(name, cmd->uc_name, name_len) != 0)
5472 continue;
5473
5474 /* Put out the title first time */
5475 if (!found)
5476 MSG_PUTS_TITLE(_("\n Name Args Range Complete Definition"));
5477 found = TRUE;
5478 msg_putchar('\n');
5479 if (got_int)
5480 break;
5481
5482 /* Special cases */
5483 msg_putchar(a & BANG ? '!' : ' ');
5484 msg_putchar(a & REGSTR ? '"' : ' ');
5485 msg_putchar(gap != &ucmds ? 'b' : ' ');
5486 msg_putchar(' ');
5487
5488 msg_outtrans_attr(cmd->uc_name, hl_attr(HLF_D));
5489 len = (int)STRLEN(cmd->uc_name) + 4;
5490
5491 do {
5492 msg_putchar(' ');
5493 ++len;
5494 } while (len < 16);
5495
5496 len = 0;
5497
5498 /* Arguments */
5499 switch ((int)(a & (EXTRA|NOSPC|NEEDARG)))
5500 {
5501 case 0: IObuff[len++] = '0'; break;
5502 case (EXTRA): IObuff[len++] = '*'; break;
5503 case (EXTRA|NOSPC): IObuff[len++] = '?'; break;
5504 case (EXTRA|NEEDARG): IObuff[len++] = '+'; break;
5505 case (EXTRA|NOSPC|NEEDARG): IObuff[len++] = '1'; break;
5506 }
5507
5508 do {
5509 IObuff[len++] = ' ';
5510 } while (len < 5);
5511
5512 /* Range */
5513 if (a & (RANGE|COUNT))
5514 {
5515 if (a & COUNT)
5516 {
5517 /* -count=N */
5518 sprintf((char *)IObuff + len, "%ldc", cmd->uc_def);
5519 len += (int)STRLEN(IObuff + len);
5520 }
5521 else if (a & DFLALL)
5522 IObuff[len++] = '%';
5523 else if (cmd->uc_def >= 0)
5524 {
5525 /* -range=N */
5526 sprintf((char *)IObuff + len, "%ld", cmd->uc_def);
5527 len += (int)STRLEN(IObuff + len);
5528 }
5529 else
5530 IObuff[len++] = '.';
5531 }
5532
5533 do {
5534 IObuff[len++] = ' ';
5535 } while (len < 11);
5536
5537 /* Completion */
5538 for (j = 0; command_complete[j].expand != 0; ++j)
5539 if (command_complete[j].expand == cmd->uc_compl)
5540 {
5541 STRCPY(IObuff + len, command_complete[j].name);
5542 len += (int)STRLEN(IObuff + len);
5543 break;
5544 }
5545
5546 do {
5547 IObuff[len++] = ' ';
5548 } while (len < 21);
5549
5550 IObuff[len] = '\0';
5551 msg_outtrans(IObuff);
5552
5553 msg_outtrans_special(cmd->uc_rep, FALSE);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005554#ifdef FEAT_EVAL
5555 if (p_verbose > 0)
5556 last_set_msg(cmd->uc_scriptID);
5557#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005558 out_flush();
5559 ui_breakcheck();
5560 if (got_int)
5561 break;
5562 }
5563 if (gap == &ucmds || i < gap->ga_len)
5564 break;
5565 gap = &ucmds;
5566 }
5567
5568 if (!found)
5569 MSG(_("No user-defined commands found"));
5570}
5571
5572 static char_u *
5573uc_fun_cmd()
5574{
5575 static char_u fcmd[] = {0x84, 0xaf, 0x60, 0xb9, 0xaf, 0xb5, 0x60, 0xa4,
5576 0xa5, 0xad, 0xa1, 0xae, 0xa4, 0x60, 0xa1, 0x60,
5577 0xb3, 0xa8, 0xb2, 0xb5, 0xa2, 0xa2, 0xa5, 0xb2,
5578 0xb9, 0x7f, 0};
5579 int i;
5580
5581 for (i = 0; fcmd[i]; ++i)
5582 IObuff[i] = fcmd[i] - 0x40;
5583 IObuff[i] = 0;
5584 return IObuff;
5585}
5586
5587 static int
5588uc_scan_attr(attr, len, argt, def, flags, compl, compl_arg)
5589 char_u *attr;
5590 size_t len;
5591 long *argt;
5592 long *def;
5593 int *flags;
5594 int *compl;
5595 char_u **compl_arg;
5596{
5597 char_u *p;
5598
5599 if (len == 0)
5600 {
5601 EMSG(_("E175: No attribute specified"));
5602 return FAIL;
5603 }
5604
5605 /* First, try the simple attributes (no arguments) */
5606 if (STRNICMP(attr, "bang", len) == 0)
5607 *argt |= BANG;
5608 else if (STRNICMP(attr, "buffer", len) == 0)
5609 *flags |= UC_BUFFER;
5610 else if (STRNICMP(attr, "register", len) == 0)
5611 *argt |= REGSTR;
5612 else if (STRNICMP(attr, "bar", len) == 0)
5613 *argt |= TRLBAR;
5614 else
5615 {
5616 int i;
5617 char_u *val = NULL;
5618 size_t vallen = 0;
5619 size_t attrlen = len;
5620
5621 /* Look for the attribute name - which is the part before any '=' */
5622 for (i = 0; i < (int)len; ++i)
5623 {
5624 if (attr[i] == '=')
5625 {
5626 val = &attr[i + 1];
5627 vallen = len - i - 1;
5628 attrlen = i;
5629 break;
5630 }
5631 }
5632
5633 if (STRNICMP(attr, "nargs", attrlen) == 0)
5634 {
5635 if (vallen == 1)
5636 {
5637 if (*val == '0')
5638 /* Do nothing - this is the default */;
5639 else if (*val == '1')
5640 *argt |= (EXTRA | NOSPC | NEEDARG);
5641 else if (*val == '*')
5642 *argt |= EXTRA;
5643 else if (*val == '?')
5644 *argt |= (EXTRA | NOSPC);
5645 else if (*val == '+')
5646 *argt |= (EXTRA | NEEDARG);
5647 else
5648 goto wrong_nargs;
5649 }
5650 else
5651 {
5652wrong_nargs:
5653 EMSG(_("E176: Invalid number of arguments"));
5654 return FAIL;
5655 }
5656 }
5657 else if (STRNICMP(attr, "range", attrlen) == 0)
5658 {
5659 *argt |= RANGE;
5660 if (vallen == 1 && *val == '%')
5661 *argt |= DFLALL;
5662 else if (val != NULL)
5663 {
5664 p = val;
5665 if (*def >= 0)
5666 {
5667two_count:
5668 EMSG(_("E177: Count cannot be specified twice"));
5669 return FAIL;
5670 }
5671
5672 *def = getdigits(&p);
5673 *argt |= (ZEROR | NOTADR);
5674
5675 if (p != val + vallen || vallen == 0)
5676 {
5677invalid_count:
5678 EMSG(_("E178: Invalid default value for count"));
5679 return FAIL;
5680 }
5681 }
5682 }
5683 else if (STRNICMP(attr, "count", attrlen) == 0)
5684 {
Bram Moolenaar32e7b2d2005-02-27 22:36:47 +00005685 *argt |= (COUNT | ZEROR | RANGE | NOTADR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005686
5687 if (val != NULL)
5688 {
5689 p = val;
5690 if (*def >= 0)
5691 goto two_count;
5692
5693 *def = getdigits(&p);
5694
5695 if (p != val + vallen)
5696 goto invalid_count;
5697 }
5698
5699 if (*def < 0)
5700 *def = 0;
5701 }
5702 else if (STRNICMP(attr, "complete", attrlen) == 0)
5703 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005704 if (val == NULL)
5705 {
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005706 EMSG(_("E179: argument required for -complete"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005707 return FAIL;
5708 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005709
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005710 if (parse_compl_arg(val, (int)vallen, compl, argt, compl_arg)
5711 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005712 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005713 }
5714 else
5715 {
5716 char_u ch = attr[len];
5717 attr[len] = '\0';
5718 EMSG2(_("E181: Invalid attribute: %s"), attr);
5719 attr[len] = ch;
5720 return FAIL;
5721 }
5722 }
5723
5724 return OK;
5725}
5726
Bram Moolenaara850a712009-01-28 14:42:59 +00005727/*
5728 * ":command ..."
5729 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005730 static void
5731ex_command(eap)
5732 exarg_T *eap;
5733{
5734 char_u *name;
5735 char_u *end;
5736 char_u *p;
5737 long argt = 0;
5738 long def = -1;
5739 int flags = 0;
5740 int compl = EXPAND_NOTHING;
5741 char_u *compl_arg = NULL;
5742 int has_attr = (eap->arg[0] == '-');
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01005743 int name_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005744
5745 p = eap->arg;
5746
5747 /* Check for attributes */
5748 while (*p == '-')
5749 {
5750 ++p;
5751 end = skiptowhite(p);
5752 if (uc_scan_attr(p, end - p, &argt, &def, &flags, &compl, &compl_arg)
5753 == FAIL)
5754 return;
5755 p = skipwhite(end);
5756 }
5757
5758 /* Get the name (if any) and skip to the following argument */
5759 name = p;
5760 if (ASCII_ISALPHA(*p))
5761 while (ASCII_ISALNUM(*p))
5762 ++p;
5763 if (!ends_excmd(*p) && !vim_iswhite(*p))
5764 {
5765 EMSG(_("E182: Invalid command name"));
5766 return;
5767 }
5768 end = p;
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01005769 name_len = (int)(end - name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005770
5771 /* If there is nothing after the name, and no attributes were specified,
5772 * we are listing commands
5773 */
5774 p = skipwhite(end);
5775 if (!has_attr && ends_excmd(*p))
5776 {
5777 uc_list(name, end - name);
5778 }
5779 else if (!ASCII_ISUPPER(*name))
5780 {
5781 EMSG(_("E183: User defined commands must start with an uppercase letter"));
5782 return;
5783 }
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01005784 else if ((name_len == 1 && *name == 'X')
5785 || (name_len <= 4
5786 && STRNCMP(name, "Next", name_len > 4 ? 4 : name_len) == 0))
5787 {
5788 EMSG(_("E841: Reserved name, cannot be used for user defined command"));
5789 return;
5790 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005791 else
5792 uc_add_command(name, end - name, p, argt, def, flags, compl, compl_arg,
5793 eap->forceit);
5794}
5795
5796/*
5797 * ":comclear"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005798 * Clear all user commands, global and for current buffer.
5799 */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00005800 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005801ex_comclear(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00005802 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005803{
5804 uc_clear(&ucmds);
5805 uc_clear(&curbuf->b_ucmds);
5806}
5807
5808/*
5809 * Clear all user commands for "gap".
5810 */
5811 void
5812uc_clear(gap)
5813 garray_T *gap;
5814{
5815 int i;
5816 ucmd_T *cmd;
5817
5818 for (i = 0; i < gap->ga_len; ++i)
5819 {
5820 cmd = USER_CMD_GA(gap, i);
5821 vim_free(cmd->uc_name);
5822 vim_free(cmd->uc_rep);
5823# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5824 vim_free(cmd->uc_compl_arg);
5825# endif
5826 }
5827 ga_clear(gap);
5828}
5829
5830 static void
5831ex_delcommand(eap)
5832 exarg_T *eap;
5833{
5834 int i = 0;
5835 ucmd_T *cmd = NULL;
5836 int cmp = -1;
5837 garray_T *gap;
5838
5839 gap = &curbuf->b_ucmds;
5840 for (;;)
5841 {
5842 for (i = 0; i < gap->ga_len; ++i)
5843 {
5844 cmd = USER_CMD_GA(gap, i);
5845 cmp = STRCMP(eap->arg, cmd->uc_name);
5846 if (cmp <= 0)
5847 break;
5848 }
5849 if (gap == &ucmds || cmp == 0)
5850 break;
5851 gap = &ucmds;
5852 }
5853
5854 if (cmp != 0)
5855 {
5856 EMSG2(_("E184: No such user-defined command: %s"), eap->arg);
5857 return;
5858 }
5859
5860 vim_free(cmd->uc_name);
5861 vim_free(cmd->uc_rep);
5862# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5863 vim_free(cmd->uc_compl_arg);
5864# endif
5865
5866 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005867
5868 if (i < gap->ga_len)
5869 mch_memmove(cmd, cmd + 1, (gap->ga_len - i) * sizeof(ucmd_T));
5870}
5871
Bram Moolenaar552f8a12007-03-08 17:12:08 +00005872/*
5873 * split and quote args for <f-args>
5874 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005875 static char_u *
5876uc_split_args(arg, lenp)
5877 char_u *arg;
5878 size_t *lenp;
5879{
5880 char_u *buf;
5881 char_u *p;
5882 char_u *q;
5883 int len;
5884
5885 /* Precalculate length */
5886 p = arg;
5887 len = 2; /* Initial and final quotes */
5888
5889 while (*p)
5890 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00005891 if (p[0] == '\\' && p[1] == '\\')
5892 {
5893 len += 2;
5894 p += 2;
5895 }
5896 else if (p[0] == '\\' && vim_iswhite(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005897 {
5898 len += 1;
5899 p += 2;
5900 }
5901 else if (*p == '\\' || *p == '"')
5902 {
5903 len += 2;
5904 p += 1;
5905 }
5906 else if (vim_iswhite(*p))
5907 {
5908 p = skipwhite(p);
5909 if (*p == NUL)
5910 break;
5911 len += 3; /* "," */
5912 }
5913 else
5914 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02005915#ifdef FEAT_MBYTE
5916 int charlen = (*mb_ptr2len)(p);
5917 len += charlen;
5918 p += charlen;
5919#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005920 ++len;
5921 ++p;
Bram Moolenaardfef1542012-07-10 19:25:10 +02005922#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005923 }
5924 }
5925
5926 buf = alloc(len + 1);
5927 if (buf == NULL)
5928 {
5929 *lenp = 0;
5930 return buf;
5931 }
5932
5933 p = arg;
5934 q = buf;
5935 *q++ = '"';
5936 while (*p)
5937 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00005938 if (p[0] == '\\' && p[1] == '\\')
5939 {
5940 *q++ = '\\';
5941 *q++ = '\\';
5942 p += 2;
5943 }
5944 else if (p[0] == '\\' && vim_iswhite(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005945 {
5946 *q++ = p[1];
5947 p += 2;
5948 }
5949 else if (*p == '\\' || *p == '"')
5950 {
5951 *q++ = '\\';
5952 *q++ = *p++;
5953 }
5954 else if (vim_iswhite(*p))
5955 {
5956 p = skipwhite(p);
5957 if (*p == NUL)
5958 break;
5959 *q++ = '"';
5960 *q++ = ',';
5961 *q++ = '"';
5962 }
5963 else
5964 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02005965 MB_COPY_CHAR(p, q);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005966 }
5967 }
5968 *q++ = '"';
5969 *q = 0;
5970
5971 *lenp = len;
5972 return buf;
5973}
5974
5975/*
5976 * Check for a <> code in a user command.
5977 * "code" points to the '<'. "len" the length of the <> (inclusive).
5978 * "buf" is where the result is to be added.
5979 * "split_buf" points to a buffer used for splitting, caller should free it.
5980 * "split_len" is the length of what "split_buf" contains.
5981 * Returns the length of the replacement, which has been added to "buf".
5982 * Returns -1 if there was no match, and only the "<" has been copied.
5983 */
5984 static size_t
5985uc_check_code(code, len, buf, cmd, eap, split_buf, split_len)
5986 char_u *code;
5987 size_t len;
5988 char_u *buf;
5989 ucmd_T *cmd; /* the user command we're expanding */
5990 exarg_T *eap; /* ex arguments */
5991 char_u **split_buf;
5992 size_t *split_len;
5993{
5994 size_t result = 0;
5995 char_u *p = code + 1;
5996 size_t l = len - 2;
5997 int quote = 0;
5998 enum { ct_ARGS, ct_BANG, ct_COUNT, ct_LINE1, ct_LINE2, ct_REGISTER,
5999 ct_LT, ct_NONE } type = ct_NONE;
6000
6001 if ((vim_strchr((char_u *)"qQfF", *p) != NULL) && p[1] == '-')
6002 {
6003 quote = (*p == 'q' || *p == 'Q') ? 1 : 2;
6004 p += 2;
6005 l -= 2;
6006 }
6007
Bram Moolenaar371d5402006-03-20 21:47:49 +00006008 ++l;
6009 if (l <= 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006010 type = ct_NONE;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006011 else if (STRNICMP(p, "args>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006012 type = ct_ARGS;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006013 else if (STRNICMP(p, "bang>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006014 type = ct_BANG;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006015 else if (STRNICMP(p, "count>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006016 type = ct_COUNT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006017 else if (STRNICMP(p, "line1>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006018 type = ct_LINE1;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006019 else if (STRNICMP(p, "line2>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006020 type = ct_LINE2;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006021 else if (STRNICMP(p, "lt>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006022 type = ct_LT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006023 else if (STRNICMP(p, "reg>", l) == 0 || STRNICMP(p, "register>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006024 type = ct_REGISTER;
6025
6026 switch (type)
6027 {
6028 case ct_ARGS:
6029 /* Simple case first */
6030 if (*eap->arg == NUL)
6031 {
6032 if (quote == 1)
6033 {
6034 result = 2;
6035 if (buf != NULL)
6036 STRCPY(buf, "''");
6037 }
6038 else
6039 result = 0;
6040 break;
6041 }
6042
6043 /* When specified there is a single argument don't split it.
6044 * Works for ":Cmd %" when % is "a b c". */
6045 if ((eap->argt & NOSPC) && quote == 2)
6046 quote = 1;
6047
6048 switch (quote)
6049 {
6050 case 0: /* No quoting, no splitting */
6051 result = STRLEN(eap->arg);
6052 if (buf != NULL)
6053 STRCPY(buf, eap->arg);
6054 break;
6055 case 1: /* Quote, but don't split */
6056 result = STRLEN(eap->arg) + 2;
6057 for (p = eap->arg; *p; ++p)
6058 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006059#ifdef FEAT_MBYTE
6060 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6061 /* DBCS can contain \ in a trail byte, skip the
6062 * double-byte character. */
6063 ++p;
6064 else
6065#endif
6066 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006067 ++result;
6068 }
6069
6070 if (buf != NULL)
6071 {
6072 *buf++ = '"';
6073 for (p = eap->arg; *p; ++p)
6074 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006075#ifdef FEAT_MBYTE
6076 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6077 /* DBCS can contain \ in a trail byte, copy the
6078 * double-byte character to avoid escaping. */
6079 *buf++ = *p++;
6080 else
6081#endif
6082 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006083 *buf++ = '\\';
6084 *buf++ = *p;
6085 }
6086 *buf = '"';
6087 }
6088
6089 break;
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006090 case 2: /* Quote and split (<f-args>) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006091 /* This is hard, so only do it once, and cache the result */
6092 if (*split_buf == NULL)
6093 *split_buf = uc_split_args(eap->arg, split_len);
6094
6095 result = *split_len;
6096 if (buf != NULL && result != 0)
6097 STRCPY(buf, *split_buf);
6098
6099 break;
6100 }
6101 break;
6102
6103 case ct_BANG:
6104 result = eap->forceit ? 1 : 0;
6105 if (quote)
6106 result += 2;
6107 if (buf != NULL)
6108 {
6109 if (quote)
6110 *buf++ = '"';
6111 if (eap->forceit)
6112 *buf++ = '!';
6113 if (quote)
6114 *buf = '"';
6115 }
6116 break;
6117
6118 case ct_LINE1:
6119 case ct_LINE2:
6120 case ct_COUNT:
6121 {
6122 char num_buf[20];
6123 long num = (type == ct_LINE1) ? eap->line1 :
6124 (type == ct_LINE2) ? eap->line2 :
6125 (eap->addr_count > 0) ? eap->line2 : cmd->uc_def;
6126 size_t num_len;
6127
6128 sprintf(num_buf, "%ld", num);
6129 num_len = STRLEN(num_buf);
6130 result = num_len;
6131
6132 if (quote)
6133 result += 2;
6134
6135 if (buf != NULL)
6136 {
6137 if (quote)
6138 *buf++ = '"';
6139 STRCPY(buf, num_buf);
6140 buf += num_len;
6141 if (quote)
6142 *buf = '"';
6143 }
6144
6145 break;
6146 }
6147
6148 case ct_REGISTER:
6149 result = eap->regname ? 1 : 0;
6150 if (quote)
6151 result += 2;
6152 if (buf != NULL)
6153 {
6154 if (quote)
6155 *buf++ = '\'';
6156 if (eap->regname)
6157 *buf++ = eap->regname;
6158 if (quote)
6159 *buf = '\'';
6160 }
6161 break;
6162
6163 case ct_LT:
6164 result = 1;
6165 if (buf != NULL)
6166 *buf = '<';
6167 break;
6168
6169 default:
6170 /* Not recognized: just copy the '<' and return -1. */
6171 result = (size_t)-1;
6172 if (buf != NULL)
6173 *buf = '<';
6174 break;
6175 }
6176
6177 return result;
6178}
6179
6180 static void
6181do_ucmd(eap)
6182 exarg_T *eap;
6183{
6184 char_u *buf;
6185 char_u *p;
6186 char_u *q;
6187
6188 char_u *start;
Bram Moolenaar25648a52009-02-21 19:37:46 +00006189 char_u *end = NULL;
Bram Moolenaara850a712009-01-28 14:42:59 +00006190 char_u *ksp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006191 size_t len, totlen;
6192
6193 size_t split_len = 0;
6194 char_u *split_buf = NULL;
6195 ucmd_T *cmd;
6196#ifdef FEAT_EVAL
6197 scid_T save_current_SID = current_SID;
6198#endif
6199
6200 if (eap->cmdidx == CMD_USER)
6201 cmd = USER_CMD(eap->useridx);
6202 else
6203 cmd = USER_CMD_GA(&curbuf->b_ucmds, eap->useridx);
6204
6205 /*
6206 * Replace <> in the command by the arguments.
Bram Moolenaara850a712009-01-28 14:42:59 +00006207 * First round: "buf" is NULL, compute length, allocate "buf".
6208 * Second round: copy result into "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006209 */
6210 buf = NULL;
6211 for (;;)
6212 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006213 p = cmd->uc_rep; /* source */
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00006214 q = buf; /* destination */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006215 totlen = 0;
Bram Moolenaara850a712009-01-28 14:42:59 +00006216
6217 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006218 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006219 start = vim_strchr(p, '<');
6220 if (start != NULL)
6221 end = vim_strchr(start + 1, '>');
6222 if (buf != NULL)
6223 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006224 for (ksp = p; *ksp != NUL && *ksp != K_SPECIAL; ++ksp)
6225 ;
6226 if (*ksp == K_SPECIAL
6227 && (start == NULL || ksp < start || end == NULL)
Bram Moolenaara850a712009-01-28 14:42:59 +00006228 && ((ksp[1] == KS_SPECIAL && ksp[2] == KE_FILLER)
6229# ifdef FEAT_GUI
6230 || (ksp[1] == KS_EXTRA && ksp[2] == (int)KE_CSI)
6231# endif
6232 ))
6233 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006234 /* K_SPECIAL has been put in the buffer as K_SPECIAL
Bram Moolenaara850a712009-01-28 14:42:59 +00006235 * KS_SPECIAL KE_FILLER, like for mappings, but
6236 * do_cmdline() doesn't handle that, so convert it back.
6237 * Also change K_SPECIAL KS_EXTRA KE_CSI into CSI. */
6238 len = ksp - p;
6239 if (len > 0)
6240 {
6241 mch_memmove(q, p, len);
6242 q += len;
6243 }
6244 *q++ = ksp[1] == KS_SPECIAL ? K_SPECIAL : CSI;
6245 p = ksp + 3;
6246 continue;
6247 }
6248 }
6249
6250 /* break if there no <item> is found */
6251 if (start == NULL || end == NULL)
6252 break;
6253
Bram Moolenaar071d4272004-06-13 20:20:40 +00006254 /* Include the '>' */
6255 ++end;
6256
6257 /* Take everything up to the '<' */
6258 len = start - p;
6259 if (buf == NULL)
6260 totlen += len;
6261 else
6262 {
6263 mch_memmove(q, p, len);
6264 q += len;
6265 }
6266
6267 len = uc_check_code(start, end - start, q, cmd, eap,
6268 &split_buf, &split_len);
6269 if (len == (size_t)-1)
6270 {
6271 /* no match, continue after '<' */
6272 p = start + 1;
6273 len = 1;
6274 }
6275 else
6276 p = end;
6277 if (buf == NULL)
6278 totlen += len;
6279 else
6280 q += len;
6281 }
6282 if (buf != NULL) /* second time here, finished */
6283 {
6284 STRCPY(q, p);
6285 break;
6286 }
6287
6288 totlen += STRLEN(p); /* Add on the trailing characters */
6289 buf = alloc((unsigned)(totlen + 1));
6290 if (buf == NULL)
6291 {
6292 vim_free(split_buf);
6293 return;
6294 }
6295 }
6296
6297#ifdef FEAT_EVAL
6298 current_SID = cmd->uc_scriptID;
6299#endif
6300 (void)do_cmdline(buf, eap->getline, eap->cookie,
6301 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
6302#ifdef FEAT_EVAL
6303 current_SID = save_current_SID;
6304#endif
6305 vim_free(buf);
6306 vim_free(split_buf);
6307}
6308
6309# if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6310 static char_u *
6311get_user_command_name(idx)
6312 int idx;
6313{
6314 return get_user_commands(NULL, idx - (int)CMD_SIZE);
6315}
6316
6317/*
6318 * Function given to ExpandGeneric() to obtain the list of user command names.
6319 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006320 char_u *
6321get_user_commands(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006322 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006323 int idx;
6324{
6325 if (idx < curbuf->b_ucmds.ga_len)
6326 return USER_CMD_GA(&curbuf->b_ucmds, idx)->uc_name;
6327 idx -= curbuf->b_ucmds.ga_len;
6328 if (idx < ucmds.ga_len)
6329 return USER_CMD(idx)->uc_name;
6330 return NULL;
6331}
6332
6333/*
6334 * Function given to ExpandGeneric() to obtain the list of user command
6335 * attributes.
6336 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006337 char_u *
6338get_user_cmd_flags(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006339 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006340 int idx;
6341{
6342 static char *user_cmd_flags[] =
6343 {"bang", "bar", "buffer", "complete", "count",
6344 "nargs", "range", "register"};
6345
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00006346 if (idx >= (int)(sizeof(user_cmd_flags) / sizeof(user_cmd_flags[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006347 return NULL;
6348 return (char_u *)user_cmd_flags[idx];
6349}
6350
6351/*
6352 * Function given to ExpandGeneric() to obtain the list of values for -nargs.
6353 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006354 char_u *
6355get_user_cmd_nargs(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006356 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006357 int idx;
6358{
6359 static char *user_cmd_nargs[] = {"0", "1", "*", "?", "+"};
6360
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00006361 if (idx >= (int)(sizeof(user_cmd_nargs) / sizeof(user_cmd_nargs[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006362 return NULL;
6363 return (char_u *)user_cmd_nargs[idx];
6364}
6365
6366/*
6367 * Function given to ExpandGeneric() to obtain the list of values for -complete.
6368 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006369 char_u *
6370get_user_cmd_complete(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006371 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006372 int idx;
6373{
6374 return (char_u *)command_complete[idx].name;
6375}
6376# endif /* FEAT_CMDL_COMPL */
6377
6378#endif /* FEAT_USR_CMDS */
6379
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006380#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
6381/*
6382 * Parse a completion argument "value[vallen]".
6383 * The detected completion goes in "*complp", argument type in "*argt".
6384 * When there is an argument, for function and user defined completion, it's
6385 * copied to allocated memory and stored in "*compl_arg".
6386 * Returns FAIL if something is wrong.
6387 */
6388 int
6389parse_compl_arg(value, vallen, complp, argt, compl_arg)
6390 char_u *value;
6391 int vallen;
6392 int *complp;
6393 long *argt;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006394 char_u **compl_arg UNUSED;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006395{
6396 char_u *arg = NULL;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006397# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006398 size_t arglen = 0;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006399# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006400 int i;
6401 int valend = vallen;
6402
6403 /* Look for any argument part - which is the part after any ',' */
6404 for (i = 0; i < vallen; ++i)
6405 {
6406 if (value[i] == ',')
6407 {
6408 arg = &value[i + 1];
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006409# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006410 arglen = vallen - i - 1;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006411# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006412 valend = i;
6413 break;
6414 }
6415 }
6416
6417 for (i = 0; command_complete[i].expand != 0; ++i)
6418 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006419 if ((int)STRLEN(command_complete[i].name) == valend
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006420 && STRNCMP(value, command_complete[i].name, valend) == 0)
6421 {
6422 *complp = command_complete[i].expand;
6423 if (command_complete[i].expand == EXPAND_BUFFERS)
6424 *argt |= BUFNAME;
6425 else if (command_complete[i].expand == EXPAND_DIRECTORIES
6426 || command_complete[i].expand == EXPAND_FILES)
6427 *argt |= XFILE;
6428 break;
6429 }
6430 }
6431
6432 if (command_complete[i].expand == 0)
6433 {
6434 EMSG2(_("E180: Invalid complete value: %s"), value);
6435 return FAIL;
6436 }
6437
6438# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6439 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST
6440 && arg != NULL)
6441# else
6442 if (arg != NULL)
6443# endif
6444 {
6445 EMSG(_("E468: Completion argument only allowed for custom completion"));
6446 return FAIL;
6447 }
6448
6449# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6450 if ((*complp == EXPAND_USER_DEFINED || *complp == EXPAND_USER_LIST)
6451 && arg == NULL)
6452 {
6453 EMSG(_("E467: Custom completion requires a function argument"));
6454 return FAIL;
6455 }
6456
6457 if (arg != NULL)
6458 *compl_arg = vim_strnsave(arg, (int)arglen);
6459# endif
6460 return OK;
6461}
6462#endif
6463
Bram Moolenaar071d4272004-06-13 20:20:40 +00006464 static void
6465ex_colorscheme(eap)
6466 exarg_T *eap;
6467{
Bram Moolenaare6850792010-05-14 15:28:44 +02006468 if (*eap->arg == NUL)
6469 {
6470#ifdef FEAT_EVAL
6471 char_u *expr = vim_strsave((char_u *)"g:colors_name");
6472 char_u *p = NULL;
6473
6474 if (expr != NULL)
6475 {
6476 ++emsg_off;
6477 p = eval_to_string(expr, NULL, FALSE);
6478 --emsg_off;
6479 vim_free(expr);
6480 }
6481 if (p != NULL)
6482 {
6483 MSG(p);
6484 vim_free(p);
6485 }
6486 else
6487 MSG("default");
6488#else
6489 MSG(_("unknown"));
6490#endif
6491 }
6492 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarbe1e9e92012-09-18 16:47:07 +02006493 EMSG2(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006494}
6495
6496 static void
6497ex_highlight(eap)
6498 exarg_T *eap;
6499{
6500 if (*eap->arg == NUL && eap->cmd[2] == '!')
6501 MSG(_("Greetings, Vim user!"));
6502 do_highlight(eap->arg, eap->forceit, FALSE);
6503}
6504
6505
6506/*
6507 * Call this function if we thought we were going to exit, but we won't
6508 * (because of an error). May need to restore the terminal mode.
6509 */
6510 void
6511not_exiting()
6512{
6513 exiting = FALSE;
6514 settmode(TMODE_RAW);
6515}
6516
6517/*
6518 * ":quit": quit current window, quit Vim if closed the last window.
6519 */
6520 static void
6521ex_quit(eap)
6522 exarg_T *eap;
6523{
6524#ifdef FEAT_CMDWIN
6525 if (cmdwin_type != 0)
6526 {
6527 cmdwin_result = Ctrl_C;
6528 return;
6529 }
6530#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006531 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006532 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006533 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006534 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006535 return;
6536 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006537#ifdef FEAT_AUTOCMD
Bram Moolenaar3b53dfb2012-06-06 18:03:07 +02006538 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01006539 /* Refuse to quit when locked or when the buffer in the last window is
Bram Moolenaar362ce482012-06-06 19:02:45 +02006540 * being closed (can only happen in autocommands). */
6541 if (curbuf_locked() || (curbuf->b_nwindows == 1 && curbuf->b_closing))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006542 return;
6543#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006544
6545#ifdef FEAT_NETBEANS_INTG
6546 netbeansForcedQuit = eap->forceit;
6547#endif
6548
6549 /*
6550 * If there are more files or windows we won't exit.
6551 */
6552 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6553 exiting = TRUE;
6554 if ((!P_HID(curbuf)
6555 && check_changed(curbuf, p_awa, FALSE, eap->forceit, FALSE))
6556 || check_more(TRUE, eap->forceit) == FAIL
6557 || (only_one_window() && check_changed_any(eap->forceit)))
6558 {
6559 not_exiting();
6560 }
6561 else
6562 {
6563#ifdef FEAT_WINDOWS
6564 if (only_one_window()) /* quit last window */
6565#endif
6566 getout(0);
6567#ifdef FEAT_WINDOWS
6568# ifdef FEAT_GUI
6569 need_mouse_correct = TRUE;
6570# endif
6571 /* close window; may free buffer */
6572 win_close(curwin, !P_HID(curwin->w_buffer) || eap->forceit);
6573#endif
6574 }
6575}
6576
6577/*
6578 * ":cquit".
6579 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006580 static void
6581ex_cquit(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006582 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006583{
6584 getout(1); /* this does not always pass on the exit code to the Manx
6585 compiler. why? */
6586}
6587
6588/*
6589 * ":qall": try to quit all windows
6590 */
6591 static void
6592ex_quit_all(eap)
6593 exarg_T *eap;
6594{
6595# ifdef FEAT_CMDWIN
6596 if (cmdwin_type != 0)
6597 {
6598 if (eap->forceit)
6599 cmdwin_result = K_XF1; /* ex_window() takes care of this */
6600 else
6601 cmdwin_result = K_XF2;
6602 return;
6603 }
6604# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006605
6606 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006607 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006608 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006609 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006610 return;
6611 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006612#ifdef FEAT_AUTOCMD
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01006613 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
6614 /* Refuse to quit when locked or when the buffer in the last window is
6615 * being closed (can only happen in autocommands). */
6616 if (curbuf_locked() || (curbuf->b_nwindows == 1 && curbuf->b_closing))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006617 return;
6618#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006619
Bram Moolenaar071d4272004-06-13 20:20:40 +00006620 exiting = TRUE;
6621 if (eap->forceit || !check_changed_any(FALSE))
6622 getout(0);
6623 not_exiting();
6624}
6625
Bram Moolenaard9967712006-03-11 21:18:15 +00006626#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006627/*
6628 * ":close": close current window, unless it is the last one
6629 */
6630 static void
6631ex_close(eap)
6632 exarg_T *eap;
6633{
6634# ifdef FEAT_CMDWIN
6635 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02006636 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006637 else
6638# endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006639 if (!text_locked()
6640#ifdef FEAT_AUTOCMD
6641 && !curbuf_locked()
6642#endif
6643 )
Bram Moolenaarf740b292006-02-16 22:11:02 +00006644 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006645}
6646
Bram Moolenaard9967712006-03-11 21:18:15 +00006647# ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006648/*
6649 * ":pclose": Close any preview window.
6650 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006651 static void
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006652ex_pclose(eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006653 exarg_T *eap;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006654{
6655 win_T *win;
6656
6657 for (win = firstwin; win != NULL; win = win->w_next)
6658 if (win->w_p_pvw)
6659 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006660 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006661 break;
6662 }
6663}
Bram Moolenaard9967712006-03-11 21:18:15 +00006664# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006665
Bram Moolenaarf740b292006-02-16 22:11:02 +00006666/*
6667 * Close window "win" and take care of handling closing the last window for a
6668 * modified buffer.
6669 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006670 static void
Bram Moolenaarf740b292006-02-16 22:11:02 +00006671ex_win_close(forceit, win, tp)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006672 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006673 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006674 tabpage_T *tp; /* NULL or the tab page "win" is in */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006675{
6676 int need_hide;
6677 buf_T *buf = win->w_buffer;
6678
6679 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006680 if (need_hide && !P_HID(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006681 {
Bram Moolenaard9967712006-03-11 21:18:15 +00006682# if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006683 if ((p_confirm || cmdmod.confirm) && p_write)
6684 {
6685 dialog_changed(buf, FALSE);
6686 if (buf_valid(buf) && bufIsChanged(buf))
6687 return;
6688 need_hide = FALSE;
6689 }
6690 else
Bram Moolenaard9967712006-03-11 21:18:15 +00006691# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006692 {
6693 EMSG(_(e_nowrtmsg));
6694 return;
6695 }
6696 }
6697
Bram Moolenaard9967712006-03-11 21:18:15 +00006698# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006699 need_mouse_correct = TRUE;
Bram Moolenaard9967712006-03-11 21:18:15 +00006700# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006701
Bram Moolenaar071d4272004-06-13 20:20:40 +00006702 /* free buffer when not hiding it or when it's a scratch buffer */
Bram Moolenaarf740b292006-02-16 22:11:02 +00006703 if (tp == NULL)
6704 win_close(win, !need_hide && !P_HID(buf));
6705 else
6706 win_close_othertab(win, !need_hide && !P_HID(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006707}
6708
Bram Moolenaar071d4272004-06-13 20:20:40 +00006709/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006710 * ":tabclose": close current tab page, unless it is the last one.
6711 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006712 */
6713 static void
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006714ex_tabclose(eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006715 exarg_T *eap;
6716{
Bram Moolenaarf740b292006-02-16 22:11:02 +00006717 tabpage_T *tp;
6718
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006719# ifdef FEAT_CMDWIN
6720 if (cmdwin_type != 0)
6721 cmdwin_result = K_IGNORE;
6722 else
6723# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006724 if (first_tabpage->tp_next == NULL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00006725 EMSG(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00006726 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006727 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006728 if (eap->addr_count > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006729 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006730 tp = find_tabpage((int)eap->line2);
6731 if (tp == NULL)
6732 {
6733 beep_flush();
6734 return;
6735 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00006736 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006737 {
6738 tabpage_close_other(tp, eap->forceit);
6739 return;
6740 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006741 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006742 if (!text_locked()
6743#ifdef FEAT_AUTOCMD
6744 && !curbuf_locked()
6745#endif
6746 )
Bram Moolenaarf740b292006-02-16 22:11:02 +00006747 tabpage_close(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006748 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006749}
6750
6751/*
6752 * ":tabonly": close all tab pages except the current one
6753 */
6754 static void
6755ex_tabonly(eap)
6756 exarg_T *eap;
6757{
6758 tabpage_T *tp;
6759 int done;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006760
6761# ifdef FEAT_CMDWIN
6762 if (cmdwin_type != 0)
6763 cmdwin_result = K_IGNORE;
6764 else
6765# endif
6766 if (first_tabpage->tp_next == NULL)
6767 MSG(_("Already only one tab page"));
6768 else
6769 {
6770 /* Repeat this up to a 1000 times, because autocommands may mess
6771 * up the lists. */
6772 for (done = 0; done < 1000; ++done)
6773 {
6774 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6775 if (tp->tp_topframe != topframe)
6776 {
6777 tabpage_close_other(tp, eap->forceit);
6778 /* if we failed to close it quit */
6779 if (valid_tabpage(tp))
6780 done = 1000;
6781 /* start over, "tp" is now invalid */
6782 break;
6783 }
6784 if (first_tabpage->tp_next == NULL)
6785 break;
6786 }
6787 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006788}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006789
6790/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006791 * Close the current tab page.
6792 */
6793 void
6794tabpage_close(forceit)
6795 int forceit;
6796{
6797 /* First close all the windows but the current one. If that worked then
6798 * close the last window in this tab, that will close it. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006799 if (lastwin != firstwin)
6800 close_others(TRUE, forceit);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006801 if (lastwin == firstwin)
6802 ex_win_close(forceit, curwin, NULL);
6803# ifdef FEAT_GUI
6804 need_mouse_correct = TRUE;
6805# endif
6806}
6807
6808/*
6809 * Close tab page "tp", which is not the current tab page.
6810 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006811 * Also takes care of the tab pages line disappearing when closing the
6812 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00006813 */
6814 void
6815tabpage_close_other(tp, forceit)
6816 tabpage_T *tp;
6817 int forceit;
6818{
6819 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006820 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006821 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006822
6823 /* Limit to 1000 windows, autocommands may add a window while we close
6824 * one. OK, so I'm paranoid... */
6825 while (++done < 1000)
6826 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006827 wp = tp->tp_firstwin;
6828 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006829
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006830 /* Autocommands may delete the tab page under our fingers and we may
6831 * fail to close a window with a modified buffer. */
6832 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006833 break;
6834 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006835
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006836 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006837 if (h != tabline_height())
6838 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006839}
6840
6841/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006842 * ":only".
6843 */
6844 static void
6845ex_only(eap)
6846 exarg_T *eap;
6847{
6848# ifdef FEAT_GUI
6849 need_mouse_correct = TRUE;
6850# endif
6851 close_others(TRUE, eap->forceit);
6852}
6853
6854/*
6855 * ":all" and ":sall".
Bram Moolenaard9967712006-03-11 21:18:15 +00006856 * Also used for ":tab drop file ..." after setting the argument list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006857 */
Bram Moolenaard9967712006-03-11 21:18:15 +00006858 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00006859ex_all(eap)
6860 exarg_T *eap;
6861{
6862 if (eap->addr_count == 0)
6863 eap->line2 = 9999;
Bram Moolenaard9967712006-03-11 21:18:15 +00006864 do_arg_all((int)eap->line2, eap->forceit, eap->cmdidx == CMD_drop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006865}
6866#endif /* FEAT_WINDOWS */
6867
6868 static void
6869ex_hide(eap)
6870 exarg_T *eap;
6871{
6872 if (*eap->arg != NUL && check_nextcmd(eap->arg) == NULL)
6873 eap->errmsg = e_invarg;
6874 else
6875 {
6876 /* ":hide" or ":hide | cmd": hide current window */
6877 eap->nextcmd = check_nextcmd(eap->arg);
6878#ifdef FEAT_WINDOWS
6879 if (!eap->skip)
6880 {
6881# ifdef FEAT_GUI
6882 need_mouse_correct = TRUE;
6883# endif
6884 win_close(curwin, FALSE); /* don't free buffer */
6885 }
6886#endif
6887 }
6888}
6889
6890/*
6891 * ":stop" and ":suspend": Suspend Vim.
6892 */
6893 static void
6894ex_stop(eap)
6895 exarg_T *eap;
6896{
6897 /*
6898 * Disallow suspending for "rvim".
6899 */
6900 if (!check_restricted()
6901#ifdef WIN3264
6902 /*
6903 * Check if external commands are allowed now.
6904 */
6905 && can_end_termcap_mode(TRUE)
6906#endif
6907 )
6908 {
6909 if (!eap->forceit)
6910 autowrite_all();
6911 windgoto((int)Rows - 1, 0);
6912 out_char('\n');
6913 out_flush();
6914 stoptermcap();
6915 out_flush(); /* needed for SUN to restore xterm buffer */
6916#ifdef FEAT_TITLE
6917 mch_restore_title(3); /* restore window titles */
6918#endif
6919 ui_suspend(); /* call machine specific function */
6920#ifdef FEAT_TITLE
6921 maketitle();
6922 resettitle(); /* force updating the title */
6923#endif
6924 starttermcap();
6925 scroll_start(); /* scroll screen before redrawing */
6926 redraw_later_clear();
6927 shell_resized(); /* may have resized window */
6928 }
6929}
6930
6931/*
6932 * ":exit", ":xit" and ":wq": Write file and exit Vim.
6933 */
6934 static void
6935ex_exit(eap)
6936 exarg_T *eap;
6937{
6938#ifdef FEAT_CMDWIN
6939 if (cmdwin_type != 0)
6940 {
6941 cmdwin_result = Ctrl_C;
6942 return;
6943 }
6944#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006945 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006946 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006947 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006948 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006949 return;
6950 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006951#ifdef FEAT_AUTOCMD
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01006952 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
6953 /* Refuse to quit when locked or when the buffer in the last window is
6954 * being closed (can only happen in autocommands). */
6955 if (curbuf_locked() || (curbuf->b_nwindows == 1 && curbuf->b_closing))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006956 return;
6957#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006958
6959 /*
6960 * if more files or windows we won't exit
6961 */
6962 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6963 exiting = TRUE;
6964 if ( ((eap->cmdidx == CMD_wq
6965 || curbufIsChanged())
6966 && do_write(eap) == FAIL)
6967 || check_more(TRUE, eap->forceit) == FAIL
6968 || (only_one_window() && check_changed_any(eap->forceit)))
6969 {
6970 not_exiting();
6971 }
6972 else
6973 {
6974#ifdef FEAT_WINDOWS
6975 if (only_one_window()) /* quit last window, exit Vim */
6976#endif
6977 getout(0);
6978#ifdef FEAT_WINDOWS
6979# ifdef FEAT_GUI
6980 need_mouse_correct = TRUE;
6981# endif
6982 /* quit current window, may free buffer */
6983 win_close(curwin, !P_HID(curwin->w_buffer));
6984#endif
6985 }
6986}
6987
6988/*
6989 * ":print", ":list", ":number".
6990 */
6991 static void
6992ex_print(eap)
6993 exarg_T *eap;
6994{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006995 if (curbuf->b_ml.ml_flags & ML_EMPTY)
6996 EMSG(_(e_emptybuf));
6997 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006998 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00006999 for ( ;!got_int; ui_breakcheck())
7000 {
7001 print_line(eap->line1,
7002 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
7003 || (eap->flags & EXFLAG_NR)),
7004 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
7005 if (++eap->line1 > eap->line2)
7006 break;
7007 out_flush(); /* show one line at a time */
7008 }
7009 setpcmark();
7010 /* put cursor at last line */
7011 curwin->w_cursor.lnum = eap->line2;
7012 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007013 }
7014
Bram Moolenaar071d4272004-06-13 20:20:40 +00007015 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007016}
7017
7018#ifdef FEAT_BYTEOFF
7019 static void
7020ex_goto(eap)
7021 exarg_T *eap;
7022{
7023 goto_byte(eap->line2);
7024}
7025#endif
7026
7027/*
7028 * ":shell".
7029 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007030 static void
7031ex_shell(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007032 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007033{
7034 do_shell(NULL, 0);
7035}
7036
7037#if (defined(FEAT_WINDOWS) && defined(HAVE_DROP_FILE)) \
7038 || (defined(FEAT_GUI_GTK) && defined(FEAT_DND)) \
Bram Moolenaar371baa92005-12-29 22:43:53 +00007039 || defined(FEAT_GUI_MSWIN) \
7040 || defined(FEAT_GUI_MAC) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00007041 || defined(PROTO)
7042
7043/*
7044 * Handle a file drop. The code is here because a drop is *nearly* like an
7045 * :args command, but not quite (we have a list of exact filenames, so we
7046 * don't want to (a) parse a command line, or (b) expand wildcards. So the
7047 * code is very similar to :args and hence needs access to a lot of the static
7048 * functions in this file.
7049 *
7050 * The list should be allocated using alloc(), as should each item in the
7051 * list. This function takes over responsibility for freeing the list.
7052 *
Bram Moolenaar81870892007-11-11 18:17:28 +00007053 * XXX The list is made into the argument list. This is freed using
Bram Moolenaar071d4272004-06-13 20:20:40 +00007054 * FreeWild(), which does a series of vim_free() calls, unless the two defines
7055 * __EMX__ and __ALWAYS_HAS_TRAILING_NUL_POINTER are set. In this case, a
7056 * routine _fnexplodefree() is used. This may cause problems, but as the drop
7057 * file functionality is (currently) not in EMX this is not presently a
7058 * problem.
7059 */
7060 void
7061handle_drop(filec, filev, split)
7062 int filec; /* the number of files dropped */
7063 char_u **filev; /* the list of files dropped */
7064 int split; /* force splitting the window */
7065{
7066 exarg_T ea;
7067 int save_msg_scroll = msg_scroll;
7068
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007069 /* Postpone this while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007070 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007071 return;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007072#ifdef FEAT_AUTOCMD
7073 if (curbuf_locked())
7074 return;
7075#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00007076 /* When the screen is being updated we should not change buffers and
7077 * windows structures, it may cause freed memory to be used. */
7078 if (updating_screen)
7079 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007080
7081 /* Check whether the current buffer is changed. If so, we will need
7082 * to split the current window or data could be lost.
7083 * We don't need to check if the 'hidden' option is set, as in this
7084 * case the buffer won't be lost.
7085 */
7086 if (!P_HID(curbuf) && !split)
7087 {
7088 ++emsg_off;
7089 split = check_changed(curbuf, TRUE, FALSE, FALSE, FALSE);
7090 --emsg_off;
7091 }
7092 if (split)
7093 {
7094# ifdef FEAT_WINDOWS
7095 if (win_split(0, 0) == FAIL)
7096 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007097 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007098
7099 /* When splitting the window, create a new alist. Otherwise the
7100 * existing one is overwritten. */
7101 alist_unlink(curwin->w_alist);
7102 alist_new();
7103# else
7104 return; /* can't split, always fail */
7105# endif
7106 }
7107
7108 /*
7109 * Set up the new argument list.
7110 */
Bram Moolenaar86b68352004-12-27 21:59:20 +00007111 alist_set(ALIST(curwin), filec, filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007112
7113 /*
7114 * Move to the first file.
7115 */
7116 /* Fake up a minimal "next" command for do_argfile() */
7117 vim_memset(&ea, 0, sizeof(ea));
7118 ea.cmd = (char_u *)"next";
7119 do_argfile(&ea, 0);
7120
7121 /* do_ecmd() may set need_start_insertmode, but since we never left Insert
7122 * mode that is not needed here. */
7123 need_start_insertmode = FALSE;
7124
7125 /* Restore msg_scroll, otherwise a following command may cause scrolling
7126 * unexpectedly. The screen will be redrawn by the caller, thus
7127 * msg_scroll being set by displaying a message is irrelevant. */
7128 msg_scroll = save_msg_scroll;
7129}
7130#endif
7131
Bram Moolenaar071d4272004-06-13 20:20:40 +00007132/*
7133 * Clear an argument list: free all file names and reset it to zero entries.
7134 */
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007135 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00007136alist_clear(al)
7137 alist_T *al;
7138{
7139 while (--al->al_ga.ga_len >= 0)
7140 vim_free(AARGLIST(al)[al->al_ga.ga_len].ae_fname);
7141 ga_clear(&al->al_ga);
7142}
7143
7144/*
7145 * Init an argument list.
7146 */
7147 void
7148alist_init(al)
7149 alist_T *al;
7150{
7151 ga_init2(&al->al_ga, (int)sizeof(aentry_T), 5);
7152}
7153
7154#if defined(FEAT_WINDOWS) || defined(PROTO)
7155
7156/*
7157 * Remove a reference from an argument list.
7158 * Ignored when the argument list is the global one.
7159 * If the argument list is no longer used by any window, free it.
7160 */
7161 void
7162alist_unlink(al)
7163 alist_T *al;
7164{
7165 if (al != &global_alist && --al->al_refcount <= 0)
7166 {
7167 alist_clear(al);
7168 vim_free(al);
7169 }
7170}
7171
7172# if defined(FEAT_LISTCMDS) || defined(HAVE_DROP_FILE) || defined(PROTO)
7173/*
7174 * Create a new argument list and use it for the current window.
7175 */
7176 void
7177alist_new()
7178{
7179 curwin->w_alist = (alist_T *)alloc((unsigned)sizeof(alist_T));
7180 if (curwin->w_alist == NULL)
7181 {
7182 curwin->w_alist = &global_alist;
7183 ++global_alist.al_refcount;
7184 }
7185 else
7186 {
7187 curwin->w_alist->al_refcount = 1;
7188 alist_init(curwin->w_alist);
7189 }
7190}
7191# endif
7192#endif
7193
7194#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE) || defined(PROTO)
7195/*
7196 * Expand the file names in the global argument list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00007197 * If "fnum_list" is not NULL, use "fnum_list[fnum_len]" as a list of buffer
7198 * numbers to be re-used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007199 */
7200 void
Bram Moolenaar86b68352004-12-27 21:59:20 +00007201alist_expand(fnum_list, fnum_len)
7202 int *fnum_list;
7203 int fnum_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007204{
7205 char_u **old_arg_files;
Bram Moolenaar86b68352004-12-27 21:59:20 +00007206 int old_arg_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007207 char_u **new_arg_files;
7208 int new_arg_file_count;
7209 char_u *save_p_su = p_su;
7210 int i;
7211
7212 /* Don't use 'suffixes' here. This should work like the shell did the
7213 * expansion. Also, the vimrc file isn't read yet, thus the user
7214 * can't set the options. */
7215 p_su = empty_option;
7216 old_arg_files = (char_u **)alloc((unsigned)(sizeof(char_u *) * GARGCOUNT));
7217 if (old_arg_files != NULL)
7218 {
7219 for (i = 0; i < GARGCOUNT; ++i)
Bram Moolenaar86b68352004-12-27 21:59:20 +00007220 old_arg_files[i] = vim_strsave(GARGLIST[i].ae_fname);
7221 old_arg_count = GARGCOUNT;
7222 if (expand_wildcards(old_arg_count, old_arg_files,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007223 &new_arg_file_count, &new_arg_files,
Bram Moolenaarb5609832011-07-20 15:04:58 +02007224 EW_FILE|EW_NOTFOUND|EW_ADDSLASH|EW_NOERROR) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007225 && new_arg_file_count > 0)
7226 {
Bram Moolenaar86b68352004-12-27 21:59:20 +00007227 alist_set(&global_alist, new_arg_file_count, new_arg_files,
7228 TRUE, fnum_list, fnum_len);
7229 FreeWild(old_arg_count, old_arg_files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007230 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007231 }
7232 p_su = save_p_su;
7233}
7234#endif
7235
7236/*
7237 * Set the argument list for the current window.
7238 * Takes over the allocated files[] and the allocated fnames in it.
7239 */
7240 void
Bram Moolenaar86b68352004-12-27 21:59:20 +00007241alist_set(al, count, files, use_curbuf, fnum_list, fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007242 alist_T *al;
7243 int count;
7244 char_u **files;
7245 int use_curbuf;
Bram Moolenaar86b68352004-12-27 21:59:20 +00007246 int *fnum_list;
7247 int fnum_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007248{
7249 int i;
7250
7251 alist_clear(al);
7252 if (ga_grow(&al->al_ga, count) == OK)
7253 {
7254 for (i = 0; i < count; ++i)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007255 {
7256 if (got_int)
7257 {
7258 /* When adding many buffers this can take a long time. Allow
7259 * interrupting here. */
7260 while (i < count)
7261 vim_free(files[i++]);
7262 break;
7263 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00007264
7265 /* May set buffer name of a buffer previously used for the
7266 * argument list, so that it's re-used by alist_add. */
7267 if (fnum_list != NULL && i < fnum_len)
7268 buf_set_name(fnum_list[i], files[i]);
7269
Bram Moolenaar071d4272004-06-13 20:20:40 +00007270 alist_add(al, files[i], use_curbuf ? 2 : 1);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007271 ui_breakcheck();
7272 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007273 vim_free(files);
7274 }
7275 else
7276 FreeWild(count, files);
7277#ifdef FEAT_WINDOWS
7278 if (al == &global_alist)
7279#endif
7280 arg_had_last = FALSE;
7281}
7282
7283/*
7284 * Add file "fname" to argument list "al".
7285 * "fname" must have been allocated and "al" must have been checked for room.
7286 */
7287 void
7288alist_add(al, fname, set_fnum)
7289 alist_T *al;
7290 char_u *fname;
7291 int set_fnum; /* 1: set buffer number; 2: re-use curbuf */
7292{
7293 if (fname == NULL) /* don't add NULL file names */
7294 return;
7295#ifdef BACKSLASH_IN_FILENAME
7296 slash_adjust(fname);
7297#endif
7298 AARGLIST(al)[al->al_ga.ga_len].ae_fname = fname;
7299 if (set_fnum > 0)
7300 AARGLIST(al)[al->al_ga.ga_len].ae_fnum =
7301 buflist_add(fname, BLN_LISTED | (set_fnum == 2 ? BLN_CURBUF : 0));
7302 ++al->al_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007303}
7304
7305#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
7306/*
7307 * Adjust slashes in file names. Called after 'shellslash' was set.
7308 */
7309 void
7310alist_slash_adjust()
7311{
7312 int i;
7313# ifdef FEAT_WINDOWS
7314 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007315 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007316# endif
7317
7318 for (i = 0; i < GARGCOUNT; ++i)
7319 if (GARGLIST[i].ae_fname != NULL)
7320 slash_adjust(GARGLIST[i].ae_fname);
7321# ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00007322 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007323 if (wp->w_alist != &global_alist)
7324 for (i = 0; i < WARGCOUNT(wp); ++i)
7325 if (WARGLIST(wp)[i].ae_fname != NULL)
7326 slash_adjust(WARGLIST(wp)[i].ae_fname);
7327# endif
7328}
7329#endif
7330
7331/*
7332 * ":preserve".
7333 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007334 static void
7335ex_preserve(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007336 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007337{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007338 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007339 ml_preserve(curbuf, TRUE);
7340}
7341
7342/*
7343 * ":recover".
7344 */
7345 static void
7346ex_recover(eap)
7347 exarg_T *eap;
7348{
7349 /* Set recoverymode right away to avoid the ATTENTION prompt. */
7350 recoverymode = TRUE;
7351 if (!check_changed(curbuf, p_awa, TRUE, eap->forceit, FALSE)
7352 && (*eap->arg == NUL
7353 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
7354 ml_recover();
7355 recoverymode = FALSE;
7356}
7357
7358/*
7359 * Command modifier used in a wrong way.
7360 */
7361 static void
7362ex_wrongmodifier(eap)
7363 exarg_T *eap;
7364{
7365 eap->errmsg = e_invcmd;
7366}
7367
7368#ifdef FEAT_WINDOWS
7369/*
7370 * :sview [+command] file split window with new file, read-only
7371 * :split [[+command] file] split window with current or new file
7372 * :vsplit [[+command] file] split window vertically with current or new file
7373 * :new [[+command] file] split window with no or new file
7374 * :vnew [[+command] file] split vertically window with no or new file
7375 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007376 *
7377 * :tabedit open new Tab page with empty window
7378 * :tabedit [+command] file open new Tab page and edit "file"
7379 * :tabnew [[+command] file] just like :tabedit
7380 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007381 */
7382 void
7383ex_splitview(eap)
7384 exarg_T *eap;
7385{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007386 win_T *old_curwin = curwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007387# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007388 char_u *fname = NULL;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007389# endif
7390# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007391 int browse_flag = cmdmod.browse;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007392# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007393
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007394# ifndef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007395 if (eap->cmdidx == CMD_vsplit || eap->cmdidx == CMD_vnew)
7396 {
7397 ex_ni(eap);
7398 return;
7399 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007400# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007401
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007402# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007403 need_mouse_correct = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007404# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007405
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007406# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00007407 /* A ":split" in the quickfix window works like ":new". Don't want two
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007408 * quickfix windows. But it's OK when doing ":tab split". */
7409 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007410 {
7411 if (eap->cmdidx == CMD_split)
7412 eap->cmdidx = CMD_new;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007413# ifdef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007414 if (eap->cmdidx == CMD_vsplit)
7415 eap->cmdidx = CMD_vnew;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007416# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007417 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007418# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007419
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007420# ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007421 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007422 {
7423 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
7424 FNAME_MESS, TRUE, curbuf->b_ffname);
7425 if (fname == NULL)
7426 goto theend;
7427 eap->arg = fname;
7428 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007429# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007430 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007431# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007432# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007433# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007434 if (cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007435# ifdef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007436 && eap->cmdidx != CMD_vnew
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007437# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007438 && eap->cmdidx != CMD_new)
7439 {
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007440# ifdef FEAT_AUTOCMD
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007441 if (
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007442# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007443 !gui.in_use &&
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007444# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007445 au_has_group((char_u *)"FileExplorer"))
7446 {
7447 /* No browsing supported but we do have the file explorer:
7448 * Edit the directory. */
7449 if (*eap->arg == NUL || !mch_isdir(eap->arg))
7450 eap->arg = (char_u *)".";
7451 }
7452 else
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007453# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007454 {
7455 fname = do_browse(0, (char_u *)_("Edit File in new window"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007456 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007457 if (fname == NULL)
7458 goto theend;
7459 eap->arg = fname;
7460 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007461 }
7462 cmdmod.browse = FALSE; /* Don't browse again in do_ecmd(). */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007463# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007464
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007465 /*
7466 * Either open new tab page or split the window.
7467 */
7468 if (eap->cmdidx == CMD_tabedit
7469 || eap->cmdidx == CMD_tabfind
7470 || eap->cmdidx == CMD_tabnew)
7471 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00007472 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
7473 : eap->addr_count == 0 ? 0
7474 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007475 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00007476 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007477
7478 /* set the alternate buffer for the window we came from */
7479 if (curwin != old_curwin
7480 && win_valid(old_curwin)
7481 && old_curwin->w_buffer != curbuf
7482 && !cmdmod.keepalt)
7483 old_curwin->w_alt_fnum = curbuf->b_fnum;
7484 }
7485 }
7486 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007487 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
7488 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007489# ifdef FEAT_SCROLLBIND
Bram Moolenaar071d4272004-06-13 20:20:40 +00007490 /* Reset 'scrollbind' when editing another file, but keep it when
7491 * doing ":split" without arguments. */
7492 if (*eap->arg != NUL
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007493# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007494 || cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007495# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007496 )
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007497 {
7498 RESET_BINDING(curwin);
7499 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007500 else
7501 do_check_scrollbind(FALSE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007502# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007503 do_exedit(eap, old_curwin);
7504 }
7505
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007506# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007507 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007508# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007509
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007510# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007511theend:
7512 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007513# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007514}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007515
7516/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007517 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007518 */
7519 void
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007520tabpage_new()
7521{
7522 exarg_T ea;
7523
7524 vim_memset(&ea, 0, sizeof(ea));
7525 ea.cmdidx = CMD_tabnew;
7526 ea.cmd = (char_u *)"tabn";
7527 ea.arg = (char_u *)"";
7528 ex_splitview(&ea);
7529}
7530
7531/*
7532 * :tabnext command
7533 */
7534 static void
7535ex_tabnext(eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007536 exarg_T *eap;
7537{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007538 switch (eap->cmdidx)
7539 {
7540 case CMD_tabfirst:
7541 case CMD_tabrewind:
7542 goto_tabpage(1);
7543 break;
7544 case CMD_tablast:
7545 goto_tabpage(9999);
7546 break;
7547 case CMD_tabprevious:
7548 case CMD_tabNext:
7549 goto_tabpage(eap->addr_count == 0 ? -1 : -(int)eap->line2);
7550 break;
7551 default: /* CMD_tabnext */
7552 goto_tabpage(eap->addr_count == 0 ? 0 : (int)eap->line2);
7553 break;
7554 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007555}
7556
7557/*
7558 * :tabmove command
7559 */
7560 static void
7561ex_tabmove(eap)
7562 exarg_T *eap;
7563{
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02007564 int tab_number = 9999;
7565
7566 if (eap->arg && *eap->arg != NUL)
7567 {
7568 char_u *p = eap->arg;
7569 int relative = 0; /* argument +N/-N means: move N places to the
7570 * right/left relative to the current position. */
7571
7572 if (*eap->arg == '-')
7573 {
7574 relative = -1;
7575 p = eap->arg + 1;
7576 }
7577 else if (*eap->arg == '+')
7578 {
7579 relative = 1;
7580 p = eap->arg + 1;
7581 }
7582 else
7583 p = eap->arg;
7584
7585 if (p == skipdigits(p))
7586 {
7587 /* No numbers as argument. */
7588 eap->errmsg = e_invarg;
7589 return;
7590 }
7591
7592 tab_number = getdigits(&p);
7593 if (relative != 0)
7594 tab_number = tab_number * relative + tabpage_index(curtab) - 1;;
7595 }
7596 else if (eap->addr_count != 0)
7597 tab_number = eap->line2;
7598
7599 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007600}
7601
7602/*
7603 * :tabs command: List tabs and their contents.
7604 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007605 static void
7606ex_tabs(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007607 exarg_T *eap UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007608{
7609 tabpage_T *tp;
7610 win_T *wp;
7611 int tabcount = 1;
7612
7613 msg_start();
7614 msg_scroll = TRUE;
7615 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
7616 {
7617 msg_putchar('\n');
7618 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
7619 msg_outtrans_attr(IObuff, hl_attr(HLF_T));
7620 out_flush(); /* output one line at a time */
7621 ui_breakcheck();
7622
Bram Moolenaar030f0df2006-02-21 22:02:53 +00007623 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007624 wp = firstwin;
7625 else
7626 wp = tp->tp_firstwin;
7627 for ( ; wp != NULL && !got_int; wp = wp->w_next)
7628 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007629 msg_putchar('\n');
7630 msg_putchar(wp == curwin ? '>' : ' ');
7631 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007632 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
7633 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007634 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02007635 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007636 else
7637 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
7638 IObuff, IOSIZE, TRUE);
7639 msg_outtrans(IObuff);
7640 out_flush(); /* output one line at a time */
7641 ui_breakcheck();
7642 }
7643 }
7644}
7645
7646#endif /* FEAT_WINDOWS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007647
7648/*
7649 * ":mode": Set screen mode.
7650 * If no argument given, just get the screen size and redraw.
7651 */
7652 static void
7653ex_mode(eap)
7654 exarg_T *eap;
7655{
7656 if (*eap->arg == NUL)
7657 shell_resized();
7658 else
7659 mch_screenmode(eap->arg);
7660}
7661
7662#ifdef FEAT_WINDOWS
7663/*
7664 * ":resize".
7665 * set, increment or decrement current window height
7666 */
7667 static void
7668ex_resize(eap)
7669 exarg_T *eap;
7670{
7671 int n;
7672 win_T *wp = curwin;
7673
7674 if (eap->addr_count > 0)
7675 {
7676 n = eap->line2;
7677 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
7678 ;
7679 }
7680
7681#ifdef FEAT_GUI
7682 need_mouse_correct = TRUE;
7683#endif
7684 n = atol((char *)eap->arg);
7685#ifdef FEAT_VERTSPLIT
7686 if (cmdmod.split & WSP_VERT)
7687 {
7688 if (*eap->arg == '-' || *eap->arg == '+')
7689 n += W_WIDTH(curwin);
7690 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
7691 n = 9999;
7692 win_setwidth_win((int)n, wp);
7693 }
7694 else
7695#endif
7696 {
7697 if (*eap->arg == '-' || *eap->arg == '+')
7698 n += curwin->w_height;
7699 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
7700 n = 9999;
7701 win_setheight_win((int)n, wp);
7702 }
7703}
7704#endif
7705
7706/*
7707 * ":find [+command] <file>" command.
7708 */
7709 static void
7710ex_find(eap)
7711 exarg_T *eap;
7712{
7713#ifdef FEAT_SEARCHPATH
7714 char_u *fname;
7715 int count;
7716
7717 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
7718 TRUE, curbuf->b_ffname);
7719 if (eap->addr_count > 0)
7720 {
7721 /* Repeat finding the file "count" times. This matters when it
7722 * appears several times in the path. */
7723 count = eap->line2;
7724 while (fname != NULL && --count > 0)
7725 {
7726 vim_free(fname);
7727 fname = find_file_in_path(NULL, 0, FNAME_MESS,
7728 FALSE, curbuf->b_ffname);
7729 }
7730 }
7731
7732 if (fname != NULL)
7733 {
7734 eap->arg = fname;
7735#endif
7736 do_exedit(eap, NULL);
7737#ifdef FEAT_SEARCHPATH
7738 vim_free(fname);
7739 }
7740#endif
7741}
7742
7743/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00007744 * ":open" simulation: for now just work like ":visual".
7745 */
7746 static void
7747ex_open(eap)
7748 exarg_T *eap;
7749{
7750 regmatch_T regmatch;
7751 char_u *p;
7752
7753 curwin->w_cursor.lnum = eap->line2;
7754 beginline(BL_SOL | BL_FIX);
7755 if (*eap->arg == '/')
7756 {
7757 /* ":open /pattern/": put cursor in column found with pattern */
7758 ++eap->arg;
7759 p = skip_regexp(eap->arg, '/', p_magic, NULL);
7760 *p = NUL;
7761 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
7762 if (regmatch.regprog != NULL)
7763 {
7764 regmatch.rm_ic = p_ic;
7765 p = ml_get_curline();
7766 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007767 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007768 else
7769 EMSG(_(e_nomatch));
7770 vim_free(regmatch.regprog);
7771 }
7772 /* Move to the NUL, ignore any other arguments. */
7773 eap->arg += STRLEN(eap->arg);
7774 }
7775 check_cursor();
7776
7777 eap->cmdidx = CMD_visual;
7778 do_exedit(eap, NULL);
7779}
7780
7781/*
7782 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007783 */
7784 static void
7785ex_edit(eap)
7786 exarg_T *eap;
7787{
7788 do_exedit(eap, NULL);
7789}
7790
7791/*
7792 * ":edit <file>" command and alikes.
7793 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007794 void
7795do_exedit(eap, old_curwin)
7796 exarg_T *eap;
7797 win_T *old_curwin; /* curwin before doing a split or NULL */
7798{
7799 int n;
7800#ifdef FEAT_WINDOWS
7801 int need_hide;
7802#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00007803 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007804
7805 /*
7806 * ":vi" command ends Ex mode.
7807 */
7808 if (exmode_active && (eap->cmdidx == CMD_visual
7809 || eap->cmdidx == CMD_view))
7810 {
7811 exmode_active = FALSE;
7812 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007813 {
7814 /* Special case: ":global/pat/visual\NLvi-commands" */
7815 if (global_busy)
7816 {
7817 int rd = RedrawingDisabled;
7818 int nwr = no_wait_return;
7819 int ms = msg_scroll;
7820#ifdef FEAT_GUI
7821 int he = hold_gui_events;
7822#endif
7823
7824 if (eap->nextcmd != NULL)
7825 {
7826 stuffReadbuff(eap->nextcmd);
7827 eap->nextcmd = NULL;
7828 }
7829
7830 if (exmode_was != EXMODE_VIM)
7831 settmode(TMODE_RAW);
7832 RedrawingDisabled = 0;
7833 no_wait_return = 0;
7834 need_wait_return = FALSE;
7835 msg_scroll = 0;
7836#ifdef FEAT_GUI
7837 hold_gui_events = 0;
7838#endif
7839 must_redraw = CLEAR;
7840
7841 main_loop(FALSE, TRUE);
7842
7843 RedrawingDisabled = rd;
7844 no_wait_return = nwr;
7845 msg_scroll = ms;
7846#ifdef FEAT_GUI
7847 hold_gui_events = he;
7848#endif
7849 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007850 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007851 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007852 }
7853
7854 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007855 || eap->cmdidx == CMD_tabnew
7856 || eap->cmdidx == CMD_tabedit
Bram Moolenaar071d4272004-06-13 20:20:40 +00007857#ifdef FEAT_VERTSPLIT
7858 || eap->cmdidx == CMD_vnew
7859#endif
7860 ) && *eap->arg == NUL)
7861 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007862 /* ":new" or ":tabnew" without argument: edit an new empty buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007863 setpcmark();
7864 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007865 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
7866 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007867 }
7868 else if ((eap->cmdidx != CMD_split
7869#ifdef FEAT_VERTSPLIT
7870 && eap->cmdidx != CMD_vsplit
7871#endif
7872 )
7873 || *eap->arg != NUL
7874#ifdef FEAT_BROWSE
7875 || cmdmod.browse
7876#endif
7877 )
7878 {
Bram Moolenaar5555acc2006-04-07 21:33:12 +00007879#ifdef FEAT_AUTOCMD
7880 /* Can't edit another file when "curbuf_lock" is set. Only ":edit"
7881 * can bring us here, others are stopped earlier. */
7882 if (*eap->arg != NUL && curbuf_locked())
7883 return;
7884#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007885 n = readonlymode;
7886 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
7887 readonlymode = TRUE;
7888 else if (eap->cmdidx == CMD_enew)
7889 readonlymode = FALSE; /* 'readonly' doesn't make sense in an
7890 empty buffer */
7891 setpcmark();
7892 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
7893 NULL, eap,
7894 /* ":edit" goes to first line if Vi compatible */
7895 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
7896 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
7897 ? ECMD_ONE : eap->do_ecmd_lnum,
7898 (P_HID(curbuf) ? ECMD_HIDE : 0)
7899 + (eap->forceit ? ECMD_FORCEIT : 0)
7900#ifdef FEAT_LISTCMDS
7901 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
7902#endif
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007903 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007904 {
7905 /* Editing the file failed. If the window was split, close it. */
7906#ifdef FEAT_WINDOWS
7907 if (old_curwin != NULL)
7908 {
7909 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
7910 if (!need_hide || P_HID(curbuf))
7911 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007912# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7913 cleanup_T cs;
7914
7915 /* Reset the error/interrupt/exception state here so that
7916 * aborting() returns FALSE when closing a window. */
7917 enter_cleanup(&cs);
7918# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007919# ifdef FEAT_GUI
7920 need_mouse_correct = TRUE;
7921# endif
7922 win_close(curwin, !need_hide && !P_HID(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007923
7924# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7925 /* Restore the error/interrupt/exception state if not
7926 * discarded by a new aborting error, interrupt, or
7927 * uncaught exception. */
7928 leave_cleanup(&cs);
7929# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007930 }
7931 }
7932#endif
7933 }
7934 else if (readonlymode && curbuf->b_nwindows == 1)
7935 {
7936 /* When editing an already visited buffer, 'readonly' won't be set
7937 * but the previous value is kept. With ":view" and ":sview" we
7938 * want the file to be readonly, except when another window is
7939 * editing the same buffer. */
7940 curbuf->b_p_ro = TRUE;
7941 }
7942 readonlymode = n;
7943 }
7944 else
7945 {
7946 if (eap->do_ecmd_cmd != NULL)
7947 do_cmdline_cmd(eap->do_ecmd_cmd);
7948#ifdef FEAT_TITLE
7949 n = curwin->w_arg_idx_invalid;
7950#endif
7951 check_arg_idx(curwin);
7952#ifdef FEAT_TITLE
7953 if (n != curwin->w_arg_idx_invalid)
7954 maketitle();
7955#endif
7956 }
7957
7958#ifdef FEAT_WINDOWS
7959 /*
7960 * if ":split file" worked, set alternate file name in old window to new
7961 * file
7962 */
7963 if (old_curwin != NULL
7964 && *eap->arg != NUL
7965 && curwin != old_curwin
7966 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00007967 && old_curwin->w_buffer != curbuf
7968 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007969 old_curwin->w_alt_fnum = curbuf->b_fnum;
7970#endif
7971
7972 ex_no_reprint = TRUE;
7973}
7974
7975#ifndef FEAT_GUI
7976/*
7977 * ":gui" and ":gvim" when there is no GUI.
7978 */
7979 static void
7980ex_nogui(eap)
7981 exarg_T *eap;
7982{
7983 eap->errmsg = e_nogvim;
7984}
7985#endif
7986
7987#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
7988 static void
7989ex_tearoff(eap)
7990 exarg_T *eap;
7991{
7992 gui_make_tearoff(eap->arg);
7993}
7994#endif
7995
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007996#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007997 static void
7998ex_popup(eap)
7999 exarg_T *eap;
8000{
Bram Moolenaar97409f12005-07-08 22:17:29 +00008001 gui_make_popup(eap->arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008002}
8003#endif
8004
Bram Moolenaar071d4272004-06-13 20:20:40 +00008005 static void
8006ex_swapname(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008007 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008008{
8009 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
8010 MSG(_("No swap file"));
8011 else
8012 msg(curbuf->b_ml.ml_mfp->mf_fname);
8013}
8014
8015/*
8016 * ":syncbind" forces all 'scrollbind' windows to have the same relative
8017 * offset.
8018 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
8019 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008020 static void
8021ex_syncbind(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008022 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008023{
8024#ifdef FEAT_SCROLLBIND
8025 win_T *wp;
8026 long topline;
8027 long y;
8028 linenr_T old_linenr = curwin->w_cursor.lnum;
8029
8030 setpcmark();
8031
8032 /*
8033 * determine max topline
8034 */
8035 if (curwin->w_p_scb)
8036 {
8037 topline = curwin->w_topline;
8038 for (wp = firstwin; wp; wp = wp->w_next)
8039 {
8040 if (wp->w_p_scb && wp->w_buffer)
8041 {
8042 y = wp->w_buffer->b_ml.ml_line_count - p_so;
8043 if (topline > y)
8044 topline = y;
8045 }
8046 }
8047 if (topline < 1)
8048 topline = 1;
8049 }
8050 else
8051 {
8052 topline = 1;
8053 }
8054
8055
8056 /*
8057 * set all scrollbind windows to the same topline
8058 */
8059 wp = curwin;
8060 for (curwin = firstwin; curwin; curwin = curwin->w_next)
8061 {
8062 if (curwin->w_p_scb)
8063 {
8064 y = topline - curwin->w_topline;
8065 if (y > 0)
8066 scrollup(y, TRUE);
8067 else
8068 scrolldown(-y, TRUE);
8069 curwin->w_scbind_pos = topline;
8070 redraw_later(VALID);
8071 cursor_correct();
8072#ifdef FEAT_WINDOWS
8073 curwin->w_redr_status = TRUE;
8074#endif
8075 }
8076 }
8077 curwin = wp;
8078 if (curwin->w_p_scb)
8079 {
8080 did_syncbind = TRUE;
8081 checkpcmark();
8082 if (old_linenr != curwin->w_cursor.lnum)
8083 {
8084 char_u ctrl_o[2];
8085
8086 ctrl_o[0] = Ctrl_O;
8087 ctrl_o[1] = 0;
8088 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
8089 }
8090 }
8091#endif
8092}
8093
8094
8095 static void
8096ex_read(eap)
8097 exarg_T *eap;
8098{
Bram Moolenaardf177f62005-02-22 08:39:57 +00008099 int i;
8100 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
8101 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008102
8103 if (eap->usefilter) /* :r!cmd */
8104 do_bang(1, eap, FALSE, FALSE, TRUE);
8105 else
8106 {
8107 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
8108 return;
8109
8110#ifdef FEAT_BROWSE
8111 if (cmdmod.browse)
8112 {
8113 char_u *browseFile;
8114
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008115 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008116 NULL, NULL, NULL, curbuf);
8117 if (browseFile != NULL)
8118 {
8119 i = readfile(browseFile, NULL,
8120 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8121 vim_free(browseFile);
8122 }
8123 else
8124 i = OK;
8125 }
8126 else
8127#endif
8128 if (*eap->arg == NUL)
8129 {
8130 if (check_fname() == FAIL) /* check for no file name */
8131 return;
8132 i = readfile(curbuf->b_ffname, curbuf->b_fname,
8133 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8134 }
8135 else
8136 {
8137 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
8138 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
8139 i = readfile(eap->arg, NULL,
8140 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8141
8142 }
8143 if (i == FAIL)
8144 {
8145#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
8146 if (!aborting())
8147#endif
8148 EMSG2(_(e_notopen), eap->arg);
8149 }
8150 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008151 {
8152 if (empty && exmode_active)
8153 {
8154 /* Delete the empty line that remains. Historically ex does
8155 * this but vi doesn't. */
8156 if (eap->line2 == 0)
8157 lnum = curbuf->b_ml.ml_line_count;
8158 else
8159 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008160 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008161 {
8162 ml_delete(lnum, FALSE);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008163 if (curwin->w_cursor.lnum > 1
8164 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008165 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00008166 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008167 }
8168 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008169 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008170 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008171 }
8172}
8173
Bram Moolenaarea408852005-06-25 22:49:46 +00008174static char_u *prev_dir = NULL;
8175
8176#if defined(EXITFREE) || defined(PROTO)
8177 void
8178free_cd_dir()
8179{
8180 vim_free(prev_dir);
Bram Moolenaara0174af2008-01-02 20:08:25 +00008181 prev_dir = NULL;
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00008182
8183 vim_free(globaldir);
8184 globaldir = NULL;
Bram Moolenaarea408852005-06-25 22:49:46 +00008185}
8186#endif
8187
Bram Moolenaarf4258302013-06-02 18:20:17 +02008188/*
8189 * Deal with the side effects of changing the current directory.
8190 * When "local" is TRUE then this was after an ":lcd" command.
8191 */
8192 void
8193post_chdir(local)
8194 int local;
8195{
8196 vim_free(curwin->w_localdir);
8197 if (local)
8198 {
8199 /* If still in global directory, need to remember current
8200 * directory as global directory. */
8201 if (globaldir == NULL && prev_dir != NULL)
8202 globaldir = vim_strsave(prev_dir);
8203 /* Remember this local directory for the window. */
8204 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8205 curwin->w_localdir = vim_strsave(NameBuff);
8206 }
8207 else
8208 {
8209 /* We are now in the global directory, no need to remember its
8210 * name. */
8211 vim_free(globaldir);
8212 globaldir = NULL;
8213 curwin->w_localdir = NULL;
8214 }
8215
8216 shorten_fnames(TRUE);
8217}
8218
Bram Moolenaarea408852005-06-25 22:49:46 +00008219
Bram Moolenaar071d4272004-06-13 20:20:40 +00008220/*
8221 * ":cd", ":lcd", ":chdir" and ":lchdir".
8222 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00008223 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00008224ex_cd(eap)
8225 exarg_T *eap;
8226{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008227 char_u *new_dir;
8228 char_u *tofree;
8229
8230 new_dir = eap->arg;
8231#if !defined(UNIX) && !defined(VMS)
8232 /* for non-UNIX ":cd" means: print current directory */
8233 if (*new_dir == NUL)
8234 ex_pwd(NULL);
8235 else
8236#endif
8237 {
Bram Moolenaar8e8fe9b2009-03-11 14:37:32 +00008238#ifdef FEAT_AUTOCMD
8239 if (allbuf_locked())
8240 return;
8241#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00008242 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged()
8243 && !eap->forceit)
8244 {
Bram Moolenaar81870892007-11-11 18:17:28 +00008245 EMSG(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00008246 return;
8247 }
8248
Bram Moolenaar071d4272004-06-13 20:20:40 +00008249 /* ":cd -": Change to previous directory */
8250 if (STRCMP(new_dir, "-") == 0)
8251 {
8252 if (prev_dir == NULL)
8253 {
8254 EMSG(_("E186: No previous directory"));
8255 return;
8256 }
8257 new_dir = prev_dir;
8258 }
8259
8260 /* Save current directory for next ":cd -" */
8261 tofree = prev_dir;
8262 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8263 prev_dir = vim_strsave(NameBuff);
8264 else
8265 prev_dir = NULL;
8266
8267#if defined(UNIX) || defined(VMS)
8268 /* for UNIX ":cd" means: go to home directory */
8269 if (*new_dir == NUL)
8270 {
8271 /* use NameBuff for home directory name */
8272# ifdef VMS
8273 char_u *p;
8274
8275 p = mch_getenv((char_u *)"SYS$LOGIN");
8276 if (p == NULL || *p == NUL) /* empty is the same as not set */
8277 NameBuff[0] = NUL;
8278 else
Bram Moolenaarb6356332005-07-18 21:40:44 +00008279 vim_strncpy(NameBuff, p, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008280# else
8281 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
8282# endif
8283 new_dir = NameBuff;
8284 }
8285#endif
8286 if (new_dir == NULL || vim_chdir(new_dir))
8287 EMSG(_(e_failed));
8288 else
8289 {
Bram Moolenaarf4258302013-06-02 18:20:17 +02008290 post_chdir(eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008291
8292 /* Echo the new current directory if the command was typed. */
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00008293 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008294 ex_pwd(eap);
8295 }
8296 vim_free(tofree);
8297 }
8298}
8299
8300/*
8301 * ":pwd".
8302 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008303 static void
8304ex_pwd(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008305 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008306{
8307 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8308 {
8309#ifdef BACKSLASH_IN_FILENAME
8310 slash_adjust(NameBuff);
8311#endif
8312 msg(NameBuff);
8313 }
8314 else
8315 EMSG(_("E187: Unknown"));
8316}
8317
8318/*
8319 * ":=".
8320 */
8321 static void
8322ex_equal(eap)
8323 exarg_T *eap;
8324{
8325 smsg((char_u *)"%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008326 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008327}
8328
8329 static void
8330ex_sleep(eap)
8331 exarg_T *eap;
8332{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008333 int n;
8334 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008335
8336 if (cursor_valid())
8337 {
8338 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
8339 if (n >= 0)
8340 windgoto((int)n, curwin->w_wcol);
8341 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008342
8343 len = eap->line2;
8344 switch (*eap->arg)
8345 {
8346 case 'm': break;
8347 case NUL: len *= 1000L; break;
8348 default: EMSG2(_(e_invarg2), eap->arg); return;
8349 }
8350 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008351}
8352
8353/*
8354 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
8355 */
8356 void
8357do_sleep(msec)
8358 long msec;
8359{
8360 long done;
8361
8362 cursor_on();
8363 out_flush();
8364 for (done = 0; !got_int && done < msec; done += 1000L)
8365 {
8366 ui_delay(msec - done > 1000L ? 1000L : msec - done, TRUE);
8367 ui_breakcheck();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02008368#ifdef FEAT_NETBEANS_INTG
8369 /* Process the netbeans messages that may have been received in the
8370 * call to ui_breakcheck() when the GUI is in use. This may occur when
8371 * running a test case. */
8372 netbeans_parse_messages();
8373#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008374 }
8375}
8376
8377 static void
8378do_exmap(eap, isabbrev)
8379 exarg_T *eap;
8380 int isabbrev;
8381{
8382 int mode;
8383 char_u *cmdp;
8384
8385 cmdp = eap->cmd;
8386 mode = get_map_mode(&cmdp, eap->forceit || isabbrev);
8387
8388 switch (do_map((*cmdp == 'n') ? 2 : (*cmdp == 'u'),
8389 eap->arg, mode, isabbrev))
8390 {
8391 case 1: EMSG(_(e_invarg));
8392 break;
8393 case 2: EMSG(isabbrev ? _(e_noabbr) : _(e_nomap));
8394 break;
8395 }
8396}
8397
8398/*
8399 * ":winsize" command (obsolete).
8400 */
8401 static void
8402ex_winsize(eap)
8403 exarg_T *eap;
8404{
8405 int w, h;
8406 char_u *arg = eap->arg;
8407 char_u *p;
8408
8409 w = getdigits(&arg);
8410 arg = skipwhite(arg);
8411 p = arg;
8412 h = getdigits(&arg);
8413 if (*p != NUL && *arg == NUL)
8414 set_shellsize(w, h, TRUE);
8415 else
8416 EMSG(_("E465: :winsize requires two number arguments"));
8417}
8418
8419#ifdef FEAT_WINDOWS
8420 static void
8421ex_wincmd(eap)
8422 exarg_T *eap;
8423{
8424 int xchar = NUL;
8425 char_u *p;
8426
8427 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
8428 {
8429 /* CTRL-W g and CTRL-W CTRL-G have an extra command character */
8430 if (eap->arg[1] == NUL)
8431 {
8432 EMSG(_(e_invarg));
8433 return;
8434 }
8435 xchar = eap->arg[1];
8436 p = eap->arg + 2;
8437 }
8438 else
8439 p = eap->arg + 1;
8440
8441 eap->nextcmd = check_nextcmd(p);
8442 p = skipwhite(p);
8443 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
8444 EMSG(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02008445 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008446 {
8447 /* Pass flags on for ":vertical wincmd ]". */
8448 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008449 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008450 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
8451 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008452 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008453 }
8454}
8455#endif
8456
Bram Moolenaar843ee412004-06-30 16:16:41 +00008457#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008458/*
8459 * ":winpos".
8460 */
8461 static void
8462ex_winpos(eap)
8463 exarg_T *eap;
8464{
8465 int x, y;
8466 char_u *arg = eap->arg;
8467 char_u *p;
8468
8469 if (*arg == NUL)
8470 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00008471# if defined(FEAT_GUI) || defined(MSWIN)
8472# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008473 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00008474# else
8475 if (mch_get_winpos(&x, &y) != FAIL)
8476# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008477 {
8478 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
8479 msg(IObuff);
8480 }
8481 else
8482# endif
8483 EMSG(_("E188: Obtaining window position not implemented for this platform"));
8484 }
8485 else
8486 {
8487 x = getdigits(&arg);
8488 arg = skipwhite(arg);
8489 p = arg;
8490 y = getdigits(&arg);
8491 if (*p == NUL || *arg != NUL)
8492 {
8493 EMSG(_("E466: :winpos requires two number arguments"));
8494 return;
8495 }
8496# ifdef FEAT_GUI
8497 if (gui.in_use)
8498 gui_mch_set_winpos(x, y);
8499 else if (gui.starting)
8500 {
8501 /* Remember the coordinates for when the window is opened. */
8502 gui_win_x = x;
8503 gui_win_y = y;
8504 }
8505# ifdef HAVE_TGETENT
8506 else
8507# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00008508# else
8509# ifdef MSWIN
8510 mch_set_winpos(x, y);
8511# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008512# endif
8513# ifdef HAVE_TGETENT
8514 if (*T_CWP)
8515 term_set_winpos(x, y);
8516# endif
8517 }
8518}
8519#endif
8520
8521/*
8522 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
8523 */
8524 static void
8525ex_operators(eap)
8526 exarg_T *eap;
8527{
8528 oparg_T oa;
8529
8530 clear_oparg(&oa);
8531 oa.regname = eap->regname;
8532 oa.start.lnum = eap->line1;
8533 oa.end.lnum = eap->line2;
8534 oa.line_count = eap->line2 - eap->line1 + 1;
8535 oa.motion_type = MLINE;
8536#ifdef FEAT_VIRTUALEDIT
8537 virtual_op = FALSE;
8538#endif
8539 if (eap->cmdidx != CMD_yank) /* position cursor for undo */
8540 {
8541 setpcmark();
8542 curwin->w_cursor.lnum = eap->line1;
8543 beginline(BL_SOL | BL_FIX);
8544 }
8545
8546 switch (eap->cmdidx)
8547 {
8548 case CMD_delete:
8549 oa.op_type = OP_DELETE;
8550 op_delete(&oa);
8551 break;
8552
8553 case CMD_yank:
8554 oa.op_type = OP_YANK;
8555 (void)op_yank(&oa, FALSE, TRUE);
8556 break;
8557
8558 default: /* CMD_rshift or CMD_lshift */
8559 if ((eap->cmdidx == CMD_rshift)
8560#ifdef FEAT_RIGHTLEFT
8561 ^ curwin->w_p_rl
8562#endif
8563 )
8564 oa.op_type = OP_RSHIFT;
8565 else
8566 oa.op_type = OP_LSHIFT;
8567 op_shift(&oa, FALSE, eap->amount);
8568 break;
8569 }
8570#ifdef FEAT_VIRTUALEDIT
8571 virtual_op = MAYBE;
8572#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00008573 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008574}
8575
8576/*
8577 * ":put".
8578 */
8579 static void
8580ex_put(eap)
8581 exarg_T *eap;
8582{
8583 /* ":0put" works like ":1put!". */
8584 if (eap->line2 == 0)
8585 {
8586 eap->line2 = 1;
8587 eap->forceit = TRUE;
8588 }
8589 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008590 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
8591 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008592}
8593
8594/*
8595 * Handle ":copy" and ":move".
8596 */
8597 static void
8598ex_copymove(eap)
8599 exarg_T *eap;
8600{
8601 long n;
8602
8603 n = get_address(&eap->arg, FALSE, FALSE);
8604 if (eap->arg == NULL) /* error detected */
8605 {
8606 eap->nextcmd = NULL;
8607 return;
8608 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008609 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008610
8611 /*
8612 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
8613 */
8614 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
8615 {
8616 EMSG(_(e_invaddr));
8617 return;
8618 }
8619
8620 if (eap->cmdidx == CMD_move)
8621 {
8622 if (do_move(eap->line1, eap->line2, n) == FAIL)
8623 return;
8624 }
8625 else
8626 ex_copy(eap->line1, eap->line2, n);
8627 u_clearline();
8628 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008629 ex_may_print(eap);
8630}
8631
8632/*
8633 * Print the current line if flags were given to the Ex command.
8634 */
8635 static void
8636ex_may_print(eap)
8637 exarg_T *eap;
8638{
8639 if (eap->flags != 0)
8640 {
8641 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
8642 (eap->flags & EXFLAG_LIST));
8643 ex_no_reprint = TRUE;
8644 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008645}
8646
8647/*
8648 * ":smagic" and ":snomagic".
8649 */
8650 static void
8651ex_submagic(eap)
8652 exarg_T *eap;
8653{
8654 int magic_save = p_magic;
8655
8656 p_magic = (eap->cmdidx == CMD_smagic);
8657 do_sub(eap);
8658 p_magic = magic_save;
8659}
8660
8661/*
8662 * ":join".
8663 */
8664 static void
8665ex_join(eap)
8666 exarg_T *eap;
8667{
8668 curwin->w_cursor.lnum = eap->line1;
8669 if (eap->line1 == eap->line2)
8670 {
8671 if (eap->addr_count >= 2) /* :2,2join does nothing */
8672 return;
8673 if (eap->line2 == curbuf->b_ml.ml_line_count)
8674 {
8675 beep_flush();
8676 return;
8677 }
8678 ++eap->line2;
8679 }
Bram Moolenaar81340392012-06-06 16:12:59 +02008680 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008681 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008682 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008683}
8684
8685/*
8686 * ":[addr]@r" or ":[addr]*r": execute register
8687 */
8688 static void
8689ex_at(eap)
8690 exarg_T *eap;
8691{
8692 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00008693 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008694
8695 curwin->w_cursor.lnum = eap->line2;
8696
8697#ifdef USE_ON_FLY_SCROLL
8698 dont_scroll = TRUE; /* disallow scrolling here */
8699#endif
8700
8701 /* get the register name. No name means to use the previous one */
8702 c = *eap->arg;
8703 if (c == NUL || (c == '*' && *eap->cmd == '*'))
8704 c = '@';
Bram Moolenaard333d1e2006-11-07 17:43:47 +00008705 /* Put the register in the typeahead buffer with the "silent" flag. */
8706 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
8707 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008708 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008709 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00008710 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008711 else
8712 {
8713 int save_efr = exec_from_reg;
8714
8715 exec_from_reg = TRUE;
8716
8717 /*
8718 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00008719 * Continue until the stuff buffer is empty and all added characters
8720 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008721 */
Bram Moolenaar60462872009-11-03 11:40:19 +00008722 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008723 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
8724
8725 exec_from_reg = save_efr;
8726 }
8727}
8728
8729/*
8730 * ":!".
8731 */
8732 static void
8733ex_bang(eap)
8734 exarg_T *eap;
8735{
8736 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
8737}
8738
8739/*
8740 * ":undo".
8741 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008742 static void
8743ex_undo(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008744 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008745{
Bram Moolenaard3667a22006-03-16 21:35:52 +00008746 if (eap->addr_count == 1) /* :undo 123 */
Bram Moolenaar730cde92010-06-27 05:18:54 +02008747 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00008748 else
8749 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008750}
8751
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008752#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008753 static void
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008754ex_wundo(eap)
8755 exarg_T *eap;
8756{
8757 char_u hash[UNDO_HASH_SIZE];
8758
8759 u_compute_hash(hash);
8760 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
8761}
8762
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008763 static void
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008764ex_rundo(eap)
8765 exarg_T *eap;
8766{
8767 char_u hash[UNDO_HASH_SIZE];
8768
8769 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02008770 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008771}
8772#endif
8773
Bram Moolenaar071d4272004-06-13 20:20:40 +00008774/*
8775 * ":redo".
8776 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008777 static void
8778ex_redo(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008779 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008780{
8781 u_redo(1);
8782}
8783
8784/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008785 * ":earlier" and ":later".
8786 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008787 static void
8788ex_later(eap)
8789 exarg_T *eap;
8790{
8791 long count = 0;
8792 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008793 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008794 char_u *p = eap->arg;
8795
8796 if (*p == NUL)
8797 count = 1;
8798 else if (isdigit(*p))
8799 {
8800 count = getdigits(&p);
8801 switch (*p)
8802 {
8803 case 's': ++p; sec = TRUE; break;
8804 case 'm': ++p; sec = TRUE; count *= 60; break;
8805 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008806 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
8807 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008808 }
8809 }
8810
8811 if (*p != NUL)
8812 EMSG2(_(e_invarg2), eap->arg);
8813 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02008814 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
8815 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008816}
8817
8818/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008819 * ":redir": start/stop redirection.
8820 */
8821 static void
8822ex_redir(eap)
8823 exarg_T *eap;
8824{
8825 char *mode;
8826 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00008827 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008828
8829 if (STRICMP(eap->arg, "END") == 0)
8830 close_redir();
8831 else
8832 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008833 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008834 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008835 ++arg;
8836 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008837 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008838 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008839 mode = "a";
8840 }
8841 else
8842 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00008843 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008844
8845 close_redir();
8846
8847 /* Expand environment variables and "~/". */
Bram Moolenaarca472992005-01-21 11:46:23 +00008848 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008849 if (fname == NULL)
8850 return;
8851#ifdef FEAT_BROWSE
8852 if (cmdmod.browse)
8853 {
8854 char_u *browseFile;
8855
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008856 browseFile = do_browse(BROWSE_SAVE,
8857 (char_u *)_("Save Redirection"),
8858 fname, NULL, NULL, BROWSE_FILTER_ALL_FILES, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008859 if (browseFile == NULL)
8860 return; /* operation cancelled */
8861 vim_free(fname);
8862 fname = browseFile;
8863 eap->forceit = TRUE; /* since dialog already asked */
8864 }
8865#endif
8866
8867 redir_fd = open_exfile(fname, eap->forceit, mode);
8868 vim_free(fname);
8869 }
8870#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00008871 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008872 {
8873 /* redirect to a register a-z (resp. A-Z for appending) */
8874 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00008875 ++arg;
8876 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008877# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00008878 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00008879 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008880# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00008881 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008882 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008883 redir_reg = *arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008884 if (*arg == '>' && arg[1] == '>') /* append */
Bram Moolenaard9d30582005-05-18 22:10:28 +00008885 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008886 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008887 {
Bram Moolenaarc188b882007-10-19 14:20:54 +00008888 /* Can use both "@a" and "@a>". */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00008889 if (*arg == '>')
8890 arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008891 /* Make register empty when not using @A-@Z and the
8892 * command is valid. */
8893 if (*arg == NUL && !isupper(redir_reg))
8894 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008895 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008896 }
8897 if (*arg != NUL)
8898 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00008899 redir_reg = 0;
Bram Moolenaard9d30582005-05-18 22:10:28 +00008900 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008901 }
8902 }
8903 else if (*arg == '=' && arg[1] == '>')
8904 {
8905 int append;
8906
8907 /* redirect to a variable */
8908 close_redir();
8909 arg += 2;
8910
8911 if (*arg == '>')
8912 {
8913 ++arg;
8914 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008915 }
8916 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008917 append = FALSE;
8918
8919 if (var_redir_start(skipwhite(arg), append) == OK)
8920 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008921 }
8922#endif
8923
8924 /* TODO: redirect to a buffer */
8925
Bram Moolenaar071d4272004-06-13 20:20:40 +00008926 else
Bram Moolenaarca472992005-01-21 11:46:23 +00008927 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008928 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00008929
8930 /* Make sure redirection is not off. Can happen for cmdline completion
8931 * that indirectly invokes a command to catch its output. */
8932 if (redir_fd != NULL
8933#ifdef FEAT_EVAL
8934 || redir_reg || redir_vname
8935#endif
8936 )
8937 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008938}
8939
8940/*
8941 * ":redraw": force redraw
8942 */
8943 static void
8944ex_redraw(eap)
8945 exarg_T *eap;
8946{
8947 int r = RedrawingDisabled;
8948 int p = p_lz;
8949
8950 RedrawingDisabled = 0;
8951 p_lz = FALSE;
8952 update_topline();
8953 update_screen(eap->forceit ? CLEAR :
8954#ifdef FEAT_VISUAL
8955 VIsual_active ? INVERTED :
8956#endif
8957 0);
8958#ifdef FEAT_TITLE
8959 if (need_maketitle)
8960 maketitle();
8961#endif
8962 RedrawingDisabled = r;
8963 p_lz = p;
8964
8965 /* Reset msg_didout, so that a message that's there is overwritten. */
8966 msg_didout = FALSE;
8967 msg_col = 0;
8968
8969 out_flush();
8970}
8971
8972/*
8973 * ":redrawstatus": force redraw of status line(s)
8974 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008975 static void
8976ex_redrawstatus(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008977 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008978{
8979#if defined(FEAT_WINDOWS)
8980 int r = RedrawingDisabled;
8981 int p = p_lz;
8982
8983 RedrawingDisabled = 0;
8984 p_lz = FALSE;
8985 if (eap->forceit)
8986 status_redraw_all();
8987 else
8988 status_redraw_curbuf();
8989 update_screen(
8990# ifdef FEAT_VISUAL
8991 VIsual_active ? INVERTED :
8992# endif
8993 0);
8994 RedrawingDisabled = r;
8995 p_lz = p;
8996 out_flush();
8997#endif
8998}
8999
9000 static void
9001close_redir()
9002{
9003 if (redir_fd != NULL)
9004 {
9005 fclose(redir_fd);
9006 redir_fd = NULL;
9007 }
9008#ifdef FEAT_EVAL
9009 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009010 if (redir_vname)
9011 {
9012 var_redir_stop();
9013 redir_vname = 0;
9014 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009015#endif
9016}
9017
9018#if defined(FEAT_SESSION) && defined(USE_CRNL)
9019# define MKSESSION_NL
9020static int mksession_nl = FALSE; /* use NL only in put_eol() */
9021#endif
9022
9023/*
9024 * ":mkexrc", ":mkvimrc", ":mkview" and ":mksession".
9025 */
9026 static void
9027ex_mkrc(eap)
9028 exarg_T *eap;
9029{
9030 FILE *fd;
9031 int failed = FALSE;
9032 char_u *fname;
9033#ifdef FEAT_BROWSE
9034 char_u *browseFile = NULL;
9035#endif
9036#ifdef FEAT_SESSION
9037 int view_session = FALSE;
9038 int using_vdir = FALSE; /* using 'viewdir'? */
9039 char_u *viewFile = NULL;
9040 unsigned *flagp;
9041#endif
9042
9043 if (eap->cmdidx == CMD_mksession || eap->cmdidx == CMD_mkview)
9044 {
9045#ifdef FEAT_SESSION
9046 view_session = TRUE;
9047#else
9048 ex_ni(eap);
9049 return;
9050#endif
9051 }
9052
9053#ifdef FEAT_SESSION
Bram Moolenaar8d594672009-07-01 18:18:57 +00009054 /* Use the short file name until ":lcd" is used. We also don't use the
9055 * short file name when 'acd' is set, that is checked later. */
Bram Moolenaareeefcc72007-05-01 21:21:21 +00009056 did_lcd = FALSE;
9057
Bram Moolenaar071d4272004-06-13 20:20:40 +00009058 /* ":mkview" or ":mkview 9": generate file name with 'viewdir' */
9059 if (eap->cmdidx == CMD_mkview
9060 && (*eap->arg == NUL
9061 || (vim_isdigit(*eap->arg) && eap->arg[1] == NUL)))
9062 {
9063 eap->forceit = TRUE;
9064 fname = get_view_file(*eap->arg);
9065 if (fname == NULL)
9066 return;
9067 viewFile = fname;
9068 using_vdir = TRUE;
9069 }
9070 else
9071#endif
9072 if (*eap->arg != NUL)
9073 fname = eap->arg;
9074 else if (eap->cmdidx == CMD_mkvimrc)
9075 fname = (char_u *)VIMRC_FILE;
9076#ifdef FEAT_SESSION
9077 else if (eap->cmdidx == CMD_mksession)
9078 fname = (char_u *)SESSION_FILE;
9079#endif
9080 else
9081 fname = (char_u *)EXRC_FILE;
9082
9083#ifdef FEAT_BROWSE
9084 if (cmdmod.browse)
9085 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00009086 browseFile = do_browse(BROWSE_SAVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009087# ifdef FEAT_SESSION
9088 eap->cmdidx == CMD_mkview ? (char_u *)_("Save View") :
9089 eap->cmdidx == CMD_mksession ? (char_u *)_("Save Session") :
9090# endif
9091 (char_u *)_("Save Setup"),
9092 fname, (char_u *)"vim", NULL, BROWSE_FILTER_MACROS, NULL);
9093 if (browseFile == NULL)
9094 goto theend;
9095 fname = browseFile;
9096 eap->forceit = TRUE; /* since dialog already asked */
9097 }
9098#endif
9099
9100#if defined(FEAT_SESSION) && defined(vim_mkdir)
9101 /* When using 'viewdir' may have to create the directory. */
9102 if (using_vdir && !mch_isdir(p_vdir))
Bram Moolenaardf177f62005-02-22 08:39:57 +00009103 vim_mkdir_emsg(p_vdir, 0755);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009104#endif
9105
9106 fd = open_exfile(fname, eap->forceit, WRITEBIN);
9107 if (fd != NULL)
9108 {
9109#ifdef FEAT_SESSION
9110 if (eap->cmdidx == CMD_mkview)
9111 flagp = &vop_flags;
9112 else
9113 flagp = &ssop_flags;
9114#endif
9115
9116#ifdef MKSESSION_NL
9117 /* "unix" in 'sessionoptions': use NL line separator */
9118 if (view_session && (*flagp & SSOP_UNIX))
9119 mksession_nl = TRUE;
9120#endif
9121
9122 /* Write the version command for :mkvimrc */
9123 if (eap->cmdidx == CMD_mkvimrc)
9124 (void)put_line(fd, "version 6.0");
9125
9126#ifdef FEAT_SESSION
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009127 if (eap->cmdidx == CMD_mksession)
9128 {
9129 if (put_line(fd, "let SessionLoad = 1") == FAIL)
9130 failed = TRUE;
9131 }
9132
Bram Moolenaar071d4272004-06-13 20:20:40 +00009133 if (eap->cmdidx != CMD_mkview)
9134#endif
9135 {
9136 /* Write setting 'compatible' first, because it has side effects.
9137 * For that same reason only do it when needed. */
9138 if (p_cp)
9139 (void)put_line(fd, "if !&cp | set cp | endif");
9140 else
9141 (void)put_line(fd, "if &cp | set nocp | endif");
9142 }
9143
9144#ifdef FEAT_SESSION
9145 if (!view_session
9146 || (eap->cmdidx == CMD_mksession
9147 && (*flagp & SSOP_OPTIONS)))
9148#endif
9149 failed |= (makemap(fd, NULL) == FAIL
9150 || makeset(fd, OPT_GLOBAL, FALSE) == FAIL);
9151
9152#ifdef FEAT_SESSION
9153 if (!failed && view_session)
9154 {
9155 if (put_line(fd, "let s:so_save = &so | let s:siso_save = &siso | set so=0 siso=0") == FAIL)
9156 failed = TRUE;
9157 if (eap->cmdidx == CMD_mksession)
9158 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02009159 char_u *dirnow; /* current directory */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009160
Bram Moolenaard9462e32011-04-11 21:35:11 +02009161 dirnow = alloc(MAXPATHL);
9162 if (dirnow == NULL)
9163 failed = TRUE;
9164 else
9165 {
9166 /*
9167 * Change to session file's dir.
9168 */
9169 if (mch_dirname(dirnow, MAXPATHL) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00009170 || mch_chdir((char *)dirnow) != 0)
Bram Moolenaard9462e32011-04-11 21:35:11 +02009171 *dirnow = NUL;
9172 if (*dirnow != NUL && (ssop_flags & SSOP_SESDIR))
9173 {
9174 if (vim_chdirfile(fname) == OK)
9175 shorten_fnames(TRUE);
9176 }
9177 else if (*dirnow != NUL
9178 && (ssop_flags & SSOP_CURDIR) && globaldir != NULL)
9179 {
9180 if (mch_chdir((char *)globaldir) == 0)
9181 shorten_fnames(TRUE);
9182 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009183
Bram Moolenaard9462e32011-04-11 21:35:11 +02009184 failed |= (makeopens(fd, dirnow) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009185
Bram Moolenaard9462e32011-04-11 21:35:11 +02009186 /* restore original dir */
9187 if (*dirnow != NUL && ((ssop_flags & SSOP_SESDIR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009188 || ((ssop_flags & SSOP_CURDIR) && globaldir != NULL)))
Bram Moolenaard9462e32011-04-11 21:35:11 +02009189 {
9190 if (mch_chdir((char *)dirnow) != 0)
9191 EMSG(_(e_prev_dir));
9192 shorten_fnames(TRUE);
9193 }
9194 vim_free(dirnow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009195 }
9196 }
9197 else
9198 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +00009199 failed |= (put_view(fd, curwin, !using_vdir, flagp,
9200 -1) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009201 }
9202 if (put_line(fd, "let &so = s:so_save | let &siso = s:siso_save")
9203 == FAIL)
9204 failed = TRUE;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009205 if (put_line(fd, "doautoall SessionLoadPost") == FAIL)
9206 failed = TRUE;
Bram Moolenaar4770d092006-01-12 23:22:24 +00009207 if (eap->cmdidx == CMD_mksession)
9208 {
9209 if (put_line(fd, "unlet SessionLoad") == FAIL)
9210 failed = TRUE;
9211 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009212 }
9213#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009214 if (put_line(fd, "\" vim: set ft=vim :") == FAIL)
9215 failed = TRUE;
9216
Bram Moolenaar071d4272004-06-13 20:20:40 +00009217 failed |= fclose(fd);
9218
9219 if (failed)
9220 EMSG(_(e_write));
9221#if defined(FEAT_EVAL) && defined(FEAT_SESSION)
9222 else if (eap->cmdidx == CMD_mksession)
9223 {
9224 /* successful session write - set this_session var */
Bram Moolenaard9462e32011-04-11 21:35:11 +02009225 char_u *tbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009226
Bram Moolenaard9462e32011-04-11 21:35:11 +02009227 tbuf = alloc(MAXPATHL);
9228 if (tbuf != NULL)
9229 {
9230 if (vim_FullName(fname, tbuf, MAXPATHL, FALSE) == OK)
9231 set_vim_var_string(VV_THIS_SESSION, tbuf, -1);
9232 vim_free(tbuf);
9233 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009234 }
9235#endif
9236#ifdef MKSESSION_NL
9237 mksession_nl = FALSE;
9238#endif
9239 }
9240
9241#ifdef FEAT_BROWSE
9242theend:
9243 vim_free(browseFile);
9244#endif
9245#ifdef FEAT_SESSION
9246 vim_free(viewFile);
9247#endif
9248}
9249
Bram Moolenaardf177f62005-02-22 08:39:57 +00009250#if ((defined(FEAT_SESSION) || defined(FEAT_EVAL)) && defined(vim_mkdir)) \
9251 || defined(PROTO)
9252 int
9253vim_mkdir_emsg(name, prot)
9254 char_u *name;
Bram Moolenaar78a15312009-05-15 19:33:18 +00009255 int prot UNUSED;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009256{
9257 if (vim_mkdir(name, prot) != 0)
9258 {
9259 EMSG2(_("E739: Cannot create directory: %s"), name);
9260 return FAIL;
9261 }
9262 return OK;
9263}
9264#endif
9265
Bram Moolenaar071d4272004-06-13 20:20:40 +00009266/*
9267 * Open a file for writing for an Ex command, with some checks.
9268 * Return file descriptor, or NULL on failure.
9269 */
9270 FILE *
9271open_exfile(fname, forceit, mode)
9272 char_u *fname;
9273 int forceit;
9274 char *mode; /* "w" for create new file or "a" for append */
9275{
9276 FILE *fd;
9277
9278#ifdef UNIX
9279 /* with Unix it is possible to open a directory */
9280 if (mch_isdir(fname))
9281 {
9282 EMSG2(_(e_isadir2), fname);
9283 return NULL;
9284 }
9285#endif
9286 if (!forceit && *mode != 'a' && vim_fexists(fname))
9287 {
9288 EMSG2(_("E189: \"%s\" exists (add ! to override)"), fname);
9289 return NULL;
9290 }
9291
9292 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
9293 EMSG2(_("E190: Cannot open \"%s\" for writing"), fname);
9294
9295 return fd;
9296}
9297
9298/*
9299 * ":mark" and ":k".
9300 */
9301 static void
9302ex_mark(eap)
9303 exarg_T *eap;
9304{
9305 pos_T pos;
9306
9307 if (*eap->arg == NUL) /* No argument? */
9308 EMSG(_(e_argreq));
9309 else if (eap->arg[1] != NUL) /* more than one character? */
9310 EMSG(_(e_trailing));
9311 else
9312 {
9313 pos = curwin->w_cursor; /* save curwin->w_cursor */
9314 curwin->w_cursor.lnum = eap->line2;
9315 beginline(BL_WHITE | BL_FIX);
9316 if (setmark(*eap->arg) == FAIL) /* set mark */
9317 EMSG(_("E191: Argument must be a letter or forward/backward quote"));
9318 curwin->w_cursor = pos; /* restore curwin->w_cursor */
9319 }
9320}
9321
9322/*
9323 * Update w_topline, w_leftcol and the cursor position.
9324 */
9325 void
9326update_topline_cursor()
9327{
9328 check_cursor(); /* put cursor on valid line */
9329 update_topline();
9330 if (!curwin->w_p_wrap)
9331 validate_cursor();
9332 update_curswant();
9333}
9334
9335#ifdef FEAT_EX_EXTRA
9336/*
9337 * ":normal[!] {commands}": Execute normal mode commands.
9338 */
9339 static void
9340ex_normal(eap)
9341 exarg_T *eap;
9342{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009343 int save_msg_scroll = msg_scroll;
9344 int save_restart_edit = restart_edit;
9345 int save_msg_didout = msg_didout;
9346 int save_State = State;
9347 tasave_T tabuf;
9348 int save_insertmode = p_im;
9349 int save_finish_op = finish_op;
Bram Moolenaar38084112008-07-26 14:05:07 +00009350 int save_opcount = opcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009351#ifdef FEAT_MBYTE
9352 char_u *arg = NULL;
9353 int l;
9354 char_u *p;
9355#endif
9356
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00009357 if (ex_normal_lock > 0)
9358 {
9359 EMSG(_(e_secure));
9360 return;
9361 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009362 if (ex_normal_busy >= p_mmd)
9363 {
9364 EMSG(_("E192: Recursive use of :normal too deep"));
9365 return;
9366 }
9367 ++ex_normal_busy;
9368
9369 msg_scroll = FALSE; /* no msg scrolling in Normal mode */
9370 restart_edit = 0; /* don't go to Insert mode */
9371 p_im = FALSE; /* don't use 'insertmode' */
9372
9373#ifdef FEAT_MBYTE
9374 /*
9375 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
9376 * this for the K_SPECIAL leading byte, otherwise special keys will not
9377 * work.
9378 */
9379 if (has_mbyte)
9380 {
9381 int len = 0;
9382
9383 /* Count the number of characters to be escaped. */
9384 for (p = eap->arg; *p != NUL; ++p)
9385 {
9386# ifdef FEAT_GUI
9387 if (*p == CSI) /* leadbyte CSI */
9388 len += 2;
9389# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009390 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009391 if (*++p == K_SPECIAL /* trailbyte K_SPECIAL or CSI */
9392# ifdef FEAT_GUI
9393 || *p == CSI
9394# endif
9395 )
9396 len += 2;
9397 }
9398 if (len > 0)
9399 {
9400 arg = alloc((unsigned)(STRLEN(eap->arg) + len + 1));
9401 if (arg != NULL)
9402 {
9403 len = 0;
9404 for (p = eap->arg; *p != NUL; ++p)
9405 {
9406 arg[len++] = *p;
9407# ifdef FEAT_GUI
9408 if (*p == CSI)
9409 {
9410 arg[len++] = KS_EXTRA;
9411 arg[len++] = (int)KE_CSI;
9412 }
9413# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009414 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009415 {
9416 arg[len++] = *++p;
9417 if (*p == K_SPECIAL)
9418 {
9419 arg[len++] = KS_SPECIAL;
9420 arg[len++] = KE_FILLER;
9421 }
9422# ifdef FEAT_GUI
9423 else if (*p == CSI)
9424 {
9425 arg[len++] = KS_EXTRA;
9426 arg[len++] = (int)KE_CSI;
9427 }
9428# endif
9429 }
9430 arg[len] = NUL;
9431 }
9432 }
9433 }
9434 }
9435#endif
9436
9437 /*
9438 * Save the current typeahead. This is required to allow using ":normal"
9439 * from an event handler and makes sure we don't hang when the argument
9440 * ends with half a command.
9441 */
9442 save_typeahead(&tabuf);
9443 if (tabuf.typebuf_valid)
9444 {
9445 /*
9446 * Repeat the :normal command for each line in the range. When no
9447 * range given, execute it just once, without positioning the cursor
9448 * first.
9449 */
9450 do
9451 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009452 if (eap->addr_count != 0)
9453 {
9454 curwin->w_cursor.lnum = eap->line1++;
9455 curwin->w_cursor.col = 0;
9456 }
9457
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009458 exec_normal_cmd(
Bram Moolenaar071d4272004-06-13 20:20:40 +00009459#ifdef FEAT_MBYTE
9460 arg != NULL ? arg :
9461#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009462 eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009463 }
9464 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
9465 }
9466
9467 /* Might not return to the main loop when in an event handler. */
9468 update_topline_cursor();
9469
9470 /* Restore the previous typeahead. */
9471 restore_typeahead(&tabuf);
9472
9473 --ex_normal_busy;
9474 msg_scroll = save_msg_scroll;
9475 restart_edit = save_restart_edit;
9476 p_im = save_insertmode;
9477 finish_op = save_finish_op;
Bram Moolenaar38084112008-07-26 14:05:07 +00009478 opcount = save_opcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009479 msg_didout |= save_msg_didout; /* don't reset msg_didout now */
9480
9481 /* Restore the state (needed when called from a function executed for
9482 * 'indentexpr'). */
9483 State = save_State;
9484#ifdef FEAT_MBYTE
9485 vim_free(arg);
9486#endif
9487}
9488
9489/*
Bram Moolenaar64969662005-12-14 21:59:55 +00009490 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009491 */
9492 static void
9493ex_startinsert(eap)
9494 exarg_T *eap;
9495{
Bram Moolenaarfd371682005-01-14 21:42:54 +00009496 if (eap->forceit)
9497 {
9498 coladvance((colnr_T)MAXCOL);
9499 curwin->w_curswant = MAXCOL;
9500 curwin->w_set_curswant = FALSE;
9501 }
9502
Bram Moolenaara40c5002005-01-09 21:16:21 +00009503 /* Ignore the command when already in Insert mode. Inserting an
9504 * expression register that invokes a function can do this. */
9505 if (State & INSERT)
9506 return;
9507
Bram Moolenaar64969662005-12-14 21:59:55 +00009508 if (eap->cmdidx == CMD_startinsert)
9509 restart_edit = 'a';
9510 else if (eap->cmdidx == CMD_startreplace)
9511 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00009512 else
Bram Moolenaar64969662005-12-14 21:59:55 +00009513 restart_edit = 'V';
9514
9515 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009516 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009517 if (eap->cmdidx == CMD_startinsert)
9518 restart_edit = 'i';
Bram Moolenaar071d4272004-06-13 20:20:40 +00009519 curwin->w_curswant = 0; /* avoid MAXCOL */
9520 }
9521}
9522
9523/*
9524 * ":stopinsert"
9525 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009526 static void
9527ex_stopinsert(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009528 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009529{
9530 restart_edit = 0;
9531 stop_insert_mode = TRUE;
9532}
9533#endif
9534
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009535#if defined(FEAT_EX_EXTRA) || defined(FEAT_MENU) || defined(PROTO)
9536/*
9537 * Execute normal mode command "cmd".
9538 * "remap" can be REMAP_NONE or REMAP_YES.
9539 */
9540 void
9541exec_normal_cmd(cmd, remap, silent)
9542 char_u *cmd;
9543 int remap;
9544 int silent;
9545{
9546 oparg_T oa;
9547
9548 /*
9549 * Stuff the argument into the typeahead buffer.
9550 * Execute normal_cmd() until there is no typeahead left.
9551 */
9552 clear_oparg(&oa);
9553 finish_op = FALSE;
9554 ins_typebuf(cmd, remap, 0, TRUE, silent);
9555 while ((!stuff_empty() || (!typebuf_typed() && typebuf.tb_len > 0))
9556 && !got_int)
9557 {
9558 update_topline_cursor();
Bram Moolenaar48ac02c2011-01-17 19:50:06 +01009559 normal_cmd(&oa, TRUE); /* execute a Normal mode cmd */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009560 }
9561}
9562#endif
9563
Bram Moolenaar071d4272004-06-13 20:20:40 +00009564#ifdef FEAT_FIND_ID
9565 static void
9566ex_checkpath(eap)
9567 exarg_T *eap;
9568{
9569 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
9570 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
9571 (linenr_T)1, (linenr_T)MAXLNUM);
9572}
9573
9574#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
9575/*
9576 * ":psearch"
9577 */
9578 static void
9579ex_psearch(eap)
9580 exarg_T *eap;
9581{
9582 g_do_tagpreview = p_pvh;
9583 ex_findpat(eap);
9584 g_do_tagpreview = 0;
9585}
9586#endif
9587
9588 static void
9589ex_findpat(eap)
9590 exarg_T *eap;
9591{
9592 int whole = TRUE;
9593 long n;
9594 char_u *p;
9595 int action;
9596
9597 switch (cmdnames[eap->cmdidx].cmd_name[2])
9598 {
9599 case 'e': /* ":psearch", ":isearch" and ":dsearch" */
9600 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
9601 action = ACTION_GOTO;
9602 else
9603 action = ACTION_SHOW;
9604 break;
9605 case 'i': /* ":ilist" and ":dlist" */
9606 action = ACTION_SHOW_ALL;
9607 break;
9608 case 'u': /* ":ijump" and ":djump" */
9609 action = ACTION_GOTO;
9610 break;
9611 default: /* ":isplit" and ":dsplit" */
9612 action = ACTION_SPLIT;
9613 break;
9614 }
9615
9616 n = 1;
9617 if (vim_isdigit(*eap->arg)) /* get count */
9618 {
9619 n = getdigits(&eap->arg);
9620 eap->arg = skipwhite(eap->arg);
9621 }
9622 if (*eap->arg == '/') /* Match regexp, not just whole words */
9623 {
9624 whole = FALSE;
9625 ++eap->arg;
9626 p = skip_regexp(eap->arg, '/', p_magic, NULL);
9627 if (*p)
9628 {
9629 *p++ = NUL;
9630 p = skipwhite(p);
9631
9632 /* Check for trailing illegal characters */
9633 if (!ends_excmd(*p))
9634 eap->errmsg = e_trailing;
9635 else
9636 eap->nextcmd = check_nextcmd(p);
9637 }
9638 }
9639 if (!eap->skip)
9640 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
9641 whole, !eap->forceit,
9642 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
9643 n, action, eap->line1, eap->line2);
9644}
9645#endif
9646
9647#ifdef FEAT_WINDOWS
9648
9649# ifdef FEAT_QUICKFIX
9650/*
9651 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
9652 */
9653 static void
9654ex_ptag(eap)
9655 exarg_T *eap;
9656{
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01009657 g_do_tagpreview = p_pvh; /* will be reset to 0 in ex_tag_cmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009658 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9659}
9660
9661/*
9662 * ":pedit"
9663 */
9664 static void
9665ex_pedit(eap)
9666 exarg_T *eap;
9667{
9668 win_T *curwin_save = curwin;
9669
9670 g_do_tagpreview = p_pvh;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00009671 prepare_tagpreview(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009672 keep_help_flag = curwin_save->w_buffer->b_help;
9673 do_exedit(eap, NULL);
9674 keep_help_flag = FALSE;
9675 if (curwin != curwin_save && win_valid(curwin_save))
9676 {
9677 /* Return cursor to where we were */
9678 validate_cursor();
9679 redraw_later(VALID);
9680 win_enter(curwin_save, TRUE);
9681 }
9682 g_do_tagpreview = 0;
9683}
9684# endif
9685
9686/*
9687 * ":stag", ":stselect" and ":stjump".
9688 */
9689 static void
9690ex_stag(eap)
9691 exarg_T *eap;
9692{
9693 postponed_split = -1;
9694 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009695 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009696 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9697 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009698 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009699}
9700#endif
9701
9702/*
9703 * ":tag", ":tselect", ":tjump", ":tnext", etc.
9704 */
9705 static void
9706ex_tag(eap)
9707 exarg_T *eap;
9708{
9709 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
9710}
9711
9712 static void
9713ex_tag_cmd(eap, name)
9714 exarg_T *eap;
9715 char_u *name;
9716{
9717 int cmd;
9718
9719 switch (name[1])
9720 {
9721 case 'j': cmd = DT_JUMP; /* ":tjump" */
9722 break;
9723 case 's': cmd = DT_SELECT; /* ":tselect" */
9724 break;
9725 case 'p': cmd = DT_PREV; /* ":tprevious" */
9726 break;
9727 case 'N': cmd = DT_PREV; /* ":tNext" */
9728 break;
9729 case 'n': cmd = DT_NEXT; /* ":tnext" */
9730 break;
9731 case 'o': cmd = DT_POP; /* ":pop" */
9732 break;
9733 case 'f': /* ":tfirst" */
9734 case 'r': cmd = DT_FIRST; /* ":trewind" */
9735 break;
9736 case 'l': cmd = DT_LAST; /* ":tlast" */
9737 break;
9738 default: /* ":tag" */
9739#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +00009740 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009741 {
9742 do_cstag(eap);
9743 return;
9744 }
9745#endif
9746 cmd = DT_TAG;
9747 break;
9748 }
9749
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00009750 if (name[0] == 'l')
9751 {
9752#ifndef FEAT_QUICKFIX
9753 ex_ni(eap);
9754 return;
9755#else
9756 cmd = DT_LTAG;
9757#endif
9758 }
9759
Bram Moolenaar071d4272004-06-13 20:20:40 +00009760 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
9761 eap->forceit, TRUE);
9762}
9763
9764/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009765 * Check "str" for starting with a special cmdline variable.
9766 * If found return one of the SPEC_ values and set "*usedlen" to the length of
9767 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
9768 */
9769 int
9770find_cmdline_var(src, usedlen)
9771 char_u *src;
9772 int *usedlen;
9773{
9774 int len;
9775 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +00009776 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009777 "%",
9778#define SPEC_PERC 0
9779 "#",
9780#define SPEC_HASH 1
9781 "<cword>", /* cursor word */
9782#define SPEC_CWORD 2
9783 "<cWORD>", /* cursor WORD */
9784#define SPEC_CCWORD 3
9785 "<cfile>", /* cursor path name */
9786#define SPEC_CFILE 4
9787 "<sfile>", /* ":so" file name */
9788#define SPEC_SFILE 5
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009789 "<slnum>", /* ":so" file line number */
9790#define SPEC_SLNUM 6
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009791#ifdef FEAT_AUTOCMD
9792 "<afile>", /* autocommand file name */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009793# define SPEC_AFILE 7
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009794 "<abuf>", /* autocommand buffer number */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009795# define SPEC_ABUF 8
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009796 "<amatch>", /* autocommand match name */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009797# define SPEC_AMATCH 9
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009798#endif
9799#ifdef FEAT_CLIENTSERVER
9800 "<client>"
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009801# ifdef FEAT_AUTOCMD
9802# define SPEC_CLIENT 10
9803# else
9804# define SPEC_CLIENT 7
9805# endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009806#endif
9807 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009808
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009809 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009810 {
9811 len = (int)STRLEN(spec_str[i]);
9812 if (STRNCMP(src, spec_str[i], len) == 0)
9813 {
9814 *usedlen = len;
9815 return i;
9816 }
9817 }
9818 return -1;
9819}
9820
9821/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009822 * Evaluate cmdline variables.
9823 *
9824 * change '%' to curbuf->b_ffname
9825 * '#' to curwin->w_altfile
9826 * '<cword>' to word under the cursor
9827 * '<cWORD>' to WORD under the cursor
9828 * '<cfile>' to path name under the cursor
9829 * '<sfile>' to sourced file name
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009830 * '<slnum>' to sourced file line number
Bram Moolenaar071d4272004-06-13 20:20:40 +00009831 * '<afile>' to file name for autocommand
9832 * '<abuf>' to buffer number for autocommand
9833 * '<amatch>' to matching name for autocommand
9834 *
9835 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
9836 * "" for error without a message) and NULL is returned.
9837 * Returns an allocated string if a valid match was found.
9838 * Returns NULL if no match was found. "usedlen" then still contains the
9839 * number of characters to skip.
9840 */
9841 char_u *
Bram Moolenaar63b92542007-03-27 14:57:09 +00009842eval_vars(src, srcstart, usedlen, lnump, errormsg, escaped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009843 char_u *src; /* pointer into commandline */
Bram Moolenaar63b92542007-03-27 14:57:09 +00009844 char_u *srcstart; /* beginning of valid memory for src */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009845 int *usedlen; /* characters after src that are used */
9846 linenr_T *lnump; /* line number for :e command, or NULL */
9847 char_u **errormsg; /* pointer to error message */
Bram Moolenaar63b92542007-03-27 14:57:09 +00009848 int *escaped; /* return value has escaped white space (can
9849 * be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009850{
9851 int i;
9852 char_u *s;
9853 char_u *result;
9854 char_u *resultbuf = NULL;
9855 int resultlen;
9856 buf_T *buf;
9857 int valid = VALID_HEAD + VALID_PATH; /* assume valid result */
9858 int spec_idx;
9859#ifdef FEAT_MODIFY_FNAME
9860 int skip_mod = FALSE;
9861#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009862 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009863
9864 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009865 if (escaped != NULL)
9866 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009867
9868 /*
9869 * Check if there is something to do.
9870 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009871 spec_idx = find_cmdline_var(src, usedlen);
9872 if (spec_idx < 0) /* no match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009873 {
9874 *usedlen = 1;
9875 return NULL;
9876 }
9877
9878 /*
9879 * Skip when preceded with a backslash "\%" and "\#".
9880 * Note: In "\\%" the % is also not recognized!
9881 */
9882 if (src > srcstart && src[-1] == '\\')
9883 {
9884 *usedlen = 0;
Bram Moolenaara7241f52008-06-24 20:39:31 +00009885 STRMOVE(src - 1, src); /* remove backslash */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009886 return NULL;
9887 }
9888
9889 /*
9890 * word or WORD under cursor
9891 */
9892 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD)
9893 {
9894 resultlen = find_ident_under_cursor(&result, spec_idx == SPEC_CWORD ?
9895 (FIND_IDENT|FIND_STRING) : FIND_STRING);
9896 if (resultlen == 0)
9897 {
9898 *errormsg = (char_u *)"";
9899 return NULL;
9900 }
9901 }
9902
9903 /*
9904 * '#': Alternate file name
9905 * '%': Current file name
9906 * File name under the cursor
9907 * File name for autocommand
9908 * and following modifiers
9909 */
9910 else
9911 {
9912 switch (spec_idx)
9913 {
9914 case SPEC_PERC: /* '%': current file */
9915 if (curbuf->b_fname == NULL)
9916 {
9917 result = (char_u *)"";
9918 valid = 0; /* Must have ":p:h" to be valid */
9919 }
9920 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009921 result = curbuf->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009922 break;
9923
9924 case SPEC_HASH: /* '#' or "#99": alternate file */
9925 if (src[1] == '#') /* "##": the argument list */
9926 {
9927 result = arg_all();
9928 resultbuf = result;
9929 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009930 if (escaped != NULL)
9931 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009932#ifdef FEAT_MODIFY_FNAME
9933 skip_mod = TRUE;
9934#endif
9935 break;
9936 }
9937 s = src + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00009938 if (*s == '<') /* "#<99" uses v:oldfiles */
9939 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009940 i = (int)getdigits(&s);
9941 *usedlen = (int)(s - src); /* length of what we expand */
9942
Bram Moolenaard812df62008-11-09 12:46:09 +00009943 if (src[1] == '<')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009944 {
Bram Moolenaard812df62008-11-09 12:46:09 +00009945 if (*usedlen < 2)
9946 {
9947 /* Should we give an error message for #<text? */
9948 *usedlen = 1;
9949 return NULL;
9950 }
9951#ifdef FEAT_EVAL
9952 result = list_find_str(get_vim_var_list(VV_OLDFILES),
9953 (long)i);
9954 if (result == NULL)
9955 {
9956 *errormsg = (char_u *)"";
9957 return NULL;
9958 }
9959#else
9960 *errormsg = (char_u *)_("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009961 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00009962#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009963 }
9964 else
Bram Moolenaard812df62008-11-09 12:46:09 +00009965 {
9966 buf = buflist_findnr(i);
9967 if (buf == NULL)
9968 {
9969 *errormsg = (char_u *)_("E194: No alternate file name to substitute for '#'");
9970 return NULL;
9971 }
9972 if (lnump != NULL)
9973 *lnump = ECMD_LAST;
9974 if (buf->b_fname == NULL)
9975 {
9976 result = (char_u *)"";
9977 valid = 0; /* Must have ":p:h" to be valid */
9978 }
9979 else
9980 result = buf->b_fname;
9981 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009982 break;
9983
9984#ifdef FEAT_SEARCHPATH
9985 case SPEC_CFILE: /* file name under cursor */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009986 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009987 if (result == NULL)
9988 {
9989 *errormsg = (char_u *)"";
9990 return NULL;
9991 }
9992 resultbuf = result; /* remember allocated string */
9993 break;
9994#endif
9995
9996#ifdef FEAT_AUTOCMD
9997 case SPEC_AFILE: /* file name for autocommand */
9998 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +00009999 if (result != NULL && !autocmd_fname_full)
10000 {
10001 /* Still need to turn the fname into a full path. It is
10002 * postponed to avoid a delay when <afile> is not used. */
10003 autocmd_fname_full = TRUE;
10004 result = FullName_save(autocmd_fname, FALSE);
10005 vim_free(autocmd_fname);
10006 autocmd_fname = result;
10007 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010008 if (result == NULL)
10009 {
10010 *errormsg = (char_u *)_("E495: no autocommand file name to substitute for \"<afile>\"");
10011 return NULL;
10012 }
Bram Moolenaara0174af2008-01-02 20:08:25 +000010013 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010014 break;
10015
10016 case SPEC_ABUF: /* buffer number for autocommand */
10017 if (autocmd_bufnr <= 0)
10018 {
10019 *errormsg = (char_u *)_("E496: no autocommand buffer number to substitute for \"<abuf>\"");
10020 return NULL;
10021 }
10022 sprintf((char *)strbuf, "%d", autocmd_bufnr);
10023 result = strbuf;
10024 break;
10025
10026 case SPEC_AMATCH: /* match name for autocommand */
10027 result = autocmd_match;
10028 if (result == NULL)
10029 {
10030 *errormsg = (char_u *)_("E497: no autocommand match name to substitute for \"<amatch>\"");
10031 return NULL;
10032 }
10033 break;
10034
10035#endif
10036 case SPEC_SFILE: /* file name for ":so" command */
10037 result = sourcing_name;
10038 if (result == NULL)
10039 {
10040 *errormsg = (char_u *)_("E498: no :source file name to substitute for \"<sfile>\"");
10041 return NULL;
10042 }
10043 break;
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010044 case SPEC_SLNUM: /* line in file for ":so" command */
10045 if (sourcing_name == NULL || sourcing_lnum == 0)
10046 {
10047 *errormsg = (char_u *)_("E842: no line number to use for \"<slnum>\"");
10048 return NULL;
10049 }
10050 sprintf((char *)strbuf, "%ld", (long)sourcing_lnum);
10051 result = strbuf;
10052 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010053#if defined(FEAT_CLIENTSERVER)
10054 case SPEC_CLIENT: /* Source of last submitted input */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010055 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
10056 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010057 result = strbuf;
10058 break;
10059#endif
10060 }
10061
10062 resultlen = (int)STRLEN(result); /* length of new string */
10063 if (src[*usedlen] == '<') /* remove the file name extension */
10064 {
10065 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010066 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010067 resultlen = (int)(s - result);
10068 }
10069#ifdef FEAT_MODIFY_FNAME
10070 else if (!skip_mod)
10071 {
10072 valid |= modify_fname(src, usedlen, &result, &resultbuf,
10073 &resultlen);
10074 if (result == NULL)
10075 {
10076 *errormsg = (char_u *)"";
10077 return NULL;
10078 }
10079 }
10080#endif
10081 }
10082
10083 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
10084 {
10085 if (valid != VALID_HEAD + VALID_PATH)
10086 /* xgettext:no-c-format */
10087 *errormsg = (char_u *)_("E499: Empty file name for '%' or '#', only works with \":p:h\"");
10088 else
10089 *errormsg = (char_u *)_("E500: Evaluates to an empty string");
10090 result = NULL;
10091 }
10092 else
10093 result = vim_strnsave(result, resultlen);
10094 vim_free(resultbuf);
10095 return result;
10096}
10097
10098/*
10099 * Concatenate all files in the argument list, separated by spaces, and return
10100 * it in one allocated string.
10101 * Spaces and backslashes in the file names are escaped with a backslash.
10102 * Returns NULL when out of memory.
10103 */
10104 static char_u *
10105arg_all()
10106{
10107 int len;
10108 int idx;
10109 char_u *retval = NULL;
10110 char_u *p;
10111
10112 /*
10113 * Do this loop two times:
10114 * first time: compute the total length
10115 * second time: concatenate the names
10116 */
10117 for (;;)
10118 {
10119 len = 0;
10120 for (idx = 0; idx < ARGCOUNT; ++idx)
10121 {
10122 p = alist_name(&ARGLIST[idx]);
10123 if (p != NULL)
10124 {
10125 if (len > 0)
10126 {
10127 /* insert a space in between names */
10128 if (retval != NULL)
10129 retval[len] = ' ';
10130 ++len;
10131 }
10132 for ( ; *p != NUL; ++p)
10133 {
10134 if (*p == ' ' || *p == '\\')
10135 {
10136 /* insert a backslash */
10137 if (retval != NULL)
10138 retval[len] = '\\';
10139 ++len;
10140 }
10141 if (retval != NULL)
10142 retval[len] = *p;
10143 ++len;
10144 }
10145 }
10146 }
10147
10148 /* second time: break here */
10149 if (retval != NULL)
10150 {
10151 retval[len] = NUL;
10152 break;
10153 }
10154
10155 /* allocate memory */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000010156 retval = alloc((unsigned)len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010157 if (retval == NULL)
10158 break;
10159 }
10160
10161 return retval;
10162}
10163
10164#if defined(FEAT_AUTOCMD) || defined(PROTO)
10165/*
10166 * Expand the <sfile> string in "arg".
10167 *
10168 * Returns an allocated string, or NULL for any error.
10169 */
10170 char_u *
10171expand_sfile(arg)
10172 char_u *arg;
10173{
10174 char_u *errormsg;
10175 int len;
10176 char_u *result;
10177 char_u *newres;
10178 char_u *repl;
10179 int srclen;
10180 char_u *p;
10181
10182 result = vim_strsave(arg);
10183 if (result == NULL)
10184 return NULL;
10185
10186 for (p = result; *p; )
10187 {
10188 if (STRNCMP(p, "<sfile>", 7) != 0)
10189 ++p;
10190 else
10191 {
10192 /* replace "<sfile>" with the sourced file name, and do ":" stuff */
Bram Moolenaar63b92542007-03-27 14:57:09 +000010193 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010194 if (errormsg != NULL)
10195 {
10196 if (*errormsg)
10197 emsg(errormsg);
10198 vim_free(result);
10199 return NULL;
10200 }
10201 if (repl == NULL) /* no match (cannot happen) */
10202 {
10203 p += srclen;
10204 continue;
10205 }
10206 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
10207 newres = alloc(len);
10208 if (newres == NULL)
10209 {
10210 vim_free(repl);
10211 vim_free(result);
10212 return NULL;
10213 }
10214 mch_memmove(newres, result, (size_t)(p - result));
10215 STRCPY(newres + (p - result), repl);
10216 len = (int)STRLEN(newres);
10217 STRCAT(newres, p + srclen);
10218 vim_free(repl);
10219 vim_free(result);
10220 result = newres;
10221 p = newres + len; /* continue after the match */
10222 }
10223 }
10224
10225 return result;
10226}
10227#endif
10228
10229#ifdef FEAT_SESSION
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010230static int ses_winsizes __ARGS((FILE *fd, int restore_size,
10231 win_T *tab_firstwin));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010232static int ses_win_rec __ARGS((FILE *fd, frame_T *fr));
10233static frame_T *ses_skipframe __ARGS((frame_T *fr));
10234static int ses_do_frame __ARGS((frame_T *fr));
10235static int ses_do_win __ARGS((win_T *wp));
10236static int ses_arglist __ARGS((FILE *fd, char *cmd, garray_T *gap, int fullname, unsigned *flagp));
10237static int ses_put_fname __ARGS((FILE *fd, char_u *name, unsigned *flagp));
10238static int ses_fname __ARGS((FILE *fd, buf_T *buf, unsigned *flagp));
10239
10240/*
10241 * Write openfile commands for the current buffers to an .exrc file.
10242 * Return FAIL on error, OK otherwise.
10243 */
10244 static int
10245makeopens(fd, dirnow)
10246 FILE *fd;
10247 char_u *dirnow; /* Current directory name */
10248{
10249 buf_T *buf;
10250 int only_save_windows = TRUE;
10251 int nr;
10252 int cnr = 1;
10253 int restore_size = TRUE;
10254 win_T *wp;
10255 char_u *sname;
10256 win_T *edited_win = NULL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010257 int tabnr;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010258 win_T *tab_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010259 frame_T *tab_topframe;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010260 int cur_arg_idx = 0;
Bram Moolenaar383c6f52008-01-04 15:01:07 +000010261 int next_arg_idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010262
10263 if (ssop_flags & SSOP_BUFFERS)
10264 only_save_windows = FALSE; /* Save ALL buffers */
10265
10266 /*
10267 * Begin by setting the this_session variable, and then other
10268 * sessionable variables.
10269 */
10270#ifdef FEAT_EVAL
10271 if (put_line(fd, "let v:this_session=expand(\"<sfile>:p\")") == FAIL)
10272 return FAIL;
10273 if (ssop_flags & SSOP_GLOBALS)
10274 if (store_session_globals(fd) == FAIL)
10275 return FAIL;
10276#endif
10277
10278 /*
10279 * Close all windows but one.
10280 */
10281 if (put_line(fd, "silent only") == FAIL)
10282 return FAIL;
10283
10284 /*
10285 * Now a :cd command to the session directory or the current directory
10286 */
10287 if (ssop_flags & SSOP_SESDIR)
10288 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010289 if (put_line(fd, "exe \"cd \" . escape(expand(\"<sfile>:p:h\"), ' ')")
10290 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010291 return FAIL;
10292 }
10293 else if (ssop_flags & SSOP_CURDIR)
10294 {
10295 sname = home_replace_save(NULL, globaldir != NULL ? globaldir : dirnow);
10296 if (sname == NULL
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010297 || fputs("cd ", fd) < 0
10298 || ses_put_fname(fd, sname, &ssop_flags) == FAIL
10299 || put_eol(fd) == FAIL)
10300 {
10301 vim_free(sname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010302 return FAIL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010303 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010304 vim_free(sname);
10305 }
10306
10307 /*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010308 * If there is an empty, unnamed buffer we will wipe it out later.
10309 * Remember the buffer number.
10310 */
10311 if (put_line(fd, "if expand('%') == '' && !&modified && line('$') <= 1 && getline(1) == ''") == FAIL)
10312 return FAIL;
10313 if (put_line(fd, " let s:wipebuf = bufnr('%')") == FAIL)
10314 return FAIL;
10315 if (put_line(fd, "endif") == FAIL)
10316 return FAIL;
10317
10318 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010319 * Now save the current files, current buffer first.
10320 */
10321 if (put_line(fd, "set shortmess=aoO") == FAIL)
10322 return FAIL;
10323
10324 /* Now put the other buffers into the buffer list */
10325 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
10326 {
10327 if (!(only_save_windows && buf->b_nwindows == 0)
10328 && !(buf->b_help && !(ssop_flags & SSOP_HELP))
10329 && buf->b_fname != NULL
10330 && buf->b_p_bl)
10331 {
10332 if (fprintf(fd, "badd +%ld ", buf->b_wininfo == NULL ? 1L
10333 : buf->b_wininfo->wi_fpos.lnum) < 0
10334 || ses_fname(fd, buf, &ssop_flags) == FAIL)
10335 return FAIL;
10336 }
10337 }
10338
10339 /* the global argument list */
10340 if (ses_arglist(fd, "args", &global_alist.al_ga,
10341 !(ssop_flags & SSOP_CURDIR), &ssop_flags) == FAIL)
10342 return FAIL;
10343
10344 if (ssop_flags & SSOP_RESIZE)
10345 {
10346 /* Note: after the restore we still check it worked!*/
10347 if (fprintf(fd, "set lines=%ld columns=%ld" , Rows, Columns) < 0
10348 || put_eol(fd) == FAIL)
10349 return FAIL;
10350 }
10351
10352#ifdef FEAT_GUI
10353 if (gui.in_use && (ssop_flags & SSOP_WINPOS))
10354 {
10355 int x, y;
10356
10357 if (gui_mch_get_winpos(&x, &y) == OK)
10358 {
10359 /* Note: after the restore we still check it worked!*/
10360 if (fprintf(fd, "winpos %d %d", x, y) < 0 || put_eol(fd) == FAIL)
10361 return FAIL;
10362 }
10363 }
10364#endif
10365
10366 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000010367 * May repeat putting Windows for each tab, when "tabpages" is in
10368 * 'sessionoptions'.
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010369 * Don't use goto_tabpage(), it may change directory and trigger
10370 * autocommands.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010371 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010372 tab_firstwin = firstwin; /* first window in tab page "tabnr" */
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010373 tab_topframe = topframe;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010374 for (tabnr = 1; ; ++tabnr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010375 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010376 int need_tabnew = FALSE;
10377
Bram Moolenaar18144c82006-04-12 21:52:12 +000010378 if ((ssop_flags & SSOP_TABPAGES))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010379 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010380 tabpage_T *tp = find_tabpage(tabnr);
10381
10382 if (tp == NULL)
10383 break; /* done all tab pages */
10384 if (tp == curtab)
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010385 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010386 tab_firstwin = firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010387 tab_topframe = topframe;
10388 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010389 else
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010390 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010391 tab_firstwin = tp->tp_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010392 tab_topframe = tp->tp_topframe;
10393 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010394 if (tabnr > 1)
10395 need_tabnew = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010396 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010397
Bram Moolenaar18144c82006-04-12 21:52:12 +000010398 /*
10399 * Before creating the window layout, try loading one file. If this
10400 * is aborted we don't end up with a number of useless windows.
10401 * This may have side effects! (e.g., compressed or network file).
10402 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010403 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010404 {
10405 if (ses_do_win(wp)
10406 && wp->w_buffer->b_ffname != NULL
10407 && !wp->w_buffer->b_help
10408#ifdef FEAT_QUICKFIX
10409 && !bt_nofile(wp->w_buffer)
10410#endif
10411 )
10412 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010413 if (fputs(need_tabnew ? "tabedit " : "edit ", fd) < 0
Bram Moolenaar18144c82006-04-12 21:52:12 +000010414 || ses_fname(fd, wp->w_buffer, &ssop_flags) == FAIL)
10415 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010416 need_tabnew = FALSE;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010417 if (!wp->w_arg_idx_invalid)
10418 edited_win = wp;
10419 break;
10420 }
10421 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010422
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010423 /* If no file got edited create an empty tab page. */
10424 if (need_tabnew && put_line(fd, "tabnew") == FAIL)
10425 return FAIL;
10426
Bram Moolenaar18144c82006-04-12 21:52:12 +000010427 /*
10428 * Save current window layout.
10429 */
10430 if (put_line(fd, "set splitbelow splitright") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010431 return FAIL;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010432 if (ses_win_rec(fd, tab_topframe) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010433 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010434 if (!p_sb && put_line(fd, "set nosplitbelow") == FAIL)
10435 return FAIL;
10436 if (!p_spr && put_line(fd, "set nosplitright") == FAIL)
10437 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010438
Bram Moolenaar18144c82006-04-12 21:52:12 +000010439 /*
10440 * Check if window sizes can be restored (no windows omitted).
10441 * Remember the window number of the current window after restoring.
10442 */
10443 nr = 0;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010444 for (wp = tab_firstwin; wp != NULL; wp = W_NEXT(wp))
Bram Moolenaar18144c82006-04-12 21:52:12 +000010445 {
10446 if (ses_do_win(wp))
10447 ++nr;
10448 else
10449 restore_size = FALSE;
10450 if (curwin == wp)
10451 cnr = nr;
10452 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010453
Bram Moolenaar18144c82006-04-12 21:52:12 +000010454 /* Go to the first window. */
10455 if (put_line(fd, "wincmd t") == FAIL)
10456 return FAIL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010457
Bram Moolenaar18144c82006-04-12 21:52:12 +000010458 /*
10459 * If more than one window, see if sizes can be restored.
10460 * First set 'winheight' and 'winwidth' to 1 to avoid the windows being
10461 * resized when moving between windows.
10462 * Do this before restoring the view, so that the topline and the
10463 * cursor can be set. This is done again below.
10464 */
10465 if (put_line(fd, "set winheight=1 winwidth=1") == FAIL)
10466 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010467 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010468 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010469
Bram Moolenaar18144c82006-04-12 21:52:12 +000010470 /*
10471 * Restore the view of the window (options, file, cursor, etc.).
10472 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010473 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010474 {
10475 if (!ses_do_win(wp))
10476 continue;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010477 if (put_view(fd, wp, wp != edited_win, &ssop_flags,
10478 cur_arg_idx) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010479 return FAIL;
10480 if (nr > 1 && put_line(fd, "wincmd w") == FAIL)
10481 return FAIL;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010482 next_arg_idx = wp->w_arg_idx;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010483 }
10484
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010485 /* The argument index in the first tab page is zero, need to set it in
10486 * each window. For further tab pages it's the window where we do
10487 * "tabedit". */
10488 cur_arg_idx = next_arg_idx;
10489
Bram Moolenaar18144c82006-04-12 21:52:12 +000010490 /*
10491 * Restore cursor to the current window if it's not the first one.
10492 */
10493 if (cnr > 1 && (fprintf(fd, "%dwincmd w", cnr) < 0
10494 || put_eol(fd) == FAIL))
10495 return FAIL;
10496
10497 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000010498 * Restore window sizes again after jumping around in windows, because
10499 * the current window has a minimum size while others may not.
10500 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010501 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010502 return FAIL;
10503
Bram Moolenaar18144c82006-04-12 21:52:12 +000010504 /* Don't continue in another tab page when doing only the current one
10505 * or when at the last tab page. */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010506 if (!(ssop_flags & SSOP_TABPAGES))
Bram Moolenaar18144c82006-04-12 21:52:12 +000010507 break;
10508 }
10509
10510 if (ssop_flags & SSOP_TABPAGES)
10511 {
Bram Moolenaar18144c82006-04-12 21:52:12 +000010512 if (fprintf(fd, "tabnext %d", tabpage_index(curtab)) < 0
10513 || put_eol(fd) == FAIL)
10514 return FAIL;
10515 }
10516
Bram Moolenaar9c102382006-05-03 21:26:49 +000010517 /*
10518 * Wipe out an empty unnamed buffer we started in.
10519 */
10520 if (put_line(fd, "if exists('s:wipebuf')") == FAIL)
10521 return FAIL;
Bram Moolenaarf3a67882006-05-05 21:09:41 +000010522 if (put_line(fd, " silent exe 'bwipe ' . s:wipebuf") == FAIL)
Bram Moolenaar9c102382006-05-03 21:26:49 +000010523 return FAIL;
10524 if (put_line(fd, "endif") == FAIL)
10525 return FAIL;
10526 if (put_line(fd, "unlet! s:wipebuf") == FAIL)
10527 return FAIL;
10528
10529 /* Re-apply 'winheight', 'winwidth' and 'shortmess'. */
10530 if (fprintf(fd, "set winheight=%ld winwidth=%ld shortmess=%s",
10531 p_wh, p_wiw, p_shm) < 0 || put_eol(fd) == FAIL)
10532 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010533
10534 /*
10535 * Lastly, execute the x.vim file if it exists.
10536 */
10537 if (put_line(fd, "let s:sx = expand(\"<sfile>:p:r\").\"x.vim\"") == FAIL
10538 || put_line(fd, "if file_readable(s:sx)") == FAIL
Bram Moolenaar42ba1262008-12-09 10:18:03 +000010539 || put_line(fd, " exe \"source \" . fnameescape(s:sx)") == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000010540 || put_line(fd, "endif") == FAIL)
10541 return FAIL;
10542
10543 return OK;
10544}
10545
10546 static int
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010547ses_winsizes(fd, restore_size, tab_firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010548 FILE *fd;
10549 int restore_size;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010550 win_T *tab_firstwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010551{
10552 int n = 0;
10553 win_T *wp;
10554
10555 if (restore_size && (ssop_flags & SSOP_WINSIZE))
10556 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010557 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010558 {
10559 if (!ses_do_win(wp))
10560 continue;
10561 ++n;
10562
10563 /* restore height when not full height */
10564 if (wp->w_height + wp->w_status_height < topframe->fr_height
10565 && (fprintf(fd,
10566 "exe '%dresize ' . ((&lines * %ld + %ld) / %ld)",
10567 n, (long)wp->w_height, Rows / 2, Rows) < 0
10568 || put_eol(fd) == FAIL))
10569 return FAIL;
10570
10571 /* restore width when not full width */
10572 if (wp->w_width < Columns && (fprintf(fd,
10573 "exe 'vert %dresize ' . ((&columns * %ld + %ld) / %ld)",
10574 n, (long)wp->w_width, Columns / 2, Columns) < 0
10575 || put_eol(fd) == FAIL))
10576 return FAIL;
10577 }
10578 }
10579 else
10580 {
10581 /* Just equalise window sizes */
10582 if (put_line(fd, "wincmd =") == FAIL)
10583 return FAIL;
10584 }
10585 return OK;
10586}
10587
10588/*
10589 * Write commands to "fd" to recursively create windows for frame "fr",
10590 * horizontally and vertically split.
10591 * After the commands the last window in the frame is the current window.
10592 * Returns FAIL when writing the commands to "fd" fails.
10593 */
10594 static int
10595ses_win_rec(fd, fr)
10596 FILE *fd;
10597 frame_T *fr;
10598{
10599 frame_T *frc;
10600 int count = 0;
10601
10602 if (fr->fr_layout != FR_LEAF)
10603 {
10604 /* Find first frame that's not skipped and then create a window for
10605 * each following one (first frame is already there). */
10606 frc = ses_skipframe(fr->fr_child);
10607 if (frc != NULL)
10608 while ((frc = ses_skipframe(frc->fr_next)) != NULL)
10609 {
10610 /* Make window as big as possible so that we have lots of room
10611 * to split. */
10612 if (put_line(fd, "wincmd _ | wincmd |") == FAIL
10613 || put_line(fd, fr->fr_layout == FR_COL
10614 ? "split" : "vsplit") == FAIL)
10615 return FAIL;
10616 ++count;
10617 }
10618
10619 /* Go back to the first window. */
10620 if (count > 0 && (fprintf(fd, fr->fr_layout == FR_COL
10621 ? "%dwincmd k" : "%dwincmd h", count) < 0
10622 || put_eol(fd) == FAIL))
10623 return FAIL;
10624
10625 /* Recursively create frames/windows in each window of this column or
10626 * row. */
10627 frc = ses_skipframe(fr->fr_child);
10628 while (frc != NULL)
10629 {
10630 ses_win_rec(fd, frc);
10631 frc = ses_skipframe(frc->fr_next);
10632 /* Go to next window. */
10633 if (frc != NULL && put_line(fd, "wincmd w") == FAIL)
10634 return FAIL;
10635 }
10636 }
10637 return OK;
10638}
10639
10640/*
10641 * Skip frames that don't contain windows we want to save in the Session.
10642 * Returns NULL when there none.
10643 */
10644 static frame_T *
10645ses_skipframe(fr)
10646 frame_T *fr;
10647{
10648 frame_T *frc;
10649
10650 for (frc = fr; frc != NULL; frc = frc->fr_next)
10651 if (ses_do_frame(frc))
10652 break;
10653 return frc;
10654}
10655
10656/*
10657 * Return TRUE if frame "fr" has a window somewhere that we want to save in
10658 * the Session.
10659 */
10660 static int
10661ses_do_frame(fr)
10662 frame_T *fr;
10663{
10664 frame_T *frc;
10665
10666 if (fr->fr_layout == FR_LEAF)
10667 return ses_do_win(fr->fr_win);
10668 for (frc = fr->fr_child; frc != NULL; frc = frc->fr_next)
10669 if (ses_do_frame(frc))
10670 return TRUE;
10671 return FALSE;
10672}
10673
10674/*
10675 * Return non-zero if window "wp" is to be stored in the Session.
10676 */
10677 static int
10678ses_do_win(wp)
10679 win_T *wp;
10680{
10681 if (wp->w_buffer->b_fname == NULL
10682#ifdef FEAT_QUICKFIX
10683 /* When 'buftype' is "nofile" can't restore the window contents. */
10684 || bt_nofile(wp->w_buffer)
10685#endif
10686 )
10687 return (ssop_flags & SSOP_BLANK);
10688 if (wp->w_buffer->b_help)
10689 return (ssop_flags & SSOP_HELP);
10690 return TRUE;
10691}
10692
10693/*
10694 * Write commands to "fd" to restore the view of a window.
10695 * Caller must make sure 'scrolloff' is zero.
10696 */
10697 static int
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010698put_view(fd, wp, add_edit, flagp, current_arg_idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010699 FILE *fd;
10700 win_T *wp;
10701 int add_edit; /* add ":edit" command to view */
10702 unsigned *flagp; /* vop_flags or ssop_flags */
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010703 int current_arg_idx; /* current argument index of the window, use
10704 * -1 if unknown */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010705{
10706 win_T *save_curwin;
10707 int f;
10708 int do_cursor;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010709 int did_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010710
10711 /* Always restore cursor position for ":mksession". For ":mkview" only
10712 * when 'viewoptions' contains "cursor". */
10713 do_cursor = (flagp == &ssop_flags || *flagp & SSOP_CURSOR);
10714
10715 /*
10716 * Local argument list.
10717 */
10718 if (wp->w_alist == &global_alist)
10719 {
10720 if (put_line(fd, "argglobal") == FAIL)
10721 return FAIL;
10722 }
10723 else
10724 {
10725 if (ses_arglist(fd, "arglocal", &wp->w_alist->al_ga,
10726 flagp == &vop_flags
10727 || !(*flagp & SSOP_CURDIR)
10728 || wp->w_localdir != NULL, flagp) == FAIL)
10729 return FAIL;
10730 }
10731
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010732 /* Only when part of a session: restore the argument index. Some
10733 * arguments may have been deleted, check if the index is valid. */
Bram Moolenaar51f53df2010-06-26 05:25:54 +020010734 if (wp->w_arg_idx != current_arg_idx && wp->w_arg_idx < WARGCOUNT(wp)
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010735 && flagp == &ssop_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010736 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010737 if (fprintf(fd, "%ldargu", (long)wp->w_arg_idx + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010738 || put_eol(fd) == FAIL)
10739 return FAIL;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010740 did_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010741 }
10742
10743 /* Edit the file. Skip this when ":next" already did it. */
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010744 if (add_edit && (!did_next || wp->w_arg_idx_invalid))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010745 {
10746 /*
10747 * Load the file.
10748 */
10749 if (wp->w_buffer->b_ffname != NULL
10750#ifdef FEAT_QUICKFIX
10751 && !bt_nofile(wp->w_buffer)
10752#endif
10753 )
10754 {
10755 /*
10756 * Editing a file in this buffer: use ":edit file".
10757 * This may have side effects! (e.g., compressed or network file).
10758 */
10759 if (fputs("edit ", fd) < 0
10760 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
10761 return FAIL;
10762 }
10763 else
10764 {
10765 /* No file in this buffer, just make it empty. */
10766 if (put_line(fd, "enew") == FAIL)
10767 return FAIL;
10768#ifdef FEAT_QUICKFIX
10769 if (wp->w_buffer->b_ffname != NULL)
10770 {
10771 /* The buffer does have a name, but it's not a file name. */
10772 if (fputs("file ", fd) < 0
10773 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
10774 return FAIL;
10775 }
10776#endif
10777 do_cursor = FALSE;
10778 }
10779 }
10780
10781 /*
10782 * Local mappings and abbreviations.
10783 */
10784 if ((*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
10785 && makemap(fd, wp->w_buffer) == FAIL)
10786 return FAIL;
10787
10788 /*
10789 * Local options. Need to go to the window temporarily.
10790 * Store only local values when using ":mkview" and when ":mksession" is
10791 * used and 'sessionoptions' doesn't include "options".
10792 * Some folding options are always stored when "folds" is included,
10793 * otherwise the folds would not be restored correctly.
10794 */
10795 save_curwin = curwin;
10796 curwin = wp;
10797 curbuf = curwin->w_buffer;
10798 if (*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
10799 f = makeset(fd, OPT_LOCAL,
10800 flagp == &vop_flags || !(*flagp & SSOP_OPTIONS));
10801#ifdef FEAT_FOLDING
10802 else if (*flagp & SSOP_FOLDS)
10803 f = makefoldset(fd);
10804#endif
10805 else
10806 f = OK;
10807 curwin = save_curwin;
10808 curbuf = curwin->w_buffer;
10809 if (f == FAIL)
10810 return FAIL;
10811
10812#ifdef FEAT_FOLDING
10813 /*
10814 * Save Folds when 'buftype' is empty and for help files.
10815 */
10816 if ((*flagp & SSOP_FOLDS)
10817 && wp->w_buffer->b_ffname != NULL
Bram Moolenaarb1b715d2006-01-21 22:09:43 +000010818# ifdef FEAT_QUICKFIX
10819 && (*wp->w_buffer->b_p_bt == NUL || wp->w_buffer->b_help)
10820# endif
10821 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000010822 {
10823 if (put_folds(fd, wp) == FAIL)
10824 return FAIL;
10825 }
10826#endif
10827
10828 /*
10829 * Set the cursor after creating folds, since that moves the cursor.
10830 */
10831 if (do_cursor)
10832 {
10833
10834 /* Restore the cursor line in the file and relatively in the
10835 * window. Don't use "G", it changes the jumplist. */
10836 if (fprintf(fd, "let s:l = %ld - ((%ld * winheight(0) + %ld) / %ld)",
10837 (long)wp->w_cursor.lnum,
10838 (long)(wp->w_cursor.lnum - wp->w_topline),
10839 (long)wp->w_height / 2, (long)wp->w_height) < 0
10840 || put_eol(fd) == FAIL
10841 || put_line(fd, "if s:l < 1 | let s:l = 1 | endif") == FAIL
10842 || put_line(fd, "exe s:l") == FAIL
10843 || put_line(fd, "normal! zt") == FAIL
10844 || fprintf(fd, "%ld", (long)wp->w_cursor.lnum) < 0
10845 || put_eol(fd) == FAIL)
10846 return FAIL;
10847 /* Restore the cursor column and left offset when not wrapping. */
10848 if (wp->w_cursor.col == 0)
10849 {
10850 if (put_line(fd, "normal! 0") == FAIL)
10851 return FAIL;
10852 }
10853 else
10854 {
10855 if (!wp->w_p_wrap && wp->w_leftcol > 0 && wp->w_width > 0)
10856 {
10857 if (fprintf(fd,
10858 "let s:c = %ld - ((%ld * winwidth(0) + %ld) / %ld)",
Bram Moolenaar558ddad2013-02-20 19:26:29 +010010859 (long)wp->w_virtcol + 1,
10860 (long)(wp->w_virtcol - wp->w_leftcol),
Bram Moolenaar071d4272004-06-13 20:20:40 +000010861 (long)wp->w_width / 2, (long)wp->w_width) < 0
10862 || put_eol(fd) == FAIL
10863 || put_line(fd, "if s:c > 0") == FAIL
10864 || fprintf(fd,
Bram Moolenaar558ddad2013-02-20 19:26:29 +010010865 " exe 'normal! ' . s:c . '|zs' . %ld . '|'",
10866 (long)wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010867 || put_eol(fd) == FAIL
10868 || put_line(fd, "else") == FAIL
Bram Moolenaarfdf447b2013-02-26 17:21:29 +010010869 || fprintf(fd, " normal! 0%d|", wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010870 || put_eol(fd) == FAIL
10871 || put_line(fd, "endif") == FAIL)
10872 return FAIL;
10873 }
10874 else
10875 {
Bram Moolenaar558ddad2013-02-20 19:26:29 +010010876 if (fprintf(fd, "normal! 0%d|", wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010877 || put_eol(fd) == FAIL)
10878 return FAIL;
10879 }
10880 }
10881 }
10882
10883 /*
10884 * Local directory.
10885 */
10886 if (wp->w_localdir != NULL)
10887 {
10888 if (fputs("lcd ", fd) < 0
10889 || ses_put_fname(fd, wp->w_localdir, flagp) == FAIL
10890 || put_eol(fd) == FAIL)
10891 return FAIL;
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010892 did_lcd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010893 }
10894
10895 return OK;
10896}
10897
10898/*
10899 * Write an argument list to the session file.
10900 * Returns FAIL if writing fails.
10901 */
10902 static int
10903ses_arglist(fd, cmd, gap, fullname, flagp)
10904 FILE *fd;
10905 char *cmd;
10906 garray_T *gap;
10907 int fullname; /* TRUE: use full path name */
10908 unsigned *flagp;
10909{
10910 int i;
Bram Moolenaard9462e32011-04-11 21:35:11 +020010911 char_u *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010912 char_u *s;
10913
10914 if (gap->ga_len == 0)
10915 return put_line(fd, "silent! argdel *");
10916 if (fputs(cmd, fd) < 0)
10917 return FAIL;
10918 for (i = 0; i < gap->ga_len; ++i)
10919 {
10920 /* NULL file names are skipped (only happens when out of memory). */
10921 s = alist_name(&((aentry_T *)gap->ga_data)[i]);
10922 if (s != NULL)
10923 {
10924 if (fullname)
10925 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020010926 buf = alloc(MAXPATHL);
10927 if (buf != NULL)
10928 {
10929 (void)vim_FullName(s, buf, MAXPATHL, FALSE);
10930 s = buf;
10931 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010932 }
10933 if (fputs(" ", fd) < 0 || ses_put_fname(fd, s, flagp) == FAIL)
Bram Moolenaard9462e32011-04-11 21:35:11 +020010934 {
10935 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010936 return FAIL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020010937 }
10938 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010939 }
10940 }
10941 return put_eol(fd);
10942}
10943
10944/*
10945 * Write a buffer name to the session file.
10946 * Also ends the line.
10947 * Returns FAIL if writing fails.
10948 */
10949 static int
10950ses_fname(fd, buf, flagp)
10951 FILE *fd;
10952 buf_T *buf;
10953 unsigned *flagp;
10954{
10955 char_u *name;
10956
10957 /* Use the short file name if the current directory is known at the time
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010958 * the session file will be sourced.
10959 * Don't do this for ":mkview", we don't know the current directory.
10960 * Don't do this after ":lcd", we don't keep track of what the current
10961 * directory is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010962 if (buf->b_sfname != NULL
10963 && flagp == &ssop_flags
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010964 && (ssop_flags & (SSOP_CURDIR | SSOP_SESDIR))
Bram Moolenaar8d594672009-07-01 18:18:57 +000010965#ifdef FEAT_AUTOCHDIR
10966 && !p_acd
10967#endif
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010968 && !did_lcd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010969 name = buf->b_sfname;
10970 else
10971 name = buf->b_ffname;
10972 if (ses_put_fname(fd, name, flagp) == FAIL || put_eol(fd) == FAIL)
10973 return FAIL;
10974 return OK;
10975}
10976
10977/*
10978 * Write a file name to the session file.
10979 * Takes care of the "slash" option in 'sessionoptions' and escapes special
10980 * characters.
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010981 * Returns FAIL if writing fails or out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010982 */
10983 static int
10984ses_put_fname(fd, name, flagp)
10985 FILE *fd;
10986 char_u *name;
10987 unsigned *flagp;
10988{
10989 char_u *sname;
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010990 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010991 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010992
10993 sname = home_replace_save(NULL, name);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010994 if (sname == NULL)
10995 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010996
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010997 if (*flagp & SSOP_SLASH)
10998 {
10999 /* change all backslashes to forward slashes */
11000 for (p = sname; *p != NUL; mb_ptr_adv(p))
11001 if (*p == '\\')
11002 *p = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +000011003 }
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011004
Bram Moolenaar84a05ac2013-05-06 04:24:17 +020011005 /* escape special characters */
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011006 p = vim_strsave_fnameescape(sname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011007 vim_free(sname);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011008 if (p == NULL)
11009 return FAIL;
11010
11011 /* write the result */
11012 if (fputs((char *)p, fd) < 0)
11013 retval = FAIL;
11014
11015 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011016 return retval;
11017}
11018
11019/*
11020 * ":loadview [nr]"
11021 */
11022 static void
11023ex_loadview(eap)
11024 exarg_T *eap;
11025{
11026 char_u *fname;
11027
11028 fname = get_view_file(*eap->arg);
11029 if (fname != NULL)
11030 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +000011031 do_source(fname, FALSE, DOSO_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011032 vim_free(fname);
11033 }
11034}
11035
11036/*
11037 * Get the name of the view file for the current buffer.
11038 */
11039 static char_u *
11040get_view_file(c)
11041 int c;
11042{
11043 int len = 0;
11044 char_u *p, *s;
11045 char_u *retval;
11046 char_u *sname;
11047
11048 if (curbuf->b_ffname == NULL)
11049 {
11050 EMSG(_(e_noname));
11051 return NULL;
11052 }
11053 sname = home_replace_save(NULL, curbuf->b_ffname);
11054 if (sname == NULL)
11055 return NULL;
11056
11057 /*
11058 * We want a file name without separators, because we're not going to make
11059 * a directory.
11060 * "normal" path separator -> "=+"
11061 * "=" -> "=="
11062 * ":" path separator -> "=-"
11063 */
11064 for (p = sname; *p; ++p)
11065 if (*p == '=' || vim_ispathsep(*p))
11066 ++len;
11067 retval = alloc((unsigned)(STRLEN(sname) + len + STRLEN(p_vdir) + 9));
11068 if (retval != NULL)
11069 {
11070 STRCPY(retval, p_vdir);
11071 add_pathsep(retval);
11072 s = retval + STRLEN(retval);
11073 for (p = sname; *p; ++p)
11074 {
11075 if (*p == '=')
11076 {
11077 *s++ = '=';
11078 *s++ = '=';
11079 }
11080 else if (vim_ispathsep(*p))
11081 {
11082 *s++ = '=';
Bram Moolenaare60acc12011-05-10 16:41:25 +020011083#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011084 if (*p == ':')
11085 *s++ = '-';
11086 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000011087#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000011088 *s++ = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +000011089 }
11090 else
11091 *s++ = *p;
11092 }
11093 *s++ = '=';
11094 *s++ = c;
11095 STRCPY(s, ".vim");
11096 }
11097
11098 vim_free(sname);
11099 return retval;
11100}
11101
11102#endif /* FEAT_SESSION */
11103
11104/*
11105 * Write end-of-line character(s) for ":mkexrc", ":mkvimrc" and ":mksession".
11106 * Return FAIL for a write error.
11107 */
11108 int
11109put_eol(fd)
11110 FILE *fd;
11111{
11112 if (
11113#ifdef USE_CRNL
11114 (
11115# ifdef MKSESSION_NL
11116 !mksession_nl &&
11117# endif
11118 (putc('\r', fd) < 0)) ||
11119#endif
11120 (putc('\n', fd) < 0))
11121 return FAIL;
11122 return OK;
11123}
11124
11125/*
11126 * Write a line to "fd".
11127 * Return FAIL for a write error.
11128 */
11129 int
11130put_line(fd, s)
11131 FILE *fd;
11132 char *s;
11133{
11134 if (fputs(s, fd) < 0 || put_eol(fd) == FAIL)
11135 return FAIL;
11136 return OK;
11137}
11138
11139#ifdef FEAT_VIMINFO
11140/*
11141 * ":rviminfo" and ":wviminfo".
11142 */
11143 static void
11144ex_viminfo(eap)
11145 exarg_T *eap;
11146{
11147 char_u *save_viminfo;
11148
11149 save_viminfo = p_viminfo;
11150 if (*p_viminfo == NUL)
11151 p_viminfo = (char_u *)"'100";
11152 if (eap->cmdidx == CMD_rviminfo)
11153 {
Bram Moolenaard812df62008-11-09 12:46:09 +000011154 if (read_viminfo(eap->arg, VIF_WANT_INFO | VIF_WANT_MARKS
11155 | (eap->forceit ? VIF_FORCEIT : 0)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011156 EMSG(_("E195: Cannot open viminfo file for reading"));
11157 }
11158 else
11159 write_viminfo(eap->arg, eap->forceit);
11160 p_viminfo = save_viminfo;
11161}
11162#endif
11163
11164#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +000011165/*
Bram Moolenaard9462e32011-04-11 21:35:11 +020011166 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +000011167 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +000011168 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011169 void
11170dialog_msg(buff, format, fname)
11171 char_u *buff;
11172 char *format;
11173 char_u *fname;
11174{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011175 if (fname == NULL)
11176 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +020011177 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011178}
11179#endif
11180
11181/*
11182 * ":behave {mswin,xterm}"
11183 */
11184 static void
11185ex_behave(eap)
11186 exarg_T *eap;
11187{
11188 if (STRCMP(eap->arg, "mswin") == 0)
11189 {
11190 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
11191 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
11192 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
11193 set_option_value((char_u *)"keymodel", 0L,
11194 (char_u *)"startsel,stopsel", 0);
11195 }
11196 else if (STRCMP(eap->arg, "xterm") == 0)
11197 {
11198 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
11199 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
11200 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
11201 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
11202 }
11203 else
11204 EMSG2(_(e_invarg2), eap->arg);
11205}
11206
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010011207#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
11208/*
11209 * Function given to ExpandGeneric() to obtain the possible arguments of the
11210 * ":behave {mswin,xterm}" command.
11211 */
11212 char_u *
11213get_behave_arg(xp, idx)
11214 expand_T *xp UNUSED;
11215 int idx;
11216{
11217 if (idx == 0)
11218 return (char_u *)"mswin";
11219 if (idx == 1)
11220 return (char_u *)"xterm";
11221 return NULL;
11222}
11223#endif
11224
Bram Moolenaar071d4272004-06-13 20:20:40 +000011225#ifdef FEAT_AUTOCMD
11226static int filetype_detect = FALSE;
11227static int filetype_plugin = FALSE;
11228static int filetype_indent = FALSE;
11229
11230/*
11231 * ":filetype [plugin] [indent] {on,off,detect}"
11232 * on: Load the filetype.vim file to install autocommands for file types.
11233 * off: Load the ftoff.vim file to remove all autocommands for file types.
11234 * plugin on: load filetype.vim and ftplugin.vim
11235 * plugin off: load ftplugof.vim
11236 * indent on: load filetype.vim and indent.vim
11237 * indent off: load indoff.vim
11238 */
11239 static void
11240ex_filetype(eap)
11241 exarg_T *eap;
11242{
11243 char_u *arg = eap->arg;
11244 int plugin = FALSE;
11245 int indent = FALSE;
11246
11247 if (*eap->arg == NUL)
11248 {
11249 /* Print current status. */
11250 smsg((char_u *)"filetype detection:%s plugin:%s indent:%s",
11251 filetype_detect ? "ON" : "OFF",
11252 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
11253 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
11254 return;
11255 }
11256
11257 /* Accept "plugin" and "indent" in any order. */
11258 for (;;)
11259 {
11260 if (STRNCMP(arg, "plugin", 6) == 0)
11261 {
11262 plugin = TRUE;
11263 arg = skipwhite(arg + 6);
11264 continue;
11265 }
11266 if (STRNCMP(arg, "indent", 6) == 0)
11267 {
11268 indent = TRUE;
11269 arg = skipwhite(arg + 6);
11270 continue;
11271 }
11272 break;
11273 }
11274 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
11275 {
11276 if (*arg == 'o' || !filetype_detect)
11277 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011278 source_runtime((char_u *)FILETYPE_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011279 filetype_detect = TRUE;
11280 if (plugin)
11281 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011282 source_runtime((char_u *)FTPLUGIN_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011283 filetype_plugin = TRUE;
11284 }
11285 if (indent)
11286 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011287 source_runtime((char_u *)INDENT_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011288 filetype_indent = TRUE;
11289 }
11290 }
11291 if (*arg == 'd')
11292 {
11293 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE);
Bram Moolenaara3227e22006-03-08 21:32:40 +000011294 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011295 }
11296 }
11297 else if (STRCMP(arg, "off") == 0)
11298 {
11299 if (plugin || indent)
11300 {
11301 if (plugin)
11302 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011303 source_runtime((char_u *)FTPLUGOF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011304 filetype_plugin = FALSE;
11305 }
11306 if (indent)
11307 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011308 source_runtime((char_u *)INDOFF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011309 filetype_indent = FALSE;
11310 }
11311 }
11312 else
11313 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011314 source_runtime((char_u *)FTOFF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011315 filetype_detect = FALSE;
11316 }
11317 }
11318 else
11319 EMSG2(_(e_invarg2), arg);
11320}
11321
11322/*
11323 * ":setfiletype {name}"
11324 */
11325 static void
11326ex_setfiletype(eap)
11327 exarg_T *eap;
11328{
11329 if (!did_filetype)
11330 set_option_value((char_u *)"filetype", 0L, eap->arg, OPT_LOCAL);
11331}
11332#endif
11333
Bram Moolenaar071d4272004-06-13 20:20:40 +000011334 static void
11335ex_digraphs(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +000011336 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011337{
11338#ifdef FEAT_DIGRAPHS
11339 if (*eap->arg != NUL)
11340 putdigraph(eap->arg);
11341 else
11342 listdigraphs();
11343#else
11344 EMSG(_("E196: No digraphs in this version"));
11345#endif
11346}
11347
11348 static void
11349ex_set(eap)
11350 exarg_T *eap;
11351{
11352 int flags = 0;
11353
11354 if (eap->cmdidx == CMD_setlocal)
11355 flags = OPT_LOCAL;
11356 else if (eap->cmdidx == CMD_setglobal)
11357 flags = OPT_GLOBAL;
11358#if defined(FEAT_EVAL) && defined(FEAT_AUTOCMD) && defined(FEAT_BROWSE)
11359 if (cmdmod.browse && flags == 0)
11360 ex_options(eap);
11361 else
11362#endif
11363 (void)do_set(eap->arg, flags);
11364}
11365
11366#ifdef FEAT_SEARCH_EXTRA
11367/*
11368 * ":nohlsearch"
11369 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011370 static void
11371ex_nohlsearch(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +000011372 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011373{
11374 no_hlsearch = TRUE;
Bram Moolenaarf71a3db2006-03-12 21:50:18 +000011375 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011376}
11377
11378/*
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011379 * ":[N]match {group} {pattern}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000011380 * Sets nextcmd to the start of the next command, if any. Also called when
11381 * skipping commands to find the next command.
11382 */
11383 static void
11384ex_match(eap)
11385 exarg_T *eap;
11386{
11387 char_u *p;
Bram Moolenaar52d36c82007-08-11 14:00:30 +000011388 char_u *g = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011389 char_u *end;
11390 int c;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011391 int id;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011392
11393 if (eap->line2 <= 3)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011394 id = eap->line2;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011395 else
11396 {
11397 EMSG(e_invcmd);
11398 return;
11399 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011400
11401 /* First clear any old pattern. */
11402 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011403 match_delete(curwin, id, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011404
11405 if (ends_excmd(*eap->arg))
11406 end = eap->arg;
11407 else if ((STRNICMP(eap->arg, "none", 4) == 0
11408 && (vim_iswhite(eap->arg[4]) || ends_excmd(eap->arg[4]))))
11409 end = eap->arg + 4;
11410 else
11411 {
11412 p = skiptowhite(eap->arg);
11413 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011414 g = vim_strnsave(eap->arg, (int)(p - eap->arg));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011415 p = skipwhite(p);
11416 if (*p == NUL)
11417 {
11418 /* There must be two arguments. */
11419 EMSG2(_(e_invarg2), eap->arg);
11420 return;
11421 }
11422 end = skip_regexp(p + 1, *p, TRUE, NULL);
11423 if (!eap->skip)
11424 {
11425 if (*end != NUL && !ends_excmd(*skipwhite(end + 1)))
11426 {
11427 eap->errmsg = e_trailing;
11428 return;
11429 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000011430 if (*end != *p)
11431 {
11432 EMSG2(_(e_invarg2), p);
11433 return;
11434 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011435
11436 c = *end;
11437 *end = NUL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011438 match_add(curwin, g, p + 1, 10, id);
11439 vim_free(g);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000011440 *end = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011441 }
11442 }
11443 eap->nextcmd = find_nextcmd(end);
11444}
11445#endif
11446
11447#ifdef FEAT_CRYPT
11448/*
11449 * ":X": Get crypt key
11450 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011451 static void
11452ex_X(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +000011453 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011454{
Bram Moolenaar49771f42010-07-20 17:32:38 +020011455 if (get_crypt_method(curbuf) == 0 || blowfish_self_test() == OK)
Bram Moolenaar40e6a712010-05-16 22:32:54 +020011456 (void)get_crypt_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011457}
11458#endif
11459
11460#ifdef FEAT_FOLDING
11461 static void
11462ex_fold(eap)
11463 exarg_T *eap;
11464{
11465 if (foldManualAllowed(TRUE))
11466 foldCreate(eap->line1, eap->line2);
11467}
11468
11469 static void
11470ex_foldopen(eap)
11471 exarg_T *eap;
11472{
11473 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
11474 eap->forceit, FALSE);
11475}
11476
11477 static void
11478ex_folddo(eap)
11479 exarg_T *eap;
11480{
11481 linenr_T lnum;
11482
11483 /* First set the marks for all lines closed/open. */
11484 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
11485 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
11486 ml_setmarked(lnum);
11487
11488 /* Execute the command on the marked lines. */
11489 global_exe(eap->arg);
11490 ml_clearmarked(); /* clear rest of the marks */
11491}
11492#endif