blob: 5ce0b5d97f403d03d73f097b7c899097202bc568 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * ex_docmd.c: functions for executing an Ex command line.
12 */
13
14#include "vim.h"
15
Bram Moolenaar071d4272004-06-13 20:20:40 +000016static int quitmore = 0;
17static int ex_pressedreturn = FALSE;
18#ifndef FEAT_PRINTER
19# define ex_hardcopy ex_ni
20#endif
21
22#ifdef FEAT_USR_CMDS
23typedef struct ucmd
24{
25 char_u *uc_name; /* The command name */
26 long_u uc_argt; /* The argument type */
27 char_u *uc_rep; /* The command's replacement string */
28 long uc_def; /* The default value for a range/count */
Bram Moolenaar071d4272004-06-13 20:20:40 +000029 int uc_compl; /* completion type */
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010030# ifdef FEAT_EVAL
31 scid_T uc_scriptID; /* SID where the command was defined */
32# ifdef FEAT_CMDL_COMPL
Bram Moolenaar071d4272004-06-13 20:20:40 +000033 char_u *uc_compl_arg; /* completion argument if any */
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010034# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000035# endif
36} ucmd_T;
37
38#define UC_BUFFER 1 /* -buffer: local to current buffer */
39
Bram Moolenaar2c29bee2005-06-01 21:46:07 +000040static garray_T ucmds = {0, 0, sizeof(ucmd_T), 4, NULL};
Bram Moolenaar071d4272004-06-13 20:20:40 +000041
42#define USER_CMD(i) (&((ucmd_T *)(ucmds.ga_data))[i])
43#define USER_CMD_GA(gap, i) (&((ucmd_T *)((gap)->ga_data))[i])
44
45static void do_ucmd __ARGS((exarg_T *eap));
46static void ex_command __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +000047static void ex_delcommand __ARGS((exarg_T *eap));
48# ifdef FEAT_CMDL_COMPL
49static char_u *get_user_command_name __ARGS((int idx));
50# endif
51
52#else
53# define ex_command ex_ni
54# define ex_comclear ex_ni
55# define ex_delcommand ex_ni
56#endif
57
58#ifdef FEAT_EVAL
Bram Moolenaar89d40322006-08-29 15:30:07 +000059static char_u *do_one_cmd __ARGS((char_u **, int, struct condstack *, char_u *(*fgetline)(int, void *, int), void *cookie));
Bram Moolenaar071d4272004-06-13 20:20:40 +000060#else
Bram Moolenaar89d40322006-08-29 15:30:07 +000061static char_u *do_one_cmd __ARGS((char_u **, int, char_u *(*fgetline)(int, void *, int), void *cookie));
Bram Moolenaar071d4272004-06-13 20:20:40 +000062static int if_level = 0; /* depth in :if */
63#endif
Bram Moolenaara6f4d612011-09-21 19:10:46 +020064static void append_command __ARGS((char_u *cmd));
Bram Moolenaar071d4272004-06-13 20:20:40 +000065static char_u *find_command __ARGS((exarg_T *eap, int *full));
66
67static void ex_abbreviate __ARGS((exarg_T *eap));
68static void ex_map __ARGS((exarg_T *eap));
69static void ex_unmap __ARGS((exarg_T *eap));
70static void ex_mapclear __ARGS((exarg_T *eap));
71static void ex_abclear __ARGS((exarg_T *eap));
72#ifndef FEAT_MENU
73# define ex_emenu ex_ni
74# define ex_menu ex_ni
75# define ex_menutranslate ex_ni
76#endif
77#ifdef FEAT_AUTOCMD
78static void ex_autocmd __ARGS((exarg_T *eap));
79static void ex_doautocmd __ARGS((exarg_T *eap));
80#else
81# define ex_autocmd ex_ni
82# define ex_doautocmd ex_ni
83# define ex_doautoall ex_ni
84#endif
85#ifdef FEAT_LISTCMDS
86static void ex_bunload __ARGS((exarg_T *eap));
87static void ex_buffer __ARGS((exarg_T *eap));
88static void ex_bmodified __ARGS((exarg_T *eap));
89static void ex_bnext __ARGS((exarg_T *eap));
90static void ex_bprevious __ARGS((exarg_T *eap));
91static void ex_brewind __ARGS((exarg_T *eap));
92static void ex_blast __ARGS((exarg_T *eap));
93#else
94# define ex_bunload ex_ni
95# define ex_buffer ex_ni
96# define ex_bmodified ex_ni
97# define ex_bnext ex_ni
98# define ex_bprevious ex_ni
99# define ex_brewind ex_ni
100# define ex_blast ex_ni
101# define buflist_list ex_ni
102# define ex_checktime ex_ni
103#endif
104#if !defined(FEAT_LISTCMDS) || !defined(FEAT_WINDOWS)
105# define ex_buffer_all ex_ni
106#endif
107static char_u *getargcmd __ARGS((char_u **));
108static char_u *skip_cmd_arg __ARGS((char_u *p, int rembs));
109static int getargopt __ARGS((exarg_T *eap));
110#ifndef FEAT_QUICKFIX
111# define ex_make ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +0000112# define ex_cbuffer ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000113# define ex_cc ex_ni
114# define ex_cnext ex_ni
115# define ex_cfile ex_ni
116# define qf_list ex_ni
117# define qf_age ex_ni
118# define ex_helpgrep ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +0000119# define ex_vimgrep ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000120#endif
121#if !defined(FEAT_QUICKFIX) || !defined(FEAT_WINDOWS)
122# define ex_cclose ex_ni
123# define ex_copen ex_ni
124# define ex_cwindow ex_ni
125#endif
Bram Moolenaar1e015462005-09-25 22:16:38 +0000126#if !defined(FEAT_QUICKFIX) || !defined(FEAT_EVAL)
127# define ex_cexpr ex_ni
128#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000129
130static int check_more __ARGS((int, int));
131static linenr_T get_address __ARGS((char_u **, int skip, int to_other_file));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000132static void get_flags __ARGS((exarg_T *eap));
Bram Moolenaar85363ab2010-07-18 13:58:26 +0200133#if !defined(FEAT_PERL) \
134 || !defined(FEAT_PYTHON) || !defined(FEAT_PYTHON3) \
135 || !defined(FEAT_TCL) \
136 || !defined(FEAT_RUBY) \
137 || !defined(FEAT_LUA) \
138 || !defined(FEAT_MZSCHEME)
Bram Moolenaar7bb75552007-07-16 18:39:49 +0000139# define HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +0000140static void ex_script_ni __ARGS((exarg_T *eap));
141#endif
142static char_u *invalid_range __ARGS((exarg_T *eap));
143static void correct_range __ARGS((exarg_T *eap));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000144#ifdef FEAT_QUICKFIX
145static char_u *replace_makeprg __ARGS((exarg_T *eap, char_u *p, char_u **cmdlinep));
146#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000147static char_u *repl_cmdline __ARGS((exarg_T *eap, char_u *src, int srclen, char_u *repl, char_u **cmdlinep));
148static void ex_highlight __ARGS((exarg_T *eap));
149static void ex_colorscheme __ARGS((exarg_T *eap));
150static void ex_quit __ARGS((exarg_T *eap));
151static void ex_cquit __ARGS((exarg_T *eap));
152static void ex_quit_all __ARGS((exarg_T *eap));
153#ifdef FEAT_WINDOWS
154static void ex_close __ARGS((exarg_T *eap));
Bram Moolenaarf740b292006-02-16 22:11:02 +0000155static void ex_win_close __ARGS((int forceit, win_T *win, tabpage_T *tp));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000156static void ex_only __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000157static void ex_resize __ARGS((exarg_T *eap));
158static void ex_stag __ARGS((exarg_T *eap));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000159static void ex_tabclose __ARGS((exarg_T *eap));
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000160static void ex_tabonly __ARGS((exarg_T *eap));
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000161static void ex_tabnext __ARGS((exarg_T *eap));
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000162static void ex_tabmove __ARGS((exarg_T *eap));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000163static void ex_tabs __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000164#else
165# define ex_close ex_ni
166# define ex_only ex_ni
167# define ex_all ex_ni
168# define ex_resize ex_ni
169# define ex_splitview ex_ni
170# define ex_stag ex_ni
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000171# define ex_tabnext ex_ni
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000172# define ex_tabmove ex_ni
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000173# define ex_tabs ex_ni
174# define ex_tabclose ex_ni
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000175# define ex_tabonly ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000176#endif
177#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
178static void ex_pclose __ARGS((exarg_T *eap));
179static void ex_ptag __ARGS((exarg_T *eap));
180static void ex_pedit __ARGS((exarg_T *eap));
181#else
182# define ex_pclose ex_ni
183# define ex_ptag ex_ni
184# define ex_pedit ex_ni
185#endif
186static void ex_hide __ARGS((exarg_T *eap));
187static void ex_stop __ARGS((exarg_T *eap));
188static void ex_exit __ARGS((exarg_T *eap));
189static void ex_print __ARGS((exarg_T *eap));
190#ifdef FEAT_BYTEOFF
191static void ex_goto __ARGS((exarg_T *eap));
192#else
193# define ex_goto ex_ni
194#endif
195static void ex_shell __ARGS((exarg_T *eap));
196static void ex_preserve __ARGS((exarg_T *eap));
197static void ex_recover __ARGS((exarg_T *eap));
198#ifndef FEAT_LISTCMDS
199# define ex_argedit ex_ni
200# define ex_argadd ex_ni
201# define ex_argdelete ex_ni
202# define ex_listdo ex_ni
203#endif
204static void ex_mode __ARGS((exarg_T *eap));
205static void ex_wrongmodifier __ARGS((exarg_T *eap));
206static void ex_find __ARGS((exarg_T *eap));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000207static void ex_open __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000208static void ex_edit __ARGS((exarg_T *eap));
209#if !defined(FEAT_GUI) && !defined(FEAT_CLIENTSERVER)
210# define ex_drop ex_ni
211#endif
212#ifndef FEAT_GUI
213# define ex_gui ex_nogui
214static void ex_nogui __ARGS((exarg_T *eap));
215#endif
216#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
217static void ex_tearoff __ARGS((exarg_T *eap));
218#else
219# define ex_tearoff ex_ni
220#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000221#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000222static void ex_popup __ARGS((exarg_T *eap));
223#else
224# define ex_popup ex_ni
225#endif
226#ifndef FEAT_GUI_MSWIN
227# define ex_simalt ex_ni
228#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000229#if !defined(FEAT_GUI_MSWIN) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000230# define gui_mch_find_dialog ex_ni
231# define gui_mch_replace_dialog ex_ni
232#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000233#if !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000234# define ex_helpfind ex_ni
235#endif
236#ifndef FEAT_CSCOPE
237# define do_cscope ex_ni
238# define do_scscope ex_ni
239# define do_cstag ex_ni
240#endif
241#ifndef FEAT_SYN_HL
242# define ex_syntax ex_ni
Bram Moolenaar860cae12010-06-05 23:22:07 +0200243# define ex_ownsyntax ex_ni
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000244#endif
245#ifndef FEAT_SPELL
Bram Moolenaarb765d632005-06-07 21:00:02 +0000246# define ex_spell ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000247# define ex_mkspell ex_ni
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000248# define ex_spelldump ex_ni
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000249# define ex_spellinfo ex_ni
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000250# define ex_spellrepall ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000251#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200252#ifndef FEAT_PERSISTENT_UNDO
253# define ex_rundo ex_ni
254# define ex_wundo ex_ni
255#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200256#ifndef FEAT_LUA
257# define ex_lua ex_script_ni
258# define ex_luado ex_ni
259# define ex_luafile ex_ni
260#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000261#ifndef FEAT_MZSCHEME
262# define ex_mzscheme ex_script_ni
263# define ex_mzfile ex_ni
264#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000265#ifndef FEAT_PERL
266# define ex_perl ex_script_ni
267# define ex_perldo ex_ni
268#endif
269#ifndef FEAT_PYTHON
270# define ex_python ex_script_ni
271# define ex_pyfile ex_ni
272#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200273#ifndef FEAT_PYTHON3
Bram Moolenaar368373e2010-07-19 20:46:22 +0200274# define ex_py3 ex_script_ni
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200275# define ex_py3do ex_ni
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200276# define ex_py3file ex_ni
277#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000278#ifndef FEAT_TCL
279# define ex_tcl ex_script_ni
280# define ex_tcldo ex_ni
281# define ex_tclfile ex_ni
282#endif
283#ifndef FEAT_RUBY
284# define ex_ruby ex_script_ni
285# define ex_rubydo ex_ni
286# define ex_rubyfile ex_ni
287#endif
288#ifndef FEAT_SNIFF
289# define ex_sniff ex_ni
290#endif
291#ifndef FEAT_KEYMAP
292# define ex_loadkeymap ex_ni
293#endif
294static void ex_swapname __ARGS((exarg_T *eap));
295static void ex_syncbind __ARGS((exarg_T *eap));
296static void ex_read __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000297static void ex_pwd __ARGS((exarg_T *eap));
298static void ex_equal __ARGS((exarg_T *eap));
299static void ex_sleep __ARGS((exarg_T *eap));
300static void do_exmap __ARGS((exarg_T *eap, int isabbrev));
301static void ex_winsize __ARGS((exarg_T *eap));
302#ifdef FEAT_WINDOWS
303static void ex_wincmd __ARGS((exarg_T *eap));
304#else
305# define ex_wincmd ex_ni
306#endif
Bram Moolenaar843ee412004-06-30 16:16:41 +0000307#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000308static void ex_winpos __ARGS((exarg_T *eap));
309#else
310# define ex_winpos ex_ni
311#endif
312static void ex_operators __ARGS((exarg_T *eap));
313static void ex_put __ARGS((exarg_T *eap));
314static void ex_copymove __ARGS((exarg_T *eap));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000315static void ex_may_print __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000316static void ex_submagic __ARGS((exarg_T *eap));
317static void ex_join __ARGS((exarg_T *eap));
318static void ex_at __ARGS((exarg_T *eap));
319static void ex_bang __ARGS((exarg_T *eap));
320static void ex_undo __ARGS((exarg_T *eap));
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200321#ifdef FEAT_PERSISTENT_UNDO
322static void ex_wundo __ARGS((exarg_T *eap));
323static void ex_rundo __ARGS((exarg_T *eap));
324#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000325static void ex_redo __ARGS((exarg_T *eap));
Bram Moolenaarc7d89352006-03-14 22:53:34 +0000326static void ex_later __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000327static void ex_redir __ARGS((exarg_T *eap));
328static void ex_redraw __ARGS((exarg_T *eap));
329static void ex_redrawstatus __ARGS((exarg_T *eap));
330static void close_redir __ARGS((void));
331static void ex_mkrc __ARGS((exarg_T *eap));
332static void ex_mark __ARGS((exarg_T *eap));
333#ifdef FEAT_USR_CMDS
334static char_u *uc_fun_cmd __ARGS((void));
Bram Moolenaar52b4b552005-03-07 23:00:57 +0000335static 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 +0000336#endif
337#ifdef FEAT_EX_EXTRA
338static void ex_normal __ARGS((exarg_T *eap));
339static void ex_startinsert __ARGS((exarg_T *eap));
340static void ex_stopinsert __ARGS((exarg_T *eap));
341#else
342# define ex_normal ex_ni
343# define ex_align ex_ni
344# define ex_retab ex_ni
345# define ex_startinsert ex_ni
346# define ex_stopinsert ex_ni
347# define ex_helptags ex_ni
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +0000348# define ex_sort ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000349#endif
350#ifdef FEAT_FIND_ID
351static void ex_checkpath __ARGS((exarg_T *eap));
352static void ex_findpat __ARGS((exarg_T *eap));
353#else
354# define ex_findpat ex_ni
355# define ex_checkpath ex_ni
356#endif
357#if defined(FEAT_FIND_ID) && defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
358static void ex_psearch __ARGS((exarg_T *eap));
359#else
360# define ex_psearch ex_ni
361#endif
362static void ex_tag __ARGS((exarg_T *eap));
363static void ex_tag_cmd __ARGS((exarg_T *eap, char_u *name));
364#ifndef FEAT_EVAL
365# define ex_scriptnames ex_ni
366# define ex_finish ex_ni
367# define ex_echo ex_ni
368# define ex_echohl ex_ni
369# define ex_execute ex_ni
370# define ex_call ex_ni
371# define ex_if ex_ni
372# define ex_endif ex_ni
373# define ex_else ex_ni
374# define ex_while ex_ni
375# define ex_continue ex_ni
376# define ex_break ex_ni
377# define ex_endwhile ex_ni
378# define ex_throw ex_ni
379# define ex_try ex_ni
380# define ex_catch ex_ni
381# define ex_finally ex_ni
382# define ex_endtry ex_ni
383# define ex_endfunction ex_ni
384# define ex_let ex_ni
385# define ex_unlet ex_ni
Bram Moolenaar65c1b012005-01-31 19:02:28 +0000386# define ex_lockvar ex_ni
387# define ex_unlockvar ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000388# define ex_function ex_ni
389# define ex_delfunction ex_ni
390# define ex_return ex_ni
Bram Moolenaard812df62008-11-09 12:46:09 +0000391# define ex_oldfiles ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000392#endif
393static char_u *arg_all __ARGS((void));
394#ifdef FEAT_SESSION
395static int makeopens __ARGS((FILE *fd, char_u *dirnow));
Bram Moolenaarf13be0d2008-01-02 14:13:10 +0000396static 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 +0000397static void ex_loadview __ARGS((exarg_T *eap));
398static char_u *get_view_file __ARGS((int c));
Bram Moolenaareeefcc72007-05-01 21:21:21 +0000399static int did_lcd; /* whether ":lcd" was produced for a session */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000400#else
401# define ex_loadview ex_ni
402#endif
403#ifndef FEAT_EVAL
404# define ex_compiler ex_ni
405#endif
406#ifdef FEAT_VIMINFO
407static void ex_viminfo __ARGS((exarg_T *eap));
408#else
409# define ex_viminfo ex_ni
410#endif
411static void ex_behave __ARGS((exarg_T *eap));
412#ifdef FEAT_AUTOCMD
413static void ex_filetype __ARGS((exarg_T *eap));
414static void ex_setfiletype __ARGS((exarg_T *eap));
415#else
416# define ex_filetype ex_ni
417# define ex_setfiletype ex_ni
418#endif
419#ifndef FEAT_DIFF
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000420# define ex_diffoff ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000421# define ex_diffpatch ex_ni
422# define ex_diffgetput ex_ni
423# define ex_diffsplit ex_ni
424# define ex_diffthis ex_ni
425# define ex_diffupdate ex_ni
426#endif
427static void ex_digraphs __ARGS((exarg_T *eap));
428static void ex_set __ARGS((exarg_T *eap));
429#if !defined(FEAT_EVAL) || !defined(FEAT_AUTOCMD)
430# define ex_options ex_ni
431#endif
432#ifdef FEAT_SEARCH_EXTRA
433static void ex_nohlsearch __ARGS((exarg_T *eap));
434static void ex_match __ARGS((exarg_T *eap));
435#else
436# define ex_nohlsearch ex_ni
437# define ex_match ex_ni
438#endif
439#ifdef FEAT_CRYPT
440static void ex_X __ARGS((exarg_T *eap));
441#else
442# define ex_X ex_ni
443#endif
444#ifdef FEAT_FOLDING
445static void ex_fold __ARGS((exarg_T *eap));
446static void ex_foldopen __ARGS((exarg_T *eap));
447static void ex_folddo __ARGS((exarg_T *eap));
448#else
449# define ex_fold ex_ni
450# define ex_foldopen ex_ni
451# define ex_folddo ex_ni
452#endif
453#if !((defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
454 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE)))
455# define ex_language ex_ni
456#endif
457#ifndef FEAT_SIGNS
458# define ex_sign ex_ni
459#endif
460#ifndef FEAT_SUN_WORKSHOP
461# define ex_wsverb ex_ni
462#endif
Bram Moolenaar009b2592004-10-24 19:18:58 +0000463#ifndef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200464# define ex_nbclose ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000465# define ex_nbkey ex_ni
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200466# define ex_nbstart ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000467#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000468
469#ifndef FEAT_EVAL
470# define ex_debug ex_ni
471# define ex_breakadd ex_ni
472# define ex_debuggreedy ex_ni
473# define ex_breakdel ex_ni
474# define ex_breaklist ex_ni
475#endif
476
477#ifndef FEAT_CMDHIST
478# define ex_history ex_ni
479#endif
480#ifndef FEAT_JUMPLIST
481# define ex_jumps ex_ni
482# define ex_changes ex_ni
483#endif
484
Bram Moolenaar05159a02005-02-26 23:04:13 +0000485#ifndef FEAT_PROFILE
486# define ex_profile ex_ni
487#endif
488
Bram Moolenaar071d4272004-06-13 20:20:40 +0000489/*
490 * Declare cmdnames[].
491 */
492#define DO_DECLARE_EXCMD
493#include "ex_cmds.h"
494
495/*
496 * Table used to quickly search for a command, based on its first character.
497 */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +0000498static cmdidx_T cmdidxs[27] =
Bram Moolenaar071d4272004-06-13 20:20:40 +0000499{
500 CMD_append,
501 CMD_buffer,
502 CMD_change,
503 CMD_delete,
504 CMD_edit,
505 CMD_file,
506 CMD_global,
507 CMD_help,
508 CMD_insert,
509 CMD_join,
510 CMD_k,
511 CMD_list,
512 CMD_move,
513 CMD_next,
514 CMD_open,
515 CMD_print,
516 CMD_quit,
517 CMD_read,
518 CMD_substitute,
519 CMD_t,
520 CMD_undo,
521 CMD_vglobal,
522 CMD_write,
523 CMD_xit,
524 CMD_yank,
525 CMD_z,
526 CMD_bang
527};
528
529static char_u dollar_command[2] = {'$', 0};
530
531
532#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000533/* Struct for storing a line inside a while/for loop */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000534typedef struct
535{
536 char_u *line; /* command line */
537 linenr_T lnum; /* sourcing_lnum of the line */
538} wcmd_T;
539
540/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000541 * Structure used to store info for line position in a while or for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000542 * This is required, because do_one_cmd() may invoke ex_function(), which
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000543 * reads more lines that may come from the while/for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000544 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000545struct loop_cookie
Bram Moolenaar071d4272004-06-13 20:20:40 +0000546{
547 garray_T *lines_gap; /* growarray with line info */
548 int current_line; /* last read line from growarray */
549 int repeating; /* TRUE when looping a second time */
550 /* When "repeating" is FALSE use "getline" and "cookie" to get lines */
551 char_u *(*getline) __ARGS((int, void *, int));
552 void *cookie;
553};
554
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000555static char_u *get_loop_line __ARGS((int c, void *cookie, int indent));
556static int store_loop_line __ARGS((garray_T *gap, char_u *line));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000557static void free_cmdlines __ARGS((garray_T *gap));
Bram Moolenaared203462004-06-16 11:19:22 +0000558
559/* Struct to save a few things while debugging. Used in do_cmdline() only. */
560struct dbg_stuff
561{
562 int trylevel;
563 int force_abort;
564 except_T *caught_stack;
565 char_u *vv_exception;
566 char_u *vv_throwpoint;
567 int did_emsg;
568 int got_int;
569 int did_throw;
570 int need_rethrow;
571 int check_cstack;
572 except_T *current_exception;
573};
574
575static void save_dbg_stuff __ARGS((struct dbg_stuff *dsp));
576static void restore_dbg_stuff __ARGS((struct dbg_stuff *dsp));
577
578 static void
579save_dbg_stuff(dsp)
580 struct dbg_stuff *dsp;
581{
582 dsp->trylevel = trylevel; trylevel = 0;
583 dsp->force_abort = force_abort; force_abort = FALSE;
584 dsp->caught_stack = caught_stack; caught_stack = NULL;
585 dsp->vv_exception = v_exception(NULL);
586 dsp->vv_throwpoint = v_throwpoint(NULL);
587
588 /* Necessary for debugging an inactive ":catch", ":finally", ":endtry" */
589 dsp->did_emsg = did_emsg; did_emsg = FALSE;
590 dsp->got_int = got_int; got_int = FALSE;
591 dsp->did_throw = did_throw; did_throw = FALSE;
592 dsp->need_rethrow = need_rethrow; need_rethrow = FALSE;
593 dsp->check_cstack = check_cstack; check_cstack = FALSE;
594 dsp->current_exception = current_exception; current_exception = NULL;
595}
596
597 static void
598restore_dbg_stuff(dsp)
599 struct dbg_stuff *dsp;
600{
601 suppress_errthrow = FALSE;
602 trylevel = dsp->trylevel;
603 force_abort = dsp->force_abort;
604 caught_stack = dsp->caught_stack;
605 (void)v_exception(dsp->vv_exception);
606 (void)v_throwpoint(dsp->vv_throwpoint);
607 did_emsg = dsp->did_emsg;
608 got_int = dsp->got_int;
609 did_throw = dsp->did_throw;
610 need_rethrow = dsp->need_rethrow;
611 check_cstack = dsp->check_cstack;
612 current_exception = dsp->current_exception;
613}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000614#endif
615
616
617/*
618 * do_exmode(): Repeatedly get commands for the "Ex" mode, until the ":vi"
619 * command is given.
620 */
621 void
622do_exmode(improved)
623 int improved; /* TRUE for "improved Ex" mode */
624{
625 int save_msg_scroll;
626 int prev_msg_row;
627 linenr_T prev_line;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000628 int changedtick;
629
630 if (improved)
631 exmode_active = EXMODE_VIM;
632 else
633 exmode_active = EXMODE_NORMAL;
634 State = NORMAL;
635
636 /* When using ":global /pat/ visual" and then "Q" we return to continue
637 * the :global command. */
638 if (global_busy)
639 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000640
641 save_msg_scroll = msg_scroll;
642 ++RedrawingDisabled; /* don't redisplay the window */
643 ++no_wait_return; /* don't wait for return */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000644#ifdef FEAT_GUI
645 /* Ignore scrollbar and mouse events in Ex mode */
646 ++hold_gui_events;
647#endif
648#ifdef FEAT_SNIFF
649 want_sniff_request = 0; /* No K_SNIFF wanted */
650#endif
651
652 MSG(_("Entering Ex mode. Type \"visual\" to go to Normal mode."));
653 while (exmode_active)
654 {
Bram Moolenaar7c626922005-02-07 22:01:03 +0000655#ifdef FEAT_EX_EXTRA
656 /* Check for a ":normal" command and no more characters left. */
657 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
658 {
659 exmode_active = FALSE;
660 break;
661 }
662#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663 msg_scroll = TRUE;
664 need_wait_return = FALSE;
665 ex_pressedreturn = FALSE;
666 ex_no_reprint = FALSE;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000667 changedtick = curbuf->b_changedtick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000668 prev_msg_row = msg_row;
669 prev_line = curwin->w_cursor.lnum;
670#ifdef FEAT_SNIFF
671 ProcessSniffRequests();
672#endif
673 if (improved)
674 {
675 cmdline_row = msg_row;
676 do_cmdline(NULL, getexline, NULL, 0);
677 }
678 else
679 do_cmdline(NULL, getexmodeline, NULL, DOCMD_NOWAIT);
680 lines_left = Rows - 1;
681
Bram Moolenaardf177f62005-02-22 08:39:57 +0000682 if ((prev_line != curwin->w_cursor.lnum
683 || changedtick != curbuf->b_changedtick) && !ex_no_reprint)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000685 if (curbuf->b_ml.ml_flags & ML_EMPTY)
686 EMSG(_(e_emptybuf));
687 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000689 if (ex_pressedreturn)
690 {
691 /* go up one line, to overwrite the ":<CR>" line, so the
Bram Moolenaar81870892007-11-11 18:17:28 +0000692 * output doesn't contain empty lines. */
Bram Moolenaardf177f62005-02-22 08:39:57 +0000693 msg_row = prev_msg_row;
694 if (prev_msg_row == Rows - 1)
695 msg_row--;
696 }
697 msg_col = 0;
698 print_line_no_prefix(curwin->w_cursor.lnum, FALSE, FALSE);
699 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000701 }
Bram Moolenaardf177f62005-02-22 08:39:57 +0000702 else if (ex_pressedreturn && !ex_no_reprint) /* must be at EOF */
703 {
704 if (curbuf->b_ml.ml_flags & ML_EMPTY)
705 EMSG(_(e_emptybuf));
706 else
707 EMSG(_("E501: At end-of-file"));
708 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709 }
710
711#ifdef FEAT_GUI
712 --hold_gui_events;
713#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000714 --RedrawingDisabled;
715 --no_wait_return;
716 update_screen(CLEAR);
717 need_wait_return = FALSE;
718 msg_scroll = save_msg_scroll;
719}
720
721/*
722 * Execute a simple command line. Used for translated commands like "*".
723 */
724 int
725do_cmdline_cmd(cmd)
726 char_u *cmd;
727{
728 return do_cmdline(cmd, NULL, NULL,
729 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
730}
731
732/*
733 * do_cmdline(): execute one Ex command line
734 *
735 * 1. Execute "cmdline" when it is not NULL.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100736 * If "cmdline" is NULL, or more lines are needed, fgetline() is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737 * 2. Split up in parts separated with '|'.
738 *
739 * This function can be called recursively!
740 *
741 * flags:
742 * DOCMD_VERBOSE - The command will be included in the error message.
743 * DOCMD_NOWAIT - Don't call wait_return() and friends.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100744 * DOCMD_REPEAT - Repeat execution until fgetline() returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000745 * DOCMD_KEYTYPED - Don't reset KeyTyped.
746 * DOCMD_EXCRESET - Reset the exception environment (used for debugging).
747 * DOCMD_KEEPLINE - Store first typed line (for repeating with ".").
748 *
749 * return FAIL if cmdline could not be executed, OK otherwise
750 */
751 int
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100752do_cmdline(cmdline, fgetline, cookie, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753 char_u *cmdline;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100754 char_u *(*fgetline) __ARGS((int, void *, int));
755 void *cookie; /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000756 int flags;
757{
758 char_u *next_cmdline; /* next cmd to execute */
759 char_u *cmdline_copy = NULL; /* copy of cmd line */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100760 int used_getline = FALSE; /* used "fgetline" to obtain command */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761 static int recursive = 0; /* recursive depth */
762 int msg_didout_before_start = 0;
763 int count = 0; /* line number count */
764 int did_inc = FALSE; /* incremented RedrawingDisabled */
765 int retval = OK;
766#ifdef FEAT_EVAL
767 struct condstack cstack; /* conditional stack */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000768 garray_T lines_ga; /* keep lines for ":while"/":for" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000769 int current_line = 0; /* active line in lines_ga */
770 char_u *fname = NULL; /* function or script name */
771 linenr_T *breakpoint = NULL; /* ptr to breakpoint field in cookie */
772 int *dbg_tick = NULL; /* ptr to dbg_tick field in cookie */
Bram Moolenaared203462004-06-16 11:19:22 +0000773 struct dbg_stuff debug_saved; /* saved things for debug mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000774 int initial_trylevel;
775 struct msglist **saved_msg_list = NULL;
776 struct msglist *private_msg_list;
777
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100778 /* "fgetline" and "cookie" passed to do_one_cmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779 char_u *(*cmd_getline) __ARGS((int, void *, int));
780 void *cmd_cookie;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000781 struct loop_cookie cmd_loop_cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782 void *real_cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000783 int getline_is_func;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784#else
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100785# define cmd_getline fgetline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786# define cmd_cookie cookie
787#endif
788 static int call_depth = 0; /* recursiveness */
789
790#ifdef FEAT_EVAL
791 /* For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory
792 * location for storing error messages to be converted to an exception.
Bram Moolenaarcf3630f2005-01-08 16:04:29 +0000793 * This ensures that the do_errthrow() call in do_one_cmd() does not
794 * combine the messages stored by an earlier invocation of do_one_cmd()
795 * with the command name of the later one. This would happen when
796 * BufWritePost autocommands are executed after a write error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000797 saved_msg_list = msg_list;
798 msg_list = &private_msg_list;
799 private_msg_list = NULL;
800#endif
801
802 /* It's possible to create an endless loop with ":execute", catch that
803 * here. The value of 200 allows nested function calls, ":source", etc. */
804 if (call_depth == 200)
805 {
806 EMSG(_("E169: Command too recursive"));
807#ifdef FEAT_EVAL
808 /* When converting to an exception, we do not include the command name
809 * since this is not an error of the specific command. */
810 do_errthrow((struct condstack *)NULL, (char_u *)NULL);
811 msg_list = saved_msg_list;
812#endif
813 return FAIL;
814 }
815 ++call_depth;
816
817#ifdef FEAT_EVAL
818 cstack.cs_idx = -1;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000819 cstack.cs_looplevel = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820 cstack.cs_trylevel = 0;
821 cstack.cs_emsg_silent_list = NULL;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000822 cstack.cs_lflags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823 ga_init2(&lines_ga, (int)sizeof(wcmd_T), 10);
824
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100825 real_cookie = getline_cookie(fgetline, cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826
827 /* Inside a function use a higher nesting level. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100828 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000829 if (getline_is_func && ex_nesting_level == func_level(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830 ++ex_nesting_level;
831
832 /* Get the function or script name and the address where the next breakpoint
833 * line and the debug tick for a function or script are stored. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000834 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000835 {
836 fname = func_name(real_cookie);
837 breakpoint = func_breakpoint(real_cookie);
838 dbg_tick = func_dbg_tick(real_cookie);
839 }
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100840 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000841 {
842 fname = sourcing_name;
843 breakpoint = source_breakpoint(real_cookie);
844 dbg_tick = source_dbg_tick(real_cookie);
845 }
846
847 /*
848 * Initialize "force_abort" and "suppress_errthrow" at the top level.
849 */
850 if (!recursive)
851 {
852 force_abort = FALSE;
853 suppress_errthrow = FALSE;
854 }
855
856 /*
857 * If requested, store and reset the global values controlling the
Bram Moolenaar89d40322006-08-29 15:30:07 +0000858 * exception handling (used when debugging). Otherwise clear it to avoid
859 * a bogus compiler warning when the optimizer uses inline functions...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000860 */
Bram Moolenaarb4872942006-05-13 10:32:52 +0000861 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +0000862 save_dbg_stuff(&debug_saved);
Bram Moolenaar89d40322006-08-29 15:30:07 +0000863 else
Bram Moolenaar7db5fc82010-05-24 11:59:29 +0200864 vim_memset(&debug_saved, 0, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000865
866 initial_trylevel = trylevel;
867
868 /*
869 * "did_throw" will be set to TRUE when an exception is being thrown.
870 */
871 did_throw = FALSE;
872#endif
873 /*
874 * "did_emsg" will be set to TRUE when emsg() is used, in which case we
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000875 * cancel the whole command line, and any if/endif or loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876 * If force_abort is set, we cancel everything.
877 */
878 did_emsg = FALSE;
879
880 /*
881 * KeyTyped is only set when calling vgetc(). Reset it here when not
882 * calling vgetc() (sourced command lines).
883 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100884 if (!(flags & DOCMD_KEYTYPED)
885 && !getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000886 KeyTyped = FALSE;
887
888 /*
889 * Continue executing command lines:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000890 * - when inside an ":if", ":while" or ":for"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000891 * - for multiple commands on one line, separated with '|'
892 * - when repeating until there are no more lines (for ":source")
893 */
894 next_cmdline = cmdline;
895 do
896 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000897#ifdef FEAT_EVAL
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100898 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000899#endif
900
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000901 /* stop skipping cmds for an error msg after all endif/while/for */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000902 if (next_cmdline == NULL
903#ifdef FEAT_EVAL
904 && !force_abort
905 && cstack.cs_idx < 0
Bram Moolenaar05159a02005-02-26 23:04:13 +0000906 && !(getline_is_func && func_has_abort(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000907#endif
908 )
909 did_emsg = FALSE;
910
911 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000912 * 1. If repeating a line in a loop, get a line from lines_ga.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100913 * 2. If no line given: Get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000914 * 3. If a line is given: Make a copy, so we can mess with it.
915 */
916
917#ifdef FEAT_EVAL
918 /* 1. If repeating, get a previous line from lines_ga. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000919 if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000920 {
921 /* Each '|' separated command is stored separately in lines_ga, to
922 * be able to jump to it. Don't use next_cmdline now. */
923 vim_free(cmdline_copy);
924 cmdline_copy = NULL;
925
926 /* Check if a function has returned or, unless it has an unclosed
927 * try conditional, aborted. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000928 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000930# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000931 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000932 func_line_end(real_cookie);
933# endif
934 if (func_has_ended(real_cookie))
935 {
936 retval = FAIL;
937 break;
938 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000940#ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000941 else if (do_profiling == PROF_YES
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100942 && getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000943 script_line_end();
944#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945
946 /* Check if a sourced file hit a ":finish" command. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100947 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000948 {
949 retval = FAIL;
950 break;
951 }
952
953 /* If breakpoints have been added/deleted need to check for it. */
954 if (breakpoint != NULL && dbg_tick != NULL
955 && *dbg_tick != debug_tick)
956 {
957 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100958 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000959 fname, sourcing_lnum);
960 *dbg_tick = debug_tick;
961 }
962
963 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
964 sourcing_lnum = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
965
966 /* Did we encounter a breakpoint? */
967 if (breakpoint != NULL && *breakpoint != 0
968 && *breakpoint <= sourcing_lnum)
969 {
970 dbg_breakpoint(fname, sourcing_lnum);
971 /* Find next breakpoint. */
972 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100973 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000974 fname, sourcing_lnum);
975 *dbg_tick = debug_tick;
976 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000977# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000978 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000979 {
980 if (getline_is_func)
981 func_line_start(real_cookie);
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100982 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000983 script_line_start();
984 }
985# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986 }
987
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000988 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000990 /* Inside a while/for loop we need to store the lines and use them
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100991 * again. Pass a different "fgetline" function to do_one_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992 * below, so that it stores lines in or reads them from
993 * "lines_ga". Makes it possible to define a function inside a
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000994 * while/for loop. */
995 cmd_getline = get_loop_line;
996 cmd_cookie = (void *)&cmd_loop_cookie;
997 cmd_loop_cookie.lines_gap = &lines_ga;
998 cmd_loop_cookie.current_line = current_line;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100999 cmd_loop_cookie.getline = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001000 cmd_loop_cookie.cookie = cookie;
1001 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002 }
1003 else
1004 {
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001005 cmd_getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006 cmd_cookie = cookie;
1007 }
1008#endif
1009
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001010 /* 2. If no line given, get an allocated line with fgetline(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001011 if (next_cmdline == NULL)
1012 {
1013 /*
1014 * Need to set msg_didout for the first line after an ":if",
1015 * otherwise the ":if" will be overwritten.
1016 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001017 if (count == 1 && getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001018 msg_didout = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001019 if (fgetline == NULL || (next_cmdline = fgetline(':', cookie,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001020#ifdef FEAT_EVAL
1021 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
1022#else
1023 0
1024#endif
1025 )) == NULL)
1026 {
1027 /* Don't call wait_return for aborted command line. The NULL
1028 * returned for the end of a sourced file or executed function
1029 * doesn't do this. */
1030 if (KeyTyped && !(flags & DOCMD_REPEAT))
1031 need_wait_return = FALSE;
1032 retval = FAIL;
1033 break;
1034 }
1035 used_getline = TRUE;
1036
1037 /*
1038 * Keep the first typed line. Clear it when more lines are typed.
1039 */
1040 if (flags & DOCMD_KEEPLINE)
1041 {
1042 vim_free(repeat_cmdline);
1043 if (count == 0)
1044 repeat_cmdline = vim_strsave(next_cmdline);
1045 else
1046 repeat_cmdline = NULL;
1047 }
1048 }
1049
1050 /* 3. Make a copy of the command so we can mess with it. */
1051 else if (cmdline_copy == NULL)
1052 {
1053 next_cmdline = vim_strsave(next_cmdline);
1054 if (next_cmdline == NULL)
1055 {
1056 EMSG(_(e_outofmem));
1057 retval = FAIL;
1058 break;
1059 }
1060 }
1061 cmdline_copy = next_cmdline;
1062
1063#ifdef FEAT_EVAL
1064 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001065 * Save the current line when inside a ":while" or ":for", and when
1066 * the command looks like a ":while" or ":for", because we may need it
1067 * later. When there is a '|' and another command, it is stored
1068 * separately, because we need to be able to jump back to it from an
1069 * :endwhile/:endfor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001070 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001071 if (current_line == lines_ga.ga_len
1072 && (cstack.cs_looplevel || has_loop_cmd(next_cmdline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001073 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001074 if (store_loop_line(&lines_ga, next_cmdline) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001075 {
1076 retval = FAIL;
1077 break;
1078 }
1079 }
1080 did_endif = FALSE;
1081#endif
1082
1083 if (count++ == 0)
1084 {
1085 /*
1086 * All output from the commands is put below each other, without
1087 * waiting for a return. Don't do this when executing commands
1088 * from a script or when being called recursive (e.g. for ":e
1089 * +command file").
1090 */
1091 if (!(flags & DOCMD_NOWAIT) && !recursive)
1092 {
1093 msg_didout_before_start = msg_didout;
1094 msg_didany = FALSE; /* no output yet */
1095 msg_start();
1096 msg_scroll = TRUE; /* put messages below each other */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001097 ++no_wait_return; /* don't wait for return until finished */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098 ++RedrawingDisabled;
1099 did_inc = TRUE;
1100 }
1101 }
1102
1103 if (p_verbose >= 15 && sourcing_name != NULL)
1104 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105 ++no_wait_return;
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001106 verbose_enter_scroll();
1107
Bram Moolenaar071d4272004-06-13 20:20:40 +00001108 smsg((char_u *)_("line %ld: %s"),
1109 (long)sourcing_lnum, cmdline_copy);
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001110 if (msg_silent == 0)
1111 msg_puts((char_u *)"\n"); /* don't overwrite this */
1112
1113 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114 --no_wait_return;
1115 }
1116
1117 /*
1118 * 2. Execute one '|' separated command.
1119 * do_one_cmd() will return NULL if there is no trailing '|'.
1120 * "cmdline_copy" can change, e.g. for '%' and '#' expansion.
1121 */
1122 ++recursive;
1123 next_cmdline = do_one_cmd(&cmdline_copy, flags & DOCMD_VERBOSE,
1124#ifdef FEAT_EVAL
1125 &cstack,
1126#endif
1127 cmd_getline, cmd_cookie);
1128 --recursive;
1129
1130#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001131 if (cmd_cookie == (void *)&cmd_loop_cookie)
1132 /* Use "current_line" from "cmd_loop_cookie", it may have been
Bram Moolenaar071d4272004-06-13 20:20:40 +00001133 * incremented when defining a function. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001134 current_line = cmd_loop_cookie.current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001135#endif
1136
1137 if (next_cmdline == NULL)
1138 {
1139 vim_free(cmdline_copy);
1140 cmdline_copy = NULL;
1141#ifdef FEAT_CMDHIST
1142 /*
1143 * If the command was typed, remember it for the ':' register.
1144 * Do this AFTER executing the command to make :@: work.
1145 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001146 if (getline_equal(fgetline, cookie, getexline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147 && new_last_cmdline != NULL)
1148 {
1149 vim_free(last_cmdline);
1150 last_cmdline = new_last_cmdline;
1151 new_last_cmdline = NULL;
1152 }
1153#endif
1154 }
1155 else
1156 {
1157 /* need to copy the command after the '|' to cmdline_copy, for the
1158 * next do_one_cmd() */
Bram Moolenaara7241f52008-06-24 20:39:31 +00001159 STRMOVE(cmdline_copy, next_cmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160 next_cmdline = cmdline_copy;
1161 }
1162
1163
1164#ifdef FEAT_EVAL
1165 /* reset did_emsg for a function that is not aborted by an error */
1166 if (did_emsg && !force_abort
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001167 && getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001168 && !func_has_abort(real_cookie))
1169 did_emsg = FALSE;
1170
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001171 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172 {
1173 ++current_line;
1174
1175 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001176 * An ":endwhile", ":endfor" and ":continue" is handled here.
1177 * If we were executing commands, jump back to the ":while" or
1178 * ":for".
1179 * If we were not executing commands, decrement cs_looplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001181 if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001182 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001183 cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001185 /* Jump back to the matching ":while" or ":for". Be careful
1186 * not to use a cs_line[] from an entry that isn't a ":while"
1187 * or ":for": It would make "current_line" invalid and can
1188 * cause a crash. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 if (!did_emsg && !got_int && !did_throw
1190 && cstack.cs_idx >= 0
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001191 && (cstack.cs_flags[cstack.cs_idx]
1192 & (CSF_WHILE | CSF_FOR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193 && cstack.cs_line[cstack.cs_idx] >= 0
1194 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
1195 {
1196 current_line = cstack.cs_line[cstack.cs_idx];
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001197 /* remember we jumped there */
1198 cstack.cs_lflags |= CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199 line_breakcheck(); /* check if CTRL-C typed */
1200
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001201 /* Check for the next breakpoint at or after the ":while"
1202 * or ":for". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203 if (breakpoint != NULL)
1204 {
1205 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001206 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207 fname,
1208 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
1209 *dbg_tick = debug_tick;
1210 }
1211 }
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001212 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001214 /* can only get here with ":endwhile" or ":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215 if (cstack.cs_idx >= 0)
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001216 rewind_conditionals(&cstack, cstack.cs_idx - 1,
1217 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218 }
1219 }
1220
1221 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001222 * For a ":while" or ":for" we need to remember the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001224 else if (cstack.cs_lflags & CSL_HAD_LOOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001226 cstack.cs_lflags &= ~CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001227 cstack.cs_line[cstack.cs_idx] = current_line - 1;
1228 }
1229 }
1230
1231 /*
1232 * When not inside any ":while" loop, clear remembered lines.
1233 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001234 if (cstack.cs_looplevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235 {
1236 if (lines_ga.ga_len > 0)
1237 {
1238 sourcing_lnum =
1239 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
1240 free_cmdlines(&lines_ga);
1241 }
1242 current_line = 0;
1243 }
1244
1245 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001246 * A ":finally" makes did_emsg, got_int, and did_throw pending for
1247 * being restored at the ":endtry". Reset them here and set the
1248 * ACTIVE and FINALLY flags, so that the finally clause gets executed.
1249 * This includes the case where a missing ":endif", ":endwhile" or
1250 * ":endfor" was detected by the ":finally" itself.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001251 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001252 if (cstack.cs_lflags & CSL_HAD_FINA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001253 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001254 cstack.cs_lflags &= ~CSL_HAD_FINA;
1255 report_make_pending(cstack.cs_pending[cstack.cs_idx]
1256 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001257 did_throw ? (void *)current_exception : NULL);
1258 did_emsg = got_int = did_throw = FALSE;
1259 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
1260 }
1261
1262 /* Update global "trylevel" for recursive calls to do_cmdline() from
1263 * within this loop. */
1264 trylevel = initial_trylevel + cstack.cs_trylevel;
1265
1266 /*
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001267 * If the outermost try conditional (across function calls and sourced
Bram Moolenaar071d4272004-06-13 20:20:40 +00001268 * files) is aborted because of an error, an interrupt, or an uncaught
1269 * exception, cancel everything. If it is left normally, reset
1270 * force_abort to get the non-EH compatible abortion behavior for
1271 * the rest of the script.
1272 */
1273 if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
1274 force_abort = FALSE;
1275
1276 /* Convert an interrupt to an exception if appropriate. */
1277 (void)do_intthrow(&cstack);
1278#endif /* FEAT_EVAL */
1279
1280 }
1281 /*
1282 * Continue executing command lines when:
1283 * - no CTRL-C typed, no aborting error, no exception thrown or try
1284 * conditionals need to be checked for executing finally clauses or
1285 * catching an interrupt exception
1286 * - didn't get an error message or lines are not typed
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001287 * - there is a command after '|', inside a :if, :while, :for or :try, or
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288 * looping for ":source" command or function call.
1289 */
1290 while (!((got_int
1291#ifdef FEAT_EVAL
1292 || (did_emsg && force_abort) || did_throw
1293#endif
1294 )
1295#ifdef FEAT_EVAL
1296 && cstack.cs_trylevel == 0
1297#endif
1298 )
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001299 && !(did_emsg
1300#ifdef FEAT_EVAL
1301 /* Keep going when inside try/catch, so that the error can be
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001302 * deal with, except when it is a syntax error, it may cause
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001303 * the :endtry to be missed. */
1304 && (cstack.cs_trylevel == 0 || did_emsg_syntax)
1305#endif
1306 && used_getline
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001307 && (getline_equal(fgetline, cookie, getexmodeline)
1308 || getline_equal(fgetline, cookie, getexline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001309 && (next_cmdline != NULL
1310#ifdef FEAT_EVAL
1311 || cstack.cs_idx >= 0
1312#endif
1313 || (flags & DOCMD_REPEAT)));
1314
1315 vim_free(cmdline_copy);
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001316 did_emsg_syntax = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001317#ifdef FEAT_EVAL
1318 free_cmdlines(&lines_ga);
1319 ga_clear(&lines_ga);
1320
1321 if (cstack.cs_idx >= 0)
1322 {
1323 /*
1324 * If a sourced file or executed function ran to its end, report the
1325 * unclosed conditional.
1326 */
1327 if (!got_int && !did_throw
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001328 && ((getline_equal(fgetline, cookie, getsourceline)
1329 && !source_finished(fgetline, cookie))
1330 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331 && !func_has_ended(real_cookie))))
1332 {
1333 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
1334 EMSG(_(e_endtry));
1335 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
1336 EMSG(_(e_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001337 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
1338 EMSG(_(e_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339 else
1340 EMSG(_(e_endif));
1341 }
1342
1343 /*
1344 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1345 * ":endtry" in a sourced file or executed function. If the try
1346 * conditional is in its finally clause, ignore anything pending.
1347 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001348 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349 */
1350 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001351 {
1352 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1353
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001354 if (idx >= 0)
1355 --idx; /* remove try block not in its finally clause */
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001356 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1357 &cstack.cs_looplevel);
1358 }
1359 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360 trylevel = initial_trylevel;
1361 }
1362
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001363 /* If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1364 * lack was reported above and the error message is to be converted to an
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365 * exception, do this now after rewinding the cstack. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001366 do_errthrow(&cstack, getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367 ? (char_u *)"endfunction" : (char_u *)NULL);
1368
1369 if (trylevel == 0)
1370 {
1371 /*
1372 * When an exception is being thrown out of the outermost try
1373 * conditional, discard the uncaught exception, disable the conversion
1374 * of interrupts or errors to exceptions, and ensure that no more
1375 * commands are executed.
1376 */
1377 if (did_throw)
1378 {
1379 void *p = NULL;
1380 char_u *saved_sourcing_name;
1381 int saved_sourcing_lnum;
1382 struct msglist *messages = NULL, *next;
1383
1384 /*
1385 * If the uncaught exception is a user exception, report it as an
1386 * error. If it is an error exception, display the saved error
1387 * message now. For an interrupt exception, do nothing; the
1388 * interrupt message is given elsewhere.
1389 */
1390 switch (current_exception->type)
1391 {
1392 case ET_USER:
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001393 vim_snprintf((char *)IObuff, IOSIZE,
1394 _("E605: Exception not caught: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395 current_exception->value);
1396 p = vim_strsave(IObuff);
1397 break;
1398 case ET_ERROR:
1399 messages = current_exception->messages;
1400 current_exception->messages = NULL;
1401 break;
1402 case ET_INTERRUPT:
1403 break;
1404 default:
1405 p = vim_strsave((char_u *)_(e_internal));
1406 }
1407
1408 saved_sourcing_name = sourcing_name;
1409 saved_sourcing_lnum = sourcing_lnum;
1410 sourcing_name = current_exception->throw_name;
1411 sourcing_lnum = current_exception->throw_lnum;
1412 current_exception->throw_name = NULL;
1413
1414 discard_current_exception(); /* uses IObuff if 'verbose' */
1415 suppress_errthrow = TRUE;
1416 force_abort = TRUE;
1417
1418 if (messages != NULL)
1419 {
1420 do
1421 {
1422 next = messages->next;
1423 emsg(messages->msg);
1424 vim_free(messages->msg);
1425 vim_free(messages);
1426 messages = next;
1427 }
1428 while (messages != NULL);
1429 }
1430 else if (p != NULL)
1431 {
1432 emsg(p);
1433 vim_free(p);
1434 }
1435 vim_free(sourcing_name);
1436 sourcing_name = saved_sourcing_name;
1437 sourcing_lnum = saved_sourcing_lnum;
1438 }
1439
1440 /*
1441 * On an interrupt or an aborting error not converted to an exception,
1442 * disable the conversion of errors to exceptions. (Interrupts are not
1443 * converted any more, here.) This enables also the interrupt message
1444 * when force_abort is set and did_emsg unset in case of an interrupt
1445 * from a finally clause after an error.
1446 */
1447 else if (got_int || (did_emsg && force_abort))
1448 suppress_errthrow = TRUE;
1449 }
1450
1451 /*
1452 * The current cstack will be freed when do_cmdline() returns. An uncaught
1453 * exception will have to be rethrown in the previous cstack. If a function
1454 * has just returned or a script file was just finished and the previous
1455 * cstack belongs to the same function or, respectively, script file, it
1456 * will have to be checked for finally clauses to be executed due to the
1457 * ":return" or ":finish". This is done in do_one_cmd().
1458 */
1459 if (did_throw)
1460 need_rethrow = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001461 if ((getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001462 && ex_nesting_level > source_level(real_cookie))
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001463 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001464 && ex_nesting_level > func_level(real_cookie) + 1))
1465 {
1466 if (!did_throw)
1467 check_cstack = TRUE;
1468 }
1469 else
1470 {
1471 /* When leaving a function, reduce nesting level. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001472 if (getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001473 --ex_nesting_level;
1474 /*
1475 * Go to debug mode when returning from a function in which we are
1476 * single-stepping.
1477 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001478 if ((getline_equal(fgetline, cookie, getsourceline)
1479 || getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001480 && ex_nesting_level + 1 <= debug_break_level)
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001481 do_debug(getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482 ? (char_u *)_("End of sourced file")
1483 : (char_u *)_("End of function"));
1484 }
1485
1486 /*
1487 * Restore the exception environment (done after returning from the
1488 * debugger).
1489 */
1490 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001491 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001492
1493 msg_list = saved_msg_list;
1494#endif /* FEAT_EVAL */
1495
1496 /*
1497 * If there was too much output to fit on the command line, ask the user to
1498 * hit return before redrawing the screen. With the ":global" command we do
1499 * this only once after the command is finished.
1500 */
1501 if (did_inc)
1502 {
1503 --RedrawingDisabled;
1504 --no_wait_return;
1505 msg_scroll = FALSE;
1506
1507 /*
1508 * When just finished an ":if"-":else" which was typed, no need to
1509 * wait for hit-return. Also for an error situation.
1510 */
1511 if (retval == FAIL
1512#ifdef FEAT_EVAL
1513 || (did_endif && KeyTyped && !did_emsg)
1514#endif
1515 )
1516 {
1517 need_wait_return = FALSE;
1518 msg_didany = FALSE; /* don't wait when restarting edit */
1519 }
1520 else if (need_wait_return)
1521 {
1522 /*
1523 * The msg_start() above clears msg_didout. The wait_return we do
1524 * here should not overwrite the command that may be shown before
1525 * doing that.
1526 */
1527 msg_didout |= msg_didout_before_start;
1528 wait_return(FALSE);
1529 }
1530 }
1531
Bram Moolenaar2a942252012-11-28 23:03:07 +01001532#ifdef FEAT_EVAL
1533 did_endif = FALSE; /* in case do_cmdline used recursively */
1534#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001535 /*
1536 * Reset if_level, in case a sourced script file contains more ":if" than
1537 * ":endif" (could be ":if x | foo | endif").
1538 */
1539 if_level = 0;
Bram Moolenaar2e18a122012-11-28 19:10:54 +01001540#endif
Bram Moolenaard4ad0d42012-11-28 17:34:48 +01001541
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542 --call_depth;
1543 return retval;
1544}
1545
1546#ifdef FEAT_EVAL
1547/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001548 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549 */
1550 static char_u *
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001551get_loop_line(c, cookie, indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552 int c;
1553 void *cookie;
1554 int indent;
1555{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001556 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001557 wcmd_T *wp;
1558 char_u *line;
1559
1560 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1561 {
1562 if (cp->repeating)
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001563 return NULL; /* trying to read past ":endwhile"/":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001564
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001565 /* First time inside the ":while"/":for": get line normally. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001566 if (cp->getline == NULL)
1567 line = getcmdline(c, 0L, indent);
1568 else
1569 line = cp->getline(c, cp->cookie, indent);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001570 if (line != NULL && store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571 ++cp->current_line;
1572
1573 return line;
1574 }
1575
1576 KeyTyped = FALSE;
1577 ++cp->current_line;
1578 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
1579 sourcing_lnum = wp->lnum;
1580 return vim_strsave(wp->line);
1581}
1582
1583/*
1584 * Store a line in "gap" so that a ":while" loop can execute it again.
1585 */
1586 static int
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001587store_loop_line(gap, line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001588 garray_T *gap;
1589 char_u *line;
1590{
1591 if (ga_grow(gap, 1) == FAIL)
1592 return FAIL;
1593 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
1594 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = sourcing_lnum;
1595 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596 return OK;
1597}
1598
1599/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001600 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001601 */
1602 static void
1603free_cmdlines(gap)
1604 garray_T *gap;
1605{
1606 while (gap->ga_len > 0)
1607 {
1608 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1609 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610 }
1611}
1612#endif
1613
1614/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001615 * If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals
1616 * "func". * Otherwise return TRUE when "fgetline" equals "func".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001617 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618 int
Bram Moolenaar89d40322006-08-29 15:30:07 +00001619getline_equal(fgetline, cookie, func)
1620 char_u *(*fgetline) __ARGS((int, void *, int));
Bram Moolenaar78a15312009-05-15 19:33:18 +00001621 void *cookie UNUSED; /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001622 char_u *(*func) __ARGS((int, void *, int));
1623{
1624#ifdef FEAT_EVAL
1625 char_u *(*gp) __ARGS((int, void *, int));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001626 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001627
Bram Moolenaar89d40322006-08-29 15:30:07 +00001628 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001629 * function that's originally used to obtain the lines. This may be
1630 * nested several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001631 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001632 cp = (struct loop_cookie *)cookie;
1633 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001634 {
1635 gp = cp->getline;
1636 cp = cp->cookie;
1637 }
1638 return gp == func;
1639#else
Bram Moolenaar89d40322006-08-29 15:30:07 +00001640 return fgetline == func;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001641#endif
1642}
1643
1644#if defined(FEAT_EVAL) || defined(FEAT_MBYTE) || defined(PROTO)
1645/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001646 * If "fgetline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001647 * getline function. Otherwise return "cookie".
1648 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001649 void *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001650getline_cookie(fgetline, cookie)
Bram Moolenaar78a15312009-05-15 19:33:18 +00001651 char_u *(*fgetline) __ARGS((int, void *, int)) UNUSED;
Bram Moolenaar89d40322006-08-29 15:30:07 +00001652 void *cookie; /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653{
1654# ifdef FEAT_EVAL
1655 char_u *(*gp) __ARGS((int, void *, int));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001656 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001657
Bram Moolenaar89d40322006-08-29 15:30:07 +00001658 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001659 * cookie that's originally used to obtain the lines. This may be nested
Bram Moolenaar071d4272004-06-13 20:20:40 +00001660 * several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001661 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001662 cp = (struct loop_cookie *)cookie;
1663 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001664 {
1665 gp = cp->getline;
1666 cp = cp->cookie;
1667 }
1668 return cp;
1669# else
1670 return cookie;
1671# endif
1672}
1673#endif
1674
1675/*
1676 * Execute one Ex command.
1677 *
1678 * If 'sourcing' is TRUE, the command will be included in the error message.
1679 *
1680 * 1. skip comment lines and leading space
1681 * 2. handle command modifiers
1682 * 3. parse range
1683 * 4. parse command
1684 * 5. parse arguments
1685 * 6. switch on command name
1686 *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001687 * Note: "fgetline" can be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001688 *
1689 * This function may be called recursively!
1690 */
1691#if (_MSC_VER == 1200)
1692/*
Bram Moolenaared203462004-06-16 11:19:22 +00001693 * Avoid optimisation bug in VC++ version 6.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001694 */
Bram Moolenaar281bdce2005-01-25 21:53:18 +00001695 #pragma optimize( "g", off )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001696#endif
1697 static char_u *
1698do_one_cmd(cmdlinep, sourcing,
1699#ifdef FEAT_EVAL
1700 cstack,
1701#endif
Bram Moolenaar89d40322006-08-29 15:30:07 +00001702 fgetline, cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001703 char_u **cmdlinep;
1704 int sourcing;
1705#ifdef FEAT_EVAL
1706 struct condstack *cstack;
1707#endif
Bram Moolenaar89d40322006-08-29 15:30:07 +00001708 char_u *(*fgetline) __ARGS((int, void *, int));
1709 void *cookie; /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710{
1711 char_u *p;
1712 linenr_T lnum;
1713 long n;
1714 char_u *errormsg = NULL; /* error message */
1715 exarg_T ea; /* Ex command arguments */
1716 long verbose_save = -1;
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001717 int save_msg_scroll = msg_scroll;
1718 int save_msg_silent = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719 int did_esilent = 0;
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001720#ifdef HAVE_SANDBOX
1721 int did_sandbox = FALSE;
1722#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723 cmdmod_T save_cmdmod;
1724 int ni; /* set when Not Implemented */
1725
1726 vim_memset(&ea, 0, sizeof(ea));
1727 ea.line1 = 1;
1728 ea.line2 = 1;
1729#ifdef FEAT_EVAL
1730 ++ex_nesting_level;
1731#endif
1732
Bram Moolenaar21691f82012-12-05 19:13:18 +01001733 /* When the last file has not been edited :q has to be typed twice. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734 if (quitmore
1735#ifdef FEAT_EVAL
1736 /* avoid that a function call in 'statusline' does this */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001737 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001738#endif
1739#ifdef FEAT_AUTOCMD
Bram Moolenaar21691f82012-12-05 19:13:18 +01001740 /* avoid that an autocommand, e.g. QuitPre, does this */
1741 && !getline_equal(fgetline, cookie, getnextac)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001742#endif
1743 )
1744 --quitmore;
1745
1746 /*
1747 * Reset browse, confirm, etc.. They are restored when returning, for
1748 * recursive calls.
1749 */
1750 save_cmdmod = cmdmod;
1751 vim_memset(&cmdmod, 0, sizeof(cmdmod));
1752
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001753 /* "#!anything" is handled like a comment. */
1754 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1755 goto doend;
1756
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757 /*
1758 * Repeat until no more command modifiers are found.
1759 */
1760 ea.cmd = *cmdlinep;
1761 for (;;)
1762 {
1763/*
1764 * 1. skip comment lines and leading white space and colons
1765 */
1766 while (*ea.cmd == ' ' || *ea.cmd == '\t' || *ea.cmd == ':')
1767 ++ea.cmd;
1768
1769 /* in ex mode, an empty line works like :+ */
1770 if (*ea.cmd == NUL && exmode_active
Bram Moolenaar89d40322006-08-29 15:30:07 +00001771 && (getline_equal(fgetline, cookie, getexmodeline)
1772 || getline_equal(fgetline, cookie, getexline))
Bram Moolenaardf177f62005-02-22 08:39:57 +00001773 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774 {
1775 ea.cmd = (char_u *)"+";
1776 ex_pressedreturn = TRUE;
1777 }
1778
1779 /* ignore comment and empty lines */
Bram Moolenaarf998c042007-11-20 11:31:26 +00001780 if (*ea.cmd == '"')
1781 goto doend;
1782 if (*ea.cmd == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001783 {
1784 ex_pressedreturn = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001785 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001786 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787
1788/*
1789 * 2. handle command modifiers.
1790 */
1791 p = ea.cmd;
1792 if (VIM_ISDIGIT(*ea.cmd))
1793 p = skipwhite(skipdigits(ea.cmd));
1794 switch (*p)
1795 {
1796 /* When adding an entry, also modify cmd_exists(). */
1797 case 'a': if (!checkforcmd(&ea.cmd, "aboveleft", 3))
1798 break;
1799#ifdef FEAT_WINDOWS
1800 cmdmod.split |= WSP_ABOVE;
1801#endif
1802 continue;
1803
1804 case 'b': if (checkforcmd(&ea.cmd, "belowright", 3))
1805 {
1806#ifdef FEAT_WINDOWS
1807 cmdmod.split |= WSP_BELOW;
1808#endif
1809 continue;
1810 }
1811 if (checkforcmd(&ea.cmd, "browse", 3))
1812 {
Bram Moolenaard812df62008-11-09 12:46:09 +00001813#ifdef FEAT_BROWSE_CMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814 cmdmod.browse = TRUE;
1815#endif
1816 continue;
1817 }
1818 if (!checkforcmd(&ea.cmd, "botright", 2))
1819 break;
1820#ifdef FEAT_WINDOWS
1821 cmdmod.split |= WSP_BOT;
1822#endif
1823 continue;
1824
1825 case 'c': if (!checkforcmd(&ea.cmd, "confirm", 4))
1826 break;
1827#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1828 cmdmod.confirm = TRUE;
1829#endif
1830 continue;
1831
1832 case 'k': if (checkforcmd(&ea.cmd, "keepmarks", 3))
1833 {
1834 cmdmod.keepmarks = TRUE;
1835 continue;
1836 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001837 if (checkforcmd(&ea.cmd, "keepalt", 5))
1838 {
1839 cmdmod.keepalt = TRUE;
1840 continue;
1841 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842 if (!checkforcmd(&ea.cmd, "keepjumps", 5))
1843 break;
1844 cmdmod.keepjumps = TRUE;
1845 continue;
1846
1847 /* ":hide" and ":hide | cmd" are not modifiers */
1848 case 'h': if (p != ea.cmd || !checkforcmd(&p, "hide", 3)
1849 || *p == NUL || ends_excmd(*p))
1850 break;
1851 ea.cmd = p;
1852 cmdmod.hide = TRUE;
1853 continue;
1854
1855 case 'l': if (checkforcmd(&ea.cmd, "lockmarks", 3))
1856 {
1857 cmdmod.lockmarks = TRUE;
1858 continue;
1859 }
1860
1861 if (!checkforcmd(&ea.cmd, "leftabove", 5))
1862 break;
1863#ifdef FEAT_WINDOWS
1864 cmdmod.split |= WSP_ABOVE;
1865#endif
1866 continue;
1867
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001868 case 'n': if (!checkforcmd(&ea.cmd, "noautocmd", 3))
1869 break;
1870#ifdef FEAT_AUTOCMD
1871 if (cmdmod.save_ei == NULL)
1872 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001873 /* Set 'eventignore' to "all". Restore the
1874 * existing option value later. */
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001875 cmdmod.save_ei = vim_strsave(p_ei);
1876 set_string_option_direct((char_u *)"ei", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001877 (char_u *)"all", OPT_FREE, SID_NONE);
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001878 }
1879#endif
1880 continue;
1881
Bram Moolenaar071d4272004-06-13 20:20:40 +00001882 case 'r': if (!checkforcmd(&ea.cmd, "rightbelow", 6))
1883 break;
1884#ifdef FEAT_WINDOWS
1885 cmdmod.split |= WSP_BELOW;
1886#endif
1887 continue;
1888
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001889 case 's': if (checkforcmd(&ea.cmd, "sandbox", 3))
1890 {
1891#ifdef HAVE_SANDBOX
1892 if (!did_sandbox)
1893 ++sandbox;
1894 did_sandbox = TRUE;
1895#endif
1896 continue;
1897 }
1898 if (!checkforcmd(&ea.cmd, "silent", 3))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899 break;
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001900 if (save_msg_silent == -1)
1901 save_msg_silent = msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001902 ++msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903 if (*ea.cmd == '!' && !vim_iswhite(ea.cmd[-1]))
1904 {
1905 /* ":silent!", but not "silent !cmd" */
1906 ea.cmd = skipwhite(ea.cmd + 1);
1907 ++emsg_silent;
1908 ++did_esilent;
1909 }
1910 continue;
1911
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001912 case 't': if (checkforcmd(&p, "tab", 3))
1913 {
1914#ifdef FEAT_WINDOWS
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001915 if (vim_isdigit(*ea.cmd))
1916 cmdmod.tab = atoi((char *)ea.cmd) + 1;
1917 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001918 cmdmod.tab = tabpage_index(curtab) + 1;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001919 ea.cmd = p;
1920#endif
1921 continue;
1922 }
1923 if (!checkforcmd(&ea.cmd, "topleft", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924 break;
1925#ifdef FEAT_WINDOWS
1926 cmdmod.split |= WSP_TOP;
1927#endif
1928 continue;
1929
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001930 case 'u': if (!checkforcmd(&ea.cmd, "unsilent", 3))
1931 break;
1932 if (save_msg_silent == -1)
1933 save_msg_silent = msg_silent;
1934 msg_silent = 0;
1935 continue;
1936
Bram Moolenaar071d4272004-06-13 20:20:40 +00001937 case 'v': if (checkforcmd(&ea.cmd, "vertical", 4))
1938 {
1939#ifdef FEAT_VERTSPLIT
1940 cmdmod.split |= WSP_VERT;
1941#endif
1942 continue;
1943 }
1944 if (!checkforcmd(&p, "verbose", 4))
1945 break;
1946 if (verbose_save < 0)
1947 verbose_save = p_verbose;
Bram Moolenaared203462004-06-16 11:19:22 +00001948 if (vim_isdigit(*ea.cmd))
1949 p_verbose = atoi((char *)ea.cmd);
1950 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951 p_verbose = 1;
1952 ea.cmd = p;
1953 continue;
1954 }
1955 break;
1956 }
1957
1958#ifdef FEAT_EVAL
1959 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
1960 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
1961#else
1962 ea.skip = (if_level > 0);
1963#endif
1964
1965#ifdef FEAT_EVAL
Bram Moolenaar05159a02005-02-26 23:04:13 +00001966# ifdef FEAT_PROFILE
1967 /* Count this line for profiling if ea.skip is FALSE. */
Bram Moolenaar371d5402006-03-20 21:47:49 +00001968 if (do_profiling == PROF_YES && !ea.skip)
Bram Moolenaar05159a02005-02-26 23:04:13 +00001969 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001970 if (getline_equal(fgetline, cookie, get_func_line))
1971 func_line_exec(getline_cookie(fgetline, cookie));
1972 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +00001973 script_line_exec();
1974 }
1975#endif
1976
Bram Moolenaar071d4272004-06-13 20:20:40 +00001977 /* May go to debug mode. If this happens and the ">quit" debug command is
1978 * used, throw an interrupt exception and skip the next command. */
1979 dbg_check_breakpoint(&ea);
1980 if (!ea.skip && got_int)
1981 {
1982 ea.skip = TRUE;
1983 (void)do_intthrow(cstack);
1984 }
1985#endif
1986
1987/*
1988 * 3. parse a range specifier of the form: addr [,addr] [;addr] ..
1989 *
1990 * where 'addr' is:
1991 *
1992 * % (entire file)
1993 * $ [+-NUM]
1994 * 'x [+-NUM] (where x denotes a currently defined mark)
1995 * . [+-NUM]
1996 * [+-NUM]..
1997 * NUM
1998 *
1999 * The ea.cmd pointer is updated to point to the first character following the
2000 * range spec. If an initial address is found, but no second, the upper bound
2001 * is equal to the lower.
2002 */
2003
2004 /* repeat for all ',' or ';' separated addresses */
2005 for (;;)
2006 {
2007 ea.line1 = ea.line2;
2008 ea.line2 = curwin->w_cursor.lnum; /* default is current line number */
2009 ea.cmd = skipwhite(ea.cmd);
2010 lnum = get_address(&ea.cmd, ea.skip, ea.addr_count == 0);
2011 if (ea.cmd == NULL) /* error detected */
2012 goto doend;
2013 if (lnum == MAXLNUM)
2014 {
2015 if (*ea.cmd == '%') /* '%' - all lines */
2016 {
2017 ++ea.cmd;
2018 ea.line1 = 1;
2019 ea.line2 = curbuf->b_ml.ml_line_count;
2020 ++ea.addr_count;
2021 }
2022 /* '*' - visual area */
2023 else if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
2024 {
2025 pos_T *fp;
2026
2027 ++ea.cmd;
2028 if (!ea.skip)
2029 {
2030 fp = getmark('<', FALSE);
2031 if (check_mark(fp) == FAIL)
2032 goto doend;
2033 ea.line1 = fp->lnum;
2034 fp = getmark('>', FALSE);
2035 if (check_mark(fp) == FAIL)
2036 goto doend;
2037 ea.line2 = fp->lnum;
2038 ++ea.addr_count;
2039 }
2040 }
2041 }
2042 else
2043 ea.line2 = lnum;
2044 ea.addr_count++;
2045
2046 if (*ea.cmd == ';')
2047 {
2048 if (!ea.skip)
2049 curwin->w_cursor.lnum = ea.line2;
2050 }
2051 else if (*ea.cmd != ',')
2052 break;
2053 ++ea.cmd;
2054 }
2055
2056 /* One address given: set start and end lines */
2057 if (ea.addr_count == 1)
2058 {
2059 ea.line1 = ea.line2;
2060 /* ... but only implicit: really no address given */
2061 if (lnum == MAXLNUM)
2062 ea.addr_count = 0;
2063 }
2064
2065 /* Don't leave the cursor on an illegal line (caused by ';') */
2066 check_cursor_lnum();
2067
2068/*
2069 * 4. parse command
2070 */
2071
2072 /*
2073 * Skip ':' and any white space
2074 */
2075 ea.cmd = skipwhite(ea.cmd);
2076 while (*ea.cmd == ':')
2077 ea.cmd = skipwhite(ea.cmd + 1);
2078
2079 /*
2080 * If we got a line, but no command, then go to the line.
2081 * If we find a '|' or '\n' we set ea.nextcmd.
2082 */
2083 if (*ea.cmd == NUL || *ea.cmd == '"' ||
2084 (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
2085 {
2086 /*
2087 * strange vi behaviour:
2088 * ":3" jumps to line 3
2089 * ":3|..." prints line 3
2090 * ":|" prints current line
2091 */
2092 if (ea.skip) /* skip this if inside :if */
2093 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00002094 if (*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002095 {
2096 ea.cmdidx = CMD_print;
2097 ea.argt = RANGE+COUNT+TRLBAR;
2098 if ((errormsg = invalid_range(&ea)) == NULL)
2099 {
2100 correct_range(&ea);
2101 ex_print(&ea);
2102 }
2103 }
2104 else if (ea.addr_count != 0)
2105 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002106 if (ea.line2 > curbuf->b_ml.ml_line_count)
2107 {
2108 /* With '-' in 'cpoptions' a line number past the file is an
2109 * error, otherwise put it at the end of the file. */
2110 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
2111 ea.line2 = -1;
2112 else
2113 ea.line2 = curbuf->b_ml.ml_line_count;
2114 }
2115
2116 if (ea.line2 < 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002117 errormsg = (char_u *)_(e_invrange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118 else
2119 {
2120 if (ea.line2 == 0)
2121 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002122 else
2123 curwin->w_cursor.lnum = ea.line2;
2124 beginline(BL_SOL | BL_FIX);
2125 }
2126 }
2127 goto doend;
2128 }
2129
2130 /* Find the command and let "p" point to after it. */
2131 p = find_command(&ea, NULL);
2132
2133#ifdef FEAT_USR_CMDS
2134 if (p == NULL)
2135 {
2136 if (!ea.skip)
2137 errormsg = (char_u *)_("E464: Ambiguous use of user-defined command");
2138 goto doend;
2139 }
2140 /* Check for wrong commands. */
2141 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78)
2142 {
2143 errormsg = uc_fun_cmd();
2144 goto doend;
2145 }
2146#endif
2147 if (ea.cmdidx == CMD_SIZE)
2148 {
2149 if (!ea.skip)
2150 {
2151 STRCPY(IObuff, _("E492: Not an editor command"));
2152 if (!sourcing)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002153 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002154 errormsg = IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02002155 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002156 }
2157 goto doend;
2158 }
2159
2160 ni = (
2161#ifdef FEAT_USR_CMDS
2162 !USER_CMDIDX(ea.cmdidx) &&
2163#endif
Bram Moolenaar3ebc1e52007-07-05 07:54:17 +00002164 (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00002165#ifdef HAVE_EX_SCRIPT_NI
2166 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
2167#endif
2168 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002169
2170#ifndef FEAT_EVAL
2171 /*
2172 * When the expression evaluation is disabled, recognize the ":if" and
2173 * ":endif" commands and ignore everything in between it.
2174 */
2175 if (ea.cmdidx == CMD_if)
2176 ++if_level;
2177 if (if_level)
2178 {
2179 if (ea.cmdidx == CMD_endif)
2180 --if_level;
2181 goto doend;
2182 }
2183
2184#endif
2185
Bram Moolenaar196b3b02008-06-20 16:51:41 +00002186 /* forced commands */
2187 if (*p == '!' && ea.cmdidx != CMD_substitute
2188 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002189 {
2190 ++p;
2191 ea.forceit = TRUE;
2192 }
2193 else
2194 ea.forceit = FALSE;
2195
2196/*
2197 * 5. parse arguments
2198 */
2199#ifdef FEAT_USR_CMDS
2200 if (!USER_CMDIDX(ea.cmdidx))
2201#endif
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002202 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002203
2204 if (!ea.skip)
2205 {
2206#ifdef HAVE_SANDBOX
2207 if (sandbox != 0 && !(ea.argt & SBOXOK))
2208 {
2209 /* Command not allowed in sandbox. */
2210 errormsg = (char_u *)_(e_sandbox);
2211 goto doend;
2212 }
2213#endif
2214 if (!curbuf->b_p_ma && (ea.argt & MODIFY))
2215 {
2216 /* Command not allowed in non-'modifiable' buffer */
2217 errormsg = (char_u *)_(e_modifiable);
2218 goto doend;
2219 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002220
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002221 if (text_locked() && !(ea.argt & CMDWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002222# ifdef FEAT_USR_CMDS
2223 && !USER_CMDIDX(ea.cmdidx)
2224# endif
2225 )
2226 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002227 /* Command not allowed when editing the command line. */
2228#ifdef FEAT_CMDWIN
2229 if (cmdwin_type != 0)
2230 errormsg = (char_u *)_(e_cmdwin);
2231 else
2232#endif
2233 errormsg = (char_u *)_(e_secure);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002234 goto doend;
2235 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002236#ifdef FEAT_AUTOCMD
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002237 /* Disallow editing another buffer when "curbuf_lock" is set.
2238 * Do allow ":edit" (check for argument later).
2239 * Do allow ":checktime" (it's postponed). */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002240 if (!(ea.argt & CMDWIN)
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002241 && ea.cmdidx != CMD_edit
2242 && ea.cmdidx != CMD_checktime
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002243# ifdef FEAT_USR_CMDS
2244 && !USER_CMDIDX(ea.cmdidx)
2245# endif
2246 && curbuf_locked())
2247 goto doend;
2248#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249
2250 if (!ni && !(ea.argt & RANGE) && ea.addr_count > 0)
2251 {
2252 /* no range allowed */
2253 errormsg = (char_u *)_(e_norange);
2254 goto doend;
2255 }
2256 }
2257
2258 if (!ni && !(ea.argt & BANG) && ea.forceit) /* no <!> allowed */
2259 {
2260 errormsg = (char_u *)_(e_nobang);
2261 goto doend;
2262 }
2263
2264 /*
2265 * Don't complain about the range if it is not used
2266 * (could happen if line_count is accidentally set to 0).
2267 */
2268 if (!ea.skip && !ni)
2269 {
2270 /*
2271 * If the range is backwards, ask for confirmation and, if given, swap
2272 * ea.line1 & ea.line2 so it's forwards again.
2273 * When global command is busy, don't ask, will fail below.
2274 */
2275 if (!global_busy && ea.line1 > ea.line2)
2276 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002277 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002278 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002279 if (sourcing || exmode_active)
2280 {
2281 errormsg = (char_u *)_("E493: Backwards range given");
2282 goto doend;
2283 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284 if (ask_yesno((char_u *)
2285 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002286 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002287 }
2288 lnum = ea.line1;
2289 ea.line1 = ea.line2;
2290 ea.line2 = lnum;
2291 }
2292 if ((errormsg = invalid_range(&ea)) != NULL)
2293 goto doend;
2294 }
2295
2296 if ((ea.argt & NOTADR) && ea.addr_count == 0) /* default is 1, not cursor */
2297 ea.line2 = 1;
2298
2299 correct_range(&ea);
2300
2301#ifdef FEAT_FOLDING
2302 if (((ea.argt & WHOLEFOLD) || ea.addr_count >= 2) && !global_busy)
2303 {
2304 /* Put the first line at the start of a closed fold, put the last line
2305 * at the end of a closed fold. */
2306 (void)hasFolding(ea.line1, &ea.line1, NULL);
2307 (void)hasFolding(ea.line2, NULL, &ea.line2);
2308 }
2309#endif
2310
2311#ifdef FEAT_QUICKFIX
2312 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002313 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314 * option here, so things like % get expanded.
2315 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002316 p = replace_makeprg(&ea, p, cmdlinep);
2317 if (p == NULL)
2318 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319#endif
2320
2321 /*
2322 * Skip to start of argument.
2323 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2324 */
2325 if (ea.cmdidx == CMD_bang)
2326 ea.arg = p;
2327 else
2328 ea.arg = skipwhite(p);
2329
2330 /*
2331 * Check for "++opt=val" argument.
2332 * Must be first, allow ":w ++enc=utf8 !cmd"
2333 */
2334 if (ea.argt & ARGOPT)
2335 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2336 if (getargopt(&ea) == FAIL && !ni)
2337 {
2338 errormsg = (char_u *)_(e_invarg);
2339 goto doend;
2340 }
2341
2342 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2343 {
2344 if (*ea.arg == '>') /* append */
2345 {
2346 if (*++ea.arg != '>') /* typed wrong */
2347 {
2348 errormsg = (char_u *)_("E494: Use w or w>>");
2349 goto doend;
2350 }
2351 ea.arg = skipwhite(ea.arg + 1);
2352 ea.append = TRUE;
2353 }
2354 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
2355 {
2356 ++ea.arg;
2357 ea.usefilter = TRUE;
2358 }
2359 }
2360
2361 if (ea.cmdidx == CMD_read)
2362 {
2363 if (ea.forceit)
2364 {
2365 ea.usefilter = TRUE; /* :r! filter if ea.forceit */
2366 ea.forceit = FALSE;
2367 }
2368 else if (*ea.arg == '!') /* :r !filter */
2369 {
2370 ++ea.arg;
2371 ea.usefilter = TRUE;
2372 }
2373 }
2374
2375 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2376 {
2377 ea.amount = 1;
2378 while (*ea.arg == *ea.cmd) /* count number of '>' or '<' */
2379 {
2380 ++ea.arg;
2381 ++ea.amount;
2382 }
2383 ea.arg = skipwhite(ea.arg);
2384 }
2385
2386 /*
2387 * Check for "+command" argument, before checking for next command.
2388 * Don't do this for ":read !cmd" and ":write !cmd".
2389 */
2390 if ((ea.argt & EDITCMD) && !ea.usefilter)
2391 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2392
2393 /*
2394 * Check for '|' to separate commands and '"' to start comments.
2395 * Don't do this for ":read !cmd" and ":write !cmd".
2396 */
2397 if ((ea.argt & TRLBAR) && !ea.usefilter)
2398 separate_nextcmd(&ea);
2399
2400 /*
2401 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002402 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2403 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002405 else if (ea.cmdidx == CMD_bang
2406 || ea.cmdidx == CMD_global
2407 || ea.cmdidx == CMD_vglobal
2408 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409 {
2410 for (p = ea.arg; *p; ++p)
2411 {
2412 /* Remove one backslash before a newline, so that it's possible to
2413 * pass a newline to the shell and also a newline that is preceded
2414 * with a backslash. This makes it impossible to end a shell
2415 * command in a backslash, but that doesn't appear useful.
2416 * Halving the number of backslashes is incompatible with previous
2417 * versions. */
2418 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002419 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420 else if (*p == '\n')
2421 {
2422 ea.nextcmd = p + 1;
2423 *p = NUL;
2424 break;
2425 }
2426 }
2427 }
2428
2429 if ((ea.argt & DFLALL) && ea.addr_count == 0)
2430 {
2431 ea.line1 = 1;
2432 ea.line2 = curbuf->b_ml.ml_line_count;
2433 }
2434
2435 /* accept numbered register only when no count allowed (:put) */
2436 if ( (ea.argt & REGSTR)
2437 && *ea.arg != NUL
2438#ifdef FEAT_USR_CMDS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002439 /* Do not allow register = for user commands */
2440 && (!USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441#endif
2442 && !((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)))
2443 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002444#ifndef FEAT_CLIPBOARD
2445 /* check these explicitly for a more specific error message */
2446 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002447 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002448 errormsg = (char_u *)_(e_invalidreg);
2449 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002450 }
2451#endif
Bram Moolenaar85de2062011-05-05 14:26:41 +02002452 if (
2453#ifdef FEAT_USR_CMDS
2454 valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2455 && USER_CMDIDX(ea.cmdidx)))
2456#else
2457 valid_yank_reg(*ea.arg, ea.cmdidx != CMD_put)
2458#endif
2459 )
2460 {
2461 ea.regname = *ea.arg++;
2462#ifdef FEAT_EVAL
2463 /* for '=' register: accept the rest of the line as an expression */
2464 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2465 {
2466 set_expr_line(vim_strsave(ea.arg));
2467 ea.arg += STRLEN(ea.arg);
2468 }
2469#endif
2470 ea.arg = skipwhite(ea.arg);
2471 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472 }
2473
2474 /*
2475 * Check for a count. When accepting a BUFNAME, don't use "123foo" as a
2476 * count, it's a buffer name.
2477 */
2478 if ((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)
2479 && (!(ea.argt & BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
2480 || vim_iswhite(*p)))
2481 {
2482 n = getdigits(&ea.arg);
2483 ea.arg = skipwhite(ea.arg);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002484 if (n <= 0 && !ni && (ea.argt & ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485 {
2486 errormsg = (char_u *)_(e_zerocount);
2487 goto doend;
2488 }
2489 if (ea.argt & NOTADR) /* e.g. :buffer 2, :sleep 3 */
2490 {
2491 ea.line2 = n;
2492 if (ea.addr_count == 0)
2493 ea.addr_count = 1;
2494 }
2495 else
2496 {
2497 ea.line1 = ea.line2;
2498 ea.line2 += n - 1;
2499 ++ea.addr_count;
2500 /*
2501 * Be vi compatible: no error message for out of range.
2502 */
2503 if (ea.line2 > curbuf->b_ml.ml_line_count)
2504 ea.line2 = curbuf->b_ml.ml_line_count;
2505 }
2506 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002507
2508 /*
2509 * Check for flags: 'l', 'p' and '#'.
2510 */
2511 if (ea.argt & EXFLAGS)
2512 get_flags(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002513 /* no arguments allowed */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002514 if (!ni && !(ea.argt & EXTRA) && *ea.arg != NUL
Bram Moolenaara2031822006-03-07 22:29:51 +00002515 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002516 {
2517 errormsg = (char_u *)_(e_trailing);
2518 goto doend;
2519 }
2520
2521 if (!ni && (ea.argt & NEEDARG) && *ea.arg == NUL)
2522 {
2523 errormsg = (char_u *)_(e_argreq);
2524 goto doend;
2525 }
2526
2527#ifdef FEAT_EVAL
2528 /*
2529 * Skip the command when it's not going to be executed.
2530 * The commands like :if, :endif, etc. always need to be executed.
2531 * Also make an exception for commands that handle a trailing command
2532 * themselves.
2533 */
2534 if (ea.skip)
2535 {
2536 switch (ea.cmdidx)
2537 {
2538 /* commands that need evaluation */
2539 case CMD_while:
2540 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002541 case CMD_for:
2542 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002543 case CMD_if:
2544 case CMD_elseif:
2545 case CMD_else:
2546 case CMD_endif:
2547 case CMD_try:
2548 case CMD_catch:
2549 case CMD_finally:
2550 case CMD_endtry:
2551 case CMD_function:
2552 break;
2553
2554 /* Commands that handle '|' themselves. Check: A command should
2555 * either have the TRLBAR flag, appear in this list or appear in
2556 * the list at ":help :bar". */
2557 case CMD_aboveleft:
2558 case CMD_and:
2559 case CMD_belowright:
2560 case CMD_botright:
2561 case CMD_browse:
2562 case CMD_call:
2563 case CMD_confirm:
2564 case CMD_delfunction:
2565 case CMD_djump:
2566 case CMD_dlist:
2567 case CMD_dsearch:
2568 case CMD_dsplit:
2569 case CMD_echo:
2570 case CMD_echoerr:
2571 case CMD_echomsg:
2572 case CMD_echon:
2573 case CMD_execute:
2574 case CMD_help:
2575 case CMD_hide:
2576 case CMD_ijump:
2577 case CMD_ilist:
2578 case CMD_isearch:
2579 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002580 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002581 case CMD_keepjumps:
2582 case CMD_keepmarks:
2583 case CMD_leftabove:
2584 case CMD_let:
2585 case CMD_lockmarks:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002586 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002587 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002588 case CMD_mzscheme:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002589 case CMD_perl:
2590 case CMD_psearch:
2591 case CMD_python:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002592 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002593 case CMD_python3:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002594 case CMD_return:
2595 case CMD_rightbelow:
2596 case CMD_ruby:
2597 case CMD_silent:
2598 case CMD_smagic:
2599 case CMD_snomagic:
2600 case CMD_substitute:
2601 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002602 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002603 case CMD_tcl:
2604 case CMD_throw:
2605 case CMD_tilde:
2606 case CMD_topleft:
2607 case CMD_unlet:
2608 case CMD_verbose:
2609 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002610 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002611 break;
2612
2613 default: goto doend;
2614 }
2615 }
2616#endif
2617
2618 if (ea.argt & XFILE)
2619 {
2620 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2621 goto doend;
2622 }
2623
2624#ifdef FEAT_LISTCMDS
2625 /*
2626 * Accept buffer name. Cannot be used at the same time with a buffer
2627 * number. Don't do this for a user command.
2628 */
2629 if ((ea.argt & BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
2630# ifdef FEAT_USR_CMDS
2631 && !USER_CMDIDX(ea.cmdidx)
2632# endif
2633 )
2634 {
2635 /*
2636 * :bdelete, :bwipeout and :bunload take several arguments, separated
2637 * by spaces: find next space (skipping over escaped characters).
2638 * The others take one argument: ignore trailing spaces.
2639 */
2640 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2641 || ea.cmdidx == CMD_bunload)
2642 p = skiptowhite_esc(ea.arg);
2643 else
2644 {
2645 p = ea.arg + STRLEN(ea.arg);
2646 while (p > ea.arg && vim_iswhite(p[-1]))
2647 --p;
2648 }
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002649 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & BUFUNL) != 0,
2650 FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651 if (ea.line2 < 0) /* failed */
2652 goto doend;
2653 ea.addr_count = 1;
2654 ea.arg = skipwhite(p);
2655 }
2656#endif
2657
2658/*
2659 * 6. switch on command name
2660 *
2661 * The "ea" structure holds the arguments that can be used.
2662 */
2663 ea.cmdlinep = cmdlinep;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002664 ea.getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665 ea.cookie = cookie;
2666#ifdef FEAT_EVAL
2667 ea.cstack = cstack;
2668#endif
2669
2670#ifdef FEAT_USR_CMDS
2671 if (USER_CMDIDX(ea.cmdidx))
2672 {
2673 /*
2674 * Execute a user-defined command.
2675 */
2676 do_ucmd(&ea);
2677 }
2678 else
2679#endif
2680 {
2681 /*
2682 * Call the function to execute the command.
2683 */
2684 ea.errmsg = NULL;
2685 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2686 if (ea.errmsg != NULL)
2687 errormsg = (char_u *)_(ea.errmsg);
2688 }
2689
2690#ifdef FEAT_EVAL
2691 /*
2692 * If the command just executed called do_cmdline(), any throw or ":return"
2693 * or ":finish" encountered there must also check the cstack of the still
2694 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2695 * exception, or reanimate a returned function or finished script file and
2696 * return or finish it again.
2697 */
2698 if (need_rethrow)
2699 do_throw(cstack);
2700 else if (check_cstack)
2701 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002702 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002703 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002704 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002705 && current_func_returned())
2706 do_return(&ea, TRUE, FALSE, NULL);
2707 }
2708 need_rethrow = check_cstack = FALSE;
2709#endif
2710
2711doend:
2712 if (curwin->w_cursor.lnum == 0) /* can happen with zero line number */
2713 curwin->w_cursor.lnum = 1;
2714
2715 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2716 {
2717 if (sourcing)
2718 {
2719 if (errormsg != IObuff)
2720 {
2721 STRCPY(IObuff, errormsg);
2722 errormsg = IObuff;
2723 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002724 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002725 }
2726 emsg(errormsg);
2727 }
2728#ifdef FEAT_EVAL
2729 do_errthrow(cstack,
2730 (ea.cmdidx != CMD_SIZE
2731# ifdef FEAT_USR_CMDS
2732 && !USER_CMDIDX(ea.cmdidx)
2733# endif
2734 ) ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
2735#endif
2736
2737 if (verbose_save >= 0)
2738 p_verbose = verbose_save;
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00002739#ifdef FEAT_AUTOCMD
2740 if (cmdmod.save_ei != NULL)
2741 {
2742 /* Restore 'eventignore' to the value before ":noautocmd". */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002743 set_string_option_direct((char_u *)"ei", -1, cmdmod.save_ei,
2744 OPT_FREE, SID_NONE);
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00002745 free_string_option(cmdmod.save_ei);
2746 }
2747#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002748
2749 cmdmod = save_cmdmod;
2750
Bram Moolenaar8e258a42009-07-09 13:55:43 +00002751 if (save_msg_silent != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002752 {
2753 /* messages could be enabled for a serious error, need to check if the
2754 * counters don't become negative */
Bram Moolenaarbecf4282009-09-30 11:24:36 +00002755 if (!did_emsg || msg_silent > save_msg_silent)
Bram Moolenaar8e258a42009-07-09 13:55:43 +00002756 msg_silent = save_msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002757 emsg_silent -= did_esilent;
2758 if (emsg_silent < 0)
2759 emsg_silent = 0;
2760 /* Restore msg_scroll, it's set by file I/O commands, even when no
2761 * message is actually displayed. */
2762 msg_scroll = save_msg_scroll;
Bram Moolenaar77ab2802009-04-22 12:44:48 +00002763
2764 /* "silent reg" or "silent echo x" inside "redir" leaves msg_col
2765 * somewhere in the line. Put it back in the first column. */
2766 if (redirecting())
2767 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002768 }
2769
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002770#ifdef HAVE_SANDBOX
2771 if (did_sandbox)
2772 --sandbox;
2773#endif
2774
Bram Moolenaar071d4272004-06-13 20:20:40 +00002775 if (ea.nextcmd && *ea.nextcmd == NUL) /* not really a next command */
2776 ea.nextcmd = NULL;
2777
2778#ifdef FEAT_EVAL
2779 --ex_nesting_level;
2780#endif
2781
2782 return ea.nextcmd;
2783}
2784#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00002785 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002786#endif
2787
2788/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002789 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002790 * If there is a match advance "pp" to the argument and return TRUE.
2791 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002792 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00002793checkforcmd(pp, cmd, len)
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002794 char_u **pp; /* start of command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002795 char *cmd; /* name of command */
2796 int len; /* required length */
2797{
2798 int i;
2799
2800 for (i = 0; cmd[i] != NUL; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00002801 if (((char_u *)cmd)[i] != (*pp)[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00002802 break;
2803 if (i >= len && !isalpha((*pp)[i]))
2804 {
2805 *pp = skipwhite(*pp + i);
2806 return TRUE;
2807 }
2808 return FALSE;
2809}
2810
2811/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002812 * Append "cmd" to the error message in IObuff.
2813 * Takes care of limiting the length and handling 0xa0, which would be
2814 * invisible otherwise.
2815 */
2816 static void
2817append_command(cmd)
2818 char_u *cmd;
2819{
2820 char_u *s = cmd;
2821 char_u *d;
2822
2823 STRCAT(IObuff, ": ");
2824 d = IObuff + STRLEN(IObuff);
2825 while (*s != NUL && d - IObuff < IOSIZE - 7)
2826 {
2827 if (
2828#ifdef FEAT_MBYTE
2829 enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) :
2830#endif
2831 *s == 0xa0)
2832 {
2833 s +=
2834#ifdef FEAT_MBYTE
2835 enc_utf8 ? 2 :
2836#endif
2837 1;
2838 STRCPY(d, "<a0>");
2839 d += 4;
2840 }
2841 else
2842 MB_COPY_CHAR(s, d);
2843 }
2844 *d = NUL;
2845}
2846
2847/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002848 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002849 * Start of the name can be found at eap->cmd.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002850 * Returns pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002851 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852 * Returns NULL for an ambiguous user command.
2853 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002854 static char_u *
2855find_command(eap, full)
2856 exarg_T *eap;
Bram Moolenaar78a15312009-05-15 19:33:18 +00002857 int *full UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858{
2859 int len;
2860 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00002861 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002862
2863 /*
2864 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00002865 * Exceptions:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002866 * - the 'k' command can directly be followed by any character.
2867 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
2868 * but :sre[wind] is another command, as are :scrip[tnames],
2869 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00002870 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002871 */
2872 p = eap->cmd;
2873 if (*p == 'k')
2874 {
2875 eap->cmdidx = CMD_k;
2876 ++p;
2877 }
2878 else if (p[0] == 's'
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002879 && ((p[1] == 'c' && p[2] != 's' && p[2] != 'r'
2880 && p[3] != 'i' && p[4] != 'p')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881 || p[1] == 'g'
2882 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
2883 || p[1] == 'I'
2884 || (p[1] == 'r' && p[2] != 'e')))
2885 {
2886 eap->cmdidx = CMD_substitute;
2887 ++p;
2888 }
2889 else
2890 {
2891 while (ASCII_ISALPHA(*p))
2892 ++p;
Bram Moolenaarb6590522010-07-21 16:00:43 +02002893 /* for python 3.x support ":py3", ":python3", ":py3file", etc. */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002894 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaarb6590522010-07-21 16:00:43 +02002895 while (ASCII_ISALNUM(*p))
2896 ++p;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02002897
Bram Moolenaar071d4272004-06-13 20:20:40 +00002898 /* check for non-alpha command */
2899 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
2900 ++p;
2901 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00002902 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
2903 {
2904 /* Check for ":dl", ":dell", etc. to ":deletel": that's
2905 * :delete with the 'l' flag. Same for 'p'. */
2906 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00002907 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00002908 break;
2909 if (i == len - 1)
2910 {
2911 --len;
2912 if (p[-1] == 'l')
2913 eap->flags |= EXFLAG_LIST;
2914 else
2915 eap->flags |= EXFLAG_PRINT;
2916 }
2917 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002918
2919 if (ASCII_ISLOWER(*eap->cmd))
2920 eap->cmdidx = cmdidxs[CharOrdLow(*eap->cmd)];
2921 else
2922 eap->cmdidx = cmdidxs[26];
2923
2924 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
2925 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
2926 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
2927 (size_t)len) == 0)
2928 {
2929#ifdef FEAT_EVAL
2930 if (full != NULL
2931 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
2932 *full = TRUE;
2933#endif
2934 break;
2935 }
2936
2937#ifdef FEAT_USR_CMDS
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01002938 /* Look for a user defined command as a last resort. Let ":Print" be
2939 * overruled by a user defined command. */
2940 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
2941 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002942 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002943 /* User defined commands may contain digits. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002944 while (ASCII_ISALNUM(*p))
2945 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002946 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947 }
2948#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002949 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002950 eap->cmdidx = CMD_SIZE;
2951 }
2952
2953 return p;
2954}
2955
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002956#ifdef FEAT_USR_CMDS
2957/*
2958 * Search for a user command that matches "eap->cmd".
2959 * Return cmdidx in "eap->cmdidx", flags in "eap->argt", idx in "eap->useridx".
2960 * Return a pointer to just after the command.
2961 * Return NULL if there is no matching command.
2962 */
2963 static char_u *
2964find_ucmd(eap, p, full, xp, compl)
2965 exarg_T *eap;
2966 char_u *p; /* end of the command (possibly including count) */
2967 int *full; /* set to TRUE for a full match */
2968 expand_T *xp; /* used for completion, NULL otherwise */
2969 int *compl; /* completion flags or NULL */
2970{
2971 int len = (int)(p - eap->cmd);
2972 int j, k, matchlen = 0;
2973 ucmd_T *uc;
2974 int found = FALSE;
2975 int possible = FALSE;
2976 char_u *cp, *np; /* Point into typed cmd and test name */
2977 garray_T *gap;
2978 int amb_local = FALSE; /* Found ambiguous buffer-local command,
2979 only full match global is accepted. */
2980
2981 /*
2982 * Look for buffer-local user commands first, then global ones.
2983 */
2984 gap = &curbuf->b_ucmds;
2985 for (;;)
2986 {
2987 for (j = 0; j < gap->ga_len; ++j)
2988 {
2989 uc = USER_CMD_GA(gap, j);
2990 cp = eap->cmd;
2991 np = uc->uc_name;
2992 k = 0;
2993 while (k < len && *np != NUL && *cp++ == *np++)
2994 k++;
2995 if (k == len || (*np == NUL && vim_isdigit(eap->cmd[k])))
2996 {
2997 /* If finding a second match, the command is ambiguous. But
2998 * not if a buffer-local command wasn't a full match and a
2999 * global command is a full match. */
3000 if (k == len && found && *np != NUL)
3001 {
3002 if (gap == &ucmds)
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003003 return NULL;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003004 amb_local = TRUE;
3005 }
3006
3007 if (!found || (k == len && *np == NUL))
3008 {
3009 /* If we matched up to a digit, then there could
3010 * be another command including the digit that we
3011 * should use instead.
3012 */
3013 if (k == len)
3014 found = TRUE;
3015 else
3016 possible = TRUE;
3017
3018 if (gap == &ucmds)
3019 eap->cmdidx = CMD_USER;
3020 else
3021 eap->cmdidx = CMD_USER_BUF;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003022 eap->argt = (long)uc->uc_argt;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003023 eap->useridx = j;
3024
3025# ifdef FEAT_CMDL_COMPL
3026 if (compl != NULL)
3027 *compl = uc->uc_compl;
3028# ifdef FEAT_EVAL
3029 if (xp != NULL)
3030 {
3031 xp->xp_arg = uc->uc_compl_arg;
3032 xp->xp_scriptID = uc->uc_scriptID;
3033 }
3034# endif
3035# endif
3036 /* Do not search for further abbreviations
3037 * if this is an exact match. */
3038 matchlen = k;
3039 if (k == len && *np == NUL)
3040 {
3041 if (full != NULL)
3042 *full = TRUE;
3043 amb_local = FALSE;
3044 break;
3045 }
3046 }
3047 }
3048 }
3049
3050 /* Stop if we found a full match or searched all. */
3051 if (j < gap->ga_len || gap == &ucmds)
3052 break;
3053 gap = &ucmds;
3054 }
3055
3056 /* Only found ambiguous matches. */
3057 if (amb_local)
3058 {
3059 if (xp != NULL)
3060 xp->xp_context = EXPAND_UNSUCCESSFUL;
3061 return NULL;
3062 }
3063
3064 /* The match we found may be followed immediately by a number. Move "p"
3065 * back to point to it. */
3066 if (found || possible)
3067 return p + (matchlen - len);
3068 return p;
3069}
3070#endif
3071
Bram Moolenaar071d4272004-06-13 20:20:40 +00003072#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003073static struct cmdmod
3074{
3075 char *name;
3076 int minlen;
3077 int has_count; /* :123verbose :3tab */
3078} cmdmods[] = {
3079 {"aboveleft", 3, FALSE},
3080 {"belowright", 3, FALSE},
3081 {"botright", 2, FALSE},
3082 {"browse", 3, FALSE},
3083 {"confirm", 4, FALSE},
3084 {"hide", 3, FALSE},
3085 {"keepalt", 5, FALSE},
3086 {"keepjumps", 5, FALSE},
3087 {"keepmarks", 3, FALSE},
3088 {"leftabove", 5, FALSE},
3089 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003090 {"noautocmd", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003091 {"rightbelow", 6, FALSE},
3092 {"sandbox", 3, FALSE},
3093 {"silent", 3, FALSE},
3094 {"tab", 3, TRUE},
3095 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003096 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003097 {"verbose", 4, TRUE},
3098 {"vertical", 4, FALSE},
3099};
3100
3101/*
3102 * Return length of a command modifier (including optional count).
3103 * Return zero when it's not a modifier.
3104 */
3105 int
3106modifier_len(cmd)
3107 char_u *cmd;
3108{
3109 int i, j;
3110 char_u *p = cmd;
3111
3112 if (VIM_ISDIGIT(*cmd))
3113 p = skipwhite(skipdigits(cmd));
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003114 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003115 {
3116 for (j = 0; p[j] != NUL; ++j)
3117 if (p[j] != cmdmods[i].name[j])
3118 break;
3119 if (!isalpha(p[j]) && j >= cmdmods[i].minlen
3120 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003121 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003122 }
3123 return 0;
3124}
3125
Bram Moolenaar071d4272004-06-13 20:20:40 +00003126/*
3127 * Return > 0 if an Ex command "name" exists.
3128 * Return 2 if there is an exact match.
3129 * Return 3 if there is an ambiguous match.
3130 */
3131 int
3132cmd_exists(name)
3133 char_u *name;
3134{
3135 exarg_T ea;
3136 int full = FALSE;
3137 int i;
3138 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003139 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003140
3141 /* Check command modifiers. */
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003142 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143 {
3144 for (j = 0; name[j] != NUL; ++j)
3145 if (name[j] != cmdmods[i].name[j])
3146 break;
3147 if (name[j] == NUL && j >= cmdmods[i].minlen)
3148 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3149 }
3150
Bram Moolenaara9587612006-05-04 21:47:50 +00003151 /* Check built-in commands and user defined commands.
3152 * For ":2match" and ":3match" we need to skip the number. */
3153 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003155 p = find_command(&ea, &full);
3156 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003157 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003158 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3159 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003160 if (*skipwhite(p) != NUL)
3161 return 0; /* trailing garbage */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003162 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3163}
3164#endif
3165
3166/*
3167 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
3168 * we don't need/want deleted. Maybe this could be done better if we didn't
3169 * repeat all this stuff. The only problem is that they may not stay
3170 * perfectly compatible with each other, but then the command line syntax
3171 * probably won't change that much -- webb.
3172 */
3173 char_u *
3174set_one_cmd_context(xp, buff)
3175 expand_T *xp;
3176 char_u *buff; /* buffer for command string */
3177{
3178 char_u *p;
3179 char_u *cmd, *arg;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003180 int len = 0;
3181 exarg_T ea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003182#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3183 int compl = EXPAND_NOTHING;
3184#endif
3185#ifdef FEAT_CMDL_COMPL
3186 int delim;
3187#endif
3188 int forceit = FALSE;
3189 int usefilter = FALSE; /* filter instead of file name */
3190
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003191 ExpandInit(xp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003192 xp->xp_pattern = buff;
3193 xp->xp_context = EXPAND_COMMANDS; /* Default until we get past command */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003194 ea.argt = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195
3196/*
3197 * 2. skip comment lines and leading space, colons or bars
3198 */
3199 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
3200 ;
3201 xp->xp_pattern = cmd;
3202
3203 if (*cmd == NUL)
3204 return NULL;
3205 if (*cmd == '"') /* ignore comment lines */
3206 {
3207 xp->xp_context = EXPAND_NOTHING;
3208 return NULL;
3209 }
3210
3211/*
3212 * 3. parse a range specifier of the form: addr [,addr] [;addr] ..
3213 */
3214 cmd = skip_range(cmd, &xp->xp_context);
3215
3216/*
3217 * 4. parse command
3218 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003219 xp->xp_pattern = cmd;
3220 if (*cmd == NUL)
3221 return NULL;
3222 if (*cmd == '"')
3223 {
3224 xp->xp_context = EXPAND_NOTHING;
3225 return NULL;
3226 }
3227
3228 if (*cmd == '|' || *cmd == '\n')
3229 return cmd + 1; /* There's another command */
3230
3231 /*
3232 * Isolate the command and search for it in the command table.
3233 * Exceptions:
3234 * - the 'k' command can directly be followed by any character, but
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003235 * do accept "keepmarks", "keepalt" and "keepjumps".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
3237 */
3238 if (*cmd == 'k' && cmd[1] != 'e')
3239 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003240 ea.cmdidx = CMD_k;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241 p = cmd + 1;
3242 }
3243 else
3244 {
3245 p = cmd;
3246 while (ASCII_ISALPHA(*p) || *p == '*') /* Allow * wild card */
3247 ++p;
3248 /* check for non-alpha command */
3249 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3250 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003251 len = (int)(p - cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003252
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003253 if (len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003254 {
3255 xp->xp_context = EXPAND_UNSUCCESSFUL;
3256 return NULL;
3257 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003258 for (ea.cmdidx = (cmdidx_T)0; (int)ea.cmdidx < (int)CMD_SIZE;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01003259 ea.cmdidx = (cmdidx_T)((int)ea.cmdidx + 1))
3260 if (STRNCMP(cmdnames[(int)ea.cmdidx].cmd_name, cmd,
3261 (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003262 break;
3263
3264#ifdef FEAT_USR_CMDS
3265 if (cmd[0] >= 'A' && cmd[0] <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003266 while (ASCII_ISALNUM(*p) || *p == '*') /* Allow * wild card */
3267 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003268#endif
3269 }
3270
3271 /*
3272 * If the cursor is touching the command, and it ends in an alpha-numeric
3273 * character, complete the command name.
3274 */
3275 if (*p == NUL && ASCII_ISALNUM(p[-1]))
3276 return NULL;
3277
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003278 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279 {
3280 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
3281 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003282 ea.cmdidx = CMD_substitute;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283 p = cmd + 1;
3284 }
3285#ifdef FEAT_USR_CMDS
3286 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
3287 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003288 ea.cmd = cmd;
3289 p = find_ucmd(&ea, p, NULL, xp,
3290# if defined(FEAT_CMDL_COMPL)
3291 &compl
3292# else
3293 NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003294# endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003295 );
Bram Moolenaarebefac62005-12-28 22:39:57 +00003296 if (p == NULL)
3297 ea.cmdidx = CMD_SIZE; /* ambiguous user command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003298 }
3299#endif
3300 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003301 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003302 {
3303 /* Not still touching the command and it was an illegal one */
3304 xp->xp_context = EXPAND_UNSUCCESSFUL;
3305 return NULL;
3306 }
3307
3308 xp->xp_context = EXPAND_NOTHING; /* Default now that we're past command */
3309
3310 if (*p == '!') /* forced commands */
3311 {
3312 forceit = TRUE;
3313 ++p;
3314 }
3315
3316/*
3317 * 5. parse arguments
3318 */
3319#ifdef FEAT_USR_CMDS
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003320 if (!USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321#endif
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003322 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323
3324 arg = skipwhite(p);
3325
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003326 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327 {
3328 if (*arg == '>') /* append */
3329 {
3330 if (*++arg == '>')
3331 ++arg;
3332 arg = skipwhite(arg);
3333 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003334 else if (*arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335 {
3336 ++arg;
3337 usefilter = TRUE;
3338 }
3339 }
3340
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003341 if (ea.cmdidx == CMD_read)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342 {
3343 usefilter = forceit; /* :r! filter if forced */
3344 if (*arg == '!') /* :r !filter */
3345 {
3346 ++arg;
3347 usefilter = TRUE;
3348 }
3349 }
3350
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003351 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352 {
3353 while (*arg == *cmd) /* allow any number of '>' or '<' */
3354 ++arg;
3355 arg = skipwhite(arg);
3356 }
3357
3358 /* Does command allow "+command"? */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003359 if ((ea.argt & EDITCMD) && !usefilter && *arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360 {
3361 /* Check if we're in the +command */
3362 p = arg + 1;
3363 arg = skip_cmd_arg(arg, FALSE);
3364
3365 /* Still touching the command after '+'? */
3366 if (*arg == NUL)
3367 return p;
3368
3369 /* Skip space(s) after +command to get to the real argument */
3370 arg = skipwhite(arg);
3371 }
3372
3373 /*
3374 * Check for '|' to separate commands and '"' to start comments.
3375 * Don't do this for ":read !cmd" and ":write !cmd".
3376 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003377 if ((ea.argt & TRLBAR) && !usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378 {
3379 p = arg;
3380 /* ":redir @" is not the start of a comment */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003381 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382 p += 2;
3383 while (*p)
3384 {
3385 if (*p == Ctrl_V)
3386 {
3387 if (p[1] != NUL)
3388 ++p;
3389 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003390 else if ( (*p == '"' && !(ea.argt & NOTRLCOM))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003391 || *p == '|' || *p == '\n')
3392 {
3393 if (*(p - 1) != '\\')
3394 {
3395 if (*p == '|' || *p == '\n')
3396 return p + 1;
3397 return NULL; /* It's a comment */
3398 }
3399 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003400 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401 }
3402 }
3403
3404 /* no arguments allowed */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003405 if (!(ea.argt & EXTRA) && *arg != NUL &&
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406 vim_strchr((char_u *)"|\"", *arg) == NULL)
3407 return NULL;
3408
3409 /* Find start of last argument (argument just before cursor): */
Bram Moolenaar848f8762012-07-25 17:22:23 +02003410 p = buff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411 xp->xp_pattern = p;
Bram Moolenaar09168a72012-08-02 21:24:42 +02003412 len = (int)STRLEN(buff);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003413 while (*p && p < buff + len)
3414 {
3415 if (*p == ' ' || *p == TAB)
3416 {
3417 /* argument starts after a space */
3418 xp->xp_pattern = ++p;
3419 }
3420 else
3421 {
3422 if (*p == '\\' && *(p + 1) != NUL)
3423 ++p; /* skip over escaped character */
3424 mb_ptr_adv(p);
3425 }
3426 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003428 if (ea.argt & XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003430 int c;
3431 int in_quote = FALSE;
3432 char_u *bow = NULL; /* Beginning of word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433
3434 /*
3435 * Allow spaces within back-quotes to count as part of the argument
3436 * being expanded.
3437 */
3438 xp->xp_pattern = skipwhite(arg);
Bram Moolenaar6529c102007-08-18 15:47:34 +00003439 p = xp->xp_pattern;
3440 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003441 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003442#ifdef FEAT_MBYTE
3443 if (has_mbyte)
3444 c = mb_ptr2char(p);
3445 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446#endif
Bram Moolenaar6529c102007-08-18 15:47:34 +00003447 c = *p;
3448 if (c == '\\' && p[1] != NUL)
3449 ++p;
3450 else if (c == '`')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451 {
3452 if (!in_quote)
3453 {
3454 xp->xp_pattern = p;
3455 bow = p + 1;
3456 }
3457 in_quote = !in_quote;
3458 }
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003459 /* An argument can contain just about everything, except
3460 * characters that end the command and white space. */
3461 else if (c == '|' || c == '\n' || c == '"' || (vim_iswhite(c)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003462#ifdef SPACE_IN_FILENAME
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003463 && (!(ea.argt & NOSPC) || usefilter)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003464#endif
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003465 ))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003466 {
Bram Moolenaarcf5a5b82008-02-26 20:30:12 +00003467 len = 0; /* avoid getting stuck when space is in 'isfname' */
Bram Moolenaar6529c102007-08-18 15:47:34 +00003468 while (*p != NUL)
3469 {
3470#ifdef FEAT_MBYTE
3471 if (has_mbyte)
3472 c = mb_ptr2char(p);
3473 else
3474#endif
3475 c = *p;
Bram Moolenaardd87969c2007-08-21 13:07:12 +00003476 if (c == '`' || vim_isfilec_or_wc(c))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003477 break;
3478#ifdef FEAT_MBYTE
3479 if (has_mbyte)
3480 len = (*mb_ptr2len)(p);
3481 else
3482#endif
3483 len = 1;
3484 mb_ptr_adv(p);
3485 }
3486 if (in_quote)
3487 bow = p;
3488 else
3489 xp->xp_pattern = p;
3490 p -= len;
3491 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003492 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493 }
3494
3495 /*
3496 * If we are still inside the quotes, and we passed a space, just
3497 * expand from there.
3498 */
3499 if (bow != NULL && in_quote)
3500 xp->xp_pattern = bow;
3501 xp->xp_context = EXPAND_FILES;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003502
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003503 /* For a shell command more chars need to be escaped. */
3504 if (usefilter || ea.cmdidx == CMD_bang)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003505 {
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003506#ifndef BACKSLASH_IN_FILENAME
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003507 xp->xp_shell = TRUE;
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003508#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003509 /* When still after the command name expand executables. */
3510 if (xp->xp_pattern == skipwhite(arg))
Bram Moolenaar5c5b0942007-05-06 12:07:59 +00003511 xp->xp_context = EXPAND_SHELLCMD;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003512 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003513
3514 /* Check for environment variable */
3515 if (*xp->xp_pattern == '$'
3516#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
3517 || *xp->xp_pattern == '%'
3518#endif
3519 )
3520 {
3521 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
3522 if (!vim_isIDc(*p))
3523 break;
3524 if (*p == NUL)
3525 {
3526 xp->xp_context = EXPAND_ENV_VARS;
3527 ++xp->xp_pattern;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003528#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3529 /* Avoid that the assignment uses EXPAND_FILES again. */
Bram Moolenaara466c992005-07-09 21:03:22 +00003530 if (compl != EXPAND_USER_DEFINED && compl != EXPAND_USER_LIST)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003531 compl = EXPAND_ENV_VARS;
3532#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533 }
3534 }
Bram Moolenaar24305862012-08-15 14:05:05 +02003535#if defined(FEAT_CMDL_COMPL)
3536 /* Check for user names */
3537 if (*xp->xp_pattern == '~')
3538 {
3539 for (p = xp->xp_pattern + 1; *p != NUL && *p != '/'; ++p)
3540 ;
3541 /* Complete ~user only if it partially matches a user name.
3542 * A full match ~user<Tab> will be replaced by user's home
3543 * directory i.e. something like ~user<Tab> -> /home/user/ */
3544 if (*p == NUL && p > xp->xp_pattern + 1
3545 && match_user(xp->xp_pattern + 1) == 1)
3546 {
3547 xp->xp_context = EXPAND_USER;
3548 ++xp->xp_pattern;
3549 }
3550 }
3551#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003552 }
3553
3554/*
3555 * 6. switch on command name
3556 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003557 switch (ea.cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003559 case CMD_find:
3560 case CMD_sfind:
3561 case CMD_tabfind:
Bram Moolenaarc24b6972010-08-16 22:34:29 +02003562 if (xp->xp_context == EXPAND_FILES)
3563 xp->xp_context = EXPAND_FILES_IN_PATH;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003564 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565 case CMD_cd:
3566 case CMD_chdir:
3567 case CMD_lcd:
3568 case CMD_lchdir:
3569 if (xp->xp_context == EXPAND_FILES)
3570 xp->xp_context = EXPAND_DIRECTORIES;
3571 break;
3572 case CMD_help:
3573 xp->xp_context = EXPAND_HELP;
3574 xp->xp_pattern = arg;
3575 break;
3576
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003577 /* Command modifiers: return the argument.
3578 * Also for commands with an argument that is a command. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579 case CMD_aboveleft:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003580 case CMD_argdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581 case CMD_belowright:
3582 case CMD_botright:
3583 case CMD_browse:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003584 case CMD_bufdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585 case CMD_confirm:
Bram Moolenaardf177f62005-02-22 08:39:57 +00003586 case CMD_debug:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587 case CMD_folddoclosed:
3588 case CMD_folddoopen:
3589 case CMD_hide:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003590 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591 case CMD_keepjumps:
3592 case CMD_keepmarks:
3593 case CMD_leftabove:
3594 case CMD_lockmarks:
3595 case CMD_rightbelow:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003596 case CMD_sandbox:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597 case CMD_silent:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003598 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599 case CMD_topleft:
3600 case CMD_verbose:
3601 case CMD_vertical:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003602 case CMD_windo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603 return arg;
3604
Bram Moolenaar4f688582007-07-24 12:34:30 +00003605#ifdef FEAT_CMDL_COMPL
3606# ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607 case CMD_match:
3608 if (*arg == NUL || !ends_excmd(*arg))
3609 {
Bram Moolenaar4f688582007-07-24 12:34:30 +00003610 /* also complete "None" */
3611 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612 arg = skipwhite(skiptowhite(arg));
3613 if (*arg != NUL)
3614 {
3615 xp->xp_context = EXPAND_NOTHING;
3616 arg = skip_regexp(arg + 1, *arg, p_magic, NULL);
3617 }
3618 }
3619 return find_nextcmd(arg);
Bram Moolenaar4f688582007-07-24 12:34:30 +00003620# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622/*
3623 * All completion for the +cmdline_compl feature goes here.
3624 */
3625
3626# ifdef FEAT_USR_CMDS
3627 case CMD_command:
3628 /* Check for attributes */
3629 while (*arg == '-')
3630 {
3631 arg++; /* Skip "-" */
3632 p = skiptowhite(arg);
3633 if (*p == NUL)
3634 {
3635 /* Cursor is still in the attribute */
3636 p = vim_strchr(arg, '=');
3637 if (p == NULL)
3638 {
3639 /* No "=", so complete attribute names */
3640 xp->xp_context = EXPAND_USER_CMD_FLAGS;
3641 xp->xp_pattern = arg;
3642 return NULL;
3643 }
3644
3645 /* For the -complete and -nargs attributes, we complete
3646 * their arguments as well.
3647 */
3648 if (STRNICMP(arg, "complete", p - arg) == 0)
3649 {
3650 xp->xp_context = EXPAND_USER_COMPLETE;
3651 xp->xp_pattern = p + 1;
3652 return NULL;
3653 }
3654 else if (STRNICMP(arg, "nargs", p - arg) == 0)
3655 {
3656 xp->xp_context = EXPAND_USER_NARGS;
3657 xp->xp_pattern = p + 1;
3658 return NULL;
3659 }
3660 return NULL;
3661 }
3662 arg = skipwhite(p);
3663 }
3664
3665 /* After the attributes comes the new command name */
3666 p = skiptowhite(arg);
3667 if (*p == NUL)
3668 {
3669 xp->xp_context = EXPAND_USER_COMMANDS;
3670 xp->xp_pattern = arg;
3671 break;
3672 }
3673
3674 /* And finally comes a normal command */
3675 return skipwhite(p);
3676
3677 case CMD_delcommand:
3678 xp->xp_context = EXPAND_USER_COMMANDS;
3679 xp->xp_pattern = arg;
3680 break;
3681# endif
3682
3683 case CMD_global:
3684 case CMD_vglobal:
3685 delim = *arg; /* get the delimiter */
3686 if (delim)
3687 ++arg; /* skip delimiter if there is one */
3688
3689 while (arg[0] != NUL && arg[0] != delim)
3690 {
3691 if (arg[0] == '\\' && arg[1] != NUL)
3692 ++arg;
3693 ++arg;
3694 }
3695 if (arg[0] != NUL)
3696 return arg + 1;
3697 break;
3698 case CMD_and:
3699 case CMD_substitute:
3700 delim = *arg;
3701 if (delim)
3702 {
3703 /* skip "from" part */
3704 ++arg;
3705 arg = skip_regexp(arg, delim, p_magic, NULL);
3706 }
3707 /* skip "to" part */
3708 while (arg[0] != NUL && arg[0] != delim)
3709 {
3710 if (arg[0] == '\\' && arg[1] != NUL)
3711 ++arg;
3712 ++arg;
3713 }
3714 if (arg[0] != NUL) /* skip delimiter */
3715 ++arg;
3716 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
3717 ++arg;
3718 if (arg[0] != NUL)
3719 return arg;
3720 break;
3721 case CMD_isearch:
3722 case CMD_dsearch:
3723 case CMD_ilist:
3724 case CMD_dlist:
3725 case CMD_ijump:
3726 case CMD_psearch:
3727 case CMD_djump:
3728 case CMD_isplit:
3729 case CMD_dsplit:
3730 arg = skipwhite(skipdigits(arg)); /* skip count */
3731 if (*arg == '/') /* Match regexp, not just whole words */
3732 {
3733 for (++arg; *arg && *arg != '/'; arg++)
3734 if (*arg == '\\' && arg[1] != NUL)
3735 arg++;
3736 if (*arg)
3737 {
3738 arg = skipwhite(arg + 1);
3739
3740 /* Check for trailing illegal characters */
3741 if (*arg && vim_strchr((char_u *)"|\"\n", *arg) == NULL)
3742 xp->xp_context = EXPAND_NOTHING;
3743 else
3744 return arg;
3745 }
3746 }
3747 break;
3748#ifdef FEAT_AUTOCMD
3749 case CMD_autocmd:
3750 return set_context_in_autocmd(xp, arg, FALSE);
3751
3752 case CMD_doautocmd:
Bram Moolenaar73a9d7b2008-11-06 16:16:44 +00003753 case CMD_doautoall:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003754 return set_context_in_autocmd(xp, arg, TRUE);
3755#endif
3756 case CMD_set:
3757 set_context_in_set_cmd(xp, arg, 0);
3758 break;
3759 case CMD_setglobal:
3760 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
3761 break;
3762 case CMD_setlocal:
3763 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
3764 break;
3765 case CMD_tag:
3766 case CMD_stag:
3767 case CMD_ptag:
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00003768 case CMD_ltag:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003769 case CMD_tselect:
3770 case CMD_stselect:
3771 case CMD_ptselect:
3772 case CMD_tjump:
3773 case CMD_stjump:
3774 case CMD_ptjump:
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003775 if (*p_wop != NUL)
3776 xp->xp_context = EXPAND_TAGS_LISTFILES;
3777 else
3778 xp->xp_context = EXPAND_TAGS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779 xp->xp_pattern = arg;
3780 break;
3781 case CMD_augroup:
3782 xp->xp_context = EXPAND_AUGROUP;
3783 xp->xp_pattern = arg;
3784 break;
3785#ifdef FEAT_SYN_HL
3786 case CMD_syntax:
3787 set_context_in_syntax_cmd(xp, arg);
3788 break;
3789#endif
3790#ifdef FEAT_EVAL
3791 case CMD_let:
3792 case CMD_if:
3793 case CMD_elseif:
3794 case CMD_while:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00003795 case CMD_for:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796 case CMD_echo:
3797 case CMD_echon:
3798 case CMD_execute:
3799 case CMD_echomsg:
3800 case CMD_echoerr:
3801 case CMD_call:
3802 case CMD_return:
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003803 set_context_for_expression(xp, arg, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804 break;
3805
3806 case CMD_unlet:
3807 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3808 arg = xp->xp_pattern + 1;
3809 xp->xp_context = EXPAND_USER_VARS;
3810 xp->xp_pattern = arg;
3811 break;
3812
3813 case CMD_function:
3814 case CMD_delfunction:
3815 xp->xp_context = EXPAND_USER_FUNC;
3816 xp->xp_pattern = arg;
3817 break;
3818
3819 case CMD_echohl:
Bram Moolenaar4f688582007-07-24 12:34:30 +00003820 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003821 break;
3822#endif
3823 case CMD_highlight:
3824 set_context_in_highlight_cmd(xp, arg);
3825 break;
Bram Moolenaarf4580d82009-03-18 11:52:53 +00003826#ifdef FEAT_CSCOPE
3827 case CMD_cscope:
Bram Moolenaar7bfef802009-04-22 14:25:01 +00003828 case CMD_lcscope:
3829 case CMD_scscope:
3830 set_context_in_cscope_cmd(xp, arg, ea.cmdidx);
Bram Moolenaarf4580d82009-03-18 11:52:53 +00003831 break;
3832#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00003833#ifdef FEAT_SIGNS
3834 case CMD_sign:
3835 set_context_in_sign_cmd(xp, arg);
3836 break;
3837#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003838#ifdef FEAT_LISTCMDS
3839 case CMD_bdelete:
3840 case CMD_bwipeout:
3841 case CMD_bunload:
3842 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3843 arg = xp->xp_pattern + 1;
3844 /*FALLTHROUGH*/
3845 case CMD_buffer:
3846 case CMD_sbuffer:
3847 case CMD_checktime:
3848 xp->xp_context = EXPAND_BUFFERS;
3849 xp->xp_pattern = arg;
3850 break;
3851#endif
3852#ifdef FEAT_USR_CMDS
3853 case CMD_USER:
3854 case CMD_USER_BUF:
3855 if (compl != EXPAND_NOTHING)
3856 {
3857 /* XFILE: file names are handled above */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003858 if (!(ea.argt & XFILE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859 {
3860# ifdef FEAT_MENU
3861 if (compl == EXPAND_MENUS)
3862 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
3863# endif
3864 if (compl == EXPAND_COMMANDS)
3865 return arg;
3866 if (compl == EXPAND_MAPPINGS)
3867 return set_context_in_map_cmd(xp, (char_u *)"map",
3868 arg, forceit, FALSE, FALSE, CMD_map);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003869 /* Find start of last argument. */
3870 p = arg;
3871 while (*p)
3872 {
3873 if (*p == ' ')
Bram Moolenaar848f8762012-07-25 17:22:23 +02003874 /* argument starts after a space */
3875 arg = p + 1;
Bram Moolenaara07c8312012-07-27 21:12:07 +02003876 else if (*p == '\\' && *(p + 1) != NUL)
3877 ++p; /* skip over escaped character */
3878 mb_ptr_adv(p);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003879 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880 xp->xp_pattern = arg;
3881 }
3882 xp->xp_context = compl;
3883 }
3884 break;
3885#endif
3886 case CMD_map: case CMD_noremap:
3887 case CMD_nmap: case CMD_nnoremap:
3888 case CMD_vmap: case CMD_vnoremap:
3889 case CMD_omap: case CMD_onoremap:
3890 case CMD_imap: case CMD_inoremap:
3891 case CMD_cmap: case CMD_cnoremap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003892 case CMD_lmap: case CMD_lnoremap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02003893 case CMD_smap: case CMD_snoremap:
3894 case CMD_xmap: case CMD_xnoremap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003895 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003896 FALSE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003897 case CMD_unmap:
3898 case CMD_nunmap:
3899 case CMD_vunmap:
3900 case CMD_ounmap:
3901 case CMD_iunmap:
3902 case CMD_cunmap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003903 case CMD_lunmap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02003904 case CMD_sunmap:
3905 case CMD_xunmap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003906 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003907 FALSE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003908 case CMD_abbreviate: case CMD_noreabbrev:
3909 case CMD_cabbrev: case CMD_cnoreabbrev:
3910 case CMD_iabbrev: case CMD_inoreabbrev:
3911 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003912 TRUE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003913 case CMD_unabbreviate:
3914 case CMD_cunabbrev:
3915 case CMD_iunabbrev:
3916 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003917 TRUE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003918#ifdef FEAT_MENU
3919 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
3920 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
3921 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
3922 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
3923 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
3924 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
3925 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
3926 case CMD_tmenu: case CMD_tunmenu:
3927 case CMD_popup: case CMD_tearoff: case CMD_emenu:
3928 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
3929#endif
3930
3931 case CMD_colorscheme:
3932 xp->xp_context = EXPAND_COLORS;
3933 xp->xp_pattern = arg;
3934 break;
3935
3936 case CMD_compiler:
3937 xp->xp_context = EXPAND_COMPILER;
3938 xp->xp_pattern = arg;
3939 break;
3940
Bram Moolenaar883f5d02010-06-21 06:24:34 +02003941 case CMD_ownsyntax:
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02003942 xp->xp_context = EXPAND_OWNSYNTAX;
3943 xp->xp_pattern = arg;
3944 break;
3945
3946 case CMD_setfiletype:
Bram Moolenaar883f5d02010-06-21 06:24:34 +02003947 xp->xp_context = EXPAND_FILETYPE;
3948 xp->xp_pattern = arg;
3949 break;
3950
Bram Moolenaar071d4272004-06-13 20:20:40 +00003951#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
3952 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
3953 case CMD_language:
Bram Moolenaara660dc82011-05-25 12:51:22 +02003954 p = skiptowhite(arg);
3955 if (*p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956 {
3957 xp->xp_context = EXPAND_LANGUAGE;
3958 xp->xp_pattern = arg;
3959 }
3960 else
Bram Moolenaara660dc82011-05-25 12:51:22 +02003961 {
3962 if ( STRNCMP(arg, "messages", p - arg) == 0
3963 || STRNCMP(arg, "ctype", p - arg) == 0
3964 || STRNCMP(arg, "time", p - arg) == 0)
3965 {
3966 xp->xp_context = EXPAND_LOCALES;
3967 xp->xp_pattern = skipwhite(p);
3968 }
3969 else
3970 xp->xp_context = EXPAND_NOTHING;
3971 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003972 break;
3973#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01003974#if defined(FEAT_PROFILE)
3975 case CMD_profile:
3976 set_context_in_profile_cmd(xp, arg);
3977 break;
3978#endif
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01003979 case CMD_behave:
3980 xp->xp_context = EXPAND_BEHAVE;
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02003981 xp->xp_pattern = arg;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01003982 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003983
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02003984#if defined(FEAT_CMDHIST)
3985 case CMD_history:
3986 xp->xp_context = EXPAND_HISTORY;
3987 xp->xp_pattern = arg;
3988 break;
3989#endif
3990
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991#endif /* FEAT_CMDL_COMPL */
3992
3993 default:
3994 break;
3995 }
3996 return NULL;
3997}
3998
3999/*
4000 * skip a range specifier of the form: addr [,addr] [;addr] ..
4001 *
4002 * Backslashed delimiters after / or ? will be skipped, and commands will
4003 * not be expanded between /'s and ?'s or after "'".
4004 *
Bram Moolenaardf177f62005-02-22 08:39:57 +00004005 * Also skip white space and ":" characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006 * Returns the "cmd" pointer advanced to beyond the range.
4007 */
4008 char_u *
4009skip_range(cmd, ctx)
4010 char_u *cmd;
4011 int *ctx; /* pointer to xp_context or NULL */
4012{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004013 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004014
Bram Moolenaardf177f62005-02-22 08:39:57 +00004015 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016 {
4017 if (*cmd == '\'')
4018 {
4019 if (*++cmd == NUL && ctx != NULL)
4020 *ctx = EXPAND_NOTHING;
4021 }
4022 else if (*cmd == '/' || *cmd == '?')
4023 {
4024 delim = *cmd++;
4025 while (*cmd != NUL && *cmd != delim)
4026 if (*cmd++ == '\\' && *cmd != NUL)
4027 ++cmd;
4028 if (*cmd == NUL && ctx != NULL)
4029 *ctx = EXPAND_NOTHING;
4030 }
4031 if (*cmd != NUL)
4032 ++cmd;
4033 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004034
4035 /* Skip ":" and white space. */
4036 while (*cmd == ':')
4037 cmd = skipwhite(cmd + 1);
4038
Bram Moolenaar071d4272004-06-13 20:20:40 +00004039 return cmd;
4040}
4041
4042/*
4043 * get a single EX address
4044 *
4045 * Set ptr to the next character after the part that was interpreted.
4046 * Set ptr to NULL when an error is encountered.
4047 *
4048 * Return MAXLNUM when no Ex address was found.
4049 */
4050 static linenr_T
4051get_address(ptr, skip, to_other_file)
4052 char_u **ptr;
4053 int skip; /* only skip the address, don't use it */
4054 int to_other_file; /* flag: may jump to other file */
4055{
4056 int c;
4057 int i;
4058 long n;
4059 char_u *cmd;
4060 pos_T pos;
4061 pos_T *fp;
4062 linenr_T lnum;
4063
4064 cmd = skipwhite(*ptr);
4065 lnum = MAXLNUM;
4066 do
4067 {
4068 switch (*cmd)
4069 {
4070 case '.': /* '.' - Cursor position */
4071 ++cmd;
4072 lnum = curwin->w_cursor.lnum;
4073 break;
4074
4075 case '$': /* '$' - last line */
4076 ++cmd;
4077 lnum = curbuf->b_ml.ml_line_count;
4078 break;
4079
4080 case '\'': /* ''' - mark */
4081 if (*++cmd == NUL)
4082 {
4083 cmd = NULL;
4084 goto error;
4085 }
4086 if (skip)
4087 ++cmd;
4088 else
4089 {
4090 /* Only accept a mark in another file when it is
4091 * used by itself: ":'M". */
4092 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
4093 ++cmd;
4094 if (fp == (pos_T *)-1)
4095 /* Jumped to another file. */
4096 lnum = curwin->w_cursor.lnum;
4097 else
4098 {
4099 if (check_mark(fp) == FAIL)
4100 {
4101 cmd = NULL;
4102 goto error;
4103 }
4104 lnum = fp->lnum;
4105 }
4106 }
4107 break;
4108
4109 case '/':
4110 case '?': /* '/' or '?' - search */
4111 c = *cmd++;
4112 if (skip) /* skip "/pat/" */
4113 {
4114 cmd = skip_regexp(cmd, c, (int)p_magic, NULL);
4115 if (*cmd == c)
4116 ++cmd;
4117 }
4118 else
4119 {
4120 pos = curwin->w_cursor; /* save curwin->w_cursor */
4121 /*
4122 * When '/' or '?' follows another address, start
4123 * from there.
4124 */
4125 if (lnum != MAXLNUM)
4126 curwin->w_cursor.lnum = lnum;
4127 /*
4128 * Start a forward search at the end of the line.
4129 * Start a backward search at the start of the line.
4130 * This makes sure we never match in the current
4131 * line, and can match anywhere in the
4132 * next/previous line.
4133 */
4134 if (c == '/')
4135 curwin->w_cursor.col = MAXCOL;
4136 else
4137 curwin->w_cursor.col = 0;
4138 searchcmdlen = 0;
4139 if (!do_search(NULL, c, cmd, 1L,
Bram Moolenaaraad86642008-03-10 20:34:59 +00004140 SEARCH_HIS | SEARCH_MSG, NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141 {
4142 curwin->w_cursor = pos;
4143 cmd = NULL;
4144 goto error;
4145 }
4146 lnum = curwin->w_cursor.lnum;
4147 curwin->w_cursor = pos;
4148 /* adjust command string pointer */
4149 cmd += searchcmdlen;
4150 }
4151 break;
4152
4153 case '\\': /* "\?", "\/" or "\&", repeat search */
4154 ++cmd;
4155 if (*cmd == '&')
4156 i = RE_SUBST;
4157 else if (*cmd == '?' || *cmd == '/')
4158 i = RE_SEARCH;
4159 else
4160 {
4161 EMSG(_(e_backslash));
4162 cmd = NULL;
4163 goto error;
4164 }
4165
4166 if (!skip)
4167 {
4168 /*
4169 * When search follows another address, start from
4170 * there.
4171 */
4172 if (lnum != MAXLNUM)
4173 pos.lnum = lnum;
4174 else
4175 pos.lnum = curwin->w_cursor.lnum;
4176
4177 /*
4178 * Start the search just like for the above
4179 * do_search().
4180 */
4181 if (*cmd != '?')
4182 pos.col = MAXCOL;
4183 else
4184 pos.col = 0;
4185 if (searchit(curwin, curbuf, &pos,
4186 *cmd == '?' ? BACKWARD : FORWARD,
Bram Moolenaaraad86642008-03-10 20:34:59 +00004187 (char_u *)"", 1L, SEARCH_MSG,
Bram Moolenaar76929292008-01-06 19:07:36 +00004188 i, (linenr_T)0, NULL) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189 lnum = pos.lnum;
4190 else
4191 {
4192 cmd = NULL;
4193 goto error;
4194 }
4195 }
4196 ++cmd;
4197 break;
4198
4199 default:
4200 if (VIM_ISDIGIT(*cmd)) /* absolute line number */
4201 lnum = getdigits(&cmd);
4202 }
4203
4204 for (;;)
4205 {
4206 cmd = skipwhite(cmd);
4207 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4208 break;
4209
4210 if (lnum == MAXLNUM)
4211 lnum = curwin->w_cursor.lnum; /* "+1" is same as ".+1" */
4212 if (VIM_ISDIGIT(*cmd))
4213 i = '+'; /* "number" is same as "+number" */
4214 else
4215 i = *cmd++;
4216 if (!VIM_ISDIGIT(*cmd)) /* '+' is '+1', but '+0' is not '+1' */
4217 n = 1;
4218 else
4219 n = getdigits(&cmd);
4220 if (i == '-')
4221 lnum -= n;
4222 else
4223 lnum += n;
4224 }
4225 } while (*cmd == '/' || *cmd == '?');
4226
4227error:
4228 *ptr = cmd;
4229 return lnum;
4230}
4231
4232/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004233 * Get flags from an Ex command argument.
4234 */
4235 static void
4236get_flags(eap)
4237 exarg_T *eap;
4238{
4239 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4240 {
4241 if (*eap->arg == 'l')
4242 eap->flags |= EXFLAG_LIST;
4243 else if (*eap->arg == 'p')
4244 eap->flags |= EXFLAG_PRINT;
4245 else
4246 eap->flags |= EXFLAG_NR;
4247 eap->arg = skipwhite(eap->arg + 1);
4248 }
4249}
4250
4251/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004252 * Function called for command which is Not Implemented. NI!
4253 */
4254 void
4255ex_ni(eap)
4256 exarg_T *eap;
4257{
4258 if (!eap->skip)
4259 eap->errmsg = (char_u *)N_("E319: Sorry, the command is not available in this version");
4260}
4261
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004262#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263/*
4264 * Function called for script command which is Not Implemented. NI!
4265 * Skips over ":perl <<EOF" constructs.
4266 */
4267 static void
4268ex_script_ni(eap)
4269 exarg_T *eap;
4270{
4271 if (!eap->skip)
4272 ex_ni(eap);
4273 else
4274 vim_free(script_get(eap, eap->arg));
4275}
4276#endif
4277
4278/*
4279 * Check range in Ex command for validity.
4280 * Return NULL when valid, error message when invalid.
4281 */
4282 static char_u *
4283invalid_range(eap)
4284 exarg_T *eap;
4285{
4286 if ( eap->line1 < 0
4287 || eap->line2 < 0
4288 || eap->line1 > eap->line2
4289 || ((eap->argt & RANGE)
4290 && !(eap->argt & NOTADR)
4291 && eap->line2 > curbuf->b_ml.ml_line_count
4292#ifdef FEAT_DIFF
4293 + (eap->cmdidx == CMD_diffget)
4294#endif
4295 ))
4296 return (char_u *)_(e_invrange);
4297 return NULL;
4298}
4299
4300/*
4301 * Correct the range for zero line number, if required.
4302 */
4303 static void
4304correct_range(eap)
4305 exarg_T *eap;
4306{
4307 if (!(eap->argt & ZEROR)) /* zero in range not allowed */
4308 {
4309 if (eap->line1 == 0)
4310 eap->line1 = 1;
4311 if (eap->line2 == 0)
4312 eap->line2 = 1;
4313 }
4314}
4315
Bram Moolenaar748bf032005-02-02 23:04:36 +00004316#ifdef FEAT_QUICKFIX
4317static char_u *skip_grep_pat __ARGS((exarg_T *eap));
4318
4319/*
4320 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4321 * pattern. Otherwise return eap->arg.
4322 */
4323 static char_u *
4324skip_grep_pat(eap)
4325 exarg_T *eap;
4326{
4327 char_u *p = eap->arg;
4328
Bram Moolenaara37420f2006-02-04 22:37:47 +00004329 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4330 || eap->cmdidx == CMD_vimgrepadd
4331 || eap->cmdidx == CMD_lvimgrepadd
4332 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004333 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004334 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004335 if (p == NULL)
4336 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004337 }
4338 return p;
4339}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004340
4341/*
4342 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4343 * in the command line, so that things like % get expanded.
4344 */
4345 static char_u *
4346replace_makeprg(eap, p, cmdlinep)
4347 exarg_T *eap;
4348 char_u *p;
4349 char_u **cmdlinep;
4350{
4351 char_u *new_cmdline;
4352 char_u *program;
4353 char_u *pos;
4354 char_u *ptr;
4355 int len;
4356 int i;
4357
4358 /*
4359 * Don't do it when ":vimgrep" is used for ":grep".
4360 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004361 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4362 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4363 || eap->cmdidx == CMD_grepadd
4364 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004365 && !grep_internal(eap->cmdidx))
4366 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004367 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4368 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004369 {
4370 if (*curbuf->b_p_gp == NUL)
4371 program = p_gp;
4372 else
4373 program = curbuf->b_p_gp;
4374 }
4375 else
4376 {
4377 if (*curbuf->b_p_mp == NUL)
4378 program = p_mp;
4379 else
4380 program = curbuf->b_p_mp;
4381 }
4382
4383 p = skipwhite(p);
4384
4385 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4386 {
4387 /* replace $* by given arguments */
4388 i = 1;
4389 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4390 ++i;
4391 len = (int)STRLEN(p);
4392 new_cmdline = alloc((int)(STRLEN(program) + i * (len - 2) + 1));
4393 if (new_cmdline == NULL)
4394 return NULL; /* out of memory */
4395 ptr = new_cmdline;
4396 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4397 {
4398 i = (int)(pos - program);
4399 STRNCPY(ptr, program, i);
4400 STRCPY(ptr += i, p);
4401 ptr += len;
4402 program = pos + 2;
4403 }
4404 STRCPY(ptr, program);
4405 }
4406 else
4407 {
4408 new_cmdline = alloc((int)(STRLEN(program) + STRLEN(p) + 2));
4409 if (new_cmdline == NULL)
4410 return NULL; /* out of memory */
4411 STRCPY(new_cmdline, program);
4412 STRCAT(new_cmdline, " ");
4413 STRCAT(new_cmdline, p);
4414 }
4415 msg_make(p);
4416
4417 /* 'eap->cmd' is not set here, because it is not used at CMD_make */
4418 vim_free(*cmdlinep);
4419 *cmdlinep = new_cmdline;
4420 p = new_cmdline;
4421 }
4422 return p;
4423}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004424#endif
4425
Bram Moolenaar071d4272004-06-13 20:20:40 +00004426/*
4427 * Expand file name in Ex command argument.
4428 * Return FAIL for failure, OK otherwise.
4429 */
4430 int
4431expand_filename(eap, cmdlinep, errormsgp)
4432 exarg_T *eap;
4433 char_u **cmdlinep;
4434 char_u **errormsgp;
4435{
4436 int has_wildcards; /* need to expand wildcards */
4437 char_u *repl;
4438 int srclen;
4439 char_u *p;
4440 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004441 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004442
Bram Moolenaar748bf032005-02-02 23:04:36 +00004443#ifdef FEAT_QUICKFIX
4444 /* Skip a regexp pattern for ":vimgrep[add] pat file..." */
4445 p = skip_grep_pat(eap);
4446#else
4447 p = eap->arg;
4448#endif
4449
Bram Moolenaar071d4272004-06-13 20:20:40 +00004450 /*
4451 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4452 * the file name contains a wildcard it should not cause expanding.
4453 * (it will be expanded anyway if there is a wildcard before replacing).
4454 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004455 has_wildcards = mch_has_wildcard(p);
4456 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004457 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004458#ifdef FEAT_EVAL
4459 /* Skip over `=expr`, wildcards in it are not expanded. */
4460 if (p[0] == '`' && p[1] == '=')
4461 {
4462 p += 2;
4463 (void)skip_expr(&p);
4464 if (*p == '`')
4465 ++p;
4466 continue;
4467 }
4468#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004469 /*
4470 * Quick check if this cannot be the start of a special string.
4471 * Also removes backslash before '%', '#' and '<'.
4472 */
4473 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4474 {
4475 ++p;
4476 continue;
4477 }
4478
4479 /*
4480 * Try to find a match at this position.
4481 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004482 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
4483 errormsgp, &escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004484 if (*errormsgp != NULL) /* error detected */
4485 return FAIL;
4486 if (repl == NULL) /* no match found */
4487 {
4488 p += srclen;
4489 continue;
4490 }
4491
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004492 /* Wildcards won't be expanded below, the replacement is taken
4493 * literally. But do expand "~/file", "~user/file" and "$HOME/file". */
4494 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4495 {
4496 char_u *l = repl;
4497
4498 repl = expand_env_save(repl);
4499 vim_free(l);
4500 }
4501
Bram Moolenaar63b92542007-03-27 14:57:09 +00004502 /* Need to escape white space et al. with a backslash.
4503 * Don't do this for:
4504 * - replacement that already has been escaped: "##"
4505 * - shell commands (may have to use quotes instead).
4506 * - non-unix systems when there is a single argument (spaces don't
4507 * separate arguments then).
4508 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004509 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00004510 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00004511 && eap->cmdidx != CMD_bang
4512 && eap->cmdidx != CMD_make
Bram Moolenaara37420f2006-02-04 22:37:47 +00004513 && eap->cmdidx != CMD_lmake
Bram Moolenaar071d4272004-06-13 20:20:40 +00004514 && eap->cmdidx != CMD_grep
Bram Moolenaara37420f2006-02-04 22:37:47 +00004515 && eap->cmdidx != CMD_lgrep
Bram Moolenaar071d4272004-06-13 20:20:40 +00004516 && eap->cmdidx != CMD_grepadd
Bram Moolenaara37420f2006-02-04 22:37:47 +00004517 && eap->cmdidx != CMD_lgrepadd
Bram Moolenaar071d4272004-06-13 20:20:40 +00004518#ifndef UNIX
4519 && !(eap->argt & NOSPC)
4520#endif
4521 )
4522 {
4523 char_u *l;
4524#ifdef BACKSLASH_IN_FILENAME
4525 /* Don't escape a backslash here, because rem_backslash() doesn't
4526 * remove it later. */
4527 static char_u *nobslash = (char_u *)" \t\"|";
4528# define ESCAPE_CHARS nobslash
4529#else
4530# define ESCAPE_CHARS escape_chars
4531#endif
4532
4533 for (l = repl; *l; ++l)
4534 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
4535 {
4536 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
4537 if (l != NULL)
4538 {
4539 vim_free(repl);
4540 repl = l;
4541 }
4542 break;
4543 }
4544 }
4545
4546 /* For a shell command a '!' must be escaped. */
4547 if ((eap->usefilter || eap->cmdidx == CMD_bang)
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004548 && vim_strpbrk(repl, (char_u *)"!&;()<>") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004549 {
4550 char_u *l;
4551
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004552 l = vim_strsave_escaped(repl, (char_u *)"!&;()<>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004553 if (l != NULL)
4554 {
4555 vim_free(repl);
4556 repl = l;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004557 /* For a sh-like shell escape "!" another time. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004558 if (strstr((char *)p_sh, "sh") != NULL)
4559 {
4560 l = vim_strsave_escaped(repl, (char_u *)"!");
4561 if (l != NULL)
4562 {
4563 vim_free(repl);
4564 repl = l;
4565 }
4566 }
4567 }
4568 }
4569
4570 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
4571 vim_free(repl);
4572 if (p == NULL)
4573 return FAIL;
4574 }
4575
4576 /*
4577 * One file argument: Expand wildcards.
4578 * Don't do this with ":r !command" or ":w !command".
4579 */
4580 if ((eap->argt & NOSPC) && !eap->usefilter)
4581 {
4582 /*
4583 * May do this twice:
4584 * 1. Replace environment variables.
4585 * 2. Replace any other wildcards, remove backslashes.
4586 */
4587 for (n = 1; n <= 2; ++n)
4588 {
4589 if (n == 2)
4590 {
4591#ifdef UNIX
4592 /*
4593 * Only for Unix we check for more than one file name.
4594 * For other systems spaces are considered to be part
4595 * of the file name.
4596 * Only check here if there is no wildcard, otherwise
4597 * ExpandOne() will check for errors. This allows
4598 * ":e `ls ve*.c`" on Unix.
4599 */
4600 if (!has_wildcards)
4601 for (p = eap->arg; *p; ++p)
4602 {
4603 /* skip escaped characters */
4604 if (p[1] && (*p == '\\' || *p == Ctrl_V))
4605 ++p;
4606 else if (vim_iswhite(*p))
4607 {
4608 *errormsgp = (char_u *)_("E172: Only one file name allowed");
4609 return FAIL;
4610 }
4611 }
4612#endif
4613
4614 /*
4615 * Halve the number of backslashes (this is Vi compatible).
4616 * For Unix and OS/2, when wildcards are expanded, this is
4617 * done by ExpandOne() below.
4618 */
4619#if defined(UNIX) || defined(OS2)
4620 if (!has_wildcards)
4621#endif
4622 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004623 }
4624
4625 if (has_wildcards)
4626 {
4627 if (n == 1)
4628 {
4629 /*
4630 * First loop: May expand environment variables. This
4631 * can be done much faster with expand_env() than with
4632 * something else (e.g., calling a shell).
4633 * After expanding environment variables, check again
4634 * if there are still wildcards present.
4635 */
4636 if (vim_strchr(eap->arg, '$') != NULL
4637 || vim_strchr(eap->arg, '~') != NULL)
4638 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00004639 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004640 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004641 has_wildcards = mch_has_wildcard(NameBuff);
4642 p = NameBuff;
4643 }
4644 else
4645 p = NULL;
4646 }
4647 else /* n == 2 */
4648 {
4649 expand_T xpc;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004650 int options = WILD_LIST_NOTFOUND|WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004651
4652 ExpandInit(&xpc);
4653 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004654 if (p_wic)
4655 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004656 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01004657 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004658 if (p == NULL)
4659 return FAIL;
4660 }
4661 if (p != NULL)
4662 {
4663 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
4664 p, cmdlinep);
4665 if (n == 2) /* p came from ExpandOne() */
4666 vim_free(p);
4667 }
4668 }
4669 }
4670 }
4671 return OK;
4672}
4673
4674/*
4675 * Replace part of the command line, keeping eap->cmd, eap->arg and
4676 * eap->nextcmd correct.
4677 * "src" points to the part that is to be replaced, of length "srclen".
4678 * "repl" is the replacement string.
4679 * Returns a pointer to the character after the replaced string.
4680 * Returns NULL for failure.
4681 */
4682 static char_u *
4683repl_cmdline(eap, src, srclen, repl, cmdlinep)
4684 exarg_T *eap;
4685 char_u *src;
4686 int srclen;
4687 char_u *repl;
4688 char_u **cmdlinep;
4689{
4690 int len;
4691 int i;
4692 char_u *new_cmdline;
4693
4694 /*
4695 * The new command line is build in new_cmdline[].
4696 * First allocate it.
4697 * Careful: a "+cmd" argument may have been NUL terminated.
4698 */
4699 len = (int)STRLEN(repl);
4700 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004701 if (eap->nextcmd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004702 i += (int)STRLEN(eap->nextcmd);/* add space for next command */
4703 if ((new_cmdline = alloc((unsigned)i)) == NULL)
4704 return NULL; /* out of memory! */
4705
4706 /*
4707 * Copy the stuff before the expanded part.
4708 * Copy the expanded stuff.
4709 * Copy what came after the expanded part.
4710 * Copy the next commands, if there are any.
4711 */
4712 i = (int)(src - *cmdlinep); /* length of part before match */
4713 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00004714
Bram Moolenaar071d4272004-06-13 20:20:40 +00004715 mch_memmove(new_cmdline + i, repl, (size_t)len);
4716 i += len; /* remember the end of the string */
4717 STRCPY(new_cmdline + i, src + srclen);
4718 src = new_cmdline + i; /* remember where to continue */
4719
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004720 if (eap->nextcmd != NULL) /* append next command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004721 {
4722 i = (int)STRLEN(new_cmdline) + 1;
4723 STRCPY(new_cmdline + i, eap->nextcmd);
4724 eap->nextcmd = new_cmdline + i;
4725 }
4726 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
4727 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
4728 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
4729 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
4730 vim_free(*cmdlinep);
4731 *cmdlinep = new_cmdline;
4732
4733 return src;
4734}
4735
4736/*
4737 * Check for '|' to separate commands and '"' to start comments.
4738 */
4739 void
4740separate_nextcmd(eap)
4741 exarg_T *eap;
4742{
4743 char_u *p;
4744
Bram Moolenaar86b68352004-12-27 21:59:20 +00004745#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004746 p = skip_grep_pat(eap);
4747#else
4748 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004749#endif
4750
4751 for ( ; *p; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004752 {
4753 if (*p == Ctrl_V)
4754 {
4755 if (eap->argt & (USECTRLV | XFILE))
4756 ++p; /* skip CTRL-V and next char */
4757 else
Bram Moolenaarb0db5692007-08-14 20:54:49 +00004758 /* remove CTRL-V and skip next char */
Bram Moolenaara7241f52008-06-24 20:39:31 +00004759 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004760 if (*p == NUL) /* stop at NUL after CTRL-V */
4761 break;
4762 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004763
4764#ifdef FEAT_EVAL
4765 /* Skip over `=expr` when wildcards are expanded. */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004766 else if (p[0] == '`' && p[1] == '=' && (eap->argt & XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004767 {
4768 p += 2;
4769 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004770 }
4771#endif
4772
Bram Moolenaar071d4272004-06-13 20:20:40 +00004773 /* Check for '"': start of comment or '|': next command */
4774 /* :@" and :*" do not start a comment!
4775 * :redir @" doesn't either. */
4776 else if ((*p == '"' && !(eap->argt & NOTRLCOM)
4777 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
4778 || p != eap->arg)
4779 && (eap->cmdidx != CMD_redir
4780 || p != eap->arg + 1 || p[-1] != '@'))
4781 || *p == '|' || *p == '\n')
4782 {
4783 /*
4784 * We remove the '\' before the '|', unless USECTRLV is used
4785 * AND 'b' is present in 'cpoptions'.
4786 */
4787 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
4788 || !(eap->argt & USECTRLV)) && *(p - 1) == '\\')
4789 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00004790 STRMOVE(p - 1, p); /* remove the '\' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004791 --p;
4792 }
4793 else
4794 {
4795 eap->nextcmd = check_nextcmd(p);
4796 *p = NUL;
4797 break;
4798 }
4799 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004800 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004801
Bram Moolenaar071d4272004-06-13 20:20:40 +00004802 if (!(eap->argt & NOTRLCOM)) /* remove trailing spaces */
4803 del_trailing_spaces(eap->arg);
4804}
4805
4806/*
4807 * get + command from ex argument
4808 */
4809 static char_u *
4810getargcmd(argp)
4811 char_u **argp;
4812{
4813 char_u *arg = *argp;
4814 char_u *command = NULL;
4815
4816 if (*arg == '+') /* +[command] */
4817 {
4818 ++arg;
4819 if (vim_isspace(*arg))
4820 command = dollar_command;
4821 else
4822 {
4823 command = arg;
4824 arg = skip_cmd_arg(command, TRUE);
4825 if (*arg != NUL)
4826 *arg++ = NUL; /* terminate command with NUL */
4827 }
4828
4829 arg = skipwhite(arg); /* skip over spaces */
4830 *argp = arg;
4831 }
4832 return command;
4833}
4834
4835/*
4836 * Find end of "+command" argument. Skip over "\ " and "\\".
4837 */
4838 static char_u *
4839skip_cmd_arg(p, rembs)
4840 char_u *p;
4841 int rembs; /* TRUE to halve the number of backslashes */
4842{
4843 while (*p && !vim_isspace(*p))
4844 {
4845 if (*p == '\\' && p[1] != NUL)
4846 {
4847 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00004848 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004849 else
4850 ++p;
4851 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004852 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853 }
4854 return p;
4855}
4856
4857/*
4858 * Get "++opt=arg" argument.
4859 * Return FAIL or OK.
4860 */
4861 static int
4862getargopt(eap)
4863 exarg_T *eap;
4864{
4865 char_u *arg = eap->arg + 2;
4866 int *pp = NULL;
4867#ifdef FEAT_MBYTE
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004868 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004869 char_u *p;
4870#endif
4871
4872 /* ":edit ++[no]bin[ary] file" */
4873 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
4874 {
4875 if (*arg == 'n')
4876 {
4877 arg += 2;
4878 eap->force_bin = FORCE_NOBIN;
4879 }
4880 else
4881 eap->force_bin = FORCE_BIN;
4882 if (!checkforcmd(&arg, "binary", 3))
4883 return FAIL;
4884 eap->arg = skipwhite(arg);
4885 return OK;
4886 }
4887
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004888 /* ":read ++edit file" */
4889 if (STRNCMP(arg, "edit", 4) == 0)
4890 {
4891 eap->read_edit = TRUE;
4892 eap->arg = skipwhite(arg + 4);
4893 return OK;
4894 }
4895
Bram Moolenaar071d4272004-06-13 20:20:40 +00004896 if (STRNCMP(arg, "ff", 2) == 0)
4897 {
4898 arg += 2;
4899 pp = &eap->force_ff;
4900 }
4901 else if (STRNCMP(arg, "fileformat", 10) == 0)
4902 {
4903 arg += 10;
4904 pp = &eap->force_ff;
4905 }
4906#ifdef FEAT_MBYTE
4907 else if (STRNCMP(arg, "enc", 3) == 0)
4908 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01004909 if (STRNCMP(arg, "encoding", 8) == 0)
4910 arg += 8;
4911 else
4912 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004913 pp = &eap->force_enc;
4914 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004915 else if (STRNCMP(arg, "bad", 3) == 0)
4916 {
4917 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004918 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004919 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004920#endif
4921
4922 if (pp == NULL || *arg != '=')
4923 return FAIL;
4924
4925 ++arg;
4926 *pp = (int)(arg - eap->cmd);
4927 arg = skip_cmd_arg(arg, FALSE);
4928 eap->arg = skipwhite(arg);
4929 *arg = NUL;
4930
4931#ifdef FEAT_MBYTE
4932 if (pp == &eap->force_ff)
4933 {
4934#endif
4935 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
4936 return FAIL;
4937#ifdef FEAT_MBYTE
4938 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004939 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004940 {
4941 /* Make 'fileencoding' lower case. */
4942 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
4943 *p = TOLOWER_ASC(*p);
4944 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004945 else
4946 {
4947 /* Check ++bad= argument. Must be a single-byte character, "keep" or
4948 * "drop". */
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004949 p = eap->cmd + bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004950 if (STRICMP(p, "keep") == 0)
4951 eap->bad_char = BAD_KEEP;
4952 else if (STRICMP(p, "drop") == 0)
4953 eap->bad_char = BAD_DROP;
4954 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
4955 eap->bad_char = *p;
4956 else
4957 return FAIL;
4958 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004959#endif
4960
4961 return OK;
4962}
4963
4964/*
4965 * ":abbreviate" and friends.
4966 */
4967 static void
4968ex_abbreviate(eap)
4969 exarg_T *eap;
4970{
4971 do_exmap(eap, TRUE); /* almost the same as mapping */
4972}
4973
4974/*
4975 * ":map" and friends.
4976 */
4977 static void
4978ex_map(eap)
4979 exarg_T *eap;
4980{
4981 /*
4982 * If we are sourcing .exrc or .vimrc in current directory we
4983 * print the mappings for security reasons.
4984 */
4985 if (secure)
4986 {
4987 secure = 2;
4988 msg_outtrans(eap->cmd);
4989 msg_putchar('\n');
4990 }
4991 do_exmap(eap, FALSE);
4992}
4993
4994/*
4995 * ":unmap" and friends.
4996 */
4997 static void
4998ex_unmap(eap)
4999 exarg_T *eap;
5000{
5001 do_exmap(eap, FALSE);
5002}
5003
5004/*
5005 * ":mapclear" and friends.
5006 */
5007 static void
5008ex_mapclear(eap)
5009 exarg_T *eap;
5010{
5011 map_clear(eap->cmd, eap->arg, eap->forceit, FALSE);
5012}
5013
5014/*
5015 * ":abclear" and friends.
5016 */
5017 static void
5018ex_abclear(eap)
5019 exarg_T *eap;
5020{
5021 map_clear(eap->cmd, eap->arg, TRUE, TRUE);
5022}
5023
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005024#if defined(FEAT_AUTOCMD) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005025 static void
5026ex_autocmd(eap)
5027 exarg_T *eap;
5028{
5029 /*
5030 * Disallow auto commands from .exrc and .vimrc in current
5031 * directory for security reasons.
5032 */
5033 if (secure)
5034 {
5035 secure = 2;
5036 eap->errmsg = e_curdir;
5037 }
5038 else if (eap->cmdidx == CMD_autocmd)
5039 do_autocmd(eap->arg, eap->forceit);
5040 else
5041 do_augroup(eap->arg, eap->forceit);
5042}
5043
5044/*
5045 * ":doautocmd": Apply the automatic commands to the current buffer.
5046 */
5047 static void
5048ex_doautocmd(eap)
5049 exarg_T *eap;
5050{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005051 char_u *arg = eap->arg;
5052 int call_do_modelines = check_nomodeline(&arg);
5053
5054 (void)do_doautocmd(arg, TRUE);
5055 if (call_do_modelines) /* Only when there is no <nomodeline>. */
5056 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057}
5058#endif
5059
5060#ifdef FEAT_LISTCMDS
5061/*
5062 * :[N]bunload[!] [N] [bufname] unload buffer
5063 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
5064 * :[N]bwipeout[!] [N] [bufname] delete buffer really
5065 */
5066 static void
5067ex_bunload(eap)
5068 exarg_T *eap;
5069{
5070 eap->errmsg = do_bufdel(
5071 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
5072 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
5073 : DOBUF_UNLOAD, eap->arg,
5074 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
5075}
5076
5077/*
5078 * :[N]buffer [N] to buffer N
5079 * :[N]sbuffer [N] to buffer N
5080 */
5081 static void
5082ex_buffer(eap)
5083 exarg_T *eap;
5084{
5085 if (*eap->arg)
5086 eap->errmsg = e_trailing;
5087 else
5088 {
5089 if (eap->addr_count == 0) /* default is current buffer */
5090 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
5091 else
5092 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
5093 }
5094}
5095
5096/*
5097 * :[N]bmodified [N] to next mod. buffer
5098 * :[N]sbmodified [N] to next mod. buffer
5099 */
5100 static void
5101ex_bmodified(eap)
5102 exarg_T *eap;
5103{
5104 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
5105}
5106
5107/*
5108 * :[N]bnext [N] to next buffer
5109 * :[N]sbnext [N] split and to next buffer
5110 */
5111 static void
5112ex_bnext(eap)
5113 exarg_T *eap;
5114{
5115 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
5116}
5117
5118/*
5119 * :[N]bNext [N] to previous buffer
5120 * :[N]bprevious [N] to previous buffer
5121 * :[N]sbNext [N] split and to previous buffer
5122 * :[N]sbprevious [N] split and to previous buffer
5123 */
5124 static void
5125ex_bprevious(eap)
5126 exarg_T *eap;
5127{
5128 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
5129}
5130
5131/*
5132 * :brewind to first buffer
5133 * :bfirst to first buffer
5134 * :sbrewind split and to first buffer
5135 * :sbfirst split and to first buffer
5136 */
5137 static void
5138ex_brewind(eap)
5139 exarg_T *eap;
5140{
5141 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
5142}
5143
5144/*
5145 * :blast to last buffer
5146 * :sblast split and to last buffer
5147 */
5148 static void
5149ex_blast(eap)
5150 exarg_T *eap;
5151{
5152 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
5153}
5154#endif
5155
5156 int
5157ends_excmd(c)
5158 int c;
5159{
5160 return (c == NUL || c == '|' || c == '"' || c == '\n');
5161}
5162
5163#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5164 || defined(PROTO)
5165/*
5166 * Return the next command, after the first '|' or '\n'.
5167 * Return NULL if not found.
5168 */
5169 char_u *
5170find_nextcmd(p)
5171 char_u *p;
5172{
5173 while (*p != '|' && *p != '\n')
5174 {
5175 if (*p == NUL)
5176 return NULL;
5177 ++p;
5178 }
5179 return (p + 1);
5180}
5181#endif
5182
5183/*
5184 * Check if *p is a separator between Ex commands.
5185 * Return NULL if it isn't, (p + 1) if it is.
5186 */
5187 char_u *
5188check_nextcmd(p)
5189 char_u *p;
5190{
5191 p = skipwhite(p);
5192 if (*p == '|' || *p == '\n')
5193 return (p + 1);
5194 else
5195 return NULL;
5196}
5197
5198/*
5199 * - if there are more files to edit
5200 * - and this is the last window
5201 * - and forceit not used
5202 * - and not repeated twice on a row
5203 * return FAIL and give error message if 'message' TRUE
5204 * return OK otherwise
5205 */
5206 static int
5207check_more(message, forceit)
5208 int message; /* when FALSE check only, no messages */
5209 int forceit;
5210{
5211 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5212
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005213 if (!forceit && only_one_window()
5214 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005215 {
5216 if (message)
5217 {
5218#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
5219 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
5220 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005221 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005222
5223 if (n == 1)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02005224 vim_strncpy(buff,
5225 (char_u *)_("1 more file to edit. Quit anyway?"),
Bram Moolenaard9462e32011-04-11 21:35:11 +02005226 DIALOG_MSG_SIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005227 else
Bram Moolenaard9462e32011-04-11 21:35:11 +02005228 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005229 _("%d more files to edit. Quit anyway?"), n);
5230 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5231 return OK;
5232 return FAIL;
5233 }
5234#endif
5235 if (n == 1)
5236 EMSG(_("E173: 1 more file to edit"));
5237 else
5238 EMSGN(_("E173: %ld more files to edit"), n);
5239 quitmore = 2; /* next try to quit is allowed */
5240 }
5241 return FAIL;
5242 }
5243 return OK;
5244}
5245
5246#ifdef FEAT_CMDL_COMPL
5247/*
5248 * Function given to ExpandGeneric() to obtain the list of command names.
5249 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005250 char_u *
5251get_command_name(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00005252 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005253 int idx;
5254{
5255 if (idx >= (int)CMD_SIZE)
5256# ifdef FEAT_USR_CMDS
5257 return get_user_command_name(idx);
5258# else
5259 return NULL;
5260# endif
5261 return cmdnames[idx].cmd_name;
5262}
5263#endif
5264
5265#if defined(FEAT_USR_CMDS) || defined(PROTO)
5266static 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));
5267static void uc_list __ARGS((char_u *name, size_t name_len));
5268static int uc_scan_attr __ARGS((char_u *attr, size_t len, long *argt, long *def, int *flags, int *compl, char_u **compl_arg));
5269static char_u *uc_split_args __ARGS((char_u *arg, size_t *lenp));
5270static 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));
5271
5272 static int
5273uc_add_command(name, name_len, rep, argt, def, flags, compl, compl_arg, force)
5274 char_u *name;
5275 size_t name_len;
5276 char_u *rep;
5277 long argt;
5278 long def;
5279 int flags;
5280 int compl;
5281 char_u *compl_arg;
5282 int force;
5283{
5284 ucmd_T *cmd = NULL;
5285 char_u *p;
5286 int i;
5287 int cmp = 1;
5288 char_u *rep_buf = NULL;
5289 garray_T *gap;
5290
Bram Moolenaar9c102382006-05-03 21:26:49 +00005291 replace_termcodes(rep, &rep_buf, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005292 if (rep_buf == NULL)
5293 {
5294 /* Can't replace termcodes - try using the string as is */
5295 rep_buf = vim_strsave(rep);
5296
5297 /* Give up if out of memory */
5298 if (rep_buf == NULL)
5299 return FAIL;
5300 }
5301
5302 /* get address of growarray: global or in curbuf */
5303 if (flags & UC_BUFFER)
5304 {
5305 gap = &curbuf->b_ucmds;
5306 if (gap->ga_itemsize == 0)
5307 ga_init2(gap, (int)sizeof(ucmd_T), 4);
5308 }
5309 else
5310 gap = &ucmds;
5311
5312 /* Search for the command in the already defined commands. */
5313 for (i = 0; i < gap->ga_len; ++i)
5314 {
5315 size_t len;
5316
5317 cmd = USER_CMD_GA(gap, i);
5318 len = STRLEN(cmd->uc_name);
5319 cmp = STRNCMP(name, cmd->uc_name, name_len);
5320 if (cmp == 0)
5321 {
5322 if (name_len < len)
5323 cmp = -1;
5324 else if (name_len > len)
5325 cmp = 1;
5326 }
5327
5328 if (cmp == 0)
5329 {
5330 if (!force)
5331 {
5332 EMSG(_("E174: Command already exists: add ! to replace it"));
5333 goto fail;
5334 }
5335
5336 vim_free(cmd->uc_rep);
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00005337 cmd->uc_rep = NULL;
5338#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5339 vim_free(cmd->uc_compl_arg);
5340 cmd->uc_compl_arg = NULL;
5341#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005342 break;
5343 }
5344
5345 /* Stop as soon as we pass the name to add */
5346 if (cmp < 0)
5347 break;
5348 }
5349
5350 /* Extend the array unless we're replacing an existing command */
5351 if (cmp != 0)
5352 {
5353 if (ga_grow(gap, 1) != OK)
5354 goto fail;
5355 if ((p = vim_strnsave(name, (int)name_len)) == NULL)
5356 goto fail;
5357
5358 cmd = USER_CMD_GA(gap, i);
5359 mch_memmove(cmd + 1, cmd, (gap->ga_len - i) * sizeof(ucmd_T));
5360
5361 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005362
5363 cmd->uc_name = p;
5364 }
5365
5366 cmd->uc_rep = rep_buf;
5367 cmd->uc_argt = argt;
5368 cmd->uc_def = def;
5369 cmd->uc_compl = compl;
5370#ifdef FEAT_EVAL
5371 cmd->uc_scriptID = current_SID;
5372# ifdef FEAT_CMDL_COMPL
5373 cmd->uc_compl_arg = compl_arg;
5374# endif
5375#endif
5376
5377 return OK;
5378
5379fail:
5380 vim_free(rep_buf);
5381#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5382 vim_free(compl_arg);
5383#endif
5384 return FAIL;
5385}
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005386#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005387
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005388#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005389/*
5390 * List of names for completion for ":command" with the EXPAND_ flag.
5391 * Must be alphabetical for completion.
5392 */
5393static struct
5394{
5395 int expand;
5396 char *name;
5397} command_complete[] =
5398{
5399 {EXPAND_AUGROUP, "augroup"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005400 {EXPAND_BEHAVE, "behave"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005401 {EXPAND_BUFFERS, "buffer"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005402 {EXPAND_COLORS, "color"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005403 {EXPAND_COMMANDS, "command"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005404 {EXPAND_COMPILER, "compiler"},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005405#if defined(FEAT_CSCOPE)
5406 {EXPAND_CSCOPE, "cscope"},
5407#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005408#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5409 {EXPAND_USER_DEFINED, "custom"},
Bram Moolenaara466c992005-07-09 21:03:22 +00005410 {EXPAND_USER_LIST, "customlist"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005411#endif
5412 {EXPAND_DIRECTORIES, "dir"},
5413 {EXPAND_ENV_VARS, "environment"},
5414 {EXPAND_EVENTS, "event"},
5415 {EXPAND_EXPRESSION, "expression"},
5416 {EXPAND_FILES, "file"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005417 {EXPAND_FILES_IN_PATH, "file_in_path"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005418 {EXPAND_FILETYPE, "filetype"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005419 {EXPAND_FUNCTIONS, "function"},
5420 {EXPAND_HELP, "help"},
5421 {EXPAND_HIGHLIGHT, "highlight"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005422#if defined(FEAT_CMDHIST)
5423 {EXPAND_HISTORY, "history"},
5424#endif
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005425#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005426 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005427 {EXPAND_LOCALES, "locale"},
5428#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005429 {EXPAND_MAPPINGS, "mapping"},
5430 {EXPAND_MENUS, "menu"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005431 {EXPAND_OWNSYNTAX, "syntax"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005432 {EXPAND_SETTINGS, "option"},
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005433 {EXPAND_SHELLCMD, "shellcmd"},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005434#if defined(FEAT_SIGNS)
5435 {EXPAND_SIGN, "sign"},
5436#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005437 {EXPAND_TAGS, "tag"},
5438 {EXPAND_TAGS_LISTFILES, "tag_listfiles"},
Bram Moolenaar24305862012-08-15 14:05:05 +02005439 {EXPAND_USER, "user"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005440 {EXPAND_USER_VARS, "var"},
5441 {0, NULL}
5442};
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005443#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005444
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005445#if defined(FEAT_USR_CMDS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005446 static void
5447uc_list(name, name_len)
5448 char_u *name;
5449 size_t name_len;
5450{
5451 int i, j;
5452 int found = FALSE;
5453 ucmd_T *cmd;
5454 int len;
5455 long a;
5456 garray_T *gap;
5457
5458 gap = &curbuf->b_ucmds;
5459 for (;;)
5460 {
5461 for (i = 0; i < gap->ga_len; ++i)
5462 {
5463 cmd = USER_CMD_GA(gap, i);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005464 a = (long)cmd->uc_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005465
5466 /* Skip commands which don't match the requested prefix */
5467 if (STRNCMP(name, cmd->uc_name, name_len) != 0)
5468 continue;
5469
5470 /* Put out the title first time */
5471 if (!found)
5472 MSG_PUTS_TITLE(_("\n Name Args Range Complete Definition"));
5473 found = TRUE;
5474 msg_putchar('\n');
5475 if (got_int)
5476 break;
5477
5478 /* Special cases */
5479 msg_putchar(a & BANG ? '!' : ' ');
5480 msg_putchar(a & REGSTR ? '"' : ' ');
5481 msg_putchar(gap != &ucmds ? 'b' : ' ');
5482 msg_putchar(' ');
5483
5484 msg_outtrans_attr(cmd->uc_name, hl_attr(HLF_D));
5485 len = (int)STRLEN(cmd->uc_name) + 4;
5486
5487 do {
5488 msg_putchar(' ');
5489 ++len;
5490 } while (len < 16);
5491
5492 len = 0;
5493
5494 /* Arguments */
5495 switch ((int)(a & (EXTRA|NOSPC|NEEDARG)))
5496 {
5497 case 0: IObuff[len++] = '0'; break;
5498 case (EXTRA): IObuff[len++] = '*'; break;
5499 case (EXTRA|NOSPC): IObuff[len++] = '?'; break;
5500 case (EXTRA|NEEDARG): IObuff[len++] = '+'; break;
5501 case (EXTRA|NOSPC|NEEDARG): IObuff[len++] = '1'; break;
5502 }
5503
5504 do {
5505 IObuff[len++] = ' ';
5506 } while (len < 5);
5507
5508 /* Range */
5509 if (a & (RANGE|COUNT))
5510 {
5511 if (a & COUNT)
5512 {
5513 /* -count=N */
5514 sprintf((char *)IObuff + len, "%ldc", cmd->uc_def);
5515 len += (int)STRLEN(IObuff + len);
5516 }
5517 else if (a & DFLALL)
5518 IObuff[len++] = '%';
5519 else if (cmd->uc_def >= 0)
5520 {
5521 /* -range=N */
5522 sprintf((char *)IObuff + len, "%ld", cmd->uc_def);
5523 len += (int)STRLEN(IObuff + len);
5524 }
5525 else
5526 IObuff[len++] = '.';
5527 }
5528
5529 do {
5530 IObuff[len++] = ' ';
5531 } while (len < 11);
5532
5533 /* Completion */
5534 for (j = 0; command_complete[j].expand != 0; ++j)
5535 if (command_complete[j].expand == cmd->uc_compl)
5536 {
5537 STRCPY(IObuff + len, command_complete[j].name);
5538 len += (int)STRLEN(IObuff + len);
5539 break;
5540 }
5541
5542 do {
5543 IObuff[len++] = ' ';
5544 } while (len < 21);
5545
5546 IObuff[len] = '\0';
5547 msg_outtrans(IObuff);
5548
5549 msg_outtrans_special(cmd->uc_rep, FALSE);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005550#ifdef FEAT_EVAL
5551 if (p_verbose > 0)
5552 last_set_msg(cmd->uc_scriptID);
5553#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005554 out_flush();
5555 ui_breakcheck();
5556 if (got_int)
5557 break;
5558 }
5559 if (gap == &ucmds || i < gap->ga_len)
5560 break;
5561 gap = &ucmds;
5562 }
5563
5564 if (!found)
5565 MSG(_("No user-defined commands found"));
5566}
5567
5568 static char_u *
5569uc_fun_cmd()
5570{
5571 static char_u fcmd[] = {0x84, 0xaf, 0x60, 0xb9, 0xaf, 0xb5, 0x60, 0xa4,
5572 0xa5, 0xad, 0xa1, 0xae, 0xa4, 0x60, 0xa1, 0x60,
5573 0xb3, 0xa8, 0xb2, 0xb5, 0xa2, 0xa2, 0xa5, 0xb2,
5574 0xb9, 0x7f, 0};
5575 int i;
5576
5577 for (i = 0; fcmd[i]; ++i)
5578 IObuff[i] = fcmd[i] - 0x40;
5579 IObuff[i] = 0;
5580 return IObuff;
5581}
5582
5583 static int
5584uc_scan_attr(attr, len, argt, def, flags, compl, compl_arg)
5585 char_u *attr;
5586 size_t len;
5587 long *argt;
5588 long *def;
5589 int *flags;
5590 int *compl;
5591 char_u **compl_arg;
5592{
5593 char_u *p;
5594
5595 if (len == 0)
5596 {
5597 EMSG(_("E175: No attribute specified"));
5598 return FAIL;
5599 }
5600
5601 /* First, try the simple attributes (no arguments) */
5602 if (STRNICMP(attr, "bang", len) == 0)
5603 *argt |= BANG;
5604 else if (STRNICMP(attr, "buffer", len) == 0)
5605 *flags |= UC_BUFFER;
5606 else if (STRNICMP(attr, "register", len) == 0)
5607 *argt |= REGSTR;
5608 else if (STRNICMP(attr, "bar", len) == 0)
5609 *argt |= TRLBAR;
5610 else
5611 {
5612 int i;
5613 char_u *val = NULL;
5614 size_t vallen = 0;
5615 size_t attrlen = len;
5616
5617 /* Look for the attribute name - which is the part before any '=' */
5618 for (i = 0; i < (int)len; ++i)
5619 {
5620 if (attr[i] == '=')
5621 {
5622 val = &attr[i + 1];
5623 vallen = len - i - 1;
5624 attrlen = i;
5625 break;
5626 }
5627 }
5628
5629 if (STRNICMP(attr, "nargs", attrlen) == 0)
5630 {
5631 if (vallen == 1)
5632 {
5633 if (*val == '0')
5634 /* Do nothing - this is the default */;
5635 else if (*val == '1')
5636 *argt |= (EXTRA | NOSPC | NEEDARG);
5637 else if (*val == '*')
5638 *argt |= EXTRA;
5639 else if (*val == '?')
5640 *argt |= (EXTRA | NOSPC);
5641 else if (*val == '+')
5642 *argt |= (EXTRA | NEEDARG);
5643 else
5644 goto wrong_nargs;
5645 }
5646 else
5647 {
5648wrong_nargs:
5649 EMSG(_("E176: Invalid number of arguments"));
5650 return FAIL;
5651 }
5652 }
5653 else if (STRNICMP(attr, "range", attrlen) == 0)
5654 {
5655 *argt |= RANGE;
5656 if (vallen == 1 && *val == '%')
5657 *argt |= DFLALL;
5658 else if (val != NULL)
5659 {
5660 p = val;
5661 if (*def >= 0)
5662 {
5663two_count:
5664 EMSG(_("E177: Count cannot be specified twice"));
5665 return FAIL;
5666 }
5667
5668 *def = getdigits(&p);
5669 *argt |= (ZEROR | NOTADR);
5670
5671 if (p != val + vallen || vallen == 0)
5672 {
5673invalid_count:
5674 EMSG(_("E178: Invalid default value for count"));
5675 return FAIL;
5676 }
5677 }
5678 }
5679 else if (STRNICMP(attr, "count", attrlen) == 0)
5680 {
Bram Moolenaar32e7b2d2005-02-27 22:36:47 +00005681 *argt |= (COUNT | ZEROR | RANGE | NOTADR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005682
5683 if (val != NULL)
5684 {
5685 p = val;
5686 if (*def >= 0)
5687 goto two_count;
5688
5689 *def = getdigits(&p);
5690
5691 if (p != val + vallen)
5692 goto invalid_count;
5693 }
5694
5695 if (*def < 0)
5696 *def = 0;
5697 }
5698 else if (STRNICMP(attr, "complete", attrlen) == 0)
5699 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005700 if (val == NULL)
5701 {
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005702 EMSG(_("E179: argument required for -complete"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005703 return FAIL;
5704 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005705
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005706 if (parse_compl_arg(val, (int)vallen, compl, argt, compl_arg)
5707 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005708 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005709 }
5710 else
5711 {
5712 char_u ch = attr[len];
5713 attr[len] = '\0';
5714 EMSG2(_("E181: Invalid attribute: %s"), attr);
5715 attr[len] = ch;
5716 return FAIL;
5717 }
5718 }
5719
5720 return OK;
5721}
5722
Bram Moolenaara850a712009-01-28 14:42:59 +00005723/*
5724 * ":command ..."
5725 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005726 static void
5727ex_command(eap)
5728 exarg_T *eap;
5729{
5730 char_u *name;
5731 char_u *end;
5732 char_u *p;
5733 long argt = 0;
5734 long def = -1;
5735 int flags = 0;
5736 int compl = EXPAND_NOTHING;
5737 char_u *compl_arg = NULL;
5738 int has_attr = (eap->arg[0] == '-');
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01005739 int name_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005740
5741 p = eap->arg;
5742
5743 /* Check for attributes */
5744 while (*p == '-')
5745 {
5746 ++p;
5747 end = skiptowhite(p);
5748 if (uc_scan_attr(p, end - p, &argt, &def, &flags, &compl, &compl_arg)
5749 == FAIL)
5750 return;
5751 p = skipwhite(end);
5752 }
5753
5754 /* Get the name (if any) and skip to the following argument */
5755 name = p;
5756 if (ASCII_ISALPHA(*p))
5757 while (ASCII_ISALNUM(*p))
5758 ++p;
5759 if (!ends_excmd(*p) && !vim_iswhite(*p))
5760 {
5761 EMSG(_("E182: Invalid command name"));
5762 return;
5763 }
5764 end = p;
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01005765 name_len = (int)(end - name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005766
5767 /* If there is nothing after the name, and no attributes were specified,
5768 * we are listing commands
5769 */
5770 p = skipwhite(end);
5771 if (!has_attr && ends_excmd(*p))
5772 {
5773 uc_list(name, end - name);
5774 }
5775 else if (!ASCII_ISUPPER(*name))
5776 {
5777 EMSG(_("E183: User defined commands must start with an uppercase letter"));
5778 return;
5779 }
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01005780 else if ((name_len == 1 && *name == 'X')
5781 || (name_len <= 4
5782 && STRNCMP(name, "Next", name_len > 4 ? 4 : name_len) == 0))
5783 {
5784 EMSG(_("E841: Reserved name, cannot be used for user defined command"));
5785 return;
5786 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005787 else
5788 uc_add_command(name, end - name, p, argt, def, flags, compl, compl_arg,
5789 eap->forceit);
5790}
5791
5792/*
5793 * ":comclear"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005794 * Clear all user commands, global and for current buffer.
5795 */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00005796 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005797ex_comclear(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00005798 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005799{
5800 uc_clear(&ucmds);
5801 uc_clear(&curbuf->b_ucmds);
5802}
5803
5804/*
5805 * Clear all user commands for "gap".
5806 */
5807 void
5808uc_clear(gap)
5809 garray_T *gap;
5810{
5811 int i;
5812 ucmd_T *cmd;
5813
5814 for (i = 0; i < gap->ga_len; ++i)
5815 {
5816 cmd = USER_CMD_GA(gap, i);
5817 vim_free(cmd->uc_name);
5818 vim_free(cmd->uc_rep);
5819# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5820 vim_free(cmd->uc_compl_arg);
5821# endif
5822 }
5823 ga_clear(gap);
5824}
5825
5826 static void
5827ex_delcommand(eap)
5828 exarg_T *eap;
5829{
5830 int i = 0;
5831 ucmd_T *cmd = NULL;
5832 int cmp = -1;
5833 garray_T *gap;
5834
5835 gap = &curbuf->b_ucmds;
5836 for (;;)
5837 {
5838 for (i = 0; i < gap->ga_len; ++i)
5839 {
5840 cmd = USER_CMD_GA(gap, i);
5841 cmp = STRCMP(eap->arg, cmd->uc_name);
5842 if (cmp <= 0)
5843 break;
5844 }
5845 if (gap == &ucmds || cmp == 0)
5846 break;
5847 gap = &ucmds;
5848 }
5849
5850 if (cmp != 0)
5851 {
5852 EMSG2(_("E184: No such user-defined command: %s"), eap->arg);
5853 return;
5854 }
5855
5856 vim_free(cmd->uc_name);
5857 vim_free(cmd->uc_rep);
5858# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5859 vim_free(cmd->uc_compl_arg);
5860# endif
5861
5862 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005863
5864 if (i < gap->ga_len)
5865 mch_memmove(cmd, cmd + 1, (gap->ga_len - i) * sizeof(ucmd_T));
5866}
5867
Bram Moolenaar552f8a12007-03-08 17:12:08 +00005868/*
5869 * split and quote args for <f-args>
5870 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005871 static char_u *
5872uc_split_args(arg, lenp)
5873 char_u *arg;
5874 size_t *lenp;
5875{
5876 char_u *buf;
5877 char_u *p;
5878 char_u *q;
5879 int len;
5880
5881 /* Precalculate length */
5882 p = arg;
5883 len = 2; /* Initial and final quotes */
5884
5885 while (*p)
5886 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00005887 if (p[0] == '\\' && p[1] == '\\')
5888 {
5889 len += 2;
5890 p += 2;
5891 }
5892 else if (p[0] == '\\' && vim_iswhite(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005893 {
5894 len += 1;
5895 p += 2;
5896 }
5897 else if (*p == '\\' || *p == '"')
5898 {
5899 len += 2;
5900 p += 1;
5901 }
5902 else if (vim_iswhite(*p))
5903 {
5904 p = skipwhite(p);
5905 if (*p == NUL)
5906 break;
5907 len += 3; /* "," */
5908 }
5909 else
5910 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02005911#ifdef FEAT_MBYTE
5912 int charlen = (*mb_ptr2len)(p);
5913 len += charlen;
5914 p += charlen;
5915#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005916 ++len;
5917 ++p;
Bram Moolenaardfef1542012-07-10 19:25:10 +02005918#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005919 }
5920 }
5921
5922 buf = alloc(len + 1);
5923 if (buf == NULL)
5924 {
5925 *lenp = 0;
5926 return buf;
5927 }
5928
5929 p = arg;
5930 q = buf;
5931 *q++ = '"';
5932 while (*p)
5933 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00005934 if (p[0] == '\\' && p[1] == '\\')
5935 {
5936 *q++ = '\\';
5937 *q++ = '\\';
5938 p += 2;
5939 }
5940 else if (p[0] == '\\' && vim_iswhite(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005941 {
5942 *q++ = p[1];
5943 p += 2;
5944 }
5945 else if (*p == '\\' || *p == '"')
5946 {
5947 *q++ = '\\';
5948 *q++ = *p++;
5949 }
5950 else if (vim_iswhite(*p))
5951 {
5952 p = skipwhite(p);
5953 if (*p == NUL)
5954 break;
5955 *q++ = '"';
5956 *q++ = ',';
5957 *q++ = '"';
5958 }
5959 else
5960 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02005961 MB_COPY_CHAR(p, q);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005962 }
5963 }
5964 *q++ = '"';
5965 *q = 0;
5966
5967 *lenp = len;
5968 return buf;
5969}
5970
5971/*
5972 * Check for a <> code in a user command.
5973 * "code" points to the '<'. "len" the length of the <> (inclusive).
5974 * "buf" is where the result is to be added.
5975 * "split_buf" points to a buffer used for splitting, caller should free it.
5976 * "split_len" is the length of what "split_buf" contains.
5977 * Returns the length of the replacement, which has been added to "buf".
5978 * Returns -1 if there was no match, and only the "<" has been copied.
5979 */
5980 static size_t
5981uc_check_code(code, len, buf, cmd, eap, split_buf, split_len)
5982 char_u *code;
5983 size_t len;
5984 char_u *buf;
5985 ucmd_T *cmd; /* the user command we're expanding */
5986 exarg_T *eap; /* ex arguments */
5987 char_u **split_buf;
5988 size_t *split_len;
5989{
5990 size_t result = 0;
5991 char_u *p = code + 1;
5992 size_t l = len - 2;
5993 int quote = 0;
5994 enum { ct_ARGS, ct_BANG, ct_COUNT, ct_LINE1, ct_LINE2, ct_REGISTER,
5995 ct_LT, ct_NONE } type = ct_NONE;
5996
5997 if ((vim_strchr((char_u *)"qQfF", *p) != NULL) && p[1] == '-')
5998 {
5999 quote = (*p == 'q' || *p == 'Q') ? 1 : 2;
6000 p += 2;
6001 l -= 2;
6002 }
6003
Bram Moolenaar371d5402006-03-20 21:47:49 +00006004 ++l;
6005 if (l <= 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006006 type = ct_NONE;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006007 else if (STRNICMP(p, "args>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006008 type = ct_ARGS;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006009 else if (STRNICMP(p, "bang>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006010 type = ct_BANG;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006011 else if (STRNICMP(p, "count>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006012 type = ct_COUNT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006013 else if (STRNICMP(p, "line1>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006014 type = ct_LINE1;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006015 else if (STRNICMP(p, "line2>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006016 type = ct_LINE2;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006017 else if (STRNICMP(p, "lt>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006018 type = ct_LT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006019 else if (STRNICMP(p, "reg>", l) == 0 || STRNICMP(p, "register>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006020 type = ct_REGISTER;
6021
6022 switch (type)
6023 {
6024 case ct_ARGS:
6025 /* Simple case first */
6026 if (*eap->arg == NUL)
6027 {
6028 if (quote == 1)
6029 {
6030 result = 2;
6031 if (buf != NULL)
6032 STRCPY(buf, "''");
6033 }
6034 else
6035 result = 0;
6036 break;
6037 }
6038
6039 /* When specified there is a single argument don't split it.
6040 * Works for ":Cmd %" when % is "a b c". */
6041 if ((eap->argt & NOSPC) && quote == 2)
6042 quote = 1;
6043
6044 switch (quote)
6045 {
6046 case 0: /* No quoting, no splitting */
6047 result = STRLEN(eap->arg);
6048 if (buf != NULL)
6049 STRCPY(buf, eap->arg);
6050 break;
6051 case 1: /* Quote, but don't split */
6052 result = STRLEN(eap->arg) + 2;
6053 for (p = eap->arg; *p; ++p)
6054 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006055#ifdef FEAT_MBYTE
6056 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6057 /* DBCS can contain \ in a trail byte, skip the
6058 * double-byte character. */
6059 ++p;
6060 else
6061#endif
6062 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006063 ++result;
6064 }
6065
6066 if (buf != NULL)
6067 {
6068 *buf++ = '"';
6069 for (p = eap->arg; *p; ++p)
6070 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006071#ifdef FEAT_MBYTE
6072 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6073 /* DBCS can contain \ in a trail byte, copy the
6074 * double-byte character to avoid escaping. */
6075 *buf++ = *p++;
6076 else
6077#endif
6078 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006079 *buf++ = '\\';
6080 *buf++ = *p;
6081 }
6082 *buf = '"';
6083 }
6084
6085 break;
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006086 case 2: /* Quote and split (<f-args>) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006087 /* This is hard, so only do it once, and cache the result */
6088 if (*split_buf == NULL)
6089 *split_buf = uc_split_args(eap->arg, split_len);
6090
6091 result = *split_len;
6092 if (buf != NULL && result != 0)
6093 STRCPY(buf, *split_buf);
6094
6095 break;
6096 }
6097 break;
6098
6099 case ct_BANG:
6100 result = eap->forceit ? 1 : 0;
6101 if (quote)
6102 result += 2;
6103 if (buf != NULL)
6104 {
6105 if (quote)
6106 *buf++ = '"';
6107 if (eap->forceit)
6108 *buf++ = '!';
6109 if (quote)
6110 *buf = '"';
6111 }
6112 break;
6113
6114 case ct_LINE1:
6115 case ct_LINE2:
6116 case ct_COUNT:
6117 {
6118 char num_buf[20];
6119 long num = (type == ct_LINE1) ? eap->line1 :
6120 (type == ct_LINE2) ? eap->line2 :
6121 (eap->addr_count > 0) ? eap->line2 : cmd->uc_def;
6122 size_t num_len;
6123
6124 sprintf(num_buf, "%ld", num);
6125 num_len = STRLEN(num_buf);
6126 result = num_len;
6127
6128 if (quote)
6129 result += 2;
6130
6131 if (buf != NULL)
6132 {
6133 if (quote)
6134 *buf++ = '"';
6135 STRCPY(buf, num_buf);
6136 buf += num_len;
6137 if (quote)
6138 *buf = '"';
6139 }
6140
6141 break;
6142 }
6143
6144 case ct_REGISTER:
6145 result = eap->regname ? 1 : 0;
6146 if (quote)
6147 result += 2;
6148 if (buf != NULL)
6149 {
6150 if (quote)
6151 *buf++ = '\'';
6152 if (eap->regname)
6153 *buf++ = eap->regname;
6154 if (quote)
6155 *buf = '\'';
6156 }
6157 break;
6158
6159 case ct_LT:
6160 result = 1;
6161 if (buf != NULL)
6162 *buf = '<';
6163 break;
6164
6165 default:
6166 /* Not recognized: just copy the '<' and return -1. */
6167 result = (size_t)-1;
6168 if (buf != NULL)
6169 *buf = '<';
6170 break;
6171 }
6172
6173 return result;
6174}
6175
6176 static void
6177do_ucmd(eap)
6178 exarg_T *eap;
6179{
6180 char_u *buf;
6181 char_u *p;
6182 char_u *q;
6183
6184 char_u *start;
Bram Moolenaar25648a52009-02-21 19:37:46 +00006185 char_u *end = NULL;
Bram Moolenaara850a712009-01-28 14:42:59 +00006186 char_u *ksp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006187 size_t len, totlen;
6188
6189 size_t split_len = 0;
6190 char_u *split_buf = NULL;
6191 ucmd_T *cmd;
6192#ifdef FEAT_EVAL
6193 scid_T save_current_SID = current_SID;
6194#endif
6195
6196 if (eap->cmdidx == CMD_USER)
6197 cmd = USER_CMD(eap->useridx);
6198 else
6199 cmd = USER_CMD_GA(&curbuf->b_ucmds, eap->useridx);
6200
6201 /*
6202 * Replace <> in the command by the arguments.
Bram Moolenaara850a712009-01-28 14:42:59 +00006203 * First round: "buf" is NULL, compute length, allocate "buf".
6204 * Second round: copy result into "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006205 */
6206 buf = NULL;
6207 for (;;)
6208 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006209 p = cmd->uc_rep; /* source */
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00006210 q = buf; /* destination */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006211 totlen = 0;
Bram Moolenaara850a712009-01-28 14:42:59 +00006212
6213 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006214 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006215 start = vim_strchr(p, '<');
6216 if (start != NULL)
6217 end = vim_strchr(start + 1, '>');
6218 if (buf != NULL)
6219 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006220 for (ksp = p; *ksp != NUL && *ksp != K_SPECIAL; ++ksp)
6221 ;
6222 if (*ksp == K_SPECIAL
6223 && (start == NULL || ksp < start || end == NULL)
Bram Moolenaara850a712009-01-28 14:42:59 +00006224 && ((ksp[1] == KS_SPECIAL && ksp[2] == KE_FILLER)
6225# ifdef FEAT_GUI
6226 || (ksp[1] == KS_EXTRA && ksp[2] == (int)KE_CSI)
6227# endif
6228 ))
6229 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006230 /* K_SPECIAL has been put in the buffer as K_SPECIAL
Bram Moolenaara850a712009-01-28 14:42:59 +00006231 * KS_SPECIAL KE_FILLER, like for mappings, but
6232 * do_cmdline() doesn't handle that, so convert it back.
6233 * Also change K_SPECIAL KS_EXTRA KE_CSI into CSI. */
6234 len = ksp - p;
6235 if (len > 0)
6236 {
6237 mch_memmove(q, p, len);
6238 q += len;
6239 }
6240 *q++ = ksp[1] == KS_SPECIAL ? K_SPECIAL : CSI;
6241 p = ksp + 3;
6242 continue;
6243 }
6244 }
6245
6246 /* break if there no <item> is found */
6247 if (start == NULL || end == NULL)
6248 break;
6249
Bram Moolenaar071d4272004-06-13 20:20:40 +00006250 /* Include the '>' */
6251 ++end;
6252
6253 /* Take everything up to the '<' */
6254 len = start - p;
6255 if (buf == NULL)
6256 totlen += len;
6257 else
6258 {
6259 mch_memmove(q, p, len);
6260 q += len;
6261 }
6262
6263 len = uc_check_code(start, end - start, q, cmd, eap,
6264 &split_buf, &split_len);
6265 if (len == (size_t)-1)
6266 {
6267 /* no match, continue after '<' */
6268 p = start + 1;
6269 len = 1;
6270 }
6271 else
6272 p = end;
6273 if (buf == NULL)
6274 totlen += len;
6275 else
6276 q += len;
6277 }
6278 if (buf != NULL) /* second time here, finished */
6279 {
6280 STRCPY(q, p);
6281 break;
6282 }
6283
6284 totlen += STRLEN(p); /* Add on the trailing characters */
6285 buf = alloc((unsigned)(totlen + 1));
6286 if (buf == NULL)
6287 {
6288 vim_free(split_buf);
6289 return;
6290 }
6291 }
6292
6293#ifdef FEAT_EVAL
6294 current_SID = cmd->uc_scriptID;
6295#endif
6296 (void)do_cmdline(buf, eap->getline, eap->cookie,
6297 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
6298#ifdef FEAT_EVAL
6299 current_SID = save_current_SID;
6300#endif
6301 vim_free(buf);
6302 vim_free(split_buf);
6303}
6304
6305# if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6306 static char_u *
6307get_user_command_name(idx)
6308 int idx;
6309{
6310 return get_user_commands(NULL, idx - (int)CMD_SIZE);
6311}
6312
6313/*
6314 * Function given to ExpandGeneric() to obtain the list of user command names.
6315 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006316 char_u *
6317get_user_commands(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006318 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006319 int idx;
6320{
6321 if (idx < curbuf->b_ucmds.ga_len)
6322 return USER_CMD_GA(&curbuf->b_ucmds, idx)->uc_name;
6323 idx -= curbuf->b_ucmds.ga_len;
6324 if (idx < ucmds.ga_len)
6325 return USER_CMD(idx)->uc_name;
6326 return NULL;
6327}
6328
6329/*
6330 * Function given to ExpandGeneric() to obtain the list of user command
6331 * attributes.
6332 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006333 char_u *
6334get_user_cmd_flags(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006335 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006336 int idx;
6337{
6338 static char *user_cmd_flags[] =
6339 {"bang", "bar", "buffer", "complete", "count",
6340 "nargs", "range", "register"};
6341
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00006342 if (idx >= (int)(sizeof(user_cmd_flags) / sizeof(user_cmd_flags[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006343 return NULL;
6344 return (char_u *)user_cmd_flags[idx];
6345}
6346
6347/*
6348 * Function given to ExpandGeneric() to obtain the list of values for -nargs.
6349 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006350 char_u *
6351get_user_cmd_nargs(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006352 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006353 int idx;
6354{
6355 static char *user_cmd_nargs[] = {"0", "1", "*", "?", "+"};
6356
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00006357 if (idx >= (int)(sizeof(user_cmd_nargs) / sizeof(user_cmd_nargs[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006358 return NULL;
6359 return (char_u *)user_cmd_nargs[idx];
6360}
6361
6362/*
6363 * Function given to ExpandGeneric() to obtain the list of values for -complete.
6364 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006365 char_u *
6366get_user_cmd_complete(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006367 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006368 int idx;
6369{
6370 return (char_u *)command_complete[idx].name;
6371}
6372# endif /* FEAT_CMDL_COMPL */
6373
6374#endif /* FEAT_USR_CMDS */
6375
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006376#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
6377/*
6378 * Parse a completion argument "value[vallen]".
6379 * The detected completion goes in "*complp", argument type in "*argt".
6380 * When there is an argument, for function and user defined completion, it's
6381 * copied to allocated memory and stored in "*compl_arg".
6382 * Returns FAIL if something is wrong.
6383 */
6384 int
6385parse_compl_arg(value, vallen, complp, argt, compl_arg)
6386 char_u *value;
6387 int vallen;
6388 int *complp;
6389 long *argt;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006390 char_u **compl_arg UNUSED;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006391{
6392 char_u *arg = NULL;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006393# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006394 size_t arglen = 0;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006395# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006396 int i;
6397 int valend = vallen;
6398
6399 /* Look for any argument part - which is the part after any ',' */
6400 for (i = 0; i < vallen; ++i)
6401 {
6402 if (value[i] == ',')
6403 {
6404 arg = &value[i + 1];
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006405# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006406 arglen = vallen - i - 1;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006407# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006408 valend = i;
6409 break;
6410 }
6411 }
6412
6413 for (i = 0; command_complete[i].expand != 0; ++i)
6414 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006415 if ((int)STRLEN(command_complete[i].name) == valend
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006416 && STRNCMP(value, command_complete[i].name, valend) == 0)
6417 {
6418 *complp = command_complete[i].expand;
6419 if (command_complete[i].expand == EXPAND_BUFFERS)
6420 *argt |= BUFNAME;
6421 else if (command_complete[i].expand == EXPAND_DIRECTORIES
6422 || command_complete[i].expand == EXPAND_FILES)
6423 *argt |= XFILE;
6424 break;
6425 }
6426 }
6427
6428 if (command_complete[i].expand == 0)
6429 {
6430 EMSG2(_("E180: Invalid complete value: %s"), value);
6431 return FAIL;
6432 }
6433
6434# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6435 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST
6436 && arg != NULL)
6437# else
6438 if (arg != NULL)
6439# endif
6440 {
6441 EMSG(_("E468: Completion argument only allowed for custom completion"));
6442 return FAIL;
6443 }
6444
6445# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6446 if ((*complp == EXPAND_USER_DEFINED || *complp == EXPAND_USER_LIST)
6447 && arg == NULL)
6448 {
6449 EMSG(_("E467: Custom completion requires a function argument"));
6450 return FAIL;
6451 }
6452
6453 if (arg != NULL)
6454 *compl_arg = vim_strnsave(arg, (int)arglen);
6455# endif
6456 return OK;
6457}
6458#endif
6459
Bram Moolenaar071d4272004-06-13 20:20:40 +00006460 static void
6461ex_colorscheme(eap)
6462 exarg_T *eap;
6463{
Bram Moolenaare6850792010-05-14 15:28:44 +02006464 if (*eap->arg == NUL)
6465 {
6466#ifdef FEAT_EVAL
6467 char_u *expr = vim_strsave((char_u *)"g:colors_name");
6468 char_u *p = NULL;
6469
6470 if (expr != NULL)
6471 {
6472 ++emsg_off;
6473 p = eval_to_string(expr, NULL, FALSE);
6474 --emsg_off;
6475 vim_free(expr);
6476 }
6477 if (p != NULL)
6478 {
6479 MSG(p);
6480 vim_free(p);
6481 }
6482 else
6483 MSG("default");
6484#else
6485 MSG(_("unknown"));
6486#endif
6487 }
6488 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarbe1e9e92012-09-18 16:47:07 +02006489 EMSG2(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006490}
6491
6492 static void
6493ex_highlight(eap)
6494 exarg_T *eap;
6495{
6496 if (*eap->arg == NUL && eap->cmd[2] == '!')
6497 MSG(_("Greetings, Vim user!"));
6498 do_highlight(eap->arg, eap->forceit, FALSE);
6499}
6500
6501
6502/*
6503 * Call this function if we thought we were going to exit, but we won't
6504 * (because of an error). May need to restore the terminal mode.
6505 */
6506 void
6507not_exiting()
6508{
6509 exiting = FALSE;
6510 settmode(TMODE_RAW);
6511}
6512
6513/*
6514 * ":quit": quit current window, quit Vim if closed the last window.
6515 */
6516 static void
6517ex_quit(eap)
6518 exarg_T *eap;
6519{
6520#ifdef FEAT_CMDWIN
6521 if (cmdwin_type != 0)
6522 {
6523 cmdwin_result = Ctrl_C;
6524 return;
6525 }
6526#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006527 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006528 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006529 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006530 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006531 return;
6532 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006533#ifdef FEAT_AUTOCMD
Bram Moolenaar3b53dfb2012-06-06 18:03:07 +02006534 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01006535 /* Refuse to quit when locked or when the buffer in the last window is
Bram Moolenaar362ce482012-06-06 19:02:45 +02006536 * being closed (can only happen in autocommands). */
6537 if (curbuf_locked() || (curbuf->b_nwindows == 1 && curbuf->b_closing))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006538 return;
6539#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006540
6541#ifdef FEAT_NETBEANS_INTG
6542 netbeansForcedQuit = eap->forceit;
6543#endif
6544
6545 /*
6546 * If there are more files or windows we won't exit.
6547 */
6548 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6549 exiting = TRUE;
6550 if ((!P_HID(curbuf)
6551 && check_changed(curbuf, p_awa, FALSE, eap->forceit, FALSE))
6552 || check_more(TRUE, eap->forceit) == FAIL
6553 || (only_one_window() && check_changed_any(eap->forceit)))
6554 {
6555 not_exiting();
6556 }
6557 else
6558 {
6559#ifdef FEAT_WINDOWS
6560 if (only_one_window()) /* quit last window */
6561#endif
6562 getout(0);
6563#ifdef FEAT_WINDOWS
6564# ifdef FEAT_GUI
6565 need_mouse_correct = TRUE;
6566# endif
6567 /* close window; may free buffer */
6568 win_close(curwin, !P_HID(curwin->w_buffer) || eap->forceit);
6569#endif
6570 }
6571}
6572
6573/*
6574 * ":cquit".
6575 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006576 static void
6577ex_cquit(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006578 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006579{
6580 getout(1); /* this does not always pass on the exit code to the Manx
6581 compiler. why? */
6582}
6583
6584/*
6585 * ":qall": try to quit all windows
6586 */
6587 static void
6588ex_quit_all(eap)
6589 exarg_T *eap;
6590{
6591# ifdef FEAT_CMDWIN
6592 if (cmdwin_type != 0)
6593 {
6594 if (eap->forceit)
6595 cmdwin_result = K_XF1; /* ex_window() takes care of this */
6596 else
6597 cmdwin_result = K_XF2;
6598 return;
6599 }
6600# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006601
6602 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006603 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006604 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006605 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006606 return;
6607 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006608#ifdef FEAT_AUTOCMD
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01006609 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
6610 /* Refuse to quit when locked or when the buffer in the last window is
6611 * being closed (can only happen in autocommands). */
6612 if (curbuf_locked() || (curbuf->b_nwindows == 1 && curbuf->b_closing))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006613 return;
6614#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006615
Bram Moolenaar071d4272004-06-13 20:20:40 +00006616 exiting = TRUE;
6617 if (eap->forceit || !check_changed_any(FALSE))
6618 getout(0);
6619 not_exiting();
6620}
6621
Bram Moolenaard9967712006-03-11 21:18:15 +00006622#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006623/*
6624 * ":close": close current window, unless it is the last one
6625 */
6626 static void
6627ex_close(eap)
6628 exarg_T *eap;
6629{
6630# ifdef FEAT_CMDWIN
6631 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02006632 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006633 else
6634# endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006635 if (!text_locked()
6636#ifdef FEAT_AUTOCMD
6637 && !curbuf_locked()
6638#endif
6639 )
Bram Moolenaarf740b292006-02-16 22:11:02 +00006640 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006641}
6642
Bram Moolenaard9967712006-03-11 21:18:15 +00006643# ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006644/*
6645 * ":pclose": Close any preview window.
6646 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006647 static void
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006648ex_pclose(eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006649 exarg_T *eap;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006650{
6651 win_T *win;
6652
6653 for (win = firstwin; win != NULL; win = win->w_next)
6654 if (win->w_p_pvw)
6655 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006656 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006657 break;
6658 }
6659}
Bram Moolenaard9967712006-03-11 21:18:15 +00006660# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006661
Bram Moolenaarf740b292006-02-16 22:11:02 +00006662/*
6663 * Close window "win" and take care of handling closing the last window for a
6664 * modified buffer.
6665 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006666 static void
Bram Moolenaarf740b292006-02-16 22:11:02 +00006667ex_win_close(forceit, win, tp)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006668 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006669 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006670 tabpage_T *tp; /* NULL or the tab page "win" is in */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006671{
6672 int need_hide;
6673 buf_T *buf = win->w_buffer;
6674
6675 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006676 if (need_hide && !P_HID(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006677 {
Bram Moolenaard9967712006-03-11 21:18:15 +00006678# if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006679 if ((p_confirm || cmdmod.confirm) && p_write)
6680 {
6681 dialog_changed(buf, FALSE);
6682 if (buf_valid(buf) && bufIsChanged(buf))
6683 return;
6684 need_hide = FALSE;
6685 }
6686 else
Bram Moolenaard9967712006-03-11 21:18:15 +00006687# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006688 {
6689 EMSG(_(e_nowrtmsg));
6690 return;
6691 }
6692 }
6693
Bram Moolenaard9967712006-03-11 21:18:15 +00006694# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006695 need_mouse_correct = TRUE;
Bram Moolenaard9967712006-03-11 21:18:15 +00006696# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006697
Bram Moolenaar071d4272004-06-13 20:20:40 +00006698 /* free buffer when not hiding it or when it's a scratch buffer */
Bram Moolenaarf740b292006-02-16 22:11:02 +00006699 if (tp == NULL)
6700 win_close(win, !need_hide && !P_HID(buf));
6701 else
6702 win_close_othertab(win, !need_hide && !P_HID(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006703}
6704
Bram Moolenaar071d4272004-06-13 20:20:40 +00006705/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006706 * ":tabclose": close current tab page, unless it is the last one.
6707 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006708 */
6709 static void
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006710ex_tabclose(eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006711 exarg_T *eap;
6712{
Bram Moolenaarf740b292006-02-16 22:11:02 +00006713 tabpage_T *tp;
6714
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006715# ifdef FEAT_CMDWIN
6716 if (cmdwin_type != 0)
6717 cmdwin_result = K_IGNORE;
6718 else
6719# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006720 if (first_tabpage->tp_next == NULL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00006721 EMSG(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00006722 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006723 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006724 if (eap->addr_count > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006725 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006726 tp = find_tabpage((int)eap->line2);
6727 if (tp == NULL)
6728 {
6729 beep_flush();
6730 return;
6731 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00006732 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006733 {
6734 tabpage_close_other(tp, eap->forceit);
6735 return;
6736 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006737 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006738 if (!text_locked()
6739#ifdef FEAT_AUTOCMD
6740 && !curbuf_locked()
6741#endif
6742 )
Bram Moolenaarf740b292006-02-16 22:11:02 +00006743 tabpage_close(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006744 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006745}
6746
6747/*
6748 * ":tabonly": close all tab pages except the current one
6749 */
6750 static void
6751ex_tabonly(eap)
6752 exarg_T *eap;
6753{
6754 tabpage_T *tp;
6755 int done;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006756
6757# ifdef FEAT_CMDWIN
6758 if (cmdwin_type != 0)
6759 cmdwin_result = K_IGNORE;
6760 else
6761# endif
6762 if (first_tabpage->tp_next == NULL)
6763 MSG(_("Already only one tab page"));
6764 else
6765 {
6766 /* Repeat this up to a 1000 times, because autocommands may mess
6767 * up the lists. */
6768 for (done = 0; done < 1000; ++done)
6769 {
6770 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6771 if (tp->tp_topframe != topframe)
6772 {
6773 tabpage_close_other(tp, eap->forceit);
6774 /* if we failed to close it quit */
6775 if (valid_tabpage(tp))
6776 done = 1000;
6777 /* start over, "tp" is now invalid */
6778 break;
6779 }
6780 if (first_tabpage->tp_next == NULL)
6781 break;
6782 }
6783 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006784}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006785
6786/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006787 * Close the current tab page.
6788 */
6789 void
6790tabpage_close(forceit)
6791 int forceit;
6792{
6793 /* First close all the windows but the current one. If that worked then
6794 * close the last window in this tab, that will close it. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006795 if (lastwin != firstwin)
6796 close_others(TRUE, forceit);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006797 if (lastwin == firstwin)
6798 ex_win_close(forceit, curwin, NULL);
6799# ifdef FEAT_GUI
6800 need_mouse_correct = TRUE;
6801# endif
6802}
6803
6804/*
6805 * Close tab page "tp", which is not the current tab page.
6806 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006807 * Also takes care of the tab pages line disappearing when closing the
6808 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00006809 */
6810 void
6811tabpage_close_other(tp, forceit)
6812 tabpage_T *tp;
6813 int forceit;
6814{
6815 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006816 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006817 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006818
6819 /* Limit to 1000 windows, autocommands may add a window while we close
6820 * one. OK, so I'm paranoid... */
6821 while (++done < 1000)
6822 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006823 wp = tp->tp_firstwin;
6824 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006825
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006826 /* Autocommands may delete the tab page under our fingers and we may
6827 * fail to close a window with a modified buffer. */
6828 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006829 break;
6830 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006831
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006832 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006833 if (h != tabline_height())
6834 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006835}
6836
6837/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006838 * ":only".
6839 */
6840 static void
6841ex_only(eap)
6842 exarg_T *eap;
6843{
6844# ifdef FEAT_GUI
6845 need_mouse_correct = TRUE;
6846# endif
6847 close_others(TRUE, eap->forceit);
6848}
6849
6850/*
6851 * ":all" and ":sall".
Bram Moolenaard9967712006-03-11 21:18:15 +00006852 * Also used for ":tab drop file ..." after setting the argument list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006853 */
Bram Moolenaard9967712006-03-11 21:18:15 +00006854 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00006855ex_all(eap)
6856 exarg_T *eap;
6857{
6858 if (eap->addr_count == 0)
6859 eap->line2 = 9999;
Bram Moolenaard9967712006-03-11 21:18:15 +00006860 do_arg_all((int)eap->line2, eap->forceit, eap->cmdidx == CMD_drop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006861}
6862#endif /* FEAT_WINDOWS */
6863
6864 static void
6865ex_hide(eap)
6866 exarg_T *eap;
6867{
6868 if (*eap->arg != NUL && check_nextcmd(eap->arg) == NULL)
6869 eap->errmsg = e_invarg;
6870 else
6871 {
6872 /* ":hide" or ":hide | cmd": hide current window */
6873 eap->nextcmd = check_nextcmd(eap->arg);
6874#ifdef FEAT_WINDOWS
6875 if (!eap->skip)
6876 {
6877# ifdef FEAT_GUI
6878 need_mouse_correct = TRUE;
6879# endif
6880 win_close(curwin, FALSE); /* don't free buffer */
6881 }
6882#endif
6883 }
6884}
6885
6886/*
6887 * ":stop" and ":suspend": Suspend Vim.
6888 */
6889 static void
6890ex_stop(eap)
6891 exarg_T *eap;
6892{
6893 /*
6894 * Disallow suspending for "rvim".
6895 */
6896 if (!check_restricted()
6897#ifdef WIN3264
6898 /*
6899 * Check if external commands are allowed now.
6900 */
6901 && can_end_termcap_mode(TRUE)
6902#endif
6903 )
6904 {
6905 if (!eap->forceit)
6906 autowrite_all();
6907 windgoto((int)Rows - 1, 0);
6908 out_char('\n');
6909 out_flush();
6910 stoptermcap();
6911 out_flush(); /* needed for SUN to restore xterm buffer */
6912#ifdef FEAT_TITLE
6913 mch_restore_title(3); /* restore window titles */
6914#endif
6915 ui_suspend(); /* call machine specific function */
6916#ifdef FEAT_TITLE
6917 maketitle();
6918 resettitle(); /* force updating the title */
6919#endif
6920 starttermcap();
6921 scroll_start(); /* scroll screen before redrawing */
6922 redraw_later_clear();
6923 shell_resized(); /* may have resized window */
6924 }
6925}
6926
6927/*
6928 * ":exit", ":xit" and ":wq": Write file and exit Vim.
6929 */
6930 static void
6931ex_exit(eap)
6932 exarg_T *eap;
6933{
6934#ifdef FEAT_CMDWIN
6935 if (cmdwin_type != 0)
6936 {
6937 cmdwin_result = Ctrl_C;
6938 return;
6939 }
6940#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006941 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006942 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006943 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006944 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006945 return;
6946 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006947#ifdef FEAT_AUTOCMD
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01006948 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
6949 /* Refuse to quit when locked or when the buffer in the last window is
6950 * being closed (can only happen in autocommands). */
6951 if (curbuf_locked() || (curbuf->b_nwindows == 1 && curbuf->b_closing))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006952 return;
6953#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006954
6955 /*
6956 * if more files or windows we won't exit
6957 */
6958 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6959 exiting = TRUE;
6960 if ( ((eap->cmdidx == CMD_wq
6961 || curbufIsChanged())
6962 && do_write(eap) == FAIL)
6963 || check_more(TRUE, eap->forceit) == FAIL
6964 || (only_one_window() && check_changed_any(eap->forceit)))
6965 {
6966 not_exiting();
6967 }
6968 else
6969 {
6970#ifdef FEAT_WINDOWS
6971 if (only_one_window()) /* quit last window, exit Vim */
6972#endif
6973 getout(0);
6974#ifdef FEAT_WINDOWS
6975# ifdef FEAT_GUI
6976 need_mouse_correct = TRUE;
6977# endif
6978 /* quit current window, may free buffer */
6979 win_close(curwin, !P_HID(curwin->w_buffer));
6980#endif
6981 }
6982}
6983
6984/*
6985 * ":print", ":list", ":number".
6986 */
6987 static void
6988ex_print(eap)
6989 exarg_T *eap;
6990{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006991 if (curbuf->b_ml.ml_flags & ML_EMPTY)
6992 EMSG(_(e_emptybuf));
6993 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006994 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00006995 for ( ;!got_int; ui_breakcheck())
6996 {
6997 print_line(eap->line1,
6998 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
6999 || (eap->flags & EXFLAG_NR)),
7000 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
7001 if (++eap->line1 > eap->line2)
7002 break;
7003 out_flush(); /* show one line at a time */
7004 }
7005 setpcmark();
7006 /* put cursor at last line */
7007 curwin->w_cursor.lnum = eap->line2;
7008 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007009 }
7010
Bram Moolenaar071d4272004-06-13 20:20:40 +00007011 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007012}
7013
7014#ifdef FEAT_BYTEOFF
7015 static void
7016ex_goto(eap)
7017 exarg_T *eap;
7018{
7019 goto_byte(eap->line2);
7020}
7021#endif
7022
7023/*
7024 * ":shell".
7025 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007026 static void
7027ex_shell(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007028 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007029{
7030 do_shell(NULL, 0);
7031}
7032
7033#if (defined(FEAT_WINDOWS) && defined(HAVE_DROP_FILE)) \
7034 || (defined(FEAT_GUI_GTK) && defined(FEAT_DND)) \
Bram Moolenaar371baa92005-12-29 22:43:53 +00007035 || defined(FEAT_GUI_MSWIN) \
7036 || defined(FEAT_GUI_MAC) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00007037 || defined(PROTO)
7038
7039/*
7040 * Handle a file drop. The code is here because a drop is *nearly* like an
7041 * :args command, but not quite (we have a list of exact filenames, so we
7042 * don't want to (a) parse a command line, or (b) expand wildcards. So the
7043 * code is very similar to :args and hence needs access to a lot of the static
7044 * functions in this file.
7045 *
7046 * The list should be allocated using alloc(), as should each item in the
7047 * list. This function takes over responsibility for freeing the list.
7048 *
Bram Moolenaar81870892007-11-11 18:17:28 +00007049 * XXX The list is made into the argument list. This is freed using
Bram Moolenaar071d4272004-06-13 20:20:40 +00007050 * FreeWild(), which does a series of vim_free() calls, unless the two defines
7051 * __EMX__ and __ALWAYS_HAS_TRAILING_NUL_POINTER are set. In this case, a
7052 * routine _fnexplodefree() is used. This may cause problems, but as the drop
7053 * file functionality is (currently) not in EMX this is not presently a
7054 * problem.
7055 */
7056 void
7057handle_drop(filec, filev, split)
7058 int filec; /* the number of files dropped */
7059 char_u **filev; /* the list of files dropped */
7060 int split; /* force splitting the window */
7061{
7062 exarg_T ea;
7063 int save_msg_scroll = msg_scroll;
7064
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007065 /* Postpone this while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007066 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007067 return;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007068#ifdef FEAT_AUTOCMD
7069 if (curbuf_locked())
7070 return;
7071#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00007072 /* When the screen is being updated we should not change buffers and
7073 * windows structures, it may cause freed memory to be used. */
7074 if (updating_screen)
7075 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007076
7077 /* Check whether the current buffer is changed. If so, we will need
7078 * to split the current window or data could be lost.
7079 * We don't need to check if the 'hidden' option is set, as in this
7080 * case the buffer won't be lost.
7081 */
7082 if (!P_HID(curbuf) && !split)
7083 {
7084 ++emsg_off;
7085 split = check_changed(curbuf, TRUE, FALSE, FALSE, FALSE);
7086 --emsg_off;
7087 }
7088 if (split)
7089 {
7090# ifdef FEAT_WINDOWS
7091 if (win_split(0, 0) == FAIL)
7092 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007093 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007094
7095 /* When splitting the window, create a new alist. Otherwise the
7096 * existing one is overwritten. */
7097 alist_unlink(curwin->w_alist);
7098 alist_new();
7099# else
7100 return; /* can't split, always fail */
7101# endif
7102 }
7103
7104 /*
7105 * Set up the new argument list.
7106 */
Bram Moolenaar86b68352004-12-27 21:59:20 +00007107 alist_set(ALIST(curwin), filec, filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007108
7109 /*
7110 * Move to the first file.
7111 */
7112 /* Fake up a minimal "next" command for do_argfile() */
7113 vim_memset(&ea, 0, sizeof(ea));
7114 ea.cmd = (char_u *)"next";
7115 do_argfile(&ea, 0);
7116
7117 /* do_ecmd() may set need_start_insertmode, but since we never left Insert
7118 * mode that is not needed here. */
7119 need_start_insertmode = FALSE;
7120
7121 /* Restore msg_scroll, otherwise a following command may cause scrolling
7122 * unexpectedly. The screen will be redrawn by the caller, thus
7123 * msg_scroll being set by displaying a message is irrelevant. */
7124 msg_scroll = save_msg_scroll;
7125}
7126#endif
7127
Bram Moolenaar071d4272004-06-13 20:20:40 +00007128/*
7129 * Clear an argument list: free all file names and reset it to zero entries.
7130 */
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007131 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00007132alist_clear(al)
7133 alist_T *al;
7134{
7135 while (--al->al_ga.ga_len >= 0)
7136 vim_free(AARGLIST(al)[al->al_ga.ga_len].ae_fname);
7137 ga_clear(&al->al_ga);
7138}
7139
7140/*
7141 * Init an argument list.
7142 */
7143 void
7144alist_init(al)
7145 alist_T *al;
7146{
7147 ga_init2(&al->al_ga, (int)sizeof(aentry_T), 5);
7148}
7149
7150#if defined(FEAT_WINDOWS) || defined(PROTO)
7151
7152/*
7153 * Remove a reference from an argument list.
7154 * Ignored when the argument list is the global one.
7155 * If the argument list is no longer used by any window, free it.
7156 */
7157 void
7158alist_unlink(al)
7159 alist_T *al;
7160{
7161 if (al != &global_alist && --al->al_refcount <= 0)
7162 {
7163 alist_clear(al);
7164 vim_free(al);
7165 }
7166}
7167
7168# if defined(FEAT_LISTCMDS) || defined(HAVE_DROP_FILE) || defined(PROTO)
7169/*
7170 * Create a new argument list and use it for the current window.
7171 */
7172 void
7173alist_new()
7174{
7175 curwin->w_alist = (alist_T *)alloc((unsigned)sizeof(alist_T));
7176 if (curwin->w_alist == NULL)
7177 {
7178 curwin->w_alist = &global_alist;
7179 ++global_alist.al_refcount;
7180 }
7181 else
7182 {
7183 curwin->w_alist->al_refcount = 1;
7184 alist_init(curwin->w_alist);
7185 }
7186}
7187# endif
7188#endif
7189
7190#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE) || defined(PROTO)
7191/*
7192 * Expand the file names in the global argument list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00007193 * If "fnum_list" is not NULL, use "fnum_list[fnum_len]" as a list of buffer
7194 * numbers to be re-used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007195 */
7196 void
Bram Moolenaar86b68352004-12-27 21:59:20 +00007197alist_expand(fnum_list, fnum_len)
7198 int *fnum_list;
7199 int fnum_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007200{
7201 char_u **old_arg_files;
Bram Moolenaar86b68352004-12-27 21:59:20 +00007202 int old_arg_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007203 char_u **new_arg_files;
7204 int new_arg_file_count;
7205 char_u *save_p_su = p_su;
7206 int i;
7207
7208 /* Don't use 'suffixes' here. This should work like the shell did the
7209 * expansion. Also, the vimrc file isn't read yet, thus the user
7210 * can't set the options. */
7211 p_su = empty_option;
7212 old_arg_files = (char_u **)alloc((unsigned)(sizeof(char_u *) * GARGCOUNT));
7213 if (old_arg_files != NULL)
7214 {
7215 for (i = 0; i < GARGCOUNT; ++i)
Bram Moolenaar86b68352004-12-27 21:59:20 +00007216 old_arg_files[i] = vim_strsave(GARGLIST[i].ae_fname);
7217 old_arg_count = GARGCOUNT;
7218 if (expand_wildcards(old_arg_count, old_arg_files,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007219 &new_arg_file_count, &new_arg_files,
Bram Moolenaarb5609832011-07-20 15:04:58 +02007220 EW_FILE|EW_NOTFOUND|EW_ADDSLASH|EW_NOERROR) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007221 && new_arg_file_count > 0)
7222 {
Bram Moolenaar86b68352004-12-27 21:59:20 +00007223 alist_set(&global_alist, new_arg_file_count, new_arg_files,
7224 TRUE, fnum_list, fnum_len);
7225 FreeWild(old_arg_count, old_arg_files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007226 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007227 }
7228 p_su = save_p_su;
7229}
7230#endif
7231
7232/*
7233 * Set the argument list for the current window.
7234 * Takes over the allocated files[] and the allocated fnames in it.
7235 */
7236 void
Bram Moolenaar86b68352004-12-27 21:59:20 +00007237alist_set(al, count, files, use_curbuf, fnum_list, fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007238 alist_T *al;
7239 int count;
7240 char_u **files;
7241 int use_curbuf;
Bram Moolenaar86b68352004-12-27 21:59:20 +00007242 int *fnum_list;
7243 int fnum_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007244{
7245 int i;
7246
7247 alist_clear(al);
7248 if (ga_grow(&al->al_ga, count) == OK)
7249 {
7250 for (i = 0; i < count; ++i)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007251 {
7252 if (got_int)
7253 {
7254 /* When adding many buffers this can take a long time. Allow
7255 * interrupting here. */
7256 while (i < count)
7257 vim_free(files[i++]);
7258 break;
7259 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00007260
7261 /* May set buffer name of a buffer previously used for the
7262 * argument list, so that it's re-used by alist_add. */
7263 if (fnum_list != NULL && i < fnum_len)
7264 buf_set_name(fnum_list[i], files[i]);
7265
Bram Moolenaar071d4272004-06-13 20:20:40 +00007266 alist_add(al, files[i], use_curbuf ? 2 : 1);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007267 ui_breakcheck();
7268 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007269 vim_free(files);
7270 }
7271 else
7272 FreeWild(count, files);
7273#ifdef FEAT_WINDOWS
7274 if (al == &global_alist)
7275#endif
7276 arg_had_last = FALSE;
7277}
7278
7279/*
7280 * Add file "fname" to argument list "al".
7281 * "fname" must have been allocated and "al" must have been checked for room.
7282 */
7283 void
7284alist_add(al, fname, set_fnum)
7285 alist_T *al;
7286 char_u *fname;
7287 int set_fnum; /* 1: set buffer number; 2: re-use curbuf */
7288{
7289 if (fname == NULL) /* don't add NULL file names */
7290 return;
7291#ifdef BACKSLASH_IN_FILENAME
7292 slash_adjust(fname);
7293#endif
7294 AARGLIST(al)[al->al_ga.ga_len].ae_fname = fname;
7295 if (set_fnum > 0)
7296 AARGLIST(al)[al->al_ga.ga_len].ae_fnum =
7297 buflist_add(fname, BLN_LISTED | (set_fnum == 2 ? BLN_CURBUF : 0));
7298 ++al->al_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007299}
7300
7301#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
7302/*
7303 * Adjust slashes in file names. Called after 'shellslash' was set.
7304 */
7305 void
7306alist_slash_adjust()
7307{
7308 int i;
7309# ifdef FEAT_WINDOWS
7310 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007311 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007312# endif
7313
7314 for (i = 0; i < GARGCOUNT; ++i)
7315 if (GARGLIST[i].ae_fname != NULL)
7316 slash_adjust(GARGLIST[i].ae_fname);
7317# ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00007318 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007319 if (wp->w_alist != &global_alist)
7320 for (i = 0; i < WARGCOUNT(wp); ++i)
7321 if (WARGLIST(wp)[i].ae_fname != NULL)
7322 slash_adjust(WARGLIST(wp)[i].ae_fname);
7323# endif
7324}
7325#endif
7326
7327/*
7328 * ":preserve".
7329 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007330 static void
7331ex_preserve(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007332 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007333{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007334 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007335 ml_preserve(curbuf, TRUE);
7336}
7337
7338/*
7339 * ":recover".
7340 */
7341 static void
7342ex_recover(eap)
7343 exarg_T *eap;
7344{
7345 /* Set recoverymode right away to avoid the ATTENTION prompt. */
7346 recoverymode = TRUE;
7347 if (!check_changed(curbuf, p_awa, TRUE, eap->forceit, FALSE)
7348 && (*eap->arg == NUL
7349 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
7350 ml_recover();
7351 recoverymode = FALSE;
7352}
7353
7354/*
7355 * Command modifier used in a wrong way.
7356 */
7357 static void
7358ex_wrongmodifier(eap)
7359 exarg_T *eap;
7360{
7361 eap->errmsg = e_invcmd;
7362}
7363
7364#ifdef FEAT_WINDOWS
7365/*
7366 * :sview [+command] file split window with new file, read-only
7367 * :split [[+command] file] split window with current or new file
7368 * :vsplit [[+command] file] split window vertically with current or new file
7369 * :new [[+command] file] split window with no or new file
7370 * :vnew [[+command] file] split vertically window with no or new file
7371 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007372 *
7373 * :tabedit open new Tab page with empty window
7374 * :tabedit [+command] file open new Tab page and edit "file"
7375 * :tabnew [[+command] file] just like :tabedit
7376 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007377 */
7378 void
7379ex_splitview(eap)
7380 exarg_T *eap;
7381{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007382 win_T *old_curwin = curwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007383# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007384 char_u *fname = NULL;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007385# endif
7386# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007387 int browse_flag = cmdmod.browse;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007388# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007389
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007390# ifndef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007391 if (eap->cmdidx == CMD_vsplit || eap->cmdidx == CMD_vnew)
7392 {
7393 ex_ni(eap);
7394 return;
7395 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007396# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007397
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007398# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007399 need_mouse_correct = TRUE;
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_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00007403 /* A ":split" in the quickfix window works like ":new". Don't want two
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007404 * quickfix windows. But it's OK when doing ":tab split". */
7405 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007406 {
7407 if (eap->cmdidx == CMD_split)
7408 eap->cmdidx = CMD_new;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007409# ifdef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007410 if (eap->cmdidx == CMD_vsplit)
7411 eap->cmdidx = CMD_vnew;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007412# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007413 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007414# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007415
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007416# ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007417 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007418 {
7419 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
7420 FNAME_MESS, TRUE, curbuf->b_ffname);
7421 if (fname == NULL)
7422 goto theend;
7423 eap->arg = fname;
7424 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007425# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007426 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007427# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007428# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007429# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007430 if (cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007431# ifdef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007432 && eap->cmdidx != CMD_vnew
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007433# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007434 && eap->cmdidx != CMD_new)
7435 {
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007436# ifdef FEAT_AUTOCMD
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007437 if (
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007438# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007439 !gui.in_use &&
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007440# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007441 au_has_group((char_u *)"FileExplorer"))
7442 {
7443 /* No browsing supported but we do have the file explorer:
7444 * Edit the directory. */
7445 if (*eap->arg == NUL || !mch_isdir(eap->arg))
7446 eap->arg = (char_u *)".";
7447 }
7448 else
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007449# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007450 {
7451 fname = do_browse(0, (char_u *)_("Edit File in new window"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007452 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007453 if (fname == NULL)
7454 goto theend;
7455 eap->arg = fname;
7456 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007457 }
7458 cmdmod.browse = FALSE; /* Don't browse again in do_ecmd(). */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007459# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007460
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007461 /*
7462 * Either open new tab page or split the window.
7463 */
7464 if (eap->cmdidx == CMD_tabedit
7465 || eap->cmdidx == CMD_tabfind
7466 || eap->cmdidx == CMD_tabnew)
7467 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00007468 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
7469 : eap->addr_count == 0 ? 0
7470 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007471 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00007472 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007473
7474 /* set the alternate buffer for the window we came from */
7475 if (curwin != old_curwin
7476 && win_valid(old_curwin)
7477 && old_curwin->w_buffer != curbuf
7478 && !cmdmod.keepalt)
7479 old_curwin->w_alt_fnum = curbuf->b_fnum;
7480 }
7481 }
7482 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007483 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
7484 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007485# ifdef FEAT_SCROLLBIND
Bram Moolenaar071d4272004-06-13 20:20:40 +00007486 /* Reset 'scrollbind' when editing another file, but keep it when
7487 * doing ":split" without arguments. */
7488 if (*eap->arg != NUL
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007489# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007490 || cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007491# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007492 )
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007493 {
7494 RESET_BINDING(curwin);
7495 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007496 else
7497 do_check_scrollbind(FALSE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007498# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007499 do_exedit(eap, old_curwin);
7500 }
7501
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007502# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007503 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007504# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007505
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007506# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007507theend:
7508 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007509# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007510}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007511
7512/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007513 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007514 */
7515 void
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007516tabpage_new()
7517{
7518 exarg_T ea;
7519
7520 vim_memset(&ea, 0, sizeof(ea));
7521 ea.cmdidx = CMD_tabnew;
7522 ea.cmd = (char_u *)"tabn";
7523 ea.arg = (char_u *)"";
7524 ex_splitview(&ea);
7525}
7526
7527/*
7528 * :tabnext command
7529 */
7530 static void
7531ex_tabnext(eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007532 exarg_T *eap;
7533{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007534 switch (eap->cmdidx)
7535 {
7536 case CMD_tabfirst:
7537 case CMD_tabrewind:
7538 goto_tabpage(1);
7539 break;
7540 case CMD_tablast:
7541 goto_tabpage(9999);
7542 break;
7543 case CMD_tabprevious:
7544 case CMD_tabNext:
7545 goto_tabpage(eap->addr_count == 0 ? -1 : -(int)eap->line2);
7546 break;
7547 default: /* CMD_tabnext */
7548 goto_tabpage(eap->addr_count == 0 ? 0 : (int)eap->line2);
7549 break;
7550 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007551}
7552
7553/*
7554 * :tabmove command
7555 */
7556 static void
7557ex_tabmove(eap)
7558 exarg_T *eap;
7559{
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02007560 int tab_number = 9999;
7561
7562 if (eap->arg && *eap->arg != NUL)
7563 {
7564 char_u *p = eap->arg;
7565 int relative = 0; /* argument +N/-N means: move N places to the
7566 * right/left relative to the current position. */
7567
7568 if (*eap->arg == '-')
7569 {
7570 relative = -1;
7571 p = eap->arg + 1;
7572 }
7573 else if (*eap->arg == '+')
7574 {
7575 relative = 1;
7576 p = eap->arg + 1;
7577 }
7578 else
7579 p = eap->arg;
7580
7581 if (p == skipdigits(p))
7582 {
7583 /* No numbers as argument. */
7584 eap->errmsg = e_invarg;
7585 return;
7586 }
7587
7588 tab_number = getdigits(&p);
7589 if (relative != 0)
7590 tab_number = tab_number * relative + tabpage_index(curtab) - 1;;
7591 }
7592 else if (eap->addr_count != 0)
7593 tab_number = eap->line2;
7594
7595 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007596}
7597
7598/*
7599 * :tabs command: List tabs and their contents.
7600 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007601 static void
7602ex_tabs(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007603 exarg_T *eap UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007604{
7605 tabpage_T *tp;
7606 win_T *wp;
7607 int tabcount = 1;
7608
7609 msg_start();
7610 msg_scroll = TRUE;
7611 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
7612 {
7613 msg_putchar('\n');
7614 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
7615 msg_outtrans_attr(IObuff, hl_attr(HLF_T));
7616 out_flush(); /* output one line at a time */
7617 ui_breakcheck();
7618
Bram Moolenaar030f0df2006-02-21 22:02:53 +00007619 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007620 wp = firstwin;
7621 else
7622 wp = tp->tp_firstwin;
7623 for ( ; wp != NULL && !got_int; wp = wp->w_next)
7624 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007625 msg_putchar('\n');
7626 msg_putchar(wp == curwin ? '>' : ' ');
7627 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007628 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
7629 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007630 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02007631 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007632 else
7633 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
7634 IObuff, IOSIZE, TRUE);
7635 msg_outtrans(IObuff);
7636 out_flush(); /* output one line at a time */
7637 ui_breakcheck();
7638 }
7639 }
7640}
7641
7642#endif /* FEAT_WINDOWS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007643
7644/*
7645 * ":mode": Set screen mode.
7646 * If no argument given, just get the screen size and redraw.
7647 */
7648 static void
7649ex_mode(eap)
7650 exarg_T *eap;
7651{
7652 if (*eap->arg == NUL)
7653 shell_resized();
7654 else
7655 mch_screenmode(eap->arg);
7656}
7657
7658#ifdef FEAT_WINDOWS
7659/*
7660 * ":resize".
7661 * set, increment or decrement current window height
7662 */
7663 static void
7664ex_resize(eap)
7665 exarg_T *eap;
7666{
7667 int n;
7668 win_T *wp = curwin;
7669
7670 if (eap->addr_count > 0)
7671 {
7672 n = eap->line2;
7673 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
7674 ;
7675 }
7676
7677#ifdef FEAT_GUI
7678 need_mouse_correct = TRUE;
7679#endif
7680 n = atol((char *)eap->arg);
7681#ifdef FEAT_VERTSPLIT
7682 if (cmdmod.split & WSP_VERT)
7683 {
7684 if (*eap->arg == '-' || *eap->arg == '+')
7685 n += W_WIDTH(curwin);
7686 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
7687 n = 9999;
7688 win_setwidth_win((int)n, wp);
7689 }
7690 else
7691#endif
7692 {
7693 if (*eap->arg == '-' || *eap->arg == '+')
7694 n += curwin->w_height;
7695 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
7696 n = 9999;
7697 win_setheight_win((int)n, wp);
7698 }
7699}
7700#endif
7701
7702/*
7703 * ":find [+command] <file>" command.
7704 */
7705 static void
7706ex_find(eap)
7707 exarg_T *eap;
7708{
7709#ifdef FEAT_SEARCHPATH
7710 char_u *fname;
7711 int count;
7712
7713 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
7714 TRUE, curbuf->b_ffname);
7715 if (eap->addr_count > 0)
7716 {
7717 /* Repeat finding the file "count" times. This matters when it
7718 * appears several times in the path. */
7719 count = eap->line2;
7720 while (fname != NULL && --count > 0)
7721 {
7722 vim_free(fname);
7723 fname = find_file_in_path(NULL, 0, FNAME_MESS,
7724 FALSE, curbuf->b_ffname);
7725 }
7726 }
7727
7728 if (fname != NULL)
7729 {
7730 eap->arg = fname;
7731#endif
7732 do_exedit(eap, NULL);
7733#ifdef FEAT_SEARCHPATH
7734 vim_free(fname);
7735 }
7736#endif
7737}
7738
7739/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00007740 * ":open" simulation: for now just work like ":visual".
7741 */
7742 static void
7743ex_open(eap)
7744 exarg_T *eap;
7745{
7746 regmatch_T regmatch;
7747 char_u *p;
7748
7749 curwin->w_cursor.lnum = eap->line2;
7750 beginline(BL_SOL | BL_FIX);
7751 if (*eap->arg == '/')
7752 {
7753 /* ":open /pattern/": put cursor in column found with pattern */
7754 ++eap->arg;
7755 p = skip_regexp(eap->arg, '/', p_magic, NULL);
7756 *p = NUL;
7757 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
7758 if (regmatch.regprog != NULL)
7759 {
7760 regmatch.rm_ic = p_ic;
7761 p = ml_get_curline();
7762 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007763 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007764 else
7765 EMSG(_(e_nomatch));
7766 vim_free(regmatch.regprog);
7767 }
7768 /* Move to the NUL, ignore any other arguments. */
7769 eap->arg += STRLEN(eap->arg);
7770 }
7771 check_cursor();
7772
7773 eap->cmdidx = CMD_visual;
7774 do_exedit(eap, NULL);
7775}
7776
7777/*
7778 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007779 */
7780 static void
7781ex_edit(eap)
7782 exarg_T *eap;
7783{
7784 do_exedit(eap, NULL);
7785}
7786
7787/*
7788 * ":edit <file>" command and alikes.
7789 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007790 void
7791do_exedit(eap, old_curwin)
7792 exarg_T *eap;
7793 win_T *old_curwin; /* curwin before doing a split or NULL */
7794{
7795 int n;
7796#ifdef FEAT_WINDOWS
7797 int need_hide;
7798#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00007799 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007800
7801 /*
7802 * ":vi" command ends Ex mode.
7803 */
7804 if (exmode_active && (eap->cmdidx == CMD_visual
7805 || eap->cmdidx == CMD_view))
7806 {
7807 exmode_active = FALSE;
7808 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007809 {
7810 /* Special case: ":global/pat/visual\NLvi-commands" */
7811 if (global_busy)
7812 {
7813 int rd = RedrawingDisabled;
7814 int nwr = no_wait_return;
7815 int ms = msg_scroll;
7816#ifdef FEAT_GUI
7817 int he = hold_gui_events;
7818#endif
7819
7820 if (eap->nextcmd != NULL)
7821 {
7822 stuffReadbuff(eap->nextcmd);
7823 eap->nextcmd = NULL;
7824 }
7825
7826 if (exmode_was != EXMODE_VIM)
7827 settmode(TMODE_RAW);
7828 RedrawingDisabled = 0;
7829 no_wait_return = 0;
7830 need_wait_return = FALSE;
7831 msg_scroll = 0;
7832#ifdef FEAT_GUI
7833 hold_gui_events = 0;
7834#endif
7835 must_redraw = CLEAR;
7836
7837 main_loop(FALSE, TRUE);
7838
7839 RedrawingDisabled = rd;
7840 no_wait_return = nwr;
7841 msg_scroll = ms;
7842#ifdef FEAT_GUI
7843 hold_gui_events = he;
7844#endif
7845 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007846 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007847 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007848 }
7849
7850 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007851 || eap->cmdidx == CMD_tabnew
7852 || eap->cmdidx == CMD_tabedit
Bram Moolenaar071d4272004-06-13 20:20:40 +00007853#ifdef FEAT_VERTSPLIT
7854 || eap->cmdidx == CMD_vnew
7855#endif
7856 ) && *eap->arg == NUL)
7857 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007858 /* ":new" or ":tabnew" without argument: edit an new empty buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007859 setpcmark();
7860 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007861 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
7862 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007863 }
7864 else if ((eap->cmdidx != CMD_split
7865#ifdef FEAT_VERTSPLIT
7866 && eap->cmdidx != CMD_vsplit
7867#endif
7868 )
7869 || *eap->arg != NUL
7870#ifdef FEAT_BROWSE
7871 || cmdmod.browse
7872#endif
7873 )
7874 {
Bram Moolenaar5555acc2006-04-07 21:33:12 +00007875#ifdef FEAT_AUTOCMD
7876 /* Can't edit another file when "curbuf_lock" is set. Only ":edit"
7877 * can bring us here, others are stopped earlier. */
7878 if (*eap->arg != NUL && curbuf_locked())
7879 return;
7880#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007881 n = readonlymode;
7882 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
7883 readonlymode = TRUE;
7884 else if (eap->cmdidx == CMD_enew)
7885 readonlymode = FALSE; /* 'readonly' doesn't make sense in an
7886 empty buffer */
7887 setpcmark();
7888 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
7889 NULL, eap,
7890 /* ":edit" goes to first line if Vi compatible */
7891 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
7892 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
7893 ? ECMD_ONE : eap->do_ecmd_lnum,
7894 (P_HID(curbuf) ? ECMD_HIDE : 0)
7895 + (eap->forceit ? ECMD_FORCEIT : 0)
7896#ifdef FEAT_LISTCMDS
7897 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
7898#endif
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007899 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007900 {
7901 /* Editing the file failed. If the window was split, close it. */
7902#ifdef FEAT_WINDOWS
7903 if (old_curwin != NULL)
7904 {
7905 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
7906 if (!need_hide || P_HID(curbuf))
7907 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007908# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7909 cleanup_T cs;
7910
7911 /* Reset the error/interrupt/exception state here so that
7912 * aborting() returns FALSE when closing a window. */
7913 enter_cleanup(&cs);
7914# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007915# ifdef FEAT_GUI
7916 need_mouse_correct = TRUE;
7917# endif
7918 win_close(curwin, !need_hide && !P_HID(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007919
7920# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7921 /* Restore the error/interrupt/exception state if not
7922 * discarded by a new aborting error, interrupt, or
7923 * uncaught exception. */
7924 leave_cleanup(&cs);
7925# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007926 }
7927 }
7928#endif
7929 }
7930 else if (readonlymode && curbuf->b_nwindows == 1)
7931 {
7932 /* When editing an already visited buffer, 'readonly' won't be set
7933 * but the previous value is kept. With ":view" and ":sview" we
7934 * want the file to be readonly, except when another window is
7935 * editing the same buffer. */
7936 curbuf->b_p_ro = TRUE;
7937 }
7938 readonlymode = n;
7939 }
7940 else
7941 {
7942 if (eap->do_ecmd_cmd != NULL)
7943 do_cmdline_cmd(eap->do_ecmd_cmd);
7944#ifdef FEAT_TITLE
7945 n = curwin->w_arg_idx_invalid;
7946#endif
7947 check_arg_idx(curwin);
7948#ifdef FEAT_TITLE
7949 if (n != curwin->w_arg_idx_invalid)
7950 maketitle();
7951#endif
7952 }
7953
7954#ifdef FEAT_WINDOWS
7955 /*
7956 * if ":split file" worked, set alternate file name in old window to new
7957 * file
7958 */
7959 if (old_curwin != NULL
7960 && *eap->arg != NUL
7961 && curwin != old_curwin
7962 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00007963 && old_curwin->w_buffer != curbuf
7964 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007965 old_curwin->w_alt_fnum = curbuf->b_fnum;
7966#endif
7967
7968 ex_no_reprint = TRUE;
7969}
7970
7971#ifndef FEAT_GUI
7972/*
7973 * ":gui" and ":gvim" when there is no GUI.
7974 */
7975 static void
7976ex_nogui(eap)
7977 exarg_T *eap;
7978{
7979 eap->errmsg = e_nogvim;
7980}
7981#endif
7982
7983#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
7984 static void
7985ex_tearoff(eap)
7986 exarg_T *eap;
7987{
7988 gui_make_tearoff(eap->arg);
7989}
7990#endif
7991
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007992#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007993 static void
7994ex_popup(eap)
7995 exarg_T *eap;
7996{
Bram Moolenaar97409f12005-07-08 22:17:29 +00007997 gui_make_popup(eap->arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007998}
7999#endif
8000
Bram Moolenaar071d4272004-06-13 20:20:40 +00008001 static void
8002ex_swapname(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008003 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008004{
8005 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
8006 MSG(_("No swap file"));
8007 else
8008 msg(curbuf->b_ml.ml_mfp->mf_fname);
8009}
8010
8011/*
8012 * ":syncbind" forces all 'scrollbind' windows to have the same relative
8013 * offset.
8014 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
8015 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008016 static void
8017ex_syncbind(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008018 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008019{
8020#ifdef FEAT_SCROLLBIND
8021 win_T *wp;
8022 long topline;
8023 long y;
8024 linenr_T old_linenr = curwin->w_cursor.lnum;
8025
8026 setpcmark();
8027
8028 /*
8029 * determine max topline
8030 */
8031 if (curwin->w_p_scb)
8032 {
8033 topline = curwin->w_topline;
8034 for (wp = firstwin; wp; wp = wp->w_next)
8035 {
8036 if (wp->w_p_scb && wp->w_buffer)
8037 {
8038 y = wp->w_buffer->b_ml.ml_line_count - p_so;
8039 if (topline > y)
8040 topline = y;
8041 }
8042 }
8043 if (topline < 1)
8044 topline = 1;
8045 }
8046 else
8047 {
8048 topline = 1;
8049 }
8050
8051
8052 /*
8053 * set all scrollbind windows to the same topline
8054 */
8055 wp = curwin;
8056 for (curwin = firstwin; curwin; curwin = curwin->w_next)
8057 {
8058 if (curwin->w_p_scb)
8059 {
8060 y = topline - curwin->w_topline;
8061 if (y > 0)
8062 scrollup(y, TRUE);
8063 else
8064 scrolldown(-y, TRUE);
8065 curwin->w_scbind_pos = topline;
8066 redraw_later(VALID);
8067 cursor_correct();
8068#ifdef FEAT_WINDOWS
8069 curwin->w_redr_status = TRUE;
8070#endif
8071 }
8072 }
8073 curwin = wp;
8074 if (curwin->w_p_scb)
8075 {
8076 did_syncbind = TRUE;
8077 checkpcmark();
8078 if (old_linenr != curwin->w_cursor.lnum)
8079 {
8080 char_u ctrl_o[2];
8081
8082 ctrl_o[0] = Ctrl_O;
8083 ctrl_o[1] = 0;
8084 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
8085 }
8086 }
8087#endif
8088}
8089
8090
8091 static void
8092ex_read(eap)
8093 exarg_T *eap;
8094{
Bram Moolenaardf177f62005-02-22 08:39:57 +00008095 int i;
8096 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
8097 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008098
8099 if (eap->usefilter) /* :r!cmd */
8100 do_bang(1, eap, FALSE, FALSE, TRUE);
8101 else
8102 {
8103 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
8104 return;
8105
8106#ifdef FEAT_BROWSE
8107 if (cmdmod.browse)
8108 {
8109 char_u *browseFile;
8110
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008111 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008112 NULL, NULL, NULL, curbuf);
8113 if (browseFile != NULL)
8114 {
8115 i = readfile(browseFile, NULL,
8116 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8117 vim_free(browseFile);
8118 }
8119 else
8120 i = OK;
8121 }
8122 else
8123#endif
8124 if (*eap->arg == NUL)
8125 {
8126 if (check_fname() == FAIL) /* check for no file name */
8127 return;
8128 i = readfile(curbuf->b_ffname, curbuf->b_fname,
8129 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8130 }
8131 else
8132 {
8133 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
8134 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
8135 i = readfile(eap->arg, NULL,
8136 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8137
8138 }
8139 if (i == FAIL)
8140 {
8141#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
8142 if (!aborting())
8143#endif
8144 EMSG2(_(e_notopen), eap->arg);
8145 }
8146 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008147 {
8148 if (empty && exmode_active)
8149 {
8150 /* Delete the empty line that remains. Historically ex does
8151 * this but vi doesn't. */
8152 if (eap->line2 == 0)
8153 lnum = curbuf->b_ml.ml_line_count;
8154 else
8155 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008156 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008157 {
8158 ml_delete(lnum, FALSE);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008159 if (curwin->w_cursor.lnum > 1
8160 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008161 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00008162 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008163 }
8164 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008165 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008166 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008167 }
8168}
8169
Bram Moolenaarea408852005-06-25 22:49:46 +00008170static char_u *prev_dir = NULL;
8171
8172#if defined(EXITFREE) || defined(PROTO)
8173 void
8174free_cd_dir()
8175{
8176 vim_free(prev_dir);
Bram Moolenaara0174af2008-01-02 20:08:25 +00008177 prev_dir = NULL;
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00008178
8179 vim_free(globaldir);
8180 globaldir = NULL;
Bram Moolenaarea408852005-06-25 22:49:46 +00008181}
8182#endif
8183
8184
Bram Moolenaar071d4272004-06-13 20:20:40 +00008185/*
8186 * ":cd", ":lcd", ":chdir" and ":lchdir".
8187 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00008188 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00008189ex_cd(eap)
8190 exarg_T *eap;
8191{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008192 char_u *new_dir;
8193 char_u *tofree;
8194
8195 new_dir = eap->arg;
8196#if !defined(UNIX) && !defined(VMS)
8197 /* for non-UNIX ":cd" means: print current directory */
8198 if (*new_dir == NUL)
8199 ex_pwd(NULL);
8200 else
8201#endif
8202 {
Bram Moolenaar8e8fe9b2009-03-11 14:37:32 +00008203#ifdef FEAT_AUTOCMD
8204 if (allbuf_locked())
8205 return;
8206#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00008207 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged()
8208 && !eap->forceit)
8209 {
Bram Moolenaar81870892007-11-11 18:17:28 +00008210 EMSG(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00008211 return;
8212 }
8213
Bram Moolenaar071d4272004-06-13 20:20:40 +00008214 /* ":cd -": Change to previous directory */
8215 if (STRCMP(new_dir, "-") == 0)
8216 {
8217 if (prev_dir == NULL)
8218 {
8219 EMSG(_("E186: No previous directory"));
8220 return;
8221 }
8222 new_dir = prev_dir;
8223 }
8224
8225 /* Save current directory for next ":cd -" */
8226 tofree = prev_dir;
8227 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8228 prev_dir = vim_strsave(NameBuff);
8229 else
8230 prev_dir = NULL;
8231
8232#if defined(UNIX) || defined(VMS)
8233 /* for UNIX ":cd" means: go to home directory */
8234 if (*new_dir == NUL)
8235 {
8236 /* use NameBuff for home directory name */
8237# ifdef VMS
8238 char_u *p;
8239
8240 p = mch_getenv((char_u *)"SYS$LOGIN");
8241 if (p == NULL || *p == NUL) /* empty is the same as not set */
8242 NameBuff[0] = NUL;
8243 else
Bram Moolenaarb6356332005-07-18 21:40:44 +00008244 vim_strncpy(NameBuff, p, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008245# else
8246 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
8247# endif
8248 new_dir = NameBuff;
8249 }
8250#endif
8251 if (new_dir == NULL || vim_chdir(new_dir))
8252 EMSG(_(e_failed));
8253 else
8254 {
8255 vim_free(curwin->w_localdir);
8256 if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir)
8257 {
8258 /* If still in global directory, need to remember current
8259 * directory as global directory. */
8260 if (globaldir == NULL && prev_dir != NULL)
8261 globaldir = vim_strsave(prev_dir);
8262 /* Remember this local directory for the window. */
8263 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8264 curwin->w_localdir = vim_strsave(NameBuff);
8265 }
8266 else
8267 {
8268 /* We are now in the global directory, no need to remember its
8269 * name. */
8270 vim_free(globaldir);
8271 globaldir = NULL;
8272 curwin->w_localdir = NULL;
8273 }
8274
8275 shorten_fnames(TRUE);
8276
8277 /* Echo the new current directory if the command was typed. */
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00008278 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008279 ex_pwd(eap);
8280 }
8281 vim_free(tofree);
8282 }
8283}
8284
8285/*
8286 * ":pwd".
8287 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008288 static void
8289ex_pwd(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008290 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008291{
8292 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8293 {
8294#ifdef BACKSLASH_IN_FILENAME
8295 slash_adjust(NameBuff);
8296#endif
8297 msg(NameBuff);
8298 }
8299 else
8300 EMSG(_("E187: Unknown"));
8301}
8302
8303/*
8304 * ":=".
8305 */
8306 static void
8307ex_equal(eap)
8308 exarg_T *eap;
8309{
8310 smsg((char_u *)"%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008311 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008312}
8313
8314 static void
8315ex_sleep(eap)
8316 exarg_T *eap;
8317{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008318 int n;
8319 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008320
8321 if (cursor_valid())
8322 {
8323 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
8324 if (n >= 0)
8325 windgoto((int)n, curwin->w_wcol);
8326 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008327
8328 len = eap->line2;
8329 switch (*eap->arg)
8330 {
8331 case 'm': break;
8332 case NUL: len *= 1000L; break;
8333 default: EMSG2(_(e_invarg2), eap->arg); return;
8334 }
8335 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008336}
8337
8338/*
8339 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
8340 */
8341 void
8342do_sleep(msec)
8343 long msec;
8344{
8345 long done;
8346
8347 cursor_on();
8348 out_flush();
8349 for (done = 0; !got_int && done < msec; done += 1000L)
8350 {
8351 ui_delay(msec - done > 1000L ? 1000L : msec - done, TRUE);
8352 ui_breakcheck();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02008353#ifdef FEAT_NETBEANS_INTG
8354 /* Process the netbeans messages that may have been received in the
8355 * call to ui_breakcheck() when the GUI is in use. This may occur when
8356 * running a test case. */
8357 netbeans_parse_messages();
8358#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008359 }
8360}
8361
8362 static void
8363do_exmap(eap, isabbrev)
8364 exarg_T *eap;
8365 int isabbrev;
8366{
8367 int mode;
8368 char_u *cmdp;
8369
8370 cmdp = eap->cmd;
8371 mode = get_map_mode(&cmdp, eap->forceit || isabbrev);
8372
8373 switch (do_map((*cmdp == 'n') ? 2 : (*cmdp == 'u'),
8374 eap->arg, mode, isabbrev))
8375 {
8376 case 1: EMSG(_(e_invarg));
8377 break;
8378 case 2: EMSG(isabbrev ? _(e_noabbr) : _(e_nomap));
8379 break;
8380 }
8381}
8382
8383/*
8384 * ":winsize" command (obsolete).
8385 */
8386 static void
8387ex_winsize(eap)
8388 exarg_T *eap;
8389{
8390 int w, h;
8391 char_u *arg = eap->arg;
8392 char_u *p;
8393
8394 w = getdigits(&arg);
8395 arg = skipwhite(arg);
8396 p = arg;
8397 h = getdigits(&arg);
8398 if (*p != NUL && *arg == NUL)
8399 set_shellsize(w, h, TRUE);
8400 else
8401 EMSG(_("E465: :winsize requires two number arguments"));
8402}
8403
8404#ifdef FEAT_WINDOWS
8405 static void
8406ex_wincmd(eap)
8407 exarg_T *eap;
8408{
8409 int xchar = NUL;
8410 char_u *p;
8411
8412 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
8413 {
8414 /* CTRL-W g and CTRL-W CTRL-G have an extra command character */
8415 if (eap->arg[1] == NUL)
8416 {
8417 EMSG(_(e_invarg));
8418 return;
8419 }
8420 xchar = eap->arg[1];
8421 p = eap->arg + 2;
8422 }
8423 else
8424 p = eap->arg + 1;
8425
8426 eap->nextcmd = check_nextcmd(p);
8427 p = skipwhite(p);
8428 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
8429 EMSG(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02008430 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008431 {
8432 /* Pass flags on for ":vertical wincmd ]". */
8433 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008434 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008435 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
8436 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008437 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008438 }
8439}
8440#endif
8441
Bram Moolenaar843ee412004-06-30 16:16:41 +00008442#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008443/*
8444 * ":winpos".
8445 */
8446 static void
8447ex_winpos(eap)
8448 exarg_T *eap;
8449{
8450 int x, y;
8451 char_u *arg = eap->arg;
8452 char_u *p;
8453
8454 if (*arg == NUL)
8455 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00008456# if defined(FEAT_GUI) || defined(MSWIN)
8457# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008458 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00008459# else
8460 if (mch_get_winpos(&x, &y) != FAIL)
8461# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008462 {
8463 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
8464 msg(IObuff);
8465 }
8466 else
8467# endif
8468 EMSG(_("E188: Obtaining window position not implemented for this platform"));
8469 }
8470 else
8471 {
8472 x = getdigits(&arg);
8473 arg = skipwhite(arg);
8474 p = arg;
8475 y = getdigits(&arg);
8476 if (*p == NUL || *arg != NUL)
8477 {
8478 EMSG(_("E466: :winpos requires two number arguments"));
8479 return;
8480 }
8481# ifdef FEAT_GUI
8482 if (gui.in_use)
8483 gui_mch_set_winpos(x, y);
8484 else if (gui.starting)
8485 {
8486 /* Remember the coordinates for when the window is opened. */
8487 gui_win_x = x;
8488 gui_win_y = y;
8489 }
8490# ifdef HAVE_TGETENT
8491 else
8492# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00008493# else
8494# ifdef MSWIN
8495 mch_set_winpos(x, y);
8496# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008497# endif
8498# ifdef HAVE_TGETENT
8499 if (*T_CWP)
8500 term_set_winpos(x, y);
8501# endif
8502 }
8503}
8504#endif
8505
8506/*
8507 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
8508 */
8509 static void
8510ex_operators(eap)
8511 exarg_T *eap;
8512{
8513 oparg_T oa;
8514
8515 clear_oparg(&oa);
8516 oa.regname = eap->regname;
8517 oa.start.lnum = eap->line1;
8518 oa.end.lnum = eap->line2;
8519 oa.line_count = eap->line2 - eap->line1 + 1;
8520 oa.motion_type = MLINE;
8521#ifdef FEAT_VIRTUALEDIT
8522 virtual_op = FALSE;
8523#endif
8524 if (eap->cmdidx != CMD_yank) /* position cursor for undo */
8525 {
8526 setpcmark();
8527 curwin->w_cursor.lnum = eap->line1;
8528 beginline(BL_SOL | BL_FIX);
8529 }
8530
8531 switch (eap->cmdidx)
8532 {
8533 case CMD_delete:
8534 oa.op_type = OP_DELETE;
8535 op_delete(&oa);
8536 break;
8537
8538 case CMD_yank:
8539 oa.op_type = OP_YANK;
8540 (void)op_yank(&oa, FALSE, TRUE);
8541 break;
8542
8543 default: /* CMD_rshift or CMD_lshift */
8544 if ((eap->cmdidx == CMD_rshift)
8545#ifdef FEAT_RIGHTLEFT
8546 ^ curwin->w_p_rl
8547#endif
8548 )
8549 oa.op_type = OP_RSHIFT;
8550 else
8551 oa.op_type = OP_LSHIFT;
8552 op_shift(&oa, FALSE, eap->amount);
8553 break;
8554 }
8555#ifdef FEAT_VIRTUALEDIT
8556 virtual_op = MAYBE;
8557#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00008558 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008559}
8560
8561/*
8562 * ":put".
8563 */
8564 static void
8565ex_put(eap)
8566 exarg_T *eap;
8567{
8568 /* ":0put" works like ":1put!". */
8569 if (eap->line2 == 0)
8570 {
8571 eap->line2 = 1;
8572 eap->forceit = TRUE;
8573 }
8574 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008575 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
8576 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008577}
8578
8579/*
8580 * Handle ":copy" and ":move".
8581 */
8582 static void
8583ex_copymove(eap)
8584 exarg_T *eap;
8585{
8586 long n;
8587
8588 n = get_address(&eap->arg, FALSE, FALSE);
8589 if (eap->arg == NULL) /* error detected */
8590 {
8591 eap->nextcmd = NULL;
8592 return;
8593 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008594 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008595
8596 /*
8597 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
8598 */
8599 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
8600 {
8601 EMSG(_(e_invaddr));
8602 return;
8603 }
8604
8605 if (eap->cmdidx == CMD_move)
8606 {
8607 if (do_move(eap->line1, eap->line2, n) == FAIL)
8608 return;
8609 }
8610 else
8611 ex_copy(eap->line1, eap->line2, n);
8612 u_clearline();
8613 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008614 ex_may_print(eap);
8615}
8616
8617/*
8618 * Print the current line if flags were given to the Ex command.
8619 */
8620 static void
8621ex_may_print(eap)
8622 exarg_T *eap;
8623{
8624 if (eap->flags != 0)
8625 {
8626 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
8627 (eap->flags & EXFLAG_LIST));
8628 ex_no_reprint = TRUE;
8629 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008630}
8631
8632/*
8633 * ":smagic" and ":snomagic".
8634 */
8635 static void
8636ex_submagic(eap)
8637 exarg_T *eap;
8638{
8639 int magic_save = p_magic;
8640
8641 p_magic = (eap->cmdidx == CMD_smagic);
8642 do_sub(eap);
8643 p_magic = magic_save;
8644}
8645
8646/*
8647 * ":join".
8648 */
8649 static void
8650ex_join(eap)
8651 exarg_T *eap;
8652{
8653 curwin->w_cursor.lnum = eap->line1;
8654 if (eap->line1 == eap->line2)
8655 {
8656 if (eap->addr_count >= 2) /* :2,2join does nothing */
8657 return;
8658 if (eap->line2 == curbuf->b_ml.ml_line_count)
8659 {
8660 beep_flush();
8661 return;
8662 }
8663 ++eap->line2;
8664 }
Bram Moolenaar81340392012-06-06 16:12:59 +02008665 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008666 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008667 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008668}
8669
8670/*
8671 * ":[addr]@r" or ":[addr]*r": execute register
8672 */
8673 static void
8674ex_at(eap)
8675 exarg_T *eap;
8676{
8677 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00008678 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008679
8680 curwin->w_cursor.lnum = eap->line2;
8681
8682#ifdef USE_ON_FLY_SCROLL
8683 dont_scroll = TRUE; /* disallow scrolling here */
8684#endif
8685
8686 /* get the register name. No name means to use the previous one */
8687 c = *eap->arg;
8688 if (c == NUL || (c == '*' && *eap->cmd == '*'))
8689 c = '@';
Bram Moolenaard333d1e2006-11-07 17:43:47 +00008690 /* Put the register in the typeahead buffer with the "silent" flag. */
8691 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
8692 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008693 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008694 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00008695 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008696 else
8697 {
8698 int save_efr = exec_from_reg;
8699
8700 exec_from_reg = TRUE;
8701
8702 /*
8703 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00008704 * Continue until the stuff buffer is empty and all added characters
8705 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008706 */
Bram Moolenaar60462872009-11-03 11:40:19 +00008707 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008708 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
8709
8710 exec_from_reg = save_efr;
8711 }
8712}
8713
8714/*
8715 * ":!".
8716 */
8717 static void
8718ex_bang(eap)
8719 exarg_T *eap;
8720{
8721 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
8722}
8723
8724/*
8725 * ":undo".
8726 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008727 static void
8728ex_undo(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008729 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008730{
Bram Moolenaard3667a22006-03-16 21:35:52 +00008731 if (eap->addr_count == 1) /* :undo 123 */
Bram Moolenaar730cde92010-06-27 05:18:54 +02008732 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00008733 else
8734 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008735}
8736
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008737#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008738 static void
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008739ex_wundo(eap)
8740 exarg_T *eap;
8741{
8742 char_u hash[UNDO_HASH_SIZE];
8743
8744 u_compute_hash(hash);
8745 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
8746}
8747
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008748 static void
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008749ex_rundo(eap)
8750 exarg_T *eap;
8751{
8752 char_u hash[UNDO_HASH_SIZE];
8753
8754 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02008755 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008756}
8757#endif
8758
Bram Moolenaar071d4272004-06-13 20:20:40 +00008759/*
8760 * ":redo".
8761 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008762 static void
8763ex_redo(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008764 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008765{
8766 u_redo(1);
8767}
8768
8769/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008770 * ":earlier" and ":later".
8771 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008772 static void
8773ex_later(eap)
8774 exarg_T *eap;
8775{
8776 long count = 0;
8777 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008778 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008779 char_u *p = eap->arg;
8780
8781 if (*p == NUL)
8782 count = 1;
8783 else if (isdigit(*p))
8784 {
8785 count = getdigits(&p);
8786 switch (*p)
8787 {
8788 case 's': ++p; sec = TRUE; break;
8789 case 'm': ++p; sec = TRUE; count *= 60; break;
8790 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008791 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
8792 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008793 }
8794 }
8795
8796 if (*p != NUL)
8797 EMSG2(_(e_invarg2), eap->arg);
8798 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02008799 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
8800 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008801}
8802
8803/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008804 * ":redir": start/stop redirection.
8805 */
8806 static void
8807ex_redir(eap)
8808 exarg_T *eap;
8809{
8810 char *mode;
8811 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00008812 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008813
8814 if (STRICMP(eap->arg, "END") == 0)
8815 close_redir();
8816 else
8817 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008818 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008819 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008820 ++arg;
8821 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008822 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008823 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008824 mode = "a";
8825 }
8826 else
8827 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00008828 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008829
8830 close_redir();
8831
8832 /* Expand environment variables and "~/". */
Bram Moolenaarca472992005-01-21 11:46:23 +00008833 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008834 if (fname == NULL)
8835 return;
8836#ifdef FEAT_BROWSE
8837 if (cmdmod.browse)
8838 {
8839 char_u *browseFile;
8840
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008841 browseFile = do_browse(BROWSE_SAVE,
8842 (char_u *)_("Save Redirection"),
8843 fname, NULL, NULL, BROWSE_FILTER_ALL_FILES, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008844 if (browseFile == NULL)
8845 return; /* operation cancelled */
8846 vim_free(fname);
8847 fname = browseFile;
8848 eap->forceit = TRUE; /* since dialog already asked */
8849 }
8850#endif
8851
8852 redir_fd = open_exfile(fname, eap->forceit, mode);
8853 vim_free(fname);
8854 }
8855#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00008856 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008857 {
8858 /* redirect to a register a-z (resp. A-Z for appending) */
8859 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00008860 ++arg;
8861 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008862# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00008863 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00008864 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008865# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00008866 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008867 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008868 redir_reg = *arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008869 if (*arg == '>' && arg[1] == '>') /* append */
Bram Moolenaard9d30582005-05-18 22:10:28 +00008870 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008871 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008872 {
Bram Moolenaarc188b882007-10-19 14:20:54 +00008873 /* Can use both "@a" and "@a>". */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00008874 if (*arg == '>')
8875 arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008876 /* Make register empty when not using @A-@Z and the
8877 * command is valid. */
8878 if (*arg == NUL && !isupper(redir_reg))
8879 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008880 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008881 }
8882 if (*arg != NUL)
8883 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00008884 redir_reg = 0;
Bram Moolenaard9d30582005-05-18 22:10:28 +00008885 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008886 }
8887 }
8888 else if (*arg == '=' && arg[1] == '>')
8889 {
8890 int append;
8891
8892 /* redirect to a variable */
8893 close_redir();
8894 arg += 2;
8895
8896 if (*arg == '>')
8897 {
8898 ++arg;
8899 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008900 }
8901 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008902 append = FALSE;
8903
8904 if (var_redir_start(skipwhite(arg), append) == OK)
8905 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008906 }
8907#endif
8908
8909 /* TODO: redirect to a buffer */
8910
Bram Moolenaar071d4272004-06-13 20:20:40 +00008911 else
Bram Moolenaarca472992005-01-21 11:46:23 +00008912 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008913 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00008914
8915 /* Make sure redirection is not off. Can happen for cmdline completion
8916 * that indirectly invokes a command to catch its output. */
8917 if (redir_fd != NULL
8918#ifdef FEAT_EVAL
8919 || redir_reg || redir_vname
8920#endif
8921 )
8922 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008923}
8924
8925/*
8926 * ":redraw": force redraw
8927 */
8928 static void
8929ex_redraw(eap)
8930 exarg_T *eap;
8931{
8932 int r = RedrawingDisabled;
8933 int p = p_lz;
8934
8935 RedrawingDisabled = 0;
8936 p_lz = FALSE;
8937 update_topline();
8938 update_screen(eap->forceit ? CLEAR :
8939#ifdef FEAT_VISUAL
8940 VIsual_active ? INVERTED :
8941#endif
8942 0);
8943#ifdef FEAT_TITLE
8944 if (need_maketitle)
8945 maketitle();
8946#endif
8947 RedrawingDisabled = r;
8948 p_lz = p;
8949
8950 /* Reset msg_didout, so that a message that's there is overwritten. */
8951 msg_didout = FALSE;
8952 msg_col = 0;
8953
8954 out_flush();
8955}
8956
8957/*
8958 * ":redrawstatus": force redraw of status line(s)
8959 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008960 static void
8961ex_redrawstatus(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008962 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008963{
8964#if defined(FEAT_WINDOWS)
8965 int r = RedrawingDisabled;
8966 int p = p_lz;
8967
8968 RedrawingDisabled = 0;
8969 p_lz = FALSE;
8970 if (eap->forceit)
8971 status_redraw_all();
8972 else
8973 status_redraw_curbuf();
8974 update_screen(
8975# ifdef FEAT_VISUAL
8976 VIsual_active ? INVERTED :
8977# endif
8978 0);
8979 RedrawingDisabled = r;
8980 p_lz = p;
8981 out_flush();
8982#endif
8983}
8984
8985 static void
8986close_redir()
8987{
8988 if (redir_fd != NULL)
8989 {
8990 fclose(redir_fd);
8991 redir_fd = NULL;
8992 }
8993#ifdef FEAT_EVAL
8994 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008995 if (redir_vname)
8996 {
8997 var_redir_stop();
8998 redir_vname = 0;
8999 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009000#endif
9001}
9002
9003#if defined(FEAT_SESSION) && defined(USE_CRNL)
9004# define MKSESSION_NL
9005static int mksession_nl = FALSE; /* use NL only in put_eol() */
9006#endif
9007
9008/*
9009 * ":mkexrc", ":mkvimrc", ":mkview" and ":mksession".
9010 */
9011 static void
9012ex_mkrc(eap)
9013 exarg_T *eap;
9014{
9015 FILE *fd;
9016 int failed = FALSE;
9017 char_u *fname;
9018#ifdef FEAT_BROWSE
9019 char_u *browseFile = NULL;
9020#endif
9021#ifdef FEAT_SESSION
9022 int view_session = FALSE;
9023 int using_vdir = FALSE; /* using 'viewdir'? */
9024 char_u *viewFile = NULL;
9025 unsigned *flagp;
9026#endif
9027
9028 if (eap->cmdidx == CMD_mksession || eap->cmdidx == CMD_mkview)
9029 {
9030#ifdef FEAT_SESSION
9031 view_session = TRUE;
9032#else
9033 ex_ni(eap);
9034 return;
9035#endif
9036 }
9037
9038#ifdef FEAT_SESSION
Bram Moolenaar8d594672009-07-01 18:18:57 +00009039 /* Use the short file name until ":lcd" is used. We also don't use the
9040 * short file name when 'acd' is set, that is checked later. */
Bram Moolenaareeefcc72007-05-01 21:21:21 +00009041 did_lcd = FALSE;
9042
Bram Moolenaar071d4272004-06-13 20:20:40 +00009043 /* ":mkview" or ":mkview 9": generate file name with 'viewdir' */
9044 if (eap->cmdidx == CMD_mkview
9045 && (*eap->arg == NUL
9046 || (vim_isdigit(*eap->arg) && eap->arg[1] == NUL)))
9047 {
9048 eap->forceit = TRUE;
9049 fname = get_view_file(*eap->arg);
9050 if (fname == NULL)
9051 return;
9052 viewFile = fname;
9053 using_vdir = TRUE;
9054 }
9055 else
9056#endif
9057 if (*eap->arg != NUL)
9058 fname = eap->arg;
9059 else if (eap->cmdidx == CMD_mkvimrc)
9060 fname = (char_u *)VIMRC_FILE;
9061#ifdef FEAT_SESSION
9062 else if (eap->cmdidx == CMD_mksession)
9063 fname = (char_u *)SESSION_FILE;
9064#endif
9065 else
9066 fname = (char_u *)EXRC_FILE;
9067
9068#ifdef FEAT_BROWSE
9069 if (cmdmod.browse)
9070 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00009071 browseFile = do_browse(BROWSE_SAVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009072# ifdef FEAT_SESSION
9073 eap->cmdidx == CMD_mkview ? (char_u *)_("Save View") :
9074 eap->cmdidx == CMD_mksession ? (char_u *)_("Save Session") :
9075# endif
9076 (char_u *)_("Save Setup"),
9077 fname, (char_u *)"vim", NULL, BROWSE_FILTER_MACROS, NULL);
9078 if (browseFile == NULL)
9079 goto theend;
9080 fname = browseFile;
9081 eap->forceit = TRUE; /* since dialog already asked */
9082 }
9083#endif
9084
9085#if defined(FEAT_SESSION) && defined(vim_mkdir)
9086 /* When using 'viewdir' may have to create the directory. */
9087 if (using_vdir && !mch_isdir(p_vdir))
Bram Moolenaardf177f62005-02-22 08:39:57 +00009088 vim_mkdir_emsg(p_vdir, 0755);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009089#endif
9090
9091 fd = open_exfile(fname, eap->forceit, WRITEBIN);
9092 if (fd != NULL)
9093 {
9094#ifdef FEAT_SESSION
9095 if (eap->cmdidx == CMD_mkview)
9096 flagp = &vop_flags;
9097 else
9098 flagp = &ssop_flags;
9099#endif
9100
9101#ifdef MKSESSION_NL
9102 /* "unix" in 'sessionoptions': use NL line separator */
9103 if (view_session && (*flagp & SSOP_UNIX))
9104 mksession_nl = TRUE;
9105#endif
9106
9107 /* Write the version command for :mkvimrc */
9108 if (eap->cmdidx == CMD_mkvimrc)
9109 (void)put_line(fd, "version 6.0");
9110
9111#ifdef FEAT_SESSION
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009112 if (eap->cmdidx == CMD_mksession)
9113 {
9114 if (put_line(fd, "let SessionLoad = 1") == FAIL)
9115 failed = TRUE;
9116 }
9117
Bram Moolenaar071d4272004-06-13 20:20:40 +00009118 if (eap->cmdidx != CMD_mkview)
9119#endif
9120 {
9121 /* Write setting 'compatible' first, because it has side effects.
9122 * For that same reason only do it when needed. */
9123 if (p_cp)
9124 (void)put_line(fd, "if !&cp | set cp | endif");
9125 else
9126 (void)put_line(fd, "if &cp | set nocp | endif");
9127 }
9128
9129#ifdef FEAT_SESSION
9130 if (!view_session
9131 || (eap->cmdidx == CMD_mksession
9132 && (*flagp & SSOP_OPTIONS)))
9133#endif
9134 failed |= (makemap(fd, NULL) == FAIL
9135 || makeset(fd, OPT_GLOBAL, FALSE) == FAIL);
9136
9137#ifdef FEAT_SESSION
9138 if (!failed && view_session)
9139 {
9140 if (put_line(fd, "let s:so_save = &so | let s:siso_save = &siso | set so=0 siso=0") == FAIL)
9141 failed = TRUE;
9142 if (eap->cmdidx == CMD_mksession)
9143 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02009144 char_u *dirnow; /* current directory */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009145
Bram Moolenaard9462e32011-04-11 21:35:11 +02009146 dirnow = alloc(MAXPATHL);
9147 if (dirnow == NULL)
9148 failed = TRUE;
9149 else
9150 {
9151 /*
9152 * Change to session file's dir.
9153 */
9154 if (mch_dirname(dirnow, MAXPATHL) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00009155 || mch_chdir((char *)dirnow) != 0)
Bram Moolenaard9462e32011-04-11 21:35:11 +02009156 *dirnow = NUL;
9157 if (*dirnow != NUL && (ssop_flags & SSOP_SESDIR))
9158 {
9159 if (vim_chdirfile(fname) == OK)
9160 shorten_fnames(TRUE);
9161 }
9162 else if (*dirnow != NUL
9163 && (ssop_flags & SSOP_CURDIR) && globaldir != NULL)
9164 {
9165 if (mch_chdir((char *)globaldir) == 0)
9166 shorten_fnames(TRUE);
9167 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009168
Bram Moolenaard9462e32011-04-11 21:35:11 +02009169 failed |= (makeopens(fd, dirnow) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009170
Bram Moolenaard9462e32011-04-11 21:35:11 +02009171 /* restore original dir */
9172 if (*dirnow != NUL && ((ssop_flags & SSOP_SESDIR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009173 || ((ssop_flags & SSOP_CURDIR) && globaldir != NULL)))
Bram Moolenaard9462e32011-04-11 21:35:11 +02009174 {
9175 if (mch_chdir((char *)dirnow) != 0)
9176 EMSG(_(e_prev_dir));
9177 shorten_fnames(TRUE);
9178 }
9179 vim_free(dirnow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009180 }
9181 }
9182 else
9183 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +00009184 failed |= (put_view(fd, curwin, !using_vdir, flagp,
9185 -1) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009186 }
9187 if (put_line(fd, "let &so = s:so_save | let &siso = s:siso_save")
9188 == FAIL)
9189 failed = TRUE;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009190 if (put_line(fd, "doautoall SessionLoadPost") == FAIL)
9191 failed = TRUE;
Bram Moolenaar4770d092006-01-12 23:22:24 +00009192 if (eap->cmdidx == CMD_mksession)
9193 {
9194 if (put_line(fd, "unlet SessionLoad") == FAIL)
9195 failed = TRUE;
9196 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009197 }
9198#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009199 if (put_line(fd, "\" vim: set ft=vim :") == FAIL)
9200 failed = TRUE;
9201
Bram Moolenaar071d4272004-06-13 20:20:40 +00009202 failed |= fclose(fd);
9203
9204 if (failed)
9205 EMSG(_(e_write));
9206#if defined(FEAT_EVAL) && defined(FEAT_SESSION)
9207 else if (eap->cmdidx == CMD_mksession)
9208 {
9209 /* successful session write - set this_session var */
Bram Moolenaard9462e32011-04-11 21:35:11 +02009210 char_u *tbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009211
Bram Moolenaard9462e32011-04-11 21:35:11 +02009212 tbuf = alloc(MAXPATHL);
9213 if (tbuf != NULL)
9214 {
9215 if (vim_FullName(fname, tbuf, MAXPATHL, FALSE) == OK)
9216 set_vim_var_string(VV_THIS_SESSION, tbuf, -1);
9217 vim_free(tbuf);
9218 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009219 }
9220#endif
9221#ifdef MKSESSION_NL
9222 mksession_nl = FALSE;
9223#endif
9224 }
9225
9226#ifdef FEAT_BROWSE
9227theend:
9228 vim_free(browseFile);
9229#endif
9230#ifdef FEAT_SESSION
9231 vim_free(viewFile);
9232#endif
9233}
9234
Bram Moolenaardf177f62005-02-22 08:39:57 +00009235#if ((defined(FEAT_SESSION) || defined(FEAT_EVAL)) && defined(vim_mkdir)) \
9236 || defined(PROTO)
9237 int
9238vim_mkdir_emsg(name, prot)
9239 char_u *name;
Bram Moolenaar78a15312009-05-15 19:33:18 +00009240 int prot UNUSED;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009241{
9242 if (vim_mkdir(name, prot) != 0)
9243 {
9244 EMSG2(_("E739: Cannot create directory: %s"), name);
9245 return FAIL;
9246 }
9247 return OK;
9248}
9249#endif
9250
Bram Moolenaar071d4272004-06-13 20:20:40 +00009251/*
9252 * Open a file for writing for an Ex command, with some checks.
9253 * Return file descriptor, or NULL on failure.
9254 */
9255 FILE *
9256open_exfile(fname, forceit, mode)
9257 char_u *fname;
9258 int forceit;
9259 char *mode; /* "w" for create new file or "a" for append */
9260{
9261 FILE *fd;
9262
9263#ifdef UNIX
9264 /* with Unix it is possible to open a directory */
9265 if (mch_isdir(fname))
9266 {
9267 EMSG2(_(e_isadir2), fname);
9268 return NULL;
9269 }
9270#endif
9271 if (!forceit && *mode != 'a' && vim_fexists(fname))
9272 {
9273 EMSG2(_("E189: \"%s\" exists (add ! to override)"), fname);
9274 return NULL;
9275 }
9276
9277 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
9278 EMSG2(_("E190: Cannot open \"%s\" for writing"), fname);
9279
9280 return fd;
9281}
9282
9283/*
9284 * ":mark" and ":k".
9285 */
9286 static void
9287ex_mark(eap)
9288 exarg_T *eap;
9289{
9290 pos_T pos;
9291
9292 if (*eap->arg == NUL) /* No argument? */
9293 EMSG(_(e_argreq));
9294 else if (eap->arg[1] != NUL) /* more than one character? */
9295 EMSG(_(e_trailing));
9296 else
9297 {
9298 pos = curwin->w_cursor; /* save curwin->w_cursor */
9299 curwin->w_cursor.lnum = eap->line2;
9300 beginline(BL_WHITE | BL_FIX);
9301 if (setmark(*eap->arg) == FAIL) /* set mark */
9302 EMSG(_("E191: Argument must be a letter or forward/backward quote"));
9303 curwin->w_cursor = pos; /* restore curwin->w_cursor */
9304 }
9305}
9306
9307/*
9308 * Update w_topline, w_leftcol and the cursor position.
9309 */
9310 void
9311update_topline_cursor()
9312{
9313 check_cursor(); /* put cursor on valid line */
9314 update_topline();
9315 if (!curwin->w_p_wrap)
9316 validate_cursor();
9317 update_curswant();
9318}
9319
9320#ifdef FEAT_EX_EXTRA
9321/*
9322 * ":normal[!] {commands}": Execute normal mode commands.
9323 */
9324 static void
9325ex_normal(eap)
9326 exarg_T *eap;
9327{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009328 int save_msg_scroll = msg_scroll;
9329 int save_restart_edit = restart_edit;
9330 int save_msg_didout = msg_didout;
9331 int save_State = State;
9332 tasave_T tabuf;
9333 int save_insertmode = p_im;
9334 int save_finish_op = finish_op;
Bram Moolenaar38084112008-07-26 14:05:07 +00009335 int save_opcount = opcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009336#ifdef FEAT_MBYTE
9337 char_u *arg = NULL;
9338 int l;
9339 char_u *p;
9340#endif
9341
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00009342 if (ex_normal_lock > 0)
9343 {
9344 EMSG(_(e_secure));
9345 return;
9346 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009347 if (ex_normal_busy >= p_mmd)
9348 {
9349 EMSG(_("E192: Recursive use of :normal too deep"));
9350 return;
9351 }
9352 ++ex_normal_busy;
9353
9354 msg_scroll = FALSE; /* no msg scrolling in Normal mode */
9355 restart_edit = 0; /* don't go to Insert mode */
9356 p_im = FALSE; /* don't use 'insertmode' */
9357
9358#ifdef FEAT_MBYTE
9359 /*
9360 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
9361 * this for the K_SPECIAL leading byte, otherwise special keys will not
9362 * work.
9363 */
9364 if (has_mbyte)
9365 {
9366 int len = 0;
9367
9368 /* Count the number of characters to be escaped. */
9369 for (p = eap->arg; *p != NUL; ++p)
9370 {
9371# ifdef FEAT_GUI
9372 if (*p == CSI) /* leadbyte CSI */
9373 len += 2;
9374# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009375 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009376 if (*++p == K_SPECIAL /* trailbyte K_SPECIAL or CSI */
9377# ifdef FEAT_GUI
9378 || *p == CSI
9379# endif
9380 )
9381 len += 2;
9382 }
9383 if (len > 0)
9384 {
9385 arg = alloc((unsigned)(STRLEN(eap->arg) + len + 1));
9386 if (arg != NULL)
9387 {
9388 len = 0;
9389 for (p = eap->arg; *p != NUL; ++p)
9390 {
9391 arg[len++] = *p;
9392# ifdef FEAT_GUI
9393 if (*p == CSI)
9394 {
9395 arg[len++] = KS_EXTRA;
9396 arg[len++] = (int)KE_CSI;
9397 }
9398# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009399 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009400 {
9401 arg[len++] = *++p;
9402 if (*p == K_SPECIAL)
9403 {
9404 arg[len++] = KS_SPECIAL;
9405 arg[len++] = KE_FILLER;
9406 }
9407# ifdef FEAT_GUI
9408 else if (*p == CSI)
9409 {
9410 arg[len++] = KS_EXTRA;
9411 arg[len++] = (int)KE_CSI;
9412 }
9413# endif
9414 }
9415 arg[len] = NUL;
9416 }
9417 }
9418 }
9419 }
9420#endif
9421
9422 /*
9423 * Save the current typeahead. This is required to allow using ":normal"
9424 * from an event handler and makes sure we don't hang when the argument
9425 * ends with half a command.
9426 */
9427 save_typeahead(&tabuf);
9428 if (tabuf.typebuf_valid)
9429 {
9430 /*
9431 * Repeat the :normal command for each line in the range. When no
9432 * range given, execute it just once, without positioning the cursor
9433 * first.
9434 */
9435 do
9436 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009437 if (eap->addr_count != 0)
9438 {
9439 curwin->w_cursor.lnum = eap->line1++;
9440 curwin->w_cursor.col = 0;
9441 }
9442
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009443 exec_normal_cmd(
Bram Moolenaar071d4272004-06-13 20:20:40 +00009444#ifdef FEAT_MBYTE
9445 arg != NULL ? arg :
9446#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009447 eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009448 }
9449 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
9450 }
9451
9452 /* Might not return to the main loop when in an event handler. */
9453 update_topline_cursor();
9454
9455 /* Restore the previous typeahead. */
9456 restore_typeahead(&tabuf);
9457
9458 --ex_normal_busy;
9459 msg_scroll = save_msg_scroll;
9460 restart_edit = save_restart_edit;
9461 p_im = save_insertmode;
9462 finish_op = save_finish_op;
Bram Moolenaar38084112008-07-26 14:05:07 +00009463 opcount = save_opcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009464 msg_didout |= save_msg_didout; /* don't reset msg_didout now */
9465
9466 /* Restore the state (needed when called from a function executed for
9467 * 'indentexpr'). */
9468 State = save_State;
9469#ifdef FEAT_MBYTE
9470 vim_free(arg);
9471#endif
9472}
9473
9474/*
Bram Moolenaar64969662005-12-14 21:59:55 +00009475 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009476 */
9477 static void
9478ex_startinsert(eap)
9479 exarg_T *eap;
9480{
Bram Moolenaarfd371682005-01-14 21:42:54 +00009481 if (eap->forceit)
9482 {
9483 coladvance((colnr_T)MAXCOL);
9484 curwin->w_curswant = MAXCOL;
9485 curwin->w_set_curswant = FALSE;
9486 }
9487
Bram Moolenaara40c5002005-01-09 21:16:21 +00009488 /* Ignore the command when already in Insert mode. Inserting an
9489 * expression register that invokes a function can do this. */
9490 if (State & INSERT)
9491 return;
9492
Bram Moolenaar64969662005-12-14 21:59:55 +00009493 if (eap->cmdidx == CMD_startinsert)
9494 restart_edit = 'a';
9495 else if (eap->cmdidx == CMD_startreplace)
9496 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00009497 else
Bram Moolenaar64969662005-12-14 21:59:55 +00009498 restart_edit = 'V';
9499
9500 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009501 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009502 if (eap->cmdidx == CMD_startinsert)
9503 restart_edit = 'i';
Bram Moolenaar071d4272004-06-13 20:20:40 +00009504 curwin->w_curswant = 0; /* avoid MAXCOL */
9505 }
9506}
9507
9508/*
9509 * ":stopinsert"
9510 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009511 static void
9512ex_stopinsert(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009513 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009514{
9515 restart_edit = 0;
9516 stop_insert_mode = TRUE;
9517}
9518#endif
9519
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009520#if defined(FEAT_EX_EXTRA) || defined(FEAT_MENU) || defined(PROTO)
9521/*
9522 * Execute normal mode command "cmd".
9523 * "remap" can be REMAP_NONE or REMAP_YES.
9524 */
9525 void
9526exec_normal_cmd(cmd, remap, silent)
9527 char_u *cmd;
9528 int remap;
9529 int silent;
9530{
9531 oparg_T oa;
9532
9533 /*
9534 * Stuff the argument into the typeahead buffer.
9535 * Execute normal_cmd() until there is no typeahead left.
9536 */
9537 clear_oparg(&oa);
9538 finish_op = FALSE;
9539 ins_typebuf(cmd, remap, 0, TRUE, silent);
9540 while ((!stuff_empty() || (!typebuf_typed() && typebuf.tb_len > 0))
9541 && !got_int)
9542 {
9543 update_topline_cursor();
Bram Moolenaar48ac02c2011-01-17 19:50:06 +01009544 normal_cmd(&oa, TRUE); /* execute a Normal mode cmd */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009545 }
9546}
9547#endif
9548
Bram Moolenaar071d4272004-06-13 20:20:40 +00009549#ifdef FEAT_FIND_ID
9550 static void
9551ex_checkpath(eap)
9552 exarg_T *eap;
9553{
9554 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
9555 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
9556 (linenr_T)1, (linenr_T)MAXLNUM);
9557}
9558
9559#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
9560/*
9561 * ":psearch"
9562 */
9563 static void
9564ex_psearch(eap)
9565 exarg_T *eap;
9566{
9567 g_do_tagpreview = p_pvh;
9568 ex_findpat(eap);
9569 g_do_tagpreview = 0;
9570}
9571#endif
9572
9573 static void
9574ex_findpat(eap)
9575 exarg_T *eap;
9576{
9577 int whole = TRUE;
9578 long n;
9579 char_u *p;
9580 int action;
9581
9582 switch (cmdnames[eap->cmdidx].cmd_name[2])
9583 {
9584 case 'e': /* ":psearch", ":isearch" and ":dsearch" */
9585 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
9586 action = ACTION_GOTO;
9587 else
9588 action = ACTION_SHOW;
9589 break;
9590 case 'i': /* ":ilist" and ":dlist" */
9591 action = ACTION_SHOW_ALL;
9592 break;
9593 case 'u': /* ":ijump" and ":djump" */
9594 action = ACTION_GOTO;
9595 break;
9596 default: /* ":isplit" and ":dsplit" */
9597 action = ACTION_SPLIT;
9598 break;
9599 }
9600
9601 n = 1;
9602 if (vim_isdigit(*eap->arg)) /* get count */
9603 {
9604 n = getdigits(&eap->arg);
9605 eap->arg = skipwhite(eap->arg);
9606 }
9607 if (*eap->arg == '/') /* Match regexp, not just whole words */
9608 {
9609 whole = FALSE;
9610 ++eap->arg;
9611 p = skip_regexp(eap->arg, '/', p_magic, NULL);
9612 if (*p)
9613 {
9614 *p++ = NUL;
9615 p = skipwhite(p);
9616
9617 /* Check for trailing illegal characters */
9618 if (!ends_excmd(*p))
9619 eap->errmsg = e_trailing;
9620 else
9621 eap->nextcmd = check_nextcmd(p);
9622 }
9623 }
9624 if (!eap->skip)
9625 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
9626 whole, !eap->forceit,
9627 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
9628 n, action, eap->line1, eap->line2);
9629}
9630#endif
9631
9632#ifdef FEAT_WINDOWS
9633
9634# ifdef FEAT_QUICKFIX
9635/*
9636 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
9637 */
9638 static void
9639ex_ptag(eap)
9640 exarg_T *eap;
9641{
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01009642 g_do_tagpreview = p_pvh; /* will be reset to 0 in ex_tag_cmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009643 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9644}
9645
9646/*
9647 * ":pedit"
9648 */
9649 static void
9650ex_pedit(eap)
9651 exarg_T *eap;
9652{
9653 win_T *curwin_save = curwin;
9654
9655 g_do_tagpreview = p_pvh;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00009656 prepare_tagpreview(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009657 keep_help_flag = curwin_save->w_buffer->b_help;
9658 do_exedit(eap, NULL);
9659 keep_help_flag = FALSE;
9660 if (curwin != curwin_save && win_valid(curwin_save))
9661 {
9662 /* Return cursor to where we were */
9663 validate_cursor();
9664 redraw_later(VALID);
9665 win_enter(curwin_save, TRUE);
9666 }
9667 g_do_tagpreview = 0;
9668}
9669# endif
9670
9671/*
9672 * ":stag", ":stselect" and ":stjump".
9673 */
9674 static void
9675ex_stag(eap)
9676 exarg_T *eap;
9677{
9678 postponed_split = -1;
9679 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009680 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009681 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9682 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009683 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009684}
9685#endif
9686
9687/*
9688 * ":tag", ":tselect", ":tjump", ":tnext", etc.
9689 */
9690 static void
9691ex_tag(eap)
9692 exarg_T *eap;
9693{
9694 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
9695}
9696
9697 static void
9698ex_tag_cmd(eap, name)
9699 exarg_T *eap;
9700 char_u *name;
9701{
9702 int cmd;
9703
9704 switch (name[1])
9705 {
9706 case 'j': cmd = DT_JUMP; /* ":tjump" */
9707 break;
9708 case 's': cmd = DT_SELECT; /* ":tselect" */
9709 break;
9710 case 'p': cmd = DT_PREV; /* ":tprevious" */
9711 break;
9712 case 'N': cmd = DT_PREV; /* ":tNext" */
9713 break;
9714 case 'n': cmd = DT_NEXT; /* ":tnext" */
9715 break;
9716 case 'o': cmd = DT_POP; /* ":pop" */
9717 break;
9718 case 'f': /* ":tfirst" */
9719 case 'r': cmd = DT_FIRST; /* ":trewind" */
9720 break;
9721 case 'l': cmd = DT_LAST; /* ":tlast" */
9722 break;
9723 default: /* ":tag" */
9724#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +00009725 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009726 {
9727 do_cstag(eap);
9728 return;
9729 }
9730#endif
9731 cmd = DT_TAG;
9732 break;
9733 }
9734
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00009735 if (name[0] == 'l')
9736 {
9737#ifndef FEAT_QUICKFIX
9738 ex_ni(eap);
9739 return;
9740#else
9741 cmd = DT_LTAG;
9742#endif
9743 }
9744
Bram Moolenaar071d4272004-06-13 20:20:40 +00009745 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
9746 eap->forceit, TRUE);
9747}
9748
9749/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009750 * Check "str" for starting with a special cmdline variable.
9751 * If found return one of the SPEC_ values and set "*usedlen" to the length of
9752 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
9753 */
9754 int
9755find_cmdline_var(src, usedlen)
9756 char_u *src;
9757 int *usedlen;
9758{
9759 int len;
9760 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +00009761 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009762 "%",
9763#define SPEC_PERC 0
9764 "#",
9765#define SPEC_HASH 1
9766 "<cword>", /* cursor word */
9767#define SPEC_CWORD 2
9768 "<cWORD>", /* cursor WORD */
9769#define SPEC_CCWORD 3
9770 "<cfile>", /* cursor path name */
9771#define SPEC_CFILE 4
9772 "<sfile>", /* ":so" file name */
9773#define SPEC_SFILE 5
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009774 "<slnum>", /* ":so" file line number */
9775#define SPEC_SLNUM 6
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009776#ifdef FEAT_AUTOCMD
9777 "<afile>", /* autocommand file name */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009778# define SPEC_AFILE 7
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009779 "<abuf>", /* autocommand buffer number */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009780# define SPEC_ABUF 8
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009781 "<amatch>", /* autocommand match name */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009782# define SPEC_AMATCH 9
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009783#endif
9784#ifdef FEAT_CLIENTSERVER
9785 "<client>"
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009786# ifdef FEAT_AUTOCMD
9787# define SPEC_CLIENT 10
9788# else
9789# define SPEC_CLIENT 7
9790# endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009791#endif
9792 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009793
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009794 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009795 {
9796 len = (int)STRLEN(spec_str[i]);
9797 if (STRNCMP(src, spec_str[i], len) == 0)
9798 {
9799 *usedlen = len;
9800 return i;
9801 }
9802 }
9803 return -1;
9804}
9805
9806/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009807 * Evaluate cmdline variables.
9808 *
9809 * change '%' to curbuf->b_ffname
9810 * '#' to curwin->w_altfile
9811 * '<cword>' to word under the cursor
9812 * '<cWORD>' to WORD under the cursor
9813 * '<cfile>' to path name under the cursor
9814 * '<sfile>' to sourced file name
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009815 * '<slnum>' to sourced file line number
Bram Moolenaar071d4272004-06-13 20:20:40 +00009816 * '<afile>' to file name for autocommand
9817 * '<abuf>' to buffer number for autocommand
9818 * '<amatch>' to matching name for autocommand
9819 *
9820 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
9821 * "" for error without a message) and NULL is returned.
9822 * Returns an allocated string if a valid match was found.
9823 * Returns NULL if no match was found. "usedlen" then still contains the
9824 * number of characters to skip.
9825 */
9826 char_u *
Bram Moolenaar63b92542007-03-27 14:57:09 +00009827eval_vars(src, srcstart, usedlen, lnump, errormsg, escaped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009828 char_u *src; /* pointer into commandline */
Bram Moolenaar63b92542007-03-27 14:57:09 +00009829 char_u *srcstart; /* beginning of valid memory for src */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009830 int *usedlen; /* characters after src that are used */
9831 linenr_T *lnump; /* line number for :e command, or NULL */
9832 char_u **errormsg; /* pointer to error message */
Bram Moolenaar63b92542007-03-27 14:57:09 +00009833 int *escaped; /* return value has escaped white space (can
9834 * be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009835{
9836 int i;
9837 char_u *s;
9838 char_u *result;
9839 char_u *resultbuf = NULL;
9840 int resultlen;
9841 buf_T *buf;
9842 int valid = VALID_HEAD + VALID_PATH; /* assume valid result */
9843 int spec_idx;
9844#ifdef FEAT_MODIFY_FNAME
9845 int skip_mod = FALSE;
9846#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009847 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009848
9849 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009850 if (escaped != NULL)
9851 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009852
9853 /*
9854 * Check if there is something to do.
9855 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009856 spec_idx = find_cmdline_var(src, usedlen);
9857 if (spec_idx < 0) /* no match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009858 {
9859 *usedlen = 1;
9860 return NULL;
9861 }
9862
9863 /*
9864 * Skip when preceded with a backslash "\%" and "\#".
9865 * Note: In "\\%" the % is also not recognized!
9866 */
9867 if (src > srcstart && src[-1] == '\\')
9868 {
9869 *usedlen = 0;
Bram Moolenaara7241f52008-06-24 20:39:31 +00009870 STRMOVE(src - 1, src); /* remove backslash */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009871 return NULL;
9872 }
9873
9874 /*
9875 * word or WORD under cursor
9876 */
9877 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD)
9878 {
9879 resultlen = find_ident_under_cursor(&result, spec_idx == SPEC_CWORD ?
9880 (FIND_IDENT|FIND_STRING) : FIND_STRING);
9881 if (resultlen == 0)
9882 {
9883 *errormsg = (char_u *)"";
9884 return NULL;
9885 }
9886 }
9887
9888 /*
9889 * '#': Alternate file name
9890 * '%': Current file name
9891 * File name under the cursor
9892 * File name for autocommand
9893 * and following modifiers
9894 */
9895 else
9896 {
9897 switch (spec_idx)
9898 {
9899 case SPEC_PERC: /* '%': current file */
9900 if (curbuf->b_fname == NULL)
9901 {
9902 result = (char_u *)"";
9903 valid = 0; /* Must have ":p:h" to be valid */
9904 }
9905 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009906 result = curbuf->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009907 break;
9908
9909 case SPEC_HASH: /* '#' or "#99": alternate file */
9910 if (src[1] == '#') /* "##": the argument list */
9911 {
9912 result = arg_all();
9913 resultbuf = result;
9914 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009915 if (escaped != NULL)
9916 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009917#ifdef FEAT_MODIFY_FNAME
9918 skip_mod = TRUE;
9919#endif
9920 break;
9921 }
9922 s = src + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00009923 if (*s == '<') /* "#<99" uses v:oldfiles */
9924 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009925 i = (int)getdigits(&s);
9926 *usedlen = (int)(s - src); /* length of what we expand */
9927
Bram Moolenaard812df62008-11-09 12:46:09 +00009928 if (src[1] == '<')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009929 {
Bram Moolenaard812df62008-11-09 12:46:09 +00009930 if (*usedlen < 2)
9931 {
9932 /* Should we give an error message for #<text? */
9933 *usedlen = 1;
9934 return NULL;
9935 }
9936#ifdef FEAT_EVAL
9937 result = list_find_str(get_vim_var_list(VV_OLDFILES),
9938 (long)i);
9939 if (result == NULL)
9940 {
9941 *errormsg = (char_u *)"";
9942 return NULL;
9943 }
9944#else
9945 *errormsg = (char_u *)_("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009946 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00009947#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009948 }
9949 else
Bram Moolenaard812df62008-11-09 12:46:09 +00009950 {
9951 buf = buflist_findnr(i);
9952 if (buf == NULL)
9953 {
9954 *errormsg = (char_u *)_("E194: No alternate file name to substitute for '#'");
9955 return NULL;
9956 }
9957 if (lnump != NULL)
9958 *lnump = ECMD_LAST;
9959 if (buf->b_fname == NULL)
9960 {
9961 result = (char_u *)"";
9962 valid = 0; /* Must have ":p:h" to be valid */
9963 }
9964 else
9965 result = buf->b_fname;
9966 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009967 break;
9968
9969#ifdef FEAT_SEARCHPATH
9970 case SPEC_CFILE: /* file name under cursor */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009971 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009972 if (result == NULL)
9973 {
9974 *errormsg = (char_u *)"";
9975 return NULL;
9976 }
9977 resultbuf = result; /* remember allocated string */
9978 break;
9979#endif
9980
9981#ifdef FEAT_AUTOCMD
9982 case SPEC_AFILE: /* file name for autocommand */
9983 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +00009984 if (result != NULL && !autocmd_fname_full)
9985 {
9986 /* Still need to turn the fname into a full path. It is
9987 * postponed to avoid a delay when <afile> is not used. */
9988 autocmd_fname_full = TRUE;
9989 result = FullName_save(autocmd_fname, FALSE);
9990 vim_free(autocmd_fname);
9991 autocmd_fname = result;
9992 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009993 if (result == NULL)
9994 {
9995 *errormsg = (char_u *)_("E495: no autocommand file name to substitute for \"<afile>\"");
9996 return NULL;
9997 }
Bram Moolenaara0174af2008-01-02 20:08:25 +00009998 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009999 break;
10000
10001 case SPEC_ABUF: /* buffer number for autocommand */
10002 if (autocmd_bufnr <= 0)
10003 {
10004 *errormsg = (char_u *)_("E496: no autocommand buffer number to substitute for \"<abuf>\"");
10005 return NULL;
10006 }
10007 sprintf((char *)strbuf, "%d", autocmd_bufnr);
10008 result = strbuf;
10009 break;
10010
10011 case SPEC_AMATCH: /* match name for autocommand */
10012 result = autocmd_match;
10013 if (result == NULL)
10014 {
10015 *errormsg = (char_u *)_("E497: no autocommand match name to substitute for \"<amatch>\"");
10016 return NULL;
10017 }
10018 break;
10019
10020#endif
10021 case SPEC_SFILE: /* file name for ":so" command */
10022 result = sourcing_name;
10023 if (result == NULL)
10024 {
10025 *errormsg = (char_u *)_("E498: no :source file name to substitute for \"<sfile>\"");
10026 return NULL;
10027 }
10028 break;
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010029 case SPEC_SLNUM: /* line in file for ":so" command */
10030 if (sourcing_name == NULL || sourcing_lnum == 0)
10031 {
10032 *errormsg = (char_u *)_("E842: no line number to use for \"<slnum>\"");
10033 return NULL;
10034 }
10035 sprintf((char *)strbuf, "%ld", (long)sourcing_lnum);
10036 result = strbuf;
10037 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010038#if defined(FEAT_CLIENTSERVER)
10039 case SPEC_CLIENT: /* Source of last submitted input */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010040 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
10041 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010042 result = strbuf;
10043 break;
10044#endif
10045 }
10046
10047 resultlen = (int)STRLEN(result); /* length of new string */
10048 if (src[*usedlen] == '<') /* remove the file name extension */
10049 {
10050 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010051 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010052 resultlen = (int)(s - result);
10053 }
10054#ifdef FEAT_MODIFY_FNAME
10055 else if (!skip_mod)
10056 {
10057 valid |= modify_fname(src, usedlen, &result, &resultbuf,
10058 &resultlen);
10059 if (result == NULL)
10060 {
10061 *errormsg = (char_u *)"";
10062 return NULL;
10063 }
10064 }
10065#endif
10066 }
10067
10068 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
10069 {
10070 if (valid != VALID_HEAD + VALID_PATH)
10071 /* xgettext:no-c-format */
10072 *errormsg = (char_u *)_("E499: Empty file name for '%' or '#', only works with \":p:h\"");
10073 else
10074 *errormsg = (char_u *)_("E500: Evaluates to an empty string");
10075 result = NULL;
10076 }
10077 else
10078 result = vim_strnsave(result, resultlen);
10079 vim_free(resultbuf);
10080 return result;
10081}
10082
10083/*
10084 * Concatenate all files in the argument list, separated by spaces, and return
10085 * it in one allocated string.
10086 * Spaces and backslashes in the file names are escaped with a backslash.
10087 * Returns NULL when out of memory.
10088 */
10089 static char_u *
10090arg_all()
10091{
10092 int len;
10093 int idx;
10094 char_u *retval = NULL;
10095 char_u *p;
10096
10097 /*
10098 * Do this loop two times:
10099 * first time: compute the total length
10100 * second time: concatenate the names
10101 */
10102 for (;;)
10103 {
10104 len = 0;
10105 for (idx = 0; idx < ARGCOUNT; ++idx)
10106 {
10107 p = alist_name(&ARGLIST[idx]);
10108 if (p != NULL)
10109 {
10110 if (len > 0)
10111 {
10112 /* insert a space in between names */
10113 if (retval != NULL)
10114 retval[len] = ' ';
10115 ++len;
10116 }
10117 for ( ; *p != NUL; ++p)
10118 {
10119 if (*p == ' ' || *p == '\\')
10120 {
10121 /* insert a backslash */
10122 if (retval != NULL)
10123 retval[len] = '\\';
10124 ++len;
10125 }
10126 if (retval != NULL)
10127 retval[len] = *p;
10128 ++len;
10129 }
10130 }
10131 }
10132
10133 /* second time: break here */
10134 if (retval != NULL)
10135 {
10136 retval[len] = NUL;
10137 break;
10138 }
10139
10140 /* allocate memory */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000010141 retval = alloc((unsigned)len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010142 if (retval == NULL)
10143 break;
10144 }
10145
10146 return retval;
10147}
10148
10149#if defined(FEAT_AUTOCMD) || defined(PROTO)
10150/*
10151 * Expand the <sfile> string in "arg".
10152 *
10153 * Returns an allocated string, or NULL for any error.
10154 */
10155 char_u *
10156expand_sfile(arg)
10157 char_u *arg;
10158{
10159 char_u *errormsg;
10160 int len;
10161 char_u *result;
10162 char_u *newres;
10163 char_u *repl;
10164 int srclen;
10165 char_u *p;
10166
10167 result = vim_strsave(arg);
10168 if (result == NULL)
10169 return NULL;
10170
10171 for (p = result; *p; )
10172 {
10173 if (STRNCMP(p, "<sfile>", 7) != 0)
10174 ++p;
10175 else
10176 {
10177 /* replace "<sfile>" with the sourced file name, and do ":" stuff */
Bram Moolenaar63b92542007-03-27 14:57:09 +000010178 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010179 if (errormsg != NULL)
10180 {
10181 if (*errormsg)
10182 emsg(errormsg);
10183 vim_free(result);
10184 return NULL;
10185 }
10186 if (repl == NULL) /* no match (cannot happen) */
10187 {
10188 p += srclen;
10189 continue;
10190 }
10191 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
10192 newres = alloc(len);
10193 if (newres == NULL)
10194 {
10195 vim_free(repl);
10196 vim_free(result);
10197 return NULL;
10198 }
10199 mch_memmove(newres, result, (size_t)(p - result));
10200 STRCPY(newres + (p - result), repl);
10201 len = (int)STRLEN(newres);
10202 STRCAT(newres, p + srclen);
10203 vim_free(repl);
10204 vim_free(result);
10205 result = newres;
10206 p = newres + len; /* continue after the match */
10207 }
10208 }
10209
10210 return result;
10211}
10212#endif
10213
10214#ifdef FEAT_SESSION
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010215static int ses_winsizes __ARGS((FILE *fd, int restore_size,
10216 win_T *tab_firstwin));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010217static int ses_win_rec __ARGS((FILE *fd, frame_T *fr));
10218static frame_T *ses_skipframe __ARGS((frame_T *fr));
10219static int ses_do_frame __ARGS((frame_T *fr));
10220static int ses_do_win __ARGS((win_T *wp));
10221static int ses_arglist __ARGS((FILE *fd, char *cmd, garray_T *gap, int fullname, unsigned *flagp));
10222static int ses_put_fname __ARGS((FILE *fd, char_u *name, unsigned *flagp));
10223static int ses_fname __ARGS((FILE *fd, buf_T *buf, unsigned *flagp));
10224
10225/*
10226 * Write openfile commands for the current buffers to an .exrc file.
10227 * Return FAIL on error, OK otherwise.
10228 */
10229 static int
10230makeopens(fd, dirnow)
10231 FILE *fd;
10232 char_u *dirnow; /* Current directory name */
10233{
10234 buf_T *buf;
10235 int only_save_windows = TRUE;
10236 int nr;
10237 int cnr = 1;
10238 int restore_size = TRUE;
10239 win_T *wp;
10240 char_u *sname;
10241 win_T *edited_win = NULL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010242 int tabnr;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010243 win_T *tab_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010244 frame_T *tab_topframe;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010245 int cur_arg_idx = 0;
Bram Moolenaar383c6f52008-01-04 15:01:07 +000010246 int next_arg_idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010247
10248 if (ssop_flags & SSOP_BUFFERS)
10249 only_save_windows = FALSE; /* Save ALL buffers */
10250
10251 /*
10252 * Begin by setting the this_session variable, and then other
10253 * sessionable variables.
10254 */
10255#ifdef FEAT_EVAL
10256 if (put_line(fd, "let v:this_session=expand(\"<sfile>:p\")") == FAIL)
10257 return FAIL;
10258 if (ssop_flags & SSOP_GLOBALS)
10259 if (store_session_globals(fd) == FAIL)
10260 return FAIL;
10261#endif
10262
10263 /*
10264 * Close all windows but one.
10265 */
10266 if (put_line(fd, "silent only") == FAIL)
10267 return FAIL;
10268
10269 /*
10270 * Now a :cd command to the session directory or the current directory
10271 */
10272 if (ssop_flags & SSOP_SESDIR)
10273 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010274 if (put_line(fd, "exe \"cd \" . escape(expand(\"<sfile>:p:h\"), ' ')")
10275 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010276 return FAIL;
10277 }
10278 else if (ssop_flags & SSOP_CURDIR)
10279 {
10280 sname = home_replace_save(NULL, globaldir != NULL ? globaldir : dirnow);
10281 if (sname == NULL
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010282 || fputs("cd ", fd) < 0
10283 || ses_put_fname(fd, sname, &ssop_flags) == FAIL
10284 || put_eol(fd) == FAIL)
10285 {
10286 vim_free(sname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010287 return FAIL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010288 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010289 vim_free(sname);
10290 }
10291
10292 /*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010293 * If there is an empty, unnamed buffer we will wipe it out later.
10294 * Remember the buffer number.
10295 */
10296 if (put_line(fd, "if expand('%') == '' && !&modified && line('$') <= 1 && getline(1) == ''") == FAIL)
10297 return FAIL;
10298 if (put_line(fd, " let s:wipebuf = bufnr('%')") == FAIL)
10299 return FAIL;
10300 if (put_line(fd, "endif") == FAIL)
10301 return FAIL;
10302
10303 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010304 * Now save the current files, current buffer first.
10305 */
10306 if (put_line(fd, "set shortmess=aoO") == FAIL)
10307 return FAIL;
10308
10309 /* Now put the other buffers into the buffer list */
10310 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
10311 {
10312 if (!(only_save_windows && buf->b_nwindows == 0)
10313 && !(buf->b_help && !(ssop_flags & SSOP_HELP))
10314 && buf->b_fname != NULL
10315 && buf->b_p_bl)
10316 {
10317 if (fprintf(fd, "badd +%ld ", buf->b_wininfo == NULL ? 1L
10318 : buf->b_wininfo->wi_fpos.lnum) < 0
10319 || ses_fname(fd, buf, &ssop_flags) == FAIL)
10320 return FAIL;
10321 }
10322 }
10323
10324 /* the global argument list */
10325 if (ses_arglist(fd, "args", &global_alist.al_ga,
10326 !(ssop_flags & SSOP_CURDIR), &ssop_flags) == FAIL)
10327 return FAIL;
10328
10329 if (ssop_flags & SSOP_RESIZE)
10330 {
10331 /* Note: after the restore we still check it worked!*/
10332 if (fprintf(fd, "set lines=%ld columns=%ld" , Rows, Columns) < 0
10333 || put_eol(fd) == FAIL)
10334 return FAIL;
10335 }
10336
10337#ifdef FEAT_GUI
10338 if (gui.in_use && (ssop_flags & SSOP_WINPOS))
10339 {
10340 int x, y;
10341
10342 if (gui_mch_get_winpos(&x, &y) == OK)
10343 {
10344 /* Note: after the restore we still check it worked!*/
10345 if (fprintf(fd, "winpos %d %d", x, y) < 0 || put_eol(fd) == FAIL)
10346 return FAIL;
10347 }
10348 }
10349#endif
10350
10351 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000010352 * May repeat putting Windows for each tab, when "tabpages" is in
10353 * 'sessionoptions'.
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010354 * Don't use goto_tabpage(), it may change directory and trigger
10355 * autocommands.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010356 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010357 tab_firstwin = firstwin; /* first window in tab page "tabnr" */
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010358 tab_topframe = topframe;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010359 for (tabnr = 1; ; ++tabnr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010360 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010361 int need_tabnew = FALSE;
10362
Bram Moolenaar18144c82006-04-12 21:52:12 +000010363 if ((ssop_flags & SSOP_TABPAGES))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010364 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010365 tabpage_T *tp = find_tabpage(tabnr);
10366
10367 if (tp == NULL)
10368 break; /* done all tab pages */
10369 if (tp == curtab)
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010370 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010371 tab_firstwin = firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010372 tab_topframe = topframe;
10373 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010374 else
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010375 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010376 tab_firstwin = tp->tp_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010377 tab_topframe = tp->tp_topframe;
10378 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010379 if (tabnr > 1)
10380 need_tabnew = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010381 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010382
Bram Moolenaar18144c82006-04-12 21:52:12 +000010383 /*
10384 * Before creating the window layout, try loading one file. If this
10385 * is aborted we don't end up with a number of useless windows.
10386 * This may have side effects! (e.g., compressed or network file).
10387 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010388 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010389 {
10390 if (ses_do_win(wp)
10391 && wp->w_buffer->b_ffname != NULL
10392 && !wp->w_buffer->b_help
10393#ifdef FEAT_QUICKFIX
10394 && !bt_nofile(wp->w_buffer)
10395#endif
10396 )
10397 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010398 if (fputs(need_tabnew ? "tabedit " : "edit ", fd) < 0
Bram Moolenaar18144c82006-04-12 21:52:12 +000010399 || ses_fname(fd, wp->w_buffer, &ssop_flags) == FAIL)
10400 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010401 need_tabnew = FALSE;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010402 if (!wp->w_arg_idx_invalid)
10403 edited_win = wp;
10404 break;
10405 }
10406 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010407
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010408 /* If no file got edited create an empty tab page. */
10409 if (need_tabnew && put_line(fd, "tabnew") == FAIL)
10410 return FAIL;
10411
Bram Moolenaar18144c82006-04-12 21:52:12 +000010412 /*
10413 * Save current window layout.
10414 */
10415 if (put_line(fd, "set splitbelow splitright") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010416 return FAIL;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010417 if (ses_win_rec(fd, tab_topframe) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010418 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010419 if (!p_sb && put_line(fd, "set nosplitbelow") == FAIL)
10420 return FAIL;
10421 if (!p_spr && put_line(fd, "set nosplitright") == FAIL)
10422 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010423
Bram Moolenaar18144c82006-04-12 21:52:12 +000010424 /*
10425 * Check if window sizes can be restored (no windows omitted).
10426 * Remember the window number of the current window after restoring.
10427 */
10428 nr = 0;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010429 for (wp = tab_firstwin; wp != NULL; wp = W_NEXT(wp))
Bram Moolenaar18144c82006-04-12 21:52:12 +000010430 {
10431 if (ses_do_win(wp))
10432 ++nr;
10433 else
10434 restore_size = FALSE;
10435 if (curwin == wp)
10436 cnr = nr;
10437 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010438
Bram Moolenaar18144c82006-04-12 21:52:12 +000010439 /* Go to the first window. */
10440 if (put_line(fd, "wincmd t") == FAIL)
10441 return FAIL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010442
Bram Moolenaar18144c82006-04-12 21:52:12 +000010443 /*
10444 * If more than one window, see if sizes can be restored.
10445 * First set 'winheight' and 'winwidth' to 1 to avoid the windows being
10446 * resized when moving between windows.
10447 * Do this before restoring the view, so that the topline and the
10448 * cursor can be set. This is done again below.
10449 */
10450 if (put_line(fd, "set winheight=1 winwidth=1") == FAIL)
10451 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010452 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010453 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010454
Bram Moolenaar18144c82006-04-12 21:52:12 +000010455 /*
10456 * Restore the view of the window (options, file, cursor, etc.).
10457 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010458 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010459 {
10460 if (!ses_do_win(wp))
10461 continue;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010462 if (put_view(fd, wp, wp != edited_win, &ssop_flags,
10463 cur_arg_idx) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010464 return FAIL;
10465 if (nr > 1 && put_line(fd, "wincmd w") == FAIL)
10466 return FAIL;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010467 next_arg_idx = wp->w_arg_idx;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010468 }
10469
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010470 /* The argument index in the first tab page is zero, need to set it in
10471 * each window. For further tab pages it's the window where we do
10472 * "tabedit". */
10473 cur_arg_idx = next_arg_idx;
10474
Bram Moolenaar18144c82006-04-12 21:52:12 +000010475 /*
10476 * Restore cursor to the current window if it's not the first one.
10477 */
10478 if (cnr > 1 && (fprintf(fd, "%dwincmd w", cnr) < 0
10479 || put_eol(fd) == FAIL))
10480 return FAIL;
10481
10482 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000010483 * Restore window sizes again after jumping around in windows, because
10484 * the current window has a minimum size while others may not.
10485 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010486 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010487 return FAIL;
10488
Bram Moolenaar18144c82006-04-12 21:52:12 +000010489 /* Don't continue in another tab page when doing only the current one
10490 * or when at the last tab page. */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010491 if (!(ssop_flags & SSOP_TABPAGES))
Bram Moolenaar18144c82006-04-12 21:52:12 +000010492 break;
10493 }
10494
10495 if (ssop_flags & SSOP_TABPAGES)
10496 {
Bram Moolenaar18144c82006-04-12 21:52:12 +000010497 if (fprintf(fd, "tabnext %d", tabpage_index(curtab)) < 0
10498 || put_eol(fd) == FAIL)
10499 return FAIL;
10500 }
10501
Bram Moolenaar9c102382006-05-03 21:26:49 +000010502 /*
10503 * Wipe out an empty unnamed buffer we started in.
10504 */
10505 if (put_line(fd, "if exists('s:wipebuf')") == FAIL)
10506 return FAIL;
Bram Moolenaarf3a67882006-05-05 21:09:41 +000010507 if (put_line(fd, " silent exe 'bwipe ' . s:wipebuf") == FAIL)
Bram Moolenaar9c102382006-05-03 21:26:49 +000010508 return FAIL;
10509 if (put_line(fd, "endif") == FAIL)
10510 return FAIL;
10511 if (put_line(fd, "unlet! s:wipebuf") == FAIL)
10512 return FAIL;
10513
10514 /* Re-apply 'winheight', 'winwidth' and 'shortmess'. */
10515 if (fprintf(fd, "set winheight=%ld winwidth=%ld shortmess=%s",
10516 p_wh, p_wiw, p_shm) < 0 || put_eol(fd) == FAIL)
10517 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010518
10519 /*
10520 * Lastly, execute the x.vim file if it exists.
10521 */
10522 if (put_line(fd, "let s:sx = expand(\"<sfile>:p:r\").\"x.vim\"") == FAIL
10523 || put_line(fd, "if file_readable(s:sx)") == FAIL
Bram Moolenaar42ba1262008-12-09 10:18:03 +000010524 || put_line(fd, " exe \"source \" . fnameescape(s:sx)") == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000010525 || put_line(fd, "endif") == FAIL)
10526 return FAIL;
10527
10528 return OK;
10529}
10530
10531 static int
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010532ses_winsizes(fd, restore_size, tab_firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010533 FILE *fd;
10534 int restore_size;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010535 win_T *tab_firstwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010536{
10537 int n = 0;
10538 win_T *wp;
10539
10540 if (restore_size && (ssop_flags & SSOP_WINSIZE))
10541 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010542 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010543 {
10544 if (!ses_do_win(wp))
10545 continue;
10546 ++n;
10547
10548 /* restore height when not full height */
10549 if (wp->w_height + wp->w_status_height < topframe->fr_height
10550 && (fprintf(fd,
10551 "exe '%dresize ' . ((&lines * %ld + %ld) / %ld)",
10552 n, (long)wp->w_height, Rows / 2, Rows) < 0
10553 || put_eol(fd) == FAIL))
10554 return FAIL;
10555
10556 /* restore width when not full width */
10557 if (wp->w_width < Columns && (fprintf(fd,
10558 "exe 'vert %dresize ' . ((&columns * %ld + %ld) / %ld)",
10559 n, (long)wp->w_width, Columns / 2, Columns) < 0
10560 || put_eol(fd) == FAIL))
10561 return FAIL;
10562 }
10563 }
10564 else
10565 {
10566 /* Just equalise window sizes */
10567 if (put_line(fd, "wincmd =") == FAIL)
10568 return FAIL;
10569 }
10570 return OK;
10571}
10572
10573/*
10574 * Write commands to "fd" to recursively create windows for frame "fr",
10575 * horizontally and vertically split.
10576 * After the commands the last window in the frame is the current window.
10577 * Returns FAIL when writing the commands to "fd" fails.
10578 */
10579 static int
10580ses_win_rec(fd, fr)
10581 FILE *fd;
10582 frame_T *fr;
10583{
10584 frame_T *frc;
10585 int count = 0;
10586
10587 if (fr->fr_layout != FR_LEAF)
10588 {
10589 /* Find first frame that's not skipped and then create a window for
10590 * each following one (first frame is already there). */
10591 frc = ses_skipframe(fr->fr_child);
10592 if (frc != NULL)
10593 while ((frc = ses_skipframe(frc->fr_next)) != NULL)
10594 {
10595 /* Make window as big as possible so that we have lots of room
10596 * to split. */
10597 if (put_line(fd, "wincmd _ | wincmd |") == FAIL
10598 || put_line(fd, fr->fr_layout == FR_COL
10599 ? "split" : "vsplit") == FAIL)
10600 return FAIL;
10601 ++count;
10602 }
10603
10604 /* Go back to the first window. */
10605 if (count > 0 && (fprintf(fd, fr->fr_layout == FR_COL
10606 ? "%dwincmd k" : "%dwincmd h", count) < 0
10607 || put_eol(fd) == FAIL))
10608 return FAIL;
10609
10610 /* Recursively create frames/windows in each window of this column or
10611 * row. */
10612 frc = ses_skipframe(fr->fr_child);
10613 while (frc != NULL)
10614 {
10615 ses_win_rec(fd, frc);
10616 frc = ses_skipframe(frc->fr_next);
10617 /* Go to next window. */
10618 if (frc != NULL && put_line(fd, "wincmd w") == FAIL)
10619 return FAIL;
10620 }
10621 }
10622 return OK;
10623}
10624
10625/*
10626 * Skip frames that don't contain windows we want to save in the Session.
10627 * Returns NULL when there none.
10628 */
10629 static frame_T *
10630ses_skipframe(fr)
10631 frame_T *fr;
10632{
10633 frame_T *frc;
10634
10635 for (frc = fr; frc != NULL; frc = frc->fr_next)
10636 if (ses_do_frame(frc))
10637 break;
10638 return frc;
10639}
10640
10641/*
10642 * Return TRUE if frame "fr" has a window somewhere that we want to save in
10643 * the Session.
10644 */
10645 static int
10646ses_do_frame(fr)
10647 frame_T *fr;
10648{
10649 frame_T *frc;
10650
10651 if (fr->fr_layout == FR_LEAF)
10652 return ses_do_win(fr->fr_win);
10653 for (frc = fr->fr_child; frc != NULL; frc = frc->fr_next)
10654 if (ses_do_frame(frc))
10655 return TRUE;
10656 return FALSE;
10657}
10658
10659/*
10660 * Return non-zero if window "wp" is to be stored in the Session.
10661 */
10662 static int
10663ses_do_win(wp)
10664 win_T *wp;
10665{
10666 if (wp->w_buffer->b_fname == NULL
10667#ifdef FEAT_QUICKFIX
10668 /* When 'buftype' is "nofile" can't restore the window contents. */
10669 || bt_nofile(wp->w_buffer)
10670#endif
10671 )
10672 return (ssop_flags & SSOP_BLANK);
10673 if (wp->w_buffer->b_help)
10674 return (ssop_flags & SSOP_HELP);
10675 return TRUE;
10676}
10677
10678/*
10679 * Write commands to "fd" to restore the view of a window.
10680 * Caller must make sure 'scrolloff' is zero.
10681 */
10682 static int
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010683put_view(fd, wp, add_edit, flagp, current_arg_idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010684 FILE *fd;
10685 win_T *wp;
10686 int add_edit; /* add ":edit" command to view */
10687 unsigned *flagp; /* vop_flags or ssop_flags */
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010688 int current_arg_idx; /* current argument index of the window, use
10689 * -1 if unknown */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010690{
10691 win_T *save_curwin;
10692 int f;
10693 int do_cursor;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010694 int did_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010695
10696 /* Always restore cursor position for ":mksession". For ":mkview" only
10697 * when 'viewoptions' contains "cursor". */
10698 do_cursor = (flagp == &ssop_flags || *flagp & SSOP_CURSOR);
10699
10700 /*
10701 * Local argument list.
10702 */
10703 if (wp->w_alist == &global_alist)
10704 {
10705 if (put_line(fd, "argglobal") == FAIL)
10706 return FAIL;
10707 }
10708 else
10709 {
10710 if (ses_arglist(fd, "arglocal", &wp->w_alist->al_ga,
10711 flagp == &vop_flags
10712 || !(*flagp & SSOP_CURDIR)
10713 || wp->w_localdir != NULL, flagp) == FAIL)
10714 return FAIL;
10715 }
10716
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010717 /* Only when part of a session: restore the argument index. Some
10718 * arguments may have been deleted, check if the index is valid. */
Bram Moolenaar51f53df2010-06-26 05:25:54 +020010719 if (wp->w_arg_idx != current_arg_idx && wp->w_arg_idx < WARGCOUNT(wp)
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010720 && flagp == &ssop_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010721 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010722 if (fprintf(fd, "%ldargu", (long)wp->w_arg_idx + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010723 || put_eol(fd) == FAIL)
10724 return FAIL;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010725 did_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010726 }
10727
10728 /* Edit the file. Skip this when ":next" already did it. */
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010729 if (add_edit && (!did_next || wp->w_arg_idx_invalid))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010730 {
10731 /*
10732 * Load the file.
10733 */
10734 if (wp->w_buffer->b_ffname != NULL
10735#ifdef FEAT_QUICKFIX
10736 && !bt_nofile(wp->w_buffer)
10737#endif
10738 )
10739 {
10740 /*
10741 * Editing a file in this buffer: use ":edit file".
10742 * This may have side effects! (e.g., compressed or network file).
10743 */
10744 if (fputs("edit ", fd) < 0
10745 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
10746 return FAIL;
10747 }
10748 else
10749 {
10750 /* No file in this buffer, just make it empty. */
10751 if (put_line(fd, "enew") == FAIL)
10752 return FAIL;
10753#ifdef FEAT_QUICKFIX
10754 if (wp->w_buffer->b_ffname != NULL)
10755 {
10756 /* The buffer does have a name, but it's not a file name. */
10757 if (fputs("file ", fd) < 0
10758 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
10759 return FAIL;
10760 }
10761#endif
10762 do_cursor = FALSE;
10763 }
10764 }
10765
10766 /*
10767 * Local mappings and abbreviations.
10768 */
10769 if ((*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
10770 && makemap(fd, wp->w_buffer) == FAIL)
10771 return FAIL;
10772
10773 /*
10774 * Local options. Need to go to the window temporarily.
10775 * Store only local values when using ":mkview" and when ":mksession" is
10776 * used and 'sessionoptions' doesn't include "options".
10777 * Some folding options are always stored when "folds" is included,
10778 * otherwise the folds would not be restored correctly.
10779 */
10780 save_curwin = curwin;
10781 curwin = wp;
10782 curbuf = curwin->w_buffer;
10783 if (*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
10784 f = makeset(fd, OPT_LOCAL,
10785 flagp == &vop_flags || !(*flagp & SSOP_OPTIONS));
10786#ifdef FEAT_FOLDING
10787 else if (*flagp & SSOP_FOLDS)
10788 f = makefoldset(fd);
10789#endif
10790 else
10791 f = OK;
10792 curwin = save_curwin;
10793 curbuf = curwin->w_buffer;
10794 if (f == FAIL)
10795 return FAIL;
10796
10797#ifdef FEAT_FOLDING
10798 /*
10799 * Save Folds when 'buftype' is empty and for help files.
10800 */
10801 if ((*flagp & SSOP_FOLDS)
10802 && wp->w_buffer->b_ffname != NULL
Bram Moolenaarb1b715d2006-01-21 22:09:43 +000010803# ifdef FEAT_QUICKFIX
10804 && (*wp->w_buffer->b_p_bt == NUL || wp->w_buffer->b_help)
10805# endif
10806 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000010807 {
10808 if (put_folds(fd, wp) == FAIL)
10809 return FAIL;
10810 }
10811#endif
10812
10813 /*
10814 * Set the cursor after creating folds, since that moves the cursor.
10815 */
10816 if (do_cursor)
10817 {
10818
10819 /* Restore the cursor line in the file and relatively in the
10820 * window. Don't use "G", it changes the jumplist. */
10821 if (fprintf(fd, "let s:l = %ld - ((%ld * winheight(0) + %ld) / %ld)",
10822 (long)wp->w_cursor.lnum,
10823 (long)(wp->w_cursor.lnum - wp->w_topline),
10824 (long)wp->w_height / 2, (long)wp->w_height) < 0
10825 || put_eol(fd) == FAIL
10826 || put_line(fd, "if s:l < 1 | let s:l = 1 | endif") == FAIL
10827 || put_line(fd, "exe s:l") == FAIL
10828 || put_line(fd, "normal! zt") == FAIL
10829 || fprintf(fd, "%ld", (long)wp->w_cursor.lnum) < 0
10830 || put_eol(fd) == FAIL)
10831 return FAIL;
10832 /* Restore the cursor column and left offset when not wrapping. */
10833 if (wp->w_cursor.col == 0)
10834 {
10835 if (put_line(fd, "normal! 0") == FAIL)
10836 return FAIL;
10837 }
10838 else
10839 {
10840 if (!wp->w_p_wrap && wp->w_leftcol > 0 && wp->w_width > 0)
10841 {
10842 if (fprintf(fd,
10843 "let s:c = %ld - ((%ld * winwidth(0) + %ld) / %ld)",
Bram Moolenaar558ddad2013-02-20 19:26:29 +010010844 (long)wp->w_virtcol + 1,
10845 (long)(wp->w_virtcol - wp->w_leftcol),
Bram Moolenaar071d4272004-06-13 20:20:40 +000010846 (long)wp->w_width / 2, (long)wp->w_width) < 0
10847 || put_eol(fd) == FAIL
10848 || put_line(fd, "if s:c > 0") == FAIL
10849 || fprintf(fd,
Bram Moolenaar558ddad2013-02-20 19:26:29 +010010850 " exe 'normal! ' . s:c . '|zs' . %ld . '|'",
10851 (long)wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010852 || put_eol(fd) == FAIL
10853 || put_line(fd, "else") == FAIL
Bram Moolenaarfdf447b2013-02-26 17:21:29 +010010854 || fprintf(fd, " normal! 0%d|", wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010855 || put_eol(fd) == FAIL
10856 || put_line(fd, "endif") == FAIL)
10857 return FAIL;
10858 }
10859 else
10860 {
Bram Moolenaar558ddad2013-02-20 19:26:29 +010010861 if (fprintf(fd, "normal! 0%d|", wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010862 || put_eol(fd) == FAIL)
10863 return FAIL;
10864 }
10865 }
10866 }
10867
10868 /*
10869 * Local directory.
10870 */
10871 if (wp->w_localdir != NULL)
10872 {
10873 if (fputs("lcd ", fd) < 0
10874 || ses_put_fname(fd, wp->w_localdir, flagp) == FAIL
10875 || put_eol(fd) == FAIL)
10876 return FAIL;
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010877 did_lcd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010878 }
10879
10880 return OK;
10881}
10882
10883/*
10884 * Write an argument list to the session file.
10885 * Returns FAIL if writing fails.
10886 */
10887 static int
10888ses_arglist(fd, cmd, gap, fullname, flagp)
10889 FILE *fd;
10890 char *cmd;
10891 garray_T *gap;
10892 int fullname; /* TRUE: use full path name */
10893 unsigned *flagp;
10894{
10895 int i;
Bram Moolenaard9462e32011-04-11 21:35:11 +020010896 char_u *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010897 char_u *s;
10898
10899 if (gap->ga_len == 0)
10900 return put_line(fd, "silent! argdel *");
10901 if (fputs(cmd, fd) < 0)
10902 return FAIL;
10903 for (i = 0; i < gap->ga_len; ++i)
10904 {
10905 /* NULL file names are skipped (only happens when out of memory). */
10906 s = alist_name(&((aentry_T *)gap->ga_data)[i]);
10907 if (s != NULL)
10908 {
10909 if (fullname)
10910 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020010911 buf = alloc(MAXPATHL);
10912 if (buf != NULL)
10913 {
10914 (void)vim_FullName(s, buf, MAXPATHL, FALSE);
10915 s = buf;
10916 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010917 }
10918 if (fputs(" ", fd) < 0 || ses_put_fname(fd, s, flagp) == FAIL)
Bram Moolenaard9462e32011-04-11 21:35:11 +020010919 {
10920 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010921 return FAIL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020010922 }
10923 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010924 }
10925 }
10926 return put_eol(fd);
10927}
10928
10929/*
10930 * Write a buffer name to the session file.
10931 * Also ends the line.
10932 * Returns FAIL if writing fails.
10933 */
10934 static int
10935ses_fname(fd, buf, flagp)
10936 FILE *fd;
10937 buf_T *buf;
10938 unsigned *flagp;
10939{
10940 char_u *name;
10941
10942 /* Use the short file name if the current directory is known at the time
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010943 * the session file will be sourced.
10944 * Don't do this for ":mkview", we don't know the current directory.
10945 * Don't do this after ":lcd", we don't keep track of what the current
10946 * directory is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010947 if (buf->b_sfname != NULL
10948 && flagp == &ssop_flags
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010949 && (ssop_flags & (SSOP_CURDIR | SSOP_SESDIR))
Bram Moolenaar8d594672009-07-01 18:18:57 +000010950#ifdef FEAT_AUTOCHDIR
10951 && !p_acd
10952#endif
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010953 && !did_lcd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010954 name = buf->b_sfname;
10955 else
10956 name = buf->b_ffname;
10957 if (ses_put_fname(fd, name, flagp) == FAIL || put_eol(fd) == FAIL)
10958 return FAIL;
10959 return OK;
10960}
10961
10962/*
10963 * Write a file name to the session file.
10964 * Takes care of the "slash" option in 'sessionoptions' and escapes special
10965 * characters.
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010966 * Returns FAIL if writing fails or out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010967 */
10968 static int
10969ses_put_fname(fd, name, flagp)
10970 FILE *fd;
10971 char_u *name;
10972 unsigned *flagp;
10973{
10974 char_u *sname;
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010975 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010976 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010977
10978 sname = home_replace_save(NULL, name);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010979 if (sname == NULL)
10980 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010981
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010982 if (*flagp & SSOP_SLASH)
10983 {
10984 /* change all backslashes to forward slashes */
10985 for (p = sname; *p != NUL; mb_ptr_adv(p))
10986 if (*p == '\\')
10987 *p = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010988 }
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010989
Bram Moolenaar84a05ac2013-05-06 04:24:17 +020010990 /* escape special characters */
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010991 p = vim_strsave_fnameescape(sname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010992 vim_free(sname);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010993 if (p == NULL)
10994 return FAIL;
10995
10996 /* write the result */
10997 if (fputs((char *)p, fd) < 0)
10998 retval = FAIL;
10999
11000 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011001 return retval;
11002}
11003
11004/*
11005 * ":loadview [nr]"
11006 */
11007 static void
11008ex_loadview(eap)
11009 exarg_T *eap;
11010{
11011 char_u *fname;
11012
11013 fname = get_view_file(*eap->arg);
11014 if (fname != NULL)
11015 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +000011016 do_source(fname, FALSE, DOSO_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011017 vim_free(fname);
11018 }
11019}
11020
11021/*
11022 * Get the name of the view file for the current buffer.
11023 */
11024 static char_u *
11025get_view_file(c)
11026 int c;
11027{
11028 int len = 0;
11029 char_u *p, *s;
11030 char_u *retval;
11031 char_u *sname;
11032
11033 if (curbuf->b_ffname == NULL)
11034 {
11035 EMSG(_(e_noname));
11036 return NULL;
11037 }
11038 sname = home_replace_save(NULL, curbuf->b_ffname);
11039 if (sname == NULL)
11040 return NULL;
11041
11042 /*
11043 * We want a file name without separators, because we're not going to make
11044 * a directory.
11045 * "normal" path separator -> "=+"
11046 * "=" -> "=="
11047 * ":" path separator -> "=-"
11048 */
11049 for (p = sname; *p; ++p)
11050 if (*p == '=' || vim_ispathsep(*p))
11051 ++len;
11052 retval = alloc((unsigned)(STRLEN(sname) + len + STRLEN(p_vdir) + 9));
11053 if (retval != NULL)
11054 {
11055 STRCPY(retval, p_vdir);
11056 add_pathsep(retval);
11057 s = retval + STRLEN(retval);
11058 for (p = sname; *p; ++p)
11059 {
11060 if (*p == '=')
11061 {
11062 *s++ = '=';
11063 *s++ = '=';
11064 }
11065 else if (vim_ispathsep(*p))
11066 {
11067 *s++ = '=';
Bram Moolenaare60acc12011-05-10 16:41:25 +020011068#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011069 if (*p == ':')
11070 *s++ = '-';
11071 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000011072#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000011073 *s++ = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +000011074 }
11075 else
11076 *s++ = *p;
11077 }
11078 *s++ = '=';
11079 *s++ = c;
11080 STRCPY(s, ".vim");
11081 }
11082
11083 vim_free(sname);
11084 return retval;
11085}
11086
11087#endif /* FEAT_SESSION */
11088
11089/*
11090 * Write end-of-line character(s) for ":mkexrc", ":mkvimrc" and ":mksession".
11091 * Return FAIL for a write error.
11092 */
11093 int
11094put_eol(fd)
11095 FILE *fd;
11096{
11097 if (
11098#ifdef USE_CRNL
11099 (
11100# ifdef MKSESSION_NL
11101 !mksession_nl &&
11102# endif
11103 (putc('\r', fd) < 0)) ||
11104#endif
11105 (putc('\n', fd) < 0))
11106 return FAIL;
11107 return OK;
11108}
11109
11110/*
11111 * Write a line to "fd".
11112 * Return FAIL for a write error.
11113 */
11114 int
11115put_line(fd, s)
11116 FILE *fd;
11117 char *s;
11118{
11119 if (fputs(s, fd) < 0 || put_eol(fd) == FAIL)
11120 return FAIL;
11121 return OK;
11122}
11123
11124#ifdef FEAT_VIMINFO
11125/*
11126 * ":rviminfo" and ":wviminfo".
11127 */
11128 static void
11129ex_viminfo(eap)
11130 exarg_T *eap;
11131{
11132 char_u *save_viminfo;
11133
11134 save_viminfo = p_viminfo;
11135 if (*p_viminfo == NUL)
11136 p_viminfo = (char_u *)"'100";
11137 if (eap->cmdidx == CMD_rviminfo)
11138 {
Bram Moolenaard812df62008-11-09 12:46:09 +000011139 if (read_viminfo(eap->arg, VIF_WANT_INFO | VIF_WANT_MARKS
11140 | (eap->forceit ? VIF_FORCEIT : 0)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011141 EMSG(_("E195: Cannot open viminfo file for reading"));
11142 }
11143 else
11144 write_viminfo(eap->arg, eap->forceit);
11145 p_viminfo = save_viminfo;
11146}
11147#endif
11148
11149#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +000011150/*
Bram Moolenaard9462e32011-04-11 21:35:11 +020011151 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +000011152 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +000011153 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011154 void
11155dialog_msg(buff, format, fname)
11156 char_u *buff;
11157 char *format;
11158 char_u *fname;
11159{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011160 if (fname == NULL)
11161 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +020011162 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011163}
11164#endif
11165
11166/*
11167 * ":behave {mswin,xterm}"
11168 */
11169 static void
11170ex_behave(eap)
11171 exarg_T *eap;
11172{
11173 if (STRCMP(eap->arg, "mswin") == 0)
11174 {
11175 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
11176 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
11177 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
11178 set_option_value((char_u *)"keymodel", 0L,
11179 (char_u *)"startsel,stopsel", 0);
11180 }
11181 else if (STRCMP(eap->arg, "xterm") == 0)
11182 {
11183 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
11184 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
11185 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
11186 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
11187 }
11188 else
11189 EMSG2(_(e_invarg2), eap->arg);
11190}
11191
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010011192#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
11193/*
11194 * Function given to ExpandGeneric() to obtain the possible arguments of the
11195 * ":behave {mswin,xterm}" command.
11196 */
11197 char_u *
11198get_behave_arg(xp, idx)
11199 expand_T *xp UNUSED;
11200 int idx;
11201{
11202 if (idx == 0)
11203 return (char_u *)"mswin";
11204 if (idx == 1)
11205 return (char_u *)"xterm";
11206 return NULL;
11207}
11208#endif
11209
Bram Moolenaar071d4272004-06-13 20:20:40 +000011210#ifdef FEAT_AUTOCMD
11211static int filetype_detect = FALSE;
11212static int filetype_plugin = FALSE;
11213static int filetype_indent = FALSE;
11214
11215/*
11216 * ":filetype [plugin] [indent] {on,off,detect}"
11217 * on: Load the filetype.vim file to install autocommands for file types.
11218 * off: Load the ftoff.vim file to remove all autocommands for file types.
11219 * plugin on: load filetype.vim and ftplugin.vim
11220 * plugin off: load ftplugof.vim
11221 * indent on: load filetype.vim and indent.vim
11222 * indent off: load indoff.vim
11223 */
11224 static void
11225ex_filetype(eap)
11226 exarg_T *eap;
11227{
11228 char_u *arg = eap->arg;
11229 int plugin = FALSE;
11230 int indent = FALSE;
11231
11232 if (*eap->arg == NUL)
11233 {
11234 /* Print current status. */
11235 smsg((char_u *)"filetype detection:%s plugin:%s indent:%s",
11236 filetype_detect ? "ON" : "OFF",
11237 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
11238 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
11239 return;
11240 }
11241
11242 /* Accept "plugin" and "indent" in any order. */
11243 for (;;)
11244 {
11245 if (STRNCMP(arg, "plugin", 6) == 0)
11246 {
11247 plugin = TRUE;
11248 arg = skipwhite(arg + 6);
11249 continue;
11250 }
11251 if (STRNCMP(arg, "indent", 6) == 0)
11252 {
11253 indent = TRUE;
11254 arg = skipwhite(arg + 6);
11255 continue;
11256 }
11257 break;
11258 }
11259 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
11260 {
11261 if (*arg == 'o' || !filetype_detect)
11262 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011263 source_runtime((char_u *)FILETYPE_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011264 filetype_detect = TRUE;
11265 if (plugin)
11266 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011267 source_runtime((char_u *)FTPLUGIN_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011268 filetype_plugin = TRUE;
11269 }
11270 if (indent)
11271 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011272 source_runtime((char_u *)INDENT_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011273 filetype_indent = TRUE;
11274 }
11275 }
11276 if (*arg == 'd')
11277 {
11278 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE);
Bram Moolenaara3227e22006-03-08 21:32:40 +000011279 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011280 }
11281 }
11282 else if (STRCMP(arg, "off") == 0)
11283 {
11284 if (plugin || indent)
11285 {
11286 if (plugin)
11287 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011288 source_runtime((char_u *)FTPLUGOF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011289 filetype_plugin = FALSE;
11290 }
11291 if (indent)
11292 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011293 source_runtime((char_u *)INDOFF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011294 filetype_indent = FALSE;
11295 }
11296 }
11297 else
11298 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011299 source_runtime((char_u *)FTOFF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011300 filetype_detect = FALSE;
11301 }
11302 }
11303 else
11304 EMSG2(_(e_invarg2), arg);
11305}
11306
11307/*
11308 * ":setfiletype {name}"
11309 */
11310 static void
11311ex_setfiletype(eap)
11312 exarg_T *eap;
11313{
11314 if (!did_filetype)
11315 set_option_value((char_u *)"filetype", 0L, eap->arg, OPT_LOCAL);
11316}
11317#endif
11318
Bram Moolenaar071d4272004-06-13 20:20:40 +000011319 static void
11320ex_digraphs(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +000011321 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011322{
11323#ifdef FEAT_DIGRAPHS
11324 if (*eap->arg != NUL)
11325 putdigraph(eap->arg);
11326 else
11327 listdigraphs();
11328#else
11329 EMSG(_("E196: No digraphs in this version"));
11330#endif
11331}
11332
11333 static void
11334ex_set(eap)
11335 exarg_T *eap;
11336{
11337 int flags = 0;
11338
11339 if (eap->cmdidx == CMD_setlocal)
11340 flags = OPT_LOCAL;
11341 else if (eap->cmdidx == CMD_setglobal)
11342 flags = OPT_GLOBAL;
11343#if defined(FEAT_EVAL) && defined(FEAT_AUTOCMD) && defined(FEAT_BROWSE)
11344 if (cmdmod.browse && flags == 0)
11345 ex_options(eap);
11346 else
11347#endif
11348 (void)do_set(eap->arg, flags);
11349}
11350
11351#ifdef FEAT_SEARCH_EXTRA
11352/*
11353 * ":nohlsearch"
11354 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011355 static void
11356ex_nohlsearch(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +000011357 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011358{
11359 no_hlsearch = TRUE;
Bram Moolenaarf71a3db2006-03-12 21:50:18 +000011360 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011361}
11362
11363/*
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011364 * ":[N]match {group} {pattern}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000011365 * Sets nextcmd to the start of the next command, if any. Also called when
11366 * skipping commands to find the next command.
11367 */
11368 static void
11369ex_match(eap)
11370 exarg_T *eap;
11371{
11372 char_u *p;
Bram Moolenaar52d36c82007-08-11 14:00:30 +000011373 char_u *g = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011374 char_u *end;
11375 int c;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011376 int id;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011377
11378 if (eap->line2 <= 3)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011379 id = eap->line2;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011380 else
11381 {
11382 EMSG(e_invcmd);
11383 return;
11384 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011385
11386 /* First clear any old pattern. */
11387 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011388 match_delete(curwin, id, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011389
11390 if (ends_excmd(*eap->arg))
11391 end = eap->arg;
11392 else if ((STRNICMP(eap->arg, "none", 4) == 0
11393 && (vim_iswhite(eap->arg[4]) || ends_excmd(eap->arg[4]))))
11394 end = eap->arg + 4;
11395 else
11396 {
11397 p = skiptowhite(eap->arg);
11398 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011399 g = vim_strnsave(eap->arg, (int)(p - eap->arg));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011400 p = skipwhite(p);
11401 if (*p == NUL)
11402 {
11403 /* There must be two arguments. */
11404 EMSG2(_(e_invarg2), eap->arg);
11405 return;
11406 }
11407 end = skip_regexp(p + 1, *p, TRUE, NULL);
11408 if (!eap->skip)
11409 {
11410 if (*end != NUL && !ends_excmd(*skipwhite(end + 1)))
11411 {
11412 eap->errmsg = e_trailing;
11413 return;
11414 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000011415 if (*end != *p)
11416 {
11417 EMSG2(_(e_invarg2), p);
11418 return;
11419 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011420
11421 c = *end;
11422 *end = NUL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011423 match_add(curwin, g, p + 1, 10, id);
11424 vim_free(g);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000011425 *end = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011426 }
11427 }
11428 eap->nextcmd = find_nextcmd(end);
11429}
11430#endif
11431
11432#ifdef FEAT_CRYPT
11433/*
11434 * ":X": Get crypt key
11435 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011436 static void
11437ex_X(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +000011438 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011439{
Bram Moolenaar49771f42010-07-20 17:32:38 +020011440 if (get_crypt_method(curbuf) == 0 || blowfish_self_test() == OK)
Bram Moolenaar40e6a712010-05-16 22:32:54 +020011441 (void)get_crypt_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011442}
11443#endif
11444
11445#ifdef FEAT_FOLDING
11446 static void
11447ex_fold(eap)
11448 exarg_T *eap;
11449{
11450 if (foldManualAllowed(TRUE))
11451 foldCreate(eap->line1, eap->line2);
11452}
11453
11454 static void
11455ex_foldopen(eap)
11456 exarg_T *eap;
11457{
11458 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
11459 eap->forceit, FALSE);
11460}
11461
11462 static void
11463ex_folddo(eap)
11464 exarg_T *eap;
11465{
11466 linenr_T lnum;
11467
11468 /* First set the marks for all lines closed/open. */
11469 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
11470 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
11471 ml_setmarked(lnum);
11472
11473 /* Execute the command on the marked lines. */
11474 global_exe(eap->arg);
11475 ml_clearmarked(); /* clear rest of the marks */
11476}
11477#endif