blob: f1aa60798d1de011ab0cb5c8602149f74e88ac24 [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 Moolenaar05a7bb32006-01-19 22:09:32 +00004583 && vim_strpbrk(repl, (char_u *)"!&;()<>") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004584 {
4585 char_u *l;
4586
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004587 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 Moolenaar05a7bb32006-01-19 22:09:32 +00004592 /* For a sh-like shell escape "!" another time. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004593 if (strstr((char *)p_sh, "sh") != NULL)
4594 {
4595 l = vim_strsave_escaped(repl, (char_u *)"!");
4596 if (l != NULL)
4597 {
4598 vim_free(repl);
4599 repl = l;
4600 }
4601 }
4602 }
4603 }
4604
4605 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
4606 vim_free(repl);
4607 if (p == NULL)
4608 return FAIL;
4609 }
4610
4611 /*
4612 * One file argument: Expand wildcards.
4613 * Don't do this with ":r !command" or ":w !command".
4614 */
4615 if ((eap->argt & NOSPC) && !eap->usefilter)
4616 {
4617 /*
4618 * May do this twice:
4619 * 1. Replace environment variables.
4620 * 2. Replace any other wildcards, remove backslashes.
4621 */
4622 for (n = 1; n <= 2; ++n)
4623 {
4624 if (n == 2)
4625 {
4626#ifdef UNIX
4627 /*
4628 * Only for Unix we check for more than one file name.
4629 * For other systems spaces are considered to be part
4630 * of the file name.
4631 * Only check here if there is no wildcard, otherwise
4632 * ExpandOne() will check for errors. This allows
4633 * ":e `ls ve*.c`" on Unix.
4634 */
4635 if (!has_wildcards)
4636 for (p = eap->arg; *p; ++p)
4637 {
4638 /* skip escaped characters */
4639 if (p[1] && (*p == '\\' || *p == Ctrl_V))
4640 ++p;
4641 else if (vim_iswhite(*p))
4642 {
4643 *errormsgp = (char_u *)_("E172: Only one file name allowed");
4644 return FAIL;
4645 }
4646 }
4647#endif
4648
4649 /*
4650 * Halve the number of backslashes (this is Vi compatible).
4651 * For Unix and OS/2, when wildcards are expanded, this is
4652 * done by ExpandOne() below.
4653 */
4654#if defined(UNIX) || defined(OS2)
4655 if (!has_wildcards)
4656#endif
4657 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004658 }
4659
4660 if (has_wildcards)
4661 {
4662 if (n == 1)
4663 {
4664 /*
4665 * First loop: May expand environment variables. This
4666 * can be done much faster with expand_env() than with
4667 * something else (e.g., calling a shell).
4668 * After expanding environment variables, check again
4669 * if there are still wildcards present.
4670 */
4671 if (vim_strchr(eap->arg, '$') != NULL
4672 || vim_strchr(eap->arg, '~') != NULL)
4673 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00004674 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004675 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004676 has_wildcards = mch_has_wildcard(NameBuff);
4677 p = NameBuff;
4678 }
4679 else
4680 p = NULL;
4681 }
4682 else /* n == 2 */
4683 {
4684 expand_T xpc;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004685 int options = WILD_LIST_NOTFOUND|WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004686
4687 ExpandInit(&xpc);
4688 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004689 if (p_wic)
4690 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004691 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01004692 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004693 if (p == NULL)
4694 return FAIL;
4695 }
4696 if (p != NULL)
4697 {
4698 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
4699 p, cmdlinep);
4700 if (n == 2) /* p came from ExpandOne() */
4701 vim_free(p);
4702 }
4703 }
4704 }
4705 }
4706 return OK;
4707}
4708
4709/*
4710 * Replace part of the command line, keeping eap->cmd, eap->arg and
4711 * eap->nextcmd correct.
4712 * "src" points to the part that is to be replaced, of length "srclen".
4713 * "repl" is the replacement string.
4714 * Returns a pointer to the character after the replaced string.
4715 * Returns NULL for failure.
4716 */
4717 static char_u *
4718repl_cmdline(eap, src, srclen, repl, cmdlinep)
4719 exarg_T *eap;
4720 char_u *src;
4721 int srclen;
4722 char_u *repl;
4723 char_u **cmdlinep;
4724{
4725 int len;
4726 int i;
4727 char_u *new_cmdline;
4728
4729 /*
4730 * The new command line is build in new_cmdline[].
4731 * First allocate it.
4732 * Careful: a "+cmd" argument may have been NUL terminated.
4733 */
4734 len = (int)STRLEN(repl);
4735 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004736 if (eap->nextcmd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004737 i += (int)STRLEN(eap->nextcmd);/* add space for next command */
4738 if ((new_cmdline = alloc((unsigned)i)) == NULL)
4739 return NULL; /* out of memory! */
4740
4741 /*
4742 * Copy the stuff before the expanded part.
4743 * Copy the expanded stuff.
4744 * Copy what came after the expanded part.
4745 * Copy the next commands, if there are any.
4746 */
4747 i = (int)(src - *cmdlinep); /* length of part before match */
4748 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00004749
Bram Moolenaar071d4272004-06-13 20:20:40 +00004750 mch_memmove(new_cmdline + i, repl, (size_t)len);
4751 i += len; /* remember the end of the string */
4752 STRCPY(new_cmdline + i, src + srclen);
4753 src = new_cmdline + i; /* remember where to continue */
4754
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004755 if (eap->nextcmd != NULL) /* append next command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004756 {
4757 i = (int)STRLEN(new_cmdline) + 1;
4758 STRCPY(new_cmdline + i, eap->nextcmd);
4759 eap->nextcmd = new_cmdline + i;
4760 }
4761 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
4762 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
4763 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
4764 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
4765 vim_free(*cmdlinep);
4766 *cmdlinep = new_cmdline;
4767
4768 return src;
4769}
4770
4771/*
4772 * Check for '|' to separate commands and '"' to start comments.
4773 */
4774 void
4775separate_nextcmd(eap)
4776 exarg_T *eap;
4777{
4778 char_u *p;
4779
Bram Moolenaar86b68352004-12-27 21:59:20 +00004780#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004781 p = skip_grep_pat(eap);
4782#else
4783 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004784#endif
4785
4786 for ( ; *p; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004787 {
4788 if (*p == Ctrl_V)
4789 {
4790 if (eap->argt & (USECTRLV | XFILE))
4791 ++p; /* skip CTRL-V and next char */
4792 else
Bram Moolenaarb0db5692007-08-14 20:54:49 +00004793 /* remove CTRL-V and skip next char */
Bram Moolenaara7241f52008-06-24 20:39:31 +00004794 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004795 if (*p == NUL) /* stop at NUL after CTRL-V */
4796 break;
4797 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004798
4799#ifdef FEAT_EVAL
4800 /* Skip over `=expr` when wildcards are expanded. */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004801 else if (p[0] == '`' && p[1] == '=' && (eap->argt & XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004802 {
4803 p += 2;
4804 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004805 }
4806#endif
4807
Bram Moolenaar071d4272004-06-13 20:20:40 +00004808 /* Check for '"': start of comment or '|': next command */
4809 /* :@" and :*" do not start a comment!
4810 * :redir @" doesn't either. */
4811 else if ((*p == '"' && !(eap->argt & NOTRLCOM)
4812 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
4813 || p != eap->arg)
4814 && (eap->cmdidx != CMD_redir
4815 || p != eap->arg + 1 || p[-1] != '@'))
4816 || *p == '|' || *p == '\n')
4817 {
4818 /*
4819 * We remove the '\' before the '|', unless USECTRLV is used
4820 * AND 'b' is present in 'cpoptions'.
4821 */
4822 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
4823 || !(eap->argt & USECTRLV)) && *(p - 1) == '\\')
4824 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00004825 STRMOVE(p - 1, p); /* remove the '\' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004826 --p;
4827 }
4828 else
4829 {
4830 eap->nextcmd = check_nextcmd(p);
4831 *p = NUL;
4832 break;
4833 }
4834 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004835 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004836
Bram Moolenaar071d4272004-06-13 20:20:40 +00004837 if (!(eap->argt & NOTRLCOM)) /* remove trailing spaces */
4838 del_trailing_spaces(eap->arg);
4839}
4840
4841/*
4842 * get + command from ex argument
4843 */
4844 static char_u *
4845getargcmd(argp)
4846 char_u **argp;
4847{
4848 char_u *arg = *argp;
4849 char_u *command = NULL;
4850
4851 if (*arg == '+') /* +[command] */
4852 {
4853 ++arg;
4854 if (vim_isspace(*arg))
4855 command = dollar_command;
4856 else
4857 {
4858 command = arg;
4859 arg = skip_cmd_arg(command, TRUE);
4860 if (*arg != NUL)
4861 *arg++ = NUL; /* terminate command with NUL */
4862 }
4863
4864 arg = skipwhite(arg); /* skip over spaces */
4865 *argp = arg;
4866 }
4867 return command;
4868}
4869
4870/*
4871 * Find end of "+command" argument. Skip over "\ " and "\\".
4872 */
4873 static char_u *
4874skip_cmd_arg(p, rembs)
4875 char_u *p;
4876 int rembs; /* TRUE to halve the number of backslashes */
4877{
4878 while (*p && !vim_isspace(*p))
4879 {
4880 if (*p == '\\' && p[1] != NUL)
4881 {
4882 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00004883 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004884 else
4885 ++p;
4886 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004887 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004888 }
4889 return p;
4890}
4891
4892/*
4893 * Get "++opt=arg" argument.
4894 * Return FAIL or OK.
4895 */
4896 static int
4897getargopt(eap)
4898 exarg_T *eap;
4899{
4900 char_u *arg = eap->arg + 2;
4901 int *pp = NULL;
4902#ifdef FEAT_MBYTE
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004903 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004904 char_u *p;
4905#endif
4906
4907 /* ":edit ++[no]bin[ary] file" */
4908 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
4909 {
4910 if (*arg == 'n')
4911 {
4912 arg += 2;
4913 eap->force_bin = FORCE_NOBIN;
4914 }
4915 else
4916 eap->force_bin = FORCE_BIN;
4917 if (!checkforcmd(&arg, "binary", 3))
4918 return FAIL;
4919 eap->arg = skipwhite(arg);
4920 return OK;
4921 }
4922
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004923 /* ":read ++edit file" */
4924 if (STRNCMP(arg, "edit", 4) == 0)
4925 {
4926 eap->read_edit = TRUE;
4927 eap->arg = skipwhite(arg + 4);
4928 return OK;
4929 }
4930
Bram Moolenaar071d4272004-06-13 20:20:40 +00004931 if (STRNCMP(arg, "ff", 2) == 0)
4932 {
4933 arg += 2;
4934 pp = &eap->force_ff;
4935 }
4936 else if (STRNCMP(arg, "fileformat", 10) == 0)
4937 {
4938 arg += 10;
4939 pp = &eap->force_ff;
4940 }
4941#ifdef FEAT_MBYTE
4942 else if (STRNCMP(arg, "enc", 3) == 0)
4943 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01004944 if (STRNCMP(arg, "encoding", 8) == 0)
4945 arg += 8;
4946 else
4947 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004948 pp = &eap->force_enc;
4949 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004950 else if (STRNCMP(arg, "bad", 3) == 0)
4951 {
4952 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004953 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004954 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004955#endif
4956
4957 if (pp == NULL || *arg != '=')
4958 return FAIL;
4959
4960 ++arg;
4961 *pp = (int)(arg - eap->cmd);
4962 arg = skip_cmd_arg(arg, FALSE);
4963 eap->arg = skipwhite(arg);
4964 *arg = NUL;
4965
4966#ifdef FEAT_MBYTE
4967 if (pp == &eap->force_ff)
4968 {
4969#endif
4970 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
4971 return FAIL;
4972#ifdef FEAT_MBYTE
4973 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004974 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004975 {
4976 /* Make 'fileencoding' lower case. */
4977 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
4978 *p = TOLOWER_ASC(*p);
4979 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004980 else
4981 {
4982 /* Check ++bad= argument. Must be a single-byte character, "keep" or
4983 * "drop". */
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004984 p = eap->cmd + bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004985 if (STRICMP(p, "keep") == 0)
4986 eap->bad_char = BAD_KEEP;
4987 else if (STRICMP(p, "drop") == 0)
4988 eap->bad_char = BAD_DROP;
4989 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
4990 eap->bad_char = *p;
4991 else
4992 return FAIL;
4993 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004994#endif
4995
4996 return OK;
4997}
4998
4999/*
5000 * ":abbreviate" and friends.
5001 */
5002 static void
5003ex_abbreviate(eap)
5004 exarg_T *eap;
5005{
5006 do_exmap(eap, TRUE); /* almost the same as mapping */
5007}
5008
5009/*
5010 * ":map" and friends.
5011 */
5012 static void
5013ex_map(eap)
5014 exarg_T *eap;
5015{
5016 /*
5017 * If we are sourcing .exrc or .vimrc in current directory we
5018 * print the mappings for security reasons.
5019 */
5020 if (secure)
5021 {
5022 secure = 2;
5023 msg_outtrans(eap->cmd);
5024 msg_putchar('\n');
5025 }
5026 do_exmap(eap, FALSE);
5027}
5028
5029/*
5030 * ":unmap" and friends.
5031 */
5032 static void
5033ex_unmap(eap)
5034 exarg_T *eap;
5035{
5036 do_exmap(eap, FALSE);
5037}
5038
5039/*
5040 * ":mapclear" and friends.
5041 */
5042 static void
5043ex_mapclear(eap)
5044 exarg_T *eap;
5045{
5046 map_clear(eap->cmd, eap->arg, eap->forceit, FALSE);
5047}
5048
5049/*
5050 * ":abclear" and friends.
5051 */
5052 static void
5053ex_abclear(eap)
5054 exarg_T *eap;
5055{
5056 map_clear(eap->cmd, eap->arg, TRUE, TRUE);
5057}
5058
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005059#if defined(FEAT_AUTOCMD) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005060 static void
5061ex_autocmd(eap)
5062 exarg_T *eap;
5063{
5064 /*
5065 * Disallow auto commands from .exrc and .vimrc in current
5066 * directory for security reasons.
5067 */
5068 if (secure)
5069 {
5070 secure = 2;
5071 eap->errmsg = e_curdir;
5072 }
5073 else if (eap->cmdidx == CMD_autocmd)
5074 do_autocmd(eap->arg, eap->forceit);
5075 else
5076 do_augroup(eap->arg, eap->forceit);
5077}
5078
5079/*
5080 * ":doautocmd": Apply the automatic commands to the current buffer.
5081 */
5082 static void
5083ex_doautocmd(eap)
5084 exarg_T *eap;
5085{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005086 char_u *arg = eap->arg;
5087 int call_do_modelines = check_nomodeline(&arg);
5088
5089 (void)do_doautocmd(arg, TRUE);
5090 if (call_do_modelines) /* Only when there is no <nomodeline>. */
5091 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005092}
5093#endif
5094
5095#ifdef FEAT_LISTCMDS
5096/*
5097 * :[N]bunload[!] [N] [bufname] unload buffer
5098 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
5099 * :[N]bwipeout[!] [N] [bufname] delete buffer really
5100 */
5101 static void
5102ex_bunload(eap)
5103 exarg_T *eap;
5104{
5105 eap->errmsg = do_bufdel(
5106 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
5107 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
5108 : DOBUF_UNLOAD, eap->arg,
5109 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
5110}
5111
5112/*
5113 * :[N]buffer [N] to buffer N
5114 * :[N]sbuffer [N] to buffer N
5115 */
5116 static void
5117ex_buffer(eap)
5118 exarg_T *eap;
5119{
5120 if (*eap->arg)
5121 eap->errmsg = e_trailing;
5122 else
5123 {
5124 if (eap->addr_count == 0) /* default is current buffer */
5125 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
5126 else
5127 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
5128 }
5129}
5130
5131/*
5132 * :[N]bmodified [N] to next mod. buffer
5133 * :[N]sbmodified [N] to next mod. buffer
5134 */
5135 static void
5136ex_bmodified(eap)
5137 exarg_T *eap;
5138{
5139 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
5140}
5141
5142/*
5143 * :[N]bnext [N] to next buffer
5144 * :[N]sbnext [N] split and to next buffer
5145 */
5146 static void
5147ex_bnext(eap)
5148 exarg_T *eap;
5149{
5150 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
5151}
5152
5153/*
5154 * :[N]bNext [N] to previous buffer
5155 * :[N]bprevious [N] to previous buffer
5156 * :[N]sbNext [N] split and to previous buffer
5157 * :[N]sbprevious [N] split and to previous buffer
5158 */
5159 static void
5160ex_bprevious(eap)
5161 exarg_T *eap;
5162{
5163 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
5164}
5165
5166/*
5167 * :brewind to first buffer
5168 * :bfirst to first buffer
5169 * :sbrewind split and to first buffer
5170 * :sbfirst split and to first buffer
5171 */
5172 static void
5173ex_brewind(eap)
5174 exarg_T *eap;
5175{
5176 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
5177}
5178
5179/*
5180 * :blast to last buffer
5181 * :sblast split and to last buffer
5182 */
5183 static void
5184ex_blast(eap)
5185 exarg_T *eap;
5186{
5187 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
5188}
5189#endif
5190
5191 int
5192ends_excmd(c)
5193 int c;
5194{
5195 return (c == NUL || c == '|' || c == '"' || c == '\n');
5196}
5197
5198#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5199 || defined(PROTO)
5200/*
5201 * Return the next command, after the first '|' or '\n'.
5202 * Return NULL if not found.
5203 */
5204 char_u *
5205find_nextcmd(p)
5206 char_u *p;
5207{
5208 while (*p != '|' && *p != '\n')
5209 {
5210 if (*p == NUL)
5211 return NULL;
5212 ++p;
5213 }
5214 return (p + 1);
5215}
5216#endif
5217
5218/*
5219 * Check if *p is a separator between Ex commands.
5220 * Return NULL if it isn't, (p + 1) if it is.
5221 */
5222 char_u *
5223check_nextcmd(p)
5224 char_u *p;
5225{
5226 p = skipwhite(p);
5227 if (*p == '|' || *p == '\n')
5228 return (p + 1);
5229 else
5230 return NULL;
5231}
5232
5233/*
5234 * - if there are more files to edit
5235 * - and this is the last window
5236 * - and forceit not used
5237 * - and not repeated twice on a row
5238 * return FAIL and give error message if 'message' TRUE
5239 * return OK otherwise
5240 */
5241 static int
5242check_more(message, forceit)
5243 int message; /* when FALSE check only, no messages */
5244 int forceit;
5245{
5246 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5247
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005248 if (!forceit && only_one_window()
5249 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005250 {
5251 if (message)
5252 {
5253#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
5254 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
5255 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005256 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005257
5258 if (n == 1)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02005259 vim_strncpy(buff,
5260 (char_u *)_("1 more file to edit. Quit anyway?"),
Bram Moolenaard9462e32011-04-11 21:35:11 +02005261 DIALOG_MSG_SIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005262 else
Bram Moolenaard9462e32011-04-11 21:35:11 +02005263 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005264 _("%d more files to edit. Quit anyway?"), n);
5265 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5266 return OK;
5267 return FAIL;
5268 }
5269#endif
5270 if (n == 1)
5271 EMSG(_("E173: 1 more file to edit"));
5272 else
5273 EMSGN(_("E173: %ld more files to edit"), n);
5274 quitmore = 2; /* next try to quit is allowed */
5275 }
5276 return FAIL;
5277 }
5278 return OK;
5279}
5280
5281#ifdef FEAT_CMDL_COMPL
5282/*
5283 * Function given to ExpandGeneric() to obtain the list of command names.
5284 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005285 char_u *
5286get_command_name(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00005287 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005288 int idx;
5289{
5290 if (idx >= (int)CMD_SIZE)
5291# ifdef FEAT_USR_CMDS
5292 return get_user_command_name(idx);
5293# else
5294 return NULL;
5295# endif
5296 return cmdnames[idx].cmd_name;
5297}
5298#endif
5299
5300#if defined(FEAT_USR_CMDS) || defined(PROTO)
5301static 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));
5302static void uc_list __ARGS((char_u *name, size_t name_len));
5303static int uc_scan_attr __ARGS((char_u *attr, size_t len, long *argt, long *def, int *flags, int *compl, char_u **compl_arg));
5304static char_u *uc_split_args __ARGS((char_u *arg, size_t *lenp));
5305static 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));
5306
5307 static int
5308uc_add_command(name, name_len, rep, argt, def, flags, compl, compl_arg, force)
5309 char_u *name;
5310 size_t name_len;
5311 char_u *rep;
5312 long argt;
5313 long def;
5314 int flags;
5315 int compl;
5316 char_u *compl_arg;
5317 int force;
5318{
5319 ucmd_T *cmd = NULL;
5320 char_u *p;
5321 int i;
5322 int cmp = 1;
5323 char_u *rep_buf = NULL;
5324 garray_T *gap;
5325
Bram Moolenaar9c102382006-05-03 21:26:49 +00005326 replace_termcodes(rep, &rep_buf, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005327 if (rep_buf == NULL)
5328 {
5329 /* Can't replace termcodes - try using the string as is */
5330 rep_buf = vim_strsave(rep);
5331
5332 /* Give up if out of memory */
5333 if (rep_buf == NULL)
5334 return FAIL;
5335 }
5336
5337 /* get address of growarray: global or in curbuf */
5338 if (flags & UC_BUFFER)
5339 {
5340 gap = &curbuf->b_ucmds;
5341 if (gap->ga_itemsize == 0)
5342 ga_init2(gap, (int)sizeof(ucmd_T), 4);
5343 }
5344 else
5345 gap = &ucmds;
5346
5347 /* Search for the command in the already defined commands. */
5348 for (i = 0; i < gap->ga_len; ++i)
5349 {
5350 size_t len;
5351
5352 cmd = USER_CMD_GA(gap, i);
5353 len = STRLEN(cmd->uc_name);
5354 cmp = STRNCMP(name, cmd->uc_name, name_len);
5355 if (cmp == 0)
5356 {
5357 if (name_len < len)
5358 cmp = -1;
5359 else if (name_len > len)
5360 cmp = 1;
5361 }
5362
5363 if (cmp == 0)
5364 {
5365 if (!force)
5366 {
5367 EMSG(_("E174: Command already exists: add ! to replace it"));
5368 goto fail;
5369 }
5370
5371 vim_free(cmd->uc_rep);
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00005372 cmd->uc_rep = NULL;
5373#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5374 vim_free(cmd->uc_compl_arg);
5375 cmd->uc_compl_arg = NULL;
5376#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005377 break;
5378 }
5379
5380 /* Stop as soon as we pass the name to add */
5381 if (cmp < 0)
5382 break;
5383 }
5384
5385 /* Extend the array unless we're replacing an existing command */
5386 if (cmp != 0)
5387 {
5388 if (ga_grow(gap, 1) != OK)
5389 goto fail;
5390 if ((p = vim_strnsave(name, (int)name_len)) == NULL)
5391 goto fail;
5392
5393 cmd = USER_CMD_GA(gap, i);
5394 mch_memmove(cmd + 1, cmd, (gap->ga_len - i) * sizeof(ucmd_T));
5395
5396 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005397
5398 cmd->uc_name = p;
5399 }
5400
5401 cmd->uc_rep = rep_buf;
5402 cmd->uc_argt = argt;
5403 cmd->uc_def = def;
5404 cmd->uc_compl = compl;
5405#ifdef FEAT_EVAL
5406 cmd->uc_scriptID = current_SID;
5407# ifdef FEAT_CMDL_COMPL
5408 cmd->uc_compl_arg = compl_arg;
5409# endif
5410#endif
5411
5412 return OK;
5413
5414fail:
5415 vim_free(rep_buf);
5416#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5417 vim_free(compl_arg);
5418#endif
5419 return FAIL;
5420}
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005421#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005422
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005423#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005424/*
5425 * List of names for completion for ":command" with the EXPAND_ flag.
5426 * Must be alphabetical for completion.
5427 */
5428static struct
5429{
5430 int expand;
5431 char *name;
5432} command_complete[] =
5433{
5434 {EXPAND_AUGROUP, "augroup"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005435 {EXPAND_BEHAVE, "behave"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005436 {EXPAND_BUFFERS, "buffer"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005437 {EXPAND_COLORS, "color"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005438 {EXPAND_COMMANDS, "command"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005439 {EXPAND_COMPILER, "compiler"},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005440#if defined(FEAT_CSCOPE)
5441 {EXPAND_CSCOPE, "cscope"},
5442#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005443#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5444 {EXPAND_USER_DEFINED, "custom"},
Bram Moolenaara466c992005-07-09 21:03:22 +00005445 {EXPAND_USER_LIST, "customlist"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005446#endif
5447 {EXPAND_DIRECTORIES, "dir"},
5448 {EXPAND_ENV_VARS, "environment"},
5449 {EXPAND_EVENTS, "event"},
5450 {EXPAND_EXPRESSION, "expression"},
5451 {EXPAND_FILES, "file"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005452 {EXPAND_FILES_IN_PATH, "file_in_path"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005453 {EXPAND_FILETYPE, "filetype"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005454 {EXPAND_FUNCTIONS, "function"},
5455 {EXPAND_HELP, "help"},
5456 {EXPAND_HIGHLIGHT, "highlight"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005457#if defined(FEAT_CMDHIST)
5458 {EXPAND_HISTORY, "history"},
5459#endif
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005460#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005461 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005462 {EXPAND_LOCALES, "locale"},
5463#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005464 {EXPAND_MAPPINGS, "mapping"},
5465 {EXPAND_MENUS, "menu"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005466 {EXPAND_OWNSYNTAX, "syntax"},
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02005467#if defined(FEAT_PROFILE)
5468 {EXPAND_SYNTIME, "syntime"},
5469#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005470 {EXPAND_SETTINGS, "option"},
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005471 {EXPAND_SHELLCMD, "shellcmd"},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005472#if defined(FEAT_SIGNS)
5473 {EXPAND_SIGN, "sign"},
5474#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005475 {EXPAND_TAGS, "tag"},
5476 {EXPAND_TAGS_LISTFILES, "tag_listfiles"},
Bram Moolenaar24305862012-08-15 14:05:05 +02005477 {EXPAND_USER, "user"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005478 {EXPAND_USER_VARS, "var"},
5479 {0, NULL}
5480};
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005481#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005482
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005483#if defined(FEAT_USR_CMDS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005484 static void
5485uc_list(name, name_len)
5486 char_u *name;
5487 size_t name_len;
5488{
5489 int i, j;
5490 int found = FALSE;
5491 ucmd_T *cmd;
5492 int len;
5493 long a;
5494 garray_T *gap;
5495
5496 gap = &curbuf->b_ucmds;
5497 for (;;)
5498 {
5499 for (i = 0; i < gap->ga_len; ++i)
5500 {
5501 cmd = USER_CMD_GA(gap, i);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005502 a = (long)cmd->uc_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005503
5504 /* Skip commands which don't match the requested prefix */
5505 if (STRNCMP(name, cmd->uc_name, name_len) != 0)
5506 continue;
5507
5508 /* Put out the title first time */
5509 if (!found)
5510 MSG_PUTS_TITLE(_("\n Name Args Range Complete Definition"));
5511 found = TRUE;
5512 msg_putchar('\n');
5513 if (got_int)
5514 break;
5515
5516 /* Special cases */
5517 msg_putchar(a & BANG ? '!' : ' ');
5518 msg_putchar(a & REGSTR ? '"' : ' ');
5519 msg_putchar(gap != &ucmds ? 'b' : ' ');
5520 msg_putchar(' ');
5521
5522 msg_outtrans_attr(cmd->uc_name, hl_attr(HLF_D));
5523 len = (int)STRLEN(cmd->uc_name) + 4;
5524
5525 do {
5526 msg_putchar(' ');
5527 ++len;
5528 } while (len < 16);
5529
5530 len = 0;
5531
5532 /* Arguments */
5533 switch ((int)(a & (EXTRA|NOSPC|NEEDARG)))
5534 {
5535 case 0: IObuff[len++] = '0'; break;
5536 case (EXTRA): IObuff[len++] = '*'; break;
5537 case (EXTRA|NOSPC): IObuff[len++] = '?'; break;
5538 case (EXTRA|NEEDARG): IObuff[len++] = '+'; break;
5539 case (EXTRA|NOSPC|NEEDARG): IObuff[len++] = '1'; break;
5540 }
5541
5542 do {
5543 IObuff[len++] = ' ';
5544 } while (len < 5);
5545
5546 /* Range */
5547 if (a & (RANGE|COUNT))
5548 {
5549 if (a & COUNT)
5550 {
5551 /* -count=N */
5552 sprintf((char *)IObuff + len, "%ldc", cmd->uc_def);
5553 len += (int)STRLEN(IObuff + len);
5554 }
5555 else if (a & DFLALL)
5556 IObuff[len++] = '%';
5557 else if (cmd->uc_def >= 0)
5558 {
5559 /* -range=N */
5560 sprintf((char *)IObuff + len, "%ld", cmd->uc_def);
5561 len += (int)STRLEN(IObuff + len);
5562 }
5563 else
5564 IObuff[len++] = '.';
5565 }
5566
5567 do {
5568 IObuff[len++] = ' ';
5569 } while (len < 11);
5570
5571 /* Completion */
5572 for (j = 0; command_complete[j].expand != 0; ++j)
5573 if (command_complete[j].expand == cmd->uc_compl)
5574 {
5575 STRCPY(IObuff + len, command_complete[j].name);
5576 len += (int)STRLEN(IObuff + len);
5577 break;
5578 }
5579
5580 do {
5581 IObuff[len++] = ' ';
5582 } while (len < 21);
5583
5584 IObuff[len] = '\0';
5585 msg_outtrans(IObuff);
5586
5587 msg_outtrans_special(cmd->uc_rep, FALSE);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005588#ifdef FEAT_EVAL
5589 if (p_verbose > 0)
5590 last_set_msg(cmd->uc_scriptID);
5591#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005592 out_flush();
5593 ui_breakcheck();
5594 if (got_int)
5595 break;
5596 }
5597 if (gap == &ucmds || i < gap->ga_len)
5598 break;
5599 gap = &ucmds;
5600 }
5601
5602 if (!found)
5603 MSG(_("No user-defined commands found"));
5604}
5605
5606 static char_u *
5607uc_fun_cmd()
5608{
5609 static char_u fcmd[] = {0x84, 0xaf, 0x60, 0xb9, 0xaf, 0xb5, 0x60, 0xa4,
5610 0xa5, 0xad, 0xa1, 0xae, 0xa4, 0x60, 0xa1, 0x60,
5611 0xb3, 0xa8, 0xb2, 0xb5, 0xa2, 0xa2, 0xa5, 0xb2,
5612 0xb9, 0x7f, 0};
5613 int i;
5614
5615 for (i = 0; fcmd[i]; ++i)
5616 IObuff[i] = fcmd[i] - 0x40;
5617 IObuff[i] = 0;
5618 return IObuff;
5619}
5620
5621 static int
5622uc_scan_attr(attr, len, argt, def, flags, compl, compl_arg)
5623 char_u *attr;
5624 size_t len;
5625 long *argt;
5626 long *def;
5627 int *flags;
5628 int *compl;
5629 char_u **compl_arg;
5630{
5631 char_u *p;
5632
5633 if (len == 0)
5634 {
5635 EMSG(_("E175: No attribute specified"));
5636 return FAIL;
5637 }
5638
5639 /* First, try the simple attributes (no arguments) */
5640 if (STRNICMP(attr, "bang", len) == 0)
5641 *argt |= BANG;
5642 else if (STRNICMP(attr, "buffer", len) == 0)
5643 *flags |= UC_BUFFER;
5644 else if (STRNICMP(attr, "register", len) == 0)
5645 *argt |= REGSTR;
5646 else if (STRNICMP(attr, "bar", len) == 0)
5647 *argt |= TRLBAR;
5648 else
5649 {
5650 int i;
5651 char_u *val = NULL;
5652 size_t vallen = 0;
5653 size_t attrlen = len;
5654
5655 /* Look for the attribute name - which is the part before any '=' */
5656 for (i = 0; i < (int)len; ++i)
5657 {
5658 if (attr[i] == '=')
5659 {
5660 val = &attr[i + 1];
5661 vallen = len - i - 1;
5662 attrlen = i;
5663 break;
5664 }
5665 }
5666
5667 if (STRNICMP(attr, "nargs", attrlen) == 0)
5668 {
5669 if (vallen == 1)
5670 {
5671 if (*val == '0')
5672 /* Do nothing - this is the default */;
5673 else if (*val == '1')
5674 *argt |= (EXTRA | NOSPC | NEEDARG);
5675 else if (*val == '*')
5676 *argt |= EXTRA;
5677 else if (*val == '?')
5678 *argt |= (EXTRA | NOSPC);
5679 else if (*val == '+')
5680 *argt |= (EXTRA | NEEDARG);
5681 else
5682 goto wrong_nargs;
5683 }
5684 else
5685 {
5686wrong_nargs:
5687 EMSG(_("E176: Invalid number of arguments"));
5688 return FAIL;
5689 }
5690 }
5691 else if (STRNICMP(attr, "range", attrlen) == 0)
5692 {
5693 *argt |= RANGE;
5694 if (vallen == 1 && *val == '%')
5695 *argt |= DFLALL;
5696 else if (val != NULL)
5697 {
5698 p = val;
5699 if (*def >= 0)
5700 {
5701two_count:
5702 EMSG(_("E177: Count cannot be specified twice"));
5703 return FAIL;
5704 }
5705
5706 *def = getdigits(&p);
5707 *argt |= (ZEROR | NOTADR);
5708
5709 if (p != val + vallen || vallen == 0)
5710 {
5711invalid_count:
5712 EMSG(_("E178: Invalid default value for count"));
5713 return FAIL;
5714 }
5715 }
5716 }
5717 else if (STRNICMP(attr, "count", attrlen) == 0)
5718 {
Bram Moolenaar32e7b2d2005-02-27 22:36:47 +00005719 *argt |= (COUNT | ZEROR | RANGE | NOTADR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005720
5721 if (val != NULL)
5722 {
5723 p = val;
5724 if (*def >= 0)
5725 goto two_count;
5726
5727 *def = getdigits(&p);
5728
5729 if (p != val + vallen)
5730 goto invalid_count;
5731 }
5732
5733 if (*def < 0)
5734 *def = 0;
5735 }
5736 else if (STRNICMP(attr, "complete", attrlen) == 0)
5737 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005738 if (val == NULL)
5739 {
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005740 EMSG(_("E179: argument required for -complete"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005741 return FAIL;
5742 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005743
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005744 if (parse_compl_arg(val, (int)vallen, compl, argt, compl_arg)
5745 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005746 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005747 }
5748 else
5749 {
5750 char_u ch = attr[len];
5751 attr[len] = '\0';
5752 EMSG2(_("E181: Invalid attribute: %s"), attr);
5753 attr[len] = ch;
5754 return FAIL;
5755 }
5756 }
5757
5758 return OK;
5759}
5760
Bram Moolenaara850a712009-01-28 14:42:59 +00005761/*
5762 * ":command ..."
5763 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005764 static void
5765ex_command(eap)
5766 exarg_T *eap;
5767{
5768 char_u *name;
5769 char_u *end;
5770 char_u *p;
5771 long argt = 0;
5772 long def = -1;
5773 int flags = 0;
5774 int compl = EXPAND_NOTHING;
5775 char_u *compl_arg = NULL;
5776 int has_attr = (eap->arg[0] == '-');
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01005777 int name_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005778
5779 p = eap->arg;
5780
5781 /* Check for attributes */
5782 while (*p == '-')
5783 {
5784 ++p;
5785 end = skiptowhite(p);
5786 if (uc_scan_attr(p, end - p, &argt, &def, &flags, &compl, &compl_arg)
5787 == FAIL)
5788 return;
5789 p = skipwhite(end);
5790 }
5791
5792 /* Get the name (if any) and skip to the following argument */
5793 name = p;
5794 if (ASCII_ISALPHA(*p))
5795 while (ASCII_ISALNUM(*p))
5796 ++p;
5797 if (!ends_excmd(*p) && !vim_iswhite(*p))
5798 {
5799 EMSG(_("E182: Invalid command name"));
5800 return;
5801 }
5802 end = p;
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01005803 name_len = (int)(end - name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005804
5805 /* If there is nothing after the name, and no attributes were specified,
5806 * we are listing commands
5807 */
5808 p = skipwhite(end);
5809 if (!has_attr && ends_excmd(*p))
5810 {
5811 uc_list(name, end - name);
5812 }
5813 else if (!ASCII_ISUPPER(*name))
5814 {
5815 EMSG(_("E183: User defined commands must start with an uppercase letter"));
5816 return;
5817 }
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01005818 else if ((name_len == 1 && *name == 'X')
5819 || (name_len <= 4
5820 && STRNCMP(name, "Next", name_len > 4 ? 4 : name_len) == 0))
5821 {
5822 EMSG(_("E841: Reserved name, cannot be used for user defined command"));
5823 return;
5824 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005825 else
5826 uc_add_command(name, end - name, p, argt, def, flags, compl, compl_arg,
5827 eap->forceit);
5828}
5829
5830/*
5831 * ":comclear"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005832 * Clear all user commands, global and for current buffer.
5833 */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00005834 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005835ex_comclear(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00005836 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005837{
5838 uc_clear(&ucmds);
5839 uc_clear(&curbuf->b_ucmds);
5840}
5841
5842/*
5843 * Clear all user commands for "gap".
5844 */
5845 void
5846uc_clear(gap)
5847 garray_T *gap;
5848{
5849 int i;
5850 ucmd_T *cmd;
5851
5852 for (i = 0; i < gap->ga_len; ++i)
5853 {
5854 cmd = USER_CMD_GA(gap, i);
5855 vim_free(cmd->uc_name);
5856 vim_free(cmd->uc_rep);
5857# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5858 vim_free(cmd->uc_compl_arg);
5859# endif
5860 }
5861 ga_clear(gap);
5862}
5863
5864 static void
5865ex_delcommand(eap)
5866 exarg_T *eap;
5867{
5868 int i = 0;
5869 ucmd_T *cmd = NULL;
5870 int cmp = -1;
5871 garray_T *gap;
5872
5873 gap = &curbuf->b_ucmds;
5874 for (;;)
5875 {
5876 for (i = 0; i < gap->ga_len; ++i)
5877 {
5878 cmd = USER_CMD_GA(gap, i);
5879 cmp = STRCMP(eap->arg, cmd->uc_name);
5880 if (cmp <= 0)
5881 break;
5882 }
5883 if (gap == &ucmds || cmp == 0)
5884 break;
5885 gap = &ucmds;
5886 }
5887
5888 if (cmp != 0)
5889 {
5890 EMSG2(_("E184: No such user-defined command: %s"), eap->arg);
5891 return;
5892 }
5893
5894 vim_free(cmd->uc_name);
5895 vim_free(cmd->uc_rep);
5896# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5897 vim_free(cmd->uc_compl_arg);
5898# endif
5899
5900 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005901
5902 if (i < gap->ga_len)
5903 mch_memmove(cmd, cmd + 1, (gap->ga_len - i) * sizeof(ucmd_T));
5904}
5905
Bram Moolenaar552f8a12007-03-08 17:12:08 +00005906/*
5907 * split and quote args for <f-args>
5908 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005909 static char_u *
5910uc_split_args(arg, lenp)
5911 char_u *arg;
5912 size_t *lenp;
5913{
5914 char_u *buf;
5915 char_u *p;
5916 char_u *q;
5917 int len;
5918
5919 /* Precalculate length */
5920 p = arg;
5921 len = 2; /* Initial and final quotes */
5922
5923 while (*p)
5924 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00005925 if (p[0] == '\\' && p[1] == '\\')
5926 {
5927 len += 2;
5928 p += 2;
5929 }
5930 else if (p[0] == '\\' && vim_iswhite(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005931 {
5932 len += 1;
5933 p += 2;
5934 }
5935 else if (*p == '\\' || *p == '"')
5936 {
5937 len += 2;
5938 p += 1;
5939 }
5940 else if (vim_iswhite(*p))
5941 {
5942 p = skipwhite(p);
5943 if (*p == NUL)
5944 break;
5945 len += 3; /* "," */
5946 }
5947 else
5948 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02005949#ifdef FEAT_MBYTE
5950 int charlen = (*mb_ptr2len)(p);
5951 len += charlen;
5952 p += charlen;
5953#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005954 ++len;
5955 ++p;
Bram Moolenaardfef1542012-07-10 19:25:10 +02005956#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005957 }
5958 }
5959
5960 buf = alloc(len + 1);
5961 if (buf == NULL)
5962 {
5963 *lenp = 0;
5964 return buf;
5965 }
5966
5967 p = arg;
5968 q = buf;
5969 *q++ = '"';
5970 while (*p)
5971 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00005972 if (p[0] == '\\' && p[1] == '\\')
5973 {
5974 *q++ = '\\';
5975 *q++ = '\\';
5976 p += 2;
5977 }
5978 else if (p[0] == '\\' && vim_iswhite(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005979 {
5980 *q++ = p[1];
5981 p += 2;
5982 }
5983 else if (*p == '\\' || *p == '"')
5984 {
5985 *q++ = '\\';
5986 *q++ = *p++;
5987 }
5988 else if (vim_iswhite(*p))
5989 {
5990 p = skipwhite(p);
5991 if (*p == NUL)
5992 break;
5993 *q++ = '"';
5994 *q++ = ',';
5995 *q++ = '"';
5996 }
5997 else
5998 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02005999 MB_COPY_CHAR(p, q);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006000 }
6001 }
6002 *q++ = '"';
6003 *q = 0;
6004
6005 *lenp = len;
6006 return buf;
6007}
6008
6009/*
6010 * Check for a <> code in a user command.
6011 * "code" points to the '<'. "len" the length of the <> (inclusive).
6012 * "buf" is where the result is to be added.
6013 * "split_buf" points to a buffer used for splitting, caller should free it.
6014 * "split_len" is the length of what "split_buf" contains.
6015 * Returns the length of the replacement, which has been added to "buf".
6016 * Returns -1 if there was no match, and only the "<" has been copied.
6017 */
6018 static size_t
6019uc_check_code(code, len, buf, cmd, eap, split_buf, split_len)
6020 char_u *code;
6021 size_t len;
6022 char_u *buf;
6023 ucmd_T *cmd; /* the user command we're expanding */
6024 exarg_T *eap; /* ex arguments */
6025 char_u **split_buf;
6026 size_t *split_len;
6027{
6028 size_t result = 0;
6029 char_u *p = code + 1;
6030 size_t l = len - 2;
6031 int quote = 0;
6032 enum { ct_ARGS, ct_BANG, ct_COUNT, ct_LINE1, ct_LINE2, ct_REGISTER,
6033 ct_LT, ct_NONE } type = ct_NONE;
6034
6035 if ((vim_strchr((char_u *)"qQfF", *p) != NULL) && p[1] == '-')
6036 {
6037 quote = (*p == 'q' || *p == 'Q') ? 1 : 2;
6038 p += 2;
6039 l -= 2;
6040 }
6041
Bram Moolenaar371d5402006-03-20 21:47:49 +00006042 ++l;
6043 if (l <= 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006044 type = ct_NONE;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006045 else if (STRNICMP(p, "args>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006046 type = ct_ARGS;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006047 else if (STRNICMP(p, "bang>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006048 type = ct_BANG;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006049 else if (STRNICMP(p, "count>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006050 type = ct_COUNT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006051 else if (STRNICMP(p, "line1>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006052 type = ct_LINE1;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006053 else if (STRNICMP(p, "line2>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006054 type = ct_LINE2;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006055 else if (STRNICMP(p, "lt>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006056 type = ct_LT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006057 else if (STRNICMP(p, "reg>", l) == 0 || STRNICMP(p, "register>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006058 type = ct_REGISTER;
6059
6060 switch (type)
6061 {
6062 case ct_ARGS:
6063 /* Simple case first */
6064 if (*eap->arg == NUL)
6065 {
6066 if (quote == 1)
6067 {
6068 result = 2;
6069 if (buf != NULL)
6070 STRCPY(buf, "''");
6071 }
6072 else
6073 result = 0;
6074 break;
6075 }
6076
6077 /* When specified there is a single argument don't split it.
6078 * Works for ":Cmd %" when % is "a b c". */
6079 if ((eap->argt & NOSPC) && quote == 2)
6080 quote = 1;
6081
6082 switch (quote)
6083 {
6084 case 0: /* No quoting, no splitting */
6085 result = STRLEN(eap->arg);
6086 if (buf != NULL)
6087 STRCPY(buf, eap->arg);
6088 break;
6089 case 1: /* Quote, but don't split */
6090 result = STRLEN(eap->arg) + 2;
6091 for (p = eap->arg; *p; ++p)
6092 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006093#ifdef FEAT_MBYTE
6094 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6095 /* DBCS can contain \ in a trail byte, skip the
6096 * double-byte character. */
6097 ++p;
6098 else
6099#endif
6100 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006101 ++result;
6102 }
6103
6104 if (buf != NULL)
6105 {
6106 *buf++ = '"';
6107 for (p = eap->arg; *p; ++p)
6108 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006109#ifdef FEAT_MBYTE
6110 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6111 /* DBCS can contain \ in a trail byte, copy the
6112 * double-byte character to avoid escaping. */
6113 *buf++ = *p++;
6114 else
6115#endif
6116 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006117 *buf++ = '\\';
6118 *buf++ = *p;
6119 }
6120 *buf = '"';
6121 }
6122
6123 break;
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006124 case 2: /* Quote and split (<f-args>) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006125 /* This is hard, so only do it once, and cache the result */
6126 if (*split_buf == NULL)
6127 *split_buf = uc_split_args(eap->arg, split_len);
6128
6129 result = *split_len;
6130 if (buf != NULL && result != 0)
6131 STRCPY(buf, *split_buf);
6132
6133 break;
6134 }
6135 break;
6136
6137 case ct_BANG:
6138 result = eap->forceit ? 1 : 0;
6139 if (quote)
6140 result += 2;
6141 if (buf != NULL)
6142 {
6143 if (quote)
6144 *buf++ = '"';
6145 if (eap->forceit)
6146 *buf++ = '!';
6147 if (quote)
6148 *buf = '"';
6149 }
6150 break;
6151
6152 case ct_LINE1:
6153 case ct_LINE2:
6154 case ct_COUNT:
6155 {
6156 char num_buf[20];
6157 long num = (type == ct_LINE1) ? eap->line1 :
6158 (type == ct_LINE2) ? eap->line2 :
6159 (eap->addr_count > 0) ? eap->line2 : cmd->uc_def;
6160 size_t num_len;
6161
6162 sprintf(num_buf, "%ld", num);
6163 num_len = STRLEN(num_buf);
6164 result = num_len;
6165
6166 if (quote)
6167 result += 2;
6168
6169 if (buf != NULL)
6170 {
6171 if (quote)
6172 *buf++ = '"';
6173 STRCPY(buf, num_buf);
6174 buf += num_len;
6175 if (quote)
6176 *buf = '"';
6177 }
6178
6179 break;
6180 }
6181
6182 case ct_REGISTER:
6183 result = eap->regname ? 1 : 0;
6184 if (quote)
6185 result += 2;
6186 if (buf != NULL)
6187 {
6188 if (quote)
6189 *buf++ = '\'';
6190 if (eap->regname)
6191 *buf++ = eap->regname;
6192 if (quote)
6193 *buf = '\'';
6194 }
6195 break;
6196
6197 case ct_LT:
6198 result = 1;
6199 if (buf != NULL)
6200 *buf = '<';
6201 break;
6202
6203 default:
6204 /* Not recognized: just copy the '<' and return -1. */
6205 result = (size_t)-1;
6206 if (buf != NULL)
6207 *buf = '<';
6208 break;
6209 }
6210
6211 return result;
6212}
6213
6214 static void
6215do_ucmd(eap)
6216 exarg_T *eap;
6217{
6218 char_u *buf;
6219 char_u *p;
6220 char_u *q;
6221
6222 char_u *start;
Bram Moolenaar25648a52009-02-21 19:37:46 +00006223 char_u *end = NULL;
Bram Moolenaara850a712009-01-28 14:42:59 +00006224 char_u *ksp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006225 size_t len, totlen;
6226
6227 size_t split_len = 0;
6228 char_u *split_buf = NULL;
6229 ucmd_T *cmd;
6230#ifdef FEAT_EVAL
6231 scid_T save_current_SID = current_SID;
6232#endif
6233
6234 if (eap->cmdidx == CMD_USER)
6235 cmd = USER_CMD(eap->useridx);
6236 else
6237 cmd = USER_CMD_GA(&curbuf->b_ucmds, eap->useridx);
6238
6239 /*
6240 * Replace <> in the command by the arguments.
Bram Moolenaara850a712009-01-28 14:42:59 +00006241 * First round: "buf" is NULL, compute length, allocate "buf".
6242 * Second round: copy result into "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006243 */
6244 buf = NULL;
6245 for (;;)
6246 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006247 p = cmd->uc_rep; /* source */
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00006248 q = buf; /* destination */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006249 totlen = 0;
Bram Moolenaara850a712009-01-28 14:42:59 +00006250
6251 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006252 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006253 start = vim_strchr(p, '<');
6254 if (start != NULL)
6255 end = vim_strchr(start + 1, '>');
6256 if (buf != NULL)
6257 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006258 for (ksp = p; *ksp != NUL && *ksp != K_SPECIAL; ++ksp)
6259 ;
6260 if (*ksp == K_SPECIAL
6261 && (start == NULL || ksp < start || end == NULL)
Bram Moolenaara850a712009-01-28 14:42:59 +00006262 && ((ksp[1] == KS_SPECIAL && ksp[2] == KE_FILLER)
6263# ifdef FEAT_GUI
6264 || (ksp[1] == KS_EXTRA && ksp[2] == (int)KE_CSI)
6265# endif
6266 ))
6267 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006268 /* K_SPECIAL has been put in the buffer as K_SPECIAL
Bram Moolenaara850a712009-01-28 14:42:59 +00006269 * KS_SPECIAL KE_FILLER, like for mappings, but
6270 * do_cmdline() doesn't handle that, so convert it back.
6271 * Also change K_SPECIAL KS_EXTRA KE_CSI into CSI. */
6272 len = ksp - p;
6273 if (len > 0)
6274 {
6275 mch_memmove(q, p, len);
6276 q += len;
6277 }
6278 *q++ = ksp[1] == KS_SPECIAL ? K_SPECIAL : CSI;
6279 p = ksp + 3;
6280 continue;
6281 }
6282 }
6283
6284 /* break if there no <item> is found */
6285 if (start == NULL || end == NULL)
6286 break;
6287
Bram Moolenaar071d4272004-06-13 20:20:40 +00006288 /* Include the '>' */
6289 ++end;
6290
6291 /* Take everything up to the '<' */
6292 len = start - p;
6293 if (buf == NULL)
6294 totlen += len;
6295 else
6296 {
6297 mch_memmove(q, p, len);
6298 q += len;
6299 }
6300
6301 len = uc_check_code(start, end - start, q, cmd, eap,
6302 &split_buf, &split_len);
6303 if (len == (size_t)-1)
6304 {
6305 /* no match, continue after '<' */
6306 p = start + 1;
6307 len = 1;
6308 }
6309 else
6310 p = end;
6311 if (buf == NULL)
6312 totlen += len;
6313 else
6314 q += len;
6315 }
6316 if (buf != NULL) /* second time here, finished */
6317 {
6318 STRCPY(q, p);
6319 break;
6320 }
6321
6322 totlen += STRLEN(p); /* Add on the trailing characters */
6323 buf = alloc((unsigned)(totlen + 1));
6324 if (buf == NULL)
6325 {
6326 vim_free(split_buf);
6327 return;
6328 }
6329 }
6330
6331#ifdef FEAT_EVAL
6332 current_SID = cmd->uc_scriptID;
6333#endif
6334 (void)do_cmdline(buf, eap->getline, eap->cookie,
6335 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
6336#ifdef FEAT_EVAL
6337 current_SID = save_current_SID;
6338#endif
6339 vim_free(buf);
6340 vim_free(split_buf);
6341}
6342
6343# if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6344 static char_u *
6345get_user_command_name(idx)
6346 int idx;
6347{
6348 return get_user_commands(NULL, idx - (int)CMD_SIZE);
6349}
6350
6351/*
6352 * Function given to ExpandGeneric() to obtain the list of user command names.
6353 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006354 char_u *
6355get_user_commands(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006356 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006357 int idx;
6358{
6359 if (idx < curbuf->b_ucmds.ga_len)
6360 return USER_CMD_GA(&curbuf->b_ucmds, idx)->uc_name;
6361 idx -= curbuf->b_ucmds.ga_len;
6362 if (idx < ucmds.ga_len)
6363 return USER_CMD(idx)->uc_name;
6364 return NULL;
6365}
6366
6367/*
6368 * Function given to ExpandGeneric() to obtain the list of user command
6369 * attributes.
6370 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006371 char_u *
6372get_user_cmd_flags(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006373 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006374 int idx;
6375{
6376 static char *user_cmd_flags[] =
6377 {"bang", "bar", "buffer", "complete", "count",
6378 "nargs", "range", "register"};
6379
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00006380 if (idx >= (int)(sizeof(user_cmd_flags) / sizeof(user_cmd_flags[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006381 return NULL;
6382 return (char_u *)user_cmd_flags[idx];
6383}
6384
6385/*
6386 * Function given to ExpandGeneric() to obtain the list of values for -nargs.
6387 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006388 char_u *
6389get_user_cmd_nargs(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006390 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006391 int idx;
6392{
6393 static char *user_cmd_nargs[] = {"0", "1", "*", "?", "+"};
6394
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00006395 if (idx >= (int)(sizeof(user_cmd_nargs) / sizeof(user_cmd_nargs[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006396 return NULL;
6397 return (char_u *)user_cmd_nargs[idx];
6398}
6399
6400/*
6401 * Function given to ExpandGeneric() to obtain the list of values for -complete.
6402 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006403 char_u *
6404get_user_cmd_complete(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006405 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006406 int idx;
6407{
6408 return (char_u *)command_complete[idx].name;
6409}
6410# endif /* FEAT_CMDL_COMPL */
6411
6412#endif /* FEAT_USR_CMDS */
6413
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006414#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
6415/*
6416 * Parse a completion argument "value[vallen]".
6417 * The detected completion goes in "*complp", argument type in "*argt".
6418 * When there is an argument, for function and user defined completion, it's
6419 * copied to allocated memory and stored in "*compl_arg".
6420 * Returns FAIL if something is wrong.
6421 */
6422 int
6423parse_compl_arg(value, vallen, complp, argt, compl_arg)
6424 char_u *value;
6425 int vallen;
6426 int *complp;
6427 long *argt;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006428 char_u **compl_arg UNUSED;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006429{
6430 char_u *arg = NULL;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006431# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006432 size_t arglen = 0;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006433# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006434 int i;
6435 int valend = vallen;
6436
6437 /* Look for any argument part - which is the part after any ',' */
6438 for (i = 0; i < vallen; ++i)
6439 {
6440 if (value[i] == ',')
6441 {
6442 arg = &value[i + 1];
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006443# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006444 arglen = vallen - i - 1;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006445# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006446 valend = i;
6447 break;
6448 }
6449 }
6450
6451 for (i = 0; command_complete[i].expand != 0; ++i)
6452 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006453 if ((int)STRLEN(command_complete[i].name) == valend
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006454 && STRNCMP(value, command_complete[i].name, valend) == 0)
6455 {
6456 *complp = command_complete[i].expand;
6457 if (command_complete[i].expand == EXPAND_BUFFERS)
6458 *argt |= BUFNAME;
6459 else if (command_complete[i].expand == EXPAND_DIRECTORIES
6460 || command_complete[i].expand == EXPAND_FILES)
6461 *argt |= XFILE;
6462 break;
6463 }
6464 }
6465
6466 if (command_complete[i].expand == 0)
6467 {
6468 EMSG2(_("E180: Invalid complete value: %s"), value);
6469 return FAIL;
6470 }
6471
6472# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6473 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST
6474 && arg != NULL)
6475# else
6476 if (arg != NULL)
6477# endif
6478 {
6479 EMSG(_("E468: Completion argument only allowed for custom completion"));
6480 return FAIL;
6481 }
6482
6483# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6484 if ((*complp == EXPAND_USER_DEFINED || *complp == EXPAND_USER_LIST)
6485 && arg == NULL)
6486 {
6487 EMSG(_("E467: Custom completion requires a function argument"));
6488 return FAIL;
6489 }
6490
6491 if (arg != NULL)
6492 *compl_arg = vim_strnsave(arg, (int)arglen);
6493# endif
6494 return OK;
6495}
6496#endif
6497
Bram Moolenaar071d4272004-06-13 20:20:40 +00006498 static void
6499ex_colorscheme(eap)
6500 exarg_T *eap;
6501{
Bram Moolenaare6850792010-05-14 15:28:44 +02006502 if (*eap->arg == NUL)
6503 {
6504#ifdef FEAT_EVAL
6505 char_u *expr = vim_strsave((char_u *)"g:colors_name");
6506 char_u *p = NULL;
6507
6508 if (expr != NULL)
6509 {
6510 ++emsg_off;
6511 p = eval_to_string(expr, NULL, FALSE);
6512 --emsg_off;
6513 vim_free(expr);
6514 }
6515 if (p != NULL)
6516 {
6517 MSG(p);
6518 vim_free(p);
6519 }
6520 else
6521 MSG("default");
6522#else
6523 MSG(_("unknown"));
6524#endif
6525 }
6526 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarbe1e9e92012-09-18 16:47:07 +02006527 EMSG2(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006528}
6529
6530 static void
6531ex_highlight(eap)
6532 exarg_T *eap;
6533{
6534 if (*eap->arg == NUL && eap->cmd[2] == '!')
6535 MSG(_("Greetings, Vim user!"));
6536 do_highlight(eap->arg, eap->forceit, FALSE);
6537}
6538
6539
6540/*
6541 * Call this function if we thought we were going to exit, but we won't
6542 * (because of an error). May need to restore the terminal mode.
6543 */
6544 void
6545not_exiting()
6546{
6547 exiting = FALSE;
6548 settmode(TMODE_RAW);
6549}
6550
6551/*
6552 * ":quit": quit current window, quit Vim if closed the last window.
6553 */
6554 static void
6555ex_quit(eap)
6556 exarg_T *eap;
6557{
6558#ifdef FEAT_CMDWIN
6559 if (cmdwin_type != 0)
6560 {
6561 cmdwin_result = Ctrl_C;
6562 return;
6563 }
6564#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006565 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006566 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006567 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006568 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006569 return;
6570 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006571#ifdef FEAT_AUTOCMD
Bram Moolenaar3b53dfb2012-06-06 18:03:07 +02006572 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01006573 /* Refuse to quit when locked or when the buffer in the last window is
Bram Moolenaar362ce482012-06-06 19:02:45 +02006574 * being closed (can only happen in autocommands). */
6575 if (curbuf_locked() || (curbuf->b_nwindows == 1 && curbuf->b_closing))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006576 return;
6577#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006578
6579#ifdef FEAT_NETBEANS_INTG
6580 netbeansForcedQuit = eap->forceit;
6581#endif
6582
6583 /*
6584 * If there are more files or windows we won't exit.
6585 */
6586 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6587 exiting = TRUE;
6588 if ((!P_HID(curbuf)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01006589 && check_changed(curbuf, (p_awa ? CCGD_AW : 0)
6590 | (eap->forceit ? CCGD_FORCEIT : 0)
6591 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006592 || check_more(TRUE, eap->forceit) == FAIL
6593 || (only_one_window() && check_changed_any(eap->forceit)))
6594 {
6595 not_exiting();
6596 }
6597 else
6598 {
6599#ifdef FEAT_WINDOWS
6600 if (only_one_window()) /* quit last window */
6601#endif
6602 getout(0);
6603#ifdef FEAT_WINDOWS
6604# ifdef FEAT_GUI
6605 need_mouse_correct = TRUE;
6606# endif
6607 /* close window; may free buffer */
6608 win_close(curwin, !P_HID(curwin->w_buffer) || eap->forceit);
6609#endif
6610 }
6611}
6612
6613/*
6614 * ":cquit".
6615 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006616 static void
6617ex_cquit(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006618 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006619{
6620 getout(1); /* this does not always pass on the exit code to the Manx
6621 compiler. why? */
6622}
6623
6624/*
6625 * ":qall": try to quit all windows
6626 */
6627 static void
6628ex_quit_all(eap)
6629 exarg_T *eap;
6630{
6631# ifdef FEAT_CMDWIN
6632 if (cmdwin_type != 0)
6633 {
6634 if (eap->forceit)
6635 cmdwin_result = K_XF1; /* ex_window() takes care of this */
6636 else
6637 cmdwin_result = K_XF2;
6638 return;
6639 }
6640# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006641
6642 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006643 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006644 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006645 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006646 return;
6647 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006648#ifdef FEAT_AUTOCMD
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01006649 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
6650 /* Refuse to quit when locked or when the buffer in the last window is
6651 * being closed (can only happen in autocommands). */
6652 if (curbuf_locked() || (curbuf->b_nwindows == 1 && curbuf->b_closing))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006653 return;
6654#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006655
Bram Moolenaar071d4272004-06-13 20:20:40 +00006656 exiting = TRUE;
6657 if (eap->forceit || !check_changed_any(FALSE))
6658 getout(0);
6659 not_exiting();
6660}
6661
Bram Moolenaard9967712006-03-11 21:18:15 +00006662#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006663/*
6664 * ":close": close current window, unless it is the last one
6665 */
6666 static void
6667ex_close(eap)
6668 exarg_T *eap;
6669{
6670# ifdef FEAT_CMDWIN
6671 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02006672 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006673 else
6674# endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006675 if (!text_locked()
6676#ifdef FEAT_AUTOCMD
6677 && !curbuf_locked()
6678#endif
6679 )
Bram Moolenaarf740b292006-02-16 22:11:02 +00006680 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006681}
6682
Bram Moolenaard9967712006-03-11 21:18:15 +00006683# ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006684/*
6685 * ":pclose": Close any preview window.
6686 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006687 static void
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006688ex_pclose(eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006689 exarg_T *eap;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006690{
6691 win_T *win;
6692
6693 for (win = firstwin; win != NULL; win = win->w_next)
6694 if (win->w_p_pvw)
6695 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006696 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006697 break;
6698 }
6699}
Bram Moolenaard9967712006-03-11 21:18:15 +00006700# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006701
Bram Moolenaarf740b292006-02-16 22:11:02 +00006702/*
6703 * Close window "win" and take care of handling closing the last window for a
6704 * modified buffer.
6705 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006706 static void
Bram Moolenaarf740b292006-02-16 22:11:02 +00006707ex_win_close(forceit, win, tp)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006708 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006709 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006710 tabpage_T *tp; /* NULL or the tab page "win" is in */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006711{
6712 int need_hide;
6713 buf_T *buf = win->w_buffer;
6714
6715 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006716 if (need_hide && !P_HID(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006717 {
Bram Moolenaard9967712006-03-11 21:18:15 +00006718# if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006719 if ((p_confirm || cmdmod.confirm) && p_write)
6720 {
6721 dialog_changed(buf, FALSE);
6722 if (buf_valid(buf) && bufIsChanged(buf))
6723 return;
6724 need_hide = FALSE;
6725 }
6726 else
Bram Moolenaard9967712006-03-11 21:18:15 +00006727# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006728 {
6729 EMSG(_(e_nowrtmsg));
6730 return;
6731 }
6732 }
6733
Bram Moolenaard9967712006-03-11 21:18:15 +00006734# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006735 need_mouse_correct = TRUE;
Bram Moolenaard9967712006-03-11 21:18:15 +00006736# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006737
Bram Moolenaar071d4272004-06-13 20:20:40 +00006738 /* free buffer when not hiding it or when it's a scratch buffer */
Bram Moolenaarf740b292006-02-16 22:11:02 +00006739 if (tp == NULL)
6740 win_close(win, !need_hide && !P_HID(buf));
6741 else
6742 win_close_othertab(win, !need_hide && !P_HID(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006743}
6744
Bram Moolenaar071d4272004-06-13 20:20:40 +00006745/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006746 * ":tabclose": close current tab page, unless it is the last one.
6747 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006748 */
6749 static void
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006750ex_tabclose(eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006751 exarg_T *eap;
6752{
Bram Moolenaarf740b292006-02-16 22:11:02 +00006753 tabpage_T *tp;
6754
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006755# ifdef FEAT_CMDWIN
6756 if (cmdwin_type != 0)
6757 cmdwin_result = K_IGNORE;
6758 else
6759# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006760 if (first_tabpage->tp_next == NULL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00006761 EMSG(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00006762 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006763 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006764 if (eap->addr_count > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006765 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006766 tp = find_tabpage((int)eap->line2);
6767 if (tp == NULL)
6768 {
6769 beep_flush();
6770 return;
6771 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00006772 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006773 {
6774 tabpage_close_other(tp, eap->forceit);
6775 return;
6776 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006777 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006778 if (!text_locked()
6779#ifdef FEAT_AUTOCMD
6780 && !curbuf_locked()
6781#endif
6782 )
Bram Moolenaarf740b292006-02-16 22:11:02 +00006783 tabpage_close(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006784 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006785}
6786
6787/*
6788 * ":tabonly": close all tab pages except the current one
6789 */
6790 static void
6791ex_tabonly(eap)
6792 exarg_T *eap;
6793{
6794 tabpage_T *tp;
6795 int done;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006796
6797# ifdef FEAT_CMDWIN
6798 if (cmdwin_type != 0)
6799 cmdwin_result = K_IGNORE;
6800 else
6801# endif
6802 if (first_tabpage->tp_next == NULL)
6803 MSG(_("Already only one tab page"));
6804 else
6805 {
6806 /* Repeat this up to a 1000 times, because autocommands may mess
6807 * up the lists. */
6808 for (done = 0; done < 1000; ++done)
6809 {
6810 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6811 if (tp->tp_topframe != topframe)
6812 {
6813 tabpage_close_other(tp, eap->forceit);
6814 /* if we failed to close it quit */
6815 if (valid_tabpage(tp))
6816 done = 1000;
6817 /* start over, "tp" is now invalid */
6818 break;
6819 }
6820 if (first_tabpage->tp_next == NULL)
6821 break;
6822 }
6823 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006824}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006825
6826/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006827 * Close the current tab page.
6828 */
6829 void
6830tabpage_close(forceit)
6831 int forceit;
6832{
6833 /* First close all the windows but the current one. If that worked then
6834 * close the last window in this tab, that will close it. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006835 if (lastwin != firstwin)
6836 close_others(TRUE, forceit);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006837 if (lastwin == firstwin)
6838 ex_win_close(forceit, curwin, NULL);
6839# ifdef FEAT_GUI
6840 need_mouse_correct = TRUE;
6841# endif
6842}
6843
6844/*
6845 * Close tab page "tp", which is not the current tab page.
6846 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006847 * Also takes care of the tab pages line disappearing when closing the
6848 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00006849 */
6850 void
6851tabpage_close_other(tp, forceit)
6852 tabpage_T *tp;
6853 int forceit;
6854{
6855 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006856 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006857 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006858
6859 /* Limit to 1000 windows, autocommands may add a window while we close
6860 * one. OK, so I'm paranoid... */
6861 while (++done < 1000)
6862 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006863 wp = tp->tp_firstwin;
6864 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006865
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006866 /* Autocommands may delete the tab page under our fingers and we may
6867 * fail to close a window with a modified buffer. */
6868 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006869 break;
6870 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006871
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006872 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006873 if (h != tabline_height())
6874 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006875}
6876
6877/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006878 * ":only".
6879 */
6880 static void
6881ex_only(eap)
6882 exarg_T *eap;
6883{
6884# ifdef FEAT_GUI
6885 need_mouse_correct = TRUE;
6886# endif
6887 close_others(TRUE, eap->forceit);
6888}
6889
6890/*
6891 * ":all" and ":sall".
Bram Moolenaard9967712006-03-11 21:18:15 +00006892 * Also used for ":tab drop file ..." after setting the argument list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006893 */
Bram Moolenaard9967712006-03-11 21:18:15 +00006894 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00006895ex_all(eap)
6896 exarg_T *eap;
6897{
6898 if (eap->addr_count == 0)
6899 eap->line2 = 9999;
Bram Moolenaard9967712006-03-11 21:18:15 +00006900 do_arg_all((int)eap->line2, eap->forceit, eap->cmdidx == CMD_drop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006901}
6902#endif /* FEAT_WINDOWS */
6903
6904 static void
6905ex_hide(eap)
6906 exarg_T *eap;
6907{
6908 if (*eap->arg != NUL && check_nextcmd(eap->arg) == NULL)
6909 eap->errmsg = e_invarg;
6910 else
6911 {
6912 /* ":hide" or ":hide | cmd": hide current window */
6913 eap->nextcmd = check_nextcmd(eap->arg);
6914#ifdef FEAT_WINDOWS
6915 if (!eap->skip)
6916 {
6917# ifdef FEAT_GUI
6918 need_mouse_correct = TRUE;
6919# endif
6920 win_close(curwin, FALSE); /* don't free buffer */
6921 }
6922#endif
6923 }
6924}
6925
6926/*
6927 * ":stop" and ":suspend": Suspend Vim.
6928 */
6929 static void
6930ex_stop(eap)
6931 exarg_T *eap;
6932{
6933 /*
6934 * Disallow suspending for "rvim".
6935 */
6936 if (!check_restricted()
6937#ifdef WIN3264
6938 /*
6939 * Check if external commands are allowed now.
6940 */
6941 && can_end_termcap_mode(TRUE)
6942#endif
6943 )
6944 {
6945 if (!eap->forceit)
6946 autowrite_all();
6947 windgoto((int)Rows - 1, 0);
6948 out_char('\n');
6949 out_flush();
6950 stoptermcap();
6951 out_flush(); /* needed for SUN to restore xterm buffer */
6952#ifdef FEAT_TITLE
6953 mch_restore_title(3); /* restore window titles */
6954#endif
6955 ui_suspend(); /* call machine specific function */
6956#ifdef FEAT_TITLE
6957 maketitle();
6958 resettitle(); /* force updating the title */
6959#endif
6960 starttermcap();
6961 scroll_start(); /* scroll screen before redrawing */
6962 redraw_later_clear();
6963 shell_resized(); /* may have resized window */
6964 }
6965}
6966
6967/*
6968 * ":exit", ":xit" and ":wq": Write file and exit Vim.
6969 */
6970 static void
6971ex_exit(eap)
6972 exarg_T *eap;
6973{
6974#ifdef FEAT_CMDWIN
6975 if (cmdwin_type != 0)
6976 {
6977 cmdwin_result = Ctrl_C;
6978 return;
6979 }
6980#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006981 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006982 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006983 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006984 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006985 return;
6986 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006987#ifdef FEAT_AUTOCMD
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01006988 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
6989 /* Refuse to quit when locked or when the buffer in the last window is
6990 * being closed (can only happen in autocommands). */
6991 if (curbuf_locked() || (curbuf->b_nwindows == 1 && curbuf->b_closing))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006992 return;
6993#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006994
6995 /*
6996 * if more files or windows we won't exit
6997 */
6998 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6999 exiting = TRUE;
7000 if ( ((eap->cmdidx == CMD_wq
7001 || curbufIsChanged())
7002 && do_write(eap) == FAIL)
7003 || check_more(TRUE, eap->forceit) == FAIL
7004 || (only_one_window() && check_changed_any(eap->forceit)))
7005 {
7006 not_exiting();
7007 }
7008 else
7009 {
7010#ifdef FEAT_WINDOWS
7011 if (only_one_window()) /* quit last window, exit Vim */
7012#endif
7013 getout(0);
7014#ifdef FEAT_WINDOWS
7015# ifdef FEAT_GUI
7016 need_mouse_correct = TRUE;
7017# endif
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02007018 /* Quit current window, may free the buffer. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007019 win_close(curwin, !P_HID(curwin->w_buffer));
7020#endif
7021 }
7022}
7023
7024/*
7025 * ":print", ":list", ":number".
7026 */
7027 static void
7028ex_print(eap)
7029 exarg_T *eap;
7030{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007031 if (curbuf->b_ml.ml_flags & ML_EMPTY)
7032 EMSG(_(e_emptybuf));
7033 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007034 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007035 for ( ;!got_int; ui_breakcheck())
7036 {
7037 print_line(eap->line1,
7038 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
7039 || (eap->flags & EXFLAG_NR)),
7040 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
7041 if (++eap->line1 > eap->line2)
7042 break;
7043 out_flush(); /* show one line at a time */
7044 }
7045 setpcmark();
7046 /* put cursor at last line */
7047 curwin->w_cursor.lnum = eap->line2;
7048 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007049 }
7050
Bram Moolenaar071d4272004-06-13 20:20:40 +00007051 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007052}
7053
7054#ifdef FEAT_BYTEOFF
7055 static void
7056ex_goto(eap)
7057 exarg_T *eap;
7058{
7059 goto_byte(eap->line2);
7060}
7061#endif
7062
7063/*
7064 * ":shell".
7065 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007066 static void
7067ex_shell(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007068 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007069{
7070 do_shell(NULL, 0);
7071}
7072
7073#if (defined(FEAT_WINDOWS) && defined(HAVE_DROP_FILE)) \
7074 || (defined(FEAT_GUI_GTK) && defined(FEAT_DND)) \
Bram Moolenaar371baa92005-12-29 22:43:53 +00007075 || defined(FEAT_GUI_MSWIN) \
7076 || defined(FEAT_GUI_MAC) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00007077 || defined(PROTO)
7078
7079/*
7080 * Handle a file drop. The code is here because a drop is *nearly* like an
7081 * :args command, but not quite (we have a list of exact filenames, so we
7082 * don't want to (a) parse a command line, or (b) expand wildcards. So the
7083 * code is very similar to :args and hence needs access to a lot of the static
7084 * functions in this file.
7085 *
7086 * The list should be allocated using alloc(), as should each item in the
7087 * list. This function takes over responsibility for freeing the list.
7088 *
Bram Moolenaar81870892007-11-11 18:17:28 +00007089 * XXX The list is made into the argument list. This is freed using
Bram Moolenaar071d4272004-06-13 20:20:40 +00007090 * FreeWild(), which does a series of vim_free() calls, unless the two defines
7091 * __EMX__ and __ALWAYS_HAS_TRAILING_NUL_POINTER are set. In this case, a
7092 * routine _fnexplodefree() is used. This may cause problems, but as the drop
7093 * file functionality is (currently) not in EMX this is not presently a
7094 * problem.
7095 */
7096 void
7097handle_drop(filec, filev, split)
7098 int filec; /* the number of files dropped */
7099 char_u **filev; /* the list of files dropped */
7100 int split; /* force splitting the window */
7101{
7102 exarg_T ea;
7103 int save_msg_scroll = msg_scroll;
7104
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007105 /* Postpone this while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007106 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007107 return;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007108#ifdef FEAT_AUTOCMD
7109 if (curbuf_locked())
7110 return;
7111#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00007112 /* When the screen is being updated we should not change buffers and
7113 * windows structures, it may cause freed memory to be used. */
7114 if (updating_screen)
7115 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007116
7117 /* Check whether the current buffer is changed. If so, we will need
7118 * to split the current window or data could be lost.
7119 * We don't need to check if the 'hidden' option is set, as in this
7120 * case the buffer won't be lost.
7121 */
7122 if (!P_HID(curbuf) && !split)
7123 {
7124 ++emsg_off;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01007125 split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007126 --emsg_off;
7127 }
7128 if (split)
7129 {
7130# ifdef FEAT_WINDOWS
7131 if (win_split(0, 0) == FAIL)
7132 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007133 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007134
7135 /* When splitting the window, create a new alist. Otherwise the
7136 * existing one is overwritten. */
7137 alist_unlink(curwin->w_alist);
7138 alist_new();
7139# else
7140 return; /* can't split, always fail */
7141# endif
7142 }
7143
7144 /*
7145 * Set up the new argument list.
7146 */
Bram Moolenaar86b68352004-12-27 21:59:20 +00007147 alist_set(ALIST(curwin), filec, filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007148
7149 /*
7150 * Move to the first file.
7151 */
7152 /* Fake up a minimal "next" command for do_argfile() */
7153 vim_memset(&ea, 0, sizeof(ea));
7154 ea.cmd = (char_u *)"next";
7155 do_argfile(&ea, 0);
7156
7157 /* do_ecmd() may set need_start_insertmode, but since we never left Insert
7158 * mode that is not needed here. */
7159 need_start_insertmode = FALSE;
7160
7161 /* Restore msg_scroll, otherwise a following command may cause scrolling
7162 * unexpectedly. The screen will be redrawn by the caller, thus
7163 * msg_scroll being set by displaying a message is irrelevant. */
7164 msg_scroll = save_msg_scroll;
7165}
7166#endif
7167
Bram Moolenaar071d4272004-06-13 20:20:40 +00007168/*
7169 * Clear an argument list: free all file names and reset it to zero entries.
7170 */
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007171 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00007172alist_clear(al)
7173 alist_T *al;
7174{
7175 while (--al->al_ga.ga_len >= 0)
7176 vim_free(AARGLIST(al)[al->al_ga.ga_len].ae_fname);
7177 ga_clear(&al->al_ga);
7178}
7179
7180/*
7181 * Init an argument list.
7182 */
7183 void
7184alist_init(al)
7185 alist_T *al;
7186{
7187 ga_init2(&al->al_ga, (int)sizeof(aentry_T), 5);
7188}
7189
7190#if defined(FEAT_WINDOWS) || defined(PROTO)
7191
7192/*
7193 * Remove a reference from an argument list.
7194 * Ignored when the argument list is the global one.
7195 * If the argument list is no longer used by any window, free it.
7196 */
7197 void
7198alist_unlink(al)
7199 alist_T *al;
7200{
7201 if (al != &global_alist && --al->al_refcount <= 0)
7202 {
7203 alist_clear(al);
7204 vim_free(al);
7205 }
7206}
7207
7208# if defined(FEAT_LISTCMDS) || defined(HAVE_DROP_FILE) || defined(PROTO)
7209/*
7210 * Create a new argument list and use it for the current window.
7211 */
7212 void
7213alist_new()
7214{
7215 curwin->w_alist = (alist_T *)alloc((unsigned)sizeof(alist_T));
7216 if (curwin->w_alist == NULL)
7217 {
7218 curwin->w_alist = &global_alist;
7219 ++global_alist.al_refcount;
7220 }
7221 else
7222 {
7223 curwin->w_alist->al_refcount = 1;
7224 alist_init(curwin->w_alist);
7225 }
7226}
7227# endif
7228#endif
7229
7230#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE) || defined(PROTO)
7231/*
7232 * Expand the file names in the global argument list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00007233 * If "fnum_list" is not NULL, use "fnum_list[fnum_len]" as a list of buffer
7234 * numbers to be re-used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007235 */
7236 void
Bram Moolenaar86b68352004-12-27 21:59:20 +00007237alist_expand(fnum_list, fnum_len)
7238 int *fnum_list;
7239 int fnum_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007240{
7241 char_u **old_arg_files;
Bram Moolenaar86b68352004-12-27 21:59:20 +00007242 int old_arg_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007243 char_u **new_arg_files;
7244 int new_arg_file_count;
7245 char_u *save_p_su = p_su;
7246 int i;
7247
7248 /* Don't use 'suffixes' here. This should work like the shell did the
7249 * expansion. Also, the vimrc file isn't read yet, thus the user
7250 * can't set the options. */
7251 p_su = empty_option;
7252 old_arg_files = (char_u **)alloc((unsigned)(sizeof(char_u *) * GARGCOUNT));
7253 if (old_arg_files != NULL)
7254 {
7255 for (i = 0; i < GARGCOUNT; ++i)
Bram Moolenaar86b68352004-12-27 21:59:20 +00007256 old_arg_files[i] = vim_strsave(GARGLIST[i].ae_fname);
7257 old_arg_count = GARGCOUNT;
7258 if (expand_wildcards(old_arg_count, old_arg_files,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007259 &new_arg_file_count, &new_arg_files,
Bram Moolenaarb5609832011-07-20 15:04:58 +02007260 EW_FILE|EW_NOTFOUND|EW_ADDSLASH|EW_NOERROR) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007261 && new_arg_file_count > 0)
7262 {
Bram Moolenaar86b68352004-12-27 21:59:20 +00007263 alist_set(&global_alist, new_arg_file_count, new_arg_files,
7264 TRUE, fnum_list, fnum_len);
7265 FreeWild(old_arg_count, old_arg_files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007266 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007267 }
7268 p_su = save_p_su;
7269}
7270#endif
7271
7272/*
7273 * Set the argument list for the current window.
7274 * Takes over the allocated files[] and the allocated fnames in it.
7275 */
7276 void
Bram Moolenaar86b68352004-12-27 21:59:20 +00007277alist_set(al, count, files, use_curbuf, fnum_list, fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007278 alist_T *al;
7279 int count;
7280 char_u **files;
7281 int use_curbuf;
Bram Moolenaar86b68352004-12-27 21:59:20 +00007282 int *fnum_list;
7283 int fnum_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007284{
7285 int i;
7286
7287 alist_clear(al);
7288 if (ga_grow(&al->al_ga, count) == OK)
7289 {
7290 for (i = 0; i < count; ++i)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007291 {
7292 if (got_int)
7293 {
7294 /* When adding many buffers this can take a long time. Allow
7295 * interrupting here. */
7296 while (i < count)
7297 vim_free(files[i++]);
7298 break;
7299 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00007300
7301 /* May set buffer name of a buffer previously used for the
7302 * argument list, so that it's re-used by alist_add. */
7303 if (fnum_list != NULL && i < fnum_len)
7304 buf_set_name(fnum_list[i], files[i]);
7305
Bram Moolenaar071d4272004-06-13 20:20:40 +00007306 alist_add(al, files[i], use_curbuf ? 2 : 1);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007307 ui_breakcheck();
7308 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007309 vim_free(files);
7310 }
7311 else
7312 FreeWild(count, files);
7313#ifdef FEAT_WINDOWS
7314 if (al == &global_alist)
7315#endif
7316 arg_had_last = FALSE;
7317}
7318
7319/*
7320 * Add file "fname" to argument list "al".
7321 * "fname" must have been allocated and "al" must have been checked for room.
7322 */
7323 void
7324alist_add(al, fname, set_fnum)
7325 alist_T *al;
7326 char_u *fname;
7327 int set_fnum; /* 1: set buffer number; 2: re-use curbuf */
7328{
7329 if (fname == NULL) /* don't add NULL file names */
7330 return;
7331#ifdef BACKSLASH_IN_FILENAME
7332 slash_adjust(fname);
7333#endif
7334 AARGLIST(al)[al->al_ga.ga_len].ae_fname = fname;
7335 if (set_fnum > 0)
7336 AARGLIST(al)[al->al_ga.ga_len].ae_fnum =
7337 buflist_add(fname, BLN_LISTED | (set_fnum == 2 ? BLN_CURBUF : 0));
7338 ++al->al_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007339}
7340
7341#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
7342/*
7343 * Adjust slashes in file names. Called after 'shellslash' was set.
7344 */
7345 void
7346alist_slash_adjust()
7347{
7348 int i;
7349# ifdef FEAT_WINDOWS
7350 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007351 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007352# endif
7353
7354 for (i = 0; i < GARGCOUNT; ++i)
7355 if (GARGLIST[i].ae_fname != NULL)
7356 slash_adjust(GARGLIST[i].ae_fname);
7357# ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00007358 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007359 if (wp->w_alist != &global_alist)
7360 for (i = 0; i < WARGCOUNT(wp); ++i)
7361 if (WARGLIST(wp)[i].ae_fname != NULL)
7362 slash_adjust(WARGLIST(wp)[i].ae_fname);
7363# endif
7364}
7365#endif
7366
7367/*
7368 * ":preserve".
7369 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007370 static void
7371ex_preserve(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007372 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007373{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007374 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007375 ml_preserve(curbuf, TRUE);
7376}
7377
7378/*
7379 * ":recover".
7380 */
7381 static void
7382ex_recover(eap)
7383 exarg_T *eap;
7384{
7385 /* Set recoverymode right away to avoid the ATTENTION prompt. */
7386 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01007387 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
7388 | CCGD_MULTWIN
7389 | (eap->forceit ? CCGD_FORCEIT : 0)
7390 | CCGD_EXCMD)
7391
Bram Moolenaar071d4272004-06-13 20:20:40 +00007392 && (*eap->arg == NUL
7393 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
7394 ml_recover();
7395 recoverymode = FALSE;
7396}
7397
7398/*
7399 * Command modifier used in a wrong way.
7400 */
7401 static void
7402ex_wrongmodifier(eap)
7403 exarg_T *eap;
7404{
7405 eap->errmsg = e_invcmd;
7406}
7407
7408#ifdef FEAT_WINDOWS
7409/*
7410 * :sview [+command] file split window with new file, read-only
7411 * :split [[+command] file] split window with current or new file
7412 * :vsplit [[+command] file] split window vertically with current or new file
7413 * :new [[+command] file] split window with no or new file
7414 * :vnew [[+command] file] split vertically window with no or new file
7415 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007416 *
7417 * :tabedit open new Tab page with empty window
7418 * :tabedit [+command] file open new Tab page and edit "file"
7419 * :tabnew [[+command] file] just like :tabedit
7420 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007421 */
7422 void
7423ex_splitview(eap)
7424 exarg_T *eap;
7425{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007426 win_T *old_curwin = curwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007427# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007428 char_u *fname = NULL;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007429# endif
7430# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007431 int browse_flag = cmdmod.browse;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007432# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007433
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007434# ifndef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007435 if (eap->cmdidx == CMD_vsplit || eap->cmdidx == CMD_vnew)
7436 {
7437 ex_ni(eap);
7438 return;
7439 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007440# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007441
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007442# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007443 need_mouse_correct = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007444# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007445
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007446# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00007447 /* A ":split" in the quickfix window works like ":new". Don't want two
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007448 * quickfix windows. But it's OK when doing ":tab split". */
7449 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007450 {
7451 if (eap->cmdidx == CMD_split)
7452 eap->cmdidx = CMD_new;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007453# ifdef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007454 if (eap->cmdidx == CMD_vsplit)
7455 eap->cmdidx = CMD_vnew;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007456# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007457 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007458# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007459
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007460# ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007461 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007462 {
7463 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
7464 FNAME_MESS, TRUE, curbuf->b_ffname);
7465 if (fname == NULL)
7466 goto theend;
7467 eap->arg = fname;
7468 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007469# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007470 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007471# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007472# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007473# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007474 if (cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007475# ifdef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007476 && eap->cmdidx != CMD_vnew
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007477# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007478 && eap->cmdidx != CMD_new)
7479 {
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007480# ifdef FEAT_AUTOCMD
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007481 if (
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007482# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007483 !gui.in_use &&
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007484# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007485 au_has_group((char_u *)"FileExplorer"))
7486 {
7487 /* No browsing supported but we do have the file explorer:
7488 * Edit the directory. */
7489 if (*eap->arg == NUL || !mch_isdir(eap->arg))
7490 eap->arg = (char_u *)".";
7491 }
7492 else
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007493# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007494 {
7495 fname = do_browse(0, (char_u *)_("Edit File in new window"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007496 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007497 if (fname == NULL)
7498 goto theend;
7499 eap->arg = fname;
7500 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007501 }
7502 cmdmod.browse = FALSE; /* Don't browse again in do_ecmd(). */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007503# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007504
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007505 /*
7506 * Either open new tab page or split the window.
7507 */
7508 if (eap->cmdidx == CMD_tabedit
7509 || eap->cmdidx == CMD_tabfind
7510 || eap->cmdidx == CMD_tabnew)
7511 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00007512 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
7513 : eap->addr_count == 0 ? 0
7514 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007515 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00007516 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007517
7518 /* set the alternate buffer for the window we came from */
7519 if (curwin != old_curwin
7520 && win_valid(old_curwin)
7521 && old_curwin->w_buffer != curbuf
7522 && !cmdmod.keepalt)
7523 old_curwin->w_alt_fnum = curbuf->b_fnum;
7524 }
7525 }
7526 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007527 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
7528 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007529# ifdef FEAT_SCROLLBIND
Bram Moolenaar071d4272004-06-13 20:20:40 +00007530 /* Reset 'scrollbind' when editing another file, but keep it when
7531 * doing ":split" without arguments. */
7532 if (*eap->arg != NUL
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007533# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007534 || cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007535# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007536 )
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007537 {
7538 RESET_BINDING(curwin);
7539 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007540 else
7541 do_check_scrollbind(FALSE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007542# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007543 do_exedit(eap, old_curwin);
7544 }
7545
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007546# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007547 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007548# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007549
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007550# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007551theend:
7552 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007553# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007554}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007555
7556/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007557 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007558 */
7559 void
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007560tabpage_new()
7561{
7562 exarg_T ea;
7563
7564 vim_memset(&ea, 0, sizeof(ea));
7565 ea.cmdidx = CMD_tabnew;
7566 ea.cmd = (char_u *)"tabn";
7567 ea.arg = (char_u *)"";
7568 ex_splitview(&ea);
7569}
7570
7571/*
7572 * :tabnext command
7573 */
7574 static void
7575ex_tabnext(eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007576 exarg_T *eap;
7577{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007578 switch (eap->cmdidx)
7579 {
7580 case CMD_tabfirst:
7581 case CMD_tabrewind:
7582 goto_tabpage(1);
7583 break;
7584 case CMD_tablast:
7585 goto_tabpage(9999);
7586 break;
7587 case CMD_tabprevious:
7588 case CMD_tabNext:
7589 goto_tabpage(eap->addr_count == 0 ? -1 : -(int)eap->line2);
7590 break;
7591 default: /* CMD_tabnext */
7592 goto_tabpage(eap->addr_count == 0 ? 0 : (int)eap->line2);
7593 break;
7594 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007595}
7596
7597/*
7598 * :tabmove command
7599 */
7600 static void
7601ex_tabmove(eap)
7602 exarg_T *eap;
7603{
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02007604 int tab_number = 9999;
7605
7606 if (eap->arg && *eap->arg != NUL)
7607 {
7608 char_u *p = eap->arg;
7609 int relative = 0; /* argument +N/-N means: move N places to the
7610 * right/left relative to the current position. */
7611
7612 if (*eap->arg == '-')
7613 {
7614 relative = -1;
7615 p = eap->arg + 1;
7616 }
7617 else if (*eap->arg == '+')
7618 {
7619 relative = 1;
7620 p = eap->arg + 1;
7621 }
7622 else
7623 p = eap->arg;
7624
7625 if (p == skipdigits(p))
7626 {
7627 /* No numbers as argument. */
7628 eap->errmsg = e_invarg;
7629 return;
7630 }
7631
7632 tab_number = getdigits(&p);
7633 if (relative != 0)
7634 tab_number = tab_number * relative + tabpage_index(curtab) - 1;;
7635 }
7636 else if (eap->addr_count != 0)
7637 tab_number = eap->line2;
7638
7639 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007640}
7641
7642/*
7643 * :tabs command: List tabs and their contents.
7644 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007645 static void
7646ex_tabs(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007647 exarg_T *eap UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007648{
7649 tabpage_T *tp;
7650 win_T *wp;
7651 int tabcount = 1;
7652
7653 msg_start();
7654 msg_scroll = TRUE;
7655 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
7656 {
7657 msg_putchar('\n');
7658 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
7659 msg_outtrans_attr(IObuff, hl_attr(HLF_T));
7660 out_flush(); /* output one line at a time */
7661 ui_breakcheck();
7662
Bram Moolenaar030f0df2006-02-21 22:02:53 +00007663 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007664 wp = firstwin;
7665 else
7666 wp = tp->tp_firstwin;
7667 for ( ; wp != NULL && !got_int; wp = wp->w_next)
7668 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007669 msg_putchar('\n');
7670 msg_putchar(wp == curwin ? '>' : ' ');
7671 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007672 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
7673 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007674 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02007675 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007676 else
7677 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
7678 IObuff, IOSIZE, TRUE);
7679 msg_outtrans(IObuff);
7680 out_flush(); /* output one line at a time */
7681 ui_breakcheck();
7682 }
7683 }
7684}
7685
7686#endif /* FEAT_WINDOWS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007687
7688/*
7689 * ":mode": Set screen mode.
7690 * If no argument given, just get the screen size and redraw.
7691 */
7692 static void
7693ex_mode(eap)
7694 exarg_T *eap;
7695{
7696 if (*eap->arg == NUL)
7697 shell_resized();
7698 else
7699 mch_screenmode(eap->arg);
7700}
7701
7702#ifdef FEAT_WINDOWS
7703/*
7704 * ":resize".
7705 * set, increment or decrement current window height
7706 */
7707 static void
7708ex_resize(eap)
7709 exarg_T *eap;
7710{
7711 int n;
7712 win_T *wp = curwin;
7713
7714 if (eap->addr_count > 0)
7715 {
7716 n = eap->line2;
7717 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
7718 ;
7719 }
7720
7721#ifdef FEAT_GUI
7722 need_mouse_correct = TRUE;
7723#endif
7724 n = atol((char *)eap->arg);
7725#ifdef FEAT_VERTSPLIT
7726 if (cmdmod.split & WSP_VERT)
7727 {
7728 if (*eap->arg == '-' || *eap->arg == '+')
7729 n += W_WIDTH(curwin);
7730 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
7731 n = 9999;
7732 win_setwidth_win((int)n, wp);
7733 }
7734 else
7735#endif
7736 {
7737 if (*eap->arg == '-' || *eap->arg == '+')
7738 n += curwin->w_height;
7739 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
7740 n = 9999;
7741 win_setheight_win((int)n, wp);
7742 }
7743}
7744#endif
7745
7746/*
7747 * ":find [+command] <file>" command.
7748 */
7749 static void
7750ex_find(eap)
7751 exarg_T *eap;
7752{
7753#ifdef FEAT_SEARCHPATH
7754 char_u *fname;
7755 int count;
7756
7757 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
7758 TRUE, curbuf->b_ffname);
7759 if (eap->addr_count > 0)
7760 {
7761 /* Repeat finding the file "count" times. This matters when it
7762 * appears several times in the path. */
7763 count = eap->line2;
7764 while (fname != NULL && --count > 0)
7765 {
7766 vim_free(fname);
7767 fname = find_file_in_path(NULL, 0, FNAME_MESS,
7768 FALSE, curbuf->b_ffname);
7769 }
7770 }
7771
7772 if (fname != NULL)
7773 {
7774 eap->arg = fname;
7775#endif
7776 do_exedit(eap, NULL);
7777#ifdef FEAT_SEARCHPATH
7778 vim_free(fname);
7779 }
7780#endif
7781}
7782
7783/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00007784 * ":open" simulation: for now just work like ":visual".
7785 */
7786 static void
7787ex_open(eap)
7788 exarg_T *eap;
7789{
7790 regmatch_T regmatch;
7791 char_u *p;
7792
7793 curwin->w_cursor.lnum = eap->line2;
7794 beginline(BL_SOL | BL_FIX);
7795 if (*eap->arg == '/')
7796 {
7797 /* ":open /pattern/": put cursor in column found with pattern */
7798 ++eap->arg;
7799 p = skip_regexp(eap->arg, '/', p_magic, NULL);
7800 *p = NUL;
7801 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
7802 if (regmatch.regprog != NULL)
7803 {
7804 regmatch.rm_ic = p_ic;
7805 p = ml_get_curline();
7806 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007807 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007808 else
7809 EMSG(_(e_nomatch));
Bram Moolenaar473de612013-06-08 18:19:48 +02007810 vim_regfree(regmatch.regprog);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007811 }
7812 /* Move to the NUL, ignore any other arguments. */
7813 eap->arg += STRLEN(eap->arg);
7814 }
7815 check_cursor();
7816
7817 eap->cmdidx = CMD_visual;
7818 do_exedit(eap, NULL);
7819}
7820
7821/*
7822 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007823 */
7824 static void
7825ex_edit(eap)
7826 exarg_T *eap;
7827{
7828 do_exedit(eap, NULL);
7829}
7830
7831/*
7832 * ":edit <file>" command and alikes.
7833 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007834 void
7835do_exedit(eap, old_curwin)
7836 exarg_T *eap;
7837 win_T *old_curwin; /* curwin before doing a split or NULL */
7838{
7839 int n;
7840#ifdef FEAT_WINDOWS
7841 int need_hide;
7842#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00007843 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007844
7845 /*
7846 * ":vi" command ends Ex mode.
7847 */
7848 if (exmode_active && (eap->cmdidx == CMD_visual
7849 || eap->cmdidx == CMD_view))
7850 {
7851 exmode_active = FALSE;
7852 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007853 {
7854 /* Special case: ":global/pat/visual\NLvi-commands" */
7855 if (global_busy)
7856 {
7857 int rd = RedrawingDisabled;
7858 int nwr = no_wait_return;
7859 int ms = msg_scroll;
7860#ifdef FEAT_GUI
7861 int he = hold_gui_events;
7862#endif
7863
7864 if (eap->nextcmd != NULL)
7865 {
7866 stuffReadbuff(eap->nextcmd);
7867 eap->nextcmd = NULL;
7868 }
7869
7870 if (exmode_was != EXMODE_VIM)
7871 settmode(TMODE_RAW);
7872 RedrawingDisabled = 0;
7873 no_wait_return = 0;
7874 need_wait_return = FALSE;
7875 msg_scroll = 0;
7876#ifdef FEAT_GUI
7877 hold_gui_events = 0;
7878#endif
7879 must_redraw = CLEAR;
7880
7881 main_loop(FALSE, TRUE);
7882
7883 RedrawingDisabled = rd;
7884 no_wait_return = nwr;
7885 msg_scroll = ms;
7886#ifdef FEAT_GUI
7887 hold_gui_events = he;
7888#endif
7889 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007890 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007891 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007892 }
7893
7894 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007895 || eap->cmdidx == CMD_tabnew
7896 || eap->cmdidx == CMD_tabedit
Bram Moolenaar071d4272004-06-13 20:20:40 +00007897#ifdef FEAT_VERTSPLIT
7898 || eap->cmdidx == CMD_vnew
7899#endif
7900 ) && *eap->arg == NUL)
7901 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007902 /* ":new" or ":tabnew" without argument: edit an new empty buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007903 setpcmark();
7904 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007905 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
7906 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007907 }
7908 else if ((eap->cmdidx != CMD_split
7909#ifdef FEAT_VERTSPLIT
7910 && eap->cmdidx != CMD_vsplit
7911#endif
7912 )
7913 || *eap->arg != NUL
7914#ifdef FEAT_BROWSE
7915 || cmdmod.browse
7916#endif
7917 )
7918 {
Bram Moolenaar5555acc2006-04-07 21:33:12 +00007919#ifdef FEAT_AUTOCMD
7920 /* Can't edit another file when "curbuf_lock" is set. Only ":edit"
7921 * can bring us here, others are stopped earlier. */
7922 if (*eap->arg != NUL && curbuf_locked())
7923 return;
7924#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007925 n = readonlymode;
7926 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
7927 readonlymode = TRUE;
7928 else if (eap->cmdidx == CMD_enew)
7929 readonlymode = FALSE; /* 'readonly' doesn't make sense in an
7930 empty buffer */
7931 setpcmark();
7932 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
7933 NULL, eap,
7934 /* ":edit" goes to first line if Vi compatible */
7935 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
7936 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
7937 ? ECMD_ONE : eap->do_ecmd_lnum,
7938 (P_HID(curbuf) ? ECMD_HIDE : 0)
7939 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar7b449342014-03-25 13:03:48 +01007940 /* after a split we can use an existing buffer */
7941 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007942#ifdef FEAT_LISTCMDS
7943 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
7944#endif
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007945 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007946 {
7947 /* Editing the file failed. If the window was split, close it. */
7948#ifdef FEAT_WINDOWS
7949 if (old_curwin != NULL)
7950 {
7951 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
7952 if (!need_hide || P_HID(curbuf))
7953 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007954# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7955 cleanup_T cs;
7956
7957 /* Reset the error/interrupt/exception state here so that
7958 * aborting() returns FALSE when closing a window. */
7959 enter_cleanup(&cs);
7960# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007961# ifdef FEAT_GUI
7962 need_mouse_correct = TRUE;
7963# endif
7964 win_close(curwin, !need_hide && !P_HID(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007965
7966# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7967 /* Restore the error/interrupt/exception state if not
7968 * discarded by a new aborting error, interrupt, or
7969 * uncaught exception. */
7970 leave_cleanup(&cs);
7971# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007972 }
7973 }
7974#endif
7975 }
7976 else if (readonlymode && curbuf->b_nwindows == 1)
7977 {
7978 /* When editing an already visited buffer, 'readonly' won't be set
7979 * but the previous value is kept. With ":view" and ":sview" we
7980 * want the file to be readonly, except when another window is
7981 * editing the same buffer. */
7982 curbuf->b_p_ro = TRUE;
7983 }
7984 readonlymode = n;
7985 }
7986 else
7987 {
7988 if (eap->do_ecmd_cmd != NULL)
7989 do_cmdline_cmd(eap->do_ecmd_cmd);
7990#ifdef FEAT_TITLE
7991 n = curwin->w_arg_idx_invalid;
7992#endif
7993 check_arg_idx(curwin);
7994#ifdef FEAT_TITLE
7995 if (n != curwin->w_arg_idx_invalid)
7996 maketitle();
7997#endif
7998 }
7999
8000#ifdef FEAT_WINDOWS
8001 /*
8002 * if ":split file" worked, set alternate file name in old window to new
8003 * file
8004 */
8005 if (old_curwin != NULL
8006 && *eap->arg != NUL
8007 && curwin != old_curwin
8008 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00008009 && old_curwin->w_buffer != curbuf
8010 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008011 old_curwin->w_alt_fnum = curbuf->b_fnum;
8012#endif
8013
8014 ex_no_reprint = TRUE;
8015}
8016
8017#ifndef FEAT_GUI
8018/*
8019 * ":gui" and ":gvim" when there is no GUI.
8020 */
8021 static void
8022ex_nogui(eap)
8023 exarg_T *eap;
8024{
8025 eap->errmsg = e_nogvim;
8026}
8027#endif
8028
8029#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
8030 static void
8031ex_tearoff(eap)
8032 exarg_T *eap;
8033{
8034 gui_make_tearoff(eap->arg);
8035}
8036#endif
8037
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008038#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008039 static void
8040ex_popup(eap)
8041 exarg_T *eap;
8042{
Bram Moolenaar97409f12005-07-08 22:17:29 +00008043 gui_make_popup(eap->arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008044}
8045#endif
8046
Bram Moolenaar071d4272004-06-13 20:20:40 +00008047 static void
8048ex_swapname(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008049 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008050{
8051 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
8052 MSG(_("No swap file"));
8053 else
8054 msg(curbuf->b_ml.ml_mfp->mf_fname);
8055}
8056
8057/*
8058 * ":syncbind" forces all 'scrollbind' windows to have the same relative
8059 * offset.
8060 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
8061 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008062 static void
8063ex_syncbind(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008064 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008065{
8066#ifdef FEAT_SCROLLBIND
8067 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008068 win_T *save_curwin = curwin;
8069 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008070 long topline;
8071 long y;
8072 linenr_T old_linenr = curwin->w_cursor.lnum;
8073
8074 setpcmark();
8075
8076 /*
8077 * determine max topline
8078 */
8079 if (curwin->w_p_scb)
8080 {
8081 topline = curwin->w_topline;
8082 for (wp = firstwin; wp; wp = wp->w_next)
8083 {
8084 if (wp->w_p_scb && wp->w_buffer)
8085 {
8086 y = wp->w_buffer->b_ml.ml_line_count - p_so;
8087 if (topline > y)
8088 topline = y;
8089 }
8090 }
8091 if (topline < 1)
8092 topline = 1;
8093 }
8094 else
8095 {
8096 topline = 1;
8097 }
8098
8099
8100 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008101 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008102 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008103 for (curwin = firstwin; curwin; curwin = curwin->w_next)
8104 {
8105 if (curwin->w_p_scb)
8106 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008107 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008108 y = topline - curwin->w_topline;
8109 if (y > 0)
8110 scrollup(y, TRUE);
8111 else
8112 scrolldown(-y, TRUE);
8113 curwin->w_scbind_pos = topline;
8114 redraw_later(VALID);
8115 cursor_correct();
8116#ifdef FEAT_WINDOWS
8117 curwin->w_redr_status = TRUE;
8118#endif
8119 }
8120 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008121 curwin = save_curwin;
8122 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008123 if (curwin->w_p_scb)
8124 {
8125 did_syncbind = TRUE;
8126 checkpcmark();
8127 if (old_linenr != curwin->w_cursor.lnum)
8128 {
8129 char_u ctrl_o[2];
8130
8131 ctrl_o[0] = Ctrl_O;
8132 ctrl_o[1] = 0;
8133 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
8134 }
8135 }
8136#endif
8137}
8138
8139
8140 static void
8141ex_read(eap)
8142 exarg_T *eap;
8143{
Bram Moolenaardf177f62005-02-22 08:39:57 +00008144 int i;
8145 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
8146 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008147
8148 if (eap->usefilter) /* :r!cmd */
8149 do_bang(1, eap, FALSE, FALSE, TRUE);
8150 else
8151 {
8152 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
8153 return;
8154
8155#ifdef FEAT_BROWSE
8156 if (cmdmod.browse)
8157 {
8158 char_u *browseFile;
8159
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008160 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008161 NULL, NULL, NULL, curbuf);
8162 if (browseFile != NULL)
8163 {
8164 i = readfile(browseFile, NULL,
8165 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8166 vim_free(browseFile);
8167 }
8168 else
8169 i = OK;
8170 }
8171 else
8172#endif
8173 if (*eap->arg == NUL)
8174 {
8175 if (check_fname() == FAIL) /* check for no file name */
8176 return;
8177 i = readfile(curbuf->b_ffname, curbuf->b_fname,
8178 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8179 }
8180 else
8181 {
8182 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
8183 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
8184 i = readfile(eap->arg, NULL,
8185 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8186
8187 }
8188 if (i == FAIL)
8189 {
8190#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
8191 if (!aborting())
8192#endif
8193 EMSG2(_(e_notopen), eap->arg);
8194 }
8195 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008196 {
8197 if (empty && exmode_active)
8198 {
8199 /* Delete the empty line that remains. Historically ex does
8200 * this but vi doesn't. */
8201 if (eap->line2 == 0)
8202 lnum = curbuf->b_ml.ml_line_count;
8203 else
8204 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008205 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008206 {
8207 ml_delete(lnum, FALSE);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008208 if (curwin->w_cursor.lnum > 1
8209 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008210 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00008211 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008212 }
8213 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008214 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008215 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008216 }
8217}
8218
Bram Moolenaarea408852005-06-25 22:49:46 +00008219static char_u *prev_dir = NULL;
8220
8221#if defined(EXITFREE) || defined(PROTO)
8222 void
8223free_cd_dir()
8224{
8225 vim_free(prev_dir);
Bram Moolenaara0174af2008-01-02 20:08:25 +00008226 prev_dir = NULL;
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00008227
8228 vim_free(globaldir);
8229 globaldir = NULL;
Bram Moolenaarea408852005-06-25 22:49:46 +00008230}
8231#endif
8232
Bram Moolenaarf4258302013-06-02 18:20:17 +02008233/*
8234 * Deal with the side effects of changing the current directory.
8235 * When "local" is TRUE then this was after an ":lcd" command.
8236 */
8237 void
8238post_chdir(local)
8239 int local;
8240{
8241 vim_free(curwin->w_localdir);
Bram Moolenaarbd2dc342014-01-10 15:53:13 +01008242 curwin->w_localdir = NULL;
Bram Moolenaarf4258302013-06-02 18:20:17 +02008243 if (local)
8244 {
8245 /* If still in global directory, need to remember current
8246 * directory as global directory. */
8247 if (globaldir == NULL && prev_dir != NULL)
8248 globaldir = vim_strsave(prev_dir);
8249 /* Remember this local directory for the window. */
8250 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8251 curwin->w_localdir = vim_strsave(NameBuff);
8252 }
8253 else
8254 {
8255 /* We are now in the global directory, no need to remember its
8256 * name. */
8257 vim_free(globaldir);
8258 globaldir = NULL;
Bram Moolenaarf4258302013-06-02 18:20:17 +02008259 }
8260
8261 shorten_fnames(TRUE);
8262}
8263
Bram Moolenaarea408852005-06-25 22:49:46 +00008264
Bram Moolenaar071d4272004-06-13 20:20:40 +00008265/*
8266 * ":cd", ":lcd", ":chdir" and ":lchdir".
8267 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00008268 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00008269ex_cd(eap)
8270 exarg_T *eap;
8271{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008272 char_u *new_dir;
8273 char_u *tofree;
8274
8275 new_dir = eap->arg;
8276#if !defined(UNIX) && !defined(VMS)
8277 /* for non-UNIX ":cd" means: print current directory */
8278 if (*new_dir == NUL)
8279 ex_pwd(NULL);
8280 else
8281#endif
8282 {
Bram Moolenaar8e8fe9b2009-03-11 14:37:32 +00008283#ifdef FEAT_AUTOCMD
8284 if (allbuf_locked())
8285 return;
8286#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00008287 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged()
8288 && !eap->forceit)
8289 {
Bram Moolenaar81870892007-11-11 18:17:28 +00008290 EMSG(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00008291 return;
8292 }
8293
Bram Moolenaar071d4272004-06-13 20:20:40 +00008294 /* ":cd -": Change to previous directory */
8295 if (STRCMP(new_dir, "-") == 0)
8296 {
8297 if (prev_dir == NULL)
8298 {
8299 EMSG(_("E186: No previous directory"));
8300 return;
8301 }
8302 new_dir = prev_dir;
8303 }
8304
8305 /* Save current directory for next ":cd -" */
8306 tofree = prev_dir;
8307 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8308 prev_dir = vim_strsave(NameBuff);
8309 else
8310 prev_dir = NULL;
8311
8312#if defined(UNIX) || defined(VMS)
8313 /* for UNIX ":cd" means: go to home directory */
8314 if (*new_dir == NUL)
8315 {
8316 /* use NameBuff for home directory name */
8317# ifdef VMS
8318 char_u *p;
8319
8320 p = mch_getenv((char_u *)"SYS$LOGIN");
8321 if (p == NULL || *p == NUL) /* empty is the same as not set */
8322 NameBuff[0] = NUL;
8323 else
Bram Moolenaarb6356332005-07-18 21:40:44 +00008324 vim_strncpy(NameBuff, p, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008325# else
8326 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
8327# endif
8328 new_dir = NameBuff;
8329 }
8330#endif
8331 if (new_dir == NULL || vim_chdir(new_dir))
8332 EMSG(_(e_failed));
8333 else
8334 {
Bram Moolenaarf4258302013-06-02 18:20:17 +02008335 post_chdir(eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008336
8337 /* Echo the new current directory if the command was typed. */
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00008338 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008339 ex_pwd(eap);
8340 }
8341 vim_free(tofree);
8342 }
8343}
8344
8345/*
8346 * ":pwd".
8347 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008348 static void
8349ex_pwd(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008350 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008351{
8352 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8353 {
8354#ifdef BACKSLASH_IN_FILENAME
8355 slash_adjust(NameBuff);
8356#endif
8357 msg(NameBuff);
8358 }
8359 else
8360 EMSG(_("E187: Unknown"));
8361}
8362
8363/*
8364 * ":=".
8365 */
8366 static void
8367ex_equal(eap)
8368 exarg_T *eap;
8369{
8370 smsg((char_u *)"%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008371 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008372}
8373
8374 static void
8375ex_sleep(eap)
8376 exarg_T *eap;
8377{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008378 int n;
8379 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008380
8381 if (cursor_valid())
8382 {
8383 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
8384 if (n >= 0)
Bram Moolenaar10395d82014-02-05 22:46:52 +01008385 windgoto((int)n, W_WINCOL(curwin) + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008386 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008387
8388 len = eap->line2;
8389 switch (*eap->arg)
8390 {
8391 case 'm': break;
8392 case NUL: len *= 1000L; break;
8393 default: EMSG2(_(e_invarg2), eap->arg); return;
8394 }
8395 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008396}
8397
8398/*
8399 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
8400 */
8401 void
8402do_sleep(msec)
8403 long msec;
8404{
8405 long done;
8406
8407 cursor_on();
8408 out_flush();
8409 for (done = 0; !got_int && done < msec; done += 1000L)
8410 {
8411 ui_delay(msec - done > 1000L ? 1000L : msec - done, TRUE);
8412 ui_breakcheck();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02008413#ifdef FEAT_NETBEANS_INTG
8414 /* Process the netbeans messages that may have been received in the
8415 * call to ui_breakcheck() when the GUI is in use. This may occur when
8416 * running a test case. */
8417 netbeans_parse_messages();
8418#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008419 }
8420}
8421
8422 static void
8423do_exmap(eap, isabbrev)
8424 exarg_T *eap;
8425 int isabbrev;
8426{
8427 int mode;
8428 char_u *cmdp;
8429
8430 cmdp = eap->cmd;
8431 mode = get_map_mode(&cmdp, eap->forceit || isabbrev);
8432
8433 switch (do_map((*cmdp == 'n') ? 2 : (*cmdp == 'u'),
8434 eap->arg, mode, isabbrev))
8435 {
8436 case 1: EMSG(_(e_invarg));
8437 break;
8438 case 2: EMSG(isabbrev ? _(e_noabbr) : _(e_nomap));
8439 break;
8440 }
8441}
8442
8443/*
8444 * ":winsize" command (obsolete).
8445 */
8446 static void
8447ex_winsize(eap)
8448 exarg_T *eap;
8449{
8450 int w, h;
8451 char_u *arg = eap->arg;
8452 char_u *p;
8453
8454 w = getdigits(&arg);
8455 arg = skipwhite(arg);
8456 p = arg;
8457 h = getdigits(&arg);
8458 if (*p != NUL && *arg == NUL)
8459 set_shellsize(w, h, TRUE);
8460 else
8461 EMSG(_("E465: :winsize requires two number arguments"));
8462}
8463
8464#ifdef FEAT_WINDOWS
8465 static void
8466ex_wincmd(eap)
8467 exarg_T *eap;
8468{
8469 int xchar = NUL;
8470 char_u *p;
8471
8472 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
8473 {
8474 /* CTRL-W g and CTRL-W CTRL-G have an extra command character */
8475 if (eap->arg[1] == NUL)
8476 {
8477 EMSG(_(e_invarg));
8478 return;
8479 }
8480 xchar = eap->arg[1];
8481 p = eap->arg + 2;
8482 }
8483 else
8484 p = eap->arg + 1;
8485
8486 eap->nextcmd = check_nextcmd(p);
8487 p = skipwhite(p);
8488 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
8489 EMSG(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02008490 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008491 {
8492 /* Pass flags on for ":vertical wincmd ]". */
8493 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008494 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008495 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
8496 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008497 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008498 }
8499}
8500#endif
8501
Bram Moolenaar843ee412004-06-30 16:16:41 +00008502#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008503/*
8504 * ":winpos".
8505 */
8506 static void
8507ex_winpos(eap)
8508 exarg_T *eap;
8509{
8510 int x, y;
8511 char_u *arg = eap->arg;
8512 char_u *p;
8513
8514 if (*arg == NUL)
8515 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00008516# if defined(FEAT_GUI) || defined(MSWIN)
8517# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008518 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00008519# else
8520 if (mch_get_winpos(&x, &y) != FAIL)
8521# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008522 {
8523 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
8524 msg(IObuff);
8525 }
8526 else
8527# endif
8528 EMSG(_("E188: Obtaining window position not implemented for this platform"));
8529 }
8530 else
8531 {
8532 x = getdigits(&arg);
8533 arg = skipwhite(arg);
8534 p = arg;
8535 y = getdigits(&arg);
8536 if (*p == NUL || *arg != NUL)
8537 {
8538 EMSG(_("E466: :winpos requires two number arguments"));
8539 return;
8540 }
8541# ifdef FEAT_GUI
8542 if (gui.in_use)
8543 gui_mch_set_winpos(x, y);
8544 else if (gui.starting)
8545 {
8546 /* Remember the coordinates for when the window is opened. */
8547 gui_win_x = x;
8548 gui_win_y = y;
8549 }
8550# ifdef HAVE_TGETENT
8551 else
8552# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00008553# else
8554# ifdef MSWIN
8555 mch_set_winpos(x, y);
8556# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008557# endif
8558# ifdef HAVE_TGETENT
8559 if (*T_CWP)
8560 term_set_winpos(x, y);
8561# endif
8562 }
8563}
8564#endif
8565
8566/*
8567 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
8568 */
8569 static void
8570ex_operators(eap)
8571 exarg_T *eap;
8572{
8573 oparg_T oa;
8574
8575 clear_oparg(&oa);
8576 oa.regname = eap->regname;
8577 oa.start.lnum = eap->line1;
8578 oa.end.lnum = eap->line2;
8579 oa.line_count = eap->line2 - eap->line1 + 1;
8580 oa.motion_type = MLINE;
8581#ifdef FEAT_VIRTUALEDIT
8582 virtual_op = FALSE;
8583#endif
8584 if (eap->cmdidx != CMD_yank) /* position cursor for undo */
8585 {
8586 setpcmark();
8587 curwin->w_cursor.lnum = eap->line1;
8588 beginline(BL_SOL | BL_FIX);
8589 }
8590
Bram Moolenaard07c6e12013-11-21 14:21:40 +01008591 if (VIsual_active)
8592 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01008593
Bram Moolenaar071d4272004-06-13 20:20:40 +00008594 switch (eap->cmdidx)
8595 {
8596 case CMD_delete:
8597 oa.op_type = OP_DELETE;
8598 op_delete(&oa);
8599 break;
8600
8601 case CMD_yank:
8602 oa.op_type = OP_YANK;
8603 (void)op_yank(&oa, FALSE, TRUE);
8604 break;
8605
8606 default: /* CMD_rshift or CMD_lshift */
Bram Moolenaar6e707362013-06-14 19:15:58 +02008607 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00008608#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02008609 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
8610#else
8611 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00008612#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02008613 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00008614 oa.op_type = OP_RSHIFT;
8615 else
8616 oa.op_type = OP_LSHIFT;
8617 op_shift(&oa, FALSE, eap->amount);
8618 break;
8619 }
8620#ifdef FEAT_VIRTUALEDIT
8621 virtual_op = MAYBE;
8622#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00008623 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008624}
8625
8626/*
8627 * ":put".
8628 */
8629 static void
8630ex_put(eap)
8631 exarg_T *eap;
8632{
8633 /* ":0put" works like ":1put!". */
8634 if (eap->line2 == 0)
8635 {
8636 eap->line2 = 1;
8637 eap->forceit = TRUE;
8638 }
8639 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008640 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
8641 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008642}
8643
8644/*
8645 * Handle ":copy" and ":move".
8646 */
8647 static void
8648ex_copymove(eap)
8649 exarg_T *eap;
8650{
8651 long n;
8652
8653 n = get_address(&eap->arg, FALSE, FALSE);
8654 if (eap->arg == NULL) /* error detected */
8655 {
8656 eap->nextcmd = NULL;
8657 return;
8658 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008659 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008660
8661 /*
8662 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
8663 */
8664 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
8665 {
8666 EMSG(_(e_invaddr));
8667 return;
8668 }
8669
8670 if (eap->cmdidx == CMD_move)
8671 {
8672 if (do_move(eap->line1, eap->line2, n) == FAIL)
8673 return;
8674 }
8675 else
8676 ex_copy(eap->line1, eap->line2, n);
8677 u_clearline();
8678 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008679 ex_may_print(eap);
8680}
8681
8682/*
8683 * Print the current line if flags were given to the Ex command.
8684 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02008685 void
Bram Moolenaardf177f62005-02-22 08:39:57 +00008686ex_may_print(eap)
8687 exarg_T *eap;
8688{
8689 if (eap->flags != 0)
8690 {
8691 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
8692 (eap->flags & EXFLAG_LIST));
8693 ex_no_reprint = TRUE;
8694 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008695}
8696
8697/*
8698 * ":smagic" and ":snomagic".
8699 */
8700 static void
8701ex_submagic(eap)
8702 exarg_T *eap;
8703{
8704 int magic_save = p_magic;
8705
8706 p_magic = (eap->cmdidx == CMD_smagic);
8707 do_sub(eap);
8708 p_magic = magic_save;
8709}
8710
8711/*
8712 * ":join".
8713 */
8714 static void
8715ex_join(eap)
8716 exarg_T *eap;
8717{
8718 curwin->w_cursor.lnum = eap->line1;
8719 if (eap->line1 == eap->line2)
8720 {
8721 if (eap->addr_count >= 2) /* :2,2join does nothing */
8722 return;
8723 if (eap->line2 == curbuf->b_ml.ml_line_count)
8724 {
8725 beep_flush();
8726 return;
8727 }
8728 ++eap->line2;
8729 }
Bram Moolenaar81340392012-06-06 16:12:59 +02008730 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008731 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008732 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008733}
8734
8735/*
8736 * ":[addr]@r" or ":[addr]*r": execute register
8737 */
8738 static void
8739ex_at(eap)
8740 exarg_T *eap;
8741{
8742 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00008743 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008744
8745 curwin->w_cursor.lnum = eap->line2;
8746
8747#ifdef USE_ON_FLY_SCROLL
8748 dont_scroll = TRUE; /* disallow scrolling here */
8749#endif
8750
8751 /* get the register name. No name means to use the previous one */
8752 c = *eap->arg;
8753 if (c == NUL || (c == '*' && *eap->cmd == '*'))
8754 c = '@';
Bram Moolenaard333d1e2006-11-07 17:43:47 +00008755 /* Put the register in the typeahead buffer with the "silent" flag. */
8756 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
8757 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008758 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008759 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00008760 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008761 else
8762 {
8763 int save_efr = exec_from_reg;
8764
8765 exec_from_reg = TRUE;
8766
8767 /*
8768 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00008769 * Continue until the stuff buffer is empty and all added characters
8770 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008771 */
Bram Moolenaar60462872009-11-03 11:40:19 +00008772 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008773 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
8774
8775 exec_from_reg = save_efr;
8776 }
8777}
8778
8779/*
8780 * ":!".
8781 */
8782 static void
8783ex_bang(eap)
8784 exarg_T *eap;
8785{
8786 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
8787}
8788
8789/*
8790 * ":undo".
8791 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008792 static void
8793ex_undo(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008794 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008795{
Bram Moolenaard3667a22006-03-16 21:35:52 +00008796 if (eap->addr_count == 1) /* :undo 123 */
Bram Moolenaar730cde92010-06-27 05:18:54 +02008797 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00008798 else
8799 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008800}
8801
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008802#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008803 static void
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008804ex_wundo(eap)
8805 exarg_T *eap;
8806{
8807 char_u hash[UNDO_HASH_SIZE];
8808
8809 u_compute_hash(hash);
8810 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
8811}
8812
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008813 static void
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008814ex_rundo(eap)
8815 exarg_T *eap;
8816{
8817 char_u hash[UNDO_HASH_SIZE];
8818
8819 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02008820 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008821}
8822#endif
8823
Bram Moolenaar071d4272004-06-13 20:20:40 +00008824/*
8825 * ":redo".
8826 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008827 static void
8828ex_redo(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008829 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008830{
8831 u_redo(1);
8832}
8833
8834/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008835 * ":earlier" and ":later".
8836 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008837 static void
8838ex_later(eap)
8839 exarg_T *eap;
8840{
8841 long count = 0;
8842 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008843 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008844 char_u *p = eap->arg;
8845
8846 if (*p == NUL)
8847 count = 1;
8848 else if (isdigit(*p))
8849 {
8850 count = getdigits(&p);
8851 switch (*p)
8852 {
8853 case 's': ++p; sec = TRUE; break;
8854 case 'm': ++p; sec = TRUE; count *= 60; break;
8855 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008856 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
8857 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008858 }
8859 }
8860
8861 if (*p != NUL)
8862 EMSG2(_(e_invarg2), eap->arg);
8863 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02008864 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
8865 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008866}
8867
8868/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008869 * ":redir": start/stop redirection.
8870 */
8871 static void
8872ex_redir(eap)
8873 exarg_T *eap;
8874{
8875 char *mode;
8876 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00008877 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008878
8879 if (STRICMP(eap->arg, "END") == 0)
8880 close_redir();
8881 else
8882 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008883 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008884 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008885 ++arg;
8886 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008887 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008888 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008889 mode = "a";
8890 }
8891 else
8892 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00008893 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008894
8895 close_redir();
8896
8897 /* Expand environment variables and "~/". */
Bram Moolenaarca472992005-01-21 11:46:23 +00008898 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008899 if (fname == NULL)
8900 return;
8901#ifdef FEAT_BROWSE
8902 if (cmdmod.browse)
8903 {
8904 char_u *browseFile;
8905
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008906 browseFile = do_browse(BROWSE_SAVE,
8907 (char_u *)_("Save Redirection"),
8908 fname, NULL, NULL, BROWSE_FILTER_ALL_FILES, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008909 if (browseFile == NULL)
8910 return; /* operation cancelled */
8911 vim_free(fname);
8912 fname = browseFile;
8913 eap->forceit = TRUE; /* since dialog already asked */
8914 }
8915#endif
8916
8917 redir_fd = open_exfile(fname, eap->forceit, mode);
8918 vim_free(fname);
8919 }
8920#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00008921 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008922 {
8923 /* redirect to a register a-z (resp. A-Z for appending) */
8924 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00008925 ++arg;
8926 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008927# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00008928 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00008929 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008930# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00008931 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008932 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008933 redir_reg = *arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008934 if (*arg == '>' && arg[1] == '>') /* append */
Bram Moolenaard9d30582005-05-18 22:10:28 +00008935 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008936 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008937 {
Bram Moolenaarc188b882007-10-19 14:20:54 +00008938 /* Can use both "@a" and "@a>". */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00008939 if (*arg == '>')
8940 arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008941 /* Make register empty when not using @A-@Z and the
8942 * command is valid. */
8943 if (*arg == NUL && !isupper(redir_reg))
8944 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008945 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008946 }
8947 if (*arg != NUL)
8948 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00008949 redir_reg = 0;
Bram Moolenaard9d30582005-05-18 22:10:28 +00008950 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008951 }
8952 }
8953 else if (*arg == '=' && arg[1] == '>')
8954 {
8955 int append;
8956
8957 /* redirect to a variable */
8958 close_redir();
8959 arg += 2;
8960
8961 if (*arg == '>')
8962 {
8963 ++arg;
8964 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008965 }
8966 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008967 append = FALSE;
8968
8969 if (var_redir_start(skipwhite(arg), append) == OK)
8970 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008971 }
8972#endif
8973
8974 /* TODO: redirect to a buffer */
8975
Bram Moolenaar071d4272004-06-13 20:20:40 +00008976 else
Bram Moolenaarca472992005-01-21 11:46:23 +00008977 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008978 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00008979
8980 /* Make sure redirection is not off. Can happen for cmdline completion
8981 * that indirectly invokes a command to catch its output. */
8982 if (redir_fd != NULL
8983#ifdef FEAT_EVAL
8984 || redir_reg || redir_vname
8985#endif
8986 )
8987 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008988}
8989
8990/*
8991 * ":redraw": force redraw
8992 */
8993 static void
8994ex_redraw(eap)
8995 exarg_T *eap;
8996{
8997 int r = RedrawingDisabled;
8998 int p = p_lz;
8999
9000 RedrawingDisabled = 0;
9001 p_lz = FALSE;
9002 update_topline();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009003 update_screen(eap->forceit ? CLEAR : VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009004#ifdef FEAT_TITLE
9005 if (need_maketitle)
9006 maketitle();
9007#endif
9008 RedrawingDisabled = r;
9009 p_lz = p;
9010
9011 /* Reset msg_didout, so that a message that's there is overwritten. */
9012 msg_didout = FALSE;
9013 msg_col = 0;
9014
Bram Moolenaar56667a52013-07-17 11:54:28 +02009015 /* No need to wait after an intentional redraw. */
9016 need_wait_return = FALSE;
9017
Bram Moolenaar071d4272004-06-13 20:20:40 +00009018 out_flush();
9019}
9020
9021/*
9022 * ":redrawstatus": force redraw of status line(s)
9023 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009024 static void
9025ex_redrawstatus(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009026 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009027{
9028#if defined(FEAT_WINDOWS)
9029 int r = RedrawingDisabled;
9030 int p = p_lz;
9031
9032 RedrawingDisabled = 0;
9033 p_lz = FALSE;
9034 if (eap->forceit)
9035 status_redraw_all();
9036 else
9037 status_redraw_curbuf();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009038 update_screen(VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009039 RedrawingDisabled = r;
9040 p_lz = p;
9041 out_flush();
9042#endif
9043}
9044
9045 static void
9046close_redir()
9047{
9048 if (redir_fd != NULL)
9049 {
9050 fclose(redir_fd);
9051 redir_fd = NULL;
9052 }
9053#ifdef FEAT_EVAL
9054 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009055 if (redir_vname)
9056 {
9057 var_redir_stop();
9058 redir_vname = 0;
9059 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009060#endif
9061}
9062
9063#if defined(FEAT_SESSION) && defined(USE_CRNL)
9064# define MKSESSION_NL
9065static int mksession_nl = FALSE; /* use NL only in put_eol() */
9066#endif
9067
9068/*
9069 * ":mkexrc", ":mkvimrc", ":mkview" and ":mksession".
9070 */
9071 static void
9072ex_mkrc(eap)
9073 exarg_T *eap;
9074{
9075 FILE *fd;
9076 int failed = FALSE;
9077 char_u *fname;
9078#ifdef FEAT_BROWSE
9079 char_u *browseFile = NULL;
9080#endif
9081#ifdef FEAT_SESSION
9082 int view_session = FALSE;
9083 int using_vdir = FALSE; /* using 'viewdir'? */
9084 char_u *viewFile = NULL;
9085 unsigned *flagp;
9086#endif
9087
9088 if (eap->cmdidx == CMD_mksession || eap->cmdidx == CMD_mkview)
9089 {
9090#ifdef FEAT_SESSION
9091 view_session = TRUE;
9092#else
9093 ex_ni(eap);
9094 return;
9095#endif
9096 }
9097
9098#ifdef FEAT_SESSION
Bram Moolenaar8d594672009-07-01 18:18:57 +00009099 /* Use the short file name until ":lcd" is used. We also don't use the
9100 * short file name when 'acd' is set, that is checked later. */
Bram Moolenaareeefcc72007-05-01 21:21:21 +00009101 did_lcd = FALSE;
9102
Bram Moolenaar071d4272004-06-13 20:20:40 +00009103 /* ":mkview" or ":mkview 9": generate file name with 'viewdir' */
9104 if (eap->cmdidx == CMD_mkview
9105 && (*eap->arg == NUL
9106 || (vim_isdigit(*eap->arg) && eap->arg[1] == NUL)))
9107 {
9108 eap->forceit = TRUE;
9109 fname = get_view_file(*eap->arg);
9110 if (fname == NULL)
9111 return;
9112 viewFile = fname;
9113 using_vdir = TRUE;
9114 }
9115 else
9116#endif
9117 if (*eap->arg != NUL)
9118 fname = eap->arg;
9119 else if (eap->cmdidx == CMD_mkvimrc)
9120 fname = (char_u *)VIMRC_FILE;
9121#ifdef FEAT_SESSION
9122 else if (eap->cmdidx == CMD_mksession)
9123 fname = (char_u *)SESSION_FILE;
9124#endif
9125 else
9126 fname = (char_u *)EXRC_FILE;
9127
9128#ifdef FEAT_BROWSE
9129 if (cmdmod.browse)
9130 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00009131 browseFile = do_browse(BROWSE_SAVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009132# ifdef FEAT_SESSION
9133 eap->cmdidx == CMD_mkview ? (char_u *)_("Save View") :
9134 eap->cmdidx == CMD_mksession ? (char_u *)_("Save Session") :
9135# endif
9136 (char_u *)_("Save Setup"),
9137 fname, (char_u *)"vim", NULL, BROWSE_FILTER_MACROS, NULL);
9138 if (browseFile == NULL)
9139 goto theend;
9140 fname = browseFile;
9141 eap->forceit = TRUE; /* since dialog already asked */
9142 }
9143#endif
9144
9145#if defined(FEAT_SESSION) && defined(vim_mkdir)
9146 /* When using 'viewdir' may have to create the directory. */
9147 if (using_vdir && !mch_isdir(p_vdir))
Bram Moolenaardf177f62005-02-22 08:39:57 +00009148 vim_mkdir_emsg(p_vdir, 0755);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009149#endif
9150
9151 fd = open_exfile(fname, eap->forceit, WRITEBIN);
9152 if (fd != NULL)
9153 {
9154#ifdef FEAT_SESSION
9155 if (eap->cmdidx == CMD_mkview)
9156 flagp = &vop_flags;
9157 else
9158 flagp = &ssop_flags;
9159#endif
9160
9161#ifdef MKSESSION_NL
9162 /* "unix" in 'sessionoptions': use NL line separator */
9163 if (view_session && (*flagp & SSOP_UNIX))
9164 mksession_nl = TRUE;
9165#endif
9166
9167 /* Write the version command for :mkvimrc */
9168 if (eap->cmdidx == CMD_mkvimrc)
9169 (void)put_line(fd, "version 6.0");
9170
9171#ifdef FEAT_SESSION
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009172 if (eap->cmdidx == CMD_mksession)
9173 {
9174 if (put_line(fd, "let SessionLoad = 1") == FAIL)
9175 failed = TRUE;
9176 }
9177
Bram Moolenaar071d4272004-06-13 20:20:40 +00009178 if (eap->cmdidx != CMD_mkview)
9179#endif
9180 {
9181 /* Write setting 'compatible' first, because it has side effects.
9182 * For that same reason only do it when needed. */
9183 if (p_cp)
9184 (void)put_line(fd, "if !&cp | set cp | endif");
9185 else
9186 (void)put_line(fd, "if &cp | set nocp | endif");
9187 }
9188
9189#ifdef FEAT_SESSION
9190 if (!view_session
9191 || (eap->cmdidx == CMD_mksession
9192 && (*flagp & SSOP_OPTIONS)))
9193#endif
9194 failed |= (makemap(fd, NULL) == FAIL
9195 || makeset(fd, OPT_GLOBAL, FALSE) == FAIL);
9196
9197#ifdef FEAT_SESSION
9198 if (!failed && view_session)
9199 {
9200 if (put_line(fd, "let s:so_save = &so | let s:siso_save = &siso | set so=0 siso=0") == FAIL)
9201 failed = TRUE;
9202 if (eap->cmdidx == CMD_mksession)
9203 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02009204 char_u *dirnow; /* current directory */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009205
Bram Moolenaard9462e32011-04-11 21:35:11 +02009206 dirnow = alloc(MAXPATHL);
9207 if (dirnow == NULL)
9208 failed = TRUE;
9209 else
9210 {
9211 /*
9212 * Change to session file's dir.
9213 */
9214 if (mch_dirname(dirnow, MAXPATHL) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00009215 || mch_chdir((char *)dirnow) != 0)
Bram Moolenaard9462e32011-04-11 21:35:11 +02009216 *dirnow = NUL;
9217 if (*dirnow != NUL && (ssop_flags & SSOP_SESDIR))
9218 {
9219 if (vim_chdirfile(fname) == OK)
9220 shorten_fnames(TRUE);
9221 }
9222 else if (*dirnow != NUL
9223 && (ssop_flags & SSOP_CURDIR) && globaldir != NULL)
9224 {
9225 if (mch_chdir((char *)globaldir) == 0)
9226 shorten_fnames(TRUE);
9227 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009228
Bram Moolenaard9462e32011-04-11 21:35:11 +02009229 failed |= (makeopens(fd, dirnow) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009230
Bram Moolenaard9462e32011-04-11 21:35:11 +02009231 /* restore original dir */
9232 if (*dirnow != NUL && ((ssop_flags & SSOP_SESDIR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009233 || ((ssop_flags & SSOP_CURDIR) && globaldir != NULL)))
Bram Moolenaard9462e32011-04-11 21:35:11 +02009234 {
9235 if (mch_chdir((char *)dirnow) != 0)
9236 EMSG(_(e_prev_dir));
9237 shorten_fnames(TRUE);
9238 }
9239 vim_free(dirnow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009240 }
9241 }
9242 else
9243 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +00009244 failed |= (put_view(fd, curwin, !using_vdir, flagp,
9245 -1) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009246 }
9247 if (put_line(fd, "let &so = s:so_save | let &siso = s:siso_save")
9248 == FAIL)
9249 failed = TRUE;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009250 if (put_line(fd, "doautoall SessionLoadPost") == FAIL)
9251 failed = TRUE;
Bram Moolenaar4770d092006-01-12 23:22:24 +00009252 if (eap->cmdidx == CMD_mksession)
9253 {
9254 if (put_line(fd, "unlet SessionLoad") == FAIL)
9255 failed = TRUE;
9256 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009257 }
9258#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009259 if (put_line(fd, "\" vim: set ft=vim :") == FAIL)
9260 failed = TRUE;
9261
Bram Moolenaar071d4272004-06-13 20:20:40 +00009262 failed |= fclose(fd);
9263
9264 if (failed)
9265 EMSG(_(e_write));
9266#if defined(FEAT_EVAL) && defined(FEAT_SESSION)
9267 else if (eap->cmdidx == CMD_mksession)
9268 {
9269 /* successful session write - set this_session var */
Bram Moolenaard9462e32011-04-11 21:35:11 +02009270 char_u *tbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009271
Bram Moolenaard9462e32011-04-11 21:35:11 +02009272 tbuf = alloc(MAXPATHL);
9273 if (tbuf != NULL)
9274 {
9275 if (vim_FullName(fname, tbuf, MAXPATHL, FALSE) == OK)
9276 set_vim_var_string(VV_THIS_SESSION, tbuf, -1);
9277 vim_free(tbuf);
9278 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009279 }
9280#endif
9281#ifdef MKSESSION_NL
9282 mksession_nl = FALSE;
9283#endif
9284 }
9285
9286#ifdef FEAT_BROWSE
9287theend:
9288 vim_free(browseFile);
9289#endif
9290#ifdef FEAT_SESSION
9291 vim_free(viewFile);
9292#endif
9293}
9294
Bram Moolenaardf177f62005-02-22 08:39:57 +00009295#if ((defined(FEAT_SESSION) || defined(FEAT_EVAL)) && defined(vim_mkdir)) \
9296 || defined(PROTO)
9297 int
9298vim_mkdir_emsg(name, prot)
9299 char_u *name;
Bram Moolenaar78a15312009-05-15 19:33:18 +00009300 int prot UNUSED;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009301{
9302 if (vim_mkdir(name, prot) != 0)
9303 {
9304 EMSG2(_("E739: Cannot create directory: %s"), name);
9305 return FAIL;
9306 }
9307 return OK;
9308}
9309#endif
9310
Bram Moolenaar071d4272004-06-13 20:20:40 +00009311/*
9312 * Open a file for writing for an Ex command, with some checks.
9313 * Return file descriptor, or NULL on failure.
9314 */
9315 FILE *
9316open_exfile(fname, forceit, mode)
9317 char_u *fname;
9318 int forceit;
9319 char *mode; /* "w" for create new file or "a" for append */
9320{
9321 FILE *fd;
9322
9323#ifdef UNIX
9324 /* with Unix it is possible to open a directory */
9325 if (mch_isdir(fname))
9326 {
9327 EMSG2(_(e_isadir2), fname);
9328 return NULL;
9329 }
9330#endif
9331 if (!forceit && *mode != 'a' && vim_fexists(fname))
9332 {
9333 EMSG2(_("E189: \"%s\" exists (add ! to override)"), fname);
9334 return NULL;
9335 }
9336
9337 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
9338 EMSG2(_("E190: Cannot open \"%s\" for writing"), fname);
9339
9340 return fd;
9341}
9342
9343/*
9344 * ":mark" and ":k".
9345 */
9346 static void
9347ex_mark(eap)
9348 exarg_T *eap;
9349{
9350 pos_T pos;
9351
9352 if (*eap->arg == NUL) /* No argument? */
9353 EMSG(_(e_argreq));
9354 else if (eap->arg[1] != NUL) /* more than one character? */
9355 EMSG(_(e_trailing));
9356 else
9357 {
9358 pos = curwin->w_cursor; /* save curwin->w_cursor */
9359 curwin->w_cursor.lnum = eap->line2;
9360 beginline(BL_WHITE | BL_FIX);
9361 if (setmark(*eap->arg) == FAIL) /* set mark */
9362 EMSG(_("E191: Argument must be a letter or forward/backward quote"));
9363 curwin->w_cursor = pos; /* restore curwin->w_cursor */
9364 }
9365}
9366
9367/*
9368 * Update w_topline, w_leftcol and the cursor position.
9369 */
9370 void
9371update_topline_cursor()
9372{
9373 check_cursor(); /* put cursor on valid line */
9374 update_topline();
9375 if (!curwin->w_p_wrap)
9376 validate_cursor();
9377 update_curswant();
9378}
9379
9380#ifdef FEAT_EX_EXTRA
9381/*
9382 * ":normal[!] {commands}": Execute normal mode commands.
9383 */
9384 static void
9385ex_normal(eap)
9386 exarg_T *eap;
9387{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009388 int save_msg_scroll = msg_scroll;
9389 int save_restart_edit = restart_edit;
9390 int save_msg_didout = msg_didout;
9391 int save_State = State;
9392 tasave_T tabuf;
9393 int save_insertmode = p_im;
9394 int save_finish_op = finish_op;
Bram Moolenaar38084112008-07-26 14:05:07 +00009395 int save_opcount = opcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009396#ifdef FEAT_MBYTE
9397 char_u *arg = NULL;
9398 int l;
9399 char_u *p;
9400#endif
9401
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00009402 if (ex_normal_lock > 0)
9403 {
9404 EMSG(_(e_secure));
9405 return;
9406 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009407 if (ex_normal_busy >= p_mmd)
9408 {
9409 EMSG(_("E192: Recursive use of :normal too deep"));
9410 return;
9411 }
9412 ++ex_normal_busy;
9413
9414 msg_scroll = FALSE; /* no msg scrolling in Normal mode */
9415 restart_edit = 0; /* don't go to Insert mode */
9416 p_im = FALSE; /* don't use 'insertmode' */
9417
9418#ifdef FEAT_MBYTE
9419 /*
9420 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
9421 * this for the K_SPECIAL leading byte, otherwise special keys will not
9422 * work.
9423 */
9424 if (has_mbyte)
9425 {
9426 int len = 0;
9427
9428 /* Count the number of characters to be escaped. */
9429 for (p = eap->arg; *p != NUL; ++p)
9430 {
9431# ifdef FEAT_GUI
9432 if (*p == CSI) /* leadbyte CSI */
9433 len += 2;
9434# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009435 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009436 if (*++p == K_SPECIAL /* trailbyte K_SPECIAL or CSI */
9437# ifdef FEAT_GUI
9438 || *p == CSI
9439# endif
9440 )
9441 len += 2;
9442 }
9443 if (len > 0)
9444 {
9445 arg = alloc((unsigned)(STRLEN(eap->arg) + len + 1));
9446 if (arg != NULL)
9447 {
9448 len = 0;
9449 for (p = eap->arg; *p != NUL; ++p)
9450 {
9451 arg[len++] = *p;
9452# ifdef FEAT_GUI
9453 if (*p == CSI)
9454 {
9455 arg[len++] = KS_EXTRA;
9456 arg[len++] = (int)KE_CSI;
9457 }
9458# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009459 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009460 {
9461 arg[len++] = *++p;
9462 if (*p == K_SPECIAL)
9463 {
9464 arg[len++] = KS_SPECIAL;
9465 arg[len++] = KE_FILLER;
9466 }
9467# ifdef FEAT_GUI
9468 else if (*p == CSI)
9469 {
9470 arg[len++] = KS_EXTRA;
9471 arg[len++] = (int)KE_CSI;
9472 }
9473# endif
9474 }
9475 arg[len] = NUL;
9476 }
9477 }
9478 }
9479 }
9480#endif
9481
9482 /*
9483 * Save the current typeahead. This is required to allow using ":normal"
9484 * from an event handler and makes sure we don't hang when the argument
9485 * ends with half a command.
9486 */
9487 save_typeahead(&tabuf);
9488 if (tabuf.typebuf_valid)
9489 {
9490 /*
9491 * Repeat the :normal command for each line in the range. When no
9492 * range given, execute it just once, without positioning the cursor
9493 * first.
9494 */
9495 do
9496 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009497 if (eap->addr_count != 0)
9498 {
9499 curwin->w_cursor.lnum = eap->line1++;
9500 curwin->w_cursor.col = 0;
9501 }
9502
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009503 exec_normal_cmd(
Bram Moolenaar071d4272004-06-13 20:20:40 +00009504#ifdef FEAT_MBYTE
9505 arg != NULL ? arg :
9506#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009507 eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009508 }
9509 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
9510 }
9511
9512 /* Might not return to the main loop when in an event handler. */
9513 update_topline_cursor();
9514
9515 /* Restore the previous typeahead. */
9516 restore_typeahead(&tabuf);
9517
9518 --ex_normal_busy;
9519 msg_scroll = save_msg_scroll;
9520 restart_edit = save_restart_edit;
9521 p_im = save_insertmode;
9522 finish_op = save_finish_op;
Bram Moolenaar38084112008-07-26 14:05:07 +00009523 opcount = save_opcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009524 msg_didout |= save_msg_didout; /* don't reset msg_didout now */
9525
9526 /* Restore the state (needed when called from a function executed for
9527 * 'indentexpr'). */
9528 State = save_State;
9529#ifdef FEAT_MBYTE
9530 vim_free(arg);
9531#endif
9532}
9533
9534/*
Bram Moolenaar64969662005-12-14 21:59:55 +00009535 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009536 */
9537 static void
9538ex_startinsert(eap)
9539 exarg_T *eap;
9540{
Bram Moolenaarfd371682005-01-14 21:42:54 +00009541 if (eap->forceit)
9542 {
9543 coladvance((colnr_T)MAXCOL);
9544 curwin->w_curswant = MAXCOL;
9545 curwin->w_set_curswant = FALSE;
9546 }
9547
Bram Moolenaara40c5002005-01-09 21:16:21 +00009548 /* Ignore the command when already in Insert mode. Inserting an
9549 * expression register that invokes a function can do this. */
9550 if (State & INSERT)
9551 return;
9552
Bram Moolenaar64969662005-12-14 21:59:55 +00009553 if (eap->cmdidx == CMD_startinsert)
9554 restart_edit = 'a';
9555 else if (eap->cmdidx == CMD_startreplace)
9556 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00009557 else
Bram Moolenaar64969662005-12-14 21:59:55 +00009558 restart_edit = 'V';
9559
9560 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009561 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009562 if (eap->cmdidx == CMD_startinsert)
9563 restart_edit = 'i';
Bram Moolenaar071d4272004-06-13 20:20:40 +00009564 curwin->w_curswant = 0; /* avoid MAXCOL */
9565 }
9566}
9567
9568/*
9569 * ":stopinsert"
9570 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009571 static void
9572ex_stopinsert(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009573 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009574{
9575 restart_edit = 0;
9576 stop_insert_mode = TRUE;
9577}
9578#endif
9579
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009580#if defined(FEAT_EX_EXTRA) || defined(FEAT_MENU) || defined(PROTO)
9581/*
9582 * Execute normal mode command "cmd".
9583 * "remap" can be REMAP_NONE or REMAP_YES.
9584 */
9585 void
9586exec_normal_cmd(cmd, remap, silent)
9587 char_u *cmd;
9588 int remap;
9589 int silent;
9590{
9591 oparg_T oa;
9592
9593 /*
9594 * Stuff the argument into the typeahead buffer.
9595 * Execute normal_cmd() until there is no typeahead left.
9596 */
9597 clear_oparg(&oa);
9598 finish_op = FALSE;
9599 ins_typebuf(cmd, remap, 0, TRUE, silent);
9600 while ((!stuff_empty() || (!typebuf_typed() && typebuf.tb_len > 0))
9601 && !got_int)
9602 {
9603 update_topline_cursor();
Bram Moolenaar48ac02c2011-01-17 19:50:06 +01009604 normal_cmd(&oa, TRUE); /* execute a Normal mode cmd */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009605 }
9606}
9607#endif
9608
Bram Moolenaar071d4272004-06-13 20:20:40 +00009609#ifdef FEAT_FIND_ID
9610 static void
9611ex_checkpath(eap)
9612 exarg_T *eap;
9613{
9614 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
9615 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
9616 (linenr_T)1, (linenr_T)MAXLNUM);
9617}
9618
9619#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
9620/*
9621 * ":psearch"
9622 */
9623 static void
9624ex_psearch(eap)
9625 exarg_T *eap;
9626{
9627 g_do_tagpreview = p_pvh;
9628 ex_findpat(eap);
9629 g_do_tagpreview = 0;
9630}
9631#endif
9632
9633 static void
9634ex_findpat(eap)
9635 exarg_T *eap;
9636{
9637 int whole = TRUE;
9638 long n;
9639 char_u *p;
9640 int action;
9641
9642 switch (cmdnames[eap->cmdidx].cmd_name[2])
9643 {
9644 case 'e': /* ":psearch", ":isearch" and ":dsearch" */
9645 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
9646 action = ACTION_GOTO;
9647 else
9648 action = ACTION_SHOW;
9649 break;
9650 case 'i': /* ":ilist" and ":dlist" */
9651 action = ACTION_SHOW_ALL;
9652 break;
9653 case 'u': /* ":ijump" and ":djump" */
9654 action = ACTION_GOTO;
9655 break;
9656 default: /* ":isplit" and ":dsplit" */
9657 action = ACTION_SPLIT;
9658 break;
9659 }
9660
9661 n = 1;
9662 if (vim_isdigit(*eap->arg)) /* get count */
9663 {
9664 n = getdigits(&eap->arg);
9665 eap->arg = skipwhite(eap->arg);
9666 }
9667 if (*eap->arg == '/') /* Match regexp, not just whole words */
9668 {
9669 whole = FALSE;
9670 ++eap->arg;
9671 p = skip_regexp(eap->arg, '/', p_magic, NULL);
9672 if (*p)
9673 {
9674 *p++ = NUL;
9675 p = skipwhite(p);
9676
9677 /* Check for trailing illegal characters */
9678 if (!ends_excmd(*p))
9679 eap->errmsg = e_trailing;
9680 else
9681 eap->nextcmd = check_nextcmd(p);
9682 }
9683 }
9684 if (!eap->skip)
9685 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
9686 whole, !eap->forceit,
9687 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
9688 n, action, eap->line1, eap->line2);
9689}
9690#endif
9691
9692#ifdef FEAT_WINDOWS
9693
9694# ifdef FEAT_QUICKFIX
9695/*
9696 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
9697 */
9698 static void
9699ex_ptag(eap)
9700 exarg_T *eap;
9701{
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01009702 g_do_tagpreview = p_pvh; /* will be reset to 0 in ex_tag_cmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009703 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9704}
9705
9706/*
9707 * ":pedit"
9708 */
9709 static void
9710ex_pedit(eap)
9711 exarg_T *eap;
9712{
9713 win_T *curwin_save = curwin;
9714
9715 g_do_tagpreview = p_pvh;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00009716 prepare_tagpreview(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009717 keep_help_flag = curwin_save->w_buffer->b_help;
9718 do_exedit(eap, NULL);
9719 keep_help_flag = FALSE;
9720 if (curwin != curwin_save && win_valid(curwin_save))
9721 {
9722 /* Return cursor to where we were */
9723 validate_cursor();
9724 redraw_later(VALID);
9725 win_enter(curwin_save, TRUE);
9726 }
9727 g_do_tagpreview = 0;
9728}
9729# endif
9730
9731/*
9732 * ":stag", ":stselect" and ":stjump".
9733 */
9734 static void
9735ex_stag(eap)
9736 exarg_T *eap;
9737{
9738 postponed_split = -1;
9739 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009740 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009741 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9742 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009743 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009744}
9745#endif
9746
9747/*
9748 * ":tag", ":tselect", ":tjump", ":tnext", etc.
9749 */
9750 static void
9751ex_tag(eap)
9752 exarg_T *eap;
9753{
9754 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
9755}
9756
9757 static void
9758ex_tag_cmd(eap, name)
9759 exarg_T *eap;
9760 char_u *name;
9761{
9762 int cmd;
9763
9764 switch (name[1])
9765 {
9766 case 'j': cmd = DT_JUMP; /* ":tjump" */
9767 break;
9768 case 's': cmd = DT_SELECT; /* ":tselect" */
9769 break;
9770 case 'p': cmd = DT_PREV; /* ":tprevious" */
9771 break;
9772 case 'N': cmd = DT_PREV; /* ":tNext" */
9773 break;
9774 case 'n': cmd = DT_NEXT; /* ":tnext" */
9775 break;
9776 case 'o': cmd = DT_POP; /* ":pop" */
9777 break;
9778 case 'f': /* ":tfirst" */
9779 case 'r': cmd = DT_FIRST; /* ":trewind" */
9780 break;
9781 case 'l': cmd = DT_LAST; /* ":tlast" */
9782 break;
9783 default: /* ":tag" */
9784#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +00009785 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009786 {
9787 do_cstag(eap);
9788 return;
9789 }
9790#endif
9791 cmd = DT_TAG;
9792 break;
9793 }
9794
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00009795 if (name[0] == 'l')
9796 {
9797#ifndef FEAT_QUICKFIX
9798 ex_ni(eap);
9799 return;
9800#else
9801 cmd = DT_LTAG;
9802#endif
9803 }
9804
Bram Moolenaar071d4272004-06-13 20:20:40 +00009805 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
9806 eap->forceit, TRUE);
9807}
9808
9809/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009810 * Check "str" for starting with a special cmdline variable.
9811 * If found return one of the SPEC_ values and set "*usedlen" to the length of
9812 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
9813 */
9814 int
9815find_cmdline_var(src, usedlen)
9816 char_u *src;
9817 int *usedlen;
9818{
9819 int len;
9820 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +00009821 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009822 "%",
9823#define SPEC_PERC 0
9824 "#",
9825#define SPEC_HASH 1
9826 "<cword>", /* cursor word */
9827#define SPEC_CWORD 2
9828 "<cWORD>", /* cursor WORD */
9829#define SPEC_CCWORD 3
9830 "<cfile>", /* cursor path name */
9831#define SPEC_CFILE 4
9832 "<sfile>", /* ":so" file name */
9833#define SPEC_SFILE 5
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009834 "<slnum>", /* ":so" file line number */
9835#define SPEC_SLNUM 6
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009836#ifdef FEAT_AUTOCMD
9837 "<afile>", /* autocommand file name */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009838# define SPEC_AFILE 7
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009839 "<abuf>", /* autocommand buffer number */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009840# define SPEC_ABUF 8
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009841 "<amatch>", /* autocommand match name */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009842# define SPEC_AMATCH 9
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009843#endif
9844#ifdef FEAT_CLIENTSERVER
9845 "<client>"
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009846# ifdef FEAT_AUTOCMD
9847# define SPEC_CLIENT 10
9848# else
9849# define SPEC_CLIENT 7
9850# endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009851#endif
9852 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009853
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009854 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009855 {
9856 len = (int)STRLEN(spec_str[i]);
9857 if (STRNCMP(src, spec_str[i], len) == 0)
9858 {
9859 *usedlen = len;
9860 return i;
9861 }
9862 }
9863 return -1;
9864}
9865
9866/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009867 * Evaluate cmdline variables.
9868 *
9869 * change '%' to curbuf->b_ffname
9870 * '#' to curwin->w_altfile
9871 * '<cword>' to word under the cursor
9872 * '<cWORD>' to WORD under the cursor
9873 * '<cfile>' to path name under the cursor
9874 * '<sfile>' to sourced file name
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009875 * '<slnum>' to sourced file line number
Bram Moolenaar071d4272004-06-13 20:20:40 +00009876 * '<afile>' to file name for autocommand
9877 * '<abuf>' to buffer number for autocommand
9878 * '<amatch>' to matching name for autocommand
9879 *
9880 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
9881 * "" for error without a message) and NULL is returned.
9882 * Returns an allocated string if a valid match was found.
9883 * Returns NULL if no match was found. "usedlen" then still contains the
9884 * number of characters to skip.
9885 */
9886 char_u *
Bram Moolenaar63b92542007-03-27 14:57:09 +00009887eval_vars(src, srcstart, usedlen, lnump, errormsg, escaped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009888 char_u *src; /* pointer into commandline */
Bram Moolenaar63b92542007-03-27 14:57:09 +00009889 char_u *srcstart; /* beginning of valid memory for src */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009890 int *usedlen; /* characters after src that are used */
9891 linenr_T *lnump; /* line number for :e command, or NULL */
9892 char_u **errormsg; /* pointer to error message */
Bram Moolenaar63b92542007-03-27 14:57:09 +00009893 int *escaped; /* return value has escaped white space (can
9894 * be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009895{
9896 int i;
9897 char_u *s;
9898 char_u *result;
9899 char_u *resultbuf = NULL;
9900 int resultlen;
9901 buf_T *buf;
9902 int valid = VALID_HEAD + VALID_PATH; /* assume valid result */
9903 int spec_idx;
9904#ifdef FEAT_MODIFY_FNAME
9905 int skip_mod = FALSE;
9906#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009907 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009908
9909 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009910 if (escaped != NULL)
9911 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009912
9913 /*
9914 * Check if there is something to do.
9915 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009916 spec_idx = find_cmdline_var(src, usedlen);
9917 if (spec_idx < 0) /* no match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009918 {
9919 *usedlen = 1;
9920 return NULL;
9921 }
9922
9923 /*
9924 * Skip when preceded with a backslash "\%" and "\#".
9925 * Note: In "\\%" the % is also not recognized!
9926 */
9927 if (src > srcstart && src[-1] == '\\')
9928 {
9929 *usedlen = 0;
Bram Moolenaara7241f52008-06-24 20:39:31 +00009930 STRMOVE(src - 1, src); /* remove backslash */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009931 return NULL;
9932 }
9933
9934 /*
9935 * word or WORD under cursor
9936 */
9937 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD)
9938 {
9939 resultlen = find_ident_under_cursor(&result, spec_idx == SPEC_CWORD ?
9940 (FIND_IDENT|FIND_STRING) : FIND_STRING);
9941 if (resultlen == 0)
9942 {
9943 *errormsg = (char_u *)"";
9944 return NULL;
9945 }
9946 }
9947
9948 /*
9949 * '#': Alternate file name
9950 * '%': Current file name
9951 * File name under the cursor
9952 * File name for autocommand
9953 * and following modifiers
9954 */
9955 else
9956 {
9957 switch (spec_idx)
9958 {
9959 case SPEC_PERC: /* '%': current file */
9960 if (curbuf->b_fname == NULL)
9961 {
9962 result = (char_u *)"";
9963 valid = 0; /* Must have ":p:h" to be valid */
9964 }
9965 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009966 result = curbuf->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009967 break;
9968
9969 case SPEC_HASH: /* '#' or "#99": alternate file */
9970 if (src[1] == '#') /* "##": the argument list */
9971 {
9972 result = arg_all();
9973 resultbuf = result;
9974 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009975 if (escaped != NULL)
9976 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009977#ifdef FEAT_MODIFY_FNAME
9978 skip_mod = TRUE;
9979#endif
9980 break;
9981 }
9982 s = src + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00009983 if (*s == '<') /* "#<99" uses v:oldfiles */
9984 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009985 i = (int)getdigits(&s);
9986 *usedlen = (int)(s - src); /* length of what we expand */
9987
Bram Moolenaard812df62008-11-09 12:46:09 +00009988 if (src[1] == '<')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009989 {
Bram Moolenaard812df62008-11-09 12:46:09 +00009990 if (*usedlen < 2)
9991 {
9992 /* Should we give an error message for #<text? */
9993 *usedlen = 1;
9994 return NULL;
9995 }
9996#ifdef FEAT_EVAL
9997 result = list_find_str(get_vim_var_list(VV_OLDFILES),
9998 (long)i);
9999 if (result == NULL)
10000 {
10001 *errormsg = (char_u *)"";
10002 return NULL;
10003 }
10004#else
10005 *errormsg = (char_u *)_("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +000010006 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +000010007#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010008 }
10009 else
Bram Moolenaard812df62008-11-09 12:46:09 +000010010 {
10011 buf = buflist_findnr(i);
10012 if (buf == NULL)
10013 {
10014 *errormsg = (char_u *)_("E194: No alternate file name to substitute for '#'");
10015 return NULL;
10016 }
10017 if (lnump != NULL)
10018 *lnump = ECMD_LAST;
10019 if (buf->b_fname == NULL)
10020 {
10021 result = (char_u *)"";
10022 valid = 0; /* Must have ":p:h" to be valid */
10023 }
10024 else
10025 result = buf->b_fname;
10026 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010027 break;
10028
10029#ifdef FEAT_SEARCHPATH
10030 case SPEC_CFILE: /* file name under cursor */
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010031 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010032 if (result == NULL)
10033 {
10034 *errormsg = (char_u *)"";
10035 return NULL;
10036 }
10037 resultbuf = result; /* remember allocated string */
10038 break;
10039#endif
10040
10041#ifdef FEAT_AUTOCMD
10042 case SPEC_AFILE: /* file name for autocommand */
10043 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +000010044 if (result != NULL && !autocmd_fname_full)
10045 {
10046 /* Still need to turn the fname into a full path. It is
10047 * postponed to avoid a delay when <afile> is not used. */
10048 autocmd_fname_full = TRUE;
10049 result = FullName_save(autocmd_fname, FALSE);
10050 vim_free(autocmd_fname);
10051 autocmd_fname = result;
10052 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010053 if (result == NULL)
10054 {
10055 *errormsg = (char_u *)_("E495: no autocommand file name to substitute for \"<afile>\"");
10056 return NULL;
10057 }
Bram Moolenaara0174af2008-01-02 20:08:25 +000010058 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010059 break;
10060
10061 case SPEC_ABUF: /* buffer number for autocommand */
10062 if (autocmd_bufnr <= 0)
10063 {
10064 *errormsg = (char_u *)_("E496: no autocommand buffer number to substitute for \"<abuf>\"");
10065 return NULL;
10066 }
10067 sprintf((char *)strbuf, "%d", autocmd_bufnr);
10068 result = strbuf;
10069 break;
10070
10071 case SPEC_AMATCH: /* match name for autocommand */
10072 result = autocmd_match;
10073 if (result == NULL)
10074 {
10075 *errormsg = (char_u *)_("E497: no autocommand match name to substitute for \"<amatch>\"");
10076 return NULL;
10077 }
10078 break;
10079
10080#endif
10081 case SPEC_SFILE: /* file name for ":so" command */
10082 result = sourcing_name;
10083 if (result == NULL)
10084 {
10085 *errormsg = (char_u *)_("E498: no :source file name to substitute for \"<sfile>\"");
10086 return NULL;
10087 }
10088 break;
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010089 case SPEC_SLNUM: /* line in file for ":so" command */
10090 if (sourcing_name == NULL || sourcing_lnum == 0)
10091 {
10092 *errormsg = (char_u *)_("E842: no line number to use for \"<slnum>\"");
10093 return NULL;
10094 }
10095 sprintf((char *)strbuf, "%ld", (long)sourcing_lnum);
10096 result = strbuf;
10097 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010098#if defined(FEAT_CLIENTSERVER)
10099 case SPEC_CLIENT: /* Source of last submitted input */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010100 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
10101 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010102 result = strbuf;
10103 break;
10104#endif
10105 }
10106
10107 resultlen = (int)STRLEN(result); /* length of new string */
10108 if (src[*usedlen] == '<') /* remove the file name extension */
10109 {
10110 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010111 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010112 resultlen = (int)(s - result);
10113 }
10114#ifdef FEAT_MODIFY_FNAME
10115 else if (!skip_mod)
10116 {
10117 valid |= modify_fname(src, usedlen, &result, &resultbuf,
10118 &resultlen);
10119 if (result == NULL)
10120 {
10121 *errormsg = (char_u *)"";
10122 return NULL;
10123 }
10124 }
10125#endif
10126 }
10127
10128 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
10129 {
10130 if (valid != VALID_HEAD + VALID_PATH)
10131 /* xgettext:no-c-format */
10132 *errormsg = (char_u *)_("E499: Empty file name for '%' or '#', only works with \":p:h\"");
10133 else
10134 *errormsg = (char_u *)_("E500: Evaluates to an empty string");
10135 result = NULL;
10136 }
10137 else
10138 result = vim_strnsave(result, resultlen);
10139 vim_free(resultbuf);
10140 return result;
10141}
10142
10143/*
10144 * Concatenate all files in the argument list, separated by spaces, and return
10145 * it in one allocated string.
10146 * Spaces and backslashes in the file names are escaped with a backslash.
10147 * Returns NULL when out of memory.
10148 */
10149 static char_u *
10150arg_all()
10151{
10152 int len;
10153 int idx;
10154 char_u *retval = NULL;
10155 char_u *p;
10156
10157 /*
10158 * Do this loop two times:
10159 * first time: compute the total length
10160 * second time: concatenate the names
10161 */
10162 for (;;)
10163 {
10164 len = 0;
10165 for (idx = 0; idx < ARGCOUNT; ++idx)
10166 {
10167 p = alist_name(&ARGLIST[idx]);
10168 if (p != NULL)
10169 {
10170 if (len > 0)
10171 {
10172 /* insert a space in between names */
10173 if (retval != NULL)
10174 retval[len] = ' ';
10175 ++len;
10176 }
10177 for ( ; *p != NUL; ++p)
10178 {
10179 if (*p == ' ' || *p == '\\')
10180 {
10181 /* insert a backslash */
10182 if (retval != NULL)
10183 retval[len] = '\\';
10184 ++len;
10185 }
10186 if (retval != NULL)
10187 retval[len] = *p;
10188 ++len;
10189 }
10190 }
10191 }
10192
10193 /* second time: break here */
10194 if (retval != NULL)
10195 {
10196 retval[len] = NUL;
10197 break;
10198 }
10199
10200 /* allocate memory */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000010201 retval = alloc((unsigned)len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010202 if (retval == NULL)
10203 break;
10204 }
10205
10206 return retval;
10207}
10208
10209#if defined(FEAT_AUTOCMD) || defined(PROTO)
10210/*
10211 * Expand the <sfile> string in "arg".
10212 *
10213 * Returns an allocated string, or NULL for any error.
10214 */
10215 char_u *
10216expand_sfile(arg)
10217 char_u *arg;
10218{
10219 char_u *errormsg;
10220 int len;
10221 char_u *result;
10222 char_u *newres;
10223 char_u *repl;
10224 int srclen;
10225 char_u *p;
10226
10227 result = vim_strsave(arg);
10228 if (result == NULL)
10229 return NULL;
10230
10231 for (p = result; *p; )
10232 {
10233 if (STRNCMP(p, "<sfile>", 7) != 0)
10234 ++p;
10235 else
10236 {
10237 /* replace "<sfile>" with the sourced file name, and do ":" stuff */
Bram Moolenaar63b92542007-03-27 14:57:09 +000010238 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010239 if (errormsg != NULL)
10240 {
10241 if (*errormsg)
10242 emsg(errormsg);
10243 vim_free(result);
10244 return NULL;
10245 }
10246 if (repl == NULL) /* no match (cannot happen) */
10247 {
10248 p += srclen;
10249 continue;
10250 }
10251 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
10252 newres = alloc(len);
10253 if (newres == NULL)
10254 {
10255 vim_free(repl);
10256 vim_free(result);
10257 return NULL;
10258 }
10259 mch_memmove(newres, result, (size_t)(p - result));
10260 STRCPY(newres + (p - result), repl);
10261 len = (int)STRLEN(newres);
10262 STRCAT(newres, p + srclen);
10263 vim_free(repl);
10264 vim_free(result);
10265 result = newres;
10266 p = newres + len; /* continue after the match */
10267 }
10268 }
10269
10270 return result;
10271}
10272#endif
10273
10274#ifdef FEAT_SESSION
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010275static int ses_winsizes __ARGS((FILE *fd, int restore_size,
10276 win_T *tab_firstwin));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010277static int ses_win_rec __ARGS((FILE *fd, frame_T *fr));
10278static frame_T *ses_skipframe __ARGS((frame_T *fr));
10279static int ses_do_frame __ARGS((frame_T *fr));
10280static int ses_do_win __ARGS((win_T *wp));
10281static int ses_arglist __ARGS((FILE *fd, char *cmd, garray_T *gap, int fullname, unsigned *flagp));
10282static int ses_put_fname __ARGS((FILE *fd, char_u *name, unsigned *flagp));
10283static int ses_fname __ARGS((FILE *fd, buf_T *buf, unsigned *flagp));
10284
10285/*
10286 * Write openfile commands for the current buffers to an .exrc file.
10287 * Return FAIL on error, OK otherwise.
10288 */
10289 static int
10290makeopens(fd, dirnow)
10291 FILE *fd;
10292 char_u *dirnow; /* Current directory name */
10293{
10294 buf_T *buf;
10295 int only_save_windows = TRUE;
10296 int nr;
10297 int cnr = 1;
10298 int restore_size = TRUE;
10299 win_T *wp;
10300 char_u *sname;
10301 win_T *edited_win = NULL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010302 int tabnr;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010303 win_T *tab_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010304 frame_T *tab_topframe;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010305 int cur_arg_idx = 0;
Bram Moolenaar383c6f52008-01-04 15:01:07 +000010306 int next_arg_idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010307
10308 if (ssop_flags & SSOP_BUFFERS)
10309 only_save_windows = FALSE; /* Save ALL buffers */
10310
10311 /*
10312 * Begin by setting the this_session variable, and then other
10313 * sessionable variables.
10314 */
10315#ifdef FEAT_EVAL
10316 if (put_line(fd, "let v:this_session=expand(\"<sfile>:p\")") == FAIL)
10317 return FAIL;
10318 if (ssop_flags & SSOP_GLOBALS)
10319 if (store_session_globals(fd) == FAIL)
10320 return FAIL;
10321#endif
10322
10323 /*
10324 * Close all windows but one.
10325 */
10326 if (put_line(fd, "silent only") == FAIL)
10327 return FAIL;
10328
10329 /*
10330 * Now a :cd command to the session directory or the current directory
10331 */
10332 if (ssop_flags & SSOP_SESDIR)
10333 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010334 if (put_line(fd, "exe \"cd \" . escape(expand(\"<sfile>:p:h\"), ' ')")
10335 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010336 return FAIL;
10337 }
10338 else if (ssop_flags & SSOP_CURDIR)
10339 {
10340 sname = home_replace_save(NULL, globaldir != NULL ? globaldir : dirnow);
10341 if (sname == NULL
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010342 || fputs("cd ", fd) < 0
10343 || ses_put_fname(fd, sname, &ssop_flags) == FAIL
10344 || put_eol(fd) == FAIL)
10345 {
10346 vim_free(sname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010347 return FAIL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010348 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010349 vim_free(sname);
10350 }
10351
10352 /*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010353 * If there is an empty, unnamed buffer we will wipe it out later.
10354 * Remember the buffer number.
10355 */
10356 if (put_line(fd, "if expand('%') == '' && !&modified && line('$') <= 1 && getline(1) == ''") == FAIL)
10357 return FAIL;
10358 if (put_line(fd, " let s:wipebuf = bufnr('%')") == FAIL)
10359 return FAIL;
10360 if (put_line(fd, "endif") == FAIL)
10361 return FAIL;
10362
10363 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010364 * Now save the current files, current buffer first.
10365 */
10366 if (put_line(fd, "set shortmess=aoO") == FAIL)
10367 return FAIL;
10368
10369 /* Now put the other buffers into the buffer list */
10370 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
10371 {
10372 if (!(only_save_windows && buf->b_nwindows == 0)
10373 && !(buf->b_help && !(ssop_flags & SSOP_HELP))
10374 && buf->b_fname != NULL
10375 && buf->b_p_bl)
10376 {
10377 if (fprintf(fd, "badd +%ld ", buf->b_wininfo == NULL ? 1L
10378 : buf->b_wininfo->wi_fpos.lnum) < 0
10379 || ses_fname(fd, buf, &ssop_flags) == FAIL)
10380 return FAIL;
10381 }
10382 }
10383
10384 /* the global argument list */
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010010385 if (ses_arglist(fd, "argglobal", &global_alist.al_ga,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010386 !(ssop_flags & SSOP_CURDIR), &ssop_flags) == FAIL)
10387 return FAIL;
10388
10389 if (ssop_flags & SSOP_RESIZE)
10390 {
10391 /* Note: after the restore we still check it worked!*/
10392 if (fprintf(fd, "set lines=%ld columns=%ld" , Rows, Columns) < 0
10393 || put_eol(fd) == FAIL)
10394 return FAIL;
10395 }
10396
10397#ifdef FEAT_GUI
10398 if (gui.in_use && (ssop_flags & SSOP_WINPOS))
10399 {
10400 int x, y;
10401
10402 if (gui_mch_get_winpos(&x, &y) == OK)
10403 {
10404 /* Note: after the restore we still check it worked!*/
10405 if (fprintf(fd, "winpos %d %d", x, y) < 0 || put_eol(fd) == FAIL)
10406 return FAIL;
10407 }
10408 }
10409#endif
10410
10411 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000010412 * May repeat putting Windows for each tab, when "tabpages" is in
10413 * 'sessionoptions'.
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010414 * Don't use goto_tabpage(), it may change directory and trigger
10415 * autocommands.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010416 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010417 tab_firstwin = firstwin; /* first window in tab page "tabnr" */
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010418 tab_topframe = topframe;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010419 for (tabnr = 1; ; ++tabnr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010420 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010421 int need_tabnew = FALSE;
10422
Bram Moolenaar18144c82006-04-12 21:52:12 +000010423 if ((ssop_flags & SSOP_TABPAGES))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010424 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010425 tabpage_T *tp = find_tabpage(tabnr);
10426
10427 if (tp == NULL)
10428 break; /* done all tab pages */
10429 if (tp == curtab)
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010430 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010431 tab_firstwin = firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010432 tab_topframe = topframe;
10433 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010434 else
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010435 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010436 tab_firstwin = tp->tp_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010437 tab_topframe = tp->tp_topframe;
10438 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010439 if (tabnr > 1)
10440 need_tabnew = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010441 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010442
Bram Moolenaar18144c82006-04-12 21:52:12 +000010443 /*
10444 * Before creating the window layout, try loading one file. If this
10445 * is aborted we don't end up with a number of useless windows.
10446 * This may have side effects! (e.g., compressed or network file).
10447 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010448 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010449 {
10450 if (ses_do_win(wp)
10451 && wp->w_buffer->b_ffname != NULL
10452 && !wp->w_buffer->b_help
10453#ifdef FEAT_QUICKFIX
10454 && !bt_nofile(wp->w_buffer)
10455#endif
10456 )
10457 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010458 if (fputs(need_tabnew ? "tabedit " : "edit ", fd) < 0
Bram Moolenaar18144c82006-04-12 21:52:12 +000010459 || ses_fname(fd, wp->w_buffer, &ssop_flags) == FAIL)
10460 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010461 need_tabnew = FALSE;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010462 if (!wp->w_arg_idx_invalid)
10463 edited_win = wp;
10464 break;
10465 }
10466 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010467
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010468 /* If no file got edited create an empty tab page. */
10469 if (need_tabnew && put_line(fd, "tabnew") == FAIL)
10470 return FAIL;
10471
Bram Moolenaar18144c82006-04-12 21:52:12 +000010472 /*
10473 * Save current window layout.
10474 */
10475 if (put_line(fd, "set splitbelow splitright") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010476 return FAIL;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010477 if (ses_win_rec(fd, tab_topframe) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010478 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010479 if (!p_sb && put_line(fd, "set nosplitbelow") == FAIL)
10480 return FAIL;
10481 if (!p_spr && put_line(fd, "set nosplitright") == FAIL)
10482 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010483
Bram Moolenaar18144c82006-04-12 21:52:12 +000010484 /*
10485 * Check if window sizes can be restored (no windows omitted).
10486 * Remember the window number of the current window after restoring.
10487 */
10488 nr = 0;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010489 for (wp = tab_firstwin; wp != NULL; wp = W_NEXT(wp))
Bram Moolenaar18144c82006-04-12 21:52:12 +000010490 {
10491 if (ses_do_win(wp))
10492 ++nr;
10493 else
10494 restore_size = FALSE;
10495 if (curwin == wp)
10496 cnr = nr;
10497 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010498
Bram Moolenaar18144c82006-04-12 21:52:12 +000010499 /* Go to the first window. */
10500 if (put_line(fd, "wincmd t") == FAIL)
10501 return FAIL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010502
Bram Moolenaar18144c82006-04-12 21:52:12 +000010503 /*
10504 * If more than one window, see if sizes can be restored.
10505 * First set 'winheight' and 'winwidth' to 1 to avoid the windows being
10506 * resized when moving between windows.
10507 * Do this before restoring the view, so that the topline and the
10508 * cursor can be set. This is done again below.
10509 */
10510 if (put_line(fd, "set winheight=1 winwidth=1") == FAIL)
10511 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010512 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010513 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010514
Bram Moolenaar18144c82006-04-12 21:52:12 +000010515 /*
10516 * Restore the view of the window (options, file, cursor, etc.).
10517 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010518 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010519 {
10520 if (!ses_do_win(wp))
10521 continue;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010522 if (put_view(fd, wp, wp != edited_win, &ssop_flags,
10523 cur_arg_idx) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010524 return FAIL;
10525 if (nr > 1 && put_line(fd, "wincmd w") == FAIL)
10526 return FAIL;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010527 next_arg_idx = wp->w_arg_idx;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010528 }
10529
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010530 /* The argument index in the first tab page is zero, need to set it in
10531 * each window. For further tab pages it's the window where we do
10532 * "tabedit". */
10533 cur_arg_idx = next_arg_idx;
10534
Bram Moolenaar18144c82006-04-12 21:52:12 +000010535 /*
10536 * Restore cursor to the current window if it's not the first one.
10537 */
10538 if (cnr > 1 && (fprintf(fd, "%dwincmd w", cnr) < 0
10539 || put_eol(fd) == FAIL))
10540 return FAIL;
10541
10542 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000010543 * Restore window sizes again after jumping around in windows, because
10544 * the current window has a minimum size while others may not.
10545 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010546 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010547 return FAIL;
10548
Bram Moolenaar18144c82006-04-12 21:52:12 +000010549 /* Don't continue in another tab page when doing only the current one
10550 * or when at the last tab page. */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010551 if (!(ssop_flags & SSOP_TABPAGES))
Bram Moolenaar18144c82006-04-12 21:52:12 +000010552 break;
10553 }
10554
10555 if (ssop_flags & SSOP_TABPAGES)
10556 {
Bram Moolenaar18144c82006-04-12 21:52:12 +000010557 if (fprintf(fd, "tabnext %d", tabpage_index(curtab)) < 0
10558 || put_eol(fd) == FAIL)
10559 return FAIL;
10560 }
10561
Bram Moolenaar9c102382006-05-03 21:26:49 +000010562 /*
10563 * Wipe out an empty unnamed buffer we started in.
10564 */
10565 if (put_line(fd, "if exists('s:wipebuf')") == FAIL)
10566 return FAIL;
Bram Moolenaarf3a67882006-05-05 21:09:41 +000010567 if (put_line(fd, " silent exe 'bwipe ' . s:wipebuf") == FAIL)
Bram Moolenaar9c102382006-05-03 21:26:49 +000010568 return FAIL;
10569 if (put_line(fd, "endif") == FAIL)
10570 return FAIL;
10571 if (put_line(fd, "unlet! s:wipebuf") == FAIL)
10572 return FAIL;
10573
10574 /* Re-apply 'winheight', 'winwidth' and 'shortmess'. */
10575 if (fprintf(fd, "set winheight=%ld winwidth=%ld shortmess=%s",
10576 p_wh, p_wiw, p_shm) < 0 || put_eol(fd) == FAIL)
10577 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010578
10579 /*
10580 * Lastly, execute the x.vim file if it exists.
10581 */
10582 if (put_line(fd, "let s:sx = expand(\"<sfile>:p:r\").\"x.vim\"") == FAIL
10583 || put_line(fd, "if file_readable(s:sx)") == FAIL
Bram Moolenaar42ba1262008-12-09 10:18:03 +000010584 || put_line(fd, " exe \"source \" . fnameescape(s:sx)") == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000010585 || put_line(fd, "endif") == FAIL)
10586 return FAIL;
10587
10588 return OK;
10589}
10590
10591 static int
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010592ses_winsizes(fd, restore_size, tab_firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010593 FILE *fd;
10594 int restore_size;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010595 win_T *tab_firstwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010596{
10597 int n = 0;
10598 win_T *wp;
10599
10600 if (restore_size && (ssop_flags & SSOP_WINSIZE))
10601 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010602 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010603 {
10604 if (!ses_do_win(wp))
10605 continue;
10606 ++n;
10607
10608 /* restore height when not full height */
10609 if (wp->w_height + wp->w_status_height < topframe->fr_height
10610 && (fprintf(fd,
10611 "exe '%dresize ' . ((&lines * %ld + %ld) / %ld)",
10612 n, (long)wp->w_height, Rows / 2, Rows) < 0
10613 || put_eol(fd) == FAIL))
10614 return FAIL;
10615
10616 /* restore width when not full width */
10617 if (wp->w_width < Columns && (fprintf(fd,
10618 "exe 'vert %dresize ' . ((&columns * %ld + %ld) / %ld)",
10619 n, (long)wp->w_width, Columns / 2, Columns) < 0
10620 || put_eol(fd) == FAIL))
10621 return FAIL;
10622 }
10623 }
10624 else
10625 {
10626 /* Just equalise window sizes */
10627 if (put_line(fd, "wincmd =") == FAIL)
10628 return FAIL;
10629 }
10630 return OK;
10631}
10632
10633/*
10634 * Write commands to "fd" to recursively create windows for frame "fr",
10635 * horizontally and vertically split.
10636 * After the commands the last window in the frame is the current window.
10637 * Returns FAIL when writing the commands to "fd" fails.
10638 */
10639 static int
10640ses_win_rec(fd, fr)
10641 FILE *fd;
10642 frame_T *fr;
10643{
10644 frame_T *frc;
10645 int count = 0;
10646
10647 if (fr->fr_layout != FR_LEAF)
10648 {
10649 /* Find first frame that's not skipped and then create a window for
10650 * each following one (first frame is already there). */
10651 frc = ses_skipframe(fr->fr_child);
10652 if (frc != NULL)
10653 while ((frc = ses_skipframe(frc->fr_next)) != NULL)
10654 {
10655 /* Make window as big as possible so that we have lots of room
10656 * to split. */
10657 if (put_line(fd, "wincmd _ | wincmd |") == FAIL
10658 || put_line(fd, fr->fr_layout == FR_COL
10659 ? "split" : "vsplit") == FAIL)
10660 return FAIL;
10661 ++count;
10662 }
10663
10664 /* Go back to the first window. */
10665 if (count > 0 && (fprintf(fd, fr->fr_layout == FR_COL
10666 ? "%dwincmd k" : "%dwincmd h", count) < 0
10667 || put_eol(fd) == FAIL))
10668 return FAIL;
10669
10670 /* Recursively create frames/windows in each window of this column or
10671 * row. */
10672 frc = ses_skipframe(fr->fr_child);
10673 while (frc != NULL)
10674 {
10675 ses_win_rec(fd, frc);
10676 frc = ses_skipframe(frc->fr_next);
10677 /* Go to next window. */
10678 if (frc != NULL && put_line(fd, "wincmd w") == FAIL)
10679 return FAIL;
10680 }
10681 }
10682 return OK;
10683}
10684
10685/*
10686 * Skip frames that don't contain windows we want to save in the Session.
10687 * Returns NULL when there none.
10688 */
10689 static frame_T *
10690ses_skipframe(fr)
10691 frame_T *fr;
10692{
10693 frame_T *frc;
10694
10695 for (frc = fr; frc != NULL; frc = frc->fr_next)
10696 if (ses_do_frame(frc))
10697 break;
10698 return frc;
10699}
10700
10701/*
10702 * Return TRUE if frame "fr" has a window somewhere that we want to save in
10703 * the Session.
10704 */
10705 static int
10706ses_do_frame(fr)
10707 frame_T *fr;
10708{
10709 frame_T *frc;
10710
10711 if (fr->fr_layout == FR_LEAF)
10712 return ses_do_win(fr->fr_win);
10713 for (frc = fr->fr_child; frc != NULL; frc = frc->fr_next)
10714 if (ses_do_frame(frc))
10715 return TRUE;
10716 return FALSE;
10717}
10718
10719/*
10720 * Return non-zero if window "wp" is to be stored in the Session.
10721 */
10722 static int
10723ses_do_win(wp)
10724 win_T *wp;
10725{
10726 if (wp->w_buffer->b_fname == NULL
10727#ifdef FEAT_QUICKFIX
10728 /* When 'buftype' is "nofile" can't restore the window contents. */
10729 || bt_nofile(wp->w_buffer)
10730#endif
10731 )
10732 return (ssop_flags & SSOP_BLANK);
10733 if (wp->w_buffer->b_help)
10734 return (ssop_flags & SSOP_HELP);
10735 return TRUE;
10736}
10737
10738/*
10739 * Write commands to "fd" to restore the view of a window.
10740 * Caller must make sure 'scrolloff' is zero.
10741 */
10742 static int
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010743put_view(fd, wp, add_edit, flagp, current_arg_idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010744 FILE *fd;
10745 win_T *wp;
10746 int add_edit; /* add ":edit" command to view */
10747 unsigned *flagp; /* vop_flags or ssop_flags */
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010748 int current_arg_idx; /* current argument index of the window, use
10749 * -1 if unknown */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010750{
10751 win_T *save_curwin;
10752 int f;
10753 int do_cursor;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010754 int did_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010755
10756 /* Always restore cursor position for ":mksession". For ":mkview" only
10757 * when 'viewoptions' contains "cursor". */
10758 do_cursor = (flagp == &ssop_flags || *flagp & SSOP_CURSOR);
10759
10760 /*
10761 * Local argument list.
10762 */
10763 if (wp->w_alist == &global_alist)
10764 {
10765 if (put_line(fd, "argglobal") == FAIL)
10766 return FAIL;
10767 }
10768 else
10769 {
10770 if (ses_arglist(fd, "arglocal", &wp->w_alist->al_ga,
10771 flagp == &vop_flags
10772 || !(*flagp & SSOP_CURDIR)
10773 || wp->w_localdir != NULL, flagp) == FAIL)
10774 return FAIL;
10775 }
10776
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010777 /* Only when part of a session: restore the argument index. Some
10778 * arguments may have been deleted, check if the index is valid. */
Bram Moolenaar51f53df2010-06-26 05:25:54 +020010779 if (wp->w_arg_idx != current_arg_idx && wp->w_arg_idx < WARGCOUNT(wp)
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010780 && flagp == &ssop_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010781 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010782 if (fprintf(fd, "%ldargu", (long)wp->w_arg_idx + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010783 || put_eol(fd) == FAIL)
10784 return FAIL;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010785 did_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010786 }
10787
10788 /* Edit the file. Skip this when ":next" already did it. */
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010789 if (add_edit && (!did_next || wp->w_arg_idx_invalid))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010790 {
10791 /*
10792 * Load the file.
10793 */
10794 if (wp->w_buffer->b_ffname != NULL
10795#ifdef FEAT_QUICKFIX
10796 && !bt_nofile(wp->w_buffer)
10797#endif
10798 )
10799 {
10800 /*
10801 * Editing a file in this buffer: use ":edit file".
10802 * This may have side effects! (e.g., compressed or network file).
10803 */
10804 if (fputs("edit ", fd) < 0
10805 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
10806 return FAIL;
10807 }
10808 else
10809 {
10810 /* No file in this buffer, just make it empty. */
10811 if (put_line(fd, "enew") == FAIL)
10812 return FAIL;
10813#ifdef FEAT_QUICKFIX
10814 if (wp->w_buffer->b_ffname != NULL)
10815 {
10816 /* The buffer does have a name, but it's not a file name. */
10817 if (fputs("file ", fd) < 0
10818 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
10819 return FAIL;
10820 }
10821#endif
10822 do_cursor = FALSE;
10823 }
10824 }
10825
10826 /*
10827 * Local mappings and abbreviations.
10828 */
10829 if ((*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
10830 && makemap(fd, wp->w_buffer) == FAIL)
10831 return FAIL;
10832
10833 /*
10834 * Local options. Need to go to the window temporarily.
10835 * Store only local values when using ":mkview" and when ":mksession" is
10836 * used and 'sessionoptions' doesn't include "options".
10837 * Some folding options are always stored when "folds" is included,
10838 * otherwise the folds would not be restored correctly.
10839 */
10840 save_curwin = curwin;
10841 curwin = wp;
10842 curbuf = curwin->w_buffer;
10843 if (*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
10844 f = makeset(fd, OPT_LOCAL,
10845 flagp == &vop_flags || !(*flagp & SSOP_OPTIONS));
10846#ifdef FEAT_FOLDING
10847 else if (*flagp & SSOP_FOLDS)
10848 f = makefoldset(fd);
10849#endif
10850 else
10851 f = OK;
10852 curwin = save_curwin;
10853 curbuf = curwin->w_buffer;
10854 if (f == FAIL)
10855 return FAIL;
10856
10857#ifdef FEAT_FOLDING
10858 /*
10859 * Save Folds when 'buftype' is empty and for help files.
10860 */
10861 if ((*flagp & SSOP_FOLDS)
10862 && wp->w_buffer->b_ffname != NULL
Bram Moolenaarb1b715d2006-01-21 22:09:43 +000010863# ifdef FEAT_QUICKFIX
10864 && (*wp->w_buffer->b_p_bt == NUL || wp->w_buffer->b_help)
10865# endif
10866 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000010867 {
10868 if (put_folds(fd, wp) == FAIL)
10869 return FAIL;
10870 }
10871#endif
10872
10873 /*
10874 * Set the cursor after creating folds, since that moves the cursor.
10875 */
10876 if (do_cursor)
10877 {
10878
10879 /* Restore the cursor line in the file and relatively in the
10880 * window. Don't use "G", it changes the jumplist. */
10881 if (fprintf(fd, "let s:l = %ld - ((%ld * winheight(0) + %ld) / %ld)",
10882 (long)wp->w_cursor.lnum,
10883 (long)(wp->w_cursor.lnum - wp->w_topline),
10884 (long)wp->w_height / 2, (long)wp->w_height) < 0
10885 || put_eol(fd) == FAIL
10886 || put_line(fd, "if s:l < 1 | let s:l = 1 | endif") == FAIL
10887 || put_line(fd, "exe s:l") == FAIL
10888 || put_line(fd, "normal! zt") == FAIL
10889 || fprintf(fd, "%ld", (long)wp->w_cursor.lnum) < 0
10890 || put_eol(fd) == FAIL)
10891 return FAIL;
10892 /* Restore the cursor column and left offset when not wrapping. */
10893 if (wp->w_cursor.col == 0)
10894 {
10895 if (put_line(fd, "normal! 0") == FAIL)
10896 return FAIL;
10897 }
10898 else
10899 {
10900 if (!wp->w_p_wrap && wp->w_leftcol > 0 && wp->w_width > 0)
10901 {
10902 if (fprintf(fd,
10903 "let s:c = %ld - ((%ld * winwidth(0) + %ld) / %ld)",
Bram Moolenaar558ddad2013-02-20 19:26:29 +010010904 (long)wp->w_virtcol + 1,
10905 (long)(wp->w_virtcol - wp->w_leftcol),
Bram Moolenaar071d4272004-06-13 20:20:40 +000010906 (long)wp->w_width / 2, (long)wp->w_width) < 0
10907 || put_eol(fd) == FAIL
10908 || put_line(fd, "if s:c > 0") == FAIL
10909 || fprintf(fd,
Bram Moolenaar558ddad2013-02-20 19:26:29 +010010910 " exe 'normal! ' . s:c . '|zs' . %ld . '|'",
10911 (long)wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010912 || put_eol(fd) == FAIL
10913 || put_line(fd, "else") == FAIL
Bram Moolenaarfdf447b2013-02-26 17:21:29 +010010914 || fprintf(fd, " normal! 0%d|", wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010915 || put_eol(fd) == FAIL
10916 || put_line(fd, "endif") == FAIL)
10917 return FAIL;
10918 }
10919 else
10920 {
Bram Moolenaar558ddad2013-02-20 19:26:29 +010010921 if (fprintf(fd, "normal! 0%d|", wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010922 || put_eol(fd) == FAIL)
10923 return FAIL;
10924 }
10925 }
10926 }
10927
10928 /*
10929 * Local directory.
10930 */
10931 if (wp->w_localdir != NULL)
10932 {
10933 if (fputs("lcd ", fd) < 0
10934 || ses_put_fname(fd, wp->w_localdir, flagp) == FAIL
10935 || put_eol(fd) == FAIL)
10936 return FAIL;
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010937 did_lcd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010938 }
10939
10940 return OK;
10941}
10942
10943/*
10944 * Write an argument list to the session file.
10945 * Returns FAIL if writing fails.
10946 */
10947 static int
10948ses_arglist(fd, cmd, gap, fullname, flagp)
10949 FILE *fd;
10950 char *cmd;
10951 garray_T *gap;
10952 int fullname; /* TRUE: use full path name */
10953 unsigned *flagp;
10954{
10955 int i;
Bram Moolenaard9462e32011-04-11 21:35:11 +020010956 char_u *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010957 char_u *s;
10958
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010010959 if (fputs(cmd, fd) < 0 || put_eol(fd) == FAIL)
10960 return FAIL;
10961 if (put_line(fd, "silent! argdel *") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010962 return FAIL;
10963 for (i = 0; i < gap->ga_len; ++i)
10964 {
10965 /* NULL file names are skipped (only happens when out of memory). */
10966 s = alist_name(&((aentry_T *)gap->ga_data)[i]);
10967 if (s != NULL)
10968 {
10969 if (fullname)
10970 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020010971 buf = alloc(MAXPATHL);
10972 if (buf != NULL)
10973 {
10974 (void)vim_FullName(s, buf, MAXPATHL, FALSE);
10975 s = buf;
10976 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010977 }
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010010978 if (fputs("argadd ", fd) < 0
10979 || ses_put_fname(fd, s, flagp) == FAIL
10980 || put_eol(fd) == FAIL)
Bram Moolenaard9462e32011-04-11 21:35:11 +020010981 {
10982 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010983 return FAIL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020010984 }
10985 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010986 }
10987 }
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010010988 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010989}
10990
10991/*
10992 * Write a buffer name to the session file.
10993 * Also ends the line.
10994 * Returns FAIL if writing fails.
10995 */
10996 static int
10997ses_fname(fd, buf, flagp)
10998 FILE *fd;
10999 buf_T *buf;
11000 unsigned *flagp;
11001{
11002 char_u *name;
11003
11004 /* Use the short file name if the current directory is known at the time
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011005 * the session file will be sourced.
11006 * Don't do this for ":mkview", we don't know the current directory.
11007 * Don't do this after ":lcd", we don't keep track of what the current
11008 * directory is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011009 if (buf->b_sfname != NULL
11010 && flagp == &ssop_flags
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011011 && (ssop_flags & (SSOP_CURDIR | SSOP_SESDIR))
Bram Moolenaar8d594672009-07-01 18:18:57 +000011012#ifdef FEAT_AUTOCHDIR
11013 && !p_acd
11014#endif
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011015 && !did_lcd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011016 name = buf->b_sfname;
11017 else
11018 name = buf->b_ffname;
11019 if (ses_put_fname(fd, name, flagp) == FAIL || put_eol(fd) == FAIL)
11020 return FAIL;
11021 return OK;
11022}
11023
11024/*
11025 * Write a file name to the session file.
11026 * Takes care of the "slash" option in 'sessionoptions' and escapes special
11027 * characters.
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011028 * Returns FAIL if writing fails or out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011029 */
11030 static int
11031ses_put_fname(fd, name, flagp)
11032 FILE *fd;
11033 char_u *name;
11034 unsigned *flagp;
11035{
11036 char_u *sname;
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011037 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011038 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011039
11040 sname = home_replace_save(NULL, name);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011041 if (sname == NULL)
11042 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011043
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011044 if (*flagp & SSOP_SLASH)
11045 {
11046 /* change all backslashes to forward slashes */
11047 for (p = sname; *p != NUL; mb_ptr_adv(p))
11048 if (*p == '\\')
11049 *p = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +000011050 }
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011051
Bram Moolenaar84a05ac2013-05-06 04:24:17 +020011052 /* escape special characters */
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011053 p = vim_strsave_fnameescape(sname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011054 vim_free(sname);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011055 if (p == NULL)
11056 return FAIL;
11057
11058 /* write the result */
11059 if (fputs((char *)p, fd) < 0)
11060 retval = FAIL;
11061
11062 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011063 return retval;
11064}
11065
11066/*
11067 * ":loadview [nr]"
11068 */
11069 static void
11070ex_loadview(eap)
11071 exarg_T *eap;
11072{
11073 char_u *fname;
11074
11075 fname = get_view_file(*eap->arg);
11076 if (fname != NULL)
11077 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +000011078 do_source(fname, FALSE, DOSO_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011079 vim_free(fname);
11080 }
11081}
11082
11083/*
11084 * Get the name of the view file for the current buffer.
11085 */
11086 static char_u *
11087get_view_file(c)
11088 int c;
11089{
11090 int len = 0;
11091 char_u *p, *s;
11092 char_u *retval;
11093 char_u *sname;
11094
11095 if (curbuf->b_ffname == NULL)
11096 {
11097 EMSG(_(e_noname));
11098 return NULL;
11099 }
11100 sname = home_replace_save(NULL, curbuf->b_ffname);
11101 if (sname == NULL)
11102 return NULL;
11103
11104 /*
11105 * We want a file name without separators, because we're not going to make
11106 * a directory.
11107 * "normal" path separator -> "=+"
11108 * "=" -> "=="
11109 * ":" path separator -> "=-"
11110 */
11111 for (p = sname; *p; ++p)
11112 if (*p == '=' || vim_ispathsep(*p))
11113 ++len;
11114 retval = alloc((unsigned)(STRLEN(sname) + len + STRLEN(p_vdir) + 9));
11115 if (retval != NULL)
11116 {
11117 STRCPY(retval, p_vdir);
11118 add_pathsep(retval);
11119 s = retval + STRLEN(retval);
11120 for (p = sname; *p; ++p)
11121 {
11122 if (*p == '=')
11123 {
11124 *s++ = '=';
11125 *s++ = '=';
11126 }
11127 else if (vim_ispathsep(*p))
11128 {
11129 *s++ = '=';
Bram Moolenaare60acc12011-05-10 16:41:25 +020011130#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011131 if (*p == ':')
11132 *s++ = '-';
11133 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000011134#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000011135 *s++ = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +000011136 }
11137 else
11138 *s++ = *p;
11139 }
11140 *s++ = '=';
11141 *s++ = c;
11142 STRCPY(s, ".vim");
11143 }
11144
11145 vim_free(sname);
11146 return retval;
11147}
11148
11149#endif /* FEAT_SESSION */
11150
11151/*
11152 * Write end-of-line character(s) for ":mkexrc", ":mkvimrc" and ":mksession".
11153 * Return FAIL for a write error.
11154 */
11155 int
11156put_eol(fd)
11157 FILE *fd;
11158{
11159 if (
11160#ifdef USE_CRNL
11161 (
11162# ifdef MKSESSION_NL
11163 !mksession_nl &&
11164# endif
11165 (putc('\r', fd) < 0)) ||
11166#endif
11167 (putc('\n', fd) < 0))
11168 return FAIL;
11169 return OK;
11170}
11171
11172/*
11173 * Write a line to "fd".
11174 * Return FAIL for a write error.
11175 */
11176 int
11177put_line(fd, s)
11178 FILE *fd;
11179 char *s;
11180{
11181 if (fputs(s, fd) < 0 || put_eol(fd) == FAIL)
11182 return FAIL;
11183 return OK;
11184}
11185
11186#ifdef FEAT_VIMINFO
11187/*
11188 * ":rviminfo" and ":wviminfo".
11189 */
11190 static void
11191ex_viminfo(eap)
11192 exarg_T *eap;
11193{
11194 char_u *save_viminfo;
11195
11196 save_viminfo = p_viminfo;
11197 if (*p_viminfo == NUL)
11198 p_viminfo = (char_u *)"'100";
11199 if (eap->cmdidx == CMD_rviminfo)
11200 {
Bram Moolenaard812df62008-11-09 12:46:09 +000011201 if (read_viminfo(eap->arg, VIF_WANT_INFO | VIF_WANT_MARKS
11202 | (eap->forceit ? VIF_FORCEIT : 0)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011203 EMSG(_("E195: Cannot open viminfo file for reading"));
11204 }
11205 else
11206 write_viminfo(eap->arg, eap->forceit);
11207 p_viminfo = save_viminfo;
11208}
11209#endif
11210
11211#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +000011212/*
Bram Moolenaard9462e32011-04-11 21:35:11 +020011213 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +000011214 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +000011215 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011216 void
11217dialog_msg(buff, format, fname)
11218 char_u *buff;
11219 char *format;
11220 char_u *fname;
11221{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011222 if (fname == NULL)
11223 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +020011224 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011225}
11226#endif
11227
11228/*
11229 * ":behave {mswin,xterm}"
11230 */
11231 static void
11232ex_behave(eap)
11233 exarg_T *eap;
11234{
11235 if (STRCMP(eap->arg, "mswin") == 0)
11236 {
11237 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
11238 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
11239 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
11240 set_option_value((char_u *)"keymodel", 0L,
11241 (char_u *)"startsel,stopsel", 0);
11242 }
11243 else if (STRCMP(eap->arg, "xterm") == 0)
11244 {
11245 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
11246 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
11247 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
11248 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
11249 }
11250 else
11251 EMSG2(_(e_invarg2), eap->arg);
11252}
11253
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010011254#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
11255/*
11256 * Function given to ExpandGeneric() to obtain the possible arguments of the
11257 * ":behave {mswin,xterm}" command.
11258 */
11259 char_u *
11260get_behave_arg(xp, idx)
11261 expand_T *xp UNUSED;
11262 int idx;
11263{
11264 if (idx == 0)
11265 return (char_u *)"mswin";
11266 if (idx == 1)
11267 return (char_u *)"xterm";
11268 return NULL;
11269}
11270#endif
11271
Bram Moolenaar071d4272004-06-13 20:20:40 +000011272#ifdef FEAT_AUTOCMD
11273static int filetype_detect = FALSE;
11274static int filetype_plugin = FALSE;
11275static int filetype_indent = FALSE;
11276
11277/*
11278 * ":filetype [plugin] [indent] {on,off,detect}"
11279 * on: Load the filetype.vim file to install autocommands for file types.
11280 * off: Load the ftoff.vim file to remove all autocommands for file types.
11281 * plugin on: load filetype.vim and ftplugin.vim
11282 * plugin off: load ftplugof.vim
11283 * indent on: load filetype.vim and indent.vim
11284 * indent off: load indoff.vim
11285 */
11286 static void
11287ex_filetype(eap)
11288 exarg_T *eap;
11289{
11290 char_u *arg = eap->arg;
11291 int plugin = FALSE;
11292 int indent = FALSE;
11293
11294 if (*eap->arg == NUL)
11295 {
11296 /* Print current status. */
11297 smsg((char_u *)"filetype detection:%s plugin:%s indent:%s",
11298 filetype_detect ? "ON" : "OFF",
11299 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
11300 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
11301 return;
11302 }
11303
11304 /* Accept "plugin" and "indent" in any order. */
11305 for (;;)
11306 {
11307 if (STRNCMP(arg, "plugin", 6) == 0)
11308 {
11309 plugin = TRUE;
11310 arg = skipwhite(arg + 6);
11311 continue;
11312 }
11313 if (STRNCMP(arg, "indent", 6) == 0)
11314 {
11315 indent = TRUE;
11316 arg = skipwhite(arg + 6);
11317 continue;
11318 }
11319 break;
11320 }
11321 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
11322 {
11323 if (*arg == 'o' || !filetype_detect)
11324 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011325 source_runtime((char_u *)FILETYPE_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011326 filetype_detect = TRUE;
11327 if (plugin)
11328 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011329 source_runtime((char_u *)FTPLUGIN_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011330 filetype_plugin = TRUE;
11331 }
11332 if (indent)
11333 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011334 source_runtime((char_u *)INDENT_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011335 filetype_indent = TRUE;
11336 }
11337 }
11338 if (*arg == 'd')
11339 {
11340 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE);
Bram Moolenaara3227e22006-03-08 21:32:40 +000011341 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011342 }
11343 }
11344 else if (STRCMP(arg, "off") == 0)
11345 {
11346 if (plugin || indent)
11347 {
11348 if (plugin)
11349 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011350 source_runtime((char_u *)FTPLUGOF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011351 filetype_plugin = FALSE;
11352 }
11353 if (indent)
11354 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011355 source_runtime((char_u *)INDOFF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011356 filetype_indent = FALSE;
11357 }
11358 }
11359 else
11360 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011361 source_runtime((char_u *)FTOFF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011362 filetype_detect = FALSE;
11363 }
11364 }
11365 else
11366 EMSG2(_(e_invarg2), arg);
11367}
11368
11369/*
11370 * ":setfiletype {name}"
11371 */
11372 static void
11373ex_setfiletype(eap)
11374 exarg_T *eap;
11375{
11376 if (!did_filetype)
11377 set_option_value((char_u *)"filetype", 0L, eap->arg, OPT_LOCAL);
11378}
11379#endif
11380
Bram Moolenaar071d4272004-06-13 20:20:40 +000011381 static void
11382ex_digraphs(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +000011383 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011384{
11385#ifdef FEAT_DIGRAPHS
11386 if (*eap->arg != NUL)
11387 putdigraph(eap->arg);
11388 else
11389 listdigraphs();
11390#else
11391 EMSG(_("E196: No digraphs in this version"));
11392#endif
11393}
11394
11395 static void
11396ex_set(eap)
11397 exarg_T *eap;
11398{
11399 int flags = 0;
11400
11401 if (eap->cmdidx == CMD_setlocal)
11402 flags = OPT_LOCAL;
11403 else if (eap->cmdidx == CMD_setglobal)
11404 flags = OPT_GLOBAL;
11405#if defined(FEAT_EVAL) && defined(FEAT_AUTOCMD) && defined(FEAT_BROWSE)
11406 if (cmdmod.browse && flags == 0)
11407 ex_options(eap);
11408 else
11409#endif
11410 (void)do_set(eap->arg, flags);
11411}
11412
11413#ifdef FEAT_SEARCH_EXTRA
11414/*
11415 * ":nohlsearch"
11416 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011417 static void
11418ex_nohlsearch(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +000011419 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011420{
Bram Moolenaar8050efa2013-11-08 04:30:20 +010011421 SET_NO_HLSEARCH(TRUE);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +000011422 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011423}
11424
11425/*
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011426 * ":[N]match {group} {pattern}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000011427 * Sets nextcmd to the start of the next command, if any. Also called when
11428 * skipping commands to find the next command.
11429 */
11430 static void
11431ex_match(eap)
11432 exarg_T *eap;
11433{
11434 char_u *p;
Bram Moolenaar52d36c82007-08-11 14:00:30 +000011435 char_u *g = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011436 char_u *end;
11437 int c;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011438 int id;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011439
11440 if (eap->line2 <= 3)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011441 id = eap->line2;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011442 else
11443 {
11444 EMSG(e_invcmd);
11445 return;
11446 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011447
11448 /* First clear any old pattern. */
11449 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011450 match_delete(curwin, id, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011451
11452 if (ends_excmd(*eap->arg))
11453 end = eap->arg;
11454 else if ((STRNICMP(eap->arg, "none", 4) == 0
11455 && (vim_iswhite(eap->arg[4]) || ends_excmd(eap->arg[4]))))
11456 end = eap->arg + 4;
11457 else
11458 {
11459 p = skiptowhite(eap->arg);
11460 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011461 g = vim_strnsave(eap->arg, (int)(p - eap->arg));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011462 p = skipwhite(p);
11463 if (*p == NUL)
11464 {
11465 /* There must be two arguments. */
11466 EMSG2(_(e_invarg2), eap->arg);
11467 return;
11468 }
11469 end = skip_regexp(p + 1, *p, TRUE, NULL);
11470 if (!eap->skip)
11471 {
11472 if (*end != NUL && !ends_excmd(*skipwhite(end + 1)))
11473 {
11474 eap->errmsg = e_trailing;
11475 return;
11476 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000011477 if (*end != *p)
11478 {
11479 EMSG2(_(e_invarg2), p);
11480 return;
11481 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011482
11483 c = *end;
11484 *end = NUL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011485 match_add(curwin, g, p + 1, 10, id);
11486 vim_free(g);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000011487 *end = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011488 }
11489 }
11490 eap->nextcmd = find_nextcmd(end);
11491}
11492#endif
11493
11494#ifdef FEAT_CRYPT
11495/*
11496 * ":X": Get crypt key
11497 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011498 static void
11499ex_X(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +000011500 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011501{
Bram Moolenaar49771f42010-07-20 17:32:38 +020011502 if (get_crypt_method(curbuf) == 0 || blowfish_self_test() == OK)
Bram Moolenaar40e6a712010-05-16 22:32:54 +020011503 (void)get_crypt_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011504}
11505#endif
11506
11507#ifdef FEAT_FOLDING
11508 static void
11509ex_fold(eap)
11510 exarg_T *eap;
11511{
11512 if (foldManualAllowed(TRUE))
11513 foldCreate(eap->line1, eap->line2);
11514}
11515
11516 static void
11517ex_foldopen(eap)
11518 exarg_T *eap;
11519{
11520 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
11521 eap->forceit, FALSE);
11522}
11523
11524 static void
11525ex_folddo(eap)
11526 exarg_T *eap;
11527{
11528 linenr_T lnum;
11529
11530 /* First set the marks for all lines closed/open. */
11531 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
11532 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
11533 ml_setmarked(lnum);
11534
11535 /* Execute the command on the marked lines. */
11536 global_exe(eap->arg);
11537 ml_clearmarked(); /* clear rest of the marks */
11538}
11539#endif