blob: a88cdd56413010c364ddc07f071e18fb72b7b4fe [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
2146#ifdef FEAT_USR_CMDS
2147 if (p == NULL)
2148 {
2149 if (!ea.skip)
2150 errormsg = (char_u *)_("E464: Ambiguous use of user-defined command");
2151 goto doend;
2152 }
2153 /* Check for wrong commands. */
2154 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78)
2155 {
2156 errormsg = uc_fun_cmd();
2157 goto doend;
2158 }
2159#endif
2160 if (ea.cmdidx == CMD_SIZE)
2161 {
2162 if (!ea.skip)
2163 {
2164 STRCPY(IObuff, _("E492: Not an editor command"));
2165 if (!sourcing)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002166 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167 errormsg = IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02002168 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002169 }
2170 goto doend;
2171 }
2172
2173 ni = (
2174#ifdef FEAT_USR_CMDS
2175 !USER_CMDIDX(ea.cmdidx) &&
2176#endif
Bram Moolenaar3ebc1e52007-07-05 07:54:17 +00002177 (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00002178#ifdef HAVE_EX_SCRIPT_NI
2179 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
2180#endif
2181 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002182
2183#ifndef FEAT_EVAL
2184 /*
2185 * When the expression evaluation is disabled, recognize the ":if" and
2186 * ":endif" commands and ignore everything in between it.
2187 */
2188 if (ea.cmdidx == CMD_if)
2189 ++if_level;
2190 if (if_level)
2191 {
2192 if (ea.cmdidx == CMD_endif)
2193 --if_level;
2194 goto doend;
2195 }
2196
2197#endif
2198
Bram Moolenaar196b3b02008-06-20 16:51:41 +00002199 /* forced commands */
2200 if (*p == '!' && ea.cmdidx != CMD_substitute
2201 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202 {
2203 ++p;
2204 ea.forceit = TRUE;
2205 }
2206 else
2207 ea.forceit = FALSE;
2208
2209/*
2210 * 5. parse arguments
2211 */
2212#ifdef FEAT_USR_CMDS
2213 if (!USER_CMDIDX(ea.cmdidx))
2214#endif
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002215 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002216
2217 if (!ea.skip)
2218 {
2219#ifdef HAVE_SANDBOX
2220 if (sandbox != 0 && !(ea.argt & SBOXOK))
2221 {
2222 /* Command not allowed in sandbox. */
2223 errormsg = (char_u *)_(e_sandbox);
2224 goto doend;
2225 }
2226#endif
2227 if (!curbuf->b_p_ma && (ea.argt & MODIFY))
2228 {
2229 /* Command not allowed in non-'modifiable' buffer */
2230 errormsg = (char_u *)_(e_modifiable);
2231 goto doend;
2232 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002233
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002234 if (text_locked() && !(ea.argt & CMDWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002235# ifdef FEAT_USR_CMDS
2236 && !USER_CMDIDX(ea.cmdidx)
2237# endif
2238 )
2239 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002240 /* Command not allowed when editing the command line. */
2241#ifdef FEAT_CMDWIN
2242 if (cmdwin_type != 0)
2243 errormsg = (char_u *)_(e_cmdwin);
2244 else
2245#endif
2246 errormsg = (char_u *)_(e_secure);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002247 goto doend;
2248 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002249#ifdef FEAT_AUTOCMD
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002250 /* Disallow editing another buffer when "curbuf_lock" is set.
2251 * Do allow ":edit" (check for argument later).
2252 * Do allow ":checktime" (it's postponed). */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002253 if (!(ea.argt & CMDWIN)
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002254 && ea.cmdidx != CMD_edit
2255 && ea.cmdidx != CMD_checktime
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002256# ifdef FEAT_USR_CMDS
2257 && !USER_CMDIDX(ea.cmdidx)
2258# endif
2259 && curbuf_locked())
2260 goto doend;
2261#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002262
2263 if (!ni && !(ea.argt & RANGE) && ea.addr_count > 0)
2264 {
2265 /* no range allowed */
2266 errormsg = (char_u *)_(e_norange);
2267 goto doend;
2268 }
2269 }
2270
2271 if (!ni && !(ea.argt & BANG) && ea.forceit) /* no <!> allowed */
2272 {
2273 errormsg = (char_u *)_(e_nobang);
2274 goto doend;
2275 }
2276
2277 /*
2278 * Don't complain about the range if it is not used
2279 * (could happen if line_count is accidentally set to 0).
2280 */
2281 if (!ea.skip && !ni)
2282 {
2283 /*
2284 * If the range is backwards, ask for confirmation and, if given, swap
2285 * ea.line1 & ea.line2 so it's forwards again.
2286 * When global command is busy, don't ask, will fail below.
2287 */
2288 if (!global_busy && ea.line1 > ea.line2)
2289 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002290 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002292 if (sourcing || exmode_active)
2293 {
2294 errormsg = (char_u *)_("E493: Backwards range given");
2295 goto doend;
2296 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297 if (ask_yesno((char_u *)
2298 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002299 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002300 }
2301 lnum = ea.line1;
2302 ea.line1 = ea.line2;
2303 ea.line2 = lnum;
2304 }
2305 if ((errormsg = invalid_range(&ea)) != NULL)
2306 goto doend;
2307 }
2308
2309 if ((ea.argt & NOTADR) && ea.addr_count == 0) /* default is 1, not cursor */
2310 ea.line2 = 1;
2311
2312 correct_range(&ea);
2313
2314#ifdef FEAT_FOLDING
2315 if (((ea.argt & WHOLEFOLD) || ea.addr_count >= 2) && !global_busy)
2316 {
2317 /* Put the first line at the start of a closed fold, put the last line
2318 * at the end of a closed fold. */
2319 (void)hasFolding(ea.line1, &ea.line1, NULL);
2320 (void)hasFolding(ea.line2, NULL, &ea.line2);
2321 }
2322#endif
2323
2324#ifdef FEAT_QUICKFIX
2325 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002326 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327 * option here, so things like % get expanded.
2328 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002329 p = replace_makeprg(&ea, p, cmdlinep);
2330 if (p == NULL)
2331 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002332#endif
2333
2334 /*
2335 * Skip to start of argument.
2336 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2337 */
2338 if (ea.cmdidx == CMD_bang)
2339 ea.arg = p;
2340 else
2341 ea.arg = skipwhite(p);
2342
2343 /*
2344 * Check for "++opt=val" argument.
2345 * Must be first, allow ":w ++enc=utf8 !cmd"
2346 */
2347 if (ea.argt & ARGOPT)
2348 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2349 if (getargopt(&ea) == FAIL && !ni)
2350 {
2351 errormsg = (char_u *)_(e_invarg);
2352 goto doend;
2353 }
2354
2355 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2356 {
2357 if (*ea.arg == '>') /* append */
2358 {
2359 if (*++ea.arg != '>') /* typed wrong */
2360 {
2361 errormsg = (char_u *)_("E494: Use w or w>>");
2362 goto doend;
2363 }
2364 ea.arg = skipwhite(ea.arg + 1);
2365 ea.append = TRUE;
2366 }
2367 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
2368 {
2369 ++ea.arg;
2370 ea.usefilter = TRUE;
2371 }
2372 }
2373
2374 if (ea.cmdidx == CMD_read)
2375 {
2376 if (ea.forceit)
2377 {
2378 ea.usefilter = TRUE; /* :r! filter if ea.forceit */
2379 ea.forceit = FALSE;
2380 }
2381 else if (*ea.arg == '!') /* :r !filter */
2382 {
2383 ++ea.arg;
2384 ea.usefilter = TRUE;
2385 }
2386 }
2387
2388 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2389 {
2390 ea.amount = 1;
2391 while (*ea.arg == *ea.cmd) /* count number of '>' or '<' */
2392 {
2393 ++ea.arg;
2394 ++ea.amount;
2395 }
2396 ea.arg = skipwhite(ea.arg);
2397 }
2398
2399 /*
2400 * Check for "+command" argument, before checking for next command.
2401 * Don't do this for ":read !cmd" and ":write !cmd".
2402 */
2403 if ((ea.argt & EDITCMD) && !ea.usefilter)
2404 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2405
2406 /*
2407 * Check for '|' to separate commands and '"' to start comments.
2408 * Don't do this for ":read !cmd" and ":write !cmd".
2409 */
2410 if ((ea.argt & TRLBAR) && !ea.usefilter)
2411 separate_nextcmd(&ea);
2412
2413 /*
2414 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002415 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2416 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002417 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002418 else if (ea.cmdidx == CMD_bang
2419 || ea.cmdidx == CMD_global
2420 || ea.cmdidx == CMD_vglobal
2421 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422 {
2423 for (p = ea.arg; *p; ++p)
2424 {
2425 /* Remove one backslash before a newline, so that it's possible to
2426 * pass a newline to the shell and also a newline that is preceded
2427 * with a backslash. This makes it impossible to end a shell
2428 * command in a backslash, but that doesn't appear useful.
2429 * Halving the number of backslashes is incompatible with previous
2430 * versions. */
2431 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002432 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433 else if (*p == '\n')
2434 {
2435 ea.nextcmd = p + 1;
2436 *p = NUL;
2437 break;
2438 }
2439 }
2440 }
2441
2442 if ((ea.argt & DFLALL) && ea.addr_count == 0)
2443 {
2444 ea.line1 = 1;
2445 ea.line2 = curbuf->b_ml.ml_line_count;
2446 }
2447
2448 /* accept numbered register only when no count allowed (:put) */
2449 if ( (ea.argt & REGSTR)
2450 && *ea.arg != NUL
2451#ifdef FEAT_USR_CMDS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002452 /* Do not allow register = for user commands */
2453 && (!USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454#endif
2455 && !((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)))
2456 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002457#ifndef FEAT_CLIPBOARD
2458 /* check these explicitly for a more specific error message */
2459 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002460 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002461 errormsg = (char_u *)_(e_invalidreg);
2462 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463 }
2464#endif
Bram Moolenaar85de2062011-05-05 14:26:41 +02002465 if (
2466#ifdef FEAT_USR_CMDS
2467 valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2468 && USER_CMDIDX(ea.cmdidx)))
2469#else
2470 valid_yank_reg(*ea.arg, ea.cmdidx != CMD_put)
2471#endif
2472 )
2473 {
2474 ea.regname = *ea.arg++;
2475#ifdef FEAT_EVAL
2476 /* for '=' register: accept the rest of the line as an expression */
2477 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2478 {
2479 set_expr_line(vim_strsave(ea.arg));
2480 ea.arg += STRLEN(ea.arg);
2481 }
2482#endif
2483 ea.arg = skipwhite(ea.arg);
2484 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485 }
2486
2487 /*
2488 * Check for a count. When accepting a BUFNAME, don't use "123foo" as a
2489 * count, it's a buffer name.
2490 */
2491 if ((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)
2492 && (!(ea.argt & BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
2493 || vim_iswhite(*p)))
2494 {
2495 n = getdigits(&ea.arg);
2496 ea.arg = skipwhite(ea.arg);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002497 if (n <= 0 && !ni && (ea.argt & ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498 {
2499 errormsg = (char_u *)_(e_zerocount);
2500 goto doend;
2501 }
2502 if (ea.argt & NOTADR) /* e.g. :buffer 2, :sleep 3 */
2503 {
2504 ea.line2 = n;
2505 if (ea.addr_count == 0)
2506 ea.addr_count = 1;
2507 }
2508 else
2509 {
2510 ea.line1 = ea.line2;
2511 ea.line2 += n - 1;
2512 ++ea.addr_count;
2513 /*
2514 * Be vi compatible: no error message for out of range.
2515 */
2516 if (ea.line2 > curbuf->b_ml.ml_line_count)
2517 ea.line2 = curbuf->b_ml.ml_line_count;
2518 }
2519 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002520
2521 /*
2522 * Check for flags: 'l', 'p' and '#'.
2523 */
2524 if (ea.argt & EXFLAGS)
2525 get_flags(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526 /* no arguments allowed */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002527 if (!ni && !(ea.argt & EXTRA) && *ea.arg != NUL
Bram Moolenaara2031822006-03-07 22:29:51 +00002528 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002529 {
2530 errormsg = (char_u *)_(e_trailing);
2531 goto doend;
2532 }
2533
2534 if (!ni && (ea.argt & NEEDARG) && *ea.arg == NUL)
2535 {
2536 errormsg = (char_u *)_(e_argreq);
2537 goto doend;
2538 }
2539
2540#ifdef FEAT_EVAL
2541 /*
2542 * Skip the command when it's not going to be executed.
2543 * The commands like :if, :endif, etc. always need to be executed.
2544 * Also make an exception for commands that handle a trailing command
2545 * themselves.
2546 */
2547 if (ea.skip)
2548 {
2549 switch (ea.cmdidx)
2550 {
2551 /* commands that need evaluation */
2552 case CMD_while:
2553 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002554 case CMD_for:
2555 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002556 case CMD_if:
2557 case CMD_elseif:
2558 case CMD_else:
2559 case CMD_endif:
2560 case CMD_try:
2561 case CMD_catch:
2562 case CMD_finally:
2563 case CMD_endtry:
2564 case CMD_function:
2565 break;
2566
2567 /* Commands that handle '|' themselves. Check: A command should
2568 * either have the TRLBAR flag, appear in this list or appear in
2569 * the list at ":help :bar". */
2570 case CMD_aboveleft:
2571 case CMD_and:
2572 case CMD_belowright:
2573 case CMD_botright:
2574 case CMD_browse:
2575 case CMD_call:
2576 case CMD_confirm:
2577 case CMD_delfunction:
2578 case CMD_djump:
2579 case CMD_dlist:
2580 case CMD_dsearch:
2581 case CMD_dsplit:
2582 case CMD_echo:
2583 case CMD_echoerr:
2584 case CMD_echomsg:
2585 case CMD_echon:
2586 case CMD_execute:
2587 case CMD_help:
2588 case CMD_hide:
2589 case CMD_ijump:
2590 case CMD_ilist:
2591 case CMD_isearch:
2592 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002593 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002594 case CMD_keepjumps:
2595 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01002596 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002597 case CMD_leftabove:
2598 case CMD_let:
2599 case CMD_lockmarks:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002600 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002601 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002602 case CMD_mzscheme:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01002603 case CMD_noautocmd:
2604 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002605 case CMD_perl:
2606 case CMD_psearch:
2607 case CMD_python:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002608 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002609 case CMD_python3:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002610 case CMD_return:
2611 case CMD_rightbelow:
2612 case CMD_ruby:
2613 case CMD_silent:
2614 case CMD_smagic:
2615 case CMD_snomagic:
2616 case CMD_substitute:
2617 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002618 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002619 case CMD_tcl:
2620 case CMD_throw:
2621 case CMD_tilde:
2622 case CMD_topleft:
2623 case CMD_unlet:
2624 case CMD_verbose:
2625 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002626 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002627 break;
2628
2629 default: goto doend;
2630 }
2631 }
2632#endif
2633
2634 if (ea.argt & XFILE)
2635 {
2636 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2637 goto doend;
2638 }
2639
2640#ifdef FEAT_LISTCMDS
2641 /*
2642 * Accept buffer name. Cannot be used at the same time with a buffer
2643 * number. Don't do this for a user command.
2644 */
2645 if ((ea.argt & BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
2646# ifdef FEAT_USR_CMDS
2647 && !USER_CMDIDX(ea.cmdidx)
2648# endif
2649 )
2650 {
2651 /*
2652 * :bdelete, :bwipeout and :bunload take several arguments, separated
2653 * by spaces: find next space (skipping over escaped characters).
2654 * The others take one argument: ignore trailing spaces.
2655 */
2656 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2657 || ea.cmdidx == CMD_bunload)
2658 p = skiptowhite_esc(ea.arg);
2659 else
2660 {
2661 p = ea.arg + STRLEN(ea.arg);
2662 while (p > ea.arg && vim_iswhite(p[-1]))
2663 --p;
2664 }
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002665 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & BUFUNL) != 0,
2666 FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667 if (ea.line2 < 0) /* failed */
2668 goto doend;
2669 ea.addr_count = 1;
2670 ea.arg = skipwhite(p);
2671 }
2672#endif
2673
2674/*
2675 * 6. switch on command name
2676 *
2677 * The "ea" structure holds the arguments that can be used.
2678 */
2679 ea.cmdlinep = cmdlinep;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002680 ea.getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002681 ea.cookie = cookie;
2682#ifdef FEAT_EVAL
2683 ea.cstack = cstack;
2684#endif
2685
2686#ifdef FEAT_USR_CMDS
2687 if (USER_CMDIDX(ea.cmdidx))
2688 {
2689 /*
2690 * Execute a user-defined command.
2691 */
2692 do_ucmd(&ea);
2693 }
2694 else
2695#endif
2696 {
2697 /*
2698 * Call the function to execute the command.
2699 */
2700 ea.errmsg = NULL;
2701 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2702 if (ea.errmsg != NULL)
2703 errormsg = (char_u *)_(ea.errmsg);
2704 }
2705
2706#ifdef FEAT_EVAL
2707 /*
2708 * If the command just executed called do_cmdline(), any throw or ":return"
2709 * or ":finish" encountered there must also check the cstack of the still
2710 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2711 * exception, or reanimate a returned function or finished script file and
2712 * return or finish it again.
2713 */
2714 if (need_rethrow)
2715 do_throw(cstack);
2716 else if (check_cstack)
2717 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002718 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002719 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002720 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002721 && current_func_returned())
2722 do_return(&ea, TRUE, FALSE, NULL);
2723 }
2724 need_rethrow = check_cstack = FALSE;
2725#endif
2726
2727doend:
2728 if (curwin->w_cursor.lnum == 0) /* can happen with zero line number */
2729 curwin->w_cursor.lnum = 1;
2730
2731 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2732 {
2733 if (sourcing)
2734 {
2735 if (errormsg != IObuff)
2736 {
2737 STRCPY(IObuff, errormsg);
2738 errormsg = IObuff;
2739 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002740 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002741 }
2742 emsg(errormsg);
2743 }
2744#ifdef FEAT_EVAL
2745 do_errthrow(cstack,
2746 (ea.cmdidx != CMD_SIZE
2747# ifdef FEAT_USR_CMDS
2748 && !USER_CMDIDX(ea.cmdidx)
2749# endif
2750 ) ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
2751#endif
2752
2753 if (verbose_save >= 0)
2754 p_verbose = verbose_save;
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00002755#ifdef FEAT_AUTOCMD
2756 if (cmdmod.save_ei != NULL)
2757 {
2758 /* Restore 'eventignore' to the value before ":noautocmd". */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002759 set_string_option_direct((char_u *)"ei", -1, cmdmod.save_ei,
2760 OPT_FREE, SID_NONE);
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00002761 free_string_option(cmdmod.save_ei);
2762 }
2763#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002764
2765 cmdmod = save_cmdmod;
2766
Bram Moolenaar8e258a42009-07-09 13:55:43 +00002767 if (save_msg_silent != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002768 {
2769 /* messages could be enabled for a serious error, need to check if the
2770 * counters don't become negative */
Bram Moolenaarbecf4282009-09-30 11:24:36 +00002771 if (!did_emsg || msg_silent > save_msg_silent)
Bram Moolenaar8e258a42009-07-09 13:55:43 +00002772 msg_silent = save_msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002773 emsg_silent -= did_esilent;
2774 if (emsg_silent < 0)
2775 emsg_silent = 0;
2776 /* Restore msg_scroll, it's set by file I/O commands, even when no
2777 * message is actually displayed. */
2778 msg_scroll = save_msg_scroll;
Bram Moolenaar77ab2802009-04-22 12:44:48 +00002779
2780 /* "silent reg" or "silent echo x" inside "redir" leaves msg_col
2781 * somewhere in the line. Put it back in the first column. */
2782 if (redirecting())
2783 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002784 }
2785
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002786#ifdef HAVE_SANDBOX
2787 if (did_sandbox)
2788 --sandbox;
2789#endif
2790
Bram Moolenaar071d4272004-06-13 20:20:40 +00002791 if (ea.nextcmd && *ea.nextcmd == NUL) /* not really a next command */
2792 ea.nextcmd = NULL;
2793
2794#ifdef FEAT_EVAL
2795 --ex_nesting_level;
2796#endif
2797
2798 return ea.nextcmd;
2799}
2800#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00002801 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002802#endif
2803
2804/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002805 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002806 * If there is a match advance "pp" to the argument and return TRUE.
2807 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002808 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00002809checkforcmd(pp, cmd, len)
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002810 char_u **pp; /* start of command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002811 char *cmd; /* name of command */
2812 int len; /* required length */
2813{
2814 int i;
2815
2816 for (i = 0; cmd[i] != NUL; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00002817 if (((char_u *)cmd)[i] != (*pp)[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00002818 break;
2819 if (i >= len && !isalpha((*pp)[i]))
2820 {
2821 *pp = skipwhite(*pp + i);
2822 return TRUE;
2823 }
2824 return FALSE;
2825}
2826
2827/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002828 * Append "cmd" to the error message in IObuff.
2829 * Takes care of limiting the length and handling 0xa0, which would be
2830 * invisible otherwise.
2831 */
2832 static void
2833append_command(cmd)
2834 char_u *cmd;
2835{
2836 char_u *s = cmd;
2837 char_u *d;
2838
2839 STRCAT(IObuff, ": ");
2840 d = IObuff + STRLEN(IObuff);
2841 while (*s != NUL && d - IObuff < IOSIZE - 7)
2842 {
2843 if (
2844#ifdef FEAT_MBYTE
2845 enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) :
2846#endif
2847 *s == 0xa0)
2848 {
2849 s +=
2850#ifdef FEAT_MBYTE
2851 enc_utf8 ? 2 :
2852#endif
2853 1;
2854 STRCPY(d, "<a0>");
2855 d += 4;
2856 }
2857 else
2858 MB_COPY_CHAR(s, d);
2859 }
2860 *d = NUL;
2861}
2862
2863/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002864 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002865 * Start of the name can be found at eap->cmd.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002866 * Returns pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002867 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002868 * Returns NULL for an ambiguous user command.
2869 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002870 static char_u *
2871find_command(eap, full)
2872 exarg_T *eap;
Bram Moolenaar78a15312009-05-15 19:33:18 +00002873 int *full UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002874{
2875 int len;
2876 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00002877 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002878
2879 /*
2880 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00002881 * Exceptions:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002882 * - the 'k' command can directly be followed by any character.
2883 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
2884 * but :sre[wind] is another command, as are :scrip[tnames],
2885 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00002886 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002887 */
2888 p = eap->cmd;
2889 if (*p == 'k')
2890 {
2891 eap->cmdidx = CMD_k;
2892 ++p;
2893 }
2894 else if (p[0] == 's'
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002895 && ((p[1] == 'c' && p[2] != 's' && p[2] != 'r'
2896 && p[3] != 'i' && p[4] != 'p')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002897 || p[1] == 'g'
2898 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
2899 || p[1] == 'I'
2900 || (p[1] == 'r' && p[2] != 'e')))
2901 {
2902 eap->cmdidx = CMD_substitute;
2903 ++p;
2904 }
2905 else
2906 {
2907 while (ASCII_ISALPHA(*p))
2908 ++p;
Bram Moolenaarb6590522010-07-21 16:00:43 +02002909 /* for python 3.x support ":py3", ":python3", ":py3file", etc. */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002910 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaarb6590522010-07-21 16:00:43 +02002911 while (ASCII_ISALNUM(*p))
2912 ++p;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02002913
Bram Moolenaar071d4272004-06-13 20:20:40 +00002914 /* check for non-alpha command */
2915 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
2916 ++p;
2917 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00002918 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
2919 {
2920 /* Check for ":dl", ":dell", etc. to ":deletel": that's
2921 * :delete with the 'l' flag. Same for 'p'. */
2922 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00002923 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00002924 break;
2925 if (i == len - 1)
2926 {
2927 --len;
2928 if (p[-1] == 'l')
2929 eap->flags |= EXFLAG_LIST;
2930 else
2931 eap->flags |= EXFLAG_PRINT;
2932 }
2933 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002934
2935 if (ASCII_ISLOWER(*eap->cmd))
2936 eap->cmdidx = cmdidxs[CharOrdLow(*eap->cmd)];
2937 else
2938 eap->cmdidx = cmdidxs[26];
2939
2940 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
2941 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
2942 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
2943 (size_t)len) == 0)
2944 {
2945#ifdef FEAT_EVAL
2946 if (full != NULL
2947 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
2948 *full = TRUE;
2949#endif
2950 break;
2951 }
2952
2953#ifdef FEAT_USR_CMDS
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01002954 /* Look for a user defined command as a last resort. Let ":Print" be
2955 * overruled by a user defined command. */
2956 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
2957 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002958 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002959 /* User defined commands may contain digits. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002960 while (ASCII_ISALNUM(*p))
2961 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002962 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002963 }
2964#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002965 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002966 eap->cmdidx = CMD_SIZE;
2967 }
2968
2969 return p;
2970}
2971
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002972#ifdef FEAT_USR_CMDS
2973/*
2974 * Search for a user command that matches "eap->cmd".
2975 * Return cmdidx in "eap->cmdidx", flags in "eap->argt", idx in "eap->useridx".
2976 * Return a pointer to just after the command.
2977 * Return NULL if there is no matching command.
2978 */
2979 static char_u *
2980find_ucmd(eap, p, full, xp, compl)
2981 exarg_T *eap;
2982 char_u *p; /* end of the command (possibly including count) */
2983 int *full; /* set to TRUE for a full match */
2984 expand_T *xp; /* used for completion, NULL otherwise */
2985 int *compl; /* completion flags or NULL */
2986{
2987 int len = (int)(p - eap->cmd);
2988 int j, k, matchlen = 0;
2989 ucmd_T *uc;
2990 int found = FALSE;
2991 int possible = FALSE;
2992 char_u *cp, *np; /* Point into typed cmd and test name */
2993 garray_T *gap;
2994 int amb_local = FALSE; /* Found ambiguous buffer-local command,
2995 only full match global is accepted. */
2996
2997 /*
2998 * Look for buffer-local user commands first, then global ones.
2999 */
3000 gap = &curbuf->b_ucmds;
3001 for (;;)
3002 {
3003 for (j = 0; j < gap->ga_len; ++j)
3004 {
3005 uc = USER_CMD_GA(gap, j);
3006 cp = eap->cmd;
3007 np = uc->uc_name;
3008 k = 0;
3009 while (k < len && *np != NUL && *cp++ == *np++)
3010 k++;
3011 if (k == len || (*np == NUL && vim_isdigit(eap->cmd[k])))
3012 {
3013 /* If finding a second match, the command is ambiguous. But
3014 * not if a buffer-local command wasn't a full match and a
3015 * global command is a full match. */
3016 if (k == len && found && *np != NUL)
3017 {
3018 if (gap == &ucmds)
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003019 return NULL;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003020 amb_local = TRUE;
3021 }
3022
3023 if (!found || (k == len && *np == NUL))
3024 {
3025 /* If we matched up to a digit, then there could
3026 * be another command including the digit that we
3027 * should use instead.
3028 */
3029 if (k == len)
3030 found = TRUE;
3031 else
3032 possible = TRUE;
3033
3034 if (gap == &ucmds)
3035 eap->cmdidx = CMD_USER;
3036 else
3037 eap->cmdidx = CMD_USER_BUF;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003038 eap->argt = (long)uc->uc_argt;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003039 eap->useridx = j;
3040
3041# ifdef FEAT_CMDL_COMPL
3042 if (compl != NULL)
3043 *compl = uc->uc_compl;
3044# ifdef FEAT_EVAL
3045 if (xp != NULL)
3046 {
3047 xp->xp_arg = uc->uc_compl_arg;
3048 xp->xp_scriptID = uc->uc_scriptID;
3049 }
3050# endif
3051# endif
3052 /* Do not search for further abbreviations
3053 * if this is an exact match. */
3054 matchlen = k;
3055 if (k == len && *np == NUL)
3056 {
3057 if (full != NULL)
3058 *full = TRUE;
3059 amb_local = FALSE;
3060 break;
3061 }
3062 }
3063 }
3064 }
3065
3066 /* Stop if we found a full match or searched all. */
3067 if (j < gap->ga_len || gap == &ucmds)
3068 break;
3069 gap = &ucmds;
3070 }
3071
3072 /* Only found ambiguous matches. */
3073 if (amb_local)
3074 {
3075 if (xp != NULL)
3076 xp->xp_context = EXPAND_UNSUCCESSFUL;
3077 return NULL;
3078 }
3079
3080 /* The match we found may be followed immediately by a number. Move "p"
3081 * back to point to it. */
3082 if (found || possible)
3083 return p + (matchlen - len);
3084 return p;
3085}
3086#endif
3087
Bram Moolenaar071d4272004-06-13 20:20:40 +00003088#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003089static struct cmdmod
3090{
3091 char *name;
3092 int minlen;
3093 int has_count; /* :123verbose :3tab */
3094} cmdmods[] = {
3095 {"aboveleft", 3, FALSE},
3096 {"belowright", 3, FALSE},
3097 {"botright", 2, FALSE},
3098 {"browse", 3, FALSE},
3099 {"confirm", 4, FALSE},
3100 {"hide", 3, FALSE},
3101 {"keepalt", 5, FALSE},
3102 {"keepjumps", 5, FALSE},
3103 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01003104 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003105 {"leftabove", 5, FALSE},
3106 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003107 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003108 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003109 {"rightbelow", 6, FALSE},
3110 {"sandbox", 3, FALSE},
3111 {"silent", 3, FALSE},
3112 {"tab", 3, TRUE},
3113 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003114 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003115 {"verbose", 4, TRUE},
3116 {"vertical", 4, FALSE},
3117};
3118
3119/*
3120 * Return length of a command modifier (including optional count).
3121 * Return zero when it's not a modifier.
3122 */
3123 int
3124modifier_len(cmd)
3125 char_u *cmd;
3126{
3127 int i, j;
3128 char_u *p = cmd;
3129
3130 if (VIM_ISDIGIT(*cmd))
3131 p = skipwhite(skipdigits(cmd));
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003132 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003133 {
3134 for (j = 0; p[j] != NUL; ++j)
3135 if (p[j] != cmdmods[i].name[j])
3136 break;
Bram Moolenaar2d473ab2013-06-12 17:12:24 +02003137 if (!ASCII_ISALPHA(p[j]) && j >= cmdmods[i].minlen
Bram Moolenaared53fb92007-11-24 20:50:24 +00003138 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003139 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003140 }
3141 return 0;
3142}
3143
Bram Moolenaar071d4272004-06-13 20:20:40 +00003144/*
3145 * Return > 0 if an Ex command "name" exists.
3146 * Return 2 if there is an exact match.
3147 * Return 3 if there is an ambiguous match.
3148 */
3149 int
3150cmd_exists(name)
3151 char_u *name;
3152{
3153 exarg_T ea;
3154 int full = FALSE;
3155 int i;
3156 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003157 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003158
3159 /* Check command modifiers. */
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003160 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161 {
3162 for (j = 0; name[j] != NUL; ++j)
3163 if (name[j] != cmdmods[i].name[j])
3164 break;
3165 if (name[j] == NUL && j >= cmdmods[i].minlen)
3166 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3167 }
3168
Bram Moolenaara9587612006-05-04 21:47:50 +00003169 /* Check built-in commands and user defined commands.
3170 * For ":2match" and ":3match" we need to skip the number. */
3171 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003172 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003173 p = find_command(&ea, &full);
3174 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003175 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003176 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3177 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003178 if (*skipwhite(p) != NUL)
3179 return 0; /* trailing garbage */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003180 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3181}
3182#endif
3183
3184/*
3185 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
3186 * we don't need/want deleted. Maybe this could be done better if we didn't
3187 * repeat all this stuff. The only problem is that they may not stay
3188 * perfectly compatible with each other, but then the command line syntax
3189 * probably won't change that much -- webb.
3190 */
3191 char_u *
3192set_one_cmd_context(xp, buff)
3193 expand_T *xp;
3194 char_u *buff; /* buffer for command string */
3195{
3196 char_u *p;
3197 char_u *cmd, *arg;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003198 int len = 0;
3199 exarg_T ea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3201 int compl = EXPAND_NOTHING;
3202#endif
3203#ifdef FEAT_CMDL_COMPL
3204 int delim;
3205#endif
3206 int forceit = FALSE;
3207 int usefilter = FALSE; /* filter instead of file name */
3208
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003209 ExpandInit(xp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003210 xp->xp_pattern = buff;
3211 xp->xp_context = EXPAND_COMMANDS; /* Default until we get past command */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003212 ea.argt = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003213
3214/*
3215 * 2. skip comment lines and leading space, colons or bars
3216 */
3217 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
3218 ;
3219 xp->xp_pattern = cmd;
3220
3221 if (*cmd == NUL)
3222 return NULL;
3223 if (*cmd == '"') /* ignore comment lines */
3224 {
3225 xp->xp_context = EXPAND_NOTHING;
3226 return NULL;
3227 }
3228
3229/*
3230 * 3. parse a range specifier of the form: addr [,addr] [;addr] ..
3231 */
3232 cmd = skip_range(cmd, &xp->xp_context);
3233
3234/*
3235 * 4. parse command
3236 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003237 xp->xp_pattern = cmd;
3238 if (*cmd == NUL)
3239 return NULL;
3240 if (*cmd == '"')
3241 {
3242 xp->xp_context = EXPAND_NOTHING;
3243 return NULL;
3244 }
3245
3246 if (*cmd == '|' || *cmd == '\n')
3247 return cmd + 1; /* There's another command */
3248
3249 /*
3250 * Isolate the command and search for it in the command table.
3251 * Exceptions:
3252 * - the 'k' command can directly be followed by any character, but
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003253 * do accept "keepmarks", "keepalt" and "keepjumps".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003254 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
3255 */
3256 if (*cmd == 'k' && cmd[1] != 'e')
3257 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003258 ea.cmdidx = CMD_k;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003259 p = cmd + 1;
3260 }
3261 else
3262 {
3263 p = cmd;
3264 while (ASCII_ISALPHA(*p) || *p == '*') /* Allow * wild card */
3265 ++p;
3266 /* check for non-alpha command */
3267 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3268 ++p;
Bram Moolenaar9f5d6002013-06-02 19:22:13 +02003269 /* for python 3.x: ":py3*" commands completion */
3270 if (cmd[0] == 'p' && cmd[1] == 'y' && p == cmd + 2 && *p == '3')
Bram Moolenaar893b2d72013-12-11 17:44:38 +01003271 {
Bram Moolenaar9f5d6002013-06-02 19:22:13 +02003272 ++p;
Bram Moolenaar893b2d72013-12-11 17:44:38 +01003273 while (ASCII_ISALPHA(*p) || *p == '*')
3274 ++p;
3275 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003276 len = (int)(p - cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003278 if (len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279 {
3280 xp->xp_context = EXPAND_UNSUCCESSFUL;
3281 return NULL;
3282 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003283 for (ea.cmdidx = (cmdidx_T)0; (int)ea.cmdidx < (int)CMD_SIZE;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01003284 ea.cmdidx = (cmdidx_T)((int)ea.cmdidx + 1))
3285 if (STRNCMP(cmdnames[(int)ea.cmdidx].cmd_name, cmd,
3286 (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003287 break;
3288
3289#ifdef FEAT_USR_CMDS
3290 if (cmd[0] >= 'A' && cmd[0] <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003291 while (ASCII_ISALNUM(*p) || *p == '*') /* Allow * wild card */
3292 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293#endif
3294 }
3295
3296 /*
3297 * If the cursor is touching the command, and it ends in an alpha-numeric
3298 * character, complete the command name.
3299 */
3300 if (*p == NUL && ASCII_ISALNUM(p[-1]))
3301 return NULL;
3302
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003303 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304 {
3305 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
3306 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003307 ea.cmdidx = CMD_substitute;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003308 p = cmd + 1;
3309 }
3310#ifdef FEAT_USR_CMDS
3311 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
3312 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003313 ea.cmd = cmd;
3314 p = find_ucmd(&ea, p, NULL, xp,
3315# if defined(FEAT_CMDL_COMPL)
3316 &compl
3317# else
3318 NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003319# endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003320 );
Bram Moolenaarebefac62005-12-28 22:39:57 +00003321 if (p == NULL)
3322 ea.cmdidx = CMD_SIZE; /* ambiguous user command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323 }
3324#endif
3325 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003326 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327 {
3328 /* Not still touching the command and it was an illegal one */
3329 xp->xp_context = EXPAND_UNSUCCESSFUL;
3330 return NULL;
3331 }
3332
3333 xp->xp_context = EXPAND_NOTHING; /* Default now that we're past command */
3334
3335 if (*p == '!') /* forced commands */
3336 {
3337 forceit = TRUE;
3338 ++p;
3339 }
3340
3341/*
3342 * 5. parse arguments
3343 */
3344#ifdef FEAT_USR_CMDS
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003345 if (!USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346#endif
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003347 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348
3349 arg = skipwhite(p);
3350
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003351 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352 {
3353 if (*arg == '>') /* append */
3354 {
3355 if (*++arg == '>')
3356 ++arg;
3357 arg = skipwhite(arg);
3358 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003359 else if (*arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360 {
3361 ++arg;
3362 usefilter = TRUE;
3363 }
3364 }
3365
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003366 if (ea.cmdidx == CMD_read)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367 {
3368 usefilter = forceit; /* :r! filter if forced */
3369 if (*arg == '!') /* :r !filter */
3370 {
3371 ++arg;
3372 usefilter = TRUE;
3373 }
3374 }
3375
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003376 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377 {
3378 while (*arg == *cmd) /* allow any number of '>' or '<' */
3379 ++arg;
3380 arg = skipwhite(arg);
3381 }
3382
3383 /* Does command allow "+command"? */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003384 if ((ea.argt & EDITCMD) && !usefilter && *arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385 {
3386 /* Check if we're in the +command */
3387 p = arg + 1;
3388 arg = skip_cmd_arg(arg, FALSE);
3389
3390 /* Still touching the command after '+'? */
3391 if (*arg == NUL)
3392 return p;
3393
3394 /* Skip space(s) after +command to get to the real argument */
3395 arg = skipwhite(arg);
3396 }
3397
3398 /*
3399 * Check for '|' to separate commands and '"' to start comments.
3400 * Don't do this for ":read !cmd" and ":write !cmd".
3401 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003402 if ((ea.argt & TRLBAR) && !usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003403 {
3404 p = arg;
3405 /* ":redir @" is not the start of a comment */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003406 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407 p += 2;
3408 while (*p)
3409 {
3410 if (*p == Ctrl_V)
3411 {
3412 if (p[1] != NUL)
3413 ++p;
3414 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003415 else if ( (*p == '"' && !(ea.argt & NOTRLCOM))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416 || *p == '|' || *p == '\n')
3417 {
3418 if (*(p - 1) != '\\')
3419 {
3420 if (*p == '|' || *p == '\n')
3421 return p + 1;
3422 return NULL; /* It's a comment */
3423 }
3424 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003425 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426 }
3427 }
3428
3429 /* no arguments allowed */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003430 if (!(ea.argt & EXTRA) && *arg != NUL &&
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431 vim_strchr((char_u *)"|\"", *arg) == NULL)
3432 return NULL;
3433
3434 /* Find start of last argument (argument just before cursor): */
Bram Moolenaar848f8762012-07-25 17:22:23 +02003435 p = buff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436 xp->xp_pattern = p;
Bram Moolenaar09168a72012-08-02 21:24:42 +02003437 len = (int)STRLEN(buff);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003438 while (*p && p < buff + len)
3439 {
3440 if (*p == ' ' || *p == TAB)
3441 {
3442 /* argument starts after a space */
3443 xp->xp_pattern = ++p;
3444 }
3445 else
3446 {
3447 if (*p == '\\' && *(p + 1) != NUL)
3448 ++p; /* skip over escaped character */
3449 mb_ptr_adv(p);
3450 }
3451 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003453 if (ea.argt & XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003455 int c;
3456 int in_quote = FALSE;
3457 char_u *bow = NULL; /* Beginning of word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458
3459 /*
3460 * Allow spaces within back-quotes to count as part of the argument
3461 * being expanded.
3462 */
3463 xp->xp_pattern = skipwhite(arg);
Bram Moolenaar6529c102007-08-18 15:47:34 +00003464 p = xp->xp_pattern;
3465 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003467#ifdef FEAT_MBYTE
3468 if (has_mbyte)
3469 c = mb_ptr2char(p);
3470 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471#endif
Bram Moolenaar6529c102007-08-18 15:47:34 +00003472 c = *p;
3473 if (c == '\\' && p[1] != NUL)
3474 ++p;
3475 else if (c == '`')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476 {
3477 if (!in_quote)
3478 {
3479 xp->xp_pattern = p;
3480 bow = p + 1;
3481 }
3482 in_quote = !in_quote;
3483 }
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003484 /* An argument can contain just about everything, except
3485 * characters that end the command and white space. */
3486 else if (c == '|' || c == '\n' || c == '"' || (vim_iswhite(c)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003487#ifdef SPACE_IN_FILENAME
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003488 && (!(ea.argt & NOSPC) || usefilter)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003489#endif
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003490 ))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003491 {
Bram Moolenaarcf5a5b82008-02-26 20:30:12 +00003492 len = 0; /* avoid getting stuck when space is in 'isfname' */
Bram Moolenaar6529c102007-08-18 15:47:34 +00003493 while (*p != NUL)
3494 {
3495#ifdef FEAT_MBYTE
3496 if (has_mbyte)
3497 c = mb_ptr2char(p);
3498 else
3499#endif
3500 c = *p;
Bram Moolenaardd87969c2007-08-21 13:07:12 +00003501 if (c == '`' || vim_isfilec_or_wc(c))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003502 break;
3503#ifdef FEAT_MBYTE
3504 if (has_mbyte)
3505 len = (*mb_ptr2len)(p);
3506 else
3507#endif
3508 len = 1;
3509 mb_ptr_adv(p);
3510 }
3511 if (in_quote)
3512 bow = p;
3513 else
3514 xp->xp_pattern = p;
3515 p -= len;
3516 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003517 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518 }
3519
3520 /*
3521 * If we are still inside the quotes, and we passed a space, just
3522 * expand from there.
3523 */
3524 if (bow != NULL && in_quote)
3525 xp->xp_pattern = bow;
3526 xp->xp_context = EXPAND_FILES;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003527
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003528 /* For a shell command more chars need to be escaped. */
3529 if (usefilter || ea.cmdidx == CMD_bang)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003530 {
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003531#ifndef BACKSLASH_IN_FILENAME
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003532 xp->xp_shell = TRUE;
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003533#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003534 /* When still after the command name expand executables. */
3535 if (xp->xp_pattern == skipwhite(arg))
Bram Moolenaar5c5b0942007-05-06 12:07:59 +00003536 xp->xp_context = EXPAND_SHELLCMD;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003537 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538
3539 /* Check for environment variable */
3540 if (*xp->xp_pattern == '$'
3541#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
3542 || *xp->xp_pattern == '%'
3543#endif
3544 )
3545 {
3546 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
3547 if (!vim_isIDc(*p))
3548 break;
3549 if (*p == NUL)
3550 {
3551 xp->xp_context = EXPAND_ENV_VARS;
3552 ++xp->xp_pattern;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003553#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3554 /* Avoid that the assignment uses EXPAND_FILES again. */
Bram Moolenaara466c992005-07-09 21:03:22 +00003555 if (compl != EXPAND_USER_DEFINED && compl != EXPAND_USER_LIST)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003556 compl = EXPAND_ENV_VARS;
3557#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558 }
3559 }
Bram Moolenaar24305862012-08-15 14:05:05 +02003560#if defined(FEAT_CMDL_COMPL)
3561 /* Check for user names */
3562 if (*xp->xp_pattern == '~')
3563 {
3564 for (p = xp->xp_pattern + 1; *p != NUL && *p != '/'; ++p)
3565 ;
3566 /* Complete ~user only if it partially matches a user name.
3567 * A full match ~user<Tab> will be replaced by user's home
3568 * directory i.e. something like ~user<Tab> -> /home/user/ */
3569 if (*p == NUL && p > xp->xp_pattern + 1
3570 && match_user(xp->xp_pattern + 1) == 1)
3571 {
3572 xp->xp_context = EXPAND_USER;
3573 ++xp->xp_pattern;
3574 }
3575 }
3576#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003577 }
3578
3579/*
3580 * 6. switch on command name
3581 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003582 switch (ea.cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003584 case CMD_find:
3585 case CMD_sfind:
3586 case CMD_tabfind:
Bram Moolenaarc24b6972010-08-16 22:34:29 +02003587 if (xp->xp_context == EXPAND_FILES)
3588 xp->xp_context = EXPAND_FILES_IN_PATH;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003589 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003590 case CMD_cd:
3591 case CMD_chdir:
3592 case CMD_lcd:
3593 case CMD_lchdir:
3594 if (xp->xp_context == EXPAND_FILES)
3595 xp->xp_context = EXPAND_DIRECTORIES;
3596 break;
3597 case CMD_help:
3598 xp->xp_context = EXPAND_HELP;
3599 xp->xp_pattern = arg;
3600 break;
3601
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003602 /* Command modifiers: return the argument.
3603 * Also for commands with an argument that is a command. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604 case CMD_aboveleft:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003605 case CMD_argdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003606 case CMD_belowright:
3607 case CMD_botright:
3608 case CMD_browse:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003609 case CMD_bufdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610 case CMD_confirm:
Bram Moolenaardf177f62005-02-22 08:39:57 +00003611 case CMD_debug:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612 case CMD_folddoclosed:
3613 case CMD_folddoopen:
3614 case CMD_hide:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003615 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616 case CMD_keepjumps:
3617 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01003618 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619 case CMD_leftabove:
3620 case CMD_lockmarks:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003621 case CMD_noautocmd:
3622 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623 case CMD_rightbelow:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003624 case CMD_sandbox:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003625 case CMD_silent:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003626 case CMD_tab:
Bram Moolenaar70baa402013-06-16 16:14:03 +02003627 case CMD_tabdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628 case CMD_topleft:
3629 case CMD_verbose:
3630 case CMD_vertical:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003631 case CMD_windo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003632 return arg;
3633
Bram Moolenaar4f688582007-07-24 12:34:30 +00003634#ifdef FEAT_CMDL_COMPL
3635# ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636 case CMD_match:
3637 if (*arg == NUL || !ends_excmd(*arg))
3638 {
Bram Moolenaar4f688582007-07-24 12:34:30 +00003639 /* also complete "None" */
3640 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641 arg = skipwhite(skiptowhite(arg));
3642 if (*arg != NUL)
3643 {
3644 xp->xp_context = EXPAND_NOTHING;
3645 arg = skip_regexp(arg + 1, *arg, p_magic, NULL);
3646 }
3647 }
3648 return find_nextcmd(arg);
Bram Moolenaar4f688582007-07-24 12:34:30 +00003649# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651/*
3652 * All completion for the +cmdline_compl feature goes here.
3653 */
3654
3655# ifdef FEAT_USR_CMDS
3656 case CMD_command:
3657 /* Check for attributes */
3658 while (*arg == '-')
3659 {
3660 arg++; /* Skip "-" */
3661 p = skiptowhite(arg);
3662 if (*p == NUL)
3663 {
3664 /* Cursor is still in the attribute */
3665 p = vim_strchr(arg, '=');
3666 if (p == NULL)
3667 {
3668 /* No "=", so complete attribute names */
3669 xp->xp_context = EXPAND_USER_CMD_FLAGS;
3670 xp->xp_pattern = arg;
3671 return NULL;
3672 }
3673
3674 /* For the -complete and -nargs attributes, we complete
3675 * their arguments as well.
3676 */
3677 if (STRNICMP(arg, "complete", p - arg) == 0)
3678 {
3679 xp->xp_context = EXPAND_USER_COMPLETE;
3680 xp->xp_pattern = p + 1;
3681 return NULL;
3682 }
3683 else if (STRNICMP(arg, "nargs", p - arg) == 0)
3684 {
3685 xp->xp_context = EXPAND_USER_NARGS;
3686 xp->xp_pattern = p + 1;
3687 return NULL;
3688 }
3689 return NULL;
3690 }
3691 arg = skipwhite(p);
3692 }
3693
3694 /* After the attributes comes the new command name */
3695 p = skiptowhite(arg);
3696 if (*p == NUL)
3697 {
3698 xp->xp_context = EXPAND_USER_COMMANDS;
3699 xp->xp_pattern = arg;
3700 break;
3701 }
3702
3703 /* And finally comes a normal command */
3704 return skipwhite(p);
3705
3706 case CMD_delcommand:
3707 xp->xp_context = EXPAND_USER_COMMANDS;
3708 xp->xp_pattern = arg;
3709 break;
3710# endif
3711
3712 case CMD_global:
3713 case CMD_vglobal:
3714 delim = *arg; /* get the delimiter */
3715 if (delim)
3716 ++arg; /* skip delimiter if there is one */
3717
3718 while (arg[0] != NUL && arg[0] != delim)
3719 {
3720 if (arg[0] == '\\' && arg[1] != NUL)
3721 ++arg;
3722 ++arg;
3723 }
3724 if (arg[0] != NUL)
3725 return arg + 1;
3726 break;
3727 case CMD_and:
3728 case CMD_substitute:
3729 delim = *arg;
3730 if (delim)
3731 {
3732 /* skip "from" part */
3733 ++arg;
3734 arg = skip_regexp(arg, delim, p_magic, NULL);
3735 }
3736 /* skip "to" part */
3737 while (arg[0] != NUL && arg[0] != delim)
3738 {
3739 if (arg[0] == '\\' && arg[1] != NUL)
3740 ++arg;
3741 ++arg;
3742 }
3743 if (arg[0] != NUL) /* skip delimiter */
3744 ++arg;
3745 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
3746 ++arg;
3747 if (arg[0] != NUL)
3748 return arg;
3749 break;
3750 case CMD_isearch:
3751 case CMD_dsearch:
3752 case CMD_ilist:
3753 case CMD_dlist:
3754 case CMD_ijump:
3755 case CMD_psearch:
3756 case CMD_djump:
3757 case CMD_isplit:
3758 case CMD_dsplit:
3759 arg = skipwhite(skipdigits(arg)); /* skip count */
3760 if (*arg == '/') /* Match regexp, not just whole words */
3761 {
3762 for (++arg; *arg && *arg != '/'; arg++)
3763 if (*arg == '\\' && arg[1] != NUL)
3764 arg++;
3765 if (*arg)
3766 {
3767 arg = skipwhite(arg + 1);
3768
3769 /* Check for trailing illegal characters */
3770 if (*arg && vim_strchr((char_u *)"|\"\n", *arg) == NULL)
3771 xp->xp_context = EXPAND_NOTHING;
3772 else
3773 return arg;
3774 }
3775 }
3776 break;
3777#ifdef FEAT_AUTOCMD
3778 case CMD_autocmd:
3779 return set_context_in_autocmd(xp, arg, FALSE);
3780
3781 case CMD_doautocmd:
Bram Moolenaar73a9d7b2008-11-06 16:16:44 +00003782 case CMD_doautoall:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003783 return set_context_in_autocmd(xp, arg, TRUE);
3784#endif
3785 case CMD_set:
3786 set_context_in_set_cmd(xp, arg, 0);
3787 break;
3788 case CMD_setglobal:
3789 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
3790 break;
3791 case CMD_setlocal:
3792 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
3793 break;
3794 case CMD_tag:
3795 case CMD_stag:
3796 case CMD_ptag:
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00003797 case CMD_ltag:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003798 case CMD_tselect:
3799 case CMD_stselect:
3800 case CMD_ptselect:
3801 case CMD_tjump:
3802 case CMD_stjump:
3803 case CMD_ptjump:
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003804 if (*p_wop != NUL)
3805 xp->xp_context = EXPAND_TAGS_LISTFILES;
3806 else
3807 xp->xp_context = EXPAND_TAGS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003808 xp->xp_pattern = arg;
3809 break;
3810 case CMD_augroup:
3811 xp->xp_context = EXPAND_AUGROUP;
3812 xp->xp_pattern = arg;
3813 break;
3814#ifdef FEAT_SYN_HL
3815 case CMD_syntax:
3816 set_context_in_syntax_cmd(xp, arg);
3817 break;
3818#endif
3819#ifdef FEAT_EVAL
3820 case CMD_let:
3821 case CMD_if:
3822 case CMD_elseif:
3823 case CMD_while:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00003824 case CMD_for:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825 case CMD_echo:
3826 case CMD_echon:
3827 case CMD_execute:
3828 case CMD_echomsg:
3829 case CMD_echoerr:
3830 case CMD_call:
3831 case CMD_return:
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003832 set_context_for_expression(xp, arg, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833 break;
3834
3835 case CMD_unlet:
3836 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3837 arg = xp->xp_pattern + 1;
3838 xp->xp_context = EXPAND_USER_VARS;
3839 xp->xp_pattern = arg;
3840 break;
3841
3842 case CMD_function:
3843 case CMD_delfunction:
3844 xp->xp_context = EXPAND_USER_FUNC;
3845 xp->xp_pattern = arg;
3846 break;
3847
3848 case CMD_echohl:
Bram Moolenaar4f688582007-07-24 12:34:30 +00003849 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003850 break;
3851#endif
3852 case CMD_highlight:
3853 set_context_in_highlight_cmd(xp, arg);
3854 break;
Bram Moolenaarf4580d82009-03-18 11:52:53 +00003855#ifdef FEAT_CSCOPE
3856 case CMD_cscope:
Bram Moolenaar7bfef802009-04-22 14:25:01 +00003857 case CMD_lcscope:
3858 case CMD_scscope:
3859 set_context_in_cscope_cmd(xp, arg, ea.cmdidx);
Bram Moolenaarf4580d82009-03-18 11:52:53 +00003860 break;
3861#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00003862#ifdef FEAT_SIGNS
3863 case CMD_sign:
3864 set_context_in_sign_cmd(xp, arg);
3865 break;
3866#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867#ifdef FEAT_LISTCMDS
3868 case CMD_bdelete:
3869 case CMD_bwipeout:
3870 case CMD_bunload:
3871 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3872 arg = xp->xp_pattern + 1;
3873 /*FALLTHROUGH*/
3874 case CMD_buffer:
3875 case CMD_sbuffer:
3876 case CMD_checktime:
3877 xp->xp_context = EXPAND_BUFFERS;
3878 xp->xp_pattern = arg;
3879 break;
3880#endif
3881#ifdef FEAT_USR_CMDS
3882 case CMD_USER:
3883 case CMD_USER_BUF:
3884 if (compl != EXPAND_NOTHING)
3885 {
3886 /* XFILE: file names are handled above */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003887 if (!(ea.argt & XFILE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888 {
3889# ifdef FEAT_MENU
3890 if (compl == EXPAND_MENUS)
3891 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
3892# endif
3893 if (compl == EXPAND_COMMANDS)
3894 return arg;
3895 if (compl == EXPAND_MAPPINGS)
3896 return set_context_in_map_cmd(xp, (char_u *)"map",
3897 arg, forceit, FALSE, FALSE, CMD_map);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003898 /* Find start of last argument. */
3899 p = arg;
3900 while (*p)
3901 {
3902 if (*p == ' ')
Bram Moolenaar848f8762012-07-25 17:22:23 +02003903 /* argument starts after a space */
3904 arg = p + 1;
Bram Moolenaara07c8312012-07-27 21:12:07 +02003905 else if (*p == '\\' && *(p + 1) != NUL)
3906 ++p; /* skip over escaped character */
3907 mb_ptr_adv(p);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003908 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003909 xp->xp_pattern = arg;
3910 }
3911 xp->xp_context = compl;
3912 }
3913 break;
3914#endif
3915 case CMD_map: case CMD_noremap:
3916 case CMD_nmap: case CMD_nnoremap:
3917 case CMD_vmap: case CMD_vnoremap:
3918 case CMD_omap: case CMD_onoremap:
3919 case CMD_imap: case CMD_inoremap:
3920 case CMD_cmap: case CMD_cnoremap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003921 case CMD_lmap: case CMD_lnoremap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02003922 case CMD_smap: case CMD_snoremap:
3923 case CMD_xmap: case CMD_xnoremap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003924 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003925 FALSE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926 case CMD_unmap:
3927 case CMD_nunmap:
3928 case CMD_vunmap:
3929 case CMD_ounmap:
3930 case CMD_iunmap:
3931 case CMD_cunmap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003932 case CMD_lunmap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02003933 case CMD_sunmap:
3934 case CMD_xunmap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003936 FALSE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003937 case CMD_abbreviate: case CMD_noreabbrev:
3938 case CMD_cabbrev: case CMD_cnoreabbrev:
3939 case CMD_iabbrev: case CMD_inoreabbrev:
3940 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003941 TRUE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942 case CMD_unabbreviate:
3943 case CMD_cunabbrev:
3944 case CMD_iunabbrev:
3945 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003946 TRUE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003947#ifdef FEAT_MENU
3948 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
3949 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
3950 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
3951 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
3952 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
3953 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
3954 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
3955 case CMD_tmenu: case CMD_tunmenu:
3956 case CMD_popup: case CMD_tearoff: case CMD_emenu:
3957 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
3958#endif
3959
3960 case CMD_colorscheme:
3961 xp->xp_context = EXPAND_COLORS;
3962 xp->xp_pattern = arg;
3963 break;
3964
3965 case CMD_compiler:
3966 xp->xp_context = EXPAND_COMPILER;
3967 xp->xp_pattern = arg;
3968 break;
3969
Bram Moolenaar883f5d02010-06-21 06:24:34 +02003970 case CMD_ownsyntax:
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02003971 xp->xp_context = EXPAND_OWNSYNTAX;
3972 xp->xp_pattern = arg;
3973 break;
3974
3975 case CMD_setfiletype:
Bram Moolenaar883f5d02010-06-21 06:24:34 +02003976 xp->xp_context = EXPAND_FILETYPE;
3977 xp->xp_pattern = arg;
3978 break;
3979
Bram Moolenaar071d4272004-06-13 20:20:40 +00003980#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
3981 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
3982 case CMD_language:
Bram Moolenaara660dc82011-05-25 12:51:22 +02003983 p = skiptowhite(arg);
3984 if (*p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003985 {
3986 xp->xp_context = EXPAND_LANGUAGE;
3987 xp->xp_pattern = arg;
3988 }
3989 else
Bram Moolenaara660dc82011-05-25 12:51:22 +02003990 {
3991 if ( STRNCMP(arg, "messages", p - arg) == 0
3992 || STRNCMP(arg, "ctype", p - arg) == 0
3993 || STRNCMP(arg, "time", p - arg) == 0)
3994 {
3995 xp->xp_context = EXPAND_LOCALES;
3996 xp->xp_pattern = skipwhite(p);
3997 }
3998 else
3999 xp->xp_context = EXPAND_NOTHING;
4000 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004001 break;
4002#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01004003#if defined(FEAT_PROFILE)
4004 case CMD_profile:
4005 set_context_in_profile_cmd(xp, arg);
4006 break;
4007#endif
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01004008 case CMD_behave:
4009 xp->xp_context = EXPAND_BEHAVE;
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004010 xp->xp_pattern = arg;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01004011 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004012
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004013#if defined(FEAT_CMDHIST)
4014 case CMD_history:
4015 xp->xp_context = EXPAND_HISTORY;
4016 xp->xp_pattern = arg;
4017 break;
4018#endif
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02004019#if defined(FEAT_PROFILE)
4020 case CMD_syntime:
4021 xp->xp_context = EXPAND_SYNTIME;
4022 xp->xp_pattern = arg;
4023 break;
4024#endif
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004025
Bram Moolenaar071d4272004-06-13 20:20:40 +00004026#endif /* FEAT_CMDL_COMPL */
4027
4028 default:
4029 break;
4030 }
4031 return NULL;
4032}
4033
4034/*
4035 * skip a range specifier of the form: addr [,addr] [;addr] ..
4036 *
4037 * Backslashed delimiters after / or ? will be skipped, and commands will
4038 * not be expanded between /'s and ?'s or after "'".
4039 *
Bram Moolenaardf177f62005-02-22 08:39:57 +00004040 * Also skip white space and ":" characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004041 * Returns the "cmd" pointer advanced to beyond the range.
4042 */
4043 char_u *
4044skip_range(cmd, ctx)
4045 char_u *cmd;
4046 int *ctx; /* pointer to xp_context or NULL */
4047{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004048 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049
Bram Moolenaardf177f62005-02-22 08:39:57 +00004050 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004051 {
4052 if (*cmd == '\'')
4053 {
4054 if (*++cmd == NUL && ctx != NULL)
4055 *ctx = EXPAND_NOTHING;
4056 }
4057 else if (*cmd == '/' || *cmd == '?')
4058 {
4059 delim = *cmd++;
4060 while (*cmd != NUL && *cmd != delim)
4061 if (*cmd++ == '\\' && *cmd != NUL)
4062 ++cmd;
4063 if (*cmd == NUL && ctx != NULL)
4064 *ctx = EXPAND_NOTHING;
4065 }
4066 if (*cmd != NUL)
4067 ++cmd;
4068 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004069
4070 /* Skip ":" and white space. */
4071 while (*cmd == ':')
4072 cmd = skipwhite(cmd + 1);
4073
Bram Moolenaar071d4272004-06-13 20:20:40 +00004074 return cmd;
4075}
4076
4077/*
4078 * get a single EX address
4079 *
4080 * Set ptr to the next character after the part that was interpreted.
4081 * Set ptr to NULL when an error is encountered.
4082 *
4083 * Return MAXLNUM when no Ex address was found.
4084 */
4085 static linenr_T
4086get_address(ptr, skip, to_other_file)
4087 char_u **ptr;
4088 int skip; /* only skip the address, don't use it */
4089 int to_other_file; /* flag: may jump to other file */
4090{
4091 int c;
4092 int i;
4093 long n;
4094 char_u *cmd;
4095 pos_T pos;
4096 pos_T *fp;
4097 linenr_T lnum;
4098
4099 cmd = skipwhite(*ptr);
4100 lnum = MAXLNUM;
4101 do
4102 {
4103 switch (*cmd)
4104 {
4105 case '.': /* '.' - Cursor position */
4106 ++cmd;
4107 lnum = curwin->w_cursor.lnum;
4108 break;
4109
4110 case '$': /* '$' - last line */
4111 ++cmd;
4112 lnum = curbuf->b_ml.ml_line_count;
4113 break;
4114
4115 case '\'': /* ''' - mark */
4116 if (*++cmd == NUL)
4117 {
4118 cmd = NULL;
4119 goto error;
4120 }
4121 if (skip)
4122 ++cmd;
4123 else
4124 {
4125 /* Only accept a mark in another file when it is
4126 * used by itself: ":'M". */
4127 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
4128 ++cmd;
4129 if (fp == (pos_T *)-1)
4130 /* Jumped to another file. */
4131 lnum = curwin->w_cursor.lnum;
4132 else
4133 {
4134 if (check_mark(fp) == FAIL)
4135 {
4136 cmd = NULL;
4137 goto error;
4138 }
4139 lnum = fp->lnum;
4140 }
4141 }
4142 break;
4143
4144 case '/':
4145 case '?': /* '/' or '?' - search */
4146 c = *cmd++;
4147 if (skip) /* skip "/pat/" */
4148 {
4149 cmd = skip_regexp(cmd, c, (int)p_magic, NULL);
4150 if (*cmd == c)
4151 ++cmd;
4152 }
4153 else
4154 {
4155 pos = curwin->w_cursor; /* save curwin->w_cursor */
4156 /*
4157 * When '/' or '?' follows another address, start
4158 * from there.
4159 */
4160 if (lnum != MAXLNUM)
4161 curwin->w_cursor.lnum = lnum;
4162 /*
4163 * Start a forward search at the end of the line.
4164 * Start a backward search at the start of the line.
4165 * This makes sure we never match in the current
4166 * line, and can match anywhere in the
4167 * next/previous line.
4168 */
4169 if (c == '/')
4170 curwin->w_cursor.col = MAXCOL;
4171 else
4172 curwin->w_cursor.col = 0;
4173 searchcmdlen = 0;
4174 if (!do_search(NULL, c, cmd, 1L,
Bram Moolenaaraad86642008-03-10 20:34:59 +00004175 SEARCH_HIS | SEARCH_MSG, NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176 {
4177 curwin->w_cursor = pos;
4178 cmd = NULL;
4179 goto error;
4180 }
4181 lnum = curwin->w_cursor.lnum;
4182 curwin->w_cursor = pos;
4183 /* adjust command string pointer */
4184 cmd += searchcmdlen;
4185 }
4186 break;
4187
4188 case '\\': /* "\?", "\/" or "\&", repeat search */
4189 ++cmd;
4190 if (*cmd == '&')
4191 i = RE_SUBST;
4192 else if (*cmd == '?' || *cmd == '/')
4193 i = RE_SEARCH;
4194 else
4195 {
4196 EMSG(_(e_backslash));
4197 cmd = NULL;
4198 goto error;
4199 }
4200
4201 if (!skip)
4202 {
4203 /*
4204 * When search follows another address, start from
4205 * there.
4206 */
4207 if (lnum != MAXLNUM)
4208 pos.lnum = lnum;
4209 else
4210 pos.lnum = curwin->w_cursor.lnum;
4211
4212 /*
4213 * Start the search just like for the above
4214 * do_search().
4215 */
4216 if (*cmd != '?')
4217 pos.col = MAXCOL;
4218 else
4219 pos.col = 0;
4220 if (searchit(curwin, curbuf, &pos,
4221 *cmd == '?' ? BACKWARD : FORWARD,
Bram Moolenaaraad86642008-03-10 20:34:59 +00004222 (char_u *)"", 1L, SEARCH_MSG,
Bram Moolenaar76929292008-01-06 19:07:36 +00004223 i, (linenr_T)0, NULL) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224 lnum = pos.lnum;
4225 else
4226 {
4227 cmd = NULL;
4228 goto error;
4229 }
4230 }
4231 ++cmd;
4232 break;
4233
4234 default:
4235 if (VIM_ISDIGIT(*cmd)) /* absolute line number */
4236 lnum = getdigits(&cmd);
4237 }
4238
4239 for (;;)
4240 {
4241 cmd = skipwhite(cmd);
4242 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4243 break;
4244
4245 if (lnum == MAXLNUM)
4246 lnum = curwin->w_cursor.lnum; /* "+1" is same as ".+1" */
4247 if (VIM_ISDIGIT(*cmd))
4248 i = '+'; /* "number" is same as "+number" */
4249 else
4250 i = *cmd++;
4251 if (!VIM_ISDIGIT(*cmd)) /* '+' is '+1', but '+0' is not '+1' */
4252 n = 1;
4253 else
4254 n = getdigits(&cmd);
4255 if (i == '-')
4256 lnum -= n;
4257 else
4258 lnum += n;
4259 }
4260 } while (*cmd == '/' || *cmd == '?');
4261
4262error:
4263 *ptr = cmd;
4264 return lnum;
4265}
4266
4267/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004268 * Get flags from an Ex command argument.
4269 */
4270 static void
4271get_flags(eap)
4272 exarg_T *eap;
4273{
4274 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4275 {
4276 if (*eap->arg == 'l')
4277 eap->flags |= EXFLAG_LIST;
4278 else if (*eap->arg == 'p')
4279 eap->flags |= EXFLAG_PRINT;
4280 else
4281 eap->flags |= EXFLAG_NR;
4282 eap->arg = skipwhite(eap->arg + 1);
4283 }
4284}
4285
4286/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004287 * Function called for command which is Not Implemented. NI!
4288 */
4289 void
4290ex_ni(eap)
4291 exarg_T *eap;
4292{
4293 if (!eap->skip)
4294 eap->errmsg = (char_u *)N_("E319: Sorry, the command is not available in this version");
4295}
4296
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004297#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004298/*
4299 * Function called for script command which is Not Implemented. NI!
4300 * Skips over ":perl <<EOF" constructs.
4301 */
4302 static void
4303ex_script_ni(eap)
4304 exarg_T *eap;
4305{
4306 if (!eap->skip)
4307 ex_ni(eap);
4308 else
4309 vim_free(script_get(eap, eap->arg));
4310}
4311#endif
4312
4313/*
4314 * Check range in Ex command for validity.
4315 * Return NULL when valid, error message when invalid.
4316 */
4317 static char_u *
4318invalid_range(eap)
4319 exarg_T *eap;
4320{
4321 if ( eap->line1 < 0
4322 || eap->line2 < 0
4323 || eap->line1 > eap->line2
4324 || ((eap->argt & RANGE)
4325 && !(eap->argt & NOTADR)
4326 && eap->line2 > curbuf->b_ml.ml_line_count
4327#ifdef FEAT_DIFF
4328 + (eap->cmdidx == CMD_diffget)
4329#endif
4330 ))
4331 return (char_u *)_(e_invrange);
4332 return NULL;
4333}
4334
4335/*
4336 * Correct the range for zero line number, if required.
4337 */
4338 static void
4339correct_range(eap)
4340 exarg_T *eap;
4341{
4342 if (!(eap->argt & ZEROR)) /* zero in range not allowed */
4343 {
4344 if (eap->line1 == 0)
4345 eap->line1 = 1;
4346 if (eap->line2 == 0)
4347 eap->line2 = 1;
4348 }
4349}
4350
Bram Moolenaar748bf032005-02-02 23:04:36 +00004351#ifdef FEAT_QUICKFIX
4352static char_u *skip_grep_pat __ARGS((exarg_T *eap));
4353
4354/*
4355 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4356 * pattern. Otherwise return eap->arg.
4357 */
4358 static char_u *
4359skip_grep_pat(eap)
4360 exarg_T *eap;
4361{
4362 char_u *p = eap->arg;
4363
Bram Moolenaara37420f2006-02-04 22:37:47 +00004364 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4365 || eap->cmdidx == CMD_vimgrepadd
4366 || eap->cmdidx == CMD_lvimgrepadd
4367 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004368 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004369 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004370 if (p == NULL)
4371 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004372 }
4373 return p;
4374}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004375
4376/*
4377 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4378 * in the command line, so that things like % get expanded.
4379 */
4380 static char_u *
4381replace_makeprg(eap, p, cmdlinep)
4382 exarg_T *eap;
4383 char_u *p;
4384 char_u **cmdlinep;
4385{
4386 char_u *new_cmdline;
4387 char_u *program;
4388 char_u *pos;
4389 char_u *ptr;
4390 int len;
4391 int i;
4392
4393 /*
4394 * Don't do it when ":vimgrep" is used for ":grep".
4395 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004396 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4397 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4398 || eap->cmdidx == CMD_grepadd
4399 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004400 && !grep_internal(eap->cmdidx))
4401 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004402 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4403 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004404 {
4405 if (*curbuf->b_p_gp == NUL)
4406 program = p_gp;
4407 else
4408 program = curbuf->b_p_gp;
4409 }
4410 else
4411 {
4412 if (*curbuf->b_p_mp == NUL)
4413 program = p_mp;
4414 else
4415 program = curbuf->b_p_mp;
4416 }
4417
4418 p = skipwhite(p);
4419
4420 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4421 {
4422 /* replace $* by given arguments */
4423 i = 1;
4424 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4425 ++i;
4426 len = (int)STRLEN(p);
4427 new_cmdline = alloc((int)(STRLEN(program) + i * (len - 2) + 1));
4428 if (new_cmdline == NULL)
4429 return NULL; /* out of memory */
4430 ptr = new_cmdline;
4431 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4432 {
4433 i = (int)(pos - program);
4434 STRNCPY(ptr, program, i);
4435 STRCPY(ptr += i, p);
4436 ptr += len;
4437 program = pos + 2;
4438 }
4439 STRCPY(ptr, program);
4440 }
4441 else
4442 {
4443 new_cmdline = alloc((int)(STRLEN(program) + STRLEN(p) + 2));
4444 if (new_cmdline == NULL)
4445 return NULL; /* out of memory */
4446 STRCPY(new_cmdline, program);
4447 STRCAT(new_cmdline, " ");
4448 STRCAT(new_cmdline, p);
4449 }
4450 msg_make(p);
4451
4452 /* 'eap->cmd' is not set here, because it is not used at CMD_make */
4453 vim_free(*cmdlinep);
4454 *cmdlinep = new_cmdline;
4455 p = new_cmdline;
4456 }
4457 return p;
4458}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004459#endif
4460
Bram Moolenaar071d4272004-06-13 20:20:40 +00004461/*
4462 * Expand file name in Ex command argument.
4463 * Return FAIL for failure, OK otherwise.
4464 */
4465 int
4466expand_filename(eap, cmdlinep, errormsgp)
4467 exarg_T *eap;
4468 char_u **cmdlinep;
4469 char_u **errormsgp;
4470{
4471 int has_wildcards; /* need to expand wildcards */
4472 char_u *repl;
4473 int srclen;
4474 char_u *p;
4475 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004476 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004477
Bram Moolenaar748bf032005-02-02 23:04:36 +00004478#ifdef FEAT_QUICKFIX
4479 /* Skip a regexp pattern for ":vimgrep[add] pat file..." */
4480 p = skip_grep_pat(eap);
4481#else
4482 p = eap->arg;
4483#endif
4484
Bram Moolenaar071d4272004-06-13 20:20:40 +00004485 /*
4486 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4487 * the file name contains a wildcard it should not cause expanding.
4488 * (it will be expanded anyway if there is a wildcard before replacing).
4489 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004490 has_wildcards = mch_has_wildcard(p);
4491 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004492 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004493#ifdef FEAT_EVAL
4494 /* Skip over `=expr`, wildcards in it are not expanded. */
4495 if (p[0] == '`' && p[1] == '=')
4496 {
4497 p += 2;
4498 (void)skip_expr(&p);
4499 if (*p == '`')
4500 ++p;
4501 continue;
4502 }
4503#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004504 /*
4505 * Quick check if this cannot be the start of a special string.
4506 * Also removes backslash before '%', '#' and '<'.
4507 */
4508 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4509 {
4510 ++p;
4511 continue;
4512 }
4513
4514 /*
4515 * Try to find a match at this position.
4516 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004517 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
4518 errormsgp, &escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004519 if (*errormsgp != NULL) /* error detected */
4520 return FAIL;
4521 if (repl == NULL) /* no match found */
4522 {
4523 p += srclen;
4524 continue;
4525 }
4526
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004527 /* Wildcards won't be expanded below, the replacement is taken
4528 * literally. But do expand "~/file", "~user/file" and "$HOME/file". */
4529 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4530 {
4531 char_u *l = repl;
4532
4533 repl = expand_env_save(repl);
4534 vim_free(l);
4535 }
4536
Bram Moolenaar63b92542007-03-27 14:57:09 +00004537 /* Need to escape white space et al. with a backslash.
4538 * Don't do this for:
4539 * - replacement that already has been escaped: "##"
4540 * - shell commands (may have to use quotes instead).
4541 * - non-unix systems when there is a single argument (spaces don't
4542 * separate arguments then).
4543 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004544 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00004545 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00004546 && eap->cmdidx != CMD_bang
4547 && eap->cmdidx != CMD_make
Bram Moolenaara37420f2006-02-04 22:37:47 +00004548 && eap->cmdidx != CMD_lmake
Bram Moolenaar071d4272004-06-13 20:20:40 +00004549 && eap->cmdidx != CMD_grep
Bram Moolenaara37420f2006-02-04 22:37:47 +00004550 && eap->cmdidx != CMD_lgrep
Bram Moolenaar071d4272004-06-13 20:20:40 +00004551 && eap->cmdidx != CMD_grepadd
Bram Moolenaara37420f2006-02-04 22:37:47 +00004552 && eap->cmdidx != CMD_lgrepadd
Bram Moolenaar071d4272004-06-13 20:20:40 +00004553#ifndef UNIX
4554 && !(eap->argt & NOSPC)
4555#endif
4556 )
4557 {
4558 char_u *l;
4559#ifdef BACKSLASH_IN_FILENAME
4560 /* Don't escape a backslash here, because rem_backslash() doesn't
4561 * remove it later. */
4562 static char_u *nobslash = (char_u *)" \t\"|";
4563# define ESCAPE_CHARS nobslash
4564#else
4565# define ESCAPE_CHARS escape_chars
4566#endif
4567
4568 for (l = repl; *l; ++l)
4569 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
4570 {
4571 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
4572 if (l != NULL)
4573 {
4574 vim_free(repl);
4575 repl = l;
4576 }
4577 break;
4578 }
4579 }
4580
4581 /* For a shell command a '!' must be escaped. */
4582 if ((eap->usefilter || eap->cmdidx == CMD_bang)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004583 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004584 {
4585 char_u *l;
4586
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004587 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004588 if (l != NULL)
4589 {
4590 vim_free(repl);
4591 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004592 }
4593 }
4594
4595 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
4596 vim_free(repl);
4597 if (p == NULL)
4598 return FAIL;
4599 }
4600
4601 /*
4602 * One file argument: Expand wildcards.
4603 * Don't do this with ":r !command" or ":w !command".
4604 */
4605 if ((eap->argt & NOSPC) && !eap->usefilter)
4606 {
4607 /*
4608 * May do this twice:
4609 * 1. Replace environment variables.
4610 * 2. Replace any other wildcards, remove backslashes.
4611 */
4612 for (n = 1; n <= 2; ++n)
4613 {
4614 if (n == 2)
4615 {
4616#ifdef UNIX
4617 /*
4618 * Only for Unix we check for more than one file name.
4619 * For other systems spaces are considered to be part
4620 * of the file name.
4621 * Only check here if there is no wildcard, otherwise
4622 * ExpandOne() will check for errors. This allows
4623 * ":e `ls ve*.c`" on Unix.
4624 */
4625 if (!has_wildcards)
4626 for (p = eap->arg; *p; ++p)
4627 {
4628 /* skip escaped characters */
4629 if (p[1] && (*p == '\\' || *p == Ctrl_V))
4630 ++p;
4631 else if (vim_iswhite(*p))
4632 {
4633 *errormsgp = (char_u *)_("E172: Only one file name allowed");
4634 return FAIL;
4635 }
4636 }
4637#endif
4638
4639 /*
4640 * Halve the number of backslashes (this is Vi compatible).
4641 * For Unix and OS/2, when wildcards are expanded, this is
4642 * done by ExpandOne() below.
4643 */
4644#if defined(UNIX) || defined(OS2)
4645 if (!has_wildcards)
4646#endif
4647 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004648 }
4649
4650 if (has_wildcards)
4651 {
4652 if (n == 1)
4653 {
4654 /*
4655 * First loop: May expand environment variables. This
4656 * can be done much faster with expand_env() than with
4657 * something else (e.g., calling a shell).
4658 * After expanding environment variables, check again
4659 * if there are still wildcards present.
4660 */
4661 if (vim_strchr(eap->arg, '$') != NULL
4662 || vim_strchr(eap->arg, '~') != NULL)
4663 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00004664 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004665 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004666 has_wildcards = mch_has_wildcard(NameBuff);
4667 p = NameBuff;
4668 }
4669 else
4670 p = NULL;
4671 }
4672 else /* n == 2 */
4673 {
4674 expand_T xpc;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004675 int options = WILD_LIST_NOTFOUND|WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004676
4677 ExpandInit(&xpc);
4678 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004679 if (p_wic)
4680 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004681 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01004682 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004683 if (p == NULL)
4684 return FAIL;
4685 }
4686 if (p != NULL)
4687 {
4688 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
4689 p, cmdlinep);
4690 if (n == 2) /* p came from ExpandOne() */
4691 vim_free(p);
4692 }
4693 }
4694 }
4695 }
4696 return OK;
4697}
4698
4699/*
4700 * Replace part of the command line, keeping eap->cmd, eap->arg and
4701 * eap->nextcmd correct.
4702 * "src" points to the part that is to be replaced, of length "srclen".
4703 * "repl" is the replacement string.
4704 * Returns a pointer to the character after the replaced string.
4705 * Returns NULL for failure.
4706 */
4707 static char_u *
4708repl_cmdline(eap, src, srclen, repl, cmdlinep)
4709 exarg_T *eap;
4710 char_u *src;
4711 int srclen;
4712 char_u *repl;
4713 char_u **cmdlinep;
4714{
4715 int len;
4716 int i;
4717 char_u *new_cmdline;
4718
4719 /*
4720 * The new command line is build in new_cmdline[].
4721 * First allocate it.
4722 * Careful: a "+cmd" argument may have been NUL terminated.
4723 */
4724 len = (int)STRLEN(repl);
4725 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004726 if (eap->nextcmd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004727 i += (int)STRLEN(eap->nextcmd);/* add space for next command */
4728 if ((new_cmdline = alloc((unsigned)i)) == NULL)
4729 return NULL; /* out of memory! */
4730
4731 /*
4732 * Copy the stuff before the expanded part.
4733 * Copy the expanded stuff.
4734 * Copy what came after the expanded part.
4735 * Copy the next commands, if there are any.
4736 */
4737 i = (int)(src - *cmdlinep); /* length of part before match */
4738 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00004739
Bram Moolenaar071d4272004-06-13 20:20:40 +00004740 mch_memmove(new_cmdline + i, repl, (size_t)len);
4741 i += len; /* remember the end of the string */
4742 STRCPY(new_cmdline + i, src + srclen);
4743 src = new_cmdline + i; /* remember where to continue */
4744
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004745 if (eap->nextcmd != NULL) /* append next command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004746 {
4747 i = (int)STRLEN(new_cmdline) + 1;
4748 STRCPY(new_cmdline + i, eap->nextcmd);
4749 eap->nextcmd = new_cmdline + i;
4750 }
4751 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
4752 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
4753 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
4754 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
4755 vim_free(*cmdlinep);
4756 *cmdlinep = new_cmdline;
4757
4758 return src;
4759}
4760
4761/*
4762 * Check for '|' to separate commands and '"' to start comments.
4763 */
4764 void
4765separate_nextcmd(eap)
4766 exarg_T *eap;
4767{
4768 char_u *p;
4769
Bram Moolenaar86b68352004-12-27 21:59:20 +00004770#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004771 p = skip_grep_pat(eap);
4772#else
4773 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004774#endif
4775
4776 for ( ; *p; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004777 {
4778 if (*p == Ctrl_V)
4779 {
4780 if (eap->argt & (USECTRLV | XFILE))
4781 ++p; /* skip CTRL-V and next char */
4782 else
Bram Moolenaarb0db5692007-08-14 20:54:49 +00004783 /* remove CTRL-V and skip next char */
Bram Moolenaara7241f52008-06-24 20:39:31 +00004784 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004785 if (*p == NUL) /* stop at NUL after CTRL-V */
4786 break;
4787 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004788
4789#ifdef FEAT_EVAL
4790 /* Skip over `=expr` when wildcards are expanded. */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004791 else if (p[0] == '`' && p[1] == '=' && (eap->argt & XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004792 {
4793 p += 2;
4794 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004795 }
4796#endif
4797
Bram Moolenaar071d4272004-06-13 20:20:40 +00004798 /* Check for '"': start of comment or '|': next command */
4799 /* :@" and :*" do not start a comment!
4800 * :redir @" doesn't either. */
4801 else if ((*p == '"' && !(eap->argt & NOTRLCOM)
4802 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
4803 || p != eap->arg)
4804 && (eap->cmdidx != CMD_redir
4805 || p != eap->arg + 1 || p[-1] != '@'))
4806 || *p == '|' || *p == '\n')
4807 {
4808 /*
4809 * We remove the '\' before the '|', unless USECTRLV is used
4810 * AND 'b' is present in 'cpoptions'.
4811 */
4812 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
4813 || !(eap->argt & USECTRLV)) && *(p - 1) == '\\')
4814 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00004815 STRMOVE(p - 1, p); /* remove the '\' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004816 --p;
4817 }
4818 else
4819 {
4820 eap->nextcmd = check_nextcmd(p);
4821 *p = NUL;
4822 break;
4823 }
4824 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004825 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004826
Bram Moolenaar071d4272004-06-13 20:20:40 +00004827 if (!(eap->argt & NOTRLCOM)) /* remove trailing spaces */
4828 del_trailing_spaces(eap->arg);
4829}
4830
4831/*
4832 * get + command from ex argument
4833 */
4834 static char_u *
4835getargcmd(argp)
4836 char_u **argp;
4837{
4838 char_u *arg = *argp;
4839 char_u *command = NULL;
4840
4841 if (*arg == '+') /* +[command] */
4842 {
4843 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02004844 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004845 command = dollar_command;
4846 else
4847 {
4848 command = arg;
4849 arg = skip_cmd_arg(command, TRUE);
4850 if (*arg != NUL)
4851 *arg++ = NUL; /* terminate command with NUL */
4852 }
4853
4854 arg = skipwhite(arg); /* skip over spaces */
4855 *argp = arg;
4856 }
4857 return command;
4858}
4859
4860/*
4861 * Find end of "+command" argument. Skip over "\ " and "\\".
4862 */
4863 static char_u *
4864skip_cmd_arg(p, rembs)
4865 char_u *p;
4866 int rembs; /* TRUE to halve the number of backslashes */
4867{
4868 while (*p && !vim_isspace(*p))
4869 {
4870 if (*p == '\\' && p[1] != NUL)
4871 {
4872 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00004873 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004874 else
4875 ++p;
4876 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004877 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878 }
4879 return p;
4880}
4881
4882/*
4883 * Get "++opt=arg" argument.
4884 * Return FAIL or OK.
4885 */
4886 static int
4887getargopt(eap)
4888 exarg_T *eap;
4889{
4890 char_u *arg = eap->arg + 2;
4891 int *pp = NULL;
4892#ifdef FEAT_MBYTE
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004893 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004894 char_u *p;
4895#endif
4896
4897 /* ":edit ++[no]bin[ary] file" */
4898 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
4899 {
4900 if (*arg == 'n')
4901 {
4902 arg += 2;
4903 eap->force_bin = FORCE_NOBIN;
4904 }
4905 else
4906 eap->force_bin = FORCE_BIN;
4907 if (!checkforcmd(&arg, "binary", 3))
4908 return FAIL;
4909 eap->arg = skipwhite(arg);
4910 return OK;
4911 }
4912
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004913 /* ":read ++edit file" */
4914 if (STRNCMP(arg, "edit", 4) == 0)
4915 {
4916 eap->read_edit = TRUE;
4917 eap->arg = skipwhite(arg + 4);
4918 return OK;
4919 }
4920
Bram Moolenaar071d4272004-06-13 20:20:40 +00004921 if (STRNCMP(arg, "ff", 2) == 0)
4922 {
4923 arg += 2;
4924 pp = &eap->force_ff;
4925 }
4926 else if (STRNCMP(arg, "fileformat", 10) == 0)
4927 {
4928 arg += 10;
4929 pp = &eap->force_ff;
4930 }
4931#ifdef FEAT_MBYTE
4932 else if (STRNCMP(arg, "enc", 3) == 0)
4933 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01004934 if (STRNCMP(arg, "encoding", 8) == 0)
4935 arg += 8;
4936 else
4937 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938 pp = &eap->force_enc;
4939 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004940 else if (STRNCMP(arg, "bad", 3) == 0)
4941 {
4942 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004943 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004944 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004945#endif
4946
4947 if (pp == NULL || *arg != '=')
4948 return FAIL;
4949
4950 ++arg;
4951 *pp = (int)(arg - eap->cmd);
4952 arg = skip_cmd_arg(arg, FALSE);
4953 eap->arg = skipwhite(arg);
4954 *arg = NUL;
4955
4956#ifdef FEAT_MBYTE
4957 if (pp == &eap->force_ff)
4958 {
4959#endif
4960 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
4961 return FAIL;
4962#ifdef FEAT_MBYTE
4963 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004964 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004965 {
4966 /* Make 'fileencoding' lower case. */
4967 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
4968 *p = TOLOWER_ASC(*p);
4969 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004970 else
4971 {
4972 /* Check ++bad= argument. Must be a single-byte character, "keep" or
4973 * "drop". */
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004974 p = eap->cmd + bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004975 if (STRICMP(p, "keep") == 0)
4976 eap->bad_char = BAD_KEEP;
4977 else if (STRICMP(p, "drop") == 0)
4978 eap->bad_char = BAD_DROP;
4979 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
4980 eap->bad_char = *p;
4981 else
4982 return FAIL;
4983 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004984#endif
4985
4986 return OK;
4987}
4988
4989/*
4990 * ":abbreviate" and friends.
4991 */
4992 static void
4993ex_abbreviate(eap)
4994 exarg_T *eap;
4995{
4996 do_exmap(eap, TRUE); /* almost the same as mapping */
4997}
4998
4999/*
5000 * ":map" and friends.
5001 */
5002 static void
5003ex_map(eap)
5004 exarg_T *eap;
5005{
5006 /*
5007 * If we are sourcing .exrc or .vimrc in current directory we
5008 * print the mappings for security reasons.
5009 */
5010 if (secure)
5011 {
5012 secure = 2;
5013 msg_outtrans(eap->cmd);
5014 msg_putchar('\n');
5015 }
5016 do_exmap(eap, FALSE);
5017}
5018
5019/*
5020 * ":unmap" and friends.
5021 */
5022 static void
5023ex_unmap(eap)
5024 exarg_T *eap;
5025{
5026 do_exmap(eap, FALSE);
5027}
5028
5029/*
5030 * ":mapclear" and friends.
5031 */
5032 static void
5033ex_mapclear(eap)
5034 exarg_T *eap;
5035{
5036 map_clear(eap->cmd, eap->arg, eap->forceit, FALSE);
5037}
5038
5039/*
5040 * ":abclear" and friends.
5041 */
5042 static void
5043ex_abclear(eap)
5044 exarg_T *eap;
5045{
5046 map_clear(eap->cmd, eap->arg, TRUE, TRUE);
5047}
5048
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005049#if defined(FEAT_AUTOCMD) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005050 static void
5051ex_autocmd(eap)
5052 exarg_T *eap;
5053{
5054 /*
5055 * Disallow auto commands from .exrc and .vimrc in current
5056 * directory for security reasons.
5057 */
5058 if (secure)
5059 {
5060 secure = 2;
5061 eap->errmsg = e_curdir;
5062 }
5063 else if (eap->cmdidx == CMD_autocmd)
5064 do_autocmd(eap->arg, eap->forceit);
5065 else
5066 do_augroup(eap->arg, eap->forceit);
5067}
5068
5069/*
5070 * ":doautocmd": Apply the automatic commands to the current buffer.
5071 */
5072 static void
5073ex_doautocmd(eap)
5074 exarg_T *eap;
5075{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005076 char_u *arg = eap->arg;
5077 int call_do_modelines = check_nomodeline(&arg);
5078
5079 (void)do_doautocmd(arg, TRUE);
5080 if (call_do_modelines) /* Only when there is no <nomodeline>. */
5081 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005082}
5083#endif
5084
5085#ifdef FEAT_LISTCMDS
5086/*
5087 * :[N]bunload[!] [N] [bufname] unload buffer
5088 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
5089 * :[N]bwipeout[!] [N] [bufname] delete buffer really
5090 */
5091 static void
5092ex_bunload(eap)
5093 exarg_T *eap;
5094{
5095 eap->errmsg = do_bufdel(
5096 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
5097 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
5098 : DOBUF_UNLOAD, eap->arg,
5099 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
5100}
5101
5102/*
5103 * :[N]buffer [N] to buffer N
5104 * :[N]sbuffer [N] to buffer N
5105 */
5106 static void
5107ex_buffer(eap)
5108 exarg_T *eap;
5109{
5110 if (*eap->arg)
5111 eap->errmsg = e_trailing;
5112 else
5113 {
5114 if (eap->addr_count == 0) /* default is current buffer */
5115 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
5116 else
5117 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
5118 }
5119}
5120
5121/*
5122 * :[N]bmodified [N] to next mod. buffer
5123 * :[N]sbmodified [N] to next mod. buffer
5124 */
5125 static void
5126ex_bmodified(eap)
5127 exarg_T *eap;
5128{
5129 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
5130}
5131
5132/*
5133 * :[N]bnext [N] to next buffer
5134 * :[N]sbnext [N] split and to next buffer
5135 */
5136 static void
5137ex_bnext(eap)
5138 exarg_T *eap;
5139{
5140 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
5141}
5142
5143/*
5144 * :[N]bNext [N] to previous buffer
5145 * :[N]bprevious [N] to previous buffer
5146 * :[N]sbNext [N] split and to previous buffer
5147 * :[N]sbprevious [N] split and to previous buffer
5148 */
5149 static void
5150ex_bprevious(eap)
5151 exarg_T *eap;
5152{
5153 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
5154}
5155
5156/*
5157 * :brewind to first buffer
5158 * :bfirst to first buffer
5159 * :sbrewind split and to first buffer
5160 * :sbfirst split and to first buffer
5161 */
5162 static void
5163ex_brewind(eap)
5164 exarg_T *eap;
5165{
5166 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
5167}
5168
5169/*
5170 * :blast to last buffer
5171 * :sblast split and to last buffer
5172 */
5173 static void
5174ex_blast(eap)
5175 exarg_T *eap;
5176{
5177 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
5178}
5179#endif
5180
5181 int
5182ends_excmd(c)
5183 int c;
5184{
5185 return (c == NUL || c == '|' || c == '"' || c == '\n');
5186}
5187
5188#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5189 || defined(PROTO)
5190/*
5191 * Return the next command, after the first '|' or '\n'.
5192 * Return NULL if not found.
5193 */
5194 char_u *
5195find_nextcmd(p)
5196 char_u *p;
5197{
5198 while (*p != '|' && *p != '\n')
5199 {
5200 if (*p == NUL)
5201 return NULL;
5202 ++p;
5203 }
5204 return (p + 1);
5205}
5206#endif
5207
5208/*
5209 * Check if *p is a separator between Ex commands.
5210 * Return NULL if it isn't, (p + 1) if it is.
5211 */
5212 char_u *
5213check_nextcmd(p)
5214 char_u *p;
5215{
5216 p = skipwhite(p);
5217 if (*p == '|' || *p == '\n')
5218 return (p + 1);
5219 else
5220 return NULL;
5221}
5222
5223/*
5224 * - if there are more files to edit
5225 * - and this is the last window
5226 * - and forceit not used
5227 * - and not repeated twice on a row
5228 * return FAIL and give error message if 'message' TRUE
5229 * return OK otherwise
5230 */
5231 static int
5232check_more(message, forceit)
5233 int message; /* when FALSE check only, no messages */
5234 int forceit;
5235{
5236 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5237
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005238 if (!forceit && only_one_window()
5239 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005240 {
5241 if (message)
5242 {
5243#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
5244 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
5245 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005246 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005247
5248 if (n == 1)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02005249 vim_strncpy(buff,
5250 (char_u *)_("1 more file to edit. Quit anyway?"),
Bram Moolenaard9462e32011-04-11 21:35:11 +02005251 DIALOG_MSG_SIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005252 else
Bram Moolenaard9462e32011-04-11 21:35:11 +02005253 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005254 _("%d more files to edit. Quit anyway?"), n);
5255 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5256 return OK;
5257 return FAIL;
5258 }
5259#endif
5260 if (n == 1)
5261 EMSG(_("E173: 1 more file to edit"));
5262 else
5263 EMSGN(_("E173: %ld more files to edit"), n);
5264 quitmore = 2; /* next try to quit is allowed */
5265 }
5266 return FAIL;
5267 }
5268 return OK;
5269}
5270
5271#ifdef FEAT_CMDL_COMPL
5272/*
5273 * Function given to ExpandGeneric() to obtain the list of command names.
5274 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005275 char_u *
5276get_command_name(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00005277 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005278 int idx;
5279{
5280 if (idx >= (int)CMD_SIZE)
5281# ifdef FEAT_USR_CMDS
5282 return get_user_command_name(idx);
5283# else
5284 return NULL;
5285# endif
5286 return cmdnames[idx].cmd_name;
5287}
5288#endif
5289
5290#if defined(FEAT_USR_CMDS) || defined(PROTO)
5291static 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));
5292static void uc_list __ARGS((char_u *name, size_t name_len));
5293static int uc_scan_attr __ARGS((char_u *attr, size_t len, long *argt, long *def, int *flags, int *compl, char_u **compl_arg));
5294static char_u *uc_split_args __ARGS((char_u *arg, size_t *lenp));
5295static 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));
5296
5297 static int
5298uc_add_command(name, name_len, rep, argt, def, flags, compl, compl_arg, force)
5299 char_u *name;
5300 size_t name_len;
5301 char_u *rep;
5302 long argt;
5303 long def;
5304 int flags;
5305 int compl;
5306 char_u *compl_arg;
5307 int force;
5308{
5309 ucmd_T *cmd = NULL;
5310 char_u *p;
5311 int i;
5312 int cmp = 1;
5313 char_u *rep_buf = NULL;
5314 garray_T *gap;
5315
Bram Moolenaar9c102382006-05-03 21:26:49 +00005316 replace_termcodes(rep, &rep_buf, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005317 if (rep_buf == NULL)
5318 {
5319 /* Can't replace termcodes - try using the string as is */
5320 rep_buf = vim_strsave(rep);
5321
5322 /* Give up if out of memory */
5323 if (rep_buf == NULL)
5324 return FAIL;
5325 }
5326
5327 /* get address of growarray: global or in curbuf */
5328 if (flags & UC_BUFFER)
5329 {
5330 gap = &curbuf->b_ucmds;
5331 if (gap->ga_itemsize == 0)
5332 ga_init2(gap, (int)sizeof(ucmd_T), 4);
5333 }
5334 else
5335 gap = &ucmds;
5336
5337 /* Search for the command in the already defined commands. */
5338 for (i = 0; i < gap->ga_len; ++i)
5339 {
5340 size_t len;
5341
5342 cmd = USER_CMD_GA(gap, i);
5343 len = STRLEN(cmd->uc_name);
5344 cmp = STRNCMP(name, cmd->uc_name, name_len);
5345 if (cmp == 0)
5346 {
5347 if (name_len < len)
5348 cmp = -1;
5349 else if (name_len > len)
5350 cmp = 1;
5351 }
5352
5353 if (cmp == 0)
5354 {
5355 if (!force)
5356 {
5357 EMSG(_("E174: Command already exists: add ! to replace it"));
5358 goto fail;
5359 }
5360
5361 vim_free(cmd->uc_rep);
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00005362 cmd->uc_rep = NULL;
5363#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5364 vim_free(cmd->uc_compl_arg);
5365 cmd->uc_compl_arg = NULL;
5366#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005367 break;
5368 }
5369
5370 /* Stop as soon as we pass the name to add */
5371 if (cmp < 0)
5372 break;
5373 }
5374
5375 /* Extend the array unless we're replacing an existing command */
5376 if (cmp != 0)
5377 {
5378 if (ga_grow(gap, 1) != OK)
5379 goto fail;
5380 if ((p = vim_strnsave(name, (int)name_len)) == NULL)
5381 goto fail;
5382
5383 cmd = USER_CMD_GA(gap, i);
5384 mch_memmove(cmd + 1, cmd, (gap->ga_len - i) * sizeof(ucmd_T));
5385
5386 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005387
5388 cmd->uc_name = p;
5389 }
5390
5391 cmd->uc_rep = rep_buf;
5392 cmd->uc_argt = argt;
5393 cmd->uc_def = def;
5394 cmd->uc_compl = compl;
5395#ifdef FEAT_EVAL
5396 cmd->uc_scriptID = current_SID;
5397# ifdef FEAT_CMDL_COMPL
5398 cmd->uc_compl_arg = compl_arg;
5399# endif
5400#endif
5401
5402 return OK;
5403
5404fail:
5405 vim_free(rep_buf);
5406#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5407 vim_free(compl_arg);
5408#endif
5409 return FAIL;
5410}
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005411#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005412
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005413#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005414/*
5415 * List of names for completion for ":command" with the EXPAND_ flag.
5416 * Must be alphabetical for completion.
5417 */
5418static struct
5419{
5420 int expand;
5421 char *name;
5422} command_complete[] =
5423{
5424 {EXPAND_AUGROUP, "augroup"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005425 {EXPAND_BEHAVE, "behave"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005426 {EXPAND_BUFFERS, "buffer"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005427 {EXPAND_COLORS, "color"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005428 {EXPAND_COMMANDS, "command"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005429 {EXPAND_COMPILER, "compiler"},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005430#if defined(FEAT_CSCOPE)
5431 {EXPAND_CSCOPE, "cscope"},
5432#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005433#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5434 {EXPAND_USER_DEFINED, "custom"},
Bram Moolenaara466c992005-07-09 21:03:22 +00005435 {EXPAND_USER_LIST, "customlist"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005436#endif
5437 {EXPAND_DIRECTORIES, "dir"},
5438 {EXPAND_ENV_VARS, "environment"},
5439 {EXPAND_EVENTS, "event"},
5440 {EXPAND_EXPRESSION, "expression"},
5441 {EXPAND_FILES, "file"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005442 {EXPAND_FILES_IN_PATH, "file_in_path"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005443 {EXPAND_FILETYPE, "filetype"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005444 {EXPAND_FUNCTIONS, "function"},
5445 {EXPAND_HELP, "help"},
5446 {EXPAND_HIGHLIGHT, "highlight"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005447#if defined(FEAT_CMDHIST)
5448 {EXPAND_HISTORY, "history"},
5449#endif
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005450#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005451 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005452 {EXPAND_LOCALES, "locale"},
5453#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005454 {EXPAND_MAPPINGS, "mapping"},
5455 {EXPAND_MENUS, "menu"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005456 {EXPAND_OWNSYNTAX, "syntax"},
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02005457#if defined(FEAT_PROFILE)
5458 {EXPAND_SYNTIME, "syntime"},
5459#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005460 {EXPAND_SETTINGS, "option"},
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005461 {EXPAND_SHELLCMD, "shellcmd"},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005462#if defined(FEAT_SIGNS)
5463 {EXPAND_SIGN, "sign"},
5464#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005465 {EXPAND_TAGS, "tag"},
5466 {EXPAND_TAGS_LISTFILES, "tag_listfiles"},
Bram Moolenaar24305862012-08-15 14:05:05 +02005467 {EXPAND_USER, "user"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005468 {EXPAND_USER_VARS, "var"},
5469 {0, NULL}
5470};
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005471#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005472
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005473#if defined(FEAT_USR_CMDS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005474 static void
5475uc_list(name, name_len)
5476 char_u *name;
5477 size_t name_len;
5478{
5479 int i, j;
5480 int found = FALSE;
5481 ucmd_T *cmd;
5482 int len;
5483 long a;
5484 garray_T *gap;
5485
5486 gap = &curbuf->b_ucmds;
5487 for (;;)
5488 {
5489 for (i = 0; i < gap->ga_len; ++i)
5490 {
5491 cmd = USER_CMD_GA(gap, i);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005492 a = (long)cmd->uc_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005493
5494 /* Skip commands which don't match the requested prefix */
5495 if (STRNCMP(name, cmd->uc_name, name_len) != 0)
5496 continue;
5497
5498 /* Put out the title first time */
5499 if (!found)
5500 MSG_PUTS_TITLE(_("\n Name Args Range Complete Definition"));
5501 found = TRUE;
5502 msg_putchar('\n');
5503 if (got_int)
5504 break;
5505
5506 /* Special cases */
5507 msg_putchar(a & BANG ? '!' : ' ');
5508 msg_putchar(a & REGSTR ? '"' : ' ');
5509 msg_putchar(gap != &ucmds ? 'b' : ' ');
5510 msg_putchar(' ');
5511
5512 msg_outtrans_attr(cmd->uc_name, hl_attr(HLF_D));
5513 len = (int)STRLEN(cmd->uc_name) + 4;
5514
5515 do {
5516 msg_putchar(' ');
5517 ++len;
5518 } while (len < 16);
5519
5520 len = 0;
5521
5522 /* Arguments */
5523 switch ((int)(a & (EXTRA|NOSPC|NEEDARG)))
5524 {
5525 case 0: IObuff[len++] = '0'; break;
5526 case (EXTRA): IObuff[len++] = '*'; break;
5527 case (EXTRA|NOSPC): IObuff[len++] = '?'; break;
5528 case (EXTRA|NEEDARG): IObuff[len++] = '+'; break;
5529 case (EXTRA|NOSPC|NEEDARG): IObuff[len++] = '1'; break;
5530 }
5531
5532 do {
5533 IObuff[len++] = ' ';
5534 } while (len < 5);
5535
5536 /* Range */
5537 if (a & (RANGE|COUNT))
5538 {
5539 if (a & COUNT)
5540 {
5541 /* -count=N */
5542 sprintf((char *)IObuff + len, "%ldc", cmd->uc_def);
5543 len += (int)STRLEN(IObuff + len);
5544 }
5545 else if (a & DFLALL)
5546 IObuff[len++] = '%';
5547 else if (cmd->uc_def >= 0)
5548 {
5549 /* -range=N */
5550 sprintf((char *)IObuff + len, "%ld", cmd->uc_def);
5551 len += (int)STRLEN(IObuff + len);
5552 }
5553 else
5554 IObuff[len++] = '.';
5555 }
5556
5557 do {
5558 IObuff[len++] = ' ';
5559 } while (len < 11);
5560
5561 /* Completion */
5562 for (j = 0; command_complete[j].expand != 0; ++j)
5563 if (command_complete[j].expand == cmd->uc_compl)
5564 {
5565 STRCPY(IObuff + len, command_complete[j].name);
5566 len += (int)STRLEN(IObuff + len);
5567 break;
5568 }
5569
5570 do {
5571 IObuff[len++] = ' ';
5572 } while (len < 21);
5573
5574 IObuff[len] = '\0';
5575 msg_outtrans(IObuff);
5576
5577 msg_outtrans_special(cmd->uc_rep, FALSE);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005578#ifdef FEAT_EVAL
5579 if (p_verbose > 0)
5580 last_set_msg(cmd->uc_scriptID);
5581#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005582 out_flush();
5583 ui_breakcheck();
5584 if (got_int)
5585 break;
5586 }
5587 if (gap == &ucmds || i < gap->ga_len)
5588 break;
5589 gap = &ucmds;
5590 }
5591
5592 if (!found)
5593 MSG(_("No user-defined commands found"));
5594}
5595
5596 static char_u *
5597uc_fun_cmd()
5598{
5599 static char_u fcmd[] = {0x84, 0xaf, 0x60, 0xb9, 0xaf, 0xb5, 0x60, 0xa4,
5600 0xa5, 0xad, 0xa1, 0xae, 0xa4, 0x60, 0xa1, 0x60,
5601 0xb3, 0xa8, 0xb2, 0xb5, 0xa2, 0xa2, 0xa5, 0xb2,
5602 0xb9, 0x7f, 0};
5603 int i;
5604
5605 for (i = 0; fcmd[i]; ++i)
5606 IObuff[i] = fcmd[i] - 0x40;
5607 IObuff[i] = 0;
5608 return IObuff;
5609}
5610
5611 static int
5612uc_scan_attr(attr, len, argt, def, flags, compl, compl_arg)
5613 char_u *attr;
5614 size_t len;
5615 long *argt;
5616 long *def;
5617 int *flags;
5618 int *compl;
5619 char_u **compl_arg;
5620{
5621 char_u *p;
5622
5623 if (len == 0)
5624 {
5625 EMSG(_("E175: No attribute specified"));
5626 return FAIL;
5627 }
5628
5629 /* First, try the simple attributes (no arguments) */
5630 if (STRNICMP(attr, "bang", len) == 0)
5631 *argt |= BANG;
5632 else if (STRNICMP(attr, "buffer", len) == 0)
5633 *flags |= UC_BUFFER;
5634 else if (STRNICMP(attr, "register", len) == 0)
5635 *argt |= REGSTR;
5636 else if (STRNICMP(attr, "bar", len) == 0)
5637 *argt |= TRLBAR;
5638 else
5639 {
5640 int i;
5641 char_u *val = NULL;
5642 size_t vallen = 0;
5643 size_t attrlen = len;
5644
5645 /* Look for the attribute name - which is the part before any '=' */
5646 for (i = 0; i < (int)len; ++i)
5647 {
5648 if (attr[i] == '=')
5649 {
5650 val = &attr[i + 1];
5651 vallen = len - i - 1;
5652 attrlen = i;
5653 break;
5654 }
5655 }
5656
5657 if (STRNICMP(attr, "nargs", attrlen) == 0)
5658 {
5659 if (vallen == 1)
5660 {
5661 if (*val == '0')
5662 /* Do nothing - this is the default */;
5663 else if (*val == '1')
5664 *argt |= (EXTRA | NOSPC | NEEDARG);
5665 else if (*val == '*')
5666 *argt |= EXTRA;
5667 else if (*val == '?')
5668 *argt |= (EXTRA | NOSPC);
5669 else if (*val == '+')
5670 *argt |= (EXTRA | NEEDARG);
5671 else
5672 goto wrong_nargs;
5673 }
5674 else
5675 {
5676wrong_nargs:
5677 EMSG(_("E176: Invalid number of arguments"));
5678 return FAIL;
5679 }
5680 }
5681 else if (STRNICMP(attr, "range", attrlen) == 0)
5682 {
5683 *argt |= RANGE;
5684 if (vallen == 1 && *val == '%')
5685 *argt |= DFLALL;
5686 else if (val != NULL)
5687 {
5688 p = val;
5689 if (*def >= 0)
5690 {
5691two_count:
5692 EMSG(_("E177: Count cannot be specified twice"));
5693 return FAIL;
5694 }
5695
5696 *def = getdigits(&p);
5697 *argt |= (ZEROR | NOTADR);
5698
5699 if (p != val + vallen || vallen == 0)
5700 {
5701invalid_count:
5702 EMSG(_("E178: Invalid default value for count"));
5703 return FAIL;
5704 }
5705 }
5706 }
5707 else if (STRNICMP(attr, "count", attrlen) == 0)
5708 {
Bram Moolenaar32e7b2d2005-02-27 22:36:47 +00005709 *argt |= (COUNT | ZEROR | RANGE | NOTADR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005710
5711 if (val != NULL)
5712 {
5713 p = val;
5714 if (*def >= 0)
5715 goto two_count;
5716
5717 *def = getdigits(&p);
5718
5719 if (p != val + vallen)
5720 goto invalid_count;
5721 }
5722
5723 if (*def < 0)
5724 *def = 0;
5725 }
5726 else if (STRNICMP(attr, "complete", attrlen) == 0)
5727 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005728 if (val == NULL)
5729 {
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005730 EMSG(_("E179: argument required for -complete"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005731 return FAIL;
5732 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005733
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005734 if (parse_compl_arg(val, (int)vallen, compl, argt, compl_arg)
5735 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005736 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005737 }
5738 else
5739 {
5740 char_u ch = attr[len];
5741 attr[len] = '\0';
5742 EMSG2(_("E181: Invalid attribute: %s"), attr);
5743 attr[len] = ch;
5744 return FAIL;
5745 }
5746 }
5747
5748 return OK;
5749}
5750
Bram Moolenaara850a712009-01-28 14:42:59 +00005751/*
5752 * ":command ..."
5753 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005754 static void
5755ex_command(eap)
5756 exarg_T *eap;
5757{
5758 char_u *name;
5759 char_u *end;
5760 char_u *p;
5761 long argt = 0;
5762 long def = -1;
5763 int flags = 0;
5764 int compl = EXPAND_NOTHING;
5765 char_u *compl_arg = NULL;
5766 int has_attr = (eap->arg[0] == '-');
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01005767 int name_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005768
5769 p = eap->arg;
5770
5771 /* Check for attributes */
5772 while (*p == '-')
5773 {
5774 ++p;
5775 end = skiptowhite(p);
5776 if (uc_scan_attr(p, end - p, &argt, &def, &flags, &compl, &compl_arg)
5777 == FAIL)
5778 return;
5779 p = skipwhite(end);
5780 }
5781
5782 /* Get the name (if any) and skip to the following argument */
5783 name = p;
5784 if (ASCII_ISALPHA(*p))
5785 while (ASCII_ISALNUM(*p))
5786 ++p;
5787 if (!ends_excmd(*p) && !vim_iswhite(*p))
5788 {
5789 EMSG(_("E182: Invalid command name"));
5790 return;
5791 }
5792 end = p;
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01005793 name_len = (int)(end - name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005794
5795 /* If there is nothing after the name, and no attributes were specified,
5796 * we are listing commands
5797 */
5798 p = skipwhite(end);
5799 if (!has_attr && ends_excmd(*p))
5800 {
5801 uc_list(name, end - name);
5802 }
5803 else if (!ASCII_ISUPPER(*name))
5804 {
5805 EMSG(_("E183: User defined commands must start with an uppercase letter"));
5806 return;
5807 }
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01005808 else if ((name_len == 1 && *name == 'X')
5809 || (name_len <= 4
5810 && STRNCMP(name, "Next", name_len > 4 ? 4 : name_len) == 0))
5811 {
5812 EMSG(_("E841: Reserved name, cannot be used for user defined command"));
5813 return;
5814 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005815 else
5816 uc_add_command(name, end - name, p, argt, def, flags, compl, compl_arg,
5817 eap->forceit);
5818}
5819
5820/*
5821 * ":comclear"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005822 * Clear all user commands, global and for current buffer.
5823 */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00005824 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005825ex_comclear(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00005826 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005827{
5828 uc_clear(&ucmds);
5829 uc_clear(&curbuf->b_ucmds);
5830}
5831
5832/*
5833 * Clear all user commands for "gap".
5834 */
5835 void
5836uc_clear(gap)
5837 garray_T *gap;
5838{
5839 int i;
5840 ucmd_T *cmd;
5841
5842 for (i = 0; i < gap->ga_len; ++i)
5843 {
5844 cmd = USER_CMD_GA(gap, i);
5845 vim_free(cmd->uc_name);
5846 vim_free(cmd->uc_rep);
5847# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5848 vim_free(cmd->uc_compl_arg);
5849# endif
5850 }
5851 ga_clear(gap);
5852}
5853
5854 static void
5855ex_delcommand(eap)
5856 exarg_T *eap;
5857{
5858 int i = 0;
5859 ucmd_T *cmd = NULL;
5860 int cmp = -1;
5861 garray_T *gap;
5862
5863 gap = &curbuf->b_ucmds;
5864 for (;;)
5865 {
5866 for (i = 0; i < gap->ga_len; ++i)
5867 {
5868 cmd = USER_CMD_GA(gap, i);
5869 cmp = STRCMP(eap->arg, cmd->uc_name);
5870 if (cmp <= 0)
5871 break;
5872 }
5873 if (gap == &ucmds || cmp == 0)
5874 break;
5875 gap = &ucmds;
5876 }
5877
5878 if (cmp != 0)
5879 {
5880 EMSG2(_("E184: No such user-defined command: %s"), eap->arg);
5881 return;
5882 }
5883
5884 vim_free(cmd->uc_name);
5885 vim_free(cmd->uc_rep);
5886# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5887 vim_free(cmd->uc_compl_arg);
5888# endif
5889
5890 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005891
5892 if (i < gap->ga_len)
5893 mch_memmove(cmd, cmd + 1, (gap->ga_len - i) * sizeof(ucmd_T));
5894}
5895
Bram Moolenaar552f8a12007-03-08 17:12:08 +00005896/*
5897 * split and quote args for <f-args>
5898 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005899 static char_u *
5900uc_split_args(arg, lenp)
5901 char_u *arg;
5902 size_t *lenp;
5903{
5904 char_u *buf;
5905 char_u *p;
5906 char_u *q;
5907 int len;
5908
5909 /* Precalculate length */
5910 p = arg;
5911 len = 2; /* Initial and final quotes */
5912
5913 while (*p)
5914 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00005915 if (p[0] == '\\' && p[1] == '\\')
5916 {
5917 len += 2;
5918 p += 2;
5919 }
5920 else if (p[0] == '\\' && vim_iswhite(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005921 {
5922 len += 1;
5923 p += 2;
5924 }
5925 else if (*p == '\\' || *p == '"')
5926 {
5927 len += 2;
5928 p += 1;
5929 }
5930 else if (vim_iswhite(*p))
5931 {
5932 p = skipwhite(p);
5933 if (*p == NUL)
5934 break;
5935 len += 3; /* "," */
5936 }
5937 else
5938 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02005939#ifdef FEAT_MBYTE
5940 int charlen = (*mb_ptr2len)(p);
5941 len += charlen;
5942 p += charlen;
5943#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005944 ++len;
5945 ++p;
Bram Moolenaardfef1542012-07-10 19:25:10 +02005946#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005947 }
5948 }
5949
5950 buf = alloc(len + 1);
5951 if (buf == NULL)
5952 {
5953 *lenp = 0;
5954 return buf;
5955 }
5956
5957 p = arg;
5958 q = buf;
5959 *q++ = '"';
5960 while (*p)
5961 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00005962 if (p[0] == '\\' && p[1] == '\\')
5963 {
5964 *q++ = '\\';
5965 *q++ = '\\';
5966 p += 2;
5967 }
5968 else if (p[0] == '\\' && vim_iswhite(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005969 {
5970 *q++ = p[1];
5971 p += 2;
5972 }
5973 else if (*p == '\\' || *p == '"')
5974 {
5975 *q++ = '\\';
5976 *q++ = *p++;
5977 }
5978 else if (vim_iswhite(*p))
5979 {
5980 p = skipwhite(p);
5981 if (*p == NUL)
5982 break;
5983 *q++ = '"';
5984 *q++ = ',';
5985 *q++ = '"';
5986 }
5987 else
5988 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02005989 MB_COPY_CHAR(p, q);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005990 }
5991 }
5992 *q++ = '"';
5993 *q = 0;
5994
5995 *lenp = len;
5996 return buf;
5997}
5998
5999/*
6000 * Check for a <> code in a user command.
6001 * "code" points to the '<'. "len" the length of the <> (inclusive).
6002 * "buf" is where the result is to be added.
6003 * "split_buf" points to a buffer used for splitting, caller should free it.
6004 * "split_len" is the length of what "split_buf" contains.
6005 * Returns the length of the replacement, which has been added to "buf".
6006 * Returns -1 if there was no match, and only the "<" has been copied.
6007 */
6008 static size_t
6009uc_check_code(code, len, buf, cmd, eap, split_buf, split_len)
6010 char_u *code;
6011 size_t len;
6012 char_u *buf;
6013 ucmd_T *cmd; /* the user command we're expanding */
6014 exarg_T *eap; /* ex arguments */
6015 char_u **split_buf;
6016 size_t *split_len;
6017{
6018 size_t result = 0;
6019 char_u *p = code + 1;
6020 size_t l = len - 2;
6021 int quote = 0;
6022 enum { ct_ARGS, ct_BANG, ct_COUNT, ct_LINE1, ct_LINE2, ct_REGISTER,
6023 ct_LT, ct_NONE } type = ct_NONE;
6024
6025 if ((vim_strchr((char_u *)"qQfF", *p) != NULL) && p[1] == '-')
6026 {
6027 quote = (*p == 'q' || *p == 'Q') ? 1 : 2;
6028 p += 2;
6029 l -= 2;
6030 }
6031
Bram Moolenaar371d5402006-03-20 21:47:49 +00006032 ++l;
6033 if (l <= 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006034 type = ct_NONE;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006035 else if (STRNICMP(p, "args>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006036 type = ct_ARGS;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006037 else if (STRNICMP(p, "bang>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006038 type = ct_BANG;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006039 else if (STRNICMP(p, "count>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006040 type = ct_COUNT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006041 else if (STRNICMP(p, "line1>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006042 type = ct_LINE1;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006043 else if (STRNICMP(p, "line2>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006044 type = ct_LINE2;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006045 else if (STRNICMP(p, "lt>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006046 type = ct_LT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006047 else if (STRNICMP(p, "reg>", l) == 0 || STRNICMP(p, "register>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006048 type = ct_REGISTER;
6049
6050 switch (type)
6051 {
6052 case ct_ARGS:
6053 /* Simple case first */
6054 if (*eap->arg == NUL)
6055 {
6056 if (quote == 1)
6057 {
6058 result = 2;
6059 if (buf != NULL)
6060 STRCPY(buf, "''");
6061 }
6062 else
6063 result = 0;
6064 break;
6065 }
6066
6067 /* When specified there is a single argument don't split it.
6068 * Works for ":Cmd %" when % is "a b c". */
6069 if ((eap->argt & NOSPC) && quote == 2)
6070 quote = 1;
6071
6072 switch (quote)
6073 {
6074 case 0: /* No quoting, no splitting */
6075 result = STRLEN(eap->arg);
6076 if (buf != NULL)
6077 STRCPY(buf, eap->arg);
6078 break;
6079 case 1: /* Quote, but don't split */
6080 result = STRLEN(eap->arg) + 2;
6081 for (p = eap->arg; *p; ++p)
6082 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006083#ifdef FEAT_MBYTE
6084 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6085 /* DBCS can contain \ in a trail byte, skip the
6086 * double-byte character. */
6087 ++p;
6088 else
6089#endif
6090 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006091 ++result;
6092 }
6093
6094 if (buf != NULL)
6095 {
6096 *buf++ = '"';
6097 for (p = eap->arg; *p; ++p)
6098 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006099#ifdef FEAT_MBYTE
6100 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6101 /* DBCS can contain \ in a trail byte, copy the
6102 * double-byte character to avoid escaping. */
6103 *buf++ = *p++;
6104 else
6105#endif
6106 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006107 *buf++ = '\\';
6108 *buf++ = *p;
6109 }
6110 *buf = '"';
6111 }
6112
6113 break;
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006114 case 2: /* Quote and split (<f-args>) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006115 /* This is hard, so only do it once, and cache the result */
6116 if (*split_buf == NULL)
6117 *split_buf = uc_split_args(eap->arg, split_len);
6118
6119 result = *split_len;
6120 if (buf != NULL && result != 0)
6121 STRCPY(buf, *split_buf);
6122
6123 break;
6124 }
6125 break;
6126
6127 case ct_BANG:
6128 result = eap->forceit ? 1 : 0;
6129 if (quote)
6130 result += 2;
6131 if (buf != NULL)
6132 {
6133 if (quote)
6134 *buf++ = '"';
6135 if (eap->forceit)
6136 *buf++ = '!';
6137 if (quote)
6138 *buf = '"';
6139 }
6140 break;
6141
6142 case ct_LINE1:
6143 case ct_LINE2:
6144 case ct_COUNT:
6145 {
6146 char num_buf[20];
6147 long num = (type == ct_LINE1) ? eap->line1 :
6148 (type == ct_LINE2) ? eap->line2 :
6149 (eap->addr_count > 0) ? eap->line2 : cmd->uc_def;
6150 size_t num_len;
6151
6152 sprintf(num_buf, "%ld", num);
6153 num_len = STRLEN(num_buf);
6154 result = num_len;
6155
6156 if (quote)
6157 result += 2;
6158
6159 if (buf != NULL)
6160 {
6161 if (quote)
6162 *buf++ = '"';
6163 STRCPY(buf, num_buf);
6164 buf += num_len;
6165 if (quote)
6166 *buf = '"';
6167 }
6168
6169 break;
6170 }
6171
6172 case ct_REGISTER:
6173 result = eap->regname ? 1 : 0;
6174 if (quote)
6175 result += 2;
6176 if (buf != NULL)
6177 {
6178 if (quote)
6179 *buf++ = '\'';
6180 if (eap->regname)
6181 *buf++ = eap->regname;
6182 if (quote)
6183 *buf = '\'';
6184 }
6185 break;
6186
6187 case ct_LT:
6188 result = 1;
6189 if (buf != NULL)
6190 *buf = '<';
6191 break;
6192
6193 default:
6194 /* Not recognized: just copy the '<' and return -1. */
6195 result = (size_t)-1;
6196 if (buf != NULL)
6197 *buf = '<';
6198 break;
6199 }
6200
6201 return result;
6202}
6203
6204 static void
6205do_ucmd(eap)
6206 exarg_T *eap;
6207{
6208 char_u *buf;
6209 char_u *p;
6210 char_u *q;
6211
6212 char_u *start;
Bram Moolenaar25648a52009-02-21 19:37:46 +00006213 char_u *end = NULL;
Bram Moolenaara850a712009-01-28 14:42:59 +00006214 char_u *ksp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006215 size_t len, totlen;
6216
6217 size_t split_len = 0;
6218 char_u *split_buf = NULL;
6219 ucmd_T *cmd;
6220#ifdef FEAT_EVAL
6221 scid_T save_current_SID = current_SID;
6222#endif
6223
6224 if (eap->cmdidx == CMD_USER)
6225 cmd = USER_CMD(eap->useridx);
6226 else
6227 cmd = USER_CMD_GA(&curbuf->b_ucmds, eap->useridx);
6228
6229 /*
6230 * Replace <> in the command by the arguments.
Bram Moolenaara850a712009-01-28 14:42:59 +00006231 * First round: "buf" is NULL, compute length, allocate "buf".
6232 * Second round: copy result into "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006233 */
6234 buf = NULL;
6235 for (;;)
6236 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006237 p = cmd->uc_rep; /* source */
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00006238 q = buf; /* destination */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006239 totlen = 0;
Bram Moolenaara850a712009-01-28 14:42:59 +00006240
6241 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006242 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006243 start = vim_strchr(p, '<');
6244 if (start != NULL)
6245 end = vim_strchr(start + 1, '>');
6246 if (buf != NULL)
6247 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006248 for (ksp = p; *ksp != NUL && *ksp != K_SPECIAL; ++ksp)
6249 ;
6250 if (*ksp == K_SPECIAL
6251 && (start == NULL || ksp < start || end == NULL)
Bram Moolenaara850a712009-01-28 14:42:59 +00006252 && ((ksp[1] == KS_SPECIAL && ksp[2] == KE_FILLER)
6253# ifdef FEAT_GUI
6254 || (ksp[1] == KS_EXTRA && ksp[2] == (int)KE_CSI)
6255# endif
6256 ))
6257 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006258 /* K_SPECIAL has been put in the buffer as K_SPECIAL
Bram Moolenaara850a712009-01-28 14:42:59 +00006259 * KS_SPECIAL KE_FILLER, like for mappings, but
6260 * do_cmdline() doesn't handle that, so convert it back.
6261 * Also change K_SPECIAL KS_EXTRA KE_CSI into CSI. */
6262 len = ksp - p;
6263 if (len > 0)
6264 {
6265 mch_memmove(q, p, len);
6266 q += len;
6267 }
6268 *q++ = ksp[1] == KS_SPECIAL ? K_SPECIAL : CSI;
6269 p = ksp + 3;
6270 continue;
6271 }
6272 }
6273
6274 /* break if there no <item> is found */
6275 if (start == NULL || end == NULL)
6276 break;
6277
Bram Moolenaar071d4272004-06-13 20:20:40 +00006278 /* Include the '>' */
6279 ++end;
6280
6281 /* Take everything up to the '<' */
6282 len = start - p;
6283 if (buf == NULL)
6284 totlen += len;
6285 else
6286 {
6287 mch_memmove(q, p, len);
6288 q += len;
6289 }
6290
6291 len = uc_check_code(start, end - start, q, cmd, eap,
6292 &split_buf, &split_len);
6293 if (len == (size_t)-1)
6294 {
6295 /* no match, continue after '<' */
6296 p = start + 1;
6297 len = 1;
6298 }
6299 else
6300 p = end;
6301 if (buf == NULL)
6302 totlen += len;
6303 else
6304 q += len;
6305 }
6306 if (buf != NULL) /* second time here, finished */
6307 {
6308 STRCPY(q, p);
6309 break;
6310 }
6311
6312 totlen += STRLEN(p); /* Add on the trailing characters */
6313 buf = alloc((unsigned)(totlen + 1));
6314 if (buf == NULL)
6315 {
6316 vim_free(split_buf);
6317 return;
6318 }
6319 }
6320
6321#ifdef FEAT_EVAL
6322 current_SID = cmd->uc_scriptID;
6323#endif
6324 (void)do_cmdline(buf, eap->getline, eap->cookie,
6325 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
6326#ifdef FEAT_EVAL
6327 current_SID = save_current_SID;
6328#endif
6329 vim_free(buf);
6330 vim_free(split_buf);
6331}
6332
6333# if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6334 static char_u *
6335get_user_command_name(idx)
6336 int idx;
6337{
6338 return get_user_commands(NULL, idx - (int)CMD_SIZE);
6339}
6340
6341/*
6342 * Function given to ExpandGeneric() to obtain the list of user command names.
6343 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006344 char_u *
6345get_user_commands(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006346 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006347 int idx;
6348{
6349 if (idx < curbuf->b_ucmds.ga_len)
6350 return USER_CMD_GA(&curbuf->b_ucmds, idx)->uc_name;
6351 idx -= curbuf->b_ucmds.ga_len;
6352 if (idx < ucmds.ga_len)
6353 return USER_CMD(idx)->uc_name;
6354 return NULL;
6355}
6356
6357/*
6358 * Function given to ExpandGeneric() to obtain the list of user command
6359 * attributes.
6360 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006361 char_u *
6362get_user_cmd_flags(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006363 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006364 int idx;
6365{
6366 static char *user_cmd_flags[] =
6367 {"bang", "bar", "buffer", "complete", "count",
6368 "nargs", "range", "register"};
6369
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00006370 if (idx >= (int)(sizeof(user_cmd_flags) / sizeof(user_cmd_flags[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006371 return NULL;
6372 return (char_u *)user_cmd_flags[idx];
6373}
6374
6375/*
6376 * Function given to ExpandGeneric() to obtain the list of values for -nargs.
6377 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006378 char_u *
6379get_user_cmd_nargs(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006380 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006381 int idx;
6382{
6383 static char *user_cmd_nargs[] = {"0", "1", "*", "?", "+"};
6384
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00006385 if (idx >= (int)(sizeof(user_cmd_nargs) / sizeof(user_cmd_nargs[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006386 return NULL;
6387 return (char_u *)user_cmd_nargs[idx];
6388}
6389
6390/*
6391 * Function given to ExpandGeneric() to obtain the list of values for -complete.
6392 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006393 char_u *
6394get_user_cmd_complete(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 return (char_u *)command_complete[idx].name;
6399}
6400# endif /* FEAT_CMDL_COMPL */
6401
6402#endif /* FEAT_USR_CMDS */
6403
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006404#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
6405/*
6406 * Parse a completion argument "value[vallen]".
6407 * The detected completion goes in "*complp", argument type in "*argt".
6408 * When there is an argument, for function and user defined completion, it's
6409 * copied to allocated memory and stored in "*compl_arg".
6410 * Returns FAIL if something is wrong.
6411 */
6412 int
6413parse_compl_arg(value, vallen, complp, argt, compl_arg)
6414 char_u *value;
6415 int vallen;
6416 int *complp;
6417 long *argt;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006418 char_u **compl_arg UNUSED;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006419{
6420 char_u *arg = NULL;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006421# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006422 size_t arglen = 0;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006423# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006424 int i;
6425 int valend = vallen;
6426
6427 /* Look for any argument part - which is the part after any ',' */
6428 for (i = 0; i < vallen; ++i)
6429 {
6430 if (value[i] == ',')
6431 {
6432 arg = &value[i + 1];
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006433# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006434 arglen = vallen - i - 1;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006435# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006436 valend = i;
6437 break;
6438 }
6439 }
6440
6441 for (i = 0; command_complete[i].expand != 0; ++i)
6442 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006443 if ((int)STRLEN(command_complete[i].name) == valend
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006444 && STRNCMP(value, command_complete[i].name, valend) == 0)
6445 {
6446 *complp = command_complete[i].expand;
6447 if (command_complete[i].expand == EXPAND_BUFFERS)
6448 *argt |= BUFNAME;
6449 else if (command_complete[i].expand == EXPAND_DIRECTORIES
6450 || command_complete[i].expand == EXPAND_FILES)
6451 *argt |= XFILE;
6452 break;
6453 }
6454 }
6455
6456 if (command_complete[i].expand == 0)
6457 {
6458 EMSG2(_("E180: Invalid complete value: %s"), value);
6459 return FAIL;
6460 }
6461
6462# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6463 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST
6464 && arg != NULL)
6465# else
6466 if (arg != NULL)
6467# endif
6468 {
6469 EMSG(_("E468: Completion argument only allowed for custom completion"));
6470 return FAIL;
6471 }
6472
6473# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6474 if ((*complp == EXPAND_USER_DEFINED || *complp == EXPAND_USER_LIST)
6475 && arg == NULL)
6476 {
6477 EMSG(_("E467: Custom completion requires a function argument"));
6478 return FAIL;
6479 }
6480
6481 if (arg != NULL)
6482 *compl_arg = vim_strnsave(arg, (int)arglen);
6483# endif
6484 return OK;
6485}
6486#endif
6487
Bram Moolenaar071d4272004-06-13 20:20:40 +00006488 static void
6489ex_colorscheme(eap)
6490 exarg_T *eap;
6491{
Bram Moolenaare6850792010-05-14 15:28:44 +02006492 if (*eap->arg == NUL)
6493 {
6494#ifdef FEAT_EVAL
6495 char_u *expr = vim_strsave((char_u *)"g:colors_name");
6496 char_u *p = NULL;
6497
6498 if (expr != NULL)
6499 {
6500 ++emsg_off;
6501 p = eval_to_string(expr, NULL, FALSE);
6502 --emsg_off;
6503 vim_free(expr);
6504 }
6505 if (p != NULL)
6506 {
6507 MSG(p);
6508 vim_free(p);
6509 }
6510 else
6511 MSG("default");
6512#else
6513 MSG(_("unknown"));
6514#endif
6515 }
6516 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarbe1e9e92012-09-18 16:47:07 +02006517 EMSG2(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006518}
6519
6520 static void
6521ex_highlight(eap)
6522 exarg_T *eap;
6523{
6524 if (*eap->arg == NUL && eap->cmd[2] == '!')
6525 MSG(_("Greetings, Vim user!"));
6526 do_highlight(eap->arg, eap->forceit, FALSE);
6527}
6528
6529
6530/*
6531 * Call this function if we thought we were going to exit, but we won't
6532 * (because of an error). May need to restore the terminal mode.
6533 */
6534 void
6535not_exiting()
6536{
6537 exiting = FALSE;
6538 settmode(TMODE_RAW);
6539}
6540
6541/*
6542 * ":quit": quit current window, quit Vim if closed the last window.
6543 */
6544 static void
6545ex_quit(eap)
6546 exarg_T *eap;
6547{
6548#ifdef FEAT_CMDWIN
6549 if (cmdwin_type != 0)
6550 {
6551 cmdwin_result = Ctrl_C;
6552 return;
6553 }
6554#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006555 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006556 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006557 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006558 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006559 return;
6560 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006561#ifdef FEAT_AUTOCMD
Bram Moolenaar3b53dfb2012-06-06 18:03:07 +02006562 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01006563 /* Refuse to quit when locked or when the buffer in the last window is
Bram Moolenaar362ce482012-06-06 19:02:45 +02006564 * being closed (can only happen in autocommands). */
6565 if (curbuf_locked() || (curbuf->b_nwindows == 1 && curbuf->b_closing))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006566 return;
6567#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006568
6569#ifdef FEAT_NETBEANS_INTG
6570 netbeansForcedQuit = eap->forceit;
6571#endif
6572
6573 /*
6574 * If there are more files or windows we won't exit.
6575 */
6576 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6577 exiting = TRUE;
6578 if ((!P_HID(curbuf)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01006579 && check_changed(curbuf, (p_awa ? CCGD_AW : 0)
6580 | (eap->forceit ? CCGD_FORCEIT : 0)
6581 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006582 || check_more(TRUE, eap->forceit) == FAIL
6583 || (only_one_window() && check_changed_any(eap->forceit)))
6584 {
6585 not_exiting();
6586 }
6587 else
6588 {
6589#ifdef FEAT_WINDOWS
6590 if (only_one_window()) /* quit last window */
6591#endif
6592 getout(0);
6593#ifdef FEAT_WINDOWS
6594# ifdef FEAT_GUI
6595 need_mouse_correct = TRUE;
6596# endif
6597 /* close window; may free buffer */
6598 win_close(curwin, !P_HID(curwin->w_buffer) || eap->forceit);
6599#endif
6600 }
6601}
6602
6603/*
6604 * ":cquit".
6605 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006606 static void
6607ex_cquit(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006608 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006609{
6610 getout(1); /* this does not always pass on the exit code to the Manx
6611 compiler. why? */
6612}
6613
6614/*
6615 * ":qall": try to quit all windows
6616 */
6617 static void
6618ex_quit_all(eap)
6619 exarg_T *eap;
6620{
6621# ifdef FEAT_CMDWIN
6622 if (cmdwin_type != 0)
6623 {
6624 if (eap->forceit)
6625 cmdwin_result = K_XF1; /* ex_window() takes care of this */
6626 else
6627 cmdwin_result = K_XF2;
6628 return;
6629 }
6630# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006631
6632 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006633 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006634 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006635 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006636 return;
6637 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006638#ifdef FEAT_AUTOCMD
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01006639 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
6640 /* Refuse to quit when locked or when the buffer in the last window is
6641 * being closed (can only happen in autocommands). */
6642 if (curbuf_locked() || (curbuf->b_nwindows == 1 && curbuf->b_closing))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006643 return;
6644#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006645
Bram Moolenaar071d4272004-06-13 20:20:40 +00006646 exiting = TRUE;
6647 if (eap->forceit || !check_changed_any(FALSE))
6648 getout(0);
6649 not_exiting();
6650}
6651
Bram Moolenaard9967712006-03-11 21:18:15 +00006652#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006653/*
6654 * ":close": close current window, unless it is the last one
6655 */
6656 static void
6657ex_close(eap)
6658 exarg_T *eap;
6659{
6660# ifdef FEAT_CMDWIN
6661 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02006662 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006663 else
6664# endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006665 if (!text_locked()
6666#ifdef FEAT_AUTOCMD
6667 && !curbuf_locked()
6668#endif
6669 )
Bram Moolenaarf740b292006-02-16 22:11:02 +00006670 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006671}
6672
Bram Moolenaard9967712006-03-11 21:18:15 +00006673# ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006674/*
6675 * ":pclose": Close any preview window.
6676 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006677 static void
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006678ex_pclose(eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006679 exarg_T *eap;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006680{
6681 win_T *win;
6682
6683 for (win = firstwin; win != NULL; win = win->w_next)
6684 if (win->w_p_pvw)
6685 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006686 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006687 break;
6688 }
6689}
Bram Moolenaard9967712006-03-11 21:18:15 +00006690# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006691
Bram Moolenaarf740b292006-02-16 22:11:02 +00006692/*
6693 * Close window "win" and take care of handling closing the last window for a
6694 * modified buffer.
6695 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006696 static void
Bram Moolenaarf740b292006-02-16 22:11:02 +00006697ex_win_close(forceit, win, tp)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006698 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006699 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006700 tabpage_T *tp; /* NULL or the tab page "win" is in */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006701{
6702 int need_hide;
6703 buf_T *buf = win->w_buffer;
6704
6705 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006706 if (need_hide && !P_HID(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006707 {
Bram Moolenaard9967712006-03-11 21:18:15 +00006708# if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006709 if ((p_confirm || cmdmod.confirm) && p_write)
6710 {
6711 dialog_changed(buf, FALSE);
6712 if (buf_valid(buf) && bufIsChanged(buf))
6713 return;
6714 need_hide = FALSE;
6715 }
6716 else
Bram Moolenaard9967712006-03-11 21:18:15 +00006717# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006718 {
6719 EMSG(_(e_nowrtmsg));
6720 return;
6721 }
6722 }
6723
Bram Moolenaard9967712006-03-11 21:18:15 +00006724# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006725 need_mouse_correct = TRUE;
Bram Moolenaard9967712006-03-11 21:18:15 +00006726# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006727
Bram Moolenaar071d4272004-06-13 20:20:40 +00006728 /* free buffer when not hiding it or when it's a scratch buffer */
Bram Moolenaarf740b292006-02-16 22:11:02 +00006729 if (tp == NULL)
6730 win_close(win, !need_hide && !P_HID(buf));
6731 else
6732 win_close_othertab(win, !need_hide && !P_HID(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006733}
6734
Bram Moolenaar071d4272004-06-13 20:20:40 +00006735/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006736 * ":tabclose": close current tab page, unless it is the last one.
6737 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006738 */
6739 static void
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006740ex_tabclose(eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006741 exarg_T *eap;
6742{
Bram Moolenaarf740b292006-02-16 22:11:02 +00006743 tabpage_T *tp;
6744
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006745# ifdef FEAT_CMDWIN
6746 if (cmdwin_type != 0)
6747 cmdwin_result = K_IGNORE;
6748 else
6749# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006750 if (first_tabpage->tp_next == NULL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00006751 EMSG(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00006752 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006753 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006754 if (eap->addr_count > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006755 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006756 tp = find_tabpage((int)eap->line2);
6757 if (tp == NULL)
6758 {
6759 beep_flush();
6760 return;
6761 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00006762 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006763 {
6764 tabpage_close_other(tp, eap->forceit);
6765 return;
6766 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006767 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006768 if (!text_locked()
6769#ifdef FEAT_AUTOCMD
6770 && !curbuf_locked()
6771#endif
6772 )
Bram Moolenaarf740b292006-02-16 22:11:02 +00006773 tabpage_close(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006774 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006775}
6776
6777/*
6778 * ":tabonly": close all tab pages except the current one
6779 */
6780 static void
6781ex_tabonly(eap)
6782 exarg_T *eap;
6783{
6784 tabpage_T *tp;
6785 int done;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006786
6787# ifdef FEAT_CMDWIN
6788 if (cmdwin_type != 0)
6789 cmdwin_result = K_IGNORE;
6790 else
6791# endif
6792 if (first_tabpage->tp_next == NULL)
6793 MSG(_("Already only one tab page"));
6794 else
6795 {
6796 /* Repeat this up to a 1000 times, because autocommands may mess
6797 * up the lists. */
6798 for (done = 0; done < 1000; ++done)
6799 {
6800 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6801 if (tp->tp_topframe != topframe)
6802 {
6803 tabpage_close_other(tp, eap->forceit);
6804 /* if we failed to close it quit */
6805 if (valid_tabpage(tp))
6806 done = 1000;
6807 /* start over, "tp" is now invalid */
6808 break;
6809 }
6810 if (first_tabpage->tp_next == NULL)
6811 break;
6812 }
6813 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006814}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006815
6816/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006817 * Close the current tab page.
6818 */
6819 void
6820tabpage_close(forceit)
6821 int forceit;
6822{
6823 /* First close all the windows but the current one. If that worked then
6824 * close the last window in this tab, that will close it. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006825 if (lastwin != firstwin)
6826 close_others(TRUE, forceit);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006827 if (lastwin == firstwin)
6828 ex_win_close(forceit, curwin, NULL);
6829# ifdef FEAT_GUI
6830 need_mouse_correct = TRUE;
6831# endif
6832}
6833
6834/*
6835 * Close tab page "tp", which is not the current tab page.
6836 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006837 * Also takes care of the tab pages line disappearing when closing the
6838 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00006839 */
6840 void
6841tabpage_close_other(tp, forceit)
6842 tabpage_T *tp;
6843 int forceit;
6844{
6845 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006846 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006847 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006848
6849 /* Limit to 1000 windows, autocommands may add a window while we close
6850 * one. OK, so I'm paranoid... */
6851 while (++done < 1000)
6852 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006853 wp = tp->tp_firstwin;
6854 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006855
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006856 /* Autocommands may delete the tab page under our fingers and we may
6857 * fail to close a window with a modified buffer. */
6858 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006859 break;
6860 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006861
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006862 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006863 if (h != tabline_height())
6864 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006865}
6866
6867/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006868 * ":only".
6869 */
6870 static void
6871ex_only(eap)
6872 exarg_T *eap;
6873{
6874# ifdef FEAT_GUI
6875 need_mouse_correct = TRUE;
6876# endif
6877 close_others(TRUE, eap->forceit);
6878}
6879
6880/*
6881 * ":all" and ":sall".
Bram Moolenaard9967712006-03-11 21:18:15 +00006882 * Also used for ":tab drop file ..." after setting the argument list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006883 */
Bram Moolenaard9967712006-03-11 21:18:15 +00006884 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00006885ex_all(eap)
6886 exarg_T *eap;
6887{
6888 if (eap->addr_count == 0)
6889 eap->line2 = 9999;
Bram Moolenaard9967712006-03-11 21:18:15 +00006890 do_arg_all((int)eap->line2, eap->forceit, eap->cmdidx == CMD_drop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006891}
6892#endif /* FEAT_WINDOWS */
6893
6894 static void
6895ex_hide(eap)
6896 exarg_T *eap;
6897{
6898 if (*eap->arg != NUL && check_nextcmd(eap->arg) == NULL)
6899 eap->errmsg = e_invarg;
6900 else
6901 {
6902 /* ":hide" or ":hide | cmd": hide current window */
6903 eap->nextcmd = check_nextcmd(eap->arg);
6904#ifdef FEAT_WINDOWS
6905 if (!eap->skip)
6906 {
6907# ifdef FEAT_GUI
6908 need_mouse_correct = TRUE;
6909# endif
6910 win_close(curwin, FALSE); /* don't free buffer */
6911 }
6912#endif
6913 }
6914}
6915
6916/*
6917 * ":stop" and ":suspend": Suspend Vim.
6918 */
6919 static void
6920ex_stop(eap)
6921 exarg_T *eap;
6922{
6923 /*
6924 * Disallow suspending for "rvim".
6925 */
6926 if (!check_restricted()
6927#ifdef WIN3264
6928 /*
6929 * Check if external commands are allowed now.
6930 */
6931 && can_end_termcap_mode(TRUE)
6932#endif
6933 )
6934 {
6935 if (!eap->forceit)
6936 autowrite_all();
6937 windgoto((int)Rows - 1, 0);
6938 out_char('\n');
6939 out_flush();
6940 stoptermcap();
6941 out_flush(); /* needed for SUN to restore xterm buffer */
6942#ifdef FEAT_TITLE
6943 mch_restore_title(3); /* restore window titles */
6944#endif
6945 ui_suspend(); /* call machine specific function */
6946#ifdef FEAT_TITLE
6947 maketitle();
6948 resettitle(); /* force updating the title */
6949#endif
6950 starttermcap();
6951 scroll_start(); /* scroll screen before redrawing */
6952 redraw_later_clear();
6953 shell_resized(); /* may have resized window */
6954 }
6955}
6956
6957/*
6958 * ":exit", ":xit" and ":wq": Write file and exit Vim.
6959 */
6960 static void
6961ex_exit(eap)
6962 exarg_T *eap;
6963{
6964#ifdef FEAT_CMDWIN
6965 if (cmdwin_type != 0)
6966 {
6967 cmdwin_result = Ctrl_C;
6968 return;
6969 }
6970#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006971 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006972 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006973 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006974 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006975 return;
6976 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006977#ifdef FEAT_AUTOCMD
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01006978 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
6979 /* Refuse to quit when locked or when the buffer in the last window is
6980 * being closed (can only happen in autocommands). */
6981 if (curbuf_locked() || (curbuf->b_nwindows == 1 && curbuf->b_closing))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006982 return;
6983#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006984
6985 /*
6986 * if more files or windows we won't exit
6987 */
6988 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6989 exiting = TRUE;
6990 if ( ((eap->cmdidx == CMD_wq
6991 || curbufIsChanged())
6992 && do_write(eap) == FAIL)
6993 || check_more(TRUE, eap->forceit) == FAIL
6994 || (only_one_window() && check_changed_any(eap->forceit)))
6995 {
6996 not_exiting();
6997 }
6998 else
6999 {
7000#ifdef FEAT_WINDOWS
7001 if (only_one_window()) /* quit last window, exit Vim */
7002#endif
7003 getout(0);
7004#ifdef FEAT_WINDOWS
7005# ifdef FEAT_GUI
7006 need_mouse_correct = TRUE;
7007# endif
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02007008 /* Quit current window, may free the buffer. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007009 win_close(curwin, !P_HID(curwin->w_buffer));
7010#endif
7011 }
7012}
7013
7014/*
7015 * ":print", ":list", ":number".
7016 */
7017 static void
7018ex_print(eap)
7019 exarg_T *eap;
7020{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007021 if (curbuf->b_ml.ml_flags & ML_EMPTY)
7022 EMSG(_(e_emptybuf));
7023 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007024 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007025 for ( ;!got_int; ui_breakcheck())
7026 {
7027 print_line(eap->line1,
7028 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
7029 || (eap->flags & EXFLAG_NR)),
7030 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
7031 if (++eap->line1 > eap->line2)
7032 break;
7033 out_flush(); /* show one line at a time */
7034 }
7035 setpcmark();
7036 /* put cursor at last line */
7037 curwin->w_cursor.lnum = eap->line2;
7038 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007039 }
7040
Bram Moolenaar071d4272004-06-13 20:20:40 +00007041 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007042}
7043
7044#ifdef FEAT_BYTEOFF
7045 static void
7046ex_goto(eap)
7047 exarg_T *eap;
7048{
7049 goto_byte(eap->line2);
7050}
7051#endif
7052
7053/*
7054 * ":shell".
7055 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007056 static void
7057ex_shell(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007058 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007059{
7060 do_shell(NULL, 0);
7061}
7062
7063#if (defined(FEAT_WINDOWS) && defined(HAVE_DROP_FILE)) \
7064 || (defined(FEAT_GUI_GTK) && defined(FEAT_DND)) \
Bram Moolenaar371baa92005-12-29 22:43:53 +00007065 || defined(FEAT_GUI_MSWIN) \
7066 || defined(FEAT_GUI_MAC) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00007067 || defined(PROTO)
7068
7069/*
7070 * Handle a file drop. The code is here because a drop is *nearly* like an
7071 * :args command, but not quite (we have a list of exact filenames, so we
7072 * don't want to (a) parse a command line, or (b) expand wildcards. So the
7073 * code is very similar to :args and hence needs access to a lot of the static
7074 * functions in this file.
7075 *
7076 * The list should be allocated using alloc(), as should each item in the
7077 * list. This function takes over responsibility for freeing the list.
7078 *
Bram Moolenaar81870892007-11-11 18:17:28 +00007079 * XXX The list is made into the argument list. This is freed using
Bram Moolenaar071d4272004-06-13 20:20:40 +00007080 * FreeWild(), which does a series of vim_free() calls, unless the two defines
7081 * __EMX__ and __ALWAYS_HAS_TRAILING_NUL_POINTER are set. In this case, a
7082 * routine _fnexplodefree() is used. This may cause problems, but as the drop
7083 * file functionality is (currently) not in EMX this is not presently a
7084 * problem.
7085 */
7086 void
7087handle_drop(filec, filev, split)
7088 int filec; /* the number of files dropped */
7089 char_u **filev; /* the list of files dropped */
7090 int split; /* force splitting the window */
7091{
7092 exarg_T ea;
7093 int save_msg_scroll = msg_scroll;
7094
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007095 /* Postpone this while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007096 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007097 return;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007098#ifdef FEAT_AUTOCMD
7099 if (curbuf_locked())
7100 return;
7101#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00007102 /* When the screen is being updated we should not change buffers and
7103 * windows structures, it may cause freed memory to be used. */
7104 if (updating_screen)
7105 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007106
7107 /* Check whether the current buffer is changed. If so, we will need
7108 * to split the current window or data could be lost.
7109 * We don't need to check if the 'hidden' option is set, as in this
7110 * case the buffer won't be lost.
7111 */
7112 if (!P_HID(curbuf) && !split)
7113 {
7114 ++emsg_off;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01007115 split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007116 --emsg_off;
7117 }
7118 if (split)
7119 {
7120# ifdef FEAT_WINDOWS
7121 if (win_split(0, 0) == FAIL)
7122 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007123 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007124
7125 /* When splitting the window, create a new alist. Otherwise the
7126 * existing one is overwritten. */
7127 alist_unlink(curwin->w_alist);
7128 alist_new();
7129# else
7130 return; /* can't split, always fail */
7131# endif
7132 }
7133
7134 /*
7135 * Set up the new argument list.
7136 */
Bram Moolenaar86b68352004-12-27 21:59:20 +00007137 alist_set(ALIST(curwin), filec, filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007138
7139 /*
7140 * Move to the first file.
7141 */
7142 /* Fake up a minimal "next" command for do_argfile() */
7143 vim_memset(&ea, 0, sizeof(ea));
7144 ea.cmd = (char_u *)"next";
7145 do_argfile(&ea, 0);
7146
7147 /* do_ecmd() may set need_start_insertmode, but since we never left Insert
7148 * mode that is not needed here. */
7149 need_start_insertmode = FALSE;
7150
7151 /* Restore msg_scroll, otherwise a following command may cause scrolling
7152 * unexpectedly. The screen will be redrawn by the caller, thus
7153 * msg_scroll being set by displaying a message is irrelevant. */
7154 msg_scroll = save_msg_scroll;
7155}
7156#endif
7157
Bram Moolenaar071d4272004-06-13 20:20:40 +00007158/*
7159 * Clear an argument list: free all file names and reset it to zero entries.
7160 */
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007161 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00007162alist_clear(al)
7163 alist_T *al;
7164{
7165 while (--al->al_ga.ga_len >= 0)
7166 vim_free(AARGLIST(al)[al->al_ga.ga_len].ae_fname);
7167 ga_clear(&al->al_ga);
7168}
7169
7170/*
7171 * Init an argument list.
7172 */
7173 void
7174alist_init(al)
7175 alist_T *al;
7176{
7177 ga_init2(&al->al_ga, (int)sizeof(aentry_T), 5);
7178}
7179
7180#if defined(FEAT_WINDOWS) || defined(PROTO)
7181
7182/*
7183 * Remove a reference from an argument list.
7184 * Ignored when the argument list is the global one.
7185 * If the argument list is no longer used by any window, free it.
7186 */
7187 void
7188alist_unlink(al)
7189 alist_T *al;
7190{
7191 if (al != &global_alist && --al->al_refcount <= 0)
7192 {
7193 alist_clear(al);
7194 vim_free(al);
7195 }
7196}
7197
7198# if defined(FEAT_LISTCMDS) || defined(HAVE_DROP_FILE) || defined(PROTO)
7199/*
7200 * Create a new argument list and use it for the current window.
7201 */
7202 void
7203alist_new()
7204{
7205 curwin->w_alist = (alist_T *)alloc((unsigned)sizeof(alist_T));
7206 if (curwin->w_alist == NULL)
7207 {
7208 curwin->w_alist = &global_alist;
7209 ++global_alist.al_refcount;
7210 }
7211 else
7212 {
7213 curwin->w_alist->al_refcount = 1;
7214 alist_init(curwin->w_alist);
7215 }
7216}
7217# endif
7218#endif
7219
7220#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE) || defined(PROTO)
7221/*
7222 * Expand the file names in the global argument list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00007223 * If "fnum_list" is not NULL, use "fnum_list[fnum_len]" as a list of buffer
7224 * numbers to be re-used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007225 */
7226 void
Bram Moolenaar86b68352004-12-27 21:59:20 +00007227alist_expand(fnum_list, fnum_len)
7228 int *fnum_list;
7229 int fnum_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007230{
7231 char_u **old_arg_files;
Bram Moolenaar86b68352004-12-27 21:59:20 +00007232 int old_arg_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007233 char_u **new_arg_files;
7234 int new_arg_file_count;
7235 char_u *save_p_su = p_su;
7236 int i;
7237
7238 /* Don't use 'suffixes' here. This should work like the shell did the
7239 * expansion. Also, the vimrc file isn't read yet, thus the user
7240 * can't set the options. */
7241 p_su = empty_option;
7242 old_arg_files = (char_u **)alloc((unsigned)(sizeof(char_u *) * GARGCOUNT));
7243 if (old_arg_files != NULL)
7244 {
7245 for (i = 0; i < GARGCOUNT; ++i)
Bram Moolenaar86b68352004-12-27 21:59:20 +00007246 old_arg_files[i] = vim_strsave(GARGLIST[i].ae_fname);
7247 old_arg_count = GARGCOUNT;
7248 if (expand_wildcards(old_arg_count, old_arg_files,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007249 &new_arg_file_count, &new_arg_files,
Bram Moolenaarb5609832011-07-20 15:04:58 +02007250 EW_FILE|EW_NOTFOUND|EW_ADDSLASH|EW_NOERROR) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007251 && new_arg_file_count > 0)
7252 {
Bram Moolenaar86b68352004-12-27 21:59:20 +00007253 alist_set(&global_alist, new_arg_file_count, new_arg_files,
7254 TRUE, fnum_list, fnum_len);
7255 FreeWild(old_arg_count, old_arg_files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007256 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007257 }
7258 p_su = save_p_su;
7259}
7260#endif
7261
7262/*
7263 * Set the argument list for the current window.
7264 * Takes over the allocated files[] and the allocated fnames in it.
7265 */
7266 void
Bram Moolenaar86b68352004-12-27 21:59:20 +00007267alist_set(al, count, files, use_curbuf, fnum_list, fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007268 alist_T *al;
7269 int count;
7270 char_u **files;
7271 int use_curbuf;
Bram Moolenaar86b68352004-12-27 21:59:20 +00007272 int *fnum_list;
7273 int fnum_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007274{
7275 int i;
7276
7277 alist_clear(al);
7278 if (ga_grow(&al->al_ga, count) == OK)
7279 {
7280 for (i = 0; i < count; ++i)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007281 {
7282 if (got_int)
7283 {
7284 /* When adding many buffers this can take a long time. Allow
7285 * interrupting here. */
7286 while (i < count)
7287 vim_free(files[i++]);
7288 break;
7289 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00007290
7291 /* May set buffer name of a buffer previously used for the
7292 * argument list, so that it's re-used by alist_add. */
7293 if (fnum_list != NULL && i < fnum_len)
7294 buf_set_name(fnum_list[i], files[i]);
7295
Bram Moolenaar071d4272004-06-13 20:20:40 +00007296 alist_add(al, files[i], use_curbuf ? 2 : 1);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007297 ui_breakcheck();
7298 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007299 vim_free(files);
7300 }
7301 else
7302 FreeWild(count, files);
7303#ifdef FEAT_WINDOWS
7304 if (al == &global_alist)
7305#endif
7306 arg_had_last = FALSE;
7307}
7308
7309/*
7310 * Add file "fname" to argument list "al".
7311 * "fname" must have been allocated and "al" must have been checked for room.
7312 */
7313 void
7314alist_add(al, fname, set_fnum)
7315 alist_T *al;
7316 char_u *fname;
7317 int set_fnum; /* 1: set buffer number; 2: re-use curbuf */
7318{
7319 if (fname == NULL) /* don't add NULL file names */
7320 return;
7321#ifdef BACKSLASH_IN_FILENAME
7322 slash_adjust(fname);
7323#endif
7324 AARGLIST(al)[al->al_ga.ga_len].ae_fname = fname;
7325 if (set_fnum > 0)
7326 AARGLIST(al)[al->al_ga.ga_len].ae_fnum =
7327 buflist_add(fname, BLN_LISTED | (set_fnum == 2 ? BLN_CURBUF : 0));
7328 ++al->al_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007329}
7330
7331#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
7332/*
7333 * Adjust slashes in file names. Called after 'shellslash' was set.
7334 */
7335 void
7336alist_slash_adjust()
7337{
7338 int i;
7339# ifdef FEAT_WINDOWS
7340 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007341 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007342# endif
7343
7344 for (i = 0; i < GARGCOUNT; ++i)
7345 if (GARGLIST[i].ae_fname != NULL)
7346 slash_adjust(GARGLIST[i].ae_fname);
7347# ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00007348 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007349 if (wp->w_alist != &global_alist)
7350 for (i = 0; i < WARGCOUNT(wp); ++i)
7351 if (WARGLIST(wp)[i].ae_fname != NULL)
7352 slash_adjust(WARGLIST(wp)[i].ae_fname);
7353# endif
7354}
7355#endif
7356
7357/*
7358 * ":preserve".
7359 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007360 static void
7361ex_preserve(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007362 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007363{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007364 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007365 ml_preserve(curbuf, TRUE);
7366}
7367
7368/*
7369 * ":recover".
7370 */
7371 static void
7372ex_recover(eap)
7373 exarg_T *eap;
7374{
7375 /* Set recoverymode right away to avoid the ATTENTION prompt. */
7376 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01007377 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
7378 | CCGD_MULTWIN
7379 | (eap->forceit ? CCGD_FORCEIT : 0)
7380 | CCGD_EXCMD)
7381
Bram Moolenaar071d4272004-06-13 20:20:40 +00007382 && (*eap->arg == NUL
7383 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
7384 ml_recover();
7385 recoverymode = FALSE;
7386}
7387
7388/*
7389 * Command modifier used in a wrong way.
7390 */
7391 static void
7392ex_wrongmodifier(eap)
7393 exarg_T *eap;
7394{
7395 eap->errmsg = e_invcmd;
7396}
7397
7398#ifdef FEAT_WINDOWS
7399/*
7400 * :sview [+command] file split window with new file, read-only
7401 * :split [[+command] file] split window with current or new file
7402 * :vsplit [[+command] file] split window vertically with current or new file
7403 * :new [[+command] file] split window with no or new file
7404 * :vnew [[+command] file] split vertically window with no or new file
7405 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007406 *
7407 * :tabedit open new Tab page with empty window
7408 * :tabedit [+command] file open new Tab page and edit "file"
7409 * :tabnew [[+command] file] just like :tabedit
7410 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007411 */
7412 void
7413ex_splitview(eap)
7414 exarg_T *eap;
7415{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007416 win_T *old_curwin = curwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007417# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007418 char_u *fname = NULL;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007419# endif
7420# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007421 int browse_flag = cmdmod.browse;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007422# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007423
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007424# ifndef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007425 if (eap->cmdidx == CMD_vsplit || eap->cmdidx == CMD_vnew)
7426 {
7427 ex_ni(eap);
7428 return;
7429 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007430# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007431
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007432# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007433 need_mouse_correct = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007434# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007435
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007436# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00007437 /* A ":split" in the quickfix window works like ":new". Don't want two
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007438 * quickfix windows. But it's OK when doing ":tab split". */
7439 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007440 {
7441 if (eap->cmdidx == CMD_split)
7442 eap->cmdidx = CMD_new;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007443# ifdef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007444 if (eap->cmdidx == CMD_vsplit)
7445 eap->cmdidx = CMD_vnew;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007446# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007447 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007448# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007449
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007450# ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007451 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007452 {
7453 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
7454 FNAME_MESS, TRUE, curbuf->b_ffname);
7455 if (fname == NULL)
7456 goto theend;
7457 eap->arg = fname;
7458 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007459# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007460 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007461# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007462# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007463# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007464 if (cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007465# ifdef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007466 && eap->cmdidx != CMD_vnew
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007467# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007468 && eap->cmdidx != CMD_new)
7469 {
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007470# ifdef FEAT_AUTOCMD
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007471 if (
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007472# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007473 !gui.in_use &&
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007474# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007475 au_has_group((char_u *)"FileExplorer"))
7476 {
7477 /* No browsing supported but we do have the file explorer:
7478 * Edit the directory. */
7479 if (*eap->arg == NUL || !mch_isdir(eap->arg))
7480 eap->arg = (char_u *)".";
7481 }
7482 else
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007483# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007484 {
7485 fname = do_browse(0, (char_u *)_("Edit File in new window"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007486 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007487 if (fname == NULL)
7488 goto theend;
7489 eap->arg = fname;
7490 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007491 }
7492 cmdmod.browse = FALSE; /* Don't browse again in do_ecmd(). */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007493# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007494
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007495 /*
7496 * Either open new tab page or split the window.
7497 */
7498 if (eap->cmdidx == CMD_tabedit
7499 || eap->cmdidx == CMD_tabfind
7500 || eap->cmdidx == CMD_tabnew)
7501 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00007502 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
7503 : eap->addr_count == 0 ? 0
7504 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007505 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00007506 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007507
7508 /* set the alternate buffer for the window we came from */
7509 if (curwin != old_curwin
7510 && win_valid(old_curwin)
7511 && old_curwin->w_buffer != curbuf
7512 && !cmdmod.keepalt)
7513 old_curwin->w_alt_fnum = curbuf->b_fnum;
7514 }
7515 }
7516 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007517 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
7518 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007519# ifdef FEAT_SCROLLBIND
Bram Moolenaar071d4272004-06-13 20:20:40 +00007520 /* Reset 'scrollbind' when editing another file, but keep it when
7521 * doing ":split" without arguments. */
7522 if (*eap->arg != NUL
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007523# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007524 || cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007525# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007526 )
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007527 {
7528 RESET_BINDING(curwin);
7529 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007530 else
7531 do_check_scrollbind(FALSE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007532# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007533 do_exedit(eap, old_curwin);
7534 }
7535
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007536# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007537 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007538# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007539
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007540# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007541theend:
7542 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007543# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007544}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007545
7546/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007547 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007548 */
7549 void
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007550tabpage_new()
7551{
7552 exarg_T ea;
7553
7554 vim_memset(&ea, 0, sizeof(ea));
7555 ea.cmdidx = CMD_tabnew;
7556 ea.cmd = (char_u *)"tabn";
7557 ea.arg = (char_u *)"";
7558 ex_splitview(&ea);
7559}
7560
7561/*
7562 * :tabnext command
7563 */
7564 static void
7565ex_tabnext(eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007566 exarg_T *eap;
7567{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007568 switch (eap->cmdidx)
7569 {
7570 case CMD_tabfirst:
7571 case CMD_tabrewind:
7572 goto_tabpage(1);
7573 break;
7574 case CMD_tablast:
7575 goto_tabpage(9999);
7576 break;
7577 case CMD_tabprevious:
7578 case CMD_tabNext:
7579 goto_tabpage(eap->addr_count == 0 ? -1 : -(int)eap->line2);
7580 break;
7581 default: /* CMD_tabnext */
7582 goto_tabpage(eap->addr_count == 0 ? 0 : (int)eap->line2);
7583 break;
7584 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007585}
7586
7587/*
7588 * :tabmove command
7589 */
7590 static void
7591ex_tabmove(eap)
7592 exarg_T *eap;
7593{
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02007594 int tab_number = 9999;
7595
7596 if (eap->arg && *eap->arg != NUL)
7597 {
7598 char_u *p = eap->arg;
7599 int relative = 0; /* argument +N/-N means: move N places to the
7600 * right/left relative to the current position. */
7601
7602 if (*eap->arg == '-')
7603 {
7604 relative = -1;
7605 p = eap->arg + 1;
7606 }
7607 else if (*eap->arg == '+')
7608 {
7609 relative = 1;
7610 p = eap->arg + 1;
7611 }
7612 else
7613 p = eap->arg;
7614
7615 if (p == skipdigits(p))
7616 {
7617 /* No numbers as argument. */
7618 eap->errmsg = e_invarg;
7619 return;
7620 }
7621
7622 tab_number = getdigits(&p);
7623 if (relative != 0)
7624 tab_number = tab_number * relative + tabpage_index(curtab) - 1;;
7625 }
7626 else if (eap->addr_count != 0)
7627 tab_number = eap->line2;
7628
7629 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007630}
7631
7632/*
7633 * :tabs command: List tabs and their contents.
7634 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007635 static void
7636ex_tabs(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007637 exarg_T *eap UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007638{
7639 tabpage_T *tp;
7640 win_T *wp;
7641 int tabcount = 1;
7642
7643 msg_start();
7644 msg_scroll = TRUE;
7645 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
7646 {
7647 msg_putchar('\n');
7648 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
7649 msg_outtrans_attr(IObuff, hl_attr(HLF_T));
7650 out_flush(); /* output one line at a time */
7651 ui_breakcheck();
7652
Bram Moolenaar030f0df2006-02-21 22:02:53 +00007653 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007654 wp = firstwin;
7655 else
7656 wp = tp->tp_firstwin;
7657 for ( ; wp != NULL && !got_int; wp = wp->w_next)
7658 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007659 msg_putchar('\n');
7660 msg_putchar(wp == curwin ? '>' : ' ');
7661 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007662 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
7663 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007664 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02007665 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007666 else
7667 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
7668 IObuff, IOSIZE, TRUE);
7669 msg_outtrans(IObuff);
7670 out_flush(); /* output one line at a time */
7671 ui_breakcheck();
7672 }
7673 }
7674}
7675
7676#endif /* FEAT_WINDOWS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007677
7678/*
7679 * ":mode": Set screen mode.
7680 * If no argument given, just get the screen size and redraw.
7681 */
7682 static void
7683ex_mode(eap)
7684 exarg_T *eap;
7685{
7686 if (*eap->arg == NUL)
7687 shell_resized();
7688 else
7689 mch_screenmode(eap->arg);
7690}
7691
7692#ifdef FEAT_WINDOWS
7693/*
7694 * ":resize".
7695 * set, increment or decrement current window height
7696 */
7697 static void
7698ex_resize(eap)
7699 exarg_T *eap;
7700{
7701 int n;
7702 win_T *wp = curwin;
7703
7704 if (eap->addr_count > 0)
7705 {
7706 n = eap->line2;
7707 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
7708 ;
7709 }
7710
7711#ifdef FEAT_GUI
7712 need_mouse_correct = TRUE;
7713#endif
7714 n = atol((char *)eap->arg);
7715#ifdef FEAT_VERTSPLIT
7716 if (cmdmod.split & WSP_VERT)
7717 {
7718 if (*eap->arg == '-' || *eap->arg == '+')
7719 n += W_WIDTH(curwin);
7720 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
7721 n = 9999;
7722 win_setwidth_win((int)n, wp);
7723 }
7724 else
7725#endif
7726 {
7727 if (*eap->arg == '-' || *eap->arg == '+')
7728 n += curwin->w_height;
7729 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
7730 n = 9999;
7731 win_setheight_win((int)n, wp);
7732 }
7733}
7734#endif
7735
7736/*
7737 * ":find [+command] <file>" command.
7738 */
7739 static void
7740ex_find(eap)
7741 exarg_T *eap;
7742{
7743#ifdef FEAT_SEARCHPATH
7744 char_u *fname;
7745 int count;
7746
7747 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
7748 TRUE, curbuf->b_ffname);
7749 if (eap->addr_count > 0)
7750 {
7751 /* Repeat finding the file "count" times. This matters when it
7752 * appears several times in the path. */
7753 count = eap->line2;
7754 while (fname != NULL && --count > 0)
7755 {
7756 vim_free(fname);
7757 fname = find_file_in_path(NULL, 0, FNAME_MESS,
7758 FALSE, curbuf->b_ffname);
7759 }
7760 }
7761
7762 if (fname != NULL)
7763 {
7764 eap->arg = fname;
7765#endif
7766 do_exedit(eap, NULL);
7767#ifdef FEAT_SEARCHPATH
7768 vim_free(fname);
7769 }
7770#endif
7771}
7772
7773/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00007774 * ":open" simulation: for now just work like ":visual".
7775 */
7776 static void
7777ex_open(eap)
7778 exarg_T *eap;
7779{
7780 regmatch_T regmatch;
7781 char_u *p;
7782
7783 curwin->w_cursor.lnum = eap->line2;
7784 beginline(BL_SOL | BL_FIX);
7785 if (*eap->arg == '/')
7786 {
7787 /* ":open /pattern/": put cursor in column found with pattern */
7788 ++eap->arg;
7789 p = skip_regexp(eap->arg, '/', p_magic, NULL);
7790 *p = NUL;
7791 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
7792 if (regmatch.regprog != NULL)
7793 {
7794 regmatch.rm_ic = p_ic;
7795 p = ml_get_curline();
7796 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007797 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007798 else
7799 EMSG(_(e_nomatch));
Bram Moolenaar473de612013-06-08 18:19:48 +02007800 vim_regfree(regmatch.regprog);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007801 }
7802 /* Move to the NUL, ignore any other arguments. */
7803 eap->arg += STRLEN(eap->arg);
7804 }
7805 check_cursor();
7806
7807 eap->cmdidx = CMD_visual;
7808 do_exedit(eap, NULL);
7809}
7810
7811/*
7812 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007813 */
7814 static void
7815ex_edit(eap)
7816 exarg_T *eap;
7817{
7818 do_exedit(eap, NULL);
7819}
7820
7821/*
7822 * ":edit <file>" command and alikes.
7823 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007824 void
7825do_exedit(eap, old_curwin)
7826 exarg_T *eap;
7827 win_T *old_curwin; /* curwin before doing a split or NULL */
7828{
7829 int n;
7830#ifdef FEAT_WINDOWS
7831 int need_hide;
7832#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00007833 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007834
7835 /*
7836 * ":vi" command ends Ex mode.
7837 */
7838 if (exmode_active && (eap->cmdidx == CMD_visual
7839 || eap->cmdidx == CMD_view))
7840 {
7841 exmode_active = FALSE;
7842 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007843 {
7844 /* Special case: ":global/pat/visual\NLvi-commands" */
7845 if (global_busy)
7846 {
7847 int rd = RedrawingDisabled;
7848 int nwr = no_wait_return;
7849 int ms = msg_scroll;
7850#ifdef FEAT_GUI
7851 int he = hold_gui_events;
7852#endif
7853
7854 if (eap->nextcmd != NULL)
7855 {
7856 stuffReadbuff(eap->nextcmd);
7857 eap->nextcmd = NULL;
7858 }
7859
7860 if (exmode_was != EXMODE_VIM)
7861 settmode(TMODE_RAW);
7862 RedrawingDisabled = 0;
7863 no_wait_return = 0;
7864 need_wait_return = FALSE;
7865 msg_scroll = 0;
7866#ifdef FEAT_GUI
7867 hold_gui_events = 0;
7868#endif
7869 must_redraw = CLEAR;
7870
7871 main_loop(FALSE, TRUE);
7872
7873 RedrawingDisabled = rd;
7874 no_wait_return = nwr;
7875 msg_scroll = ms;
7876#ifdef FEAT_GUI
7877 hold_gui_events = he;
7878#endif
7879 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007880 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007881 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007882 }
7883
7884 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007885 || eap->cmdidx == CMD_tabnew
7886 || eap->cmdidx == CMD_tabedit
Bram Moolenaar071d4272004-06-13 20:20:40 +00007887#ifdef FEAT_VERTSPLIT
7888 || eap->cmdidx == CMD_vnew
7889#endif
7890 ) && *eap->arg == NUL)
7891 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007892 /* ":new" or ":tabnew" without argument: edit an new empty buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007893 setpcmark();
7894 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007895 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
7896 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007897 }
7898 else if ((eap->cmdidx != CMD_split
7899#ifdef FEAT_VERTSPLIT
7900 && eap->cmdidx != CMD_vsplit
7901#endif
7902 )
7903 || *eap->arg != NUL
7904#ifdef FEAT_BROWSE
7905 || cmdmod.browse
7906#endif
7907 )
7908 {
Bram Moolenaar5555acc2006-04-07 21:33:12 +00007909#ifdef FEAT_AUTOCMD
7910 /* Can't edit another file when "curbuf_lock" is set. Only ":edit"
7911 * can bring us here, others are stopped earlier. */
7912 if (*eap->arg != NUL && curbuf_locked())
7913 return;
7914#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007915 n = readonlymode;
7916 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
7917 readonlymode = TRUE;
7918 else if (eap->cmdidx == CMD_enew)
7919 readonlymode = FALSE; /* 'readonly' doesn't make sense in an
7920 empty buffer */
7921 setpcmark();
7922 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
7923 NULL, eap,
7924 /* ":edit" goes to first line if Vi compatible */
7925 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
7926 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
7927 ? ECMD_ONE : eap->do_ecmd_lnum,
7928 (P_HID(curbuf) ? ECMD_HIDE : 0)
7929 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar7b449342014-03-25 13:03:48 +01007930 /* after a split we can use an existing buffer */
7931 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007932#ifdef FEAT_LISTCMDS
7933 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
7934#endif
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007935 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007936 {
7937 /* Editing the file failed. If the window was split, close it. */
7938#ifdef FEAT_WINDOWS
7939 if (old_curwin != NULL)
7940 {
7941 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
7942 if (!need_hide || P_HID(curbuf))
7943 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007944# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7945 cleanup_T cs;
7946
7947 /* Reset the error/interrupt/exception state here so that
7948 * aborting() returns FALSE when closing a window. */
7949 enter_cleanup(&cs);
7950# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007951# ifdef FEAT_GUI
7952 need_mouse_correct = TRUE;
7953# endif
7954 win_close(curwin, !need_hide && !P_HID(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007955
7956# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7957 /* Restore the error/interrupt/exception state if not
7958 * discarded by a new aborting error, interrupt, or
7959 * uncaught exception. */
7960 leave_cleanup(&cs);
7961# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007962 }
7963 }
7964#endif
7965 }
7966 else if (readonlymode && curbuf->b_nwindows == 1)
7967 {
7968 /* When editing an already visited buffer, 'readonly' won't be set
7969 * but the previous value is kept. With ":view" and ":sview" we
7970 * want the file to be readonly, except when another window is
7971 * editing the same buffer. */
7972 curbuf->b_p_ro = TRUE;
7973 }
7974 readonlymode = n;
7975 }
7976 else
7977 {
7978 if (eap->do_ecmd_cmd != NULL)
7979 do_cmdline_cmd(eap->do_ecmd_cmd);
7980#ifdef FEAT_TITLE
7981 n = curwin->w_arg_idx_invalid;
7982#endif
7983 check_arg_idx(curwin);
7984#ifdef FEAT_TITLE
7985 if (n != curwin->w_arg_idx_invalid)
7986 maketitle();
7987#endif
7988 }
7989
7990#ifdef FEAT_WINDOWS
7991 /*
7992 * if ":split file" worked, set alternate file name in old window to new
7993 * file
7994 */
7995 if (old_curwin != NULL
7996 && *eap->arg != NUL
7997 && curwin != old_curwin
7998 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00007999 && old_curwin->w_buffer != curbuf
8000 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008001 old_curwin->w_alt_fnum = curbuf->b_fnum;
8002#endif
8003
8004 ex_no_reprint = TRUE;
8005}
8006
8007#ifndef FEAT_GUI
8008/*
8009 * ":gui" and ":gvim" when there is no GUI.
8010 */
8011 static void
8012ex_nogui(eap)
8013 exarg_T *eap;
8014{
8015 eap->errmsg = e_nogvim;
8016}
8017#endif
8018
8019#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
8020 static void
8021ex_tearoff(eap)
8022 exarg_T *eap;
8023{
8024 gui_make_tearoff(eap->arg);
8025}
8026#endif
8027
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008028#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008029 static void
8030ex_popup(eap)
8031 exarg_T *eap;
8032{
Bram Moolenaar97409f12005-07-08 22:17:29 +00008033 gui_make_popup(eap->arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008034}
8035#endif
8036
Bram Moolenaar071d4272004-06-13 20:20:40 +00008037 static void
8038ex_swapname(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008039 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008040{
8041 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
8042 MSG(_("No swap file"));
8043 else
8044 msg(curbuf->b_ml.ml_mfp->mf_fname);
8045}
8046
8047/*
8048 * ":syncbind" forces all 'scrollbind' windows to have the same relative
8049 * offset.
8050 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
8051 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008052 static void
8053ex_syncbind(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008054 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008055{
8056#ifdef FEAT_SCROLLBIND
8057 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008058 win_T *save_curwin = curwin;
8059 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008060 long topline;
8061 long y;
8062 linenr_T old_linenr = curwin->w_cursor.lnum;
8063
8064 setpcmark();
8065
8066 /*
8067 * determine max topline
8068 */
8069 if (curwin->w_p_scb)
8070 {
8071 topline = curwin->w_topline;
8072 for (wp = firstwin; wp; wp = wp->w_next)
8073 {
8074 if (wp->w_p_scb && wp->w_buffer)
8075 {
8076 y = wp->w_buffer->b_ml.ml_line_count - p_so;
8077 if (topline > y)
8078 topline = y;
8079 }
8080 }
8081 if (topline < 1)
8082 topline = 1;
8083 }
8084 else
8085 {
8086 topline = 1;
8087 }
8088
8089
8090 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008091 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008092 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008093 for (curwin = firstwin; curwin; curwin = curwin->w_next)
8094 {
8095 if (curwin->w_p_scb)
8096 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008097 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008098 y = topline - curwin->w_topline;
8099 if (y > 0)
8100 scrollup(y, TRUE);
8101 else
8102 scrolldown(-y, TRUE);
8103 curwin->w_scbind_pos = topline;
8104 redraw_later(VALID);
8105 cursor_correct();
8106#ifdef FEAT_WINDOWS
8107 curwin->w_redr_status = TRUE;
8108#endif
8109 }
8110 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008111 curwin = save_curwin;
8112 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008113 if (curwin->w_p_scb)
8114 {
8115 did_syncbind = TRUE;
8116 checkpcmark();
8117 if (old_linenr != curwin->w_cursor.lnum)
8118 {
8119 char_u ctrl_o[2];
8120
8121 ctrl_o[0] = Ctrl_O;
8122 ctrl_o[1] = 0;
8123 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
8124 }
8125 }
8126#endif
8127}
8128
8129
8130 static void
8131ex_read(eap)
8132 exarg_T *eap;
8133{
Bram Moolenaardf177f62005-02-22 08:39:57 +00008134 int i;
8135 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
8136 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008137
8138 if (eap->usefilter) /* :r!cmd */
8139 do_bang(1, eap, FALSE, FALSE, TRUE);
8140 else
8141 {
8142 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
8143 return;
8144
8145#ifdef FEAT_BROWSE
8146 if (cmdmod.browse)
8147 {
8148 char_u *browseFile;
8149
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008150 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008151 NULL, NULL, NULL, curbuf);
8152 if (browseFile != NULL)
8153 {
8154 i = readfile(browseFile, NULL,
8155 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8156 vim_free(browseFile);
8157 }
8158 else
8159 i = OK;
8160 }
8161 else
8162#endif
8163 if (*eap->arg == NUL)
8164 {
8165 if (check_fname() == FAIL) /* check for no file name */
8166 return;
8167 i = readfile(curbuf->b_ffname, curbuf->b_fname,
8168 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8169 }
8170 else
8171 {
8172 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
8173 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
8174 i = readfile(eap->arg, NULL,
8175 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8176
8177 }
8178 if (i == FAIL)
8179 {
8180#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
8181 if (!aborting())
8182#endif
8183 EMSG2(_(e_notopen), eap->arg);
8184 }
8185 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008186 {
8187 if (empty && exmode_active)
8188 {
8189 /* Delete the empty line that remains. Historically ex does
8190 * this but vi doesn't. */
8191 if (eap->line2 == 0)
8192 lnum = curbuf->b_ml.ml_line_count;
8193 else
8194 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008195 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008196 {
8197 ml_delete(lnum, FALSE);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008198 if (curwin->w_cursor.lnum > 1
8199 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008200 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00008201 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008202 }
8203 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008204 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008205 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008206 }
8207}
8208
Bram Moolenaarea408852005-06-25 22:49:46 +00008209static char_u *prev_dir = NULL;
8210
8211#if defined(EXITFREE) || defined(PROTO)
8212 void
8213free_cd_dir()
8214{
8215 vim_free(prev_dir);
Bram Moolenaara0174af2008-01-02 20:08:25 +00008216 prev_dir = NULL;
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00008217
8218 vim_free(globaldir);
8219 globaldir = NULL;
Bram Moolenaarea408852005-06-25 22:49:46 +00008220}
8221#endif
8222
Bram Moolenaarf4258302013-06-02 18:20:17 +02008223/*
8224 * Deal with the side effects of changing the current directory.
8225 * When "local" is TRUE then this was after an ":lcd" command.
8226 */
8227 void
8228post_chdir(local)
8229 int local;
8230{
8231 vim_free(curwin->w_localdir);
Bram Moolenaarbd2dc342014-01-10 15:53:13 +01008232 curwin->w_localdir = NULL;
Bram Moolenaarf4258302013-06-02 18:20:17 +02008233 if (local)
8234 {
8235 /* If still in global directory, need to remember current
8236 * directory as global directory. */
8237 if (globaldir == NULL && prev_dir != NULL)
8238 globaldir = vim_strsave(prev_dir);
8239 /* Remember this local directory for the window. */
8240 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8241 curwin->w_localdir = vim_strsave(NameBuff);
8242 }
8243 else
8244 {
8245 /* We are now in the global directory, no need to remember its
8246 * name. */
8247 vim_free(globaldir);
8248 globaldir = NULL;
Bram Moolenaarf4258302013-06-02 18:20:17 +02008249 }
8250
8251 shorten_fnames(TRUE);
8252}
8253
Bram Moolenaarea408852005-06-25 22:49:46 +00008254
Bram Moolenaar071d4272004-06-13 20:20:40 +00008255/*
8256 * ":cd", ":lcd", ":chdir" and ":lchdir".
8257 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00008258 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00008259ex_cd(eap)
8260 exarg_T *eap;
8261{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008262 char_u *new_dir;
8263 char_u *tofree;
8264
8265 new_dir = eap->arg;
8266#if !defined(UNIX) && !defined(VMS)
8267 /* for non-UNIX ":cd" means: print current directory */
8268 if (*new_dir == NUL)
8269 ex_pwd(NULL);
8270 else
8271#endif
8272 {
Bram Moolenaar8e8fe9b2009-03-11 14:37:32 +00008273#ifdef FEAT_AUTOCMD
8274 if (allbuf_locked())
8275 return;
8276#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00008277 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged()
8278 && !eap->forceit)
8279 {
Bram Moolenaar81870892007-11-11 18:17:28 +00008280 EMSG(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00008281 return;
8282 }
8283
Bram Moolenaar071d4272004-06-13 20:20:40 +00008284 /* ":cd -": Change to previous directory */
8285 if (STRCMP(new_dir, "-") == 0)
8286 {
8287 if (prev_dir == NULL)
8288 {
8289 EMSG(_("E186: No previous directory"));
8290 return;
8291 }
8292 new_dir = prev_dir;
8293 }
8294
8295 /* Save current directory for next ":cd -" */
8296 tofree = prev_dir;
8297 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8298 prev_dir = vim_strsave(NameBuff);
8299 else
8300 prev_dir = NULL;
8301
8302#if defined(UNIX) || defined(VMS)
8303 /* for UNIX ":cd" means: go to home directory */
8304 if (*new_dir == NUL)
8305 {
8306 /* use NameBuff for home directory name */
8307# ifdef VMS
8308 char_u *p;
8309
8310 p = mch_getenv((char_u *)"SYS$LOGIN");
8311 if (p == NULL || *p == NUL) /* empty is the same as not set */
8312 NameBuff[0] = NUL;
8313 else
Bram Moolenaarb6356332005-07-18 21:40:44 +00008314 vim_strncpy(NameBuff, p, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008315# else
8316 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
8317# endif
8318 new_dir = NameBuff;
8319 }
8320#endif
8321 if (new_dir == NULL || vim_chdir(new_dir))
8322 EMSG(_(e_failed));
8323 else
8324 {
Bram Moolenaarf4258302013-06-02 18:20:17 +02008325 post_chdir(eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008326
8327 /* Echo the new current directory if the command was typed. */
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00008328 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008329 ex_pwd(eap);
8330 }
8331 vim_free(tofree);
8332 }
8333}
8334
8335/*
8336 * ":pwd".
8337 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008338 static void
8339ex_pwd(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008340 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008341{
8342 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8343 {
8344#ifdef BACKSLASH_IN_FILENAME
8345 slash_adjust(NameBuff);
8346#endif
8347 msg(NameBuff);
8348 }
8349 else
8350 EMSG(_("E187: Unknown"));
8351}
8352
8353/*
8354 * ":=".
8355 */
8356 static void
8357ex_equal(eap)
8358 exarg_T *eap;
8359{
8360 smsg((char_u *)"%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008361 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008362}
8363
8364 static void
8365ex_sleep(eap)
8366 exarg_T *eap;
8367{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008368 int n;
8369 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008370
8371 if (cursor_valid())
8372 {
8373 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
8374 if (n >= 0)
Bram Moolenaar10395d82014-02-05 22:46:52 +01008375 windgoto((int)n, W_WINCOL(curwin) + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008376 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008377
8378 len = eap->line2;
8379 switch (*eap->arg)
8380 {
8381 case 'm': break;
8382 case NUL: len *= 1000L; break;
8383 default: EMSG2(_(e_invarg2), eap->arg); return;
8384 }
8385 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008386}
8387
8388/*
8389 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
8390 */
8391 void
8392do_sleep(msec)
8393 long msec;
8394{
8395 long done;
8396
8397 cursor_on();
8398 out_flush();
8399 for (done = 0; !got_int && done < msec; done += 1000L)
8400 {
8401 ui_delay(msec - done > 1000L ? 1000L : msec - done, TRUE);
8402 ui_breakcheck();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02008403#ifdef FEAT_NETBEANS_INTG
8404 /* Process the netbeans messages that may have been received in the
8405 * call to ui_breakcheck() when the GUI is in use. This may occur when
8406 * running a test case. */
8407 netbeans_parse_messages();
8408#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008409 }
8410}
8411
8412 static void
8413do_exmap(eap, isabbrev)
8414 exarg_T *eap;
8415 int isabbrev;
8416{
8417 int mode;
8418 char_u *cmdp;
8419
8420 cmdp = eap->cmd;
8421 mode = get_map_mode(&cmdp, eap->forceit || isabbrev);
8422
8423 switch (do_map((*cmdp == 'n') ? 2 : (*cmdp == 'u'),
8424 eap->arg, mode, isabbrev))
8425 {
8426 case 1: EMSG(_(e_invarg));
8427 break;
8428 case 2: EMSG(isabbrev ? _(e_noabbr) : _(e_nomap));
8429 break;
8430 }
8431}
8432
8433/*
8434 * ":winsize" command (obsolete).
8435 */
8436 static void
8437ex_winsize(eap)
8438 exarg_T *eap;
8439{
8440 int w, h;
8441 char_u *arg = eap->arg;
8442 char_u *p;
8443
8444 w = getdigits(&arg);
8445 arg = skipwhite(arg);
8446 p = arg;
8447 h = getdigits(&arg);
8448 if (*p != NUL && *arg == NUL)
8449 set_shellsize(w, h, TRUE);
8450 else
8451 EMSG(_("E465: :winsize requires two number arguments"));
8452}
8453
8454#ifdef FEAT_WINDOWS
8455 static void
8456ex_wincmd(eap)
8457 exarg_T *eap;
8458{
8459 int xchar = NUL;
8460 char_u *p;
8461
8462 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
8463 {
8464 /* CTRL-W g and CTRL-W CTRL-G have an extra command character */
8465 if (eap->arg[1] == NUL)
8466 {
8467 EMSG(_(e_invarg));
8468 return;
8469 }
8470 xchar = eap->arg[1];
8471 p = eap->arg + 2;
8472 }
8473 else
8474 p = eap->arg + 1;
8475
8476 eap->nextcmd = check_nextcmd(p);
8477 p = skipwhite(p);
8478 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
8479 EMSG(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02008480 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008481 {
8482 /* Pass flags on for ":vertical wincmd ]". */
8483 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008484 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008485 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
8486 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008487 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008488 }
8489}
8490#endif
8491
Bram Moolenaar843ee412004-06-30 16:16:41 +00008492#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008493/*
8494 * ":winpos".
8495 */
8496 static void
8497ex_winpos(eap)
8498 exarg_T *eap;
8499{
8500 int x, y;
8501 char_u *arg = eap->arg;
8502 char_u *p;
8503
8504 if (*arg == NUL)
8505 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00008506# if defined(FEAT_GUI) || defined(MSWIN)
8507# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008508 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00008509# else
8510 if (mch_get_winpos(&x, &y) != FAIL)
8511# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008512 {
8513 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
8514 msg(IObuff);
8515 }
8516 else
8517# endif
8518 EMSG(_("E188: Obtaining window position not implemented for this platform"));
8519 }
8520 else
8521 {
8522 x = getdigits(&arg);
8523 arg = skipwhite(arg);
8524 p = arg;
8525 y = getdigits(&arg);
8526 if (*p == NUL || *arg != NUL)
8527 {
8528 EMSG(_("E466: :winpos requires two number arguments"));
8529 return;
8530 }
8531# ifdef FEAT_GUI
8532 if (gui.in_use)
8533 gui_mch_set_winpos(x, y);
8534 else if (gui.starting)
8535 {
8536 /* Remember the coordinates for when the window is opened. */
8537 gui_win_x = x;
8538 gui_win_y = y;
8539 }
8540# ifdef HAVE_TGETENT
8541 else
8542# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00008543# else
8544# ifdef MSWIN
8545 mch_set_winpos(x, y);
8546# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008547# endif
8548# ifdef HAVE_TGETENT
8549 if (*T_CWP)
8550 term_set_winpos(x, y);
8551# endif
8552 }
8553}
8554#endif
8555
8556/*
8557 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
8558 */
8559 static void
8560ex_operators(eap)
8561 exarg_T *eap;
8562{
8563 oparg_T oa;
8564
8565 clear_oparg(&oa);
8566 oa.regname = eap->regname;
8567 oa.start.lnum = eap->line1;
8568 oa.end.lnum = eap->line2;
8569 oa.line_count = eap->line2 - eap->line1 + 1;
8570 oa.motion_type = MLINE;
8571#ifdef FEAT_VIRTUALEDIT
8572 virtual_op = FALSE;
8573#endif
8574 if (eap->cmdidx != CMD_yank) /* position cursor for undo */
8575 {
8576 setpcmark();
8577 curwin->w_cursor.lnum = eap->line1;
8578 beginline(BL_SOL | BL_FIX);
8579 }
8580
Bram Moolenaard07c6e12013-11-21 14:21:40 +01008581 if (VIsual_active)
8582 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01008583
Bram Moolenaar071d4272004-06-13 20:20:40 +00008584 switch (eap->cmdidx)
8585 {
8586 case CMD_delete:
8587 oa.op_type = OP_DELETE;
8588 op_delete(&oa);
8589 break;
8590
8591 case CMD_yank:
8592 oa.op_type = OP_YANK;
8593 (void)op_yank(&oa, FALSE, TRUE);
8594 break;
8595
8596 default: /* CMD_rshift or CMD_lshift */
Bram Moolenaar6e707362013-06-14 19:15:58 +02008597 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00008598#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02008599 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
8600#else
8601 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00008602#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02008603 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00008604 oa.op_type = OP_RSHIFT;
8605 else
8606 oa.op_type = OP_LSHIFT;
8607 op_shift(&oa, FALSE, eap->amount);
8608 break;
8609 }
8610#ifdef FEAT_VIRTUALEDIT
8611 virtual_op = MAYBE;
8612#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00008613 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008614}
8615
8616/*
8617 * ":put".
8618 */
8619 static void
8620ex_put(eap)
8621 exarg_T *eap;
8622{
8623 /* ":0put" works like ":1put!". */
8624 if (eap->line2 == 0)
8625 {
8626 eap->line2 = 1;
8627 eap->forceit = TRUE;
8628 }
8629 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008630 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
8631 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008632}
8633
8634/*
8635 * Handle ":copy" and ":move".
8636 */
8637 static void
8638ex_copymove(eap)
8639 exarg_T *eap;
8640{
8641 long n;
8642
8643 n = get_address(&eap->arg, FALSE, FALSE);
8644 if (eap->arg == NULL) /* error detected */
8645 {
8646 eap->nextcmd = NULL;
8647 return;
8648 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008649 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008650
8651 /*
8652 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
8653 */
8654 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
8655 {
8656 EMSG(_(e_invaddr));
8657 return;
8658 }
8659
8660 if (eap->cmdidx == CMD_move)
8661 {
8662 if (do_move(eap->line1, eap->line2, n) == FAIL)
8663 return;
8664 }
8665 else
8666 ex_copy(eap->line1, eap->line2, n);
8667 u_clearline();
8668 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008669 ex_may_print(eap);
8670}
8671
8672/*
8673 * Print the current line if flags were given to the Ex command.
8674 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02008675 void
Bram Moolenaardf177f62005-02-22 08:39:57 +00008676ex_may_print(eap)
8677 exarg_T *eap;
8678{
8679 if (eap->flags != 0)
8680 {
8681 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
8682 (eap->flags & EXFLAG_LIST));
8683 ex_no_reprint = TRUE;
8684 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008685}
8686
8687/*
8688 * ":smagic" and ":snomagic".
8689 */
8690 static void
8691ex_submagic(eap)
8692 exarg_T *eap;
8693{
8694 int magic_save = p_magic;
8695
8696 p_magic = (eap->cmdidx == CMD_smagic);
8697 do_sub(eap);
8698 p_magic = magic_save;
8699}
8700
8701/*
8702 * ":join".
8703 */
8704 static void
8705ex_join(eap)
8706 exarg_T *eap;
8707{
8708 curwin->w_cursor.lnum = eap->line1;
8709 if (eap->line1 == eap->line2)
8710 {
8711 if (eap->addr_count >= 2) /* :2,2join does nothing */
8712 return;
8713 if (eap->line2 == curbuf->b_ml.ml_line_count)
8714 {
8715 beep_flush();
8716 return;
8717 }
8718 ++eap->line2;
8719 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02008720 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008721 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008722 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008723}
8724
8725/*
8726 * ":[addr]@r" or ":[addr]*r": execute register
8727 */
8728 static void
8729ex_at(eap)
8730 exarg_T *eap;
8731{
8732 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00008733 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008734
8735 curwin->w_cursor.lnum = eap->line2;
8736
8737#ifdef USE_ON_FLY_SCROLL
8738 dont_scroll = TRUE; /* disallow scrolling here */
8739#endif
8740
8741 /* get the register name. No name means to use the previous one */
8742 c = *eap->arg;
8743 if (c == NUL || (c == '*' && *eap->cmd == '*'))
8744 c = '@';
Bram Moolenaard333d1e2006-11-07 17:43:47 +00008745 /* Put the register in the typeahead buffer with the "silent" flag. */
8746 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
8747 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008748 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008749 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00008750 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008751 else
8752 {
8753 int save_efr = exec_from_reg;
8754
8755 exec_from_reg = TRUE;
8756
8757 /*
8758 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00008759 * Continue until the stuff buffer is empty and all added characters
8760 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008761 */
Bram Moolenaar60462872009-11-03 11:40:19 +00008762 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008763 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
8764
8765 exec_from_reg = save_efr;
8766 }
8767}
8768
8769/*
8770 * ":!".
8771 */
8772 static void
8773ex_bang(eap)
8774 exarg_T *eap;
8775{
8776 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
8777}
8778
8779/*
8780 * ":undo".
8781 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008782 static void
8783ex_undo(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008784 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008785{
Bram Moolenaard3667a22006-03-16 21:35:52 +00008786 if (eap->addr_count == 1) /* :undo 123 */
Bram Moolenaar730cde92010-06-27 05:18:54 +02008787 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00008788 else
8789 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008790}
8791
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008792#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008793 static void
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008794ex_wundo(eap)
8795 exarg_T *eap;
8796{
8797 char_u hash[UNDO_HASH_SIZE];
8798
8799 u_compute_hash(hash);
8800 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
8801}
8802
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008803 static void
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008804ex_rundo(eap)
8805 exarg_T *eap;
8806{
8807 char_u hash[UNDO_HASH_SIZE];
8808
8809 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02008810 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008811}
8812#endif
8813
Bram Moolenaar071d4272004-06-13 20:20:40 +00008814/*
8815 * ":redo".
8816 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008817 static void
8818ex_redo(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008819 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008820{
8821 u_redo(1);
8822}
8823
8824/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008825 * ":earlier" and ":later".
8826 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008827 static void
8828ex_later(eap)
8829 exarg_T *eap;
8830{
8831 long count = 0;
8832 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008833 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008834 char_u *p = eap->arg;
8835
8836 if (*p == NUL)
8837 count = 1;
8838 else if (isdigit(*p))
8839 {
8840 count = getdigits(&p);
8841 switch (*p)
8842 {
8843 case 's': ++p; sec = TRUE; break;
8844 case 'm': ++p; sec = TRUE; count *= 60; break;
8845 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008846 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
8847 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008848 }
8849 }
8850
8851 if (*p != NUL)
8852 EMSG2(_(e_invarg2), eap->arg);
8853 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02008854 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
8855 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008856}
8857
8858/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008859 * ":redir": start/stop redirection.
8860 */
8861 static void
8862ex_redir(eap)
8863 exarg_T *eap;
8864{
8865 char *mode;
8866 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00008867 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008868
8869 if (STRICMP(eap->arg, "END") == 0)
8870 close_redir();
8871 else
8872 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008873 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008874 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008875 ++arg;
8876 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008877 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008878 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008879 mode = "a";
8880 }
8881 else
8882 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00008883 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008884
8885 close_redir();
8886
8887 /* Expand environment variables and "~/". */
Bram Moolenaarca472992005-01-21 11:46:23 +00008888 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008889 if (fname == NULL)
8890 return;
8891#ifdef FEAT_BROWSE
8892 if (cmdmod.browse)
8893 {
8894 char_u *browseFile;
8895
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008896 browseFile = do_browse(BROWSE_SAVE,
8897 (char_u *)_("Save Redirection"),
8898 fname, NULL, NULL, BROWSE_FILTER_ALL_FILES, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008899 if (browseFile == NULL)
8900 return; /* operation cancelled */
8901 vim_free(fname);
8902 fname = browseFile;
8903 eap->forceit = TRUE; /* since dialog already asked */
8904 }
8905#endif
8906
8907 redir_fd = open_exfile(fname, eap->forceit, mode);
8908 vim_free(fname);
8909 }
8910#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00008911 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008912 {
8913 /* redirect to a register a-z (resp. A-Z for appending) */
8914 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00008915 ++arg;
8916 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008917# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00008918 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00008919 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008920# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00008921 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008922 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008923 redir_reg = *arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008924 if (*arg == '>' && arg[1] == '>') /* append */
Bram Moolenaard9d30582005-05-18 22:10:28 +00008925 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008926 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008927 {
Bram Moolenaarc188b882007-10-19 14:20:54 +00008928 /* Can use both "@a" and "@a>". */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00008929 if (*arg == '>')
8930 arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008931 /* Make register empty when not using @A-@Z and the
8932 * command is valid. */
8933 if (*arg == NUL && !isupper(redir_reg))
8934 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008935 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008936 }
8937 if (*arg != NUL)
8938 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00008939 redir_reg = 0;
Bram Moolenaard9d30582005-05-18 22:10:28 +00008940 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008941 }
8942 }
8943 else if (*arg == '=' && arg[1] == '>')
8944 {
8945 int append;
8946
8947 /* redirect to a variable */
8948 close_redir();
8949 arg += 2;
8950
8951 if (*arg == '>')
8952 {
8953 ++arg;
8954 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008955 }
8956 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008957 append = FALSE;
8958
8959 if (var_redir_start(skipwhite(arg), append) == OK)
8960 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008961 }
8962#endif
8963
8964 /* TODO: redirect to a buffer */
8965
Bram Moolenaar071d4272004-06-13 20:20:40 +00008966 else
Bram Moolenaarca472992005-01-21 11:46:23 +00008967 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008968 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00008969
8970 /* Make sure redirection is not off. Can happen for cmdline completion
8971 * that indirectly invokes a command to catch its output. */
8972 if (redir_fd != NULL
8973#ifdef FEAT_EVAL
8974 || redir_reg || redir_vname
8975#endif
8976 )
8977 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008978}
8979
8980/*
8981 * ":redraw": force redraw
8982 */
8983 static void
8984ex_redraw(eap)
8985 exarg_T *eap;
8986{
8987 int r = RedrawingDisabled;
8988 int p = p_lz;
8989
8990 RedrawingDisabled = 0;
8991 p_lz = FALSE;
8992 update_topline();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008993 update_screen(eap->forceit ? CLEAR : VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008994#ifdef FEAT_TITLE
8995 if (need_maketitle)
8996 maketitle();
8997#endif
8998 RedrawingDisabled = r;
8999 p_lz = p;
9000
9001 /* Reset msg_didout, so that a message that's there is overwritten. */
9002 msg_didout = FALSE;
9003 msg_col = 0;
9004
Bram Moolenaar56667a52013-07-17 11:54:28 +02009005 /* No need to wait after an intentional redraw. */
9006 need_wait_return = FALSE;
9007
Bram Moolenaar071d4272004-06-13 20:20:40 +00009008 out_flush();
9009}
9010
9011/*
9012 * ":redrawstatus": force redraw of status line(s)
9013 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009014 static void
9015ex_redrawstatus(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009016 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009017{
9018#if defined(FEAT_WINDOWS)
9019 int r = RedrawingDisabled;
9020 int p = p_lz;
9021
9022 RedrawingDisabled = 0;
9023 p_lz = FALSE;
9024 if (eap->forceit)
9025 status_redraw_all();
9026 else
9027 status_redraw_curbuf();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009028 update_screen(VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009029 RedrawingDisabled = r;
9030 p_lz = p;
9031 out_flush();
9032#endif
9033}
9034
9035 static void
9036close_redir()
9037{
9038 if (redir_fd != NULL)
9039 {
9040 fclose(redir_fd);
9041 redir_fd = NULL;
9042 }
9043#ifdef FEAT_EVAL
9044 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009045 if (redir_vname)
9046 {
9047 var_redir_stop();
9048 redir_vname = 0;
9049 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009050#endif
9051}
9052
9053#if defined(FEAT_SESSION) && defined(USE_CRNL)
9054# define MKSESSION_NL
9055static int mksession_nl = FALSE; /* use NL only in put_eol() */
9056#endif
9057
9058/*
9059 * ":mkexrc", ":mkvimrc", ":mkview" and ":mksession".
9060 */
9061 static void
9062ex_mkrc(eap)
9063 exarg_T *eap;
9064{
9065 FILE *fd;
9066 int failed = FALSE;
9067 char_u *fname;
9068#ifdef FEAT_BROWSE
9069 char_u *browseFile = NULL;
9070#endif
9071#ifdef FEAT_SESSION
9072 int view_session = FALSE;
9073 int using_vdir = FALSE; /* using 'viewdir'? */
9074 char_u *viewFile = NULL;
9075 unsigned *flagp;
9076#endif
9077
9078 if (eap->cmdidx == CMD_mksession || eap->cmdidx == CMD_mkview)
9079 {
9080#ifdef FEAT_SESSION
9081 view_session = TRUE;
9082#else
9083 ex_ni(eap);
9084 return;
9085#endif
9086 }
9087
9088#ifdef FEAT_SESSION
Bram Moolenaar8d594672009-07-01 18:18:57 +00009089 /* Use the short file name until ":lcd" is used. We also don't use the
9090 * short file name when 'acd' is set, that is checked later. */
Bram Moolenaareeefcc72007-05-01 21:21:21 +00009091 did_lcd = FALSE;
9092
Bram Moolenaar071d4272004-06-13 20:20:40 +00009093 /* ":mkview" or ":mkview 9": generate file name with 'viewdir' */
9094 if (eap->cmdidx == CMD_mkview
9095 && (*eap->arg == NUL
9096 || (vim_isdigit(*eap->arg) && eap->arg[1] == NUL)))
9097 {
9098 eap->forceit = TRUE;
9099 fname = get_view_file(*eap->arg);
9100 if (fname == NULL)
9101 return;
9102 viewFile = fname;
9103 using_vdir = TRUE;
9104 }
9105 else
9106#endif
9107 if (*eap->arg != NUL)
9108 fname = eap->arg;
9109 else if (eap->cmdidx == CMD_mkvimrc)
9110 fname = (char_u *)VIMRC_FILE;
9111#ifdef FEAT_SESSION
9112 else if (eap->cmdidx == CMD_mksession)
9113 fname = (char_u *)SESSION_FILE;
9114#endif
9115 else
9116 fname = (char_u *)EXRC_FILE;
9117
9118#ifdef FEAT_BROWSE
9119 if (cmdmod.browse)
9120 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00009121 browseFile = do_browse(BROWSE_SAVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009122# ifdef FEAT_SESSION
9123 eap->cmdidx == CMD_mkview ? (char_u *)_("Save View") :
9124 eap->cmdidx == CMD_mksession ? (char_u *)_("Save Session") :
9125# endif
9126 (char_u *)_("Save Setup"),
9127 fname, (char_u *)"vim", NULL, BROWSE_FILTER_MACROS, NULL);
9128 if (browseFile == NULL)
9129 goto theend;
9130 fname = browseFile;
9131 eap->forceit = TRUE; /* since dialog already asked */
9132 }
9133#endif
9134
9135#if defined(FEAT_SESSION) && defined(vim_mkdir)
9136 /* When using 'viewdir' may have to create the directory. */
9137 if (using_vdir && !mch_isdir(p_vdir))
Bram Moolenaardf177f62005-02-22 08:39:57 +00009138 vim_mkdir_emsg(p_vdir, 0755);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009139#endif
9140
9141 fd = open_exfile(fname, eap->forceit, WRITEBIN);
9142 if (fd != NULL)
9143 {
9144#ifdef FEAT_SESSION
9145 if (eap->cmdidx == CMD_mkview)
9146 flagp = &vop_flags;
9147 else
9148 flagp = &ssop_flags;
9149#endif
9150
9151#ifdef MKSESSION_NL
9152 /* "unix" in 'sessionoptions': use NL line separator */
9153 if (view_session && (*flagp & SSOP_UNIX))
9154 mksession_nl = TRUE;
9155#endif
9156
9157 /* Write the version command for :mkvimrc */
9158 if (eap->cmdidx == CMD_mkvimrc)
9159 (void)put_line(fd, "version 6.0");
9160
9161#ifdef FEAT_SESSION
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009162 if (eap->cmdidx == CMD_mksession)
9163 {
9164 if (put_line(fd, "let SessionLoad = 1") == FAIL)
9165 failed = TRUE;
9166 }
9167
Bram Moolenaar071d4272004-06-13 20:20:40 +00009168 if (eap->cmdidx != CMD_mkview)
9169#endif
9170 {
9171 /* Write setting 'compatible' first, because it has side effects.
9172 * For that same reason only do it when needed. */
9173 if (p_cp)
9174 (void)put_line(fd, "if !&cp | set cp | endif");
9175 else
9176 (void)put_line(fd, "if &cp | set nocp | endif");
9177 }
9178
9179#ifdef FEAT_SESSION
9180 if (!view_session
9181 || (eap->cmdidx == CMD_mksession
9182 && (*flagp & SSOP_OPTIONS)))
9183#endif
9184 failed |= (makemap(fd, NULL) == FAIL
9185 || makeset(fd, OPT_GLOBAL, FALSE) == FAIL);
9186
9187#ifdef FEAT_SESSION
9188 if (!failed && view_session)
9189 {
9190 if (put_line(fd, "let s:so_save = &so | let s:siso_save = &siso | set so=0 siso=0") == FAIL)
9191 failed = TRUE;
9192 if (eap->cmdidx == CMD_mksession)
9193 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02009194 char_u *dirnow; /* current directory */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009195
Bram Moolenaard9462e32011-04-11 21:35:11 +02009196 dirnow = alloc(MAXPATHL);
9197 if (dirnow == NULL)
9198 failed = TRUE;
9199 else
9200 {
9201 /*
9202 * Change to session file's dir.
9203 */
9204 if (mch_dirname(dirnow, MAXPATHL) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00009205 || mch_chdir((char *)dirnow) != 0)
Bram Moolenaard9462e32011-04-11 21:35:11 +02009206 *dirnow = NUL;
9207 if (*dirnow != NUL && (ssop_flags & SSOP_SESDIR))
9208 {
9209 if (vim_chdirfile(fname) == OK)
9210 shorten_fnames(TRUE);
9211 }
9212 else if (*dirnow != NUL
9213 && (ssop_flags & SSOP_CURDIR) && globaldir != NULL)
9214 {
9215 if (mch_chdir((char *)globaldir) == 0)
9216 shorten_fnames(TRUE);
9217 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009218
Bram Moolenaard9462e32011-04-11 21:35:11 +02009219 failed |= (makeopens(fd, dirnow) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009220
Bram Moolenaard9462e32011-04-11 21:35:11 +02009221 /* restore original dir */
9222 if (*dirnow != NUL && ((ssop_flags & SSOP_SESDIR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009223 || ((ssop_flags & SSOP_CURDIR) && globaldir != NULL)))
Bram Moolenaard9462e32011-04-11 21:35:11 +02009224 {
9225 if (mch_chdir((char *)dirnow) != 0)
9226 EMSG(_(e_prev_dir));
9227 shorten_fnames(TRUE);
9228 }
9229 vim_free(dirnow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009230 }
9231 }
9232 else
9233 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +00009234 failed |= (put_view(fd, curwin, !using_vdir, flagp,
9235 -1) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009236 }
9237 if (put_line(fd, "let &so = s:so_save | let &siso = s:siso_save")
9238 == FAIL)
9239 failed = TRUE;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009240 if (put_line(fd, "doautoall SessionLoadPost") == FAIL)
9241 failed = TRUE;
Bram Moolenaar4770d092006-01-12 23:22:24 +00009242 if (eap->cmdidx == CMD_mksession)
9243 {
9244 if (put_line(fd, "unlet SessionLoad") == FAIL)
9245 failed = TRUE;
9246 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009247 }
9248#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009249 if (put_line(fd, "\" vim: set ft=vim :") == FAIL)
9250 failed = TRUE;
9251
Bram Moolenaar071d4272004-06-13 20:20:40 +00009252 failed |= fclose(fd);
9253
9254 if (failed)
9255 EMSG(_(e_write));
9256#if defined(FEAT_EVAL) && defined(FEAT_SESSION)
9257 else if (eap->cmdidx == CMD_mksession)
9258 {
9259 /* successful session write - set this_session var */
Bram Moolenaard9462e32011-04-11 21:35:11 +02009260 char_u *tbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009261
Bram Moolenaard9462e32011-04-11 21:35:11 +02009262 tbuf = alloc(MAXPATHL);
9263 if (tbuf != NULL)
9264 {
9265 if (vim_FullName(fname, tbuf, MAXPATHL, FALSE) == OK)
9266 set_vim_var_string(VV_THIS_SESSION, tbuf, -1);
9267 vim_free(tbuf);
9268 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009269 }
9270#endif
9271#ifdef MKSESSION_NL
9272 mksession_nl = FALSE;
9273#endif
9274 }
9275
9276#ifdef FEAT_BROWSE
9277theend:
9278 vim_free(browseFile);
9279#endif
9280#ifdef FEAT_SESSION
9281 vim_free(viewFile);
9282#endif
9283}
9284
Bram Moolenaardf177f62005-02-22 08:39:57 +00009285#if ((defined(FEAT_SESSION) || defined(FEAT_EVAL)) && defined(vim_mkdir)) \
9286 || defined(PROTO)
9287 int
9288vim_mkdir_emsg(name, prot)
9289 char_u *name;
Bram Moolenaar78a15312009-05-15 19:33:18 +00009290 int prot UNUSED;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009291{
9292 if (vim_mkdir(name, prot) != 0)
9293 {
9294 EMSG2(_("E739: Cannot create directory: %s"), name);
9295 return FAIL;
9296 }
9297 return OK;
9298}
9299#endif
9300
Bram Moolenaar071d4272004-06-13 20:20:40 +00009301/*
9302 * Open a file for writing for an Ex command, with some checks.
9303 * Return file descriptor, or NULL on failure.
9304 */
9305 FILE *
9306open_exfile(fname, forceit, mode)
9307 char_u *fname;
9308 int forceit;
9309 char *mode; /* "w" for create new file or "a" for append */
9310{
9311 FILE *fd;
9312
9313#ifdef UNIX
9314 /* with Unix it is possible to open a directory */
9315 if (mch_isdir(fname))
9316 {
9317 EMSG2(_(e_isadir2), fname);
9318 return NULL;
9319 }
9320#endif
9321 if (!forceit && *mode != 'a' && vim_fexists(fname))
9322 {
9323 EMSG2(_("E189: \"%s\" exists (add ! to override)"), fname);
9324 return NULL;
9325 }
9326
9327 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
9328 EMSG2(_("E190: Cannot open \"%s\" for writing"), fname);
9329
9330 return fd;
9331}
9332
9333/*
9334 * ":mark" and ":k".
9335 */
9336 static void
9337ex_mark(eap)
9338 exarg_T *eap;
9339{
9340 pos_T pos;
9341
9342 if (*eap->arg == NUL) /* No argument? */
9343 EMSG(_(e_argreq));
9344 else if (eap->arg[1] != NUL) /* more than one character? */
9345 EMSG(_(e_trailing));
9346 else
9347 {
9348 pos = curwin->w_cursor; /* save curwin->w_cursor */
9349 curwin->w_cursor.lnum = eap->line2;
9350 beginline(BL_WHITE | BL_FIX);
9351 if (setmark(*eap->arg) == FAIL) /* set mark */
9352 EMSG(_("E191: Argument must be a letter or forward/backward quote"));
9353 curwin->w_cursor = pos; /* restore curwin->w_cursor */
9354 }
9355}
9356
9357/*
9358 * Update w_topline, w_leftcol and the cursor position.
9359 */
9360 void
9361update_topline_cursor()
9362{
9363 check_cursor(); /* put cursor on valid line */
9364 update_topline();
9365 if (!curwin->w_p_wrap)
9366 validate_cursor();
9367 update_curswant();
9368}
9369
9370#ifdef FEAT_EX_EXTRA
9371/*
9372 * ":normal[!] {commands}": Execute normal mode commands.
9373 */
9374 static void
9375ex_normal(eap)
9376 exarg_T *eap;
9377{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009378 int save_msg_scroll = msg_scroll;
9379 int save_restart_edit = restart_edit;
9380 int save_msg_didout = msg_didout;
9381 int save_State = State;
9382 tasave_T tabuf;
9383 int save_insertmode = p_im;
9384 int save_finish_op = finish_op;
Bram Moolenaar38084112008-07-26 14:05:07 +00009385 int save_opcount = opcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009386#ifdef FEAT_MBYTE
9387 char_u *arg = NULL;
9388 int l;
9389 char_u *p;
9390#endif
9391
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00009392 if (ex_normal_lock > 0)
9393 {
9394 EMSG(_(e_secure));
9395 return;
9396 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009397 if (ex_normal_busy >= p_mmd)
9398 {
9399 EMSG(_("E192: Recursive use of :normal too deep"));
9400 return;
9401 }
9402 ++ex_normal_busy;
9403
9404 msg_scroll = FALSE; /* no msg scrolling in Normal mode */
9405 restart_edit = 0; /* don't go to Insert mode */
9406 p_im = FALSE; /* don't use 'insertmode' */
9407
9408#ifdef FEAT_MBYTE
9409 /*
9410 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
9411 * this for the K_SPECIAL leading byte, otherwise special keys will not
9412 * work.
9413 */
9414 if (has_mbyte)
9415 {
9416 int len = 0;
9417
9418 /* Count the number of characters to be escaped. */
9419 for (p = eap->arg; *p != NUL; ++p)
9420 {
9421# ifdef FEAT_GUI
9422 if (*p == CSI) /* leadbyte CSI */
9423 len += 2;
9424# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009425 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009426 if (*++p == K_SPECIAL /* trailbyte K_SPECIAL or CSI */
9427# ifdef FEAT_GUI
9428 || *p == CSI
9429# endif
9430 )
9431 len += 2;
9432 }
9433 if (len > 0)
9434 {
9435 arg = alloc((unsigned)(STRLEN(eap->arg) + len + 1));
9436 if (arg != NULL)
9437 {
9438 len = 0;
9439 for (p = eap->arg; *p != NUL; ++p)
9440 {
9441 arg[len++] = *p;
9442# ifdef FEAT_GUI
9443 if (*p == CSI)
9444 {
9445 arg[len++] = KS_EXTRA;
9446 arg[len++] = (int)KE_CSI;
9447 }
9448# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009449 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009450 {
9451 arg[len++] = *++p;
9452 if (*p == K_SPECIAL)
9453 {
9454 arg[len++] = KS_SPECIAL;
9455 arg[len++] = KE_FILLER;
9456 }
9457# ifdef FEAT_GUI
9458 else if (*p == CSI)
9459 {
9460 arg[len++] = KS_EXTRA;
9461 arg[len++] = (int)KE_CSI;
9462 }
9463# endif
9464 }
9465 arg[len] = NUL;
9466 }
9467 }
9468 }
9469 }
9470#endif
9471
9472 /*
9473 * Save the current typeahead. This is required to allow using ":normal"
9474 * from an event handler and makes sure we don't hang when the argument
9475 * ends with half a command.
9476 */
9477 save_typeahead(&tabuf);
9478 if (tabuf.typebuf_valid)
9479 {
9480 /*
9481 * Repeat the :normal command for each line in the range. When no
9482 * range given, execute it just once, without positioning the cursor
9483 * first.
9484 */
9485 do
9486 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009487 if (eap->addr_count != 0)
9488 {
9489 curwin->w_cursor.lnum = eap->line1++;
9490 curwin->w_cursor.col = 0;
9491 }
9492
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009493 exec_normal_cmd(
Bram Moolenaar071d4272004-06-13 20:20:40 +00009494#ifdef FEAT_MBYTE
9495 arg != NULL ? arg :
9496#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009497 eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009498 }
9499 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
9500 }
9501
9502 /* Might not return to the main loop when in an event handler. */
9503 update_topline_cursor();
9504
9505 /* Restore the previous typeahead. */
9506 restore_typeahead(&tabuf);
9507
9508 --ex_normal_busy;
9509 msg_scroll = save_msg_scroll;
9510 restart_edit = save_restart_edit;
9511 p_im = save_insertmode;
9512 finish_op = save_finish_op;
Bram Moolenaar38084112008-07-26 14:05:07 +00009513 opcount = save_opcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009514 msg_didout |= save_msg_didout; /* don't reset msg_didout now */
9515
9516 /* Restore the state (needed when called from a function executed for
9517 * 'indentexpr'). */
9518 State = save_State;
9519#ifdef FEAT_MBYTE
9520 vim_free(arg);
9521#endif
9522}
9523
9524/*
Bram Moolenaar64969662005-12-14 21:59:55 +00009525 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009526 */
9527 static void
9528ex_startinsert(eap)
9529 exarg_T *eap;
9530{
Bram Moolenaarfd371682005-01-14 21:42:54 +00009531 if (eap->forceit)
9532 {
9533 coladvance((colnr_T)MAXCOL);
9534 curwin->w_curswant = MAXCOL;
9535 curwin->w_set_curswant = FALSE;
9536 }
9537
Bram Moolenaara40c5002005-01-09 21:16:21 +00009538 /* Ignore the command when already in Insert mode. Inserting an
9539 * expression register that invokes a function can do this. */
9540 if (State & INSERT)
9541 return;
9542
Bram Moolenaar64969662005-12-14 21:59:55 +00009543 if (eap->cmdidx == CMD_startinsert)
9544 restart_edit = 'a';
9545 else if (eap->cmdidx == CMD_startreplace)
9546 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00009547 else
Bram Moolenaar64969662005-12-14 21:59:55 +00009548 restart_edit = 'V';
9549
9550 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009551 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009552 if (eap->cmdidx == CMD_startinsert)
9553 restart_edit = 'i';
Bram Moolenaar071d4272004-06-13 20:20:40 +00009554 curwin->w_curswant = 0; /* avoid MAXCOL */
9555 }
9556}
9557
9558/*
9559 * ":stopinsert"
9560 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009561 static void
9562ex_stopinsert(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009563 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009564{
9565 restart_edit = 0;
9566 stop_insert_mode = TRUE;
9567}
9568#endif
9569
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009570#if defined(FEAT_EX_EXTRA) || defined(FEAT_MENU) || defined(PROTO)
9571/*
9572 * Execute normal mode command "cmd".
9573 * "remap" can be REMAP_NONE or REMAP_YES.
9574 */
9575 void
9576exec_normal_cmd(cmd, remap, silent)
9577 char_u *cmd;
9578 int remap;
9579 int silent;
9580{
9581 oparg_T oa;
9582
9583 /*
9584 * Stuff the argument into the typeahead buffer.
9585 * Execute normal_cmd() until there is no typeahead left.
9586 */
9587 clear_oparg(&oa);
9588 finish_op = FALSE;
9589 ins_typebuf(cmd, remap, 0, TRUE, silent);
9590 while ((!stuff_empty() || (!typebuf_typed() && typebuf.tb_len > 0))
9591 && !got_int)
9592 {
9593 update_topline_cursor();
Bram Moolenaar48ac02c2011-01-17 19:50:06 +01009594 normal_cmd(&oa, TRUE); /* execute a Normal mode cmd */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009595 }
9596}
9597#endif
9598
Bram Moolenaar071d4272004-06-13 20:20:40 +00009599#ifdef FEAT_FIND_ID
9600 static void
9601ex_checkpath(eap)
9602 exarg_T *eap;
9603{
9604 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
9605 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
9606 (linenr_T)1, (linenr_T)MAXLNUM);
9607}
9608
9609#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
9610/*
9611 * ":psearch"
9612 */
9613 static void
9614ex_psearch(eap)
9615 exarg_T *eap;
9616{
9617 g_do_tagpreview = p_pvh;
9618 ex_findpat(eap);
9619 g_do_tagpreview = 0;
9620}
9621#endif
9622
9623 static void
9624ex_findpat(eap)
9625 exarg_T *eap;
9626{
9627 int whole = TRUE;
9628 long n;
9629 char_u *p;
9630 int action;
9631
9632 switch (cmdnames[eap->cmdidx].cmd_name[2])
9633 {
9634 case 'e': /* ":psearch", ":isearch" and ":dsearch" */
9635 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
9636 action = ACTION_GOTO;
9637 else
9638 action = ACTION_SHOW;
9639 break;
9640 case 'i': /* ":ilist" and ":dlist" */
9641 action = ACTION_SHOW_ALL;
9642 break;
9643 case 'u': /* ":ijump" and ":djump" */
9644 action = ACTION_GOTO;
9645 break;
9646 default: /* ":isplit" and ":dsplit" */
9647 action = ACTION_SPLIT;
9648 break;
9649 }
9650
9651 n = 1;
9652 if (vim_isdigit(*eap->arg)) /* get count */
9653 {
9654 n = getdigits(&eap->arg);
9655 eap->arg = skipwhite(eap->arg);
9656 }
9657 if (*eap->arg == '/') /* Match regexp, not just whole words */
9658 {
9659 whole = FALSE;
9660 ++eap->arg;
9661 p = skip_regexp(eap->arg, '/', p_magic, NULL);
9662 if (*p)
9663 {
9664 *p++ = NUL;
9665 p = skipwhite(p);
9666
9667 /* Check for trailing illegal characters */
9668 if (!ends_excmd(*p))
9669 eap->errmsg = e_trailing;
9670 else
9671 eap->nextcmd = check_nextcmd(p);
9672 }
9673 }
9674 if (!eap->skip)
9675 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
9676 whole, !eap->forceit,
9677 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
9678 n, action, eap->line1, eap->line2);
9679}
9680#endif
9681
9682#ifdef FEAT_WINDOWS
9683
9684# ifdef FEAT_QUICKFIX
9685/*
9686 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
9687 */
9688 static void
9689ex_ptag(eap)
9690 exarg_T *eap;
9691{
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01009692 g_do_tagpreview = p_pvh; /* will be reset to 0 in ex_tag_cmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009693 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9694}
9695
9696/*
9697 * ":pedit"
9698 */
9699 static void
9700ex_pedit(eap)
9701 exarg_T *eap;
9702{
9703 win_T *curwin_save = curwin;
9704
9705 g_do_tagpreview = p_pvh;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00009706 prepare_tagpreview(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009707 keep_help_flag = curwin_save->w_buffer->b_help;
9708 do_exedit(eap, NULL);
9709 keep_help_flag = FALSE;
9710 if (curwin != curwin_save && win_valid(curwin_save))
9711 {
9712 /* Return cursor to where we were */
9713 validate_cursor();
9714 redraw_later(VALID);
9715 win_enter(curwin_save, TRUE);
9716 }
9717 g_do_tagpreview = 0;
9718}
9719# endif
9720
9721/*
9722 * ":stag", ":stselect" and ":stjump".
9723 */
9724 static void
9725ex_stag(eap)
9726 exarg_T *eap;
9727{
9728 postponed_split = -1;
9729 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009730 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009731 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9732 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009733 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009734}
9735#endif
9736
9737/*
9738 * ":tag", ":tselect", ":tjump", ":tnext", etc.
9739 */
9740 static void
9741ex_tag(eap)
9742 exarg_T *eap;
9743{
9744 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
9745}
9746
9747 static void
9748ex_tag_cmd(eap, name)
9749 exarg_T *eap;
9750 char_u *name;
9751{
9752 int cmd;
9753
9754 switch (name[1])
9755 {
9756 case 'j': cmd = DT_JUMP; /* ":tjump" */
9757 break;
9758 case 's': cmd = DT_SELECT; /* ":tselect" */
9759 break;
9760 case 'p': cmd = DT_PREV; /* ":tprevious" */
9761 break;
9762 case 'N': cmd = DT_PREV; /* ":tNext" */
9763 break;
9764 case 'n': cmd = DT_NEXT; /* ":tnext" */
9765 break;
9766 case 'o': cmd = DT_POP; /* ":pop" */
9767 break;
9768 case 'f': /* ":tfirst" */
9769 case 'r': cmd = DT_FIRST; /* ":trewind" */
9770 break;
9771 case 'l': cmd = DT_LAST; /* ":tlast" */
9772 break;
9773 default: /* ":tag" */
9774#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +00009775 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009776 {
9777 do_cstag(eap);
9778 return;
9779 }
9780#endif
9781 cmd = DT_TAG;
9782 break;
9783 }
9784
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00009785 if (name[0] == 'l')
9786 {
9787#ifndef FEAT_QUICKFIX
9788 ex_ni(eap);
9789 return;
9790#else
9791 cmd = DT_LTAG;
9792#endif
9793 }
9794
Bram Moolenaar071d4272004-06-13 20:20:40 +00009795 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
9796 eap->forceit, TRUE);
9797}
9798
9799/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009800 * Check "str" for starting with a special cmdline variable.
9801 * If found return one of the SPEC_ values and set "*usedlen" to the length of
9802 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
9803 */
9804 int
9805find_cmdline_var(src, usedlen)
9806 char_u *src;
9807 int *usedlen;
9808{
9809 int len;
9810 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +00009811 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009812 "%",
9813#define SPEC_PERC 0
9814 "#",
9815#define SPEC_HASH 1
9816 "<cword>", /* cursor word */
9817#define SPEC_CWORD 2
9818 "<cWORD>", /* cursor WORD */
9819#define SPEC_CCWORD 3
9820 "<cfile>", /* cursor path name */
9821#define SPEC_CFILE 4
9822 "<sfile>", /* ":so" file name */
9823#define SPEC_SFILE 5
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009824 "<slnum>", /* ":so" file line number */
9825#define SPEC_SLNUM 6
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009826#ifdef FEAT_AUTOCMD
9827 "<afile>", /* autocommand file name */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009828# define SPEC_AFILE 7
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009829 "<abuf>", /* autocommand buffer number */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009830# define SPEC_ABUF 8
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009831 "<amatch>", /* autocommand match name */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009832# define SPEC_AMATCH 9
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009833#endif
9834#ifdef FEAT_CLIENTSERVER
9835 "<client>"
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009836# ifdef FEAT_AUTOCMD
9837# define SPEC_CLIENT 10
9838# else
9839# define SPEC_CLIENT 7
9840# endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009841#endif
9842 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009843
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009844 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009845 {
9846 len = (int)STRLEN(spec_str[i]);
9847 if (STRNCMP(src, spec_str[i], len) == 0)
9848 {
9849 *usedlen = len;
9850 return i;
9851 }
9852 }
9853 return -1;
9854}
9855
9856/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009857 * Evaluate cmdline variables.
9858 *
9859 * change '%' to curbuf->b_ffname
9860 * '#' to curwin->w_altfile
9861 * '<cword>' to word under the cursor
9862 * '<cWORD>' to WORD under the cursor
9863 * '<cfile>' to path name under the cursor
9864 * '<sfile>' to sourced file name
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009865 * '<slnum>' to sourced file line number
Bram Moolenaar071d4272004-06-13 20:20:40 +00009866 * '<afile>' to file name for autocommand
9867 * '<abuf>' to buffer number for autocommand
9868 * '<amatch>' to matching name for autocommand
9869 *
9870 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
9871 * "" for error without a message) and NULL is returned.
9872 * Returns an allocated string if a valid match was found.
9873 * Returns NULL if no match was found. "usedlen" then still contains the
9874 * number of characters to skip.
9875 */
9876 char_u *
Bram Moolenaar63b92542007-03-27 14:57:09 +00009877eval_vars(src, srcstart, usedlen, lnump, errormsg, escaped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009878 char_u *src; /* pointer into commandline */
Bram Moolenaar63b92542007-03-27 14:57:09 +00009879 char_u *srcstart; /* beginning of valid memory for src */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009880 int *usedlen; /* characters after src that are used */
9881 linenr_T *lnump; /* line number for :e command, or NULL */
9882 char_u **errormsg; /* pointer to error message */
Bram Moolenaar63b92542007-03-27 14:57:09 +00009883 int *escaped; /* return value has escaped white space (can
9884 * be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009885{
9886 int i;
9887 char_u *s;
9888 char_u *result;
9889 char_u *resultbuf = NULL;
9890 int resultlen;
9891 buf_T *buf;
9892 int valid = VALID_HEAD + VALID_PATH; /* assume valid result */
9893 int spec_idx;
9894#ifdef FEAT_MODIFY_FNAME
9895 int skip_mod = FALSE;
9896#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009897 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009898
9899 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009900 if (escaped != NULL)
9901 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009902
9903 /*
9904 * Check if there is something to do.
9905 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009906 spec_idx = find_cmdline_var(src, usedlen);
9907 if (spec_idx < 0) /* no match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009908 {
9909 *usedlen = 1;
9910 return NULL;
9911 }
9912
9913 /*
9914 * Skip when preceded with a backslash "\%" and "\#".
9915 * Note: In "\\%" the % is also not recognized!
9916 */
9917 if (src > srcstart && src[-1] == '\\')
9918 {
9919 *usedlen = 0;
Bram Moolenaara7241f52008-06-24 20:39:31 +00009920 STRMOVE(src - 1, src); /* remove backslash */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009921 return NULL;
9922 }
9923
9924 /*
9925 * word or WORD under cursor
9926 */
9927 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD)
9928 {
9929 resultlen = find_ident_under_cursor(&result, spec_idx == SPEC_CWORD ?
9930 (FIND_IDENT|FIND_STRING) : FIND_STRING);
9931 if (resultlen == 0)
9932 {
9933 *errormsg = (char_u *)"";
9934 return NULL;
9935 }
9936 }
9937
9938 /*
9939 * '#': Alternate file name
9940 * '%': Current file name
9941 * File name under the cursor
9942 * File name for autocommand
9943 * and following modifiers
9944 */
9945 else
9946 {
9947 switch (spec_idx)
9948 {
9949 case SPEC_PERC: /* '%': current file */
9950 if (curbuf->b_fname == NULL)
9951 {
9952 result = (char_u *)"";
9953 valid = 0; /* Must have ":p:h" to be valid */
9954 }
9955 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009956 result = curbuf->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009957 break;
9958
9959 case SPEC_HASH: /* '#' or "#99": alternate file */
9960 if (src[1] == '#') /* "##": the argument list */
9961 {
9962 result = arg_all();
9963 resultbuf = result;
9964 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009965 if (escaped != NULL)
9966 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009967#ifdef FEAT_MODIFY_FNAME
9968 skip_mod = TRUE;
9969#endif
9970 break;
9971 }
9972 s = src + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00009973 if (*s == '<') /* "#<99" uses v:oldfiles */
9974 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009975 i = (int)getdigits(&s);
9976 *usedlen = (int)(s - src); /* length of what we expand */
9977
Bram Moolenaard812df62008-11-09 12:46:09 +00009978 if (src[1] == '<')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009979 {
Bram Moolenaard812df62008-11-09 12:46:09 +00009980 if (*usedlen < 2)
9981 {
9982 /* Should we give an error message for #<text? */
9983 *usedlen = 1;
9984 return NULL;
9985 }
9986#ifdef FEAT_EVAL
9987 result = list_find_str(get_vim_var_list(VV_OLDFILES),
9988 (long)i);
9989 if (result == NULL)
9990 {
9991 *errormsg = (char_u *)"";
9992 return NULL;
9993 }
9994#else
9995 *errormsg = (char_u *)_("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009996 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00009997#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009998 }
9999 else
Bram Moolenaard812df62008-11-09 12:46:09 +000010000 {
10001 buf = buflist_findnr(i);
10002 if (buf == NULL)
10003 {
10004 *errormsg = (char_u *)_("E194: No alternate file name to substitute for '#'");
10005 return NULL;
10006 }
10007 if (lnump != NULL)
10008 *lnump = ECMD_LAST;
10009 if (buf->b_fname == NULL)
10010 {
10011 result = (char_u *)"";
10012 valid = 0; /* Must have ":p:h" to be valid */
10013 }
10014 else
10015 result = buf->b_fname;
10016 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010017 break;
10018
10019#ifdef FEAT_SEARCHPATH
10020 case SPEC_CFILE: /* file name under cursor */
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010021 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010022 if (result == NULL)
10023 {
10024 *errormsg = (char_u *)"";
10025 return NULL;
10026 }
10027 resultbuf = result; /* remember allocated string */
10028 break;
10029#endif
10030
10031#ifdef FEAT_AUTOCMD
10032 case SPEC_AFILE: /* file name for autocommand */
10033 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +000010034 if (result != NULL && !autocmd_fname_full)
10035 {
10036 /* Still need to turn the fname into a full path. It is
10037 * postponed to avoid a delay when <afile> is not used. */
10038 autocmd_fname_full = TRUE;
10039 result = FullName_save(autocmd_fname, FALSE);
10040 vim_free(autocmd_fname);
10041 autocmd_fname = result;
10042 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010043 if (result == NULL)
10044 {
10045 *errormsg = (char_u *)_("E495: no autocommand file name to substitute for \"<afile>\"");
10046 return NULL;
10047 }
Bram Moolenaara0174af2008-01-02 20:08:25 +000010048 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010049 break;
10050
10051 case SPEC_ABUF: /* buffer number for autocommand */
10052 if (autocmd_bufnr <= 0)
10053 {
10054 *errormsg = (char_u *)_("E496: no autocommand buffer number to substitute for \"<abuf>\"");
10055 return NULL;
10056 }
10057 sprintf((char *)strbuf, "%d", autocmd_bufnr);
10058 result = strbuf;
10059 break;
10060
10061 case SPEC_AMATCH: /* match name for autocommand */
10062 result = autocmd_match;
10063 if (result == NULL)
10064 {
10065 *errormsg = (char_u *)_("E497: no autocommand match name to substitute for \"<amatch>\"");
10066 return NULL;
10067 }
10068 break;
10069
10070#endif
10071 case SPEC_SFILE: /* file name for ":so" command */
10072 result = sourcing_name;
10073 if (result == NULL)
10074 {
10075 *errormsg = (char_u *)_("E498: no :source file name to substitute for \"<sfile>\"");
10076 return NULL;
10077 }
10078 break;
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010079 case SPEC_SLNUM: /* line in file for ":so" command */
10080 if (sourcing_name == NULL || sourcing_lnum == 0)
10081 {
10082 *errormsg = (char_u *)_("E842: no line number to use for \"<slnum>\"");
10083 return NULL;
10084 }
10085 sprintf((char *)strbuf, "%ld", (long)sourcing_lnum);
10086 result = strbuf;
10087 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010088#if defined(FEAT_CLIENTSERVER)
10089 case SPEC_CLIENT: /* Source of last submitted input */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010090 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
10091 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010092 result = strbuf;
10093 break;
10094#endif
10095 }
10096
10097 resultlen = (int)STRLEN(result); /* length of new string */
10098 if (src[*usedlen] == '<') /* remove the file name extension */
10099 {
10100 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010101 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010102 resultlen = (int)(s - result);
10103 }
10104#ifdef FEAT_MODIFY_FNAME
10105 else if (!skip_mod)
10106 {
10107 valid |= modify_fname(src, usedlen, &result, &resultbuf,
10108 &resultlen);
10109 if (result == NULL)
10110 {
10111 *errormsg = (char_u *)"";
10112 return NULL;
10113 }
10114 }
10115#endif
10116 }
10117
10118 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
10119 {
10120 if (valid != VALID_HEAD + VALID_PATH)
10121 /* xgettext:no-c-format */
10122 *errormsg = (char_u *)_("E499: Empty file name for '%' or '#', only works with \":p:h\"");
10123 else
10124 *errormsg = (char_u *)_("E500: Evaluates to an empty string");
10125 result = NULL;
10126 }
10127 else
10128 result = vim_strnsave(result, resultlen);
10129 vim_free(resultbuf);
10130 return result;
10131}
10132
10133/*
10134 * Concatenate all files in the argument list, separated by spaces, and return
10135 * it in one allocated string.
10136 * Spaces and backslashes in the file names are escaped with a backslash.
10137 * Returns NULL when out of memory.
10138 */
10139 static char_u *
10140arg_all()
10141{
10142 int len;
10143 int idx;
10144 char_u *retval = NULL;
10145 char_u *p;
10146
10147 /*
10148 * Do this loop two times:
10149 * first time: compute the total length
10150 * second time: concatenate the names
10151 */
10152 for (;;)
10153 {
10154 len = 0;
10155 for (idx = 0; idx < ARGCOUNT; ++idx)
10156 {
10157 p = alist_name(&ARGLIST[idx]);
10158 if (p != NULL)
10159 {
10160 if (len > 0)
10161 {
10162 /* insert a space in between names */
10163 if (retval != NULL)
10164 retval[len] = ' ';
10165 ++len;
10166 }
10167 for ( ; *p != NUL; ++p)
10168 {
10169 if (*p == ' ' || *p == '\\')
10170 {
10171 /* insert a backslash */
10172 if (retval != NULL)
10173 retval[len] = '\\';
10174 ++len;
10175 }
10176 if (retval != NULL)
10177 retval[len] = *p;
10178 ++len;
10179 }
10180 }
10181 }
10182
10183 /* second time: break here */
10184 if (retval != NULL)
10185 {
10186 retval[len] = NUL;
10187 break;
10188 }
10189
10190 /* allocate memory */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000010191 retval = alloc((unsigned)len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010192 if (retval == NULL)
10193 break;
10194 }
10195
10196 return retval;
10197}
10198
10199#if defined(FEAT_AUTOCMD) || defined(PROTO)
10200/*
10201 * Expand the <sfile> string in "arg".
10202 *
10203 * Returns an allocated string, or NULL for any error.
10204 */
10205 char_u *
10206expand_sfile(arg)
10207 char_u *arg;
10208{
10209 char_u *errormsg;
10210 int len;
10211 char_u *result;
10212 char_u *newres;
10213 char_u *repl;
10214 int srclen;
10215 char_u *p;
10216
10217 result = vim_strsave(arg);
10218 if (result == NULL)
10219 return NULL;
10220
10221 for (p = result; *p; )
10222 {
10223 if (STRNCMP(p, "<sfile>", 7) != 0)
10224 ++p;
10225 else
10226 {
10227 /* replace "<sfile>" with the sourced file name, and do ":" stuff */
Bram Moolenaar63b92542007-03-27 14:57:09 +000010228 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010229 if (errormsg != NULL)
10230 {
10231 if (*errormsg)
10232 emsg(errormsg);
10233 vim_free(result);
10234 return NULL;
10235 }
10236 if (repl == NULL) /* no match (cannot happen) */
10237 {
10238 p += srclen;
10239 continue;
10240 }
10241 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
10242 newres = alloc(len);
10243 if (newres == NULL)
10244 {
10245 vim_free(repl);
10246 vim_free(result);
10247 return NULL;
10248 }
10249 mch_memmove(newres, result, (size_t)(p - result));
10250 STRCPY(newres + (p - result), repl);
10251 len = (int)STRLEN(newres);
10252 STRCAT(newres, p + srclen);
10253 vim_free(repl);
10254 vim_free(result);
10255 result = newres;
10256 p = newres + len; /* continue after the match */
10257 }
10258 }
10259
10260 return result;
10261}
10262#endif
10263
10264#ifdef FEAT_SESSION
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010265static int ses_winsizes __ARGS((FILE *fd, int restore_size,
10266 win_T *tab_firstwin));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010267static int ses_win_rec __ARGS((FILE *fd, frame_T *fr));
10268static frame_T *ses_skipframe __ARGS((frame_T *fr));
10269static int ses_do_frame __ARGS((frame_T *fr));
10270static int ses_do_win __ARGS((win_T *wp));
10271static int ses_arglist __ARGS((FILE *fd, char *cmd, garray_T *gap, int fullname, unsigned *flagp));
10272static int ses_put_fname __ARGS((FILE *fd, char_u *name, unsigned *flagp));
10273static int ses_fname __ARGS((FILE *fd, buf_T *buf, unsigned *flagp));
10274
10275/*
10276 * Write openfile commands for the current buffers to an .exrc file.
10277 * Return FAIL on error, OK otherwise.
10278 */
10279 static int
10280makeopens(fd, dirnow)
10281 FILE *fd;
10282 char_u *dirnow; /* Current directory name */
10283{
10284 buf_T *buf;
10285 int only_save_windows = TRUE;
10286 int nr;
10287 int cnr = 1;
10288 int restore_size = TRUE;
10289 win_T *wp;
10290 char_u *sname;
10291 win_T *edited_win = NULL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010292 int tabnr;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010293 win_T *tab_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010294 frame_T *tab_topframe;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010295 int cur_arg_idx = 0;
Bram Moolenaar383c6f52008-01-04 15:01:07 +000010296 int next_arg_idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010297
10298 if (ssop_flags & SSOP_BUFFERS)
10299 only_save_windows = FALSE; /* Save ALL buffers */
10300
10301 /*
10302 * Begin by setting the this_session variable, and then other
10303 * sessionable variables.
10304 */
10305#ifdef FEAT_EVAL
10306 if (put_line(fd, "let v:this_session=expand(\"<sfile>:p\")") == FAIL)
10307 return FAIL;
10308 if (ssop_flags & SSOP_GLOBALS)
10309 if (store_session_globals(fd) == FAIL)
10310 return FAIL;
10311#endif
10312
10313 /*
10314 * Close all windows but one.
10315 */
10316 if (put_line(fd, "silent only") == FAIL)
10317 return FAIL;
10318
10319 /*
10320 * Now a :cd command to the session directory or the current directory
10321 */
10322 if (ssop_flags & SSOP_SESDIR)
10323 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010324 if (put_line(fd, "exe \"cd \" . escape(expand(\"<sfile>:p:h\"), ' ')")
10325 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010326 return FAIL;
10327 }
10328 else if (ssop_flags & SSOP_CURDIR)
10329 {
10330 sname = home_replace_save(NULL, globaldir != NULL ? globaldir : dirnow);
10331 if (sname == NULL
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010332 || fputs("cd ", fd) < 0
10333 || ses_put_fname(fd, sname, &ssop_flags) == FAIL
10334 || put_eol(fd) == FAIL)
10335 {
10336 vim_free(sname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010337 return FAIL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010338 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010339 vim_free(sname);
10340 }
10341
10342 /*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010343 * If there is an empty, unnamed buffer we will wipe it out later.
10344 * Remember the buffer number.
10345 */
10346 if (put_line(fd, "if expand('%') == '' && !&modified && line('$') <= 1 && getline(1) == ''") == FAIL)
10347 return FAIL;
10348 if (put_line(fd, " let s:wipebuf = bufnr('%')") == FAIL)
10349 return FAIL;
10350 if (put_line(fd, "endif") == FAIL)
10351 return FAIL;
10352
10353 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010354 * Now save the current files, current buffer first.
10355 */
10356 if (put_line(fd, "set shortmess=aoO") == FAIL)
10357 return FAIL;
10358
10359 /* Now put the other buffers into the buffer list */
10360 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
10361 {
10362 if (!(only_save_windows && buf->b_nwindows == 0)
10363 && !(buf->b_help && !(ssop_flags & SSOP_HELP))
10364 && buf->b_fname != NULL
10365 && buf->b_p_bl)
10366 {
10367 if (fprintf(fd, "badd +%ld ", buf->b_wininfo == NULL ? 1L
10368 : buf->b_wininfo->wi_fpos.lnum) < 0
10369 || ses_fname(fd, buf, &ssop_flags) == FAIL)
10370 return FAIL;
10371 }
10372 }
10373
10374 /* the global argument list */
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010010375 if (ses_arglist(fd, "argglobal", &global_alist.al_ga,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010376 !(ssop_flags & SSOP_CURDIR), &ssop_flags) == FAIL)
10377 return FAIL;
10378
10379 if (ssop_flags & SSOP_RESIZE)
10380 {
10381 /* Note: after the restore we still check it worked!*/
10382 if (fprintf(fd, "set lines=%ld columns=%ld" , Rows, Columns) < 0
10383 || put_eol(fd) == FAIL)
10384 return FAIL;
10385 }
10386
10387#ifdef FEAT_GUI
10388 if (gui.in_use && (ssop_flags & SSOP_WINPOS))
10389 {
10390 int x, y;
10391
10392 if (gui_mch_get_winpos(&x, &y) == OK)
10393 {
10394 /* Note: after the restore we still check it worked!*/
10395 if (fprintf(fd, "winpos %d %d", x, y) < 0 || put_eol(fd) == FAIL)
10396 return FAIL;
10397 }
10398 }
10399#endif
10400
10401 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000010402 * May repeat putting Windows for each tab, when "tabpages" is in
10403 * 'sessionoptions'.
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010404 * Don't use goto_tabpage(), it may change directory and trigger
10405 * autocommands.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010406 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010407 tab_firstwin = firstwin; /* first window in tab page "tabnr" */
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010408 tab_topframe = topframe;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010409 for (tabnr = 1; ; ++tabnr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010410 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010411 int need_tabnew = FALSE;
10412
Bram Moolenaar18144c82006-04-12 21:52:12 +000010413 if ((ssop_flags & SSOP_TABPAGES))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010414 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010415 tabpage_T *tp = find_tabpage(tabnr);
10416
10417 if (tp == NULL)
10418 break; /* done all tab pages */
10419 if (tp == curtab)
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010420 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010421 tab_firstwin = firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010422 tab_topframe = topframe;
10423 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010424 else
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010425 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010426 tab_firstwin = tp->tp_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010427 tab_topframe = tp->tp_topframe;
10428 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010429 if (tabnr > 1)
10430 need_tabnew = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010431 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010432
Bram Moolenaar18144c82006-04-12 21:52:12 +000010433 /*
10434 * Before creating the window layout, try loading one file. If this
10435 * is aborted we don't end up with a number of useless windows.
10436 * This may have side effects! (e.g., compressed or network file).
10437 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010438 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010439 {
10440 if (ses_do_win(wp)
10441 && wp->w_buffer->b_ffname != NULL
10442 && !wp->w_buffer->b_help
10443#ifdef FEAT_QUICKFIX
10444 && !bt_nofile(wp->w_buffer)
10445#endif
10446 )
10447 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010448 if (fputs(need_tabnew ? "tabedit " : "edit ", fd) < 0
Bram Moolenaar18144c82006-04-12 21:52:12 +000010449 || ses_fname(fd, wp->w_buffer, &ssop_flags) == FAIL)
10450 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010451 need_tabnew = FALSE;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010452 if (!wp->w_arg_idx_invalid)
10453 edited_win = wp;
10454 break;
10455 }
10456 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010457
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010458 /* If no file got edited create an empty tab page. */
10459 if (need_tabnew && put_line(fd, "tabnew") == FAIL)
10460 return FAIL;
10461
Bram Moolenaar18144c82006-04-12 21:52:12 +000010462 /*
10463 * Save current window layout.
10464 */
10465 if (put_line(fd, "set splitbelow splitright") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010466 return FAIL;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010467 if (ses_win_rec(fd, tab_topframe) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010468 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010469 if (!p_sb && put_line(fd, "set nosplitbelow") == FAIL)
10470 return FAIL;
10471 if (!p_spr && put_line(fd, "set nosplitright") == FAIL)
10472 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010473
Bram Moolenaar18144c82006-04-12 21:52:12 +000010474 /*
10475 * Check if window sizes can be restored (no windows omitted).
10476 * Remember the window number of the current window after restoring.
10477 */
10478 nr = 0;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010479 for (wp = tab_firstwin; wp != NULL; wp = W_NEXT(wp))
Bram Moolenaar18144c82006-04-12 21:52:12 +000010480 {
10481 if (ses_do_win(wp))
10482 ++nr;
10483 else
10484 restore_size = FALSE;
10485 if (curwin == wp)
10486 cnr = nr;
10487 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010488
Bram Moolenaar18144c82006-04-12 21:52:12 +000010489 /* Go to the first window. */
10490 if (put_line(fd, "wincmd t") == FAIL)
10491 return FAIL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010492
Bram Moolenaar18144c82006-04-12 21:52:12 +000010493 /*
10494 * If more than one window, see if sizes can be restored.
10495 * First set 'winheight' and 'winwidth' to 1 to avoid the windows being
10496 * resized when moving between windows.
10497 * Do this before restoring the view, so that the topline and the
10498 * cursor can be set. This is done again below.
10499 */
10500 if (put_line(fd, "set winheight=1 winwidth=1") == FAIL)
10501 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010502 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010503 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010504
Bram Moolenaar18144c82006-04-12 21:52:12 +000010505 /*
10506 * Restore the view of the window (options, file, cursor, etc.).
10507 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010508 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010509 {
10510 if (!ses_do_win(wp))
10511 continue;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010512 if (put_view(fd, wp, wp != edited_win, &ssop_flags,
10513 cur_arg_idx) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010514 return FAIL;
10515 if (nr > 1 && put_line(fd, "wincmd w") == FAIL)
10516 return FAIL;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010517 next_arg_idx = wp->w_arg_idx;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010518 }
10519
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010520 /* The argument index in the first tab page is zero, need to set it in
10521 * each window. For further tab pages it's the window where we do
10522 * "tabedit". */
10523 cur_arg_idx = next_arg_idx;
10524
Bram Moolenaar18144c82006-04-12 21:52:12 +000010525 /*
10526 * Restore cursor to the current window if it's not the first one.
10527 */
10528 if (cnr > 1 && (fprintf(fd, "%dwincmd w", cnr) < 0
10529 || put_eol(fd) == FAIL))
10530 return FAIL;
10531
10532 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000010533 * Restore window sizes again after jumping around in windows, because
10534 * the current window has a minimum size while others may not.
10535 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010536 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010537 return FAIL;
10538
Bram Moolenaar18144c82006-04-12 21:52:12 +000010539 /* Don't continue in another tab page when doing only the current one
10540 * or when at the last tab page. */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010541 if (!(ssop_flags & SSOP_TABPAGES))
Bram Moolenaar18144c82006-04-12 21:52:12 +000010542 break;
10543 }
10544
10545 if (ssop_flags & SSOP_TABPAGES)
10546 {
Bram Moolenaar18144c82006-04-12 21:52:12 +000010547 if (fprintf(fd, "tabnext %d", tabpage_index(curtab)) < 0
10548 || put_eol(fd) == FAIL)
10549 return FAIL;
10550 }
10551
Bram Moolenaar9c102382006-05-03 21:26:49 +000010552 /*
10553 * Wipe out an empty unnamed buffer we started in.
10554 */
10555 if (put_line(fd, "if exists('s:wipebuf')") == FAIL)
10556 return FAIL;
Bram Moolenaarf3a67882006-05-05 21:09:41 +000010557 if (put_line(fd, " silent exe 'bwipe ' . s:wipebuf") == FAIL)
Bram Moolenaar9c102382006-05-03 21:26:49 +000010558 return FAIL;
10559 if (put_line(fd, "endif") == FAIL)
10560 return FAIL;
10561 if (put_line(fd, "unlet! s:wipebuf") == FAIL)
10562 return FAIL;
10563
10564 /* Re-apply 'winheight', 'winwidth' and 'shortmess'. */
10565 if (fprintf(fd, "set winheight=%ld winwidth=%ld shortmess=%s",
10566 p_wh, p_wiw, p_shm) < 0 || put_eol(fd) == FAIL)
10567 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010568
10569 /*
10570 * Lastly, execute the x.vim file if it exists.
10571 */
10572 if (put_line(fd, "let s:sx = expand(\"<sfile>:p:r\").\"x.vim\"") == FAIL
10573 || put_line(fd, "if file_readable(s:sx)") == FAIL
Bram Moolenaar42ba1262008-12-09 10:18:03 +000010574 || put_line(fd, " exe \"source \" . fnameescape(s:sx)") == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000010575 || put_line(fd, "endif") == FAIL)
10576 return FAIL;
10577
10578 return OK;
10579}
10580
10581 static int
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010582ses_winsizes(fd, restore_size, tab_firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010583 FILE *fd;
10584 int restore_size;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010585 win_T *tab_firstwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010586{
10587 int n = 0;
10588 win_T *wp;
10589
10590 if (restore_size && (ssop_flags & SSOP_WINSIZE))
10591 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010592 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010593 {
10594 if (!ses_do_win(wp))
10595 continue;
10596 ++n;
10597
10598 /* restore height when not full height */
10599 if (wp->w_height + wp->w_status_height < topframe->fr_height
10600 && (fprintf(fd,
10601 "exe '%dresize ' . ((&lines * %ld + %ld) / %ld)",
10602 n, (long)wp->w_height, Rows / 2, Rows) < 0
10603 || put_eol(fd) == FAIL))
10604 return FAIL;
10605
10606 /* restore width when not full width */
10607 if (wp->w_width < Columns && (fprintf(fd,
10608 "exe 'vert %dresize ' . ((&columns * %ld + %ld) / %ld)",
10609 n, (long)wp->w_width, Columns / 2, Columns) < 0
10610 || put_eol(fd) == FAIL))
10611 return FAIL;
10612 }
10613 }
10614 else
10615 {
10616 /* Just equalise window sizes */
10617 if (put_line(fd, "wincmd =") == FAIL)
10618 return FAIL;
10619 }
10620 return OK;
10621}
10622
10623/*
10624 * Write commands to "fd" to recursively create windows for frame "fr",
10625 * horizontally and vertically split.
10626 * After the commands the last window in the frame is the current window.
10627 * Returns FAIL when writing the commands to "fd" fails.
10628 */
10629 static int
10630ses_win_rec(fd, fr)
10631 FILE *fd;
10632 frame_T *fr;
10633{
10634 frame_T *frc;
10635 int count = 0;
10636
10637 if (fr->fr_layout != FR_LEAF)
10638 {
10639 /* Find first frame that's not skipped and then create a window for
10640 * each following one (first frame is already there). */
10641 frc = ses_skipframe(fr->fr_child);
10642 if (frc != NULL)
10643 while ((frc = ses_skipframe(frc->fr_next)) != NULL)
10644 {
10645 /* Make window as big as possible so that we have lots of room
10646 * to split. */
10647 if (put_line(fd, "wincmd _ | wincmd |") == FAIL
10648 || put_line(fd, fr->fr_layout == FR_COL
10649 ? "split" : "vsplit") == FAIL)
10650 return FAIL;
10651 ++count;
10652 }
10653
10654 /* Go back to the first window. */
10655 if (count > 0 && (fprintf(fd, fr->fr_layout == FR_COL
10656 ? "%dwincmd k" : "%dwincmd h", count) < 0
10657 || put_eol(fd) == FAIL))
10658 return FAIL;
10659
10660 /* Recursively create frames/windows in each window of this column or
10661 * row. */
10662 frc = ses_skipframe(fr->fr_child);
10663 while (frc != NULL)
10664 {
10665 ses_win_rec(fd, frc);
10666 frc = ses_skipframe(frc->fr_next);
10667 /* Go to next window. */
10668 if (frc != NULL && put_line(fd, "wincmd w") == FAIL)
10669 return FAIL;
10670 }
10671 }
10672 return OK;
10673}
10674
10675/*
10676 * Skip frames that don't contain windows we want to save in the Session.
10677 * Returns NULL when there none.
10678 */
10679 static frame_T *
10680ses_skipframe(fr)
10681 frame_T *fr;
10682{
10683 frame_T *frc;
10684
10685 for (frc = fr; frc != NULL; frc = frc->fr_next)
10686 if (ses_do_frame(frc))
10687 break;
10688 return frc;
10689}
10690
10691/*
10692 * Return TRUE if frame "fr" has a window somewhere that we want to save in
10693 * the Session.
10694 */
10695 static int
10696ses_do_frame(fr)
10697 frame_T *fr;
10698{
10699 frame_T *frc;
10700
10701 if (fr->fr_layout == FR_LEAF)
10702 return ses_do_win(fr->fr_win);
10703 for (frc = fr->fr_child; frc != NULL; frc = frc->fr_next)
10704 if (ses_do_frame(frc))
10705 return TRUE;
10706 return FALSE;
10707}
10708
10709/*
10710 * Return non-zero if window "wp" is to be stored in the Session.
10711 */
10712 static int
10713ses_do_win(wp)
10714 win_T *wp;
10715{
10716 if (wp->w_buffer->b_fname == NULL
10717#ifdef FEAT_QUICKFIX
10718 /* When 'buftype' is "nofile" can't restore the window contents. */
10719 || bt_nofile(wp->w_buffer)
10720#endif
10721 )
10722 return (ssop_flags & SSOP_BLANK);
10723 if (wp->w_buffer->b_help)
10724 return (ssop_flags & SSOP_HELP);
10725 return TRUE;
10726}
10727
10728/*
10729 * Write commands to "fd" to restore the view of a window.
10730 * Caller must make sure 'scrolloff' is zero.
10731 */
10732 static int
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010733put_view(fd, wp, add_edit, flagp, current_arg_idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010734 FILE *fd;
10735 win_T *wp;
10736 int add_edit; /* add ":edit" command to view */
10737 unsigned *flagp; /* vop_flags or ssop_flags */
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010738 int current_arg_idx; /* current argument index of the window, use
10739 * -1 if unknown */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010740{
10741 win_T *save_curwin;
10742 int f;
10743 int do_cursor;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010744 int did_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010745
10746 /* Always restore cursor position for ":mksession". For ":mkview" only
10747 * when 'viewoptions' contains "cursor". */
10748 do_cursor = (flagp == &ssop_flags || *flagp & SSOP_CURSOR);
10749
10750 /*
10751 * Local argument list.
10752 */
10753 if (wp->w_alist == &global_alist)
10754 {
10755 if (put_line(fd, "argglobal") == FAIL)
10756 return FAIL;
10757 }
10758 else
10759 {
10760 if (ses_arglist(fd, "arglocal", &wp->w_alist->al_ga,
10761 flagp == &vop_flags
10762 || !(*flagp & SSOP_CURDIR)
10763 || wp->w_localdir != NULL, flagp) == FAIL)
10764 return FAIL;
10765 }
10766
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010767 /* Only when part of a session: restore the argument index. Some
10768 * arguments may have been deleted, check if the index is valid. */
Bram Moolenaar51f53df2010-06-26 05:25:54 +020010769 if (wp->w_arg_idx != current_arg_idx && wp->w_arg_idx < WARGCOUNT(wp)
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010770 && flagp == &ssop_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010771 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010772 if (fprintf(fd, "%ldargu", (long)wp->w_arg_idx + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010773 || put_eol(fd) == FAIL)
10774 return FAIL;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010775 did_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010776 }
10777
10778 /* Edit the file. Skip this when ":next" already did it. */
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010779 if (add_edit && (!did_next || wp->w_arg_idx_invalid))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010780 {
10781 /*
10782 * Load the file.
10783 */
10784 if (wp->w_buffer->b_ffname != NULL
10785#ifdef FEAT_QUICKFIX
10786 && !bt_nofile(wp->w_buffer)
10787#endif
10788 )
10789 {
10790 /*
10791 * Editing a file in this buffer: use ":edit file".
10792 * This may have side effects! (e.g., compressed or network file).
10793 */
10794 if (fputs("edit ", fd) < 0
10795 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
10796 return FAIL;
10797 }
10798 else
10799 {
10800 /* No file in this buffer, just make it empty. */
10801 if (put_line(fd, "enew") == FAIL)
10802 return FAIL;
10803#ifdef FEAT_QUICKFIX
10804 if (wp->w_buffer->b_ffname != NULL)
10805 {
10806 /* The buffer does have a name, but it's not a file name. */
10807 if (fputs("file ", fd) < 0
10808 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
10809 return FAIL;
10810 }
10811#endif
10812 do_cursor = FALSE;
10813 }
10814 }
10815
10816 /*
10817 * Local mappings and abbreviations.
10818 */
10819 if ((*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
10820 && makemap(fd, wp->w_buffer) == FAIL)
10821 return FAIL;
10822
10823 /*
10824 * Local options. Need to go to the window temporarily.
10825 * Store only local values when using ":mkview" and when ":mksession" is
10826 * used and 'sessionoptions' doesn't include "options".
10827 * Some folding options are always stored when "folds" is included,
10828 * otherwise the folds would not be restored correctly.
10829 */
10830 save_curwin = curwin;
10831 curwin = wp;
10832 curbuf = curwin->w_buffer;
10833 if (*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
10834 f = makeset(fd, OPT_LOCAL,
10835 flagp == &vop_flags || !(*flagp & SSOP_OPTIONS));
10836#ifdef FEAT_FOLDING
10837 else if (*flagp & SSOP_FOLDS)
10838 f = makefoldset(fd);
10839#endif
10840 else
10841 f = OK;
10842 curwin = save_curwin;
10843 curbuf = curwin->w_buffer;
10844 if (f == FAIL)
10845 return FAIL;
10846
10847#ifdef FEAT_FOLDING
10848 /*
10849 * Save Folds when 'buftype' is empty and for help files.
10850 */
10851 if ((*flagp & SSOP_FOLDS)
10852 && wp->w_buffer->b_ffname != NULL
Bram Moolenaarb1b715d2006-01-21 22:09:43 +000010853# ifdef FEAT_QUICKFIX
10854 && (*wp->w_buffer->b_p_bt == NUL || wp->w_buffer->b_help)
10855# endif
10856 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000010857 {
10858 if (put_folds(fd, wp) == FAIL)
10859 return FAIL;
10860 }
10861#endif
10862
10863 /*
10864 * Set the cursor after creating folds, since that moves the cursor.
10865 */
10866 if (do_cursor)
10867 {
10868
10869 /* Restore the cursor line in the file and relatively in the
10870 * window. Don't use "G", it changes the jumplist. */
10871 if (fprintf(fd, "let s:l = %ld - ((%ld * winheight(0) + %ld) / %ld)",
10872 (long)wp->w_cursor.lnum,
10873 (long)(wp->w_cursor.lnum - wp->w_topline),
10874 (long)wp->w_height / 2, (long)wp->w_height) < 0
10875 || put_eol(fd) == FAIL
10876 || put_line(fd, "if s:l < 1 | let s:l = 1 | endif") == FAIL
10877 || put_line(fd, "exe s:l") == FAIL
10878 || put_line(fd, "normal! zt") == FAIL
10879 || fprintf(fd, "%ld", (long)wp->w_cursor.lnum) < 0
10880 || put_eol(fd) == FAIL)
10881 return FAIL;
10882 /* Restore the cursor column and left offset when not wrapping. */
10883 if (wp->w_cursor.col == 0)
10884 {
10885 if (put_line(fd, "normal! 0") == FAIL)
10886 return FAIL;
10887 }
10888 else
10889 {
10890 if (!wp->w_p_wrap && wp->w_leftcol > 0 && wp->w_width > 0)
10891 {
10892 if (fprintf(fd,
10893 "let s:c = %ld - ((%ld * winwidth(0) + %ld) / %ld)",
Bram Moolenaar558ddad2013-02-20 19:26:29 +010010894 (long)wp->w_virtcol + 1,
10895 (long)(wp->w_virtcol - wp->w_leftcol),
Bram Moolenaar071d4272004-06-13 20:20:40 +000010896 (long)wp->w_width / 2, (long)wp->w_width) < 0
10897 || put_eol(fd) == FAIL
10898 || put_line(fd, "if s:c > 0") == FAIL
10899 || fprintf(fd,
Bram Moolenaar558ddad2013-02-20 19:26:29 +010010900 " exe 'normal! ' . s:c . '|zs' . %ld . '|'",
10901 (long)wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010902 || put_eol(fd) == FAIL
10903 || put_line(fd, "else") == FAIL
Bram Moolenaarfdf447b2013-02-26 17:21:29 +010010904 || fprintf(fd, " normal! 0%d|", wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010905 || put_eol(fd) == FAIL
10906 || put_line(fd, "endif") == FAIL)
10907 return FAIL;
10908 }
10909 else
10910 {
Bram Moolenaar558ddad2013-02-20 19:26:29 +010010911 if (fprintf(fd, "normal! 0%d|", wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010912 || put_eol(fd) == FAIL)
10913 return FAIL;
10914 }
10915 }
10916 }
10917
10918 /*
10919 * Local directory.
10920 */
10921 if (wp->w_localdir != NULL)
10922 {
10923 if (fputs("lcd ", fd) < 0
10924 || ses_put_fname(fd, wp->w_localdir, flagp) == FAIL
10925 || put_eol(fd) == FAIL)
10926 return FAIL;
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010927 did_lcd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010928 }
10929
10930 return OK;
10931}
10932
10933/*
10934 * Write an argument list to the session file.
10935 * Returns FAIL if writing fails.
10936 */
10937 static int
10938ses_arglist(fd, cmd, gap, fullname, flagp)
10939 FILE *fd;
10940 char *cmd;
10941 garray_T *gap;
10942 int fullname; /* TRUE: use full path name */
10943 unsigned *flagp;
10944{
10945 int i;
Bram Moolenaard9462e32011-04-11 21:35:11 +020010946 char_u *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010947 char_u *s;
10948
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010010949 if (fputs(cmd, fd) < 0 || put_eol(fd) == FAIL)
10950 return FAIL;
10951 if (put_line(fd, "silent! argdel *") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010952 return FAIL;
10953 for (i = 0; i < gap->ga_len; ++i)
10954 {
10955 /* NULL file names are skipped (only happens when out of memory). */
10956 s = alist_name(&((aentry_T *)gap->ga_data)[i]);
10957 if (s != NULL)
10958 {
10959 if (fullname)
10960 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020010961 buf = alloc(MAXPATHL);
10962 if (buf != NULL)
10963 {
10964 (void)vim_FullName(s, buf, MAXPATHL, FALSE);
10965 s = buf;
10966 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010967 }
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010010968 if (fputs("argadd ", fd) < 0
10969 || ses_put_fname(fd, s, flagp) == FAIL
10970 || put_eol(fd) == FAIL)
Bram Moolenaard9462e32011-04-11 21:35:11 +020010971 {
10972 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010973 return FAIL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020010974 }
10975 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010976 }
10977 }
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010010978 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010979}
10980
10981/*
10982 * Write a buffer name to the session file.
10983 * Also ends the line.
10984 * Returns FAIL if writing fails.
10985 */
10986 static int
10987ses_fname(fd, buf, flagp)
10988 FILE *fd;
10989 buf_T *buf;
10990 unsigned *flagp;
10991{
10992 char_u *name;
10993
10994 /* Use the short file name if the current directory is known at the time
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010995 * the session file will be sourced.
10996 * Don't do this for ":mkview", we don't know the current directory.
10997 * Don't do this after ":lcd", we don't keep track of what the current
10998 * directory is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010999 if (buf->b_sfname != NULL
11000 && flagp == &ssop_flags
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011001 && (ssop_flags & (SSOP_CURDIR | SSOP_SESDIR))
Bram Moolenaar8d594672009-07-01 18:18:57 +000011002#ifdef FEAT_AUTOCHDIR
11003 && !p_acd
11004#endif
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011005 && !did_lcd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011006 name = buf->b_sfname;
11007 else
11008 name = buf->b_ffname;
11009 if (ses_put_fname(fd, name, flagp) == FAIL || put_eol(fd) == FAIL)
11010 return FAIL;
11011 return OK;
11012}
11013
11014/*
11015 * Write a file name to the session file.
11016 * Takes care of the "slash" option in 'sessionoptions' and escapes special
11017 * characters.
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011018 * Returns FAIL if writing fails or out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011019 */
11020 static int
11021ses_put_fname(fd, name, flagp)
11022 FILE *fd;
11023 char_u *name;
11024 unsigned *flagp;
11025{
11026 char_u *sname;
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011027 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011028 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011029
11030 sname = home_replace_save(NULL, name);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011031 if (sname == NULL)
11032 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011033
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011034 if (*flagp & SSOP_SLASH)
11035 {
11036 /* change all backslashes to forward slashes */
11037 for (p = sname; *p != NUL; mb_ptr_adv(p))
11038 if (*p == '\\')
11039 *p = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +000011040 }
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011041
Bram Moolenaar84a05ac2013-05-06 04:24:17 +020011042 /* escape special characters */
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011043 p = vim_strsave_fnameescape(sname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011044 vim_free(sname);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011045 if (p == NULL)
11046 return FAIL;
11047
11048 /* write the result */
11049 if (fputs((char *)p, fd) < 0)
11050 retval = FAIL;
11051
11052 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011053 return retval;
11054}
11055
11056/*
11057 * ":loadview [nr]"
11058 */
11059 static void
11060ex_loadview(eap)
11061 exarg_T *eap;
11062{
11063 char_u *fname;
11064
11065 fname = get_view_file(*eap->arg);
11066 if (fname != NULL)
11067 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +000011068 do_source(fname, FALSE, DOSO_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011069 vim_free(fname);
11070 }
11071}
11072
11073/*
11074 * Get the name of the view file for the current buffer.
11075 */
11076 static char_u *
11077get_view_file(c)
11078 int c;
11079{
11080 int len = 0;
11081 char_u *p, *s;
11082 char_u *retval;
11083 char_u *sname;
11084
11085 if (curbuf->b_ffname == NULL)
11086 {
11087 EMSG(_(e_noname));
11088 return NULL;
11089 }
11090 sname = home_replace_save(NULL, curbuf->b_ffname);
11091 if (sname == NULL)
11092 return NULL;
11093
11094 /*
11095 * We want a file name without separators, because we're not going to make
11096 * a directory.
11097 * "normal" path separator -> "=+"
11098 * "=" -> "=="
11099 * ":" path separator -> "=-"
11100 */
11101 for (p = sname; *p; ++p)
11102 if (*p == '=' || vim_ispathsep(*p))
11103 ++len;
11104 retval = alloc((unsigned)(STRLEN(sname) + len + STRLEN(p_vdir) + 9));
11105 if (retval != NULL)
11106 {
11107 STRCPY(retval, p_vdir);
11108 add_pathsep(retval);
11109 s = retval + STRLEN(retval);
11110 for (p = sname; *p; ++p)
11111 {
11112 if (*p == '=')
11113 {
11114 *s++ = '=';
11115 *s++ = '=';
11116 }
11117 else if (vim_ispathsep(*p))
11118 {
11119 *s++ = '=';
Bram Moolenaare60acc12011-05-10 16:41:25 +020011120#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011121 if (*p == ':')
11122 *s++ = '-';
11123 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000011124#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000011125 *s++ = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +000011126 }
11127 else
11128 *s++ = *p;
11129 }
11130 *s++ = '=';
11131 *s++ = c;
11132 STRCPY(s, ".vim");
11133 }
11134
11135 vim_free(sname);
11136 return retval;
11137}
11138
11139#endif /* FEAT_SESSION */
11140
11141/*
11142 * Write end-of-line character(s) for ":mkexrc", ":mkvimrc" and ":mksession".
11143 * Return FAIL for a write error.
11144 */
11145 int
11146put_eol(fd)
11147 FILE *fd;
11148{
11149 if (
11150#ifdef USE_CRNL
11151 (
11152# ifdef MKSESSION_NL
11153 !mksession_nl &&
11154# endif
11155 (putc('\r', fd) < 0)) ||
11156#endif
11157 (putc('\n', fd) < 0))
11158 return FAIL;
11159 return OK;
11160}
11161
11162/*
11163 * Write a line to "fd".
11164 * Return FAIL for a write error.
11165 */
11166 int
11167put_line(fd, s)
11168 FILE *fd;
11169 char *s;
11170{
11171 if (fputs(s, fd) < 0 || put_eol(fd) == FAIL)
11172 return FAIL;
11173 return OK;
11174}
11175
11176#ifdef FEAT_VIMINFO
11177/*
11178 * ":rviminfo" and ":wviminfo".
11179 */
11180 static void
11181ex_viminfo(eap)
11182 exarg_T *eap;
11183{
11184 char_u *save_viminfo;
11185
11186 save_viminfo = p_viminfo;
11187 if (*p_viminfo == NUL)
11188 p_viminfo = (char_u *)"'100";
11189 if (eap->cmdidx == CMD_rviminfo)
11190 {
Bram Moolenaard812df62008-11-09 12:46:09 +000011191 if (read_viminfo(eap->arg, VIF_WANT_INFO | VIF_WANT_MARKS
11192 | (eap->forceit ? VIF_FORCEIT : 0)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011193 EMSG(_("E195: Cannot open viminfo file for reading"));
11194 }
11195 else
11196 write_viminfo(eap->arg, eap->forceit);
11197 p_viminfo = save_viminfo;
11198}
11199#endif
11200
11201#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +000011202/*
Bram Moolenaard9462e32011-04-11 21:35:11 +020011203 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +000011204 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +000011205 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011206 void
11207dialog_msg(buff, format, fname)
11208 char_u *buff;
11209 char *format;
11210 char_u *fname;
11211{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011212 if (fname == NULL)
11213 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +020011214 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011215}
11216#endif
11217
11218/*
11219 * ":behave {mswin,xterm}"
11220 */
11221 static void
11222ex_behave(eap)
11223 exarg_T *eap;
11224{
11225 if (STRCMP(eap->arg, "mswin") == 0)
11226 {
11227 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
11228 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
11229 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
11230 set_option_value((char_u *)"keymodel", 0L,
11231 (char_u *)"startsel,stopsel", 0);
11232 }
11233 else if (STRCMP(eap->arg, "xterm") == 0)
11234 {
11235 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
11236 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
11237 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
11238 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
11239 }
11240 else
11241 EMSG2(_(e_invarg2), eap->arg);
11242}
11243
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010011244#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
11245/*
11246 * Function given to ExpandGeneric() to obtain the possible arguments of the
11247 * ":behave {mswin,xterm}" command.
11248 */
11249 char_u *
11250get_behave_arg(xp, idx)
11251 expand_T *xp UNUSED;
11252 int idx;
11253{
11254 if (idx == 0)
11255 return (char_u *)"mswin";
11256 if (idx == 1)
11257 return (char_u *)"xterm";
11258 return NULL;
11259}
11260#endif
11261
Bram Moolenaar071d4272004-06-13 20:20:40 +000011262#ifdef FEAT_AUTOCMD
11263static int filetype_detect = FALSE;
11264static int filetype_plugin = FALSE;
11265static int filetype_indent = FALSE;
11266
11267/*
11268 * ":filetype [plugin] [indent] {on,off,detect}"
11269 * on: Load the filetype.vim file to install autocommands for file types.
11270 * off: Load the ftoff.vim file to remove all autocommands for file types.
11271 * plugin on: load filetype.vim and ftplugin.vim
11272 * plugin off: load ftplugof.vim
11273 * indent on: load filetype.vim and indent.vim
11274 * indent off: load indoff.vim
11275 */
11276 static void
11277ex_filetype(eap)
11278 exarg_T *eap;
11279{
11280 char_u *arg = eap->arg;
11281 int plugin = FALSE;
11282 int indent = FALSE;
11283
11284 if (*eap->arg == NUL)
11285 {
11286 /* Print current status. */
11287 smsg((char_u *)"filetype detection:%s plugin:%s indent:%s",
11288 filetype_detect ? "ON" : "OFF",
11289 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
11290 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
11291 return;
11292 }
11293
11294 /* Accept "plugin" and "indent" in any order. */
11295 for (;;)
11296 {
11297 if (STRNCMP(arg, "plugin", 6) == 0)
11298 {
11299 plugin = TRUE;
11300 arg = skipwhite(arg + 6);
11301 continue;
11302 }
11303 if (STRNCMP(arg, "indent", 6) == 0)
11304 {
11305 indent = TRUE;
11306 arg = skipwhite(arg + 6);
11307 continue;
11308 }
11309 break;
11310 }
11311 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
11312 {
11313 if (*arg == 'o' || !filetype_detect)
11314 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011315 source_runtime((char_u *)FILETYPE_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011316 filetype_detect = TRUE;
11317 if (plugin)
11318 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011319 source_runtime((char_u *)FTPLUGIN_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011320 filetype_plugin = TRUE;
11321 }
11322 if (indent)
11323 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011324 source_runtime((char_u *)INDENT_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011325 filetype_indent = TRUE;
11326 }
11327 }
11328 if (*arg == 'd')
11329 {
11330 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE);
Bram Moolenaara3227e22006-03-08 21:32:40 +000011331 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011332 }
11333 }
11334 else if (STRCMP(arg, "off") == 0)
11335 {
11336 if (plugin || indent)
11337 {
11338 if (plugin)
11339 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011340 source_runtime((char_u *)FTPLUGOF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011341 filetype_plugin = FALSE;
11342 }
11343 if (indent)
11344 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011345 source_runtime((char_u *)INDOFF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011346 filetype_indent = FALSE;
11347 }
11348 }
11349 else
11350 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011351 source_runtime((char_u *)FTOFF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011352 filetype_detect = FALSE;
11353 }
11354 }
11355 else
11356 EMSG2(_(e_invarg2), arg);
11357}
11358
11359/*
11360 * ":setfiletype {name}"
11361 */
11362 static void
11363ex_setfiletype(eap)
11364 exarg_T *eap;
11365{
11366 if (!did_filetype)
11367 set_option_value((char_u *)"filetype", 0L, eap->arg, OPT_LOCAL);
11368}
11369#endif
11370
Bram Moolenaar071d4272004-06-13 20:20:40 +000011371 static void
11372ex_digraphs(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +000011373 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011374{
11375#ifdef FEAT_DIGRAPHS
11376 if (*eap->arg != NUL)
11377 putdigraph(eap->arg);
11378 else
11379 listdigraphs();
11380#else
11381 EMSG(_("E196: No digraphs in this version"));
11382#endif
11383}
11384
11385 static void
11386ex_set(eap)
11387 exarg_T *eap;
11388{
11389 int flags = 0;
11390
11391 if (eap->cmdidx == CMD_setlocal)
11392 flags = OPT_LOCAL;
11393 else if (eap->cmdidx == CMD_setglobal)
11394 flags = OPT_GLOBAL;
11395#if defined(FEAT_EVAL) && defined(FEAT_AUTOCMD) && defined(FEAT_BROWSE)
11396 if (cmdmod.browse && flags == 0)
11397 ex_options(eap);
11398 else
11399#endif
11400 (void)do_set(eap->arg, flags);
11401}
11402
11403#ifdef FEAT_SEARCH_EXTRA
11404/*
11405 * ":nohlsearch"
11406 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011407 static void
11408ex_nohlsearch(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +000011409 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011410{
Bram Moolenaar8050efa2013-11-08 04:30:20 +010011411 SET_NO_HLSEARCH(TRUE);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +000011412 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011413}
11414
11415/*
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011416 * ":[N]match {group} {pattern}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000011417 * Sets nextcmd to the start of the next command, if any. Also called when
11418 * skipping commands to find the next command.
11419 */
11420 static void
11421ex_match(eap)
11422 exarg_T *eap;
11423{
11424 char_u *p;
Bram Moolenaar52d36c82007-08-11 14:00:30 +000011425 char_u *g = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011426 char_u *end;
11427 int c;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011428 int id;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011429
11430 if (eap->line2 <= 3)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011431 id = eap->line2;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011432 else
11433 {
11434 EMSG(e_invcmd);
11435 return;
11436 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011437
11438 /* First clear any old pattern. */
11439 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011440 match_delete(curwin, id, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011441
11442 if (ends_excmd(*eap->arg))
11443 end = eap->arg;
11444 else if ((STRNICMP(eap->arg, "none", 4) == 0
11445 && (vim_iswhite(eap->arg[4]) || ends_excmd(eap->arg[4]))))
11446 end = eap->arg + 4;
11447 else
11448 {
11449 p = skiptowhite(eap->arg);
11450 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011451 g = vim_strnsave(eap->arg, (int)(p - eap->arg));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011452 p = skipwhite(p);
11453 if (*p == NUL)
11454 {
11455 /* There must be two arguments. */
11456 EMSG2(_(e_invarg2), eap->arg);
11457 return;
11458 }
11459 end = skip_regexp(p + 1, *p, TRUE, NULL);
11460 if (!eap->skip)
11461 {
11462 if (*end != NUL && !ends_excmd(*skipwhite(end + 1)))
11463 {
11464 eap->errmsg = e_trailing;
11465 return;
11466 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000011467 if (*end != *p)
11468 {
11469 EMSG2(_(e_invarg2), p);
11470 return;
11471 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011472
11473 c = *end;
11474 *end = NUL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011475 match_add(curwin, g, p + 1, 10, id);
11476 vim_free(g);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000011477 *end = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011478 }
11479 }
11480 eap->nextcmd = find_nextcmd(end);
11481}
11482#endif
11483
11484#ifdef FEAT_CRYPT
11485/*
11486 * ":X": Get crypt key
11487 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011488 static void
11489ex_X(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +000011490 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011491{
Bram Moolenaar49771f42010-07-20 17:32:38 +020011492 if (get_crypt_method(curbuf) == 0 || blowfish_self_test() == OK)
Bram Moolenaar40e6a712010-05-16 22:32:54 +020011493 (void)get_crypt_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011494}
11495#endif
11496
11497#ifdef FEAT_FOLDING
11498 static void
11499ex_fold(eap)
11500 exarg_T *eap;
11501{
11502 if (foldManualAllowed(TRUE))
11503 foldCreate(eap->line1, eap->line2);
11504}
11505
11506 static void
11507ex_foldopen(eap)
11508 exarg_T *eap;
11509{
11510 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
11511 eap->forceit, FALSE);
11512}
11513
11514 static void
11515ex_folddo(eap)
11516 exarg_T *eap;
11517{
11518 linenr_T lnum;
11519
11520 /* First set the marks for all lines closed/open. */
11521 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
11522 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
11523 ml_setmarked(lnum);
11524
11525 /* Execute the command on the marked lines. */
11526 global_exe(eap->arg);
11527 ml_clearmarked(); /* clear rest of the marks */
11528}
11529#endif