blob: c957b5e73567f46114d247ca5f07ba2c181497b2 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * ex_docmd.c: functions for executing an Ex command line.
12 */
13
14#include "vim.h"
15
Bram Moolenaar071d4272004-06-13 20:20:40 +000016static int quitmore = 0;
17static int ex_pressedreturn = FALSE;
18#ifndef FEAT_PRINTER
19# define ex_hardcopy ex_ni
20#endif
21
22#ifdef FEAT_USR_CMDS
23typedef struct ucmd
24{
25 char_u *uc_name; /* The command name */
26 long_u uc_argt; /* The argument type */
27 char_u *uc_rep; /* The command's replacement string */
28 long uc_def; /* The default value for a range/count */
Bram Moolenaar071d4272004-06-13 20:20:40 +000029 int uc_compl; /* completion type */
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010030# ifdef FEAT_EVAL
31 scid_T uc_scriptID; /* SID where the command was defined */
32# ifdef FEAT_CMDL_COMPL
Bram Moolenaar071d4272004-06-13 20:20:40 +000033 char_u *uc_compl_arg; /* completion argument if any */
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010034# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000035# endif
36} ucmd_T;
37
38#define UC_BUFFER 1 /* -buffer: local to current buffer */
39
Bram Moolenaar2c29bee2005-06-01 21:46:07 +000040static garray_T ucmds = {0, 0, sizeof(ucmd_T), 4, NULL};
Bram Moolenaar071d4272004-06-13 20:20:40 +000041
42#define USER_CMD(i) (&((ucmd_T *)(ucmds.ga_data))[i])
43#define USER_CMD_GA(gap, i) (&((ucmd_T *)((gap)->ga_data))[i])
44
45static void do_ucmd __ARGS((exarg_T *eap));
46static void ex_command __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +000047static void ex_delcommand __ARGS((exarg_T *eap));
48# ifdef FEAT_CMDL_COMPL
49static char_u *get_user_command_name __ARGS((int idx));
50# endif
51
52#else
53# define ex_command ex_ni
54# define ex_comclear ex_ni
55# define ex_delcommand ex_ni
56#endif
57
58#ifdef FEAT_EVAL
Bram Moolenaar89d40322006-08-29 15:30:07 +000059static char_u *do_one_cmd __ARGS((char_u **, int, struct condstack *, char_u *(*fgetline)(int, void *, int), void *cookie));
Bram Moolenaar071d4272004-06-13 20:20:40 +000060#else
Bram Moolenaar89d40322006-08-29 15:30:07 +000061static char_u *do_one_cmd __ARGS((char_u **, int, char_u *(*fgetline)(int, void *, int), void *cookie));
Bram Moolenaar071d4272004-06-13 20:20:40 +000062static int if_level = 0; /* depth in :if */
63#endif
Bram Moolenaara6f4d612011-09-21 19:10:46 +020064static void append_command __ARGS((char_u *cmd));
Bram Moolenaar071d4272004-06-13 20:20:40 +000065static char_u *find_command __ARGS((exarg_T *eap, int *full));
66
67static void ex_abbreviate __ARGS((exarg_T *eap));
68static void ex_map __ARGS((exarg_T *eap));
69static void ex_unmap __ARGS((exarg_T *eap));
70static void ex_mapclear __ARGS((exarg_T *eap));
71static void ex_abclear __ARGS((exarg_T *eap));
72#ifndef FEAT_MENU
73# define ex_emenu ex_ni
74# define ex_menu ex_ni
75# define ex_menutranslate ex_ni
76#endif
77#ifdef FEAT_AUTOCMD
78static void ex_autocmd __ARGS((exarg_T *eap));
79static void ex_doautocmd __ARGS((exarg_T *eap));
80#else
81# define ex_autocmd ex_ni
82# define ex_doautocmd ex_ni
83# define ex_doautoall ex_ni
84#endif
85#ifdef FEAT_LISTCMDS
86static void ex_bunload __ARGS((exarg_T *eap));
87static void ex_buffer __ARGS((exarg_T *eap));
88static void ex_bmodified __ARGS((exarg_T *eap));
89static void ex_bnext __ARGS((exarg_T *eap));
90static void ex_bprevious __ARGS((exarg_T *eap));
91static void ex_brewind __ARGS((exarg_T *eap));
92static void ex_blast __ARGS((exarg_T *eap));
93#else
94# define ex_bunload ex_ni
95# define ex_buffer ex_ni
96# define ex_bmodified ex_ni
97# define ex_bnext ex_ni
98# define ex_bprevious ex_ni
99# define ex_brewind ex_ni
100# define ex_blast ex_ni
101# define buflist_list ex_ni
102# define ex_checktime ex_ni
103#endif
104#if !defined(FEAT_LISTCMDS) || !defined(FEAT_WINDOWS)
105# define ex_buffer_all ex_ni
106#endif
107static char_u *getargcmd __ARGS((char_u **));
108static char_u *skip_cmd_arg __ARGS((char_u *p, int rembs));
109static int getargopt __ARGS((exarg_T *eap));
110#ifndef FEAT_QUICKFIX
111# define ex_make ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +0000112# define ex_cbuffer ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000113# define ex_cc ex_ni
114# define ex_cnext ex_ni
115# define ex_cfile ex_ni
116# define qf_list ex_ni
117# define qf_age ex_ni
118# define ex_helpgrep ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +0000119# define ex_vimgrep ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000120#endif
121#if !defined(FEAT_QUICKFIX) || !defined(FEAT_WINDOWS)
122# define ex_cclose ex_ni
123# define ex_copen ex_ni
124# define ex_cwindow ex_ni
125#endif
Bram Moolenaar1e015462005-09-25 22:16:38 +0000126#if !defined(FEAT_QUICKFIX) || !defined(FEAT_EVAL)
127# define ex_cexpr ex_ni
128#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000129
130static int check_more __ARGS((int, int));
131static linenr_T get_address __ARGS((char_u **, int skip, int to_other_file));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000132static void get_flags __ARGS((exarg_T *eap));
Bram Moolenaar85363ab2010-07-18 13:58:26 +0200133#if !defined(FEAT_PERL) \
134 || !defined(FEAT_PYTHON) || !defined(FEAT_PYTHON3) \
135 || !defined(FEAT_TCL) \
136 || !defined(FEAT_RUBY) \
137 || !defined(FEAT_LUA) \
138 || !defined(FEAT_MZSCHEME)
Bram Moolenaar7bb75552007-07-16 18:39:49 +0000139# define HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +0000140static void ex_script_ni __ARGS((exarg_T *eap));
141#endif
142static char_u *invalid_range __ARGS((exarg_T *eap));
143static void correct_range __ARGS((exarg_T *eap));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000144#ifdef FEAT_QUICKFIX
145static char_u *replace_makeprg __ARGS((exarg_T *eap, char_u *p, char_u **cmdlinep));
146#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000147static char_u *repl_cmdline __ARGS((exarg_T *eap, char_u *src, int srclen, char_u *repl, char_u **cmdlinep));
148static void ex_highlight __ARGS((exarg_T *eap));
149static void ex_colorscheme __ARGS((exarg_T *eap));
150static void ex_quit __ARGS((exarg_T *eap));
151static void ex_cquit __ARGS((exarg_T *eap));
152static void ex_quit_all __ARGS((exarg_T *eap));
153#ifdef FEAT_WINDOWS
154static void ex_close __ARGS((exarg_T *eap));
Bram Moolenaarf740b292006-02-16 22:11:02 +0000155static void ex_win_close __ARGS((int forceit, win_T *win, tabpage_T *tp));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000156static void ex_only __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000157static void ex_resize __ARGS((exarg_T *eap));
158static void ex_stag __ARGS((exarg_T *eap));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000159static void ex_tabclose __ARGS((exarg_T *eap));
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000160static void ex_tabonly __ARGS((exarg_T *eap));
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000161static void ex_tabnext __ARGS((exarg_T *eap));
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000162static void ex_tabmove __ARGS((exarg_T *eap));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000163static void ex_tabs __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000164#else
165# define ex_close ex_ni
166# define ex_only ex_ni
167# define ex_all ex_ni
168# define ex_resize ex_ni
169# define ex_splitview ex_ni
170# define ex_stag ex_ni
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000171# define ex_tabnext ex_ni
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000172# define ex_tabmove ex_ni
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000173# define ex_tabs ex_ni
174# define ex_tabclose ex_ni
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000175# define ex_tabonly ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000176#endif
177#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
178static void ex_pclose __ARGS((exarg_T *eap));
179static void ex_ptag __ARGS((exarg_T *eap));
180static void ex_pedit __ARGS((exarg_T *eap));
181#else
182# define ex_pclose ex_ni
183# define ex_ptag ex_ni
184# define ex_pedit ex_ni
185#endif
186static void ex_hide __ARGS((exarg_T *eap));
187static void ex_stop __ARGS((exarg_T *eap));
188static void ex_exit __ARGS((exarg_T *eap));
189static void ex_print __ARGS((exarg_T *eap));
190#ifdef FEAT_BYTEOFF
191static void ex_goto __ARGS((exarg_T *eap));
192#else
193# define ex_goto ex_ni
194#endif
195static void ex_shell __ARGS((exarg_T *eap));
196static void ex_preserve __ARGS((exarg_T *eap));
197static void ex_recover __ARGS((exarg_T *eap));
198#ifndef FEAT_LISTCMDS
199# define ex_argedit ex_ni
200# define ex_argadd ex_ni
201# define ex_argdelete ex_ni
202# define ex_listdo ex_ni
203#endif
204static void ex_mode __ARGS((exarg_T *eap));
205static void ex_wrongmodifier __ARGS((exarg_T *eap));
206static void ex_find __ARGS((exarg_T *eap));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000207static void ex_open __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000208static void ex_edit __ARGS((exarg_T *eap));
209#if !defined(FEAT_GUI) && !defined(FEAT_CLIENTSERVER)
210# define ex_drop ex_ni
211#endif
212#ifndef FEAT_GUI
213# define ex_gui ex_nogui
214static void ex_nogui __ARGS((exarg_T *eap));
215#endif
216#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
217static void ex_tearoff __ARGS((exarg_T *eap));
218#else
219# define ex_tearoff ex_ni
220#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000221#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000222static void ex_popup __ARGS((exarg_T *eap));
223#else
224# define ex_popup ex_ni
225#endif
226#ifndef FEAT_GUI_MSWIN
227# define ex_simalt ex_ni
228#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000229#if !defined(FEAT_GUI_MSWIN) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000230# define gui_mch_find_dialog ex_ni
231# define gui_mch_replace_dialog ex_ni
232#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000233#if !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000234# define ex_helpfind ex_ni
235#endif
236#ifndef FEAT_CSCOPE
237# define do_cscope ex_ni
238# define do_scscope ex_ni
239# define do_cstag ex_ni
240#endif
241#ifndef FEAT_SYN_HL
242# define ex_syntax ex_ni
Bram Moolenaar860cae12010-06-05 23:22:07 +0200243# define ex_ownsyntax ex_ni
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000244#endif
Bram Moolenaarf7512552013-06-06 14:55:19 +0200245#if !defined(FEAT_SYN_HL) || !defined(FEAT_PROFILE)
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +0200246# define ex_syntime ex_ni
247#endif
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000248#ifndef FEAT_SPELL
Bram Moolenaarb765d632005-06-07 21:00:02 +0000249# define ex_spell ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000250# define ex_mkspell ex_ni
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000251# define ex_spelldump ex_ni
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000252# define ex_spellinfo ex_ni
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000253# define ex_spellrepall ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000254#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200255#ifndef FEAT_PERSISTENT_UNDO
256# define ex_rundo ex_ni
257# define ex_wundo ex_ni
258#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200259#ifndef FEAT_LUA
260# define ex_lua ex_script_ni
261# define ex_luado ex_ni
262# define ex_luafile ex_ni
263#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000264#ifndef FEAT_MZSCHEME
265# define ex_mzscheme ex_script_ni
266# define ex_mzfile ex_ni
267#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000268#ifndef FEAT_PERL
269# define ex_perl ex_script_ni
270# define ex_perldo ex_ni
271#endif
272#ifndef FEAT_PYTHON
273# define ex_python ex_script_ni
Bram Moolenaard620aa92013-05-17 16:40:06 +0200274# define ex_pydo ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000275# define ex_pyfile ex_ni
276#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200277#ifndef FEAT_PYTHON3
Bram Moolenaar368373e2010-07-19 20:46:22 +0200278# define ex_py3 ex_script_ni
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200279# define ex_py3do ex_ni
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200280# define ex_py3file ex_ni
281#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000282#ifndef FEAT_TCL
283# define ex_tcl ex_script_ni
284# define ex_tcldo ex_ni
285# define ex_tclfile ex_ni
286#endif
287#ifndef FEAT_RUBY
288# define ex_ruby ex_script_ni
289# define ex_rubydo ex_ni
290# define ex_rubyfile ex_ni
291#endif
292#ifndef FEAT_SNIFF
293# define ex_sniff ex_ni
294#endif
295#ifndef FEAT_KEYMAP
296# define ex_loadkeymap ex_ni
297#endif
298static void ex_swapname __ARGS((exarg_T *eap));
299static void ex_syncbind __ARGS((exarg_T *eap));
300static void ex_read __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000301static void ex_pwd __ARGS((exarg_T *eap));
302static void ex_equal __ARGS((exarg_T *eap));
303static void ex_sleep __ARGS((exarg_T *eap));
304static void do_exmap __ARGS((exarg_T *eap, int isabbrev));
305static void ex_winsize __ARGS((exarg_T *eap));
306#ifdef FEAT_WINDOWS
307static void ex_wincmd __ARGS((exarg_T *eap));
308#else
309# define ex_wincmd ex_ni
310#endif
Bram Moolenaar843ee412004-06-30 16:16:41 +0000311#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000312static void ex_winpos __ARGS((exarg_T *eap));
313#else
314# define ex_winpos ex_ni
315#endif
316static void ex_operators __ARGS((exarg_T *eap));
317static void ex_put __ARGS((exarg_T *eap));
318static void ex_copymove __ARGS((exarg_T *eap));
319static void ex_submagic __ARGS((exarg_T *eap));
320static void ex_join __ARGS((exarg_T *eap));
321static void ex_at __ARGS((exarg_T *eap));
322static void ex_bang __ARGS((exarg_T *eap));
323static void ex_undo __ARGS((exarg_T *eap));
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200324#ifdef FEAT_PERSISTENT_UNDO
325static void ex_wundo __ARGS((exarg_T *eap));
326static void ex_rundo __ARGS((exarg_T *eap));
327#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000328static void ex_redo __ARGS((exarg_T *eap));
Bram Moolenaarc7d89352006-03-14 22:53:34 +0000329static void ex_later __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000330static void ex_redir __ARGS((exarg_T *eap));
331static void ex_redraw __ARGS((exarg_T *eap));
332static void ex_redrawstatus __ARGS((exarg_T *eap));
333static void close_redir __ARGS((void));
334static void ex_mkrc __ARGS((exarg_T *eap));
335static void ex_mark __ARGS((exarg_T *eap));
336#ifdef FEAT_USR_CMDS
337static char_u *uc_fun_cmd __ARGS((void));
Bram Moolenaar52b4b552005-03-07 23:00:57 +0000338static 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 +0000339#endif
340#ifdef FEAT_EX_EXTRA
341static void ex_normal __ARGS((exarg_T *eap));
342static void ex_startinsert __ARGS((exarg_T *eap));
343static void ex_stopinsert __ARGS((exarg_T *eap));
344#else
345# define ex_normal ex_ni
346# define ex_align ex_ni
347# define ex_retab ex_ni
348# define ex_startinsert ex_ni
349# define ex_stopinsert ex_ni
350# define ex_helptags ex_ni
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +0000351# define ex_sort ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000352#endif
353#ifdef FEAT_FIND_ID
354static void ex_checkpath __ARGS((exarg_T *eap));
355static void ex_findpat __ARGS((exarg_T *eap));
356#else
357# define ex_findpat ex_ni
358# define ex_checkpath ex_ni
359#endif
360#if defined(FEAT_FIND_ID) && defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
361static void ex_psearch __ARGS((exarg_T *eap));
362#else
363# define ex_psearch ex_ni
364#endif
365static void ex_tag __ARGS((exarg_T *eap));
366static void ex_tag_cmd __ARGS((exarg_T *eap, char_u *name));
367#ifndef FEAT_EVAL
368# define ex_scriptnames ex_ni
369# define ex_finish ex_ni
370# define ex_echo ex_ni
371# define ex_echohl ex_ni
372# define ex_execute ex_ni
373# define ex_call ex_ni
374# define ex_if ex_ni
375# define ex_endif ex_ni
376# define ex_else ex_ni
377# define ex_while ex_ni
378# define ex_continue ex_ni
379# define ex_break ex_ni
380# define ex_endwhile ex_ni
381# define ex_throw ex_ni
382# define ex_try ex_ni
383# define ex_catch ex_ni
384# define ex_finally ex_ni
385# define ex_endtry ex_ni
386# define ex_endfunction ex_ni
387# define ex_let ex_ni
388# define ex_unlet ex_ni
Bram Moolenaar65c1b012005-01-31 19:02:28 +0000389# define ex_lockvar ex_ni
390# define ex_unlockvar ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000391# define ex_function ex_ni
392# define ex_delfunction ex_ni
393# define ex_return ex_ni
Bram Moolenaard812df62008-11-09 12:46:09 +0000394# define ex_oldfiles ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000395#endif
396static char_u *arg_all __ARGS((void));
397#ifdef FEAT_SESSION
398static int makeopens __ARGS((FILE *fd, char_u *dirnow));
Bram Moolenaarf13be0d2008-01-02 14:13:10 +0000399static 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 +0000400static void ex_loadview __ARGS((exarg_T *eap));
401static char_u *get_view_file __ARGS((int c));
Bram Moolenaareeefcc72007-05-01 21:21:21 +0000402static int did_lcd; /* whether ":lcd" was produced for a session */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000403#else
404# define ex_loadview ex_ni
405#endif
406#ifndef FEAT_EVAL
407# define ex_compiler ex_ni
408#endif
409#ifdef FEAT_VIMINFO
410static void ex_viminfo __ARGS((exarg_T *eap));
411#else
412# define ex_viminfo ex_ni
413#endif
414static void ex_behave __ARGS((exarg_T *eap));
415#ifdef FEAT_AUTOCMD
416static void ex_filetype __ARGS((exarg_T *eap));
417static void ex_setfiletype __ARGS((exarg_T *eap));
418#else
419# define ex_filetype ex_ni
420# define ex_setfiletype ex_ni
421#endif
422#ifndef FEAT_DIFF
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000423# define ex_diffoff ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000424# define ex_diffpatch ex_ni
425# define ex_diffgetput ex_ni
426# define ex_diffsplit ex_ni
427# define ex_diffthis ex_ni
428# define ex_diffupdate ex_ni
429#endif
430static void ex_digraphs __ARGS((exarg_T *eap));
431static void ex_set __ARGS((exarg_T *eap));
432#if !defined(FEAT_EVAL) || !defined(FEAT_AUTOCMD)
433# define ex_options ex_ni
434#endif
435#ifdef FEAT_SEARCH_EXTRA
436static void ex_nohlsearch __ARGS((exarg_T *eap));
437static void ex_match __ARGS((exarg_T *eap));
438#else
439# define ex_nohlsearch ex_ni
440# define ex_match ex_ni
441#endif
442#ifdef FEAT_CRYPT
443static void ex_X __ARGS((exarg_T *eap));
444#else
445# define ex_X ex_ni
446#endif
447#ifdef FEAT_FOLDING
448static void ex_fold __ARGS((exarg_T *eap));
449static void ex_foldopen __ARGS((exarg_T *eap));
450static void ex_folddo __ARGS((exarg_T *eap));
451#else
452# define ex_fold ex_ni
453# define ex_foldopen ex_ni
454# define ex_folddo ex_ni
455#endif
456#if !((defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
457 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE)))
458# define ex_language ex_ni
459#endif
460#ifndef FEAT_SIGNS
461# define ex_sign ex_ni
462#endif
463#ifndef FEAT_SUN_WORKSHOP
464# define ex_wsverb ex_ni
465#endif
Bram Moolenaar009b2592004-10-24 19:18:58 +0000466#ifndef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200467# define ex_nbclose ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000468# define ex_nbkey ex_ni
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200469# define ex_nbstart ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000470#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000471
472#ifndef FEAT_EVAL
473# define ex_debug ex_ni
474# define ex_breakadd ex_ni
475# define ex_debuggreedy ex_ni
476# define ex_breakdel ex_ni
477# define ex_breaklist ex_ni
478#endif
479
480#ifndef FEAT_CMDHIST
481# define ex_history ex_ni
482#endif
483#ifndef FEAT_JUMPLIST
484# define ex_jumps ex_ni
485# define ex_changes ex_ni
486#endif
487
Bram Moolenaar05159a02005-02-26 23:04:13 +0000488#ifndef FEAT_PROFILE
489# define ex_profile ex_ni
490#endif
491
Bram Moolenaar071d4272004-06-13 20:20:40 +0000492/*
493 * Declare cmdnames[].
494 */
495#define DO_DECLARE_EXCMD
496#include "ex_cmds.h"
497
498/*
499 * Table used to quickly search for a command, based on its first character.
500 */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +0000501static cmdidx_T cmdidxs[27] =
Bram Moolenaar071d4272004-06-13 20:20:40 +0000502{
503 CMD_append,
504 CMD_buffer,
505 CMD_change,
506 CMD_delete,
507 CMD_edit,
508 CMD_file,
509 CMD_global,
510 CMD_help,
511 CMD_insert,
512 CMD_join,
513 CMD_k,
514 CMD_list,
515 CMD_move,
516 CMD_next,
517 CMD_open,
518 CMD_print,
519 CMD_quit,
520 CMD_read,
521 CMD_substitute,
522 CMD_t,
523 CMD_undo,
524 CMD_vglobal,
525 CMD_write,
526 CMD_xit,
527 CMD_yank,
528 CMD_z,
529 CMD_bang
530};
531
532static char_u dollar_command[2] = {'$', 0};
533
534
535#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000536/* Struct for storing a line inside a while/for loop */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000537typedef struct
538{
539 char_u *line; /* command line */
540 linenr_T lnum; /* sourcing_lnum of the line */
541} wcmd_T;
542
543/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000544 * Structure used to store info for line position in a while or for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545 * This is required, because do_one_cmd() may invoke ex_function(), which
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000546 * reads more lines that may come from the while/for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000547 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000548struct loop_cookie
Bram Moolenaar071d4272004-06-13 20:20:40 +0000549{
550 garray_T *lines_gap; /* growarray with line info */
551 int current_line; /* last read line from growarray */
552 int repeating; /* TRUE when looping a second time */
553 /* When "repeating" is FALSE use "getline" and "cookie" to get lines */
554 char_u *(*getline) __ARGS((int, void *, int));
555 void *cookie;
556};
557
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000558static char_u *get_loop_line __ARGS((int c, void *cookie, int indent));
559static int store_loop_line __ARGS((garray_T *gap, char_u *line));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000560static void free_cmdlines __ARGS((garray_T *gap));
Bram Moolenaared203462004-06-16 11:19:22 +0000561
562/* Struct to save a few things while debugging. Used in do_cmdline() only. */
563struct dbg_stuff
564{
565 int trylevel;
566 int force_abort;
567 except_T *caught_stack;
568 char_u *vv_exception;
569 char_u *vv_throwpoint;
570 int did_emsg;
571 int got_int;
572 int did_throw;
573 int need_rethrow;
574 int check_cstack;
575 except_T *current_exception;
576};
577
578static void save_dbg_stuff __ARGS((struct dbg_stuff *dsp));
579static void restore_dbg_stuff __ARGS((struct dbg_stuff *dsp));
580
581 static void
582save_dbg_stuff(dsp)
583 struct dbg_stuff *dsp;
584{
585 dsp->trylevel = trylevel; trylevel = 0;
586 dsp->force_abort = force_abort; force_abort = FALSE;
587 dsp->caught_stack = caught_stack; caught_stack = NULL;
588 dsp->vv_exception = v_exception(NULL);
589 dsp->vv_throwpoint = v_throwpoint(NULL);
590
591 /* Necessary for debugging an inactive ":catch", ":finally", ":endtry" */
592 dsp->did_emsg = did_emsg; did_emsg = FALSE;
593 dsp->got_int = got_int; got_int = FALSE;
594 dsp->did_throw = did_throw; did_throw = FALSE;
595 dsp->need_rethrow = need_rethrow; need_rethrow = FALSE;
596 dsp->check_cstack = check_cstack; check_cstack = FALSE;
597 dsp->current_exception = current_exception; current_exception = NULL;
598}
599
600 static void
601restore_dbg_stuff(dsp)
602 struct dbg_stuff *dsp;
603{
604 suppress_errthrow = FALSE;
605 trylevel = dsp->trylevel;
606 force_abort = dsp->force_abort;
607 caught_stack = dsp->caught_stack;
608 (void)v_exception(dsp->vv_exception);
609 (void)v_throwpoint(dsp->vv_throwpoint);
610 did_emsg = dsp->did_emsg;
611 got_int = dsp->got_int;
612 did_throw = dsp->did_throw;
613 need_rethrow = dsp->need_rethrow;
614 check_cstack = dsp->check_cstack;
615 current_exception = dsp->current_exception;
616}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000617#endif
618
619
620/*
621 * do_exmode(): Repeatedly get commands for the "Ex" mode, until the ":vi"
622 * command is given.
623 */
624 void
625do_exmode(improved)
626 int improved; /* TRUE for "improved Ex" mode */
627{
628 int save_msg_scroll;
629 int prev_msg_row;
630 linenr_T prev_line;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000631 int changedtick;
632
633 if (improved)
634 exmode_active = EXMODE_VIM;
635 else
636 exmode_active = EXMODE_NORMAL;
637 State = NORMAL;
638
639 /* When using ":global /pat/ visual" and then "Q" we return to continue
640 * the :global command. */
641 if (global_busy)
642 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000643
644 save_msg_scroll = msg_scroll;
645 ++RedrawingDisabled; /* don't redisplay the window */
646 ++no_wait_return; /* don't wait for return */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000647#ifdef FEAT_GUI
648 /* Ignore scrollbar and mouse events in Ex mode */
649 ++hold_gui_events;
650#endif
651#ifdef FEAT_SNIFF
652 want_sniff_request = 0; /* No K_SNIFF wanted */
653#endif
654
655 MSG(_("Entering Ex mode. Type \"visual\" to go to Normal mode."));
656 while (exmode_active)
657 {
Bram Moolenaar7c626922005-02-07 22:01:03 +0000658#ifdef FEAT_EX_EXTRA
659 /* Check for a ":normal" command and no more characters left. */
660 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
661 {
662 exmode_active = FALSE;
663 break;
664 }
665#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000666 msg_scroll = TRUE;
667 need_wait_return = FALSE;
668 ex_pressedreturn = FALSE;
669 ex_no_reprint = FALSE;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000670 changedtick = curbuf->b_changedtick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000671 prev_msg_row = msg_row;
672 prev_line = curwin->w_cursor.lnum;
673#ifdef FEAT_SNIFF
674 ProcessSniffRequests();
675#endif
676 if (improved)
677 {
678 cmdline_row = msg_row;
679 do_cmdline(NULL, getexline, NULL, 0);
680 }
681 else
682 do_cmdline(NULL, getexmodeline, NULL, DOCMD_NOWAIT);
683 lines_left = Rows - 1;
684
Bram Moolenaardf177f62005-02-22 08:39:57 +0000685 if ((prev_line != curwin->w_cursor.lnum
686 || changedtick != curbuf->b_changedtick) && !ex_no_reprint)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000687 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000688 if (curbuf->b_ml.ml_flags & ML_EMPTY)
689 EMSG(_(e_emptybuf));
690 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000692 if (ex_pressedreturn)
693 {
694 /* go up one line, to overwrite the ":<CR>" line, so the
Bram Moolenaar81870892007-11-11 18:17:28 +0000695 * output doesn't contain empty lines. */
Bram Moolenaardf177f62005-02-22 08:39:57 +0000696 msg_row = prev_msg_row;
697 if (prev_msg_row == Rows - 1)
698 msg_row--;
699 }
700 msg_col = 0;
701 print_line_no_prefix(curwin->w_cursor.lnum, FALSE, FALSE);
702 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000703 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704 }
Bram Moolenaardf177f62005-02-22 08:39:57 +0000705 else if (ex_pressedreturn && !ex_no_reprint) /* must be at EOF */
706 {
707 if (curbuf->b_ml.ml_flags & ML_EMPTY)
708 EMSG(_(e_emptybuf));
709 else
710 EMSG(_("E501: At end-of-file"));
711 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000712 }
713
714#ifdef FEAT_GUI
715 --hold_gui_events;
716#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000717 --RedrawingDisabled;
718 --no_wait_return;
719 update_screen(CLEAR);
720 need_wait_return = FALSE;
721 msg_scroll = save_msg_scroll;
722}
723
724/*
725 * Execute a simple command line. Used for translated commands like "*".
726 */
727 int
728do_cmdline_cmd(cmd)
729 char_u *cmd;
730{
731 return do_cmdline(cmd, NULL, NULL,
732 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
733}
734
735/*
736 * do_cmdline(): execute one Ex command line
737 *
738 * 1. Execute "cmdline" when it is not NULL.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100739 * If "cmdline" is NULL, or more lines are needed, fgetline() is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000740 * 2. Split up in parts separated with '|'.
741 *
742 * This function can be called recursively!
743 *
744 * flags:
745 * DOCMD_VERBOSE - The command will be included in the error message.
746 * DOCMD_NOWAIT - Don't call wait_return() and friends.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100747 * DOCMD_REPEAT - Repeat execution until fgetline() returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000748 * DOCMD_KEYTYPED - Don't reset KeyTyped.
749 * DOCMD_EXCRESET - Reset the exception environment (used for debugging).
750 * DOCMD_KEEPLINE - Store first typed line (for repeating with ".").
751 *
752 * return FAIL if cmdline could not be executed, OK otherwise
753 */
754 int
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100755do_cmdline(cmdline, fgetline, cookie, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000756 char_u *cmdline;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100757 char_u *(*fgetline) __ARGS((int, void *, int));
758 void *cookie; /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000759 int flags;
760{
761 char_u *next_cmdline; /* next cmd to execute */
762 char_u *cmdline_copy = NULL; /* copy of cmd line */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100763 int used_getline = FALSE; /* used "fgetline" to obtain command */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000764 static int recursive = 0; /* recursive depth */
765 int msg_didout_before_start = 0;
766 int count = 0; /* line number count */
767 int did_inc = FALSE; /* incremented RedrawingDisabled */
768 int retval = OK;
769#ifdef FEAT_EVAL
770 struct condstack cstack; /* conditional stack */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000771 garray_T lines_ga; /* keep lines for ":while"/":for" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772 int current_line = 0; /* active line in lines_ga */
773 char_u *fname = NULL; /* function or script name */
774 linenr_T *breakpoint = NULL; /* ptr to breakpoint field in cookie */
775 int *dbg_tick = NULL; /* ptr to dbg_tick field in cookie */
Bram Moolenaared203462004-06-16 11:19:22 +0000776 struct dbg_stuff debug_saved; /* saved things for debug mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000777 int initial_trylevel;
778 struct msglist **saved_msg_list = NULL;
779 struct msglist *private_msg_list;
780
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100781 /* "fgetline" and "cookie" passed to do_one_cmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782 char_u *(*cmd_getline) __ARGS((int, void *, int));
783 void *cmd_cookie;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000784 struct loop_cookie cmd_loop_cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785 void *real_cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000786 int getline_is_func;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787#else
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100788# define cmd_getline fgetline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000789# define cmd_cookie cookie
790#endif
791 static int call_depth = 0; /* recursiveness */
792
793#ifdef FEAT_EVAL
794 /* For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory
795 * location for storing error messages to be converted to an exception.
Bram Moolenaarcf3630f2005-01-08 16:04:29 +0000796 * This ensures that the do_errthrow() call in do_one_cmd() does not
797 * combine the messages stored by an earlier invocation of do_one_cmd()
798 * with the command name of the later one. This would happen when
799 * BufWritePost autocommands are executed after a write error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800 saved_msg_list = msg_list;
801 msg_list = &private_msg_list;
802 private_msg_list = NULL;
803#endif
804
805 /* It's possible to create an endless loop with ":execute", catch that
806 * here. The value of 200 allows nested function calls, ":source", etc. */
807 if (call_depth == 200)
808 {
809 EMSG(_("E169: Command too recursive"));
810#ifdef FEAT_EVAL
811 /* When converting to an exception, we do not include the command name
812 * since this is not an error of the specific command. */
813 do_errthrow((struct condstack *)NULL, (char_u *)NULL);
814 msg_list = saved_msg_list;
815#endif
816 return FAIL;
817 }
818 ++call_depth;
819
820#ifdef FEAT_EVAL
821 cstack.cs_idx = -1;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000822 cstack.cs_looplevel = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823 cstack.cs_trylevel = 0;
824 cstack.cs_emsg_silent_list = NULL;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000825 cstack.cs_lflags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826 ga_init2(&lines_ga, (int)sizeof(wcmd_T), 10);
827
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100828 real_cookie = getline_cookie(fgetline, cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000829
830 /* Inside a function use a higher nesting level. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100831 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000832 if (getline_is_func && ex_nesting_level == func_level(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000833 ++ex_nesting_level;
834
835 /* Get the function or script name and the address where the next breakpoint
836 * line and the debug tick for a function or script are stored. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000837 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000838 {
839 fname = func_name(real_cookie);
840 breakpoint = func_breakpoint(real_cookie);
841 dbg_tick = func_dbg_tick(real_cookie);
842 }
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100843 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000844 {
845 fname = sourcing_name;
846 breakpoint = source_breakpoint(real_cookie);
847 dbg_tick = source_dbg_tick(real_cookie);
848 }
849
850 /*
851 * Initialize "force_abort" and "suppress_errthrow" at the top level.
852 */
853 if (!recursive)
854 {
855 force_abort = FALSE;
856 suppress_errthrow = FALSE;
857 }
858
859 /*
860 * If requested, store and reset the global values controlling the
Bram Moolenaar89d40322006-08-29 15:30:07 +0000861 * exception handling (used when debugging). Otherwise clear it to avoid
862 * a bogus compiler warning when the optimizer uses inline functions...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000863 */
Bram Moolenaarb4872942006-05-13 10:32:52 +0000864 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +0000865 save_dbg_stuff(&debug_saved);
Bram Moolenaar89d40322006-08-29 15:30:07 +0000866 else
Bram Moolenaar7db5fc82010-05-24 11:59:29 +0200867 vim_memset(&debug_saved, 0, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000868
869 initial_trylevel = trylevel;
870
871 /*
872 * "did_throw" will be set to TRUE when an exception is being thrown.
873 */
874 did_throw = FALSE;
875#endif
876 /*
877 * "did_emsg" will be set to TRUE when emsg() is used, in which case we
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000878 * cancel the whole command line, and any if/endif or loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879 * If force_abort is set, we cancel everything.
880 */
881 did_emsg = FALSE;
882
883 /*
884 * KeyTyped is only set when calling vgetc(). Reset it here when not
885 * calling vgetc() (sourced command lines).
886 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100887 if (!(flags & DOCMD_KEYTYPED)
888 && !getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889 KeyTyped = FALSE;
890
891 /*
892 * Continue executing command lines:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000893 * - when inside an ":if", ":while" or ":for"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894 * - for multiple commands on one line, separated with '|'
895 * - when repeating until there are no more lines (for ":source")
896 */
897 next_cmdline = cmdline;
898 do
899 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000900#ifdef FEAT_EVAL
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100901 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000902#endif
903
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000904 /* stop skipping cmds for an error msg after all endif/while/for */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000905 if (next_cmdline == NULL
906#ifdef FEAT_EVAL
907 && !force_abort
908 && cstack.cs_idx < 0
Bram Moolenaar05159a02005-02-26 23:04:13 +0000909 && !(getline_is_func && func_has_abort(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000910#endif
911 )
912 did_emsg = FALSE;
913
914 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000915 * 1. If repeating a line in a loop, get a line from lines_ga.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100916 * 2. If no line given: Get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000917 * 3. If a line is given: Make a copy, so we can mess with it.
918 */
919
920#ifdef FEAT_EVAL
921 /* 1. If repeating, get a previous line from lines_ga. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000922 if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000923 {
924 /* Each '|' separated command is stored separately in lines_ga, to
925 * be able to jump to it. Don't use next_cmdline now. */
926 vim_free(cmdline_copy);
927 cmdline_copy = NULL;
928
929 /* Check if a function has returned or, unless it has an unclosed
930 * try conditional, aborted. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000931 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000932 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000933# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000934 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000935 func_line_end(real_cookie);
936# endif
937 if (func_has_ended(real_cookie))
938 {
939 retval = FAIL;
940 break;
941 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000942 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000943#ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000944 else if (do_profiling == PROF_YES
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100945 && getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000946 script_line_end();
947#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000948
949 /* Check if a sourced file hit a ":finish" command. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100950 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000951 {
952 retval = FAIL;
953 break;
954 }
955
956 /* If breakpoints have been added/deleted need to check for it. */
957 if (breakpoint != NULL && dbg_tick != NULL
958 && *dbg_tick != debug_tick)
959 {
960 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100961 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962 fname, sourcing_lnum);
963 *dbg_tick = debug_tick;
964 }
965
966 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
967 sourcing_lnum = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
968
969 /* Did we encounter a breakpoint? */
970 if (breakpoint != NULL && *breakpoint != 0
971 && *breakpoint <= sourcing_lnum)
972 {
973 dbg_breakpoint(fname, sourcing_lnum);
974 /* Find next breakpoint. */
975 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100976 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977 fname, sourcing_lnum);
978 *dbg_tick = debug_tick;
979 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000980# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000981 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000982 {
983 if (getline_is_func)
984 func_line_start(real_cookie);
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100985 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000986 script_line_start();
987 }
988# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989 }
990
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000991 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000993 /* Inside a while/for loop we need to store the lines and use them
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100994 * again. Pass a different "fgetline" function to do_one_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995 * below, so that it stores lines in or reads them from
996 * "lines_ga". Makes it possible to define a function inside a
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000997 * while/for loop. */
998 cmd_getline = get_loop_line;
999 cmd_cookie = (void *)&cmd_loop_cookie;
1000 cmd_loop_cookie.lines_gap = &lines_ga;
1001 cmd_loop_cookie.current_line = current_line;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001002 cmd_loop_cookie.getline = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001003 cmd_loop_cookie.cookie = cookie;
1004 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005 }
1006 else
1007 {
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001008 cmd_getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009 cmd_cookie = cookie;
1010 }
1011#endif
1012
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001013 /* 2. If no line given, get an allocated line with fgetline(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014 if (next_cmdline == NULL)
1015 {
1016 /*
1017 * Need to set msg_didout for the first line after an ":if",
1018 * otherwise the ":if" will be overwritten.
1019 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001020 if (count == 1 && getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021 msg_didout = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001022 if (fgetline == NULL || (next_cmdline = fgetline(':', cookie,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023#ifdef FEAT_EVAL
1024 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
1025#else
1026 0
1027#endif
1028 )) == NULL)
1029 {
1030 /* Don't call wait_return for aborted command line. The NULL
1031 * returned for the end of a sourced file or executed function
1032 * doesn't do this. */
1033 if (KeyTyped && !(flags & DOCMD_REPEAT))
1034 need_wait_return = FALSE;
1035 retval = FAIL;
1036 break;
1037 }
1038 used_getline = TRUE;
1039
1040 /*
1041 * Keep the first typed line. Clear it when more lines are typed.
1042 */
1043 if (flags & DOCMD_KEEPLINE)
1044 {
1045 vim_free(repeat_cmdline);
1046 if (count == 0)
1047 repeat_cmdline = vim_strsave(next_cmdline);
1048 else
1049 repeat_cmdline = NULL;
1050 }
1051 }
1052
1053 /* 3. Make a copy of the command so we can mess with it. */
1054 else if (cmdline_copy == NULL)
1055 {
1056 next_cmdline = vim_strsave(next_cmdline);
1057 if (next_cmdline == NULL)
1058 {
1059 EMSG(_(e_outofmem));
1060 retval = FAIL;
1061 break;
1062 }
1063 }
1064 cmdline_copy = next_cmdline;
1065
1066#ifdef FEAT_EVAL
1067 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001068 * Save the current line when inside a ":while" or ":for", and when
1069 * the command looks like a ":while" or ":for", because we may need it
1070 * later. When there is a '|' and another command, it is stored
1071 * separately, because we need to be able to jump back to it from an
1072 * :endwhile/:endfor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001073 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001074 if (current_line == lines_ga.ga_len
1075 && (cstack.cs_looplevel || has_loop_cmd(next_cmdline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001076 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001077 if (store_loop_line(&lines_ga, next_cmdline) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078 {
1079 retval = FAIL;
1080 break;
1081 }
1082 }
1083 did_endif = FALSE;
1084#endif
1085
1086 if (count++ == 0)
1087 {
1088 /*
1089 * All output from the commands is put below each other, without
1090 * waiting for a return. Don't do this when executing commands
1091 * from a script or when being called recursive (e.g. for ":e
1092 * +command file").
1093 */
1094 if (!(flags & DOCMD_NOWAIT) && !recursive)
1095 {
1096 msg_didout_before_start = msg_didout;
1097 msg_didany = FALSE; /* no output yet */
1098 msg_start();
1099 msg_scroll = TRUE; /* put messages below each other */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001100 ++no_wait_return; /* don't wait for return until finished */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101 ++RedrawingDisabled;
1102 did_inc = TRUE;
1103 }
1104 }
1105
1106 if (p_verbose >= 15 && sourcing_name != NULL)
1107 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001108 ++no_wait_return;
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001109 verbose_enter_scroll();
1110
Bram Moolenaar071d4272004-06-13 20:20:40 +00001111 smsg((char_u *)_("line %ld: %s"),
1112 (long)sourcing_lnum, cmdline_copy);
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001113 if (msg_silent == 0)
1114 msg_puts((char_u *)"\n"); /* don't overwrite this */
1115
1116 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001117 --no_wait_return;
1118 }
1119
1120 /*
1121 * 2. Execute one '|' separated command.
1122 * do_one_cmd() will return NULL if there is no trailing '|'.
1123 * "cmdline_copy" can change, e.g. for '%' and '#' expansion.
1124 */
1125 ++recursive;
1126 next_cmdline = do_one_cmd(&cmdline_copy, flags & DOCMD_VERBOSE,
1127#ifdef FEAT_EVAL
1128 &cstack,
1129#endif
1130 cmd_getline, cmd_cookie);
1131 --recursive;
1132
1133#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001134 if (cmd_cookie == (void *)&cmd_loop_cookie)
1135 /* Use "current_line" from "cmd_loop_cookie", it may have been
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136 * incremented when defining a function. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001137 current_line = cmd_loop_cookie.current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001138#endif
1139
1140 if (next_cmdline == NULL)
1141 {
1142 vim_free(cmdline_copy);
1143 cmdline_copy = NULL;
1144#ifdef FEAT_CMDHIST
1145 /*
1146 * If the command was typed, remember it for the ':' register.
1147 * Do this AFTER executing the command to make :@: work.
1148 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001149 if (getline_equal(fgetline, cookie, getexline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150 && new_last_cmdline != NULL)
1151 {
1152 vim_free(last_cmdline);
1153 last_cmdline = new_last_cmdline;
1154 new_last_cmdline = NULL;
1155 }
1156#endif
1157 }
1158 else
1159 {
1160 /* need to copy the command after the '|' to cmdline_copy, for the
1161 * next do_one_cmd() */
Bram Moolenaara7241f52008-06-24 20:39:31 +00001162 STRMOVE(cmdline_copy, next_cmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001163 next_cmdline = cmdline_copy;
1164 }
1165
1166
1167#ifdef FEAT_EVAL
1168 /* reset did_emsg for a function that is not aborted by an error */
1169 if (did_emsg && !force_abort
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001170 && getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171 && !func_has_abort(real_cookie))
1172 did_emsg = FALSE;
1173
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001174 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175 {
1176 ++current_line;
1177
1178 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001179 * An ":endwhile", ":endfor" and ":continue" is handled here.
1180 * If we were executing commands, jump back to the ":while" or
1181 * ":for".
1182 * If we were not executing commands, decrement cs_looplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001184 if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001186 cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001187
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001188 /* Jump back to the matching ":while" or ":for". Be careful
1189 * not to use a cs_line[] from an entry that isn't a ":while"
1190 * or ":for": It would make "current_line" invalid and can
1191 * cause a crash. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192 if (!did_emsg && !got_int && !did_throw
1193 && cstack.cs_idx >= 0
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001194 && (cstack.cs_flags[cstack.cs_idx]
1195 & (CSF_WHILE | CSF_FOR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001196 && cstack.cs_line[cstack.cs_idx] >= 0
1197 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
1198 {
1199 current_line = cstack.cs_line[cstack.cs_idx];
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001200 /* remember we jumped there */
1201 cstack.cs_lflags |= CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202 line_breakcheck(); /* check if CTRL-C typed */
1203
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001204 /* Check for the next breakpoint at or after the ":while"
1205 * or ":for". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206 if (breakpoint != NULL)
1207 {
1208 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001209 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210 fname,
1211 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
1212 *dbg_tick = debug_tick;
1213 }
1214 }
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001215 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001217 /* can only get here with ":endwhile" or ":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218 if (cstack.cs_idx >= 0)
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001219 rewind_conditionals(&cstack, cstack.cs_idx - 1,
1220 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221 }
1222 }
1223
1224 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001225 * For a ":while" or ":for" we need to remember the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001227 else if (cstack.cs_lflags & CSL_HAD_LOOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001228 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001229 cstack.cs_lflags &= ~CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001230 cstack.cs_line[cstack.cs_idx] = current_line - 1;
1231 }
1232 }
1233
1234 /*
1235 * When not inside any ":while" loop, clear remembered lines.
1236 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001237 if (cstack.cs_looplevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001238 {
1239 if (lines_ga.ga_len > 0)
1240 {
1241 sourcing_lnum =
1242 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
1243 free_cmdlines(&lines_ga);
1244 }
1245 current_line = 0;
1246 }
1247
1248 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001249 * A ":finally" makes did_emsg, got_int, and did_throw pending for
1250 * being restored at the ":endtry". Reset them here and set the
1251 * ACTIVE and FINALLY flags, so that the finally clause gets executed.
1252 * This includes the case where a missing ":endif", ":endwhile" or
1253 * ":endfor" was detected by the ":finally" itself.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001255 if (cstack.cs_lflags & CSL_HAD_FINA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001257 cstack.cs_lflags &= ~CSL_HAD_FINA;
1258 report_make_pending(cstack.cs_pending[cstack.cs_idx]
1259 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001260 did_throw ? (void *)current_exception : NULL);
1261 did_emsg = got_int = did_throw = FALSE;
1262 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
1263 }
1264
1265 /* Update global "trylevel" for recursive calls to do_cmdline() from
1266 * within this loop. */
1267 trylevel = initial_trylevel + cstack.cs_trylevel;
1268
1269 /*
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001270 * If the outermost try conditional (across function calls and sourced
Bram Moolenaar071d4272004-06-13 20:20:40 +00001271 * files) is aborted because of an error, an interrupt, or an uncaught
1272 * exception, cancel everything. If it is left normally, reset
1273 * force_abort to get the non-EH compatible abortion behavior for
1274 * the rest of the script.
1275 */
1276 if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
1277 force_abort = FALSE;
1278
1279 /* Convert an interrupt to an exception if appropriate. */
1280 (void)do_intthrow(&cstack);
1281#endif /* FEAT_EVAL */
1282
1283 }
1284 /*
1285 * Continue executing command lines when:
1286 * - no CTRL-C typed, no aborting error, no exception thrown or try
1287 * conditionals need to be checked for executing finally clauses or
1288 * catching an interrupt exception
1289 * - didn't get an error message or lines are not typed
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001290 * - there is a command after '|', inside a :if, :while, :for or :try, or
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291 * looping for ":source" command or function call.
1292 */
1293 while (!((got_int
1294#ifdef FEAT_EVAL
1295 || (did_emsg && force_abort) || did_throw
1296#endif
1297 )
1298#ifdef FEAT_EVAL
1299 && cstack.cs_trylevel == 0
1300#endif
1301 )
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001302 && !(did_emsg
1303#ifdef FEAT_EVAL
1304 /* Keep going when inside try/catch, so that the error can be
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001305 * deal with, except when it is a syntax error, it may cause
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001306 * the :endtry to be missed. */
1307 && (cstack.cs_trylevel == 0 || did_emsg_syntax)
1308#endif
1309 && used_getline
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001310 && (getline_equal(fgetline, cookie, getexmodeline)
1311 || getline_equal(fgetline, cookie, getexline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312 && (next_cmdline != NULL
1313#ifdef FEAT_EVAL
1314 || cstack.cs_idx >= 0
1315#endif
1316 || (flags & DOCMD_REPEAT)));
1317
1318 vim_free(cmdline_copy);
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001319 did_emsg_syntax = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001320#ifdef FEAT_EVAL
1321 free_cmdlines(&lines_ga);
1322 ga_clear(&lines_ga);
1323
1324 if (cstack.cs_idx >= 0)
1325 {
1326 /*
1327 * If a sourced file or executed function ran to its end, report the
1328 * unclosed conditional.
1329 */
1330 if (!got_int && !did_throw
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001331 && ((getline_equal(fgetline, cookie, getsourceline)
1332 && !source_finished(fgetline, cookie))
1333 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001334 && !func_has_ended(real_cookie))))
1335 {
1336 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
1337 EMSG(_(e_endtry));
1338 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
1339 EMSG(_(e_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001340 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
1341 EMSG(_(e_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342 else
1343 EMSG(_(e_endif));
1344 }
1345
1346 /*
1347 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1348 * ":endtry" in a sourced file or executed function. If the try
1349 * conditional is in its finally clause, ignore anything pending.
1350 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001351 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352 */
1353 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001354 {
1355 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1356
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001357 if (idx >= 0)
1358 --idx; /* remove try block not in its finally clause */
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001359 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1360 &cstack.cs_looplevel);
1361 }
1362 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001363 trylevel = initial_trylevel;
1364 }
1365
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001366 /* If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1367 * lack was reported above and the error message is to be converted to an
Bram Moolenaar071d4272004-06-13 20:20:40 +00001368 * exception, do this now after rewinding the cstack. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001369 do_errthrow(&cstack, getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370 ? (char_u *)"endfunction" : (char_u *)NULL);
1371
1372 if (trylevel == 0)
1373 {
1374 /*
1375 * When an exception is being thrown out of the outermost try
1376 * conditional, discard the uncaught exception, disable the conversion
1377 * of interrupts or errors to exceptions, and ensure that no more
1378 * commands are executed.
1379 */
1380 if (did_throw)
1381 {
1382 void *p = NULL;
1383 char_u *saved_sourcing_name;
1384 int saved_sourcing_lnum;
1385 struct msglist *messages = NULL, *next;
1386
1387 /*
1388 * If the uncaught exception is a user exception, report it as an
1389 * error. If it is an error exception, display the saved error
1390 * message now. For an interrupt exception, do nothing; the
1391 * interrupt message is given elsewhere.
1392 */
1393 switch (current_exception->type)
1394 {
1395 case ET_USER:
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001396 vim_snprintf((char *)IObuff, IOSIZE,
1397 _("E605: Exception not caught: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398 current_exception->value);
1399 p = vim_strsave(IObuff);
1400 break;
1401 case ET_ERROR:
1402 messages = current_exception->messages;
1403 current_exception->messages = NULL;
1404 break;
1405 case ET_INTERRUPT:
1406 break;
1407 default:
1408 p = vim_strsave((char_u *)_(e_internal));
1409 }
1410
1411 saved_sourcing_name = sourcing_name;
1412 saved_sourcing_lnum = sourcing_lnum;
1413 sourcing_name = current_exception->throw_name;
1414 sourcing_lnum = current_exception->throw_lnum;
1415 current_exception->throw_name = NULL;
1416
1417 discard_current_exception(); /* uses IObuff if 'verbose' */
1418 suppress_errthrow = TRUE;
1419 force_abort = TRUE;
1420
1421 if (messages != NULL)
1422 {
1423 do
1424 {
1425 next = messages->next;
1426 emsg(messages->msg);
1427 vim_free(messages->msg);
1428 vim_free(messages);
1429 messages = next;
1430 }
1431 while (messages != NULL);
1432 }
1433 else if (p != NULL)
1434 {
1435 emsg(p);
1436 vim_free(p);
1437 }
1438 vim_free(sourcing_name);
1439 sourcing_name = saved_sourcing_name;
1440 sourcing_lnum = saved_sourcing_lnum;
1441 }
1442
1443 /*
1444 * On an interrupt or an aborting error not converted to an exception,
1445 * disable the conversion of errors to exceptions. (Interrupts are not
1446 * converted any more, here.) This enables also the interrupt message
1447 * when force_abort is set and did_emsg unset in case of an interrupt
1448 * from a finally clause after an error.
1449 */
1450 else if (got_int || (did_emsg && force_abort))
1451 suppress_errthrow = TRUE;
1452 }
1453
1454 /*
1455 * The current cstack will be freed when do_cmdline() returns. An uncaught
1456 * exception will have to be rethrown in the previous cstack. If a function
1457 * has just returned or a script file was just finished and the previous
1458 * cstack belongs to the same function or, respectively, script file, it
1459 * will have to be checked for finally clauses to be executed due to the
1460 * ":return" or ":finish". This is done in do_one_cmd().
1461 */
1462 if (did_throw)
1463 need_rethrow = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001464 if ((getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001465 && ex_nesting_level > source_level(real_cookie))
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001466 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001467 && ex_nesting_level > func_level(real_cookie) + 1))
1468 {
1469 if (!did_throw)
1470 check_cstack = TRUE;
1471 }
1472 else
1473 {
1474 /* When leaving a function, reduce nesting level. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001475 if (getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476 --ex_nesting_level;
1477 /*
1478 * Go to debug mode when returning from a function in which we are
1479 * single-stepping.
1480 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001481 if ((getline_equal(fgetline, cookie, getsourceline)
1482 || getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483 && ex_nesting_level + 1 <= debug_break_level)
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001484 do_debug(getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001485 ? (char_u *)_("End of sourced file")
1486 : (char_u *)_("End of function"));
1487 }
1488
1489 /*
1490 * Restore the exception environment (done after returning from the
1491 * debugger).
1492 */
1493 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001494 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001495
1496 msg_list = saved_msg_list;
1497#endif /* FEAT_EVAL */
1498
1499 /*
1500 * If there was too much output to fit on the command line, ask the user to
1501 * hit return before redrawing the screen. With the ":global" command we do
1502 * this only once after the command is finished.
1503 */
1504 if (did_inc)
1505 {
1506 --RedrawingDisabled;
1507 --no_wait_return;
1508 msg_scroll = FALSE;
1509
1510 /*
1511 * When just finished an ":if"-":else" which was typed, no need to
1512 * wait for hit-return. Also for an error situation.
1513 */
1514 if (retval == FAIL
1515#ifdef FEAT_EVAL
1516 || (did_endif && KeyTyped && !did_emsg)
1517#endif
1518 )
1519 {
1520 need_wait_return = FALSE;
1521 msg_didany = FALSE; /* don't wait when restarting edit */
1522 }
1523 else if (need_wait_return)
1524 {
1525 /*
1526 * The msg_start() above clears msg_didout. The wait_return we do
1527 * here should not overwrite the command that may be shown before
1528 * doing that.
1529 */
1530 msg_didout |= msg_didout_before_start;
1531 wait_return(FALSE);
1532 }
1533 }
1534
Bram Moolenaar2a942252012-11-28 23:03:07 +01001535#ifdef FEAT_EVAL
1536 did_endif = FALSE; /* in case do_cmdline used recursively */
1537#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001538 /*
1539 * Reset if_level, in case a sourced script file contains more ":if" than
1540 * ":endif" (could be ":if x | foo | endif").
1541 */
1542 if_level = 0;
Bram Moolenaar2e18a122012-11-28 19:10:54 +01001543#endif
Bram Moolenaard4ad0d42012-11-28 17:34:48 +01001544
Bram Moolenaar071d4272004-06-13 20:20:40 +00001545 --call_depth;
1546 return retval;
1547}
1548
1549#ifdef FEAT_EVAL
1550/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001551 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552 */
1553 static char_u *
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001554get_loop_line(c, cookie, indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001555 int c;
1556 void *cookie;
1557 int indent;
1558{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001559 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560 wcmd_T *wp;
1561 char_u *line;
1562
1563 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1564 {
1565 if (cp->repeating)
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001566 return NULL; /* trying to read past ":endwhile"/":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001568 /* First time inside the ":while"/":for": get line normally. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569 if (cp->getline == NULL)
1570 line = getcmdline(c, 0L, indent);
1571 else
1572 line = cp->getline(c, cp->cookie, indent);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001573 if (line != NULL && store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001574 ++cp->current_line;
1575
1576 return line;
1577 }
1578
1579 KeyTyped = FALSE;
1580 ++cp->current_line;
1581 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
1582 sourcing_lnum = wp->lnum;
1583 return vim_strsave(wp->line);
1584}
1585
1586/*
1587 * Store a line in "gap" so that a ":while" loop can execute it again.
1588 */
1589 static int
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001590store_loop_line(gap, line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591 garray_T *gap;
1592 char_u *line;
1593{
1594 if (ga_grow(gap, 1) == FAIL)
1595 return FAIL;
1596 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
1597 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = sourcing_lnum;
1598 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599 return OK;
1600}
1601
1602/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001603 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001604 */
1605 static void
1606free_cmdlines(gap)
1607 garray_T *gap;
1608{
1609 while (gap->ga_len > 0)
1610 {
1611 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1612 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613 }
1614}
1615#endif
1616
1617/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001618 * If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals
1619 * "func". * Otherwise return TRUE when "fgetline" equals "func".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621 int
Bram Moolenaar89d40322006-08-29 15:30:07 +00001622getline_equal(fgetline, cookie, func)
1623 char_u *(*fgetline) __ARGS((int, void *, int));
Bram Moolenaar78a15312009-05-15 19:33:18 +00001624 void *cookie UNUSED; /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625 char_u *(*func) __ARGS((int, void *, int));
1626{
1627#ifdef FEAT_EVAL
1628 char_u *(*gp) __ARGS((int, void *, int));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001629 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001630
Bram Moolenaar89d40322006-08-29 15:30:07 +00001631 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001632 * function that's originally used to obtain the lines. This may be
1633 * nested several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001634 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001635 cp = (struct loop_cookie *)cookie;
1636 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637 {
1638 gp = cp->getline;
1639 cp = cp->cookie;
1640 }
1641 return gp == func;
1642#else
Bram Moolenaar89d40322006-08-29 15:30:07 +00001643 return fgetline == func;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644#endif
1645}
1646
1647#if defined(FEAT_EVAL) || defined(FEAT_MBYTE) || defined(PROTO)
1648/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001649 * If "fgetline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001650 * getline function. Otherwise return "cookie".
1651 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001652 void *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001653getline_cookie(fgetline, cookie)
Bram Moolenaar78a15312009-05-15 19:33:18 +00001654 char_u *(*fgetline) __ARGS((int, void *, int)) UNUSED;
Bram Moolenaar89d40322006-08-29 15:30:07 +00001655 void *cookie; /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656{
1657# ifdef FEAT_EVAL
1658 char_u *(*gp) __ARGS((int, void *, int));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001659 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001660
Bram Moolenaar89d40322006-08-29 15:30:07 +00001661 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001662 * cookie that's originally used to obtain the lines. This may be nested
Bram Moolenaar071d4272004-06-13 20:20:40 +00001663 * several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001664 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001665 cp = (struct loop_cookie *)cookie;
1666 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667 {
1668 gp = cp->getline;
1669 cp = cp->cookie;
1670 }
1671 return cp;
1672# else
1673 return cookie;
1674# endif
1675}
1676#endif
1677
1678/*
1679 * Execute one Ex command.
1680 *
1681 * If 'sourcing' is TRUE, the command will be included in the error message.
1682 *
1683 * 1. skip comment lines and leading space
1684 * 2. handle command modifiers
1685 * 3. parse range
1686 * 4. parse command
1687 * 5. parse arguments
1688 * 6. switch on command name
1689 *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001690 * Note: "fgetline" can be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001691 *
1692 * This function may be called recursively!
1693 */
1694#if (_MSC_VER == 1200)
1695/*
Bram Moolenaared203462004-06-16 11:19:22 +00001696 * Avoid optimisation bug in VC++ version 6.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001697 */
Bram Moolenaar281bdce2005-01-25 21:53:18 +00001698 #pragma optimize( "g", off )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001699#endif
1700 static char_u *
1701do_one_cmd(cmdlinep, sourcing,
1702#ifdef FEAT_EVAL
1703 cstack,
1704#endif
Bram Moolenaar89d40322006-08-29 15:30:07 +00001705 fgetline, cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001706 char_u **cmdlinep;
1707 int sourcing;
1708#ifdef FEAT_EVAL
1709 struct condstack *cstack;
1710#endif
Bram Moolenaar89d40322006-08-29 15:30:07 +00001711 char_u *(*fgetline) __ARGS((int, void *, int));
1712 void *cookie; /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713{
1714 char_u *p;
1715 linenr_T lnum;
1716 long n;
1717 char_u *errormsg = NULL; /* error message */
1718 exarg_T ea; /* Ex command arguments */
1719 long verbose_save = -1;
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001720 int save_msg_scroll = msg_scroll;
1721 int save_msg_silent = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001722 int did_esilent = 0;
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001723#ifdef HAVE_SANDBOX
1724 int did_sandbox = FALSE;
1725#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001726 cmdmod_T save_cmdmod;
1727 int ni; /* set when Not Implemented */
1728
1729 vim_memset(&ea, 0, sizeof(ea));
1730 ea.line1 = 1;
1731 ea.line2 = 1;
1732#ifdef FEAT_EVAL
1733 ++ex_nesting_level;
1734#endif
1735
Bram Moolenaar21691f82012-12-05 19:13:18 +01001736 /* When the last file has not been edited :q has to be typed twice. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001737 if (quitmore
1738#ifdef FEAT_EVAL
1739 /* avoid that a function call in 'statusline' does this */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001740 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001741#endif
1742#ifdef FEAT_AUTOCMD
Bram Moolenaar21691f82012-12-05 19:13:18 +01001743 /* avoid that an autocommand, e.g. QuitPre, does this */
1744 && !getline_equal(fgetline, cookie, getnextac)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745#endif
1746 )
1747 --quitmore;
1748
1749 /*
1750 * Reset browse, confirm, etc.. They are restored when returning, for
1751 * recursive calls.
1752 */
1753 save_cmdmod = cmdmod;
1754 vim_memset(&cmdmod, 0, sizeof(cmdmod));
1755
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001756 /* "#!anything" is handled like a comment. */
1757 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1758 goto doend;
1759
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760 /*
1761 * Repeat until no more command modifiers are found.
1762 */
1763 ea.cmd = *cmdlinep;
1764 for (;;)
1765 {
1766/*
1767 * 1. skip comment lines and leading white space and colons
1768 */
1769 while (*ea.cmd == ' ' || *ea.cmd == '\t' || *ea.cmd == ':')
1770 ++ea.cmd;
1771
1772 /* in ex mode, an empty line works like :+ */
1773 if (*ea.cmd == NUL && exmode_active
Bram Moolenaar89d40322006-08-29 15:30:07 +00001774 && (getline_equal(fgetline, cookie, getexmodeline)
1775 || getline_equal(fgetline, cookie, getexline))
Bram Moolenaardf177f62005-02-22 08:39:57 +00001776 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777 {
1778 ea.cmd = (char_u *)"+";
1779 ex_pressedreturn = TRUE;
1780 }
1781
1782 /* ignore comment and empty lines */
Bram Moolenaarf998c042007-11-20 11:31:26 +00001783 if (*ea.cmd == '"')
1784 goto doend;
1785 if (*ea.cmd == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001786 {
1787 ex_pressedreturn = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001789 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001790
1791/*
1792 * 2. handle command modifiers.
1793 */
1794 p = ea.cmd;
1795 if (VIM_ISDIGIT(*ea.cmd))
1796 p = skipwhite(skipdigits(ea.cmd));
1797 switch (*p)
1798 {
1799 /* When adding an entry, also modify cmd_exists(). */
1800 case 'a': if (!checkforcmd(&ea.cmd, "aboveleft", 3))
1801 break;
1802#ifdef FEAT_WINDOWS
1803 cmdmod.split |= WSP_ABOVE;
1804#endif
1805 continue;
1806
1807 case 'b': if (checkforcmd(&ea.cmd, "belowright", 3))
1808 {
1809#ifdef FEAT_WINDOWS
1810 cmdmod.split |= WSP_BELOW;
1811#endif
1812 continue;
1813 }
1814 if (checkforcmd(&ea.cmd, "browse", 3))
1815 {
Bram Moolenaard812df62008-11-09 12:46:09 +00001816#ifdef FEAT_BROWSE_CMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817 cmdmod.browse = TRUE;
1818#endif
1819 continue;
1820 }
1821 if (!checkforcmd(&ea.cmd, "botright", 2))
1822 break;
1823#ifdef FEAT_WINDOWS
1824 cmdmod.split |= WSP_BOT;
1825#endif
1826 continue;
1827
1828 case 'c': if (!checkforcmd(&ea.cmd, "confirm", 4))
1829 break;
1830#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1831 cmdmod.confirm = TRUE;
1832#endif
1833 continue;
1834
1835 case 'k': if (checkforcmd(&ea.cmd, "keepmarks", 3))
1836 {
1837 cmdmod.keepmarks = TRUE;
1838 continue;
1839 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001840 if (checkforcmd(&ea.cmd, "keepalt", 5))
1841 {
1842 cmdmod.keepalt = TRUE;
1843 continue;
1844 }
Bram Moolenaara939e432013-11-09 05:30:26 +01001845 if (checkforcmd(&ea.cmd, "keeppatterns", 5))
1846 {
1847 cmdmod.keeppatterns = TRUE;
1848 continue;
1849 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001850 if (!checkforcmd(&ea.cmd, "keepjumps", 5))
1851 break;
1852 cmdmod.keepjumps = TRUE;
1853 continue;
1854
1855 /* ":hide" and ":hide | cmd" are not modifiers */
1856 case 'h': if (p != ea.cmd || !checkforcmd(&p, "hide", 3)
1857 || *p == NUL || ends_excmd(*p))
1858 break;
1859 ea.cmd = p;
1860 cmdmod.hide = TRUE;
1861 continue;
1862
1863 case 'l': if (checkforcmd(&ea.cmd, "lockmarks", 3))
1864 {
1865 cmdmod.lockmarks = TRUE;
1866 continue;
1867 }
1868
1869 if (!checkforcmd(&ea.cmd, "leftabove", 5))
1870 break;
1871#ifdef FEAT_WINDOWS
1872 cmdmod.split |= WSP_ABOVE;
1873#endif
1874 continue;
1875
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001876 case 'n': if (checkforcmd(&ea.cmd, "noautocmd", 3))
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001877 {
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001878#ifdef FEAT_AUTOCMD
1879 if (cmdmod.save_ei == NULL)
1880 {
1881 /* Set 'eventignore' to "all". Restore the
1882 * existing option value later. */
1883 cmdmod.save_ei = vim_strsave(p_ei);
1884 set_string_option_direct((char_u *)"ei", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001885 (char_u *)"all", OPT_FREE, SID_NONE);
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001886 }
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001887#endif
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001888 continue;
1889 }
1890 if (!checkforcmd(&ea.cmd, "noswapfile", 6))
1891 break;
1892 cmdmod.noswapfile = TRUE;
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001893 continue;
1894
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895 case 'r': if (!checkforcmd(&ea.cmd, "rightbelow", 6))
1896 break;
1897#ifdef FEAT_WINDOWS
1898 cmdmod.split |= WSP_BELOW;
1899#endif
1900 continue;
1901
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001902 case 's': if (checkforcmd(&ea.cmd, "sandbox", 3))
1903 {
1904#ifdef HAVE_SANDBOX
1905 if (!did_sandbox)
1906 ++sandbox;
1907 did_sandbox = TRUE;
1908#endif
1909 continue;
1910 }
1911 if (!checkforcmd(&ea.cmd, "silent", 3))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912 break;
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001913 if (save_msg_silent == -1)
1914 save_msg_silent = msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001915 ++msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001916 if (*ea.cmd == '!' && !vim_iswhite(ea.cmd[-1]))
1917 {
1918 /* ":silent!", but not "silent !cmd" */
1919 ea.cmd = skipwhite(ea.cmd + 1);
1920 ++emsg_silent;
1921 ++did_esilent;
1922 }
1923 continue;
1924
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001925 case 't': if (checkforcmd(&p, "tab", 3))
1926 {
1927#ifdef FEAT_WINDOWS
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001928 if (vim_isdigit(*ea.cmd))
1929 cmdmod.tab = atoi((char *)ea.cmd) + 1;
1930 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001931 cmdmod.tab = tabpage_index(curtab) + 1;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001932 ea.cmd = p;
1933#endif
1934 continue;
1935 }
1936 if (!checkforcmd(&ea.cmd, "topleft", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001937 break;
1938#ifdef FEAT_WINDOWS
1939 cmdmod.split |= WSP_TOP;
1940#endif
1941 continue;
1942
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001943 case 'u': if (!checkforcmd(&ea.cmd, "unsilent", 3))
1944 break;
1945 if (save_msg_silent == -1)
1946 save_msg_silent = msg_silent;
1947 msg_silent = 0;
1948 continue;
1949
Bram Moolenaar071d4272004-06-13 20:20:40 +00001950 case 'v': if (checkforcmd(&ea.cmd, "vertical", 4))
1951 {
1952#ifdef FEAT_VERTSPLIT
1953 cmdmod.split |= WSP_VERT;
1954#endif
1955 continue;
1956 }
1957 if (!checkforcmd(&p, "verbose", 4))
1958 break;
1959 if (verbose_save < 0)
1960 verbose_save = p_verbose;
Bram Moolenaared203462004-06-16 11:19:22 +00001961 if (vim_isdigit(*ea.cmd))
1962 p_verbose = atoi((char *)ea.cmd);
1963 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001964 p_verbose = 1;
1965 ea.cmd = p;
1966 continue;
1967 }
1968 break;
1969 }
1970
1971#ifdef FEAT_EVAL
1972 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
1973 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
1974#else
1975 ea.skip = (if_level > 0);
1976#endif
1977
1978#ifdef FEAT_EVAL
Bram Moolenaar05159a02005-02-26 23:04:13 +00001979# ifdef FEAT_PROFILE
1980 /* Count this line for profiling if ea.skip is FALSE. */
Bram Moolenaar371d5402006-03-20 21:47:49 +00001981 if (do_profiling == PROF_YES && !ea.skip)
Bram Moolenaar05159a02005-02-26 23:04:13 +00001982 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001983 if (getline_equal(fgetline, cookie, get_func_line))
1984 func_line_exec(getline_cookie(fgetline, cookie));
1985 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +00001986 script_line_exec();
1987 }
1988#endif
1989
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990 /* May go to debug mode. If this happens and the ">quit" debug command is
1991 * used, throw an interrupt exception and skip the next command. */
1992 dbg_check_breakpoint(&ea);
1993 if (!ea.skip && got_int)
1994 {
1995 ea.skip = TRUE;
1996 (void)do_intthrow(cstack);
1997 }
1998#endif
1999
2000/*
2001 * 3. parse a range specifier of the form: addr [,addr] [;addr] ..
2002 *
2003 * where 'addr' is:
2004 *
2005 * % (entire file)
2006 * $ [+-NUM]
2007 * 'x [+-NUM] (where x denotes a currently defined mark)
2008 * . [+-NUM]
2009 * [+-NUM]..
2010 * NUM
2011 *
2012 * The ea.cmd pointer is updated to point to the first character following the
2013 * range spec. If an initial address is found, but no second, the upper bound
2014 * is equal to the lower.
2015 */
2016
2017 /* repeat for all ',' or ';' separated addresses */
2018 for (;;)
2019 {
2020 ea.line1 = ea.line2;
2021 ea.line2 = curwin->w_cursor.lnum; /* default is current line number */
2022 ea.cmd = skipwhite(ea.cmd);
2023 lnum = get_address(&ea.cmd, ea.skip, ea.addr_count == 0);
2024 if (ea.cmd == NULL) /* error detected */
2025 goto doend;
2026 if (lnum == MAXLNUM)
2027 {
2028 if (*ea.cmd == '%') /* '%' - all lines */
2029 {
2030 ++ea.cmd;
2031 ea.line1 = 1;
2032 ea.line2 = curbuf->b_ml.ml_line_count;
2033 ++ea.addr_count;
2034 }
2035 /* '*' - visual area */
2036 else if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
2037 {
2038 pos_T *fp;
2039
2040 ++ea.cmd;
2041 if (!ea.skip)
2042 {
2043 fp = getmark('<', FALSE);
2044 if (check_mark(fp) == FAIL)
2045 goto doend;
2046 ea.line1 = fp->lnum;
2047 fp = getmark('>', FALSE);
2048 if (check_mark(fp) == FAIL)
2049 goto doend;
2050 ea.line2 = fp->lnum;
2051 ++ea.addr_count;
2052 }
2053 }
2054 }
2055 else
2056 ea.line2 = lnum;
2057 ea.addr_count++;
2058
2059 if (*ea.cmd == ';')
2060 {
2061 if (!ea.skip)
2062 curwin->w_cursor.lnum = ea.line2;
2063 }
2064 else if (*ea.cmd != ',')
2065 break;
2066 ++ea.cmd;
2067 }
2068
2069 /* One address given: set start and end lines */
2070 if (ea.addr_count == 1)
2071 {
2072 ea.line1 = ea.line2;
2073 /* ... but only implicit: really no address given */
2074 if (lnum == MAXLNUM)
2075 ea.addr_count = 0;
2076 }
2077
2078 /* Don't leave the cursor on an illegal line (caused by ';') */
2079 check_cursor_lnum();
2080
2081/*
2082 * 4. parse command
2083 */
2084
2085 /*
2086 * Skip ':' and any white space
2087 */
2088 ea.cmd = skipwhite(ea.cmd);
2089 while (*ea.cmd == ':')
2090 ea.cmd = skipwhite(ea.cmd + 1);
2091
2092 /*
2093 * If we got a line, but no command, then go to the line.
2094 * If we find a '|' or '\n' we set ea.nextcmd.
2095 */
2096 if (*ea.cmd == NUL || *ea.cmd == '"' ||
2097 (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
2098 {
2099 /*
2100 * strange vi behaviour:
2101 * ":3" jumps to line 3
2102 * ":3|..." prints line 3
2103 * ":|" prints current line
2104 */
2105 if (ea.skip) /* skip this if inside :if */
2106 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00002107 if (*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002108 {
2109 ea.cmdidx = CMD_print;
2110 ea.argt = RANGE+COUNT+TRLBAR;
2111 if ((errormsg = invalid_range(&ea)) == NULL)
2112 {
2113 correct_range(&ea);
2114 ex_print(&ea);
2115 }
2116 }
2117 else if (ea.addr_count != 0)
2118 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002119 if (ea.line2 > curbuf->b_ml.ml_line_count)
2120 {
2121 /* With '-' in 'cpoptions' a line number past the file is an
2122 * error, otherwise put it at the end of the file. */
2123 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
2124 ea.line2 = -1;
2125 else
2126 ea.line2 = curbuf->b_ml.ml_line_count;
2127 }
2128
2129 if (ea.line2 < 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002130 errormsg = (char_u *)_(e_invrange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002131 else
2132 {
2133 if (ea.line2 == 0)
2134 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135 else
2136 curwin->w_cursor.lnum = ea.line2;
2137 beginline(BL_SOL | BL_FIX);
2138 }
2139 }
2140 goto doend;
2141 }
2142
2143 /* Find the command and let "p" point to after it. */
2144 p = find_command(&ea, NULL);
2145
Bram Moolenaard5005162014-08-22 23:05:54 +02002146#ifdef FEAT_AUTOCMD
2147 /* If this looks like an undefined user command and there are CmdUndefined
2148 * autocommands defined, trigger the matching autocommands. */
2149 if (p != NULL && ea.cmdidx == CMD_SIZE && !ea.skip
2150 && ASCII_ISUPPER(*ea.cmd)
2151 && has_cmdundefined())
2152 {
Bram Moolenaard5005162014-08-22 23:05:54 +02002153 int ret;
2154
Bram Moolenaar5a31b462014-08-23 14:16:20 +02002155 p = ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02002156 while (ASCII_ISALNUM(*p))
2157 ++p;
Bram Moolenaar120f4a82014-09-09 12:22:06 +02002158 p = vim_strnsave(ea.cmd, (int)(p - ea.cmd));
Bram Moolenaard5005162014-08-22 23:05:54 +02002159 ret = apply_autocmds(EVENT_CMDUNDEFINED, p, p, TRUE, NULL);
2160 vim_free(p);
2161 if (ret && !aborting())
2162 p = find_command(&ea, NULL);
2163 }
2164#endif
2165
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166#ifdef FEAT_USR_CMDS
2167 if (p == NULL)
2168 {
2169 if (!ea.skip)
2170 errormsg = (char_u *)_("E464: Ambiguous use of user-defined command");
2171 goto doend;
2172 }
2173 /* Check for wrong commands. */
2174 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78)
2175 {
2176 errormsg = uc_fun_cmd();
2177 goto doend;
2178 }
2179#endif
2180 if (ea.cmdidx == CMD_SIZE)
2181 {
2182 if (!ea.skip)
2183 {
2184 STRCPY(IObuff, _("E492: Not an editor command"));
2185 if (!sourcing)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002186 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187 errormsg = IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02002188 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002189 }
2190 goto doend;
2191 }
2192
2193 ni = (
2194#ifdef FEAT_USR_CMDS
2195 !USER_CMDIDX(ea.cmdidx) &&
2196#endif
Bram Moolenaar3ebc1e52007-07-05 07:54:17 +00002197 (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00002198#ifdef HAVE_EX_SCRIPT_NI
2199 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
2200#endif
2201 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202
2203#ifndef FEAT_EVAL
2204 /*
2205 * When the expression evaluation is disabled, recognize the ":if" and
2206 * ":endif" commands and ignore everything in between it.
2207 */
2208 if (ea.cmdidx == CMD_if)
2209 ++if_level;
2210 if (if_level)
2211 {
2212 if (ea.cmdidx == CMD_endif)
2213 --if_level;
2214 goto doend;
2215 }
2216
2217#endif
2218
Bram Moolenaar196b3b02008-06-20 16:51:41 +00002219 /* forced commands */
2220 if (*p == '!' && ea.cmdidx != CMD_substitute
2221 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002222 {
2223 ++p;
2224 ea.forceit = TRUE;
2225 }
2226 else
2227 ea.forceit = FALSE;
2228
2229/*
2230 * 5. parse arguments
2231 */
2232#ifdef FEAT_USR_CMDS
2233 if (!USER_CMDIDX(ea.cmdidx))
2234#endif
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 Moolenaar071d4272004-06-13 20:20:40 +00002255# ifdef FEAT_USR_CMDS
2256 && !USER_CMDIDX(ea.cmdidx)
2257# endif
2258 )
2259 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002260 /* Command not allowed when editing the command line. */
2261#ifdef FEAT_CMDWIN
2262 if (cmdwin_type != 0)
2263 errormsg = (char_u *)_(e_cmdwin);
2264 else
2265#endif
2266 errormsg = (char_u *)_(e_secure);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002267 goto doend;
2268 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002269#ifdef FEAT_AUTOCMD
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002270 /* Disallow editing another buffer when "curbuf_lock" is set.
2271 * Do allow ":edit" (check for argument later).
2272 * Do allow ":checktime" (it's postponed). */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002273 if (!(ea.argt & CMDWIN)
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002274 && ea.cmdidx != CMD_edit
2275 && ea.cmdidx != CMD_checktime
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002276# ifdef FEAT_USR_CMDS
2277 && !USER_CMDIDX(ea.cmdidx)
2278# endif
2279 && curbuf_locked())
2280 goto doend;
2281#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282
2283 if (!ni && !(ea.argt & RANGE) && ea.addr_count > 0)
2284 {
2285 /* no range allowed */
2286 errormsg = (char_u *)_(e_norange);
2287 goto doend;
2288 }
2289 }
2290
2291 if (!ni && !(ea.argt & BANG) && ea.forceit) /* no <!> allowed */
2292 {
2293 errormsg = (char_u *)_(e_nobang);
2294 goto doend;
2295 }
2296
2297 /*
2298 * Don't complain about the range if it is not used
2299 * (could happen if line_count is accidentally set to 0).
2300 */
2301 if (!ea.skip && !ni)
2302 {
2303 /*
2304 * If the range is backwards, ask for confirmation and, if given, swap
2305 * ea.line1 & ea.line2 so it's forwards again.
2306 * When global command is busy, don't ask, will fail below.
2307 */
2308 if (!global_busy && ea.line1 > ea.line2)
2309 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002310 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002311 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002312 if (sourcing || exmode_active)
2313 {
2314 errormsg = (char_u *)_("E493: Backwards range given");
2315 goto doend;
2316 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317 if (ask_yesno((char_u *)
2318 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002319 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002320 }
2321 lnum = ea.line1;
2322 ea.line1 = ea.line2;
2323 ea.line2 = lnum;
2324 }
2325 if ((errormsg = invalid_range(&ea)) != NULL)
2326 goto doend;
2327 }
2328
2329 if ((ea.argt & NOTADR) && ea.addr_count == 0) /* default is 1, not cursor */
2330 ea.line2 = 1;
2331
2332 correct_range(&ea);
2333
2334#ifdef FEAT_FOLDING
2335 if (((ea.argt & WHOLEFOLD) || ea.addr_count >= 2) && !global_busy)
2336 {
2337 /* Put the first line at the start of a closed fold, put the last line
2338 * at the end of a closed fold. */
2339 (void)hasFolding(ea.line1, &ea.line1, NULL);
2340 (void)hasFolding(ea.line2, NULL, &ea.line2);
2341 }
2342#endif
2343
2344#ifdef FEAT_QUICKFIX
2345 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002346 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347 * option here, so things like % get expanded.
2348 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002349 p = replace_makeprg(&ea, p, cmdlinep);
2350 if (p == NULL)
2351 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352#endif
2353
2354 /*
2355 * Skip to start of argument.
2356 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2357 */
2358 if (ea.cmdidx == CMD_bang)
2359 ea.arg = p;
2360 else
2361 ea.arg = skipwhite(p);
2362
2363 /*
2364 * Check for "++opt=val" argument.
2365 * Must be first, allow ":w ++enc=utf8 !cmd"
2366 */
2367 if (ea.argt & ARGOPT)
2368 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2369 if (getargopt(&ea) == FAIL && !ni)
2370 {
2371 errormsg = (char_u *)_(e_invarg);
2372 goto doend;
2373 }
2374
2375 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2376 {
2377 if (*ea.arg == '>') /* append */
2378 {
2379 if (*++ea.arg != '>') /* typed wrong */
2380 {
2381 errormsg = (char_u *)_("E494: Use w or w>>");
2382 goto doend;
2383 }
2384 ea.arg = skipwhite(ea.arg + 1);
2385 ea.append = TRUE;
2386 }
2387 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
2388 {
2389 ++ea.arg;
2390 ea.usefilter = TRUE;
2391 }
2392 }
2393
2394 if (ea.cmdidx == CMD_read)
2395 {
2396 if (ea.forceit)
2397 {
2398 ea.usefilter = TRUE; /* :r! filter if ea.forceit */
2399 ea.forceit = FALSE;
2400 }
2401 else if (*ea.arg == '!') /* :r !filter */
2402 {
2403 ++ea.arg;
2404 ea.usefilter = TRUE;
2405 }
2406 }
2407
2408 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2409 {
2410 ea.amount = 1;
2411 while (*ea.arg == *ea.cmd) /* count number of '>' or '<' */
2412 {
2413 ++ea.arg;
2414 ++ea.amount;
2415 }
2416 ea.arg = skipwhite(ea.arg);
2417 }
2418
2419 /*
2420 * Check for "+command" argument, before checking for next command.
2421 * Don't do this for ":read !cmd" and ":write !cmd".
2422 */
2423 if ((ea.argt & EDITCMD) && !ea.usefilter)
2424 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2425
2426 /*
2427 * Check for '|' to separate commands and '"' to start comments.
2428 * Don't do this for ":read !cmd" and ":write !cmd".
2429 */
2430 if ((ea.argt & TRLBAR) && !ea.usefilter)
2431 separate_nextcmd(&ea);
2432
2433 /*
2434 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002435 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2436 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002438 else if (ea.cmdidx == CMD_bang
2439 || ea.cmdidx == CMD_global
2440 || ea.cmdidx == CMD_vglobal
2441 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002442 {
2443 for (p = ea.arg; *p; ++p)
2444 {
2445 /* Remove one backslash before a newline, so that it's possible to
2446 * pass a newline to the shell and also a newline that is preceded
2447 * with a backslash. This makes it impossible to end a shell
2448 * command in a backslash, but that doesn't appear useful.
2449 * Halving the number of backslashes is incompatible with previous
2450 * versions. */
2451 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002452 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453 else if (*p == '\n')
2454 {
2455 ea.nextcmd = p + 1;
2456 *p = NUL;
2457 break;
2458 }
2459 }
2460 }
2461
2462 if ((ea.argt & DFLALL) && ea.addr_count == 0)
2463 {
2464 ea.line1 = 1;
2465 ea.line2 = curbuf->b_ml.ml_line_count;
2466 }
2467
2468 /* accept numbered register only when no count allowed (:put) */
2469 if ( (ea.argt & REGSTR)
2470 && *ea.arg != NUL
2471#ifdef FEAT_USR_CMDS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472 /* Do not allow register = for user commands */
2473 && (!USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474#endif
2475 && !((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)))
2476 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002477#ifndef FEAT_CLIPBOARD
2478 /* check these explicitly for a more specific error message */
2479 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002481 errormsg = (char_u *)_(e_invalidreg);
2482 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483 }
2484#endif
Bram Moolenaar85de2062011-05-05 14:26:41 +02002485 if (
2486#ifdef FEAT_USR_CMDS
2487 valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2488 && USER_CMDIDX(ea.cmdidx)))
2489#else
2490 valid_yank_reg(*ea.arg, ea.cmdidx != CMD_put)
2491#endif
2492 )
2493 {
2494 ea.regname = *ea.arg++;
2495#ifdef FEAT_EVAL
2496 /* for '=' register: accept the rest of the line as an expression */
2497 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2498 {
2499 set_expr_line(vim_strsave(ea.arg));
2500 ea.arg += STRLEN(ea.arg);
2501 }
2502#endif
2503 ea.arg = skipwhite(ea.arg);
2504 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002505 }
2506
2507 /*
2508 * Check for a count. When accepting a BUFNAME, don't use "123foo" as a
2509 * count, it's a buffer name.
2510 */
2511 if ((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)
2512 && (!(ea.argt & BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
2513 || vim_iswhite(*p)))
2514 {
2515 n = getdigits(&ea.arg);
2516 ea.arg = skipwhite(ea.arg);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002517 if (n <= 0 && !ni && (ea.argt & ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002518 {
2519 errormsg = (char_u *)_(e_zerocount);
2520 goto doend;
2521 }
2522 if (ea.argt & NOTADR) /* e.g. :buffer 2, :sleep 3 */
2523 {
2524 ea.line2 = n;
2525 if (ea.addr_count == 0)
2526 ea.addr_count = 1;
2527 }
2528 else
2529 {
2530 ea.line1 = ea.line2;
2531 ea.line2 += n - 1;
2532 ++ea.addr_count;
2533 /*
2534 * Be vi compatible: no error message for out of range.
2535 */
2536 if (ea.line2 > curbuf->b_ml.ml_line_count)
2537 ea.line2 = curbuf->b_ml.ml_line_count;
2538 }
2539 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002540
2541 /*
2542 * Check for flags: 'l', 'p' and '#'.
2543 */
2544 if (ea.argt & EXFLAGS)
2545 get_flags(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546 /* no arguments allowed */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002547 if (!ni && !(ea.argt & EXTRA) && *ea.arg != NUL
Bram Moolenaara2031822006-03-07 22:29:51 +00002548 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002549 {
2550 errormsg = (char_u *)_(e_trailing);
2551 goto doend;
2552 }
2553
2554 if (!ni && (ea.argt & NEEDARG) && *ea.arg == NUL)
2555 {
2556 errormsg = (char_u *)_(e_argreq);
2557 goto doend;
2558 }
2559
2560#ifdef FEAT_EVAL
2561 /*
2562 * Skip the command when it's not going to be executed.
2563 * The commands like :if, :endif, etc. always need to be executed.
2564 * Also make an exception for commands that handle a trailing command
2565 * themselves.
2566 */
2567 if (ea.skip)
2568 {
2569 switch (ea.cmdidx)
2570 {
2571 /* commands that need evaluation */
2572 case CMD_while:
2573 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002574 case CMD_for:
2575 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576 case CMD_if:
2577 case CMD_elseif:
2578 case CMD_else:
2579 case CMD_endif:
2580 case CMD_try:
2581 case CMD_catch:
2582 case CMD_finally:
2583 case CMD_endtry:
2584 case CMD_function:
2585 break;
2586
2587 /* Commands that handle '|' themselves. Check: A command should
2588 * either have the TRLBAR flag, appear in this list or appear in
2589 * the list at ":help :bar". */
2590 case CMD_aboveleft:
2591 case CMD_and:
2592 case CMD_belowright:
2593 case CMD_botright:
2594 case CMD_browse:
2595 case CMD_call:
2596 case CMD_confirm:
2597 case CMD_delfunction:
2598 case CMD_djump:
2599 case CMD_dlist:
2600 case CMD_dsearch:
2601 case CMD_dsplit:
2602 case CMD_echo:
2603 case CMD_echoerr:
2604 case CMD_echomsg:
2605 case CMD_echon:
2606 case CMD_execute:
2607 case CMD_help:
2608 case CMD_hide:
2609 case CMD_ijump:
2610 case CMD_ilist:
2611 case CMD_isearch:
2612 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002613 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002614 case CMD_keepjumps:
2615 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01002616 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617 case CMD_leftabove:
2618 case CMD_let:
2619 case CMD_lockmarks:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002620 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002621 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002622 case CMD_mzscheme:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01002623 case CMD_noautocmd:
2624 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002625 case CMD_perl:
2626 case CMD_psearch:
2627 case CMD_python:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002628 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002629 case CMD_python3:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002630 case CMD_return:
2631 case CMD_rightbelow:
2632 case CMD_ruby:
2633 case CMD_silent:
2634 case CMD_smagic:
2635 case CMD_snomagic:
2636 case CMD_substitute:
2637 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002638 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639 case CMD_tcl:
2640 case CMD_throw:
2641 case CMD_tilde:
2642 case CMD_topleft:
2643 case CMD_unlet:
2644 case CMD_verbose:
2645 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002646 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002647 break;
2648
2649 default: goto doend;
2650 }
2651 }
2652#endif
2653
2654 if (ea.argt & XFILE)
2655 {
2656 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2657 goto doend;
2658 }
2659
2660#ifdef FEAT_LISTCMDS
2661 /*
2662 * Accept buffer name. Cannot be used at the same time with a buffer
2663 * number. Don't do this for a user command.
2664 */
2665 if ((ea.argt & BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
2666# ifdef FEAT_USR_CMDS
2667 && !USER_CMDIDX(ea.cmdidx)
2668# endif
2669 )
2670 {
2671 /*
2672 * :bdelete, :bwipeout and :bunload take several arguments, separated
2673 * by spaces: find next space (skipping over escaped characters).
2674 * The others take one argument: ignore trailing spaces.
2675 */
2676 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2677 || ea.cmdidx == CMD_bunload)
2678 p = skiptowhite_esc(ea.arg);
2679 else
2680 {
2681 p = ea.arg + STRLEN(ea.arg);
2682 while (p > ea.arg && vim_iswhite(p[-1]))
2683 --p;
2684 }
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002685 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & BUFUNL) != 0,
2686 FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002687 if (ea.line2 < 0) /* failed */
2688 goto doend;
2689 ea.addr_count = 1;
2690 ea.arg = skipwhite(p);
2691 }
2692#endif
2693
2694/*
2695 * 6. switch on command name
2696 *
2697 * The "ea" structure holds the arguments that can be used.
2698 */
2699 ea.cmdlinep = cmdlinep;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002700 ea.getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701 ea.cookie = cookie;
2702#ifdef FEAT_EVAL
2703 ea.cstack = cstack;
2704#endif
2705
2706#ifdef FEAT_USR_CMDS
2707 if (USER_CMDIDX(ea.cmdidx))
2708 {
2709 /*
2710 * Execute a user-defined command.
2711 */
2712 do_ucmd(&ea);
2713 }
2714 else
2715#endif
2716 {
2717 /*
2718 * Call the function to execute the command.
2719 */
2720 ea.errmsg = NULL;
2721 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2722 if (ea.errmsg != NULL)
2723 errormsg = (char_u *)_(ea.errmsg);
2724 }
2725
2726#ifdef FEAT_EVAL
2727 /*
2728 * If the command just executed called do_cmdline(), any throw or ":return"
2729 * or ":finish" encountered there must also check the cstack of the still
2730 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2731 * exception, or reanimate a returned function or finished script file and
2732 * return or finish it again.
2733 */
2734 if (need_rethrow)
2735 do_throw(cstack);
2736 else if (check_cstack)
2737 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002738 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002739 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002740 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002741 && current_func_returned())
2742 do_return(&ea, TRUE, FALSE, NULL);
2743 }
2744 need_rethrow = check_cstack = FALSE;
2745#endif
2746
2747doend:
2748 if (curwin->w_cursor.lnum == 0) /* can happen with zero line number */
2749 curwin->w_cursor.lnum = 1;
2750
2751 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2752 {
2753 if (sourcing)
2754 {
2755 if (errormsg != IObuff)
2756 {
2757 STRCPY(IObuff, errormsg);
2758 errormsg = IObuff;
2759 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002760 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002761 }
2762 emsg(errormsg);
2763 }
2764#ifdef FEAT_EVAL
2765 do_errthrow(cstack,
2766 (ea.cmdidx != CMD_SIZE
2767# ifdef FEAT_USR_CMDS
2768 && !USER_CMDIDX(ea.cmdidx)
2769# endif
2770 ) ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
2771#endif
2772
2773 if (verbose_save >= 0)
2774 p_verbose = verbose_save;
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00002775#ifdef FEAT_AUTOCMD
2776 if (cmdmod.save_ei != NULL)
2777 {
2778 /* Restore 'eventignore' to the value before ":noautocmd". */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002779 set_string_option_direct((char_u *)"ei", -1, cmdmod.save_ei,
2780 OPT_FREE, SID_NONE);
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00002781 free_string_option(cmdmod.save_ei);
2782 }
2783#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002784
2785 cmdmod = save_cmdmod;
2786
Bram Moolenaar8e258a42009-07-09 13:55:43 +00002787 if (save_msg_silent != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002788 {
2789 /* messages could be enabled for a serious error, need to check if the
2790 * counters don't become negative */
Bram Moolenaarbecf4282009-09-30 11:24:36 +00002791 if (!did_emsg || msg_silent > save_msg_silent)
Bram Moolenaar8e258a42009-07-09 13:55:43 +00002792 msg_silent = save_msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002793 emsg_silent -= did_esilent;
2794 if (emsg_silent < 0)
2795 emsg_silent = 0;
2796 /* Restore msg_scroll, it's set by file I/O commands, even when no
2797 * message is actually displayed. */
2798 msg_scroll = save_msg_scroll;
Bram Moolenaar77ab2802009-04-22 12:44:48 +00002799
2800 /* "silent reg" or "silent echo x" inside "redir" leaves msg_col
2801 * somewhere in the line. Put it back in the first column. */
2802 if (redirecting())
2803 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002804 }
2805
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002806#ifdef HAVE_SANDBOX
2807 if (did_sandbox)
2808 --sandbox;
2809#endif
2810
Bram Moolenaar071d4272004-06-13 20:20:40 +00002811 if (ea.nextcmd && *ea.nextcmd == NUL) /* not really a next command */
2812 ea.nextcmd = NULL;
2813
2814#ifdef FEAT_EVAL
2815 --ex_nesting_level;
2816#endif
2817
2818 return ea.nextcmd;
2819}
2820#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00002821 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002822#endif
2823
2824/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002825 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002826 * If there is a match advance "pp" to the argument and return TRUE.
2827 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002828 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00002829checkforcmd(pp, cmd, len)
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002830 char_u **pp; /* start of command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002831 char *cmd; /* name of command */
2832 int len; /* required length */
2833{
2834 int i;
2835
2836 for (i = 0; cmd[i] != NUL; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00002837 if (((char_u *)cmd)[i] != (*pp)[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00002838 break;
2839 if (i >= len && !isalpha((*pp)[i]))
2840 {
2841 *pp = skipwhite(*pp + i);
2842 return TRUE;
2843 }
2844 return FALSE;
2845}
2846
2847/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002848 * Append "cmd" to the error message in IObuff.
2849 * Takes care of limiting the length and handling 0xa0, which would be
2850 * invisible otherwise.
2851 */
2852 static void
2853append_command(cmd)
2854 char_u *cmd;
2855{
2856 char_u *s = cmd;
2857 char_u *d;
2858
2859 STRCAT(IObuff, ": ");
2860 d = IObuff + STRLEN(IObuff);
2861 while (*s != NUL && d - IObuff < IOSIZE - 7)
2862 {
2863 if (
2864#ifdef FEAT_MBYTE
2865 enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) :
2866#endif
2867 *s == 0xa0)
2868 {
2869 s +=
2870#ifdef FEAT_MBYTE
2871 enc_utf8 ? 2 :
2872#endif
2873 1;
2874 STRCPY(d, "<a0>");
2875 d += 4;
2876 }
2877 else
2878 MB_COPY_CHAR(s, d);
2879 }
2880 *d = NUL;
2881}
2882
2883/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002884 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002885 * Start of the name can be found at eap->cmd.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002886 * Returns pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002887 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002888 * Returns NULL for an ambiguous user command.
2889 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002890 static char_u *
2891find_command(eap, full)
2892 exarg_T *eap;
Bram Moolenaar78a15312009-05-15 19:33:18 +00002893 int *full UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002894{
2895 int len;
2896 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00002897 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002898
2899 /*
2900 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00002901 * Exceptions:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002902 * - the 'k' command can directly be followed by any character.
2903 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
2904 * but :sre[wind] is another command, as are :scrip[tnames],
2905 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00002906 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002907 */
2908 p = eap->cmd;
2909 if (*p == 'k')
2910 {
2911 eap->cmdidx = CMD_k;
2912 ++p;
2913 }
2914 else if (p[0] == 's'
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002915 && ((p[1] == 'c' && p[2] != 's' && p[2] != 'r'
2916 && p[3] != 'i' && p[4] != 'p')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002917 || p[1] == 'g'
2918 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
2919 || p[1] == 'I'
2920 || (p[1] == 'r' && p[2] != 'e')))
2921 {
2922 eap->cmdidx = CMD_substitute;
2923 ++p;
2924 }
2925 else
2926 {
2927 while (ASCII_ISALPHA(*p))
2928 ++p;
Bram Moolenaarb6590522010-07-21 16:00:43 +02002929 /* for python 3.x support ":py3", ":python3", ":py3file", etc. */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002930 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaarb6590522010-07-21 16:00:43 +02002931 while (ASCII_ISALNUM(*p))
2932 ++p;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02002933
Bram Moolenaar071d4272004-06-13 20:20:40 +00002934 /* check for non-alpha command */
2935 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
2936 ++p;
2937 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00002938 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
2939 {
2940 /* Check for ":dl", ":dell", etc. to ":deletel": that's
2941 * :delete with the 'l' flag. Same for 'p'. */
2942 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00002943 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00002944 break;
2945 if (i == len - 1)
2946 {
2947 --len;
2948 if (p[-1] == 'l')
2949 eap->flags |= EXFLAG_LIST;
2950 else
2951 eap->flags |= EXFLAG_PRINT;
2952 }
2953 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002954
2955 if (ASCII_ISLOWER(*eap->cmd))
2956 eap->cmdidx = cmdidxs[CharOrdLow(*eap->cmd)];
2957 else
2958 eap->cmdidx = cmdidxs[26];
2959
2960 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
2961 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
2962 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
2963 (size_t)len) == 0)
2964 {
2965#ifdef FEAT_EVAL
2966 if (full != NULL
2967 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
2968 *full = TRUE;
2969#endif
2970 break;
2971 }
2972
2973#ifdef FEAT_USR_CMDS
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01002974 /* Look for a user defined command as a last resort. Let ":Print" be
2975 * overruled by a user defined command. */
2976 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
2977 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002978 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002979 /* User defined commands may contain digits. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002980 while (ASCII_ISALNUM(*p))
2981 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002982 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002983 }
2984#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002985 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002986 eap->cmdidx = CMD_SIZE;
2987 }
2988
2989 return p;
2990}
2991
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002992#ifdef FEAT_USR_CMDS
2993/*
2994 * Search for a user command that matches "eap->cmd".
2995 * Return cmdidx in "eap->cmdidx", flags in "eap->argt", idx in "eap->useridx".
2996 * Return a pointer to just after the command.
2997 * Return NULL if there is no matching command.
2998 */
2999 static char_u *
3000find_ucmd(eap, p, full, xp, compl)
3001 exarg_T *eap;
3002 char_u *p; /* end of the command (possibly including count) */
3003 int *full; /* set to TRUE for a full match */
3004 expand_T *xp; /* used for completion, NULL otherwise */
3005 int *compl; /* completion flags or NULL */
3006{
3007 int len = (int)(p - eap->cmd);
3008 int j, k, matchlen = 0;
3009 ucmd_T *uc;
3010 int found = FALSE;
3011 int possible = FALSE;
3012 char_u *cp, *np; /* Point into typed cmd and test name */
3013 garray_T *gap;
3014 int amb_local = FALSE; /* Found ambiguous buffer-local command,
3015 only full match global is accepted. */
3016
3017 /*
3018 * Look for buffer-local user commands first, then global ones.
3019 */
3020 gap = &curbuf->b_ucmds;
3021 for (;;)
3022 {
3023 for (j = 0; j < gap->ga_len; ++j)
3024 {
3025 uc = USER_CMD_GA(gap, j);
3026 cp = eap->cmd;
3027 np = uc->uc_name;
3028 k = 0;
3029 while (k < len && *np != NUL && *cp++ == *np++)
3030 k++;
3031 if (k == len || (*np == NUL && vim_isdigit(eap->cmd[k])))
3032 {
3033 /* If finding a second match, the command is ambiguous. But
3034 * not if a buffer-local command wasn't a full match and a
3035 * global command is a full match. */
3036 if (k == len && found && *np != NUL)
3037 {
3038 if (gap == &ucmds)
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003039 return NULL;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003040 amb_local = TRUE;
3041 }
3042
3043 if (!found || (k == len && *np == NUL))
3044 {
3045 /* If we matched up to a digit, then there could
3046 * be another command including the digit that we
3047 * should use instead.
3048 */
3049 if (k == len)
3050 found = TRUE;
3051 else
3052 possible = TRUE;
3053
3054 if (gap == &ucmds)
3055 eap->cmdidx = CMD_USER;
3056 else
3057 eap->cmdidx = CMD_USER_BUF;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003058 eap->argt = (long)uc->uc_argt;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003059 eap->useridx = j;
3060
3061# ifdef FEAT_CMDL_COMPL
3062 if (compl != NULL)
3063 *compl = uc->uc_compl;
3064# ifdef FEAT_EVAL
3065 if (xp != NULL)
3066 {
3067 xp->xp_arg = uc->uc_compl_arg;
3068 xp->xp_scriptID = uc->uc_scriptID;
3069 }
3070# endif
3071# endif
3072 /* Do not search for further abbreviations
3073 * if this is an exact match. */
3074 matchlen = k;
3075 if (k == len && *np == NUL)
3076 {
3077 if (full != NULL)
3078 *full = TRUE;
3079 amb_local = FALSE;
3080 break;
3081 }
3082 }
3083 }
3084 }
3085
3086 /* Stop if we found a full match or searched all. */
3087 if (j < gap->ga_len || gap == &ucmds)
3088 break;
3089 gap = &ucmds;
3090 }
3091
3092 /* Only found ambiguous matches. */
3093 if (amb_local)
3094 {
3095 if (xp != NULL)
3096 xp->xp_context = EXPAND_UNSUCCESSFUL;
3097 return NULL;
3098 }
3099
3100 /* The match we found may be followed immediately by a number. Move "p"
3101 * back to point to it. */
3102 if (found || possible)
3103 return p + (matchlen - len);
3104 return p;
3105}
3106#endif
3107
Bram Moolenaar071d4272004-06-13 20:20:40 +00003108#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003109static struct cmdmod
3110{
3111 char *name;
3112 int minlen;
3113 int has_count; /* :123verbose :3tab */
3114} cmdmods[] = {
3115 {"aboveleft", 3, FALSE},
3116 {"belowright", 3, FALSE},
3117 {"botright", 2, FALSE},
3118 {"browse", 3, FALSE},
3119 {"confirm", 4, FALSE},
3120 {"hide", 3, FALSE},
3121 {"keepalt", 5, FALSE},
3122 {"keepjumps", 5, FALSE},
3123 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01003124 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003125 {"leftabove", 5, FALSE},
3126 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003127 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003128 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003129 {"rightbelow", 6, FALSE},
3130 {"sandbox", 3, FALSE},
3131 {"silent", 3, FALSE},
3132 {"tab", 3, TRUE},
3133 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003134 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003135 {"verbose", 4, TRUE},
3136 {"vertical", 4, FALSE},
3137};
3138
3139/*
3140 * Return length of a command modifier (including optional count).
3141 * Return zero when it's not a modifier.
3142 */
3143 int
3144modifier_len(cmd)
3145 char_u *cmd;
3146{
3147 int i, j;
3148 char_u *p = cmd;
3149
3150 if (VIM_ISDIGIT(*cmd))
3151 p = skipwhite(skipdigits(cmd));
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003152 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003153 {
3154 for (j = 0; p[j] != NUL; ++j)
3155 if (p[j] != cmdmods[i].name[j])
3156 break;
Bram Moolenaar2d473ab2013-06-12 17:12:24 +02003157 if (!ASCII_ISALPHA(p[j]) && j >= cmdmods[i].minlen
Bram Moolenaared53fb92007-11-24 20:50:24 +00003158 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003159 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003160 }
3161 return 0;
3162}
3163
Bram Moolenaar071d4272004-06-13 20:20:40 +00003164/*
3165 * Return > 0 if an Ex command "name" exists.
3166 * Return 2 if there is an exact match.
3167 * Return 3 if there is an ambiguous match.
3168 */
3169 int
3170cmd_exists(name)
3171 char_u *name;
3172{
3173 exarg_T ea;
3174 int full = FALSE;
3175 int i;
3176 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003177 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003178
3179 /* Check command modifiers. */
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003180 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003181 {
3182 for (j = 0; name[j] != NUL; ++j)
3183 if (name[j] != cmdmods[i].name[j])
3184 break;
3185 if (name[j] == NUL && j >= cmdmods[i].minlen)
3186 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3187 }
3188
Bram Moolenaara9587612006-05-04 21:47:50 +00003189 /* Check built-in commands and user defined commands.
3190 * For ":2match" and ":3match" we need to skip the number. */
3191 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003192 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003193 p = find_command(&ea, &full);
3194 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003196 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3197 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003198 if (*skipwhite(p) != NUL)
3199 return 0; /* trailing garbage */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3201}
3202#endif
3203
3204/*
3205 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
3206 * we don't need/want deleted. Maybe this could be done better if we didn't
3207 * repeat all this stuff. The only problem is that they may not stay
3208 * perfectly compatible with each other, but then the command line syntax
3209 * probably won't change that much -- webb.
3210 */
3211 char_u *
3212set_one_cmd_context(xp, buff)
3213 expand_T *xp;
3214 char_u *buff; /* buffer for command string */
3215{
3216 char_u *p;
3217 char_u *cmd, *arg;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003218 int len = 0;
3219 exarg_T ea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003220#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3221 int compl = EXPAND_NOTHING;
3222#endif
3223#ifdef FEAT_CMDL_COMPL
3224 int delim;
3225#endif
3226 int forceit = FALSE;
3227 int usefilter = FALSE; /* filter instead of file name */
3228
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003229 ExpandInit(xp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230 xp->xp_pattern = buff;
3231 xp->xp_context = EXPAND_COMMANDS; /* Default until we get past command */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003232 ea.argt = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003233
3234/*
3235 * 2. skip comment lines and leading space, colons or bars
3236 */
3237 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
3238 ;
3239 xp->xp_pattern = cmd;
3240
3241 if (*cmd == NUL)
3242 return NULL;
3243 if (*cmd == '"') /* ignore comment lines */
3244 {
3245 xp->xp_context = EXPAND_NOTHING;
3246 return NULL;
3247 }
3248
3249/*
3250 * 3. parse a range specifier of the form: addr [,addr] [;addr] ..
3251 */
3252 cmd = skip_range(cmd, &xp->xp_context);
3253
3254/*
3255 * 4. parse command
3256 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003257 xp->xp_pattern = cmd;
3258 if (*cmd == NUL)
3259 return NULL;
3260 if (*cmd == '"')
3261 {
3262 xp->xp_context = EXPAND_NOTHING;
3263 return NULL;
3264 }
3265
3266 if (*cmd == '|' || *cmd == '\n')
3267 return cmd + 1; /* There's another command */
3268
3269 /*
3270 * Isolate the command and search for it in the command table.
3271 * Exceptions:
3272 * - the 'k' command can directly be followed by any character, but
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003273 * do accept "keepmarks", "keepalt" and "keepjumps".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003274 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
3275 */
3276 if (*cmd == 'k' && cmd[1] != 'e')
3277 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003278 ea.cmdidx = CMD_k;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279 p = cmd + 1;
3280 }
3281 else
3282 {
3283 p = cmd;
3284 while (ASCII_ISALPHA(*p) || *p == '*') /* Allow * wild card */
3285 ++p;
3286 /* check for non-alpha command */
3287 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3288 ++p;
Bram Moolenaar9f5d6002013-06-02 19:22:13 +02003289 /* for python 3.x: ":py3*" commands completion */
3290 if (cmd[0] == 'p' && cmd[1] == 'y' && p == cmd + 2 && *p == '3')
Bram Moolenaar893b2d72013-12-11 17:44:38 +01003291 {
Bram Moolenaar9f5d6002013-06-02 19:22:13 +02003292 ++p;
Bram Moolenaar893b2d72013-12-11 17:44:38 +01003293 while (ASCII_ISALPHA(*p) || *p == '*')
3294 ++p;
3295 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003296 len = (int)(p - cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003298 if (len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003299 {
3300 xp->xp_context = EXPAND_UNSUCCESSFUL;
3301 return NULL;
3302 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003303 for (ea.cmdidx = (cmdidx_T)0; (int)ea.cmdidx < (int)CMD_SIZE;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01003304 ea.cmdidx = (cmdidx_T)((int)ea.cmdidx + 1))
3305 if (STRNCMP(cmdnames[(int)ea.cmdidx].cmd_name, cmd,
3306 (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003307 break;
3308
3309#ifdef FEAT_USR_CMDS
3310 if (cmd[0] >= 'A' && cmd[0] <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311 while (ASCII_ISALNUM(*p) || *p == '*') /* Allow * wild card */
3312 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313#endif
3314 }
3315
3316 /*
3317 * If the cursor is touching the command, and it ends in an alpha-numeric
3318 * character, complete the command name.
3319 */
3320 if (*p == NUL && ASCII_ISALNUM(p[-1]))
3321 return NULL;
3322
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003323 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324 {
3325 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
3326 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003327 ea.cmdidx = CMD_substitute;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328 p = cmd + 1;
3329 }
3330#ifdef FEAT_USR_CMDS
3331 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
3332 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003333 ea.cmd = cmd;
3334 p = find_ucmd(&ea, p, NULL, xp,
3335# if defined(FEAT_CMDL_COMPL)
3336 &compl
3337# else
3338 NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003339# endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003340 );
Bram Moolenaarebefac62005-12-28 22:39:57 +00003341 if (p == NULL)
3342 ea.cmdidx = CMD_SIZE; /* ambiguous user command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343 }
3344#endif
3345 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003346 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003347 {
3348 /* Not still touching the command and it was an illegal one */
3349 xp->xp_context = EXPAND_UNSUCCESSFUL;
3350 return NULL;
3351 }
3352
3353 xp->xp_context = EXPAND_NOTHING; /* Default now that we're past command */
3354
3355 if (*p == '!') /* forced commands */
3356 {
3357 forceit = TRUE;
3358 ++p;
3359 }
3360
3361/*
3362 * 5. parse arguments
3363 */
3364#ifdef FEAT_USR_CMDS
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003365 if (!USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366#endif
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003367 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368
3369 arg = skipwhite(p);
3370
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003371 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372 {
3373 if (*arg == '>') /* append */
3374 {
3375 if (*++arg == '>')
3376 ++arg;
3377 arg = skipwhite(arg);
3378 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003379 else if (*arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380 {
3381 ++arg;
3382 usefilter = TRUE;
3383 }
3384 }
3385
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003386 if (ea.cmdidx == CMD_read)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387 {
3388 usefilter = forceit; /* :r! filter if forced */
3389 if (*arg == '!') /* :r !filter */
3390 {
3391 ++arg;
3392 usefilter = TRUE;
3393 }
3394 }
3395
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003396 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397 {
3398 while (*arg == *cmd) /* allow any number of '>' or '<' */
3399 ++arg;
3400 arg = skipwhite(arg);
3401 }
3402
3403 /* Does command allow "+command"? */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003404 if ((ea.argt & EDITCMD) && !usefilter && *arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405 {
3406 /* Check if we're in the +command */
3407 p = arg + 1;
3408 arg = skip_cmd_arg(arg, FALSE);
3409
3410 /* Still touching the command after '+'? */
3411 if (*arg == NUL)
3412 return p;
3413
3414 /* Skip space(s) after +command to get to the real argument */
3415 arg = skipwhite(arg);
3416 }
3417
3418 /*
3419 * Check for '|' to separate commands and '"' to start comments.
3420 * Don't do this for ":read !cmd" and ":write !cmd".
3421 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003422 if ((ea.argt & TRLBAR) && !usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423 {
3424 p = arg;
3425 /* ":redir @" is not the start of a comment */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003426 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427 p += 2;
3428 while (*p)
3429 {
3430 if (*p == Ctrl_V)
3431 {
3432 if (p[1] != NUL)
3433 ++p;
3434 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003435 else if ( (*p == '"' && !(ea.argt & NOTRLCOM))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436 || *p == '|' || *p == '\n')
3437 {
3438 if (*(p - 1) != '\\')
3439 {
3440 if (*p == '|' || *p == '\n')
3441 return p + 1;
3442 return NULL; /* It's a comment */
3443 }
3444 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003445 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446 }
3447 }
3448
3449 /* no arguments allowed */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003450 if (!(ea.argt & EXTRA) && *arg != NUL &&
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451 vim_strchr((char_u *)"|\"", *arg) == NULL)
3452 return NULL;
3453
3454 /* Find start of last argument (argument just before cursor): */
Bram Moolenaar848f8762012-07-25 17:22:23 +02003455 p = buff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456 xp->xp_pattern = p;
Bram Moolenaar09168a72012-08-02 21:24:42 +02003457 len = (int)STRLEN(buff);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003458 while (*p && p < buff + len)
3459 {
3460 if (*p == ' ' || *p == TAB)
3461 {
3462 /* argument starts after a space */
3463 xp->xp_pattern = ++p;
3464 }
3465 else
3466 {
3467 if (*p == '\\' && *(p + 1) != NUL)
3468 ++p; /* skip over escaped character */
3469 mb_ptr_adv(p);
3470 }
3471 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003473 if (ea.argt & XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003475 int c;
3476 int in_quote = FALSE;
3477 char_u *bow = NULL; /* Beginning of word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478
3479 /*
3480 * Allow spaces within back-quotes to count as part of the argument
3481 * being expanded.
3482 */
3483 xp->xp_pattern = skipwhite(arg);
Bram Moolenaar6529c102007-08-18 15:47:34 +00003484 p = xp->xp_pattern;
3485 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003487#ifdef FEAT_MBYTE
3488 if (has_mbyte)
3489 c = mb_ptr2char(p);
3490 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491#endif
Bram Moolenaar6529c102007-08-18 15:47:34 +00003492 c = *p;
3493 if (c == '\\' && p[1] != NUL)
3494 ++p;
3495 else if (c == '`')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496 {
3497 if (!in_quote)
3498 {
3499 xp->xp_pattern = p;
3500 bow = p + 1;
3501 }
3502 in_quote = !in_quote;
3503 }
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003504 /* An argument can contain just about everything, except
3505 * characters that end the command and white space. */
3506 else if (c == '|' || c == '\n' || c == '"' || (vim_iswhite(c)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003507#ifdef SPACE_IN_FILENAME
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003508 && (!(ea.argt & NOSPC) || usefilter)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003509#endif
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003510 ))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003511 {
Bram Moolenaarcf5a5b82008-02-26 20:30:12 +00003512 len = 0; /* avoid getting stuck when space is in 'isfname' */
Bram Moolenaar6529c102007-08-18 15:47:34 +00003513 while (*p != NUL)
3514 {
3515#ifdef FEAT_MBYTE
3516 if (has_mbyte)
3517 c = mb_ptr2char(p);
3518 else
3519#endif
3520 c = *p;
Bram Moolenaardd87969c2007-08-21 13:07:12 +00003521 if (c == '`' || vim_isfilec_or_wc(c))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003522 break;
3523#ifdef FEAT_MBYTE
3524 if (has_mbyte)
3525 len = (*mb_ptr2len)(p);
3526 else
3527#endif
3528 len = 1;
3529 mb_ptr_adv(p);
3530 }
3531 if (in_quote)
3532 bow = p;
3533 else
3534 xp->xp_pattern = p;
3535 p -= len;
3536 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003537 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538 }
3539
3540 /*
3541 * If we are still inside the quotes, and we passed a space, just
3542 * expand from there.
3543 */
3544 if (bow != NULL && in_quote)
3545 xp->xp_pattern = bow;
3546 xp->xp_context = EXPAND_FILES;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003547
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003548 /* For a shell command more chars need to be escaped. */
3549 if (usefilter || ea.cmdidx == CMD_bang)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003550 {
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003551#ifndef BACKSLASH_IN_FILENAME
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003552 xp->xp_shell = TRUE;
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003553#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003554 /* When still after the command name expand executables. */
3555 if (xp->xp_pattern == skipwhite(arg))
Bram Moolenaar5c5b0942007-05-06 12:07:59 +00003556 xp->xp_context = EXPAND_SHELLCMD;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003557 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558
3559 /* Check for environment variable */
3560 if (*xp->xp_pattern == '$'
3561#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
3562 || *xp->xp_pattern == '%'
3563#endif
3564 )
3565 {
3566 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
3567 if (!vim_isIDc(*p))
3568 break;
3569 if (*p == NUL)
3570 {
3571 xp->xp_context = EXPAND_ENV_VARS;
3572 ++xp->xp_pattern;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003573#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3574 /* Avoid that the assignment uses EXPAND_FILES again. */
Bram Moolenaara466c992005-07-09 21:03:22 +00003575 if (compl != EXPAND_USER_DEFINED && compl != EXPAND_USER_LIST)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003576 compl = EXPAND_ENV_VARS;
3577#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578 }
3579 }
Bram Moolenaar24305862012-08-15 14:05:05 +02003580#if defined(FEAT_CMDL_COMPL)
3581 /* Check for user names */
3582 if (*xp->xp_pattern == '~')
3583 {
3584 for (p = xp->xp_pattern + 1; *p != NUL && *p != '/'; ++p)
3585 ;
3586 /* Complete ~user only if it partially matches a user name.
3587 * A full match ~user<Tab> will be replaced by user's home
3588 * directory i.e. something like ~user<Tab> -> /home/user/ */
3589 if (*p == NUL && p > xp->xp_pattern + 1
3590 && match_user(xp->xp_pattern + 1) == 1)
3591 {
3592 xp->xp_context = EXPAND_USER;
3593 ++xp->xp_pattern;
3594 }
3595 }
3596#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597 }
3598
3599/*
3600 * 6. switch on command name
3601 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003602 switch (ea.cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003604 case CMD_find:
3605 case CMD_sfind:
3606 case CMD_tabfind:
Bram Moolenaarc24b6972010-08-16 22:34:29 +02003607 if (xp->xp_context == EXPAND_FILES)
3608 xp->xp_context = EXPAND_FILES_IN_PATH;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003609 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610 case CMD_cd:
3611 case CMD_chdir:
3612 case CMD_lcd:
3613 case CMD_lchdir:
3614 if (xp->xp_context == EXPAND_FILES)
3615 xp->xp_context = EXPAND_DIRECTORIES;
3616 break;
3617 case CMD_help:
3618 xp->xp_context = EXPAND_HELP;
3619 xp->xp_pattern = arg;
3620 break;
3621
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003622 /* Command modifiers: return the argument.
3623 * Also for commands with an argument that is a command. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624 case CMD_aboveleft:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003625 case CMD_argdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626 case CMD_belowright:
3627 case CMD_botright:
3628 case CMD_browse:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003629 case CMD_bufdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630 case CMD_confirm:
Bram Moolenaardf177f62005-02-22 08:39:57 +00003631 case CMD_debug:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003632 case CMD_folddoclosed:
3633 case CMD_folddoopen:
3634 case CMD_hide:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003635 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636 case CMD_keepjumps:
3637 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01003638 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639 case CMD_leftabove:
3640 case CMD_lockmarks:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003641 case CMD_noautocmd:
3642 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643 case CMD_rightbelow:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003644 case CMD_sandbox:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645 case CMD_silent:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003646 case CMD_tab:
Bram Moolenaar70baa402013-06-16 16:14:03 +02003647 case CMD_tabdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003648 case CMD_topleft:
3649 case CMD_verbose:
3650 case CMD_vertical:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003651 case CMD_windo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003652 return arg;
3653
Bram Moolenaar4f688582007-07-24 12:34:30 +00003654#ifdef FEAT_CMDL_COMPL
3655# ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar071d4272004-06-13 20:20:40 +00003656 case CMD_match:
3657 if (*arg == NUL || !ends_excmd(*arg))
3658 {
Bram Moolenaar4f688582007-07-24 12:34:30 +00003659 /* also complete "None" */
3660 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003661 arg = skipwhite(skiptowhite(arg));
3662 if (*arg != NUL)
3663 {
3664 xp->xp_context = EXPAND_NOTHING;
3665 arg = skip_regexp(arg + 1, *arg, p_magic, NULL);
3666 }
3667 }
3668 return find_nextcmd(arg);
Bram Moolenaar4f688582007-07-24 12:34:30 +00003669# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003670
Bram Moolenaar071d4272004-06-13 20:20:40 +00003671/*
3672 * All completion for the +cmdline_compl feature goes here.
3673 */
3674
3675# ifdef FEAT_USR_CMDS
3676 case CMD_command:
3677 /* Check for attributes */
3678 while (*arg == '-')
3679 {
3680 arg++; /* Skip "-" */
3681 p = skiptowhite(arg);
3682 if (*p == NUL)
3683 {
3684 /* Cursor is still in the attribute */
3685 p = vim_strchr(arg, '=');
3686 if (p == NULL)
3687 {
3688 /* No "=", so complete attribute names */
3689 xp->xp_context = EXPAND_USER_CMD_FLAGS;
3690 xp->xp_pattern = arg;
3691 return NULL;
3692 }
3693
3694 /* For the -complete and -nargs attributes, we complete
3695 * their arguments as well.
3696 */
3697 if (STRNICMP(arg, "complete", p - arg) == 0)
3698 {
3699 xp->xp_context = EXPAND_USER_COMPLETE;
3700 xp->xp_pattern = p + 1;
3701 return NULL;
3702 }
3703 else if (STRNICMP(arg, "nargs", p - arg) == 0)
3704 {
3705 xp->xp_context = EXPAND_USER_NARGS;
3706 xp->xp_pattern = p + 1;
3707 return NULL;
3708 }
3709 return NULL;
3710 }
3711 arg = skipwhite(p);
3712 }
3713
3714 /* After the attributes comes the new command name */
3715 p = skiptowhite(arg);
3716 if (*p == NUL)
3717 {
3718 xp->xp_context = EXPAND_USER_COMMANDS;
3719 xp->xp_pattern = arg;
3720 break;
3721 }
3722
3723 /* And finally comes a normal command */
3724 return skipwhite(p);
3725
3726 case CMD_delcommand:
3727 xp->xp_context = EXPAND_USER_COMMANDS;
3728 xp->xp_pattern = arg;
3729 break;
3730# endif
3731
3732 case CMD_global:
3733 case CMD_vglobal:
3734 delim = *arg; /* get the delimiter */
3735 if (delim)
3736 ++arg; /* skip delimiter if there is one */
3737
3738 while (arg[0] != NUL && arg[0] != delim)
3739 {
3740 if (arg[0] == '\\' && arg[1] != NUL)
3741 ++arg;
3742 ++arg;
3743 }
3744 if (arg[0] != NUL)
3745 return arg + 1;
3746 break;
3747 case CMD_and:
3748 case CMD_substitute:
3749 delim = *arg;
3750 if (delim)
3751 {
3752 /* skip "from" part */
3753 ++arg;
3754 arg = skip_regexp(arg, delim, p_magic, NULL);
3755 }
3756 /* skip "to" part */
3757 while (arg[0] != NUL && arg[0] != delim)
3758 {
3759 if (arg[0] == '\\' && arg[1] != NUL)
3760 ++arg;
3761 ++arg;
3762 }
3763 if (arg[0] != NUL) /* skip delimiter */
3764 ++arg;
3765 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
3766 ++arg;
3767 if (arg[0] != NUL)
3768 return arg;
3769 break;
3770 case CMD_isearch:
3771 case CMD_dsearch:
3772 case CMD_ilist:
3773 case CMD_dlist:
3774 case CMD_ijump:
3775 case CMD_psearch:
3776 case CMD_djump:
3777 case CMD_isplit:
3778 case CMD_dsplit:
3779 arg = skipwhite(skipdigits(arg)); /* skip count */
3780 if (*arg == '/') /* Match regexp, not just whole words */
3781 {
3782 for (++arg; *arg && *arg != '/'; arg++)
3783 if (*arg == '\\' && arg[1] != NUL)
3784 arg++;
3785 if (*arg)
3786 {
3787 arg = skipwhite(arg + 1);
3788
3789 /* Check for trailing illegal characters */
3790 if (*arg && vim_strchr((char_u *)"|\"\n", *arg) == NULL)
3791 xp->xp_context = EXPAND_NOTHING;
3792 else
3793 return arg;
3794 }
3795 }
3796 break;
3797#ifdef FEAT_AUTOCMD
3798 case CMD_autocmd:
3799 return set_context_in_autocmd(xp, arg, FALSE);
3800
3801 case CMD_doautocmd:
Bram Moolenaar73a9d7b2008-11-06 16:16:44 +00003802 case CMD_doautoall:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803 return set_context_in_autocmd(xp, arg, TRUE);
3804#endif
3805 case CMD_set:
3806 set_context_in_set_cmd(xp, arg, 0);
3807 break;
3808 case CMD_setglobal:
3809 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
3810 break;
3811 case CMD_setlocal:
3812 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
3813 break;
3814 case CMD_tag:
3815 case CMD_stag:
3816 case CMD_ptag:
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00003817 case CMD_ltag:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818 case CMD_tselect:
3819 case CMD_stselect:
3820 case CMD_ptselect:
3821 case CMD_tjump:
3822 case CMD_stjump:
3823 case CMD_ptjump:
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003824 if (*p_wop != NUL)
3825 xp->xp_context = EXPAND_TAGS_LISTFILES;
3826 else
3827 xp->xp_context = EXPAND_TAGS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003828 xp->xp_pattern = arg;
3829 break;
3830 case CMD_augroup:
3831 xp->xp_context = EXPAND_AUGROUP;
3832 xp->xp_pattern = arg;
3833 break;
3834#ifdef FEAT_SYN_HL
3835 case CMD_syntax:
3836 set_context_in_syntax_cmd(xp, arg);
3837 break;
3838#endif
3839#ifdef FEAT_EVAL
3840 case CMD_let:
3841 case CMD_if:
3842 case CMD_elseif:
3843 case CMD_while:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00003844 case CMD_for:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003845 case CMD_echo:
3846 case CMD_echon:
3847 case CMD_execute:
3848 case CMD_echomsg:
3849 case CMD_echoerr:
3850 case CMD_call:
3851 case CMD_return:
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003852 set_context_for_expression(xp, arg, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003853 break;
3854
3855 case CMD_unlet:
3856 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3857 arg = xp->xp_pattern + 1;
3858 xp->xp_context = EXPAND_USER_VARS;
3859 xp->xp_pattern = arg;
3860 break;
3861
3862 case CMD_function:
3863 case CMD_delfunction:
3864 xp->xp_context = EXPAND_USER_FUNC;
3865 xp->xp_pattern = arg;
3866 break;
3867
3868 case CMD_echohl:
Bram Moolenaar4f688582007-07-24 12:34:30 +00003869 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870 break;
3871#endif
3872 case CMD_highlight:
3873 set_context_in_highlight_cmd(xp, arg);
3874 break;
Bram Moolenaarf4580d82009-03-18 11:52:53 +00003875#ifdef FEAT_CSCOPE
3876 case CMD_cscope:
Bram Moolenaar7bfef802009-04-22 14:25:01 +00003877 case CMD_lcscope:
3878 case CMD_scscope:
3879 set_context_in_cscope_cmd(xp, arg, ea.cmdidx);
Bram Moolenaarf4580d82009-03-18 11:52:53 +00003880 break;
3881#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00003882#ifdef FEAT_SIGNS
3883 case CMD_sign:
3884 set_context_in_sign_cmd(xp, arg);
3885 break;
3886#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887#ifdef FEAT_LISTCMDS
3888 case CMD_bdelete:
3889 case CMD_bwipeout:
3890 case CMD_bunload:
3891 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3892 arg = xp->xp_pattern + 1;
3893 /*FALLTHROUGH*/
3894 case CMD_buffer:
3895 case CMD_sbuffer:
3896 case CMD_checktime:
3897 xp->xp_context = EXPAND_BUFFERS;
3898 xp->xp_pattern = arg;
3899 break;
3900#endif
3901#ifdef FEAT_USR_CMDS
3902 case CMD_USER:
3903 case CMD_USER_BUF:
3904 if (compl != EXPAND_NOTHING)
3905 {
3906 /* XFILE: file names are handled above */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003907 if (!(ea.argt & XFILE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003908 {
3909# ifdef FEAT_MENU
3910 if (compl == EXPAND_MENUS)
3911 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
3912# endif
3913 if (compl == EXPAND_COMMANDS)
3914 return arg;
3915 if (compl == EXPAND_MAPPINGS)
3916 return set_context_in_map_cmd(xp, (char_u *)"map",
3917 arg, forceit, FALSE, FALSE, CMD_map);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003918 /* Find start of last argument. */
3919 p = arg;
3920 while (*p)
3921 {
3922 if (*p == ' ')
Bram Moolenaar848f8762012-07-25 17:22:23 +02003923 /* argument starts after a space */
3924 arg = p + 1;
Bram Moolenaara07c8312012-07-27 21:12:07 +02003925 else if (*p == '\\' && *(p + 1) != NUL)
3926 ++p; /* skip over escaped character */
3927 mb_ptr_adv(p);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003928 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929 xp->xp_pattern = arg;
3930 }
3931 xp->xp_context = compl;
3932 }
3933 break;
3934#endif
3935 case CMD_map: case CMD_noremap:
3936 case CMD_nmap: case CMD_nnoremap:
3937 case CMD_vmap: case CMD_vnoremap:
3938 case CMD_omap: case CMD_onoremap:
3939 case CMD_imap: case CMD_inoremap:
3940 case CMD_cmap: case CMD_cnoremap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003941 case CMD_lmap: case CMD_lnoremap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02003942 case CMD_smap: case CMD_snoremap:
3943 case CMD_xmap: case CMD_xnoremap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003944 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003945 FALSE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946 case CMD_unmap:
3947 case CMD_nunmap:
3948 case CMD_vunmap:
3949 case CMD_ounmap:
3950 case CMD_iunmap:
3951 case CMD_cunmap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003952 case CMD_lunmap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02003953 case CMD_sunmap:
3954 case CMD_xunmap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003955 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003956 FALSE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003957 case CMD_abbreviate: case CMD_noreabbrev:
3958 case CMD_cabbrev: case CMD_cnoreabbrev:
3959 case CMD_iabbrev: case CMD_inoreabbrev:
3960 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003961 TRUE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003962 case CMD_unabbreviate:
3963 case CMD_cunabbrev:
3964 case CMD_iunabbrev:
3965 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003966 TRUE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003967#ifdef FEAT_MENU
3968 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
3969 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
3970 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
3971 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
3972 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
3973 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
3974 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
3975 case CMD_tmenu: case CMD_tunmenu:
3976 case CMD_popup: case CMD_tearoff: case CMD_emenu:
3977 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
3978#endif
3979
3980 case CMD_colorscheme:
3981 xp->xp_context = EXPAND_COLORS;
3982 xp->xp_pattern = arg;
3983 break;
3984
3985 case CMD_compiler:
3986 xp->xp_context = EXPAND_COMPILER;
3987 xp->xp_pattern = arg;
3988 break;
3989
Bram Moolenaar883f5d02010-06-21 06:24:34 +02003990 case CMD_ownsyntax:
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02003991 xp->xp_context = EXPAND_OWNSYNTAX;
3992 xp->xp_pattern = arg;
3993 break;
3994
3995 case CMD_setfiletype:
Bram Moolenaar883f5d02010-06-21 06:24:34 +02003996 xp->xp_context = EXPAND_FILETYPE;
3997 xp->xp_pattern = arg;
3998 break;
3999
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
4001 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
4002 case CMD_language:
Bram Moolenaara660dc82011-05-25 12:51:22 +02004003 p = skiptowhite(arg);
4004 if (*p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005 {
4006 xp->xp_context = EXPAND_LANGUAGE;
4007 xp->xp_pattern = arg;
4008 }
4009 else
Bram Moolenaara660dc82011-05-25 12:51:22 +02004010 {
4011 if ( STRNCMP(arg, "messages", p - arg) == 0
4012 || STRNCMP(arg, "ctype", p - arg) == 0
4013 || STRNCMP(arg, "time", p - arg) == 0)
4014 {
4015 xp->xp_context = EXPAND_LOCALES;
4016 xp->xp_pattern = skipwhite(p);
4017 }
4018 else
4019 xp->xp_context = EXPAND_NOTHING;
4020 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004021 break;
4022#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01004023#if defined(FEAT_PROFILE)
4024 case CMD_profile:
4025 set_context_in_profile_cmd(xp, arg);
4026 break;
4027#endif
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01004028 case CMD_behave:
4029 xp->xp_context = EXPAND_BEHAVE;
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004030 xp->xp_pattern = arg;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01004031 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004032
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004033#if defined(FEAT_CMDHIST)
4034 case CMD_history:
4035 xp->xp_context = EXPAND_HISTORY;
4036 xp->xp_pattern = arg;
4037 break;
4038#endif
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02004039#if defined(FEAT_PROFILE)
4040 case CMD_syntime:
4041 xp->xp_context = EXPAND_SYNTIME;
4042 xp->xp_pattern = arg;
4043 break;
4044#endif
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004045
Bram Moolenaar071d4272004-06-13 20:20:40 +00004046#endif /* FEAT_CMDL_COMPL */
4047
4048 default:
4049 break;
4050 }
4051 return NULL;
4052}
4053
4054/*
4055 * skip a range specifier of the form: addr [,addr] [;addr] ..
4056 *
4057 * Backslashed delimiters after / or ? will be skipped, and commands will
4058 * not be expanded between /'s and ?'s or after "'".
4059 *
Bram Moolenaardf177f62005-02-22 08:39:57 +00004060 * Also skip white space and ":" characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061 * Returns the "cmd" pointer advanced to beyond the range.
4062 */
4063 char_u *
4064skip_range(cmd, ctx)
4065 char_u *cmd;
4066 int *ctx; /* pointer to xp_context or NULL */
4067{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004068 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069
Bram Moolenaardf177f62005-02-22 08:39:57 +00004070 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071 {
4072 if (*cmd == '\'')
4073 {
4074 if (*++cmd == NUL && ctx != NULL)
4075 *ctx = EXPAND_NOTHING;
4076 }
4077 else if (*cmd == '/' || *cmd == '?')
4078 {
4079 delim = *cmd++;
4080 while (*cmd != NUL && *cmd != delim)
4081 if (*cmd++ == '\\' && *cmd != NUL)
4082 ++cmd;
4083 if (*cmd == NUL && ctx != NULL)
4084 *ctx = EXPAND_NOTHING;
4085 }
4086 if (*cmd != NUL)
4087 ++cmd;
4088 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004089
4090 /* Skip ":" and white space. */
4091 while (*cmd == ':')
4092 cmd = skipwhite(cmd + 1);
4093
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094 return cmd;
4095}
4096
4097/*
4098 * get a single EX address
4099 *
4100 * Set ptr to the next character after the part that was interpreted.
4101 * Set ptr to NULL when an error is encountered.
4102 *
4103 * Return MAXLNUM when no Ex address was found.
4104 */
4105 static linenr_T
4106get_address(ptr, skip, to_other_file)
4107 char_u **ptr;
4108 int skip; /* only skip the address, don't use it */
4109 int to_other_file; /* flag: may jump to other file */
4110{
4111 int c;
4112 int i;
4113 long n;
4114 char_u *cmd;
4115 pos_T pos;
4116 pos_T *fp;
4117 linenr_T lnum;
4118
4119 cmd = skipwhite(*ptr);
4120 lnum = MAXLNUM;
4121 do
4122 {
4123 switch (*cmd)
4124 {
4125 case '.': /* '.' - Cursor position */
4126 ++cmd;
4127 lnum = curwin->w_cursor.lnum;
4128 break;
4129
4130 case '$': /* '$' - last line */
4131 ++cmd;
4132 lnum = curbuf->b_ml.ml_line_count;
4133 break;
4134
4135 case '\'': /* ''' - mark */
4136 if (*++cmd == NUL)
4137 {
4138 cmd = NULL;
4139 goto error;
4140 }
4141 if (skip)
4142 ++cmd;
4143 else
4144 {
4145 /* Only accept a mark in another file when it is
4146 * used by itself: ":'M". */
4147 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
4148 ++cmd;
4149 if (fp == (pos_T *)-1)
4150 /* Jumped to another file. */
4151 lnum = curwin->w_cursor.lnum;
4152 else
4153 {
4154 if (check_mark(fp) == FAIL)
4155 {
4156 cmd = NULL;
4157 goto error;
4158 }
4159 lnum = fp->lnum;
4160 }
4161 }
4162 break;
4163
4164 case '/':
4165 case '?': /* '/' or '?' - search */
4166 c = *cmd++;
4167 if (skip) /* skip "/pat/" */
4168 {
4169 cmd = skip_regexp(cmd, c, (int)p_magic, NULL);
4170 if (*cmd == c)
4171 ++cmd;
4172 }
4173 else
4174 {
4175 pos = curwin->w_cursor; /* save curwin->w_cursor */
4176 /*
4177 * When '/' or '?' follows another address, start
4178 * from there.
4179 */
4180 if (lnum != MAXLNUM)
4181 curwin->w_cursor.lnum = lnum;
4182 /*
4183 * Start a forward search at the end of the line.
4184 * Start a backward search at the start of the line.
4185 * This makes sure we never match in the current
4186 * line, and can match anywhere in the
4187 * next/previous line.
4188 */
4189 if (c == '/')
4190 curwin->w_cursor.col = MAXCOL;
4191 else
4192 curwin->w_cursor.col = 0;
4193 searchcmdlen = 0;
4194 if (!do_search(NULL, c, cmd, 1L,
Bram Moolenaaraad86642008-03-10 20:34:59 +00004195 SEARCH_HIS | SEARCH_MSG, NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004196 {
4197 curwin->w_cursor = pos;
4198 cmd = NULL;
4199 goto error;
4200 }
4201 lnum = curwin->w_cursor.lnum;
4202 curwin->w_cursor = pos;
4203 /* adjust command string pointer */
4204 cmd += searchcmdlen;
4205 }
4206 break;
4207
4208 case '\\': /* "\?", "\/" or "\&", repeat search */
4209 ++cmd;
4210 if (*cmd == '&')
4211 i = RE_SUBST;
4212 else if (*cmd == '?' || *cmd == '/')
4213 i = RE_SEARCH;
4214 else
4215 {
4216 EMSG(_(e_backslash));
4217 cmd = NULL;
4218 goto error;
4219 }
4220
4221 if (!skip)
4222 {
4223 /*
4224 * When search follows another address, start from
4225 * there.
4226 */
4227 if (lnum != MAXLNUM)
4228 pos.lnum = lnum;
4229 else
4230 pos.lnum = curwin->w_cursor.lnum;
4231
4232 /*
4233 * Start the search just like for the above
4234 * do_search().
4235 */
4236 if (*cmd != '?')
4237 pos.col = MAXCOL;
4238 else
4239 pos.col = 0;
4240 if (searchit(curwin, curbuf, &pos,
4241 *cmd == '?' ? BACKWARD : FORWARD,
Bram Moolenaaraad86642008-03-10 20:34:59 +00004242 (char_u *)"", 1L, SEARCH_MSG,
Bram Moolenaar76929292008-01-06 19:07:36 +00004243 i, (linenr_T)0, NULL) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244 lnum = pos.lnum;
4245 else
4246 {
4247 cmd = NULL;
4248 goto error;
4249 }
4250 }
4251 ++cmd;
4252 break;
4253
4254 default:
4255 if (VIM_ISDIGIT(*cmd)) /* absolute line number */
4256 lnum = getdigits(&cmd);
4257 }
4258
4259 for (;;)
4260 {
4261 cmd = skipwhite(cmd);
4262 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4263 break;
4264
4265 if (lnum == MAXLNUM)
4266 lnum = curwin->w_cursor.lnum; /* "+1" is same as ".+1" */
4267 if (VIM_ISDIGIT(*cmd))
4268 i = '+'; /* "number" is same as "+number" */
4269 else
4270 i = *cmd++;
4271 if (!VIM_ISDIGIT(*cmd)) /* '+' is '+1', but '+0' is not '+1' */
4272 n = 1;
4273 else
4274 n = getdigits(&cmd);
4275 if (i == '-')
4276 lnum -= n;
4277 else
4278 lnum += n;
4279 }
4280 } while (*cmd == '/' || *cmd == '?');
4281
4282error:
4283 *ptr = cmd;
4284 return lnum;
4285}
4286
4287/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004288 * Get flags from an Ex command argument.
4289 */
4290 static void
4291get_flags(eap)
4292 exarg_T *eap;
4293{
4294 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4295 {
4296 if (*eap->arg == 'l')
4297 eap->flags |= EXFLAG_LIST;
4298 else if (*eap->arg == 'p')
4299 eap->flags |= EXFLAG_PRINT;
4300 else
4301 eap->flags |= EXFLAG_NR;
4302 eap->arg = skipwhite(eap->arg + 1);
4303 }
4304}
4305
4306/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307 * Function called for command which is Not Implemented. NI!
4308 */
4309 void
4310ex_ni(eap)
4311 exarg_T *eap;
4312{
4313 if (!eap->skip)
4314 eap->errmsg = (char_u *)N_("E319: Sorry, the command is not available in this version");
4315}
4316
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004317#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004318/*
4319 * Function called for script command which is Not Implemented. NI!
4320 * Skips over ":perl <<EOF" constructs.
4321 */
4322 static void
4323ex_script_ni(eap)
4324 exarg_T *eap;
4325{
4326 if (!eap->skip)
4327 ex_ni(eap);
4328 else
4329 vim_free(script_get(eap, eap->arg));
4330}
4331#endif
4332
4333/*
4334 * Check range in Ex command for validity.
4335 * Return NULL when valid, error message when invalid.
4336 */
4337 static char_u *
4338invalid_range(eap)
4339 exarg_T *eap;
4340{
4341 if ( eap->line1 < 0
4342 || eap->line2 < 0
4343 || eap->line1 > eap->line2
4344 || ((eap->argt & RANGE)
4345 && !(eap->argt & NOTADR)
4346 && eap->line2 > curbuf->b_ml.ml_line_count
4347#ifdef FEAT_DIFF
4348 + (eap->cmdidx == CMD_diffget)
4349#endif
4350 ))
4351 return (char_u *)_(e_invrange);
4352 return NULL;
4353}
4354
4355/*
4356 * Correct the range for zero line number, if required.
4357 */
4358 static void
4359correct_range(eap)
4360 exarg_T *eap;
4361{
4362 if (!(eap->argt & ZEROR)) /* zero in range not allowed */
4363 {
4364 if (eap->line1 == 0)
4365 eap->line1 = 1;
4366 if (eap->line2 == 0)
4367 eap->line2 = 1;
4368 }
4369}
4370
Bram Moolenaar748bf032005-02-02 23:04:36 +00004371#ifdef FEAT_QUICKFIX
4372static char_u *skip_grep_pat __ARGS((exarg_T *eap));
4373
4374/*
4375 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4376 * pattern. Otherwise return eap->arg.
4377 */
4378 static char_u *
4379skip_grep_pat(eap)
4380 exarg_T *eap;
4381{
4382 char_u *p = eap->arg;
4383
Bram Moolenaara37420f2006-02-04 22:37:47 +00004384 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4385 || eap->cmdidx == CMD_vimgrepadd
4386 || eap->cmdidx == CMD_lvimgrepadd
4387 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004388 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004389 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004390 if (p == NULL)
4391 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004392 }
4393 return p;
4394}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004395
4396/*
4397 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4398 * in the command line, so that things like % get expanded.
4399 */
4400 static char_u *
4401replace_makeprg(eap, p, cmdlinep)
4402 exarg_T *eap;
4403 char_u *p;
4404 char_u **cmdlinep;
4405{
4406 char_u *new_cmdline;
4407 char_u *program;
4408 char_u *pos;
4409 char_u *ptr;
4410 int len;
4411 int i;
4412
4413 /*
4414 * Don't do it when ":vimgrep" is used for ":grep".
4415 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004416 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4417 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4418 || eap->cmdidx == CMD_grepadd
4419 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004420 && !grep_internal(eap->cmdidx))
4421 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004422 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4423 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004424 {
4425 if (*curbuf->b_p_gp == NUL)
4426 program = p_gp;
4427 else
4428 program = curbuf->b_p_gp;
4429 }
4430 else
4431 {
4432 if (*curbuf->b_p_mp == NUL)
4433 program = p_mp;
4434 else
4435 program = curbuf->b_p_mp;
4436 }
4437
4438 p = skipwhite(p);
4439
4440 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4441 {
4442 /* replace $* by given arguments */
4443 i = 1;
4444 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4445 ++i;
4446 len = (int)STRLEN(p);
4447 new_cmdline = alloc((int)(STRLEN(program) + i * (len - 2) + 1));
4448 if (new_cmdline == NULL)
4449 return NULL; /* out of memory */
4450 ptr = new_cmdline;
4451 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4452 {
4453 i = (int)(pos - program);
4454 STRNCPY(ptr, program, i);
4455 STRCPY(ptr += i, p);
4456 ptr += len;
4457 program = pos + 2;
4458 }
4459 STRCPY(ptr, program);
4460 }
4461 else
4462 {
4463 new_cmdline = alloc((int)(STRLEN(program) + STRLEN(p) + 2));
4464 if (new_cmdline == NULL)
4465 return NULL; /* out of memory */
4466 STRCPY(new_cmdline, program);
4467 STRCAT(new_cmdline, " ");
4468 STRCAT(new_cmdline, p);
4469 }
4470 msg_make(p);
4471
4472 /* 'eap->cmd' is not set here, because it is not used at CMD_make */
4473 vim_free(*cmdlinep);
4474 *cmdlinep = new_cmdline;
4475 p = new_cmdline;
4476 }
4477 return p;
4478}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004479#endif
4480
Bram Moolenaar071d4272004-06-13 20:20:40 +00004481/*
4482 * Expand file name in Ex command argument.
4483 * Return FAIL for failure, OK otherwise.
4484 */
4485 int
4486expand_filename(eap, cmdlinep, errormsgp)
4487 exarg_T *eap;
4488 char_u **cmdlinep;
4489 char_u **errormsgp;
4490{
4491 int has_wildcards; /* need to expand wildcards */
4492 char_u *repl;
4493 int srclen;
4494 char_u *p;
4495 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004496 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004497
Bram Moolenaar748bf032005-02-02 23:04:36 +00004498#ifdef FEAT_QUICKFIX
4499 /* Skip a regexp pattern for ":vimgrep[add] pat file..." */
4500 p = skip_grep_pat(eap);
4501#else
4502 p = eap->arg;
4503#endif
4504
Bram Moolenaar071d4272004-06-13 20:20:40 +00004505 /*
4506 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4507 * the file name contains a wildcard it should not cause expanding.
4508 * (it will be expanded anyway if there is a wildcard before replacing).
4509 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004510 has_wildcards = mch_has_wildcard(p);
4511 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004512 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004513#ifdef FEAT_EVAL
4514 /* Skip over `=expr`, wildcards in it are not expanded. */
4515 if (p[0] == '`' && p[1] == '=')
4516 {
4517 p += 2;
4518 (void)skip_expr(&p);
4519 if (*p == '`')
4520 ++p;
4521 continue;
4522 }
4523#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004524 /*
4525 * Quick check if this cannot be the start of a special string.
4526 * Also removes backslash before '%', '#' and '<'.
4527 */
4528 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4529 {
4530 ++p;
4531 continue;
4532 }
4533
4534 /*
4535 * Try to find a match at this position.
4536 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004537 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
4538 errormsgp, &escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004539 if (*errormsgp != NULL) /* error detected */
4540 return FAIL;
4541 if (repl == NULL) /* no match found */
4542 {
4543 p += srclen;
4544 continue;
4545 }
4546
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004547 /* Wildcards won't be expanded below, the replacement is taken
4548 * literally. But do expand "~/file", "~user/file" and "$HOME/file". */
4549 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4550 {
4551 char_u *l = repl;
4552
4553 repl = expand_env_save(repl);
4554 vim_free(l);
4555 }
4556
Bram Moolenaar63b92542007-03-27 14:57:09 +00004557 /* Need to escape white space et al. with a backslash.
4558 * Don't do this for:
4559 * - replacement that already has been escaped: "##"
4560 * - shell commands (may have to use quotes instead).
4561 * - non-unix systems when there is a single argument (spaces don't
4562 * separate arguments then).
4563 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004564 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00004565 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00004566 && eap->cmdidx != CMD_bang
4567 && eap->cmdidx != CMD_make
Bram Moolenaara37420f2006-02-04 22:37:47 +00004568 && eap->cmdidx != CMD_lmake
Bram Moolenaar071d4272004-06-13 20:20:40 +00004569 && eap->cmdidx != CMD_grep
Bram Moolenaara37420f2006-02-04 22:37:47 +00004570 && eap->cmdidx != CMD_lgrep
Bram Moolenaar071d4272004-06-13 20:20:40 +00004571 && eap->cmdidx != CMD_grepadd
Bram Moolenaara37420f2006-02-04 22:37:47 +00004572 && eap->cmdidx != CMD_lgrepadd
Bram Moolenaar071d4272004-06-13 20:20:40 +00004573#ifndef UNIX
4574 && !(eap->argt & NOSPC)
4575#endif
4576 )
4577 {
4578 char_u *l;
4579#ifdef BACKSLASH_IN_FILENAME
4580 /* Don't escape a backslash here, because rem_backslash() doesn't
4581 * remove it later. */
4582 static char_u *nobslash = (char_u *)" \t\"|";
4583# define ESCAPE_CHARS nobslash
4584#else
4585# define ESCAPE_CHARS escape_chars
4586#endif
4587
4588 for (l = repl; *l; ++l)
4589 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
4590 {
4591 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
4592 if (l != NULL)
4593 {
4594 vim_free(repl);
4595 repl = l;
4596 }
4597 break;
4598 }
4599 }
4600
4601 /* For a shell command a '!' must be escaped. */
4602 if ((eap->usefilter || eap->cmdidx == CMD_bang)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004603 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604 {
4605 char_u *l;
4606
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004607 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004608 if (l != NULL)
4609 {
4610 vim_free(repl);
4611 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004612 }
4613 }
4614
4615 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
4616 vim_free(repl);
4617 if (p == NULL)
4618 return FAIL;
4619 }
4620
4621 /*
4622 * One file argument: Expand wildcards.
4623 * Don't do this with ":r !command" or ":w !command".
4624 */
4625 if ((eap->argt & NOSPC) && !eap->usefilter)
4626 {
4627 /*
4628 * May do this twice:
4629 * 1. Replace environment variables.
4630 * 2. Replace any other wildcards, remove backslashes.
4631 */
4632 for (n = 1; n <= 2; ++n)
4633 {
4634 if (n == 2)
4635 {
4636#ifdef UNIX
4637 /*
4638 * Only for Unix we check for more than one file name.
4639 * For other systems spaces are considered to be part
4640 * of the file name.
4641 * Only check here if there is no wildcard, otherwise
4642 * ExpandOne() will check for errors. This allows
4643 * ":e `ls ve*.c`" on Unix.
4644 */
4645 if (!has_wildcards)
4646 for (p = eap->arg; *p; ++p)
4647 {
4648 /* skip escaped characters */
4649 if (p[1] && (*p == '\\' || *p == Ctrl_V))
4650 ++p;
4651 else if (vim_iswhite(*p))
4652 {
4653 *errormsgp = (char_u *)_("E172: Only one file name allowed");
4654 return FAIL;
4655 }
4656 }
4657#endif
4658
4659 /*
4660 * Halve the number of backslashes (this is Vi compatible).
4661 * For Unix and OS/2, when wildcards are expanded, this is
4662 * done by ExpandOne() below.
4663 */
4664#if defined(UNIX) || defined(OS2)
4665 if (!has_wildcards)
4666#endif
4667 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004668 }
4669
4670 if (has_wildcards)
4671 {
4672 if (n == 1)
4673 {
4674 /*
4675 * First loop: May expand environment variables. This
4676 * can be done much faster with expand_env() than with
4677 * something else (e.g., calling a shell).
4678 * After expanding environment variables, check again
4679 * if there are still wildcards present.
4680 */
4681 if (vim_strchr(eap->arg, '$') != NULL
4682 || vim_strchr(eap->arg, '~') != NULL)
4683 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00004684 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004685 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004686 has_wildcards = mch_has_wildcard(NameBuff);
4687 p = NameBuff;
4688 }
4689 else
4690 p = NULL;
4691 }
4692 else /* n == 2 */
4693 {
4694 expand_T xpc;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004695 int options = WILD_LIST_NOTFOUND|WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004696
4697 ExpandInit(&xpc);
4698 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004699 if (p_wic)
4700 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004701 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01004702 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004703 if (p == NULL)
4704 return FAIL;
4705 }
4706 if (p != NULL)
4707 {
4708 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
4709 p, cmdlinep);
4710 if (n == 2) /* p came from ExpandOne() */
4711 vim_free(p);
4712 }
4713 }
4714 }
4715 }
4716 return OK;
4717}
4718
4719/*
4720 * Replace part of the command line, keeping eap->cmd, eap->arg and
4721 * eap->nextcmd correct.
4722 * "src" points to the part that is to be replaced, of length "srclen".
4723 * "repl" is the replacement string.
4724 * Returns a pointer to the character after the replaced string.
4725 * Returns NULL for failure.
4726 */
4727 static char_u *
4728repl_cmdline(eap, src, srclen, repl, cmdlinep)
4729 exarg_T *eap;
4730 char_u *src;
4731 int srclen;
4732 char_u *repl;
4733 char_u **cmdlinep;
4734{
4735 int len;
4736 int i;
4737 char_u *new_cmdline;
4738
4739 /*
4740 * The new command line is build in new_cmdline[].
4741 * First allocate it.
4742 * Careful: a "+cmd" argument may have been NUL terminated.
4743 */
4744 len = (int)STRLEN(repl);
4745 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004746 if (eap->nextcmd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004747 i += (int)STRLEN(eap->nextcmd);/* add space for next command */
4748 if ((new_cmdline = alloc((unsigned)i)) == NULL)
4749 return NULL; /* out of memory! */
4750
4751 /*
4752 * Copy the stuff before the expanded part.
4753 * Copy the expanded stuff.
4754 * Copy what came after the expanded part.
4755 * Copy the next commands, if there are any.
4756 */
4757 i = (int)(src - *cmdlinep); /* length of part before match */
4758 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00004759
Bram Moolenaar071d4272004-06-13 20:20:40 +00004760 mch_memmove(new_cmdline + i, repl, (size_t)len);
4761 i += len; /* remember the end of the string */
4762 STRCPY(new_cmdline + i, src + srclen);
4763 src = new_cmdline + i; /* remember where to continue */
4764
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004765 if (eap->nextcmd != NULL) /* append next command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004766 {
4767 i = (int)STRLEN(new_cmdline) + 1;
4768 STRCPY(new_cmdline + i, eap->nextcmd);
4769 eap->nextcmd = new_cmdline + i;
4770 }
4771 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
4772 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
4773 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
4774 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
4775 vim_free(*cmdlinep);
4776 *cmdlinep = new_cmdline;
4777
4778 return src;
4779}
4780
4781/*
4782 * Check for '|' to separate commands and '"' to start comments.
4783 */
4784 void
4785separate_nextcmd(eap)
4786 exarg_T *eap;
4787{
4788 char_u *p;
4789
Bram Moolenaar86b68352004-12-27 21:59:20 +00004790#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004791 p = skip_grep_pat(eap);
4792#else
4793 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004794#endif
4795
4796 for ( ; *p; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004797 {
4798 if (*p == Ctrl_V)
4799 {
4800 if (eap->argt & (USECTRLV | XFILE))
4801 ++p; /* skip CTRL-V and next char */
4802 else
Bram Moolenaarb0db5692007-08-14 20:54:49 +00004803 /* remove CTRL-V and skip next char */
Bram Moolenaara7241f52008-06-24 20:39:31 +00004804 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004805 if (*p == NUL) /* stop at NUL after CTRL-V */
4806 break;
4807 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004808
4809#ifdef FEAT_EVAL
4810 /* Skip over `=expr` when wildcards are expanded. */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004811 else if (p[0] == '`' && p[1] == '=' && (eap->argt & XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004812 {
4813 p += 2;
4814 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004815 }
4816#endif
4817
Bram Moolenaar071d4272004-06-13 20:20:40 +00004818 /* Check for '"': start of comment or '|': next command */
4819 /* :@" and :*" do not start a comment!
4820 * :redir @" doesn't either. */
4821 else if ((*p == '"' && !(eap->argt & NOTRLCOM)
4822 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
4823 || p != eap->arg)
4824 && (eap->cmdidx != CMD_redir
4825 || p != eap->arg + 1 || p[-1] != '@'))
4826 || *p == '|' || *p == '\n')
4827 {
4828 /*
4829 * We remove the '\' before the '|', unless USECTRLV is used
4830 * AND 'b' is present in 'cpoptions'.
4831 */
4832 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
4833 || !(eap->argt & USECTRLV)) && *(p - 1) == '\\')
4834 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00004835 STRMOVE(p - 1, p); /* remove the '\' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004836 --p;
4837 }
4838 else
4839 {
4840 eap->nextcmd = check_nextcmd(p);
4841 *p = NUL;
4842 break;
4843 }
4844 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004845 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004846
Bram Moolenaar071d4272004-06-13 20:20:40 +00004847 if (!(eap->argt & NOTRLCOM)) /* remove trailing spaces */
4848 del_trailing_spaces(eap->arg);
4849}
4850
4851/*
4852 * get + command from ex argument
4853 */
4854 static char_u *
4855getargcmd(argp)
4856 char_u **argp;
4857{
4858 char_u *arg = *argp;
4859 char_u *command = NULL;
4860
4861 if (*arg == '+') /* +[command] */
4862 {
4863 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02004864 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004865 command = dollar_command;
4866 else
4867 {
4868 command = arg;
4869 arg = skip_cmd_arg(command, TRUE);
4870 if (*arg != NUL)
4871 *arg++ = NUL; /* terminate command with NUL */
4872 }
4873
4874 arg = skipwhite(arg); /* skip over spaces */
4875 *argp = arg;
4876 }
4877 return command;
4878}
4879
4880/*
4881 * Find end of "+command" argument. Skip over "\ " and "\\".
4882 */
4883 static char_u *
4884skip_cmd_arg(p, rembs)
4885 char_u *p;
4886 int rembs; /* TRUE to halve the number of backslashes */
4887{
4888 while (*p && !vim_isspace(*p))
4889 {
4890 if (*p == '\\' && p[1] != NUL)
4891 {
4892 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00004893 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004894 else
4895 ++p;
4896 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004897 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004898 }
4899 return p;
4900}
4901
4902/*
4903 * Get "++opt=arg" argument.
4904 * Return FAIL or OK.
4905 */
4906 static int
4907getargopt(eap)
4908 exarg_T *eap;
4909{
4910 char_u *arg = eap->arg + 2;
4911 int *pp = NULL;
4912#ifdef FEAT_MBYTE
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004913 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004914 char_u *p;
4915#endif
4916
4917 /* ":edit ++[no]bin[ary] file" */
4918 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
4919 {
4920 if (*arg == 'n')
4921 {
4922 arg += 2;
4923 eap->force_bin = FORCE_NOBIN;
4924 }
4925 else
4926 eap->force_bin = FORCE_BIN;
4927 if (!checkforcmd(&arg, "binary", 3))
4928 return FAIL;
4929 eap->arg = skipwhite(arg);
4930 return OK;
4931 }
4932
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004933 /* ":read ++edit file" */
4934 if (STRNCMP(arg, "edit", 4) == 0)
4935 {
4936 eap->read_edit = TRUE;
4937 eap->arg = skipwhite(arg + 4);
4938 return OK;
4939 }
4940
Bram Moolenaar071d4272004-06-13 20:20:40 +00004941 if (STRNCMP(arg, "ff", 2) == 0)
4942 {
4943 arg += 2;
4944 pp = &eap->force_ff;
4945 }
4946 else if (STRNCMP(arg, "fileformat", 10) == 0)
4947 {
4948 arg += 10;
4949 pp = &eap->force_ff;
4950 }
4951#ifdef FEAT_MBYTE
4952 else if (STRNCMP(arg, "enc", 3) == 0)
4953 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01004954 if (STRNCMP(arg, "encoding", 8) == 0)
4955 arg += 8;
4956 else
4957 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004958 pp = &eap->force_enc;
4959 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004960 else if (STRNCMP(arg, "bad", 3) == 0)
4961 {
4962 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004963 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004964 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004965#endif
4966
4967 if (pp == NULL || *arg != '=')
4968 return FAIL;
4969
4970 ++arg;
4971 *pp = (int)(arg - eap->cmd);
4972 arg = skip_cmd_arg(arg, FALSE);
4973 eap->arg = skipwhite(arg);
4974 *arg = NUL;
4975
4976#ifdef FEAT_MBYTE
4977 if (pp == &eap->force_ff)
4978 {
4979#endif
4980 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
4981 return FAIL;
4982#ifdef FEAT_MBYTE
4983 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004984 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985 {
4986 /* Make 'fileencoding' lower case. */
4987 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
4988 *p = TOLOWER_ASC(*p);
4989 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004990 else
4991 {
4992 /* Check ++bad= argument. Must be a single-byte character, "keep" or
4993 * "drop". */
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004994 p = eap->cmd + bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004995 if (STRICMP(p, "keep") == 0)
4996 eap->bad_char = BAD_KEEP;
4997 else if (STRICMP(p, "drop") == 0)
4998 eap->bad_char = BAD_DROP;
4999 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
5000 eap->bad_char = *p;
5001 else
5002 return FAIL;
5003 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005004#endif
5005
5006 return OK;
5007}
5008
5009/*
5010 * ":abbreviate" and friends.
5011 */
5012 static void
5013ex_abbreviate(eap)
5014 exarg_T *eap;
5015{
5016 do_exmap(eap, TRUE); /* almost the same as mapping */
5017}
5018
5019/*
5020 * ":map" and friends.
5021 */
5022 static void
5023ex_map(eap)
5024 exarg_T *eap;
5025{
5026 /*
5027 * If we are sourcing .exrc or .vimrc in current directory we
5028 * print the mappings for security reasons.
5029 */
5030 if (secure)
5031 {
5032 secure = 2;
5033 msg_outtrans(eap->cmd);
5034 msg_putchar('\n');
5035 }
5036 do_exmap(eap, FALSE);
5037}
5038
5039/*
5040 * ":unmap" and friends.
5041 */
5042 static void
5043ex_unmap(eap)
5044 exarg_T *eap;
5045{
5046 do_exmap(eap, FALSE);
5047}
5048
5049/*
5050 * ":mapclear" and friends.
5051 */
5052 static void
5053ex_mapclear(eap)
5054 exarg_T *eap;
5055{
5056 map_clear(eap->cmd, eap->arg, eap->forceit, FALSE);
5057}
5058
5059/*
5060 * ":abclear" and friends.
5061 */
5062 static void
5063ex_abclear(eap)
5064 exarg_T *eap;
5065{
5066 map_clear(eap->cmd, eap->arg, TRUE, TRUE);
5067}
5068
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005069#if defined(FEAT_AUTOCMD) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070 static void
5071ex_autocmd(eap)
5072 exarg_T *eap;
5073{
5074 /*
5075 * Disallow auto commands from .exrc and .vimrc in current
5076 * directory for security reasons.
5077 */
5078 if (secure)
5079 {
5080 secure = 2;
5081 eap->errmsg = e_curdir;
5082 }
5083 else if (eap->cmdidx == CMD_autocmd)
5084 do_autocmd(eap->arg, eap->forceit);
5085 else
5086 do_augroup(eap->arg, eap->forceit);
5087}
5088
5089/*
5090 * ":doautocmd": Apply the automatic commands to the current buffer.
5091 */
5092 static void
5093ex_doautocmd(eap)
5094 exarg_T *eap;
5095{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005096 char_u *arg = eap->arg;
5097 int call_do_modelines = check_nomodeline(&arg);
5098
5099 (void)do_doautocmd(arg, TRUE);
5100 if (call_do_modelines) /* Only when there is no <nomodeline>. */
5101 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005102}
5103#endif
5104
5105#ifdef FEAT_LISTCMDS
5106/*
5107 * :[N]bunload[!] [N] [bufname] unload buffer
5108 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
5109 * :[N]bwipeout[!] [N] [bufname] delete buffer really
5110 */
5111 static void
5112ex_bunload(eap)
5113 exarg_T *eap;
5114{
5115 eap->errmsg = do_bufdel(
5116 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
5117 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
5118 : DOBUF_UNLOAD, eap->arg,
5119 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
5120}
5121
5122/*
5123 * :[N]buffer [N] to buffer N
5124 * :[N]sbuffer [N] to buffer N
5125 */
5126 static void
5127ex_buffer(eap)
5128 exarg_T *eap;
5129{
5130 if (*eap->arg)
5131 eap->errmsg = e_trailing;
5132 else
5133 {
5134 if (eap->addr_count == 0) /* default is current buffer */
5135 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
5136 else
5137 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005138 if (eap->do_ecmd_cmd != NULL)
5139 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005140 }
5141}
5142
5143/*
5144 * :[N]bmodified [N] to next mod. buffer
5145 * :[N]sbmodified [N] to next mod. buffer
5146 */
5147 static void
5148ex_bmodified(eap)
5149 exarg_T *eap;
5150{
5151 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005152 if (eap->do_ecmd_cmd != NULL)
5153 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005154}
5155
5156/*
5157 * :[N]bnext [N] to next buffer
5158 * :[N]sbnext [N] split and to next buffer
5159 */
5160 static void
5161ex_bnext(eap)
5162 exarg_T *eap;
5163{
5164 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005165 if (eap->do_ecmd_cmd != NULL)
5166 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005167}
5168
5169/*
5170 * :[N]bNext [N] to previous buffer
5171 * :[N]bprevious [N] to previous buffer
5172 * :[N]sbNext [N] split and to previous buffer
5173 * :[N]sbprevious [N] split and to previous buffer
5174 */
5175 static void
5176ex_bprevious(eap)
5177 exarg_T *eap;
5178{
5179 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005180 if (eap->do_ecmd_cmd != NULL)
5181 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005182}
5183
5184/*
5185 * :brewind to first buffer
5186 * :bfirst to first buffer
5187 * :sbrewind split and to first buffer
5188 * :sbfirst split and to first buffer
5189 */
5190 static void
5191ex_brewind(eap)
5192 exarg_T *eap;
5193{
5194 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005195 if (eap->do_ecmd_cmd != NULL)
5196 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005197}
5198
5199/*
5200 * :blast to last buffer
5201 * :sblast split and to last buffer
5202 */
5203 static void
5204ex_blast(eap)
5205 exarg_T *eap;
5206{
5207 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005208 if (eap->do_ecmd_cmd != NULL)
5209 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005210}
5211#endif
5212
5213 int
5214ends_excmd(c)
5215 int c;
5216{
5217 return (c == NUL || c == '|' || c == '"' || c == '\n');
5218}
5219
5220#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5221 || defined(PROTO)
5222/*
5223 * Return the next command, after the first '|' or '\n'.
5224 * Return NULL if not found.
5225 */
5226 char_u *
5227find_nextcmd(p)
5228 char_u *p;
5229{
5230 while (*p != '|' && *p != '\n')
5231 {
5232 if (*p == NUL)
5233 return NULL;
5234 ++p;
5235 }
5236 return (p + 1);
5237}
5238#endif
5239
5240/*
5241 * Check if *p is a separator between Ex commands.
5242 * Return NULL if it isn't, (p + 1) if it is.
5243 */
5244 char_u *
5245check_nextcmd(p)
5246 char_u *p;
5247{
5248 p = skipwhite(p);
5249 if (*p == '|' || *p == '\n')
5250 return (p + 1);
5251 else
5252 return NULL;
5253}
5254
5255/*
5256 * - if there are more files to edit
5257 * - and this is the last window
5258 * - and forceit not used
5259 * - and not repeated twice on a row
5260 * return FAIL and give error message if 'message' TRUE
5261 * return OK otherwise
5262 */
5263 static int
5264check_more(message, forceit)
5265 int message; /* when FALSE check only, no messages */
5266 int forceit;
5267{
5268 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5269
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005270 if (!forceit && only_one_window()
5271 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005272 {
5273 if (message)
5274 {
5275#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
5276 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
5277 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005278 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005279
5280 if (n == 1)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02005281 vim_strncpy(buff,
5282 (char_u *)_("1 more file to edit. Quit anyway?"),
Bram Moolenaard9462e32011-04-11 21:35:11 +02005283 DIALOG_MSG_SIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005284 else
Bram Moolenaard9462e32011-04-11 21:35:11 +02005285 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005286 _("%d more files to edit. Quit anyway?"), n);
5287 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5288 return OK;
5289 return FAIL;
5290 }
5291#endif
5292 if (n == 1)
5293 EMSG(_("E173: 1 more file to edit"));
5294 else
5295 EMSGN(_("E173: %ld more files to edit"), n);
5296 quitmore = 2; /* next try to quit is allowed */
5297 }
5298 return FAIL;
5299 }
5300 return OK;
5301}
5302
5303#ifdef FEAT_CMDL_COMPL
5304/*
5305 * Function given to ExpandGeneric() to obtain the list of command names.
5306 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005307 char_u *
5308get_command_name(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00005309 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005310 int idx;
5311{
5312 if (idx >= (int)CMD_SIZE)
5313# ifdef FEAT_USR_CMDS
5314 return get_user_command_name(idx);
5315# else
5316 return NULL;
5317# endif
5318 return cmdnames[idx].cmd_name;
5319}
5320#endif
5321
5322#if defined(FEAT_USR_CMDS) || defined(PROTO)
5323static 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));
5324static void uc_list __ARGS((char_u *name, size_t name_len));
5325static int uc_scan_attr __ARGS((char_u *attr, size_t len, long *argt, long *def, int *flags, int *compl, char_u **compl_arg));
5326static char_u *uc_split_args __ARGS((char_u *arg, size_t *lenp));
5327static 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));
5328
5329 static int
5330uc_add_command(name, name_len, rep, argt, def, flags, compl, compl_arg, force)
5331 char_u *name;
5332 size_t name_len;
5333 char_u *rep;
5334 long argt;
5335 long def;
5336 int flags;
5337 int compl;
5338 char_u *compl_arg;
5339 int force;
5340{
5341 ucmd_T *cmd = NULL;
5342 char_u *p;
5343 int i;
5344 int cmp = 1;
5345 char_u *rep_buf = NULL;
5346 garray_T *gap;
5347
Bram Moolenaar9c102382006-05-03 21:26:49 +00005348 replace_termcodes(rep, &rep_buf, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005349 if (rep_buf == NULL)
5350 {
5351 /* Can't replace termcodes - try using the string as is */
5352 rep_buf = vim_strsave(rep);
5353
5354 /* Give up if out of memory */
5355 if (rep_buf == NULL)
5356 return FAIL;
5357 }
5358
5359 /* get address of growarray: global or in curbuf */
5360 if (flags & UC_BUFFER)
5361 {
5362 gap = &curbuf->b_ucmds;
5363 if (gap->ga_itemsize == 0)
5364 ga_init2(gap, (int)sizeof(ucmd_T), 4);
5365 }
5366 else
5367 gap = &ucmds;
5368
5369 /* Search for the command in the already defined commands. */
5370 for (i = 0; i < gap->ga_len; ++i)
5371 {
5372 size_t len;
5373
5374 cmd = USER_CMD_GA(gap, i);
5375 len = STRLEN(cmd->uc_name);
5376 cmp = STRNCMP(name, cmd->uc_name, name_len);
5377 if (cmp == 0)
5378 {
5379 if (name_len < len)
5380 cmp = -1;
5381 else if (name_len > len)
5382 cmp = 1;
5383 }
5384
5385 if (cmp == 0)
5386 {
5387 if (!force)
5388 {
5389 EMSG(_("E174: Command already exists: add ! to replace it"));
5390 goto fail;
5391 }
5392
5393 vim_free(cmd->uc_rep);
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00005394 cmd->uc_rep = NULL;
5395#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5396 vim_free(cmd->uc_compl_arg);
5397 cmd->uc_compl_arg = NULL;
5398#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005399 break;
5400 }
5401
5402 /* Stop as soon as we pass the name to add */
5403 if (cmp < 0)
5404 break;
5405 }
5406
5407 /* Extend the array unless we're replacing an existing command */
5408 if (cmp != 0)
5409 {
5410 if (ga_grow(gap, 1) != OK)
5411 goto fail;
5412 if ((p = vim_strnsave(name, (int)name_len)) == NULL)
5413 goto fail;
5414
5415 cmd = USER_CMD_GA(gap, i);
5416 mch_memmove(cmd + 1, cmd, (gap->ga_len - i) * sizeof(ucmd_T));
5417
5418 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005419
5420 cmd->uc_name = p;
5421 }
5422
5423 cmd->uc_rep = rep_buf;
5424 cmd->uc_argt = argt;
5425 cmd->uc_def = def;
5426 cmd->uc_compl = compl;
5427#ifdef FEAT_EVAL
5428 cmd->uc_scriptID = current_SID;
5429# ifdef FEAT_CMDL_COMPL
5430 cmd->uc_compl_arg = compl_arg;
5431# endif
5432#endif
5433
5434 return OK;
5435
5436fail:
5437 vim_free(rep_buf);
5438#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5439 vim_free(compl_arg);
5440#endif
5441 return FAIL;
5442}
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005443#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005444
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005445#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005446/*
5447 * List of names for completion for ":command" with the EXPAND_ flag.
5448 * Must be alphabetical for completion.
5449 */
5450static struct
5451{
5452 int expand;
5453 char *name;
5454} command_complete[] =
5455{
5456 {EXPAND_AUGROUP, "augroup"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005457 {EXPAND_BEHAVE, "behave"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005458 {EXPAND_BUFFERS, "buffer"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005459 {EXPAND_COLORS, "color"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005460 {EXPAND_COMMANDS, "command"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005461 {EXPAND_COMPILER, "compiler"},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005462#if defined(FEAT_CSCOPE)
5463 {EXPAND_CSCOPE, "cscope"},
5464#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005465#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5466 {EXPAND_USER_DEFINED, "custom"},
Bram Moolenaara466c992005-07-09 21:03:22 +00005467 {EXPAND_USER_LIST, "customlist"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005468#endif
5469 {EXPAND_DIRECTORIES, "dir"},
5470 {EXPAND_ENV_VARS, "environment"},
5471 {EXPAND_EVENTS, "event"},
5472 {EXPAND_EXPRESSION, "expression"},
5473 {EXPAND_FILES, "file"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005474 {EXPAND_FILES_IN_PATH, "file_in_path"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005475 {EXPAND_FILETYPE, "filetype"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005476 {EXPAND_FUNCTIONS, "function"},
5477 {EXPAND_HELP, "help"},
5478 {EXPAND_HIGHLIGHT, "highlight"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005479#if defined(FEAT_CMDHIST)
5480 {EXPAND_HISTORY, "history"},
5481#endif
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005482#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005483 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005484 {EXPAND_LOCALES, "locale"},
5485#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005486 {EXPAND_MAPPINGS, "mapping"},
5487 {EXPAND_MENUS, "menu"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005488 {EXPAND_OWNSYNTAX, "syntax"},
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02005489#if defined(FEAT_PROFILE)
5490 {EXPAND_SYNTIME, "syntime"},
5491#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005492 {EXPAND_SETTINGS, "option"},
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005493 {EXPAND_SHELLCMD, "shellcmd"},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005494#if defined(FEAT_SIGNS)
5495 {EXPAND_SIGN, "sign"},
5496#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005497 {EXPAND_TAGS, "tag"},
5498 {EXPAND_TAGS_LISTFILES, "tag_listfiles"},
Bram Moolenaar24305862012-08-15 14:05:05 +02005499 {EXPAND_USER, "user"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005500 {EXPAND_USER_VARS, "var"},
5501 {0, NULL}
5502};
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005503#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005504
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005505#if defined(FEAT_USR_CMDS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005506 static void
5507uc_list(name, name_len)
5508 char_u *name;
5509 size_t name_len;
5510{
5511 int i, j;
5512 int found = FALSE;
5513 ucmd_T *cmd;
5514 int len;
5515 long a;
5516 garray_T *gap;
5517
5518 gap = &curbuf->b_ucmds;
5519 for (;;)
5520 {
5521 for (i = 0; i < gap->ga_len; ++i)
5522 {
5523 cmd = USER_CMD_GA(gap, i);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005524 a = (long)cmd->uc_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005525
5526 /* Skip commands which don't match the requested prefix */
5527 if (STRNCMP(name, cmd->uc_name, name_len) != 0)
5528 continue;
5529
5530 /* Put out the title first time */
5531 if (!found)
5532 MSG_PUTS_TITLE(_("\n Name Args Range Complete Definition"));
5533 found = TRUE;
5534 msg_putchar('\n');
5535 if (got_int)
5536 break;
5537
5538 /* Special cases */
5539 msg_putchar(a & BANG ? '!' : ' ');
5540 msg_putchar(a & REGSTR ? '"' : ' ');
5541 msg_putchar(gap != &ucmds ? 'b' : ' ');
5542 msg_putchar(' ');
5543
5544 msg_outtrans_attr(cmd->uc_name, hl_attr(HLF_D));
5545 len = (int)STRLEN(cmd->uc_name) + 4;
5546
5547 do {
5548 msg_putchar(' ');
5549 ++len;
5550 } while (len < 16);
5551
5552 len = 0;
5553
5554 /* Arguments */
5555 switch ((int)(a & (EXTRA|NOSPC|NEEDARG)))
5556 {
5557 case 0: IObuff[len++] = '0'; break;
5558 case (EXTRA): IObuff[len++] = '*'; break;
5559 case (EXTRA|NOSPC): IObuff[len++] = '?'; break;
5560 case (EXTRA|NEEDARG): IObuff[len++] = '+'; break;
5561 case (EXTRA|NOSPC|NEEDARG): IObuff[len++] = '1'; break;
5562 }
5563
5564 do {
5565 IObuff[len++] = ' ';
5566 } while (len < 5);
5567
5568 /* Range */
5569 if (a & (RANGE|COUNT))
5570 {
5571 if (a & COUNT)
5572 {
5573 /* -count=N */
5574 sprintf((char *)IObuff + len, "%ldc", cmd->uc_def);
5575 len += (int)STRLEN(IObuff + len);
5576 }
5577 else if (a & DFLALL)
5578 IObuff[len++] = '%';
5579 else if (cmd->uc_def >= 0)
5580 {
5581 /* -range=N */
5582 sprintf((char *)IObuff + len, "%ld", cmd->uc_def);
5583 len += (int)STRLEN(IObuff + len);
5584 }
5585 else
5586 IObuff[len++] = '.';
5587 }
5588
5589 do {
5590 IObuff[len++] = ' ';
5591 } while (len < 11);
5592
5593 /* Completion */
5594 for (j = 0; command_complete[j].expand != 0; ++j)
5595 if (command_complete[j].expand == cmd->uc_compl)
5596 {
5597 STRCPY(IObuff + len, command_complete[j].name);
5598 len += (int)STRLEN(IObuff + len);
5599 break;
5600 }
5601
5602 do {
5603 IObuff[len++] = ' ';
5604 } while (len < 21);
5605
5606 IObuff[len] = '\0';
5607 msg_outtrans(IObuff);
5608
5609 msg_outtrans_special(cmd->uc_rep, FALSE);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005610#ifdef FEAT_EVAL
5611 if (p_verbose > 0)
5612 last_set_msg(cmd->uc_scriptID);
5613#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005614 out_flush();
5615 ui_breakcheck();
5616 if (got_int)
5617 break;
5618 }
5619 if (gap == &ucmds || i < gap->ga_len)
5620 break;
5621 gap = &ucmds;
5622 }
5623
5624 if (!found)
5625 MSG(_("No user-defined commands found"));
5626}
5627
5628 static char_u *
5629uc_fun_cmd()
5630{
5631 static char_u fcmd[] = {0x84, 0xaf, 0x60, 0xb9, 0xaf, 0xb5, 0x60, 0xa4,
5632 0xa5, 0xad, 0xa1, 0xae, 0xa4, 0x60, 0xa1, 0x60,
5633 0xb3, 0xa8, 0xb2, 0xb5, 0xa2, 0xa2, 0xa5, 0xb2,
5634 0xb9, 0x7f, 0};
5635 int i;
5636
5637 for (i = 0; fcmd[i]; ++i)
5638 IObuff[i] = fcmd[i] - 0x40;
5639 IObuff[i] = 0;
5640 return IObuff;
5641}
5642
5643 static int
5644uc_scan_attr(attr, len, argt, def, flags, compl, compl_arg)
5645 char_u *attr;
5646 size_t len;
5647 long *argt;
5648 long *def;
5649 int *flags;
5650 int *compl;
5651 char_u **compl_arg;
5652{
5653 char_u *p;
5654
5655 if (len == 0)
5656 {
5657 EMSG(_("E175: No attribute specified"));
5658 return FAIL;
5659 }
5660
5661 /* First, try the simple attributes (no arguments) */
5662 if (STRNICMP(attr, "bang", len) == 0)
5663 *argt |= BANG;
5664 else if (STRNICMP(attr, "buffer", len) == 0)
5665 *flags |= UC_BUFFER;
5666 else if (STRNICMP(attr, "register", len) == 0)
5667 *argt |= REGSTR;
5668 else if (STRNICMP(attr, "bar", len) == 0)
5669 *argt |= TRLBAR;
5670 else
5671 {
5672 int i;
5673 char_u *val = NULL;
5674 size_t vallen = 0;
5675 size_t attrlen = len;
5676
5677 /* Look for the attribute name - which is the part before any '=' */
5678 for (i = 0; i < (int)len; ++i)
5679 {
5680 if (attr[i] == '=')
5681 {
5682 val = &attr[i + 1];
5683 vallen = len - i - 1;
5684 attrlen = i;
5685 break;
5686 }
5687 }
5688
5689 if (STRNICMP(attr, "nargs", attrlen) == 0)
5690 {
5691 if (vallen == 1)
5692 {
5693 if (*val == '0')
5694 /* Do nothing - this is the default */;
5695 else if (*val == '1')
5696 *argt |= (EXTRA | NOSPC | NEEDARG);
5697 else if (*val == '*')
5698 *argt |= EXTRA;
5699 else if (*val == '?')
5700 *argt |= (EXTRA | NOSPC);
5701 else if (*val == '+')
5702 *argt |= (EXTRA | NEEDARG);
5703 else
5704 goto wrong_nargs;
5705 }
5706 else
5707 {
5708wrong_nargs:
5709 EMSG(_("E176: Invalid number of arguments"));
5710 return FAIL;
5711 }
5712 }
5713 else if (STRNICMP(attr, "range", attrlen) == 0)
5714 {
5715 *argt |= RANGE;
5716 if (vallen == 1 && *val == '%')
5717 *argt |= DFLALL;
5718 else if (val != NULL)
5719 {
5720 p = val;
5721 if (*def >= 0)
5722 {
5723two_count:
5724 EMSG(_("E177: Count cannot be specified twice"));
5725 return FAIL;
5726 }
5727
5728 *def = getdigits(&p);
5729 *argt |= (ZEROR | NOTADR);
5730
5731 if (p != val + vallen || vallen == 0)
5732 {
5733invalid_count:
5734 EMSG(_("E178: Invalid default value for count"));
5735 return FAIL;
5736 }
5737 }
5738 }
5739 else if (STRNICMP(attr, "count", attrlen) == 0)
5740 {
Bram Moolenaar32e7b2d2005-02-27 22:36:47 +00005741 *argt |= (COUNT | ZEROR | RANGE | NOTADR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005742
5743 if (val != NULL)
5744 {
5745 p = val;
5746 if (*def >= 0)
5747 goto two_count;
5748
5749 *def = getdigits(&p);
5750
5751 if (p != val + vallen)
5752 goto invalid_count;
5753 }
5754
5755 if (*def < 0)
5756 *def = 0;
5757 }
5758 else if (STRNICMP(attr, "complete", attrlen) == 0)
5759 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005760 if (val == NULL)
5761 {
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005762 EMSG(_("E179: argument required for -complete"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005763 return FAIL;
5764 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005765
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005766 if (parse_compl_arg(val, (int)vallen, compl, argt, compl_arg)
5767 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005768 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005769 }
5770 else
5771 {
5772 char_u ch = attr[len];
5773 attr[len] = '\0';
5774 EMSG2(_("E181: Invalid attribute: %s"), attr);
5775 attr[len] = ch;
5776 return FAIL;
5777 }
5778 }
5779
5780 return OK;
5781}
5782
Bram Moolenaara850a712009-01-28 14:42:59 +00005783/*
5784 * ":command ..."
5785 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005786 static void
5787ex_command(eap)
5788 exarg_T *eap;
5789{
5790 char_u *name;
5791 char_u *end;
5792 char_u *p;
5793 long argt = 0;
5794 long def = -1;
5795 int flags = 0;
5796 int compl = EXPAND_NOTHING;
5797 char_u *compl_arg = NULL;
5798 int has_attr = (eap->arg[0] == '-');
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01005799 int name_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005800
5801 p = eap->arg;
5802
5803 /* Check for attributes */
5804 while (*p == '-')
5805 {
5806 ++p;
5807 end = skiptowhite(p);
5808 if (uc_scan_attr(p, end - p, &argt, &def, &flags, &compl, &compl_arg)
5809 == FAIL)
5810 return;
5811 p = skipwhite(end);
5812 }
5813
5814 /* Get the name (if any) and skip to the following argument */
5815 name = p;
5816 if (ASCII_ISALPHA(*p))
5817 while (ASCII_ISALNUM(*p))
5818 ++p;
5819 if (!ends_excmd(*p) && !vim_iswhite(*p))
5820 {
5821 EMSG(_("E182: Invalid command name"));
5822 return;
5823 }
5824 end = p;
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01005825 name_len = (int)(end - name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005826
5827 /* If there is nothing after the name, and no attributes were specified,
5828 * we are listing commands
5829 */
5830 p = skipwhite(end);
5831 if (!has_attr && ends_excmd(*p))
5832 {
5833 uc_list(name, end - name);
5834 }
5835 else if (!ASCII_ISUPPER(*name))
5836 {
5837 EMSG(_("E183: User defined commands must start with an uppercase letter"));
5838 return;
5839 }
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01005840 else if ((name_len == 1 && *name == 'X')
5841 || (name_len <= 4
5842 && STRNCMP(name, "Next", name_len > 4 ? 4 : name_len) == 0))
5843 {
5844 EMSG(_("E841: Reserved name, cannot be used for user defined command"));
5845 return;
5846 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005847 else
5848 uc_add_command(name, end - name, p, argt, def, flags, compl, compl_arg,
5849 eap->forceit);
5850}
5851
5852/*
5853 * ":comclear"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005854 * Clear all user commands, global and for current buffer.
5855 */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00005856 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005857ex_comclear(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00005858 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005859{
5860 uc_clear(&ucmds);
5861 uc_clear(&curbuf->b_ucmds);
5862}
5863
5864/*
5865 * Clear all user commands for "gap".
5866 */
5867 void
5868uc_clear(gap)
5869 garray_T *gap;
5870{
5871 int i;
5872 ucmd_T *cmd;
5873
5874 for (i = 0; i < gap->ga_len; ++i)
5875 {
5876 cmd = USER_CMD_GA(gap, i);
5877 vim_free(cmd->uc_name);
5878 vim_free(cmd->uc_rep);
5879# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5880 vim_free(cmd->uc_compl_arg);
5881# endif
5882 }
5883 ga_clear(gap);
5884}
5885
5886 static void
5887ex_delcommand(eap)
5888 exarg_T *eap;
5889{
5890 int i = 0;
5891 ucmd_T *cmd = NULL;
5892 int cmp = -1;
5893 garray_T *gap;
5894
5895 gap = &curbuf->b_ucmds;
5896 for (;;)
5897 {
5898 for (i = 0; i < gap->ga_len; ++i)
5899 {
5900 cmd = USER_CMD_GA(gap, i);
5901 cmp = STRCMP(eap->arg, cmd->uc_name);
5902 if (cmp <= 0)
5903 break;
5904 }
5905 if (gap == &ucmds || cmp == 0)
5906 break;
5907 gap = &ucmds;
5908 }
5909
5910 if (cmp != 0)
5911 {
5912 EMSG2(_("E184: No such user-defined command: %s"), eap->arg);
5913 return;
5914 }
5915
5916 vim_free(cmd->uc_name);
5917 vim_free(cmd->uc_rep);
5918# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5919 vim_free(cmd->uc_compl_arg);
5920# endif
5921
5922 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005923
5924 if (i < gap->ga_len)
5925 mch_memmove(cmd, cmd + 1, (gap->ga_len - i) * sizeof(ucmd_T));
5926}
5927
Bram Moolenaar552f8a12007-03-08 17:12:08 +00005928/*
5929 * split and quote args for <f-args>
5930 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005931 static char_u *
5932uc_split_args(arg, lenp)
5933 char_u *arg;
5934 size_t *lenp;
5935{
5936 char_u *buf;
5937 char_u *p;
5938 char_u *q;
5939 int len;
5940
5941 /* Precalculate length */
5942 p = arg;
5943 len = 2; /* Initial and final quotes */
5944
5945 while (*p)
5946 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00005947 if (p[0] == '\\' && p[1] == '\\')
5948 {
5949 len += 2;
5950 p += 2;
5951 }
5952 else if (p[0] == '\\' && vim_iswhite(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005953 {
5954 len += 1;
5955 p += 2;
5956 }
5957 else if (*p == '\\' || *p == '"')
5958 {
5959 len += 2;
5960 p += 1;
5961 }
5962 else if (vim_iswhite(*p))
5963 {
5964 p = skipwhite(p);
5965 if (*p == NUL)
5966 break;
5967 len += 3; /* "," */
5968 }
5969 else
5970 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02005971#ifdef FEAT_MBYTE
5972 int charlen = (*mb_ptr2len)(p);
5973 len += charlen;
5974 p += charlen;
5975#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005976 ++len;
5977 ++p;
Bram Moolenaardfef1542012-07-10 19:25:10 +02005978#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005979 }
5980 }
5981
5982 buf = alloc(len + 1);
5983 if (buf == NULL)
5984 {
5985 *lenp = 0;
5986 return buf;
5987 }
5988
5989 p = arg;
5990 q = buf;
5991 *q++ = '"';
5992 while (*p)
5993 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00005994 if (p[0] == '\\' && p[1] == '\\')
5995 {
5996 *q++ = '\\';
5997 *q++ = '\\';
5998 p += 2;
5999 }
6000 else if (p[0] == '\\' && vim_iswhite(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006001 {
6002 *q++ = p[1];
6003 p += 2;
6004 }
6005 else if (*p == '\\' || *p == '"')
6006 {
6007 *q++ = '\\';
6008 *q++ = *p++;
6009 }
6010 else if (vim_iswhite(*p))
6011 {
6012 p = skipwhite(p);
6013 if (*p == NUL)
6014 break;
6015 *q++ = '"';
6016 *q++ = ',';
6017 *q++ = '"';
6018 }
6019 else
6020 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02006021 MB_COPY_CHAR(p, q);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006022 }
6023 }
6024 *q++ = '"';
6025 *q = 0;
6026
6027 *lenp = len;
6028 return buf;
6029}
6030
6031/*
6032 * Check for a <> code in a user command.
6033 * "code" points to the '<'. "len" the length of the <> (inclusive).
6034 * "buf" is where the result is to be added.
6035 * "split_buf" points to a buffer used for splitting, caller should free it.
6036 * "split_len" is the length of what "split_buf" contains.
6037 * Returns the length of the replacement, which has been added to "buf".
6038 * Returns -1 if there was no match, and only the "<" has been copied.
6039 */
6040 static size_t
6041uc_check_code(code, len, buf, cmd, eap, split_buf, split_len)
6042 char_u *code;
6043 size_t len;
6044 char_u *buf;
6045 ucmd_T *cmd; /* the user command we're expanding */
6046 exarg_T *eap; /* ex arguments */
6047 char_u **split_buf;
6048 size_t *split_len;
6049{
6050 size_t result = 0;
6051 char_u *p = code + 1;
6052 size_t l = len - 2;
6053 int quote = 0;
6054 enum { ct_ARGS, ct_BANG, ct_COUNT, ct_LINE1, ct_LINE2, ct_REGISTER,
6055 ct_LT, ct_NONE } type = ct_NONE;
6056
6057 if ((vim_strchr((char_u *)"qQfF", *p) != NULL) && p[1] == '-')
6058 {
6059 quote = (*p == 'q' || *p == 'Q') ? 1 : 2;
6060 p += 2;
6061 l -= 2;
6062 }
6063
Bram Moolenaar371d5402006-03-20 21:47:49 +00006064 ++l;
6065 if (l <= 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006066 type = ct_NONE;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006067 else if (STRNICMP(p, "args>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006068 type = ct_ARGS;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006069 else if (STRNICMP(p, "bang>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006070 type = ct_BANG;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006071 else if (STRNICMP(p, "count>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006072 type = ct_COUNT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006073 else if (STRNICMP(p, "line1>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006074 type = ct_LINE1;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006075 else if (STRNICMP(p, "line2>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006076 type = ct_LINE2;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006077 else if (STRNICMP(p, "lt>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006078 type = ct_LT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006079 else if (STRNICMP(p, "reg>", l) == 0 || STRNICMP(p, "register>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006080 type = ct_REGISTER;
6081
6082 switch (type)
6083 {
6084 case ct_ARGS:
6085 /* Simple case first */
6086 if (*eap->arg == NUL)
6087 {
6088 if (quote == 1)
6089 {
6090 result = 2;
6091 if (buf != NULL)
6092 STRCPY(buf, "''");
6093 }
6094 else
6095 result = 0;
6096 break;
6097 }
6098
6099 /* When specified there is a single argument don't split it.
6100 * Works for ":Cmd %" when % is "a b c". */
6101 if ((eap->argt & NOSPC) && quote == 2)
6102 quote = 1;
6103
6104 switch (quote)
6105 {
6106 case 0: /* No quoting, no splitting */
6107 result = STRLEN(eap->arg);
6108 if (buf != NULL)
6109 STRCPY(buf, eap->arg);
6110 break;
6111 case 1: /* Quote, but don't split */
6112 result = STRLEN(eap->arg) + 2;
6113 for (p = eap->arg; *p; ++p)
6114 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006115#ifdef FEAT_MBYTE
6116 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6117 /* DBCS can contain \ in a trail byte, skip the
6118 * double-byte character. */
6119 ++p;
6120 else
6121#endif
6122 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006123 ++result;
6124 }
6125
6126 if (buf != NULL)
6127 {
6128 *buf++ = '"';
6129 for (p = eap->arg; *p; ++p)
6130 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006131#ifdef FEAT_MBYTE
6132 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6133 /* DBCS can contain \ in a trail byte, copy the
6134 * double-byte character to avoid escaping. */
6135 *buf++ = *p++;
6136 else
6137#endif
6138 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006139 *buf++ = '\\';
6140 *buf++ = *p;
6141 }
6142 *buf = '"';
6143 }
6144
6145 break;
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006146 case 2: /* Quote and split (<f-args>) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006147 /* This is hard, so only do it once, and cache the result */
6148 if (*split_buf == NULL)
6149 *split_buf = uc_split_args(eap->arg, split_len);
6150
6151 result = *split_len;
6152 if (buf != NULL && result != 0)
6153 STRCPY(buf, *split_buf);
6154
6155 break;
6156 }
6157 break;
6158
6159 case ct_BANG:
6160 result = eap->forceit ? 1 : 0;
6161 if (quote)
6162 result += 2;
6163 if (buf != NULL)
6164 {
6165 if (quote)
6166 *buf++ = '"';
6167 if (eap->forceit)
6168 *buf++ = '!';
6169 if (quote)
6170 *buf = '"';
6171 }
6172 break;
6173
6174 case ct_LINE1:
6175 case ct_LINE2:
6176 case ct_COUNT:
6177 {
6178 char num_buf[20];
6179 long num = (type == ct_LINE1) ? eap->line1 :
6180 (type == ct_LINE2) ? eap->line2 :
6181 (eap->addr_count > 0) ? eap->line2 : cmd->uc_def;
6182 size_t num_len;
6183
6184 sprintf(num_buf, "%ld", num);
6185 num_len = STRLEN(num_buf);
6186 result = num_len;
6187
6188 if (quote)
6189 result += 2;
6190
6191 if (buf != NULL)
6192 {
6193 if (quote)
6194 *buf++ = '"';
6195 STRCPY(buf, num_buf);
6196 buf += num_len;
6197 if (quote)
6198 *buf = '"';
6199 }
6200
6201 break;
6202 }
6203
6204 case ct_REGISTER:
6205 result = eap->regname ? 1 : 0;
6206 if (quote)
6207 result += 2;
6208 if (buf != NULL)
6209 {
6210 if (quote)
6211 *buf++ = '\'';
6212 if (eap->regname)
6213 *buf++ = eap->regname;
6214 if (quote)
6215 *buf = '\'';
6216 }
6217 break;
6218
6219 case ct_LT:
6220 result = 1;
6221 if (buf != NULL)
6222 *buf = '<';
6223 break;
6224
6225 default:
6226 /* Not recognized: just copy the '<' and return -1. */
6227 result = (size_t)-1;
6228 if (buf != NULL)
6229 *buf = '<';
6230 break;
6231 }
6232
6233 return result;
6234}
6235
6236 static void
6237do_ucmd(eap)
6238 exarg_T *eap;
6239{
6240 char_u *buf;
6241 char_u *p;
6242 char_u *q;
6243
6244 char_u *start;
Bram Moolenaar25648a52009-02-21 19:37:46 +00006245 char_u *end = NULL;
Bram Moolenaara850a712009-01-28 14:42:59 +00006246 char_u *ksp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006247 size_t len, totlen;
6248
6249 size_t split_len = 0;
6250 char_u *split_buf = NULL;
6251 ucmd_T *cmd;
6252#ifdef FEAT_EVAL
6253 scid_T save_current_SID = current_SID;
6254#endif
6255
6256 if (eap->cmdidx == CMD_USER)
6257 cmd = USER_CMD(eap->useridx);
6258 else
6259 cmd = USER_CMD_GA(&curbuf->b_ucmds, eap->useridx);
6260
6261 /*
6262 * Replace <> in the command by the arguments.
Bram Moolenaara850a712009-01-28 14:42:59 +00006263 * First round: "buf" is NULL, compute length, allocate "buf".
6264 * Second round: copy result into "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006265 */
6266 buf = NULL;
6267 for (;;)
6268 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006269 p = cmd->uc_rep; /* source */
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00006270 q = buf; /* destination */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006271 totlen = 0;
Bram Moolenaara850a712009-01-28 14:42:59 +00006272
6273 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006274 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006275 start = vim_strchr(p, '<');
6276 if (start != NULL)
6277 end = vim_strchr(start + 1, '>');
6278 if (buf != NULL)
6279 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006280 for (ksp = p; *ksp != NUL && *ksp != K_SPECIAL; ++ksp)
6281 ;
6282 if (*ksp == K_SPECIAL
6283 && (start == NULL || ksp < start || end == NULL)
Bram Moolenaara850a712009-01-28 14:42:59 +00006284 && ((ksp[1] == KS_SPECIAL && ksp[2] == KE_FILLER)
6285# ifdef FEAT_GUI
6286 || (ksp[1] == KS_EXTRA && ksp[2] == (int)KE_CSI)
6287# endif
6288 ))
6289 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006290 /* K_SPECIAL has been put in the buffer as K_SPECIAL
Bram Moolenaara850a712009-01-28 14:42:59 +00006291 * KS_SPECIAL KE_FILLER, like for mappings, but
6292 * do_cmdline() doesn't handle that, so convert it back.
6293 * Also change K_SPECIAL KS_EXTRA KE_CSI into CSI. */
6294 len = ksp - p;
6295 if (len > 0)
6296 {
6297 mch_memmove(q, p, len);
6298 q += len;
6299 }
6300 *q++ = ksp[1] == KS_SPECIAL ? K_SPECIAL : CSI;
6301 p = ksp + 3;
6302 continue;
6303 }
6304 }
6305
6306 /* break if there no <item> is found */
6307 if (start == NULL || end == NULL)
6308 break;
6309
Bram Moolenaar071d4272004-06-13 20:20:40 +00006310 /* Include the '>' */
6311 ++end;
6312
6313 /* Take everything up to the '<' */
6314 len = start - p;
6315 if (buf == NULL)
6316 totlen += len;
6317 else
6318 {
6319 mch_memmove(q, p, len);
6320 q += len;
6321 }
6322
6323 len = uc_check_code(start, end - start, q, cmd, eap,
6324 &split_buf, &split_len);
6325 if (len == (size_t)-1)
6326 {
6327 /* no match, continue after '<' */
6328 p = start + 1;
6329 len = 1;
6330 }
6331 else
6332 p = end;
6333 if (buf == NULL)
6334 totlen += len;
6335 else
6336 q += len;
6337 }
6338 if (buf != NULL) /* second time here, finished */
6339 {
6340 STRCPY(q, p);
6341 break;
6342 }
6343
6344 totlen += STRLEN(p); /* Add on the trailing characters */
6345 buf = alloc((unsigned)(totlen + 1));
6346 if (buf == NULL)
6347 {
6348 vim_free(split_buf);
6349 return;
6350 }
6351 }
6352
6353#ifdef FEAT_EVAL
6354 current_SID = cmd->uc_scriptID;
6355#endif
6356 (void)do_cmdline(buf, eap->getline, eap->cookie,
6357 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
6358#ifdef FEAT_EVAL
6359 current_SID = save_current_SID;
6360#endif
6361 vim_free(buf);
6362 vim_free(split_buf);
6363}
6364
6365# if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6366 static char_u *
6367get_user_command_name(idx)
6368 int idx;
6369{
6370 return get_user_commands(NULL, idx - (int)CMD_SIZE);
6371}
6372
6373/*
6374 * Function given to ExpandGeneric() to obtain the list of user command names.
6375 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006376 char_u *
6377get_user_commands(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006378 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006379 int idx;
6380{
6381 if (idx < curbuf->b_ucmds.ga_len)
6382 return USER_CMD_GA(&curbuf->b_ucmds, idx)->uc_name;
6383 idx -= curbuf->b_ucmds.ga_len;
6384 if (idx < ucmds.ga_len)
6385 return USER_CMD(idx)->uc_name;
6386 return NULL;
6387}
6388
6389/*
6390 * Function given to ExpandGeneric() to obtain the list of user command
6391 * attributes.
6392 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006393 char_u *
6394get_user_cmd_flags(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006395 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006396 int idx;
6397{
6398 static char *user_cmd_flags[] =
6399 {"bang", "bar", "buffer", "complete", "count",
6400 "nargs", "range", "register"};
6401
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00006402 if (idx >= (int)(sizeof(user_cmd_flags) / sizeof(user_cmd_flags[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006403 return NULL;
6404 return (char_u *)user_cmd_flags[idx];
6405}
6406
6407/*
6408 * Function given to ExpandGeneric() to obtain the list of values for -nargs.
6409 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006410 char_u *
6411get_user_cmd_nargs(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006412 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006413 int idx;
6414{
6415 static char *user_cmd_nargs[] = {"0", "1", "*", "?", "+"};
6416
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00006417 if (idx >= (int)(sizeof(user_cmd_nargs) / sizeof(user_cmd_nargs[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006418 return NULL;
6419 return (char_u *)user_cmd_nargs[idx];
6420}
6421
6422/*
6423 * Function given to ExpandGeneric() to obtain the list of values for -complete.
6424 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006425 char_u *
6426get_user_cmd_complete(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006427 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006428 int idx;
6429{
6430 return (char_u *)command_complete[idx].name;
6431}
6432# endif /* FEAT_CMDL_COMPL */
6433
6434#endif /* FEAT_USR_CMDS */
6435
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006436#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
6437/*
6438 * Parse a completion argument "value[vallen]".
6439 * The detected completion goes in "*complp", argument type in "*argt".
6440 * When there is an argument, for function and user defined completion, it's
6441 * copied to allocated memory and stored in "*compl_arg".
6442 * Returns FAIL if something is wrong.
6443 */
6444 int
6445parse_compl_arg(value, vallen, complp, argt, compl_arg)
6446 char_u *value;
6447 int vallen;
6448 int *complp;
6449 long *argt;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006450 char_u **compl_arg UNUSED;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006451{
6452 char_u *arg = NULL;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006453# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006454 size_t arglen = 0;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006455# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006456 int i;
6457 int valend = vallen;
6458
6459 /* Look for any argument part - which is the part after any ',' */
6460 for (i = 0; i < vallen; ++i)
6461 {
6462 if (value[i] == ',')
6463 {
6464 arg = &value[i + 1];
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006465# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006466 arglen = vallen - i - 1;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006467# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006468 valend = i;
6469 break;
6470 }
6471 }
6472
6473 for (i = 0; command_complete[i].expand != 0; ++i)
6474 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006475 if ((int)STRLEN(command_complete[i].name) == valend
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006476 && STRNCMP(value, command_complete[i].name, valend) == 0)
6477 {
6478 *complp = command_complete[i].expand;
6479 if (command_complete[i].expand == EXPAND_BUFFERS)
6480 *argt |= BUFNAME;
6481 else if (command_complete[i].expand == EXPAND_DIRECTORIES
6482 || command_complete[i].expand == EXPAND_FILES)
6483 *argt |= XFILE;
6484 break;
6485 }
6486 }
6487
6488 if (command_complete[i].expand == 0)
6489 {
6490 EMSG2(_("E180: Invalid complete value: %s"), value);
6491 return FAIL;
6492 }
6493
6494# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6495 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST
6496 && arg != NULL)
6497# else
6498 if (arg != NULL)
6499# endif
6500 {
6501 EMSG(_("E468: Completion argument only allowed for custom completion"));
6502 return FAIL;
6503 }
6504
6505# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6506 if ((*complp == EXPAND_USER_DEFINED || *complp == EXPAND_USER_LIST)
6507 && arg == NULL)
6508 {
6509 EMSG(_("E467: Custom completion requires a function argument"));
6510 return FAIL;
6511 }
6512
6513 if (arg != NULL)
6514 *compl_arg = vim_strnsave(arg, (int)arglen);
6515# endif
6516 return OK;
6517}
6518#endif
6519
Bram Moolenaar071d4272004-06-13 20:20:40 +00006520 static void
6521ex_colorscheme(eap)
6522 exarg_T *eap;
6523{
Bram Moolenaare6850792010-05-14 15:28:44 +02006524 if (*eap->arg == NUL)
6525 {
6526#ifdef FEAT_EVAL
6527 char_u *expr = vim_strsave((char_u *)"g:colors_name");
6528 char_u *p = NULL;
6529
6530 if (expr != NULL)
6531 {
6532 ++emsg_off;
6533 p = eval_to_string(expr, NULL, FALSE);
6534 --emsg_off;
6535 vim_free(expr);
6536 }
6537 if (p != NULL)
6538 {
6539 MSG(p);
6540 vim_free(p);
6541 }
6542 else
6543 MSG("default");
6544#else
6545 MSG(_("unknown"));
6546#endif
6547 }
6548 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarbe1e9e92012-09-18 16:47:07 +02006549 EMSG2(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006550}
6551
6552 static void
6553ex_highlight(eap)
6554 exarg_T *eap;
6555{
6556 if (*eap->arg == NUL && eap->cmd[2] == '!')
6557 MSG(_("Greetings, Vim user!"));
6558 do_highlight(eap->arg, eap->forceit, FALSE);
6559}
6560
6561
6562/*
6563 * Call this function if we thought we were going to exit, but we won't
6564 * (because of an error). May need to restore the terminal mode.
6565 */
6566 void
6567not_exiting()
6568{
6569 exiting = FALSE;
6570 settmode(TMODE_RAW);
6571}
6572
6573/*
6574 * ":quit": quit current window, quit Vim if closed the last window.
6575 */
6576 static void
6577ex_quit(eap)
6578 exarg_T *eap;
6579{
6580#ifdef FEAT_CMDWIN
6581 if (cmdwin_type != 0)
6582 {
6583 cmdwin_result = Ctrl_C;
6584 return;
6585 }
6586#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006587 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006588 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006589 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006590 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006591 return;
6592 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006593#ifdef FEAT_AUTOCMD
Bram Moolenaar3b53dfb2012-06-06 18:03:07 +02006594 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01006595 /* Refuse to quit when locked or when the buffer in the last window is
Bram Moolenaar362ce482012-06-06 19:02:45 +02006596 * being closed (can only happen in autocommands). */
6597 if (curbuf_locked() || (curbuf->b_nwindows == 1 && curbuf->b_closing))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006598 return;
6599#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006600
6601#ifdef FEAT_NETBEANS_INTG
6602 netbeansForcedQuit = eap->forceit;
6603#endif
6604
6605 /*
6606 * If there are more files or windows we won't exit.
6607 */
6608 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6609 exiting = TRUE;
6610 if ((!P_HID(curbuf)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01006611 && check_changed(curbuf, (p_awa ? CCGD_AW : 0)
6612 | (eap->forceit ? CCGD_FORCEIT : 0)
6613 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006614 || check_more(TRUE, eap->forceit) == FAIL
6615 || (only_one_window() && check_changed_any(eap->forceit)))
6616 {
6617 not_exiting();
6618 }
6619 else
6620 {
6621#ifdef FEAT_WINDOWS
6622 if (only_one_window()) /* quit last window */
6623#endif
6624 getout(0);
6625#ifdef FEAT_WINDOWS
6626# ifdef FEAT_GUI
6627 need_mouse_correct = TRUE;
6628# endif
6629 /* close window; may free buffer */
6630 win_close(curwin, !P_HID(curwin->w_buffer) || eap->forceit);
6631#endif
6632 }
6633}
6634
6635/*
6636 * ":cquit".
6637 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006638 static void
6639ex_cquit(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006640 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006641{
6642 getout(1); /* this does not always pass on the exit code to the Manx
6643 compiler. why? */
6644}
6645
6646/*
6647 * ":qall": try to quit all windows
6648 */
6649 static void
6650ex_quit_all(eap)
6651 exarg_T *eap;
6652{
6653# ifdef FEAT_CMDWIN
6654 if (cmdwin_type != 0)
6655 {
6656 if (eap->forceit)
6657 cmdwin_result = K_XF1; /* ex_window() takes care of this */
6658 else
6659 cmdwin_result = K_XF2;
6660 return;
6661 }
6662# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006663
6664 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006665 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006666 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006667 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006668 return;
6669 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006670#ifdef FEAT_AUTOCMD
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01006671 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
6672 /* Refuse to quit when locked or when the buffer in the last window is
6673 * being closed (can only happen in autocommands). */
6674 if (curbuf_locked() || (curbuf->b_nwindows == 1 && curbuf->b_closing))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006675 return;
6676#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006677
Bram Moolenaar071d4272004-06-13 20:20:40 +00006678 exiting = TRUE;
6679 if (eap->forceit || !check_changed_any(FALSE))
6680 getout(0);
6681 not_exiting();
6682}
6683
Bram Moolenaard9967712006-03-11 21:18:15 +00006684#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006685/*
6686 * ":close": close current window, unless it is the last one
6687 */
6688 static void
6689ex_close(eap)
6690 exarg_T *eap;
6691{
6692# ifdef FEAT_CMDWIN
6693 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02006694 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006695 else
6696# endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006697 if (!text_locked()
6698#ifdef FEAT_AUTOCMD
6699 && !curbuf_locked()
6700#endif
6701 )
Bram Moolenaarf740b292006-02-16 22:11:02 +00006702 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006703}
6704
Bram Moolenaard9967712006-03-11 21:18:15 +00006705# ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006706/*
6707 * ":pclose": Close any preview window.
6708 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006709 static void
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006710ex_pclose(eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006711 exarg_T *eap;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006712{
6713 win_T *win;
6714
6715 for (win = firstwin; win != NULL; win = win->w_next)
6716 if (win->w_p_pvw)
6717 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006718 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006719 break;
6720 }
6721}
Bram Moolenaard9967712006-03-11 21:18:15 +00006722# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006723
Bram Moolenaarf740b292006-02-16 22:11:02 +00006724/*
6725 * Close window "win" and take care of handling closing the last window for a
6726 * modified buffer.
6727 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006728 static void
Bram Moolenaarf740b292006-02-16 22:11:02 +00006729ex_win_close(forceit, win, tp)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006730 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006731 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006732 tabpage_T *tp; /* NULL or the tab page "win" is in */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006733{
6734 int need_hide;
6735 buf_T *buf = win->w_buffer;
6736
6737 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006738 if (need_hide && !P_HID(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006739 {
Bram Moolenaard9967712006-03-11 21:18:15 +00006740# if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006741 if ((p_confirm || cmdmod.confirm) && p_write)
6742 {
6743 dialog_changed(buf, FALSE);
6744 if (buf_valid(buf) && bufIsChanged(buf))
6745 return;
6746 need_hide = FALSE;
6747 }
6748 else
Bram Moolenaard9967712006-03-11 21:18:15 +00006749# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006750 {
6751 EMSG(_(e_nowrtmsg));
6752 return;
6753 }
6754 }
6755
Bram Moolenaard9967712006-03-11 21:18:15 +00006756# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006757 need_mouse_correct = TRUE;
Bram Moolenaard9967712006-03-11 21:18:15 +00006758# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006759
Bram Moolenaar071d4272004-06-13 20:20:40 +00006760 /* free buffer when not hiding it or when it's a scratch buffer */
Bram Moolenaarf740b292006-02-16 22:11:02 +00006761 if (tp == NULL)
6762 win_close(win, !need_hide && !P_HID(buf));
6763 else
6764 win_close_othertab(win, !need_hide && !P_HID(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006765}
6766
Bram Moolenaar071d4272004-06-13 20:20:40 +00006767/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006768 * ":tabclose": close current tab page, unless it is the last one.
6769 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006770 */
6771 static void
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006772ex_tabclose(eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006773 exarg_T *eap;
6774{
Bram Moolenaarf740b292006-02-16 22:11:02 +00006775 tabpage_T *tp;
6776
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006777# ifdef FEAT_CMDWIN
6778 if (cmdwin_type != 0)
6779 cmdwin_result = K_IGNORE;
6780 else
6781# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006782 if (first_tabpage->tp_next == NULL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00006783 EMSG(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00006784 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006785 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006786 if (eap->addr_count > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006787 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006788 tp = find_tabpage((int)eap->line2);
6789 if (tp == NULL)
6790 {
6791 beep_flush();
6792 return;
6793 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00006794 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006795 {
6796 tabpage_close_other(tp, eap->forceit);
6797 return;
6798 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006799 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006800 if (!text_locked()
6801#ifdef FEAT_AUTOCMD
6802 && !curbuf_locked()
6803#endif
6804 )
Bram Moolenaarf740b292006-02-16 22:11:02 +00006805 tabpage_close(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006806 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006807}
6808
6809/*
6810 * ":tabonly": close all tab pages except the current one
6811 */
6812 static void
6813ex_tabonly(eap)
6814 exarg_T *eap;
6815{
6816 tabpage_T *tp;
6817 int done;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006818
6819# ifdef FEAT_CMDWIN
6820 if (cmdwin_type != 0)
6821 cmdwin_result = K_IGNORE;
6822 else
6823# endif
6824 if (first_tabpage->tp_next == NULL)
6825 MSG(_("Already only one tab page"));
6826 else
6827 {
6828 /* Repeat this up to a 1000 times, because autocommands may mess
6829 * up the lists. */
6830 for (done = 0; done < 1000; ++done)
6831 {
6832 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6833 if (tp->tp_topframe != topframe)
6834 {
6835 tabpage_close_other(tp, eap->forceit);
6836 /* if we failed to close it quit */
6837 if (valid_tabpage(tp))
6838 done = 1000;
6839 /* start over, "tp" is now invalid */
6840 break;
6841 }
6842 if (first_tabpage->tp_next == NULL)
6843 break;
6844 }
6845 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006846}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006847
6848/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006849 * Close the current tab page.
6850 */
6851 void
6852tabpage_close(forceit)
6853 int forceit;
6854{
6855 /* First close all the windows but the current one. If that worked then
6856 * close the last window in this tab, that will close it. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006857 if (lastwin != firstwin)
6858 close_others(TRUE, forceit);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006859 if (lastwin == firstwin)
6860 ex_win_close(forceit, curwin, NULL);
6861# ifdef FEAT_GUI
6862 need_mouse_correct = TRUE;
6863# endif
6864}
6865
6866/*
6867 * Close tab page "tp", which is not the current tab page.
6868 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006869 * Also takes care of the tab pages line disappearing when closing the
6870 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00006871 */
6872 void
6873tabpage_close_other(tp, forceit)
6874 tabpage_T *tp;
6875 int forceit;
6876{
6877 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006878 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006879 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006880
6881 /* Limit to 1000 windows, autocommands may add a window while we close
6882 * one. OK, so I'm paranoid... */
6883 while (++done < 1000)
6884 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006885 wp = tp->tp_firstwin;
6886 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006887
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006888 /* Autocommands may delete the tab page under our fingers and we may
6889 * fail to close a window with a modified buffer. */
6890 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006891 break;
6892 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006893
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006894 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006895 if (h != tabline_height())
6896 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006897}
6898
6899/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006900 * ":only".
6901 */
6902 static void
6903ex_only(eap)
6904 exarg_T *eap;
6905{
6906# ifdef FEAT_GUI
6907 need_mouse_correct = TRUE;
6908# endif
6909 close_others(TRUE, eap->forceit);
6910}
6911
6912/*
6913 * ":all" and ":sall".
Bram Moolenaard9967712006-03-11 21:18:15 +00006914 * Also used for ":tab drop file ..." after setting the argument list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006915 */
Bram Moolenaard9967712006-03-11 21:18:15 +00006916 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00006917ex_all(eap)
6918 exarg_T *eap;
6919{
6920 if (eap->addr_count == 0)
6921 eap->line2 = 9999;
Bram Moolenaard9967712006-03-11 21:18:15 +00006922 do_arg_all((int)eap->line2, eap->forceit, eap->cmdidx == CMD_drop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006923}
6924#endif /* FEAT_WINDOWS */
6925
6926 static void
6927ex_hide(eap)
6928 exarg_T *eap;
6929{
6930 if (*eap->arg != NUL && check_nextcmd(eap->arg) == NULL)
6931 eap->errmsg = e_invarg;
6932 else
6933 {
6934 /* ":hide" or ":hide | cmd": hide current window */
6935 eap->nextcmd = check_nextcmd(eap->arg);
6936#ifdef FEAT_WINDOWS
6937 if (!eap->skip)
6938 {
6939# ifdef FEAT_GUI
6940 need_mouse_correct = TRUE;
6941# endif
6942 win_close(curwin, FALSE); /* don't free buffer */
6943 }
6944#endif
6945 }
6946}
6947
6948/*
6949 * ":stop" and ":suspend": Suspend Vim.
6950 */
6951 static void
6952ex_stop(eap)
6953 exarg_T *eap;
6954{
6955 /*
6956 * Disallow suspending for "rvim".
6957 */
6958 if (!check_restricted()
6959#ifdef WIN3264
6960 /*
6961 * Check if external commands are allowed now.
6962 */
6963 && can_end_termcap_mode(TRUE)
6964#endif
6965 )
6966 {
6967 if (!eap->forceit)
6968 autowrite_all();
6969 windgoto((int)Rows - 1, 0);
6970 out_char('\n');
6971 out_flush();
6972 stoptermcap();
6973 out_flush(); /* needed for SUN to restore xterm buffer */
6974#ifdef FEAT_TITLE
6975 mch_restore_title(3); /* restore window titles */
6976#endif
6977 ui_suspend(); /* call machine specific function */
6978#ifdef FEAT_TITLE
6979 maketitle();
6980 resettitle(); /* force updating the title */
6981#endif
6982 starttermcap();
6983 scroll_start(); /* scroll screen before redrawing */
6984 redraw_later_clear();
6985 shell_resized(); /* may have resized window */
6986 }
6987}
6988
6989/*
6990 * ":exit", ":xit" and ":wq": Write file and exit Vim.
6991 */
6992 static void
6993ex_exit(eap)
6994 exarg_T *eap;
6995{
6996#ifdef FEAT_CMDWIN
6997 if (cmdwin_type != 0)
6998 {
6999 cmdwin_result = Ctrl_C;
7000 return;
7001 }
7002#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007003 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007004 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007005 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007006 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007007 return;
7008 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007009#ifdef FEAT_AUTOCMD
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01007010 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
7011 /* Refuse to quit when locked or when the buffer in the last window is
7012 * being closed (can only happen in autocommands). */
7013 if (curbuf_locked() || (curbuf->b_nwindows == 1 && curbuf->b_closing))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007014 return;
7015#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007016
7017 /*
7018 * if more files or windows we won't exit
7019 */
7020 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
7021 exiting = TRUE;
7022 if ( ((eap->cmdidx == CMD_wq
7023 || curbufIsChanged())
7024 && do_write(eap) == FAIL)
7025 || check_more(TRUE, eap->forceit) == FAIL
7026 || (only_one_window() && check_changed_any(eap->forceit)))
7027 {
7028 not_exiting();
7029 }
7030 else
7031 {
7032#ifdef FEAT_WINDOWS
7033 if (only_one_window()) /* quit last window, exit Vim */
7034#endif
7035 getout(0);
7036#ifdef FEAT_WINDOWS
7037# ifdef FEAT_GUI
7038 need_mouse_correct = TRUE;
7039# endif
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02007040 /* Quit current window, may free the buffer. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007041 win_close(curwin, !P_HID(curwin->w_buffer));
7042#endif
7043 }
7044}
7045
7046/*
7047 * ":print", ":list", ":number".
7048 */
7049 static void
7050ex_print(eap)
7051 exarg_T *eap;
7052{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007053 if (curbuf->b_ml.ml_flags & ML_EMPTY)
7054 EMSG(_(e_emptybuf));
7055 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007056 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007057 for ( ;!got_int; ui_breakcheck())
7058 {
7059 print_line(eap->line1,
7060 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
7061 || (eap->flags & EXFLAG_NR)),
7062 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
7063 if (++eap->line1 > eap->line2)
7064 break;
7065 out_flush(); /* show one line at a time */
7066 }
7067 setpcmark();
7068 /* put cursor at last line */
7069 curwin->w_cursor.lnum = eap->line2;
7070 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007071 }
7072
Bram Moolenaar071d4272004-06-13 20:20:40 +00007073 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007074}
7075
7076#ifdef FEAT_BYTEOFF
7077 static void
7078ex_goto(eap)
7079 exarg_T *eap;
7080{
7081 goto_byte(eap->line2);
7082}
7083#endif
7084
7085/*
7086 * ":shell".
7087 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007088 static void
7089ex_shell(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007090 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007091{
7092 do_shell(NULL, 0);
7093}
7094
7095#if (defined(FEAT_WINDOWS) && defined(HAVE_DROP_FILE)) \
7096 || (defined(FEAT_GUI_GTK) && defined(FEAT_DND)) \
Bram Moolenaar371baa92005-12-29 22:43:53 +00007097 || defined(FEAT_GUI_MSWIN) \
7098 || defined(FEAT_GUI_MAC) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00007099 || defined(PROTO)
7100
7101/*
7102 * Handle a file drop. The code is here because a drop is *nearly* like an
7103 * :args command, but not quite (we have a list of exact filenames, so we
7104 * don't want to (a) parse a command line, or (b) expand wildcards. So the
7105 * code is very similar to :args and hence needs access to a lot of the static
7106 * functions in this file.
7107 *
7108 * The list should be allocated using alloc(), as should each item in the
7109 * list. This function takes over responsibility for freeing the list.
7110 *
Bram Moolenaar81870892007-11-11 18:17:28 +00007111 * XXX The list is made into the argument list. This is freed using
Bram Moolenaar071d4272004-06-13 20:20:40 +00007112 * FreeWild(), which does a series of vim_free() calls, unless the two defines
7113 * __EMX__ and __ALWAYS_HAS_TRAILING_NUL_POINTER are set. In this case, a
7114 * routine _fnexplodefree() is used. This may cause problems, but as the drop
7115 * file functionality is (currently) not in EMX this is not presently a
7116 * problem.
7117 */
7118 void
7119handle_drop(filec, filev, split)
7120 int filec; /* the number of files dropped */
7121 char_u **filev; /* the list of files dropped */
7122 int split; /* force splitting the window */
7123{
7124 exarg_T ea;
7125 int save_msg_scroll = msg_scroll;
7126
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007127 /* Postpone this while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007128 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007129 return;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007130#ifdef FEAT_AUTOCMD
7131 if (curbuf_locked())
7132 return;
7133#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00007134 /* When the screen is being updated we should not change buffers and
7135 * windows structures, it may cause freed memory to be used. */
7136 if (updating_screen)
7137 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007138
7139 /* Check whether the current buffer is changed. If so, we will need
7140 * to split the current window or data could be lost.
7141 * We don't need to check if the 'hidden' option is set, as in this
7142 * case the buffer won't be lost.
7143 */
7144 if (!P_HID(curbuf) && !split)
7145 {
7146 ++emsg_off;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01007147 split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007148 --emsg_off;
7149 }
7150 if (split)
7151 {
7152# ifdef FEAT_WINDOWS
7153 if (win_split(0, 0) == FAIL)
7154 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007155 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007156
7157 /* When splitting the window, create a new alist. Otherwise the
7158 * existing one is overwritten. */
7159 alist_unlink(curwin->w_alist);
7160 alist_new();
7161# else
7162 return; /* can't split, always fail */
7163# endif
7164 }
7165
7166 /*
7167 * Set up the new argument list.
7168 */
Bram Moolenaar86b68352004-12-27 21:59:20 +00007169 alist_set(ALIST(curwin), filec, filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007170
7171 /*
7172 * Move to the first file.
7173 */
7174 /* Fake up a minimal "next" command for do_argfile() */
7175 vim_memset(&ea, 0, sizeof(ea));
7176 ea.cmd = (char_u *)"next";
7177 do_argfile(&ea, 0);
7178
7179 /* do_ecmd() may set need_start_insertmode, but since we never left Insert
7180 * mode that is not needed here. */
7181 need_start_insertmode = FALSE;
7182
7183 /* Restore msg_scroll, otherwise a following command may cause scrolling
7184 * unexpectedly. The screen will be redrawn by the caller, thus
7185 * msg_scroll being set by displaying a message is irrelevant. */
7186 msg_scroll = save_msg_scroll;
7187}
7188#endif
7189
Bram Moolenaar071d4272004-06-13 20:20:40 +00007190/*
7191 * Clear an argument list: free all file names and reset it to zero entries.
7192 */
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007193 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00007194alist_clear(al)
7195 alist_T *al;
7196{
7197 while (--al->al_ga.ga_len >= 0)
7198 vim_free(AARGLIST(al)[al->al_ga.ga_len].ae_fname);
7199 ga_clear(&al->al_ga);
7200}
7201
7202/*
7203 * Init an argument list.
7204 */
7205 void
7206alist_init(al)
7207 alist_T *al;
7208{
7209 ga_init2(&al->al_ga, (int)sizeof(aentry_T), 5);
7210}
7211
7212#if defined(FEAT_WINDOWS) || defined(PROTO)
7213
7214/*
7215 * Remove a reference from an argument list.
7216 * Ignored when the argument list is the global one.
7217 * If the argument list is no longer used by any window, free it.
7218 */
7219 void
7220alist_unlink(al)
7221 alist_T *al;
7222{
7223 if (al != &global_alist && --al->al_refcount <= 0)
7224 {
7225 alist_clear(al);
7226 vim_free(al);
7227 }
7228}
7229
7230# if defined(FEAT_LISTCMDS) || defined(HAVE_DROP_FILE) || defined(PROTO)
7231/*
7232 * Create a new argument list and use it for the current window.
7233 */
7234 void
7235alist_new()
7236{
7237 curwin->w_alist = (alist_T *)alloc((unsigned)sizeof(alist_T));
7238 if (curwin->w_alist == NULL)
7239 {
7240 curwin->w_alist = &global_alist;
7241 ++global_alist.al_refcount;
7242 }
7243 else
7244 {
7245 curwin->w_alist->al_refcount = 1;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02007246 curwin->w_alist->id = ++max_alist_id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007247 alist_init(curwin->w_alist);
7248 }
7249}
7250# endif
7251#endif
7252
7253#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE) || defined(PROTO)
7254/*
7255 * Expand the file names in the global argument list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00007256 * If "fnum_list" is not NULL, use "fnum_list[fnum_len]" as a list of buffer
7257 * numbers to be re-used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007258 */
7259 void
Bram Moolenaar86b68352004-12-27 21:59:20 +00007260alist_expand(fnum_list, fnum_len)
7261 int *fnum_list;
7262 int fnum_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007263{
7264 char_u **old_arg_files;
Bram Moolenaar86b68352004-12-27 21:59:20 +00007265 int old_arg_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007266 char_u **new_arg_files;
7267 int new_arg_file_count;
7268 char_u *save_p_su = p_su;
7269 int i;
7270
7271 /* Don't use 'suffixes' here. This should work like the shell did the
7272 * expansion. Also, the vimrc file isn't read yet, thus the user
7273 * can't set the options. */
7274 p_su = empty_option;
7275 old_arg_files = (char_u **)alloc((unsigned)(sizeof(char_u *) * GARGCOUNT));
7276 if (old_arg_files != NULL)
7277 {
7278 for (i = 0; i < GARGCOUNT; ++i)
Bram Moolenaar86b68352004-12-27 21:59:20 +00007279 old_arg_files[i] = vim_strsave(GARGLIST[i].ae_fname);
7280 old_arg_count = GARGCOUNT;
7281 if (expand_wildcards(old_arg_count, old_arg_files,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007282 &new_arg_file_count, &new_arg_files,
Bram Moolenaarb5609832011-07-20 15:04:58 +02007283 EW_FILE|EW_NOTFOUND|EW_ADDSLASH|EW_NOERROR) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007284 && new_arg_file_count > 0)
7285 {
Bram Moolenaar86b68352004-12-27 21:59:20 +00007286 alist_set(&global_alist, new_arg_file_count, new_arg_files,
7287 TRUE, fnum_list, fnum_len);
7288 FreeWild(old_arg_count, old_arg_files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007289 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007290 }
7291 p_su = save_p_su;
7292}
7293#endif
7294
7295/*
7296 * Set the argument list for the current window.
7297 * Takes over the allocated files[] and the allocated fnames in it.
7298 */
7299 void
Bram Moolenaar86b68352004-12-27 21:59:20 +00007300alist_set(al, count, files, use_curbuf, fnum_list, fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007301 alist_T *al;
7302 int count;
7303 char_u **files;
7304 int use_curbuf;
Bram Moolenaar86b68352004-12-27 21:59:20 +00007305 int *fnum_list;
7306 int fnum_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007307{
7308 int i;
7309
7310 alist_clear(al);
7311 if (ga_grow(&al->al_ga, count) == OK)
7312 {
7313 for (i = 0; i < count; ++i)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007314 {
7315 if (got_int)
7316 {
7317 /* When adding many buffers this can take a long time. Allow
7318 * interrupting here. */
7319 while (i < count)
7320 vim_free(files[i++]);
7321 break;
7322 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00007323
7324 /* May set buffer name of a buffer previously used for the
7325 * argument list, so that it's re-used by alist_add. */
7326 if (fnum_list != NULL && i < fnum_len)
7327 buf_set_name(fnum_list[i], files[i]);
7328
Bram Moolenaar071d4272004-06-13 20:20:40 +00007329 alist_add(al, files[i], use_curbuf ? 2 : 1);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007330 ui_breakcheck();
7331 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007332 vim_free(files);
7333 }
7334 else
7335 FreeWild(count, files);
7336#ifdef FEAT_WINDOWS
7337 if (al == &global_alist)
7338#endif
7339 arg_had_last = FALSE;
7340}
7341
7342/*
7343 * Add file "fname" to argument list "al".
7344 * "fname" must have been allocated and "al" must have been checked for room.
7345 */
7346 void
7347alist_add(al, fname, set_fnum)
7348 alist_T *al;
7349 char_u *fname;
7350 int set_fnum; /* 1: set buffer number; 2: re-use curbuf */
7351{
7352 if (fname == NULL) /* don't add NULL file names */
7353 return;
7354#ifdef BACKSLASH_IN_FILENAME
7355 slash_adjust(fname);
7356#endif
7357 AARGLIST(al)[al->al_ga.ga_len].ae_fname = fname;
7358 if (set_fnum > 0)
7359 AARGLIST(al)[al->al_ga.ga_len].ae_fnum =
7360 buflist_add(fname, BLN_LISTED | (set_fnum == 2 ? BLN_CURBUF : 0));
7361 ++al->al_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007362}
7363
7364#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
7365/*
7366 * Adjust slashes in file names. Called after 'shellslash' was set.
7367 */
7368 void
7369alist_slash_adjust()
7370{
7371 int i;
7372# ifdef FEAT_WINDOWS
7373 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007374 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007375# endif
7376
7377 for (i = 0; i < GARGCOUNT; ++i)
7378 if (GARGLIST[i].ae_fname != NULL)
7379 slash_adjust(GARGLIST[i].ae_fname);
7380# ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00007381 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007382 if (wp->w_alist != &global_alist)
7383 for (i = 0; i < WARGCOUNT(wp); ++i)
7384 if (WARGLIST(wp)[i].ae_fname != NULL)
7385 slash_adjust(WARGLIST(wp)[i].ae_fname);
7386# endif
7387}
7388#endif
7389
7390/*
7391 * ":preserve".
7392 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007393 static void
7394ex_preserve(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007395 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007396{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007397 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007398 ml_preserve(curbuf, TRUE);
7399}
7400
7401/*
7402 * ":recover".
7403 */
7404 static void
7405ex_recover(eap)
7406 exarg_T *eap;
7407{
7408 /* Set recoverymode right away to avoid the ATTENTION prompt. */
7409 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01007410 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
7411 | CCGD_MULTWIN
7412 | (eap->forceit ? CCGD_FORCEIT : 0)
7413 | CCGD_EXCMD)
7414
Bram Moolenaar071d4272004-06-13 20:20:40 +00007415 && (*eap->arg == NUL
7416 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
7417 ml_recover();
7418 recoverymode = FALSE;
7419}
7420
7421/*
7422 * Command modifier used in a wrong way.
7423 */
7424 static void
7425ex_wrongmodifier(eap)
7426 exarg_T *eap;
7427{
7428 eap->errmsg = e_invcmd;
7429}
7430
7431#ifdef FEAT_WINDOWS
7432/*
7433 * :sview [+command] file split window with new file, read-only
7434 * :split [[+command] file] split window with current or new file
7435 * :vsplit [[+command] file] split window vertically with current or new file
7436 * :new [[+command] file] split window with no or new file
7437 * :vnew [[+command] file] split vertically window with no or new file
7438 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007439 *
7440 * :tabedit open new Tab page with empty window
7441 * :tabedit [+command] file open new Tab page and edit "file"
7442 * :tabnew [[+command] file] just like :tabedit
7443 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007444 */
7445 void
7446ex_splitview(eap)
7447 exarg_T *eap;
7448{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007449 win_T *old_curwin = curwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007450# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007451 char_u *fname = NULL;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007452# endif
7453# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007454 int browse_flag = cmdmod.browse;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007455# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007456
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007457# ifndef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007458 if (eap->cmdidx == CMD_vsplit || eap->cmdidx == CMD_vnew)
7459 {
7460 ex_ni(eap);
7461 return;
7462 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007463# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007464
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007465# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007466 need_mouse_correct = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007467# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007468
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007469# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00007470 /* A ":split" in the quickfix window works like ":new". Don't want two
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007471 * quickfix windows. But it's OK when doing ":tab split". */
7472 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007473 {
7474 if (eap->cmdidx == CMD_split)
7475 eap->cmdidx = CMD_new;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007476# ifdef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007477 if (eap->cmdidx == CMD_vsplit)
7478 eap->cmdidx = CMD_vnew;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007479# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007480 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007481# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007482
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007483# ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007484 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007485 {
7486 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
7487 FNAME_MESS, TRUE, curbuf->b_ffname);
7488 if (fname == NULL)
7489 goto theend;
7490 eap->arg = fname;
7491 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007492# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007493 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007494# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007495# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007496# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007497 if (cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007498# ifdef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007499 && eap->cmdidx != CMD_vnew
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007500# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007501 && eap->cmdidx != CMD_new)
7502 {
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007503# ifdef FEAT_AUTOCMD
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007504 if (
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007505# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007506 !gui.in_use &&
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007507# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007508 au_has_group((char_u *)"FileExplorer"))
7509 {
7510 /* No browsing supported but we do have the file explorer:
7511 * Edit the directory. */
7512 if (*eap->arg == NUL || !mch_isdir(eap->arg))
7513 eap->arg = (char_u *)".";
7514 }
7515 else
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007516# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007517 {
7518 fname = do_browse(0, (char_u *)_("Edit File in new window"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007519 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007520 if (fname == NULL)
7521 goto theend;
7522 eap->arg = fname;
7523 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007524 }
7525 cmdmod.browse = FALSE; /* Don't browse again in do_ecmd(). */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007526# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007527
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007528 /*
7529 * Either open new tab page or split the window.
7530 */
7531 if (eap->cmdidx == CMD_tabedit
7532 || eap->cmdidx == CMD_tabfind
7533 || eap->cmdidx == CMD_tabnew)
7534 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00007535 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
7536 : eap->addr_count == 0 ? 0
7537 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007538 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00007539 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007540
7541 /* set the alternate buffer for the window we came from */
7542 if (curwin != old_curwin
7543 && win_valid(old_curwin)
7544 && old_curwin->w_buffer != curbuf
7545 && !cmdmod.keepalt)
7546 old_curwin->w_alt_fnum = curbuf->b_fnum;
7547 }
7548 }
7549 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007550 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
7551 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007552# ifdef FEAT_SCROLLBIND
Bram Moolenaar071d4272004-06-13 20:20:40 +00007553 /* Reset 'scrollbind' when editing another file, but keep it when
7554 * doing ":split" without arguments. */
7555 if (*eap->arg != NUL
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007556# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007557 || cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007558# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007559 )
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007560 {
7561 RESET_BINDING(curwin);
7562 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007563 else
7564 do_check_scrollbind(FALSE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007565# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007566 do_exedit(eap, old_curwin);
7567 }
7568
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007569# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007570 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007571# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007572
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007573# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007574theend:
7575 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007576# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007577}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007578
7579/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007580 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007581 */
7582 void
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007583tabpage_new()
7584{
7585 exarg_T ea;
7586
7587 vim_memset(&ea, 0, sizeof(ea));
7588 ea.cmdidx = CMD_tabnew;
7589 ea.cmd = (char_u *)"tabn";
7590 ea.arg = (char_u *)"";
7591 ex_splitview(&ea);
7592}
7593
7594/*
7595 * :tabnext command
7596 */
7597 static void
7598ex_tabnext(eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007599 exarg_T *eap;
7600{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007601 switch (eap->cmdidx)
7602 {
7603 case CMD_tabfirst:
7604 case CMD_tabrewind:
7605 goto_tabpage(1);
7606 break;
7607 case CMD_tablast:
7608 goto_tabpage(9999);
7609 break;
7610 case CMD_tabprevious:
7611 case CMD_tabNext:
7612 goto_tabpage(eap->addr_count == 0 ? -1 : -(int)eap->line2);
7613 break;
7614 default: /* CMD_tabnext */
7615 goto_tabpage(eap->addr_count == 0 ? 0 : (int)eap->line2);
7616 break;
7617 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007618}
7619
7620/*
7621 * :tabmove command
7622 */
7623 static void
7624ex_tabmove(eap)
7625 exarg_T *eap;
7626{
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02007627 int tab_number = 9999;
7628
7629 if (eap->arg && *eap->arg != NUL)
7630 {
7631 char_u *p = eap->arg;
7632 int relative = 0; /* argument +N/-N means: move N places to the
7633 * right/left relative to the current position. */
7634
7635 if (*eap->arg == '-')
7636 {
7637 relative = -1;
7638 p = eap->arg + 1;
7639 }
7640 else if (*eap->arg == '+')
7641 {
7642 relative = 1;
7643 p = eap->arg + 1;
7644 }
7645 else
7646 p = eap->arg;
7647
7648 if (p == skipdigits(p))
7649 {
7650 /* No numbers as argument. */
7651 eap->errmsg = e_invarg;
7652 return;
7653 }
7654
7655 tab_number = getdigits(&p);
7656 if (relative != 0)
7657 tab_number = tab_number * relative + tabpage_index(curtab) - 1;;
7658 }
7659 else if (eap->addr_count != 0)
7660 tab_number = eap->line2;
7661
7662 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007663}
7664
7665/*
7666 * :tabs command: List tabs and their contents.
7667 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007668 static void
7669ex_tabs(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007670 exarg_T *eap UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007671{
7672 tabpage_T *tp;
7673 win_T *wp;
7674 int tabcount = 1;
7675
7676 msg_start();
7677 msg_scroll = TRUE;
7678 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
7679 {
7680 msg_putchar('\n');
7681 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
7682 msg_outtrans_attr(IObuff, hl_attr(HLF_T));
7683 out_flush(); /* output one line at a time */
7684 ui_breakcheck();
7685
Bram Moolenaar030f0df2006-02-21 22:02:53 +00007686 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007687 wp = firstwin;
7688 else
7689 wp = tp->tp_firstwin;
7690 for ( ; wp != NULL && !got_int; wp = wp->w_next)
7691 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007692 msg_putchar('\n');
7693 msg_putchar(wp == curwin ? '>' : ' ');
7694 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007695 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
7696 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007697 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02007698 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007699 else
7700 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
7701 IObuff, IOSIZE, TRUE);
7702 msg_outtrans(IObuff);
7703 out_flush(); /* output one line at a time */
7704 ui_breakcheck();
7705 }
7706 }
7707}
7708
7709#endif /* FEAT_WINDOWS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007710
7711/*
7712 * ":mode": Set screen mode.
7713 * If no argument given, just get the screen size and redraw.
7714 */
7715 static void
7716ex_mode(eap)
7717 exarg_T *eap;
7718{
7719 if (*eap->arg == NUL)
7720 shell_resized();
7721 else
7722 mch_screenmode(eap->arg);
7723}
7724
7725#ifdef FEAT_WINDOWS
7726/*
7727 * ":resize".
7728 * set, increment or decrement current window height
7729 */
7730 static void
7731ex_resize(eap)
7732 exarg_T *eap;
7733{
7734 int n;
7735 win_T *wp = curwin;
7736
7737 if (eap->addr_count > 0)
7738 {
7739 n = eap->line2;
7740 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
7741 ;
7742 }
7743
7744#ifdef FEAT_GUI
7745 need_mouse_correct = TRUE;
7746#endif
7747 n = atol((char *)eap->arg);
7748#ifdef FEAT_VERTSPLIT
7749 if (cmdmod.split & WSP_VERT)
7750 {
7751 if (*eap->arg == '-' || *eap->arg == '+')
7752 n += W_WIDTH(curwin);
7753 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
7754 n = 9999;
7755 win_setwidth_win((int)n, wp);
7756 }
7757 else
7758#endif
7759 {
7760 if (*eap->arg == '-' || *eap->arg == '+')
7761 n += curwin->w_height;
7762 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
7763 n = 9999;
7764 win_setheight_win((int)n, wp);
7765 }
7766}
7767#endif
7768
7769/*
7770 * ":find [+command] <file>" command.
7771 */
7772 static void
7773ex_find(eap)
7774 exarg_T *eap;
7775{
7776#ifdef FEAT_SEARCHPATH
7777 char_u *fname;
7778 int count;
7779
7780 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
7781 TRUE, curbuf->b_ffname);
7782 if (eap->addr_count > 0)
7783 {
7784 /* Repeat finding the file "count" times. This matters when it
7785 * appears several times in the path. */
7786 count = eap->line2;
7787 while (fname != NULL && --count > 0)
7788 {
7789 vim_free(fname);
7790 fname = find_file_in_path(NULL, 0, FNAME_MESS,
7791 FALSE, curbuf->b_ffname);
7792 }
7793 }
7794
7795 if (fname != NULL)
7796 {
7797 eap->arg = fname;
7798#endif
7799 do_exedit(eap, NULL);
7800#ifdef FEAT_SEARCHPATH
7801 vim_free(fname);
7802 }
7803#endif
7804}
7805
7806/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00007807 * ":open" simulation: for now just work like ":visual".
7808 */
7809 static void
7810ex_open(eap)
7811 exarg_T *eap;
7812{
7813 regmatch_T regmatch;
7814 char_u *p;
7815
7816 curwin->w_cursor.lnum = eap->line2;
7817 beginline(BL_SOL | BL_FIX);
7818 if (*eap->arg == '/')
7819 {
7820 /* ":open /pattern/": put cursor in column found with pattern */
7821 ++eap->arg;
7822 p = skip_regexp(eap->arg, '/', p_magic, NULL);
7823 *p = NUL;
7824 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
7825 if (regmatch.regprog != NULL)
7826 {
7827 regmatch.rm_ic = p_ic;
7828 p = ml_get_curline();
7829 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007830 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007831 else
7832 EMSG(_(e_nomatch));
Bram Moolenaar473de612013-06-08 18:19:48 +02007833 vim_regfree(regmatch.regprog);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007834 }
7835 /* Move to the NUL, ignore any other arguments. */
7836 eap->arg += STRLEN(eap->arg);
7837 }
7838 check_cursor();
7839
7840 eap->cmdidx = CMD_visual;
7841 do_exedit(eap, NULL);
7842}
7843
7844/*
7845 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007846 */
7847 static void
7848ex_edit(eap)
7849 exarg_T *eap;
7850{
7851 do_exedit(eap, NULL);
7852}
7853
7854/*
7855 * ":edit <file>" command and alikes.
7856 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007857 void
7858do_exedit(eap, old_curwin)
7859 exarg_T *eap;
7860 win_T *old_curwin; /* curwin before doing a split or NULL */
7861{
7862 int n;
7863#ifdef FEAT_WINDOWS
7864 int need_hide;
7865#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00007866 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007867
7868 /*
7869 * ":vi" command ends Ex mode.
7870 */
7871 if (exmode_active && (eap->cmdidx == CMD_visual
7872 || eap->cmdidx == CMD_view))
7873 {
7874 exmode_active = FALSE;
7875 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007876 {
7877 /* Special case: ":global/pat/visual\NLvi-commands" */
7878 if (global_busy)
7879 {
7880 int rd = RedrawingDisabled;
7881 int nwr = no_wait_return;
7882 int ms = msg_scroll;
7883#ifdef FEAT_GUI
7884 int he = hold_gui_events;
7885#endif
7886
7887 if (eap->nextcmd != NULL)
7888 {
7889 stuffReadbuff(eap->nextcmd);
7890 eap->nextcmd = NULL;
7891 }
7892
7893 if (exmode_was != EXMODE_VIM)
7894 settmode(TMODE_RAW);
7895 RedrawingDisabled = 0;
7896 no_wait_return = 0;
7897 need_wait_return = FALSE;
7898 msg_scroll = 0;
7899#ifdef FEAT_GUI
7900 hold_gui_events = 0;
7901#endif
7902 must_redraw = CLEAR;
7903
7904 main_loop(FALSE, TRUE);
7905
7906 RedrawingDisabled = rd;
7907 no_wait_return = nwr;
7908 msg_scroll = ms;
7909#ifdef FEAT_GUI
7910 hold_gui_events = he;
7911#endif
7912 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007913 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007914 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007915 }
7916
7917 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007918 || eap->cmdidx == CMD_tabnew
7919 || eap->cmdidx == CMD_tabedit
Bram Moolenaar071d4272004-06-13 20:20:40 +00007920#ifdef FEAT_VERTSPLIT
7921 || eap->cmdidx == CMD_vnew
7922#endif
7923 ) && *eap->arg == NUL)
7924 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007925 /* ":new" or ":tabnew" without argument: edit an new empty buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007926 setpcmark();
7927 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007928 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
7929 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007930 }
7931 else if ((eap->cmdidx != CMD_split
7932#ifdef FEAT_VERTSPLIT
7933 && eap->cmdidx != CMD_vsplit
7934#endif
7935 )
7936 || *eap->arg != NUL
7937#ifdef FEAT_BROWSE
7938 || cmdmod.browse
7939#endif
7940 )
7941 {
Bram Moolenaar5555acc2006-04-07 21:33:12 +00007942#ifdef FEAT_AUTOCMD
7943 /* Can't edit another file when "curbuf_lock" is set. Only ":edit"
7944 * can bring us here, others are stopped earlier. */
7945 if (*eap->arg != NUL && curbuf_locked())
7946 return;
7947#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007948 n = readonlymode;
7949 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
7950 readonlymode = TRUE;
7951 else if (eap->cmdidx == CMD_enew)
7952 readonlymode = FALSE; /* 'readonly' doesn't make sense in an
7953 empty buffer */
7954 setpcmark();
7955 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
7956 NULL, eap,
7957 /* ":edit" goes to first line if Vi compatible */
7958 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
7959 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
7960 ? ECMD_ONE : eap->do_ecmd_lnum,
7961 (P_HID(curbuf) ? ECMD_HIDE : 0)
7962 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar7b449342014-03-25 13:03:48 +01007963 /* after a split we can use an existing buffer */
7964 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007965#ifdef FEAT_LISTCMDS
7966 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
7967#endif
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007968 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007969 {
7970 /* Editing the file failed. If the window was split, close it. */
7971#ifdef FEAT_WINDOWS
7972 if (old_curwin != NULL)
7973 {
7974 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
7975 if (!need_hide || P_HID(curbuf))
7976 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007977# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7978 cleanup_T cs;
7979
7980 /* Reset the error/interrupt/exception state here so that
7981 * aborting() returns FALSE when closing a window. */
7982 enter_cleanup(&cs);
7983# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007984# ifdef FEAT_GUI
7985 need_mouse_correct = TRUE;
7986# endif
7987 win_close(curwin, !need_hide && !P_HID(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007988
7989# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7990 /* Restore the error/interrupt/exception state if not
7991 * discarded by a new aborting error, interrupt, or
7992 * uncaught exception. */
7993 leave_cleanup(&cs);
7994# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007995 }
7996 }
7997#endif
7998 }
7999 else if (readonlymode && curbuf->b_nwindows == 1)
8000 {
8001 /* When editing an already visited buffer, 'readonly' won't be set
8002 * but the previous value is kept. With ":view" and ":sview" we
8003 * want the file to be readonly, except when another window is
8004 * editing the same buffer. */
8005 curbuf->b_p_ro = TRUE;
8006 }
8007 readonlymode = n;
8008 }
8009 else
8010 {
8011 if (eap->do_ecmd_cmd != NULL)
8012 do_cmdline_cmd(eap->do_ecmd_cmd);
8013#ifdef FEAT_TITLE
8014 n = curwin->w_arg_idx_invalid;
8015#endif
8016 check_arg_idx(curwin);
8017#ifdef FEAT_TITLE
8018 if (n != curwin->w_arg_idx_invalid)
8019 maketitle();
8020#endif
8021 }
8022
8023#ifdef FEAT_WINDOWS
8024 /*
8025 * if ":split file" worked, set alternate file name in old window to new
8026 * file
8027 */
8028 if (old_curwin != NULL
8029 && *eap->arg != NUL
8030 && curwin != old_curwin
8031 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00008032 && old_curwin->w_buffer != curbuf
8033 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008034 old_curwin->w_alt_fnum = curbuf->b_fnum;
8035#endif
8036
8037 ex_no_reprint = TRUE;
8038}
8039
8040#ifndef FEAT_GUI
8041/*
8042 * ":gui" and ":gvim" when there is no GUI.
8043 */
8044 static void
8045ex_nogui(eap)
8046 exarg_T *eap;
8047{
8048 eap->errmsg = e_nogvim;
8049}
8050#endif
8051
8052#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
8053 static void
8054ex_tearoff(eap)
8055 exarg_T *eap;
8056{
8057 gui_make_tearoff(eap->arg);
8058}
8059#endif
8060
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008061#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008062 static void
8063ex_popup(eap)
8064 exarg_T *eap;
8065{
Bram Moolenaar97409f12005-07-08 22:17:29 +00008066 gui_make_popup(eap->arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008067}
8068#endif
8069
Bram Moolenaar071d4272004-06-13 20:20:40 +00008070 static void
8071ex_swapname(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008072 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008073{
8074 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
8075 MSG(_("No swap file"));
8076 else
8077 msg(curbuf->b_ml.ml_mfp->mf_fname);
8078}
8079
8080/*
8081 * ":syncbind" forces all 'scrollbind' windows to have the same relative
8082 * offset.
8083 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
8084 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008085 static void
8086ex_syncbind(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008087 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008088{
8089#ifdef FEAT_SCROLLBIND
8090 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008091 win_T *save_curwin = curwin;
8092 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008093 long topline;
8094 long y;
8095 linenr_T old_linenr = curwin->w_cursor.lnum;
8096
8097 setpcmark();
8098
8099 /*
8100 * determine max topline
8101 */
8102 if (curwin->w_p_scb)
8103 {
8104 topline = curwin->w_topline;
8105 for (wp = firstwin; wp; wp = wp->w_next)
8106 {
8107 if (wp->w_p_scb && wp->w_buffer)
8108 {
8109 y = wp->w_buffer->b_ml.ml_line_count - p_so;
8110 if (topline > y)
8111 topline = y;
8112 }
8113 }
8114 if (topline < 1)
8115 topline = 1;
8116 }
8117 else
8118 {
8119 topline = 1;
8120 }
8121
8122
8123 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008124 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008125 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008126 for (curwin = firstwin; curwin; curwin = curwin->w_next)
8127 {
8128 if (curwin->w_p_scb)
8129 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008130 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008131 y = topline - curwin->w_topline;
8132 if (y > 0)
8133 scrollup(y, TRUE);
8134 else
8135 scrolldown(-y, TRUE);
8136 curwin->w_scbind_pos = topline;
8137 redraw_later(VALID);
8138 cursor_correct();
8139#ifdef FEAT_WINDOWS
8140 curwin->w_redr_status = TRUE;
8141#endif
8142 }
8143 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008144 curwin = save_curwin;
8145 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008146 if (curwin->w_p_scb)
8147 {
8148 did_syncbind = TRUE;
8149 checkpcmark();
8150 if (old_linenr != curwin->w_cursor.lnum)
8151 {
8152 char_u ctrl_o[2];
8153
8154 ctrl_o[0] = Ctrl_O;
8155 ctrl_o[1] = 0;
8156 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
8157 }
8158 }
8159#endif
8160}
8161
8162
8163 static void
8164ex_read(eap)
8165 exarg_T *eap;
8166{
Bram Moolenaardf177f62005-02-22 08:39:57 +00008167 int i;
8168 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
8169 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008170
8171 if (eap->usefilter) /* :r!cmd */
8172 do_bang(1, eap, FALSE, FALSE, TRUE);
8173 else
8174 {
8175 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
8176 return;
8177
8178#ifdef FEAT_BROWSE
8179 if (cmdmod.browse)
8180 {
8181 char_u *browseFile;
8182
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008183 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008184 NULL, NULL, NULL, curbuf);
8185 if (browseFile != NULL)
8186 {
8187 i = readfile(browseFile, NULL,
8188 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8189 vim_free(browseFile);
8190 }
8191 else
8192 i = OK;
8193 }
8194 else
8195#endif
8196 if (*eap->arg == NUL)
8197 {
8198 if (check_fname() == FAIL) /* check for no file name */
8199 return;
8200 i = readfile(curbuf->b_ffname, curbuf->b_fname,
8201 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8202 }
8203 else
8204 {
8205 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
8206 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
8207 i = readfile(eap->arg, NULL,
8208 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8209
8210 }
8211 if (i == FAIL)
8212 {
8213#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
8214 if (!aborting())
8215#endif
8216 EMSG2(_(e_notopen), eap->arg);
8217 }
8218 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008219 {
8220 if (empty && exmode_active)
8221 {
8222 /* Delete the empty line that remains. Historically ex does
8223 * this but vi doesn't. */
8224 if (eap->line2 == 0)
8225 lnum = curbuf->b_ml.ml_line_count;
8226 else
8227 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008228 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008229 {
8230 ml_delete(lnum, FALSE);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008231 if (curwin->w_cursor.lnum > 1
8232 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008233 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00008234 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008235 }
8236 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008237 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008238 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008239 }
8240}
8241
Bram Moolenaarea408852005-06-25 22:49:46 +00008242static char_u *prev_dir = NULL;
8243
8244#if defined(EXITFREE) || defined(PROTO)
8245 void
8246free_cd_dir()
8247{
8248 vim_free(prev_dir);
Bram Moolenaara0174af2008-01-02 20:08:25 +00008249 prev_dir = NULL;
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00008250
8251 vim_free(globaldir);
8252 globaldir = NULL;
Bram Moolenaarea408852005-06-25 22:49:46 +00008253}
8254#endif
8255
Bram Moolenaarf4258302013-06-02 18:20:17 +02008256/*
8257 * Deal with the side effects of changing the current directory.
8258 * When "local" is TRUE then this was after an ":lcd" command.
8259 */
8260 void
8261post_chdir(local)
8262 int local;
8263{
8264 vim_free(curwin->w_localdir);
Bram Moolenaarbd2dc342014-01-10 15:53:13 +01008265 curwin->w_localdir = NULL;
Bram Moolenaarf4258302013-06-02 18:20:17 +02008266 if (local)
8267 {
8268 /* If still in global directory, need to remember current
8269 * directory as global directory. */
8270 if (globaldir == NULL && prev_dir != NULL)
8271 globaldir = vim_strsave(prev_dir);
8272 /* Remember this local directory for the window. */
8273 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8274 curwin->w_localdir = vim_strsave(NameBuff);
8275 }
8276 else
8277 {
8278 /* We are now in the global directory, no need to remember its
8279 * name. */
8280 vim_free(globaldir);
8281 globaldir = NULL;
Bram Moolenaarf4258302013-06-02 18:20:17 +02008282 }
8283
8284 shorten_fnames(TRUE);
8285}
8286
Bram Moolenaarea408852005-06-25 22:49:46 +00008287
Bram Moolenaar071d4272004-06-13 20:20:40 +00008288/*
8289 * ":cd", ":lcd", ":chdir" and ":lchdir".
8290 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00008291 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00008292ex_cd(eap)
8293 exarg_T *eap;
8294{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008295 char_u *new_dir;
8296 char_u *tofree;
8297
8298 new_dir = eap->arg;
8299#if !defined(UNIX) && !defined(VMS)
8300 /* for non-UNIX ":cd" means: print current directory */
8301 if (*new_dir == NUL)
8302 ex_pwd(NULL);
8303 else
8304#endif
8305 {
Bram Moolenaar8e8fe9b2009-03-11 14:37:32 +00008306#ifdef FEAT_AUTOCMD
8307 if (allbuf_locked())
8308 return;
8309#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00008310 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged()
8311 && !eap->forceit)
8312 {
Bram Moolenaar81870892007-11-11 18:17:28 +00008313 EMSG(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00008314 return;
8315 }
8316
Bram Moolenaar071d4272004-06-13 20:20:40 +00008317 /* ":cd -": Change to previous directory */
8318 if (STRCMP(new_dir, "-") == 0)
8319 {
8320 if (prev_dir == NULL)
8321 {
8322 EMSG(_("E186: No previous directory"));
8323 return;
8324 }
8325 new_dir = prev_dir;
8326 }
8327
8328 /* Save current directory for next ":cd -" */
8329 tofree = prev_dir;
8330 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8331 prev_dir = vim_strsave(NameBuff);
8332 else
8333 prev_dir = NULL;
8334
8335#if defined(UNIX) || defined(VMS)
8336 /* for UNIX ":cd" means: go to home directory */
8337 if (*new_dir == NUL)
8338 {
8339 /* use NameBuff for home directory name */
8340# ifdef VMS
8341 char_u *p;
8342
8343 p = mch_getenv((char_u *)"SYS$LOGIN");
8344 if (p == NULL || *p == NUL) /* empty is the same as not set */
8345 NameBuff[0] = NUL;
8346 else
Bram Moolenaarb6356332005-07-18 21:40:44 +00008347 vim_strncpy(NameBuff, p, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008348# else
8349 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
8350# endif
8351 new_dir = NameBuff;
8352 }
8353#endif
8354 if (new_dir == NULL || vim_chdir(new_dir))
8355 EMSG(_(e_failed));
8356 else
8357 {
Bram Moolenaarf4258302013-06-02 18:20:17 +02008358 post_chdir(eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008359
8360 /* Echo the new current directory if the command was typed. */
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00008361 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008362 ex_pwd(eap);
8363 }
8364 vim_free(tofree);
8365 }
8366}
8367
8368/*
8369 * ":pwd".
8370 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008371 static void
8372ex_pwd(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008373 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008374{
8375 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8376 {
8377#ifdef BACKSLASH_IN_FILENAME
8378 slash_adjust(NameBuff);
8379#endif
8380 msg(NameBuff);
8381 }
8382 else
8383 EMSG(_("E187: Unknown"));
8384}
8385
8386/*
8387 * ":=".
8388 */
8389 static void
8390ex_equal(eap)
8391 exarg_T *eap;
8392{
8393 smsg((char_u *)"%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008394 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008395}
8396
8397 static void
8398ex_sleep(eap)
8399 exarg_T *eap;
8400{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008401 int n;
8402 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008403
8404 if (cursor_valid())
8405 {
8406 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
8407 if (n >= 0)
Bram Moolenaar10395d82014-02-05 22:46:52 +01008408 windgoto((int)n, W_WINCOL(curwin) + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008409 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008410
8411 len = eap->line2;
8412 switch (*eap->arg)
8413 {
8414 case 'm': break;
8415 case NUL: len *= 1000L; break;
8416 default: EMSG2(_(e_invarg2), eap->arg); return;
8417 }
8418 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008419}
8420
8421/*
8422 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
8423 */
8424 void
8425do_sleep(msec)
8426 long msec;
8427{
8428 long done;
8429
8430 cursor_on();
8431 out_flush();
8432 for (done = 0; !got_int && done < msec; done += 1000L)
8433 {
8434 ui_delay(msec - done > 1000L ? 1000L : msec - done, TRUE);
8435 ui_breakcheck();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02008436#ifdef FEAT_NETBEANS_INTG
8437 /* Process the netbeans messages that may have been received in the
8438 * call to ui_breakcheck() when the GUI is in use. This may occur when
8439 * running a test case. */
8440 netbeans_parse_messages();
8441#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008442 }
8443}
8444
8445 static void
8446do_exmap(eap, isabbrev)
8447 exarg_T *eap;
8448 int isabbrev;
8449{
8450 int mode;
8451 char_u *cmdp;
8452
8453 cmdp = eap->cmd;
8454 mode = get_map_mode(&cmdp, eap->forceit || isabbrev);
8455
8456 switch (do_map((*cmdp == 'n') ? 2 : (*cmdp == 'u'),
8457 eap->arg, mode, isabbrev))
8458 {
8459 case 1: EMSG(_(e_invarg));
8460 break;
8461 case 2: EMSG(isabbrev ? _(e_noabbr) : _(e_nomap));
8462 break;
8463 }
8464}
8465
8466/*
8467 * ":winsize" command (obsolete).
8468 */
8469 static void
8470ex_winsize(eap)
8471 exarg_T *eap;
8472{
8473 int w, h;
8474 char_u *arg = eap->arg;
8475 char_u *p;
8476
8477 w = getdigits(&arg);
8478 arg = skipwhite(arg);
8479 p = arg;
8480 h = getdigits(&arg);
8481 if (*p != NUL && *arg == NUL)
8482 set_shellsize(w, h, TRUE);
8483 else
8484 EMSG(_("E465: :winsize requires two number arguments"));
8485}
8486
8487#ifdef FEAT_WINDOWS
8488 static void
8489ex_wincmd(eap)
8490 exarg_T *eap;
8491{
8492 int xchar = NUL;
8493 char_u *p;
8494
8495 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
8496 {
8497 /* CTRL-W g and CTRL-W CTRL-G have an extra command character */
8498 if (eap->arg[1] == NUL)
8499 {
8500 EMSG(_(e_invarg));
8501 return;
8502 }
8503 xchar = eap->arg[1];
8504 p = eap->arg + 2;
8505 }
8506 else
8507 p = eap->arg + 1;
8508
8509 eap->nextcmd = check_nextcmd(p);
8510 p = skipwhite(p);
8511 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
8512 EMSG(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02008513 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008514 {
8515 /* Pass flags on for ":vertical wincmd ]". */
8516 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008517 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008518 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
8519 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008520 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008521 }
8522}
8523#endif
8524
Bram Moolenaar843ee412004-06-30 16:16:41 +00008525#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008526/*
8527 * ":winpos".
8528 */
8529 static void
8530ex_winpos(eap)
8531 exarg_T *eap;
8532{
8533 int x, y;
8534 char_u *arg = eap->arg;
8535 char_u *p;
8536
8537 if (*arg == NUL)
8538 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00008539# if defined(FEAT_GUI) || defined(MSWIN)
8540# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008541 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00008542# else
8543 if (mch_get_winpos(&x, &y) != FAIL)
8544# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008545 {
8546 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
8547 msg(IObuff);
8548 }
8549 else
8550# endif
8551 EMSG(_("E188: Obtaining window position not implemented for this platform"));
8552 }
8553 else
8554 {
8555 x = getdigits(&arg);
8556 arg = skipwhite(arg);
8557 p = arg;
8558 y = getdigits(&arg);
8559 if (*p == NUL || *arg != NUL)
8560 {
8561 EMSG(_("E466: :winpos requires two number arguments"));
8562 return;
8563 }
8564# ifdef FEAT_GUI
8565 if (gui.in_use)
8566 gui_mch_set_winpos(x, y);
8567 else if (gui.starting)
8568 {
8569 /* Remember the coordinates for when the window is opened. */
8570 gui_win_x = x;
8571 gui_win_y = y;
8572 }
8573# ifdef HAVE_TGETENT
8574 else
8575# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00008576# else
8577# ifdef MSWIN
8578 mch_set_winpos(x, y);
8579# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008580# endif
8581# ifdef HAVE_TGETENT
8582 if (*T_CWP)
8583 term_set_winpos(x, y);
8584# endif
8585 }
8586}
8587#endif
8588
8589/*
8590 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
8591 */
8592 static void
8593ex_operators(eap)
8594 exarg_T *eap;
8595{
8596 oparg_T oa;
8597
8598 clear_oparg(&oa);
8599 oa.regname = eap->regname;
8600 oa.start.lnum = eap->line1;
8601 oa.end.lnum = eap->line2;
8602 oa.line_count = eap->line2 - eap->line1 + 1;
8603 oa.motion_type = MLINE;
8604#ifdef FEAT_VIRTUALEDIT
8605 virtual_op = FALSE;
8606#endif
8607 if (eap->cmdidx != CMD_yank) /* position cursor for undo */
8608 {
8609 setpcmark();
8610 curwin->w_cursor.lnum = eap->line1;
8611 beginline(BL_SOL | BL_FIX);
8612 }
8613
Bram Moolenaard07c6e12013-11-21 14:21:40 +01008614 if (VIsual_active)
8615 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01008616
Bram Moolenaar071d4272004-06-13 20:20:40 +00008617 switch (eap->cmdidx)
8618 {
8619 case CMD_delete:
8620 oa.op_type = OP_DELETE;
8621 op_delete(&oa);
8622 break;
8623
8624 case CMD_yank:
8625 oa.op_type = OP_YANK;
8626 (void)op_yank(&oa, FALSE, TRUE);
8627 break;
8628
8629 default: /* CMD_rshift or CMD_lshift */
Bram Moolenaar6e707362013-06-14 19:15:58 +02008630 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00008631#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02008632 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
8633#else
8634 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00008635#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02008636 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00008637 oa.op_type = OP_RSHIFT;
8638 else
8639 oa.op_type = OP_LSHIFT;
8640 op_shift(&oa, FALSE, eap->amount);
8641 break;
8642 }
8643#ifdef FEAT_VIRTUALEDIT
8644 virtual_op = MAYBE;
8645#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00008646 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008647}
8648
8649/*
8650 * ":put".
8651 */
8652 static void
8653ex_put(eap)
8654 exarg_T *eap;
8655{
8656 /* ":0put" works like ":1put!". */
8657 if (eap->line2 == 0)
8658 {
8659 eap->line2 = 1;
8660 eap->forceit = TRUE;
8661 }
8662 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008663 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
8664 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008665}
8666
8667/*
8668 * Handle ":copy" and ":move".
8669 */
8670 static void
8671ex_copymove(eap)
8672 exarg_T *eap;
8673{
8674 long n;
8675
8676 n = get_address(&eap->arg, FALSE, FALSE);
8677 if (eap->arg == NULL) /* error detected */
8678 {
8679 eap->nextcmd = NULL;
8680 return;
8681 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008682 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008683
8684 /*
8685 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
8686 */
8687 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
8688 {
8689 EMSG(_(e_invaddr));
8690 return;
8691 }
8692
8693 if (eap->cmdidx == CMD_move)
8694 {
8695 if (do_move(eap->line1, eap->line2, n) == FAIL)
8696 return;
8697 }
8698 else
8699 ex_copy(eap->line1, eap->line2, n);
8700 u_clearline();
8701 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008702 ex_may_print(eap);
8703}
8704
8705/*
8706 * Print the current line if flags were given to the Ex command.
8707 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02008708 void
Bram Moolenaardf177f62005-02-22 08:39:57 +00008709ex_may_print(eap)
8710 exarg_T *eap;
8711{
8712 if (eap->flags != 0)
8713 {
8714 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
8715 (eap->flags & EXFLAG_LIST));
8716 ex_no_reprint = TRUE;
8717 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008718}
8719
8720/*
8721 * ":smagic" and ":snomagic".
8722 */
8723 static void
8724ex_submagic(eap)
8725 exarg_T *eap;
8726{
8727 int magic_save = p_magic;
8728
8729 p_magic = (eap->cmdidx == CMD_smagic);
8730 do_sub(eap);
8731 p_magic = magic_save;
8732}
8733
8734/*
8735 * ":join".
8736 */
8737 static void
8738ex_join(eap)
8739 exarg_T *eap;
8740{
8741 curwin->w_cursor.lnum = eap->line1;
8742 if (eap->line1 == eap->line2)
8743 {
8744 if (eap->addr_count >= 2) /* :2,2join does nothing */
8745 return;
8746 if (eap->line2 == curbuf->b_ml.ml_line_count)
8747 {
8748 beep_flush();
8749 return;
8750 }
8751 ++eap->line2;
8752 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02008753 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008754 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008755 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008756}
8757
8758/*
8759 * ":[addr]@r" or ":[addr]*r": execute register
8760 */
8761 static void
8762ex_at(eap)
8763 exarg_T *eap;
8764{
8765 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00008766 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008767
8768 curwin->w_cursor.lnum = eap->line2;
8769
8770#ifdef USE_ON_FLY_SCROLL
8771 dont_scroll = TRUE; /* disallow scrolling here */
8772#endif
8773
8774 /* get the register name. No name means to use the previous one */
8775 c = *eap->arg;
8776 if (c == NUL || (c == '*' && *eap->cmd == '*'))
8777 c = '@';
Bram Moolenaard333d1e2006-11-07 17:43:47 +00008778 /* Put the register in the typeahead buffer with the "silent" flag. */
8779 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
8780 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008781 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008782 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00008783 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008784 else
8785 {
8786 int save_efr = exec_from_reg;
8787
8788 exec_from_reg = TRUE;
8789
8790 /*
8791 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00008792 * Continue until the stuff buffer is empty and all added characters
8793 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008794 */
Bram Moolenaar60462872009-11-03 11:40:19 +00008795 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008796 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
8797
8798 exec_from_reg = save_efr;
8799 }
8800}
8801
8802/*
8803 * ":!".
8804 */
8805 static void
8806ex_bang(eap)
8807 exarg_T *eap;
8808{
8809 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
8810}
8811
8812/*
8813 * ":undo".
8814 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008815 static void
8816ex_undo(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008817 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008818{
Bram Moolenaard3667a22006-03-16 21:35:52 +00008819 if (eap->addr_count == 1) /* :undo 123 */
Bram Moolenaar730cde92010-06-27 05:18:54 +02008820 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00008821 else
8822 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008823}
8824
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008825#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008826 static void
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008827ex_wundo(eap)
8828 exarg_T *eap;
8829{
8830 char_u hash[UNDO_HASH_SIZE];
8831
8832 u_compute_hash(hash);
8833 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
8834}
8835
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008836 static void
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008837ex_rundo(eap)
8838 exarg_T *eap;
8839{
8840 char_u hash[UNDO_HASH_SIZE];
8841
8842 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02008843 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008844}
8845#endif
8846
Bram Moolenaar071d4272004-06-13 20:20:40 +00008847/*
8848 * ":redo".
8849 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008850 static void
8851ex_redo(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008852 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008853{
8854 u_redo(1);
8855}
8856
8857/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008858 * ":earlier" and ":later".
8859 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008860 static void
8861ex_later(eap)
8862 exarg_T *eap;
8863{
8864 long count = 0;
8865 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008866 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008867 char_u *p = eap->arg;
8868
8869 if (*p == NUL)
8870 count = 1;
8871 else if (isdigit(*p))
8872 {
8873 count = getdigits(&p);
8874 switch (*p)
8875 {
8876 case 's': ++p; sec = TRUE; break;
8877 case 'm': ++p; sec = TRUE; count *= 60; break;
8878 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008879 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
8880 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008881 }
8882 }
8883
8884 if (*p != NUL)
8885 EMSG2(_(e_invarg2), eap->arg);
8886 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02008887 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
8888 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008889}
8890
8891/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008892 * ":redir": start/stop redirection.
8893 */
8894 static void
8895ex_redir(eap)
8896 exarg_T *eap;
8897{
8898 char *mode;
8899 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00008900 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008901
8902 if (STRICMP(eap->arg, "END") == 0)
8903 close_redir();
8904 else
8905 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008906 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008907 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008908 ++arg;
8909 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008910 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008911 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008912 mode = "a";
8913 }
8914 else
8915 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00008916 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008917
8918 close_redir();
8919
8920 /* Expand environment variables and "~/". */
Bram Moolenaarca472992005-01-21 11:46:23 +00008921 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008922 if (fname == NULL)
8923 return;
8924#ifdef FEAT_BROWSE
8925 if (cmdmod.browse)
8926 {
8927 char_u *browseFile;
8928
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008929 browseFile = do_browse(BROWSE_SAVE,
8930 (char_u *)_("Save Redirection"),
8931 fname, NULL, NULL, BROWSE_FILTER_ALL_FILES, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008932 if (browseFile == NULL)
8933 return; /* operation cancelled */
8934 vim_free(fname);
8935 fname = browseFile;
8936 eap->forceit = TRUE; /* since dialog already asked */
8937 }
8938#endif
8939
8940 redir_fd = open_exfile(fname, eap->forceit, mode);
8941 vim_free(fname);
8942 }
8943#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00008944 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008945 {
8946 /* redirect to a register a-z (resp. A-Z for appending) */
8947 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00008948 ++arg;
8949 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008950# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00008951 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00008952 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008953# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00008954 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008955 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008956 redir_reg = *arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008957 if (*arg == '>' && arg[1] == '>') /* append */
Bram Moolenaard9d30582005-05-18 22:10:28 +00008958 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008959 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008960 {
Bram Moolenaarc188b882007-10-19 14:20:54 +00008961 /* Can use both "@a" and "@a>". */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00008962 if (*arg == '>')
8963 arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008964 /* Make register empty when not using @A-@Z and the
8965 * command is valid. */
8966 if (*arg == NUL && !isupper(redir_reg))
8967 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008968 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008969 }
8970 if (*arg != NUL)
8971 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00008972 redir_reg = 0;
Bram Moolenaard9d30582005-05-18 22:10:28 +00008973 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008974 }
8975 }
8976 else if (*arg == '=' && arg[1] == '>')
8977 {
8978 int append;
8979
8980 /* redirect to a variable */
8981 close_redir();
8982 arg += 2;
8983
8984 if (*arg == '>')
8985 {
8986 ++arg;
8987 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008988 }
8989 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008990 append = FALSE;
8991
8992 if (var_redir_start(skipwhite(arg), append) == OK)
8993 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008994 }
8995#endif
8996
8997 /* TODO: redirect to a buffer */
8998
Bram Moolenaar071d4272004-06-13 20:20:40 +00008999 else
Bram Moolenaarca472992005-01-21 11:46:23 +00009000 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009001 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00009002
9003 /* Make sure redirection is not off. Can happen for cmdline completion
9004 * that indirectly invokes a command to catch its output. */
9005 if (redir_fd != NULL
9006#ifdef FEAT_EVAL
9007 || redir_reg || redir_vname
9008#endif
9009 )
9010 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009011}
9012
9013/*
9014 * ":redraw": force redraw
9015 */
9016 static void
9017ex_redraw(eap)
9018 exarg_T *eap;
9019{
9020 int r = RedrawingDisabled;
9021 int p = p_lz;
9022
9023 RedrawingDisabled = 0;
9024 p_lz = FALSE;
9025 update_topline();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009026 update_screen(eap->forceit ? CLEAR : VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009027#ifdef FEAT_TITLE
9028 if (need_maketitle)
9029 maketitle();
9030#endif
9031 RedrawingDisabled = r;
9032 p_lz = p;
9033
9034 /* Reset msg_didout, so that a message that's there is overwritten. */
9035 msg_didout = FALSE;
9036 msg_col = 0;
9037
Bram Moolenaar56667a52013-07-17 11:54:28 +02009038 /* No need to wait after an intentional redraw. */
9039 need_wait_return = FALSE;
9040
Bram Moolenaar071d4272004-06-13 20:20:40 +00009041 out_flush();
9042}
9043
9044/*
9045 * ":redrawstatus": force redraw of status line(s)
9046 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009047 static void
9048ex_redrawstatus(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009049 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009050{
9051#if defined(FEAT_WINDOWS)
9052 int r = RedrawingDisabled;
9053 int p = p_lz;
9054
9055 RedrawingDisabled = 0;
9056 p_lz = FALSE;
9057 if (eap->forceit)
9058 status_redraw_all();
9059 else
9060 status_redraw_curbuf();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009061 update_screen(VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009062 RedrawingDisabled = r;
9063 p_lz = p;
9064 out_flush();
9065#endif
9066}
9067
9068 static void
9069close_redir()
9070{
9071 if (redir_fd != NULL)
9072 {
9073 fclose(redir_fd);
9074 redir_fd = NULL;
9075 }
9076#ifdef FEAT_EVAL
9077 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009078 if (redir_vname)
9079 {
9080 var_redir_stop();
9081 redir_vname = 0;
9082 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009083#endif
9084}
9085
9086#if defined(FEAT_SESSION) && defined(USE_CRNL)
9087# define MKSESSION_NL
9088static int mksession_nl = FALSE; /* use NL only in put_eol() */
9089#endif
9090
9091/*
9092 * ":mkexrc", ":mkvimrc", ":mkview" and ":mksession".
9093 */
9094 static void
9095ex_mkrc(eap)
9096 exarg_T *eap;
9097{
9098 FILE *fd;
9099 int failed = FALSE;
9100 char_u *fname;
9101#ifdef FEAT_BROWSE
9102 char_u *browseFile = NULL;
9103#endif
9104#ifdef FEAT_SESSION
9105 int view_session = FALSE;
9106 int using_vdir = FALSE; /* using 'viewdir'? */
9107 char_u *viewFile = NULL;
9108 unsigned *flagp;
9109#endif
9110
9111 if (eap->cmdidx == CMD_mksession || eap->cmdidx == CMD_mkview)
9112 {
9113#ifdef FEAT_SESSION
9114 view_session = TRUE;
9115#else
9116 ex_ni(eap);
9117 return;
9118#endif
9119 }
9120
9121#ifdef FEAT_SESSION
Bram Moolenaar8d594672009-07-01 18:18:57 +00009122 /* Use the short file name until ":lcd" is used. We also don't use the
9123 * short file name when 'acd' is set, that is checked later. */
Bram Moolenaareeefcc72007-05-01 21:21:21 +00009124 did_lcd = FALSE;
9125
Bram Moolenaar071d4272004-06-13 20:20:40 +00009126 /* ":mkview" or ":mkview 9": generate file name with 'viewdir' */
9127 if (eap->cmdidx == CMD_mkview
9128 && (*eap->arg == NUL
9129 || (vim_isdigit(*eap->arg) && eap->arg[1] == NUL)))
9130 {
9131 eap->forceit = TRUE;
9132 fname = get_view_file(*eap->arg);
9133 if (fname == NULL)
9134 return;
9135 viewFile = fname;
9136 using_vdir = TRUE;
9137 }
9138 else
9139#endif
9140 if (*eap->arg != NUL)
9141 fname = eap->arg;
9142 else if (eap->cmdidx == CMD_mkvimrc)
9143 fname = (char_u *)VIMRC_FILE;
9144#ifdef FEAT_SESSION
9145 else if (eap->cmdidx == CMD_mksession)
9146 fname = (char_u *)SESSION_FILE;
9147#endif
9148 else
9149 fname = (char_u *)EXRC_FILE;
9150
9151#ifdef FEAT_BROWSE
9152 if (cmdmod.browse)
9153 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00009154 browseFile = do_browse(BROWSE_SAVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009155# ifdef FEAT_SESSION
9156 eap->cmdidx == CMD_mkview ? (char_u *)_("Save View") :
9157 eap->cmdidx == CMD_mksession ? (char_u *)_("Save Session") :
9158# endif
9159 (char_u *)_("Save Setup"),
9160 fname, (char_u *)"vim", NULL, BROWSE_FILTER_MACROS, NULL);
9161 if (browseFile == NULL)
9162 goto theend;
9163 fname = browseFile;
9164 eap->forceit = TRUE; /* since dialog already asked */
9165 }
9166#endif
9167
9168#if defined(FEAT_SESSION) && defined(vim_mkdir)
9169 /* When using 'viewdir' may have to create the directory. */
9170 if (using_vdir && !mch_isdir(p_vdir))
Bram Moolenaardf177f62005-02-22 08:39:57 +00009171 vim_mkdir_emsg(p_vdir, 0755);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009172#endif
9173
9174 fd = open_exfile(fname, eap->forceit, WRITEBIN);
9175 if (fd != NULL)
9176 {
9177#ifdef FEAT_SESSION
9178 if (eap->cmdidx == CMD_mkview)
9179 flagp = &vop_flags;
9180 else
9181 flagp = &ssop_flags;
9182#endif
9183
9184#ifdef MKSESSION_NL
9185 /* "unix" in 'sessionoptions': use NL line separator */
9186 if (view_session && (*flagp & SSOP_UNIX))
9187 mksession_nl = TRUE;
9188#endif
9189
9190 /* Write the version command for :mkvimrc */
9191 if (eap->cmdidx == CMD_mkvimrc)
9192 (void)put_line(fd, "version 6.0");
9193
9194#ifdef FEAT_SESSION
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009195 if (eap->cmdidx == CMD_mksession)
9196 {
9197 if (put_line(fd, "let SessionLoad = 1") == FAIL)
9198 failed = TRUE;
9199 }
9200
Bram Moolenaar071d4272004-06-13 20:20:40 +00009201 if (eap->cmdidx != CMD_mkview)
9202#endif
9203 {
9204 /* Write setting 'compatible' first, because it has side effects.
9205 * For that same reason only do it when needed. */
9206 if (p_cp)
9207 (void)put_line(fd, "if !&cp | set cp | endif");
9208 else
9209 (void)put_line(fd, "if &cp | set nocp | endif");
9210 }
9211
9212#ifdef FEAT_SESSION
9213 if (!view_session
9214 || (eap->cmdidx == CMD_mksession
9215 && (*flagp & SSOP_OPTIONS)))
9216#endif
9217 failed |= (makemap(fd, NULL) == FAIL
9218 || makeset(fd, OPT_GLOBAL, FALSE) == FAIL);
9219
9220#ifdef FEAT_SESSION
9221 if (!failed && view_session)
9222 {
9223 if (put_line(fd, "let s:so_save = &so | let s:siso_save = &siso | set so=0 siso=0") == FAIL)
9224 failed = TRUE;
9225 if (eap->cmdidx == CMD_mksession)
9226 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02009227 char_u *dirnow; /* current directory */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009228
Bram Moolenaard9462e32011-04-11 21:35:11 +02009229 dirnow = alloc(MAXPATHL);
9230 if (dirnow == NULL)
9231 failed = TRUE;
9232 else
9233 {
9234 /*
9235 * Change to session file's dir.
9236 */
9237 if (mch_dirname(dirnow, MAXPATHL) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00009238 || mch_chdir((char *)dirnow) != 0)
Bram Moolenaard9462e32011-04-11 21:35:11 +02009239 *dirnow = NUL;
9240 if (*dirnow != NUL && (ssop_flags & SSOP_SESDIR))
9241 {
9242 if (vim_chdirfile(fname) == OK)
9243 shorten_fnames(TRUE);
9244 }
9245 else if (*dirnow != NUL
9246 && (ssop_flags & SSOP_CURDIR) && globaldir != NULL)
9247 {
9248 if (mch_chdir((char *)globaldir) == 0)
9249 shorten_fnames(TRUE);
9250 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009251
Bram Moolenaard9462e32011-04-11 21:35:11 +02009252 failed |= (makeopens(fd, dirnow) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009253
Bram Moolenaard9462e32011-04-11 21:35:11 +02009254 /* restore original dir */
9255 if (*dirnow != NUL && ((ssop_flags & SSOP_SESDIR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009256 || ((ssop_flags & SSOP_CURDIR) && globaldir != NULL)))
Bram Moolenaard9462e32011-04-11 21:35:11 +02009257 {
9258 if (mch_chdir((char *)dirnow) != 0)
9259 EMSG(_(e_prev_dir));
9260 shorten_fnames(TRUE);
9261 }
9262 vim_free(dirnow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009263 }
9264 }
9265 else
9266 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +00009267 failed |= (put_view(fd, curwin, !using_vdir, flagp,
9268 -1) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009269 }
9270 if (put_line(fd, "let &so = s:so_save | let &siso = s:siso_save")
9271 == FAIL)
9272 failed = TRUE;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009273 if (put_line(fd, "doautoall SessionLoadPost") == FAIL)
9274 failed = TRUE;
Bram Moolenaar4770d092006-01-12 23:22:24 +00009275 if (eap->cmdidx == CMD_mksession)
9276 {
9277 if (put_line(fd, "unlet SessionLoad") == FAIL)
9278 failed = TRUE;
9279 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009280 }
9281#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009282 if (put_line(fd, "\" vim: set ft=vim :") == FAIL)
9283 failed = TRUE;
9284
Bram Moolenaar071d4272004-06-13 20:20:40 +00009285 failed |= fclose(fd);
9286
9287 if (failed)
9288 EMSG(_(e_write));
9289#if defined(FEAT_EVAL) && defined(FEAT_SESSION)
9290 else if (eap->cmdidx == CMD_mksession)
9291 {
9292 /* successful session write - set this_session var */
Bram Moolenaard9462e32011-04-11 21:35:11 +02009293 char_u *tbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009294
Bram Moolenaard9462e32011-04-11 21:35:11 +02009295 tbuf = alloc(MAXPATHL);
9296 if (tbuf != NULL)
9297 {
9298 if (vim_FullName(fname, tbuf, MAXPATHL, FALSE) == OK)
9299 set_vim_var_string(VV_THIS_SESSION, tbuf, -1);
9300 vim_free(tbuf);
9301 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009302 }
9303#endif
9304#ifdef MKSESSION_NL
9305 mksession_nl = FALSE;
9306#endif
9307 }
9308
9309#ifdef FEAT_BROWSE
9310theend:
9311 vim_free(browseFile);
9312#endif
9313#ifdef FEAT_SESSION
9314 vim_free(viewFile);
9315#endif
9316}
9317
Bram Moolenaardf177f62005-02-22 08:39:57 +00009318#if ((defined(FEAT_SESSION) || defined(FEAT_EVAL)) && defined(vim_mkdir)) \
9319 || defined(PROTO)
9320 int
9321vim_mkdir_emsg(name, prot)
9322 char_u *name;
Bram Moolenaar78a15312009-05-15 19:33:18 +00009323 int prot UNUSED;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009324{
9325 if (vim_mkdir(name, prot) != 0)
9326 {
9327 EMSG2(_("E739: Cannot create directory: %s"), name);
9328 return FAIL;
9329 }
9330 return OK;
9331}
9332#endif
9333
Bram Moolenaar071d4272004-06-13 20:20:40 +00009334/*
9335 * Open a file for writing for an Ex command, with some checks.
9336 * Return file descriptor, or NULL on failure.
9337 */
9338 FILE *
9339open_exfile(fname, forceit, mode)
9340 char_u *fname;
9341 int forceit;
9342 char *mode; /* "w" for create new file or "a" for append */
9343{
9344 FILE *fd;
9345
9346#ifdef UNIX
9347 /* with Unix it is possible to open a directory */
9348 if (mch_isdir(fname))
9349 {
9350 EMSG2(_(e_isadir2), fname);
9351 return NULL;
9352 }
9353#endif
9354 if (!forceit && *mode != 'a' && vim_fexists(fname))
9355 {
9356 EMSG2(_("E189: \"%s\" exists (add ! to override)"), fname);
9357 return NULL;
9358 }
9359
9360 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
9361 EMSG2(_("E190: Cannot open \"%s\" for writing"), fname);
9362
9363 return fd;
9364}
9365
9366/*
9367 * ":mark" and ":k".
9368 */
9369 static void
9370ex_mark(eap)
9371 exarg_T *eap;
9372{
9373 pos_T pos;
9374
9375 if (*eap->arg == NUL) /* No argument? */
9376 EMSG(_(e_argreq));
9377 else if (eap->arg[1] != NUL) /* more than one character? */
9378 EMSG(_(e_trailing));
9379 else
9380 {
9381 pos = curwin->w_cursor; /* save curwin->w_cursor */
9382 curwin->w_cursor.lnum = eap->line2;
9383 beginline(BL_WHITE | BL_FIX);
9384 if (setmark(*eap->arg) == FAIL) /* set mark */
9385 EMSG(_("E191: Argument must be a letter or forward/backward quote"));
9386 curwin->w_cursor = pos; /* restore curwin->w_cursor */
9387 }
9388}
9389
9390/*
9391 * Update w_topline, w_leftcol and the cursor position.
9392 */
9393 void
9394update_topline_cursor()
9395{
9396 check_cursor(); /* put cursor on valid line */
9397 update_topline();
9398 if (!curwin->w_p_wrap)
9399 validate_cursor();
9400 update_curswant();
9401}
9402
9403#ifdef FEAT_EX_EXTRA
9404/*
9405 * ":normal[!] {commands}": Execute normal mode commands.
9406 */
9407 static void
9408ex_normal(eap)
9409 exarg_T *eap;
9410{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009411 int save_msg_scroll = msg_scroll;
9412 int save_restart_edit = restart_edit;
9413 int save_msg_didout = msg_didout;
9414 int save_State = State;
9415 tasave_T tabuf;
9416 int save_insertmode = p_im;
9417 int save_finish_op = finish_op;
Bram Moolenaar38084112008-07-26 14:05:07 +00009418 int save_opcount = opcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009419#ifdef FEAT_MBYTE
9420 char_u *arg = NULL;
9421 int l;
9422 char_u *p;
9423#endif
9424
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00009425 if (ex_normal_lock > 0)
9426 {
9427 EMSG(_(e_secure));
9428 return;
9429 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009430 if (ex_normal_busy >= p_mmd)
9431 {
9432 EMSG(_("E192: Recursive use of :normal too deep"));
9433 return;
9434 }
9435 ++ex_normal_busy;
9436
9437 msg_scroll = FALSE; /* no msg scrolling in Normal mode */
9438 restart_edit = 0; /* don't go to Insert mode */
9439 p_im = FALSE; /* don't use 'insertmode' */
9440
9441#ifdef FEAT_MBYTE
9442 /*
9443 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
9444 * this for the K_SPECIAL leading byte, otherwise special keys will not
9445 * work.
9446 */
9447 if (has_mbyte)
9448 {
9449 int len = 0;
9450
9451 /* Count the number of characters to be escaped. */
9452 for (p = eap->arg; *p != NUL; ++p)
9453 {
9454# ifdef FEAT_GUI
9455 if (*p == CSI) /* leadbyte CSI */
9456 len += 2;
9457# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009458 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009459 if (*++p == K_SPECIAL /* trailbyte K_SPECIAL or CSI */
9460# ifdef FEAT_GUI
9461 || *p == CSI
9462# endif
9463 )
9464 len += 2;
9465 }
9466 if (len > 0)
9467 {
9468 arg = alloc((unsigned)(STRLEN(eap->arg) + len + 1));
9469 if (arg != NULL)
9470 {
9471 len = 0;
9472 for (p = eap->arg; *p != NUL; ++p)
9473 {
9474 arg[len++] = *p;
9475# ifdef FEAT_GUI
9476 if (*p == CSI)
9477 {
9478 arg[len++] = KS_EXTRA;
9479 arg[len++] = (int)KE_CSI;
9480 }
9481# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009482 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009483 {
9484 arg[len++] = *++p;
9485 if (*p == K_SPECIAL)
9486 {
9487 arg[len++] = KS_SPECIAL;
9488 arg[len++] = KE_FILLER;
9489 }
9490# ifdef FEAT_GUI
9491 else if (*p == CSI)
9492 {
9493 arg[len++] = KS_EXTRA;
9494 arg[len++] = (int)KE_CSI;
9495 }
9496# endif
9497 }
9498 arg[len] = NUL;
9499 }
9500 }
9501 }
9502 }
9503#endif
9504
9505 /*
9506 * Save the current typeahead. This is required to allow using ":normal"
9507 * from an event handler and makes sure we don't hang when the argument
9508 * ends with half a command.
9509 */
9510 save_typeahead(&tabuf);
9511 if (tabuf.typebuf_valid)
9512 {
9513 /*
9514 * Repeat the :normal command for each line in the range. When no
9515 * range given, execute it just once, without positioning the cursor
9516 * first.
9517 */
9518 do
9519 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009520 if (eap->addr_count != 0)
9521 {
9522 curwin->w_cursor.lnum = eap->line1++;
9523 curwin->w_cursor.col = 0;
9524 }
9525
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009526 exec_normal_cmd(
Bram Moolenaar071d4272004-06-13 20:20:40 +00009527#ifdef FEAT_MBYTE
9528 arg != NULL ? arg :
9529#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009530 eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009531 }
9532 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
9533 }
9534
9535 /* Might not return to the main loop when in an event handler. */
9536 update_topline_cursor();
9537
9538 /* Restore the previous typeahead. */
9539 restore_typeahead(&tabuf);
9540
9541 --ex_normal_busy;
9542 msg_scroll = save_msg_scroll;
9543 restart_edit = save_restart_edit;
9544 p_im = save_insertmode;
9545 finish_op = save_finish_op;
Bram Moolenaar38084112008-07-26 14:05:07 +00009546 opcount = save_opcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009547 msg_didout |= save_msg_didout; /* don't reset msg_didout now */
9548
9549 /* Restore the state (needed when called from a function executed for
9550 * 'indentexpr'). */
9551 State = save_State;
9552#ifdef FEAT_MBYTE
9553 vim_free(arg);
9554#endif
9555}
9556
9557/*
Bram Moolenaar64969662005-12-14 21:59:55 +00009558 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009559 */
9560 static void
9561ex_startinsert(eap)
9562 exarg_T *eap;
9563{
Bram Moolenaarfd371682005-01-14 21:42:54 +00009564 if (eap->forceit)
9565 {
9566 coladvance((colnr_T)MAXCOL);
9567 curwin->w_curswant = MAXCOL;
9568 curwin->w_set_curswant = FALSE;
9569 }
9570
Bram Moolenaara40c5002005-01-09 21:16:21 +00009571 /* Ignore the command when already in Insert mode. Inserting an
9572 * expression register that invokes a function can do this. */
9573 if (State & INSERT)
9574 return;
9575
Bram Moolenaar64969662005-12-14 21:59:55 +00009576 if (eap->cmdidx == CMD_startinsert)
9577 restart_edit = 'a';
9578 else if (eap->cmdidx == CMD_startreplace)
9579 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00009580 else
Bram Moolenaar64969662005-12-14 21:59:55 +00009581 restart_edit = 'V';
9582
9583 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009584 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009585 if (eap->cmdidx == CMD_startinsert)
9586 restart_edit = 'i';
Bram Moolenaar071d4272004-06-13 20:20:40 +00009587 curwin->w_curswant = 0; /* avoid MAXCOL */
9588 }
9589}
9590
9591/*
9592 * ":stopinsert"
9593 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009594 static void
9595ex_stopinsert(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009596 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009597{
9598 restart_edit = 0;
9599 stop_insert_mode = TRUE;
9600}
9601#endif
9602
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009603#if defined(FEAT_EX_EXTRA) || defined(FEAT_MENU) || defined(PROTO)
9604/*
9605 * Execute normal mode command "cmd".
9606 * "remap" can be REMAP_NONE or REMAP_YES.
9607 */
9608 void
9609exec_normal_cmd(cmd, remap, silent)
9610 char_u *cmd;
9611 int remap;
9612 int silent;
9613{
9614 oparg_T oa;
9615
9616 /*
9617 * Stuff the argument into the typeahead buffer.
9618 * Execute normal_cmd() until there is no typeahead left.
9619 */
9620 clear_oparg(&oa);
9621 finish_op = FALSE;
9622 ins_typebuf(cmd, remap, 0, TRUE, silent);
9623 while ((!stuff_empty() || (!typebuf_typed() && typebuf.tb_len > 0))
9624 && !got_int)
9625 {
9626 update_topline_cursor();
Bram Moolenaar48ac02c2011-01-17 19:50:06 +01009627 normal_cmd(&oa, TRUE); /* execute a Normal mode cmd */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009628 }
9629}
9630#endif
9631
Bram Moolenaar071d4272004-06-13 20:20:40 +00009632#ifdef FEAT_FIND_ID
9633 static void
9634ex_checkpath(eap)
9635 exarg_T *eap;
9636{
9637 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
9638 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
9639 (linenr_T)1, (linenr_T)MAXLNUM);
9640}
9641
9642#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
9643/*
9644 * ":psearch"
9645 */
9646 static void
9647ex_psearch(eap)
9648 exarg_T *eap;
9649{
9650 g_do_tagpreview = p_pvh;
9651 ex_findpat(eap);
9652 g_do_tagpreview = 0;
9653}
9654#endif
9655
9656 static void
9657ex_findpat(eap)
9658 exarg_T *eap;
9659{
9660 int whole = TRUE;
9661 long n;
9662 char_u *p;
9663 int action;
9664
9665 switch (cmdnames[eap->cmdidx].cmd_name[2])
9666 {
9667 case 'e': /* ":psearch", ":isearch" and ":dsearch" */
9668 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
9669 action = ACTION_GOTO;
9670 else
9671 action = ACTION_SHOW;
9672 break;
9673 case 'i': /* ":ilist" and ":dlist" */
9674 action = ACTION_SHOW_ALL;
9675 break;
9676 case 'u': /* ":ijump" and ":djump" */
9677 action = ACTION_GOTO;
9678 break;
9679 default: /* ":isplit" and ":dsplit" */
9680 action = ACTION_SPLIT;
9681 break;
9682 }
9683
9684 n = 1;
9685 if (vim_isdigit(*eap->arg)) /* get count */
9686 {
9687 n = getdigits(&eap->arg);
9688 eap->arg = skipwhite(eap->arg);
9689 }
9690 if (*eap->arg == '/') /* Match regexp, not just whole words */
9691 {
9692 whole = FALSE;
9693 ++eap->arg;
9694 p = skip_regexp(eap->arg, '/', p_magic, NULL);
9695 if (*p)
9696 {
9697 *p++ = NUL;
9698 p = skipwhite(p);
9699
9700 /* Check for trailing illegal characters */
9701 if (!ends_excmd(*p))
9702 eap->errmsg = e_trailing;
9703 else
9704 eap->nextcmd = check_nextcmd(p);
9705 }
9706 }
9707 if (!eap->skip)
9708 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
9709 whole, !eap->forceit,
9710 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
9711 n, action, eap->line1, eap->line2);
9712}
9713#endif
9714
9715#ifdef FEAT_WINDOWS
9716
9717# ifdef FEAT_QUICKFIX
9718/*
9719 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
9720 */
9721 static void
9722ex_ptag(eap)
9723 exarg_T *eap;
9724{
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01009725 g_do_tagpreview = p_pvh; /* will be reset to 0 in ex_tag_cmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009726 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9727}
9728
9729/*
9730 * ":pedit"
9731 */
9732 static void
9733ex_pedit(eap)
9734 exarg_T *eap;
9735{
9736 win_T *curwin_save = curwin;
9737
9738 g_do_tagpreview = p_pvh;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00009739 prepare_tagpreview(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009740 keep_help_flag = curwin_save->w_buffer->b_help;
9741 do_exedit(eap, NULL);
9742 keep_help_flag = FALSE;
9743 if (curwin != curwin_save && win_valid(curwin_save))
9744 {
9745 /* Return cursor to where we were */
9746 validate_cursor();
9747 redraw_later(VALID);
9748 win_enter(curwin_save, TRUE);
9749 }
9750 g_do_tagpreview = 0;
9751}
9752# endif
9753
9754/*
9755 * ":stag", ":stselect" and ":stjump".
9756 */
9757 static void
9758ex_stag(eap)
9759 exarg_T *eap;
9760{
9761 postponed_split = -1;
9762 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009763 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009764 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9765 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009766 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009767}
9768#endif
9769
9770/*
9771 * ":tag", ":tselect", ":tjump", ":tnext", etc.
9772 */
9773 static void
9774ex_tag(eap)
9775 exarg_T *eap;
9776{
9777 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
9778}
9779
9780 static void
9781ex_tag_cmd(eap, name)
9782 exarg_T *eap;
9783 char_u *name;
9784{
9785 int cmd;
9786
9787 switch (name[1])
9788 {
9789 case 'j': cmd = DT_JUMP; /* ":tjump" */
9790 break;
9791 case 's': cmd = DT_SELECT; /* ":tselect" */
9792 break;
9793 case 'p': cmd = DT_PREV; /* ":tprevious" */
9794 break;
9795 case 'N': cmd = DT_PREV; /* ":tNext" */
9796 break;
9797 case 'n': cmd = DT_NEXT; /* ":tnext" */
9798 break;
9799 case 'o': cmd = DT_POP; /* ":pop" */
9800 break;
9801 case 'f': /* ":tfirst" */
9802 case 'r': cmd = DT_FIRST; /* ":trewind" */
9803 break;
9804 case 'l': cmd = DT_LAST; /* ":tlast" */
9805 break;
9806 default: /* ":tag" */
9807#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +00009808 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009809 {
9810 do_cstag(eap);
9811 return;
9812 }
9813#endif
9814 cmd = DT_TAG;
9815 break;
9816 }
9817
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00009818 if (name[0] == 'l')
9819 {
9820#ifndef FEAT_QUICKFIX
9821 ex_ni(eap);
9822 return;
9823#else
9824 cmd = DT_LTAG;
9825#endif
9826 }
9827
Bram Moolenaar071d4272004-06-13 20:20:40 +00009828 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
9829 eap->forceit, TRUE);
9830}
9831
9832/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009833 * Check "str" for starting with a special cmdline variable.
9834 * If found return one of the SPEC_ values and set "*usedlen" to the length of
9835 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
9836 */
9837 int
9838find_cmdline_var(src, usedlen)
9839 char_u *src;
9840 int *usedlen;
9841{
9842 int len;
9843 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +00009844 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009845 "%",
9846#define SPEC_PERC 0
9847 "#",
9848#define SPEC_HASH 1
9849 "<cword>", /* cursor word */
9850#define SPEC_CWORD 2
9851 "<cWORD>", /* cursor WORD */
9852#define SPEC_CCWORD 3
9853 "<cfile>", /* cursor path name */
9854#define SPEC_CFILE 4
9855 "<sfile>", /* ":so" file name */
9856#define SPEC_SFILE 5
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009857 "<slnum>", /* ":so" file line number */
9858#define SPEC_SLNUM 6
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009859#ifdef FEAT_AUTOCMD
9860 "<afile>", /* autocommand file name */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009861# define SPEC_AFILE 7
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009862 "<abuf>", /* autocommand buffer number */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009863# define SPEC_ABUF 8
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009864 "<amatch>", /* autocommand match name */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009865# define SPEC_AMATCH 9
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009866#endif
9867#ifdef FEAT_CLIENTSERVER
9868 "<client>"
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009869# ifdef FEAT_AUTOCMD
9870# define SPEC_CLIENT 10
9871# else
9872# define SPEC_CLIENT 7
9873# endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009874#endif
9875 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009876
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009877 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009878 {
9879 len = (int)STRLEN(spec_str[i]);
9880 if (STRNCMP(src, spec_str[i], len) == 0)
9881 {
9882 *usedlen = len;
9883 return i;
9884 }
9885 }
9886 return -1;
9887}
9888
9889/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009890 * Evaluate cmdline variables.
9891 *
9892 * change '%' to curbuf->b_ffname
9893 * '#' to curwin->w_altfile
9894 * '<cword>' to word under the cursor
9895 * '<cWORD>' to WORD under the cursor
9896 * '<cfile>' to path name under the cursor
9897 * '<sfile>' to sourced file name
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009898 * '<slnum>' to sourced file line number
Bram Moolenaar071d4272004-06-13 20:20:40 +00009899 * '<afile>' to file name for autocommand
9900 * '<abuf>' to buffer number for autocommand
9901 * '<amatch>' to matching name for autocommand
9902 *
9903 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
9904 * "" for error without a message) and NULL is returned.
9905 * Returns an allocated string if a valid match was found.
9906 * Returns NULL if no match was found. "usedlen" then still contains the
9907 * number of characters to skip.
9908 */
9909 char_u *
Bram Moolenaar63b92542007-03-27 14:57:09 +00009910eval_vars(src, srcstart, usedlen, lnump, errormsg, escaped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009911 char_u *src; /* pointer into commandline */
Bram Moolenaar63b92542007-03-27 14:57:09 +00009912 char_u *srcstart; /* beginning of valid memory for src */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009913 int *usedlen; /* characters after src that are used */
9914 linenr_T *lnump; /* line number for :e command, or NULL */
9915 char_u **errormsg; /* pointer to error message */
Bram Moolenaar63b92542007-03-27 14:57:09 +00009916 int *escaped; /* return value has escaped white space (can
9917 * be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009918{
9919 int i;
9920 char_u *s;
9921 char_u *result;
9922 char_u *resultbuf = NULL;
9923 int resultlen;
9924 buf_T *buf;
9925 int valid = VALID_HEAD + VALID_PATH; /* assume valid result */
9926 int spec_idx;
9927#ifdef FEAT_MODIFY_FNAME
9928 int skip_mod = FALSE;
9929#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009930 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009931
9932 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009933 if (escaped != NULL)
9934 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009935
9936 /*
9937 * Check if there is something to do.
9938 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009939 spec_idx = find_cmdline_var(src, usedlen);
9940 if (spec_idx < 0) /* no match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009941 {
9942 *usedlen = 1;
9943 return NULL;
9944 }
9945
9946 /*
9947 * Skip when preceded with a backslash "\%" and "\#".
9948 * Note: In "\\%" the % is also not recognized!
9949 */
9950 if (src > srcstart && src[-1] == '\\')
9951 {
9952 *usedlen = 0;
Bram Moolenaara7241f52008-06-24 20:39:31 +00009953 STRMOVE(src - 1, src); /* remove backslash */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009954 return NULL;
9955 }
9956
9957 /*
9958 * word or WORD under cursor
9959 */
9960 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD)
9961 {
9962 resultlen = find_ident_under_cursor(&result, spec_idx == SPEC_CWORD ?
9963 (FIND_IDENT|FIND_STRING) : FIND_STRING);
9964 if (resultlen == 0)
9965 {
9966 *errormsg = (char_u *)"";
9967 return NULL;
9968 }
9969 }
9970
9971 /*
9972 * '#': Alternate file name
9973 * '%': Current file name
9974 * File name under the cursor
9975 * File name for autocommand
9976 * and following modifiers
9977 */
9978 else
9979 {
9980 switch (spec_idx)
9981 {
9982 case SPEC_PERC: /* '%': current file */
9983 if (curbuf->b_fname == NULL)
9984 {
9985 result = (char_u *)"";
9986 valid = 0; /* Must have ":p:h" to be valid */
9987 }
9988 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009989 result = curbuf->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009990 break;
9991
9992 case SPEC_HASH: /* '#' or "#99": alternate file */
9993 if (src[1] == '#') /* "##": the argument list */
9994 {
9995 result = arg_all();
9996 resultbuf = result;
9997 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009998 if (escaped != NULL)
9999 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010000#ifdef FEAT_MODIFY_FNAME
10001 skip_mod = TRUE;
10002#endif
10003 break;
10004 }
10005 s = src + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +000010006 if (*s == '<') /* "#<99" uses v:oldfiles */
10007 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010008 i = (int)getdigits(&s);
10009 *usedlen = (int)(s - src); /* length of what we expand */
10010
Bram Moolenaard812df62008-11-09 12:46:09 +000010011 if (src[1] == '<')
Bram Moolenaar071d4272004-06-13 20:20:40 +000010012 {
Bram Moolenaard812df62008-11-09 12:46:09 +000010013 if (*usedlen < 2)
10014 {
10015 /* Should we give an error message for #<text? */
10016 *usedlen = 1;
10017 return NULL;
10018 }
10019#ifdef FEAT_EVAL
10020 result = list_find_str(get_vim_var_list(VV_OLDFILES),
10021 (long)i);
10022 if (result == NULL)
10023 {
10024 *errormsg = (char_u *)"";
10025 return NULL;
10026 }
10027#else
10028 *errormsg = (char_u *)_("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +000010029 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +000010030#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010031 }
10032 else
Bram Moolenaard812df62008-11-09 12:46:09 +000010033 {
10034 buf = buflist_findnr(i);
10035 if (buf == NULL)
10036 {
10037 *errormsg = (char_u *)_("E194: No alternate file name to substitute for '#'");
10038 return NULL;
10039 }
10040 if (lnump != NULL)
10041 *lnump = ECMD_LAST;
10042 if (buf->b_fname == NULL)
10043 {
10044 result = (char_u *)"";
10045 valid = 0; /* Must have ":p:h" to be valid */
10046 }
10047 else
10048 result = buf->b_fname;
10049 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010050 break;
10051
10052#ifdef FEAT_SEARCHPATH
10053 case SPEC_CFILE: /* file name under cursor */
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010054 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010055 if (result == NULL)
10056 {
10057 *errormsg = (char_u *)"";
10058 return NULL;
10059 }
10060 resultbuf = result; /* remember allocated string */
10061 break;
10062#endif
10063
10064#ifdef FEAT_AUTOCMD
10065 case SPEC_AFILE: /* file name for autocommand */
10066 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +000010067 if (result != NULL && !autocmd_fname_full)
10068 {
10069 /* Still need to turn the fname into a full path. It is
10070 * postponed to avoid a delay when <afile> is not used. */
10071 autocmd_fname_full = TRUE;
10072 result = FullName_save(autocmd_fname, FALSE);
10073 vim_free(autocmd_fname);
10074 autocmd_fname = result;
10075 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010076 if (result == NULL)
10077 {
10078 *errormsg = (char_u *)_("E495: no autocommand file name to substitute for \"<afile>\"");
10079 return NULL;
10080 }
Bram Moolenaara0174af2008-01-02 20:08:25 +000010081 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010082 break;
10083
10084 case SPEC_ABUF: /* buffer number for autocommand */
10085 if (autocmd_bufnr <= 0)
10086 {
10087 *errormsg = (char_u *)_("E496: no autocommand buffer number to substitute for \"<abuf>\"");
10088 return NULL;
10089 }
10090 sprintf((char *)strbuf, "%d", autocmd_bufnr);
10091 result = strbuf;
10092 break;
10093
10094 case SPEC_AMATCH: /* match name for autocommand */
10095 result = autocmd_match;
10096 if (result == NULL)
10097 {
10098 *errormsg = (char_u *)_("E497: no autocommand match name to substitute for \"<amatch>\"");
10099 return NULL;
10100 }
10101 break;
10102
10103#endif
10104 case SPEC_SFILE: /* file name for ":so" command */
10105 result = sourcing_name;
10106 if (result == NULL)
10107 {
10108 *errormsg = (char_u *)_("E498: no :source file name to substitute for \"<sfile>\"");
10109 return NULL;
10110 }
10111 break;
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010112 case SPEC_SLNUM: /* line in file for ":so" command */
10113 if (sourcing_name == NULL || sourcing_lnum == 0)
10114 {
10115 *errormsg = (char_u *)_("E842: no line number to use for \"<slnum>\"");
10116 return NULL;
10117 }
10118 sprintf((char *)strbuf, "%ld", (long)sourcing_lnum);
10119 result = strbuf;
10120 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010121#if defined(FEAT_CLIENTSERVER)
10122 case SPEC_CLIENT: /* Source of last submitted input */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010123 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
10124 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010125 result = strbuf;
10126 break;
10127#endif
10128 }
10129
10130 resultlen = (int)STRLEN(result); /* length of new string */
10131 if (src[*usedlen] == '<') /* remove the file name extension */
10132 {
10133 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010134 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010135 resultlen = (int)(s - result);
10136 }
10137#ifdef FEAT_MODIFY_FNAME
10138 else if (!skip_mod)
10139 {
10140 valid |= modify_fname(src, usedlen, &result, &resultbuf,
10141 &resultlen);
10142 if (result == NULL)
10143 {
10144 *errormsg = (char_u *)"";
10145 return NULL;
10146 }
10147 }
10148#endif
10149 }
10150
10151 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
10152 {
10153 if (valid != VALID_HEAD + VALID_PATH)
10154 /* xgettext:no-c-format */
10155 *errormsg = (char_u *)_("E499: Empty file name for '%' or '#', only works with \":p:h\"");
10156 else
10157 *errormsg = (char_u *)_("E500: Evaluates to an empty string");
10158 result = NULL;
10159 }
10160 else
10161 result = vim_strnsave(result, resultlen);
10162 vim_free(resultbuf);
10163 return result;
10164}
10165
10166/*
10167 * Concatenate all files in the argument list, separated by spaces, and return
10168 * it in one allocated string.
10169 * Spaces and backslashes in the file names are escaped with a backslash.
10170 * Returns NULL when out of memory.
10171 */
10172 static char_u *
10173arg_all()
10174{
10175 int len;
10176 int idx;
10177 char_u *retval = NULL;
10178 char_u *p;
10179
10180 /*
10181 * Do this loop two times:
10182 * first time: compute the total length
10183 * second time: concatenate the names
10184 */
10185 for (;;)
10186 {
10187 len = 0;
10188 for (idx = 0; idx < ARGCOUNT; ++idx)
10189 {
10190 p = alist_name(&ARGLIST[idx]);
10191 if (p != NULL)
10192 {
10193 if (len > 0)
10194 {
10195 /* insert a space in between names */
10196 if (retval != NULL)
10197 retval[len] = ' ';
10198 ++len;
10199 }
10200 for ( ; *p != NUL; ++p)
10201 {
10202 if (*p == ' ' || *p == '\\')
10203 {
10204 /* insert a backslash */
10205 if (retval != NULL)
10206 retval[len] = '\\';
10207 ++len;
10208 }
10209 if (retval != NULL)
10210 retval[len] = *p;
10211 ++len;
10212 }
10213 }
10214 }
10215
10216 /* second time: break here */
10217 if (retval != NULL)
10218 {
10219 retval[len] = NUL;
10220 break;
10221 }
10222
10223 /* allocate memory */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000010224 retval = alloc((unsigned)len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010225 if (retval == NULL)
10226 break;
10227 }
10228
10229 return retval;
10230}
10231
10232#if defined(FEAT_AUTOCMD) || defined(PROTO)
10233/*
10234 * Expand the <sfile> string in "arg".
10235 *
10236 * Returns an allocated string, or NULL for any error.
10237 */
10238 char_u *
10239expand_sfile(arg)
10240 char_u *arg;
10241{
10242 char_u *errormsg;
10243 int len;
10244 char_u *result;
10245 char_u *newres;
10246 char_u *repl;
10247 int srclen;
10248 char_u *p;
10249
10250 result = vim_strsave(arg);
10251 if (result == NULL)
10252 return NULL;
10253
10254 for (p = result; *p; )
10255 {
10256 if (STRNCMP(p, "<sfile>", 7) != 0)
10257 ++p;
10258 else
10259 {
10260 /* replace "<sfile>" with the sourced file name, and do ":" stuff */
Bram Moolenaar63b92542007-03-27 14:57:09 +000010261 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010262 if (errormsg != NULL)
10263 {
10264 if (*errormsg)
10265 emsg(errormsg);
10266 vim_free(result);
10267 return NULL;
10268 }
10269 if (repl == NULL) /* no match (cannot happen) */
10270 {
10271 p += srclen;
10272 continue;
10273 }
10274 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
10275 newres = alloc(len);
10276 if (newres == NULL)
10277 {
10278 vim_free(repl);
10279 vim_free(result);
10280 return NULL;
10281 }
10282 mch_memmove(newres, result, (size_t)(p - result));
10283 STRCPY(newres + (p - result), repl);
10284 len = (int)STRLEN(newres);
10285 STRCAT(newres, p + srclen);
10286 vim_free(repl);
10287 vim_free(result);
10288 result = newres;
10289 p = newres + len; /* continue after the match */
10290 }
10291 }
10292
10293 return result;
10294}
10295#endif
10296
10297#ifdef FEAT_SESSION
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010298static int ses_winsizes __ARGS((FILE *fd, int restore_size,
10299 win_T *tab_firstwin));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010300static int ses_win_rec __ARGS((FILE *fd, frame_T *fr));
10301static frame_T *ses_skipframe __ARGS((frame_T *fr));
10302static int ses_do_frame __ARGS((frame_T *fr));
10303static int ses_do_win __ARGS((win_T *wp));
10304static int ses_arglist __ARGS((FILE *fd, char *cmd, garray_T *gap, int fullname, unsigned *flagp));
10305static int ses_put_fname __ARGS((FILE *fd, char_u *name, unsigned *flagp));
10306static int ses_fname __ARGS((FILE *fd, buf_T *buf, unsigned *flagp));
10307
10308/*
10309 * Write openfile commands for the current buffers to an .exrc file.
10310 * Return FAIL on error, OK otherwise.
10311 */
10312 static int
10313makeopens(fd, dirnow)
10314 FILE *fd;
10315 char_u *dirnow; /* Current directory name */
10316{
10317 buf_T *buf;
10318 int only_save_windows = TRUE;
10319 int nr;
10320 int cnr = 1;
10321 int restore_size = TRUE;
10322 win_T *wp;
10323 char_u *sname;
10324 win_T *edited_win = NULL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010325 int tabnr;
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020010326 int restore_stal = FALSE;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010327 win_T *tab_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010328 frame_T *tab_topframe;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010329 int cur_arg_idx = 0;
Bram Moolenaar383c6f52008-01-04 15:01:07 +000010330 int next_arg_idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010331
10332 if (ssop_flags & SSOP_BUFFERS)
10333 only_save_windows = FALSE; /* Save ALL buffers */
10334
10335 /*
10336 * Begin by setting the this_session variable, and then other
10337 * sessionable variables.
10338 */
10339#ifdef FEAT_EVAL
10340 if (put_line(fd, "let v:this_session=expand(\"<sfile>:p\")") == FAIL)
10341 return FAIL;
10342 if (ssop_flags & SSOP_GLOBALS)
10343 if (store_session_globals(fd) == FAIL)
10344 return FAIL;
10345#endif
10346
10347 /*
10348 * Close all windows but one.
10349 */
10350 if (put_line(fd, "silent only") == FAIL)
10351 return FAIL;
10352
10353 /*
10354 * Now a :cd command to the session directory or the current directory
10355 */
10356 if (ssop_flags & SSOP_SESDIR)
10357 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010358 if (put_line(fd, "exe \"cd \" . escape(expand(\"<sfile>:p:h\"), ' ')")
10359 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010360 return FAIL;
10361 }
10362 else if (ssop_flags & SSOP_CURDIR)
10363 {
10364 sname = home_replace_save(NULL, globaldir != NULL ? globaldir : dirnow);
10365 if (sname == NULL
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010366 || fputs("cd ", fd) < 0
10367 || ses_put_fname(fd, sname, &ssop_flags) == FAIL
10368 || put_eol(fd) == FAIL)
10369 {
10370 vim_free(sname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010371 return FAIL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010372 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010373 vim_free(sname);
10374 }
10375
10376 /*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010377 * If there is an empty, unnamed buffer we will wipe it out later.
10378 * Remember the buffer number.
10379 */
10380 if (put_line(fd, "if expand('%') == '' && !&modified && line('$') <= 1 && getline(1) == ''") == FAIL)
10381 return FAIL;
10382 if (put_line(fd, " let s:wipebuf = bufnr('%')") == FAIL)
10383 return FAIL;
10384 if (put_line(fd, "endif") == FAIL)
10385 return FAIL;
10386
10387 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010388 * Now save the current files, current buffer first.
10389 */
10390 if (put_line(fd, "set shortmess=aoO") == FAIL)
10391 return FAIL;
10392
10393 /* Now put the other buffers into the buffer list */
10394 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
10395 {
10396 if (!(only_save_windows && buf->b_nwindows == 0)
10397 && !(buf->b_help && !(ssop_flags & SSOP_HELP))
10398 && buf->b_fname != NULL
10399 && buf->b_p_bl)
10400 {
10401 if (fprintf(fd, "badd +%ld ", buf->b_wininfo == NULL ? 1L
10402 : buf->b_wininfo->wi_fpos.lnum) < 0
10403 || ses_fname(fd, buf, &ssop_flags) == FAIL)
10404 return FAIL;
10405 }
10406 }
10407
10408 /* the global argument list */
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010010409 if (ses_arglist(fd, "argglobal", &global_alist.al_ga,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010410 !(ssop_flags & SSOP_CURDIR), &ssop_flags) == FAIL)
10411 return FAIL;
10412
10413 if (ssop_flags & SSOP_RESIZE)
10414 {
10415 /* Note: after the restore we still check it worked!*/
10416 if (fprintf(fd, "set lines=%ld columns=%ld" , Rows, Columns) < 0
10417 || put_eol(fd) == FAIL)
10418 return FAIL;
10419 }
10420
10421#ifdef FEAT_GUI
10422 if (gui.in_use && (ssop_flags & SSOP_WINPOS))
10423 {
10424 int x, y;
10425
10426 if (gui_mch_get_winpos(&x, &y) == OK)
10427 {
10428 /* Note: after the restore we still check it worked!*/
10429 if (fprintf(fd, "winpos %d %d", x, y) < 0 || put_eol(fd) == FAIL)
10430 return FAIL;
10431 }
10432 }
10433#endif
10434
10435 /*
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020010436 * When there are two or more tabpages and 'showtabline' is 1 the tabline
10437 * will be displayed when creating the next tab. That resizes the windows
10438 * in the first tab, which may cause problems. Set 'showtabline' to 2
10439 * temporarily to avoid that.
10440 */
10441 if (p_stal == 1 && first_tabpage->tp_next != NULL)
10442 {
10443 if (put_line(fd, "set stal=2") == FAIL)
10444 return FAIL;
10445 restore_stal = TRUE;
10446 }
10447
10448 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000010449 * May repeat putting Windows for each tab, when "tabpages" is in
10450 * 'sessionoptions'.
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010451 * Don't use goto_tabpage(), it may change directory and trigger
10452 * autocommands.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010453 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010454 tab_firstwin = firstwin; /* first window in tab page "tabnr" */
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010455 tab_topframe = topframe;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010456 for (tabnr = 1; ; ++tabnr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010457 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010458 int need_tabnew = FALSE;
10459
Bram Moolenaar18144c82006-04-12 21:52:12 +000010460 if ((ssop_flags & SSOP_TABPAGES))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010461 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010462 tabpage_T *tp = find_tabpage(tabnr);
10463
10464 if (tp == NULL)
10465 break; /* done all tab pages */
10466 if (tp == curtab)
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010467 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010468 tab_firstwin = firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010469 tab_topframe = topframe;
10470 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010471 else
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010472 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010473 tab_firstwin = tp->tp_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010474 tab_topframe = tp->tp_topframe;
10475 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010476 if (tabnr > 1)
10477 need_tabnew = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010478 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010479
Bram Moolenaar18144c82006-04-12 21:52:12 +000010480 /*
10481 * Before creating the window layout, try loading one file. If this
10482 * is aborted we don't end up with a number of useless windows.
10483 * This may have side effects! (e.g., compressed or network file).
10484 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010485 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010486 {
10487 if (ses_do_win(wp)
10488 && wp->w_buffer->b_ffname != NULL
10489 && !wp->w_buffer->b_help
10490#ifdef FEAT_QUICKFIX
10491 && !bt_nofile(wp->w_buffer)
10492#endif
10493 )
10494 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010495 if (fputs(need_tabnew ? "tabedit " : "edit ", fd) < 0
Bram Moolenaar18144c82006-04-12 21:52:12 +000010496 || ses_fname(fd, wp->w_buffer, &ssop_flags) == FAIL)
10497 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010498 need_tabnew = FALSE;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010499 if (!wp->w_arg_idx_invalid)
10500 edited_win = wp;
10501 break;
10502 }
10503 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010504
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010505 /* If no file got edited create an empty tab page. */
10506 if (need_tabnew && put_line(fd, "tabnew") == FAIL)
10507 return FAIL;
10508
Bram Moolenaar18144c82006-04-12 21:52:12 +000010509 /*
10510 * Save current window layout.
10511 */
10512 if (put_line(fd, "set splitbelow splitright") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010513 return FAIL;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010514 if (ses_win_rec(fd, tab_topframe) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010515 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010516 if (!p_sb && put_line(fd, "set nosplitbelow") == FAIL)
10517 return FAIL;
10518 if (!p_spr && put_line(fd, "set nosplitright") == FAIL)
10519 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010520
Bram Moolenaar18144c82006-04-12 21:52:12 +000010521 /*
10522 * Check if window sizes can be restored (no windows omitted).
10523 * Remember the window number of the current window after restoring.
10524 */
10525 nr = 0;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010526 for (wp = tab_firstwin; wp != NULL; wp = W_NEXT(wp))
Bram Moolenaar18144c82006-04-12 21:52:12 +000010527 {
10528 if (ses_do_win(wp))
10529 ++nr;
10530 else
10531 restore_size = FALSE;
10532 if (curwin == wp)
10533 cnr = nr;
10534 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010535
Bram Moolenaar18144c82006-04-12 21:52:12 +000010536 /* Go to the first window. */
10537 if (put_line(fd, "wincmd t") == FAIL)
10538 return FAIL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010539
Bram Moolenaar18144c82006-04-12 21:52:12 +000010540 /*
10541 * If more than one window, see if sizes can be restored.
10542 * First set 'winheight' and 'winwidth' to 1 to avoid the windows being
10543 * resized when moving between windows.
10544 * Do this before restoring the view, so that the topline and the
10545 * cursor can be set. This is done again below.
10546 */
10547 if (put_line(fd, "set winheight=1 winwidth=1") == FAIL)
10548 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010549 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010550 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010551
Bram Moolenaar18144c82006-04-12 21:52:12 +000010552 /*
10553 * Restore the view of the window (options, file, cursor, etc.).
10554 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010555 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010556 {
10557 if (!ses_do_win(wp))
10558 continue;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010559 if (put_view(fd, wp, wp != edited_win, &ssop_flags,
10560 cur_arg_idx) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010561 return FAIL;
10562 if (nr > 1 && put_line(fd, "wincmd w") == FAIL)
10563 return FAIL;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010564 next_arg_idx = wp->w_arg_idx;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010565 }
10566
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010567 /* The argument index in the first tab page is zero, need to set it in
10568 * each window. For further tab pages it's the window where we do
10569 * "tabedit". */
10570 cur_arg_idx = next_arg_idx;
10571
Bram Moolenaar18144c82006-04-12 21:52:12 +000010572 /*
10573 * Restore cursor to the current window if it's not the first one.
10574 */
10575 if (cnr > 1 && (fprintf(fd, "%dwincmd w", cnr) < 0
10576 || put_eol(fd) == FAIL))
10577 return FAIL;
10578
10579 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000010580 * Restore window sizes again after jumping around in windows, because
10581 * the current window has a minimum size while others may not.
10582 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010583 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010584 return FAIL;
10585
Bram Moolenaar18144c82006-04-12 21:52:12 +000010586 /* Don't continue in another tab page when doing only the current one
10587 * or when at the last tab page. */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010588 if (!(ssop_flags & SSOP_TABPAGES))
Bram Moolenaar18144c82006-04-12 21:52:12 +000010589 break;
10590 }
10591
10592 if (ssop_flags & SSOP_TABPAGES)
10593 {
Bram Moolenaar18144c82006-04-12 21:52:12 +000010594 if (fprintf(fd, "tabnext %d", tabpage_index(curtab)) < 0
10595 || put_eol(fd) == FAIL)
10596 return FAIL;
10597 }
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020010598 if (restore_stal && put_line(fd, "set stal=1") == FAIL)
10599 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010600
Bram Moolenaar9c102382006-05-03 21:26:49 +000010601 /*
10602 * Wipe out an empty unnamed buffer we started in.
10603 */
10604 if (put_line(fd, "if exists('s:wipebuf')") == FAIL)
10605 return FAIL;
Bram Moolenaarf3a67882006-05-05 21:09:41 +000010606 if (put_line(fd, " silent exe 'bwipe ' . s:wipebuf") == FAIL)
Bram Moolenaar9c102382006-05-03 21:26:49 +000010607 return FAIL;
10608 if (put_line(fd, "endif") == FAIL)
10609 return FAIL;
10610 if (put_line(fd, "unlet! s:wipebuf") == FAIL)
10611 return FAIL;
10612
10613 /* Re-apply 'winheight', 'winwidth' and 'shortmess'. */
10614 if (fprintf(fd, "set winheight=%ld winwidth=%ld shortmess=%s",
10615 p_wh, p_wiw, p_shm) < 0 || put_eol(fd) == FAIL)
10616 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010617
10618 /*
10619 * Lastly, execute the x.vim file if it exists.
10620 */
10621 if (put_line(fd, "let s:sx = expand(\"<sfile>:p:r\").\"x.vim\"") == FAIL
10622 || put_line(fd, "if file_readable(s:sx)") == FAIL
Bram Moolenaar42ba1262008-12-09 10:18:03 +000010623 || put_line(fd, " exe \"source \" . fnameescape(s:sx)") == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000010624 || put_line(fd, "endif") == FAIL)
10625 return FAIL;
10626
10627 return OK;
10628}
10629
10630 static int
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010631ses_winsizes(fd, restore_size, tab_firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010632 FILE *fd;
10633 int restore_size;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010634 win_T *tab_firstwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010635{
10636 int n = 0;
10637 win_T *wp;
10638
10639 if (restore_size && (ssop_flags & SSOP_WINSIZE))
10640 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010641 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010642 {
10643 if (!ses_do_win(wp))
10644 continue;
10645 ++n;
10646
10647 /* restore height when not full height */
10648 if (wp->w_height + wp->w_status_height < topframe->fr_height
10649 && (fprintf(fd,
10650 "exe '%dresize ' . ((&lines * %ld + %ld) / %ld)",
10651 n, (long)wp->w_height, Rows / 2, Rows) < 0
10652 || put_eol(fd) == FAIL))
10653 return FAIL;
10654
10655 /* restore width when not full width */
10656 if (wp->w_width < Columns && (fprintf(fd,
10657 "exe 'vert %dresize ' . ((&columns * %ld + %ld) / %ld)",
10658 n, (long)wp->w_width, Columns / 2, Columns) < 0
10659 || put_eol(fd) == FAIL))
10660 return FAIL;
10661 }
10662 }
10663 else
10664 {
10665 /* Just equalise window sizes */
10666 if (put_line(fd, "wincmd =") == FAIL)
10667 return FAIL;
10668 }
10669 return OK;
10670}
10671
10672/*
10673 * Write commands to "fd" to recursively create windows for frame "fr",
10674 * horizontally and vertically split.
10675 * After the commands the last window in the frame is the current window.
10676 * Returns FAIL when writing the commands to "fd" fails.
10677 */
10678 static int
10679ses_win_rec(fd, fr)
10680 FILE *fd;
10681 frame_T *fr;
10682{
10683 frame_T *frc;
10684 int count = 0;
10685
10686 if (fr->fr_layout != FR_LEAF)
10687 {
10688 /* Find first frame that's not skipped and then create a window for
10689 * each following one (first frame is already there). */
10690 frc = ses_skipframe(fr->fr_child);
10691 if (frc != NULL)
10692 while ((frc = ses_skipframe(frc->fr_next)) != NULL)
10693 {
10694 /* Make window as big as possible so that we have lots of room
10695 * to split. */
10696 if (put_line(fd, "wincmd _ | wincmd |") == FAIL
10697 || put_line(fd, fr->fr_layout == FR_COL
10698 ? "split" : "vsplit") == FAIL)
10699 return FAIL;
10700 ++count;
10701 }
10702
10703 /* Go back to the first window. */
10704 if (count > 0 && (fprintf(fd, fr->fr_layout == FR_COL
10705 ? "%dwincmd k" : "%dwincmd h", count) < 0
10706 || put_eol(fd) == FAIL))
10707 return FAIL;
10708
10709 /* Recursively create frames/windows in each window of this column or
10710 * row. */
10711 frc = ses_skipframe(fr->fr_child);
10712 while (frc != NULL)
10713 {
10714 ses_win_rec(fd, frc);
10715 frc = ses_skipframe(frc->fr_next);
10716 /* Go to next window. */
10717 if (frc != NULL && put_line(fd, "wincmd w") == FAIL)
10718 return FAIL;
10719 }
10720 }
10721 return OK;
10722}
10723
10724/*
10725 * Skip frames that don't contain windows we want to save in the Session.
10726 * Returns NULL when there none.
10727 */
10728 static frame_T *
10729ses_skipframe(fr)
10730 frame_T *fr;
10731{
10732 frame_T *frc;
10733
10734 for (frc = fr; frc != NULL; frc = frc->fr_next)
10735 if (ses_do_frame(frc))
10736 break;
10737 return frc;
10738}
10739
10740/*
10741 * Return TRUE if frame "fr" has a window somewhere that we want to save in
10742 * the Session.
10743 */
10744 static int
10745ses_do_frame(fr)
10746 frame_T *fr;
10747{
10748 frame_T *frc;
10749
10750 if (fr->fr_layout == FR_LEAF)
10751 return ses_do_win(fr->fr_win);
10752 for (frc = fr->fr_child; frc != NULL; frc = frc->fr_next)
10753 if (ses_do_frame(frc))
10754 return TRUE;
10755 return FALSE;
10756}
10757
10758/*
10759 * Return non-zero if window "wp" is to be stored in the Session.
10760 */
10761 static int
10762ses_do_win(wp)
10763 win_T *wp;
10764{
10765 if (wp->w_buffer->b_fname == NULL
10766#ifdef FEAT_QUICKFIX
10767 /* When 'buftype' is "nofile" can't restore the window contents. */
10768 || bt_nofile(wp->w_buffer)
10769#endif
10770 )
10771 return (ssop_flags & SSOP_BLANK);
10772 if (wp->w_buffer->b_help)
10773 return (ssop_flags & SSOP_HELP);
10774 return TRUE;
10775}
10776
10777/*
10778 * Write commands to "fd" to restore the view of a window.
10779 * Caller must make sure 'scrolloff' is zero.
10780 */
10781 static int
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010782put_view(fd, wp, add_edit, flagp, current_arg_idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010783 FILE *fd;
10784 win_T *wp;
10785 int add_edit; /* add ":edit" command to view */
10786 unsigned *flagp; /* vop_flags or ssop_flags */
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010787 int current_arg_idx; /* current argument index of the window, use
10788 * -1 if unknown */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010789{
10790 win_T *save_curwin;
10791 int f;
10792 int do_cursor;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010793 int did_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010794
10795 /* Always restore cursor position for ":mksession". For ":mkview" only
10796 * when 'viewoptions' contains "cursor". */
10797 do_cursor = (flagp == &ssop_flags || *flagp & SSOP_CURSOR);
10798
10799 /*
10800 * Local argument list.
10801 */
10802 if (wp->w_alist == &global_alist)
10803 {
10804 if (put_line(fd, "argglobal") == FAIL)
10805 return FAIL;
10806 }
10807 else
10808 {
10809 if (ses_arglist(fd, "arglocal", &wp->w_alist->al_ga,
10810 flagp == &vop_flags
10811 || !(*flagp & SSOP_CURDIR)
10812 || wp->w_localdir != NULL, flagp) == FAIL)
10813 return FAIL;
10814 }
10815
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010816 /* Only when part of a session: restore the argument index. Some
10817 * arguments may have been deleted, check if the index is valid. */
Bram Moolenaar51f53df2010-06-26 05:25:54 +020010818 if (wp->w_arg_idx != current_arg_idx && wp->w_arg_idx < WARGCOUNT(wp)
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010819 && flagp == &ssop_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010820 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010821 if (fprintf(fd, "%ldargu", (long)wp->w_arg_idx + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010822 || put_eol(fd) == FAIL)
10823 return FAIL;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010824 did_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010825 }
10826
10827 /* Edit the file. Skip this when ":next" already did it. */
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010828 if (add_edit && (!did_next || wp->w_arg_idx_invalid))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010829 {
10830 /*
10831 * Load the file.
10832 */
10833 if (wp->w_buffer->b_ffname != NULL
10834#ifdef FEAT_QUICKFIX
10835 && !bt_nofile(wp->w_buffer)
10836#endif
10837 )
10838 {
10839 /*
10840 * Editing a file in this buffer: use ":edit file".
10841 * This may have side effects! (e.g., compressed or network file).
10842 */
10843 if (fputs("edit ", fd) < 0
10844 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
10845 return FAIL;
10846 }
10847 else
10848 {
10849 /* No file in this buffer, just make it empty. */
10850 if (put_line(fd, "enew") == FAIL)
10851 return FAIL;
10852#ifdef FEAT_QUICKFIX
10853 if (wp->w_buffer->b_ffname != NULL)
10854 {
10855 /* The buffer does have a name, but it's not a file name. */
10856 if (fputs("file ", fd) < 0
10857 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
10858 return FAIL;
10859 }
10860#endif
10861 do_cursor = FALSE;
10862 }
10863 }
10864
10865 /*
10866 * Local mappings and abbreviations.
10867 */
10868 if ((*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
10869 && makemap(fd, wp->w_buffer) == FAIL)
10870 return FAIL;
10871
10872 /*
10873 * Local options. Need to go to the window temporarily.
10874 * Store only local values when using ":mkview" and when ":mksession" is
10875 * used and 'sessionoptions' doesn't include "options".
10876 * Some folding options are always stored when "folds" is included,
10877 * otherwise the folds would not be restored correctly.
10878 */
10879 save_curwin = curwin;
10880 curwin = wp;
10881 curbuf = curwin->w_buffer;
10882 if (*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
10883 f = makeset(fd, OPT_LOCAL,
10884 flagp == &vop_flags || !(*flagp & SSOP_OPTIONS));
10885#ifdef FEAT_FOLDING
10886 else if (*flagp & SSOP_FOLDS)
10887 f = makefoldset(fd);
10888#endif
10889 else
10890 f = OK;
10891 curwin = save_curwin;
10892 curbuf = curwin->w_buffer;
10893 if (f == FAIL)
10894 return FAIL;
10895
10896#ifdef FEAT_FOLDING
10897 /*
10898 * Save Folds when 'buftype' is empty and for help files.
10899 */
10900 if ((*flagp & SSOP_FOLDS)
10901 && wp->w_buffer->b_ffname != NULL
Bram Moolenaarb1b715d2006-01-21 22:09:43 +000010902# ifdef FEAT_QUICKFIX
10903 && (*wp->w_buffer->b_p_bt == NUL || wp->w_buffer->b_help)
10904# endif
10905 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000010906 {
10907 if (put_folds(fd, wp) == FAIL)
10908 return FAIL;
10909 }
10910#endif
10911
10912 /*
10913 * Set the cursor after creating folds, since that moves the cursor.
10914 */
10915 if (do_cursor)
10916 {
10917
10918 /* Restore the cursor line in the file and relatively in the
10919 * window. Don't use "G", it changes the jumplist. */
10920 if (fprintf(fd, "let s:l = %ld - ((%ld * winheight(0) + %ld) / %ld)",
10921 (long)wp->w_cursor.lnum,
10922 (long)(wp->w_cursor.lnum - wp->w_topline),
10923 (long)wp->w_height / 2, (long)wp->w_height) < 0
10924 || put_eol(fd) == FAIL
10925 || put_line(fd, "if s:l < 1 | let s:l = 1 | endif") == FAIL
10926 || put_line(fd, "exe s:l") == FAIL
10927 || put_line(fd, "normal! zt") == FAIL
10928 || fprintf(fd, "%ld", (long)wp->w_cursor.lnum) < 0
10929 || put_eol(fd) == FAIL)
10930 return FAIL;
10931 /* Restore the cursor column and left offset when not wrapping. */
10932 if (wp->w_cursor.col == 0)
10933 {
10934 if (put_line(fd, "normal! 0") == FAIL)
10935 return FAIL;
10936 }
10937 else
10938 {
10939 if (!wp->w_p_wrap && wp->w_leftcol > 0 && wp->w_width > 0)
10940 {
10941 if (fprintf(fd,
10942 "let s:c = %ld - ((%ld * winwidth(0) + %ld) / %ld)",
Bram Moolenaar558ddad2013-02-20 19:26:29 +010010943 (long)wp->w_virtcol + 1,
10944 (long)(wp->w_virtcol - wp->w_leftcol),
Bram Moolenaar071d4272004-06-13 20:20:40 +000010945 (long)wp->w_width / 2, (long)wp->w_width) < 0
10946 || put_eol(fd) == FAIL
10947 || put_line(fd, "if s:c > 0") == FAIL
10948 || fprintf(fd,
Bram Moolenaar558ddad2013-02-20 19:26:29 +010010949 " exe 'normal! ' . s:c . '|zs' . %ld . '|'",
10950 (long)wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010951 || put_eol(fd) == FAIL
10952 || put_line(fd, "else") == FAIL
Bram Moolenaarfdf447b2013-02-26 17:21:29 +010010953 || fprintf(fd, " normal! 0%d|", wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010954 || put_eol(fd) == FAIL
10955 || put_line(fd, "endif") == FAIL)
10956 return FAIL;
10957 }
10958 else
10959 {
Bram Moolenaar558ddad2013-02-20 19:26:29 +010010960 if (fprintf(fd, "normal! 0%d|", wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010961 || put_eol(fd) == FAIL)
10962 return FAIL;
10963 }
10964 }
10965 }
10966
10967 /*
10968 * Local directory.
10969 */
10970 if (wp->w_localdir != NULL)
10971 {
10972 if (fputs("lcd ", fd) < 0
10973 || ses_put_fname(fd, wp->w_localdir, flagp) == FAIL
10974 || put_eol(fd) == FAIL)
10975 return FAIL;
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010976 did_lcd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010977 }
10978
10979 return OK;
10980}
10981
10982/*
10983 * Write an argument list to the session file.
10984 * Returns FAIL if writing fails.
10985 */
10986 static int
10987ses_arglist(fd, cmd, gap, fullname, flagp)
10988 FILE *fd;
10989 char *cmd;
10990 garray_T *gap;
10991 int fullname; /* TRUE: use full path name */
10992 unsigned *flagp;
10993{
10994 int i;
Bram Moolenaard9462e32011-04-11 21:35:11 +020010995 char_u *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010996 char_u *s;
10997
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010010998 if (fputs(cmd, fd) < 0 || put_eol(fd) == FAIL)
10999 return FAIL;
11000 if (put_line(fd, "silent! argdel *") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011001 return FAIL;
11002 for (i = 0; i < gap->ga_len; ++i)
11003 {
11004 /* NULL file names are skipped (only happens when out of memory). */
11005 s = alist_name(&((aentry_T *)gap->ga_data)[i]);
11006 if (s != NULL)
11007 {
11008 if (fullname)
11009 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020011010 buf = alloc(MAXPATHL);
11011 if (buf != NULL)
11012 {
11013 (void)vim_FullName(s, buf, MAXPATHL, FALSE);
11014 s = buf;
11015 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011016 }
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011017 if (fputs("argadd ", fd) < 0
11018 || ses_put_fname(fd, s, flagp) == FAIL
11019 || put_eol(fd) == FAIL)
Bram Moolenaard9462e32011-04-11 21:35:11 +020011020 {
11021 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011022 return FAIL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011023 }
11024 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011025 }
11026 }
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011027 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011028}
11029
11030/*
11031 * Write a buffer name to the session file.
11032 * Also ends the line.
11033 * Returns FAIL if writing fails.
11034 */
11035 static int
11036ses_fname(fd, buf, flagp)
11037 FILE *fd;
11038 buf_T *buf;
11039 unsigned *flagp;
11040{
11041 char_u *name;
11042
11043 /* Use the short file name if the current directory is known at the time
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011044 * the session file will be sourced.
11045 * Don't do this for ":mkview", we don't know the current directory.
11046 * Don't do this after ":lcd", we don't keep track of what the current
11047 * directory is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011048 if (buf->b_sfname != NULL
11049 && flagp == &ssop_flags
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011050 && (ssop_flags & (SSOP_CURDIR | SSOP_SESDIR))
Bram Moolenaar8d594672009-07-01 18:18:57 +000011051#ifdef FEAT_AUTOCHDIR
11052 && !p_acd
11053#endif
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011054 && !did_lcd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011055 name = buf->b_sfname;
11056 else
11057 name = buf->b_ffname;
11058 if (ses_put_fname(fd, name, flagp) == FAIL || put_eol(fd) == FAIL)
11059 return FAIL;
11060 return OK;
11061}
11062
11063/*
11064 * Write a file name to the session file.
11065 * Takes care of the "slash" option in 'sessionoptions' and escapes special
11066 * characters.
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011067 * Returns FAIL if writing fails or out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011068 */
11069 static int
11070ses_put_fname(fd, name, flagp)
11071 FILE *fd;
11072 char_u *name;
11073 unsigned *flagp;
11074{
11075 char_u *sname;
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011076 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011077 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011078
11079 sname = home_replace_save(NULL, name);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011080 if (sname == NULL)
11081 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011082
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011083 if (*flagp & SSOP_SLASH)
11084 {
11085 /* change all backslashes to forward slashes */
11086 for (p = sname; *p != NUL; mb_ptr_adv(p))
11087 if (*p == '\\')
11088 *p = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +000011089 }
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011090
Bram Moolenaar84a05ac2013-05-06 04:24:17 +020011091 /* escape special characters */
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011092 p = vim_strsave_fnameescape(sname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011093 vim_free(sname);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011094 if (p == NULL)
11095 return FAIL;
11096
11097 /* write the result */
11098 if (fputs((char *)p, fd) < 0)
11099 retval = FAIL;
11100
11101 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011102 return retval;
11103}
11104
11105/*
11106 * ":loadview [nr]"
11107 */
11108 static void
11109ex_loadview(eap)
11110 exarg_T *eap;
11111{
11112 char_u *fname;
11113
11114 fname = get_view_file(*eap->arg);
11115 if (fname != NULL)
11116 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +000011117 do_source(fname, FALSE, DOSO_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011118 vim_free(fname);
11119 }
11120}
11121
11122/*
11123 * Get the name of the view file for the current buffer.
11124 */
11125 static char_u *
11126get_view_file(c)
11127 int c;
11128{
11129 int len = 0;
11130 char_u *p, *s;
11131 char_u *retval;
11132 char_u *sname;
11133
11134 if (curbuf->b_ffname == NULL)
11135 {
11136 EMSG(_(e_noname));
11137 return NULL;
11138 }
11139 sname = home_replace_save(NULL, curbuf->b_ffname);
11140 if (sname == NULL)
11141 return NULL;
11142
11143 /*
11144 * We want a file name without separators, because we're not going to make
11145 * a directory.
11146 * "normal" path separator -> "=+"
11147 * "=" -> "=="
11148 * ":" path separator -> "=-"
11149 */
11150 for (p = sname; *p; ++p)
11151 if (*p == '=' || vim_ispathsep(*p))
11152 ++len;
11153 retval = alloc((unsigned)(STRLEN(sname) + len + STRLEN(p_vdir) + 9));
11154 if (retval != NULL)
11155 {
11156 STRCPY(retval, p_vdir);
11157 add_pathsep(retval);
11158 s = retval + STRLEN(retval);
11159 for (p = sname; *p; ++p)
11160 {
11161 if (*p == '=')
11162 {
11163 *s++ = '=';
11164 *s++ = '=';
11165 }
11166 else if (vim_ispathsep(*p))
11167 {
11168 *s++ = '=';
Bram Moolenaare60acc12011-05-10 16:41:25 +020011169#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011170 if (*p == ':')
11171 *s++ = '-';
11172 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000011173#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000011174 *s++ = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +000011175 }
11176 else
11177 *s++ = *p;
11178 }
11179 *s++ = '=';
11180 *s++ = c;
11181 STRCPY(s, ".vim");
11182 }
11183
11184 vim_free(sname);
11185 return retval;
11186}
11187
11188#endif /* FEAT_SESSION */
11189
11190/*
11191 * Write end-of-line character(s) for ":mkexrc", ":mkvimrc" and ":mksession".
11192 * Return FAIL for a write error.
11193 */
11194 int
11195put_eol(fd)
11196 FILE *fd;
11197{
11198 if (
11199#ifdef USE_CRNL
11200 (
11201# ifdef MKSESSION_NL
11202 !mksession_nl &&
11203# endif
11204 (putc('\r', fd) < 0)) ||
11205#endif
11206 (putc('\n', fd) < 0))
11207 return FAIL;
11208 return OK;
11209}
11210
11211/*
11212 * Write a line to "fd".
11213 * Return FAIL for a write error.
11214 */
11215 int
11216put_line(fd, s)
11217 FILE *fd;
11218 char *s;
11219{
11220 if (fputs(s, fd) < 0 || put_eol(fd) == FAIL)
11221 return FAIL;
11222 return OK;
11223}
11224
11225#ifdef FEAT_VIMINFO
11226/*
11227 * ":rviminfo" and ":wviminfo".
11228 */
11229 static void
11230ex_viminfo(eap)
11231 exarg_T *eap;
11232{
11233 char_u *save_viminfo;
11234
11235 save_viminfo = p_viminfo;
11236 if (*p_viminfo == NUL)
11237 p_viminfo = (char_u *)"'100";
11238 if (eap->cmdidx == CMD_rviminfo)
11239 {
Bram Moolenaard812df62008-11-09 12:46:09 +000011240 if (read_viminfo(eap->arg, VIF_WANT_INFO | VIF_WANT_MARKS
11241 | (eap->forceit ? VIF_FORCEIT : 0)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011242 EMSG(_("E195: Cannot open viminfo file for reading"));
11243 }
11244 else
11245 write_viminfo(eap->arg, eap->forceit);
11246 p_viminfo = save_viminfo;
11247}
11248#endif
11249
11250#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +000011251/*
Bram Moolenaard9462e32011-04-11 21:35:11 +020011252 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +000011253 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +000011254 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011255 void
11256dialog_msg(buff, format, fname)
11257 char_u *buff;
11258 char *format;
11259 char_u *fname;
11260{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011261 if (fname == NULL)
11262 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +020011263 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011264}
11265#endif
11266
11267/*
11268 * ":behave {mswin,xterm}"
11269 */
11270 static void
11271ex_behave(eap)
11272 exarg_T *eap;
11273{
11274 if (STRCMP(eap->arg, "mswin") == 0)
11275 {
11276 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
11277 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
11278 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
11279 set_option_value((char_u *)"keymodel", 0L,
11280 (char_u *)"startsel,stopsel", 0);
11281 }
11282 else if (STRCMP(eap->arg, "xterm") == 0)
11283 {
11284 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
11285 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
11286 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
11287 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
11288 }
11289 else
11290 EMSG2(_(e_invarg2), eap->arg);
11291}
11292
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010011293#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
11294/*
11295 * Function given to ExpandGeneric() to obtain the possible arguments of the
11296 * ":behave {mswin,xterm}" command.
11297 */
11298 char_u *
11299get_behave_arg(xp, idx)
11300 expand_T *xp UNUSED;
11301 int idx;
11302{
11303 if (idx == 0)
11304 return (char_u *)"mswin";
11305 if (idx == 1)
11306 return (char_u *)"xterm";
11307 return NULL;
11308}
11309#endif
11310
Bram Moolenaar071d4272004-06-13 20:20:40 +000011311#ifdef FEAT_AUTOCMD
11312static int filetype_detect = FALSE;
11313static int filetype_plugin = FALSE;
11314static int filetype_indent = FALSE;
11315
11316/*
11317 * ":filetype [plugin] [indent] {on,off,detect}"
11318 * on: Load the filetype.vim file to install autocommands for file types.
11319 * off: Load the ftoff.vim file to remove all autocommands for file types.
11320 * plugin on: load filetype.vim and ftplugin.vim
11321 * plugin off: load ftplugof.vim
11322 * indent on: load filetype.vim and indent.vim
11323 * indent off: load indoff.vim
11324 */
11325 static void
11326ex_filetype(eap)
11327 exarg_T *eap;
11328{
11329 char_u *arg = eap->arg;
11330 int plugin = FALSE;
11331 int indent = FALSE;
11332
11333 if (*eap->arg == NUL)
11334 {
11335 /* Print current status. */
11336 smsg((char_u *)"filetype detection:%s plugin:%s indent:%s",
11337 filetype_detect ? "ON" : "OFF",
11338 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
11339 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
11340 return;
11341 }
11342
11343 /* Accept "plugin" and "indent" in any order. */
11344 for (;;)
11345 {
11346 if (STRNCMP(arg, "plugin", 6) == 0)
11347 {
11348 plugin = TRUE;
11349 arg = skipwhite(arg + 6);
11350 continue;
11351 }
11352 if (STRNCMP(arg, "indent", 6) == 0)
11353 {
11354 indent = TRUE;
11355 arg = skipwhite(arg + 6);
11356 continue;
11357 }
11358 break;
11359 }
11360 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
11361 {
11362 if (*arg == 'o' || !filetype_detect)
11363 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011364 source_runtime((char_u *)FILETYPE_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011365 filetype_detect = TRUE;
11366 if (plugin)
11367 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011368 source_runtime((char_u *)FTPLUGIN_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011369 filetype_plugin = TRUE;
11370 }
11371 if (indent)
11372 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011373 source_runtime((char_u *)INDENT_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011374 filetype_indent = TRUE;
11375 }
11376 }
11377 if (*arg == 'd')
11378 {
11379 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE);
Bram Moolenaara3227e22006-03-08 21:32:40 +000011380 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011381 }
11382 }
11383 else if (STRCMP(arg, "off") == 0)
11384 {
11385 if (plugin || indent)
11386 {
11387 if (plugin)
11388 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011389 source_runtime((char_u *)FTPLUGOF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011390 filetype_plugin = FALSE;
11391 }
11392 if (indent)
11393 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011394 source_runtime((char_u *)INDOFF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011395 filetype_indent = FALSE;
11396 }
11397 }
11398 else
11399 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011400 source_runtime((char_u *)FTOFF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011401 filetype_detect = FALSE;
11402 }
11403 }
11404 else
11405 EMSG2(_(e_invarg2), arg);
11406}
11407
11408/*
11409 * ":setfiletype {name}"
11410 */
11411 static void
11412ex_setfiletype(eap)
11413 exarg_T *eap;
11414{
11415 if (!did_filetype)
11416 set_option_value((char_u *)"filetype", 0L, eap->arg, OPT_LOCAL);
11417}
11418#endif
11419
Bram Moolenaar071d4272004-06-13 20:20:40 +000011420 static void
11421ex_digraphs(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +000011422 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011423{
11424#ifdef FEAT_DIGRAPHS
11425 if (*eap->arg != NUL)
11426 putdigraph(eap->arg);
11427 else
11428 listdigraphs();
11429#else
11430 EMSG(_("E196: No digraphs in this version"));
11431#endif
11432}
11433
11434 static void
11435ex_set(eap)
11436 exarg_T *eap;
11437{
11438 int flags = 0;
11439
11440 if (eap->cmdidx == CMD_setlocal)
11441 flags = OPT_LOCAL;
11442 else if (eap->cmdidx == CMD_setglobal)
11443 flags = OPT_GLOBAL;
11444#if defined(FEAT_EVAL) && defined(FEAT_AUTOCMD) && defined(FEAT_BROWSE)
11445 if (cmdmod.browse && flags == 0)
11446 ex_options(eap);
11447 else
11448#endif
11449 (void)do_set(eap->arg, flags);
11450}
11451
11452#ifdef FEAT_SEARCH_EXTRA
11453/*
11454 * ":nohlsearch"
11455 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011456 static void
11457ex_nohlsearch(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +000011458 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011459{
Bram Moolenaar8050efa2013-11-08 04:30:20 +010011460 SET_NO_HLSEARCH(TRUE);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +000011461 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011462}
11463
11464/*
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011465 * ":[N]match {group} {pattern}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000011466 * Sets nextcmd to the start of the next command, if any. Also called when
11467 * skipping commands to find the next command.
11468 */
11469 static void
11470ex_match(eap)
11471 exarg_T *eap;
11472{
11473 char_u *p;
Bram Moolenaar52d36c82007-08-11 14:00:30 +000011474 char_u *g = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011475 char_u *end;
11476 int c;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011477 int id;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011478
11479 if (eap->line2 <= 3)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011480 id = eap->line2;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011481 else
11482 {
11483 EMSG(e_invcmd);
11484 return;
11485 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011486
11487 /* First clear any old pattern. */
11488 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011489 match_delete(curwin, id, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011490
11491 if (ends_excmd(*eap->arg))
11492 end = eap->arg;
11493 else if ((STRNICMP(eap->arg, "none", 4) == 0
11494 && (vim_iswhite(eap->arg[4]) || ends_excmd(eap->arg[4]))))
11495 end = eap->arg + 4;
11496 else
11497 {
11498 p = skiptowhite(eap->arg);
11499 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011500 g = vim_strnsave(eap->arg, (int)(p - eap->arg));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011501 p = skipwhite(p);
11502 if (*p == NUL)
11503 {
11504 /* There must be two arguments. */
11505 EMSG2(_(e_invarg2), eap->arg);
11506 return;
11507 }
11508 end = skip_regexp(p + 1, *p, TRUE, NULL);
11509 if (!eap->skip)
11510 {
11511 if (*end != NUL && !ends_excmd(*skipwhite(end + 1)))
11512 {
11513 eap->errmsg = e_trailing;
11514 return;
11515 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000011516 if (*end != *p)
11517 {
11518 EMSG2(_(e_invarg2), p);
11519 return;
11520 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011521
11522 c = *end;
11523 *end = NUL;
Bram Moolenaarb3414592014-06-17 17:48:32 +020011524 match_add(curwin, g, p + 1, 10, id, NULL);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011525 vim_free(g);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000011526 *end = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011527 }
11528 }
11529 eap->nextcmd = find_nextcmd(end);
11530}
11531#endif
11532
11533#ifdef FEAT_CRYPT
11534/*
11535 * ":X": Get crypt key
11536 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011537 static void
11538ex_X(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +000011539 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011540{
Bram Moolenaar8f4ac012014-08-10 13:38:34 +020011541 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011542}
11543#endif
11544
11545#ifdef FEAT_FOLDING
11546 static void
11547ex_fold(eap)
11548 exarg_T *eap;
11549{
11550 if (foldManualAllowed(TRUE))
11551 foldCreate(eap->line1, eap->line2);
11552}
11553
11554 static void
11555ex_foldopen(eap)
11556 exarg_T *eap;
11557{
11558 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
11559 eap->forceit, FALSE);
11560}
11561
11562 static void
11563ex_folddo(eap)
11564 exarg_T *eap;
11565{
11566 linenr_T lnum;
11567
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020011568#ifdef FEAT_CLIPBOARD
11569 start_global_changes();
11570#endif
11571
Bram Moolenaar071d4272004-06-13 20:20:40 +000011572 /* First set the marks for all lines closed/open. */
11573 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
11574 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
11575 ml_setmarked(lnum);
11576
11577 /* Execute the command on the marked lines. */
11578 global_exe(eap->arg);
11579 ml_clearmarked(); /* clear rest of the marks */
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020011580#ifdef FEAT_CLIPBOARD
11581 end_global_changes();
11582#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011583}
11584#endif