blob: 15ce34247ce8bfc80689706b7cc2b8ae896e9aa5 [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
Bram Moolenaar958636c2014-10-21 20:01:58 +020052/* Wether a command index indicates a user command. */
53# define IS_USER_CMDIDX(idx) ((int)(idx) < 0)
54
Bram Moolenaar071d4272004-06-13 20:20:40 +000055#else
56# define ex_command ex_ni
57# define ex_comclear ex_ni
58# define ex_delcommand ex_ni
Bram Moolenaar958636c2014-10-21 20:01:58 +020059/* Wether a command index indicates a user command. */
60# define IS_USER_CMDIDX(idx) (FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +000061#endif
62
63#ifdef FEAT_EVAL
Bram Moolenaar89d40322006-08-29 15:30:07 +000064static 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 +000065#else
Bram Moolenaar89d40322006-08-29 15:30:07 +000066static char_u *do_one_cmd __ARGS((char_u **, int, char_u *(*fgetline)(int, void *, int), void *cookie));
Bram Moolenaar071d4272004-06-13 20:20:40 +000067static int if_level = 0; /* depth in :if */
68#endif
Bram Moolenaara6f4d612011-09-21 19:10:46 +020069static void append_command __ARGS((char_u *cmd));
Bram Moolenaar071d4272004-06-13 20:20:40 +000070static char_u *find_command __ARGS((exarg_T *eap, int *full));
71
72static void ex_abbreviate __ARGS((exarg_T *eap));
73static void ex_map __ARGS((exarg_T *eap));
74static void ex_unmap __ARGS((exarg_T *eap));
75static void ex_mapclear __ARGS((exarg_T *eap));
76static void ex_abclear __ARGS((exarg_T *eap));
77#ifndef FEAT_MENU
78# define ex_emenu ex_ni
79# define ex_menu ex_ni
80# define ex_menutranslate ex_ni
81#endif
82#ifdef FEAT_AUTOCMD
83static void ex_autocmd __ARGS((exarg_T *eap));
84static void ex_doautocmd __ARGS((exarg_T *eap));
85#else
86# define ex_autocmd ex_ni
87# define ex_doautocmd ex_ni
88# define ex_doautoall ex_ni
89#endif
90#ifdef FEAT_LISTCMDS
91static void ex_bunload __ARGS((exarg_T *eap));
92static void ex_buffer __ARGS((exarg_T *eap));
93static void ex_bmodified __ARGS((exarg_T *eap));
94static void ex_bnext __ARGS((exarg_T *eap));
95static void ex_bprevious __ARGS((exarg_T *eap));
96static void ex_brewind __ARGS((exarg_T *eap));
97static void ex_blast __ARGS((exarg_T *eap));
98#else
99# define ex_bunload ex_ni
100# define ex_buffer ex_ni
101# define ex_bmodified ex_ni
102# define ex_bnext ex_ni
103# define ex_bprevious ex_ni
104# define ex_brewind ex_ni
105# define ex_blast ex_ni
106# define buflist_list ex_ni
107# define ex_checktime ex_ni
108#endif
109#if !defined(FEAT_LISTCMDS) || !defined(FEAT_WINDOWS)
110# define ex_buffer_all ex_ni
111#endif
112static char_u *getargcmd __ARGS((char_u **));
113static char_u *skip_cmd_arg __ARGS((char_u *p, int rembs));
114static int getargopt __ARGS((exarg_T *eap));
115#ifndef FEAT_QUICKFIX
116# define ex_make ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +0000117# define ex_cbuffer ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000118# define ex_cc ex_ni
119# define ex_cnext ex_ni
120# define ex_cfile ex_ni
121# define qf_list ex_ni
122# define qf_age ex_ni
123# define ex_helpgrep ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +0000124# define ex_vimgrep ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000125#endif
126#if !defined(FEAT_QUICKFIX) || !defined(FEAT_WINDOWS)
127# define ex_cclose ex_ni
128# define ex_copen ex_ni
129# define ex_cwindow ex_ni
130#endif
Bram Moolenaar1e015462005-09-25 22:16:38 +0000131#if !defined(FEAT_QUICKFIX) || !defined(FEAT_EVAL)
132# define ex_cexpr ex_ni
133#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000134
135static int check_more __ARGS((int, int));
136static linenr_T get_address __ARGS((char_u **, int skip, int to_other_file));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000137static void get_flags __ARGS((exarg_T *eap));
Bram Moolenaar85363ab2010-07-18 13:58:26 +0200138#if !defined(FEAT_PERL) \
139 || !defined(FEAT_PYTHON) || !defined(FEAT_PYTHON3) \
140 || !defined(FEAT_TCL) \
141 || !defined(FEAT_RUBY) \
142 || !defined(FEAT_LUA) \
143 || !defined(FEAT_MZSCHEME)
Bram Moolenaar7bb75552007-07-16 18:39:49 +0000144# define HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +0000145static void ex_script_ni __ARGS((exarg_T *eap));
146#endif
147static char_u *invalid_range __ARGS((exarg_T *eap));
148static void correct_range __ARGS((exarg_T *eap));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000149#ifdef FEAT_QUICKFIX
150static char_u *replace_makeprg __ARGS((exarg_T *eap, char_u *p, char_u **cmdlinep));
151#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000152static char_u *repl_cmdline __ARGS((exarg_T *eap, char_u *src, int srclen, char_u *repl, char_u **cmdlinep));
153static void ex_highlight __ARGS((exarg_T *eap));
154static void ex_colorscheme __ARGS((exarg_T *eap));
155static void ex_quit __ARGS((exarg_T *eap));
156static void ex_cquit __ARGS((exarg_T *eap));
157static void ex_quit_all __ARGS((exarg_T *eap));
158#ifdef FEAT_WINDOWS
159static void ex_close __ARGS((exarg_T *eap));
Bram Moolenaarf740b292006-02-16 22:11:02 +0000160static void ex_win_close __ARGS((int forceit, win_T *win, tabpage_T *tp));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000161static void ex_only __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162static void ex_resize __ARGS((exarg_T *eap));
163static void ex_stag __ARGS((exarg_T *eap));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000164static void ex_tabclose __ARGS((exarg_T *eap));
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000165static void ex_tabonly __ARGS((exarg_T *eap));
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000166static void ex_tabnext __ARGS((exarg_T *eap));
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000167static void ex_tabmove __ARGS((exarg_T *eap));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000168static void ex_tabs __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000169#else
170# define ex_close ex_ni
171# define ex_only ex_ni
172# define ex_all ex_ni
173# define ex_resize ex_ni
174# define ex_splitview ex_ni
175# define ex_stag ex_ni
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000176# define ex_tabnext ex_ni
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000177# define ex_tabmove ex_ni
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000178# define ex_tabs ex_ni
179# define ex_tabclose ex_ni
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000180# define ex_tabonly ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000181#endif
182#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
183static void ex_pclose __ARGS((exarg_T *eap));
184static void ex_ptag __ARGS((exarg_T *eap));
185static void ex_pedit __ARGS((exarg_T *eap));
186#else
187# define ex_pclose ex_ni
188# define ex_ptag ex_ni
189# define ex_pedit ex_ni
190#endif
191static void ex_hide __ARGS((exarg_T *eap));
192static void ex_stop __ARGS((exarg_T *eap));
193static void ex_exit __ARGS((exarg_T *eap));
194static void ex_print __ARGS((exarg_T *eap));
195#ifdef FEAT_BYTEOFF
196static void ex_goto __ARGS((exarg_T *eap));
197#else
198# define ex_goto ex_ni
199#endif
200static void ex_shell __ARGS((exarg_T *eap));
201static void ex_preserve __ARGS((exarg_T *eap));
202static void ex_recover __ARGS((exarg_T *eap));
203#ifndef FEAT_LISTCMDS
204# define ex_argedit ex_ni
205# define ex_argadd ex_ni
206# define ex_argdelete ex_ni
207# define ex_listdo ex_ni
208#endif
209static void ex_mode __ARGS((exarg_T *eap));
210static void ex_wrongmodifier __ARGS((exarg_T *eap));
211static void ex_find __ARGS((exarg_T *eap));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000212static void ex_open __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000213static void ex_edit __ARGS((exarg_T *eap));
214#if !defined(FEAT_GUI) && !defined(FEAT_CLIENTSERVER)
215# define ex_drop ex_ni
216#endif
217#ifndef FEAT_GUI
218# define ex_gui ex_nogui
219static void ex_nogui __ARGS((exarg_T *eap));
220#endif
221#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
222static void ex_tearoff __ARGS((exarg_T *eap));
223#else
224# define ex_tearoff ex_ni
225#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000226#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000227static void ex_popup __ARGS((exarg_T *eap));
228#else
229# define ex_popup ex_ni
230#endif
231#ifndef FEAT_GUI_MSWIN
232# define ex_simalt ex_ni
233#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000234#if !defined(FEAT_GUI_MSWIN) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000235# define gui_mch_find_dialog ex_ni
236# define gui_mch_replace_dialog ex_ni
237#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000238#if !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000239# define ex_helpfind ex_ni
240#endif
241#ifndef FEAT_CSCOPE
242# define do_cscope ex_ni
243# define do_scscope ex_ni
244# define do_cstag ex_ni
245#endif
246#ifndef FEAT_SYN_HL
247# define ex_syntax ex_ni
Bram Moolenaar860cae12010-06-05 23:22:07 +0200248# define ex_ownsyntax ex_ni
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000249#endif
Bram Moolenaarf7512552013-06-06 14:55:19 +0200250#if !defined(FEAT_SYN_HL) || !defined(FEAT_PROFILE)
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +0200251# define ex_syntime ex_ni
252#endif
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000253#ifndef FEAT_SPELL
Bram Moolenaarb765d632005-06-07 21:00:02 +0000254# define ex_spell ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000255# define ex_mkspell ex_ni
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000256# define ex_spelldump ex_ni
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000257# define ex_spellinfo ex_ni
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000258# define ex_spellrepall ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000259#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200260#ifndef FEAT_PERSISTENT_UNDO
261# define ex_rundo ex_ni
262# define ex_wundo ex_ni
263#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200264#ifndef FEAT_LUA
265# define ex_lua ex_script_ni
266# define ex_luado ex_ni
267# define ex_luafile ex_ni
268#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000269#ifndef FEAT_MZSCHEME
270# define ex_mzscheme ex_script_ni
271# define ex_mzfile ex_ni
272#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000273#ifndef FEAT_PERL
274# define ex_perl ex_script_ni
275# define ex_perldo ex_ni
276#endif
277#ifndef FEAT_PYTHON
278# define ex_python ex_script_ni
Bram Moolenaard620aa92013-05-17 16:40:06 +0200279# define ex_pydo ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000280# define ex_pyfile ex_ni
281#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200282#ifndef FEAT_PYTHON3
Bram Moolenaar368373e2010-07-19 20:46:22 +0200283# define ex_py3 ex_script_ni
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200284# define ex_py3do ex_ni
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200285# define ex_py3file ex_ni
286#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000287#ifndef FEAT_TCL
288# define ex_tcl ex_script_ni
289# define ex_tcldo ex_ni
290# define ex_tclfile ex_ni
291#endif
292#ifndef FEAT_RUBY
293# define ex_ruby ex_script_ni
294# define ex_rubydo ex_ni
295# define ex_rubyfile ex_ni
296#endif
297#ifndef FEAT_SNIFF
298# define ex_sniff ex_ni
299#endif
300#ifndef FEAT_KEYMAP
301# define ex_loadkeymap ex_ni
302#endif
303static void ex_swapname __ARGS((exarg_T *eap));
304static void ex_syncbind __ARGS((exarg_T *eap));
305static void ex_read __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000306static void ex_pwd __ARGS((exarg_T *eap));
307static void ex_equal __ARGS((exarg_T *eap));
308static void ex_sleep __ARGS((exarg_T *eap));
309static void do_exmap __ARGS((exarg_T *eap, int isabbrev));
310static void ex_winsize __ARGS((exarg_T *eap));
311#ifdef FEAT_WINDOWS
312static void ex_wincmd __ARGS((exarg_T *eap));
313#else
314# define ex_wincmd ex_ni
315#endif
Bram Moolenaar843ee412004-06-30 16:16:41 +0000316#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000317static void ex_winpos __ARGS((exarg_T *eap));
318#else
319# define ex_winpos ex_ni
320#endif
321static void ex_operators __ARGS((exarg_T *eap));
322static void ex_put __ARGS((exarg_T *eap));
323static void ex_copymove __ARGS((exarg_T *eap));
324static void ex_submagic __ARGS((exarg_T *eap));
325static void ex_join __ARGS((exarg_T *eap));
326static void ex_at __ARGS((exarg_T *eap));
327static void ex_bang __ARGS((exarg_T *eap));
328static void ex_undo __ARGS((exarg_T *eap));
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200329#ifdef FEAT_PERSISTENT_UNDO
330static void ex_wundo __ARGS((exarg_T *eap));
331static void ex_rundo __ARGS((exarg_T *eap));
332#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000333static void ex_redo __ARGS((exarg_T *eap));
Bram Moolenaarc7d89352006-03-14 22:53:34 +0000334static void ex_later __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000335static void ex_redir __ARGS((exarg_T *eap));
336static void ex_redraw __ARGS((exarg_T *eap));
337static void ex_redrawstatus __ARGS((exarg_T *eap));
338static void close_redir __ARGS((void));
339static void ex_mkrc __ARGS((exarg_T *eap));
340static void ex_mark __ARGS((exarg_T *eap));
341#ifdef FEAT_USR_CMDS
342static char_u *uc_fun_cmd __ARGS((void));
Bram Moolenaar52b4b552005-03-07 23:00:57 +0000343static 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 +0000344#endif
345#ifdef FEAT_EX_EXTRA
346static void ex_normal __ARGS((exarg_T *eap));
347static void ex_startinsert __ARGS((exarg_T *eap));
348static void ex_stopinsert __ARGS((exarg_T *eap));
349#else
350# define ex_normal ex_ni
351# define ex_align ex_ni
352# define ex_retab ex_ni
353# define ex_startinsert ex_ni
354# define ex_stopinsert ex_ni
355# define ex_helptags ex_ni
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +0000356# define ex_sort ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000357#endif
358#ifdef FEAT_FIND_ID
359static void ex_checkpath __ARGS((exarg_T *eap));
360static void ex_findpat __ARGS((exarg_T *eap));
361#else
362# define ex_findpat ex_ni
363# define ex_checkpath ex_ni
364#endif
365#if defined(FEAT_FIND_ID) && defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
366static void ex_psearch __ARGS((exarg_T *eap));
367#else
368# define ex_psearch ex_ni
369#endif
370static void ex_tag __ARGS((exarg_T *eap));
371static void ex_tag_cmd __ARGS((exarg_T *eap, char_u *name));
372#ifndef FEAT_EVAL
373# define ex_scriptnames ex_ni
374# define ex_finish ex_ni
375# define ex_echo ex_ni
376# define ex_echohl ex_ni
377# define ex_execute ex_ni
378# define ex_call ex_ni
379# define ex_if ex_ni
380# define ex_endif ex_ni
381# define ex_else ex_ni
382# define ex_while ex_ni
383# define ex_continue ex_ni
384# define ex_break ex_ni
385# define ex_endwhile ex_ni
386# define ex_throw ex_ni
387# define ex_try ex_ni
388# define ex_catch ex_ni
389# define ex_finally ex_ni
390# define ex_endtry ex_ni
391# define ex_endfunction ex_ni
392# define ex_let ex_ni
393# define ex_unlet ex_ni
Bram Moolenaar65c1b012005-01-31 19:02:28 +0000394# define ex_lockvar ex_ni
395# define ex_unlockvar ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000396# define ex_function ex_ni
397# define ex_delfunction ex_ni
398# define ex_return ex_ni
Bram Moolenaard812df62008-11-09 12:46:09 +0000399# define ex_oldfiles ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000400#endif
401static char_u *arg_all __ARGS((void));
402#ifdef FEAT_SESSION
403static int makeopens __ARGS((FILE *fd, char_u *dirnow));
Bram Moolenaarf13be0d2008-01-02 14:13:10 +0000404static 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 +0000405static void ex_loadview __ARGS((exarg_T *eap));
406static char_u *get_view_file __ARGS((int c));
Bram Moolenaareeefcc72007-05-01 21:21:21 +0000407static int did_lcd; /* whether ":lcd" was produced for a session */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000408#else
409# define ex_loadview ex_ni
410#endif
411#ifndef FEAT_EVAL
412# define ex_compiler ex_ni
413#endif
414#ifdef FEAT_VIMINFO
415static void ex_viminfo __ARGS((exarg_T *eap));
416#else
417# define ex_viminfo ex_ni
418#endif
419static void ex_behave __ARGS((exarg_T *eap));
420#ifdef FEAT_AUTOCMD
421static void ex_filetype __ARGS((exarg_T *eap));
422static void ex_setfiletype __ARGS((exarg_T *eap));
423#else
424# define ex_filetype ex_ni
425# define ex_setfiletype ex_ni
426#endif
427#ifndef FEAT_DIFF
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000428# define ex_diffoff ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000429# define ex_diffpatch ex_ni
430# define ex_diffgetput ex_ni
431# define ex_diffsplit ex_ni
432# define ex_diffthis ex_ni
433# define ex_diffupdate ex_ni
434#endif
435static void ex_digraphs __ARGS((exarg_T *eap));
436static void ex_set __ARGS((exarg_T *eap));
437#if !defined(FEAT_EVAL) || !defined(FEAT_AUTOCMD)
438# define ex_options ex_ni
439#endif
440#ifdef FEAT_SEARCH_EXTRA
441static void ex_nohlsearch __ARGS((exarg_T *eap));
442static void ex_match __ARGS((exarg_T *eap));
443#else
444# define ex_nohlsearch ex_ni
445# define ex_match ex_ni
446#endif
447#ifdef FEAT_CRYPT
448static void ex_X __ARGS((exarg_T *eap));
449#else
450# define ex_X ex_ni
451#endif
452#ifdef FEAT_FOLDING
453static void ex_fold __ARGS((exarg_T *eap));
454static void ex_foldopen __ARGS((exarg_T *eap));
455static void ex_folddo __ARGS((exarg_T *eap));
456#else
457# define ex_fold ex_ni
458# define ex_foldopen ex_ni
459# define ex_folddo ex_ni
460#endif
461#if !((defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
462 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE)))
463# define ex_language ex_ni
464#endif
465#ifndef FEAT_SIGNS
466# define ex_sign ex_ni
467#endif
468#ifndef FEAT_SUN_WORKSHOP
469# define ex_wsverb ex_ni
470#endif
Bram Moolenaar009b2592004-10-24 19:18:58 +0000471#ifndef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200472# define ex_nbclose ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000473# define ex_nbkey ex_ni
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200474# define ex_nbstart ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000475#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000476
477#ifndef FEAT_EVAL
478# define ex_debug ex_ni
479# define ex_breakadd ex_ni
480# define ex_debuggreedy ex_ni
481# define ex_breakdel ex_ni
482# define ex_breaklist ex_ni
483#endif
484
485#ifndef FEAT_CMDHIST
486# define ex_history ex_ni
487#endif
488#ifndef FEAT_JUMPLIST
489# define ex_jumps ex_ni
490# define ex_changes ex_ni
491#endif
492
Bram Moolenaar05159a02005-02-26 23:04:13 +0000493#ifndef FEAT_PROFILE
494# define ex_profile ex_ni
495#endif
496
Bram Moolenaar071d4272004-06-13 20:20:40 +0000497/*
498 * Declare cmdnames[].
499 */
500#define DO_DECLARE_EXCMD
501#include "ex_cmds.h"
502
503/*
504 * Table used to quickly search for a command, based on its first character.
505 */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +0000506static cmdidx_T cmdidxs[27] =
Bram Moolenaar071d4272004-06-13 20:20:40 +0000507{
508 CMD_append,
509 CMD_buffer,
510 CMD_change,
511 CMD_delete,
512 CMD_edit,
513 CMD_file,
514 CMD_global,
515 CMD_help,
516 CMD_insert,
517 CMD_join,
518 CMD_k,
519 CMD_list,
520 CMD_move,
521 CMD_next,
522 CMD_open,
523 CMD_print,
524 CMD_quit,
525 CMD_read,
526 CMD_substitute,
527 CMD_t,
528 CMD_undo,
529 CMD_vglobal,
530 CMD_write,
531 CMD_xit,
532 CMD_yank,
533 CMD_z,
534 CMD_bang
535};
536
537static char_u dollar_command[2] = {'$', 0};
538
539
540#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000541/* Struct for storing a line inside a while/for loop */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000542typedef struct
543{
544 char_u *line; /* command line */
545 linenr_T lnum; /* sourcing_lnum of the line */
546} wcmd_T;
547
548/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000549 * Structure used to store info for line position in a while or for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000550 * This is required, because do_one_cmd() may invoke ex_function(), which
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000551 * reads more lines that may come from the while/for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000552 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000553struct loop_cookie
Bram Moolenaar071d4272004-06-13 20:20:40 +0000554{
555 garray_T *lines_gap; /* growarray with line info */
556 int current_line; /* last read line from growarray */
557 int repeating; /* TRUE when looping a second time */
558 /* When "repeating" is FALSE use "getline" and "cookie" to get lines */
559 char_u *(*getline) __ARGS((int, void *, int));
560 void *cookie;
561};
562
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000563static char_u *get_loop_line __ARGS((int c, void *cookie, int indent));
564static int store_loop_line __ARGS((garray_T *gap, char_u *line));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000565static void free_cmdlines __ARGS((garray_T *gap));
Bram Moolenaared203462004-06-16 11:19:22 +0000566
567/* Struct to save a few things while debugging. Used in do_cmdline() only. */
568struct dbg_stuff
569{
570 int trylevel;
571 int force_abort;
572 except_T *caught_stack;
573 char_u *vv_exception;
574 char_u *vv_throwpoint;
575 int did_emsg;
576 int got_int;
577 int did_throw;
578 int need_rethrow;
579 int check_cstack;
580 except_T *current_exception;
581};
582
583static void save_dbg_stuff __ARGS((struct dbg_stuff *dsp));
584static void restore_dbg_stuff __ARGS((struct dbg_stuff *dsp));
585
586 static void
587save_dbg_stuff(dsp)
588 struct dbg_stuff *dsp;
589{
590 dsp->trylevel = trylevel; trylevel = 0;
591 dsp->force_abort = force_abort; force_abort = FALSE;
592 dsp->caught_stack = caught_stack; caught_stack = NULL;
593 dsp->vv_exception = v_exception(NULL);
594 dsp->vv_throwpoint = v_throwpoint(NULL);
595
596 /* Necessary for debugging an inactive ":catch", ":finally", ":endtry" */
597 dsp->did_emsg = did_emsg; did_emsg = FALSE;
598 dsp->got_int = got_int; got_int = FALSE;
599 dsp->did_throw = did_throw; did_throw = FALSE;
600 dsp->need_rethrow = need_rethrow; need_rethrow = FALSE;
601 dsp->check_cstack = check_cstack; check_cstack = FALSE;
602 dsp->current_exception = current_exception; current_exception = NULL;
603}
604
605 static void
606restore_dbg_stuff(dsp)
607 struct dbg_stuff *dsp;
608{
609 suppress_errthrow = FALSE;
610 trylevel = dsp->trylevel;
611 force_abort = dsp->force_abort;
612 caught_stack = dsp->caught_stack;
613 (void)v_exception(dsp->vv_exception);
614 (void)v_throwpoint(dsp->vv_throwpoint);
615 did_emsg = dsp->did_emsg;
616 got_int = dsp->got_int;
617 did_throw = dsp->did_throw;
618 need_rethrow = dsp->need_rethrow;
619 check_cstack = dsp->check_cstack;
620 current_exception = dsp->current_exception;
621}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000622#endif
623
624
625/*
626 * do_exmode(): Repeatedly get commands for the "Ex" mode, until the ":vi"
627 * command is given.
628 */
629 void
630do_exmode(improved)
631 int improved; /* TRUE for "improved Ex" mode */
632{
633 int save_msg_scroll;
634 int prev_msg_row;
635 linenr_T prev_line;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000636 int changedtick;
637
638 if (improved)
639 exmode_active = EXMODE_VIM;
640 else
641 exmode_active = EXMODE_NORMAL;
642 State = NORMAL;
643
644 /* When using ":global /pat/ visual" and then "Q" we return to continue
645 * the :global command. */
646 if (global_busy)
647 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000648
649 save_msg_scroll = msg_scroll;
650 ++RedrawingDisabled; /* don't redisplay the window */
651 ++no_wait_return; /* don't wait for return */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000652#ifdef FEAT_GUI
653 /* Ignore scrollbar and mouse events in Ex mode */
654 ++hold_gui_events;
655#endif
656#ifdef FEAT_SNIFF
657 want_sniff_request = 0; /* No K_SNIFF wanted */
658#endif
659
660 MSG(_("Entering Ex mode. Type \"visual\" to go to Normal mode."));
661 while (exmode_active)
662 {
Bram Moolenaar7c626922005-02-07 22:01:03 +0000663#ifdef FEAT_EX_EXTRA
664 /* Check for a ":normal" command and no more characters left. */
665 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
666 {
667 exmode_active = FALSE;
668 break;
669 }
670#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000671 msg_scroll = TRUE;
672 need_wait_return = FALSE;
673 ex_pressedreturn = FALSE;
674 ex_no_reprint = FALSE;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000675 changedtick = curbuf->b_changedtick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000676 prev_msg_row = msg_row;
677 prev_line = curwin->w_cursor.lnum;
678#ifdef FEAT_SNIFF
679 ProcessSniffRequests();
680#endif
681 if (improved)
682 {
683 cmdline_row = msg_row;
684 do_cmdline(NULL, getexline, NULL, 0);
685 }
686 else
687 do_cmdline(NULL, getexmodeline, NULL, DOCMD_NOWAIT);
688 lines_left = Rows - 1;
689
Bram Moolenaardf177f62005-02-22 08:39:57 +0000690 if ((prev_line != curwin->w_cursor.lnum
691 || changedtick != curbuf->b_changedtick) && !ex_no_reprint)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000693 if (curbuf->b_ml.ml_flags & ML_EMPTY)
694 EMSG(_(e_emptybuf));
695 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000696 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000697 if (ex_pressedreturn)
698 {
699 /* go up one line, to overwrite the ":<CR>" line, so the
Bram Moolenaar81870892007-11-11 18:17:28 +0000700 * output doesn't contain empty lines. */
Bram Moolenaardf177f62005-02-22 08:39:57 +0000701 msg_row = prev_msg_row;
702 if (prev_msg_row == Rows - 1)
703 msg_row--;
704 }
705 msg_col = 0;
706 print_line_no_prefix(curwin->w_cursor.lnum, FALSE, FALSE);
707 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709 }
Bram Moolenaardf177f62005-02-22 08:39:57 +0000710 else if (ex_pressedreturn && !ex_no_reprint) /* must be at EOF */
711 {
712 if (curbuf->b_ml.ml_flags & ML_EMPTY)
713 EMSG(_(e_emptybuf));
714 else
715 EMSG(_("E501: At end-of-file"));
716 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000717 }
718
719#ifdef FEAT_GUI
720 --hold_gui_events;
721#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000722 --RedrawingDisabled;
723 --no_wait_return;
724 update_screen(CLEAR);
725 need_wait_return = FALSE;
726 msg_scroll = save_msg_scroll;
727}
728
729/*
730 * Execute a simple command line. Used for translated commands like "*".
731 */
732 int
733do_cmdline_cmd(cmd)
734 char_u *cmd;
735{
736 return do_cmdline(cmd, NULL, NULL,
737 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
738}
739
740/*
741 * do_cmdline(): execute one Ex command line
742 *
743 * 1. Execute "cmdline" when it is not NULL.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100744 * If "cmdline" is NULL, or more lines are needed, fgetline() is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000745 * 2. Split up in parts separated with '|'.
746 *
747 * This function can be called recursively!
748 *
749 * flags:
750 * DOCMD_VERBOSE - The command will be included in the error message.
751 * DOCMD_NOWAIT - Don't call wait_return() and friends.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100752 * DOCMD_REPEAT - Repeat execution until fgetline() returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753 * DOCMD_KEYTYPED - Don't reset KeyTyped.
754 * DOCMD_EXCRESET - Reset the exception environment (used for debugging).
755 * DOCMD_KEEPLINE - Store first typed line (for repeating with ".").
756 *
757 * return FAIL if cmdline could not be executed, OK otherwise
758 */
759 int
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100760do_cmdline(cmdline, fgetline, cookie, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761 char_u *cmdline;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100762 char_u *(*fgetline) __ARGS((int, void *, int));
763 void *cookie; /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000764 int flags;
765{
766 char_u *next_cmdline; /* next cmd to execute */
767 char_u *cmdline_copy = NULL; /* copy of cmd line */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100768 int used_getline = FALSE; /* used "fgetline" to obtain command */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000769 static int recursive = 0; /* recursive depth */
770 int msg_didout_before_start = 0;
771 int count = 0; /* line number count */
772 int did_inc = FALSE; /* incremented RedrawingDisabled */
773 int retval = OK;
774#ifdef FEAT_EVAL
775 struct condstack cstack; /* conditional stack */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000776 garray_T lines_ga; /* keep lines for ":while"/":for" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000777 int current_line = 0; /* active line in lines_ga */
778 char_u *fname = NULL; /* function or script name */
779 linenr_T *breakpoint = NULL; /* ptr to breakpoint field in cookie */
780 int *dbg_tick = NULL; /* ptr to dbg_tick field in cookie */
Bram Moolenaared203462004-06-16 11:19:22 +0000781 struct dbg_stuff debug_saved; /* saved things for debug mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782 int initial_trylevel;
783 struct msglist **saved_msg_list = NULL;
784 struct msglist *private_msg_list;
785
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100786 /* "fgetline" and "cookie" passed to do_one_cmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787 char_u *(*cmd_getline) __ARGS((int, void *, int));
788 void *cmd_cookie;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000789 struct loop_cookie cmd_loop_cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790 void *real_cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000791 int getline_is_func;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792#else
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100793# define cmd_getline fgetline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794# define cmd_cookie cookie
795#endif
796 static int call_depth = 0; /* recursiveness */
797
798#ifdef FEAT_EVAL
799 /* For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory
800 * location for storing error messages to be converted to an exception.
Bram Moolenaarcf3630f2005-01-08 16:04:29 +0000801 * This ensures that the do_errthrow() call in do_one_cmd() does not
802 * combine the messages stored by an earlier invocation of do_one_cmd()
803 * with the command name of the later one. This would happen when
804 * BufWritePost autocommands are executed after a write error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805 saved_msg_list = msg_list;
806 msg_list = &private_msg_list;
807 private_msg_list = NULL;
808#endif
809
810 /* It's possible to create an endless loop with ":execute", catch that
811 * here. The value of 200 allows nested function calls, ":source", etc. */
812 if (call_depth == 200)
813 {
814 EMSG(_("E169: Command too recursive"));
815#ifdef FEAT_EVAL
816 /* When converting to an exception, we do not include the command name
817 * since this is not an error of the specific command. */
818 do_errthrow((struct condstack *)NULL, (char_u *)NULL);
819 msg_list = saved_msg_list;
820#endif
821 return FAIL;
822 }
823 ++call_depth;
824
825#ifdef FEAT_EVAL
826 cstack.cs_idx = -1;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000827 cstack.cs_looplevel = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000828 cstack.cs_trylevel = 0;
829 cstack.cs_emsg_silent_list = NULL;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000830 cstack.cs_lflags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000831 ga_init2(&lines_ga, (int)sizeof(wcmd_T), 10);
832
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100833 real_cookie = getline_cookie(fgetline, cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834
835 /* Inside a function use a higher nesting level. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100836 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000837 if (getline_is_func && ex_nesting_level == func_level(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000838 ++ex_nesting_level;
839
840 /* Get the function or script name and the address where the next breakpoint
841 * line and the debug tick for a function or script are stored. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000842 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000843 {
844 fname = func_name(real_cookie);
845 breakpoint = func_breakpoint(real_cookie);
846 dbg_tick = func_dbg_tick(real_cookie);
847 }
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100848 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849 {
850 fname = sourcing_name;
851 breakpoint = source_breakpoint(real_cookie);
852 dbg_tick = source_dbg_tick(real_cookie);
853 }
854
855 /*
856 * Initialize "force_abort" and "suppress_errthrow" at the top level.
857 */
858 if (!recursive)
859 {
860 force_abort = FALSE;
861 suppress_errthrow = FALSE;
862 }
863
864 /*
865 * If requested, store and reset the global values controlling the
Bram Moolenaar89d40322006-08-29 15:30:07 +0000866 * exception handling (used when debugging). Otherwise clear it to avoid
867 * a bogus compiler warning when the optimizer uses inline functions...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000868 */
Bram Moolenaarb4872942006-05-13 10:32:52 +0000869 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +0000870 save_dbg_stuff(&debug_saved);
Bram Moolenaar89d40322006-08-29 15:30:07 +0000871 else
Bram Moolenaar7db5fc82010-05-24 11:59:29 +0200872 vim_memset(&debug_saved, 0, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000873
874 initial_trylevel = trylevel;
875
876 /*
877 * "did_throw" will be set to TRUE when an exception is being thrown.
878 */
879 did_throw = FALSE;
880#endif
881 /*
882 * "did_emsg" will be set to TRUE when emsg() is used, in which case we
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000883 * cancel the whole command line, and any if/endif or loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000884 * If force_abort is set, we cancel everything.
885 */
886 did_emsg = FALSE;
887
888 /*
889 * KeyTyped is only set when calling vgetc(). Reset it here when not
890 * calling vgetc() (sourced command lines).
891 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100892 if (!(flags & DOCMD_KEYTYPED)
893 && !getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894 KeyTyped = FALSE;
895
896 /*
897 * Continue executing command lines:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000898 * - when inside an ":if", ":while" or ":for"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899 * - for multiple commands on one line, separated with '|'
900 * - when repeating until there are no more lines (for ":source")
901 */
902 next_cmdline = cmdline;
903 do
904 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000905#ifdef FEAT_EVAL
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100906 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000907#endif
908
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000909 /* stop skipping cmds for an error msg after all endif/while/for */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000910 if (next_cmdline == NULL
911#ifdef FEAT_EVAL
912 && !force_abort
913 && cstack.cs_idx < 0
Bram Moolenaar05159a02005-02-26 23:04:13 +0000914 && !(getline_is_func && func_has_abort(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000915#endif
916 )
917 did_emsg = FALSE;
918
919 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000920 * 1. If repeating a line in a loop, get a line from lines_ga.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100921 * 2. If no line given: Get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000922 * 3. If a line is given: Make a copy, so we can mess with it.
923 */
924
925#ifdef FEAT_EVAL
926 /* 1. If repeating, get a previous line from lines_ga. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000927 if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000928 {
929 /* Each '|' separated command is stored separately in lines_ga, to
930 * be able to jump to it. Don't use next_cmdline now. */
931 vim_free(cmdline_copy);
932 cmdline_copy = NULL;
933
934 /* Check if a function has returned or, unless it has an unclosed
935 * try conditional, aborted. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000936 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000938# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000939 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000940 func_line_end(real_cookie);
941# endif
942 if (func_has_ended(real_cookie))
943 {
944 retval = FAIL;
945 break;
946 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000947 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000948#ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000949 else if (do_profiling == PROF_YES
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100950 && getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000951 script_line_end();
952#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000953
954 /* Check if a sourced file hit a ":finish" command. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100955 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956 {
957 retval = FAIL;
958 break;
959 }
960
961 /* If breakpoints have been added/deleted need to check for it. */
962 if (breakpoint != NULL && dbg_tick != NULL
963 && *dbg_tick != debug_tick)
964 {
965 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100966 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967 fname, sourcing_lnum);
968 *dbg_tick = debug_tick;
969 }
970
971 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
972 sourcing_lnum = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
973
974 /* Did we encounter a breakpoint? */
975 if (breakpoint != NULL && *breakpoint != 0
976 && *breakpoint <= sourcing_lnum)
977 {
978 dbg_breakpoint(fname, sourcing_lnum);
979 /* Find next breakpoint. */
980 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100981 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000982 fname, sourcing_lnum);
983 *dbg_tick = debug_tick;
984 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000985# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000986 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000987 {
988 if (getline_is_func)
989 func_line_start(real_cookie);
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100990 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000991 script_line_start();
992 }
993# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994 }
995
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000996 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000998 /* Inside a while/for loop we need to store the lines and use them
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100999 * again. Pass a different "fgetline" function to do_one_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001000 * below, so that it stores lines in or reads them from
1001 * "lines_ga". Makes it possible to define a function inside a
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001002 * while/for loop. */
1003 cmd_getline = get_loop_line;
1004 cmd_cookie = (void *)&cmd_loop_cookie;
1005 cmd_loop_cookie.lines_gap = &lines_ga;
1006 cmd_loop_cookie.current_line = current_line;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001007 cmd_loop_cookie.getline = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001008 cmd_loop_cookie.cookie = cookie;
1009 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010 }
1011 else
1012 {
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001013 cmd_getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014 cmd_cookie = cookie;
1015 }
1016#endif
1017
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001018 /* 2. If no line given, get an allocated line with fgetline(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019 if (next_cmdline == NULL)
1020 {
1021 /*
1022 * Need to set msg_didout for the first line after an ":if",
1023 * otherwise the ":if" will be overwritten.
1024 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001025 if (count == 1 && getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026 msg_didout = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001027 if (fgetline == NULL || (next_cmdline = fgetline(':', cookie,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028#ifdef FEAT_EVAL
1029 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
1030#else
1031 0
1032#endif
1033 )) == NULL)
1034 {
1035 /* Don't call wait_return for aborted command line. The NULL
1036 * returned for the end of a sourced file or executed function
1037 * doesn't do this. */
1038 if (KeyTyped && !(flags & DOCMD_REPEAT))
1039 need_wait_return = FALSE;
1040 retval = FAIL;
1041 break;
1042 }
1043 used_getline = TRUE;
1044
1045 /*
1046 * Keep the first typed line. Clear it when more lines are typed.
1047 */
1048 if (flags & DOCMD_KEEPLINE)
1049 {
1050 vim_free(repeat_cmdline);
1051 if (count == 0)
1052 repeat_cmdline = vim_strsave(next_cmdline);
1053 else
1054 repeat_cmdline = NULL;
1055 }
1056 }
1057
1058 /* 3. Make a copy of the command so we can mess with it. */
1059 else if (cmdline_copy == NULL)
1060 {
1061 next_cmdline = vim_strsave(next_cmdline);
1062 if (next_cmdline == NULL)
1063 {
1064 EMSG(_(e_outofmem));
1065 retval = FAIL;
1066 break;
1067 }
1068 }
1069 cmdline_copy = next_cmdline;
1070
1071#ifdef FEAT_EVAL
1072 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001073 * Save the current line when inside a ":while" or ":for", and when
1074 * the command looks like a ":while" or ":for", because we may need it
1075 * later. When there is a '|' and another command, it is stored
1076 * separately, because we need to be able to jump back to it from an
1077 * :endwhile/:endfor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001079 if (current_line == lines_ga.ga_len
1080 && (cstack.cs_looplevel || has_loop_cmd(next_cmdline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001082 if (store_loop_line(&lines_ga, next_cmdline) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083 {
1084 retval = FAIL;
1085 break;
1086 }
1087 }
1088 did_endif = FALSE;
1089#endif
1090
1091 if (count++ == 0)
1092 {
1093 /*
1094 * All output from the commands is put below each other, without
1095 * waiting for a return. Don't do this when executing commands
1096 * from a script or when being called recursive (e.g. for ":e
1097 * +command file").
1098 */
1099 if (!(flags & DOCMD_NOWAIT) && !recursive)
1100 {
1101 msg_didout_before_start = msg_didout;
1102 msg_didany = FALSE; /* no output yet */
1103 msg_start();
1104 msg_scroll = TRUE; /* put messages below each other */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001105 ++no_wait_return; /* don't wait for return until finished */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001106 ++RedrawingDisabled;
1107 did_inc = TRUE;
1108 }
1109 }
1110
1111 if (p_verbose >= 15 && sourcing_name != NULL)
1112 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113 ++no_wait_return;
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001114 verbose_enter_scroll();
1115
Bram Moolenaar071d4272004-06-13 20:20:40 +00001116 smsg((char_u *)_("line %ld: %s"),
1117 (long)sourcing_lnum, cmdline_copy);
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001118 if (msg_silent == 0)
1119 msg_puts((char_u *)"\n"); /* don't overwrite this */
1120
1121 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001122 --no_wait_return;
1123 }
1124
1125 /*
1126 * 2. Execute one '|' separated command.
1127 * do_one_cmd() will return NULL if there is no trailing '|'.
1128 * "cmdline_copy" can change, e.g. for '%' and '#' expansion.
1129 */
1130 ++recursive;
1131 next_cmdline = do_one_cmd(&cmdline_copy, flags & DOCMD_VERBOSE,
1132#ifdef FEAT_EVAL
1133 &cstack,
1134#endif
1135 cmd_getline, cmd_cookie);
1136 --recursive;
1137
1138#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001139 if (cmd_cookie == (void *)&cmd_loop_cookie)
1140 /* Use "current_line" from "cmd_loop_cookie", it may have been
Bram Moolenaar071d4272004-06-13 20:20:40 +00001141 * incremented when defining a function. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001142 current_line = cmd_loop_cookie.current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001143#endif
1144
1145 if (next_cmdline == NULL)
1146 {
1147 vim_free(cmdline_copy);
1148 cmdline_copy = NULL;
1149#ifdef FEAT_CMDHIST
1150 /*
1151 * If the command was typed, remember it for the ':' register.
1152 * Do this AFTER executing the command to make :@: work.
1153 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001154 if (getline_equal(fgetline, cookie, getexline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001155 && new_last_cmdline != NULL)
1156 {
1157 vim_free(last_cmdline);
1158 last_cmdline = new_last_cmdline;
1159 new_last_cmdline = NULL;
1160 }
1161#endif
1162 }
1163 else
1164 {
1165 /* need to copy the command after the '|' to cmdline_copy, for the
1166 * next do_one_cmd() */
Bram Moolenaara7241f52008-06-24 20:39:31 +00001167 STRMOVE(cmdline_copy, next_cmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001168 next_cmdline = cmdline_copy;
1169 }
1170
1171
1172#ifdef FEAT_EVAL
1173 /* reset did_emsg for a function that is not aborted by an error */
1174 if (did_emsg && !force_abort
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001175 && getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001176 && !func_has_abort(real_cookie))
1177 did_emsg = FALSE;
1178
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001179 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180 {
1181 ++current_line;
1182
1183 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001184 * An ":endwhile", ":endfor" and ":continue" is handled here.
1185 * If we were executing commands, jump back to the ":while" or
1186 * ":for".
1187 * If we were not executing commands, decrement cs_looplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001189 if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001191 cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001193 /* Jump back to the matching ":while" or ":for". Be careful
1194 * not to use a cs_line[] from an entry that isn't a ":while"
1195 * or ":for": It would make "current_line" invalid and can
1196 * cause a crash. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197 if (!did_emsg && !got_int && !did_throw
1198 && cstack.cs_idx >= 0
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001199 && (cstack.cs_flags[cstack.cs_idx]
1200 & (CSF_WHILE | CSF_FOR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201 && cstack.cs_line[cstack.cs_idx] >= 0
1202 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
1203 {
1204 current_line = cstack.cs_line[cstack.cs_idx];
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001205 /* remember we jumped there */
1206 cstack.cs_lflags |= CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207 line_breakcheck(); /* check if CTRL-C typed */
1208
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001209 /* Check for the next breakpoint at or after the ":while"
1210 * or ":for". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211 if (breakpoint != NULL)
1212 {
1213 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001214 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215 fname,
1216 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
1217 *dbg_tick = debug_tick;
1218 }
1219 }
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001220 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001222 /* can only get here with ":endwhile" or ":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223 if (cstack.cs_idx >= 0)
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001224 rewind_conditionals(&cstack, cstack.cs_idx - 1,
1225 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226 }
1227 }
1228
1229 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001230 * For a ":while" or ":for" we need to remember the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001231 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001232 else if (cstack.cs_lflags & CSL_HAD_LOOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001234 cstack.cs_lflags &= ~CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235 cstack.cs_line[cstack.cs_idx] = current_line - 1;
1236 }
1237 }
1238
1239 /*
1240 * When not inside any ":while" loop, clear remembered lines.
1241 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001242 if (cstack.cs_looplevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243 {
1244 if (lines_ga.ga_len > 0)
1245 {
1246 sourcing_lnum =
1247 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
1248 free_cmdlines(&lines_ga);
1249 }
1250 current_line = 0;
1251 }
1252
1253 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001254 * A ":finally" makes did_emsg, got_int, and did_throw pending for
1255 * being restored at the ":endtry". Reset them here and set the
1256 * ACTIVE and FINALLY flags, so that the finally clause gets executed.
1257 * This includes the case where a missing ":endif", ":endwhile" or
1258 * ":endfor" was detected by the ":finally" itself.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001259 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001260 if (cstack.cs_lflags & CSL_HAD_FINA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001262 cstack.cs_lflags &= ~CSL_HAD_FINA;
1263 report_make_pending(cstack.cs_pending[cstack.cs_idx]
1264 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265 did_throw ? (void *)current_exception : NULL);
1266 did_emsg = got_int = did_throw = FALSE;
1267 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
1268 }
1269
1270 /* Update global "trylevel" for recursive calls to do_cmdline() from
1271 * within this loop. */
1272 trylevel = initial_trylevel + cstack.cs_trylevel;
1273
1274 /*
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001275 * If the outermost try conditional (across function calls and sourced
Bram Moolenaar071d4272004-06-13 20:20:40 +00001276 * files) is aborted because of an error, an interrupt, or an uncaught
1277 * exception, cancel everything. If it is left normally, reset
1278 * force_abort to get the non-EH compatible abortion behavior for
1279 * the rest of the script.
1280 */
1281 if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
1282 force_abort = FALSE;
1283
1284 /* Convert an interrupt to an exception if appropriate. */
1285 (void)do_intthrow(&cstack);
1286#endif /* FEAT_EVAL */
1287
1288 }
1289 /*
1290 * Continue executing command lines when:
1291 * - no CTRL-C typed, no aborting error, no exception thrown or try
1292 * conditionals need to be checked for executing finally clauses or
1293 * catching an interrupt exception
1294 * - didn't get an error message or lines are not typed
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001295 * - there is a command after '|', inside a :if, :while, :for or :try, or
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296 * looping for ":source" command or function call.
1297 */
1298 while (!((got_int
1299#ifdef FEAT_EVAL
1300 || (did_emsg && force_abort) || did_throw
1301#endif
1302 )
1303#ifdef FEAT_EVAL
1304 && cstack.cs_trylevel == 0
1305#endif
1306 )
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001307 && !(did_emsg
1308#ifdef FEAT_EVAL
1309 /* Keep going when inside try/catch, so that the error can be
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001310 * deal with, except when it is a syntax error, it may cause
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001311 * the :endtry to be missed. */
1312 && (cstack.cs_trylevel == 0 || did_emsg_syntax)
1313#endif
1314 && used_getline
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001315 && (getline_equal(fgetline, cookie, getexmodeline)
1316 || getline_equal(fgetline, cookie, getexline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001317 && (next_cmdline != NULL
1318#ifdef FEAT_EVAL
1319 || cstack.cs_idx >= 0
1320#endif
1321 || (flags & DOCMD_REPEAT)));
1322
1323 vim_free(cmdline_copy);
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001324 did_emsg_syntax = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325#ifdef FEAT_EVAL
1326 free_cmdlines(&lines_ga);
1327 ga_clear(&lines_ga);
1328
1329 if (cstack.cs_idx >= 0)
1330 {
1331 /*
1332 * If a sourced file or executed function ran to its end, report the
1333 * unclosed conditional.
1334 */
1335 if (!got_int && !did_throw
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001336 && ((getline_equal(fgetline, cookie, getsourceline)
1337 && !source_finished(fgetline, cookie))
1338 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339 && !func_has_ended(real_cookie))))
1340 {
1341 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
1342 EMSG(_(e_endtry));
1343 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
1344 EMSG(_(e_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001345 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
1346 EMSG(_(e_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347 else
1348 EMSG(_(e_endif));
1349 }
1350
1351 /*
1352 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1353 * ":endtry" in a sourced file or executed function. If the try
1354 * conditional is in its finally clause, ignore anything pending.
1355 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001356 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357 */
1358 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001359 {
1360 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1361
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001362 if (idx >= 0)
1363 --idx; /* remove try block not in its finally clause */
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001364 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1365 &cstack.cs_looplevel);
1366 }
1367 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001368 trylevel = initial_trylevel;
1369 }
1370
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001371 /* If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1372 * lack was reported above and the error message is to be converted to an
Bram Moolenaar071d4272004-06-13 20:20:40 +00001373 * exception, do this now after rewinding the cstack. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001374 do_errthrow(&cstack, getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001375 ? (char_u *)"endfunction" : (char_u *)NULL);
1376
1377 if (trylevel == 0)
1378 {
1379 /*
1380 * When an exception is being thrown out of the outermost try
1381 * conditional, discard the uncaught exception, disable the conversion
1382 * of interrupts or errors to exceptions, and ensure that no more
1383 * commands are executed.
1384 */
1385 if (did_throw)
1386 {
1387 void *p = NULL;
1388 char_u *saved_sourcing_name;
1389 int saved_sourcing_lnum;
1390 struct msglist *messages = NULL, *next;
1391
1392 /*
1393 * If the uncaught exception is a user exception, report it as an
1394 * error. If it is an error exception, display the saved error
1395 * message now. For an interrupt exception, do nothing; the
1396 * interrupt message is given elsewhere.
1397 */
1398 switch (current_exception->type)
1399 {
1400 case ET_USER:
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001401 vim_snprintf((char *)IObuff, IOSIZE,
1402 _("E605: Exception not caught: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001403 current_exception->value);
1404 p = vim_strsave(IObuff);
1405 break;
1406 case ET_ERROR:
1407 messages = current_exception->messages;
1408 current_exception->messages = NULL;
1409 break;
1410 case ET_INTERRUPT:
1411 break;
1412 default:
1413 p = vim_strsave((char_u *)_(e_internal));
1414 }
1415
1416 saved_sourcing_name = sourcing_name;
1417 saved_sourcing_lnum = sourcing_lnum;
1418 sourcing_name = current_exception->throw_name;
1419 sourcing_lnum = current_exception->throw_lnum;
1420 current_exception->throw_name = NULL;
1421
1422 discard_current_exception(); /* uses IObuff if 'verbose' */
1423 suppress_errthrow = TRUE;
1424 force_abort = TRUE;
1425
1426 if (messages != NULL)
1427 {
1428 do
1429 {
1430 next = messages->next;
1431 emsg(messages->msg);
1432 vim_free(messages->msg);
1433 vim_free(messages);
1434 messages = next;
1435 }
1436 while (messages != NULL);
1437 }
1438 else if (p != NULL)
1439 {
1440 emsg(p);
1441 vim_free(p);
1442 }
1443 vim_free(sourcing_name);
1444 sourcing_name = saved_sourcing_name;
1445 sourcing_lnum = saved_sourcing_lnum;
1446 }
1447
1448 /*
1449 * On an interrupt or an aborting error not converted to an exception,
1450 * disable the conversion of errors to exceptions. (Interrupts are not
1451 * converted any more, here.) This enables also the interrupt message
1452 * when force_abort is set and did_emsg unset in case of an interrupt
1453 * from a finally clause after an error.
1454 */
1455 else if (got_int || (did_emsg && force_abort))
1456 suppress_errthrow = TRUE;
1457 }
1458
1459 /*
1460 * The current cstack will be freed when do_cmdline() returns. An uncaught
1461 * exception will have to be rethrown in the previous cstack. If a function
1462 * has just returned or a script file was just finished and the previous
1463 * cstack belongs to the same function or, respectively, script file, it
1464 * will have to be checked for finally clauses to be executed due to the
1465 * ":return" or ":finish". This is done in do_one_cmd().
1466 */
1467 if (did_throw)
1468 need_rethrow = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001469 if ((getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470 && ex_nesting_level > source_level(real_cookie))
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001471 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472 && ex_nesting_level > func_level(real_cookie) + 1))
1473 {
1474 if (!did_throw)
1475 check_cstack = TRUE;
1476 }
1477 else
1478 {
1479 /* When leaving a function, reduce nesting level. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001480 if (getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001481 --ex_nesting_level;
1482 /*
1483 * Go to debug mode when returning from a function in which we are
1484 * single-stepping.
1485 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001486 if ((getline_equal(fgetline, cookie, getsourceline)
1487 || getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488 && ex_nesting_level + 1 <= debug_break_level)
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001489 do_debug(getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490 ? (char_u *)_("End of sourced file")
1491 : (char_u *)_("End of function"));
1492 }
1493
1494 /*
1495 * Restore the exception environment (done after returning from the
1496 * debugger).
1497 */
1498 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001499 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001500
1501 msg_list = saved_msg_list;
1502#endif /* FEAT_EVAL */
1503
1504 /*
1505 * If there was too much output to fit on the command line, ask the user to
1506 * hit return before redrawing the screen. With the ":global" command we do
1507 * this only once after the command is finished.
1508 */
1509 if (did_inc)
1510 {
1511 --RedrawingDisabled;
1512 --no_wait_return;
1513 msg_scroll = FALSE;
1514
1515 /*
1516 * When just finished an ":if"-":else" which was typed, no need to
1517 * wait for hit-return. Also for an error situation.
1518 */
1519 if (retval == FAIL
1520#ifdef FEAT_EVAL
1521 || (did_endif && KeyTyped && !did_emsg)
1522#endif
1523 )
1524 {
1525 need_wait_return = FALSE;
1526 msg_didany = FALSE; /* don't wait when restarting edit */
1527 }
1528 else if (need_wait_return)
1529 {
1530 /*
1531 * The msg_start() above clears msg_didout. The wait_return we do
1532 * here should not overwrite the command that may be shown before
1533 * doing that.
1534 */
1535 msg_didout |= msg_didout_before_start;
1536 wait_return(FALSE);
1537 }
1538 }
1539
Bram Moolenaar2a942252012-11-28 23:03:07 +01001540#ifdef FEAT_EVAL
1541 did_endif = FALSE; /* in case do_cmdline used recursively */
1542#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543 /*
1544 * Reset if_level, in case a sourced script file contains more ":if" than
1545 * ":endif" (could be ":if x | foo | endif").
1546 */
1547 if_level = 0;
Bram Moolenaar2e18a122012-11-28 19:10:54 +01001548#endif
Bram Moolenaard4ad0d42012-11-28 17:34:48 +01001549
Bram Moolenaar071d4272004-06-13 20:20:40 +00001550 --call_depth;
1551 return retval;
1552}
1553
1554#ifdef FEAT_EVAL
1555/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001556 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001557 */
1558 static char_u *
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001559get_loop_line(c, cookie, indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560 int c;
1561 void *cookie;
1562 int indent;
1563{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001564 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001565 wcmd_T *wp;
1566 char_u *line;
1567
1568 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1569 {
1570 if (cp->repeating)
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001571 return NULL; /* trying to read past ":endwhile"/":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001572
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001573 /* First time inside the ":while"/":for": get line normally. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001574 if (cp->getline == NULL)
1575 line = getcmdline(c, 0L, indent);
1576 else
1577 line = cp->getline(c, cp->cookie, indent);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001578 if (line != NULL && store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579 ++cp->current_line;
1580
1581 return line;
1582 }
1583
1584 KeyTyped = FALSE;
1585 ++cp->current_line;
1586 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
1587 sourcing_lnum = wp->lnum;
1588 return vim_strsave(wp->line);
1589}
1590
1591/*
1592 * Store a line in "gap" so that a ":while" loop can execute it again.
1593 */
1594 static int
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001595store_loop_line(gap, line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596 garray_T *gap;
1597 char_u *line;
1598{
1599 if (ga_grow(gap, 1) == FAIL)
1600 return FAIL;
1601 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
1602 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = sourcing_lnum;
1603 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001604 return OK;
1605}
1606
1607/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001608 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609 */
1610 static void
1611free_cmdlines(gap)
1612 garray_T *gap;
1613{
1614 while (gap->ga_len > 0)
1615 {
1616 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1617 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618 }
1619}
1620#endif
1621
1622/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001623 * If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals
1624 * "func". * Otherwise return TRUE when "fgetline" equals "func".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626 int
Bram Moolenaar89d40322006-08-29 15:30:07 +00001627getline_equal(fgetline, cookie, func)
1628 char_u *(*fgetline) __ARGS((int, void *, int));
Bram Moolenaar78a15312009-05-15 19:33:18 +00001629 void *cookie UNUSED; /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001630 char_u *(*func) __ARGS((int, void *, int));
1631{
1632#ifdef FEAT_EVAL
1633 char_u *(*gp) __ARGS((int, void *, int));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001634 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001635
Bram Moolenaar89d40322006-08-29 15:30:07 +00001636 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001637 * function that's originally used to obtain the lines. This may be
1638 * nested several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001639 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001640 cp = (struct loop_cookie *)cookie;
1641 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001642 {
1643 gp = cp->getline;
1644 cp = cp->cookie;
1645 }
1646 return gp == func;
1647#else
Bram Moolenaar89d40322006-08-29 15:30:07 +00001648 return fgetline == func;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001649#endif
1650}
1651
1652#if defined(FEAT_EVAL) || defined(FEAT_MBYTE) || defined(PROTO)
1653/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001654 * If "fgetline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001655 * getline function. Otherwise return "cookie".
1656 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001657 void *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001658getline_cookie(fgetline, cookie)
Bram Moolenaar78a15312009-05-15 19:33:18 +00001659 char_u *(*fgetline) __ARGS((int, void *, int)) UNUSED;
Bram Moolenaar89d40322006-08-29 15:30:07 +00001660 void *cookie; /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661{
1662# ifdef FEAT_EVAL
1663 char_u *(*gp) __ARGS((int, void *, int));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001664 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001665
Bram Moolenaar89d40322006-08-29 15:30:07 +00001666 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001667 * cookie that's originally used to obtain the lines. This may be nested
Bram Moolenaar071d4272004-06-13 20:20:40 +00001668 * several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001669 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001670 cp = (struct loop_cookie *)cookie;
1671 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001672 {
1673 gp = cp->getline;
1674 cp = cp->cookie;
1675 }
1676 return cp;
1677# else
1678 return cookie;
1679# endif
1680}
1681#endif
1682
1683/*
1684 * Execute one Ex command.
1685 *
1686 * If 'sourcing' is TRUE, the command will be included in the error message.
1687 *
1688 * 1. skip comment lines and leading space
1689 * 2. handle command modifiers
1690 * 3. parse range
1691 * 4. parse command
1692 * 5. parse arguments
1693 * 6. switch on command name
1694 *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001695 * Note: "fgetline" can be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001696 *
1697 * This function may be called recursively!
1698 */
1699#if (_MSC_VER == 1200)
1700/*
Bram Moolenaared203462004-06-16 11:19:22 +00001701 * Avoid optimisation bug in VC++ version 6.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001702 */
Bram Moolenaar281bdce2005-01-25 21:53:18 +00001703 #pragma optimize( "g", off )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001704#endif
1705 static char_u *
1706do_one_cmd(cmdlinep, sourcing,
1707#ifdef FEAT_EVAL
1708 cstack,
1709#endif
Bram Moolenaar89d40322006-08-29 15:30:07 +00001710 fgetline, cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001711 char_u **cmdlinep;
1712 int sourcing;
1713#ifdef FEAT_EVAL
1714 struct condstack *cstack;
1715#endif
Bram Moolenaar89d40322006-08-29 15:30:07 +00001716 char_u *(*fgetline) __ARGS((int, void *, int));
1717 void *cookie; /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718{
1719 char_u *p;
1720 linenr_T lnum;
1721 long n;
1722 char_u *errormsg = NULL; /* error message */
1723 exarg_T ea; /* Ex command arguments */
1724 long verbose_save = -1;
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001725 int save_msg_scroll = msg_scroll;
1726 int save_msg_silent = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727 int did_esilent = 0;
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001728#ifdef HAVE_SANDBOX
1729 int did_sandbox = FALSE;
1730#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001731 cmdmod_T save_cmdmod;
1732 int ni; /* set when Not Implemented */
1733
1734 vim_memset(&ea, 0, sizeof(ea));
1735 ea.line1 = 1;
1736 ea.line2 = 1;
1737#ifdef FEAT_EVAL
1738 ++ex_nesting_level;
1739#endif
1740
Bram Moolenaar21691f82012-12-05 19:13:18 +01001741 /* When the last file has not been edited :q has to be typed twice. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001742 if (quitmore
1743#ifdef FEAT_EVAL
1744 /* avoid that a function call in 'statusline' does this */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001745 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001746#endif
1747#ifdef FEAT_AUTOCMD
Bram Moolenaar21691f82012-12-05 19:13:18 +01001748 /* avoid that an autocommand, e.g. QuitPre, does this */
1749 && !getline_equal(fgetline, cookie, getnextac)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001750#endif
1751 )
1752 --quitmore;
1753
1754 /*
1755 * Reset browse, confirm, etc.. They are restored when returning, for
1756 * recursive calls.
1757 */
1758 save_cmdmod = cmdmod;
1759 vim_memset(&cmdmod, 0, sizeof(cmdmod));
1760
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001761 /* "#!anything" is handled like a comment. */
1762 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1763 goto doend;
1764
Bram Moolenaar071d4272004-06-13 20:20:40 +00001765 /*
1766 * Repeat until no more command modifiers are found.
1767 */
1768 ea.cmd = *cmdlinep;
1769 for (;;)
1770 {
1771/*
1772 * 1. skip comment lines and leading white space and colons
1773 */
1774 while (*ea.cmd == ' ' || *ea.cmd == '\t' || *ea.cmd == ':')
1775 ++ea.cmd;
1776
1777 /* in ex mode, an empty line works like :+ */
1778 if (*ea.cmd == NUL && exmode_active
Bram Moolenaar89d40322006-08-29 15:30:07 +00001779 && (getline_equal(fgetline, cookie, getexmodeline)
1780 || getline_equal(fgetline, cookie, getexline))
Bram Moolenaardf177f62005-02-22 08:39:57 +00001781 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782 {
1783 ea.cmd = (char_u *)"+";
1784 ex_pressedreturn = TRUE;
1785 }
1786
1787 /* ignore comment and empty lines */
Bram Moolenaarf998c042007-11-20 11:31:26 +00001788 if (*ea.cmd == '"')
1789 goto doend;
1790 if (*ea.cmd == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001791 {
1792 ex_pressedreturn = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001794 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795
1796/*
1797 * 2. handle command modifiers.
1798 */
1799 p = ea.cmd;
1800 if (VIM_ISDIGIT(*ea.cmd))
1801 p = skipwhite(skipdigits(ea.cmd));
1802 switch (*p)
1803 {
1804 /* When adding an entry, also modify cmd_exists(). */
1805 case 'a': if (!checkforcmd(&ea.cmd, "aboveleft", 3))
1806 break;
1807#ifdef FEAT_WINDOWS
1808 cmdmod.split |= WSP_ABOVE;
1809#endif
1810 continue;
1811
1812 case 'b': if (checkforcmd(&ea.cmd, "belowright", 3))
1813 {
1814#ifdef FEAT_WINDOWS
1815 cmdmod.split |= WSP_BELOW;
1816#endif
1817 continue;
1818 }
1819 if (checkforcmd(&ea.cmd, "browse", 3))
1820 {
Bram Moolenaard812df62008-11-09 12:46:09 +00001821#ifdef FEAT_BROWSE_CMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822 cmdmod.browse = TRUE;
1823#endif
1824 continue;
1825 }
1826 if (!checkforcmd(&ea.cmd, "botright", 2))
1827 break;
1828#ifdef FEAT_WINDOWS
1829 cmdmod.split |= WSP_BOT;
1830#endif
1831 continue;
1832
1833 case 'c': if (!checkforcmd(&ea.cmd, "confirm", 4))
1834 break;
1835#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1836 cmdmod.confirm = TRUE;
1837#endif
1838 continue;
1839
1840 case 'k': if (checkforcmd(&ea.cmd, "keepmarks", 3))
1841 {
1842 cmdmod.keepmarks = TRUE;
1843 continue;
1844 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001845 if (checkforcmd(&ea.cmd, "keepalt", 5))
1846 {
1847 cmdmod.keepalt = TRUE;
1848 continue;
1849 }
Bram Moolenaara939e432013-11-09 05:30:26 +01001850 if (checkforcmd(&ea.cmd, "keeppatterns", 5))
1851 {
1852 cmdmod.keeppatterns = TRUE;
1853 continue;
1854 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855 if (!checkforcmd(&ea.cmd, "keepjumps", 5))
1856 break;
1857 cmdmod.keepjumps = TRUE;
1858 continue;
1859
1860 /* ":hide" and ":hide | cmd" are not modifiers */
1861 case 'h': if (p != ea.cmd || !checkforcmd(&p, "hide", 3)
1862 || *p == NUL || ends_excmd(*p))
1863 break;
1864 ea.cmd = p;
1865 cmdmod.hide = TRUE;
1866 continue;
1867
1868 case 'l': if (checkforcmd(&ea.cmd, "lockmarks", 3))
1869 {
1870 cmdmod.lockmarks = TRUE;
1871 continue;
1872 }
1873
1874 if (!checkforcmd(&ea.cmd, "leftabove", 5))
1875 break;
1876#ifdef FEAT_WINDOWS
1877 cmdmod.split |= WSP_ABOVE;
1878#endif
1879 continue;
1880
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001881 case 'n': if (checkforcmd(&ea.cmd, "noautocmd", 3))
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001882 {
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001883#ifdef FEAT_AUTOCMD
1884 if (cmdmod.save_ei == NULL)
1885 {
1886 /* Set 'eventignore' to "all". Restore the
1887 * existing option value later. */
1888 cmdmod.save_ei = vim_strsave(p_ei);
1889 set_string_option_direct((char_u *)"ei", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001890 (char_u *)"all", OPT_FREE, SID_NONE);
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001891 }
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001892#endif
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001893 continue;
1894 }
1895 if (!checkforcmd(&ea.cmd, "noswapfile", 6))
1896 break;
1897 cmdmod.noswapfile = TRUE;
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001898 continue;
1899
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900 case 'r': if (!checkforcmd(&ea.cmd, "rightbelow", 6))
1901 break;
1902#ifdef FEAT_WINDOWS
1903 cmdmod.split |= WSP_BELOW;
1904#endif
1905 continue;
1906
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001907 case 's': if (checkforcmd(&ea.cmd, "sandbox", 3))
1908 {
1909#ifdef HAVE_SANDBOX
1910 if (!did_sandbox)
1911 ++sandbox;
1912 did_sandbox = TRUE;
1913#endif
1914 continue;
1915 }
1916 if (!checkforcmd(&ea.cmd, "silent", 3))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917 break;
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001918 if (save_msg_silent == -1)
1919 save_msg_silent = msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001920 ++msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001921 if (*ea.cmd == '!' && !vim_iswhite(ea.cmd[-1]))
1922 {
1923 /* ":silent!", but not "silent !cmd" */
1924 ea.cmd = skipwhite(ea.cmd + 1);
1925 ++emsg_silent;
1926 ++did_esilent;
1927 }
1928 continue;
1929
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001930 case 't': if (checkforcmd(&p, "tab", 3))
1931 {
1932#ifdef FEAT_WINDOWS
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001933 if (vim_isdigit(*ea.cmd))
1934 cmdmod.tab = atoi((char *)ea.cmd) + 1;
1935 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001936 cmdmod.tab = tabpage_index(curtab) + 1;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001937 ea.cmd = p;
1938#endif
1939 continue;
1940 }
1941 if (!checkforcmd(&ea.cmd, "topleft", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001942 break;
1943#ifdef FEAT_WINDOWS
1944 cmdmod.split |= WSP_TOP;
1945#endif
1946 continue;
1947
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001948 case 'u': if (!checkforcmd(&ea.cmd, "unsilent", 3))
1949 break;
1950 if (save_msg_silent == -1)
1951 save_msg_silent = msg_silent;
1952 msg_silent = 0;
1953 continue;
1954
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955 case 'v': if (checkforcmd(&ea.cmd, "vertical", 4))
1956 {
1957#ifdef FEAT_VERTSPLIT
1958 cmdmod.split |= WSP_VERT;
1959#endif
1960 continue;
1961 }
1962 if (!checkforcmd(&p, "verbose", 4))
1963 break;
1964 if (verbose_save < 0)
1965 verbose_save = p_verbose;
Bram Moolenaared203462004-06-16 11:19:22 +00001966 if (vim_isdigit(*ea.cmd))
1967 p_verbose = atoi((char *)ea.cmd);
1968 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001969 p_verbose = 1;
1970 ea.cmd = p;
1971 continue;
1972 }
1973 break;
1974 }
1975
1976#ifdef FEAT_EVAL
1977 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
1978 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
1979#else
1980 ea.skip = (if_level > 0);
1981#endif
1982
1983#ifdef FEAT_EVAL
Bram Moolenaar05159a02005-02-26 23:04:13 +00001984# ifdef FEAT_PROFILE
1985 /* Count this line for profiling if ea.skip is FALSE. */
Bram Moolenaar371d5402006-03-20 21:47:49 +00001986 if (do_profiling == PROF_YES && !ea.skip)
Bram Moolenaar05159a02005-02-26 23:04:13 +00001987 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001988 if (getline_equal(fgetline, cookie, get_func_line))
1989 func_line_exec(getline_cookie(fgetline, cookie));
1990 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +00001991 script_line_exec();
1992 }
1993#endif
1994
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995 /* May go to debug mode. If this happens and the ">quit" debug command is
1996 * used, throw an interrupt exception and skip the next command. */
1997 dbg_check_breakpoint(&ea);
1998 if (!ea.skip && got_int)
1999 {
2000 ea.skip = TRUE;
2001 (void)do_intthrow(cstack);
2002 }
2003#endif
2004
2005/*
2006 * 3. parse a range specifier of the form: addr [,addr] [;addr] ..
2007 *
2008 * where 'addr' is:
2009 *
2010 * % (entire file)
2011 * $ [+-NUM]
2012 * 'x [+-NUM] (where x denotes a currently defined mark)
2013 * . [+-NUM]
2014 * [+-NUM]..
2015 * NUM
2016 *
2017 * The ea.cmd pointer is updated to point to the first character following the
2018 * range spec. If an initial address is found, but no second, the upper bound
2019 * is equal to the lower.
2020 */
2021
2022 /* repeat for all ',' or ';' separated addresses */
2023 for (;;)
2024 {
2025 ea.line1 = ea.line2;
2026 ea.line2 = curwin->w_cursor.lnum; /* default is current line number */
2027 ea.cmd = skipwhite(ea.cmd);
2028 lnum = get_address(&ea.cmd, ea.skip, ea.addr_count == 0);
2029 if (ea.cmd == NULL) /* error detected */
2030 goto doend;
2031 if (lnum == MAXLNUM)
2032 {
2033 if (*ea.cmd == '%') /* '%' - all lines */
2034 {
2035 ++ea.cmd;
2036 ea.line1 = 1;
2037 ea.line2 = curbuf->b_ml.ml_line_count;
2038 ++ea.addr_count;
2039 }
2040 /* '*' - visual area */
2041 else if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
2042 {
2043 pos_T *fp;
2044
2045 ++ea.cmd;
2046 if (!ea.skip)
2047 {
2048 fp = getmark('<', FALSE);
2049 if (check_mark(fp) == FAIL)
2050 goto doend;
2051 ea.line1 = fp->lnum;
2052 fp = getmark('>', FALSE);
2053 if (check_mark(fp) == FAIL)
2054 goto doend;
2055 ea.line2 = fp->lnum;
2056 ++ea.addr_count;
2057 }
2058 }
2059 }
2060 else
2061 ea.line2 = lnum;
2062 ea.addr_count++;
2063
2064 if (*ea.cmd == ';')
2065 {
2066 if (!ea.skip)
2067 curwin->w_cursor.lnum = ea.line2;
2068 }
2069 else if (*ea.cmd != ',')
2070 break;
2071 ++ea.cmd;
2072 }
2073
2074 /* One address given: set start and end lines */
2075 if (ea.addr_count == 1)
2076 {
2077 ea.line1 = ea.line2;
2078 /* ... but only implicit: really no address given */
2079 if (lnum == MAXLNUM)
2080 ea.addr_count = 0;
2081 }
2082
2083 /* Don't leave the cursor on an illegal line (caused by ';') */
2084 check_cursor_lnum();
2085
2086/*
2087 * 4. parse command
2088 */
2089
2090 /*
2091 * Skip ':' and any white space
2092 */
2093 ea.cmd = skipwhite(ea.cmd);
2094 while (*ea.cmd == ':')
2095 ea.cmd = skipwhite(ea.cmd + 1);
2096
2097 /*
2098 * If we got a line, but no command, then go to the line.
2099 * If we find a '|' or '\n' we set ea.nextcmd.
2100 */
2101 if (*ea.cmd == NUL || *ea.cmd == '"' ||
2102 (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
2103 {
2104 /*
2105 * strange vi behaviour:
2106 * ":3" jumps to line 3
2107 * ":3|..." prints line 3
2108 * ":|" prints current line
2109 */
2110 if (ea.skip) /* skip this if inside :if */
2111 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00002112 if (*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113 {
2114 ea.cmdidx = CMD_print;
2115 ea.argt = RANGE+COUNT+TRLBAR;
2116 if ((errormsg = invalid_range(&ea)) == NULL)
2117 {
2118 correct_range(&ea);
2119 ex_print(&ea);
2120 }
2121 }
2122 else if (ea.addr_count != 0)
2123 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002124 if (ea.line2 > curbuf->b_ml.ml_line_count)
2125 {
2126 /* With '-' in 'cpoptions' a line number past the file is an
2127 * error, otherwise put it at the end of the file. */
2128 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
2129 ea.line2 = -1;
2130 else
2131 ea.line2 = curbuf->b_ml.ml_line_count;
2132 }
2133
2134 if (ea.line2 < 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002135 errormsg = (char_u *)_(e_invrange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002136 else
2137 {
2138 if (ea.line2 == 0)
2139 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002140 else
2141 curwin->w_cursor.lnum = ea.line2;
2142 beginline(BL_SOL | BL_FIX);
2143 }
2144 }
2145 goto doend;
2146 }
2147
2148 /* Find the command and let "p" point to after it. */
2149 p = find_command(&ea, NULL);
2150
Bram Moolenaard5005162014-08-22 23:05:54 +02002151#ifdef FEAT_AUTOCMD
2152 /* If this looks like an undefined user command and there are CmdUndefined
2153 * autocommands defined, trigger the matching autocommands. */
2154 if (p != NULL && ea.cmdidx == CMD_SIZE && !ea.skip
2155 && ASCII_ISUPPER(*ea.cmd)
2156 && has_cmdundefined())
2157 {
Bram Moolenaard5005162014-08-22 23:05:54 +02002158 int ret;
2159
Bram Moolenaar5a31b462014-08-23 14:16:20 +02002160 p = ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02002161 while (ASCII_ISALNUM(*p))
2162 ++p;
Bram Moolenaar120f4a82014-09-09 12:22:06 +02002163 p = vim_strnsave(ea.cmd, (int)(p - ea.cmd));
Bram Moolenaard5005162014-08-22 23:05:54 +02002164 ret = apply_autocmds(EVENT_CMDUNDEFINED, p, p, TRUE, NULL);
2165 vim_free(p);
2166 if (ret && !aborting())
2167 p = find_command(&ea, NULL);
2168 }
2169#endif
2170
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171#ifdef FEAT_USR_CMDS
2172 if (p == NULL)
2173 {
2174 if (!ea.skip)
2175 errormsg = (char_u *)_("E464: Ambiguous use of user-defined command");
2176 goto doend;
2177 }
2178 /* Check for wrong commands. */
2179 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78)
2180 {
2181 errormsg = uc_fun_cmd();
2182 goto doend;
2183 }
2184#endif
2185 if (ea.cmdidx == CMD_SIZE)
2186 {
2187 if (!ea.skip)
2188 {
2189 STRCPY(IObuff, _("E492: Not an editor command"));
2190 if (!sourcing)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002191 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002192 errormsg = IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02002193 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194 }
2195 goto doend;
2196 }
2197
Bram Moolenaar958636c2014-10-21 20:01:58 +02002198 ni = (!IS_USER_CMDIDX(ea.cmdidx)
2199 && (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00002200#ifdef HAVE_EX_SCRIPT_NI
2201 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
2202#endif
2203 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002204
2205#ifndef FEAT_EVAL
2206 /*
2207 * When the expression evaluation is disabled, recognize the ":if" and
2208 * ":endif" commands and ignore everything in between it.
2209 */
2210 if (ea.cmdidx == CMD_if)
2211 ++if_level;
2212 if (if_level)
2213 {
2214 if (ea.cmdidx == CMD_endif)
2215 --if_level;
2216 goto doend;
2217 }
2218
2219#endif
2220
Bram Moolenaar196b3b02008-06-20 16:51:41 +00002221 /* forced commands */
2222 if (*p == '!' && ea.cmdidx != CMD_substitute
2223 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224 {
2225 ++p;
2226 ea.forceit = TRUE;
2227 }
2228 else
2229 ea.forceit = FALSE;
2230
2231/*
2232 * 5. parse arguments
2233 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02002234 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002235 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002236
2237 if (!ea.skip)
2238 {
2239#ifdef HAVE_SANDBOX
2240 if (sandbox != 0 && !(ea.argt & SBOXOK))
2241 {
2242 /* Command not allowed in sandbox. */
2243 errormsg = (char_u *)_(e_sandbox);
2244 goto doend;
2245 }
2246#endif
2247 if (!curbuf->b_p_ma && (ea.argt & MODIFY))
2248 {
2249 /* Command not allowed in non-'modifiable' buffer */
2250 errormsg = (char_u *)_(e_modifiable);
2251 goto doend;
2252 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002253
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002254 if (text_locked() && !(ea.argt & CMDWIN)
Bram Moolenaar958636c2014-10-21 20:01:58 +02002255 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002256 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002257 /* Command not allowed when editing the command line. */
2258#ifdef FEAT_CMDWIN
2259 if (cmdwin_type != 0)
2260 errormsg = (char_u *)_(e_cmdwin);
2261 else
2262#endif
2263 errormsg = (char_u *)_(e_secure);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002264 goto doend;
2265 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002266#ifdef FEAT_AUTOCMD
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002267 /* Disallow editing another buffer when "curbuf_lock" is set.
2268 * Do allow ":edit" (check for argument later).
2269 * Do allow ":checktime" (it's postponed). */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002270 if (!(ea.argt & CMDWIN)
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002271 && ea.cmdidx != CMD_edit
2272 && ea.cmdidx != CMD_checktime
Bram Moolenaar958636c2014-10-21 20:01:58 +02002273 && !IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002274 && curbuf_locked())
2275 goto doend;
2276#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277
2278 if (!ni && !(ea.argt & RANGE) && ea.addr_count > 0)
2279 {
2280 /* no range allowed */
2281 errormsg = (char_u *)_(e_norange);
2282 goto doend;
2283 }
2284 }
2285
2286 if (!ni && !(ea.argt & BANG) && ea.forceit) /* no <!> allowed */
2287 {
2288 errormsg = (char_u *)_(e_nobang);
2289 goto doend;
2290 }
2291
2292 /*
2293 * Don't complain about the range if it is not used
2294 * (could happen if line_count is accidentally set to 0).
2295 */
2296 if (!ea.skip && !ni)
2297 {
2298 /*
2299 * If the range is backwards, ask for confirmation and, if given, swap
2300 * ea.line1 & ea.line2 so it's forwards again.
2301 * When global command is busy, don't ask, will fail below.
2302 */
2303 if (!global_busy && ea.line1 > ea.line2)
2304 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002305 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002307 if (sourcing || exmode_active)
2308 {
2309 errormsg = (char_u *)_("E493: Backwards range given");
2310 goto doend;
2311 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312 if (ask_yesno((char_u *)
2313 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002314 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315 }
2316 lnum = ea.line1;
2317 ea.line1 = ea.line2;
2318 ea.line2 = lnum;
2319 }
2320 if ((errormsg = invalid_range(&ea)) != NULL)
2321 goto doend;
2322 }
2323
2324 if ((ea.argt & NOTADR) && ea.addr_count == 0) /* default is 1, not cursor */
2325 ea.line2 = 1;
2326
2327 correct_range(&ea);
2328
2329#ifdef FEAT_FOLDING
2330 if (((ea.argt & WHOLEFOLD) || ea.addr_count >= 2) && !global_busy)
2331 {
2332 /* Put the first line at the start of a closed fold, put the last line
2333 * at the end of a closed fold. */
2334 (void)hasFolding(ea.line1, &ea.line1, NULL);
2335 (void)hasFolding(ea.line2, NULL, &ea.line2);
2336 }
2337#endif
2338
2339#ifdef FEAT_QUICKFIX
2340 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002341 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002342 * option here, so things like % get expanded.
2343 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002344 p = replace_makeprg(&ea, p, cmdlinep);
2345 if (p == NULL)
2346 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347#endif
2348
2349 /*
2350 * Skip to start of argument.
2351 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2352 */
2353 if (ea.cmdidx == CMD_bang)
2354 ea.arg = p;
2355 else
2356 ea.arg = skipwhite(p);
2357
2358 /*
2359 * Check for "++opt=val" argument.
2360 * Must be first, allow ":w ++enc=utf8 !cmd"
2361 */
2362 if (ea.argt & ARGOPT)
2363 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2364 if (getargopt(&ea) == FAIL && !ni)
2365 {
2366 errormsg = (char_u *)_(e_invarg);
2367 goto doend;
2368 }
2369
2370 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2371 {
2372 if (*ea.arg == '>') /* append */
2373 {
2374 if (*++ea.arg != '>') /* typed wrong */
2375 {
2376 errormsg = (char_u *)_("E494: Use w or w>>");
2377 goto doend;
2378 }
2379 ea.arg = skipwhite(ea.arg + 1);
2380 ea.append = TRUE;
2381 }
2382 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
2383 {
2384 ++ea.arg;
2385 ea.usefilter = TRUE;
2386 }
2387 }
2388
2389 if (ea.cmdidx == CMD_read)
2390 {
2391 if (ea.forceit)
2392 {
2393 ea.usefilter = TRUE; /* :r! filter if ea.forceit */
2394 ea.forceit = FALSE;
2395 }
2396 else if (*ea.arg == '!') /* :r !filter */
2397 {
2398 ++ea.arg;
2399 ea.usefilter = TRUE;
2400 }
2401 }
2402
2403 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2404 {
2405 ea.amount = 1;
2406 while (*ea.arg == *ea.cmd) /* count number of '>' or '<' */
2407 {
2408 ++ea.arg;
2409 ++ea.amount;
2410 }
2411 ea.arg = skipwhite(ea.arg);
2412 }
2413
2414 /*
2415 * Check for "+command" argument, before checking for next command.
2416 * Don't do this for ":read !cmd" and ":write !cmd".
2417 */
2418 if ((ea.argt & EDITCMD) && !ea.usefilter)
2419 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2420
2421 /*
2422 * Check for '|' to separate commands and '"' to start comments.
2423 * Don't do this for ":read !cmd" and ":write !cmd".
2424 */
2425 if ((ea.argt & TRLBAR) && !ea.usefilter)
2426 separate_nextcmd(&ea);
2427
2428 /*
2429 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002430 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2431 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002432 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002433 else if (ea.cmdidx == CMD_bang
2434 || ea.cmdidx == CMD_global
2435 || ea.cmdidx == CMD_vglobal
2436 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437 {
2438 for (p = ea.arg; *p; ++p)
2439 {
2440 /* Remove one backslash before a newline, so that it's possible to
2441 * pass a newline to the shell and also a newline that is preceded
2442 * with a backslash. This makes it impossible to end a shell
2443 * command in a backslash, but that doesn't appear useful.
2444 * Halving the number of backslashes is incompatible with previous
2445 * versions. */
2446 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002447 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448 else if (*p == '\n')
2449 {
2450 ea.nextcmd = p + 1;
2451 *p = NUL;
2452 break;
2453 }
2454 }
2455 }
2456
2457 if ((ea.argt & DFLALL) && ea.addr_count == 0)
2458 {
2459 ea.line1 = 1;
2460 ea.line2 = curbuf->b_ml.ml_line_count;
2461 }
2462
2463 /* accept numbered register only when no count allowed (:put) */
2464 if ( (ea.argt & REGSTR)
2465 && *ea.arg != NUL
Bram Moolenaar958636c2014-10-21 20:01:58 +02002466 /* Do not allow register = for user commands */
2467 && (!IS_USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468 && !((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)))
2469 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002470#ifndef FEAT_CLIPBOARD
2471 /* check these explicitly for a more specific error message */
2472 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002474 errormsg = (char_u *)_(e_invalidreg);
2475 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002476 }
2477#endif
Bram Moolenaar958636c2014-10-21 20:01:58 +02002478 if (valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2479 && !IS_USER_CMDIDX(ea.cmdidx))))
Bram Moolenaar85de2062011-05-05 14:26:41 +02002480 {
2481 ea.regname = *ea.arg++;
2482#ifdef FEAT_EVAL
2483 /* for '=' register: accept the rest of the line as an expression */
2484 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2485 {
2486 set_expr_line(vim_strsave(ea.arg));
2487 ea.arg += STRLEN(ea.arg);
2488 }
2489#endif
2490 ea.arg = skipwhite(ea.arg);
2491 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002492 }
2493
2494 /*
2495 * Check for a count. When accepting a BUFNAME, don't use "123foo" as a
2496 * count, it's a buffer name.
2497 */
2498 if ((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)
2499 && (!(ea.argt & BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
2500 || vim_iswhite(*p)))
2501 {
2502 n = getdigits(&ea.arg);
2503 ea.arg = skipwhite(ea.arg);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002504 if (n <= 0 && !ni && (ea.argt & ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002505 {
2506 errormsg = (char_u *)_(e_zerocount);
2507 goto doend;
2508 }
2509 if (ea.argt & NOTADR) /* e.g. :buffer 2, :sleep 3 */
2510 {
2511 ea.line2 = n;
2512 if (ea.addr_count == 0)
2513 ea.addr_count = 1;
2514 }
2515 else
2516 {
2517 ea.line1 = ea.line2;
2518 ea.line2 += n - 1;
2519 ++ea.addr_count;
2520 /*
2521 * Be vi compatible: no error message for out of range.
2522 */
2523 if (ea.line2 > curbuf->b_ml.ml_line_count)
2524 ea.line2 = curbuf->b_ml.ml_line_count;
2525 }
2526 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002527
2528 /*
2529 * Check for flags: 'l', 'p' and '#'.
2530 */
2531 if (ea.argt & EXFLAGS)
2532 get_flags(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002533 /* no arguments allowed */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002534 if (!ni && !(ea.argt & EXTRA) && *ea.arg != NUL
Bram Moolenaara2031822006-03-07 22:29:51 +00002535 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002536 {
2537 errormsg = (char_u *)_(e_trailing);
2538 goto doend;
2539 }
2540
2541 if (!ni && (ea.argt & NEEDARG) && *ea.arg == NUL)
2542 {
2543 errormsg = (char_u *)_(e_argreq);
2544 goto doend;
2545 }
2546
2547#ifdef FEAT_EVAL
2548 /*
2549 * Skip the command when it's not going to be executed.
2550 * The commands like :if, :endif, etc. always need to be executed.
2551 * Also make an exception for commands that handle a trailing command
2552 * themselves.
2553 */
2554 if (ea.skip)
2555 {
2556 switch (ea.cmdidx)
2557 {
2558 /* commands that need evaluation */
2559 case CMD_while:
2560 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002561 case CMD_for:
2562 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002563 case CMD_if:
2564 case CMD_elseif:
2565 case CMD_else:
2566 case CMD_endif:
2567 case CMD_try:
2568 case CMD_catch:
2569 case CMD_finally:
2570 case CMD_endtry:
2571 case CMD_function:
2572 break;
2573
2574 /* Commands that handle '|' themselves. Check: A command should
2575 * either have the TRLBAR flag, appear in this list or appear in
2576 * the list at ":help :bar". */
2577 case CMD_aboveleft:
2578 case CMD_and:
2579 case CMD_belowright:
2580 case CMD_botright:
2581 case CMD_browse:
2582 case CMD_call:
2583 case CMD_confirm:
2584 case CMD_delfunction:
2585 case CMD_djump:
2586 case CMD_dlist:
2587 case CMD_dsearch:
2588 case CMD_dsplit:
2589 case CMD_echo:
2590 case CMD_echoerr:
2591 case CMD_echomsg:
2592 case CMD_echon:
2593 case CMD_execute:
2594 case CMD_help:
2595 case CMD_hide:
2596 case CMD_ijump:
2597 case CMD_ilist:
2598 case CMD_isearch:
2599 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002600 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002601 case CMD_keepjumps:
2602 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01002603 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604 case CMD_leftabove:
2605 case CMD_let:
2606 case CMD_lockmarks:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002607 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002608 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002609 case CMD_mzscheme:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01002610 case CMD_noautocmd:
2611 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002612 case CMD_perl:
2613 case CMD_psearch:
2614 case CMD_python:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002615 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002616 case CMD_python3:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617 case CMD_return:
2618 case CMD_rightbelow:
2619 case CMD_ruby:
2620 case CMD_silent:
2621 case CMD_smagic:
2622 case CMD_snomagic:
2623 case CMD_substitute:
2624 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002625 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002626 case CMD_tcl:
2627 case CMD_throw:
2628 case CMD_tilde:
2629 case CMD_topleft:
2630 case CMD_unlet:
2631 case CMD_verbose:
2632 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002633 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002634 break;
2635
2636 default: goto doend;
2637 }
2638 }
2639#endif
2640
2641 if (ea.argt & XFILE)
2642 {
2643 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2644 goto doend;
2645 }
2646
2647#ifdef FEAT_LISTCMDS
2648 /*
2649 * Accept buffer name. Cannot be used at the same time with a buffer
2650 * number. Don't do this for a user command.
2651 */
2652 if ((ea.argt & BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
Bram Moolenaar958636c2014-10-21 20:01:58 +02002653 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002654 {
2655 /*
2656 * :bdelete, :bwipeout and :bunload take several arguments, separated
2657 * by spaces: find next space (skipping over escaped characters).
2658 * The others take one argument: ignore trailing spaces.
2659 */
2660 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2661 || ea.cmdidx == CMD_bunload)
2662 p = skiptowhite_esc(ea.arg);
2663 else
2664 {
2665 p = ea.arg + STRLEN(ea.arg);
2666 while (p > ea.arg && vim_iswhite(p[-1]))
2667 --p;
2668 }
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002669 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & BUFUNL) != 0,
2670 FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671 if (ea.line2 < 0) /* failed */
2672 goto doend;
2673 ea.addr_count = 1;
2674 ea.arg = skipwhite(p);
2675 }
2676#endif
2677
2678/*
2679 * 6. switch on command name
2680 *
2681 * The "ea" structure holds the arguments that can be used.
2682 */
2683 ea.cmdlinep = cmdlinep;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002684 ea.getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685 ea.cookie = cookie;
2686#ifdef FEAT_EVAL
2687 ea.cstack = cstack;
2688#endif
2689
2690#ifdef FEAT_USR_CMDS
Bram Moolenaar958636c2014-10-21 20:01:58 +02002691 if (IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002692 {
2693 /*
2694 * Execute a user-defined command.
2695 */
2696 do_ucmd(&ea);
2697 }
2698 else
2699#endif
2700 {
2701 /*
2702 * Call the function to execute the command.
2703 */
2704 ea.errmsg = NULL;
2705 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2706 if (ea.errmsg != NULL)
2707 errormsg = (char_u *)_(ea.errmsg);
2708 }
2709
2710#ifdef FEAT_EVAL
2711 /*
2712 * If the command just executed called do_cmdline(), any throw or ":return"
2713 * or ":finish" encountered there must also check the cstack of the still
2714 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2715 * exception, or reanimate a returned function or finished script file and
2716 * return or finish it again.
2717 */
2718 if (need_rethrow)
2719 do_throw(cstack);
2720 else if (check_cstack)
2721 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002722 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002724 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002725 && current_func_returned())
2726 do_return(&ea, TRUE, FALSE, NULL);
2727 }
2728 need_rethrow = check_cstack = FALSE;
2729#endif
2730
2731doend:
2732 if (curwin->w_cursor.lnum == 0) /* can happen with zero line number */
2733 curwin->w_cursor.lnum = 1;
2734
2735 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2736 {
2737 if (sourcing)
2738 {
2739 if (errormsg != IObuff)
2740 {
2741 STRCPY(IObuff, errormsg);
2742 errormsg = IObuff;
2743 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002744 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002745 }
2746 emsg(errormsg);
2747 }
2748#ifdef FEAT_EVAL
2749 do_errthrow(cstack,
Bram Moolenaar958636c2014-10-21 20:01:58 +02002750 (ea.cmdidx != CMD_SIZE && !IS_USER_CMDIDX(ea.cmdidx))
2751 ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002752#endif
2753
2754 if (verbose_save >= 0)
2755 p_verbose = verbose_save;
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00002756#ifdef FEAT_AUTOCMD
2757 if (cmdmod.save_ei != NULL)
2758 {
2759 /* Restore 'eventignore' to the value before ":noautocmd". */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002760 set_string_option_direct((char_u *)"ei", -1, cmdmod.save_ei,
2761 OPT_FREE, SID_NONE);
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00002762 free_string_option(cmdmod.save_ei);
2763 }
2764#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002765
2766 cmdmod = save_cmdmod;
2767
Bram Moolenaar8e258a42009-07-09 13:55:43 +00002768 if (save_msg_silent != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002769 {
2770 /* messages could be enabled for a serious error, need to check if the
2771 * counters don't become negative */
Bram Moolenaarbecf4282009-09-30 11:24:36 +00002772 if (!did_emsg || msg_silent > save_msg_silent)
Bram Moolenaar8e258a42009-07-09 13:55:43 +00002773 msg_silent = save_msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774 emsg_silent -= did_esilent;
2775 if (emsg_silent < 0)
2776 emsg_silent = 0;
2777 /* Restore msg_scroll, it's set by file I/O commands, even when no
2778 * message is actually displayed. */
2779 msg_scroll = save_msg_scroll;
Bram Moolenaar77ab2802009-04-22 12:44:48 +00002780
2781 /* "silent reg" or "silent echo x" inside "redir" leaves msg_col
2782 * somewhere in the line. Put it back in the first column. */
2783 if (redirecting())
2784 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002785 }
2786
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002787#ifdef HAVE_SANDBOX
2788 if (did_sandbox)
2789 --sandbox;
2790#endif
2791
Bram Moolenaar071d4272004-06-13 20:20:40 +00002792 if (ea.nextcmd && *ea.nextcmd == NUL) /* not really a next command */
2793 ea.nextcmd = NULL;
2794
2795#ifdef FEAT_EVAL
2796 --ex_nesting_level;
2797#endif
2798
2799 return ea.nextcmd;
2800}
2801#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00002802 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002803#endif
2804
2805/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002806 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002807 * If there is a match advance "pp" to the argument and return TRUE.
2808 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002809 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00002810checkforcmd(pp, cmd, len)
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002811 char_u **pp; /* start of command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002812 char *cmd; /* name of command */
2813 int len; /* required length */
2814{
2815 int i;
2816
2817 for (i = 0; cmd[i] != NUL; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00002818 if (((char_u *)cmd)[i] != (*pp)[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00002819 break;
2820 if (i >= len && !isalpha((*pp)[i]))
2821 {
2822 *pp = skipwhite(*pp + i);
2823 return TRUE;
2824 }
2825 return FALSE;
2826}
2827
2828/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002829 * Append "cmd" to the error message in IObuff.
2830 * Takes care of limiting the length and handling 0xa0, which would be
2831 * invisible otherwise.
2832 */
2833 static void
2834append_command(cmd)
2835 char_u *cmd;
2836{
2837 char_u *s = cmd;
2838 char_u *d;
2839
2840 STRCAT(IObuff, ": ");
2841 d = IObuff + STRLEN(IObuff);
2842 while (*s != NUL && d - IObuff < IOSIZE - 7)
2843 {
2844 if (
2845#ifdef FEAT_MBYTE
2846 enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) :
2847#endif
2848 *s == 0xa0)
2849 {
2850 s +=
2851#ifdef FEAT_MBYTE
2852 enc_utf8 ? 2 :
2853#endif
2854 1;
2855 STRCPY(d, "<a0>");
2856 d += 4;
2857 }
2858 else
2859 MB_COPY_CHAR(s, d);
2860 }
2861 *d = NUL;
2862}
2863
2864/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002865 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002866 * Start of the name can be found at eap->cmd.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002867 * Returns pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002868 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002869 * Returns NULL for an ambiguous user command.
2870 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002871 static char_u *
2872find_command(eap, full)
2873 exarg_T *eap;
Bram Moolenaar78a15312009-05-15 19:33:18 +00002874 int *full UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002875{
2876 int len;
2877 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00002878 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002879
2880 /*
2881 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00002882 * Exceptions:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002883 * - the 'k' command can directly be followed by any character.
2884 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
2885 * but :sre[wind] is another command, as are :scrip[tnames],
2886 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00002887 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002888 */
2889 p = eap->cmd;
2890 if (*p == 'k')
2891 {
2892 eap->cmdidx = CMD_k;
2893 ++p;
2894 }
2895 else if (p[0] == 's'
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002896 && ((p[1] == 'c' && p[2] != 's' && p[2] != 'r'
2897 && p[3] != 'i' && p[4] != 'p')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002898 || p[1] == 'g'
2899 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
2900 || p[1] == 'I'
2901 || (p[1] == 'r' && p[2] != 'e')))
2902 {
2903 eap->cmdidx = CMD_substitute;
2904 ++p;
2905 }
2906 else
2907 {
2908 while (ASCII_ISALPHA(*p))
2909 ++p;
Bram Moolenaarb6590522010-07-21 16:00:43 +02002910 /* for python 3.x support ":py3", ":python3", ":py3file", etc. */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002911 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaarb6590522010-07-21 16:00:43 +02002912 while (ASCII_ISALNUM(*p))
2913 ++p;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02002914
Bram Moolenaar071d4272004-06-13 20:20:40 +00002915 /* check for non-alpha command */
2916 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
2917 ++p;
2918 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00002919 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
2920 {
2921 /* Check for ":dl", ":dell", etc. to ":deletel": that's
2922 * :delete with the 'l' flag. Same for 'p'. */
2923 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00002924 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00002925 break;
2926 if (i == len - 1)
2927 {
2928 --len;
2929 if (p[-1] == 'l')
2930 eap->flags |= EXFLAG_LIST;
2931 else
2932 eap->flags |= EXFLAG_PRINT;
2933 }
2934 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002935
2936 if (ASCII_ISLOWER(*eap->cmd))
2937 eap->cmdidx = cmdidxs[CharOrdLow(*eap->cmd)];
2938 else
2939 eap->cmdidx = cmdidxs[26];
2940
2941 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
2942 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
2943 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
2944 (size_t)len) == 0)
2945 {
2946#ifdef FEAT_EVAL
2947 if (full != NULL
2948 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
2949 *full = TRUE;
2950#endif
2951 break;
2952 }
2953
2954#ifdef FEAT_USR_CMDS
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01002955 /* Look for a user defined command as a last resort. Let ":Print" be
2956 * overruled by a user defined command. */
2957 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
2958 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002959 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002960 /* User defined commands may contain digits. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002961 while (ASCII_ISALNUM(*p))
2962 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002963 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002964 }
2965#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002966 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002967 eap->cmdidx = CMD_SIZE;
2968 }
2969
2970 return p;
2971}
2972
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002973#ifdef FEAT_USR_CMDS
2974/*
2975 * Search for a user command that matches "eap->cmd".
2976 * Return cmdidx in "eap->cmdidx", flags in "eap->argt", idx in "eap->useridx".
2977 * Return a pointer to just after the command.
2978 * Return NULL if there is no matching command.
2979 */
2980 static char_u *
2981find_ucmd(eap, p, full, xp, compl)
2982 exarg_T *eap;
2983 char_u *p; /* end of the command (possibly including count) */
2984 int *full; /* set to TRUE for a full match */
2985 expand_T *xp; /* used for completion, NULL otherwise */
2986 int *compl; /* completion flags or NULL */
2987{
2988 int len = (int)(p - eap->cmd);
2989 int j, k, matchlen = 0;
2990 ucmd_T *uc;
2991 int found = FALSE;
2992 int possible = FALSE;
2993 char_u *cp, *np; /* Point into typed cmd and test name */
2994 garray_T *gap;
2995 int amb_local = FALSE; /* Found ambiguous buffer-local command,
2996 only full match global is accepted. */
2997
2998 /*
2999 * Look for buffer-local user commands first, then global ones.
3000 */
3001 gap = &curbuf->b_ucmds;
3002 for (;;)
3003 {
3004 for (j = 0; j < gap->ga_len; ++j)
3005 {
3006 uc = USER_CMD_GA(gap, j);
3007 cp = eap->cmd;
3008 np = uc->uc_name;
3009 k = 0;
3010 while (k < len && *np != NUL && *cp++ == *np++)
3011 k++;
3012 if (k == len || (*np == NUL && vim_isdigit(eap->cmd[k])))
3013 {
3014 /* If finding a second match, the command is ambiguous. But
3015 * not if a buffer-local command wasn't a full match and a
3016 * global command is a full match. */
3017 if (k == len && found && *np != NUL)
3018 {
3019 if (gap == &ucmds)
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003020 return NULL;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003021 amb_local = TRUE;
3022 }
3023
3024 if (!found || (k == len && *np == NUL))
3025 {
3026 /* If we matched up to a digit, then there could
3027 * be another command including the digit that we
3028 * should use instead.
3029 */
3030 if (k == len)
3031 found = TRUE;
3032 else
3033 possible = TRUE;
3034
3035 if (gap == &ucmds)
3036 eap->cmdidx = CMD_USER;
3037 else
3038 eap->cmdidx = CMD_USER_BUF;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003039 eap->argt = (long)uc->uc_argt;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003040 eap->useridx = j;
3041
3042# ifdef FEAT_CMDL_COMPL
3043 if (compl != NULL)
3044 *compl = uc->uc_compl;
3045# ifdef FEAT_EVAL
3046 if (xp != NULL)
3047 {
3048 xp->xp_arg = uc->uc_compl_arg;
3049 xp->xp_scriptID = uc->uc_scriptID;
3050 }
3051# endif
3052# endif
3053 /* Do not search for further abbreviations
3054 * if this is an exact match. */
3055 matchlen = k;
3056 if (k == len && *np == NUL)
3057 {
3058 if (full != NULL)
3059 *full = TRUE;
3060 amb_local = FALSE;
3061 break;
3062 }
3063 }
3064 }
3065 }
3066
3067 /* Stop if we found a full match or searched all. */
3068 if (j < gap->ga_len || gap == &ucmds)
3069 break;
3070 gap = &ucmds;
3071 }
3072
3073 /* Only found ambiguous matches. */
3074 if (amb_local)
3075 {
3076 if (xp != NULL)
3077 xp->xp_context = EXPAND_UNSUCCESSFUL;
3078 return NULL;
3079 }
3080
3081 /* The match we found may be followed immediately by a number. Move "p"
3082 * back to point to it. */
3083 if (found || possible)
3084 return p + (matchlen - len);
3085 return p;
3086}
3087#endif
3088
Bram Moolenaar071d4272004-06-13 20:20:40 +00003089#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003090static struct cmdmod
3091{
3092 char *name;
3093 int minlen;
3094 int has_count; /* :123verbose :3tab */
3095} cmdmods[] = {
3096 {"aboveleft", 3, FALSE},
3097 {"belowright", 3, FALSE},
3098 {"botright", 2, FALSE},
3099 {"browse", 3, FALSE},
3100 {"confirm", 4, FALSE},
3101 {"hide", 3, FALSE},
3102 {"keepalt", 5, FALSE},
3103 {"keepjumps", 5, FALSE},
3104 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01003105 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003106 {"leftabove", 5, FALSE},
3107 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003108 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003109 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003110 {"rightbelow", 6, FALSE},
3111 {"sandbox", 3, FALSE},
3112 {"silent", 3, FALSE},
3113 {"tab", 3, TRUE},
3114 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003115 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003116 {"verbose", 4, TRUE},
3117 {"vertical", 4, FALSE},
3118};
3119
3120/*
3121 * Return length of a command modifier (including optional count).
3122 * Return zero when it's not a modifier.
3123 */
3124 int
3125modifier_len(cmd)
3126 char_u *cmd;
3127{
3128 int i, j;
3129 char_u *p = cmd;
3130
3131 if (VIM_ISDIGIT(*cmd))
3132 p = skipwhite(skipdigits(cmd));
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003133 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003134 {
3135 for (j = 0; p[j] != NUL; ++j)
3136 if (p[j] != cmdmods[i].name[j])
3137 break;
Bram Moolenaar2d473ab2013-06-12 17:12:24 +02003138 if (!ASCII_ISALPHA(p[j]) && j >= cmdmods[i].minlen
Bram Moolenaared53fb92007-11-24 20:50:24 +00003139 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003140 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003141 }
3142 return 0;
3143}
3144
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145/*
3146 * Return > 0 if an Ex command "name" exists.
3147 * Return 2 if there is an exact match.
3148 * Return 3 if there is an ambiguous match.
3149 */
3150 int
3151cmd_exists(name)
3152 char_u *name;
3153{
3154 exarg_T ea;
3155 int full = FALSE;
3156 int i;
3157 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003158 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003159
3160 /* Check command modifiers. */
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003161 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003162 {
3163 for (j = 0; name[j] != NUL; ++j)
3164 if (name[j] != cmdmods[i].name[j])
3165 break;
3166 if (name[j] == NUL && j >= cmdmods[i].minlen)
3167 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3168 }
3169
Bram Moolenaara9587612006-05-04 21:47:50 +00003170 /* Check built-in commands and user defined commands.
3171 * For ":2match" and ":3match" we need to skip the number. */
3172 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003174 p = find_command(&ea, &full);
3175 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003176 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003177 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3178 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003179 if (*skipwhite(p) != NUL)
3180 return 0; /* trailing garbage */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003181 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3182}
3183#endif
3184
3185/*
3186 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
3187 * we don't need/want deleted. Maybe this could be done better if we didn't
3188 * repeat all this stuff. The only problem is that they may not stay
3189 * perfectly compatible with each other, but then the command line syntax
3190 * probably won't change that much -- webb.
3191 */
3192 char_u *
3193set_one_cmd_context(xp, buff)
3194 expand_T *xp;
3195 char_u *buff; /* buffer for command string */
3196{
3197 char_u *p;
3198 char_u *cmd, *arg;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003199 int len = 0;
3200 exarg_T ea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3202 int compl = EXPAND_NOTHING;
3203#endif
3204#ifdef FEAT_CMDL_COMPL
3205 int delim;
3206#endif
3207 int forceit = FALSE;
3208 int usefilter = FALSE; /* filter instead of file name */
3209
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003210 ExpandInit(xp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211 xp->xp_pattern = buff;
3212 xp->xp_context = EXPAND_COMMANDS; /* Default until we get past command */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003213 ea.argt = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003214
3215/*
3216 * 2. skip comment lines and leading space, colons or bars
3217 */
3218 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
3219 ;
3220 xp->xp_pattern = cmd;
3221
3222 if (*cmd == NUL)
3223 return NULL;
3224 if (*cmd == '"') /* ignore comment lines */
3225 {
3226 xp->xp_context = EXPAND_NOTHING;
3227 return NULL;
3228 }
3229
3230/*
3231 * 3. parse a range specifier of the form: addr [,addr] [;addr] ..
3232 */
3233 cmd = skip_range(cmd, &xp->xp_context);
3234
3235/*
3236 * 4. parse command
3237 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238 xp->xp_pattern = cmd;
3239 if (*cmd == NUL)
3240 return NULL;
3241 if (*cmd == '"')
3242 {
3243 xp->xp_context = EXPAND_NOTHING;
3244 return NULL;
3245 }
3246
3247 if (*cmd == '|' || *cmd == '\n')
3248 return cmd + 1; /* There's another command */
3249
3250 /*
3251 * Isolate the command and search for it in the command table.
3252 * Exceptions:
3253 * - the 'k' command can directly be followed by any character, but
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003254 * do accept "keepmarks", "keepalt" and "keepjumps".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
3256 */
3257 if (*cmd == 'k' && cmd[1] != 'e')
3258 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003259 ea.cmdidx = CMD_k;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003260 p = cmd + 1;
3261 }
3262 else
3263 {
3264 p = cmd;
3265 while (ASCII_ISALPHA(*p) || *p == '*') /* Allow * wild card */
3266 ++p;
3267 /* check for non-alpha command */
3268 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3269 ++p;
Bram Moolenaar9f5d6002013-06-02 19:22:13 +02003270 /* for python 3.x: ":py3*" commands completion */
3271 if (cmd[0] == 'p' && cmd[1] == 'y' && p == cmd + 2 && *p == '3')
Bram Moolenaar893b2d72013-12-11 17:44:38 +01003272 {
Bram Moolenaar9f5d6002013-06-02 19:22:13 +02003273 ++p;
Bram Moolenaar893b2d72013-12-11 17:44:38 +01003274 while (ASCII_ISALPHA(*p) || *p == '*')
3275 ++p;
3276 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003277 len = (int)(p - cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003279 if (len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280 {
3281 xp->xp_context = EXPAND_UNSUCCESSFUL;
3282 return NULL;
3283 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003284 for (ea.cmdidx = (cmdidx_T)0; (int)ea.cmdidx < (int)CMD_SIZE;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01003285 ea.cmdidx = (cmdidx_T)((int)ea.cmdidx + 1))
3286 if (STRNCMP(cmdnames[(int)ea.cmdidx].cmd_name, cmd,
3287 (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003288 break;
3289
3290#ifdef FEAT_USR_CMDS
3291 if (cmd[0] >= 'A' && cmd[0] <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292 while (ASCII_ISALNUM(*p) || *p == '*') /* Allow * wild card */
3293 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003294#endif
3295 }
3296
3297 /*
3298 * If the cursor is touching the command, and it ends in an alpha-numeric
3299 * character, complete the command name.
3300 */
3301 if (*p == NUL && ASCII_ISALNUM(p[-1]))
3302 return NULL;
3303
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003304 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305 {
3306 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
3307 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003308 ea.cmdidx = CMD_substitute;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309 p = cmd + 1;
3310 }
3311#ifdef FEAT_USR_CMDS
3312 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
3313 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003314 ea.cmd = cmd;
3315 p = find_ucmd(&ea, p, NULL, xp,
3316# if defined(FEAT_CMDL_COMPL)
3317 &compl
3318# else
3319 NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320# endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003321 );
Bram Moolenaarebefac62005-12-28 22:39:57 +00003322 if (p == NULL)
3323 ea.cmdidx = CMD_SIZE; /* ambiguous user command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324 }
3325#endif
3326 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003327 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328 {
3329 /* Not still touching the command and it was an illegal one */
3330 xp->xp_context = EXPAND_UNSUCCESSFUL;
3331 return NULL;
3332 }
3333
3334 xp->xp_context = EXPAND_NOTHING; /* Default now that we're past command */
3335
3336 if (*p == '!') /* forced commands */
3337 {
3338 forceit = TRUE;
3339 ++p;
3340 }
3341
3342/*
3343 * 5. parse arguments
3344 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02003345 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003346 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003347
3348 arg = skipwhite(p);
3349
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003350 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003351 {
3352 if (*arg == '>') /* append */
3353 {
3354 if (*++arg == '>')
3355 ++arg;
3356 arg = skipwhite(arg);
3357 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003358 else if (*arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359 {
3360 ++arg;
3361 usefilter = TRUE;
3362 }
3363 }
3364
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003365 if (ea.cmdidx == CMD_read)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366 {
3367 usefilter = forceit; /* :r! filter if forced */
3368 if (*arg == '!') /* :r !filter */
3369 {
3370 ++arg;
3371 usefilter = TRUE;
3372 }
3373 }
3374
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003375 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376 {
3377 while (*arg == *cmd) /* allow any number of '>' or '<' */
3378 ++arg;
3379 arg = skipwhite(arg);
3380 }
3381
3382 /* Does command allow "+command"? */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003383 if ((ea.argt & EDITCMD) && !usefilter && *arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384 {
3385 /* Check if we're in the +command */
3386 p = arg + 1;
3387 arg = skip_cmd_arg(arg, FALSE);
3388
3389 /* Still touching the command after '+'? */
3390 if (*arg == NUL)
3391 return p;
3392
3393 /* Skip space(s) after +command to get to the real argument */
3394 arg = skipwhite(arg);
3395 }
3396
3397 /*
3398 * Check for '|' to separate commands and '"' to start comments.
3399 * Don't do this for ":read !cmd" and ":write !cmd".
3400 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003401 if ((ea.argt & TRLBAR) && !usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402 {
3403 p = arg;
3404 /* ":redir @" is not the start of a comment */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003405 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406 p += 2;
3407 while (*p)
3408 {
3409 if (*p == Ctrl_V)
3410 {
3411 if (p[1] != NUL)
3412 ++p;
3413 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003414 else if ( (*p == '"' && !(ea.argt & NOTRLCOM))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415 || *p == '|' || *p == '\n')
3416 {
3417 if (*(p - 1) != '\\')
3418 {
3419 if (*p == '|' || *p == '\n')
3420 return p + 1;
3421 return NULL; /* It's a comment */
3422 }
3423 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003424 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003425 }
3426 }
3427
3428 /* no arguments allowed */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003429 if (!(ea.argt & EXTRA) && *arg != NUL &&
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430 vim_strchr((char_u *)"|\"", *arg) == NULL)
3431 return NULL;
3432
3433 /* Find start of last argument (argument just before cursor): */
Bram Moolenaar848f8762012-07-25 17:22:23 +02003434 p = buff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435 xp->xp_pattern = p;
Bram Moolenaar09168a72012-08-02 21:24:42 +02003436 len = (int)STRLEN(buff);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003437 while (*p && p < buff + len)
3438 {
3439 if (*p == ' ' || *p == TAB)
3440 {
3441 /* argument starts after a space */
3442 xp->xp_pattern = ++p;
3443 }
3444 else
3445 {
3446 if (*p == '\\' && *(p + 1) != NUL)
3447 ++p; /* skip over escaped character */
3448 mb_ptr_adv(p);
3449 }
3450 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003452 if (ea.argt & XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003454 int c;
3455 int in_quote = FALSE;
3456 char_u *bow = NULL; /* Beginning of word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457
3458 /*
3459 * Allow spaces within back-quotes to count as part of the argument
3460 * being expanded.
3461 */
3462 xp->xp_pattern = skipwhite(arg);
Bram Moolenaar6529c102007-08-18 15:47:34 +00003463 p = xp->xp_pattern;
3464 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003466#ifdef FEAT_MBYTE
3467 if (has_mbyte)
3468 c = mb_ptr2char(p);
3469 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470#endif
Bram Moolenaar6529c102007-08-18 15:47:34 +00003471 c = *p;
3472 if (c == '\\' && p[1] != NUL)
3473 ++p;
3474 else if (c == '`')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475 {
3476 if (!in_quote)
3477 {
3478 xp->xp_pattern = p;
3479 bow = p + 1;
3480 }
3481 in_quote = !in_quote;
3482 }
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003483 /* An argument can contain just about everything, except
3484 * characters that end the command and white space. */
3485 else if (c == '|' || c == '\n' || c == '"' || (vim_iswhite(c)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003486#ifdef SPACE_IN_FILENAME
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003487 && (!(ea.argt & NOSPC) || usefilter)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003488#endif
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003489 ))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003490 {
Bram Moolenaarcf5a5b82008-02-26 20:30:12 +00003491 len = 0; /* avoid getting stuck when space is in 'isfname' */
Bram Moolenaar6529c102007-08-18 15:47:34 +00003492 while (*p != NUL)
3493 {
3494#ifdef FEAT_MBYTE
3495 if (has_mbyte)
3496 c = mb_ptr2char(p);
3497 else
3498#endif
3499 c = *p;
Bram Moolenaardd87969c2007-08-21 13:07:12 +00003500 if (c == '`' || vim_isfilec_or_wc(c))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003501 break;
3502#ifdef FEAT_MBYTE
3503 if (has_mbyte)
3504 len = (*mb_ptr2len)(p);
3505 else
3506#endif
3507 len = 1;
3508 mb_ptr_adv(p);
3509 }
3510 if (in_quote)
3511 bow = p;
3512 else
3513 xp->xp_pattern = p;
3514 p -= len;
3515 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003516 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517 }
3518
3519 /*
3520 * If we are still inside the quotes, and we passed a space, just
3521 * expand from there.
3522 */
3523 if (bow != NULL && in_quote)
3524 xp->xp_pattern = bow;
3525 xp->xp_context = EXPAND_FILES;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003526
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003527 /* For a shell command more chars need to be escaped. */
3528 if (usefilter || ea.cmdidx == CMD_bang)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003529 {
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003530#ifndef BACKSLASH_IN_FILENAME
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003531 xp->xp_shell = TRUE;
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003532#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003533 /* When still after the command name expand executables. */
3534 if (xp->xp_pattern == skipwhite(arg))
Bram Moolenaar5c5b0942007-05-06 12:07:59 +00003535 xp->xp_context = EXPAND_SHELLCMD;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003536 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537
3538 /* Check for environment variable */
3539 if (*xp->xp_pattern == '$'
3540#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
3541 || *xp->xp_pattern == '%'
3542#endif
3543 )
3544 {
3545 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
3546 if (!vim_isIDc(*p))
3547 break;
3548 if (*p == NUL)
3549 {
3550 xp->xp_context = EXPAND_ENV_VARS;
3551 ++xp->xp_pattern;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003552#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3553 /* Avoid that the assignment uses EXPAND_FILES again. */
Bram Moolenaara466c992005-07-09 21:03:22 +00003554 if (compl != EXPAND_USER_DEFINED && compl != EXPAND_USER_LIST)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003555 compl = EXPAND_ENV_VARS;
3556#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557 }
3558 }
Bram Moolenaar24305862012-08-15 14:05:05 +02003559#if defined(FEAT_CMDL_COMPL)
3560 /* Check for user names */
3561 if (*xp->xp_pattern == '~')
3562 {
3563 for (p = xp->xp_pattern + 1; *p != NUL && *p != '/'; ++p)
3564 ;
3565 /* Complete ~user only if it partially matches a user name.
3566 * A full match ~user<Tab> will be replaced by user's home
3567 * directory i.e. something like ~user<Tab> -> /home/user/ */
3568 if (*p == NUL && p > xp->xp_pattern + 1
3569 && match_user(xp->xp_pattern + 1) == 1)
3570 {
3571 xp->xp_context = EXPAND_USER;
3572 ++xp->xp_pattern;
3573 }
3574 }
3575#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003576 }
3577
3578/*
3579 * 6. switch on command name
3580 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003581 switch (ea.cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003583 case CMD_find:
3584 case CMD_sfind:
3585 case CMD_tabfind:
Bram Moolenaarc24b6972010-08-16 22:34:29 +02003586 if (xp->xp_context == EXPAND_FILES)
3587 xp->xp_context = EXPAND_FILES_IN_PATH;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003588 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589 case CMD_cd:
3590 case CMD_chdir:
3591 case CMD_lcd:
3592 case CMD_lchdir:
3593 if (xp->xp_context == EXPAND_FILES)
3594 xp->xp_context = EXPAND_DIRECTORIES;
3595 break;
3596 case CMD_help:
3597 xp->xp_context = EXPAND_HELP;
3598 xp->xp_pattern = arg;
3599 break;
3600
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003601 /* Command modifiers: return the argument.
3602 * Also for commands with an argument that is a command. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603 case CMD_aboveleft:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003604 case CMD_argdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605 case CMD_belowright:
3606 case CMD_botright:
3607 case CMD_browse:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003608 case CMD_bufdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003609 case CMD_confirm:
Bram Moolenaardf177f62005-02-22 08:39:57 +00003610 case CMD_debug:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611 case CMD_folddoclosed:
3612 case CMD_folddoopen:
3613 case CMD_hide:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003614 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003615 case CMD_keepjumps:
3616 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01003617 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618 case CMD_leftabove:
3619 case CMD_lockmarks:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003620 case CMD_noautocmd:
3621 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622 case CMD_rightbelow:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003623 case CMD_sandbox:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624 case CMD_silent:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003625 case CMD_tab:
Bram Moolenaar70baa402013-06-16 16:14:03 +02003626 case CMD_tabdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003627 case CMD_topleft:
3628 case CMD_verbose:
3629 case CMD_vertical:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003630 case CMD_windo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003631 return arg;
3632
Bram Moolenaar4f688582007-07-24 12:34:30 +00003633#ifdef FEAT_CMDL_COMPL
3634# ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar071d4272004-06-13 20:20:40 +00003635 case CMD_match:
3636 if (*arg == NUL || !ends_excmd(*arg))
3637 {
Bram Moolenaar4f688582007-07-24 12:34:30 +00003638 /* also complete "None" */
3639 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003640 arg = skipwhite(skiptowhite(arg));
3641 if (*arg != NUL)
3642 {
3643 xp->xp_context = EXPAND_NOTHING;
3644 arg = skip_regexp(arg + 1, *arg, p_magic, NULL);
3645 }
3646 }
3647 return find_nextcmd(arg);
Bram Moolenaar4f688582007-07-24 12:34:30 +00003648# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003649
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650/*
3651 * All completion for the +cmdline_compl feature goes here.
3652 */
3653
3654# ifdef FEAT_USR_CMDS
3655 case CMD_command:
3656 /* Check for attributes */
3657 while (*arg == '-')
3658 {
3659 arg++; /* Skip "-" */
3660 p = skiptowhite(arg);
3661 if (*p == NUL)
3662 {
3663 /* Cursor is still in the attribute */
3664 p = vim_strchr(arg, '=');
3665 if (p == NULL)
3666 {
3667 /* No "=", so complete attribute names */
3668 xp->xp_context = EXPAND_USER_CMD_FLAGS;
3669 xp->xp_pattern = arg;
3670 return NULL;
3671 }
3672
3673 /* For the -complete and -nargs attributes, we complete
3674 * their arguments as well.
3675 */
3676 if (STRNICMP(arg, "complete", p - arg) == 0)
3677 {
3678 xp->xp_context = EXPAND_USER_COMPLETE;
3679 xp->xp_pattern = p + 1;
3680 return NULL;
3681 }
3682 else if (STRNICMP(arg, "nargs", p - arg) == 0)
3683 {
3684 xp->xp_context = EXPAND_USER_NARGS;
3685 xp->xp_pattern = p + 1;
3686 return NULL;
3687 }
3688 return NULL;
3689 }
3690 arg = skipwhite(p);
3691 }
3692
3693 /* After the attributes comes the new command name */
3694 p = skiptowhite(arg);
3695 if (*p == NUL)
3696 {
3697 xp->xp_context = EXPAND_USER_COMMANDS;
3698 xp->xp_pattern = arg;
3699 break;
3700 }
3701
3702 /* And finally comes a normal command */
3703 return skipwhite(p);
3704
3705 case CMD_delcommand:
3706 xp->xp_context = EXPAND_USER_COMMANDS;
3707 xp->xp_pattern = arg;
3708 break;
3709# endif
3710
3711 case CMD_global:
3712 case CMD_vglobal:
3713 delim = *arg; /* get the delimiter */
3714 if (delim)
3715 ++arg; /* skip delimiter if there is one */
3716
3717 while (arg[0] != NUL && arg[0] != delim)
3718 {
3719 if (arg[0] == '\\' && arg[1] != NUL)
3720 ++arg;
3721 ++arg;
3722 }
3723 if (arg[0] != NUL)
3724 return arg + 1;
3725 break;
3726 case CMD_and:
3727 case CMD_substitute:
3728 delim = *arg;
3729 if (delim)
3730 {
3731 /* skip "from" part */
3732 ++arg;
3733 arg = skip_regexp(arg, delim, p_magic, NULL);
3734 }
3735 /* skip "to" part */
3736 while (arg[0] != NUL && arg[0] != delim)
3737 {
3738 if (arg[0] == '\\' && arg[1] != NUL)
3739 ++arg;
3740 ++arg;
3741 }
3742 if (arg[0] != NUL) /* skip delimiter */
3743 ++arg;
3744 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
3745 ++arg;
3746 if (arg[0] != NUL)
3747 return arg;
3748 break;
3749 case CMD_isearch:
3750 case CMD_dsearch:
3751 case CMD_ilist:
3752 case CMD_dlist:
3753 case CMD_ijump:
3754 case CMD_psearch:
3755 case CMD_djump:
3756 case CMD_isplit:
3757 case CMD_dsplit:
3758 arg = skipwhite(skipdigits(arg)); /* skip count */
3759 if (*arg == '/') /* Match regexp, not just whole words */
3760 {
3761 for (++arg; *arg && *arg != '/'; arg++)
3762 if (*arg == '\\' && arg[1] != NUL)
3763 arg++;
3764 if (*arg)
3765 {
3766 arg = skipwhite(arg + 1);
3767
3768 /* Check for trailing illegal characters */
3769 if (*arg && vim_strchr((char_u *)"|\"\n", *arg) == NULL)
3770 xp->xp_context = EXPAND_NOTHING;
3771 else
3772 return arg;
3773 }
3774 }
3775 break;
3776#ifdef FEAT_AUTOCMD
3777 case CMD_autocmd:
3778 return set_context_in_autocmd(xp, arg, FALSE);
3779
3780 case CMD_doautocmd:
Bram Moolenaar73a9d7b2008-11-06 16:16:44 +00003781 case CMD_doautoall:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003782 return set_context_in_autocmd(xp, arg, TRUE);
3783#endif
3784 case CMD_set:
3785 set_context_in_set_cmd(xp, arg, 0);
3786 break;
3787 case CMD_setglobal:
3788 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
3789 break;
3790 case CMD_setlocal:
3791 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
3792 break;
3793 case CMD_tag:
3794 case CMD_stag:
3795 case CMD_ptag:
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00003796 case CMD_ltag:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003797 case CMD_tselect:
3798 case CMD_stselect:
3799 case CMD_ptselect:
3800 case CMD_tjump:
3801 case CMD_stjump:
3802 case CMD_ptjump:
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003803 if (*p_wop != NUL)
3804 xp->xp_context = EXPAND_TAGS_LISTFILES;
3805 else
3806 xp->xp_context = EXPAND_TAGS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807 xp->xp_pattern = arg;
3808 break;
3809 case CMD_augroup:
3810 xp->xp_context = EXPAND_AUGROUP;
3811 xp->xp_pattern = arg;
3812 break;
3813#ifdef FEAT_SYN_HL
3814 case CMD_syntax:
3815 set_context_in_syntax_cmd(xp, arg);
3816 break;
3817#endif
3818#ifdef FEAT_EVAL
3819 case CMD_let:
3820 case CMD_if:
3821 case CMD_elseif:
3822 case CMD_while:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00003823 case CMD_for:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003824 case CMD_echo:
3825 case CMD_echon:
3826 case CMD_execute:
3827 case CMD_echomsg:
3828 case CMD_echoerr:
3829 case CMD_call:
3830 case CMD_return:
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003831 set_context_for_expression(xp, arg, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003832 break;
3833
3834 case CMD_unlet:
3835 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3836 arg = xp->xp_pattern + 1;
3837 xp->xp_context = EXPAND_USER_VARS;
3838 xp->xp_pattern = arg;
3839 break;
3840
3841 case CMD_function:
3842 case CMD_delfunction:
3843 xp->xp_context = EXPAND_USER_FUNC;
3844 xp->xp_pattern = arg;
3845 break;
3846
3847 case CMD_echohl:
Bram Moolenaar4f688582007-07-24 12:34:30 +00003848 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003849 break;
3850#endif
3851 case CMD_highlight:
3852 set_context_in_highlight_cmd(xp, arg);
3853 break;
Bram Moolenaarf4580d82009-03-18 11:52:53 +00003854#ifdef FEAT_CSCOPE
3855 case CMD_cscope:
Bram Moolenaar7bfef802009-04-22 14:25:01 +00003856 case CMD_lcscope:
3857 case CMD_scscope:
3858 set_context_in_cscope_cmd(xp, arg, ea.cmdidx);
Bram Moolenaarf4580d82009-03-18 11:52:53 +00003859 break;
3860#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00003861#ifdef FEAT_SIGNS
3862 case CMD_sign:
3863 set_context_in_sign_cmd(xp, arg);
3864 break;
3865#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003866#ifdef FEAT_LISTCMDS
3867 case CMD_bdelete:
3868 case CMD_bwipeout:
3869 case CMD_bunload:
3870 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3871 arg = xp->xp_pattern + 1;
3872 /*FALLTHROUGH*/
3873 case CMD_buffer:
3874 case CMD_sbuffer:
3875 case CMD_checktime:
3876 xp->xp_context = EXPAND_BUFFERS;
3877 xp->xp_pattern = arg;
3878 break;
3879#endif
3880#ifdef FEAT_USR_CMDS
3881 case CMD_USER:
3882 case CMD_USER_BUF:
3883 if (compl != EXPAND_NOTHING)
3884 {
3885 /* XFILE: file names are handled above */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003886 if (!(ea.argt & XFILE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887 {
3888# ifdef FEAT_MENU
3889 if (compl == EXPAND_MENUS)
3890 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
3891# endif
3892 if (compl == EXPAND_COMMANDS)
3893 return arg;
3894 if (compl == EXPAND_MAPPINGS)
3895 return set_context_in_map_cmd(xp, (char_u *)"map",
3896 arg, forceit, FALSE, FALSE, CMD_map);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003897 /* Find start of last argument. */
3898 p = arg;
3899 while (*p)
3900 {
3901 if (*p == ' ')
Bram Moolenaar848f8762012-07-25 17:22:23 +02003902 /* argument starts after a space */
3903 arg = p + 1;
Bram Moolenaara07c8312012-07-27 21:12:07 +02003904 else if (*p == '\\' && *(p + 1) != NUL)
3905 ++p; /* skip over escaped character */
3906 mb_ptr_adv(p);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003907 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003908 xp->xp_pattern = arg;
3909 }
3910 xp->xp_context = compl;
3911 }
3912 break;
3913#endif
3914 case CMD_map: case CMD_noremap:
3915 case CMD_nmap: case CMD_nnoremap:
3916 case CMD_vmap: case CMD_vnoremap:
3917 case CMD_omap: case CMD_onoremap:
3918 case CMD_imap: case CMD_inoremap:
3919 case CMD_cmap: case CMD_cnoremap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003920 case CMD_lmap: case CMD_lnoremap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02003921 case CMD_smap: case CMD_snoremap:
3922 case CMD_xmap: case CMD_xnoremap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003923 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003924 FALSE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003925 case CMD_unmap:
3926 case CMD_nunmap:
3927 case CMD_vunmap:
3928 case CMD_ounmap:
3929 case CMD_iunmap:
3930 case CMD_cunmap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003931 case CMD_lunmap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02003932 case CMD_sunmap:
3933 case CMD_xunmap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003934 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003935 FALSE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003936 case CMD_abbreviate: case CMD_noreabbrev:
3937 case CMD_cabbrev: case CMD_cnoreabbrev:
3938 case CMD_iabbrev: case CMD_inoreabbrev:
3939 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003940 TRUE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003941 case CMD_unabbreviate:
3942 case CMD_cunabbrev:
3943 case CMD_iunabbrev:
3944 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003945 TRUE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946#ifdef FEAT_MENU
3947 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
3948 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
3949 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
3950 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
3951 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
3952 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
3953 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
3954 case CMD_tmenu: case CMD_tunmenu:
3955 case CMD_popup: case CMD_tearoff: case CMD_emenu:
3956 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
3957#endif
3958
3959 case CMD_colorscheme:
3960 xp->xp_context = EXPAND_COLORS;
3961 xp->xp_pattern = arg;
3962 break;
3963
3964 case CMD_compiler:
3965 xp->xp_context = EXPAND_COMPILER;
3966 xp->xp_pattern = arg;
3967 break;
3968
Bram Moolenaar883f5d02010-06-21 06:24:34 +02003969 case CMD_ownsyntax:
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02003970 xp->xp_context = EXPAND_OWNSYNTAX;
3971 xp->xp_pattern = arg;
3972 break;
3973
3974 case CMD_setfiletype:
Bram Moolenaar883f5d02010-06-21 06:24:34 +02003975 xp->xp_context = EXPAND_FILETYPE;
3976 xp->xp_pattern = arg;
3977 break;
3978
Bram Moolenaar071d4272004-06-13 20:20:40 +00003979#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
3980 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
3981 case CMD_language:
Bram Moolenaara660dc82011-05-25 12:51:22 +02003982 p = skiptowhite(arg);
3983 if (*p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003984 {
3985 xp->xp_context = EXPAND_LANGUAGE;
3986 xp->xp_pattern = arg;
3987 }
3988 else
Bram Moolenaara660dc82011-05-25 12:51:22 +02003989 {
3990 if ( STRNCMP(arg, "messages", p - arg) == 0
3991 || STRNCMP(arg, "ctype", p - arg) == 0
3992 || STRNCMP(arg, "time", p - arg) == 0)
3993 {
3994 xp->xp_context = EXPAND_LOCALES;
3995 xp->xp_pattern = skipwhite(p);
3996 }
3997 else
3998 xp->xp_context = EXPAND_NOTHING;
3999 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000 break;
4001#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01004002#if defined(FEAT_PROFILE)
4003 case CMD_profile:
4004 set_context_in_profile_cmd(xp, arg);
4005 break;
4006#endif
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01004007 case CMD_behave:
4008 xp->xp_context = EXPAND_BEHAVE;
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004009 xp->xp_pattern = arg;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01004010 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004012#if defined(FEAT_CMDHIST)
4013 case CMD_history:
4014 xp->xp_context = EXPAND_HISTORY;
4015 xp->xp_pattern = arg;
4016 break;
4017#endif
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02004018#if defined(FEAT_PROFILE)
4019 case CMD_syntime:
4020 xp->xp_context = EXPAND_SYNTIME;
4021 xp->xp_pattern = arg;
4022 break;
4023#endif
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004024
Bram Moolenaar071d4272004-06-13 20:20:40 +00004025#endif /* FEAT_CMDL_COMPL */
4026
4027 default:
4028 break;
4029 }
4030 return NULL;
4031}
4032
4033/*
4034 * skip a range specifier of the form: addr [,addr] [;addr] ..
4035 *
4036 * Backslashed delimiters after / or ? will be skipped, and commands will
4037 * not be expanded between /'s and ?'s or after "'".
4038 *
Bram Moolenaardf177f62005-02-22 08:39:57 +00004039 * Also skip white space and ":" characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004040 * Returns the "cmd" pointer advanced to beyond the range.
4041 */
4042 char_u *
4043skip_range(cmd, ctx)
4044 char_u *cmd;
4045 int *ctx; /* pointer to xp_context or NULL */
4046{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004047 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004048
Bram Moolenaardf177f62005-02-22 08:39:57 +00004049 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004050 {
4051 if (*cmd == '\'')
4052 {
4053 if (*++cmd == NUL && ctx != NULL)
4054 *ctx = EXPAND_NOTHING;
4055 }
4056 else if (*cmd == '/' || *cmd == '?')
4057 {
4058 delim = *cmd++;
4059 while (*cmd != NUL && *cmd != delim)
4060 if (*cmd++ == '\\' && *cmd != NUL)
4061 ++cmd;
4062 if (*cmd == NUL && ctx != NULL)
4063 *ctx = EXPAND_NOTHING;
4064 }
4065 if (*cmd != NUL)
4066 ++cmd;
4067 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004068
4069 /* Skip ":" and white space. */
4070 while (*cmd == ':')
4071 cmd = skipwhite(cmd + 1);
4072
Bram Moolenaar071d4272004-06-13 20:20:40 +00004073 return cmd;
4074}
4075
4076/*
4077 * get a single EX address
4078 *
4079 * Set ptr to the next character after the part that was interpreted.
4080 * Set ptr to NULL when an error is encountered.
4081 *
4082 * Return MAXLNUM when no Ex address was found.
4083 */
4084 static linenr_T
4085get_address(ptr, skip, to_other_file)
4086 char_u **ptr;
4087 int skip; /* only skip the address, don't use it */
4088 int to_other_file; /* flag: may jump to other file */
4089{
4090 int c;
4091 int i;
4092 long n;
4093 char_u *cmd;
4094 pos_T pos;
4095 pos_T *fp;
4096 linenr_T lnum;
4097
4098 cmd = skipwhite(*ptr);
4099 lnum = MAXLNUM;
4100 do
4101 {
4102 switch (*cmd)
4103 {
4104 case '.': /* '.' - Cursor position */
4105 ++cmd;
4106 lnum = curwin->w_cursor.lnum;
4107 break;
4108
4109 case '$': /* '$' - last line */
4110 ++cmd;
4111 lnum = curbuf->b_ml.ml_line_count;
4112 break;
4113
4114 case '\'': /* ''' - mark */
4115 if (*++cmd == NUL)
4116 {
4117 cmd = NULL;
4118 goto error;
4119 }
4120 if (skip)
4121 ++cmd;
4122 else
4123 {
4124 /* Only accept a mark in another file when it is
4125 * used by itself: ":'M". */
4126 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
4127 ++cmd;
4128 if (fp == (pos_T *)-1)
4129 /* Jumped to another file. */
4130 lnum = curwin->w_cursor.lnum;
4131 else
4132 {
4133 if (check_mark(fp) == FAIL)
4134 {
4135 cmd = NULL;
4136 goto error;
4137 }
4138 lnum = fp->lnum;
4139 }
4140 }
4141 break;
4142
4143 case '/':
4144 case '?': /* '/' or '?' - search */
4145 c = *cmd++;
4146 if (skip) /* skip "/pat/" */
4147 {
4148 cmd = skip_regexp(cmd, c, (int)p_magic, NULL);
4149 if (*cmd == c)
4150 ++cmd;
4151 }
4152 else
4153 {
4154 pos = curwin->w_cursor; /* save curwin->w_cursor */
4155 /*
4156 * When '/' or '?' follows another address, start
4157 * from there.
4158 */
4159 if (lnum != MAXLNUM)
4160 curwin->w_cursor.lnum = lnum;
4161 /*
4162 * Start a forward search at the end of the line.
4163 * Start a backward search at the start of the line.
4164 * This makes sure we never match in the current
4165 * line, and can match anywhere in the
4166 * next/previous line.
4167 */
4168 if (c == '/')
4169 curwin->w_cursor.col = MAXCOL;
4170 else
4171 curwin->w_cursor.col = 0;
4172 searchcmdlen = 0;
4173 if (!do_search(NULL, c, cmd, 1L,
Bram Moolenaaraad86642008-03-10 20:34:59 +00004174 SEARCH_HIS | SEARCH_MSG, NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004175 {
4176 curwin->w_cursor = pos;
4177 cmd = NULL;
4178 goto error;
4179 }
4180 lnum = curwin->w_cursor.lnum;
4181 curwin->w_cursor = pos;
4182 /* adjust command string pointer */
4183 cmd += searchcmdlen;
4184 }
4185 break;
4186
4187 case '\\': /* "\?", "\/" or "\&", repeat search */
4188 ++cmd;
4189 if (*cmd == '&')
4190 i = RE_SUBST;
4191 else if (*cmd == '?' || *cmd == '/')
4192 i = RE_SEARCH;
4193 else
4194 {
4195 EMSG(_(e_backslash));
4196 cmd = NULL;
4197 goto error;
4198 }
4199
4200 if (!skip)
4201 {
4202 /*
4203 * When search follows another address, start from
4204 * there.
4205 */
4206 if (lnum != MAXLNUM)
4207 pos.lnum = lnum;
4208 else
4209 pos.lnum = curwin->w_cursor.lnum;
4210
4211 /*
4212 * Start the search just like for the above
4213 * do_search().
4214 */
4215 if (*cmd != '?')
4216 pos.col = MAXCOL;
4217 else
4218 pos.col = 0;
4219 if (searchit(curwin, curbuf, &pos,
4220 *cmd == '?' ? BACKWARD : FORWARD,
Bram Moolenaaraad86642008-03-10 20:34:59 +00004221 (char_u *)"", 1L, SEARCH_MSG,
Bram Moolenaar76929292008-01-06 19:07:36 +00004222 i, (linenr_T)0, NULL) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223 lnum = pos.lnum;
4224 else
4225 {
4226 cmd = NULL;
4227 goto error;
4228 }
4229 }
4230 ++cmd;
4231 break;
4232
4233 default:
4234 if (VIM_ISDIGIT(*cmd)) /* absolute line number */
4235 lnum = getdigits(&cmd);
4236 }
4237
4238 for (;;)
4239 {
4240 cmd = skipwhite(cmd);
4241 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4242 break;
4243
4244 if (lnum == MAXLNUM)
4245 lnum = curwin->w_cursor.lnum; /* "+1" is same as ".+1" */
4246 if (VIM_ISDIGIT(*cmd))
4247 i = '+'; /* "number" is same as "+number" */
4248 else
4249 i = *cmd++;
4250 if (!VIM_ISDIGIT(*cmd)) /* '+' is '+1', but '+0' is not '+1' */
4251 n = 1;
4252 else
4253 n = getdigits(&cmd);
4254 if (i == '-')
4255 lnum -= n;
4256 else
4257 lnum += n;
4258 }
4259 } while (*cmd == '/' || *cmd == '?');
4260
4261error:
4262 *ptr = cmd;
4263 return lnum;
4264}
4265
4266/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004267 * Get flags from an Ex command argument.
4268 */
4269 static void
4270get_flags(eap)
4271 exarg_T *eap;
4272{
4273 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4274 {
4275 if (*eap->arg == 'l')
4276 eap->flags |= EXFLAG_LIST;
4277 else if (*eap->arg == 'p')
4278 eap->flags |= EXFLAG_PRINT;
4279 else
4280 eap->flags |= EXFLAG_NR;
4281 eap->arg = skipwhite(eap->arg + 1);
4282 }
4283}
4284
4285/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004286 * Function called for command which is Not Implemented. NI!
4287 */
4288 void
4289ex_ni(eap)
4290 exarg_T *eap;
4291{
4292 if (!eap->skip)
4293 eap->errmsg = (char_u *)N_("E319: Sorry, the command is not available in this version");
4294}
4295
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004296#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004297/*
4298 * Function called for script command which is Not Implemented. NI!
4299 * Skips over ":perl <<EOF" constructs.
4300 */
4301 static void
4302ex_script_ni(eap)
4303 exarg_T *eap;
4304{
4305 if (!eap->skip)
4306 ex_ni(eap);
4307 else
4308 vim_free(script_get(eap, eap->arg));
4309}
4310#endif
4311
4312/*
4313 * Check range in Ex command for validity.
4314 * Return NULL when valid, error message when invalid.
4315 */
4316 static char_u *
4317invalid_range(eap)
4318 exarg_T *eap;
4319{
4320 if ( eap->line1 < 0
4321 || eap->line2 < 0
4322 || eap->line1 > eap->line2
4323 || ((eap->argt & RANGE)
4324 && !(eap->argt & NOTADR)
4325 && eap->line2 > curbuf->b_ml.ml_line_count
4326#ifdef FEAT_DIFF
4327 + (eap->cmdidx == CMD_diffget)
4328#endif
4329 ))
4330 return (char_u *)_(e_invrange);
4331 return NULL;
4332}
4333
4334/*
4335 * Correct the range for zero line number, if required.
4336 */
4337 static void
4338correct_range(eap)
4339 exarg_T *eap;
4340{
4341 if (!(eap->argt & ZEROR)) /* zero in range not allowed */
4342 {
4343 if (eap->line1 == 0)
4344 eap->line1 = 1;
4345 if (eap->line2 == 0)
4346 eap->line2 = 1;
4347 }
4348}
4349
Bram Moolenaar748bf032005-02-02 23:04:36 +00004350#ifdef FEAT_QUICKFIX
4351static char_u *skip_grep_pat __ARGS((exarg_T *eap));
4352
4353/*
4354 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4355 * pattern. Otherwise return eap->arg.
4356 */
4357 static char_u *
4358skip_grep_pat(eap)
4359 exarg_T *eap;
4360{
4361 char_u *p = eap->arg;
4362
Bram Moolenaara37420f2006-02-04 22:37:47 +00004363 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4364 || eap->cmdidx == CMD_vimgrepadd
4365 || eap->cmdidx == CMD_lvimgrepadd
4366 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004367 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004368 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004369 if (p == NULL)
4370 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004371 }
4372 return p;
4373}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004374
4375/*
4376 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4377 * in the command line, so that things like % get expanded.
4378 */
4379 static char_u *
4380replace_makeprg(eap, p, cmdlinep)
4381 exarg_T *eap;
4382 char_u *p;
4383 char_u **cmdlinep;
4384{
4385 char_u *new_cmdline;
4386 char_u *program;
4387 char_u *pos;
4388 char_u *ptr;
4389 int len;
4390 int i;
4391
4392 /*
4393 * Don't do it when ":vimgrep" is used for ":grep".
4394 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004395 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4396 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4397 || eap->cmdidx == CMD_grepadd
4398 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004399 && !grep_internal(eap->cmdidx))
4400 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004401 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4402 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004403 {
4404 if (*curbuf->b_p_gp == NUL)
4405 program = p_gp;
4406 else
4407 program = curbuf->b_p_gp;
4408 }
4409 else
4410 {
4411 if (*curbuf->b_p_mp == NUL)
4412 program = p_mp;
4413 else
4414 program = curbuf->b_p_mp;
4415 }
4416
4417 p = skipwhite(p);
4418
4419 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4420 {
4421 /* replace $* by given arguments */
4422 i = 1;
4423 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4424 ++i;
4425 len = (int)STRLEN(p);
4426 new_cmdline = alloc((int)(STRLEN(program) + i * (len - 2) + 1));
4427 if (new_cmdline == NULL)
4428 return NULL; /* out of memory */
4429 ptr = new_cmdline;
4430 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4431 {
4432 i = (int)(pos - program);
4433 STRNCPY(ptr, program, i);
4434 STRCPY(ptr += i, p);
4435 ptr += len;
4436 program = pos + 2;
4437 }
4438 STRCPY(ptr, program);
4439 }
4440 else
4441 {
4442 new_cmdline = alloc((int)(STRLEN(program) + STRLEN(p) + 2));
4443 if (new_cmdline == NULL)
4444 return NULL; /* out of memory */
4445 STRCPY(new_cmdline, program);
4446 STRCAT(new_cmdline, " ");
4447 STRCAT(new_cmdline, p);
4448 }
4449 msg_make(p);
4450
4451 /* 'eap->cmd' is not set here, because it is not used at CMD_make */
4452 vim_free(*cmdlinep);
4453 *cmdlinep = new_cmdline;
4454 p = new_cmdline;
4455 }
4456 return p;
4457}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004458#endif
4459
Bram Moolenaar071d4272004-06-13 20:20:40 +00004460/*
4461 * Expand file name in Ex command argument.
4462 * Return FAIL for failure, OK otherwise.
4463 */
4464 int
4465expand_filename(eap, cmdlinep, errormsgp)
4466 exarg_T *eap;
4467 char_u **cmdlinep;
4468 char_u **errormsgp;
4469{
4470 int has_wildcards; /* need to expand wildcards */
4471 char_u *repl;
4472 int srclen;
4473 char_u *p;
4474 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004475 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476
Bram Moolenaar748bf032005-02-02 23:04:36 +00004477#ifdef FEAT_QUICKFIX
4478 /* Skip a regexp pattern for ":vimgrep[add] pat file..." */
4479 p = skip_grep_pat(eap);
4480#else
4481 p = eap->arg;
4482#endif
4483
Bram Moolenaar071d4272004-06-13 20:20:40 +00004484 /*
4485 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4486 * the file name contains a wildcard it should not cause expanding.
4487 * (it will be expanded anyway if there is a wildcard before replacing).
4488 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004489 has_wildcards = mch_has_wildcard(p);
4490 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004491 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004492#ifdef FEAT_EVAL
4493 /* Skip over `=expr`, wildcards in it are not expanded. */
4494 if (p[0] == '`' && p[1] == '=')
4495 {
4496 p += 2;
4497 (void)skip_expr(&p);
4498 if (*p == '`')
4499 ++p;
4500 continue;
4501 }
4502#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004503 /*
4504 * Quick check if this cannot be the start of a special string.
4505 * Also removes backslash before '%', '#' and '<'.
4506 */
4507 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4508 {
4509 ++p;
4510 continue;
4511 }
4512
4513 /*
4514 * Try to find a match at this position.
4515 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004516 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
4517 errormsgp, &escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004518 if (*errormsgp != NULL) /* error detected */
4519 return FAIL;
4520 if (repl == NULL) /* no match found */
4521 {
4522 p += srclen;
4523 continue;
4524 }
4525
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004526 /* Wildcards won't be expanded below, the replacement is taken
4527 * literally. But do expand "~/file", "~user/file" and "$HOME/file". */
4528 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4529 {
4530 char_u *l = repl;
4531
4532 repl = expand_env_save(repl);
4533 vim_free(l);
4534 }
4535
Bram Moolenaar63b92542007-03-27 14:57:09 +00004536 /* Need to escape white space et al. with a backslash.
4537 * Don't do this for:
4538 * - replacement that already has been escaped: "##"
4539 * - shell commands (may have to use quotes instead).
4540 * - non-unix systems when there is a single argument (spaces don't
4541 * separate arguments then).
4542 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004543 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00004544 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00004545 && eap->cmdidx != CMD_bang
4546 && eap->cmdidx != CMD_make
Bram Moolenaara37420f2006-02-04 22:37:47 +00004547 && eap->cmdidx != CMD_lmake
Bram Moolenaar071d4272004-06-13 20:20:40 +00004548 && eap->cmdidx != CMD_grep
Bram Moolenaara37420f2006-02-04 22:37:47 +00004549 && eap->cmdidx != CMD_lgrep
Bram Moolenaar071d4272004-06-13 20:20:40 +00004550 && eap->cmdidx != CMD_grepadd
Bram Moolenaara37420f2006-02-04 22:37:47 +00004551 && eap->cmdidx != CMD_lgrepadd
Bram Moolenaar071d4272004-06-13 20:20:40 +00004552#ifndef UNIX
4553 && !(eap->argt & NOSPC)
4554#endif
4555 )
4556 {
4557 char_u *l;
4558#ifdef BACKSLASH_IN_FILENAME
4559 /* Don't escape a backslash here, because rem_backslash() doesn't
4560 * remove it later. */
4561 static char_u *nobslash = (char_u *)" \t\"|";
4562# define ESCAPE_CHARS nobslash
4563#else
4564# define ESCAPE_CHARS escape_chars
4565#endif
4566
4567 for (l = repl; *l; ++l)
4568 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
4569 {
4570 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
4571 if (l != NULL)
4572 {
4573 vim_free(repl);
4574 repl = l;
4575 }
4576 break;
4577 }
4578 }
4579
4580 /* For a shell command a '!' must be escaped. */
4581 if ((eap->usefilter || eap->cmdidx == CMD_bang)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004582 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004583 {
4584 char_u *l;
4585
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004586 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004587 if (l != NULL)
4588 {
4589 vim_free(repl);
4590 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004591 }
4592 }
4593
4594 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
4595 vim_free(repl);
4596 if (p == NULL)
4597 return FAIL;
4598 }
4599
4600 /*
4601 * One file argument: Expand wildcards.
4602 * Don't do this with ":r !command" or ":w !command".
4603 */
4604 if ((eap->argt & NOSPC) && !eap->usefilter)
4605 {
4606 /*
4607 * May do this twice:
4608 * 1. Replace environment variables.
4609 * 2. Replace any other wildcards, remove backslashes.
4610 */
4611 for (n = 1; n <= 2; ++n)
4612 {
4613 if (n == 2)
4614 {
4615#ifdef UNIX
4616 /*
4617 * Only for Unix we check for more than one file name.
4618 * For other systems spaces are considered to be part
4619 * of the file name.
4620 * Only check here if there is no wildcard, otherwise
4621 * ExpandOne() will check for errors. This allows
4622 * ":e `ls ve*.c`" on Unix.
4623 */
4624 if (!has_wildcards)
4625 for (p = eap->arg; *p; ++p)
4626 {
4627 /* skip escaped characters */
4628 if (p[1] && (*p == '\\' || *p == Ctrl_V))
4629 ++p;
4630 else if (vim_iswhite(*p))
4631 {
4632 *errormsgp = (char_u *)_("E172: Only one file name allowed");
4633 return FAIL;
4634 }
4635 }
4636#endif
4637
4638 /*
4639 * Halve the number of backslashes (this is Vi compatible).
4640 * For Unix and OS/2, when wildcards are expanded, this is
4641 * done by ExpandOne() below.
4642 */
4643#if defined(UNIX) || defined(OS2)
4644 if (!has_wildcards)
4645#endif
4646 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004647 }
4648
4649 if (has_wildcards)
4650 {
4651 if (n == 1)
4652 {
4653 /*
4654 * First loop: May expand environment variables. This
4655 * can be done much faster with expand_env() than with
4656 * something else (e.g., calling a shell).
4657 * After expanding environment variables, check again
4658 * if there are still wildcards present.
4659 */
4660 if (vim_strchr(eap->arg, '$') != NULL
4661 || vim_strchr(eap->arg, '~') != NULL)
4662 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00004663 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004664 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004665 has_wildcards = mch_has_wildcard(NameBuff);
4666 p = NameBuff;
4667 }
4668 else
4669 p = NULL;
4670 }
4671 else /* n == 2 */
4672 {
4673 expand_T xpc;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004674 int options = WILD_LIST_NOTFOUND|WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004675
4676 ExpandInit(&xpc);
4677 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004678 if (p_wic)
4679 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004680 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01004681 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004682 if (p == NULL)
4683 return FAIL;
4684 }
4685 if (p != NULL)
4686 {
4687 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
4688 p, cmdlinep);
4689 if (n == 2) /* p came from ExpandOne() */
4690 vim_free(p);
4691 }
4692 }
4693 }
4694 }
4695 return OK;
4696}
4697
4698/*
4699 * Replace part of the command line, keeping eap->cmd, eap->arg and
4700 * eap->nextcmd correct.
4701 * "src" points to the part that is to be replaced, of length "srclen".
4702 * "repl" is the replacement string.
4703 * Returns a pointer to the character after the replaced string.
4704 * Returns NULL for failure.
4705 */
4706 static char_u *
4707repl_cmdline(eap, src, srclen, repl, cmdlinep)
4708 exarg_T *eap;
4709 char_u *src;
4710 int srclen;
4711 char_u *repl;
4712 char_u **cmdlinep;
4713{
4714 int len;
4715 int i;
4716 char_u *new_cmdline;
4717
4718 /*
4719 * The new command line is build in new_cmdline[].
4720 * First allocate it.
4721 * Careful: a "+cmd" argument may have been NUL terminated.
4722 */
4723 len = (int)STRLEN(repl);
4724 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004725 if (eap->nextcmd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004726 i += (int)STRLEN(eap->nextcmd);/* add space for next command */
4727 if ((new_cmdline = alloc((unsigned)i)) == NULL)
4728 return NULL; /* out of memory! */
4729
4730 /*
4731 * Copy the stuff before the expanded part.
4732 * Copy the expanded stuff.
4733 * Copy what came after the expanded part.
4734 * Copy the next commands, if there are any.
4735 */
4736 i = (int)(src - *cmdlinep); /* length of part before match */
4737 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00004738
Bram Moolenaar071d4272004-06-13 20:20:40 +00004739 mch_memmove(new_cmdline + i, repl, (size_t)len);
4740 i += len; /* remember the end of the string */
4741 STRCPY(new_cmdline + i, src + srclen);
4742 src = new_cmdline + i; /* remember where to continue */
4743
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004744 if (eap->nextcmd != NULL) /* append next command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004745 {
4746 i = (int)STRLEN(new_cmdline) + 1;
4747 STRCPY(new_cmdline + i, eap->nextcmd);
4748 eap->nextcmd = new_cmdline + i;
4749 }
4750 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
4751 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
4752 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
4753 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
4754 vim_free(*cmdlinep);
4755 *cmdlinep = new_cmdline;
4756
4757 return src;
4758}
4759
4760/*
4761 * Check for '|' to separate commands and '"' to start comments.
4762 */
4763 void
4764separate_nextcmd(eap)
4765 exarg_T *eap;
4766{
4767 char_u *p;
4768
Bram Moolenaar86b68352004-12-27 21:59:20 +00004769#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004770 p = skip_grep_pat(eap);
4771#else
4772 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004773#endif
4774
4775 for ( ; *p; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004776 {
4777 if (*p == Ctrl_V)
4778 {
4779 if (eap->argt & (USECTRLV | XFILE))
4780 ++p; /* skip CTRL-V and next char */
4781 else
Bram Moolenaarb0db5692007-08-14 20:54:49 +00004782 /* remove CTRL-V and skip next char */
Bram Moolenaara7241f52008-06-24 20:39:31 +00004783 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004784 if (*p == NUL) /* stop at NUL after CTRL-V */
4785 break;
4786 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004787
4788#ifdef FEAT_EVAL
4789 /* Skip over `=expr` when wildcards are expanded. */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004790 else if (p[0] == '`' && p[1] == '=' && (eap->argt & XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004791 {
4792 p += 2;
4793 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004794 }
4795#endif
4796
Bram Moolenaar071d4272004-06-13 20:20:40 +00004797 /* Check for '"': start of comment or '|': next command */
4798 /* :@" and :*" do not start a comment!
4799 * :redir @" doesn't either. */
4800 else if ((*p == '"' && !(eap->argt & NOTRLCOM)
4801 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
4802 || p != eap->arg)
4803 && (eap->cmdidx != CMD_redir
4804 || p != eap->arg + 1 || p[-1] != '@'))
4805 || *p == '|' || *p == '\n')
4806 {
4807 /*
4808 * We remove the '\' before the '|', unless USECTRLV is used
4809 * AND 'b' is present in 'cpoptions'.
4810 */
4811 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
4812 || !(eap->argt & USECTRLV)) && *(p - 1) == '\\')
4813 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00004814 STRMOVE(p - 1, p); /* remove the '\' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004815 --p;
4816 }
4817 else
4818 {
4819 eap->nextcmd = check_nextcmd(p);
4820 *p = NUL;
4821 break;
4822 }
4823 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004824 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004825
Bram Moolenaar071d4272004-06-13 20:20:40 +00004826 if (!(eap->argt & NOTRLCOM)) /* remove trailing spaces */
4827 del_trailing_spaces(eap->arg);
4828}
4829
4830/*
4831 * get + command from ex argument
4832 */
4833 static char_u *
4834getargcmd(argp)
4835 char_u **argp;
4836{
4837 char_u *arg = *argp;
4838 char_u *command = NULL;
4839
4840 if (*arg == '+') /* +[command] */
4841 {
4842 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02004843 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004844 command = dollar_command;
4845 else
4846 {
4847 command = arg;
4848 arg = skip_cmd_arg(command, TRUE);
4849 if (*arg != NUL)
4850 *arg++ = NUL; /* terminate command with NUL */
4851 }
4852
4853 arg = skipwhite(arg); /* skip over spaces */
4854 *argp = arg;
4855 }
4856 return command;
4857}
4858
4859/*
4860 * Find end of "+command" argument. Skip over "\ " and "\\".
4861 */
4862 static char_u *
4863skip_cmd_arg(p, rembs)
4864 char_u *p;
4865 int rembs; /* TRUE to halve the number of backslashes */
4866{
4867 while (*p && !vim_isspace(*p))
4868 {
4869 if (*p == '\\' && p[1] != NUL)
4870 {
4871 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00004872 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004873 else
4874 ++p;
4875 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004876 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004877 }
4878 return p;
4879}
4880
4881/*
4882 * Get "++opt=arg" argument.
4883 * Return FAIL or OK.
4884 */
4885 static int
4886getargopt(eap)
4887 exarg_T *eap;
4888{
4889 char_u *arg = eap->arg + 2;
4890 int *pp = NULL;
4891#ifdef FEAT_MBYTE
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004892 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004893 char_u *p;
4894#endif
4895
4896 /* ":edit ++[no]bin[ary] file" */
4897 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
4898 {
4899 if (*arg == 'n')
4900 {
4901 arg += 2;
4902 eap->force_bin = FORCE_NOBIN;
4903 }
4904 else
4905 eap->force_bin = FORCE_BIN;
4906 if (!checkforcmd(&arg, "binary", 3))
4907 return FAIL;
4908 eap->arg = skipwhite(arg);
4909 return OK;
4910 }
4911
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004912 /* ":read ++edit file" */
4913 if (STRNCMP(arg, "edit", 4) == 0)
4914 {
4915 eap->read_edit = TRUE;
4916 eap->arg = skipwhite(arg + 4);
4917 return OK;
4918 }
4919
Bram Moolenaar071d4272004-06-13 20:20:40 +00004920 if (STRNCMP(arg, "ff", 2) == 0)
4921 {
4922 arg += 2;
4923 pp = &eap->force_ff;
4924 }
4925 else if (STRNCMP(arg, "fileformat", 10) == 0)
4926 {
4927 arg += 10;
4928 pp = &eap->force_ff;
4929 }
4930#ifdef FEAT_MBYTE
4931 else if (STRNCMP(arg, "enc", 3) == 0)
4932 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01004933 if (STRNCMP(arg, "encoding", 8) == 0)
4934 arg += 8;
4935 else
4936 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004937 pp = &eap->force_enc;
4938 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004939 else if (STRNCMP(arg, "bad", 3) == 0)
4940 {
4941 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004942 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004943 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944#endif
4945
4946 if (pp == NULL || *arg != '=')
4947 return FAIL;
4948
4949 ++arg;
4950 *pp = (int)(arg - eap->cmd);
4951 arg = skip_cmd_arg(arg, FALSE);
4952 eap->arg = skipwhite(arg);
4953 *arg = NUL;
4954
4955#ifdef FEAT_MBYTE
4956 if (pp == &eap->force_ff)
4957 {
4958#endif
4959 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
4960 return FAIL;
4961#ifdef FEAT_MBYTE
4962 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004963 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004964 {
4965 /* Make 'fileencoding' lower case. */
4966 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
4967 *p = TOLOWER_ASC(*p);
4968 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004969 else
4970 {
4971 /* Check ++bad= argument. Must be a single-byte character, "keep" or
4972 * "drop". */
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004973 p = eap->cmd + bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004974 if (STRICMP(p, "keep") == 0)
4975 eap->bad_char = BAD_KEEP;
4976 else if (STRICMP(p, "drop") == 0)
4977 eap->bad_char = BAD_DROP;
4978 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
4979 eap->bad_char = *p;
4980 else
4981 return FAIL;
4982 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004983#endif
4984
4985 return OK;
4986}
4987
4988/*
4989 * ":abbreviate" and friends.
4990 */
4991 static void
4992ex_abbreviate(eap)
4993 exarg_T *eap;
4994{
4995 do_exmap(eap, TRUE); /* almost the same as mapping */
4996}
4997
4998/*
4999 * ":map" and friends.
5000 */
5001 static void
5002ex_map(eap)
5003 exarg_T *eap;
5004{
5005 /*
5006 * If we are sourcing .exrc or .vimrc in current directory we
5007 * print the mappings for security reasons.
5008 */
5009 if (secure)
5010 {
5011 secure = 2;
5012 msg_outtrans(eap->cmd);
5013 msg_putchar('\n');
5014 }
5015 do_exmap(eap, FALSE);
5016}
5017
5018/*
5019 * ":unmap" and friends.
5020 */
5021 static void
5022ex_unmap(eap)
5023 exarg_T *eap;
5024{
5025 do_exmap(eap, FALSE);
5026}
5027
5028/*
5029 * ":mapclear" and friends.
5030 */
5031 static void
5032ex_mapclear(eap)
5033 exarg_T *eap;
5034{
5035 map_clear(eap->cmd, eap->arg, eap->forceit, FALSE);
5036}
5037
5038/*
5039 * ":abclear" and friends.
5040 */
5041 static void
5042ex_abclear(eap)
5043 exarg_T *eap;
5044{
5045 map_clear(eap->cmd, eap->arg, TRUE, TRUE);
5046}
5047
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005048#if defined(FEAT_AUTOCMD) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005049 static void
5050ex_autocmd(eap)
5051 exarg_T *eap;
5052{
5053 /*
5054 * Disallow auto commands from .exrc and .vimrc in current
5055 * directory for security reasons.
5056 */
5057 if (secure)
5058 {
5059 secure = 2;
5060 eap->errmsg = e_curdir;
5061 }
5062 else if (eap->cmdidx == CMD_autocmd)
5063 do_autocmd(eap->arg, eap->forceit);
5064 else
5065 do_augroup(eap->arg, eap->forceit);
5066}
5067
5068/*
5069 * ":doautocmd": Apply the automatic commands to the current buffer.
5070 */
5071 static void
5072ex_doautocmd(eap)
5073 exarg_T *eap;
5074{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005075 char_u *arg = eap->arg;
5076 int call_do_modelines = check_nomodeline(&arg);
5077
5078 (void)do_doautocmd(arg, TRUE);
5079 if (call_do_modelines) /* Only when there is no <nomodeline>. */
5080 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005081}
5082#endif
5083
5084#ifdef FEAT_LISTCMDS
5085/*
5086 * :[N]bunload[!] [N] [bufname] unload buffer
5087 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
5088 * :[N]bwipeout[!] [N] [bufname] delete buffer really
5089 */
5090 static void
5091ex_bunload(eap)
5092 exarg_T *eap;
5093{
5094 eap->errmsg = do_bufdel(
5095 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
5096 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
5097 : DOBUF_UNLOAD, eap->arg,
5098 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
5099}
5100
5101/*
5102 * :[N]buffer [N] to buffer N
5103 * :[N]sbuffer [N] to buffer N
5104 */
5105 static void
5106ex_buffer(eap)
5107 exarg_T *eap;
5108{
5109 if (*eap->arg)
5110 eap->errmsg = e_trailing;
5111 else
5112 {
5113 if (eap->addr_count == 0) /* default is current buffer */
5114 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
5115 else
5116 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005117 if (eap->do_ecmd_cmd != NULL)
5118 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005119 }
5120}
5121
5122/*
5123 * :[N]bmodified [N] to next mod. buffer
5124 * :[N]sbmodified [N] to next mod. buffer
5125 */
5126 static void
5127ex_bmodified(eap)
5128 exarg_T *eap;
5129{
5130 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005131 if (eap->do_ecmd_cmd != NULL)
5132 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005133}
5134
5135/*
5136 * :[N]bnext [N] to next buffer
5137 * :[N]sbnext [N] split and to next buffer
5138 */
5139 static void
5140ex_bnext(eap)
5141 exarg_T *eap;
5142{
5143 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005144 if (eap->do_ecmd_cmd != NULL)
5145 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005146}
5147
5148/*
5149 * :[N]bNext [N] to previous buffer
5150 * :[N]bprevious [N] to previous buffer
5151 * :[N]sbNext [N] split and to previous buffer
5152 * :[N]sbprevious [N] split and to previous buffer
5153 */
5154 static void
5155ex_bprevious(eap)
5156 exarg_T *eap;
5157{
5158 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005159 if (eap->do_ecmd_cmd != NULL)
5160 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005161}
5162
5163/*
5164 * :brewind to first buffer
5165 * :bfirst to first buffer
5166 * :sbrewind split and to first buffer
5167 * :sbfirst split and to first buffer
5168 */
5169 static void
5170ex_brewind(eap)
5171 exarg_T *eap;
5172{
5173 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005174 if (eap->do_ecmd_cmd != NULL)
5175 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005176}
5177
5178/*
5179 * :blast to last buffer
5180 * :sblast split and to last buffer
5181 */
5182 static void
5183ex_blast(eap)
5184 exarg_T *eap;
5185{
5186 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005187 if (eap->do_ecmd_cmd != NULL)
5188 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005189}
5190#endif
5191
5192 int
5193ends_excmd(c)
5194 int c;
5195{
5196 return (c == NUL || c == '|' || c == '"' || c == '\n');
5197}
5198
5199#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5200 || defined(PROTO)
5201/*
5202 * Return the next command, after the first '|' or '\n'.
5203 * Return NULL if not found.
5204 */
5205 char_u *
5206find_nextcmd(p)
5207 char_u *p;
5208{
5209 while (*p != '|' && *p != '\n')
5210 {
5211 if (*p == NUL)
5212 return NULL;
5213 ++p;
5214 }
5215 return (p + 1);
5216}
5217#endif
5218
5219/*
5220 * Check if *p is a separator between Ex commands.
5221 * Return NULL if it isn't, (p + 1) if it is.
5222 */
5223 char_u *
5224check_nextcmd(p)
5225 char_u *p;
5226{
5227 p = skipwhite(p);
5228 if (*p == '|' || *p == '\n')
5229 return (p + 1);
5230 else
5231 return NULL;
5232}
5233
5234/*
5235 * - if there are more files to edit
5236 * - and this is the last window
5237 * - and forceit not used
5238 * - and not repeated twice on a row
5239 * return FAIL and give error message if 'message' TRUE
5240 * return OK otherwise
5241 */
5242 static int
5243check_more(message, forceit)
5244 int message; /* when FALSE check only, no messages */
5245 int forceit;
5246{
5247 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5248
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005249 if (!forceit && only_one_window()
5250 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005251 {
5252 if (message)
5253 {
5254#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
5255 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
5256 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005257 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005258
5259 if (n == 1)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02005260 vim_strncpy(buff,
5261 (char_u *)_("1 more file to edit. Quit anyway?"),
Bram Moolenaard9462e32011-04-11 21:35:11 +02005262 DIALOG_MSG_SIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005263 else
Bram Moolenaard9462e32011-04-11 21:35:11 +02005264 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005265 _("%d more files to edit. Quit anyway?"), n);
5266 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5267 return OK;
5268 return FAIL;
5269 }
5270#endif
5271 if (n == 1)
5272 EMSG(_("E173: 1 more file to edit"));
5273 else
5274 EMSGN(_("E173: %ld more files to edit"), n);
5275 quitmore = 2; /* next try to quit is allowed */
5276 }
5277 return FAIL;
5278 }
5279 return OK;
5280}
5281
5282#ifdef FEAT_CMDL_COMPL
5283/*
5284 * Function given to ExpandGeneric() to obtain the list of command names.
5285 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005286 char_u *
5287get_command_name(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00005288 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005289 int idx;
5290{
5291 if (idx >= (int)CMD_SIZE)
5292# ifdef FEAT_USR_CMDS
5293 return get_user_command_name(idx);
5294# else
5295 return NULL;
5296# endif
5297 return cmdnames[idx].cmd_name;
5298}
5299#endif
5300
5301#if defined(FEAT_USR_CMDS) || defined(PROTO)
5302static 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));
5303static void uc_list __ARGS((char_u *name, size_t name_len));
5304static int uc_scan_attr __ARGS((char_u *attr, size_t len, long *argt, long *def, int *flags, int *compl, char_u **compl_arg));
5305static char_u *uc_split_args __ARGS((char_u *arg, size_t *lenp));
5306static 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));
5307
5308 static int
5309uc_add_command(name, name_len, rep, argt, def, flags, compl, compl_arg, force)
5310 char_u *name;
5311 size_t name_len;
5312 char_u *rep;
5313 long argt;
5314 long def;
5315 int flags;
5316 int compl;
5317 char_u *compl_arg;
5318 int force;
5319{
5320 ucmd_T *cmd = NULL;
5321 char_u *p;
5322 int i;
5323 int cmp = 1;
5324 char_u *rep_buf = NULL;
5325 garray_T *gap;
5326
Bram Moolenaar9c102382006-05-03 21:26:49 +00005327 replace_termcodes(rep, &rep_buf, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005328 if (rep_buf == NULL)
5329 {
5330 /* Can't replace termcodes - try using the string as is */
5331 rep_buf = vim_strsave(rep);
5332
5333 /* Give up if out of memory */
5334 if (rep_buf == NULL)
5335 return FAIL;
5336 }
5337
5338 /* get address of growarray: global or in curbuf */
5339 if (flags & UC_BUFFER)
5340 {
5341 gap = &curbuf->b_ucmds;
5342 if (gap->ga_itemsize == 0)
5343 ga_init2(gap, (int)sizeof(ucmd_T), 4);
5344 }
5345 else
5346 gap = &ucmds;
5347
5348 /* Search for the command in the already defined commands. */
5349 for (i = 0; i < gap->ga_len; ++i)
5350 {
5351 size_t len;
5352
5353 cmd = USER_CMD_GA(gap, i);
5354 len = STRLEN(cmd->uc_name);
5355 cmp = STRNCMP(name, cmd->uc_name, name_len);
5356 if (cmp == 0)
5357 {
5358 if (name_len < len)
5359 cmp = -1;
5360 else if (name_len > len)
5361 cmp = 1;
5362 }
5363
5364 if (cmp == 0)
5365 {
5366 if (!force)
5367 {
5368 EMSG(_("E174: Command already exists: add ! to replace it"));
5369 goto fail;
5370 }
5371
5372 vim_free(cmd->uc_rep);
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00005373 cmd->uc_rep = NULL;
5374#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5375 vim_free(cmd->uc_compl_arg);
5376 cmd->uc_compl_arg = NULL;
5377#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005378 break;
5379 }
5380
5381 /* Stop as soon as we pass the name to add */
5382 if (cmp < 0)
5383 break;
5384 }
5385
5386 /* Extend the array unless we're replacing an existing command */
5387 if (cmp != 0)
5388 {
5389 if (ga_grow(gap, 1) != OK)
5390 goto fail;
5391 if ((p = vim_strnsave(name, (int)name_len)) == NULL)
5392 goto fail;
5393
5394 cmd = USER_CMD_GA(gap, i);
5395 mch_memmove(cmd + 1, cmd, (gap->ga_len - i) * sizeof(ucmd_T));
5396
5397 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005398
5399 cmd->uc_name = p;
5400 }
5401
5402 cmd->uc_rep = rep_buf;
5403 cmd->uc_argt = argt;
5404 cmd->uc_def = def;
5405 cmd->uc_compl = compl;
5406#ifdef FEAT_EVAL
5407 cmd->uc_scriptID = current_SID;
5408# ifdef FEAT_CMDL_COMPL
5409 cmd->uc_compl_arg = compl_arg;
5410# endif
5411#endif
5412
5413 return OK;
5414
5415fail:
5416 vim_free(rep_buf);
5417#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5418 vim_free(compl_arg);
5419#endif
5420 return FAIL;
5421}
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005422#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005423
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005424#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005425/*
5426 * List of names for completion for ":command" with the EXPAND_ flag.
5427 * Must be alphabetical for completion.
5428 */
5429static struct
5430{
5431 int expand;
5432 char *name;
5433} command_complete[] =
5434{
5435 {EXPAND_AUGROUP, "augroup"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005436 {EXPAND_BEHAVE, "behave"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005437 {EXPAND_BUFFERS, "buffer"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005438 {EXPAND_COLORS, "color"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005439 {EXPAND_COMMANDS, "command"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005440 {EXPAND_COMPILER, "compiler"},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005441#if defined(FEAT_CSCOPE)
5442 {EXPAND_CSCOPE, "cscope"},
5443#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005444#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5445 {EXPAND_USER_DEFINED, "custom"},
Bram Moolenaara466c992005-07-09 21:03:22 +00005446 {EXPAND_USER_LIST, "customlist"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005447#endif
5448 {EXPAND_DIRECTORIES, "dir"},
5449 {EXPAND_ENV_VARS, "environment"},
5450 {EXPAND_EVENTS, "event"},
5451 {EXPAND_EXPRESSION, "expression"},
5452 {EXPAND_FILES, "file"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005453 {EXPAND_FILES_IN_PATH, "file_in_path"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005454 {EXPAND_FILETYPE, "filetype"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005455 {EXPAND_FUNCTIONS, "function"},
5456 {EXPAND_HELP, "help"},
5457 {EXPAND_HIGHLIGHT, "highlight"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005458#if defined(FEAT_CMDHIST)
5459 {EXPAND_HISTORY, "history"},
5460#endif
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005461#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005462 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005463 {EXPAND_LOCALES, "locale"},
5464#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005465 {EXPAND_MAPPINGS, "mapping"},
5466 {EXPAND_MENUS, "menu"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005467 {EXPAND_OWNSYNTAX, "syntax"},
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02005468#if defined(FEAT_PROFILE)
5469 {EXPAND_SYNTIME, "syntime"},
5470#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005471 {EXPAND_SETTINGS, "option"},
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005472 {EXPAND_SHELLCMD, "shellcmd"},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005473#if defined(FEAT_SIGNS)
5474 {EXPAND_SIGN, "sign"},
5475#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005476 {EXPAND_TAGS, "tag"},
5477 {EXPAND_TAGS_LISTFILES, "tag_listfiles"},
Bram Moolenaar24305862012-08-15 14:05:05 +02005478 {EXPAND_USER, "user"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005479 {EXPAND_USER_VARS, "var"},
5480 {0, NULL}
5481};
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005482#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005483
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005484#if defined(FEAT_USR_CMDS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005485 static void
5486uc_list(name, name_len)
5487 char_u *name;
5488 size_t name_len;
5489{
5490 int i, j;
5491 int found = FALSE;
5492 ucmd_T *cmd;
5493 int len;
5494 long a;
5495 garray_T *gap;
5496
5497 gap = &curbuf->b_ucmds;
5498 for (;;)
5499 {
5500 for (i = 0; i < gap->ga_len; ++i)
5501 {
5502 cmd = USER_CMD_GA(gap, i);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005503 a = (long)cmd->uc_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005504
5505 /* Skip commands which don't match the requested prefix */
5506 if (STRNCMP(name, cmd->uc_name, name_len) != 0)
5507 continue;
5508
5509 /* Put out the title first time */
5510 if (!found)
5511 MSG_PUTS_TITLE(_("\n Name Args Range Complete Definition"));
5512 found = TRUE;
5513 msg_putchar('\n');
5514 if (got_int)
5515 break;
5516
5517 /* Special cases */
5518 msg_putchar(a & BANG ? '!' : ' ');
5519 msg_putchar(a & REGSTR ? '"' : ' ');
5520 msg_putchar(gap != &ucmds ? 'b' : ' ');
5521 msg_putchar(' ');
5522
5523 msg_outtrans_attr(cmd->uc_name, hl_attr(HLF_D));
5524 len = (int)STRLEN(cmd->uc_name) + 4;
5525
5526 do {
5527 msg_putchar(' ');
5528 ++len;
5529 } while (len < 16);
5530
5531 len = 0;
5532
5533 /* Arguments */
5534 switch ((int)(a & (EXTRA|NOSPC|NEEDARG)))
5535 {
5536 case 0: IObuff[len++] = '0'; break;
5537 case (EXTRA): IObuff[len++] = '*'; break;
5538 case (EXTRA|NOSPC): IObuff[len++] = '?'; break;
5539 case (EXTRA|NEEDARG): IObuff[len++] = '+'; break;
5540 case (EXTRA|NOSPC|NEEDARG): IObuff[len++] = '1'; break;
5541 }
5542
5543 do {
5544 IObuff[len++] = ' ';
5545 } while (len < 5);
5546
5547 /* Range */
5548 if (a & (RANGE|COUNT))
5549 {
5550 if (a & COUNT)
5551 {
5552 /* -count=N */
5553 sprintf((char *)IObuff + len, "%ldc", cmd->uc_def);
5554 len += (int)STRLEN(IObuff + len);
5555 }
5556 else if (a & DFLALL)
5557 IObuff[len++] = '%';
5558 else if (cmd->uc_def >= 0)
5559 {
5560 /* -range=N */
5561 sprintf((char *)IObuff + len, "%ld", cmd->uc_def);
5562 len += (int)STRLEN(IObuff + len);
5563 }
5564 else
5565 IObuff[len++] = '.';
5566 }
5567
5568 do {
5569 IObuff[len++] = ' ';
5570 } while (len < 11);
5571
5572 /* Completion */
5573 for (j = 0; command_complete[j].expand != 0; ++j)
5574 if (command_complete[j].expand == cmd->uc_compl)
5575 {
5576 STRCPY(IObuff + len, command_complete[j].name);
5577 len += (int)STRLEN(IObuff + len);
5578 break;
5579 }
5580
5581 do {
5582 IObuff[len++] = ' ';
5583 } while (len < 21);
5584
5585 IObuff[len] = '\0';
5586 msg_outtrans(IObuff);
5587
5588 msg_outtrans_special(cmd->uc_rep, FALSE);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005589#ifdef FEAT_EVAL
5590 if (p_verbose > 0)
5591 last_set_msg(cmd->uc_scriptID);
5592#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005593 out_flush();
5594 ui_breakcheck();
5595 if (got_int)
5596 break;
5597 }
5598 if (gap == &ucmds || i < gap->ga_len)
5599 break;
5600 gap = &ucmds;
5601 }
5602
5603 if (!found)
5604 MSG(_("No user-defined commands found"));
5605}
5606
5607 static char_u *
5608uc_fun_cmd()
5609{
5610 static char_u fcmd[] = {0x84, 0xaf, 0x60, 0xb9, 0xaf, 0xb5, 0x60, 0xa4,
5611 0xa5, 0xad, 0xa1, 0xae, 0xa4, 0x60, 0xa1, 0x60,
5612 0xb3, 0xa8, 0xb2, 0xb5, 0xa2, 0xa2, 0xa5, 0xb2,
5613 0xb9, 0x7f, 0};
5614 int i;
5615
5616 for (i = 0; fcmd[i]; ++i)
5617 IObuff[i] = fcmd[i] - 0x40;
5618 IObuff[i] = 0;
5619 return IObuff;
5620}
5621
5622 static int
5623uc_scan_attr(attr, len, argt, def, flags, compl, compl_arg)
5624 char_u *attr;
5625 size_t len;
5626 long *argt;
5627 long *def;
5628 int *flags;
5629 int *compl;
5630 char_u **compl_arg;
5631{
5632 char_u *p;
5633
5634 if (len == 0)
5635 {
5636 EMSG(_("E175: No attribute specified"));
5637 return FAIL;
5638 }
5639
5640 /* First, try the simple attributes (no arguments) */
5641 if (STRNICMP(attr, "bang", len) == 0)
5642 *argt |= BANG;
5643 else if (STRNICMP(attr, "buffer", len) == 0)
5644 *flags |= UC_BUFFER;
5645 else if (STRNICMP(attr, "register", len) == 0)
5646 *argt |= REGSTR;
5647 else if (STRNICMP(attr, "bar", len) == 0)
5648 *argt |= TRLBAR;
5649 else
5650 {
5651 int i;
5652 char_u *val = NULL;
5653 size_t vallen = 0;
5654 size_t attrlen = len;
5655
5656 /* Look for the attribute name - which is the part before any '=' */
5657 for (i = 0; i < (int)len; ++i)
5658 {
5659 if (attr[i] == '=')
5660 {
5661 val = &attr[i + 1];
5662 vallen = len - i - 1;
5663 attrlen = i;
5664 break;
5665 }
5666 }
5667
5668 if (STRNICMP(attr, "nargs", attrlen) == 0)
5669 {
5670 if (vallen == 1)
5671 {
5672 if (*val == '0')
5673 /* Do nothing - this is the default */;
5674 else if (*val == '1')
5675 *argt |= (EXTRA | NOSPC | NEEDARG);
5676 else if (*val == '*')
5677 *argt |= EXTRA;
5678 else if (*val == '?')
5679 *argt |= (EXTRA | NOSPC);
5680 else if (*val == '+')
5681 *argt |= (EXTRA | NEEDARG);
5682 else
5683 goto wrong_nargs;
5684 }
5685 else
5686 {
5687wrong_nargs:
5688 EMSG(_("E176: Invalid number of arguments"));
5689 return FAIL;
5690 }
5691 }
5692 else if (STRNICMP(attr, "range", attrlen) == 0)
5693 {
5694 *argt |= RANGE;
5695 if (vallen == 1 && *val == '%')
5696 *argt |= DFLALL;
5697 else if (val != NULL)
5698 {
5699 p = val;
5700 if (*def >= 0)
5701 {
5702two_count:
5703 EMSG(_("E177: Count cannot be specified twice"));
5704 return FAIL;
5705 }
5706
5707 *def = getdigits(&p);
5708 *argt |= (ZEROR | NOTADR);
5709
5710 if (p != val + vallen || vallen == 0)
5711 {
5712invalid_count:
5713 EMSG(_("E178: Invalid default value for count"));
5714 return FAIL;
5715 }
5716 }
5717 }
5718 else if (STRNICMP(attr, "count", attrlen) == 0)
5719 {
Bram Moolenaar32e7b2d2005-02-27 22:36:47 +00005720 *argt |= (COUNT | ZEROR | RANGE | NOTADR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005721
5722 if (val != NULL)
5723 {
5724 p = val;
5725 if (*def >= 0)
5726 goto two_count;
5727
5728 *def = getdigits(&p);
5729
5730 if (p != val + vallen)
5731 goto invalid_count;
5732 }
5733
5734 if (*def < 0)
5735 *def = 0;
5736 }
5737 else if (STRNICMP(attr, "complete", attrlen) == 0)
5738 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005739 if (val == NULL)
5740 {
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005741 EMSG(_("E179: argument required for -complete"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005742 return FAIL;
5743 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005744
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005745 if (parse_compl_arg(val, (int)vallen, compl, argt, compl_arg)
5746 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005747 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005748 }
5749 else
5750 {
5751 char_u ch = attr[len];
5752 attr[len] = '\0';
5753 EMSG2(_("E181: Invalid attribute: %s"), attr);
5754 attr[len] = ch;
5755 return FAIL;
5756 }
5757 }
5758
5759 return OK;
5760}
5761
Bram Moolenaara850a712009-01-28 14:42:59 +00005762/*
5763 * ":command ..."
5764 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005765 static void
5766ex_command(eap)
5767 exarg_T *eap;
5768{
5769 char_u *name;
5770 char_u *end;
5771 char_u *p;
5772 long argt = 0;
5773 long def = -1;
5774 int flags = 0;
5775 int compl = EXPAND_NOTHING;
5776 char_u *compl_arg = NULL;
5777 int has_attr = (eap->arg[0] == '-');
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01005778 int name_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005779
5780 p = eap->arg;
5781
5782 /* Check for attributes */
5783 while (*p == '-')
5784 {
5785 ++p;
5786 end = skiptowhite(p);
5787 if (uc_scan_attr(p, end - p, &argt, &def, &flags, &compl, &compl_arg)
5788 == FAIL)
5789 return;
5790 p = skipwhite(end);
5791 }
5792
5793 /* Get the name (if any) and skip to the following argument */
5794 name = p;
5795 if (ASCII_ISALPHA(*p))
5796 while (ASCII_ISALNUM(*p))
5797 ++p;
5798 if (!ends_excmd(*p) && !vim_iswhite(*p))
5799 {
5800 EMSG(_("E182: Invalid command name"));
5801 return;
5802 }
5803 end = p;
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01005804 name_len = (int)(end - name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005805
5806 /* If there is nothing after the name, and no attributes were specified,
5807 * we are listing commands
5808 */
5809 p = skipwhite(end);
5810 if (!has_attr && ends_excmd(*p))
5811 {
5812 uc_list(name, end - name);
5813 }
5814 else if (!ASCII_ISUPPER(*name))
5815 {
5816 EMSG(_("E183: User defined commands must start with an uppercase letter"));
5817 return;
5818 }
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01005819 else if ((name_len == 1 && *name == 'X')
5820 || (name_len <= 4
5821 && STRNCMP(name, "Next", name_len > 4 ? 4 : name_len) == 0))
5822 {
5823 EMSG(_("E841: Reserved name, cannot be used for user defined command"));
5824 return;
5825 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005826 else
5827 uc_add_command(name, end - name, p, argt, def, flags, compl, compl_arg,
5828 eap->forceit);
5829}
5830
5831/*
5832 * ":comclear"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005833 * Clear all user commands, global and for current buffer.
5834 */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00005835 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005836ex_comclear(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00005837 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005838{
5839 uc_clear(&ucmds);
5840 uc_clear(&curbuf->b_ucmds);
5841}
5842
5843/*
5844 * Clear all user commands for "gap".
5845 */
5846 void
5847uc_clear(gap)
5848 garray_T *gap;
5849{
5850 int i;
5851 ucmd_T *cmd;
5852
5853 for (i = 0; i < gap->ga_len; ++i)
5854 {
5855 cmd = USER_CMD_GA(gap, i);
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 ga_clear(gap);
5863}
5864
5865 static void
5866ex_delcommand(eap)
5867 exarg_T *eap;
5868{
5869 int i = 0;
5870 ucmd_T *cmd = NULL;
5871 int cmp = -1;
5872 garray_T *gap;
5873
5874 gap = &curbuf->b_ucmds;
5875 for (;;)
5876 {
5877 for (i = 0; i < gap->ga_len; ++i)
5878 {
5879 cmd = USER_CMD_GA(gap, i);
5880 cmp = STRCMP(eap->arg, cmd->uc_name);
5881 if (cmp <= 0)
5882 break;
5883 }
5884 if (gap == &ucmds || cmp == 0)
5885 break;
5886 gap = &ucmds;
5887 }
5888
5889 if (cmp != 0)
5890 {
5891 EMSG2(_("E184: No such user-defined command: %s"), eap->arg);
5892 return;
5893 }
5894
5895 vim_free(cmd->uc_name);
5896 vim_free(cmd->uc_rep);
5897# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5898 vim_free(cmd->uc_compl_arg);
5899# endif
5900
5901 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005902
5903 if (i < gap->ga_len)
5904 mch_memmove(cmd, cmd + 1, (gap->ga_len - i) * sizeof(ucmd_T));
5905}
5906
Bram Moolenaar552f8a12007-03-08 17:12:08 +00005907/*
5908 * split and quote args for <f-args>
5909 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005910 static char_u *
5911uc_split_args(arg, lenp)
5912 char_u *arg;
5913 size_t *lenp;
5914{
5915 char_u *buf;
5916 char_u *p;
5917 char_u *q;
5918 int len;
5919
5920 /* Precalculate length */
5921 p = arg;
5922 len = 2; /* Initial and final quotes */
5923
5924 while (*p)
5925 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00005926 if (p[0] == '\\' && p[1] == '\\')
5927 {
5928 len += 2;
5929 p += 2;
5930 }
5931 else if (p[0] == '\\' && vim_iswhite(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005932 {
5933 len += 1;
5934 p += 2;
5935 }
5936 else if (*p == '\\' || *p == '"')
5937 {
5938 len += 2;
5939 p += 1;
5940 }
5941 else if (vim_iswhite(*p))
5942 {
5943 p = skipwhite(p);
5944 if (*p == NUL)
5945 break;
5946 len += 3; /* "," */
5947 }
5948 else
5949 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02005950#ifdef FEAT_MBYTE
5951 int charlen = (*mb_ptr2len)(p);
5952 len += charlen;
5953 p += charlen;
5954#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005955 ++len;
5956 ++p;
Bram Moolenaardfef1542012-07-10 19:25:10 +02005957#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005958 }
5959 }
5960
5961 buf = alloc(len + 1);
5962 if (buf == NULL)
5963 {
5964 *lenp = 0;
5965 return buf;
5966 }
5967
5968 p = arg;
5969 q = buf;
5970 *q++ = '"';
5971 while (*p)
5972 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00005973 if (p[0] == '\\' && p[1] == '\\')
5974 {
5975 *q++ = '\\';
5976 *q++ = '\\';
5977 p += 2;
5978 }
5979 else if (p[0] == '\\' && vim_iswhite(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005980 {
5981 *q++ = p[1];
5982 p += 2;
5983 }
5984 else if (*p == '\\' || *p == '"')
5985 {
5986 *q++ = '\\';
5987 *q++ = *p++;
5988 }
5989 else if (vim_iswhite(*p))
5990 {
5991 p = skipwhite(p);
5992 if (*p == NUL)
5993 break;
5994 *q++ = '"';
5995 *q++ = ',';
5996 *q++ = '"';
5997 }
5998 else
5999 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02006000 MB_COPY_CHAR(p, q);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006001 }
6002 }
6003 *q++ = '"';
6004 *q = 0;
6005
6006 *lenp = len;
6007 return buf;
6008}
6009
6010/*
6011 * Check for a <> code in a user command.
6012 * "code" points to the '<'. "len" the length of the <> (inclusive).
6013 * "buf" is where the result is to be added.
6014 * "split_buf" points to a buffer used for splitting, caller should free it.
6015 * "split_len" is the length of what "split_buf" contains.
6016 * Returns the length of the replacement, which has been added to "buf".
6017 * Returns -1 if there was no match, and only the "<" has been copied.
6018 */
6019 static size_t
6020uc_check_code(code, len, buf, cmd, eap, split_buf, split_len)
6021 char_u *code;
6022 size_t len;
6023 char_u *buf;
6024 ucmd_T *cmd; /* the user command we're expanding */
6025 exarg_T *eap; /* ex arguments */
6026 char_u **split_buf;
6027 size_t *split_len;
6028{
6029 size_t result = 0;
6030 char_u *p = code + 1;
6031 size_t l = len - 2;
6032 int quote = 0;
6033 enum { ct_ARGS, ct_BANG, ct_COUNT, ct_LINE1, ct_LINE2, ct_REGISTER,
6034 ct_LT, ct_NONE } type = ct_NONE;
6035
6036 if ((vim_strchr((char_u *)"qQfF", *p) != NULL) && p[1] == '-')
6037 {
6038 quote = (*p == 'q' || *p == 'Q') ? 1 : 2;
6039 p += 2;
6040 l -= 2;
6041 }
6042
Bram Moolenaar371d5402006-03-20 21:47:49 +00006043 ++l;
6044 if (l <= 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006045 type = ct_NONE;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006046 else if (STRNICMP(p, "args>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006047 type = ct_ARGS;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006048 else if (STRNICMP(p, "bang>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006049 type = ct_BANG;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006050 else if (STRNICMP(p, "count>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006051 type = ct_COUNT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006052 else if (STRNICMP(p, "line1>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006053 type = ct_LINE1;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006054 else if (STRNICMP(p, "line2>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006055 type = ct_LINE2;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006056 else if (STRNICMP(p, "lt>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006057 type = ct_LT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006058 else if (STRNICMP(p, "reg>", l) == 0 || STRNICMP(p, "register>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006059 type = ct_REGISTER;
6060
6061 switch (type)
6062 {
6063 case ct_ARGS:
6064 /* Simple case first */
6065 if (*eap->arg == NUL)
6066 {
6067 if (quote == 1)
6068 {
6069 result = 2;
6070 if (buf != NULL)
6071 STRCPY(buf, "''");
6072 }
6073 else
6074 result = 0;
6075 break;
6076 }
6077
6078 /* When specified there is a single argument don't split it.
6079 * Works for ":Cmd %" when % is "a b c". */
6080 if ((eap->argt & NOSPC) && quote == 2)
6081 quote = 1;
6082
6083 switch (quote)
6084 {
6085 case 0: /* No quoting, no splitting */
6086 result = STRLEN(eap->arg);
6087 if (buf != NULL)
6088 STRCPY(buf, eap->arg);
6089 break;
6090 case 1: /* Quote, but don't split */
6091 result = STRLEN(eap->arg) + 2;
6092 for (p = eap->arg; *p; ++p)
6093 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006094#ifdef FEAT_MBYTE
6095 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6096 /* DBCS can contain \ in a trail byte, skip the
6097 * double-byte character. */
6098 ++p;
6099 else
6100#endif
6101 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006102 ++result;
6103 }
6104
6105 if (buf != NULL)
6106 {
6107 *buf++ = '"';
6108 for (p = eap->arg; *p; ++p)
6109 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006110#ifdef FEAT_MBYTE
6111 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6112 /* DBCS can contain \ in a trail byte, copy the
6113 * double-byte character to avoid escaping. */
6114 *buf++ = *p++;
6115 else
6116#endif
6117 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006118 *buf++ = '\\';
6119 *buf++ = *p;
6120 }
6121 *buf = '"';
6122 }
6123
6124 break;
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006125 case 2: /* Quote and split (<f-args>) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006126 /* This is hard, so only do it once, and cache the result */
6127 if (*split_buf == NULL)
6128 *split_buf = uc_split_args(eap->arg, split_len);
6129
6130 result = *split_len;
6131 if (buf != NULL && result != 0)
6132 STRCPY(buf, *split_buf);
6133
6134 break;
6135 }
6136 break;
6137
6138 case ct_BANG:
6139 result = eap->forceit ? 1 : 0;
6140 if (quote)
6141 result += 2;
6142 if (buf != NULL)
6143 {
6144 if (quote)
6145 *buf++ = '"';
6146 if (eap->forceit)
6147 *buf++ = '!';
6148 if (quote)
6149 *buf = '"';
6150 }
6151 break;
6152
6153 case ct_LINE1:
6154 case ct_LINE2:
6155 case ct_COUNT:
6156 {
6157 char num_buf[20];
6158 long num = (type == ct_LINE1) ? eap->line1 :
6159 (type == ct_LINE2) ? eap->line2 :
6160 (eap->addr_count > 0) ? eap->line2 : cmd->uc_def;
6161 size_t num_len;
6162
6163 sprintf(num_buf, "%ld", num);
6164 num_len = STRLEN(num_buf);
6165 result = num_len;
6166
6167 if (quote)
6168 result += 2;
6169
6170 if (buf != NULL)
6171 {
6172 if (quote)
6173 *buf++ = '"';
6174 STRCPY(buf, num_buf);
6175 buf += num_len;
6176 if (quote)
6177 *buf = '"';
6178 }
6179
6180 break;
6181 }
6182
6183 case ct_REGISTER:
6184 result = eap->regname ? 1 : 0;
6185 if (quote)
6186 result += 2;
6187 if (buf != NULL)
6188 {
6189 if (quote)
6190 *buf++ = '\'';
6191 if (eap->regname)
6192 *buf++ = eap->regname;
6193 if (quote)
6194 *buf = '\'';
6195 }
6196 break;
6197
6198 case ct_LT:
6199 result = 1;
6200 if (buf != NULL)
6201 *buf = '<';
6202 break;
6203
6204 default:
6205 /* Not recognized: just copy the '<' and return -1. */
6206 result = (size_t)-1;
6207 if (buf != NULL)
6208 *buf = '<';
6209 break;
6210 }
6211
6212 return result;
6213}
6214
6215 static void
6216do_ucmd(eap)
6217 exarg_T *eap;
6218{
6219 char_u *buf;
6220 char_u *p;
6221 char_u *q;
6222
6223 char_u *start;
Bram Moolenaar25648a52009-02-21 19:37:46 +00006224 char_u *end = NULL;
Bram Moolenaara850a712009-01-28 14:42:59 +00006225 char_u *ksp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006226 size_t len, totlen;
6227
6228 size_t split_len = 0;
6229 char_u *split_buf = NULL;
6230 ucmd_T *cmd;
6231#ifdef FEAT_EVAL
6232 scid_T save_current_SID = current_SID;
6233#endif
6234
6235 if (eap->cmdidx == CMD_USER)
6236 cmd = USER_CMD(eap->useridx);
6237 else
6238 cmd = USER_CMD_GA(&curbuf->b_ucmds, eap->useridx);
6239
6240 /*
6241 * Replace <> in the command by the arguments.
Bram Moolenaara850a712009-01-28 14:42:59 +00006242 * First round: "buf" is NULL, compute length, allocate "buf".
6243 * Second round: copy result into "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006244 */
6245 buf = NULL;
6246 for (;;)
6247 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006248 p = cmd->uc_rep; /* source */
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00006249 q = buf; /* destination */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006250 totlen = 0;
Bram Moolenaara850a712009-01-28 14:42:59 +00006251
6252 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006253 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006254 start = vim_strchr(p, '<');
6255 if (start != NULL)
6256 end = vim_strchr(start + 1, '>');
6257 if (buf != NULL)
6258 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006259 for (ksp = p; *ksp != NUL && *ksp != K_SPECIAL; ++ksp)
6260 ;
6261 if (*ksp == K_SPECIAL
6262 && (start == NULL || ksp < start || end == NULL)
Bram Moolenaara850a712009-01-28 14:42:59 +00006263 && ((ksp[1] == KS_SPECIAL && ksp[2] == KE_FILLER)
6264# ifdef FEAT_GUI
6265 || (ksp[1] == KS_EXTRA && ksp[2] == (int)KE_CSI)
6266# endif
6267 ))
6268 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006269 /* K_SPECIAL has been put in the buffer as K_SPECIAL
Bram Moolenaara850a712009-01-28 14:42:59 +00006270 * KS_SPECIAL KE_FILLER, like for mappings, but
6271 * do_cmdline() doesn't handle that, so convert it back.
6272 * Also change K_SPECIAL KS_EXTRA KE_CSI into CSI. */
6273 len = ksp - p;
6274 if (len > 0)
6275 {
6276 mch_memmove(q, p, len);
6277 q += len;
6278 }
6279 *q++ = ksp[1] == KS_SPECIAL ? K_SPECIAL : CSI;
6280 p = ksp + 3;
6281 continue;
6282 }
6283 }
6284
6285 /* break if there no <item> is found */
6286 if (start == NULL || end == NULL)
6287 break;
6288
Bram Moolenaar071d4272004-06-13 20:20:40 +00006289 /* Include the '>' */
6290 ++end;
6291
6292 /* Take everything up to the '<' */
6293 len = start - p;
6294 if (buf == NULL)
6295 totlen += len;
6296 else
6297 {
6298 mch_memmove(q, p, len);
6299 q += len;
6300 }
6301
6302 len = uc_check_code(start, end - start, q, cmd, eap,
6303 &split_buf, &split_len);
6304 if (len == (size_t)-1)
6305 {
6306 /* no match, continue after '<' */
6307 p = start + 1;
6308 len = 1;
6309 }
6310 else
6311 p = end;
6312 if (buf == NULL)
6313 totlen += len;
6314 else
6315 q += len;
6316 }
6317 if (buf != NULL) /* second time here, finished */
6318 {
6319 STRCPY(q, p);
6320 break;
6321 }
6322
6323 totlen += STRLEN(p); /* Add on the trailing characters */
6324 buf = alloc((unsigned)(totlen + 1));
6325 if (buf == NULL)
6326 {
6327 vim_free(split_buf);
6328 return;
6329 }
6330 }
6331
6332#ifdef FEAT_EVAL
6333 current_SID = cmd->uc_scriptID;
6334#endif
6335 (void)do_cmdline(buf, eap->getline, eap->cookie,
6336 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
6337#ifdef FEAT_EVAL
6338 current_SID = save_current_SID;
6339#endif
6340 vim_free(buf);
6341 vim_free(split_buf);
6342}
6343
6344# if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6345 static char_u *
6346get_user_command_name(idx)
6347 int idx;
6348{
6349 return get_user_commands(NULL, idx - (int)CMD_SIZE);
6350}
6351
6352/*
6353 * Function given to ExpandGeneric() to obtain the list of user command names.
6354 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006355 char_u *
6356get_user_commands(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006357 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006358 int idx;
6359{
6360 if (idx < curbuf->b_ucmds.ga_len)
6361 return USER_CMD_GA(&curbuf->b_ucmds, idx)->uc_name;
6362 idx -= curbuf->b_ucmds.ga_len;
6363 if (idx < ucmds.ga_len)
6364 return USER_CMD(idx)->uc_name;
6365 return NULL;
6366}
6367
6368/*
6369 * Function given to ExpandGeneric() to obtain the list of user command
6370 * attributes.
6371 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006372 char_u *
6373get_user_cmd_flags(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006374 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006375 int idx;
6376{
6377 static char *user_cmd_flags[] =
6378 {"bang", "bar", "buffer", "complete", "count",
6379 "nargs", "range", "register"};
6380
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00006381 if (idx >= (int)(sizeof(user_cmd_flags) / sizeof(user_cmd_flags[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006382 return NULL;
6383 return (char_u *)user_cmd_flags[idx];
6384}
6385
6386/*
6387 * Function given to ExpandGeneric() to obtain the list of values for -nargs.
6388 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006389 char_u *
6390get_user_cmd_nargs(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006391 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006392 int idx;
6393{
6394 static char *user_cmd_nargs[] = {"0", "1", "*", "?", "+"};
6395
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00006396 if (idx >= (int)(sizeof(user_cmd_nargs) / sizeof(user_cmd_nargs[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006397 return NULL;
6398 return (char_u *)user_cmd_nargs[idx];
6399}
6400
6401/*
6402 * Function given to ExpandGeneric() to obtain the list of values for -complete.
6403 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006404 char_u *
6405get_user_cmd_complete(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006406 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006407 int idx;
6408{
6409 return (char_u *)command_complete[idx].name;
6410}
6411# endif /* FEAT_CMDL_COMPL */
6412
6413#endif /* FEAT_USR_CMDS */
6414
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006415#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
6416/*
6417 * Parse a completion argument "value[vallen]".
6418 * The detected completion goes in "*complp", argument type in "*argt".
6419 * When there is an argument, for function and user defined completion, it's
6420 * copied to allocated memory and stored in "*compl_arg".
6421 * Returns FAIL if something is wrong.
6422 */
6423 int
6424parse_compl_arg(value, vallen, complp, argt, compl_arg)
6425 char_u *value;
6426 int vallen;
6427 int *complp;
6428 long *argt;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006429 char_u **compl_arg UNUSED;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006430{
6431 char_u *arg = NULL;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006432# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006433 size_t arglen = 0;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006434# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006435 int i;
6436 int valend = vallen;
6437
6438 /* Look for any argument part - which is the part after any ',' */
6439 for (i = 0; i < vallen; ++i)
6440 {
6441 if (value[i] == ',')
6442 {
6443 arg = &value[i + 1];
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006444# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006445 arglen = vallen - i - 1;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006446# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006447 valend = i;
6448 break;
6449 }
6450 }
6451
6452 for (i = 0; command_complete[i].expand != 0; ++i)
6453 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006454 if ((int)STRLEN(command_complete[i].name) == valend
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006455 && STRNCMP(value, command_complete[i].name, valend) == 0)
6456 {
6457 *complp = command_complete[i].expand;
6458 if (command_complete[i].expand == EXPAND_BUFFERS)
6459 *argt |= BUFNAME;
6460 else if (command_complete[i].expand == EXPAND_DIRECTORIES
6461 || command_complete[i].expand == EXPAND_FILES)
6462 *argt |= XFILE;
6463 break;
6464 }
6465 }
6466
6467 if (command_complete[i].expand == 0)
6468 {
6469 EMSG2(_("E180: Invalid complete value: %s"), value);
6470 return FAIL;
6471 }
6472
6473# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6474 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST
6475 && arg != NULL)
6476# else
6477 if (arg != NULL)
6478# endif
6479 {
6480 EMSG(_("E468: Completion argument only allowed for custom completion"));
6481 return FAIL;
6482 }
6483
6484# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6485 if ((*complp == EXPAND_USER_DEFINED || *complp == EXPAND_USER_LIST)
6486 && arg == NULL)
6487 {
6488 EMSG(_("E467: Custom completion requires a function argument"));
6489 return FAIL;
6490 }
6491
6492 if (arg != NULL)
6493 *compl_arg = vim_strnsave(arg, (int)arglen);
6494# endif
6495 return OK;
6496}
6497#endif
6498
Bram Moolenaar071d4272004-06-13 20:20:40 +00006499 static void
6500ex_colorscheme(eap)
6501 exarg_T *eap;
6502{
Bram Moolenaare6850792010-05-14 15:28:44 +02006503 if (*eap->arg == NUL)
6504 {
6505#ifdef FEAT_EVAL
6506 char_u *expr = vim_strsave((char_u *)"g:colors_name");
6507 char_u *p = NULL;
6508
6509 if (expr != NULL)
6510 {
6511 ++emsg_off;
6512 p = eval_to_string(expr, NULL, FALSE);
6513 --emsg_off;
6514 vim_free(expr);
6515 }
6516 if (p != NULL)
6517 {
6518 MSG(p);
6519 vim_free(p);
6520 }
6521 else
6522 MSG("default");
6523#else
6524 MSG(_("unknown"));
6525#endif
6526 }
6527 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarbe1e9e92012-09-18 16:47:07 +02006528 EMSG2(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006529}
6530
6531 static void
6532ex_highlight(eap)
6533 exarg_T *eap;
6534{
6535 if (*eap->arg == NUL && eap->cmd[2] == '!')
6536 MSG(_("Greetings, Vim user!"));
6537 do_highlight(eap->arg, eap->forceit, FALSE);
6538}
6539
6540
6541/*
6542 * Call this function if we thought we were going to exit, but we won't
6543 * (because of an error). May need to restore the terminal mode.
6544 */
6545 void
6546not_exiting()
6547{
6548 exiting = FALSE;
6549 settmode(TMODE_RAW);
6550}
6551
6552/*
6553 * ":quit": quit current window, quit Vim if closed the last window.
6554 */
6555 static void
6556ex_quit(eap)
6557 exarg_T *eap;
6558{
6559#ifdef FEAT_CMDWIN
6560 if (cmdwin_type != 0)
6561 {
6562 cmdwin_result = Ctrl_C;
6563 return;
6564 }
6565#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006566 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006567 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006568 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006569 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006570 return;
6571 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006572#ifdef FEAT_AUTOCMD
Bram Moolenaar3b53dfb2012-06-06 18:03:07 +02006573 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01006574 /* Refuse to quit when locked or when the buffer in the last window is
Bram Moolenaar362ce482012-06-06 19:02:45 +02006575 * being closed (can only happen in autocommands). */
6576 if (curbuf_locked() || (curbuf->b_nwindows == 1 && curbuf->b_closing))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006577 return;
6578#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006579
6580#ifdef FEAT_NETBEANS_INTG
6581 netbeansForcedQuit = eap->forceit;
6582#endif
6583
6584 /*
6585 * If there are more files or windows we won't exit.
6586 */
6587 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6588 exiting = TRUE;
6589 if ((!P_HID(curbuf)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01006590 && check_changed(curbuf, (p_awa ? CCGD_AW : 0)
6591 | (eap->forceit ? CCGD_FORCEIT : 0)
6592 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006593 || check_more(TRUE, eap->forceit) == FAIL
6594 || (only_one_window() && check_changed_any(eap->forceit)))
6595 {
6596 not_exiting();
6597 }
6598 else
6599 {
6600#ifdef FEAT_WINDOWS
6601 if (only_one_window()) /* quit last window */
6602#endif
6603 getout(0);
6604#ifdef FEAT_WINDOWS
6605# ifdef FEAT_GUI
6606 need_mouse_correct = TRUE;
6607# endif
6608 /* close window; may free buffer */
6609 win_close(curwin, !P_HID(curwin->w_buffer) || eap->forceit);
6610#endif
6611 }
6612}
6613
6614/*
6615 * ":cquit".
6616 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006617 static void
6618ex_cquit(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006619 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006620{
6621 getout(1); /* this does not always pass on the exit code to the Manx
6622 compiler. why? */
6623}
6624
6625/*
6626 * ":qall": try to quit all windows
6627 */
6628 static void
6629ex_quit_all(eap)
6630 exarg_T *eap;
6631{
6632# ifdef FEAT_CMDWIN
6633 if (cmdwin_type != 0)
6634 {
6635 if (eap->forceit)
6636 cmdwin_result = K_XF1; /* ex_window() takes care of this */
6637 else
6638 cmdwin_result = K_XF2;
6639 return;
6640 }
6641# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006642
6643 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006644 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006645 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006646 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006647 return;
6648 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006649#ifdef FEAT_AUTOCMD
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01006650 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
6651 /* Refuse to quit when locked or when the buffer in the last window is
6652 * being closed (can only happen in autocommands). */
6653 if (curbuf_locked() || (curbuf->b_nwindows == 1 && curbuf->b_closing))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006654 return;
6655#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006656
Bram Moolenaar071d4272004-06-13 20:20:40 +00006657 exiting = TRUE;
6658 if (eap->forceit || !check_changed_any(FALSE))
6659 getout(0);
6660 not_exiting();
6661}
6662
Bram Moolenaard9967712006-03-11 21:18:15 +00006663#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006664/*
6665 * ":close": close current window, unless it is the last one
6666 */
6667 static void
6668ex_close(eap)
6669 exarg_T *eap;
6670{
6671# ifdef FEAT_CMDWIN
6672 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02006673 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006674 else
6675# endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006676 if (!text_locked()
6677#ifdef FEAT_AUTOCMD
6678 && !curbuf_locked()
6679#endif
6680 )
Bram Moolenaarf740b292006-02-16 22:11:02 +00006681 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006682}
6683
Bram Moolenaard9967712006-03-11 21:18:15 +00006684# ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006685/*
6686 * ":pclose": Close any preview window.
6687 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006688 static void
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006689ex_pclose(eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006690 exarg_T *eap;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006691{
6692 win_T *win;
6693
6694 for (win = firstwin; win != NULL; win = win->w_next)
6695 if (win->w_p_pvw)
6696 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006697 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006698 break;
6699 }
6700}
Bram Moolenaard9967712006-03-11 21:18:15 +00006701# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006702
Bram Moolenaarf740b292006-02-16 22:11:02 +00006703/*
6704 * Close window "win" and take care of handling closing the last window for a
6705 * modified buffer.
6706 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006707 static void
Bram Moolenaarf740b292006-02-16 22:11:02 +00006708ex_win_close(forceit, win, tp)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006709 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006710 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006711 tabpage_T *tp; /* NULL or the tab page "win" is in */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006712{
6713 int need_hide;
6714 buf_T *buf = win->w_buffer;
6715
6716 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006717 if (need_hide && !P_HID(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006718 {
Bram Moolenaard9967712006-03-11 21:18:15 +00006719# if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006720 if ((p_confirm || cmdmod.confirm) && p_write)
6721 {
6722 dialog_changed(buf, FALSE);
6723 if (buf_valid(buf) && bufIsChanged(buf))
6724 return;
6725 need_hide = FALSE;
6726 }
6727 else
Bram Moolenaard9967712006-03-11 21:18:15 +00006728# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006729 {
6730 EMSG(_(e_nowrtmsg));
6731 return;
6732 }
6733 }
6734
Bram Moolenaard9967712006-03-11 21:18:15 +00006735# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006736 need_mouse_correct = TRUE;
Bram Moolenaard9967712006-03-11 21:18:15 +00006737# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006738
Bram Moolenaar071d4272004-06-13 20:20:40 +00006739 /* free buffer when not hiding it or when it's a scratch buffer */
Bram Moolenaarf740b292006-02-16 22:11:02 +00006740 if (tp == NULL)
6741 win_close(win, !need_hide && !P_HID(buf));
6742 else
6743 win_close_othertab(win, !need_hide && !P_HID(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006744}
6745
Bram Moolenaar071d4272004-06-13 20:20:40 +00006746/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006747 * ":tabclose": close current tab page, unless it is the last one.
6748 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006749 */
6750 static void
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006751ex_tabclose(eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006752 exarg_T *eap;
6753{
Bram Moolenaarf740b292006-02-16 22:11:02 +00006754 tabpage_T *tp;
6755
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006756# ifdef FEAT_CMDWIN
6757 if (cmdwin_type != 0)
6758 cmdwin_result = K_IGNORE;
6759 else
6760# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006761 if (first_tabpage->tp_next == NULL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00006762 EMSG(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00006763 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006764 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006765 if (eap->addr_count > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006766 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006767 tp = find_tabpage((int)eap->line2);
6768 if (tp == NULL)
6769 {
6770 beep_flush();
6771 return;
6772 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00006773 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006774 {
6775 tabpage_close_other(tp, eap->forceit);
6776 return;
6777 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006778 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006779 if (!text_locked()
6780#ifdef FEAT_AUTOCMD
6781 && !curbuf_locked()
6782#endif
6783 )
Bram Moolenaarf740b292006-02-16 22:11:02 +00006784 tabpage_close(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006785 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006786}
6787
6788/*
6789 * ":tabonly": close all tab pages except the current one
6790 */
6791 static void
6792ex_tabonly(eap)
6793 exarg_T *eap;
6794{
6795 tabpage_T *tp;
6796 int done;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006797
6798# ifdef FEAT_CMDWIN
6799 if (cmdwin_type != 0)
6800 cmdwin_result = K_IGNORE;
6801 else
6802# endif
6803 if (first_tabpage->tp_next == NULL)
6804 MSG(_("Already only one tab page"));
6805 else
6806 {
6807 /* Repeat this up to a 1000 times, because autocommands may mess
6808 * up the lists. */
6809 for (done = 0; done < 1000; ++done)
6810 {
6811 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6812 if (tp->tp_topframe != topframe)
6813 {
6814 tabpage_close_other(tp, eap->forceit);
6815 /* if we failed to close it quit */
6816 if (valid_tabpage(tp))
6817 done = 1000;
6818 /* start over, "tp" is now invalid */
6819 break;
6820 }
6821 if (first_tabpage->tp_next == NULL)
6822 break;
6823 }
6824 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006825}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006826
6827/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006828 * Close the current tab page.
6829 */
6830 void
6831tabpage_close(forceit)
6832 int forceit;
6833{
6834 /* First close all the windows but the current one. If that worked then
6835 * close the last window in this tab, that will close it. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006836 if (lastwin != firstwin)
6837 close_others(TRUE, forceit);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006838 if (lastwin == firstwin)
6839 ex_win_close(forceit, curwin, NULL);
6840# ifdef FEAT_GUI
6841 need_mouse_correct = TRUE;
6842# endif
6843}
6844
6845/*
6846 * Close tab page "tp", which is not the current tab page.
6847 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006848 * Also takes care of the tab pages line disappearing when closing the
6849 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00006850 */
6851 void
6852tabpage_close_other(tp, forceit)
6853 tabpage_T *tp;
6854 int forceit;
6855{
6856 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006857 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006858 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006859
6860 /* Limit to 1000 windows, autocommands may add a window while we close
6861 * one. OK, so I'm paranoid... */
6862 while (++done < 1000)
6863 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006864 wp = tp->tp_firstwin;
6865 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006866
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006867 /* Autocommands may delete the tab page under our fingers and we may
6868 * fail to close a window with a modified buffer. */
6869 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006870 break;
6871 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006872
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006873 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006874 if (h != tabline_height())
6875 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006876}
6877
6878/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006879 * ":only".
6880 */
6881 static void
6882ex_only(eap)
6883 exarg_T *eap;
6884{
6885# ifdef FEAT_GUI
6886 need_mouse_correct = TRUE;
6887# endif
6888 close_others(TRUE, eap->forceit);
6889}
6890
6891/*
6892 * ":all" and ":sall".
Bram Moolenaard9967712006-03-11 21:18:15 +00006893 * Also used for ":tab drop file ..." after setting the argument list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006894 */
Bram Moolenaard9967712006-03-11 21:18:15 +00006895 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00006896ex_all(eap)
6897 exarg_T *eap;
6898{
6899 if (eap->addr_count == 0)
6900 eap->line2 = 9999;
Bram Moolenaard9967712006-03-11 21:18:15 +00006901 do_arg_all((int)eap->line2, eap->forceit, eap->cmdidx == CMD_drop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006902}
6903#endif /* FEAT_WINDOWS */
6904
6905 static void
6906ex_hide(eap)
6907 exarg_T *eap;
6908{
6909 if (*eap->arg != NUL && check_nextcmd(eap->arg) == NULL)
6910 eap->errmsg = e_invarg;
6911 else
6912 {
6913 /* ":hide" or ":hide | cmd": hide current window */
6914 eap->nextcmd = check_nextcmd(eap->arg);
6915#ifdef FEAT_WINDOWS
6916 if (!eap->skip)
6917 {
6918# ifdef FEAT_GUI
6919 need_mouse_correct = TRUE;
6920# endif
6921 win_close(curwin, FALSE); /* don't free buffer */
6922 }
6923#endif
6924 }
6925}
6926
6927/*
6928 * ":stop" and ":suspend": Suspend Vim.
6929 */
6930 static void
6931ex_stop(eap)
6932 exarg_T *eap;
6933{
6934 /*
6935 * Disallow suspending for "rvim".
6936 */
6937 if (!check_restricted()
6938#ifdef WIN3264
6939 /*
6940 * Check if external commands are allowed now.
6941 */
6942 && can_end_termcap_mode(TRUE)
6943#endif
6944 )
6945 {
6946 if (!eap->forceit)
6947 autowrite_all();
6948 windgoto((int)Rows - 1, 0);
6949 out_char('\n');
6950 out_flush();
6951 stoptermcap();
6952 out_flush(); /* needed for SUN to restore xterm buffer */
6953#ifdef FEAT_TITLE
6954 mch_restore_title(3); /* restore window titles */
6955#endif
6956 ui_suspend(); /* call machine specific function */
6957#ifdef FEAT_TITLE
6958 maketitle();
6959 resettitle(); /* force updating the title */
6960#endif
6961 starttermcap();
6962 scroll_start(); /* scroll screen before redrawing */
6963 redraw_later_clear();
6964 shell_resized(); /* may have resized window */
6965 }
6966}
6967
6968/*
6969 * ":exit", ":xit" and ":wq": Write file and exit Vim.
6970 */
6971 static void
6972ex_exit(eap)
6973 exarg_T *eap;
6974{
6975#ifdef FEAT_CMDWIN
6976 if (cmdwin_type != 0)
6977 {
6978 cmdwin_result = Ctrl_C;
6979 return;
6980 }
6981#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006982 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006983 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006984 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006985 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006986 return;
6987 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006988#ifdef FEAT_AUTOCMD
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01006989 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
6990 /* Refuse to quit when locked or when the buffer in the last window is
6991 * being closed (can only happen in autocommands). */
6992 if (curbuf_locked() || (curbuf->b_nwindows == 1 && curbuf->b_closing))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006993 return;
6994#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006995
6996 /*
6997 * if more files or windows we won't exit
6998 */
6999 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
7000 exiting = TRUE;
7001 if ( ((eap->cmdidx == CMD_wq
7002 || curbufIsChanged())
7003 && do_write(eap) == FAIL)
7004 || check_more(TRUE, eap->forceit) == FAIL
7005 || (only_one_window() && check_changed_any(eap->forceit)))
7006 {
7007 not_exiting();
7008 }
7009 else
7010 {
7011#ifdef FEAT_WINDOWS
7012 if (only_one_window()) /* quit last window, exit Vim */
7013#endif
7014 getout(0);
7015#ifdef FEAT_WINDOWS
7016# ifdef FEAT_GUI
7017 need_mouse_correct = TRUE;
7018# endif
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02007019 /* Quit current window, may free the buffer. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007020 win_close(curwin, !P_HID(curwin->w_buffer));
7021#endif
7022 }
7023}
7024
7025/*
7026 * ":print", ":list", ":number".
7027 */
7028 static void
7029ex_print(eap)
7030 exarg_T *eap;
7031{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007032 if (curbuf->b_ml.ml_flags & ML_EMPTY)
7033 EMSG(_(e_emptybuf));
7034 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007035 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007036 for ( ;!got_int; ui_breakcheck())
7037 {
7038 print_line(eap->line1,
7039 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
7040 || (eap->flags & EXFLAG_NR)),
7041 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
7042 if (++eap->line1 > eap->line2)
7043 break;
7044 out_flush(); /* show one line at a time */
7045 }
7046 setpcmark();
7047 /* put cursor at last line */
7048 curwin->w_cursor.lnum = eap->line2;
7049 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007050 }
7051
Bram Moolenaar071d4272004-06-13 20:20:40 +00007052 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007053}
7054
7055#ifdef FEAT_BYTEOFF
7056 static void
7057ex_goto(eap)
7058 exarg_T *eap;
7059{
7060 goto_byte(eap->line2);
7061}
7062#endif
7063
7064/*
7065 * ":shell".
7066 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007067 static void
7068ex_shell(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007069 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007070{
7071 do_shell(NULL, 0);
7072}
7073
7074#if (defined(FEAT_WINDOWS) && defined(HAVE_DROP_FILE)) \
7075 || (defined(FEAT_GUI_GTK) && defined(FEAT_DND)) \
Bram Moolenaar371baa92005-12-29 22:43:53 +00007076 || defined(FEAT_GUI_MSWIN) \
7077 || defined(FEAT_GUI_MAC) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00007078 || defined(PROTO)
7079
7080/*
7081 * Handle a file drop. The code is here because a drop is *nearly* like an
7082 * :args command, but not quite (we have a list of exact filenames, so we
7083 * don't want to (a) parse a command line, or (b) expand wildcards. So the
7084 * code is very similar to :args and hence needs access to a lot of the static
7085 * functions in this file.
7086 *
7087 * The list should be allocated using alloc(), as should each item in the
7088 * list. This function takes over responsibility for freeing the list.
7089 *
Bram Moolenaar81870892007-11-11 18:17:28 +00007090 * XXX The list is made into the argument list. This is freed using
Bram Moolenaar071d4272004-06-13 20:20:40 +00007091 * FreeWild(), which does a series of vim_free() calls, unless the two defines
7092 * __EMX__ and __ALWAYS_HAS_TRAILING_NUL_POINTER are set. In this case, a
7093 * routine _fnexplodefree() is used. This may cause problems, but as the drop
7094 * file functionality is (currently) not in EMX this is not presently a
7095 * problem.
7096 */
7097 void
7098handle_drop(filec, filev, split)
7099 int filec; /* the number of files dropped */
7100 char_u **filev; /* the list of files dropped */
7101 int split; /* force splitting the window */
7102{
7103 exarg_T ea;
7104 int save_msg_scroll = msg_scroll;
7105
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007106 /* Postpone this while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007107 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007108 return;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007109#ifdef FEAT_AUTOCMD
7110 if (curbuf_locked())
7111 return;
7112#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00007113 /* When the screen is being updated we should not change buffers and
7114 * windows structures, it may cause freed memory to be used. */
7115 if (updating_screen)
7116 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007117
7118 /* Check whether the current buffer is changed. If so, we will need
7119 * to split the current window or data could be lost.
7120 * We don't need to check if the 'hidden' option is set, as in this
7121 * case the buffer won't be lost.
7122 */
7123 if (!P_HID(curbuf) && !split)
7124 {
7125 ++emsg_off;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01007126 split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007127 --emsg_off;
7128 }
7129 if (split)
7130 {
7131# ifdef FEAT_WINDOWS
7132 if (win_split(0, 0) == FAIL)
7133 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007134 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007135
7136 /* When splitting the window, create a new alist. Otherwise the
7137 * existing one is overwritten. */
7138 alist_unlink(curwin->w_alist);
7139 alist_new();
7140# else
7141 return; /* can't split, always fail */
7142# endif
7143 }
7144
7145 /*
7146 * Set up the new argument list.
7147 */
Bram Moolenaar86b68352004-12-27 21:59:20 +00007148 alist_set(ALIST(curwin), filec, filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007149
7150 /*
7151 * Move to the first file.
7152 */
7153 /* Fake up a minimal "next" command for do_argfile() */
7154 vim_memset(&ea, 0, sizeof(ea));
7155 ea.cmd = (char_u *)"next";
7156 do_argfile(&ea, 0);
7157
7158 /* do_ecmd() may set need_start_insertmode, but since we never left Insert
7159 * mode that is not needed here. */
7160 need_start_insertmode = FALSE;
7161
7162 /* Restore msg_scroll, otherwise a following command may cause scrolling
7163 * unexpectedly. The screen will be redrawn by the caller, thus
7164 * msg_scroll being set by displaying a message is irrelevant. */
7165 msg_scroll = save_msg_scroll;
7166}
7167#endif
7168
Bram Moolenaar071d4272004-06-13 20:20:40 +00007169/*
7170 * Clear an argument list: free all file names and reset it to zero entries.
7171 */
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007172 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00007173alist_clear(al)
7174 alist_T *al;
7175{
7176 while (--al->al_ga.ga_len >= 0)
7177 vim_free(AARGLIST(al)[al->al_ga.ga_len].ae_fname);
7178 ga_clear(&al->al_ga);
7179}
7180
7181/*
7182 * Init an argument list.
7183 */
7184 void
7185alist_init(al)
7186 alist_T *al;
7187{
7188 ga_init2(&al->al_ga, (int)sizeof(aentry_T), 5);
7189}
7190
7191#if defined(FEAT_WINDOWS) || defined(PROTO)
7192
7193/*
7194 * Remove a reference from an argument list.
7195 * Ignored when the argument list is the global one.
7196 * If the argument list is no longer used by any window, free it.
7197 */
7198 void
7199alist_unlink(al)
7200 alist_T *al;
7201{
7202 if (al != &global_alist && --al->al_refcount <= 0)
7203 {
7204 alist_clear(al);
7205 vim_free(al);
7206 }
7207}
7208
7209# if defined(FEAT_LISTCMDS) || defined(HAVE_DROP_FILE) || defined(PROTO)
7210/*
7211 * Create a new argument list and use it for the current window.
7212 */
7213 void
7214alist_new()
7215{
7216 curwin->w_alist = (alist_T *)alloc((unsigned)sizeof(alist_T));
7217 if (curwin->w_alist == NULL)
7218 {
7219 curwin->w_alist = &global_alist;
7220 ++global_alist.al_refcount;
7221 }
7222 else
7223 {
7224 curwin->w_alist->al_refcount = 1;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02007225 curwin->w_alist->id = ++max_alist_id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007226 alist_init(curwin->w_alist);
7227 }
7228}
7229# endif
7230#endif
7231
7232#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE) || defined(PROTO)
7233/*
7234 * Expand the file names in the global argument list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00007235 * If "fnum_list" is not NULL, use "fnum_list[fnum_len]" as a list of buffer
7236 * numbers to be re-used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007237 */
7238 void
Bram Moolenaar86b68352004-12-27 21:59:20 +00007239alist_expand(fnum_list, fnum_len)
7240 int *fnum_list;
7241 int fnum_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007242{
7243 char_u **old_arg_files;
Bram Moolenaar86b68352004-12-27 21:59:20 +00007244 int old_arg_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007245 char_u **new_arg_files;
7246 int new_arg_file_count;
7247 char_u *save_p_su = p_su;
7248 int i;
7249
7250 /* Don't use 'suffixes' here. This should work like the shell did the
7251 * expansion. Also, the vimrc file isn't read yet, thus the user
7252 * can't set the options. */
7253 p_su = empty_option;
7254 old_arg_files = (char_u **)alloc((unsigned)(sizeof(char_u *) * GARGCOUNT));
7255 if (old_arg_files != NULL)
7256 {
7257 for (i = 0; i < GARGCOUNT; ++i)
Bram Moolenaar86b68352004-12-27 21:59:20 +00007258 old_arg_files[i] = vim_strsave(GARGLIST[i].ae_fname);
7259 old_arg_count = GARGCOUNT;
7260 if (expand_wildcards(old_arg_count, old_arg_files,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007261 &new_arg_file_count, &new_arg_files,
Bram Moolenaarb5609832011-07-20 15:04:58 +02007262 EW_FILE|EW_NOTFOUND|EW_ADDSLASH|EW_NOERROR) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007263 && new_arg_file_count > 0)
7264 {
Bram Moolenaar86b68352004-12-27 21:59:20 +00007265 alist_set(&global_alist, new_arg_file_count, new_arg_files,
7266 TRUE, fnum_list, fnum_len);
7267 FreeWild(old_arg_count, old_arg_files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007268 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007269 }
7270 p_su = save_p_su;
7271}
7272#endif
7273
7274/*
7275 * Set the argument list for the current window.
7276 * Takes over the allocated files[] and the allocated fnames in it.
7277 */
7278 void
Bram Moolenaar86b68352004-12-27 21:59:20 +00007279alist_set(al, count, files, use_curbuf, fnum_list, fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007280 alist_T *al;
7281 int count;
7282 char_u **files;
7283 int use_curbuf;
Bram Moolenaar86b68352004-12-27 21:59:20 +00007284 int *fnum_list;
7285 int fnum_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007286{
7287 int i;
7288
7289 alist_clear(al);
7290 if (ga_grow(&al->al_ga, count) == OK)
7291 {
7292 for (i = 0; i < count; ++i)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007293 {
7294 if (got_int)
7295 {
7296 /* When adding many buffers this can take a long time. Allow
7297 * interrupting here. */
7298 while (i < count)
7299 vim_free(files[i++]);
7300 break;
7301 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00007302
7303 /* May set buffer name of a buffer previously used for the
7304 * argument list, so that it's re-used by alist_add. */
7305 if (fnum_list != NULL && i < fnum_len)
7306 buf_set_name(fnum_list[i], files[i]);
7307
Bram Moolenaar071d4272004-06-13 20:20:40 +00007308 alist_add(al, files[i], use_curbuf ? 2 : 1);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007309 ui_breakcheck();
7310 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007311 vim_free(files);
7312 }
7313 else
7314 FreeWild(count, files);
7315#ifdef FEAT_WINDOWS
7316 if (al == &global_alist)
7317#endif
7318 arg_had_last = FALSE;
7319}
7320
7321/*
7322 * Add file "fname" to argument list "al".
7323 * "fname" must have been allocated and "al" must have been checked for room.
7324 */
7325 void
7326alist_add(al, fname, set_fnum)
7327 alist_T *al;
7328 char_u *fname;
7329 int set_fnum; /* 1: set buffer number; 2: re-use curbuf */
7330{
7331 if (fname == NULL) /* don't add NULL file names */
7332 return;
7333#ifdef BACKSLASH_IN_FILENAME
7334 slash_adjust(fname);
7335#endif
7336 AARGLIST(al)[al->al_ga.ga_len].ae_fname = fname;
7337 if (set_fnum > 0)
7338 AARGLIST(al)[al->al_ga.ga_len].ae_fnum =
7339 buflist_add(fname, BLN_LISTED | (set_fnum == 2 ? BLN_CURBUF : 0));
7340 ++al->al_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007341}
7342
7343#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
7344/*
7345 * Adjust slashes in file names. Called after 'shellslash' was set.
7346 */
7347 void
7348alist_slash_adjust()
7349{
7350 int i;
7351# ifdef FEAT_WINDOWS
7352 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007353 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007354# endif
7355
7356 for (i = 0; i < GARGCOUNT; ++i)
7357 if (GARGLIST[i].ae_fname != NULL)
7358 slash_adjust(GARGLIST[i].ae_fname);
7359# ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00007360 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007361 if (wp->w_alist != &global_alist)
7362 for (i = 0; i < WARGCOUNT(wp); ++i)
7363 if (WARGLIST(wp)[i].ae_fname != NULL)
7364 slash_adjust(WARGLIST(wp)[i].ae_fname);
7365# endif
7366}
7367#endif
7368
7369/*
7370 * ":preserve".
7371 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007372 static void
7373ex_preserve(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007374 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007375{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007376 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007377 ml_preserve(curbuf, TRUE);
7378}
7379
7380/*
7381 * ":recover".
7382 */
7383 static void
7384ex_recover(eap)
7385 exarg_T *eap;
7386{
7387 /* Set recoverymode right away to avoid the ATTENTION prompt. */
7388 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01007389 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
7390 | CCGD_MULTWIN
7391 | (eap->forceit ? CCGD_FORCEIT : 0)
7392 | CCGD_EXCMD)
7393
Bram Moolenaar071d4272004-06-13 20:20:40 +00007394 && (*eap->arg == NUL
7395 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
7396 ml_recover();
7397 recoverymode = FALSE;
7398}
7399
7400/*
7401 * Command modifier used in a wrong way.
7402 */
7403 static void
7404ex_wrongmodifier(eap)
7405 exarg_T *eap;
7406{
7407 eap->errmsg = e_invcmd;
7408}
7409
7410#ifdef FEAT_WINDOWS
7411/*
7412 * :sview [+command] file split window with new file, read-only
7413 * :split [[+command] file] split window with current or new file
7414 * :vsplit [[+command] file] split window vertically with current or new file
7415 * :new [[+command] file] split window with no or new file
7416 * :vnew [[+command] file] split vertically window with no or new file
7417 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007418 *
7419 * :tabedit open new Tab page with empty window
7420 * :tabedit [+command] file open new Tab page and edit "file"
7421 * :tabnew [[+command] file] just like :tabedit
7422 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007423 */
7424 void
7425ex_splitview(eap)
7426 exarg_T *eap;
7427{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007428 win_T *old_curwin = curwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007429# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007430 char_u *fname = NULL;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007431# endif
7432# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007433 int browse_flag = cmdmod.browse;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007434# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007435
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007436# ifndef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007437 if (eap->cmdidx == CMD_vsplit || eap->cmdidx == CMD_vnew)
7438 {
7439 ex_ni(eap);
7440 return;
7441 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007442# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007443
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007444# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007445 need_mouse_correct = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007446# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007447
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007448# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00007449 /* A ":split" in the quickfix window works like ":new". Don't want two
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007450 * quickfix windows. But it's OK when doing ":tab split". */
7451 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007452 {
7453 if (eap->cmdidx == CMD_split)
7454 eap->cmdidx = CMD_new;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007455# ifdef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007456 if (eap->cmdidx == CMD_vsplit)
7457 eap->cmdidx = CMD_vnew;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007458# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007459 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007460# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007461
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007462# ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007463 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007464 {
7465 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
7466 FNAME_MESS, TRUE, curbuf->b_ffname);
7467 if (fname == NULL)
7468 goto theend;
7469 eap->arg = fname;
7470 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007471# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007472 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007473# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007474# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007475# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007476 if (cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007477# ifdef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007478 && eap->cmdidx != CMD_vnew
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007479# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007480 && eap->cmdidx != CMD_new)
7481 {
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007482# ifdef FEAT_AUTOCMD
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007483 if (
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007484# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007485 !gui.in_use &&
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007486# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007487 au_has_group((char_u *)"FileExplorer"))
7488 {
7489 /* No browsing supported but we do have the file explorer:
7490 * Edit the directory. */
7491 if (*eap->arg == NUL || !mch_isdir(eap->arg))
7492 eap->arg = (char_u *)".";
7493 }
7494 else
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007495# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007496 {
7497 fname = do_browse(0, (char_u *)_("Edit File in new window"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007498 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007499 if (fname == NULL)
7500 goto theend;
7501 eap->arg = fname;
7502 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007503 }
7504 cmdmod.browse = FALSE; /* Don't browse again in do_ecmd(). */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007505# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007506
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007507 /*
7508 * Either open new tab page or split the window.
7509 */
7510 if (eap->cmdidx == CMD_tabedit
7511 || eap->cmdidx == CMD_tabfind
7512 || eap->cmdidx == CMD_tabnew)
7513 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00007514 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
7515 : eap->addr_count == 0 ? 0
7516 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007517 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00007518 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007519
7520 /* set the alternate buffer for the window we came from */
7521 if (curwin != old_curwin
7522 && win_valid(old_curwin)
7523 && old_curwin->w_buffer != curbuf
7524 && !cmdmod.keepalt)
7525 old_curwin->w_alt_fnum = curbuf->b_fnum;
7526 }
7527 }
7528 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007529 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
7530 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007531# ifdef FEAT_SCROLLBIND
Bram Moolenaar071d4272004-06-13 20:20:40 +00007532 /* Reset 'scrollbind' when editing another file, but keep it when
7533 * doing ":split" without arguments. */
7534 if (*eap->arg != NUL
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007535# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007536 || cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007537# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007538 )
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007539 {
7540 RESET_BINDING(curwin);
7541 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007542 else
7543 do_check_scrollbind(FALSE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007544# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007545 do_exedit(eap, old_curwin);
7546 }
7547
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007548# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007549 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007550# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007551
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007552# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007553theend:
7554 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007555# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007556}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007557
7558/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007559 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007560 */
7561 void
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007562tabpage_new()
7563{
7564 exarg_T ea;
7565
7566 vim_memset(&ea, 0, sizeof(ea));
7567 ea.cmdidx = CMD_tabnew;
7568 ea.cmd = (char_u *)"tabn";
7569 ea.arg = (char_u *)"";
7570 ex_splitview(&ea);
7571}
7572
7573/*
7574 * :tabnext command
7575 */
7576 static void
7577ex_tabnext(eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007578 exarg_T *eap;
7579{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007580 switch (eap->cmdidx)
7581 {
7582 case CMD_tabfirst:
7583 case CMD_tabrewind:
7584 goto_tabpage(1);
7585 break;
7586 case CMD_tablast:
7587 goto_tabpage(9999);
7588 break;
7589 case CMD_tabprevious:
7590 case CMD_tabNext:
7591 goto_tabpage(eap->addr_count == 0 ? -1 : -(int)eap->line2);
7592 break;
7593 default: /* CMD_tabnext */
7594 goto_tabpage(eap->addr_count == 0 ? 0 : (int)eap->line2);
7595 break;
7596 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007597}
7598
7599/*
7600 * :tabmove command
7601 */
7602 static void
7603ex_tabmove(eap)
7604 exarg_T *eap;
7605{
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02007606 int tab_number = 9999;
7607
7608 if (eap->arg && *eap->arg != NUL)
7609 {
7610 char_u *p = eap->arg;
7611 int relative = 0; /* argument +N/-N means: move N places to the
7612 * right/left relative to the current position. */
7613
7614 if (*eap->arg == '-')
7615 {
7616 relative = -1;
7617 p = eap->arg + 1;
7618 }
7619 else if (*eap->arg == '+')
7620 {
7621 relative = 1;
7622 p = eap->arg + 1;
7623 }
7624 else
7625 p = eap->arg;
7626
7627 if (p == skipdigits(p))
7628 {
7629 /* No numbers as argument. */
7630 eap->errmsg = e_invarg;
7631 return;
7632 }
7633
7634 tab_number = getdigits(&p);
7635 if (relative != 0)
7636 tab_number = tab_number * relative + tabpage_index(curtab) - 1;;
7637 }
7638 else if (eap->addr_count != 0)
7639 tab_number = eap->line2;
7640
7641 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007642}
7643
7644/*
7645 * :tabs command: List tabs and their contents.
7646 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007647 static void
7648ex_tabs(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007649 exarg_T *eap UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007650{
7651 tabpage_T *tp;
7652 win_T *wp;
7653 int tabcount = 1;
7654
7655 msg_start();
7656 msg_scroll = TRUE;
7657 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
7658 {
7659 msg_putchar('\n');
7660 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
7661 msg_outtrans_attr(IObuff, hl_attr(HLF_T));
7662 out_flush(); /* output one line at a time */
7663 ui_breakcheck();
7664
Bram Moolenaar030f0df2006-02-21 22:02:53 +00007665 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007666 wp = firstwin;
7667 else
7668 wp = tp->tp_firstwin;
7669 for ( ; wp != NULL && !got_int; wp = wp->w_next)
7670 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007671 msg_putchar('\n');
7672 msg_putchar(wp == curwin ? '>' : ' ');
7673 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007674 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
7675 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007676 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02007677 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007678 else
7679 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
7680 IObuff, IOSIZE, TRUE);
7681 msg_outtrans(IObuff);
7682 out_flush(); /* output one line at a time */
7683 ui_breakcheck();
7684 }
7685 }
7686}
7687
7688#endif /* FEAT_WINDOWS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007689
7690/*
7691 * ":mode": Set screen mode.
7692 * If no argument given, just get the screen size and redraw.
7693 */
7694 static void
7695ex_mode(eap)
7696 exarg_T *eap;
7697{
7698 if (*eap->arg == NUL)
7699 shell_resized();
7700 else
7701 mch_screenmode(eap->arg);
7702}
7703
7704#ifdef FEAT_WINDOWS
7705/*
7706 * ":resize".
7707 * set, increment or decrement current window height
7708 */
7709 static void
7710ex_resize(eap)
7711 exarg_T *eap;
7712{
7713 int n;
7714 win_T *wp = curwin;
7715
7716 if (eap->addr_count > 0)
7717 {
7718 n = eap->line2;
7719 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
7720 ;
7721 }
7722
7723#ifdef FEAT_GUI
7724 need_mouse_correct = TRUE;
7725#endif
7726 n = atol((char *)eap->arg);
7727#ifdef FEAT_VERTSPLIT
7728 if (cmdmod.split & WSP_VERT)
7729 {
7730 if (*eap->arg == '-' || *eap->arg == '+')
7731 n += W_WIDTH(curwin);
7732 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
7733 n = 9999;
7734 win_setwidth_win((int)n, wp);
7735 }
7736 else
7737#endif
7738 {
7739 if (*eap->arg == '-' || *eap->arg == '+')
7740 n += curwin->w_height;
7741 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
7742 n = 9999;
7743 win_setheight_win((int)n, wp);
7744 }
7745}
7746#endif
7747
7748/*
7749 * ":find [+command] <file>" command.
7750 */
7751 static void
7752ex_find(eap)
7753 exarg_T *eap;
7754{
7755#ifdef FEAT_SEARCHPATH
7756 char_u *fname;
7757 int count;
7758
7759 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
7760 TRUE, curbuf->b_ffname);
7761 if (eap->addr_count > 0)
7762 {
7763 /* Repeat finding the file "count" times. This matters when it
7764 * appears several times in the path. */
7765 count = eap->line2;
7766 while (fname != NULL && --count > 0)
7767 {
7768 vim_free(fname);
7769 fname = find_file_in_path(NULL, 0, FNAME_MESS,
7770 FALSE, curbuf->b_ffname);
7771 }
7772 }
7773
7774 if (fname != NULL)
7775 {
7776 eap->arg = fname;
7777#endif
7778 do_exedit(eap, NULL);
7779#ifdef FEAT_SEARCHPATH
7780 vim_free(fname);
7781 }
7782#endif
7783}
7784
7785/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00007786 * ":open" simulation: for now just work like ":visual".
7787 */
7788 static void
7789ex_open(eap)
7790 exarg_T *eap;
7791{
7792 regmatch_T regmatch;
7793 char_u *p;
7794
7795 curwin->w_cursor.lnum = eap->line2;
7796 beginline(BL_SOL | BL_FIX);
7797 if (*eap->arg == '/')
7798 {
7799 /* ":open /pattern/": put cursor in column found with pattern */
7800 ++eap->arg;
7801 p = skip_regexp(eap->arg, '/', p_magic, NULL);
7802 *p = NUL;
7803 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
7804 if (regmatch.regprog != NULL)
7805 {
7806 regmatch.rm_ic = p_ic;
7807 p = ml_get_curline();
7808 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007809 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007810 else
7811 EMSG(_(e_nomatch));
Bram Moolenaar473de612013-06-08 18:19:48 +02007812 vim_regfree(regmatch.regprog);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007813 }
7814 /* Move to the NUL, ignore any other arguments. */
7815 eap->arg += STRLEN(eap->arg);
7816 }
7817 check_cursor();
7818
7819 eap->cmdidx = CMD_visual;
7820 do_exedit(eap, NULL);
7821}
7822
7823/*
7824 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007825 */
7826 static void
7827ex_edit(eap)
7828 exarg_T *eap;
7829{
7830 do_exedit(eap, NULL);
7831}
7832
7833/*
7834 * ":edit <file>" command and alikes.
7835 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007836 void
7837do_exedit(eap, old_curwin)
7838 exarg_T *eap;
7839 win_T *old_curwin; /* curwin before doing a split or NULL */
7840{
7841 int n;
7842#ifdef FEAT_WINDOWS
7843 int need_hide;
7844#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00007845 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007846
7847 /*
7848 * ":vi" command ends Ex mode.
7849 */
7850 if (exmode_active && (eap->cmdidx == CMD_visual
7851 || eap->cmdidx == CMD_view))
7852 {
7853 exmode_active = FALSE;
7854 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007855 {
7856 /* Special case: ":global/pat/visual\NLvi-commands" */
7857 if (global_busy)
7858 {
7859 int rd = RedrawingDisabled;
7860 int nwr = no_wait_return;
7861 int ms = msg_scroll;
7862#ifdef FEAT_GUI
7863 int he = hold_gui_events;
7864#endif
7865
7866 if (eap->nextcmd != NULL)
7867 {
7868 stuffReadbuff(eap->nextcmd);
7869 eap->nextcmd = NULL;
7870 }
7871
7872 if (exmode_was != EXMODE_VIM)
7873 settmode(TMODE_RAW);
7874 RedrawingDisabled = 0;
7875 no_wait_return = 0;
7876 need_wait_return = FALSE;
7877 msg_scroll = 0;
7878#ifdef FEAT_GUI
7879 hold_gui_events = 0;
7880#endif
7881 must_redraw = CLEAR;
7882
7883 main_loop(FALSE, TRUE);
7884
7885 RedrawingDisabled = rd;
7886 no_wait_return = nwr;
7887 msg_scroll = ms;
7888#ifdef FEAT_GUI
7889 hold_gui_events = he;
7890#endif
7891 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007892 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007893 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007894 }
7895
7896 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007897 || eap->cmdidx == CMD_tabnew
7898 || eap->cmdidx == CMD_tabedit
Bram Moolenaar071d4272004-06-13 20:20:40 +00007899#ifdef FEAT_VERTSPLIT
7900 || eap->cmdidx == CMD_vnew
7901#endif
7902 ) && *eap->arg == NUL)
7903 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007904 /* ":new" or ":tabnew" without argument: edit an new empty buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007905 setpcmark();
7906 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007907 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
7908 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007909 }
7910 else if ((eap->cmdidx != CMD_split
7911#ifdef FEAT_VERTSPLIT
7912 && eap->cmdidx != CMD_vsplit
7913#endif
7914 )
7915 || *eap->arg != NUL
7916#ifdef FEAT_BROWSE
7917 || cmdmod.browse
7918#endif
7919 )
7920 {
Bram Moolenaar5555acc2006-04-07 21:33:12 +00007921#ifdef FEAT_AUTOCMD
7922 /* Can't edit another file when "curbuf_lock" is set. Only ":edit"
7923 * can bring us here, others are stopped earlier. */
7924 if (*eap->arg != NUL && curbuf_locked())
7925 return;
7926#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007927 n = readonlymode;
7928 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
7929 readonlymode = TRUE;
7930 else if (eap->cmdidx == CMD_enew)
7931 readonlymode = FALSE; /* 'readonly' doesn't make sense in an
7932 empty buffer */
7933 setpcmark();
7934 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
7935 NULL, eap,
7936 /* ":edit" goes to first line if Vi compatible */
7937 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
7938 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
7939 ? ECMD_ONE : eap->do_ecmd_lnum,
7940 (P_HID(curbuf) ? ECMD_HIDE : 0)
7941 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar7b449342014-03-25 13:03:48 +01007942 /* after a split we can use an existing buffer */
7943 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007944#ifdef FEAT_LISTCMDS
7945 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
7946#endif
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007947 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007948 {
7949 /* Editing the file failed. If the window was split, close it. */
7950#ifdef FEAT_WINDOWS
7951 if (old_curwin != NULL)
7952 {
7953 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
7954 if (!need_hide || P_HID(curbuf))
7955 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007956# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7957 cleanup_T cs;
7958
7959 /* Reset the error/interrupt/exception state here so that
7960 * aborting() returns FALSE when closing a window. */
7961 enter_cleanup(&cs);
7962# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007963# ifdef FEAT_GUI
7964 need_mouse_correct = TRUE;
7965# endif
7966 win_close(curwin, !need_hide && !P_HID(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007967
7968# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7969 /* Restore the error/interrupt/exception state if not
7970 * discarded by a new aborting error, interrupt, or
7971 * uncaught exception. */
7972 leave_cleanup(&cs);
7973# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007974 }
7975 }
7976#endif
7977 }
7978 else if (readonlymode && curbuf->b_nwindows == 1)
7979 {
7980 /* When editing an already visited buffer, 'readonly' won't be set
7981 * but the previous value is kept. With ":view" and ":sview" we
7982 * want the file to be readonly, except when another window is
7983 * editing the same buffer. */
7984 curbuf->b_p_ro = TRUE;
7985 }
7986 readonlymode = n;
7987 }
7988 else
7989 {
7990 if (eap->do_ecmd_cmd != NULL)
7991 do_cmdline_cmd(eap->do_ecmd_cmd);
7992#ifdef FEAT_TITLE
7993 n = curwin->w_arg_idx_invalid;
7994#endif
7995 check_arg_idx(curwin);
7996#ifdef FEAT_TITLE
7997 if (n != curwin->w_arg_idx_invalid)
7998 maketitle();
7999#endif
8000 }
8001
8002#ifdef FEAT_WINDOWS
8003 /*
8004 * if ":split file" worked, set alternate file name in old window to new
8005 * file
8006 */
8007 if (old_curwin != NULL
8008 && *eap->arg != NUL
8009 && curwin != old_curwin
8010 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00008011 && old_curwin->w_buffer != curbuf
8012 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008013 old_curwin->w_alt_fnum = curbuf->b_fnum;
8014#endif
8015
8016 ex_no_reprint = TRUE;
8017}
8018
8019#ifndef FEAT_GUI
8020/*
8021 * ":gui" and ":gvim" when there is no GUI.
8022 */
8023 static void
8024ex_nogui(eap)
8025 exarg_T *eap;
8026{
8027 eap->errmsg = e_nogvim;
8028}
8029#endif
8030
8031#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
8032 static void
8033ex_tearoff(eap)
8034 exarg_T *eap;
8035{
8036 gui_make_tearoff(eap->arg);
8037}
8038#endif
8039
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008040#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008041 static void
8042ex_popup(eap)
8043 exarg_T *eap;
8044{
Bram Moolenaar97409f12005-07-08 22:17:29 +00008045 gui_make_popup(eap->arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008046}
8047#endif
8048
Bram Moolenaar071d4272004-06-13 20:20:40 +00008049 static void
8050ex_swapname(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008051 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008052{
8053 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
8054 MSG(_("No swap file"));
8055 else
8056 msg(curbuf->b_ml.ml_mfp->mf_fname);
8057}
8058
8059/*
8060 * ":syncbind" forces all 'scrollbind' windows to have the same relative
8061 * offset.
8062 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
8063 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008064 static void
8065ex_syncbind(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008066 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008067{
8068#ifdef FEAT_SCROLLBIND
8069 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008070 win_T *save_curwin = curwin;
8071 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008072 long topline;
8073 long y;
8074 linenr_T old_linenr = curwin->w_cursor.lnum;
8075
8076 setpcmark();
8077
8078 /*
8079 * determine max topline
8080 */
8081 if (curwin->w_p_scb)
8082 {
8083 topline = curwin->w_topline;
8084 for (wp = firstwin; wp; wp = wp->w_next)
8085 {
8086 if (wp->w_p_scb && wp->w_buffer)
8087 {
8088 y = wp->w_buffer->b_ml.ml_line_count - p_so;
8089 if (topline > y)
8090 topline = y;
8091 }
8092 }
8093 if (topline < 1)
8094 topline = 1;
8095 }
8096 else
8097 {
8098 topline = 1;
8099 }
8100
8101
8102 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008103 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008104 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008105 for (curwin = firstwin; curwin; curwin = curwin->w_next)
8106 {
8107 if (curwin->w_p_scb)
8108 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008109 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008110 y = topline - curwin->w_topline;
8111 if (y > 0)
8112 scrollup(y, TRUE);
8113 else
8114 scrolldown(-y, TRUE);
8115 curwin->w_scbind_pos = topline;
8116 redraw_later(VALID);
8117 cursor_correct();
8118#ifdef FEAT_WINDOWS
8119 curwin->w_redr_status = TRUE;
8120#endif
8121 }
8122 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008123 curwin = save_curwin;
8124 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008125 if (curwin->w_p_scb)
8126 {
8127 did_syncbind = TRUE;
8128 checkpcmark();
8129 if (old_linenr != curwin->w_cursor.lnum)
8130 {
8131 char_u ctrl_o[2];
8132
8133 ctrl_o[0] = Ctrl_O;
8134 ctrl_o[1] = 0;
8135 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
8136 }
8137 }
8138#endif
8139}
8140
8141
8142 static void
8143ex_read(eap)
8144 exarg_T *eap;
8145{
Bram Moolenaardf177f62005-02-22 08:39:57 +00008146 int i;
8147 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
8148 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008149
8150 if (eap->usefilter) /* :r!cmd */
8151 do_bang(1, eap, FALSE, FALSE, TRUE);
8152 else
8153 {
8154 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
8155 return;
8156
8157#ifdef FEAT_BROWSE
8158 if (cmdmod.browse)
8159 {
8160 char_u *browseFile;
8161
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008162 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008163 NULL, NULL, NULL, curbuf);
8164 if (browseFile != NULL)
8165 {
8166 i = readfile(browseFile, NULL,
8167 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8168 vim_free(browseFile);
8169 }
8170 else
8171 i = OK;
8172 }
8173 else
8174#endif
8175 if (*eap->arg == NUL)
8176 {
8177 if (check_fname() == FAIL) /* check for no file name */
8178 return;
8179 i = readfile(curbuf->b_ffname, curbuf->b_fname,
8180 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8181 }
8182 else
8183 {
8184 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
8185 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
8186 i = readfile(eap->arg, NULL,
8187 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8188
8189 }
8190 if (i == FAIL)
8191 {
8192#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
8193 if (!aborting())
8194#endif
8195 EMSG2(_(e_notopen), eap->arg);
8196 }
8197 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008198 {
8199 if (empty && exmode_active)
8200 {
8201 /* Delete the empty line that remains. Historically ex does
8202 * this but vi doesn't. */
8203 if (eap->line2 == 0)
8204 lnum = curbuf->b_ml.ml_line_count;
8205 else
8206 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008207 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008208 {
8209 ml_delete(lnum, FALSE);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008210 if (curwin->w_cursor.lnum > 1
8211 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008212 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00008213 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008214 }
8215 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008216 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008217 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008218 }
8219}
8220
Bram Moolenaarea408852005-06-25 22:49:46 +00008221static char_u *prev_dir = NULL;
8222
8223#if defined(EXITFREE) || defined(PROTO)
8224 void
8225free_cd_dir()
8226{
8227 vim_free(prev_dir);
Bram Moolenaara0174af2008-01-02 20:08:25 +00008228 prev_dir = NULL;
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00008229
8230 vim_free(globaldir);
8231 globaldir = NULL;
Bram Moolenaarea408852005-06-25 22:49:46 +00008232}
8233#endif
8234
Bram Moolenaarf4258302013-06-02 18:20:17 +02008235/*
8236 * Deal with the side effects of changing the current directory.
8237 * When "local" is TRUE then this was after an ":lcd" command.
8238 */
8239 void
8240post_chdir(local)
8241 int local;
8242{
8243 vim_free(curwin->w_localdir);
Bram Moolenaarbd2dc342014-01-10 15:53:13 +01008244 curwin->w_localdir = NULL;
Bram Moolenaarf4258302013-06-02 18:20:17 +02008245 if (local)
8246 {
8247 /* If still in global directory, need to remember current
8248 * directory as global directory. */
8249 if (globaldir == NULL && prev_dir != NULL)
8250 globaldir = vim_strsave(prev_dir);
8251 /* Remember this local directory for the window. */
8252 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8253 curwin->w_localdir = vim_strsave(NameBuff);
8254 }
8255 else
8256 {
8257 /* We are now in the global directory, no need to remember its
8258 * name. */
8259 vim_free(globaldir);
8260 globaldir = NULL;
Bram Moolenaarf4258302013-06-02 18:20:17 +02008261 }
8262
8263 shorten_fnames(TRUE);
8264}
8265
Bram Moolenaarea408852005-06-25 22:49:46 +00008266
Bram Moolenaar071d4272004-06-13 20:20:40 +00008267/*
8268 * ":cd", ":lcd", ":chdir" and ":lchdir".
8269 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00008270 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00008271ex_cd(eap)
8272 exarg_T *eap;
8273{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008274 char_u *new_dir;
8275 char_u *tofree;
8276
8277 new_dir = eap->arg;
8278#if !defined(UNIX) && !defined(VMS)
8279 /* for non-UNIX ":cd" means: print current directory */
8280 if (*new_dir == NUL)
8281 ex_pwd(NULL);
8282 else
8283#endif
8284 {
Bram Moolenaar8e8fe9b2009-03-11 14:37:32 +00008285#ifdef FEAT_AUTOCMD
8286 if (allbuf_locked())
8287 return;
8288#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00008289 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged()
8290 && !eap->forceit)
8291 {
Bram Moolenaar81870892007-11-11 18:17:28 +00008292 EMSG(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00008293 return;
8294 }
8295
Bram Moolenaar071d4272004-06-13 20:20:40 +00008296 /* ":cd -": Change to previous directory */
8297 if (STRCMP(new_dir, "-") == 0)
8298 {
8299 if (prev_dir == NULL)
8300 {
8301 EMSG(_("E186: No previous directory"));
8302 return;
8303 }
8304 new_dir = prev_dir;
8305 }
8306
8307 /* Save current directory for next ":cd -" */
8308 tofree = prev_dir;
8309 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8310 prev_dir = vim_strsave(NameBuff);
8311 else
8312 prev_dir = NULL;
8313
8314#if defined(UNIX) || defined(VMS)
8315 /* for UNIX ":cd" means: go to home directory */
8316 if (*new_dir == NUL)
8317 {
8318 /* use NameBuff for home directory name */
8319# ifdef VMS
8320 char_u *p;
8321
8322 p = mch_getenv((char_u *)"SYS$LOGIN");
8323 if (p == NULL || *p == NUL) /* empty is the same as not set */
8324 NameBuff[0] = NUL;
8325 else
Bram Moolenaarb6356332005-07-18 21:40:44 +00008326 vim_strncpy(NameBuff, p, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008327# else
8328 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
8329# endif
8330 new_dir = NameBuff;
8331 }
8332#endif
8333 if (new_dir == NULL || vim_chdir(new_dir))
8334 EMSG(_(e_failed));
8335 else
8336 {
Bram Moolenaarf4258302013-06-02 18:20:17 +02008337 post_chdir(eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008338
8339 /* Echo the new current directory if the command was typed. */
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00008340 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008341 ex_pwd(eap);
8342 }
8343 vim_free(tofree);
8344 }
8345}
8346
8347/*
8348 * ":pwd".
8349 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008350 static void
8351ex_pwd(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008352 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008353{
8354 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8355 {
8356#ifdef BACKSLASH_IN_FILENAME
8357 slash_adjust(NameBuff);
8358#endif
8359 msg(NameBuff);
8360 }
8361 else
8362 EMSG(_("E187: Unknown"));
8363}
8364
8365/*
8366 * ":=".
8367 */
8368 static void
8369ex_equal(eap)
8370 exarg_T *eap;
8371{
8372 smsg((char_u *)"%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008373 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008374}
8375
8376 static void
8377ex_sleep(eap)
8378 exarg_T *eap;
8379{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008380 int n;
8381 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008382
8383 if (cursor_valid())
8384 {
8385 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
8386 if (n >= 0)
Bram Moolenaar10395d82014-02-05 22:46:52 +01008387 windgoto((int)n, W_WINCOL(curwin) + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008388 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008389
8390 len = eap->line2;
8391 switch (*eap->arg)
8392 {
8393 case 'm': break;
8394 case NUL: len *= 1000L; break;
8395 default: EMSG2(_(e_invarg2), eap->arg); return;
8396 }
8397 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008398}
8399
8400/*
8401 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
8402 */
8403 void
8404do_sleep(msec)
8405 long msec;
8406{
8407 long done;
8408
8409 cursor_on();
8410 out_flush();
8411 for (done = 0; !got_int && done < msec; done += 1000L)
8412 {
8413 ui_delay(msec - done > 1000L ? 1000L : msec - done, TRUE);
8414 ui_breakcheck();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02008415#ifdef FEAT_NETBEANS_INTG
8416 /* Process the netbeans messages that may have been received in the
8417 * call to ui_breakcheck() when the GUI is in use. This may occur when
8418 * running a test case. */
8419 netbeans_parse_messages();
8420#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008421 }
8422}
8423
8424 static void
8425do_exmap(eap, isabbrev)
8426 exarg_T *eap;
8427 int isabbrev;
8428{
8429 int mode;
8430 char_u *cmdp;
8431
8432 cmdp = eap->cmd;
8433 mode = get_map_mode(&cmdp, eap->forceit || isabbrev);
8434
8435 switch (do_map((*cmdp == 'n') ? 2 : (*cmdp == 'u'),
8436 eap->arg, mode, isabbrev))
8437 {
8438 case 1: EMSG(_(e_invarg));
8439 break;
8440 case 2: EMSG(isabbrev ? _(e_noabbr) : _(e_nomap));
8441 break;
8442 }
8443}
8444
8445/*
8446 * ":winsize" command (obsolete).
8447 */
8448 static void
8449ex_winsize(eap)
8450 exarg_T *eap;
8451{
8452 int w, h;
8453 char_u *arg = eap->arg;
8454 char_u *p;
8455
8456 w = getdigits(&arg);
8457 arg = skipwhite(arg);
8458 p = arg;
8459 h = getdigits(&arg);
8460 if (*p != NUL && *arg == NUL)
8461 set_shellsize(w, h, TRUE);
8462 else
8463 EMSG(_("E465: :winsize requires two number arguments"));
8464}
8465
8466#ifdef FEAT_WINDOWS
8467 static void
8468ex_wincmd(eap)
8469 exarg_T *eap;
8470{
8471 int xchar = NUL;
8472 char_u *p;
8473
8474 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
8475 {
8476 /* CTRL-W g and CTRL-W CTRL-G have an extra command character */
8477 if (eap->arg[1] == NUL)
8478 {
8479 EMSG(_(e_invarg));
8480 return;
8481 }
8482 xchar = eap->arg[1];
8483 p = eap->arg + 2;
8484 }
8485 else
8486 p = eap->arg + 1;
8487
8488 eap->nextcmd = check_nextcmd(p);
8489 p = skipwhite(p);
8490 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
8491 EMSG(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02008492 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008493 {
8494 /* Pass flags on for ":vertical wincmd ]". */
8495 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008496 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008497 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
8498 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008499 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008500 }
8501}
8502#endif
8503
Bram Moolenaar843ee412004-06-30 16:16:41 +00008504#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008505/*
8506 * ":winpos".
8507 */
8508 static void
8509ex_winpos(eap)
8510 exarg_T *eap;
8511{
8512 int x, y;
8513 char_u *arg = eap->arg;
8514 char_u *p;
8515
8516 if (*arg == NUL)
8517 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00008518# if defined(FEAT_GUI) || defined(MSWIN)
8519# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008520 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00008521# else
8522 if (mch_get_winpos(&x, &y) != FAIL)
8523# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008524 {
8525 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
8526 msg(IObuff);
8527 }
8528 else
8529# endif
8530 EMSG(_("E188: Obtaining window position not implemented for this platform"));
8531 }
8532 else
8533 {
8534 x = getdigits(&arg);
8535 arg = skipwhite(arg);
8536 p = arg;
8537 y = getdigits(&arg);
8538 if (*p == NUL || *arg != NUL)
8539 {
8540 EMSG(_("E466: :winpos requires two number arguments"));
8541 return;
8542 }
8543# ifdef FEAT_GUI
8544 if (gui.in_use)
8545 gui_mch_set_winpos(x, y);
8546 else if (gui.starting)
8547 {
8548 /* Remember the coordinates for when the window is opened. */
8549 gui_win_x = x;
8550 gui_win_y = y;
8551 }
8552# ifdef HAVE_TGETENT
8553 else
8554# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00008555# else
8556# ifdef MSWIN
8557 mch_set_winpos(x, y);
8558# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008559# endif
8560# ifdef HAVE_TGETENT
8561 if (*T_CWP)
8562 term_set_winpos(x, y);
8563# endif
8564 }
8565}
8566#endif
8567
8568/*
8569 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
8570 */
8571 static void
8572ex_operators(eap)
8573 exarg_T *eap;
8574{
8575 oparg_T oa;
8576
8577 clear_oparg(&oa);
8578 oa.regname = eap->regname;
8579 oa.start.lnum = eap->line1;
8580 oa.end.lnum = eap->line2;
8581 oa.line_count = eap->line2 - eap->line1 + 1;
8582 oa.motion_type = MLINE;
8583#ifdef FEAT_VIRTUALEDIT
8584 virtual_op = FALSE;
8585#endif
8586 if (eap->cmdidx != CMD_yank) /* position cursor for undo */
8587 {
8588 setpcmark();
8589 curwin->w_cursor.lnum = eap->line1;
8590 beginline(BL_SOL | BL_FIX);
8591 }
8592
Bram Moolenaard07c6e12013-11-21 14:21:40 +01008593 if (VIsual_active)
8594 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01008595
Bram Moolenaar071d4272004-06-13 20:20:40 +00008596 switch (eap->cmdidx)
8597 {
8598 case CMD_delete:
8599 oa.op_type = OP_DELETE;
8600 op_delete(&oa);
8601 break;
8602
8603 case CMD_yank:
8604 oa.op_type = OP_YANK;
8605 (void)op_yank(&oa, FALSE, TRUE);
8606 break;
8607
8608 default: /* CMD_rshift or CMD_lshift */
Bram Moolenaar6e707362013-06-14 19:15:58 +02008609 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00008610#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02008611 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
8612#else
8613 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00008614#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02008615 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00008616 oa.op_type = OP_RSHIFT;
8617 else
8618 oa.op_type = OP_LSHIFT;
8619 op_shift(&oa, FALSE, eap->amount);
8620 break;
8621 }
8622#ifdef FEAT_VIRTUALEDIT
8623 virtual_op = MAYBE;
8624#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00008625 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008626}
8627
8628/*
8629 * ":put".
8630 */
8631 static void
8632ex_put(eap)
8633 exarg_T *eap;
8634{
8635 /* ":0put" works like ":1put!". */
8636 if (eap->line2 == 0)
8637 {
8638 eap->line2 = 1;
8639 eap->forceit = TRUE;
8640 }
8641 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008642 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
8643 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008644}
8645
8646/*
8647 * Handle ":copy" and ":move".
8648 */
8649 static void
8650ex_copymove(eap)
8651 exarg_T *eap;
8652{
8653 long n;
8654
8655 n = get_address(&eap->arg, FALSE, FALSE);
8656 if (eap->arg == NULL) /* error detected */
8657 {
8658 eap->nextcmd = NULL;
8659 return;
8660 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008661 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008662
8663 /*
8664 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
8665 */
8666 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
8667 {
8668 EMSG(_(e_invaddr));
8669 return;
8670 }
8671
8672 if (eap->cmdidx == CMD_move)
8673 {
8674 if (do_move(eap->line1, eap->line2, n) == FAIL)
8675 return;
8676 }
8677 else
8678 ex_copy(eap->line1, eap->line2, n);
8679 u_clearline();
8680 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008681 ex_may_print(eap);
8682}
8683
8684/*
8685 * Print the current line if flags were given to the Ex command.
8686 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02008687 void
Bram Moolenaardf177f62005-02-22 08:39:57 +00008688ex_may_print(eap)
8689 exarg_T *eap;
8690{
8691 if (eap->flags != 0)
8692 {
8693 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
8694 (eap->flags & EXFLAG_LIST));
8695 ex_no_reprint = TRUE;
8696 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008697}
8698
8699/*
8700 * ":smagic" and ":snomagic".
8701 */
8702 static void
8703ex_submagic(eap)
8704 exarg_T *eap;
8705{
8706 int magic_save = p_magic;
8707
8708 p_magic = (eap->cmdidx == CMD_smagic);
8709 do_sub(eap);
8710 p_magic = magic_save;
8711}
8712
8713/*
8714 * ":join".
8715 */
8716 static void
8717ex_join(eap)
8718 exarg_T *eap;
8719{
8720 curwin->w_cursor.lnum = eap->line1;
8721 if (eap->line1 == eap->line2)
8722 {
8723 if (eap->addr_count >= 2) /* :2,2join does nothing */
8724 return;
8725 if (eap->line2 == curbuf->b_ml.ml_line_count)
8726 {
8727 beep_flush();
8728 return;
8729 }
8730 ++eap->line2;
8731 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02008732 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008733 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008734 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008735}
8736
8737/*
8738 * ":[addr]@r" or ":[addr]*r": execute register
8739 */
8740 static void
8741ex_at(eap)
8742 exarg_T *eap;
8743{
8744 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00008745 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008746
8747 curwin->w_cursor.lnum = eap->line2;
8748
8749#ifdef USE_ON_FLY_SCROLL
8750 dont_scroll = TRUE; /* disallow scrolling here */
8751#endif
8752
8753 /* get the register name. No name means to use the previous one */
8754 c = *eap->arg;
8755 if (c == NUL || (c == '*' && *eap->cmd == '*'))
8756 c = '@';
Bram Moolenaard333d1e2006-11-07 17:43:47 +00008757 /* Put the register in the typeahead buffer with the "silent" flag. */
8758 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
8759 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008760 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008761 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00008762 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008763 else
8764 {
8765 int save_efr = exec_from_reg;
8766
8767 exec_from_reg = TRUE;
8768
8769 /*
8770 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00008771 * Continue until the stuff buffer is empty and all added characters
8772 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008773 */
Bram Moolenaar60462872009-11-03 11:40:19 +00008774 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008775 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
8776
8777 exec_from_reg = save_efr;
8778 }
8779}
8780
8781/*
8782 * ":!".
8783 */
8784 static void
8785ex_bang(eap)
8786 exarg_T *eap;
8787{
8788 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
8789}
8790
8791/*
8792 * ":undo".
8793 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008794 static void
8795ex_undo(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008796 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008797{
Bram Moolenaard3667a22006-03-16 21:35:52 +00008798 if (eap->addr_count == 1) /* :undo 123 */
Bram Moolenaar730cde92010-06-27 05:18:54 +02008799 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00008800 else
8801 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008802}
8803
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008804#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008805 static void
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008806ex_wundo(eap)
8807 exarg_T *eap;
8808{
8809 char_u hash[UNDO_HASH_SIZE];
8810
8811 u_compute_hash(hash);
8812 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
8813}
8814
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008815 static void
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008816ex_rundo(eap)
8817 exarg_T *eap;
8818{
8819 char_u hash[UNDO_HASH_SIZE];
8820
8821 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02008822 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008823}
8824#endif
8825
Bram Moolenaar071d4272004-06-13 20:20:40 +00008826/*
8827 * ":redo".
8828 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008829 static void
8830ex_redo(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008831 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008832{
8833 u_redo(1);
8834}
8835
8836/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008837 * ":earlier" and ":later".
8838 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008839 static void
8840ex_later(eap)
8841 exarg_T *eap;
8842{
8843 long count = 0;
8844 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008845 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008846 char_u *p = eap->arg;
8847
8848 if (*p == NUL)
8849 count = 1;
8850 else if (isdigit(*p))
8851 {
8852 count = getdigits(&p);
8853 switch (*p)
8854 {
8855 case 's': ++p; sec = TRUE; break;
8856 case 'm': ++p; sec = TRUE; count *= 60; break;
8857 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008858 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
8859 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008860 }
8861 }
8862
8863 if (*p != NUL)
8864 EMSG2(_(e_invarg2), eap->arg);
8865 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02008866 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
8867 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008868}
8869
8870/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008871 * ":redir": start/stop redirection.
8872 */
8873 static void
8874ex_redir(eap)
8875 exarg_T *eap;
8876{
8877 char *mode;
8878 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00008879 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008880
8881 if (STRICMP(eap->arg, "END") == 0)
8882 close_redir();
8883 else
8884 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008885 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008886 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008887 ++arg;
8888 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008889 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008890 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008891 mode = "a";
8892 }
8893 else
8894 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00008895 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008896
8897 close_redir();
8898
8899 /* Expand environment variables and "~/". */
Bram Moolenaarca472992005-01-21 11:46:23 +00008900 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008901 if (fname == NULL)
8902 return;
8903#ifdef FEAT_BROWSE
8904 if (cmdmod.browse)
8905 {
8906 char_u *browseFile;
8907
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008908 browseFile = do_browse(BROWSE_SAVE,
8909 (char_u *)_("Save Redirection"),
8910 fname, NULL, NULL, BROWSE_FILTER_ALL_FILES, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008911 if (browseFile == NULL)
8912 return; /* operation cancelled */
8913 vim_free(fname);
8914 fname = browseFile;
8915 eap->forceit = TRUE; /* since dialog already asked */
8916 }
8917#endif
8918
8919 redir_fd = open_exfile(fname, eap->forceit, mode);
8920 vim_free(fname);
8921 }
8922#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00008923 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008924 {
8925 /* redirect to a register a-z (resp. A-Z for appending) */
8926 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00008927 ++arg;
8928 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008929# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00008930 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00008931 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008932# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00008933 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008934 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008935 redir_reg = *arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008936 if (*arg == '>' && arg[1] == '>') /* append */
Bram Moolenaard9d30582005-05-18 22:10:28 +00008937 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008938 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008939 {
Bram Moolenaarc188b882007-10-19 14:20:54 +00008940 /* Can use both "@a" and "@a>". */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00008941 if (*arg == '>')
8942 arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008943 /* Make register empty when not using @A-@Z and the
8944 * command is valid. */
8945 if (*arg == NUL && !isupper(redir_reg))
8946 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008947 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008948 }
8949 if (*arg != NUL)
8950 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00008951 redir_reg = 0;
Bram Moolenaard9d30582005-05-18 22:10:28 +00008952 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008953 }
8954 }
8955 else if (*arg == '=' && arg[1] == '>')
8956 {
8957 int append;
8958
8959 /* redirect to a variable */
8960 close_redir();
8961 arg += 2;
8962
8963 if (*arg == '>')
8964 {
8965 ++arg;
8966 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008967 }
8968 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008969 append = FALSE;
8970
8971 if (var_redir_start(skipwhite(arg), append) == OK)
8972 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008973 }
8974#endif
8975
8976 /* TODO: redirect to a buffer */
8977
Bram Moolenaar071d4272004-06-13 20:20:40 +00008978 else
Bram Moolenaarca472992005-01-21 11:46:23 +00008979 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008980 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00008981
8982 /* Make sure redirection is not off. Can happen for cmdline completion
8983 * that indirectly invokes a command to catch its output. */
8984 if (redir_fd != NULL
8985#ifdef FEAT_EVAL
8986 || redir_reg || redir_vname
8987#endif
8988 )
8989 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008990}
8991
8992/*
8993 * ":redraw": force redraw
8994 */
8995 static void
8996ex_redraw(eap)
8997 exarg_T *eap;
8998{
8999 int r = RedrawingDisabled;
9000 int p = p_lz;
9001
9002 RedrawingDisabled = 0;
9003 p_lz = FALSE;
9004 update_topline();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009005 update_screen(eap->forceit ? CLEAR : VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009006#ifdef FEAT_TITLE
9007 if (need_maketitle)
9008 maketitle();
9009#endif
9010 RedrawingDisabled = r;
9011 p_lz = p;
9012
9013 /* Reset msg_didout, so that a message that's there is overwritten. */
9014 msg_didout = FALSE;
9015 msg_col = 0;
9016
Bram Moolenaar56667a52013-07-17 11:54:28 +02009017 /* No need to wait after an intentional redraw. */
9018 need_wait_return = FALSE;
9019
Bram Moolenaar071d4272004-06-13 20:20:40 +00009020 out_flush();
9021}
9022
9023/*
9024 * ":redrawstatus": force redraw of status line(s)
9025 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009026 static void
9027ex_redrawstatus(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009028 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009029{
9030#if defined(FEAT_WINDOWS)
9031 int r = RedrawingDisabled;
9032 int p = p_lz;
9033
9034 RedrawingDisabled = 0;
9035 p_lz = FALSE;
9036 if (eap->forceit)
9037 status_redraw_all();
9038 else
9039 status_redraw_curbuf();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009040 update_screen(VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009041 RedrawingDisabled = r;
9042 p_lz = p;
9043 out_flush();
9044#endif
9045}
9046
9047 static void
9048close_redir()
9049{
9050 if (redir_fd != NULL)
9051 {
9052 fclose(redir_fd);
9053 redir_fd = NULL;
9054 }
9055#ifdef FEAT_EVAL
9056 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009057 if (redir_vname)
9058 {
9059 var_redir_stop();
9060 redir_vname = 0;
9061 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009062#endif
9063}
9064
9065#if defined(FEAT_SESSION) && defined(USE_CRNL)
9066# define MKSESSION_NL
9067static int mksession_nl = FALSE; /* use NL only in put_eol() */
9068#endif
9069
9070/*
9071 * ":mkexrc", ":mkvimrc", ":mkview" and ":mksession".
9072 */
9073 static void
9074ex_mkrc(eap)
9075 exarg_T *eap;
9076{
9077 FILE *fd;
9078 int failed = FALSE;
9079 char_u *fname;
9080#ifdef FEAT_BROWSE
9081 char_u *browseFile = NULL;
9082#endif
9083#ifdef FEAT_SESSION
9084 int view_session = FALSE;
9085 int using_vdir = FALSE; /* using 'viewdir'? */
9086 char_u *viewFile = NULL;
9087 unsigned *flagp;
9088#endif
9089
9090 if (eap->cmdidx == CMD_mksession || eap->cmdidx == CMD_mkview)
9091 {
9092#ifdef FEAT_SESSION
9093 view_session = TRUE;
9094#else
9095 ex_ni(eap);
9096 return;
9097#endif
9098 }
9099
9100#ifdef FEAT_SESSION
Bram Moolenaar8d594672009-07-01 18:18:57 +00009101 /* Use the short file name until ":lcd" is used. We also don't use the
9102 * short file name when 'acd' is set, that is checked later. */
Bram Moolenaareeefcc72007-05-01 21:21:21 +00009103 did_lcd = FALSE;
9104
Bram Moolenaar071d4272004-06-13 20:20:40 +00009105 /* ":mkview" or ":mkview 9": generate file name with 'viewdir' */
9106 if (eap->cmdidx == CMD_mkview
9107 && (*eap->arg == NUL
9108 || (vim_isdigit(*eap->arg) && eap->arg[1] == NUL)))
9109 {
9110 eap->forceit = TRUE;
9111 fname = get_view_file(*eap->arg);
9112 if (fname == NULL)
9113 return;
9114 viewFile = fname;
9115 using_vdir = TRUE;
9116 }
9117 else
9118#endif
9119 if (*eap->arg != NUL)
9120 fname = eap->arg;
9121 else if (eap->cmdidx == CMD_mkvimrc)
9122 fname = (char_u *)VIMRC_FILE;
9123#ifdef FEAT_SESSION
9124 else if (eap->cmdidx == CMD_mksession)
9125 fname = (char_u *)SESSION_FILE;
9126#endif
9127 else
9128 fname = (char_u *)EXRC_FILE;
9129
9130#ifdef FEAT_BROWSE
9131 if (cmdmod.browse)
9132 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00009133 browseFile = do_browse(BROWSE_SAVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009134# ifdef FEAT_SESSION
9135 eap->cmdidx == CMD_mkview ? (char_u *)_("Save View") :
9136 eap->cmdidx == CMD_mksession ? (char_u *)_("Save Session") :
9137# endif
9138 (char_u *)_("Save Setup"),
9139 fname, (char_u *)"vim", NULL, BROWSE_FILTER_MACROS, NULL);
9140 if (browseFile == NULL)
9141 goto theend;
9142 fname = browseFile;
9143 eap->forceit = TRUE; /* since dialog already asked */
9144 }
9145#endif
9146
9147#if defined(FEAT_SESSION) && defined(vim_mkdir)
9148 /* When using 'viewdir' may have to create the directory. */
9149 if (using_vdir && !mch_isdir(p_vdir))
Bram Moolenaardf177f62005-02-22 08:39:57 +00009150 vim_mkdir_emsg(p_vdir, 0755);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009151#endif
9152
9153 fd = open_exfile(fname, eap->forceit, WRITEBIN);
9154 if (fd != NULL)
9155 {
9156#ifdef FEAT_SESSION
9157 if (eap->cmdidx == CMD_mkview)
9158 flagp = &vop_flags;
9159 else
9160 flagp = &ssop_flags;
9161#endif
9162
9163#ifdef MKSESSION_NL
9164 /* "unix" in 'sessionoptions': use NL line separator */
9165 if (view_session && (*flagp & SSOP_UNIX))
9166 mksession_nl = TRUE;
9167#endif
9168
9169 /* Write the version command for :mkvimrc */
9170 if (eap->cmdidx == CMD_mkvimrc)
9171 (void)put_line(fd, "version 6.0");
9172
9173#ifdef FEAT_SESSION
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009174 if (eap->cmdidx == CMD_mksession)
9175 {
9176 if (put_line(fd, "let SessionLoad = 1") == FAIL)
9177 failed = TRUE;
9178 }
9179
Bram Moolenaar071d4272004-06-13 20:20:40 +00009180 if (eap->cmdidx != CMD_mkview)
9181#endif
9182 {
9183 /* Write setting 'compatible' first, because it has side effects.
9184 * For that same reason only do it when needed. */
9185 if (p_cp)
9186 (void)put_line(fd, "if !&cp | set cp | endif");
9187 else
9188 (void)put_line(fd, "if &cp | set nocp | endif");
9189 }
9190
9191#ifdef FEAT_SESSION
9192 if (!view_session
9193 || (eap->cmdidx == CMD_mksession
9194 && (*flagp & SSOP_OPTIONS)))
9195#endif
9196 failed |= (makemap(fd, NULL) == FAIL
9197 || makeset(fd, OPT_GLOBAL, FALSE) == FAIL);
9198
9199#ifdef FEAT_SESSION
9200 if (!failed && view_session)
9201 {
9202 if (put_line(fd, "let s:so_save = &so | let s:siso_save = &siso | set so=0 siso=0") == FAIL)
9203 failed = TRUE;
9204 if (eap->cmdidx == CMD_mksession)
9205 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02009206 char_u *dirnow; /* current directory */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009207
Bram Moolenaard9462e32011-04-11 21:35:11 +02009208 dirnow = alloc(MAXPATHL);
9209 if (dirnow == NULL)
9210 failed = TRUE;
9211 else
9212 {
9213 /*
9214 * Change to session file's dir.
9215 */
9216 if (mch_dirname(dirnow, MAXPATHL) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00009217 || mch_chdir((char *)dirnow) != 0)
Bram Moolenaard9462e32011-04-11 21:35:11 +02009218 *dirnow = NUL;
9219 if (*dirnow != NUL && (ssop_flags & SSOP_SESDIR))
9220 {
9221 if (vim_chdirfile(fname) == OK)
9222 shorten_fnames(TRUE);
9223 }
9224 else if (*dirnow != NUL
9225 && (ssop_flags & SSOP_CURDIR) && globaldir != NULL)
9226 {
9227 if (mch_chdir((char *)globaldir) == 0)
9228 shorten_fnames(TRUE);
9229 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009230
Bram Moolenaard9462e32011-04-11 21:35:11 +02009231 failed |= (makeopens(fd, dirnow) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009232
Bram Moolenaard9462e32011-04-11 21:35:11 +02009233 /* restore original dir */
9234 if (*dirnow != NUL && ((ssop_flags & SSOP_SESDIR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009235 || ((ssop_flags & SSOP_CURDIR) && globaldir != NULL)))
Bram Moolenaard9462e32011-04-11 21:35:11 +02009236 {
9237 if (mch_chdir((char *)dirnow) != 0)
9238 EMSG(_(e_prev_dir));
9239 shorten_fnames(TRUE);
9240 }
9241 vim_free(dirnow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009242 }
9243 }
9244 else
9245 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +00009246 failed |= (put_view(fd, curwin, !using_vdir, flagp,
9247 -1) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009248 }
9249 if (put_line(fd, "let &so = s:so_save | let &siso = s:siso_save")
9250 == FAIL)
9251 failed = TRUE;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009252 if (put_line(fd, "doautoall SessionLoadPost") == FAIL)
9253 failed = TRUE;
Bram Moolenaar4770d092006-01-12 23:22:24 +00009254 if (eap->cmdidx == CMD_mksession)
9255 {
9256 if (put_line(fd, "unlet SessionLoad") == FAIL)
9257 failed = TRUE;
9258 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009259 }
9260#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009261 if (put_line(fd, "\" vim: set ft=vim :") == FAIL)
9262 failed = TRUE;
9263
Bram Moolenaar071d4272004-06-13 20:20:40 +00009264 failed |= fclose(fd);
9265
9266 if (failed)
9267 EMSG(_(e_write));
9268#if defined(FEAT_EVAL) && defined(FEAT_SESSION)
9269 else if (eap->cmdidx == CMD_mksession)
9270 {
9271 /* successful session write - set this_session var */
Bram Moolenaard9462e32011-04-11 21:35:11 +02009272 char_u *tbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009273
Bram Moolenaard9462e32011-04-11 21:35:11 +02009274 tbuf = alloc(MAXPATHL);
9275 if (tbuf != NULL)
9276 {
9277 if (vim_FullName(fname, tbuf, MAXPATHL, FALSE) == OK)
9278 set_vim_var_string(VV_THIS_SESSION, tbuf, -1);
9279 vim_free(tbuf);
9280 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009281 }
9282#endif
9283#ifdef MKSESSION_NL
9284 mksession_nl = FALSE;
9285#endif
9286 }
9287
9288#ifdef FEAT_BROWSE
9289theend:
9290 vim_free(browseFile);
9291#endif
9292#ifdef FEAT_SESSION
9293 vim_free(viewFile);
9294#endif
9295}
9296
Bram Moolenaardf177f62005-02-22 08:39:57 +00009297#if ((defined(FEAT_SESSION) || defined(FEAT_EVAL)) && defined(vim_mkdir)) \
9298 || defined(PROTO)
9299 int
9300vim_mkdir_emsg(name, prot)
9301 char_u *name;
Bram Moolenaar78a15312009-05-15 19:33:18 +00009302 int prot UNUSED;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009303{
9304 if (vim_mkdir(name, prot) != 0)
9305 {
9306 EMSG2(_("E739: Cannot create directory: %s"), name);
9307 return FAIL;
9308 }
9309 return OK;
9310}
9311#endif
9312
Bram Moolenaar071d4272004-06-13 20:20:40 +00009313/*
9314 * Open a file for writing for an Ex command, with some checks.
9315 * Return file descriptor, or NULL on failure.
9316 */
9317 FILE *
9318open_exfile(fname, forceit, mode)
9319 char_u *fname;
9320 int forceit;
9321 char *mode; /* "w" for create new file or "a" for append */
9322{
9323 FILE *fd;
9324
9325#ifdef UNIX
9326 /* with Unix it is possible to open a directory */
9327 if (mch_isdir(fname))
9328 {
9329 EMSG2(_(e_isadir2), fname);
9330 return NULL;
9331 }
9332#endif
9333 if (!forceit && *mode != 'a' && vim_fexists(fname))
9334 {
9335 EMSG2(_("E189: \"%s\" exists (add ! to override)"), fname);
9336 return NULL;
9337 }
9338
9339 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
9340 EMSG2(_("E190: Cannot open \"%s\" for writing"), fname);
9341
9342 return fd;
9343}
9344
9345/*
9346 * ":mark" and ":k".
9347 */
9348 static void
9349ex_mark(eap)
9350 exarg_T *eap;
9351{
9352 pos_T pos;
9353
9354 if (*eap->arg == NUL) /* No argument? */
9355 EMSG(_(e_argreq));
9356 else if (eap->arg[1] != NUL) /* more than one character? */
9357 EMSG(_(e_trailing));
9358 else
9359 {
9360 pos = curwin->w_cursor; /* save curwin->w_cursor */
9361 curwin->w_cursor.lnum = eap->line2;
9362 beginline(BL_WHITE | BL_FIX);
9363 if (setmark(*eap->arg) == FAIL) /* set mark */
9364 EMSG(_("E191: Argument must be a letter or forward/backward quote"));
9365 curwin->w_cursor = pos; /* restore curwin->w_cursor */
9366 }
9367}
9368
9369/*
9370 * Update w_topline, w_leftcol and the cursor position.
9371 */
9372 void
9373update_topline_cursor()
9374{
9375 check_cursor(); /* put cursor on valid line */
9376 update_topline();
9377 if (!curwin->w_p_wrap)
9378 validate_cursor();
9379 update_curswant();
9380}
9381
9382#ifdef FEAT_EX_EXTRA
9383/*
9384 * ":normal[!] {commands}": Execute normal mode commands.
9385 */
9386 static void
9387ex_normal(eap)
9388 exarg_T *eap;
9389{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009390 int save_msg_scroll = msg_scroll;
9391 int save_restart_edit = restart_edit;
9392 int save_msg_didout = msg_didout;
9393 int save_State = State;
9394 tasave_T tabuf;
9395 int save_insertmode = p_im;
9396 int save_finish_op = finish_op;
Bram Moolenaar38084112008-07-26 14:05:07 +00009397 int save_opcount = opcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009398#ifdef FEAT_MBYTE
9399 char_u *arg = NULL;
9400 int l;
9401 char_u *p;
9402#endif
9403
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00009404 if (ex_normal_lock > 0)
9405 {
9406 EMSG(_(e_secure));
9407 return;
9408 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009409 if (ex_normal_busy >= p_mmd)
9410 {
9411 EMSG(_("E192: Recursive use of :normal too deep"));
9412 return;
9413 }
9414 ++ex_normal_busy;
9415
9416 msg_scroll = FALSE; /* no msg scrolling in Normal mode */
9417 restart_edit = 0; /* don't go to Insert mode */
9418 p_im = FALSE; /* don't use 'insertmode' */
9419
9420#ifdef FEAT_MBYTE
9421 /*
9422 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
9423 * this for the K_SPECIAL leading byte, otherwise special keys will not
9424 * work.
9425 */
9426 if (has_mbyte)
9427 {
9428 int len = 0;
9429
9430 /* Count the number of characters to be escaped. */
9431 for (p = eap->arg; *p != NUL; ++p)
9432 {
9433# ifdef FEAT_GUI
9434 if (*p == CSI) /* leadbyte CSI */
9435 len += 2;
9436# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009437 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009438 if (*++p == K_SPECIAL /* trailbyte K_SPECIAL or CSI */
9439# ifdef FEAT_GUI
9440 || *p == CSI
9441# endif
9442 )
9443 len += 2;
9444 }
9445 if (len > 0)
9446 {
9447 arg = alloc((unsigned)(STRLEN(eap->arg) + len + 1));
9448 if (arg != NULL)
9449 {
9450 len = 0;
9451 for (p = eap->arg; *p != NUL; ++p)
9452 {
9453 arg[len++] = *p;
9454# ifdef FEAT_GUI
9455 if (*p == CSI)
9456 {
9457 arg[len++] = KS_EXTRA;
9458 arg[len++] = (int)KE_CSI;
9459 }
9460# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009461 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009462 {
9463 arg[len++] = *++p;
9464 if (*p == K_SPECIAL)
9465 {
9466 arg[len++] = KS_SPECIAL;
9467 arg[len++] = KE_FILLER;
9468 }
9469# ifdef FEAT_GUI
9470 else if (*p == CSI)
9471 {
9472 arg[len++] = KS_EXTRA;
9473 arg[len++] = (int)KE_CSI;
9474 }
9475# endif
9476 }
9477 arg[len] = NUL;
9478 }
9479 }
9480 }
9481 }
9482#endif
9483
9484 /*
9485 * Save the current typeahead. This is required to allow using ":normal"
9486 * from an event handler and makes sure we don't hang when the argument
9487 * ends with half a command.
9488 */
9489 save_typeahead(&tabuf);
9490 if (tabuf.typebuf_valid)
9491 {
9492 /*
9493 * Repeat the :normal command for each line in the range. When no
9494 * range given, execute it just once, without positioning the cursor
9495 * first.
9496 */
9497 do
9498 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009499 if (eap->addr_count != 0)
9500 {
9501 curwin->w_cursor.lnum = eap->line1++;
9502 curwin->w_cursor.col = 0;
9503 }
9504
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009505 exec_normal_cmd(
Bram Moolenaar071d4272004-06-13 20:20:40 +00009506#ifdef FEAT_MBYTE
9507 arg != NULL ? arg :
9508#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009509 eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009510 }
9511 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
9512 }
9513
9514 /* Might not return to the main loop when in an event handler. */
9515 update_topline_cursor();
9516
9517 /* Restore the previous typeahead. */
9518 restore_typeahead(&tabuf);
9519
9520 --ex_normal_busy;
9521 msg_scroll = save_msg_scroll;
9522 restart_edit = save_restart_edit;
9523 p_im = save_insertmode;
9524 finish_op = save_finish_op;
Bram Moolenaar38084112008-07-26 14:05:07 +00009525 opcount = save_opcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009526 msg_didout |= save_msg_didout; /* don't reset msg_didout now */
9527
9528 /* Restore the state (needed when called from a function executed for
9529 * 'indentexpr'). */
9530 State = save_State;
9531#ifdef FEAT_MBYTE
9532 vim_free(arg);
9533#endif
9534}
9535
9536/*
Bram Moolenaar64969662005-12-14 21:59:55 +00009537 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009538 */
9539 static void
9540ex_startinsert(eap)
9541 exarg_T *eap;
9542{
Bram Moolenaarfd371682005-01-14 21:42:54 +00009543 if (eap->forceit)
9544 {
9545 coladvance((colnr_T)MAXCOL);
9546 curwin->w_curswant = MAXCOL;
9547 curwin->w_set_curswant = FALSE;
9548 }
9549
Bram Moolenaara40c5002005-01-09 21:16:21 +00009550 /* Ignore the command when already in Insert mode. Inserting an
9551 * expression register that invokes a function can do this. */
9552 if (State & INSERT)
9553 return;
9554
Bram Moolenaar64969662005-12-14 21:59:55 +00009555 if (eap->cmdidx == CMD_startinsert)
9556 restart_edit = 'a';
9557 else if (eap->cmdidx == CMD_startreplace)
9558 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00009559 else
Bram Moolenaar64969662005-12-14 21:59:55 +00009560 restart_edit = 'V';
9561
9562 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009563 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009564 if (eap->cmdidx == CMD_startinsert)
9565 restart_edit = 'i';
Bram Moolenaar071d4272004-06-13 20:20:40 +00009566 curwin->w_curswant = 0; /* avoid MAXCOL */
9567 }
9568}
9569
9570/*
9571 * ":stopinsert"
9572 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009573 static void
9574ex_stopinsert(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009575 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009576{
9577 restart_edit = 0;
9578 stop_insert_mode = TRUE;
9579}
9580#endif
9581
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009582#if defined(FEAT_EX_EXTRA) || defined(FEAT_MENU) || defined(PROTO)
9583/*
9584 * Execute normal mode command "cmd".
9585 * "remap" can be REMAP_NONE or REMAP_YES.
9586 */
9587 void
9588exec_normal_cmd(cmd, remap, silent)
9589 char_u *cmd;
9590 int remap;
9591 int silent;
9592{
9593 oparg_T oa;
9594
9595 /*
9596 * Stuff the argument into the typeahead buffer.
9597 * Execute normal_cmd() until there is no typeahead left.
9598 */
9599 clear_oparg(&oa);
9600 finish_op = FALSE;
9601 ins_typebuf(cmd, remap, 0, TRUE, silent);
9602 while ((!stuff_empty() || (!typebuf_typed() && typebuf.tb_len > 0))
9603 && !got_int)
9604 {
9605 update_topline_cursor();
Bram Moolenaar48ac02c2011-01-17 19:50:06 +01009606 normal_cmd(&oa, TRUE); /* execute a Normal mode cmd */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009607 }
9608}
9609#endif
9610
Bram Moolenaar071d4272004-06-13 20:20:40 +00009611#ifdef FEAT_FIND_ID
9612 static void
9613ex_checkpath(eap)
9614 exarg_T *eap;
9615{
9616 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
9617 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
9618 (linenr_T)1, (linenr_T)MAXLNUM);
9619}
9620
9621#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
9622/*
9623 * ":psearch"
9624 */
9625 static void
9626ex_psearch(eap)
9627 exarg_T *eap;
9628{
9629 g_do_tagpreview = p_pvh;
9630 ex_findpat(eap);
9631 g_do_tagpreview = 0;
9632}
9633#endif
9634
9635 static void
9636ex_findpat(eap)
9637 exarg_T *eap;
9638{
9639 int whole = TRUE;
9640 long n;
9641 char_u *p;
9642 int action;
9643
9644 switch (cmdnames[eap->cmdidx].cmd_name[2])
9645 {
9646 case 'e': /* ":psearch", ":isearch" and ":dsearch" */
9647 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
9648 action = ACTION_GOTO;
9649 else
9650 action = ACTION_SHOW;
9651 break;
9652 case 'i': /* ":ilist" and ":dlist" */
9653 action = ACTION_SHOW_ALL;
9654 break;
9655 case 'u': /* ":ijump" and ":djump" */
9656 action = ACTION_GOTO;
9657 break;
9658 default: /* ":isplit" and ":dsplit" */
9659 action = ACTION_SPLIT;
9660 break;
9661 }
9662
9663 n = 1;
9664 if (vim_isdigit(*eap->arg)) /* get count */
9665 {
9666 n = getdigits(&eap->arg);
9667 eap->arg = skipwhite(eap->arg);
9668 }
9669 if (*eap->arg == '/') /* Match regexp, not just whole words */
9670 {
9671 whole = FALSE;
9672 ++eap->arg;
9673 p = skip_regexp(eap->arg, '/', p_magic, NULL);
9674 if (*p)
9675 {
9676 *p++ = NUL;
9677 p = skipwhite(p);
9678
9679 /* Check for trailing illegal characters */
9680 if (!ends_excmd(*p))
9681 eap->errmsg = e_trailing;
9682 else
9683 eap->nextcmd = check_nextcmd(p);
9684 }
9685 }
9686 if (!eap->skip)
9687 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
9688 whole, !eap->forceit,
9689 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
9690 n, action, eap->line1, eap->line2);
9691}
9692#endif
9693
9694#ifdef FEAT_WINDOWS
9695
9696# ifdef FEAT_QUICKFIX
9697/*
9698 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
9699 */
9700 static void
9701ex_ptag(eap)
9702 exarg_T *eap;
9703{
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01009704 g_do_tagpreview = p_pvh; /* will be reset to 0 in ex_tag_cmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009705 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9706}
9707
9708/*
9709 * ":pedit"
9710 */
9711 static void
9712ex_pedit(eap)
9713 exarg_T *eap;
9714{
9715 win_T *curwin_save = curwin;
9716
9717 g_do_tagpreview = p_pvh;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00009718 prepare_tagpreview(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009719 keep_help_flag = curwin_save->w_buffer->b_help;
9720 do_exedit(eap, NULL);
9721 keep_help_flag = FALSE;
9722 if (curwin != curwin_save && win_valid(curwin_save))
9723 {
9724 /* Return cursor to where we were */
9725 validate_cursor();
9726 redraw_later(VALID);
9727 win_enter(curwin_save, TRUE);
9728 }
9729 g_do_tagpreview = 0;
9730}
9731# endif
9732
9733/*
9734 * ":stag", ":stselect" and ":stjump".
9735 */
9736 static void
9737ex_stag(eap)
9738 exarg_T *eap;
9739{
9740 postponed_split = -1;
9741 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009742 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009743 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9744 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009745 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009746}
9747#endif
9748
9749/*
9750 * ":tag", ":tselect", ":tjump", ":tnext", etc.
9751 */
9752 static void
9753ex_tag(eap)
9754 exarg_T *eap;
9755{
9756 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
9757}
9758
9759 static void
9760ex_tag_cmd(eap, name)
9761 exarg_T *eap;
9762 char_u *name;
9763{
9764 int cmd;
9765
9766 switch (name[1])
9767 {
9768 case 'j': cmd = DT_JUMP; /* ":tjump" */
9769 break;
9770 case 's': cmd = DT_SELECT; /* ":tselect" */
9771 break;
9772 case 'p': cmd = DT_PREV; /* ":tprevious" */
9773 break;
9774 case 'N': cmd = DT_PREV; /* ":tNext" */
9775 break;
9776 case 'n': cmd = DT_NEXT; /* ":tnext" */
9777 break;
9778 case 'o': cmd = DT_POP; /* ":pop" */
9779 break;
9780 case 'f': /* ":tfirst" */
9781 case 'r': cmd = DT_FIRST; /* ":trewind" */
9782 break;
9783 case 'l': cmd = DT_LAST; /* ":tlast" */
9784 break;
9785 default: /* ":tag" */
9786#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +00009787 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009788 {
9789 do_cstag(eap);
9790 return;
9791 }
9792#endif
9793 cmd = DT_TAG;
9794 break;
9795 }
9796
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00009797 if (name[0] == 'l')
9798 {
9799#ifndef FEAT_QUICKFIX
9800 ex_ni(eap);
9801 return;
9802#else
9803 cmd = DT_LTAG;
9804#endif
9805 }
9806
Bram Moolenaar071d4272004-06-13 20:20:40 +00009807 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
9808 eap->forceit, TRUE);
9809}
9810
9811/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009812 * Check "str" for starting with a special cmdline variable.
9813 * If found return one of the SPEC_ values and set "*usedlen" to the length of
9814 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
9815 */
9816 int
9817find_cmdline_var(src, usedlen)
9818 char_u *src;
9819 int *usedlen;
9820{
9821 int len;
9822 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +00009823 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009824 "%",
9825#define SPEC_PERC 0
9826 "#",
9827#define SPEC_HASH 1
9828 "<cword>", /* cursor word */
9829#define SPEC_CWORD 2
9830 "<cWORD>", /* cursor WORD */
9831#define SPEC_CCWORD 3
9832 "<cfile>", /* cursor path name */
9833#define SPEC_CFILE 4
9834 "<sfile>", /* ":so" file name */
9835#define SPEC_SFILE 5
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009836 "<slnum>", /* ":so" file line number */
9837#define SPEC_SLNUM 6
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009838#ifdef FEAT_AUTOCMD
9839 "<afile>", /* autocommand file name */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009840# define SPEC_AFILE 7
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009841 "<abuf>", /* autocommand buffer number */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009842# define SPEC_ABUF 8
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009843 "<amatch>", /* autocommand match name */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009844# define SPEC_AMATCH 9
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009845#endif
9846#ifdef FEAT_CLIENTSERVER
9847 "<client>"
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009848# ifdef FEAT_AUTOCMD
9849# define SPEC_CLIENT 10
9850# else
9851# define SPEC_CLIENT 7
9852# endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009853#endif
9854 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009855
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009856 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009857 {
9858 len = (int)STRLEN(spec_str[i]);
9859 if (STRNCMP(src, spec_str[i], len) == 0)
9860 {
9861 *usedlen = len;
9862 return i;
9863 }
9864 }
9865 return -1;
9866}
9867
9868/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009869 * Evaluate cmdline variables.
9870 *
9871 * change '%' to curbuf->b_ffname
9872 * '#' to curwin->w_altfile
9873 * '<cword>' to word under the cursor
9874 * '<cWORD>' to WORD under the cursor
9875 * '<cfile>' to path name under the cursor
9876 * '<sfile>' to sourced file name
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009877 * '<slnum>' to sourced file line number
Bram Moolenaar071d4272004-06-13 20:20:40 +00009878 * '<afile>' to file name for autocommand
9879 * '<abuf>' to buffer number for autocommand
9880 * '<amatch>' to matching name for autocommand
9881 *
9882 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
9883 * "" for error without a message) and NULL is returned.
9884 * Returns an allocated string if a valid match was found.
9885 * Returns NULL if no match was found. "usedlen" then still contains the
9886 * number of characters to skip.
9887 */
9888 char_u *
Bram Moolenaar63b92542007-03-27 14:57:09 +00009889eval_vars(src, srcstart, usedlen, lnump, errormsg, escaped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009890 char_u *src; /* pointer into commandline */
Bram Moolenaar63b92542007-03-27 14:57:09 +00009891 char_u *srcstart; /* beginning of valid memory for src */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009892 int *usedlen; /* characters after src that are used */
9893 linenr_T *lnump; /* line number for :e command, or NULL */
9894 char_u **errormsg; /* pointer to error message */
Bram Moolenaar63b92542007-03-27 14:57:09 +00009895 int *escaped; /* return value has escaped white space (can
9896 * be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009897{
9898 int i;
9899 char_u *s;
9900 char_u *result;
9901 char_u *resultbuf = NULL;
9902 int resultlen;
9903 buf_T *buf;
9904 int valid = VALID_HEAD + VALID_PATH; /* assume valid result */
9905 int spec_idx;
9906#ifdef FEAT_MODIFY_FNAME
9907 int skip_mod = FALSE;
9908#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009909 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009910
9911 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009912 if (escaped != NULL)
9913 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009914
9915 /*
9916 * Check if there is something to do.
9917 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009918 spec_idx = find_cmdline_var(src, usedlen);
9919 if (spec_idx < 0) /* no match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009920 {
9921 *usedlen = 1;
9922 return NULL;
9923 }
9924
9925 /*
9926 * Skip when preceded with a backslash "\%" and "\#".
9927 * Note: In "\\%" the % is also not recognized!
9928 */
9929 if (src > srcstart && src[-1] == '\\')
9930 {
9931 *usedlen = 0;
Bram Moolenaara7241f52008-06-24 20:39:31 +00009932 STRMOVE(src - 1, src); /* remove backslash */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009933 return NULL;
9934 }
9935
9936 /*
9937 * word or WORD under cursor
9938 */
9939 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD)
9940 {
9941 resultlen = find_ident_under_cursor(&result, spec_idx == SPEC_CWORD ?
9942 (FIND_IDENT|FIND_STRING) : FIND_STRING);
9943 if (resultlen == 0)
9944 {
9945 *errormsg = (char_u *)"";
9946 return NULL;
9947 }
9948 }
9949
9950 /*
9951 * '#': Alternate file name
9952 * '%': Current file name
9953 * File name under the cursor
9954 * File name for autocommand
9955 * and following modifiers
9956 */
9957 else
9958 {
9959 switch (spec_idx)
9960 {
9961 case SPEC_PERC: /* '%': current file */
9962 if (curbuf->b_fname == NULL)
9963 {
9964 result = (char_u *)"";
9965 valid = 0; /* Must have ":p:h" to be valid */
9966 }
9967 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009968 result = curbuf->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009969 break;
9970
9971 case SPEC_HASH: /* '#' or "#99": alternate file */
9972 if (src[1] == '#') /* "##": the argument list */
9973 {
9974 result = arg_all();
9975 resultbuf = result;
9976 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009977 if (escaped != NULL)
9978 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009979#ifdef FEAT_MODIFY_FNAME
9980 skip_mod = TRUE;
9981#endif
9982 break;
9983 }
9984 s = src + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00009985 if (*s == '<') /* "#<99" uses v:oldfiles */
9986 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009987 i = (int)getdigits(&s);
9988 *usedlen = (int)(s - src); /* length of what we expand */
9989
Bram Moolenaard812df62008-11-09 12:46:09 +00009990 if (src[1] == '<')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009991 {
Bram Moolenaard812df62008-11-09 12:46:09 +00009992 if (*usedlen < 2)
9993 {
9994 /* Should we give an error message for #<text? */
9995 *usedlen = 1;
9996 return NULL;
9997 }
9998#ifdef FEAT_EVAL
9999 result = list_find_str(get_vim_var_list(VV_OLDFILES),
10000 (long)i);
10001 if (result == NULL)
10002 {
10003 *errormsg = (char_u *)"";
10004 return NULL;
10005 }
10006#else
10007 *errormsg = (char_u *)_("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +000010008 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +000010009#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010010 }
10011 else
Bram Moolenaard812df62008-11-09 12:46:09 +000010012 {
10013 buf = buflist_findnr(i);
10014 if (buf == NULL)
10015 {
10016 *errormsg = (char_u *)_("E194: No alternate file name to substitute for '#'");
10017 return NULL;
10018 }
10019 if (lnump != NULL)
10020 *lnump = ECMD_LAST;
10021 if (buf->b_fname == NULL)
10022 {
10023 result = (char_u *)"";
10024 valid = 0; /* Must have ":p:h" to be valid */
10025 }
10026 else
10027 result = buf->b_fname;
10028 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010029 break;
10030
10031#ifdef FEAT_SEARCHPATH
10032 case SPEC_CFILE: /* file name under cursor */
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010033 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010034 if (result == NULL)
10035 {
10036 *errormsg = (char_u *)"";
10037 return NULL;
10038 }
10039 resultbuf = result; /* remember allocated string */
10040 break;
10041#endif
10042
10043#ifdef FEAT_AUTOCMD
10044 case SPEC_AFILE: /* file name for autocommand */
10045 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +000010046 if (result != NULL && !autocmd_fname_full)
10047 {
10048 /* Still need to turn the fname into a full path. It is
10049 * postponed to avoid a delay when <afile> is not used. */
10050 autocmd_fname_full = TRUE;
10051 result = FullName_save(autocmd_fname, FALSE);
10052 vim_free(autocmd_fname);
10053 autocmd_fname = result;
10054 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010055 if (result == NULL)
10056 {
10057 *errormsg = (char_u *)_("E495: no autocommand file name to substitute for \"<afile>\"");
10058 return NULL;
10059 }
Bram Moolenaara0174af2008-01-02 20:08:25 +000010060 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010061 break;
10062
10063 case SPEC_ABUF: /* buffer number for autocommand */
10064 if (autocmd_bufnr <= 0)
10065 {
10066 *errormsg = (char_u *)_("E496: no autocommand buffer number to substitute for \"<abuf>\"");
10067 return NULL;
10068 }
10069 sprintf((char *)strbuf, "%d", autocmd_bufnr);
10070 result = strbuf;
10071 break;
10072
10073 case SPEC_AMATCH: /* match name for autocommand */
10074 result = autocmd_match;
10075 if (result == NULL)
10076 {
10077 *errormsg = (char_u *)_("E497: no autocommand match name to substitute for \"<amatch>\"");
10078 return NULL;
10079 }
10080 break;
10081
10082#endif
10083 case SPEC_SFILE: /* file name for ":so" command */
10084 result = sourcing_name;
10085 if (result == NULL)
10086 {
10087 *errormsg = (char_u *)_("E498: no :source file name to substitute for \"<sfile>\"");
10088 return NULL;
10089 }
10090 break;
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010091 case SPEC_SLNUM: /* line in file for ":so" command */
10092 if (sourcing_name == NULL || sourcing_lnum == 0)
10093 {
10094 *errormsg = (char_u *)_("E842: no line number to use for \"<slnum>\"");
10095 return NULL;
10096 }
10097 sprintf((char *)strbuf, "%ld", (long)sourcing_lnum);
10098 result = strbuf;
10099 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010100#if defined(FEAT_CLIENTSERVER)
10101 case SPEC_CLIENT: /* Source of last submitted input */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010102 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
10103 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010104 result = strbuf;
10105 break;
10106#endif
10107 }
10108
10109 resultlen = (int)STRLEN(result); /* length of new string */
10110 if (src[*usedlen] == '<') /* remove the file name extension */
10111 {
10112 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010113 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010114 resultlen = (int)(s - result);
10115 }
10116#ifdef FEAT_MODIFY_FNAME
10117 else if (!skip_mod)
10118 {
10119 valid |= modify_fname(src, usedlen, &result, &resultbuf,
10120 &resultlen);
10121 if (result == NULL)
10122 {
10123 *errormsg = (char_u *)"";
10124 return NULL;
10125 }
10126 }
10127#endif
10128 }
10129
10130 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
10131 {
10132 if (valid != VALID_HEAD + VALID_PATH)
10133 /* xgettext:no-c-format */
10134 *errormsg = (char_u *)_("E499: Empty file name for '%' or '#', only works with \":p:h\"");
10135 else
10136 *errormsg = (char_u *)_("E500: Evaluates to an empty string");
10137 result = NULL;
10138 }
10139 else
10140 result = vim_strnsave(result, resultlen);
10141 vim_free(resultbuf);
10142 return result;
10143}
10144
10145/*
10146 * Concatenate all files in the argument list, separated by spaces, and return
10147 * it in one allocated string.
10148 * Spaces and backslashes in the file names are escaped with a backslash.
10149 * Returns NULL when out of memory.
10150 */
10151 static char_u *
10152arg_all()
10153{
10154 int len;
10155 int idx;
10156 char_u *retval = NULL;
10157 char_u *p;
10158
10159 /*
10160 * Do this loop two times:
10161 * first time: compute the total length
10162 * second time: concatenate the names
10163 */
10164 for (;;)
10165 {
10166 len = 0;
10167 for (idx = 0; idx < ARGCOUNT; ++idx)
10168 {
10169 p = alist_name(&ARGLIST[idx]);
10170 if (p != NULL)
10171 {
10172 if (len > 0)
10173 {
10174 /* insert a space in between names */
10175 if (retval != NULL)
10176 retval[len] = ' ';
10177 ++len;
10178 }
10179 for ( ; *p != NUL; ++p)
10180 {
10181 if (*p == ' ' || *p == '\\')
10182 {
10183 /* insert a backslash */
10184 if (retval != NULL)
10185 retval[len] = '\\';
10186 ++len;
10187 }
10188 if (retval != NULL)
10189 retval[len] = *p;
10190 ++len;
10191 }
10192 }
10193 }
10194
10195 /* second time: break here */
10196 if (retval != NULL)
10197 {
10198 retval[len] = NUL;
10199 break;
10200 }
10201
10202 /* allocate memory */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000010203 retval = alloc((unsigned)len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010204 if (retval == NULL)
10205 break;
10206 }
10207
10208 return retval;
10209}
10210
10211#if defined(FEAT_AUTOCMD) || defined(PROTO)
10212/*
10213 * Expand the <sfile> string in "arg".
10214 *
10215 * Returns an allocated string, or NULL for any error.
10216 */
10217 char_u *
10218expand_sfile(arg)
10219 char_u *arg;
10220{
10221 char_u *errormsg;
10222 int len;
10223 char_u *result;
10224 char_u *newres;
10225 char_u *repl;
10226 int srclen;
10227 char_u *p;
10228
10229 result = vim_strsave(arg);
10230 if (result == NULL)
10231 return NULL;
10232
10233 for (p = result; *p; )
10234 {
10235 if (STRNCMP(p, "<sfile>", 7) != 0)
10236 ++p;
10237 else
10238 {
10239 /* replace "<sfile>" with the sourced file name, and do ":" stuff */
Bram Moolenaar63b92542007-03-27 14:57:09 +000010240 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010241 if (errormsg != NULL)
10242 {
10243 if (*errormsg)
10244 emsg(errormsg);
10245 vim_free(result);
10246 return NULL;
10247 }
10248 if (repl == NULL) /* no match (cannot happen) */
10249 {
10250 p += srclen;
10251 continue;
10252 }
10253 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
10254 newres = alloc(len);
10255 if (newres == NULL)
10256 {
10257 vim_free(repl);
10258 vim_free(result);
10259 return NULL;
10260 }
10261 mch_memmove(newres, result, (size_t)(p - result));
10262 STRCPY(newres + (p - result), repl);
10263 len = (int)STRLEN(newres);
10264 STRCAT(newres, p + srclen);
10265 vim_free(repl);
10266 vim_free(result);
10267 result = newres;
10268 p = newres + len; /* continue after the match */
10269 }
10270 }
10271
10272 return result;
10273}
10274#endif
10275
10276#ifdef FEAT_SESSION
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010277static int ses_winsizes __ARGS((FILE *fd, int restore_size,
10278 win_T *tab_firstwin));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010279static int ses_win_rec __ARGS((FILE *fd, frame_T *fr));
10280static frame_T *ses_skipframe __ARGS((frame_T *fr));
10281static int ses_do_frame __ARGS((frame_T *fr));
10282static int ses_do_win __ARGS((win_T *wp));
10283static int ses_arglist __ARGS((FILE *fd, char *cmd, garray_T *gap, int fullname, unsigned *flagp));
10284static int ses_put_fname __ARGS((FILE *fd, char_u *name, unsigned *flagp));
10285static int ses_fname __ARGS((FILE *fd, buf_T *buf, unsigned *flagp));
10286
10287/*
10288 * Write openfile commands for the current buffers to an .exrc file.
10289 * Return FAIL on error, OK otherwise.
10290 */
10291 static int
10292makeopens(fd, dirnow)
10293 FILE *fd;
10294 char_u *dirnow; /* Current directory name */
10295{
10296 buf_T *buf;
10297 int only_save_windows = TRUE;
10298 int nr;
10299 int cnr = 1;
10300 int restore_size = TRUE;
10301 win_T *wp;
10302 char_u *sname;
10303 win_T *edited_win = NULL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010304 int tabnr;
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020010305 int restore_stal = FALSE;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010306 win_T *tab_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010307 frame_T *tab_topframe;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010308 int cur_arg_idx = 0;
Bram Moolenaar383c6f52008-01-04 15:01:07 +000010309 int next_arg_idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010310
10311 if (ssop_flags & SSOP_BUFFERS)
10312 only_save_windows = FALSE; /* Save ALL buffers */
10313
10314 /*
10315 * Begin by setting the this_session variable, and then other
10316 * sessionable variables.
10317 */
10318#ifdef FEAT_EVAL
10319 if (put_line(fd, "let v:this_session=expand(\"<sfile>:p\")") == FAIL)
10320 return FAIL;
10321 if (ssop_flags & SSOP_GLOBALS)
10322 if (store_session_globals(fd) == FAIL)
10323 return FAIL;
10324#endif
10325
10326 /*
10327 * Close all windows but one.
10328 */
10329 if (put_line(fd, "silent only") == FAIL)
10330 return FAIL;
10331
10332 /*
10333 * Now a :cd command to the session directory or the current directory
10334 */
10335 if (ssop_flags & SSOP_SESDIR)
10336 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010337 if (put_line(fd, "exe \"cd \" . escape(expand(\"<sfile>:p:h\"), ' ')")
10338 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010339 return FAIL;
10340 }
10341 else if (ssop_flags & SSOP_CURDIR)
10342 {
10343 sname = home_replace_save(NULL, globaldir != NULL ? globaldir : dirnow);
10344 if (sname == NULL
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010345 || fputs("cd ", fd) < 0
10346 || ses_put_fname(fd, sname, &ssop_flags) == FAIL
10347 || put_eol(fd) == FAIL)
10348 {
10349 vim_free(sname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010350 return FAIL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010351 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010352 vim_free(sname);
10353 }
10354
10355 /*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010356 * If there is an empty, unnamed buffer we will wipe it out later.
10357 * Remember the buffer number.
10358 */
10359 if (put_line(fd, "if expand('%') == '' && !&modified && line('$') <= 1 && getline(1) == ''") == FAIL)
10360 return FAIL;
10361 if (put_line(fd, " let s:wipebuf = bufnr('%')") == FAIL)
10362 return FAIL;
10363 if (put_line(fd, "endif") == FAIL)
10364 return FAIL;
10365
10366 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010367 * Now save the current files, current buffer first.
10368 */
10369 if (put_line(fd, "set shortmess=aoO") == FAIL)
10370 return FAIL;
10371
10372 /* Now put the other buffers into the buffer list */
10373 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
10374 {
10375 if (!(only_save_windows && buf->b_nwindows == 0)
10376 && !(buf->b_help && !(ssop_flags & SSOP_HELP))
10377 && buf->b_fname != NULL
10378 && buf->b_p_bl)
10379 {
10380 if (fprintf(fd, "badd +%ld ", buf->b_wininfo == NULL ? 1L
10381 : buf->b_wininfo->wi_fpos.lnum) < 0
10382 || ses_fname(fd, buf, &ssop_flags) == FAIL)
10383 return FAIL;
10384 }
10385 }
10386
10387 /* the global argument list */
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010010388 if (ses_arglist(fd, "argglobal", &global_alist.al_ga,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010389 !(ssop_flags & SSOP_CURDIR), &ssop_flags) == FAIL)
10390 return FAIL;
10391
10392 if (ssop_flags & SSOP_RESIZE)
10393 {
10394 /* Note: after the restore we still check it worked!*/
10395 if (fprintf(fd, "set lines=%ld columns=%ld" , Rows, Columns) < 0
10396 || put_eol(fd) == FAIL)
10397 return FAIL;
10398 }
10399
10400#ifdef FEAT_GUI
10401 if (gui.in_use && (ssop_flags & SSOP_WINPOS))
10402 {
10403 int x, y;
10404
10405 if (gui_mch_get_winpos(&x, &y) == OK)
10406 {
10407 /* Note: after the restore we still check it worked!*/
10408 if (fprintf(fd, "winpos %d %d", x, y) < 0 || put_eol(fd) == FAIL)
10409 return FAIL;
10410 }
10411 }
10412#endif
10413
10414 /*
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020010415 * When there are two or more tabpages and 'showtabline' is 1 the tabline
10416 * will be displayed when creating the next tab. That resizes the windows
10417 * in the first tab, which may cause problems. Set 'showtabline' to 2
10418 * temporarily to avoid that.
10419 */
10420 if (p_stal == 1 && first_tabpage->tp_next != NULL)
10421 {
10422 if (put_line(fd, "set stal=2") == FAIL)
10423 return FAIL;
10424 restore_stal = TRUE;
10425 }
10426
10427 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000010428 * May repeat putting Windows for each tab, when "tabpages" is in
10429 * 'sessionoptions'.
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010430 * Don't use goto_tabpage(), it may change directory and trigger
10431 * autocommands.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010432 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010433 tab_firstwin = firstwin; /* first window in tab page "tabnr" */
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010434 tab_topframe = topframe;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010435 for (tabnr = 1; ; ++tabnr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010436 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010437 int need_tabnew = FALSE;
10438
Bram Moolenaar18144c82006-04-12 21:52:12 +000010439 if ((ssop_flags & SSOP_TABPAGES))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010440 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010441 tabpage_T *tp = find_tabpage(tabnr);
10442
10443 if (tp == NULL)
10444 break; /* done all tab pages */
10445 if (tp == curtab)
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010446 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010447 tab_firstwin = firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010448 tab_topframe = topframe;
10449 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010450 else
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010451 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010452 tab_firstwin = tp->tp_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010453 tab_topframe = tp->tp_topframe;
10454 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010455 if (tabnr > 1)
10456 need_tabnew = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010457 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010458
Bram Moolenaar18144c82006-04-12 21:52:12 +000010459 /*
10460 * Before creating the window layout, try loading one file. If this
10461 * is aborted we don't end up with a number of useless windows.
10462 * This may have side effects! (e.g., compressed or network file).
10463 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010464 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010465 {
10466 if (ses_do_win(wp)
10467 && wp->w_buffer->b_ffname != NULL
10468 && !wp->w_buffer->b_help
10469#ifdef FEAT_QUICKFIX
10470 && !bt_nofile(wp->w_buffer)
10471#endif
10472 )
10473 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010474 if (fputs(need_tabnew ? "tabedit " : "edit ", fd) < 0
Bram Moolenaar18144c82006-04-12 21:52:12 +000010475 || ses_fname(fd, wp->w_buffer, &ssop_flags) == FAIL)
10476 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010477 need_tabnew = FALSE;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010478 if (!wp->w_arg_idx_invalid)
10479 edited_win = wp;
10480 break;
10481 }
10482 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010483
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010484 /* If no file got edited create an empty tab page. */
10485 if (need_tabnew && put_line(fd, "tabnew") == FAIL)
10486 return FAIL;
10487
Bram Moolenaar18144c82006-04-12 21:52:12 +000010488 /*
10489 * Save current window layout.
10490 */
10491 if (put_line(fd, "set splitbelow splitright") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010492 return FAIL;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010493 if (ses_win_rec(fd, tab_topframe) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010494 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010495 if (!p_sb && put_line(fd, "set nosplitbelow") == FAIL)
10496 return FAIL;
10497 if (!p_spr && put_line(fd, "set nosplitright") == FAIL)
10498 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010499
Bram Moolenaar18144c82006-04-12 21:52:12 +000010500 /*
10501 * Check if window sizes can be restored (no windows omitted).
10502 * Remember the window number of the current window after restoring.
10503 */
10504 nr = 0;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010505 for (wp = tab_firstwin; wp != NULL; wp = W_NEXT(wp))
Bram Moolenaar18144c82006-04-12 21:52:12 +000010506 {
10507 if (ses_do_win(wp))
10508 ++nr;
10509 else
10510 restore_size = FALSE;
10511 if (curwin == wp)
10512 cnr = nr;
10513 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010514
Bram Moolenaar18144c82006-04-12 21:52:12 +000010515 /* Go to the first window. */
10516 if (put_line(fd, "wincmd t") == FAIL)
10517 return FAIL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010518
Bram Moolenaar18144c82006-04-12 21:52:12 +000010519 /*
10520 * If more than one window, see if sizes can be restored.
10521 * First set 'winheight' and 'winwidth' to 1 to avoid the windows being
10522 * resized when moving between windows.
10523 * Do this before restoring the view, so that the topline and the
10524 * cursor can be set. This is done again below.
10525 */
10526 if (put_line(fd, "set winheight=1 winwidth=1") == FAIL)
10527 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010528 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010529 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010530
Bram Moolenaar18144c82006-04-12 21:52:12 +000010531 /*
10532 * Restore the view of the window (options, file, cursor, etc.).
10533 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010534 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010535 {
10536 if (!ses_do_win(wp))
10537 continue;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010538 if (put_view(fd, wp, wp != edited_win, &ssop_flags,
10539 cur_arg_idx) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010540 return FAIL;
10541 if (nr > 1 && put_line(fd, "wincmd w") == FAIL)
10542 return FAIL;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010543 next_arg_idx = wp->w_arg_idx;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010544 }
10545
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010546 /* The argument index in the first tab page is zero, need to set it in
10547 * each window. For further tab pages it's the window where we do
10548 * "tabedit". */
10549 cur_arg_idx = next_arg_idx;
10550
Bram Moolenaar18144c82006-04-12 21:52:12 +000010551 /*
10552 * Restore cursor to the current window if it's not the first one.
10553 */
10554 if (cnr > 1 && (fprintf(fd, "%dwincmd w", cnr) < 0
10555 || put_eol(fd) == FAIL))
10556 return FAIL;
10557
10558 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000010559 * Restore window sizes again after jumping around in windows, because
10560 * the current window has a minimum size while others may not.
10561 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010562 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010563 return FAIL;
10564
Bram Moolenaar18144c82006-04-12 21:52:12 +000010565 /* Don't continue in another tab page when doing only the current one
10566 * or when at the last tab page. */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010567 if (!(ssop_flags & SSOP_TABPAGES))
Bram Moolenaar18144c82006-04-12 21:52:12 +000010568 break;
10569 }
10570
10571 if (ssop_flags & SSOP_TABPAGES)
10572 {
Bram Moolenaar18144c82006-04-12 21:52:12 +000010573 if (fprintf(fd, "tabnext %d", tabpage_index(curtab)) < 0
10574 || put_eol(fd) == FAIL)
10575 return FAIL;
10576 }
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020010577 if (restore_stal && put_line(fd, "set stal=1") == FAIL)
10578 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010579
Bram Moolenaar9c102382006-05-03 21:26:49 +000010580 /*
10581 * Wipe out an empty unnamed buffer we started in.
10582 */
10583 if (put_line(fd, "if exists('s:wipebuf')") == FAIL)
10584 return FAIL;
Bram Moolenaarf3a67882006-05-05 21:09:41 +000010585 if (put_line(fd, " silent exe 'bwipe ' . s:wipebuf") == FAIL)
Bram Moolenaar9c102382006-05-03 21:26:49 +000010586 return FAIL;
10587 if (put_line(fd, "endif") == FAIL)
10588 return FAIL;
10589 if (put_line(fd, "unlet! s:wipebuf") == FAIL)
10590 return FAIL;
10591
10592 /* Re-apply 'winheight', 'winwidth' and 'shortmess'. */
10593 if (fprintf(fd, "set winheight=%ld winwidth=%ld shortmess=%s",
10594 p_wh, p_wiw, p_shm) < 0 || put_eol(fd) == FAIL)
10595 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010596
10597 /*
10598 * Lastly, execute the x.vim file if it exists.
10599 */
10600 if (put_line(fd, "let s:sx = expand(\"<sfile>:p:r\").\"x.vim\"") == FAIL
10601 || put_line(fd, "if file_readable(s:sx)") == FAIL
Bram Moolenaar42ba1262008-12-09 10:18:03 +000010602 || put_line(fd, " exe \"source \" . fnameescape(s:sx)") == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000010603 || put_line(fd, "endif") == FAIL)
10604 return FAIL;
10605
10606 return OK;
10607}
10608
10609 static int
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010610ses_winsizes(fd, restore_size, tab_firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010611 FILE *fd;
10612 int restore_size;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010613 win_T *tab_firstwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010614{
10615 int n = 0;
10616 win_T *wp;
10617
10618 if (restore_size && (ssop_flags & SSOP_WINSIZE))
10619 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010620 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010621 {
10622 if (!ses_do_win(wp))
10623 continue;
10624 ++n;
10625
10626 /* restore height when not full height */
10627 if (wp->w_height + wp->w_status_height < topframe->fr_height
10628 && (fprintf(fd,
10629 "exe '%dresize ' . ((&lines * %ld + %ld) / %ld)",
10630 n, (long)wp->w_height, Rows / 2, Rows) < 0
10631 || put_eol(fd) == FAIL))
10632 return FAIL;
10633
10634 /* restore width when not full width */
10635 if (wp->w_width < Columns && (fprintf(fd,
10636 "exe 'vert %dresize ' . ((&columns * %ld + %ld) / %ld)",
10637 n, (long)wp->w_width, Columns / 2, Columns) < 0
10638 || put_eol(fd) == FAIL))
10639 return FAIL;
10640 }
10641 }
10642 else
10643 {
10644 /* Just equalise window sizes */
10645 if (put_line(fd, "wincmd =") == FAIL)
10646 return FAIL;
10647 }
10648 return OK;
10649}
10650
10651/*
10652 * Write commands to "fd" to recursively create windows for frame "fr",
10653 * horizontally and vertically split.
10654 * After the commands the last window in the frame is the current window.
10655 * Returns FAIL when writing the commands to "fd" fails.
10656 */
10657 static int
10658ses_win_rec(fd, fr)
10659 FILE *fd;
10660 frame_T *fr;
10661{
10662 frame_T *frc;
10663 int count = 0;
10664
10665 if (fr->fr_layout != FR_LEAF)
10666 {
10667 /* Find first frame that's not skipped and then create a window for
10668 * each following one (first frame is already there). */
10669 frc = ses_skipframe(fr->fr_child);
10670 if (frc != NULL)
10671 while ((frc = ses_skipframe(frc->fr_next)) != NULL)
10672 {
10673 /* Make window as big as possible so that we have lots of room
10674 * to split. */
10675 if (put_line(fd, "wincmd _ | wincmd |") == FAIL
10676 || put_line(fd, fr->fr_layout == FR_COL
10677 ? "split" : "vsplit") == FAIL)
10678 return FAIL;
10679 ++count;
10680 }
10681
10682 /* Go back to the first window. */
10683 if (count > 0 && (fprintf(fd, fr->fr_layout == FR_COL
10684 ? "%dwincmd k" : "%dwincmd h", count) < 0
10685 || put_eol(fd) == FAIL))
10686 return FAIL;
10687
10688 /* Recursively create frames/windows in each window of this column or
10689 * row. */
10690 frc = ses_skipframe(fr->fr_child);
10691 while (frc != NULL)
10692 {
10693 ses_win_rec(fd, frc);
10694 frc = ses_skipframe(frc->fr_next);
10695 /* Go to next window. */
10696 if (frc != NULL && put_line(fd, "wincmd w") == FAIL)
10697 return FAIL;
10698 }
10699 }
10700 return OK;
10701}
10702
10703/*
10704 * Skip frames that don't contain windows we want to save in the Session.
10705 * Returns NULL when there none.
10706 */
10707 static frame_T *
10708ses_skipframe(fr)
10709 frame_T *fr;
10710{
10711 frame_T *frc;
10712
10713 for (frc = fr; frc != NULL; frc = frc->fr_next)
10714 if (ses_do_frame(frc))
10715 break;
10716 return frc;
10717}
10718
10719/*
10720 * Return TRUE if frame "fr" has a window somewhere that we want to save in
10721 * the Session.
10722 */
10723 static int
10724ses_do_frame(fr)
10725 frame_T *fr;
10726{
10727 frame_T *frc;
10728
10729 if (fr->fr_layout == FR_LEAF)
10730 return ses_do_win(fr->fr_win);
10731 for (frc = fr->fr_child; frc != NULL; frc = frc->fr_next)
10732 if (ses_do_frame(frc))
10733 return TRUE;
10734 return FALSE;
10735}
10736
10737/*
10738 * Return non-zero if window "wp" is to be stored in the Session.
10739 */
10740 static int
10741ses_do_win(wp)
10742 win_T *wp;
10743{
10744 if (wp->w_buffer->b_fname == NULL
10745#ifdef FEAT_QUICKFIX
10746 /* When 'buftype' is "nofile" can't restore the window contents. */
10747 || bt_nofile(wp->w_buffer)
10748#endif
10749 )
10750 return (ssop_flags & SSOP_BLANK);
10751 if (wp->w_buffer->b_help)
10752 return (ssop_flags & SSOP_HELP);
10753 return TRUE;
10754}
10755
10756/*
10757 * Write commands to "fd" to restore the view of a window.
10758 * Caller must make sure 'scrolloff' is zero.
10759 */
10760 static int
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010761put_view(fd, wp, add_edit, flagp, current_arg_idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010762 FILE *fd;
10763 win_T *wp;
10764 int add_edit; /* add ":edit" command to view */
10765 unsigned *flagp; /* vop_flags or ssop_flags */
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010766 int current_arg_idx; /* current argument index of the window, use
10767 * -1 if unknown */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010768{
10769 win_T *save_curwin;
10770 int f;
10771 int do_cursor;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010772 int did_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010773
10774 /* Always restore cursor position for ":mksession". For ":mkview" only
10775 * when 'viewoptions' contains "cursor". */
10776 do_cursor = (flagp == &ssop_flags || *flagp & SSOP_CURSOR);
10777
10778 /*
10779 * Local argument list.
10780 */
10781 if (wp->w_alist == &global_alist)
10782 {
10783 if (put_line(fd, "argglobal") == FAIL)
10784 return FAIL;
10785 }
10786 else
10787 {
10788 if (ses_arglist(fd, "arglocal", &wp->w_alist->al_ga,
10789 flagp == &vop_flags
10790 || !(*flagp & SSOP_CURDIR)
10791 || wp->w_localdir != NULL, flagp) == FAIL)
10792 return FAIL;
10793 }
10794
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010795 /* Only when part of a session: restore the argument index. Some
10796 * arguments may have been deleted, check if the index is valid. */
Bram Moolenaar51f53df2010-06-26 05:25:54 +020010797 if (wp->w_arg_idx != current_arg_idx && wp->w_arg_idx < WARGCOUNT(wp)
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010798 && flagp == &ssop_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010799 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010800 if (fprintf(fd, "%ldargu", (long)wp->w_arg_idx + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010801 || put_eol(fd) == FAIL)
10802 return FAIL;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010803 did_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010804 }
10805
10806 /* Edit the file. Skip this when ":next" already did it. */
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010807 if (add_edit && (!did_next || wp->w_arg_idx_invalid))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010808 {
10809 /*
10810 * Load the file.
10811 */
10812 if (wp->w_buffer->b_ffname != NULL
10813#ifdef FEAT_QUICKFIX
10814 && !bt_nofile(wp->w_buffer)
10815#endif
10816 )
10817 {
10818 /*
10819 * Editing a file in this buffer: use ":edit file".
10820 * This may have side effects! (e.g., compressed or network file).
10821 */
10822 if (fputs("edit ", fd) < 0
10823 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
10824 return FAIL;
10825 }
10826 else
10827 {
10828 /* No file in this buffer, just make it empty. */
10829 if (put_line(fd, "enew") == FAIL)
10830 return FAIL;
10831#ifdef FEAT_QUICKFIX
10832 if (wp->w_buffer->b_ffname != NULL)
10833 {
10834 /* The buffer does have a name, but it's not a file name. */
10835 if (fputs("file ", fd) < 0
10836 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
10837 return FAIL;
10838 }
10839#endif
10840 do_cursor = FALSE;
10841 }
10842 }
10843
10844 /*
10845 * Local mappings and abbreviations.
10846 */
10847 if ((*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
10848 && makemap(fd, wp->w_buffer) == FAIL)
10849 return FAIL;
10850
10851 /*
10852 * Local options. Need to go to the window temporarily.
10853 * Store only local values when using ":mkview" and when ":mksession" is
10854 * used and 'sessionoptions' doesn't include "options".
10855 * Some folding options are always stored when "folds" is included,
10856 * otherwise the folds would not be restored correctly.
10857 */
10858 save_curwin = curwin;
10859 curwin = wp;
10860 curbuf = curwin->w_buffer;
10861 if (*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
10862 f = makeset(fd, OPT_LOCAL,
10863 flagp == &vop_flags || !(*flagp & SSOP_OPTIONS));
10864#ifdef FEAT_FOLDING
10865 else if (*flagp & SSOP_FOLDS)
10866 f = makefoldset(fd);
10867#endif
10868 else
10869 f = OK;
10870 curwin = save_curwin;
10871 curbuf = curwin->w_buffer;
10872 if (f == FAIL)
10873 return FAIL;
10874
10875#ifdef FEAT_FOLDING
10876 /*
10877 * Save Folds when 'buftype' is empty and for help files.
10878 */
10879 if ((*flagp & SSOP_FOLDS)
10880 && wp->w_buffer->b_ffname != NULL
Bram Moolenaarb1b715d2006-01-21 22:09:43 +000010881# ifdef FEAT_QUICKFIX
10882 && (*wp->w_buffer->b_p_bt == NUL || wp->w_buffer->b_help)
10883# endif
10884 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000010885 {
10886 if (put_folds(fd, wp) == FAIL)
10887 return FAIL;
10888 }
10889#endif
10890
10891 /*
10892 * Set the cursor after creating folds, since that moves the cursor.
10893 */
10894 if (do_cursor)
10895 {
10896
10897 /* Restore the cursor line in the file and relatively in the
10898 * window. Don't use "G", it changes the jumplist. */
10899 if (fprintf(fd, "let s:l = %ld - ((%ld * winheight(0) + %ld) / %ld)",
10900 (long)wp->w_cursor.lnum,
10901 (long)(wp->w_cursor.lnum - wp->w_topline),
10902 (long)wp->w_height / 2, (long)wp->w_height) < 0
10903 || put_eol(fd) == FAIL
10904 || put_line(fd, "if s:l < 1 | let s:l = 1 | endif") == FAIL
10905 || put_line(fd, "exe s:l") == FAIL
10906 || put_line(fd, "normal! zt") == FAIL
10907 || fprintf(fd, "%ld", (long)wp->w_cursor.lnum) < 0
10908 || put_eol(fd) == FAIL)
10909 return FAIL;
10910 /* Restore the cursor column and left offset when not wrapping. */
10911 if (wp->w_cursor.col == 0)
10912 {
10913 if (put_line(fd, "normal! 0") == FAIL)
10914 return FAIL;
10915 }
10916 else
10917 {
10918 if (!wp->w_p_wrap && wp->w_leftcol > 0 && wp->w_width > 0)
10919 {
10920 if (fprintf(fd,
10921 "let s:c = %ld - ((%ld * winwidth(0) + %ld) / %ld)",
Bram Moolenaar558ddad2013-02-20 19:26:29 +010010922 (long)wp->w_virtcol + 1,
10923 (long)(wp->w_virtcol - wp->w_leftcol),
Bram Moolenaar071d4272004-06-13 20:20:40 +000010924 (long)wp->w_width / 2, (long)wp->w_width) < 0
10925 || put_eol(fd) == FAIL
10926 || put_line(fd, "if s:c > 0") == FAIL
10927 || fprintf(fd,
Bram Moolenaar558ddad2013-02-20 19:26:29 +010010928 " exe 'normal! ' . s:c . '|zs' . %ld . '|'",
10929 (long)wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010930 || put_eol(fd) == FAIL
10931 || put_line(fd, "else") == FAIL
Bram Moolenaarfdf447b2013-02-26 17:21:29 +010010932 || fprintf(fd, " normal! 0%d|", wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010933 || put_eol(fd) == FAIL
10934 || put_line(fd, "endif") == FAIL)
10935 return FAIL;
10936 }
10937 else
10938 {
Bram Moolenaar558ddad2013-02-20 19:26:29 +010010939 if (fprintf(fd, "normal! 0%d|", wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010940 || put_eol(fd) == FAIL)
10941 return FAIL;
10942 }
10943 }
10944 }
10945
10946 /*
10947 * Local directory.
10948 */
10949 if (wp->w_localdir != NULL)
10950 {
10951 if (fputs("lcd ", fd) < 0
10952 || ses_put_fname(fd, wp->w_localdir, flagp) == FAIL
10953 || put_eol(fd) == FAIL)
10954 return FAIL;
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010955 did_lcd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010956 }
10957
10958 return OK;
10959}
10960
10961/*
10962 * Write an argument list to the session file.
10963 * Returns FAIL if writing fails.
10964 */
10965 static int
10966ses_arglist(fd, cmd, gap, fullname, flagp)
10967 FILE *fd;
10968 char *cmd;
10969 garray_T *gap;
10970 int fullname; /* TRUE: use full path name */
10971 unsigned *flagp;
10972{
10973 int i;
Bram Moolenaard9462e32011-04-11 21:35:11 +020010974 char_u *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010975 char_u *s;
10976
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010010977 if (fputs(cmd, fd) < 0 || put_eol(fd) == FAIL)
10978 return FAIL;
10979 if (put_line(fd, "silent! argdel *") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010980 return FAIL;
10981 for (i = 0; i < gap->ga_len; ++i)
10982 {
10983 /* NULL file names are skipped (only happens when out of memory). */
10984 s = alist_name(&((aentry_T *)gap->ga_data)[i]);
10985 if (s != NULL)
10986 {
10987 if (fullname)
10988 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020010989 buf = alloc(MAXPATHL);
10990 if (buf != NULL)
10991 {
10992 (void)vim_FullName(s, buf, MAXPATHL, FALSE);
10993 s = buf;
10994 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010995 }
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010010996 if (fputs("argadd ", fd) < 0
10997 || ses_put_fname(fd, s, flagp) == FAIL
10998 || put_eol(fd) == FAIL)
Bram Moolenaard9462e32011-04-11 21:35:11 +020010999 {
11000 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011001 return FAIL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011002 }
11003 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011004 }
11005 }
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011006 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011007}
11008
11009/*
11010 * Write a buffer name to the session file.
11011 * Also ends the line.
11012 * Returns FAIL if writing fails.
11013 */
11014 static int
11015ses_fname(fd, buf, flagp)
11016 FILE *fd;
11017 buf_T *buf;
11018 unsigned *flagp;
11019{
11020 char_u *name;
11021
11022 /* Use the short file name if the current directory is known at the time
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011023 * the session file will be sourced.
11024 * Don't do this for ":mkview", we don't know the current directory.
11025 * Don't do this after ":lcd", we don't keep track of what the current
11026 * directory is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011027 if (buf->b_sfname != NULL
11028 && flagp == &ssop_flags
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011029 && (ssop_flags & (SSOP_CURDIR | SSOP_SESDIR))
Bram Moolenaar8d594672009-07-01 18:18:57 +000011030#ifdef FEAT_AUTOCHDIR
11031 && !p_acd
11032#endif
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011033 && !did_lcd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011034 name = buf->b_sfname;
11035 else
11036 name = buf->b_ffname;
11037 if (ses_put_fname(fd, name, flagp) == FAIL || put_eol(fd) == FAIL)
11038 return FAIL;
11039 return OK;
11040}
11041
11042/*
11043 * Write a file name to the session file.
11044 * Takes care of the "slash" option in 'sessionoptions' and escapes special
11045 * characters.
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011046 * Returns FAIL if writing fails or out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011047 */
11048 static int
11049ses_put_fname(fd, name, flagp)
11050 FILE *fd;
11051 char_u *name;
11052 unsigned *flagp;
11053{
11054 char_u *sname;
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011055 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011056 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011057
11058 sname = home_replace_save(NULL, name);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011059 if (sname == NULL)
11060 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011061
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011062 if (*flagp & SSOP_SLASH)
11063 {
11064 /* change all backslashes to forward slashes */
11065 for (p = sname; *p != NUL; mb_ptr_adv(p))
11066 if (*p == '\\')
11067 *p = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +000011068 }
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011069
Bram Moolenaar84a05ac2013-05-06 04:24:17 +020011070 /* escape special characters */
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011071 p = vim_strsave_fnameescape(sname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011072 vim_free(sname);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011073 if (p == NULL)
11074 return FAIL;
11075
11076 /* write the result */
11077 if (fputs((char *)p, fd) < 0)
11078 retval = FAIL;
11079
11080 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011081 return retval;
11082}
11083
11084/*
11085 * ":loadview [nr]"
11086 */
11087 static void
11088ex_loadview(eap)
11089 exarg_T *eap;
11090{
11091 char_u *fname;
11092
11093 fname = get_view_file(*eap->arg);
11094 if (fname != NULL)
11095 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +000011096 do_source(fname, FALSE, DOSO_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011097 vim_free(fname);
11098 }
11099}
11100
11101/*
11102 * Get the name of the view file for the current buffer.
11103 */
11104 static char_u *
11105get_view_file(c)
11106 int c;
11107{
11108 int len = 0;
11109 char_u *p, *s;
11110 char_u *retval;
11111 char_u *sname;
11112
11113 if (curbuf->b_ffname == NULL)
11114 {
11115 EMSG(_(e_noname));
11116 return NULL;
11117 }
11118 sname = home_replace_save(NULL, curbuf->b_ffname);
11119 if (sname == NULL)
11120 return NULL;
11121
11122 /*
11123 * We want a file name without separators, because we're not going to make
11124 * a directory.
11125 * "normal" path separator -> "=+"
11126 * "=" -> "=="
11127 * ":" path separator -> "=-"
11128 */
11129 for (p = sname; *p; ++p)
11130 if (*p == '=' || vim_ispathsep(*p))
11131 ++len;
11132 retval = alloc((unsigned)(STRLEN(sname) + len + STRLEN(p_vdir) + 9));
11133 if (retval != NULL)
11134 {
11135 STRCPY(retval, p_vdir);
11136 add_pathsep(retval);
11137 s = retval + STRLEN(retval);
11138 for (p = sname; *p; ++p)
11139 {
11140 if (*p == '=')
11141 {
11142 *s++ = '=';
11143 *s++ = '=';
11144 }
11145 else if (vim_ispathsep(*p))
11146 {
11147 *s++ = '=';
Bram Moolenaare60acc12011-05-10 16:41:25 +020011148#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011149 if (*p == ':')
11150 *s++ = '-';
11151 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000011152#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000011153 *s++ = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +000011154 }
11155 else
11156 *s++ = *p;
11157 }
11158 *s++ = '=';
11159 *s++ = c;
11160 STRCPY(s, ".vim");
11161 }
11162
11163 vim_free(sname);
11164 return retval;
11165}
11166
11167#endif /* FEAT_SESSION */
11168
11169/*
11170 * Write end-of-line character(s) for ":mkexrc", ":mkvimrc" and ":mksession".
11171 * Return FAIL for a write error.
11172 */
11173 int
11174put_eol(fd)
11175 FILE *fd;
11176{
11177 if (
11178#ifdef USE_CRNL
11179 (
11180# ifdef MKSESSION_NL
11181 !mksession_nl &&
11182# endif
11183 (putc('\r', fd) < 0)) ||
11184#endif
11185 (putc('\n', fd) < 0))
11186 return FAIL;
11187 return OK;
11188}
11189
11190/*
11191 * Write a line to "fd".
11192 * Return FAIL for a write error.
11193 */
11194 int
11195put_line(fd, s)
11196 FILE *fd;
11197 char *s;
11198{
11199 if (fputs(s, fd) < 0 || put_eol(fd) == FAIL)
11200 return FAIL;
11201 return OK;
11202}
11203
11204#ifdef FEAT_VIMINFO
11205/*
11206 * ":rviminfo" and ":wviminfo".
11207 */
11208 static void
11209ex_viminfo(eap)
11210 exarg_T *eap;
11211{
11212 char_u *save_viminfo;
11213
11214 save_viminfo = p_viminfo;
11215 if (*p_viminfo == NUL)
11216 p_viminfo = (char_u *)"'100";
11217 if (eap->cmdidx == CMD_rviminfo)
11218 {
Bram Moolenaard812df62008-11-09 12:46:09 +000011219 if (read_viminfo(eap->arg, VIF_WANT_INFO | VIF_WANT_MARKS
11220 | (eap->forceit ? VIF_FORCEIT : 0)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011221 EMSG(_("E195: Cannot open viminfo file for reading"));
11222 }
11223 else
11224 write_viminfo(eap->arg, eap->forceit);
11225 p_viminfo = save_viminfo;
11226}
11227#endif
11228
11229#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +000011230/*
Bram Moolenaard9462e32011-04-11 21:35:11 +020011231 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +000011232 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +000011233 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011234 void
11235dialog_msg(buff, format, fname)
11236 char_u *buff;
11237 char *format;
11238 char_u *fname;
11239{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011240 if (fname == NULL)
11241 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +020011242 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011243}
11244#endif
11245
11246/*
11247 * ":behave {mswin,xterm}"
11248 */
11249 static void
11250ex_behave(eap)
11251 exarg_T *eap;
11252{
11253 if (STRCMP(eap->arg, "mswin") == 0)
11254 {
11255 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
11256 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
11257 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
11258 set_option_value((char_u *)"keymodel", 0L,
11259 (char_u *)"startsel,stopsel", 0);
11260 }
11261 else if (STRCMP(eap->arg, "xterm") == 0)
11262 {
11263 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
11264 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
11265 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
11266 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
11267 }
11268 else
11269 EMSG2(_(e_invarg2), eap->arg);
11270}
11271
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010011272#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
11273/*
11274 * Function given to ExpandGeneric() to obtain the possible arguments of the
11275 * ":behave {mswin,xterm}" command.
11276 */
11277 char_u *
11278get_behave_arg(xp, idx)
11279 expand_T *xp UNUSED;
11280 int idx;
11281{
11282 if (idx == 0)
11283 return (char_u *)"mswin";
11284 if (idx == 1)
11285 return (char_u *)"xterm";
11286 return NULL;
11287}
11288#endif
11289
Bram Moolenaar071d4272004-06-13 20:20:40 +000011290#ifdef FEAT_AUTOCMD
11291static int filetype_detect = FALSE;
11292static int filetype_plugin = FALSE;
11293static int filetype_indent = FALSE;
11294
11295/*
11296 * ":filetype [plugin] [indent] {on,off,detect}"
11297 * on: Load the filetype.vim file to install autocommands for file types.
11298 * off: Load the ftoff.vim file to remove all autocommands for file types.
11299 * plugin on: load filetype.vim and ftplugin.vim
11300 * plugin off: load ftplugof.vim
11301 * indent on: load filetype.vim and indent.vim
11302 * indent off: load indoff.vim
11303 */
11304 static void
11305ex_filetype(eap)
11306 exarg_T *eap;
11307{
11308 char_u *arg = eap->arg;
11309 int plugin = FALSE;
11310 int indent = FALSE;
11311
11312 if (*eap->arg == NUL)
11313 {
11314 /* Print current status. */
11315 smsg((char_u *)"filetype detection:%s plugin:%s indent:%s",
11316 filetype_detect ? "ON" : "OFF",
11317 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
11318 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
11319 return;
11320 }
11321
11322 /* Accept "plugin" and "indent" in any order. */
11323 for (;;)
11324 {
11325 if (STRNCMP(arg, "plugin", 6) == 0)
11326 {
11327 plugin = TRUE;
11328 arg = skipwhite(arg + 6);
11329 continue;
11330 }
11331 if (STRNCMP(arg, "indent", 6) == 0)
11332 {
11333 indent = TRUE;
11334 arg = skipwhite(arg + 6);
11335 continue;
11336 }
11337 break;
11338 }
11339 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
11340 {
11341 if (*arg == 'o' || !filetype_detect)
11342 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011343 source_runtime((char_u *)FILETYPE_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011344 filetype_detect = TRUE;
11345 if (plugin)
11346 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011347 source_runtime((char_u *)FTPLUGIN_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011348 filetype_plugin = TRUE;
11349 }
11350 if (indent)
11351 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011352 source_runtime((char_u *)INDENT_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011353 filetype_indent = TRUE;
11354 }
11355 }
11356 if (*arg == 'd')
11357 {
11358 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE);
Bram Moolenaara3227e22006-03-08 21:32:40 +000011359 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011360 }
11361 }
11362 else if (STRCMP(arg, "off") == 0)
11363 {
11364 if (plugin || indent)
11365 {
11366 if (plugin)
11367 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011368 source_runtime((char_u *)FTPLUGOF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011369 filetype_plugin = FALSE;
11370 }
11371 if (indent)
11372 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011373 source_runtime((char_u *)INDOFF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011374 filetype_indent = FALSE;
11375 }
11376 }
11377 else
11378 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011379 source_runtime((char_u *)FTOFF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011380 filetype_detect = FALSE;
11381 }
11382 }
11383 else
11384 EMSG2(_(e_invarg2), arg);
11385}
11386
11387/*
11388 * ":setfiletype {name}"
11389 */
11390 static void
11391ex_setfiletype(eap)
11392 exarg_T *eap;
11393{
11394 if (!did_filetype)
11395 set_option_value((char_u *)"filetype", 0L, eap->arg, OPT_LOCAL);
11396}
11397#endif
11398
Bram Moolenaar071d4272004-06-13 20:20:40 +000011399 static void
11400ex_digraphs(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +000011401 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011402{
11403#ifdef FEAT_DIGRAPHS
11404 if (*eap->arg != NUL)
11405 putdigraph(eap->arg);
11406 else
11407 listdigraphs();
11408#else
11409 EMSG(_("E196: No digraphs in this version"));
11410#endif
11411}
11412
11413 static void
11414ex_set(eap)
11415 exarg_T *eap;
11416{
11417 int flags = 0;
11418
11419 if (eap->cmdidx == CMD_setlocal)
11420 flags = OPT_LOCAL;
11421 else if (eap->cmdidx == CMD_setglobal)
11422 flags = OPT_GLOBAL;
11423#if defined(FEAT_EVAL) && defined(FEAT_AUTOCMD) && defined(FEAT_BROWSE)
11424 if (cmdmod.browse && flags == 0)
11425 ex_options(eap);
11426 else
11427#endif
11428 (void)do_set(eap->arg, flags);
11429}
11430
11431#ifdef FEAT_SEARCH_EXTRA
11432/*
11433 * ":nohlsearch"
11434 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011435 static void
11436ex_nohlsearch(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +000011437 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011438{
Bram Moolenaar8050efa2013-11-08 04:30:20 +010011439 SET_NO_HLSEARCH(TRUE);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +000011440 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011441}
11442
11443/*
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011444 * ":[N]match {group} {pattern}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000011445 * Sets nextcmd to the start of the next command, if any. Also called when
11446 * skipping commands to find the next command.
11447 */
11448 static void
11449ex_match(eap)
11450 exarg_T *eap;
11451{
11452 char_u *p;
Bram Moolenaar52d36c82007-08-11 14:00:30 +000011453 char_u *g = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011454 char_u *end;
11455 int c;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011456 int id;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011457
11458 if (eap->line2 <= 3)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011459 id = eap->line2;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011460 else
11461 {
11462 EMSG(e_invcmd);
11463 return;
11464 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011465
11466 /* First clear any old pattern. */
11467 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011468 match_delete(curwin, id, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011469
11470 if (ends_excmd(*eap->arg))
11471 end = eap->arg;
11472 else if ((STRNICMP(eap->arg, "none", 4) == 0
11473 && (vim_iswhite(eap->arg[4]) || ends_excmd(eap->arg[4]))))
11474 end = eap->arg + 4;
11475 else
11476 {
11477 p = skiptowhite(eap->arg);
11478 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011479 g = vim_strnsave(eap->arg, (int)(p - eap->arg));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011480 p = skipwhite(p);
11481 if (*p == NUL)
11482 {
11483 /* There must be two arguments. */
11484 EMSG2(_(e_invarg2), eap->arg);
11485 return;
11486 }
11487 end = skip_regexp(p + 1, *p, TRUE, NULL);
11488 if (!eap->skip)
11489 {
11490 if (*end != NUL && !ends_excmd(*skipwhite(end + 1)))
11491 {
11492 eap->errmsg = e_trailing;
11493 return;
11494 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000011495 if (*end != *p)
11496 {
11497 EMSG2(_(e_invarg2), p);
11498 return;
11499 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011500
11501 c = *end;
11502 *end = NUL;
Bram Moolenaarb3414592014-06-17 17:48:32 +020011503 match_add(curwin, g, p + 1, 10, id, NULL);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011504 vim_free(g);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000011505 *end = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011506 }
11507 }
11508 eap->nextcmd = find_nextcmd(end);
11509}
11510#endif
11511
11512#ifdef FEAT_CRYPT
11513/*
11514 * ":X": Get crypt key
11515 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011516 static void
11517ex_X(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +000011518 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011519{
Bram Moolenaar8f4ac012014-08-10 13:38:34 +020011520 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011521}
11522#endif
11523
11524#ifdef FEAT_FOLDING
11525 static void
11526ex_fold(eap)
11527 exarg_T *eap;
11528{
11529 if (foldManualAllowed(TRUE))
11530 foldCreate(eap->line1, eap->line2);
11531}
11532
11533 static void
11534ex_foldopen(eap)
11535 exarg_T *eap;
11536{
11537 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
11538 eap->forceit, FALSE);
11539}
11540
11541 static void
11542ex_folddo(eap)
11543 exarg_T *eap;
11544{
11545 linenr_T lnum;
11546
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020011547#ifdef FEAT_CLIPBOARD
11548 start_global_changes();
11549#endif
11550
Bram Moolenaar071d4272004-06-13 20:20:40 +000011551 /* First set the marks for all lines closed/open. */
11552 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
11553 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
11554 ml_setmarked(lnum);
11555
11556 /* Execute the command on the marked lines. */
11557 global_exe(eap->arg);
11558 ml_clearmarked(); /* clear rest of the marks */
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020011559#ifdef FEAT_CLIPBOARD
11560 end_global_changes();
11561#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011562}
11563#endif