blob: 1fead394810dd6628fe78c63d0c17e2823e8fd67 [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));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000319static void ex_may_print __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000320static void ex_submagic __ARGS((exarg_T *eap));
321static void ex_join __ARGS((exarg_T *eap));
322static void ex_at __ARGS((exarg_T *eap));
323static void ex_bang __ARGS((exarg_T *eap));
324static void ex_undo __ARGS((exarg_T *eap));
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200325#ifdef FEAT_PERSISTENT_UNDO
326static void ex_wundo __ARGS((exarg_T *eap));
327static void ex_rundo __ARGS((exarg_T *eap));
328#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000329static void ex_redo __ARGS((exarg_T *eap));
Bram Moolenaarc7d89352006-03-14 22:53:34 +0000330static void ex_later __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000331static void ex_redir __ARGS((exarg_T *eap));
332static void ex_redraw __ARGS((exarg_T *eap));
333static void ex_redrawstatus __ARGS((exarg_T *eap));
334static void close_redir __ARGS((void));
335static void ex_mkrc __ARGS((exarg_T *eap));
336static void ex_mark __ARGS((exarg_T *eap));
337#ifdef FEAT_USR_CMDS
338static char_u *uc_fun_cmd __ARGS((void));
Bram Moolenaar52b4b552005-03-07 23:00:57 +0000339static 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 +0000340#endif
341#ifdef FEAT_EX_EXTRA
342static void ex_normal __ARGS((exarg_T *eap));
343static void ex_startinsert __ARGS((exarg_T *eap));
344static void ex_stopinsert __ARGS((exarg_T *eap));
345#else
346# define ex_normal ex_ni
347# define ex_align ex_ni
348# define ex_retab ex_ni
349# define ex_startinsert ex_ni
350# define ex_stopinsert ex_ni
351# define ex_helptags ex_ni
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +0000352# define ex_sort ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000353#endif
354#ifdef FEAT_FIND_ID
355static void ex_checkpath __ARGS((exarg_T *eap));
356static void ex_findpat __ARGS((exarg_T *eap));
357#else
358# define ex_findpat ex_ni
359# define ex_checkpath ex_ni
360#endif
361#if defined(FEAT_FIND_ID) && defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
362static void ex_psearch __ARGS((exarg_T *eap));
363#else
364# define ex_psearch ex_ni
365#endif
366static void ex_tag __ARGS((exarg_T *eap));
367static void ex_tag_cmd __ARGS((exarg_T *eap, char_u *name));
368#ifndef FEAT_EVAL
369# define ex_scriptnames ex_ni
370# define ex_finish ex_ni
371# define ex_echo ex_ni
372# define ex_echohl ex_ni
373# define ex_execute ex_ni
374# define ex_call ex_ni
375# define ex_if ex_ni
376# define ex_endif ex_ni
377# define ex_else ex_ni
378# define ex_while ex_ni
379# define ex_continue ex_ni
380# define ex_break ex_ni
381# define ex_endwhile ex_ni
382# define ex_throw ex_ni
383# define ex_try ex_ni
384# define ex_catch ex_ni
385# define ex_finally ex_ni
386# define ex_endtry ex_ni
387# define ex_endfunction ex_ni
388# define ex_let ex_ni
389# define ex_unlet ex_ni
Bram Moolenaar65c1b012005-01-31 19:02:28 +0000390# define ex_lockvar ex_ni
391# define ex_unlockvar ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000392# define ex_function ex_ni
393# define ex_delfunction ex_ni
394# define ex_return ex_ni
Bram Moolenaard812df62008-11-09 12:46:09 +0000395# define ex_oldfiles ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000396#endif
397static char_u *arg_all __ARGS((void));
398#ifdef FEAT_SESSION
399static int makeopens __ARGS((FILE *fd, char_u *dirnow));
Bram Moolenaarf13be0d2008-01-02 14:13:10 +0000400static 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 +0000401static void ex_loadview __ARGS((exarg_T *eap));
402static char_u *get_view_file __ARGS((int c));
Bram Moolenaareeefcc72007-05-01 21:21:21 +0000403static int did_lcd; /* whether ":lcd" was produced for a session */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000404#else
405# define ex_loadview ex_ni
406#endif
407#ifndef FEAT_EVAL
408# define ex_compiler ex_ni
409#endif
410#ifdef FEAT_VIMINFO
411static void ex_viminfo __ARGS((exarg_T *eap));
412#else
413# define ex_viminfo ex_ni
414#endif
415static void ex_behave __ARGS((exarg_T *eap));
416#ifdef FEAT_AUTOCMD
417static void ex_filetype __ARGS((exarg_T *eap));
418static void ex_setfiletype __ARGS((exarg_T *eap));
419#else
420# define ex_filetype ex_ni
421# define ex_setfiletype ex_ni
422#endif
423#ifndef FEAT_DIFF
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000424# define ex_diffoff ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000425# define ex_diffpatch ex_ni
426# define ex_diffgetput ex_ni
427# define ex_diffsplit ex_ni
428# define ex_diffthis ex_ni
429# define ex_diffupdate ex_ni
430#endif
431static void ex_digraphs __ARGS((exarg_T *eap));
432static void ex_set __ARGS((exarg_T *eap));
433#if !defined(FEAT_EVAL) || !defined(FEAT_AUTOCMD)
434# define ex_options ex_ni
435#endif
436#ifdef FEAT_SEARCH_EXTRA
437static void ex_nohlsearch __ARGS((exarg_T *eap));
438static void ex_match __ARGS((exarg_T *eap));
439#else
440# define ex_nohlsearch ex_ni
441# define ex_match ex_ni
442#endif
443#ifdef FEAT_CRYPT
444static void ex_X __ARGS((exarg_T *eap));
445#else
446# define ex_X ex_ni
447#endif
448#ifdef FEAT_FOLDING
449static void ex_fold __ARGS((exarg_T *eap));
450static void ex_foldopen __ARGS((exarg_T *eap));
451static void ex_folddo __ARGS((exarg_T *eap));
452#else
453# define ex_fold ex_ni
454# define ex_foldopen ex_ni
455# define ex_folddo ex_ni
456#endif
457#if !((defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
458 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE)))
459# define ex_language ex_ni
460#endif
461#ifndef FEAT_SIGNS
462# define ex_sign ex_ni
463#endif
464#ifndef FEAT_SUN_WORKSHOP
465# define ex_wsverb ex_ni
466#endif
Bram Moolenaar009b2592004-10-24 19:18:58 +0000467#ifndef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200468# define ex_nbclose ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000469# define ex_nbkey ex_ni
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200470# define ex_nbstart ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000471#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000472
473#ifndef FEAT_EVAL
474# define ex_debug ex_ni
475# define ex_breakadd ex_ni
476# define ex_debuggreedy ex_ni
477# define ex_breakdel ex_ni
478# define ex_breaklist ex_ni
479#endif
480
481#ifndef FEAT_CMDHIST
482# define ex_history ex_ni
483#endif
484#ifndef FEAT_JUMPLIST
485# define ex_jumps ex_ni
486# define ex_changes ex_ni
487#endif
488
Bram Moolenaar05159a02005-02-26 23:04:13 +0000489#ifndef FEAT_PROFILE
490# define ex_profile ex_ni
491#endif
492
Bram Moolenaar071d4272004-06-13 20:20:40 +0000493/*
494 * Declare cmdnames[].
495 */
496#define DO_DECLARE_EXCMD
497#include "ex_cmds.h"
498
499/*
500 * Table used to quickly search for a command, based on its first character.
501 */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +0000502static cmdidx_T cmdidxs[27] =
Bram Moolenaar071d4272004-06-13 20:20:40 +0000503{
504 CMD_append,
505 CMD_buffer,
506 CMD_change,
507 CMD_delete,
508 CMD_edit,
509 CMD_file,
510 CMD_global,
511 CMD_help,
512 CMD_insert,
513 CMD_join,
514 CMD_k,
515 CMD_list,
516 CMD_move,
517 CMD_next,
518 CMD_open,
519 CMD_print,
520 CMD_quit,
521 CMD_read,
522 CMD_substitute,
523 CMD_t,
524 CMD_undo,
525 CMD_vglobal,
526 CMD_write,
527 CMD_xit,
528 CMD_yank,
529 CMD_z,
530 CMD_bang
531};
532
533static char_u dollar_command[2] = {'$', 0};
534
535
536#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000537/* Struct for storing a line inside a while/for loop */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000538typedef struct
539{
540 char_u *line; /* command line */
541 linenr_T lnum; /* sourcing_lnum of the line */
542} wcmd_T;
543
544/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000545 * Structure used to store info for line position in a while or for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000546 * This is required, because do_one_cmd() may invoke ex_function(), which
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000547 * reads more lines that may come from the while/for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000548 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000549struct loop_cookie
Bram Moolenaar071d4272004-06-13 20:20:40 +0000550{
551 garray_T *lines_gap; /* growarray with line info */
552 int current_line; /* last read line from growarray */
553 int repeating; /* TRUE when looping a second time */
554 /* When "repeating" is FALSE use "getline" and "cookie" to get lines */
555 char_u *(*getline) __ARGS((int, void *, int));
556 void *cookie;
557};
558
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000559static char_u *get_loop_line __ARGS((int c, void *cookie, int indent));
560static int store_loop_line __ARGS((garray_T *gap, char_u *line));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000561static void free_cmdlines __ARGS((garray_T *gap));
Bram Moolenaared203462004-06-16 11:19:22 +0000562
563/* Struct to save a few things while debugging. Used in do_cmdline() only. */
564struct dbg_stuff
565{
566 int trylevel;
567 int force_abort;
568 except_T *caught_stack;
569 char_u *vv_exception;
570 char_u *vv_throwpoint;
571 int did_emsg;
572 int got_int;
573 int did_throw;
574 int need_rethrow;
575 int check_cstack;
576 except_T *current_exception;
577};
578
579static void save_dbg_stuff __ARGS((struct dbg_stuff *dsp));
580static void restore_dbg_stuff __ARGS((struct dbg_stuff *dsp));
581
582 static void
583save_dbg_stuff(dsp)
584 struct dbg_stuff *dsp;
585{
586 dsp->trylevel = trylevel; trylevel = 0;
587 dsp->force_abort = force_abort; force_abort = FALSE;
588 dsp->caught_stack = caught_stack; caught_stack = NULL;
589 dsp->vv_exception = v_exception(NULL);
590 dsp->vv_throwpoint = v_throwpoint(NULL);
591
592 /* Necessary for debugging an inactive ":catch", ":finally", ":endtry" */
593 dsp->did_emsg = did_emsg; did_emsg = FALSE;
594 dsp->got_int = got_int; got_int = FALSE;
595 dsp->did_throw = did_throw; did_throw = FALSE;
596 dsp->need_rethrow = need_rethrow; need_rethrow = FALSE;
597 dsp->check_cstack = check_cstack; check_cstack = FALSE;
598 dsp->current_exception = current_exception; current_exception = NULL;
599}
600
601 static void
602restore_dbg_stuff(dsp)
603 struct dbg_stuff *dsp;
604{
605 suppress_errthrow = FALSE;
606 trylevel = dsp->trylevel;
607 force_abort = dsp->force_abort;
608 caught_stack = dsp->caught_stack;
609 (void)v_exception(dsp->vv_exception);
610 (void)v_throwpoint(dsp->vv_throwpoint);
611 did_emsg = dsp->did_emsg;
612 got_int = dsp->got_int;
613 did_throw = dsp->did_throw;
614 need_rethrow = dsp->need_rethrow;
615 check_cstack = dsp->check_cstack;
616 current_exception = dsp->current_exception;
617}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000618#endif
619
620
621/*
622 * do_exmode(): Repeatedly get commands for the "Ex" mode, until the ":vi"
623 * command is given.
624 */
625 void
626do_exmode(improved)
627 int improved; /* TRUE for "improved Ex" mode */
628{
629 int save_msg_scroll;
630 int prev_msg_row;
631 linenr_T prev_line;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000632 int changedtick;
633
634 if (improved)
635 exmode_active = EXMODE_VIM;
636 else
637 exmode_active = EXMODE_NORMAL;
638 State = NORMAL;
639
640 /* When using ":global /pat/ visual" and then "Q" we return to continue
641 * the :global command. */
642 if (global_busy)
643 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000644
645 save_msg_scroll = msg_scroll;
646 ++RedrawingDisabled; /* don't redisplay the window */
647 ++no_wait_return; /* don't wait for return */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000648#ifdef FEAT_GUI
649 /* Ignore scrollbar and mouse events in Ex mode */
650 ++hold_gui_events;
651#endif
652#ifdef FEAT_SNIFF
653 want_sniff_request = 0; /* No K_SNIFF wanted */
654#endif
655
656 MSG(_("Entering Ex mode. Type \"visual\" to go to Normal mode."));
657 while (exmode_active)
658 {
Bram Moolenaar7c626922005-02-07 22:01:03 +0000659#ifdef FEAT_EX_EXTRA
660 /* Check for a ":normal" command and no more characters left. */
661 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
662 {
663 exmode_active = FALSE;
664 break;
665 }
666#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667 msg_scroll = TRUE;
668 need_wait_return = FALSE;
669 ex_pressedreturn = FALSE;
670 ex_no_reprint = FALSE;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000671 changedtick = curbuf->b_changedtick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000672 prev_msg_row = msg_row;
673 prev_line = curwin->w_cursor.lnum;
674#ifdef FEAT_SNIFF
675 ProcessSniffRequests();
676#endif
677 if (improved)
678 {
679 cmdline_row = msg_row;
680 do_cmdline(NULL, getexline, NULL, 0);
681 }
682 else
683 do_cmdline(NULL, getexmodeline, NULL, DOCMD_NOWAIT);
684 lines_left = Rows - 1;
685
Bram Moolenaardf177f62005-02-22 08:39:57 +0000686 if ((prev_line != curwin->w_cursor.lnum
687 || changedtick != curbuf->b_changedtick) && !ex_no_reprint)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000689 if (curbuf->b_ml.ml_flags & ML_EMPTY)
690 EMSG(_(e_emptybuf));
691 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000693 if (ex_pressedreturn)
694 {
695 /* go up one line, to overwrite the ":<CR>" line, so the
Bram Moolenaar81870892007-11-11 18:17:28 +0000696 * output doesn't contain empty lines. */
Bram Moolenaardf177f62005-02-22 08:39:57 +0000697 msg_row = prev_msg_row;
698 if (prev_msg_row == Rows - 1)
699 msg_row--;
700 }
701 msg_col = 0;
702 print_line_no_prefix(curwin->w_cursor.lnum, FALSE, FALSE);
703 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000705 }
Bram Moolenaardf177f62005-02-22 08:39:57 +0000706 else if (ex_pressedreturn && !ex_no_reprint) /* must be at EOF */
707 {
708 if (curbuf->b_ml.ml_flags & ML_EMPTY)
709 EMSG(_(e_emptybuf));
710 else
711 EMSG(_("E501: At end-of-file"));
712 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000713 }
714
715#ifdef FEAT_GUI
716 --hold_gui_events;
717#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000718 --RedrawingDisabled;
719 --no_wait_return;
720 update_screen(CLEAR);
721 need_wait_return = FALSE;
722 msg_scroll = save_msg_scroll;
723}
724
725/*
726 * Execute a simple command line. Used for translated commands like "*".
727 */
728 int
729do_cmdline_cmd(cmd)
730 char_u *cmd;
731{
732 return do_cmdline(cmd, NULL, NULL,
733 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
734}
735
736/*
737 * do_cmdline(): execute one Ex command line
738 *
739 * 1. Execute "cmdline" when it is not NULL.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100740 * If "cmdline" is NULL, or more lines are needed, fgetline() is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000741 * 2. Split up in parts separated with '|'.
742 *
743 * This function can be called recursively!
744 *
745 * flags:
746 * DOCMD_VERBOSE - The command will be included in the error message.
747 * DOCMD_NOWAIT - Don't call wait_return() and friends.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100748 * DOCMD_REPEAT - Repeat execution until fgetline() returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000749 * DOCMD_KEYTYPED - Don't reset KeyTyped.
750 * DOCMD_EXCRESET - Reset the exception environment (used for debugging).
751 * DOCMD_KEEPLINE - Store first typed line (for repeating with ".").
752 *
753 * return FAIL if cmdline could not be executed, OK otherwise
754 */
755 int
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100756do_cmdline(cmdline, fgetline, cookie, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000757 char_u *cmdline;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100758 char_u *(*fgetline) __ARGS((int, void *, int));
759 void *cookie; /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000760 int flags;
761{
762 char_u *next_cmdline; /* next cmd to execute */
763 char_u *cmdline_copy = NULL; /* copy of cmd line */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100764 int used_getline = FALSE; /* used "fgetline" to obtain command */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765 static int recursive = 0; /* recursive depth */
766 int msg_didout_before_start = 0;
767 int count = 0; /* line number count */
768 int did_inc = FALSE; /* incremented RedrawingDisabled */
769 int retval = OK;
770#ifdef FEAT_EVAL
771 struct condstack cstack; /* conditional stack */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000772 garray_T lines_ga; /* keep lines for ":while"/":for" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773 int current_line = 0; /* active line in lines_ga */
774 char_u *fname = NULL; /* function or script name */
775 linenr_T *breakpoint = NULL; /* ptr to breakpoint field in cookie */
776 int *dbg_tick = NULL; /* ptr to dbg_tick field in cookie */
Bram Moolenaared203462004-06-16 11:19:22 +0000777 struct dbg_stuff debug_saved; /* saved things for debug mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778 int initial_trylevel;
779 struct msglist **saved_msg_list = NULL;
780 struct msglist *private_msg_list;
781
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100782 /* "fgetline" and "cookie" passed to do_one_cmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783 char_u *(*cmd_getline) __ARGS((int, void *, int));
784 void *cmd_cookie;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000785 struct loop_cookie cmd_loop_cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786 void *real_cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000787 int getline_is_func;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788#else
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100789# define cmd_getline fgetline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790# define cmd_cookie cookie
791#endif
792 static int call_depth = 0; /* recursiveness */
793
794#ifdef FEAT_EVAL
795 /* For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory
796 * location for storing error messages to be converted to an exception.
Bram Moolenaarcf3630f2005-01-08 16:04:29 +0000797 * This ensures that the do_errthrow() call in do_one_cmd() does not
798 * combine the messages stored by an earlier invocation of do_one_cmd()
799 * with the command name of the later one. This would happen when
800 * BufWritePost autocommands are executed after a write error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801 saved_msg_list = msg_list;
802 msg_list = &private_msg_list;
803 private_msg_list = NULL;
804#endif
805
806 /* It's possible to create an endless loop with ":execute", catch that
807 * here. The value of 200 allows nested function calls, ":source", etc. */
808 if (call_depth == 200)
809 {
810 EMSG(_("E169: Command too recursive"));
811#ifdef FEAT_EVAL
812 /* When converting to an exception, we do not include the command name
813 * since this is not an error of the specific command. */
814 do_errthrow((struct condstack *)NULL, (char_u *)NULL);
815 msg_list = saved_msg_list;
816#endif
817 return FAIL;
818 }
819 ++call_depth;
820
821#ifdef FEAT_EVAL
822 cstack.cs_idx = -1;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000823 cstack.cs_looplevel = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824 cstack.cs_trylevel = 0;
825 cstack.cs_emsg_silent_list = NULL;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000826 cstack.cs_lflags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827 ga_init2(&lines_ga, (int)sizeof(wcmd_T), 10);
828
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100829 real_cookie = getline_cookie(fgetline, cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830
831 /* Inside a function use a higher nesting level. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100832 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000833 if (getline_is_func && ex_nesting_level == func_level(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834 ++ex_nesting_level;
835
836 /* Get the function or script name and the address where the next breakpoint
837 * line and the debug tick for a function or script are stored. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000838 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839 {
840 fname = func_name(real_cookie);
841 breakpoint = func_breakpoint(real_cookie);
842 dbg_tick = func_dbg_tick(real_cookie);
843 }
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100844 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000845 {
846 fname = sourcing_name;
847 breakpoint = source_breakpoint(real_cookie);
848 dbg_tick = source_dbg_tick(real_cookie);
849 }
850
851 /*
852 * Initialize "force_abort" and "suppress_errthrow" at the top level.
853 */
854 if (!recursive)
855 {
856 force_abort = FALSE;
857 suppress_errthrow = FALSE;
858 }
859
860 /*
861 * If requested, store and reset the global values controlling the
Bram Moolenaar89d40322006-08-29 15:30:07 +0000862 * exception handling (used when debugging). Otherwise clear it to avoid
863 * a bogus compiler warning when the optimizer uses inline functions...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000864 */
Bram Moolenaarb4872942006-05-13 10:32:52 +0000865 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +0000866 save_dbg_stuff(&debug_saved);
Bram Moolenaar89d40322006-08-29 15:30:07 +0000867 else
Bram Moolenaar7db5fc82010-05-24 11:59:29 +0200868 vim_memset(&debug_saved, 0, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000869
870 initial_trylevel = trylevel;
871
872 /*
873 * "did_throw" will be set to TRUE when an exception is being thrown.
874 */
875 did_throw = FALSE;
876#endif
877 /*
878 * "did_emsg" will be set to TRUE when emsg() is used, in which case we
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000879 * cancel the whole command line, and any if/endif or loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000880 * If force_abort is set, we cancel everything.
881 */
882 did_emsg = FALSE;
883
884 /*
885 * KeyTyped is only set when calling vgetc(). Reset it here when not
886 * calling vgetc() (sourced command lines).
887 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100888 if (!(flags & DOCMD_KEYTYPED)
889 && !getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000890 KeyTyped = FALSE;
891
892 /*
893 * Continue executing command lines:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000894 * - when inside an ":if", ":while" or ":for"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000895 * - for multiple commands on one line, separated with '|'
896 * - when repeating until there are no more lines (for ":source")
897 */
898 next_cmdline = cmdline;
899 do
900 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000901#ifdef FEAT_EVAL
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100902 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000903#endif
904
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000905 /* stop skipping cmds for an error msg after all endif/while/for */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906 if (next_cmdline == NULL
907#ifdef FEAT_EVAL
908 && !force_abort
909 && cstack.cs_idx < 0
Bram Moolenaar05159a02005-02-26 23:04:13 +0000910 && !(getline_is_func && func_has_abort(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000911#endif
912 )
913 did_emsg = FALSE;
914
915 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000916 * 1. If repeating a line in a loop, get a line from lines_ga.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100917 * 2. If no line given: Get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000918 * 3. If a line is given: Make a copy, so we can mess with it.
919 */
920
921#ifdef FEAT_EVAL
922 /* 1. If repeating, get a previous line from lines_ga. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000923 if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000924 {
925 /* Each '|' separated command is stored separately in lines_ga, to
926 * be able to jump to it. Don't use next_cmdline now. */
927 vim_free(cmdline_copy);
928 cmdline_copy = NULL;
929
930 /* Check if a function has returned or, unless it has an unclosed
931 * try conditional, aborted. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000932 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000933 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000934# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000935 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000936 func_line_end(real_cookie);
937# endif
938 if (func_has_ended(real_cookie))
939 {
940 retval = FAIL;
941 break;
942 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000943 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000944#ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000945 else if (do_profiling == PROF_YES
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100946 && getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000947 script_line_end();
948#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949
950 /* Check if a sourced file hit a ":finish" command. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100951 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000952 {
953 retval = FAIL;
954 break;
955 }
956
957 /* If breakpoints have been added/deleted need to check for it. */
958 if (breakpoint != NULL && dbg_tick != NULL
959 && *dbg_tick != debug_tick)
960 {
961 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100962 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000963 fname, sourcing_lnum);
964 *dbg_tick = debug_tick;
965 }
966
967 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
968 sourcing_lnum = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
969
970 /* Did we encounter a breakpoint? */
971 if (breakpoint != NULL && *breakpoint != 0
972 && *breakpoint <= sourcing_lnum)
973 {
974 dbg_breakpoint(fname, sourcing_lnum);
975 /* Find next breakpoint. */
976 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100977 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978 fname, sourcing_lnum);
979 *dbg_tick = debug_tick;
980 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000981# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000982 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000983 {
984 if (getline_is_func)
985 func_line_start(real_cookie);
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100986 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000987 script_line_start();
988 }
989# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990 }
991
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000992 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000994 /* Inside a while/for loop we need to store the lines and use them
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100995 * again. Pass a different "fgetline" function to do_one_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996 * below, so that it stores lines in or reads them from
997 * "lines_ga". Makes it possible to define a function inside a
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000998 * while/for loop. */
999 cmd_getline = get_loop_line;
1000 cmd_cookie = (void *)&cmd_loop_cookie;
1001 cmd_loop_cookie.lines_gap = &lines_ga;
1002 cmd_loop_cookie.current_line = current_line;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001003 cmd_loop_cookie.getline = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001004 cmd_loop_cookie.cookie = cookie;
1005 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006 }
1007 else
1008 {
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001009 cmd_getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010 cmd_cookie = cookie;
1011 }
1012#endif
1013
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001014 /* 2. If no line given, get an allocated line with fgetline(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015 if (next_cmdline == NULL)
1016 {
1017 /*
1018 * Need to set msg_didout for the first line after an ":if",
1019 * otherwise the ":if" will be overwritten.
1020 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001021 if (count == 1 && getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022 msg_didout = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001023 if (fgetline == NULL || (next_cmdline = fgetline(':', cookie,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001024#ifdef FEAT_EVAL
1025 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
1026#else
1027 0
1028#endif
1029 )) == NULL)
1030 {
1031 /* Don't call wait_return for aborted command line. The NULL
1032 * returned for the end of a sourced file or executed function
1033 * doesn't do this. */
1034 if (KeyTyped && !(flags & DOCMD_REPEAT))
1035 need_wait_return = FALSE;
1036 retval = FAIL;
1037 break;
1038 }
1039 used_getline = TRUE;
1040
1041 /*
1042 * Keep the first typed line. Clear it when more lines are typed.
1043 */
1044 if (flags & DOCMD_KEEPLINE)
1045 {
1046 vim_free(repeat_cmdline);
1047 if (count == 0)
1048 repeat_cmdline = vim_strsave(next_cmdline);
1049 else
1050 repeat_cmdline = NULL;
1051 }
1052 }
1053
1054 /* 3. Make a copy of the command so we can mess with it. */
1055 else if (cmdline_copy == NULL)
1056 {
1057 next_cmdline = vim_strsave(next_cmdline);
1058 if (next_cmdline == NULL)
1059 {
1060 EMSG(_(e_outofmem));
1061 retval = FAIL;
1062 break;
1063 }
1064 }
1065 cmdline_copy = next_cmdline;
1066
1067#ifdef FEAT_EVAL
1068 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001069 * Save the current line when inside a ":while" or ":for", and when
1070 * the command looks like a ":while" or ":for", because we may need it
1071 * later. When there is a '|' and another command, it is stored
1072 * separately, because we need to be able to jump back to it from an
1073 * :endwhile/:endfor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001075 if (current_line == lines_ga.ga_len
1076 && (cstack.cs_looplevel || has_loop_cmd(next_cmdline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001078 if (store_loop_line(&lines_ga, next_cmdline) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079 {
1080 retval = FAIL;
1081 break;
1082 }
1083 }
1084 did_endif = FALSE;
1085#endif
1086
1087 if (count++ == 0)
1088 {
1089 /*
1090 * All output from the commands is put below each other, without
1091 * waiting for a return. Don't do this when executing commands
1092 * from a script or when being called recursive (e.g. for ":e
1093 * +command file").
1094 */
1095 if (!(flags & DOCMD_NOWAIT) && !recursive)
1096 {
1097 msg_didout_before_start = msg_didout;
1098 msg_didany = FALSE; /* no output yet */
1099 msg_start();
1100 msg_scroll = TRUE; /* put messages below each other */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001101 ++no_wait_return; /* don't wait for return until finished */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102 ++RedrawingDisabled;
1103 did_inc = TRUE;
1104 }
1105 }
1106
1107 if (p_verbose >= 15 && sourcing_name != NULL)
1108 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109 ++no_wait_return;
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001110 verbose_enter_scroll();
1111
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112 smsg((char_u *)_("line %ld: %s"),
1113 (long)sourcing_lnum, cmdline_copy);
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001114 if (msg_silent == 0)
1115 msg_puts((char_u *)"\n"); /* don't overwrite this */
1116
1117 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118 --no_wait_return;
1119 }
1120
1121 /*
1122 * 2. Execute one '|' separated command.
1123 * do_one_cmd() will return NULL if there is no trailing '|'.
1124 * "cmdline_copy" can change, e.g. for '%' and '#' expansion.
1125 */
1126 ++recursive;
1127 next_cmdline = do_one_cmd(&cmdline_copy, flags & DOCMD_VERBOSE,
1128#ifdef FEAT_EVAL
1129 &cstack,
1130#endif
1131 cmd_getline, cmd_cookie);
1132 --recursive;
1133
1134#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001135 if (cmd_cookie == (void *)&cmd_loop_cookie)
1136 /* Use "current_line" from "cmd_loop_cookie", it may have been
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137 * incremented when defining a function. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001138 current_line = cmd_loop_cookie.current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139#endif
1140
1141 if (next_cmdline == NULL)
1142 {
1143 vim_free(cmdline_copy);
1144 cmdline_copy = NULL;
1145#ifdef FEAT_CMDHIST
1146 /*
1147 * If the command was typed, remember it for the ':' register.
1148 * Do this AFTER executing the command to make :@: work.
1149 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001150 if (getline_equal(fgetline, cookie, getexline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001151 && new_last_cmdline != NULL)
1152 {
1153 vim_free(last_cmdline);
1154 last_cmdline = new_last_cmdline;
1155 new_last_cmdline = NULL;
1156 }
1157#endif
1158 }
1159 else
1160 {
1161 /* need to copy the command after the '|' to cmdline_copy, for the
1162 * next do_one_cmd() */
Bram Moolenaara7241f52008-06-24 20:39:31 +00001163 STRMOVE(cmdline_copy, next_cmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001164 next_cmdline = cmdline_copy;
1165 }
1166
1167
1168#ifdef FEAT_EVAL
1169 /* reset did_emsg for a function that is not aborted by an error */
1170 if (did_emsg && !force_abort
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001171 && getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172 && !func_has_abort(real_cookie))
1173 did_emsg = FALSE;
1174
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001175 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001176 {
1177 ++current_line;
1178
1179 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001180 * An ":endwhile", ":endfor" and ":continue" is handled here.
1181 * If we were executing commands, jump back to the ":while" or
1182 * ":for".
1183 * If we were not executing commands, decrement cs_looplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001185 if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001187 cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001189 /* Jump back to the matching ":while" or ":for". Be careful
1190 * not to use a cs_line[] from an entry that isn't a ":while"
1191 * or ":for": It would make "current_line" invalid and can
1192 * cause a crash. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193 if (!did_emsg && !got_int && !did_throw
1194 && cstack.cs_idx >= 0
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001195 && (cstack.cs_flags[cstack.cs_idx]
1196 & (CSF_WHILE | CSF_FOR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197 && cstack.cs_line[cstack.cs_idx] >= 0
1198 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
1199 {
1200 current_line = cstack.cs_line[cstack.cs_idx];
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001201 /* remember we jumped there */
1202 cstack.cs_lflags |= CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203 line_breakcheck(); /* check if CTRL-C typed */
1204
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001205 /* Check for the next breakpoint at or after the ":while"
1206 * or ":for". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207 if (breakpoint != NULL)
1208 {
1209 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001210 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211 fname,
1212 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
1213 *dbg_tick = debug_tick;
1214 }
1215 }
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001216 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001217 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001218 /* can only get here with ":endwhile" or ":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219 if (cstack.cs_idx >= 0)
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001220 rewind_conditionals(&cstack, cstack.cs_idx - 1,
1221 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222 }
1223 }
1224
1225 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001226 * For a ":while" or ":for" we need to remember the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001227 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001228 else if (cstack.cs_lflags & CSL_HAD_LOOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001229 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001230 cstack.cs_lflags &= ~CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001231 cstack.cs_line[cstack.cs_idx] = current_line - 1;
1232 }
1233 }
1234
1235 /*
1236 * When not inside any ":while" loop, clear remembered lines.
1237 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001238 if (cstack.cs_looplevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239 {
1240 if (lines_ga.ga_len > 0)
1241 {
1242 sourcing_lnum =
1243 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
1244 free_cmdlines(&lines_ga);
1245 }
1246 current_line = 0;
1247 }
1248
1249 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001250 * A ":finally" makes did_emsg, got_int, and did_throw pending for
1251 * being restored at the ":endtry". Reset them here and set the
1252 * ACTIVE and FINALLY flags, so that the finally clause gets executed.
1253 * This includes the case where a missing ":endif", ":endwhile" or
1254 * ":endfor" was detected by the ":finally" itself.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001256 if (cstack.cs_lflags & CSL_HAD_FINA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001257 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001258 cstack.cs_lflags &= ~CSL_HAD_FINA;
1259 report_make_pending(cstack.cs_pending[cstack.cs_idx]
1260 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261 did_throw ? (void *)current_exception : NULL);
1262 did_emsg = got_int = did_throw = FALSE;
1263 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
1264 }
1265
1266 /* Update global "trylevel" for recursive calls to do_cmdline() from
1267 * within this loop. */
1268 trylevel = initial_trylevel + cstack.cs_trylevel;
1269
1270 /*
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001271 * If the outermost try conditional (across function calls and sourced
Bram Moolenaar071d4272004-06-13 20:20:40 +00001272 * files) is aborted because of an error, an interrupt, or an uncaught
1273 * exception, cancel everything. If it is left normally, reset
1274 * force_abort to get the non-EH compatible abortion behavior for
1275 * the rest of the script.
1276 */
1277 if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
1278 force_abort = FALSE;
1279
1280 /* Convert an interrupt to an exception if appropriate. */
1281 (void)do_intthrow(&cstack);
1282#endif /* FEAT_EVAL */
1283
1284 }
1285 /*
1286 * Continue executing command lines when:
1287 * - no CTRL-C typed, no aborting error, no exception thrown or try
1288 * conditionals need to be checked for executing finally clauses or
1289 * catching an interrupt exception
1290 * - didn't get an error message or lines are not typed
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001291 * - there is a command after '|', inside a :if, :while, :for or :try, or
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292 * looping for ":source" command or function call.
1293 */
1294 while (!((got_int
1295#ifdef FEAT_EVAL
1296 || (did_emsg && force_abort) || did_throw
1297#endif
1298 )
1299#ifdef FEAT_EVAL
1300 && cstack.cs_trylevel == 0
1301#endif
1302 )
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001303 && !(did_emsg
1304#ifdef FEAT_EVAL
1305 /* Keep going when inside try/catch, so that the error can be
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001306 * deal with, except when it is a syntax error, it may cause
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001307 * the :endtry to be missed. */
1308 && (cstack.cs_trylevel == 0 || did_emsg_syntax)
1309#endif
1310 && used_getline
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001311 && (getline_equal(fgetline, cookie, getexmodeline)
1312 || getline_equal(fgetline, cookie, getexline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313 && (next_cmdline != NULL
1314#ifdef FEAT_EVAL
1315 || cstack.cs_idx >= 0
1316#endif
1317 || (flags & DOCMD_REPEAT)));
1318
1319 vim_free(cmdline_copy);
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001320 did_emsg_syntax = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321#ifdef FEAT_EVAL
1322 free_cmdlines(&lines_ga);
1323 ga_clear(&lines_ga);
1324
1325 if (cstack.cs_idx >= 0)
1326 {
1327 /*
1328 * If a sourced file or executed function ran to its end, report the
1329 * unclosed conditional.
1330 */
1331 if (!got_int && !did_throw
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001332 && ((getline_equal(fgetline, cookie, getsourceline)
1333 && !source_finished(fgetline, cookie))
1334 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335 && !func_has_ended(real_cookie))))
1336 {
1337 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
1338 EMSG(_(e_endtry));
1339 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
1340 EMSG(_(e_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001341 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
1342 EMSG(_(e_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343 else
1344 EMSG(_(e_endif));
1345 }
1346
1347 /*
1348 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1349 * ":endtry" in a sourced file or executed function. If the try
1350 * conditional is in its finally clause, ignore anything pending.
1351 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001352 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353 */
1354 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001355 {
1356 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1357
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001358 if (idx >= 0)
1359 --idx; /* remove try block not in its finally clause */
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001360 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1361 &cstack.cs_looplevel);
1362 }
1363 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364 trylevel = initial_trylevel;
1365 }
1366
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001367 /* If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1368 * lack was reported above and the error message is to be converted to an
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369 * exception, do this now after rewinding the cstack. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001370 do_errthrow(&cstack, getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371 ? (char_u *)"endfunction" : (char_u *)NULL);
1372
1373 if (trylevel == 0)
1374 {
1375 /*
1376 * When an exception is being thrown out of the outermost try
1377 * conditional, discard the uncaught exception, disable the conversion
1378 * of interrupts or errors to exceptions, and ensure that no more
1379 * commands are executed.
1380 */
1381 if (did_throw)
1382 {
1383 void *p = NULL;
1384 char_u *saved_sourcing_name;
1385 int saved_sourcing_lnum;
1386 struct msglist *messages = NULL, *next;
1387
1388 /*
1389 * If the uncaught exception is a user exception, report it as an
1390 * error. If it is an error exception, display the saved error
1391 * message now. For an interrupt exception, do nothing; the
1392 * interrupt message is given elsewhere.
1393 */
1394 switch (current_exception->type)
1395 {
1396 case ET_USER:
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001397 vim_snprintf((char *)IObuff, IOSIZE,
1398 _("E605: Exception not caught: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001399 current_exception->value);
1400 p = vim_strsave(IObuff);
1401 break;
1402 case ET_ERROR:
1403 messages = current_exception->messages;
1404 current_exception->messages = NULL;
1405 break;
1406 case ET_INTERRUPT:
1407 break;
1408 default:
1409 p = vim_strsave((char_u *)_(e_internal));
1410 }
1411
1412 saved_sourcing_name = sourcing_name;
1413 saved_sourcing_lnum = sourcing_lnum;
1414 sourcing_name = current_exception->throw_name;
1415 sourcing_lnum = current_exception->throw_lnum;
1416 current_exception->throw_name = NULL;
1417
1418 discard_current_exception(); /* uses IObuff if 'verbose' */
1419 suppress_errthrow = TRUE;
1420 force_abort = TRUE;
1421
1422 if (messages != NULL)
1423 {
1424 do
1425 {
1426 next = messages->next;
1427 emsg(messages->msg);
1428 vim_free(messages->msg);
1429 vim_free(messages);
1430 messages = next;
1431 }
1432 while (messages != NULL);
1433 }
1434 else if (p != NULL)
1435 {
1436 emsg(p);
1437 vim_free(p);
1438 }
1439 vim_free(sourcing_name);
1440 sourcing_name = saved_sourcing_name;
1441 sourcing_lnum = saved_sourcing_lnum;
1442 }
1443
1444 /*
1445 * On an interrupt or an aborting error not converted to an exception,
1446 * disable the conversion of errors to exceptions. (Interrupts are not
1447 * converted any more, here.) This enables also the interrupt message
1448 * when force_abort is set and did_emsg unset in case of an interrupt
1449 * from a finally clause after an error.
1450 */
1451 else if (got_int || (did_emsg && force_abort))
1452 suppress_errthrow = TRUE;
1453 }
1454
1455 /*
1456 * The current cstack will be freed when do_cmdline() returns. An uncaught
1457 * exception will have to be rethrown in the previous cstack. If a function
1458 * has just returned or a script file was just finished and the previous
1459 * cstack belongs to the same function or, respectively, script file, it
1460 * will have to be checked for finally clauses to be executed due to the
1461 * ":return" or ":finish". This is done in do_one_cmd().
1462 */
1463 if (did_throw)
1464 need_rethrow = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001465 if ((getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466 && ex_nesting_level > source_level(real_cookie))
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001467 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001468 && ex_nesting_level > func_level(real_cookie) + 1))
1469 {
1470 if (!did_throw)
1471 check_cstack = TRUE;
1472 }
1473 else
1474 {
1475 /* When leaving a function, reduce nesting level. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001476 if (getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001477 --ex_nesting_level;
1478 /*
1479 * Go to debug mode when returning from a function in which we are
1480 * single-stepping.
1481 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001482 if ((getline_equal(fgetline, cookie, getsourceline)
1483 || getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484 && ex_nesting_level + 1 <= debug_break_level)
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001485 do_debug(getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001486 ? (char_u *)_("End of sourced file")
1487 : (char_u *)_("End of function"));
1488 }
1489
1490 /*
1491 * Restore the exception environment (done after returning from the
1492 * debugger).
1493 */
1494 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001495 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001496
1497 msg_list = saved_msg_list;
1498#endif /* FEAT_EVAL */
1499
1500 /*
1501 * If there was too much output to fit on the command line, ask the user to
1502 * hit return before redrawing the screen. With the ":global" command we do
1503 * this only once after the command is finished.
1504 */
1505 if (did_inc)
1506 {
1507 --RedrawingDisabled;
1508 --no_wait_return;
1509 msg_scroll = FALSE;
1510
1511 /*
1512 * When just finished an ":if"-":else" which was typed, no need to
1513 * wait for hit-return. Also for an error situation.
1514 */
1515 if (retval == FAIL
1516#ifdef FEAT_EVAL
1517 || (did_endif && KeyTyped && !did_emsg)
1518#endif
1519 )
1520 {
1521 need_wait_return = FALSE;
1522 msg_didany = FALSE; /* don't wait when restarting edit */
1523 }
1524 else if (need_wait_return)
1525 {
1526 /*
1527 * The msg_start() above clears msg_didout. The wait_return we do
1528 * here should not overwrite the command that may be shown before
1529 * doing that.
1530 */
1531 msg_didout |= msg_didout_before_start;
1532 wait_return(FALSE);
1533 }
1534 }
1535
Bram Moolenaar2a942252012-11-28 23:03:07 +01001536#ifdef FEAT_EVAL
1537 did_endif = FALSE; /* in case do_cmdline used recursively */
1538#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001539 /*
1540 * Reset if_level, in case a sourced script file contains more ":if" than
1541 * ":endif" (could be ":if x | foo | endif").
1542 */
1543 if_level = 0;
Bram Moolenaar2e18a122012-11-28 19:10:54 +01001544#endif
Bram Moolenaard4ad0d42012-11-28 17:34:48 +01001545
Bram Moolenaar071d4272004-06-13 20:20:40 +00001546 --call_depth;
1547 return retval;
1548}
1549
1550#ifdef FEAT_EVAL
1551/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001552 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001553 */
1554 static char_u *
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001555get_loop_line(c, cookie, indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001556 int c;
1557 void *cookie;
1558 int indent;
1559{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001560 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561 wcmd_T *wp;
1562 char_u *line;
1563
1564 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1565 {
1566 if (cp->repeating)
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001567 return NULL; /* trying to read past ":endwhile"/":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001568
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001569 /* First time inside the ":while"/":for": get line normally. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570 if (cp->getline == NULL)
1571 line = getcmdline(c, 0L, indent);
1572 else
1573 line = cp->getline(c, cp->cookie, indent);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001574 if (line != NULL && store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575 ++cp->current_line;
1576
1577 return line;
1578 }
1579
1580 KeyTyped = FALSE;
1581 ++cp->current_line;
1582 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
1583 sourcing_lnum = wp->lnum;
1584 return vim_strsave(wp->line);
1585}
1586
1587/*
1588 * Store a line in "gap" so that a ":while" loop can execute it again.
1589 */
1590 static int
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001591store_loop_line(gap, line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592 garray_T *gap;
1593 char_u *line;
1594{
1595 if (ga_grow(gap, 1) == FAIL)
1596 return FAIL;
1597 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
1598 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = sourcing_lnum;
1599 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001600 return OK;
1601}
1602
1603/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001604 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001605 */
1606 static void
1607free_cmdlines(gap)
1608 garray_T *gap;
1609{
1610 while (gap->ga_len > 0)
1611 {
1612 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1613 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001614 }
1615}
1616#endif
1617
1618/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001619 * If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals
1620 * "func". * Otherwise return TRUE when "fgetline" equals "func".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001622 int
Bram Moolenaar89d40322006-08-29 15:30:07 +00001623getline_equal(fgetline, cookie, func)
1624 char_u *(*fgetline) __ARGS((int, void *, int));
Bram Moolenaar78a15312009-05-15 19:33:18 +00001625 void *cookie UNUSED; /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626 char_u *(*func) __ARGS((int, void *, int));
1627{
1628#ifdef FEAT_EVAL
1629 char_u *(*gp) __ARGS((int, void *, int));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001630 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631
Bram Moolenaar89d40322006-08-29 15:30:07 +00001632 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001633 * function that's originally used to obtain the lines. This may be
1634 * nested several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001635 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001636 cp = (struct loop_cookie *)cookie;
1637 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638 {
1639 gp = cp->getline;
1640 cp = cp->cookie;
1641 }
1642 return gp == func;
1643#else
Bram Moolenaar89d40322006-08-29 15:30:07 +00001644 return fgetline == func;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001645#endif
1646}
1647
1648#if defined(FEAT_EVAL) || defined(FEAT_MBYTE) || defined(PROTO)
1649/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001650 * If "fgetline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651 * getline function. Otherwise return "cookie".
1652 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653 void *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001654getline_cookie(fgetline, cookie)
Bram Moolenaar78a15312009-05-15 19:33:18 +00001655 char_u *(*fgetline) __ARGS((int, void *, int)) UNUSED;
Bram Moolenaar89d40322006-08-29 15:30:07 +00001656 void *cookie; /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001657{
1658# ifdef FEAT_EVAL
1659 char_u *(*gp) __ARGS((int, void *, int));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001660 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661
Bram Moolenaar89d40322006-08-29 15:30:07 +00001662 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001663 * cookie that's originally used to obtain the lines. This may be nested
Bram Moolenaar071d4272004-06-13 20:20:40 +00001664 * several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001665 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001666 cp = (struct loop_cookie *)cookie;
1667 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001668 {
1669 gp = cp->getline;
1670 cp = cp->cookie;
1671 }
1672 return cp;
1673# else
1674 return cookie;
1675# endif
1676}
1677#endif
1678
1679/*
1680 * Execute one Ex command.
1681 *
1682 * If 'sourcing' is TRUE, the command will be included in the error message.
1683 *
1684 * 1. skip comment lines and leading space
1685 * 2. handle command modifiers
1686 * 3. parse range
1687 * 4. parse command
1688 * 5. parse arguments
1689 * 6. switch on command name
1690 *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001691 * Note: "fgetline" can be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001692 *
1693 * This function may be called recursively!
1694 */
1695#if (_MSC_VER == 1200)
1696/*
Bram Moolenaared203462004-06-16 11:19:22 +00001697 * Avoid optimisation bug in VC++ version 6.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001698 */
Bram Moolenaar281bdce2005-01-25 21:53:18 +00001699 #pragma optimize( "g", off )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001700#endif
1701 static char_u *
1702do_one_cmd(cmdlinep, sourcing,
1703#ifdef FEAT_EVAL
1704 cstack,
1705#endif
Bram Moolenaar89d40322006-08-29 15:30:07 +00001706 fgetline, cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001707 char_u **cmdlinep;
1708 int sourcing;
1709#ifdef FEAT_EVAL
1710 struct condstack *cstack;
1711#endif
Bram Moolenaar89d40322006-08-29 15:30:07 +00001712 char_u *(*fgetline) __ARGS((int, void *, int));
1713 void *cookie; /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714{
1715 char_u *p;
1716 linenr_T lnum;
1717 long n;
1718 char_u *errormsg = NULL; /* error message */
1719 exarg_T ea; /* Ex command arguments */
1720 long verbose_save = -1;
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001721 int save_msg_scroll = msg_scroll;
1722 int save_msg_silent = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723 int did_esilent = 0;
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001724#ifdef HAVE_SANDBOX
1725 int did_sandbox = FALSE;
1726#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727 cmdmod_T save_cmdmod;
1728 int ni; /* set when Not Implemented */
1729
1730 vim_memset(&ea, 0, sizeof(ea));
1731 ea.line1 = 1;
1732 ea.line2 = 1;
1733#ifdef FEAT_EVAL
1734 ++ex_nesting_level;
1735#endif
1736
Bram Moolenaar21691f82012-12-05 19:13:18 +01001737 /* When the last file has not been edited :q has to be typed twice. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738 if (quitmore
1739#ifdef FEAT_EVAL
1740 /* avoid that a function call in 'statusline' does this */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001741 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001742#endif
1743#ifdef FEAT_AUTOCMD
Bram Moolenaar21691f82012-12-05 19:13:18 +01001744 /* avoid that an autocommand, e.g. QuitPre, does this */
1745 && !getline_equal(fgetline, cookie, getnextac)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001746#endif
1747 )
1748 --quitmore;
1749
1750 /*
1751 * Reset browse, confirm, etc.. They are restored when returning, for
1752 * recursive calls.
1753 */
1754 save_cmdmod = cmdmod;
1755 vim_memset(&cmdmod, 0, sizeof(cmdmod));
1756
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001757 /* "#!anything" is handled like a comment. */
1758 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1759 goto doend;
1760
Bram Moolenaar071d4272004-06-13 20:20:40 +00001761 /*
1762 * Repeat until no more command modifiers are found.
1763 */
1764 ea.cmd = *cmdlinep;
1765 for (;;)
1766 {
1767/*
1768 * 1. skip comment lines and leading white space and colons
1769 */
1770 while (*ea.cmd == ' ' || *ea.cmd == '\t' || *ea.cmd == ':')
1771 ++ea.cmd;
1772
1773 /* in ex mode, an empty line works like :+ */
1774 if (*ea.cmd == NUL && exmode_active
Bram Moolenaar89d40322006-08-29 15:30:07 +00001775 && (getline_equal(fgetline, cookie, getexmodeline)
1776 || getline_equal(fgetline, cookie, getexline))
Bram Moolenaardf177f62005-02-22 08:39:57 +00001777 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778 {
1779 ea.cmd = (char_u *)"+";
1780 ex_pressedreturn = TRUE;
1781 }
1782
1783 /* ignore comment and empty lines */
Bram Moolenaarf998c042007-11-20 11:31:26 +00001784 if (*ea.cmd == '"')
1785 goto doend;
1786 if (*ea.cmd == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001787 {
1788 ex_pressedreturn = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001790 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791
1792/*
1793 * 2. handle command modifiers.
1794 */
1795 p = ea.cmd;
1796 if (VIM_ISDIGIT(*ea.cmd))
1797 p = skipwhite(skipdigits(ea.cmd));
1798 switch (*p)
1799 {
1800 /* When adding an entry, also modify cmd_exists(). */
1801 case 'a': if (!checkforcmd(&ea.cmd, "aboveleft", 3))
1802 break;
1803#ifdef FEAT_WINDOWS
1804 cmdmod.split |= WSP_ABOVE;
1805#endif
1806 continue;
1807
1808 case 'b': if (checkforcmd(&ea.cmd, "belowright", 3))
1809 {
1810#ifdef FEAT_WINDOWS
1811 cmdmod.split |= WSP_BELOW;
1812#endif
1813 continue;
1814 }
1815 if (checkforcmd(&ea.cmd, "browse", 3))
1816 {
Bram Moolenaard812df62008-11-09 12:46:09 +00001817#ifdef FEAT_BROWSE_CMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818 cmdmod.browse = TRUE;
1819#endif
1820 continue;
1821 }
1822 if (!checkforcmd(&ea.cmd, "botright", 2))
1823 break;
1824#ifdef FEAT_WINDOWS
1825 cmdmod.split |= WSP_BOT;
1826#endif
1827 continue;
1828
1829 case 'c': if (!checkforcmd(&ea.cmd, "confirm", 4))
1830 break;
1831#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1832 cmdmod.confirm = TRUE;
1833#endif
1834 continue;
1835
1836 case 'k': if (checkforcmd(&ea.cmd, "keepmarks", 3))
1837 {
1838 cmdmod.keepmarks = TRUE;
1839 continue;
1840 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001841 if (checkforcmd(&ea.cmd, "keepalt", 5))
1842 {
1843 cmdmod.keepalt = TRUE;
1844 continue;
1845 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846 if (!checkforcmd(&ea.cmd, "keepjumps", 5))
1847 break;
1848 cmdmod.keepjumps = TRUE;
1849 continue;
1850
1851 /* ":hide" and ":hide | cmd" are not modifiers */
1852 case 'h': if (p != ea.cmd || !checkforcmd(&p, "hide", 3)
1853 || *p == NUL || ends_excmd(*p))
1854 break;
1855 ea.cmd = p;
1856 cmdmod.hide = TRUE;
1857 continue;
1858
1859 case 'l': if (checkforcmd(&ea.cmd, "lockmarks", 3))
1860 {
1861 cmdmod.lockmarks = TRUE;
1862 continue;
1863 }
1864
1865 if (!checkforcmd(&ea.cmd, "leftabove", 5))
1866 break;
1867#ifdef FEAT_WINDOWS
1868 cmdmod.split |= WSP_ABOVE;
1869#endif
1870 continue;
1871
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001872 case 'n': if (!checkforcmd(&ea.cmd, "noautocmd", 3))
1873 break;
1874#ifdef FEAT_AUTOCMD
1875 if (cmdmod.save_ei == NULL)
1876 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001877 /* Set 'eventignore' to "all". Restore the
1878 * existing option value later. */
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001879 cmdmod.save_ei = vim_strsave(p_ei);
1880 set_string_option_direct((char_u *)"ei", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001881 (char_u *)"all", OPT_FREE, SID_NONE);
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001882 }
1883#endif
1884 continue;
1885
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886 case 'r': if (!checkforcmd(&ea.cmd, "rightbelow", 6))
1887 break;
1888#ifdef FEAT_WINDOWS
1889 cmdmod.split |= WSP_BELOW;
1890#endif
1891 continue;
1892
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001893 case 's': if (checkforcmd(&ea.cmd, "sandbox", 3))
1894 {
1895#ifdef HAVE_SANDBOX
1896 if (!did_sandbox)
1897 ++sandbox;
1898 did_sandbox = TRUE;
1899#endif
1900 continue;
1901 }
1902 if (!checkforcmd(&ea.cmd, "silent", 3))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903 break;
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001904 if (save_msg_silent == -1)
1905 save_msg_silent = msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001906 ++msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907 if (*ea.cmd == '!' && !vim_iswhite(ea.cmd[-1]))
1908 {
1909 /* ":silent!", but not "silent !cmd" */
1910 ea.cmd = skipwhite(ea.cmd + 1);
1911 ++emsg_silent;
1912 ++did_esilent;
1913 }
1914 continue;
1915
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001916 case 't': if (checkforcmd(&p, "tab", 3))
1917 {
1918#ifdef FEAT_WINDOWS
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001919 if (vim_isdigit(*ea.cmd))
1920 cmdmod.tab = atoi((char *)ea.cmd) + 1;
1921 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001922 cmdmod.tab = tabpage_index(curtab) + 1;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001923 ea.cmd = p;
1924#endif
1925 continue;
1926 }
1927 if (!checkforcmd(&ea.cmd, "topleft", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001928 break;
1929#ifdef FEAT_WINDOWS
1930 cmdmod.split |= WSP_TOP;
1931#endif
1932 continue;
1933
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001934 case 'u': if (!checkforcmd(&ea.cmd, "unsilent", 3))
1935 break;
1936 if (save_msg_silent == -1)
1937 save_msg_silent = msg_silent;
1938 msg_silent = 0;
1939 continue;
1940
Bram Moolenaar071d4272004-06-13 20:20:40 +00001941 case 'v': if (checkforcmd(&ea.cmd, "vertical", 4))
1942 {
1943#ifdef FEAT_VERTSPLIT
1944 cmdmod.split |= WSP_VERT;
1945#endif
1946 continue;
1947 }
1948 if (!checkforcmd(&p, "verbose", 4))
1949 break;
1950 if (verbose_save < 0)
1951 verbose_save = p_verbose;
Bram Moolenaared203462004-06-16 11:19:22 +00001952 if (vim_isdigit(*ea.cmd))
1953 p_verbose = atoi((char *)ea.cmd);
1954 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955 p_verbose = 1;
1956 ea.cmd = p;
1957 continue;
1958 }
1959 break;
1960 }
1961
1962#ifdef FEAT_EVAL
1963 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
1964 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
1965#else
1966 ea.skip = (if_level > 0);
1967#endif
1968
1969#ifdef FEAT_EVAL
Bram Moolenaar05159a02005-02-26 23:04:13 +00001970# ifdef FEAT_PROFILE
1971 /* Count this line for profiling if ea.skip is FALSE. */
Bram Moolenaar371d5402006-03-20 21:47:49 +00001972 if (do_profiling == PROF_YES && !ea.skip)
Bram Moolenaar05159a02005-02-26 23:04:13 +00001973 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001974 if (getline_equal(fgetline, cookie, get_func_line))
1975 func_line_exec(getline_cookie(fgetline, cookie));
1976 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +00001977 script_line_exec();
1978 }
1979#endif
1980
Bram Moolenaar071d4272004-06-13 20:20:40 +00001981 /* May go to debug mode. If this happens and the ">quit" debug command is
1982 * used, throw an interrupt exception and skip the next command. */
1983 dbg_check_breakpoint(&ea);
1984 if (!ea.skip && got_int)
1985 {
1986 ea.skip = TRUE;
1987 (void)do_intthrow(cstack);
1988 }
1989#endif
1990
1991/*
1992 * 3. parse a range specifier of the form: addr [,addr] [;addr] ..
1993 *
1994 * where 'addr' is:
1995 *
1996 * % (entire file)
1997 * $ [+-NUM]
1998 * 'x [+-NUM] (where x denotes a currently defined mark)
1999 * . [+-NUM]
2000 * [+-NUM]..
2001 * NUM
2002 *
2003 * The ea.cmd pointer is updated to point to the first character following the
2004 * range spec. If an initial address is found, but no second, the upper bound
2005 * is equal to the lower.
2006 */
2007
2008 /* repeat for all ',' or ';' separated addresses */
2009 for (;;)
2010 {
2011 ea.line1 = ea.line2;
2012 ea.line2 = curwin->w_cursor.lnum; /* default is current line number */
2013 ea.cmd = skipwhite(ea.cmd);
2014 lnum = get_address(&ea.cmd, ea.skip, ea.addr_count == 0);
2015 if (ea.cmd == NULL) /* error detected */
2016 goto doend;
2017 if (lnum == MAXLNUM)
2018 {
2019 if (*ea.cmd == '%') /* '%' - all lines */
2020 {
2021 ++ea.cmd;
2022 ea.line1 = 1;
2023 ea.line2 = curbuf->b_ml.ml_line_count;
2024 ++ea.addr_count;
2025 }
2026 /* '*' - visual area */
2027 else if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
2028 {
2029 pos_T *fp;
2030
2031 ++ea.cmd;
2032 if (!ea.skip)
2033 {
2034 fp = getmark('<', FALSE);
2035 if (check_mark(fp) == FAIL)
2036 goto doend;
2037 ea.line1 = fp->lnum;
2038 fp = getmark('>', FALSE);
2039 if (check_mark(fp) == FAIL)
2040 goto doend;
2041 ea.line2 = fp->lnum;
2042 ++ea.addr_count;
2043 }
2044 }
2045 }
2046 else
2047 ea.line2 = lnum;
2048 ea.addr_count++;
2049
2050 if (*ea.cmd == ';')
2051 {
2052 if (!ea.skip)
2053 curwin->w_cursor.lnum = ea.line2;
2054 }
2055 else if (*ea.cmd != ',')
2056 break;
2057 ++ea.cmd;
2058 }
2059
2060 /* One address given: set start and end lines */
2061 if (ea.addr_count == 1)
2062 {
2063 ea.line1 = ea.line2;
2064 /* ... but only implicit: really no address given */
2065 if (lnum == MAXLNUM)
2066 ea.addr_count = 0;
2067 }
2068
2069 /* Don't leave the cursor on an illegal line (caused by ';') */
2070 check_cursor_lnum();
2071
2072/*
2073 * 4. parse command
2074 */
2075
2076 /*
2077 * Skip ':' and any white space
2078 */
2079 ea.cmd = skipwhite(ea.cmd);
2080 while (*ea.cmd == ':')
2081 ea.cmd = skipwhite(ea.cmd + 1);
2082
2083 /*
2084 * If we got a line, but no command, then go to the line.
2085 * If we find a '|' or '\n' we set ea.nextcmd.
2086 */
2087 if (*ea.cmd == NUL || *ea.cmd == '"' ||
2088 (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
2089 {
2090 /*
2091 * strange vi behaviour:
2092 * ":3" jumps to line 3
2093 * ":3|..." prints line 3
2094 * ":|" prints current line
2095 */
2096 if (ea.skip) /* skip this if inside :if */
2097 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00002098 if (*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002099 {
2100 ea.cmdidx = CMD_print;
2101 ea.argt = RANGE+COUNT+TRLBAR;
2102 if ((errormsg = invalid_range(&ea)) == NULL)
2103 {
2104 correct_range(&ea);
2105 ex_print(&ea);
2106 }
2107 }
2108 else if (ea.addr_count != 0)
2109 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002110 if (ea.line2 > curbuf->b_ml.ml_line_count)
2111 {
2112 /* With '-' in 'cpoptions' a line number past the file is an
2113 * error, otherwise put it at the end of the file. */
2114 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
2115 ea.line2 = -1;
2116 else
2117 ea.line2 = curbuf->b_ml.ml_line_count;
2118 }
2119
2120 if (ea.line2 < 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002121 errormsg = (char_u *)_(e_invrange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002122 else
2123 {
2124 if (ea.line2 == 0)
2125 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002126 else
2127 curwin->w_cursor.lnum = ea.line2;
2128 beginline(BL_SOL | BL_FIX);
2129 }
2130 }
2131 goto doend;
2132 }
2133
2134 /* Find the command and let "p" point to after it. */
2135 p = find_command(&ea, NULL);
2136
2137#ifdef FEAT_USR_CMDS
2138 if (p == NULL)
2139 {
2140 if (!ea.skip)
2141 errormsg = (char_u *)_("E464: Ambiguous use of user-defined command");
2142 goto doend;
2143 }
2144 /* Check for wrong commands. */
2145 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78)
2146 {
2147 errormsg = uc_fun_cmd();
2148 goto doend;
2149 }
2150#endif
2151 if (ea.cmdidx == CMD_SIZE)
2152 {
2153 if (!ea.skip)
2154 {
2155 STRCPY(IObuff, _("E492: Not an editor command"));
2156 if (!sourcing)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002157 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158 errormsg = IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02002159 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160 }
2161 goto doend;
2162 }
2163
2164 ni = (
2165#ifdef FEAT_USR_CMDS
2166 !USER_CMDIDX(ea.cmdidx) &&
2167#endif
Bram Moolenaar3ebc1e52007-07-05 07:54:17 +00002168 (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00002169#ifdef HAVE_EX_SCRIPT_NI
2170 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
2171#endif
2172 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002173
2174#ifndef FEAT_EVAL
2175 /*
2176 * When the expression evaluation is disabled, recognize the ":if" and
2177 * ":endif" commands and ignore everything in between it.
2178 */
2179 if (ea.cmdidx == CMD_if)
2180 ++if_level;
2181 if (if_level)
2182 {
2183 if (ea.cmdidx == CMD_endif)
2184 --if_level;
2185 goto doend;
2186 }
2187
2188#endif
2189
Bram Moolenaar196b3b02008-06-20 16:51:41 +00002190 /* forced commands */
2191 if (*p == '!' && ea.cmdidx != CMD_substitute
2192 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002193 {
2194 ++p;
2195 ea.forceit = TRUE;
2196 }
2197 else
2198 ea.forceit = FALSE;
2199
2200/*
2201 * 5. parse arguments
2202 */
2203#ifdef FEAT_USR_CMDS
2204 if (!USER_CMDIDX(ea.cmdidx))
2205#endif
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002206 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002207
2208 if (!ea.skip)
2209 {
2210#ifdef HAVE_SANDBOX
2211 if (sandbox != 0 && !(ea.argt & SBOXOK))
2212 {
2213 /* Command not allowed in sandbox. */
2214 errormsg = (char_u *)_(e_sandbox);
2215 goto doend;
2216 }
2217#endif
2218 if (!curbuf->b_p_ma && (ea.argt & MODIFY))
2219 {
2220 /* Command not allowed in non-'modifiable' buffer */
2221 errormsg = (char_u *)_(e_modifiable);
2222 goto doend;
2223 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002224
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002225 if (text_locked() && !(ea.argt & CMDWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002226# ifdef FEAT_USR_CMDS
2227 && !USER_CMDIDX(ea.cmdidx)
2228# endif
2229 )
2230 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002231 /* Command not allowed when editing the command line. */
2232#ifdef FEAT_CMDWIN
2233 if (cmdwin_type != 0)
2234 errormsg = (char_u *)_(e_cmdwin);
2235 else
2236#endif
2237 errormsg = (char_u *)_(e_secure);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002238 goto doend;
2239 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002240#ifdef FEAT_AUTOCMD
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002241 /* Disallow editing another buffer when "curbuf_lock" is set.
2242 * Do allow ":edit" (check for argument later).
2243 * Do allow ":checktime" (it's postponed). */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002244 if (!(ea.argt & CMDWIN)
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002245 && ea.cmdidx != CMD_edit
2246 && ea.cmdidx != CMD_checktime
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002247# ifdef FEAT_USR_CMDS
2248 && !USER_CMDIDX(ea.cmdidx)
2249# endif
2250 && curbuf_locked())
2251 goto doend;
2252#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002253
2254 if (!ni && !(ea.argt & RANGE) && ea.addr_count > 0)
2255 {
2256 /* no range allowed */
2257 errormsg = (char_u *)_(e_norange);
2258 goto doend;
2259 }
2260 }
2261
2262 if (!ni && !(ea.argt & BANG) && ea.forceit) /* no <!> allowed */
2263 {
2264 errormsg = (char_u *)_(e_nobang);
2265 goto doend;
2266 }
2267
2268 /*
2269 * Don't complain about the range if it is not used
2270 * (could happen if line_count is accidentally set to 0).
2271 */
2272 if (!ea.skip && !ni)
2273 {
2274 /*
2275 * If the range is backwards, ask for confirmation and, if given, swap
2276 * ea.line1 & ea.line2 so it's forwards again.
2277 * When global command is busy, don't ask, will fail below.
2278 */
2279 if (!global_busy && ea.line1 > ea.line2)
2280 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002281 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002283 if (sourcing || exmode_active)
2284 {
2285 errormsg = (char_u *)_("E493: Backwards range given");
2286 goto doend;
2287 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288 if (ask_yesno((char_u *)
2289 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002290 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291 }
2292 lnum = ea.line1;
2293 ea.line1 = ea.line2;
2294 ea.line2 = lnum;
2295 }
2296 if ((errormsg = invalid_range(&ea)) != NULL)
2297 goto doend;
2298 }
2299
2300 if ((ea.argt & NOTADR) && ea.addr_count == 0) /* default is 1, not cursor */
2301 ea.line2 = 1;
2302
2303 correct_range(&ea);
2304
2305#ifdef FEAT_FOLDING
2306 if (((ea.argt & WHOLEFOLD) || ea.addr_count >= 2) && !global_busy)
2307 {
2308 /* Put the first line at the start of a closed fold, put the last line
2309 * at the end of a closed fold. */
2310 (void)hasFolding(ea.line1, &ea.line1, NULL);
2311 (void)hasFolding(ea.line2, NULL, &ea.line2);
2312 }
2313#endif
2314
2315#ifdef FEAT_QUICKFIX
2316 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002317 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318 * option here, so things like % get expanded.
2319 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002320 p = replace_makeprg(&ea, p, cmdlinep);
2321 if (p == NULL)
2322 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323#endif
2324
2325 /*
2326 * Skip to start of argument.
2327 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2328 */
2329 if (ea.cmdidx == CMD_bang)
2330 ea.arg = p;
2331 else
2332 ea.arg = skipwhite(p);
2333
2334 /*
2335 * Check for "++opt=val" argument.
2336 * Must be first, allow ":w ++enc=utf8 !cmd"
2337 */
2338 if (ea.argt & ARGOPT)
2339 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2340 if (getargopt(&ea) == FAIL && !ni)
2341 {
2342 errormsg = (char_u *)_(e_invarg);
2343 goto doend;
2344 }
2345
2346 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2347 {
2348 if (*ea.arg == '>') /* append */
2349 {
2350 if (*++ea.arg != '>') /* typed wrong */
2351 {
2352 errormsg = (char_u *)_("E494: Use w or w>>");
2353 goto doend;
2354 }
2355 ea.arg = skipwhite(ea.arg + 1);
2356 ea.append = TRUE;
2357 }
2358 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
2359 {
2360 ++ea.arg;
2361 ea.usefilter = TRUE;
2362 }
2363 }
2364
2365 if (ea.cmdidx == CMD_read)
2366 {
2367 if (ea.forceit)
2368 {
2369 ea.usefilter = TRUE; /* :r! filter if ea.forceit */
2370 ea.forceit = FALSE;
2371 }
2372 else if (*ea.arg == '!') /* :r !filter */
2373 {
2374 ++ea.arg;
2375 ea.usefilter = TRUE;
2376 }
2377 }
2378
2379 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2380 {
2381 ea.amount = 1;
2382 while (*ea.arg == *ea.cmd) /* count number of '>' or '<' */
2383 {
2384 ++ea.arg;
2385 ++ea.amount;
2386 }
2387 ea.arg = skipwhite(ea.arg);
2388 }
2389
2390 /*
2391 * Check for "+command" argument, before checking for next command.
2392 * Don't do this for ":read !cmd" and ":write !cmd".
2393 */
2394 if ((ea.argt & EDITCMD) && !ea.usefilter)
2395 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2396
2397 /*
2398 * Check for '|' to separate commands and '"' to start comments.
2399 * Don't do this for ":read !cmd" and ":write !cmd".
2400 */
2401 if ((ea.argt & TRLBAR) && !ea.usefilter)
2402 separate_nextcmd(&ea);
2403
2404 /*
2405 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002406 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2407 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002409 else if (ea.cmdidx == CMD_bang
2410 || ea.cmdidx == CMD_global
2411 || ea.cmdidx == CMD_vglobal
2412 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413 {
2414 for (p = ea.arg; *p; ++p)
2415 {
2416 /* Remove one backslash before a newline, so that it's possible to
2417 * pass a newline to the shell and also a newline that is preceded
2418 * with a backslash. This makes it impossible to end a shell
2419 * command in a backslash, but that doesn't appear useful.
2420 * Halving the number of backslashes is incompatible with previous
2421 * versions. */
2422 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002423 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424 else if (*p == '\n')
2425 {
2426 ea.nextcmd = p + 1;
2427 *p = NUL;
2428 break;
2429 }
2430 }
2431 }
2432
2433 if ((ea.argt & DFLALL) && ea.addr_count == 0)
2434 {
2435 ea.line1 = 1;
2436 ea.line2 = curbuf->b_ml.ml_line_count;
2437 }
2438
2439 /* accept numbered register only when no count allowed (:put) */
2440 if ( (ea.argt & REGSTR)
2441 && *ea.arg != NUL
2442#ifdef FEAT_USR_CMDS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443 /* Do not allow register = for user commands */
2444 && (!USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002445#endif
2446 && !((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)))
2447 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002448#ifndef FEAT_CLIPBOARD
2449 /* check these explicitly for a more specific error message */
2450 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002452 errormsg = (char_u *)_(e_invalidreg);
2453 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454 }
2455#endif
Bram Moolenaar85de2062011-05-05 14:26:41 +02002456 if (
2457#ifdef FEAT_USR_CMDS
2458 valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2459 && USER_CMDIDX(ea.cmdidx)))
2460#else
2461 valid_yank_reg(*ea.arg, ea.cmdidx != CMD_put)
2462#endif
2463 )
2464 {
2465 ea.regname = *ea.arg++;
2466#ifdef FEAT_EVAL
2467 /* for '=' register: accept the rest of the line as an expression */
2468 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2469 {
2470 set_expr_line(vim_strsave(ea.arg));
2471 ea.arg += STRLEN(ea.arg);
2472 }
2473#endif
2474 ea.arg = skipwhite(ea.arg);
2475 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002476 }
2477
2478 /*
2479 * Check for a count. When accepting a BUFNAME, don't use "123foo" as a
2480 * count, it's a buffer name.
2481 */
2482 if ((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)
2483 && (!(ea.argt & BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
2484 || vim_iswhite(*p)))
2485 {
2486 n = getdigits(&ea.arg);
2487 ea.arg = skipwhite(ea.arg);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002488 if (n <= 0 && !ni && (ea.argt & ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002489 {
2490 errormsg = (char_u *)_(e_zerocount);
2491 goto doend;
2492 }
2493 if (ea.argt & NOTADR) /* e.g. :buffer 2, :sleep 3 */
2494 {
2495 ea.line2 = n;
2496 if (ea.addr_count == 0)
2497 ea.addr_count = 1;
2498 }
2499 else
2500 {
2501 ea.line1 = ea.line2;
2502 ea.line2 += n - 1;
2503 ++ea.addr_count;
2504 /*
2505 * Be vi compatible: no error message for out of range.
2506 */
2507 if (ea.line2 > curbuf->b_ml.ml_line_count)
2508 ea.line2 = curbuf->b_ml.ml_line_count;
2509 }
2510 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002511
2512 /*
2513 * Check for flags: 'l', 'p' and '#'.
2514 */
2515 if (ea.argt & EXFLAGS)
2516 get_flags(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002517 /* no arguments allowed */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002518 if (!ni && !(ea.argt & EXTRA) && *ea.arg != NUL
Bram Moolenaara2031822006-03-07 22:29:51 +00002519 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520 {
2521 errormsg = (char_u *)_(e_trailing);
2522 goto doend;
2523 }
2524
2525 if (!ni && (ea.argt & NEEDARG) && *ea.arg == NUL)
2526 {
2527 errormsg = (char_u *)_(e_argreq);
2528 goto doend;
2529 }
2530
2531#ifdef FEAT_EVAL
2532 /*
2533 * Skip the command when it's not going to be executed.
2534 * The commands like :if, :endif, etc. always need to be executed.
2535 * Also make an exception for commands that handle a trailing command
2536 * themselves.
2537 */
2538 if (ea.skip)
2539 {
2540 switch (ea.cmdidx)
2541 {
2542 /* commands that need evaluation */
2543 case CMD_while:
2544 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002545 case CMD_for:
2546 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547 case CMD_if:
2548 case CMD_elseif:
2549 case CMD_else:
2550 case CMD_endif:
2551 case CMD_try:
2552 case CMD_catch:
2553 case CMD_finally:
2554 case CMD_endtry:
2555 case CMD_function:
2556 break;
2557
2558 /* Commands that handle '|' themselves. Check: A command should
2559 * either have the TRLBAR flag, appear in this list or appear in
2560 * the list at ":help :bar". */
2561 case CMD_aboveleft:
2562 case CMD_and:
2563 case CMD_belowright:
2564 case CMD_botright:
2565 case CMD_browse:
2566 case CMD_call:
2567 case CMD_confirm:
2568 case CMD_delfunction:
2569 case CMD_djump:
2570 case CMD_dlist:
2571 case CMD_dsearch:
2572 case CMD_dsplit:
2573 case CMD_echo:
2574 case CMD_echoerr:
2575 case CMD_echomsg:
2576 case CMD_echon:
2577 case CMD_execute:
2578 case CMD_help:
2579 case CMD_hide:
2580 case CMD_ijump:
2581 case CMD_ilist:
2582 case CMD_isearch:
2583 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002584 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002585 case CMD_keepjumps:
2586 case CMD_keepmarks:
2587 case CMD_leftabove:
2588 case CMD_let:
2589 case CMD_lockmarks:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002590 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002591 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002592 case CMD_mzscheme:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593 case CMD_perl:
2594 case CMD_psearch:
2595 case CMD_python:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002596 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002597 case CMD_python3:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002598 case CMD_return:
2599 case CMD_rightbelow:
2600 case CMD_ruby:
2601 case CMD_silent:
2602 case CMD_smagic:
2603 case CMD_snomagic:
2604 case CMD_substitute:
2605 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002606 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002607 case CMD_tcl:
2608 case CMD_throw:
2609 case CMD_tilde:
2610 case CMD_topleft:
2611 case CMD_unlet:
2612 case CMD_verbose:
2613 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002614 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002615 break;
2616
2617 default: goto doend;
2618 }
2619 }
2620#endif
2621
2622 if (ea.argt & XFILE)
2623 {
2624 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2625 goto doend;
2626 }
2627
2628#ifdef FEAT_LISTCMDS
2629 /*
2630 * Accept buffer name. Cannot be used at the same time with a buffer
2631 * number. Don't do this for a user command.
2632 */
2633 if ((ea.argt & BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
2634# ifdef FEAT_USR_CMDS
2635 && !USER_CMDIDX(ea.cmdidx)
2636# endif
2637 )
2638 {
2639 /*
2640 * :bdelete, :bwipeout and :bunload take several arguments, separated
2641 * by spaces: find next space (skipping over escaped characters).
2642 * The others take one argument: ignore trailing spaces.
2643 */
2644 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2645 || ea.cmdidx == CMD_bunload)
2646 p = skiptowhite_esc(ea.arg);
2647 else
2648 {
2649 p = ea.arg + STRLEN(ea.arg);
2650 while (p > ea.arg && vim_iswhite(p[-1]))
2651 --p;
2652 }
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002653 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & BUFUNL) != 0,
2654 FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655 if (ea.line2 < 0) /* failed */
2656 goto doend;
2657 ea.addr_count = 1;
2658 ea.arg = skipwhite(p);
2659 }
2660#endif
2661
2662/*
2663 * 6. switch on command name
2664 *
2665 * The "ea" structure holds the arguments that can be used.
2666 */
2667 ea.cmdlinep = cmdlinep;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002668 ea.getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002669 ea.cookie = cookie;
2670#ifdef FEAT_EVAL
2671 ea.cstack = cstack;
2672#endif
2673
2674#ifdef FEAT_USR_CMDS
2675 if (USER_CMDIDX(ea.cmdidx))
2676 {
2677 /*
2678 * Execute a user-defined command.
2679 */
2680 do_ucmd(&ea);
2681 }
2682 else
2683#endif
2684 {
2685 /*
2686 * Call the function to execute the command.
2687 */
2688 ea.errmsg = NULL;
2689 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2690 if (ea.errmsg != NULL)
2691 errormsg = (char_u *)_(ea.errmsg);
2692 }
2693
2694#ifdef FEAT_EVAL
2695 /*
2696 * If the command just executed called do_cmdline(), any throw or ":return"
2697 * or ":finish" encountered there must also check the cstack of the still
2698 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2699 * exception, or reanimate a returned function or finished script file and
2700 * return or finish it again.
2701 */
2702 if (need_rethrow)
2703 do_throw(cstack);
2704 else if (check_cstack)
2705 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002706 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002708 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709 && current_func_returned())
2710 do_return(&ea, TRUE, FALSE, NULL);
2711 }
2712 need_rethrow = check_cstack = FALSE;
2713#endif
2714
2715doend:
2716 if (curwin->w_cursor.lnum == 0) /* can happen with zero line number */
2717 curwin->w_cursor.lnum = 1;
2718
2719 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2720 {
2721 if (sourcing)
2722 {
2723 if (errormsg != IObuff)
2724 {
2725 STRCPY(IObuff, errormsg);
2726 errormsg = IObuff;
2727 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002728 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002729 }
2730 emsg(errormsg);
2731 }
2732#ifdef FEAT_EVAL
2733 do_errthrow(cstack,
2734 (ea.cmdidx != CMD_SIZE
2735# ifdef FEAT_USR_CMDS
2736 && !USER_CMDIDX(ea.cmdidx)
2737# endif
2738 ) ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
2739#endif
2740
2741 if (verbose_save >= 0)
2742 p_verbose = verbose_save;
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00002743#ifdef FEAT_AUTOCMD
2744 if (cmdmod.save_ei != NULL)
2745 {
2746 /* Restore 'eventignore' to the value before ":noautocmd". */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002747 set_string_option_direct((char_u *)"ei", -1, cmdmod.save_ei,
2748 OPT_FREE, SID_NONE);
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00002749 free_string_option(cmdmod.save_ei);
2750 }
2751#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002752
2753 cmdmod = save_cmdmod;
2754
Bram Moolenaar8e258a42009-07-09 13:55:43 +00002755 if (save_msg_silent != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002756 {
2757 /* messages could be enabled for a serious error, need to check if the
2758 * counters don't become negative */
Bram Moolenaarbecf4282009-09-30 11:24:36 +00002759 if (!did_emsg || msg_silent > save_msg_silent)
Bram Moolenaar8e258a42009-07-09 13:55:43 +00002760 msg_silent = save_msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002761 emsg_silent -= did_esilent;
2762 if (emsg_silent < 0)
2763 emsg_silent = 0;
2764 /* Restore msg_scroll, it's set by file I/O commands, even when no
2765 * message is actually displayed. */
2766 msg_scroll = save_msg_scroll;
Bram Moolenaar77ab2802009-04-22 12:44:48 +00002767
2768 /* "silent reg" or "silent echo x" inside "redir" leaves msg_col
2769 * somewhere in the line. Put it back in the first column. */
2770 if (redirecting())
2771 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002772 }
2773
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002774#ifdef HAVE_SANDBOX
2775 if (did_sandbox)
2776 --sandbox;
2777#endif
2778
Bram Moolenaar071d4272004-06-13 20:20:40 +00002779 if (ea.nextcmd && *ea.nextcmd == NUL) /* not really a next command */
2780 ea.nextcmd = NULL;
2781
2782#ifdef FEAT_EVAL
2783 --ex_nesting_level;
2784#endif
2785
2786 return ea.nextcmd;
2787}
2788#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00002789 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002790#endif
2791
2792/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002793 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002794 * If there is a match advance "pp" to the argument and return TRUE.
2795 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002796 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00002797checkforcmd(pp, cmd, len)
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002798 char_u **pp; /* start of command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799 char *cmd; /* name of command */
2800 int len; /* required length */
2801{
2802 int i;
2803
2804 for (i = 0; cmd[i] != NUL; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00002805 if (((char_u *)cmd)[i] != (*pp)[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00002806 break;
2807 if (i >= len && !isalpha((*pp)[i]))
2808 {
2809 *pp = skipwhite(*pp + i);
2810 return TRUE;
2811 }
2812 return FALSE;
2813}
2814
2815/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002816 * Append "cmd" to the error message in IObuff.
2817 * Takes care of limiting the length and handling 0xa0, which would be
2818 * invisible otherwise.
2819 */
2820 static void
2821append_command(cmd)
2822 char_u *cmd;
2823{
2824 char_u *s = cmd;
2825 char_u *d;
2826
2827 STRCAT(IObuff, ": ");
2828 d = IObuff + STRLEN(IObuff);
2829 while (*s != NUL && d - IObuff < IOSIZE - 7)
2830 {
2831 if (
2832#ifdef FEAT_MBYTE
2833 enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) :
2834#endif
2835 *s == 0xa0)
2836 {
2837 s +=
2838#ifdef FEAT_MBYTE
2839 enc_utf8 ? 2 :
2840#endif
2841 1;
2842 STRCPY(d, "<a0>");
2843 d += 4;
2844 }
2845 else
2846 MB_COPY_CHAR(s, d);
2847 }
2848 *d = NUL;
2849}
2850
2851/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002853 * Start of the name can be found at eap->cmd.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002854 * Returns pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002855 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002856 * Returns NULL for an ambiguous user command.
2857 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858 static char_u *
2859find_command(eap, full)
2860 exarg_T *eap;
Bram Moolenaar78a15312009-05-15 19:33:18 +00002861 int *full UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002862{
2863 int len;
2864 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00002865 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002866
2867 /*
2868 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00002869 * Exceptions:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002870 * - the 'k' command can directly be followed by any character.
2871 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
2872 * but :sre[wind] is another command, as are :scrip[tnames],
2873 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00002874 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002875 */
2876 p = eap->cmd;
2877 if (*p == 'k')
2878 {
2879 eap->cmdidx = CMD_k;
2880 ++p;
2881 }
2882 else if (p[0] == 's'
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002883 && ((p[1] == 'c' && p[2] != 's' && p[2] != 'r'
2884 && p[3] != 'i' && p[4] != 'p')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002885 || p[1] == 'g'
2886 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
2887 || p[1] == 'I'
2888 || (p[1] == 'r' && p[2] != 'e')))
2889 {
2890 eap->cmdidx = CMD_substitute;
2891 ++p;
2892 }
2893 else
2894 {
2895 while (ASCII_ISALPHA(*p))
2896 ++p;
Bram Moolenaarb6590522010-07-21 16:00:43 +02002897 /* for python 3.x support ":py3", ":python3", ":py3file", etc. */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002898 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaarb6590522010-07-21 16:00:43 +02002899 while (ASCII_ISALNUM(*p))
2900 ++p;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02002901
Bram Moolenaar071d4272004-06-13 20:20:40 +00002902 /* check for non-alpha command */
2903 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
2904 ++p;
2905 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00002906 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
2907 {
2908 /* Check for ":dl", ":dell", etc. to ":deletel": that's
2909 * :delete with the 'l' flag. Same for 'p'. */
2910 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00002911 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00002912 break;
2913 if (i == len - 1)
2914 {
2915 --len;
2916 if (p[-1] == 'l')
2917 eap->flags |= EXFLAG_LIST;
2918 else
2919 eap->flags |= EXFLAG_PRINT;
2920 }
2921 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002922
2923 if (ASCII_ISLOWER(*eap->cmd))
2924 eap->cmdidx = cmdidxs[CharOrdLow(*eap->cmd)];
2925 else
2926 eap->cmdidx = cmdidxs[26];
2927
2928 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
2929 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
2930 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
2931 (size_t)len) == 0)
2932 {
2933#ifdef FEAT_EVAL
2934 if (full != NULL
2935 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
2936 *full = TRUE;
2937#endif
2938 break;
2939 }
2940
2941#ifdef FEAT_USR_CMDS
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01002942 /* Look for a user defined command as a last resort. Let ":Print" be
2943 * overruled by a user defined command. */
2944 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
2945 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002946 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002947 /* User defined commands may contain digits. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002948 while (ASCII_ISALNUM(*p))
2949 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002950 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002951 }
2952#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002953 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002954 eap->cmdidx = CMD_SIZE;
2955 }
2956
2957 return p;
2958}
2959
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002960#ifdef FEAT_USR_CMDS
2961/*
2962 * Search for a user command that matches "eap->cmd".
2963 * Return cmdidx in "eap->cmdidx", flags in "eap->argt", idx in "eap->useridx".
2964 * Return a pointer to just after the command.
2965 * Return NULL if there is no matching command.
2966 */
2967 static char_u *
2968find_ucmd(eap, p, full, xp, compl)
2969 exarg_T *eap;
2970 char_u *p; /* end of the command (possibly including count) */
2971 int *full; /* set to TRUE for a full match */
2972 expand_T *xp; /* used for completion, NULL otherwise */
2973 int *compl; /* completion flags or NULL */
2974{
2975 int len = (int)(p - eap->cmd);
2976 int j, k, matchlen = 0;
2977 ucmd_T *uc;
2978 int found = FALSE;
2979 int possible = FALSE;
2980 char_u *cp, *np; /* Point into typed cmd and test name */
2981 garray_T *gap;
2982 int amb_local = FALSE; /* Found ambiguous buffer-local command,
2983 only full match global is accepted. */
2984
2985 /*
2986 * Look for buffer-local user commands first, then global ones.
2987 */
2988 gap = &curbuf->b_ucmds;
2989 for (;;)
2990 {
2991 for (j = 0; j < gap->ga_len; ++j)
2992 {
2993 uc = USER_CMD_GA(gap, j);
2994 cp = eap->cmd;
2995 np = uc->uc_name;
2996 k = 0;
2997 while (k < len && *np != NUL && *cp++ == *np++)
2998 k++;
2999 if (k == len || (*np == NUL && vim_isdigit(eap->cmd[k])))
3000 {
3001 /* If finding a second match, the command is ambiguous. But
3002 * not if a buffer-local command wasn't a full match and a
3003 * global command is a full match. */
3004 if (k == len && found && *np != NUL)
3005 {
3006 if (gap == &ucmds)
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003007 return NULL;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003008 amb_local = TRUE;
3009 }
3010
3011 if (!found || (k == len && *np == NUL))
3012 {
3013 /* If we matched up to a digit, then there could
3014 * be another command including the digit that we
3015 * should use instead.
3016 */
3017 if (k == len)
3018 found = TRUE;
3019 else
3020 possible = TRUE;
3021
3022 if (gap == &ucmds)
3023 eap->cmdidx = CMD_USER;
3024 else
3025 eap->cmdidx = CMD_USER_BUF;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003026 eap->argt = (long)uc->uc_argt;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003027 eap->useridx = j;
3028
3029# ifdef FEAT_CMDL_COMPL
3030 if (compl != NULL)
3031 *compl = uc->uc_compl;
3032# ifdef FEAT_EVAL
3033 if (xp != NULL)
3034 {
3035 xp->xp_arg = uc->uc_compl_arg;
3036 xp->xp_scriptID = uc->uc_scriptID;
3037 }
3038# endif
3039# endif
3040 /* Do not search for further abbreviations
3041 * if this is an exact match. */
3042 matchlen = k;
3043 if (k == len && *np == NUL)
3044 {
3045 if (full != NULL)
3046 *full = TRUE;
3047 amb_local = FALSE;
3048 break;
3049 }
3050 }
3051 }
3052 }
3053
3054 /* Stop if we found a full match or searched all. */
3055 if (j < gap->ga_len || gap == &ucmds)
3056 break;
3057 gap = &ucmds;
3058 }
3059
3060 /* Only found ambiguous matches. */
3061 if (amb_local)
3062 {
3063 if (xp != NULL)
3064 xp->xp_context = EXPAND_UNSUCCESSFUL;
3065 return NULL;
3066 }
3067
3068 /* The match we found may be followed immediately by a number. Move "p"
3069 * back to point to it. */
3070 if (found || possible)
3071 return p + (matchlen - len);
3072 return p;
3073}
3074#endif
3075
Bram Moolenaar071d4272004-06-13 20:20:40 +00003076#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003077static struct cmdmod
3078{
3079 char *name;
3080 int minlen;
3081 int has_count; /* :123verbose :3tab */
3082} cmdmods[] = {
3083 {"aboveleft", 3, FALSE},
3084 {"belowright", 3, FALSE},
3085 {"botright", 2, FALSE},
3086 {"browse", 3, FALSE},
3087 {"confirm", 4, FALSE},
3088 {"hide", 3, FALSE},
3089 {"keepalt", 5, FALSE},
3090 {"keepjumps", 5, FALSE},
3091 {"keepmarks", 3, FALSE},
3092 {"leftabove", 5, FALSE},
3093 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003094 {"noautocmd", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003095 {"rightbelow", 6, FALSE},
3096 {"sandbox", 3, FALSE},
3097 {"silent", 3, FALSE},
3098 {"tab", 3, TRUE},
3099 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003100 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003101 {"verbose", 4, TRUE},
3102 {"vertical", 4, FALSE},
3103};
3104
3105/*
3106 * Return length of a command modifier (including optional count).
3107 * Return zero when it's not a modifier.
3108 */
3109 int
3110modifier_len(cmd)
3111 char_u *cmd;
3112{
3113 int i, j;
3114 char_u *p = cmd;
3115
3116 if (VIM_ISDIGIT(*cmd))
3117 p = skipwhite(skipdigits(cmd));
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003118 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003119 {
3120 for (j = 0; p[j] != NUL; ++j)
3121 if (p[j] != cmdmods[i].name[j])
3122 break;
Bram Moolenaar2d473ab2013-06-12 17:12:24 +02003123 if (!ASCII_ISALPHA(p[j]) && j >= cmdmods[i].minlen
Bram Moolenaared53fb92007-11-24 20:50:24 +00003124 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003125 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003126 }
3127 return 0;
3128}
3129
Bram Moolenaar071d4272004-06-13 20:20:40 +00003130/*
3131 * Return > 0 if an Ex command "name" exists.
3132 * Return 2 if there is an exact match.
3133 * Return 3 if there is an ambiguous match.
3134 */
3135 int
3136cmd_exists(name)
3137 char_u *name;
3138{
3139 exarg_T ea;
3140 int full = FALSE;
3141 int i;
3142 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003143 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003144
3145 /* Check command modifiers. */
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003146 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003147 {
3148 for (j = 0; name[j] != NUL; ++j)
3149 if (name[j] != cmdmods[i].name[j])
3150 break;
3151 if (name[j] == NUL && j >= cmdmods[i].minlen)
3152 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3153 }
3154
Bram Moolenaara9587612006-05-04 21:47:50 +00003155 /* Check built-in commands and user defined commands.
3156 * For ":2match" and ":3match" we need to skip the number. */
3157 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003158 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003159 p = find_command(&ea, &full);
3160 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003162 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3163 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003164 if (*skipwhite(p) != NUL)
3165 return 0; /* trailing garbage */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003166 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3167}
3168#endif
3169
3170/*
3171 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
3172 * we don't need/want deleted. Maybe this could be done better if we didn't
3173 * repeat all this stuff. The only problem is that they may not stay
3174 * perfectly compatible with each other, but then the command line syntax
3175 * probably won't change that much -- webb.
3176 */
3177 char_u *
3178set_one_cmd_context(xp, buff)
3179 expand_T *xp;
3180 char_u *buff; /* buffer for command string */
3181{
3182 char_u *p;
3183 char_u *cmd, *arg;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003184 int len = 0;
3185 exarg_T ea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003186#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3187 int compl = EXPAND_NOTHING;
3188#endif
3189#ifdef FEAT_CMDL_COMPL
3190 int delim;
3191#endif
3192 int forceit = FALSE;
3193 int usefilter = FALSE; /* filter instead of file name */
3194
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003195 ExpandInit(xp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003196 xp->xp_pattern = buff;
3197 xp->xp_context = EXPAND_COMMANDS; /* Default until we get past command */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003198 ea.argt = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003199
3200/*
3201 * 2. skip comment lines and leading space, colons or bars
3202 */
3203 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
3204 ;
3205 xp->xp_pattern = cmd;
3206
3207 if (*cmd == NUL)
3208 return NULL;
3209 if (*cmd == '"') /* ignore comment lines */
3210 {
3211 xp->xp_context = EXPAND_NOTHING;
3212 return NULL;
3213 }
3214
3215/*
3216 * 3. parse a range specifier of the form: addr [,addr] [;addr] ..
3217 */
3218 cmd = skip_range(cmd, &xp->xp_context);
3219
3220/*
3221 * 4. parse command
3222 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223 xp->xp_pattern = cmd;
3224 if (*cmd == NUL)
3225 return NULL;
3226 if (*cmd == '"')
3227 {
3228 xp->xp_context = EXPAND_NOTHING;
3229 return NULL;
3230 }
3231
3232 if (*cmd == '|' || *cmd == '\n')
3233 return cmd + 1; /* There's another command */
3234
3235 /*
3236 * Isolate the command and search for it in the command table.
3237 * Exceptions:
3238 * - the 'k' command can directly be followed by any character, but
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003239 * do accept "keepmarks", "keepalt" and "keepjumps".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003240 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
3241 */
3242 if (*cmd == 'k' && cmd[1] != 'e')
3243 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003244 ea.cmdidx = CMD_k;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003245 p = cmd + 1;
3246 }
3247 else
3248 {
3249 p = cmd;
3250 while (ASCII_ISALPHA(*p) || *p == '*') /* Allow * wild card */
3251 ++p;
3252 /* check for non-alpha command */
3253 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3254 ++p;
Bram Moolenaar9f5d6002013-06-02 19:22:13 +02003255 /* for python 3.x: ":py3*" commands completion */
3256 if (cmd[0] == 'p' && cmd[1] == 'y' && p == cmd + 2 && *p == '3')
3257 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003258 len = (int)(p - cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003259
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003260 if (len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003261 {
3262 xp->xp_context = EXPAND_UNSUCCESSFUL;
3263 return NULL;
3264 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003265 for (ea.cmdidx = (cmdidx_T)0; (int)ea.cmdidx < (int)CMD_SIZE;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01003266 ea.cmdidx = (cmdidx_T)((int)ea.cmdidx + 1))
3267 if (STRNCMP(cmdnames[(int)ea.cmdidx].cmd_name, cmd,
3268 (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003269 break;
3270
3271#ifdef FEAT_USR_CMDS
3272 if (cmd[0] >= 'A' && cmd[0] <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273 while (ASCII_ISALNUM(*p) || *p == '*') /* Allow * wild card */
3274 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275#endif
3276 }
3277
3278 /*
3279 * If the cursor is touching the command, and it ends in an alpha-numeric
3280 * character, complete the command name.
3281 */
3282 if (*p == NUL && ASCII_ISALNUM(p[-1]))
3283 return NULL;
3284
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003285 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003286 {
3287 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
3288 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003289 ea.cmdidx = CMD_substitute;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290 p = cmd + 1;
3291 }
3292#ifdef FEAT_USR_CMDS
3293 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
3294 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003295 ea.cmd = cmd;
3296 p = find_ucmd(&ea, p, NULL, xp,
3297# if defined(FEAT_CMDL_COMPL)
3298 &compl
3299# else
3300 NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003301# endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003302 );
Bram Moolenaarebefac62005-12-28 22:39:57 +00003303 if (p == NULL)
3304 ea.cmdidx = CMD_SIZE; /* ambiguous user command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305 }
3306#endif
3307 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003308 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309 {
3310 /* Not still touching the command and it was an illegal one */
3311 xp->xp_context = EXPAND_UNSUCCESSFUL;
3312 return NULL;
3313 }
3314
3315 xp->xp_context = EXPAND_NOTHING; /* Default now that we're past command */
3316
3317 if (*p == '!') /* forced commands */
3318 {
3319 forceit = TRUE;
3320 ++p;
3321 }
3322
3323/*
3324 * 5. parse arguments
3325 */
3326#ifdef FEAT_USR_CMDS
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003327 if (!USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328#endif
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003329 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003330
3331 arg = skipwhite(p);
3332
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003333 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334 {
3335 if (*arg == '>') /* append */
3336 {
3337 if (*++arg == '>')
3338 ++arg;
3339 arg = skipwhite(arg);
3340 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003341 else if (*arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342 {
3343 ++arg;
3344 usefilter = TRUE;
3345 }
3346 }
3347
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003348 if (ea.cmdidx == CMD_read)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349 {
3350 usefilter = forceit; /* :r! filter if forced */
3351 if (*arg == '!') /* :r !filter */
3352 {
3353 ++arg;
3354 usefilter = TRUE;
3355 }
3356 }
3357
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003358 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359 {
3360 while (*arg == *cmd) /* allow any number of '>' or '<' */
3361 ++arg;
3362 arg = skipwhite(arg);
3363 }
3364
3365 /* Does command allow "+command"? */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003366 if ((ea.argt & EDITCMD) && !usefilter && *arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367 {
3368 /* Check if we're in the +command */
3369 p = arg + 1;
3370 arg = skip_cmd_arg(arg, FALSE);
3371
3372 /* Still touching the command after '+'? */
3373 if (*arg == NUL)
3374 return p;
3375
3376 /* Skip space(s) after +command to get to the real argument */
3377 arg = skipwhite(arg);
3378 }
3379
3380 /*
3381 * Check for '|' to separate commands and '"' to start comments.
3382 * Don't do this for ":read !cmd" and ":write !cmd".
3383 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003384 if ((ea.argt & TRLBAR) && !usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385 {
3386 p = arg;
3387 /* ":redir @" is not the start of a comment */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003388 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389 p += 2;
3390 while (*p)
3391 {
3392 if (*p == Ctrl_V)
3393 {
3394 if (p[1] != NUL)
3395 ++p;
3396 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003397 else if ( (*p == '"' && !(ea.argt & NOTRLCOM))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398 || *p == '|' || *p == '\n')
3399 {
3400 if (*(p - 1) != '\\')
3401 {
3402 if (*p == '|' || *p == '\n')
3403 return p + 1;
3404 return NULL; /* It's a comment */
3405 }
3406 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003407 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408 }
3409 }
3410
3411 /* no arguments allowed */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003412 if (!(ea.argt & EXTRA) && *arg != NUL &&
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413 vim_strchr((char_u *)"|\"", *arg) == NULL)
3414 return NULL;
3415
3416 /* Find start of last argument (argument just before cursor): */
Bram Moolenaar848f8762012-07-25 17:22:23 +02003417 p = buff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418 xp->xp_pattern = p;
Bram Moolenaar09168a72012-08-02 21:24:42 +02003419 len = (int)STRLEN(buff);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003420 while (*p && p < buff + len)
3421 {
3422 if (*p == ' ' || *p == TAB)
3423 {
3424 /* argument starts after a space */
3425 xp->xp_pattern = ++p;
3426 }
3427 else
3428 {
3429 if (*p == '\\' && *(p + 1) != NUL)
3430 ++p; /* skip over escaped character */
3431 mb_ptr_adv(p);
3432 }
3433 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003435 if (ea.argt & XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003437 int c;
3438 int in_quote = FALSE;
3439 char_u *bow = NULL; /* Beginning of word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440
3441 /*
3442 * Allow spaces within back-quotes to count as part of the argument
3443 * being expanded.
3444 */
3445 xp->xp_pattern = skipwhite(arg);
Bram Moolenaar6529c102007-08-18 15:47:34 +00003446 p = xp->xp_pattern;
3447 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003449#ifdef FEAT_MBYTE
3450 if (has_mbyte)
3451 c = mb_ptr2char(p);
3452 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453#endif
Bram Moolenaar6529c102007-08-18 15:47:34 +00003454 c = *p;
3455 if (c == '\\' && p[1] != NUL)
3456 ++p;
3457 else if (c == '`')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458 {
3459 if (!in_quote)
3460 {
3461 xp->xp_pattern = p;
3462 bow = p + 1;
3463 }
3464 in_quote = !in_quote;
3465 }
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003466 /* An argument can contain just about everything, except
3467 * characters that end the command and white space. */
3468 else if (c == '|' || c == '\n' || c == '"' || (vim_iswhite(c)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003469#ifdef SPACE_IN_FILENAME
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003470 && (!(ea.argt & NOSPC) || usefilter)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003471#endif
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003472 ))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003473 {
Bram Moolenaarcf5a5b82008-02-26 20:30:12 +00003474 len = 0; /* avoid getting stuck when space is in 'isfname' */
Bram Moolenaar6529c102007-08-18 15:47:34 +00003475 while (*p != NUL)
3476 {
3477#ifdef FEAT_MBYTE
3478 if (has_mbyte)
3479 c = mb_ptr2char(p);
3480 else
3481#endif
3482 c = *p;
Bram Moolenaardd87969c2007-08-21 13:07:12 +00003483 if (c == '`' || vim_isfilec_or_wc(c))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003484 break;
3485#ifdef FEAT_MBYTE
3486 if (has_mbyte)
3487 len = (*mb_ptr2len)(p);
3488 else
3489#endif
3490 len = 1;
3491 mb_ptr_adv(p);
3492 }
3493 if (in_quote)
3494 bow = p;
3495 else
3496 xp->xp_pattern = p;
3497 p -= len;
3498 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003499 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500 }
3501
3502 /*
3503 * If we are still inside the quotes, and we passed a space, just
3504 * expand from there.
3505 */
3506 if (bow != NULL && in_quote)
3507 xp->xp_pattern = bow;
3508 xp->xp_context = EXPAND_FILES;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003509
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003510 /* For a shell command more chars need to be escaped. */
3511 if (usefilter || ea.cmdidx == CMD_bang)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003512 {
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003513#ifndef BACKSLASH_IN_FILENAME
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003514 xp->xp_shell = TRUE;
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003515#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003516 /* When still after the command name expand executables. */
3517 if (xp->xp_pattern == skipwhite(arg))
Bram Moolenaar5c5b0942007-05-06 12:07:59 +00003518 xp->xp_context = EXPAND_SHELLCMD;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003519 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520
3521 /* Check for environment variable */
3522 if (*xp->xp_pattern == '$'
3523#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
3524 || *xp->xp_pattern == '%'
3525#endif
3526 )
3527 {
3528 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
3529 if (!vim_isIDc(*p))
3530 break;
3531 if (*p == NUL)
3532 {
3533 xp->xp_context = EXPAND_ENV_VARS;
3534 ++xp->xp_pattern;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003535#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3536 /* Avoid that the assignment uses EXPAND_FILES again. */
Bram Moolenaara466c992005-07-09 21:03:22 +00003537 if (compl != EXPAND_USER_DEFINED && compl != EXPAND_USER_LIST)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003538 compl = EXPAND_ENV_VARS;
3539#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540 }
3541 }
Bram Moolenaar24305862012-08-15 14:05:05 +02003542#if defined(FEAT_CMDL_COMPL)
3543 /* Check for user names */
3544 if (*xp->xp_pattern == '~')
3545 {
3546 for (p = xp->xp_pattern + 1; *p != NUL && *p != '/'; ++p)
3547 ;
3548 /* Complete ~user only if it partially matches a user name.
3549 * A full match ~user<Tab> will be replaced by user's home
3550 * directory i.e. something like ~user<Tab> -> /home/user/ */
3551 if (*p == NUL && p > xp->xp_pattern + 1
3552 && match_user(xp->xp_pattern + 1) == 1)
3553 {
3554 xp->xp_context = EXPAND_USER;
3555 ++xp->xp_pattern;
3556 }
3557 }
3558#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559 }
3560
3561/*
3562 * 6. switch on command name
3563 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003564 switch (ea.cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003566 case CMD_find:
3567 case CMD_sfind:
3568 case CMD_tabfind:
Bram Moolenaarc24b6972010-08-16 22:34:29 +02003569 if (xp->xp_context == EXPAND_FILES)
3570 xp->xp_context = EXPAND_FILES_IN_PATH;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003571 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003572 case CMD_cd:
3573 case CMD_chdir:
3574 case CMD_lcd:
3575 case CMD_lchdir:
3576 if (xp->xp_context == EXPAND_FILES)
3577 xp->xp_context = EXPAND_DIRECTORIES;
3578 break;
3579 case CMD_help:
3580 xp->xp_context = EXPAND_HELP;
3581 xp->xp_pattern = arg;
3582 break;
3583
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003584 /* Command modifiers: return the argument.
3585 * Also for commands with an argument that is a command. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586 case CMD_aboveleft:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003587 case CMD_argdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588 case CMD_belowright:
3589 case CMD_botright:
3590 case CMD_browse:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003591 case CMD_bufdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003592 case CMD_confirm:
Bram Moolenaardf177f62005-02-22 08:39:57 +00003593 case CMD_debug:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594 case CMD_folddoclosed:
3595 case CMD_folddoopen:
3596 case CMD_hide:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003597 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598 case CMD_keepjumps:
3599 case CMD_keepmarks:
3600 case CMD_leftabove:
3601 case CMD_lockmarks:
3602 case CMD_rightbelow:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003603 case CMD_sandbox:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604 case CMD_silent:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003605 case CMD_tab:
Bram Moolenaar70baa402013-06-16 16:14:03 +02003606 case CMD_tabdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607 case CMD_topleft:
3608 case CMD_verbose:
3609 case CMD_vertical:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003610 case CMD_windo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611 return arg;
3612
Bram Moolenaar4f688582007-07-24 12:34:30 +00003613#ifdef FEAT_CMDL_COMPL
3614# ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar071d4272004-06-13 20:20:40 +00003615 case CMD_match:
3616 if (*arg == NUL || !ends_excmd(*arg))
3617 {
Bram Moolenaar4f688582007-07-24 12:34:30 +00003618 /* also complete "None" */
3619 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003620 arg = skipwhite(skiptowhite(arg));
3621 if (*arg != NUL)
3622 {
3623 xp->xp_context = EXPAND_NOTHING;
3624 arg = skip_regexp(arg + 1, *arg, p_magic, NULL);
3625 }
3626 }
3627 return find_nextcmd(arg);
Bram Moolenaar4f688582007-07-24 12:34:30 +00003628# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630/*
3631 * All completion for the +cmdline_compl feature goes here.
3632 */
3633
3634# ifdef FEAT_USR_CMDS
3635 case CMD_command:
3636 /* Check for attributes */
3637 while (*arg == '-')
3638 {
3639 arg++; /* Skip "-" */
3640 p = skiptowhite(arg);
3641 if (*p == NUL)
3642 {
3643 /* Cursor is still in the attribute */
3644 p = vim_strchr(arg, '=');
3645 if (p == NULL)
3646 {
3647 /* No "=", so complete attribute names */
3648 xp->xp_context = EXPAND_USER_CMD_FLAGS;
3649 xp->xp_pattern = arg;
3650 return NULL;
3651 }
3652
3653 /* For the -complete and -nargs attributes, we complete
3654 * their arguments as well.
3655 */
3656 if (STRNICMP(arg, "complete", p - arg) == 0)
3657 {
3658 xp->xp_context = EXPAND_USER_COMPLETE;
3659 xp->xp_pattern = p + 1;
3660 return NULL;
3661 }
3662 else if (STRNICMP(arg, "nargs", p - arg) == 0)
3663 {
3664 xp->xp_context = EXPAND_USER_NARGS;
3665 xp->xp_pattern = p + 1;
3666 return NULL;
3667 }
3668 return NULL;
3669 }
3670 arg = skipwhite(p);
3671 }
3672
3673 /* After the attributes comes the new command name */
3674 p = skiptowhite(arg);
3675 if (*p == NUL)
3676 {
3677 xp->xp_context = EXPAND_USER_COMMANDS;
3678 xp->xp_pattern = arg;
3679 break;
3680 }
3681
3682 /* And finally comes a normal command */
3683 return skipwhite(p);
3684
3685 case CMD_delcommand:
3686 xp->xp_context = EXPAND_USER_COMMANDS;
3687 xp->xp_pattern = arg;
3688 break;
3689# endif
3690
3691 case CMD_global:
3692 case CMD_vglobal:
3693 delim = *arg; /* get the delimiter */
3694 if (delim)
3695 ++arg; /* skip delimiter if there is one */
3696
3697 while (arg[0] != NUL && arg[0] != delim)
3698 {
3699 if (arg[0] == '\\' && arg[1] != NUL)
3700 ++arg;
3701 ++arg;
3702 }
3703 if (arg[0] != NUL)
3704 return arg + 1;
3705 break;
3706 case CMD_and:
3707 case CMD_substitute:
3708 delim = *arg;
3709 if (delim)
3710 {
3711 /* skip "from" part */
3712 ++arg;
3713 arg = skip_regexp(arg, delim, p_magic, NULL);
3714 }
3715 /* skip "to" part */
3716 while (arg[0] != NUL && arg[0] != delim)
3717 {
3718 if (arg[0] == '\\' && arg[1] != NUL)
3719 ++arg;
3720 ++arg;
3721 }
3722 if (arg[0] != NUL) /* skip delimiter */
3723 ++arg;
3724 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
3725 ++arg;
3726 if (arg[0] != NUL)
3727 return arg;
3728 break;
3729 case CMD_isearch:
3730 case CMD_dsearch:
3731 case CMD_ilist:
3732 case CMD_dlist:
3733 case CMD_ijump:
3734 case CMD_psearch:
3735 case CMD_djump:
3736 case CMD_isplit:
3737 case CMD_dsplit:
3738 arg = skipwhite(skipdigits(arg)); /* skip count */
3739 if (*arg == '/') /* Match regexp, not just whole words */
3740 {
3741 for (++arg; *arg && *arg != '/'; arg++)
3742 if (*arg == '\\' && arg[1] != NUL)
3743 arg++;
3744 if (*arg)
3745 {
3746 arg = skipwhite(arg + 1);
3747
3748 /* Check for trailing illegal characters */
3749 if (*arg && vim_strchr((char_u *)"|\"\n", *arg) == NULL)
3750 xp->xp_context = EXPAND_NOTHING;
3751 else
3752 return arg;
3753 }
3754 }
3755 break;
3756#ifdef FEAT_AUTOCMD
3757 case CMD_autocmd:
3758 return set_context_in_autocmd(xp, arg, FALSE);
3759
3760 case CMD_doautocmd:
Bram Moolenaar73a9d7b2008-11-06 16:16:44 +00003761 case CMD_doautoall:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003762 return set_context_in_autocmd(xp, arg, TRUE);
3763#endif
3764 case CMD_set:
3765 set_context_in_set_cmd(xp, arg, 0);
3766 break;
3767 case CMD_setglobal:
3768 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
3769 break;
3770 case CMD_setlocal:
3771 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
3772 break;
3773 case CMD_tag:
3774 case CMD_stag:
3775 case CMD_ptag:
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00003776 case CMD_ltag:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003777 case CMD_tselect:
3778 case CMD_stselect:
3779 case CMD_ptselect:
3780 case CMD_tjump:
3781 case CMD_stjump:
3782 case CMD_ptjump:
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003783 if (*p_wop != NUL)
3784 xp->xp_context = EXPAND_TAGS_LISTFILES;
3785 else
3786 xp->xp_context = EXPAND_TAGS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003787 xp->xp_pattern = arg;
3788 break;
3789 case CMD_augroup:
3790 xp->xp_context = EXPAND_AUGROUP;
3791 xp->xp_pattern = arg;
3792 break;
3793#ifdef FEAT_SYN_HL
3794 case CMD_syntax:
3795 set_context_in_syntax_cmd(xp, arg);
3796 break;
3797#endif
3798#ifdef FEAT_EVAL
3799 case CMD_let:
3800 case CMD_if:
3801 case CMD_elseif:
3802 case CMD_while:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00003803 case CMD_for:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804 case CMD_echo:
3805 case CMD_echon:
3806 case CMD_execute:
3807 case CMD_echomsg:
3808 case CMD_echoerr:
3809 case CMD_call:
3810 case CMD_return:
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003811 set_context_for_expression(xp, arg, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003812 break;
3813
3814 case CMD_unlet:
3815 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3816 arg = xp->xp_pattern + 1;
3817 xp->xp_context = EXPAND_USER_VARS;
3818 xp->xp_pattern = arg;
3819 break;
3820
3821 case CMD_function:
3822 case CMD_delfunction:
3823 xp->xp_context = EXPAND_USER_FUNC;
3824 xp->xp_pattern = arg;
3825 break;
3826
3827 case CMD_echohl:
Bram Moolenaar4f688582007-07-24 12:34:30 +00003828 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003829 break;
3830#endif
3831 case CMD_highlight:
3832 set_context_in_highlight_cmd(xp, arg);
3833 break;
Bram Moolenaarf4580d82009-03-18 11:52:53 +00003834#ifdef FEAT_CSCOPE
3835 case CMD_cscope:
Bram Moolenaar7bfef802009-04-22 14:25:01 +00003836 case CMD_lcscope:
3837 case CMD_scscope:
3838 set_context_in_cscope_cmd(xp, arg, ea.cmdidx);
Bram Moolenaarf4580d82009-03-18 11:52:53 +00003839 break;
3840#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00003841#ifdef FEAT_SIGNS
3842 case CMD_sign:
3843 set_context_in_sign_cmd(xp, arg);
3844 break;
3845#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003846#ifdef FEAT_LISTCMDS
3847 case CMD_bdelete:
3848 case CMD_bwipeout:
3849 case CMD_bunload:
3850 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3851 arg = xp->xp_pattern + 1;
3852 /*FALLTHROUGH*/
3853 case CMD_buffer:
3854 case CMD_sbuffer:
3855 case CMD_checktime:
3856 xp->xp_context = EXPAND_BUFFERS;
3857 xp->xp_pattern = arg;
3858 break;
3859#endif
3860#ifdef FEAT_USR_CMDS
3861 case CMD_USER:
3862 case CMD_USER_BUF:
3863 if (compl != EXPAND_NOTHING)
3864 {
3865 /* XFILE: file names are handled above */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003866 if (!(ea.argt & XFILE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867 {
3868# ifdef FEAT_MENU
3869 if (compl == EXPAND_MENUS)
3870 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
3871# endif
3872 if (compl == EXPAND_COMMANDS)
3873 return arg;
3874 if (compl == EXPAND_MAPPINGS)
3875 return set_context_in_map_cmd(xp, (char_u *)"map",
3876 arg, forceit, FALSE, FALSE, CMD_map);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003877 /* Find start of last argument. */
3878 p = arg;
3879 while (*p)
3880 {
3881 if (*p == ' ')
Bram Moolenaar848f8762012-07-25 17:22:23 +02003882 /* argument starts after a space */
3883 arg = p + 1;
Bram Moolenaara07c8312012-07-27 21:12:07 +02003884 else if (*p == '\\' && *(p + 1) != NUL)
3885 ++p; /* skip over escaped character */
3886 mb_ptr_adv(p);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003887 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888 xp->xp_pattern = arg;
3889 }
3890 xp->xp_context = compl;
3891 }
3892 break;
3893#endif
3894 case CMD_map: case CMD_noremap:
3895 case CMD_nmap: case CMD_nnoremap:
3896 case CMD_vmap: case CMD_vnoremap:
3897 case CMD_omap: case CMD_onoremap:
3898 case CMD_imap: case CMD_inoremap:
3899 case CMD_cmap: case CMD_cnoremap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003900 case CMD_lmap: case CMD_lnoremap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02003901 case CMD_smap: case CMD_snoremap:
3902 case CMD_xmap: case CMD_xnoremap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003903 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003904 FALSE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003905 case CMD_unmap:
3906 case CMD_nunmap:
3907 case CMD_vunmap:
3908 case CMD_ounmap:
3909 case CMD_iunmap:
3910 case CMD_cunmap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003911 case CMD_lunmap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02003912 case CMD_sunmap:
3913 case CMD_xunmap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003914 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003915 FALSE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003916 case CMD_abbreviate: case CMD_noreabbrev:
3917 case CMD_cabbrev: case CMD_cnoreabbrev:
3918 case CMD_iabbrev: case CMD_inoreabbrev:
3919 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003920 TRUE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003921 case CMD_unabbreviate:
3922 case CMD_cunabbrev:
3923 case CMD_iunabbrev:
3924 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003925 TRUE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926#ifdef FEAT_MENU
3927 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
3928 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
3929 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
3930 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
3931 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
3932 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
3933 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
3934 case CMD_tmenu: case CMD_tunmenu:
3935 case CMD_popup: case CMD_tearoff: case CMD_emenu:
3936 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
3937#endif
3938
3939 case CMD_colorscheme:
3940 xp->xp_context = EXPAND_COLORS;
3941 xp->xp_pattern = arg;
3942 break;
3943
3944 case CMD_compiler:
3945 xp->xp_context = EXPAND_COMPILER;
3946 xp->xp_pattern = arg;
3947 break;
3948
Bram Moolenaar883f5d02010-06-21 06:24:34 +02003949 case CMD_ownsyntax:
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02003950 xp->xp_context = EXPAND_OWNSYNTAX;
3951 xp->xp_pattern = arg;
3952 break;
3953
3954 case CMD_setfiletype:
Bram Moolenaar883f5d02010-06-21 06:24:34 +02003955 xp->xp_context = EXPAND_FILETYPE;
3956 xp->xp_pattern = arg;
3957 break;
3958
Bram Moolenaar071d4272004-06-13 20:20:40 +00003959#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
3960 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
3961 case CMD_language:
Bram Moolenaara660dc82011-05-25 12:51:22 +02003962 p = skiptowhite(arg);
3963 if (*p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003964 {
3965 xp->xp_context = EXPAND_LANGUAGE;
3966 xp->xp_pattern = arg;
3967 }
3968 else
Bram Moolenaara660dc82011-05-25 12:51:22 +02003969 {
3970 if ( STRNCMP(arg, "messages", p - arg) == 0
3971 || STRNCMP(arg, "ctype", p - arg) == 0
3972 || STRNCMP(arg, "time", p - arg) == 0)
3973 {
3974 xp->xp_context = EXPAND_LOCALES;
3975 xp->xp_pattern = skipwhite(p);
3976 }
3977 else
3978 xp->xp_context = EXPAND_NOTHING;
3979 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003980 break;
3981#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01003982#if defined(FEAT_PROFILE)
3983 case CMD_profile:
3984 set_context_in_profile_cmd(xp, arg);
3985 break;
3986#endif
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01003987 case CMD_behave:
3988 xp->xp_context = EXPAND_BEHAVE;
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02003989 xp->xp_pattern = arg;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01003990 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02003992#if defined(FEAT_CMDHIST)
3993 case CMD_history:
3994 xp->xp_context = EXPAND_HISTORY;
3995 xp->xp_pattern = arg;
3996 break;
3997#endif
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02003998#if defined(FEAT_PROFILE)
3999 case CMD_syntime:
4000 xp->xp_context = EXPAND_SYNTIME;
4001 xp->xp_pattern = arg;
4002 break;
4003#endif
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004004
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005#endif /* FEAT_CMDL_COMPL */
4006
4007 default:
4008 break;
4009 }
4010 return NULL;
4011}
4012
4013/*
4014 * skip a range specifier of the form: addr [,addr] [;addr] ..
4015 *
4016 * Backslashed delimiters after / or ? will be skipped, and commands will
4017 * not be expanded between /'s and ?'s or after "'".
4018 *
Bram Moolenaardf177f62005-02-22 08:39:57 +00004019 * Also skip white space and ":" characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004020 * Returns the "cmd" pointer advanced to beyond the range.
4021 */
4022 char_u *
4023skip_range(cmd, ctx)
4024 char_u *cmd;
4025 int *ctx; /* pointer to xp_context or NULL */
4026{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004027 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028
Bram Moolenaardf177f62005-02-22 08:39:57 +00004029 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004030 {
4031 if (*cmd == '\'')
4032 {
4033 if (*++cmd == NUL && ctx != NULL)
4034 *ctx = EXPAND_NOTHING;
4035 }
4036 else if (*cmd == '/' || *cmd == '?')
4037 {
4038 delim = *cmd++;
4039 while (*cmd != NUL && *cmd != delim)
4040 if (*cmd++ == '\\' && *cmd != NUL)
4041 ++cmd;
4042 if (*cmd == NUL && ctx != NULL)
4043 *ctx = EXPAND_NOTHING;
4044 }
4045 if (*cmd != NUL)
4046 ++cmd;
4047 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004048
4049 /* Skip ":" and white space. */
4050 while (*cmd == ':')
4051 cmd = skipwhite(cmd + 1);
4052
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053 return cmd;
4054}
4055
4056/*
4057 * get a single EX address
4058 *
4059 * Set ptr to the next character after the part that was interpreted.
4060 * Set ptr to NULL when an error is encountered.
4061 *
4062 * Return MAXLNUM when no Ex address was found.
4063 */
4064 static linenr_T
4065get_address(ptr, skip, to_other_file)
4066 char_u **ptr;
4067 int skip; /* only skip the address, don't use it */
4068 int to_other_file; /* flag: may jump to other file */
4069{
4070 int c;
4071 int i;
4072 long n;
4073 char_u *cmd;
4074 pos_T pos;
4075 pos_T *fp;
4076 linenr_T lnum;
4077
4078 cmd = skipwhite(*ptr);
4079 lnum = MAXLNUM;
4080 do
4081 {
4082 switch (*cmd)
4083 {
4084 case '.': /* '.' - Cursor position */
4085 ++cmd;
4086 lnum = curwin->w_cursor.lnum;
4087 break;
4088
4089 case '$': /* '$' - last line */
4090 ++cmd;
4091 lnum = curbuf->b_ml.ml_line_count;
4092 break;
4093
4094 case '\'': /* ''' - mark */
4095 if (*++cmd == NUL)
4096 {
4097 cmd = NULL;
4098 goto error;
4099 }
4100 if (skip)
4101 ++cmd;
4102 else
4103 {
4104 /* Only accept a mark in another file when it is
4105 * used by itself: ":'M". */
4106 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
4107 ++cmd;
4108 if (fp == (pos_T *)-1)
4109 /* Jumped to another file. */
4110 lnum = curwin->w_cursor.lnum;
4111 else
4112 {
4113 if (check_mark(fp) == FAIL)
4114 {
4115 cmd = NULL;
4116 goto error;
4117 }
4118 lnum = fp->lnum;
4119 }
4120 }
4121 break;
4122
4123 case '/':
4124 case '?': /* '/' or '?' - search */
4125 c = *cmd++;
4126 if (skip) /* skip "/pat/" */
4127 {
4128 cmd = skip_regexp(cmd, c, (int)p_magic, NULL);
4129 if (*cmd == c)
4130 ++cmd;
4131 }
4132 else
4133 {
4134 pos = curwin->w_cursor; /* save curwin->w_cursor */
4135 /*
4136 * When '/' or '?' follows another address, start
4137 * from there.
4138 */
4139 if (lnum != MAXLNUM)
4140 curwin->w_cursor.lnum = lnum;
4141 /*
4142 * Start a forward search at the end of the line.
4143 * Start a backward search at the start of the line.
4144 * This makes sure we never match in the current
4145 * line, and can match anywhere in the
4146 * next/previous line.
4147 */
4148 if (c == '/')
4149 curwin->w_cursor.col = MAXCOL;
4150 else
4151 curwin->w_cursor.col = 0;
4152 searchcmdlen = 0;
4153 if (!do_search(NULL, c, cmd, 1L,
Bram Moolenaaraad86642008-03-10 20:34:59 +00004154 SEARCH_HIS | SEARCH_MSG, NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004155 {
4156 curwin->w_cursor = pos;
4157 cmd = NULL;
4158 goto error;
4159 }
4160 lnum = curwin->w_cursor.lnum;
4161 curwin->w_cursor = pos;
4162 /* adjust command string pointer */
4163 cmd += searchcmdlen;
4164 }
4165 break;
4166
4167 case '\\': /* "\?", "\/" or "\&", repeat search */
4168 ++cmd;
4169 if (*cmd == '&')
4170 i = RE_SUBST;
4171 else if (*cmd == '?' || *cmd == '/')
4172 i = RE_SEARCH;
4173 else
4174 {
4175 EMSG(_(e_backslash));
4176 cmd = NULL;
4177 goto error;
4178 }
4179
4180 if (!skip)
4181 {
4182 /*
4183 * When search follows another address, start from
4184 * there.
4185 */
4186 if (lnum != MAXLNUM)
4187 pos.lnum = lnum;
4188 else
4189 pos.lnum = curwin->w_cursor.lnum;
4190
4191 /*
4192 * Start the search just like for the above
4193 * do_search().
4194 */
4195 if (*cmd != '?')
4196 pos.col = MAXCOL;
4197 else
4198 pos.col = 0;
4199 if (searchit(curwin, curbuf, &pos,
4200 *cmd == '?' ? BACKWARD : FORWARD,
Bram Moolenaaraad86642008-03-10 20:34:59 +00004201 (char_u *)"", 1L, SEARCH_MSG,
Bram Moolenaar76929292008-01-06 19:07:36 +00004202 i, (linenr_T)0, NULL) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203 lnum = pos.lnum;
4204 else
4205 {
4206 cmd = NULL;
4207 goto error;
4208 }
4209 }
4210 ++cmd;
4211 break;
4212
4213 default:
4214 if (VIM_ISDIGIT(*cmd)) /* absolute line number */
4215 lnum = getdigits(&cmd);
4216 }
4217
4218 for (;;)
4219 {
4220 cmd = skipwhite(cmd);
4221 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4222 break;
4223
4224 if (lnum == MAXLNUM)
4225 lnum = curwin->w_cursor.lnum; /* "+1" is same as ".+1" */
4226 if (VIM_ISDIGIT(*cmd))
4227 i = '+'; /* "number" is same as "+number" */
4228 else
4229 i = *cmd++;
4230 if (!VIM_ISDIGIT(*cmd)) /* '+' is '+1', but '+0' is not '+1' */
4231 n = 1;
4232 else
4233 n = getdigits(&cmd);
4234 if (i == '-')
4235 lnum -= n;
4236 else
4237 lnum += n;
4238 }
4239 } while (*cmd == '/' || *cmd == '?');
4240
4241error:
4242 *ptr = cmd;
4243 return lnum;
4244}
4245
4246/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004247 * Get flags from an Ex command argument.
4248 */
4249 static void
4250get_flags(eap)
4251 exarg_T *eap;
4252{
4253 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4254 {
4255 if (*eap->arg == 'l')
4256 eap->flags |= EXFLAG_LIST;
4257 else if (*eap->arg == 'p')
4258 eap->flags |= EXFLAG_PRINT;
4259 else
4260 eap->flags |= EXFLAG_NR;
4261 eap->arg = skipwhite(eap->arg + 1);
4262 }
4263}
4264
4265/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004266 * Function called for command which is Not Implemented. NI!
4267 */
4268 void
4269ex_ni(eap)
4270 exarg_T *eap;
4271{
4272 if (!eap->skip)
4273 eap->errmsg = (char_u *)N_("E319: Sorry, the command is not available in this version");
4274}
4275
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004276#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277/*
4278 * Function called for script command which is Not Implemented. NI!
4279 * Skips over ":perl <<EOF" constructs.
4280 */
4281 static void
4282ex_script_ni(eap)
4283 exarg_T *eap;
4284{
4285 if (!eap->skip)
4286 ex_ni(eap);
4287 else
4288 vim_free(script_get(eap, eap->arg));
4289}
4290#endif
4291
4292/*
4293 * Check range in Ex command for validity.
4294 * Return NULL when valid, error message when invalid.
4295 */
4296 static char_u *
4297invalid_range(eap)
4298 exarg_T *eap;
4299{
4300 if ( eap->line1 < 0
4301 || eap->line2 < 0
4302 || eap->line1 > eap->line2
4303 || ((eap->argt & RANGE)
4304 && !(eap->argt & NOTADR)
4305 && eap->line2 > curbuf->b_ml.ml_line_count
4306#ifdef FEAT_DIFF
4307 + (eap->cmdidx == CMD_diffget)
4308#endif
4309 ))
4310 return (char_u *)_(e_invrange);
4311 return NULL;
4312}
4313
4314/*
4315 * Correct the range for zero line number, if required.
4316 */
4317 static void
4318correct_range(eap)
4319 exarg_T *eap;
4320{
4321 if (!(eap->argt & ZEROR)) /* zero in range not allowed */
4322 {
4323 if (eap->line1 == 0)
4324 eap->line1 = 1;
4325 if (eap->line2 == 0)
4326 eap->line2 = 1;
4327 }
4328}
4329
Bram Moolenaar748bf032005-02-02 23:04:36 +00004330#ifdef FEAT_QUICKFIX
4331static char_u *skip_grep_pat __ARGS((exarg_T *eap));
4332
4333/*
4334 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4335 * pattern. Otherwise return eap->arg.
4336 */
4337 static char_u *
4338skip_grep_pat(eap)
4339 exarg_T *eap;
4340{
4341 char_u *p = eap->arg;
4342
Bram Moolenaara37420f2006-02-04 22:37:47 +00004343 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4344 || eap->cmdidx == CMD_vimgrepadd
4345 || eap->cmdidx == CMD_lvimgrepadd
4346 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004347 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004348 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004349 if (p == NULL)
4350 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004351 }
4352 return p;
4353}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004354
4355/*
4356 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4357 * in the command line, so that things like % get expanded.
4358 */
4359 static char_u *
4360replace_makeprg(eap, p, cmdlinep)
4361 exarg_T *eap;
4362 char_u *p;
4363 char_u **cmdlinep;
4364{
4365 char_u *new_cmdline;
4366 char_u *program;
4367 char_u *pos;
4368 char_u *ptr;
4369 int len;
4370 int i;
4371
4372 /*
4373 * Don't do it when ":vimgrep" is used for ":grep".
4374 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004375 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4376 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4377 || eap->cmdidx == CMD_grepadd
4378 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004379 && !grep_internal(eap->cmdidx))
4380 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004381 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4382 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004383 {
4384 if (*curbuf->b_p_gp == NUL)
4385 program = p_gp;
4386 else
4387 program = curbuf->b_p_gp;
4388 }
4389 else
4390 {
4391 if (*curbuf->b_p_mp == NUL)
4392 program = p_mp;
4393 else
4394 program = curbuf->b_p_mp;
4395 }
4396
4397 p = skipwhite(p);
4398
4399 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4400 {
4401 /* replace $* by given arguments */
4402 i = 1;
4403 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4404 ++i;
4405 len = (int)STRLEN(p);
4406 new_cmdline = alloc((int)(STRLEN(program) + i * (len - 2) + 1));
4407 if (new_cmdline == NULL)
4408 return NULL; /* out of memory */
4409 ptr = new_cmdline;
4410 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4411 {
4412 i = (int)(pos - program);
4413 STRNCPY(ptr, program, i);
4414 STRCPY(ptr += i, p);
4415 ptr += len;
4416 program = pos + 2;
4417 }
4418 STRCPY(ptr, program);
4419 }
4420 else
4421 {
4422 new_cmdline = alloc((int)(STRLEN(program) + STRLEN(p) + 2));
4423 if (new_cmdline == NULL)
4424 return NULL; /* out of memory */
4425 STRCPY(new_cmdline, program);
4426 STRCAT(new_cmdline, " ");
4427 STRCAT(new_cmdline, p);
4428 }
4429 msg_make(p);
4430
4431 /* 'eap->cmd' is not set here, because it is not used at CMD_make */
4432 vim_free(*cmdlinep);
4433 *cmdlinep = new_cmdline;
4434 p = new_cmdline;
4435 }
4436 return p;
4437}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004438#endif
4439
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440/*
4441 * Expand file name in Ex command argument.
4442 * Return FAIL for failure, OK otherwise.
4443 */
4444 int
4445expand_filename(eap, cmdlinep, errormsgp)
4446 exarg_T *eap;
4447 char_u **cmdlinep;
4448 char_u **errormsgp;
4449{
4450 int has_wildcards; /* need to expand wildcards */
4451 char_u *repl;
4452 int srclen;
4453 char_u *p;
4454 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004455 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004456
Bram Moolenaar748bf032005-02-02 23:04:36 +00004457#ifdef FEAT_QUICKFIX
4458 /* Skip a regexp pattern for ":vimgrep[add] pat file..." */
4459 p = skip_grep_pat(eap);
4460#else
4461 p = eap->arg;
4462#endif
4463
Bram Moolenaar071d4272004-06-13 20:20:40 +00004464 /*
4465 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4466 * the file name contains a wildcard it should not cause expanding.
4467 * (it will be expanded anyway if there is a wildcard before replacing).
4468 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004469 has_wildcards = mch_has_wildcard(p);
4470 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004471 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004472#ifdef FEAT_EVAL
4473 /* Skip over `=expr`, wildcards in it are not expanded. */
4474 if (p[0] == '`' && p[1] == '=')
4475 {
4476 p += 2;
4477 (void)skip_expr(&p);
4478 if (*p == '`')
4479 ++p;
4480 continue;
4481 }
4482#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004483 /*
4484 * Quick check if this cannot be the start of a special string.
4485 * Also removes backslash before '%', '#' and '<'.
4486 */
4487 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4488 {
4489 ++p;
4490 continue;
4491 }
4492
4493 /*
4494 * Try to find a match at this position.
4495 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004496 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
4497 errormsgp, &escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004498 if (*errormsgp != NULL) /* error detected */
4499 return FAIL;
4500 if (repl == NULL) /* no match found */
4501 {
4502 p += srclen;
4503 continue;
4504 }
4505
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004506 /* Wildcards won't be expanded below, the replacement is taken
4507 * literally. But do expand "~/file", "~user/file" and "$HOME/file". */
4508 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4509 {
4510 char_u *l = repl;
4511
4512 repl = expand_env_save(repl);
4513 vim_free(l);
4514 }
4515
Bram Moolenaar63b92542007-03-27 14:57:09 +00004516 /* Need to escape white space et al. with a backslash.
4517 * Don't do this for:
4518 * - replacement that already has been escaped: "##"
4519 * - shell commands (may have to use quotes instead).
4520 * - non-unix systems when there is a single argument (spaces don't
4521 * separate arguments then).
4522 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004523 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00004524 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525 && eap->cmdidx != CMD_bang
4526 && eap->cmdidx != CMD_make
Bram Moolenaara37420f2006-02-04 22:37:47 +00004527 && eap->cmdidx != CMD_lmake
Bram Moolenaar071d4272004-06-13 20:20:40 +00004528 && eap->cmdidx != CMD_grep
Bram Moolenaara37420f2006-02-04 22:37:47 +00004529 && eap->cmdidx != CMD_lgrep
Bram Moolenaar071d4272004-06-13 20:20:40 +00004530 && eap->cmdidx != CMD_grepadd
Bram Moolenaara37420f2006-02-04 22:37:47 +00004531 && eap->cmdidx != CMD_lgrepadd
Bram Moolenaar071d4272004-06-13 20:20:40 +00004532#ifndef UNIX
4533 && !(eap->argt & NOSPC)
4534#endif
4535 )
4536 {
4537 char_u *l;
4538#ifdef BACKSLASH_IN_FILENAME
4539 /* Don't escape a backslash here, because rem_backslash() doesn't
4540 * remove it later. */
4541 static char_u *nobslash = (char_u *)" \t\"|";
4542# define ESCAPE_CHARS nobslash
4543#else
4544# define ESCAPE_CHARS escape_chars
4545#endif
4546
4547 for (l = repl; *l; ++l)
4548 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
4549 {
4550 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
4551 if (l != NULL)
4552 {
4553 vim_free(repl);
4554 repl = l;
4555 }
4556 break;
4557 }
4558 }
4559
4560 /* For a shell command a '!' must be escaped. */
4561 if ((eap->usefilter || eap->cmdidx == CMD_bang)
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004562 && vim_strpbrk(repl, (char_u *)"!&;()<>") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004563 {
4564 char_u *l;
4565
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004566 l = vim_strsave_escaped(repl, (char_u *)"!&;()<>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004567 if (l != NULL)
4568 {
4569 vim_free(repl);
4570 repl = l;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004571 /* For a sh-like shell escape "!" another time. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004572 if (strstr((char *)p_sh, "sh") != NULL)
4573 {
4574 l = vim_strsave_escaped(repl, (char_u *)"!");
4575 if (l != NULL)
4576 {
4577 vim_free(repl);
4578 repl = l;
4579 }
4580 }
4581 }
4582 }
4583
4584 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
4585 vim_free(repl);
4586 if (p == NULL)
4587 return FAIL;
4588 }
4589
4590 /*
4591 * One file argument: Expand wildcards.
4592 * Don't do this with ":r !command" or ":w !command".
4593 */
4594 if ((eap->argt & NOSPC) && !eap->usefilter)
4595 {
4596 /*
4597 * May do this twice:
4598 * 1. Replace environment variables.
4599 * 2. Replace any other wildcards, remove backslashes.
4600 */
4601 for (n = 1; n <= 2; ++n)
4602 {
4603 if (n == 2)
4604 {
4605#ifdef UNIX
4606 /*
4607 * Only for Unix we check for more than one file name.
4608 * For other systems spaces are considered to be part
4609 * of the file name.
4610 * Only check here if there is no wildcard, otherwise
4611 * ExpandOne() will check for errors. This allows
4612 * ":e `ls ve*.c`" on Unix.
4613 */
4614 if (!has_wildcards)
4615 for (p = eap->arg; *p; ++p)
4616 {
4617 /* skip escaped characters */
4618 if (p[1] && (*p == '\\' || *p == Ctrl_V))
4619 ++p;
4620 else if (vim_iswhite(*p))
4621 {
4622 *errormsgp = (char_u *)_("E172: Only one file name allowed");
4623 return FAIL;
4624 }
4625 }
4626#endif
4627
4628 /*
4629 * Halve the number of backslashes (this is Vi compatible).
4630 * For Unix and OS/2, when wildcards are expanded, this is
4631 * done by ExpandOne() below.
4632 */
4633#if defined(UNIX) || defined(OS2)
4634 if (!has_wildcards)
4635#endif
4636 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004637 }
4638
4639 if (has_wildcards)
4640 {
4641 if (n == 1)
4642 {
4643 /*
4644 * First loop: May expand environment variables. This
4645 * can be done much faster with expand_env() than with
4646 * something else (e.g., calling a shell).
4647 * After expanding environment variables, check again
4648 * if there are still wildcards present.
4649 */
4650 if (vim_strchr(eap->arg, '$') != NULL
4651 || vim_strchr(eap->arg, '~') != NULL)
4652 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00004653 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004654 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004655 has_wildcards = mch_has_wildcard(NameBuff);
4656 p = NameBuff;
4657 }
4658 else
4659 p = NULL;
4660 }
4661 else /* n == 2 */
4662 {
4663 expand_T xpc;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004664 int options = WILD_LIST_NOTFOUND|WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004665
4666 ExpandInit(&xpc);
4667 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004668 if (p_wic)
4669 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004670 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01004671 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004672 if (p == NULL)
4673 return FAIL;
4674 }
4675 if (p != NULL)
4676 {
4677 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
4678 p, cmdlinep);
4679 if (n == 2) /* p came from ExpandOne() */
4680 vim_free(p);
4681 }
4682 }
4683 }
4684 }
4685 return OK;
4686}
4687
4688/*
4689 * Replace part of the command line, keeping eap->cmd, eap->arg and
4690 * eap->nextcmd correct.
4691 * "src" points to the part that is to be replaced, of length "srclen".
4692 * "repl" is the replacement string.
4693 * Returns a pointer to the character after the replaced string.
4694 * Returns NULL for failure.
4695 */
4696 static char_u *
4697repl_cmdline(eap, src, srclen, repl, cmdlinep)
4698 exarg_T *eap;
4699 char_u *src;
4700 int srclen;
4701 char_u *repl;
4702 char_u **cmdlinep;
4703{
4704 int len;
4705 int i;
4706 char_u *new_cmdline;
4707
4708 /*
4709 * The new command line is build in new_cmdline[].
4710 * First allocate it.
4711 * Careful: a "+cmd" argument may have been NUL terminated.
4712 */
4713 len = (int)STRLEN(repl);
4714 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004715 if (eap->nextcmd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716 i += (int)STRLEN(eap->nextcmd);/* add space for next command */
4717 if ((new_cmdline = alloc((unsigned)i)) == NULL)
4718 return NULL; /* out of memory! */
4719
4720 /*
4721 * Copy the stuff before the expanded part.
4722 * Copy the expanded stuff.
4723 * Copy what came after the expanded part.
4724 * Copy the next commands, if there are any.
4725 */
4726 i = (int)(src - *cmdlinep); /* length of part before match */
4727 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00004728
Bram Moolenaar071d4272004-06-13 20:20:40 +00004729 mch_memmove(new_cmdline + i, repl, (size_t)len);
4730 i += len; /* remember the end of the string */
4731 STRCPY(new_cmdline + i, src + srclen);
4732 src = new_cmdline + i; /* remember where to continue */
4733
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004734 if (eap->nextcmd != NULL) /* append next command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004735 {
4736 i = (int)STRLEN(new_cmdline) + 1;
4737 STRCPY(new_cmdline + i, eap->nextcmd);
4738 eap->nextcmd = new_cmdline + i;
4739 }
4740 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
4741 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
4742 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
4743 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
4744 vim_free(*cmdlinep);
4745 *cmdlinep = new_cmdline;
4746
4747 return src;
4748}
4749
4750/*
4751 * Check for '|' to separate commands and '"' to start comments.
4752 */
4753 void
4754separate_nextcmd(eap)
4755 exarg_T *eap;
4756{
4757 char_u *p;
4758
Bram Moolenaar86b68352004-12-27 21:59:20 +00004759#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004760 p = skip_grep_pat(eap);
4761#else
4762 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004763#endif
4764
4765 for ( ; *p; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004766 {
4767 if (*p == Ctrl_V)
4768 {
4769 if (eap->argt & (USECTRLV | XFILE))
4770 ++p; /* skip CTRL-V and next char */
4771 else
Bram Moolenaarb0db5692007-08-14 20:54:49 +00004772 /* remove CTRL-V and skip next char */
Bram Moolenaara7241f52008-06-24 20:39:31 +00004773 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004774 if (*p == NUL) /* stop at NUL after CTRL-V */
4775 break;
4776 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004777
4778#ifdef FEAT_EVAL
4779 /* Skip over `=expr` when wildcards are expanded. */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004780 else if (p[0] == '`' && p[1] == '=' && (eap->argt & XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004781 {
4782 p += 2;
4783 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004784 }
4785#endif
4786
Bram Moolenaar071d4272004-06-13 20:20:40 +00004787 /* Check for '"': start of comment or '|': next command */
4788 /* :@" and :*" do not start a comment!
4789 * :redir @" doesn't either. */
4790 else if ((*p == '"' && !(eap->argt & NOTRLCOM)
4791 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
4792 || p != eap->arg)
4793 && (eap->cmdidx != CMD_redir
4794 || p != eap->arg + 1 || p[-1] != '@'))
4795 || *p == '|' || *p == '\n')
4796 {
4797 /*
4798 * We remove the '\' before the '|', unless USECTRLV is used
4799 * AND 'b' is present in 'cpoptions'.
4800 */
4801 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
4802 || !(eap->argt & USECTRLV)) && *(p - 1) == '\\')
4803 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00004804 STRMOVE(p - 1, p); /* remove the '\' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004805 --p;
4806 }
4807 else
4808 {
4809 eap->nextcmd = check_nextcmd(p);
4810 *p = NUL;
4811 break;
4812 }
4813 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004814 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004815
Bram Moolenaar071d4272004-06-13 20:20:40 +00004816 if (!(eap->argt & NOTRLCOM)) /* remove trailing spaces */
4817 del_trailing_spaces(eap->arg);
4818}
4819
4820/*
4821 * get + command from ex argument
4822 */
4823 static char_u *
4824getargcmd(argp)
4825 char_u **argp;
4826{
4827 char_u *arg = *argp;
4828 char_u *command = NULL;
4829
4830 if (*arg == '+') /* +[command] */
4831 {
4832 ++arg;
4833 if (vim_isspace(*arg))
4834 command = dollar_command;
4835 else
4836 {
4837 command = arg;
4838 arg = skip_cmd_arg(command, TRUE);
4839 if (*arg != NUL)
4840 *arg++ = NUL; /* terminate command with NUL */
4841 }
4842
4843 arg = skipwhite(arg); /* skip over spaces */
4844 *argp = arg;
4845 }
4846 return command;
4847}
4848
4849/*
4850 * Find end of "+command" argument. Skip over "\ " and "\\".
4851 */
4852 static char_u *
4853skip_cmd_arg(p, rembs)
4854 char_u *p;
4855 int rembs; /* TRUE to halve the number of backslashes */
4856{
4857 while (*p && !vim_isspace(*p))
4858 {
4859 if (*p == '\\' && p[1] != NUL)
4860 {
4861 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00004862 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004863 else
4864 ++p;
4865 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004866 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004867 }
4868 return p;
4869}
4870
4871/*
4872 * Get "++opt=arg" argument.
4873 * Return FAIL or OK.
4874 */
4875 static int
4876getargopt(eap)
4877 exarg_T *eap;
4878{
4879 char_u *arg = eap->arg + 2;
4880 int *pp = NULL;
4881#ifdef FEAT_MBYTE
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004882 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004883 char_u *p;
4884#endif
4885
4886 /* ":edit ++[no]bin[ary] file" */
4887 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
4888 {
4889 if (*arg == 'n')
4890 {
4891 arg += 2;
4892 eap->force_bin = FORCE_NOBIN;
4893 }
4894 else
4895 eap->force_bin = FORCE_BIN;
4896 if (!checkforcmd(&arg, "binary", 3))
4897 return FAIL;
4898 eap->arg = skipwhite(arg);
4899 return OK;
4900 }
4901
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004902 /* ":read ++edit file" */
4903 if (STRNCMP(arg, "edit", 4) == 0)
4904 {
4905 eap->read_edit = TRUE;
4906 eap->arg = skipwhite(arg + 4);
4907 return OK;
4908 }
4909
Bram Moolenaar071d4272004-06-13 20:20:40 +00004910 if (STRNCMP(arg, "ff", 2) == 0)
4911 {
4912 arg += 2;
4913 pp = &eap->force_ff;
4914 }
4915 else if (STRNCMP(arg, "fileformat", 10) == 0)
4916 {
4917 arg += 10;
4918 pp = &eap->force_ff;
4919 }
4920#ifdef FEAT_MBYTE
4921 else if (STRNCMP(arg, "enc", 3) == 0)
4922 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01004923 if (STRNCMP(arg, "encoding", 8) == 0)
4924 arg += 8;
4925 else
4926 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927 pp = &eap->force_enc;
4928 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004929 else if (STRNCMP(arg, "bad", 3) == 0)
4930 {
4931 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004932 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004933 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004934#endif
4935
4936 if (pp == NULL || *arg != '=')
4937 return FAIL;
4938
4939 ++arg;
4940 *pp = (int)(arg - eap->cmd);
4941 arg = skip_cmd_arg(arg, FALSE);
4942 eap->arg = skipwhite(arg);
4943 *arg = NUL;
4944
4945#ifdef FEAT_MBYTE
4946 if (pp == &eap->force_ff)
4947 {
4948#endif
4949 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
4950 return FAIL;
4951#ifdef FEAT_MBYTE
4952 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004953 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954 {
4955 /* Make 'fileencoding' lower case. */
4956 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
4957 *p = TOLOWER_ASC(*p);
4958 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004959 else
4960 {
4961 /* Check ++bad= argument. Must be a single-byte character, "keep" or
4962 * "drop". */
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004963 p = eap->cmd + bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004964 if (STRICMP(p, "keep") == 0)
4965 eap->bad_char = BAD_KEEP;
4966 else if (STRICMP(p, "drop") == 0)
4967 eap->bad_char = BAD_DROP;
4968 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
4969 eap->bad_char = *p;
4970 else
4971 return FAIL;
4972 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004973#endif
4974
4975 return OK;
4976}
4977
4978/*
4979 * ":abbreviate" and friends.
4980 */
4981 static void
4982ex_abbreviate(eap)
4983 exarg_T *eap;
4984{
4985 do_exmap(eap, TRUE); /* almost the same as mapping */
4986}
4987
4988/*
4989 * ":map" and friends.
4990 */
4991 static void
4992ex_map(eap)
4993 exarg_T *eap;
4994{
4995 /*
4996 * If we are sourcing .exrc or .vimrc in current directory we
4997 * print the mappings for security reasons.
4998 */
4999 if (secure)
5000 {
5001 secure = 2;
5002 msg_outtrans(eap->cmd);
5003 msg_putchar('\n');
5004 }
5005 do_exmap(eap, FALSE);
5006}
5007
5008/*
5009 * ":unmap" and friends.
5010 */
5011 static void
5012ex_unmap(eap)
5013 exarg_T *eap;
5014{
5015 do_exmap(eap, FALSE);
5016}
5017
5018/*
5019 * ":mapclear" and friends.
5020 */
5021 static void
5022ex_mapclear(eap)
5023 exarg_T *eap;
5024{
5025 map_clear(eap->cmd, eap->arg, eap->forceit, FALSE);
5026}
5027
5028/*
5029 * ":abclear" and friends.
5030 */
5031 static void
5032ex_abclear(eap)
5033 exarg_T *eap;
5034{
5035 map_clear(eap->cmd, eap->arg, TRUE, TRUE);
5036}
5037
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005038#if defined(FEAT_AUTOCMD) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005039 static void
5040ex_autocmd(eap)
5041 exarg_T *eap;
5042{
5043 /*
5044 * Disallow auto commands from .exrc and .vimrc in current
5045 * directory for security reasons.
5046 */
5047 if (secure)
5048 {
5049 secure = 2;
5050 eap->errmsg = e_curdir;
5051 }
5052 else if (eap->cmdidx == CMD_autocmd)
5053 do_autocmd(eap->arg, eap->forceit);
5054 else
5055 do_augroup(eap->arg, eap->forceit);
5056}
5057
5058/*
5059 * ":doautocmd": Apply the automatic commands to the current buffer.
5060 */
5061 static void
5062ex_doautocmd(eap)
5063 exarg_T *eap;
5064{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005065 char_u *arg = eap->arg;
5066 int call_do_modelines = check_nomodeline(&arg);
5067
5068 (void)do_doautocmd(arg, TRUE);
5069 if (call_do_modelines) /* Only when there is no <nomodeline>. */
5070 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005071}
5072#endif
5073
5074#ifdef FEAT_LISTCMDS
5075/*
5076 * :[N]bunload[!] [N] [bufname] unload buffer
5077 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
5078 * :[N]bwipeout[!] [N] [bufname] delete buffer really
5079 */
5080 static void
5081ex_bunload(eap)
5082 exarg_T *eap;
5083{
5084 eap->errmsg = do_bufdel(
5085 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
5086 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
5087 : DOBUF_UNLOAD, eap->arg,
5088 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
5089}
5090
5091/*
5092 * :[N]buffer [N] to buffer N
5093 * :[N]sbuffer [N] to buffer N
5094 */
5095 static void
5096ex_buffer(eap)
5097 exarg_T *eap;
5098{
5099 if (*eap->arg)
5100 eap->errmsg = e_trailing;
5101 else
5102 {
5103 if (eap->addr_count == 0) /* default is current buffer */
5104 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
5105 else
5106 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
5107 }
5108}
5109
5110/*
5111 * :[N]bmodified [N] to next mod. buffer
5112 * :[N]sbmodified [N] to next mod. buffer
5113 */
5114 static void
5115ex_bmodified(eap)
5116 exarg_T *eap;
5117{
5118 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
5119}
5120
5121/*
5122 * :[N]bnext [N] to next buffer
5123 * :[N]sbnext [N] split and to next buffer
5124 */
5125 static void
5126ex_bnext(eap)
5127 exarg_T *eap;
5128{
5129 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
5130}
5131
5132/*
5133 * :[N]bNext [N] to previous buffer
5134 * :[N]bprevious [N] to previous buffer
5135 * :[N]sbNext [N] split and to previous buffer
5136 * :[N]sbprevious [N] split and to previous buffer
5137 */
5138 static void
5139ex_bprevious(eap)
5140 exarg_T *eap;
5141{
5142 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
5143}
5144
5145/*
5146 * :brewind to first buffer
5147 * :bfirst to first buffer
5148 * :sbrewind split and to first buffer
5149 * :sbfirst split and to first buffer
5150 */
5151 static void
5152ex_brewind(eap)
5153 exarg_T *eap;
5154{
5155 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
5156}
5157
5158/*
5159 * :blast to last buffer
5160 * :sblast split and to last buffer
5161 */
5162 static void
5163ex_blast(eap)
5164 exarg_T *eap;
5165{
5166 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
5167}
5168#endif
5169
5170 int
5171ends_excmd(c)
5172 int c;
5173{
5174 return (c == NUL || c == '|' || c == '"' || c == '\n');
5175}
5176
5177#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5178 || defined(PROTO)
5179/*
5180 * Return the next command, after the first '|' or '\n'.
5181 * Return NULL if not found.
5182 */
5183 char_u *
5184find_nextcmd(p)
5185 char_u *p;
5186{
5187 while (*p != '|' && *p != '\n')
5188 {
5189 if (*p == NUL)
5190 return NULL;
5191 ++p;
5192 }
5193 return (p + 1);
5194}
5195#endif
5196
5197/*
5198 * Check if *p is a separator between Ex commands.
5199 * Return NULL if it isn't, (p + 1) if it is.
5200 */
5201 char_u *
5202check_nextcmd(p)
5203 char_u *p;
5204{
5205 p = skipwhite(p);
5206 if (*p == '|' || *p == '\n')
5207 return (p + 1);
5208 else
5209 return NULL;
5210}
5211
5212/*
5213 * - if there are more files to edit
5214 * - and this is the last window
5215 * - and forceit not used
5216 * - and not repeated twice on a row
5217 * return FAIL and give error message if 'message' TRUE
5218 * return OK otherwise
5219 */
5220 static int
5221check_more(message, forceit)
5222 int message; /* when FALSE check only, no messages */
5223 int forceit;
5224{
5225 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5226
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005227 if (!forceit && only_one_window()
5228 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005229 {
5230 if (message)
5231 {
5232#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
5233 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
5234 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005235 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005236
5237 if (n == 1)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02005238 vim_strncpy(buff,
5239 (char_u *)_("1 more file to edit. Quit anyway?"),
Bram Moolenaard9462e32011-04-11 21:35:11 +02005240 DIALOG_MSG_SIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005241 else
Bram Moolenaard9462e32011-04-11 21:35:11 +02005242 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243 _("%d more files to edit. Quit anyway?"), n);
5244 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5245 return OK;
5246 return FAIL;
5247 }
5248#endif
5249 if (n == 1)
5250 EMSG(_("E173: 1 more file to edit"));
5251 else
5252 EMSGN(_("E173: %ld more files to edit"), n);
5253 quitmore = 2; /* next try to quit is allowed */
5254 }
5255 return FAIL;
5256 }
5257 return OK;
5258}
5259
5260#ifdef FEAT_CMDL_COMPL
5261/*
5262 * Function given to ExpandGeneric() to obtain the list of command names.
5263 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005264 char_u *
5265get_command_name(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00005266 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005267 int idx;
5268{
5269 if (idx >= (int)CMD_SIZE)
5270# ifdef FEAT_USR_CMDS
5271 return get_user_command_name(idx);
5272# else
5273 return NULL;
5274# endif
5275 return cmdnames[idx].cmd_name;
5276}
5277#endif
5278
5279#if defined(FEAT_USR_CMDS) || defined(PROTO)
5280static 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));
5281static void uc_list __ARGS((char_u *name, size_t name_len));
5282static int uc_scan_attr __ARGS((char_u *attr, size_t len, long *argt, long *def, int *flags, int *compl, char_u **compl_arg));
5283static char_u *uc_split_args __ARGS((char_u *arg, size_t *lenp));
5284static 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));
5285
5286 static int
5287uc_add_command(name, name_len, rep, argt, def, flags, compl, compl_arg, force)
5288 char_u *name;
5289 size_t name_len;
5290 char_u *rep;
5291 long argt;
5292 long def;
5293 int flags;
5294 int compl;
5295 char_u *compl_arg;
5296 int force;
5297{
5298 ucmd_T *cmd = NULL;
5299 char_u *p;
5300 int i;
5301 int cmp = 1;
5302 char_u *rep_buf = NULL;
5303 garray_T *gap;
5304
Bram Moolenaar9c102382006-05-03 21:26:49 +00005305 replace_termcodes(rep, &rep_buf, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005306 if (rep_buf == NULL)
5307 {
5308 /* Can't replace termcodes - try using the string as is */
5309 rep_buf = vim_strsave(rep);
5310
5311 /* Give up if out of memory */
5312 if (rep_buf == NULL)
5313 return FAIL;
5314 }
5315
5316 /* get address of growarray: global or in curbuf */
5317 if (flags & UC_BUFFER)
5318 {
5319 gap = &curbuf->b_ucmds;
5320 if (gap->ga_itemsize == 0)
5321 ga_init2(gap, (int)sizeof(ucmd_T), 4);
5322 }
5323 else
5324 gap = &ucmds;
5325
5326 /* Search for the command in the already defined commands. */
5327 for (i = 0; i < gap->ga_len; ++i)
5328 {
5329 size_t len;
5330
5331 cmd = USER_CMD_GA(gap, i);
5332 len = STRLEN(cmd->uc_name);
5333 cmp = STRNCMP(name, cmd->uc_name, name_len);
5334 if (cmp == 0)
5335 {
5336 if (name_len < len)
5337 cmp = -1;
5338 else if (name_len > len)
5339 cmp = 1;
5340 }
5341
5342 if (cmp == 0)
5343 {
5344 if (!force)
5345 {
5346 EMSG(_("E174: Command already exists: add ! to replace it"));
5347 goto fail;
5348 }
5349
5350 vim_free(cmd->uc_rep);
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00005351 cmd->uc_rep = NULL;
5352#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5353 vim_free(cmd->uc_compl_arg);
5354 cmd->uc_compl_arg = NULL;
5355#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005356 break;
5357 }
5358
5359 /* Stop as soon as we pass the name to add */
5360 if (cmp < 0)
5361 break;
5362 }
5363
5364 /* Extend the array unless we're replacing an existing command */
5365 if (cmp != 0)
5366 {
5367 if (ga_grow(gap, 1) != OK)
5368 goto fail;
5369 if ((p = vim_strnsave(name, (int)name_len)) == NULL)
5370 goto fail;
5371
5372 cmd = USER_CMD_GA(gap, i);
5373 mch_memmove(cmd + 1, cmd, (gap->ga_len - i) * sizeof(ucmd_T));
5374
5375 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005376
5377 cmd->uc_name = p;
5378 }
5379
5380 cmd->uc_rep = rep_buf;
5381 cmd->uc_argt = argt;
5382 cmd->uc_def = def;
5383 cmd->uc_compl = compl;
5384#ifdef FEAT_EVAL
5385 cmd->uc_scriptID = current_SID;
5386# ifdef FEAT_CMDL_COMPL
5387 cmd->uc_compl_arg = compl_arg;
5388# endif
5389#endif
5390
5391 return OK;
5392
5393fail:
5394 vim_free(rep_buf);
5395#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5396 vim_free(compl_arg);
5397#endif
5398 return FAIL;
5399}
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005400#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005401
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005402#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005403/*
5404 * List of names for completion for ":command" with the EXPAND_ flag.
5405 * Must be alphabetical for completion.
5406 */
5407static struct
5408{
5409 int expand;
5410 char *name;
5411} command_complete[] =
5412{
5413 {EXPAND_AUGROUP, "augroup"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005414 {EXPAND_BEHAVE, "behave"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005415 {EXPAND_BUFFERS, "buffer"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005416 {EXPAND_COLORS, "color"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005417 {EXPAND_COMMANDS, "command"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005418 {EXPAND_COMPILER, "compiler"},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005419#if defined(FEAT_CSCOPE)
5420 {EXPAND_CSCOPE, "cscope"},
5421#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005422#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5423 {EXPAND_USER_DEFINED, "custom"},
Bram Moolenaara466c992005-07-09 21:03:22 +00005424 {EXPAND_USER_LIST, "customlist"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005425#endif
5426 {EXPAND_DIRECTORIES, "dir"},
5427 {EXPAND_ENV_VARS, "environment"},
5428 {EXPAND_EVENTS, "event"},
5429 {EXPAND_EXPRESSION, "expression"},
5430 {EXPAND_FILES, "file"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005431 {EXPAND_FILES_IN_PATH, "file_in_path"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005432 {EXPAND_FILETYPE, "filetype"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005433 {EXPAND_FUNCTIONS, "function"},
5434 {EXPAND_HELP, "help"},
5435 {EXPAND_HIGHLIGHT, "highlight"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005436#if defined(FEAT_CMDHIST)
5437 {EXPAND_HISTORY, "history"},
5438#endif
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005439#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005440 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005441 {EXPAND_LOCALES, "locale"},
5442#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005443 {EXPAND_MAPPINGS, "mapping"},
5444 {EXPAND_MENUS, "menu"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005445 {EXPAND_OWNSYNTAX, "syntax"},
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02005446#if defined(FEAT_PROFILE)
5447 {EXPAND_SYNTIME, "syntime"},
5448#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005449 {EXPAND_SETTINGS, "option"},
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005450 {EXPAND_SHELLCMD, "shellcmd"},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005451#if defined(FEAT_SIGNS)
5452 {EXPAND_SIGN, "sign"},
5453#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005454 {EXPAND_TAGS, "tag"},
5455 {EXPAND_TAGS_LISTFILES, "tag_listfiles"},
Bram Moolenaar24305862012-08-15 14:05:05 +02005456 {EXPAND_USER, "user"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005457 {EXPAND_USER_VARS, "var"},
5458 {0, NULL}
5459};
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005460#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005461
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005462#if defined(FEAT_USR_CMDS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005463 static void
5464uc_list(name, name_len)
5465 char_u *name;
5466 size_t name_len;
5467{
5468 int i, j;
5469 int found = FALSE;
5470 ucmd_T *cmd;
5471 int len;
5472 long a;
5473 garray_T *gap;
5474
5475 gap = &curbuf->b_ucmds;
5476 for (;;)
5477 {
5478 for (i = 0; i < gap->ga_len; ++i)
5479 {
5480 cmd = USER_CMD_GA(gap, i);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005481 a = (long)cmd->uc_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005482
5483 /* Skip commands which don't match the requested prefix */
5484 if (STRNCMP(name, cmd->uc_name, name_len) != 0)
5485 continue;
5486
5487 /* Put out the title first time */
5488 if (!found)
5489 MSG_PUTS_TITLE(_("\n Name Args Range Complete Definition"));
5490 found = TRUE;
5491 msg_putchar('\n');
5492 if (got_int)
5493 break;
5494
5495 /* Special cases */
5496 msg_putchar(a & BANG ? '!' : ' ');
5497 msg_putchar(a & REGSTR ? '"' : ' ');
5498 msg_putchar(gap != &ucmds ? 'b' : ' ');
5499 msg_putchar(' ');
5500
5501 msg_outtrans_attr(cmd->uc_name, hl_attr(HLF_D));
5502 len = (int)STRLEN(cmd->uc_name) + 4;
5503
5504 do {
5505 msg_putchar(' ');
5506 ++len;
5507 } while (len < 16);
5508
5509 len = 0;
5510
5511 /* Arguments */
5512 switch ((int)(a & (EXTRA|NOSPC|NEEDARG)))
5513 {
5514 case 0: IObuff[len++] = '0'; break;
5515 case (EXTRA): IObuff[len++] = '*'; break;
5516 case (EXTRA|NOSPC): IObuff[len++] = '?'; break;
5517 case (EXTRA|NEEDARG): IObuff[len++] = '+'; break;
5518 case (EXTRA|NOSPC|NEEDARG): IObuff[len++] = '1'; break;
5519 }
5520
5521 do {
5522 IObuff[len++] = ' ';
5523 } while (len < 5);
5524
5525 /* Range */
5526 if (a & (RANGE|COUNT))
5527 {
5528 if (a & COUNT)
5529 {
5530 /* -count=N */
5531 sprintf((char *)IObuff + len, "%ldc", cmd->uc_def);
5532 len += (int)STRLEN(IObuff + len);
5533 }
5534 else if (a & DFLALL)
5535 IObuff[len++] = '%';
5536 else if (cmd->uc_def >= 0)
5537 {
5538 /* -range=N */
5539 sprintf((char *)IObuff + len, "%ld", cmd->uc_def);
5540 len += (int)STRLEN(IObuff + len);
5541 }
5542 else
5543 IObuff[len++] = '.';
5544 }
5545
5546 do {
5547 IObuff[len++] = ' ';
5548 } while (len < 11);
5549
5550 /* Completion */
5551 for (j = 0; command_complete[j].expand != 0; ++j)
5552 if (command_complete[j].expand == cmd->uc_compl)
5553 {
5554 STRCPY(IObuff + len, command_complete[j].name);
5555 len += (int)STRLEN(IObuff + len);
5556 break;
5557 }
5558
5559 do {
5560 IObuff[len++] = ' ';
5561 } while (len < 21);
5562
5563 IObuff[len] = '\0';
5564 msg_outtrans(IObuff);
5565
5566 msg_outtrans_special(cmd->uc_rep, FALSE);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005567#ifdef FEAT_EVAL
5568 if (p_verbose > 0)
5569 last_set_msg(cmd->uc_scriptID);
5570#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005571 out_flush();
5572 ui_breakcheck();
5573 if (got_int)
5574 break;
5575 }
5576 if (gap == &ucmds || i < gap->ga_len)
5577 break;
5578 gap = &ucmds;
5579 }
5580
5581 if (!found)
5582 MSG(_("No user-defined commands found"));
5583}
5584
5585 static char_u *
5586uc_fun_cmd()
5587{
5588 static char_u fcmd[] = {0x84, 0xaf, 0x60, 0xb9, 0xaf, 0xb5, 0x60, 0xa4,
5589 0xa5, 0xad, 0xa1, 0xae, 0xa4, 0x60, 0xa1, 0x60,
5590 0xb3, 0xa8, 0xb2, 0xb5, 0xa2, 0xa2, 0xa5, 0xb2,
5591 0xb9, 0x7f, 0};
5592 int i;
5593
5594 for (i = 0; fcmd[i]; ++i)
5595 IObuff[i] = fcmd[i] - 0x40;
5596 IObuff[i] = 0;
5597 return IObuff;
5598}
5599
5600 static int
5601uc_scan_attr(attr, len, argt, def, flags, compl, compl_arg)
5602 char_u *attr;
5603 size_t len;
5604 long *argt;
5605 long *def;
5606 int *flags;
5607 int *compl;
5608 char_u **compl_arg;
5609{
5610 char_u *p;
5611
5612 if (len == 0)
5613 {
5614 EMSG(_("E175: No attribute specified"));
5615 return FAIL;
5616 }
5617
5618 /* First, try the simple attributes (no arguments) */
5619 if (STRNICMP(attr, "bang", len) == 0)
5620 *argt |= BANG;
5621 else if (STRNICMP(attr, "buffer", len) == 0)
5622 *flags |= UC_BUFFER;
5623 else if (STRNICMP(attr, "register", len) == 0)
5624 *argt |= REGSTR;
5625 else if (STRNICMP(attr, "bar", len) == 0)
5626 *argt |= TRLBAR;
5627 else
5628 {
5629 int i;
5630 char_u *val = NULL;
5631 size_t vallen = 0;
5632 size_t attrlen = len;
5633
5634 /* Look for the attribute name - which is the part before any '=' */
5635 for (i = 0; i < (int)len; ++i)
5636 {
5637 if (attr[i] == '=')
5638 {
5639 val = &attr[i + 1];
5640 vallen = len - i - 1;
5641 attrlen = i;
5642 break;
5643 }
5644 }
5645
5646 if (STRNICMP(attr, "nargs", attrlen) == 0)
5647 {
5648 if (vallen == 1)
5649 {
5650 if (*val == '0')
5651 /* Do nothing - this is the default */;
5652 else if (*val == '1')
5653 *argt |= (EXTRA | NOSPC | NEEDARG);
5654 else if (*val == '*')
5655 *argt |= EXTRA;
5656 else if (*val == '?')
5657 *argt |= (EXTRA | NOSPC);
5658 else if (*val == '+')
5659 *argt |= (EXTRA | NEEDARG);
5660 else
5661 goto wrong_nargs;
5662 }
5663 else
5664 {
5665wrong_nargs:
5666 EMSG(_("E176: Invalid number of arguments"));
5667 return FAIL;
5668 }
5669 }
5670 else if (STRNICMP(attr, "range", attrlen) == 0)
5671 {
5672 *argt |= RANGE;
5673 if (vallen == 1 && *val == '%')
5674 *argt |= DFLALL;
5675 else if (val != NULL)
5676 {
5677 p = val;
5678 if (*def >= 0)
5679 {
5680two_count:
5681 EMSG(_("E177: Count cannot be specified twice"));
5682 return FAIL;
5683 }
5684
5685 *def = getdigits(&p);
5686 *argt |= (ZEROR | NOTADR);
5687
5688 if (p != val + vallen || vallen == 0)
5689 {
5690invalid_count:
5691 EMSG(_("E178: Invalid default value for count"));
5692 return FAIL;
5693 }
5694 }
5695 }
5696 else if (STRNICMP(attr, "count", attrlen) == 0)
5697 {
Bram Moolenaar32e7b2d2005-02-27 22:36:47 +00005698 *argt |= (COUNT | ZEROR | RANGE | NOTADR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005699
5700 if (val != NULL)
5701 {
5702 p = val;
5703 if (*def >= 0)
5704 goto two_count;
5705
5706 *def = getdigits(&p);
5707
5708 if (p != val + vallen)
5709 goto invalid_count;
5710 }
5711
5712 if (*def < 0)
5713 *def = 0;
5714 }
5715 else if (STRNICMP(attr, "complete", attrlen) == 0)
5716 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005717 if (val == NULL)
5718 {
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005719 EMSG(_("E179: argument required for -complete"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005720 return FAIL;
5721 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005722
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005723 if (parse_compl_arg(val, (int)vallen, compl, argt, compl_arg)
5724 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005725 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005726 }
5727 else
5728 {
5729 char_u ch = attr[len];
5730 attr[len] = '\0';
5731 EMSG2(_("E181: Invalid attribute: %s"), attr);
5732 attr[len] = ch;
5733 return FAIL;
5734 }
5735 }
5736
5737 return OK;
5738}
5739
Bram Moolenaara850a712009-01-28 14:42:59 +00005740/*
5741 * ":command ..."
5742 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005743 static void
5744ex_command(eap)
5745 exarg_T *eap;
5746{
5747 char_u *name;
5748 char_u *end;
5749 char_u *p;
5750 long argt = 0;
5751 long def = -1;
5752 int flags = 0;
5753 int compl = EXPAND_NOTHING;
5754 char_u *compl_arg = NULL;
5755 int has_attr = (eap->arg[0] == '-');
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01005756 int name_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005757
5758 p = eap->arg;
5759
5760 /* Check for attributes */
5761 while (*p == '-')
5762 {
5763 ++p;
5764 end = skiptowhite(p);
5765 if (uc_scan_attr(p, end - p, &argt, &def, &flags, &compl, &compl_arg)
5766 == FAIL)
5767 return;
5768 p = skipwhite(end);
5769 }
5770
5771 /* Get the name (if any) and skip to the following argument */
5772 name = p;
5773 if (ASCII_ISALPHA(*p))
5774 while (ASCII_ISALNUM(*p))
5775 ++p;
5776 if (!ends_excmd(*p) && !vim_iswhite(*p))
5777 {
5778 EMSG(_("E182: Invalid command name"));
5779 return;
5780 }
5781 end = p;
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01005782 name_len = (int)(end - name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005783
5784 /* If there is nothing after the name, and no attributes were specified,
5785 * we are listing commands
5786 */
5787 p = skipwhite(end);
5788 if (!has_attr && ends_excmd(*p))
5789 {
5790 uc_list(name, end - name);
5791 }
5792 else if (!ASCII_ISUPPER(*name))
5793 {
5794 EMSG(_("E183: User defined commands must start with an uppercase letter"));
5795 return;
5796 }
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01005797 else if ((name_len == 1 && *name == 'X')
5798 || (name_len <= 4
5799 && STRNCMP(name, "Next", name_len > 4 ? 4 : name_len) == 0))
5800 {
5801 EMSG(_("E841: Reserved name, cannot be used for user defined command"));
5802 return;
5803 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005804 else
5805 uc_add_command(name, end - name, p, argt, def, flags, compl, compl_arg,
5806 eap->forceit);
5807}
5808
5809/*
5810 * ":comclear"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005811 * Clear all user commands, global and for current buffer.
5812 */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00005813 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005814ex_comclear(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00005815 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005816{
5817 uc_clear(&ucmds);
5818 uc_clear(&curbuf->b_ucmds);
5819}
5820
5821/*
5822 * Clear all user commands for "gap".
5823 */
5824 void
5825uc_clear(gap)
5826 garray_T *gap;
5827{
5828 int i;
5829 ucmd_T *cmd;
5830
5831 for (i = 0; i < gap->ga_len; ++i)
5832 {
5833 cmd = USER_CMD_GA(gap, i);
5834 vim_free(cmd->uc_name);
5835 vim_free(cmd->uc_rep);
5836# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5837 vim_free(cmd->uc_compl_arg);
5838# endif
5839 }
5840 ga_clear(gap);
5841}
5842
5843 static void
5844ex_delcommand(eap)
5845 exarg_T *eap;
5846{
5847 int i = 0;
5848 ucmd_T *cmd = NULL;
5849 int cmp = -1;
5850 garray_T *gap;
5851
5852 gap = &curbuf->b_ucmds;
5853 for (;;)
5854 {
5855 for (i = 0; i < gap->ga_len; ++i)
5856 {
5857 cmd = USER_CMD_GA(gap, i);
5858 cmp = STRCMP(eap->arg, cmd->uc_name);
5859 if (cmp <= 0)
5860 break;
5861 }
5862 if (gap == &ucmds || cmp == 0)
5863 break;
5864 gap = &ucmds;
5865 }
5866
5867 if (cmp != 0)
5868 {
5869 EMSG2(_("E184: No such user-defined command: %s"), eap->arg);
5870 return;
5871 }
5872
5873 vim_free(cmd->uc_name);
5874 vim_free(cmd->uc_rep);
5875# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5876 vim_free(cmd->uc_compl_arg);
5877# endif
5878
5879 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005880
5881 if (i < gap->ga_len)
5882 mch_memmove(cmd, cmd + 1, (gap->ga_len - i) * sizeof(ucmd_T));
5883}
5884
Bram Moolenaar552f8a12007-03-08 17:12:08 +00005885/*
5886 * split and quote args for <f-args>
5887 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005888 static char_u *
5889uc_split_args(arg, lenp)
5890 char_u *arg;
5891 size_t *lenp;
5892{
5893 char_u *buf;
5894 char_u *p;
5895 char_u *q;
5896 int len;
5897
5898 /* Precalculate length */
5899 p = arg;
5900 len = 2; /* Initial and final quotes */
5901
5902 while (*p)
5903 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00005904 if (p[0] == '\\' && p[1] == '\\')
5905 {
5906 len += 2;
5907 p += 2;
5908 }
5909 else if (p[0] == '\\' && vim_iswhite(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005910 {
5911 len += 1;
5912 p += 2;
5913 }
5914 else if (*p == '\\' || *p == '"')
5915 {
5916 len += 2;
5917 p += 1;
5918 }
5919 else if (vim_iswhite(*p))
5920 {
5921 p = skipwhite(p);
5922 if (*p == NUL)
5923 break;
5924 len += 3; /* "," */
5925 }
5926 else
5927 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02005928#ifdef FEAT_MBYTE
5929 int charlen = (*mb_ptr2len)(p);
5930 len += charlen;
5931 p += charlen;
5932#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005933 ++len;
5934 ++p;
Bram Moolenaardfef1542012-07-10 19:25:10 +02005935#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005936 }
5937 }
5938
5939 buf = alloc(len + 1);
5940 if (buf == NULL)
5941 {
5942 *lenp = 0;
5943 return buf;
5944 }
5945
5946 p = arg;
5947 q = buf;
5948 *q++ = '"';
5949 while (*p)
5950 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00005951 if (p[0] == '\\' && p[1] == '\\')
5952 {
5953 *q++ = '\\';
5954 *q++ = '\\';
5955 p += 2;
5956 }
5957 else if (p[0] == '\\' && vim_iswhite(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005958 {
5959 *q++ = p[1];
5960 p += 2;
5961 }
5962 else if (*p == '\\' || *p == '"')
5963 {
5964 *q++ = '\\';
5965 *q++ = *p++;
5966 }
5967 else if (vim_iswhite(*p))
5968 {
5969 p = skipwhite(p);
5970 if (*p == NUL)
5971 break;
5972 *q++ = '"';
5973 *q++ = ',';
5974 *q++ = '"';
5975 }
5976 else
5977 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02005978 MB_COPY_CHAR(p, q);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005979 }
5980 }
5981 *q++ = '"';
5982 *q = 0;
5983
5984 *lenp = len;
5985 return buf;
5986}
5987
5988/*
5989 * Check for a <> code in a user command.
5990 * "code" points to the '<'. "len" the length of the <> (inclusive).
5991 * "buf" is where the result is to be added.
5992 * "split_buf" points to a buffer used for splitting, caller should free it.
5993 * "split_len" is the length of what "split_buf" contains.
5994 * Returns the length of the replacement, which has been added to "buf".
5995 * Returns -1 if there was no match, and only the "<" has been copied.
5996 */
5997 static size_t
5998uc_check_code(code, len, buf, cmd, eap, split_buf, split_len)
5999 char_u *code;
6000 size_t len;
6001 char_u *buf;
6002 ucmd_T *cmd; /* the user command we're expanding */
6003 exarg_T *eap; /* ex arguments */
6004 char_u **split_buf;
6005 size_t *split_len;
6006{
6007 size_t result = 0;
6008 char_u *p = code + 1;
6009 size_t l = len - 2;
6010 int quote = 0;
6011 enum { ct_ARGS, ct_BANG, ct_COUNT, ct_LINE1, ct_LINE2, ct_REGISTER,
6012 ct_LT, ct_NONE } type = ct_NONE;
6013
6014 if ((vim_strchr((char_u *)"qQfF", *p) != NULL) && p[1] == '-')
6015 {
6016 quote = (*p == 'q' || *p == 'Q') ? 1 : 2;
6017 p += 2;
6018 l -= 2;
6019 }
6020
Bram Moolenaar371d5402006-03-20 21:47:49 +00006021 ++l;
6022 if (l <= 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006023 type = ct_NONE;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006024 else if (STRNICMP(p, "args>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006025 type = ct_ARGS;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006026 else if (STRNICMP(p, "bang>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006027 type = ct_BANG;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006028 else if (STRNICMP(p, "count>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006029 type = ct_COUNT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006030 else if (STRNICMP(p, "line1>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006031 type = ct_LINE1;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006032 else if (STRNICMP(p, "line2>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006033 type = ct_LINE2;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006034 else if (STRNICMP(p, "lt>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006035 type = ct_LT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006036 else if (STRNICMP(p, "reg>", l) == 0 || STRNICMP(p, "register>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006037 type = ct_REGISTER;
6038
6039 switch (type)
6040 {
6041 case ct_ARGS:
6042 /* Simple case first */
6043 if (*eap->arg == NUL)
6044 {
6045 if (quote == 1)
6046 {
6047 result = 2;
6048 if (buf != NULL)
6049 STRCPY(buf, "''");
6050 }
6051 else
6052 result = 0;
6053 break;
6054 }
6055
6056 /* When specified there is a single argument don't split it.
6057 * Works for ":Cmd %" when % is "a b c". */
6058 if ((eap->argt & NOSPC) && quote == 2)
6059 quote = 1;
6060
6061 switch (quote)
6062 {
6063 case 0: /* No quoting, no splitting */
6064 result = STRLEN(eap->arg);
6065 if (buf != NULL)
6066 STRCPY(buf, eap->arg);
6067 break;
6068 case 1: /* Quote, but don't split */
6069 result = STRLEN(eap->arg) + 2;
6070 for (p = eap->arg; *p; ++p)
6071 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006072#ifdef FEAT_MBYTE
6073 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6074 /* DBCS can contain \ in a trail byte, skip the
6075 * double-byte character. */
6076 ++p;
6077 else
6078#endif
6079 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006080 ++result;
6081 }
6082
6083 if (buf != NULL)
6084 {
6085 *buf++ = '"';
6086 for (p = eap->arg; *p; ++p)
6087 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006088#ifdef FEAT_MBYTE
6089 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6090 /* DBCS can contain \ in a trail byte, copy the
6091 * double-byte character to avoid escaping. */
6092 *buf++ = *p++;
6093 else
6094#endif
6095 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006096 *buf++ = '\\';
6097 *buf++ = *p;
6098 }
6099 *buf = '"';
6100 }
6101
6102 break;
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006103 case 2: /* Quote and split (<f-args>) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006104 /* This is hard, so only do it once, and cache the result */
6105 if (*split_buf == NULL)
6106 *split_buf = uc_split_args(eap->arg, split_len);
6107
6108 result = *split_len;
6109 if (buf != NULL && result != 0)
6110 STRCPY(buf, *split_buf);
6111
6112 break;
6113 }
6114 break;
6115
6116 case ct_BANG:
6117 result = eap->forceit ? 1 : 0;
6118 if (quote)
6119 result += 2;
6120 if (buf != NULL)
6121 {
6122 if (quote)
6123 *buf++ = '"';
6124 if (eap->forceit)
6125 *buf++ = '!';
6126 if (quote)
6127 *buf = '"';
6128 }
6129 break;
6130
6131 case ct_LINE1:
6132 case ct_LINE2:
6133 case ct_COUNT:
6134 {
6135 char num_buf[20];
6136 long num = (type == ct_LINE1) ? eap->line1 :
6137 (type == ct_LINE2) ? eap->line2 :
6138 (eap->addr_count > 0) ? eap->line2 : cmd->uc_def;
6139 size_t num_len;
6140
6141 sprintf(num_buf, "%ld", num);
6142 num_len = STRLEN(num_buf);
6143 result = num_len;
6144
6145 if (quote)
6146 result += 2;
6147
6148 if (buf != NULL)
6149 {
6150 if (quote)
6151 *buf++ = '"';
6152 STRCPY(buf, num_buf);
6153 buf += num_len;
6154 if (quote)
6155 *buf = '"';
6156 }
6157
6158 break;
6159 }
6160
6161 case ct_REGISTER:
6162 result = eap->regname ? 1 : 0;
6163 if (quote)
6164 result += 2;
6165 if (buf != NULL)
6166 {
6167 if (quote)
6168 *buf++ = '\'';
6169 if (eap->regname)
6170 *buf++ = eap->regname;
6171 if (quote)
6172 *buf = '\'';
6173 }
6174 break;
6175
6176 case ct_LT:
6177 result = 1;
6178 if (buf != NULL)
6179 *buf = '<';
6180 break;
6181
6182 default:
6183 /* Not recognized: just copy the '<' and return -1. */
6184 result = (size_t)-1;
6185 if (buf != NULL)
6186 *buf = '<';
6187 break;
6188 }
6189
6190 return result;
6191}
6192
6193 static void
6194do_ucmd(eap)
6195 exarg_T *eap;
6196{
6197 char_u *buf;
6198 char_u *p;
6199 char_u *q;
6200
6201 char_u *start;
Bram Moolenaar25648a52009-02-21 19:37:46 +00006202 char_u *end = NULL;
Bram Moolenaara850a712009-01-28 14:42:59 +00006203 char_u *ksp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006204 size_t len, totlen;
6205
6206 size_t split_len = 0;
6207 char_u *split_buf = NULL;
6208 ucmd_T *cmd;
6209#ifdef FEAT_EVAL
6210 scid_T save_current_SID = current_SID;
6211#endif
6212
6213 if (eap->cmdidx == CMD_USER)
6214 cmd = USER_CMD(eap->useridx);
6215 else
6216 cmd = USER_CMD_GA(&curbuf->b_ucmds, eap->useridx);
6217
6218 /*
6219 * Replace <> in the command by the arguments.
Bram Moolenaara850a712009-01-28 14:42:59 +00006220 * First round: "buf" is NULL, compute length, allocate "buf".
6221 * Second round: copy result into "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006222 */
6223 buf = NULL;
6224 for (;;)
6225 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006226 p = cmd->uc_rep; /* source */
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00006227 q = buf; /* destination */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006228 totlen = 0;
Bram Moolenaara850a712009-01-28 14:42:59 +00006229
6230 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006231 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006232 start = vim_strchr(p, '<');
6233 if (start != NULL)
6234 end = vim_strchr(start + 1, '>');
6235 if (buf != NULL)
6236 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006237 for (ksp = p; *ksp != NUL && *ksp != K_SPECIAL; ++ksp)
6238 ;
6239 if (*ksp == K_SPECIAL
6240 && (start == NULL || ksp < start || end == NULL)
Bram Moolenaara850a712009-01-28 14:42:59 +00006241 && ((ksp[1] == KS_SPECIAL && ksp[2] == KE_FILLER)
6242# ifdef FEAT_GUI
6243 || (ksp[1] == KS_EXTRA && ksp[2] == (int)KE_CSI)
6244# endif
6245 ))
6246 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006247 /* K_SPECIAL has been put in the buffer as K_SPECIAL
Bram Moolenaara850a712009-01-28 14:42:59 +00006248 * KS_SPECIAL KE_FILLER, like for mappings, but
6249 * do_cmdline() doesn't handle that, so convert it back.
6250 * Also change K_SPECIAL KS_EXTRA KE_CSI into CSI. */
6251 len = ksp - p;
6252 if (len > 0)
6253 {
6254 mch_memmove(q, p, len);
6255 q += len;
6256 }
6257 *q++ = ksp[1] == KS_SPECIAL ? K_SPECIAL : CSI;
6258 p = ksp + 3;
6259 continue;
6260 }
6261 }
6262
6263 /* break if there no <item> is found */
6264 if (start == NULL || end == NULL)
6265 break;
6266
Bram Moolenaar071d4272004-06-13 20:20:40 +00006267 /* Include the '>' */
6268 ++end;
6269
6270 /* Take everything up to the '<' */
6271 len = start - p;
6272 if (buf == NULL)
6273 totlen += len;
6274 else
6275 {
6276 mch_memmove(q, p, len);
6277 q += len;
6278 }
6279
6280 len = uc_check_code(start, end - start, q, cmd, eap,
6281 &split_buf, &split_len);
6282 if (len == (size_t)-1)
6283 {
6284 /* no match, continue after '<' */
6285 p = start + 1;
6286 len = 1;
6287 }
6288 else
6289 p = end;
6290 if (buf == NULL)
6291 totlen += len;
6292 else
6293 q += len;
6294 }
6295 if (buf != NULL) /* second time here, finished */
6296 {
6297 STRCPY(q, p);
6298 break;
6299 }
6300
6301 totlen += STRLEN(p); /* Add on the trailing characters */
6302 buf = alloc((unsigned)(totlen + 1));
6303 if (buf == NULL)
6304 {
6305 vim_free(split_buf);
6306 return;
6307 }
6308 }
6309
6310#ifdef FEAT_EVAL
6311 current_SID = cmd->uc_scriptID;
6312#endif
6313 (void)do_cmdline(buf, eap->getline, eap->cookie,
6314 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
6315#ifdef FEAT_EVAL
6316 current_SID = save_current_SID;
6317#endif
6318 vim_free(buf);
6319 vim_free(split_buf);
6320}
6321
6322# if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6323 static char_u *
6324get_user_command_name(idx)
6325 int idx;
6326{
6327 return get_user_commands(NULL, idx - (int)CMD_SIZE);
6328}
6329
6330/*
6331 * Function given to ExpandGeneric() to obtain the list of user command names.
6332 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006333 char_u *
6334get_user_commands(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006335 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006336 int idx;
6337{
6338 if (idx < curbuf->b_ucmds.ga_len)
6339 return USER_CMD_GA(&curbuf->b_ucmds, idx)->uc_name;
6340 idx -= curbuf->b_ucmds.ga_len;
6341 if (idx < ucmds.ga_len)
6342 return USER_CMD(idx)->uc_name;
6343 return NULL;
6344}
6345
6346/*
6347 * Function given to ExpandGeneric() to obtain the list of user command
6348 * attributes.
6349 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006350 char_u *
6351get_user_cmd_flags(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006352 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006353 int idx;
6354{
6355 static char *user_cmd_flags[] =
6356 {"bang", "bar", "buffer", "complete", "count",
6357 "nargs", "range", "register"};
6358
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00006359 if (idx >= (int)(sizeof(user_cmd_flags) / sizeof(user_cmd_flags[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006360 return NULL;
6361 return (char_u *)user_cmd_flags[idx];
6362}
6363
6364/*
6365 * Function given to ExpandGeneric() to obtain the list of values for -nargs.
6366 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006367 char_u *
6368get_user_cmd_nargs(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006369 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006370 int idx;
6371{
6372 static char *user_cmd_nargs[] = {"0", "1", "*", "?", "+"};
6373
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00006374 if (idx >= (int)(sizeof(user_cmd_nargs) / sizeof(user_cmd_nargs[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006375 return NULL;
6376 return (char_u *)user_cmd_nargs[idx];
6377}
6378
6379/*
6380 * Function given to ExpandGeneric() to obtain the list of values for -complete.
6381 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006382 char_u *
6383get_user_cmd_complete(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006384 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006385 int idx;
6386{
6387 return (char_u *)command_complete[idx].name;
6388}
6389# endif /* FEAT_CMDL_COMPL */
6390
6391#endif /* FEAT_USR_CMDS */
6392
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006393#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
6394/*
6395 * Parse a completion argument "value[vallen]".
6396 * The detected completion goes in "*complp", argument type in "*argt".
6397 * When there is an argument, for function and user defined completion, it's
6398 * copied to allocated memory and stored in "*compl_arg".
6399 * Returns FAIL if something is wrong.
6400 */
6401 int
6402parse_compl_arg(value, vallen, complp, argt, compl_arg)
6403 char_u *value;
6404 int vallen;
6405 int *complp;
6406 long *argt;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006407 char_u **compl_arg UNUSED;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006408{
6409 char_u *arg = NULL;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006410# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006411 size_t arglen = 0;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006412# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006413 int i;
6414 int valend = vallen;
6415
6416 /* Look for any argument part - which is the part after any ',' */
6417 for (i = 0; i < vallen; ++i)
6418 {
6419 if (value[i] == ',')
6420 {
6421 arg = &value[i + 1];
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006422# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006423 arglen = vallen - i - 1;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006424# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006425 valend = i;
6426 break;
6427 }
6428 }
6429
6430 for (i = 0; command_complete[i].expand != 0; ++i)
6431 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006432 if ((int)STRLEN(command_complete[i].name) == valend
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006433 && STRNCMP(value, command_complete[i].name, valend) == 0)
6434 {
6435 *complp = command_complete[i].expand;
6436 if (command_complete[i].expand == EXPAND_BUFFERS)
6437 *argt |= BUFNAME;
6438 else if (command_complete[i].expand == EXPAND_DIRECTORIES
6439 || command_complete[i].expand == EXPAND_FILES)
6440 *argt |= XFILE;
6441 break;
6442 }
6443 }
6444
6445 if (command_complete[i].expand == 0)
6446 {
6447 EMSG2(_("E180: Invalid complete value: %s"), value);
6448 return FAIL;
6449 }
6450
6451# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6452 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST
6453 && arg != NULL)
6454# else
6455 if (arg != NULL)
6456# endif
6457 {
6458 EMSG(_("E468: Completion argument only allowed for custom completion"));
6459 return FAIL;
6460 }
6461
6462# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6463 if ((*complp == EXPAND_USER_DEFINED || *complp == EXPAND_USER_LIST)
6464 && arg == NULL)
6465 {
6466 EMSG(_("E467: Custom completion requires a function argument"));
6467 return FAIL;
6468 }
6469
6470 if (arg != NULL)
6471 *compl_arg = vim_strnsave(arg, (int)arglen);
6472# endif
6473 return OK;
6474}
6475#endif
6476
Bram Moolenaar071d4272004-06-13 20:20:40 +00006477 static void
6478ex_colorscheme(eap)
6479 exarg_T *eap;
6480{
Bram Moolenaare6850792010-05-14 15:28:44 +02006481 if (*eap->arg == NUL)
6482 {
6483#ifdef FEAT_EVAL
6484 char_u *expr = vim_strsave((char_u *)"g:colors_name");
6485 char_u *p = NULL;
6486
6487 if (expr != NULL)
6488 {
6489 ++emsg_off;
6490 p = eval_to_string(expr, NULL, FALSE);
6491 --emsg_off;
6492 vim_free(expr);
6493 }
6494 if (p != NULL)
6495 {
6496 MSG(p);
6497 vim_free(p);
6498 }
6499 else
6500 MSG("default");
6501#else
6502 MSG(_("unknown"));
6503#endif
6504 }
6505 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarbe1e9e92012-09-18 16:47:07 +02006506 EMSG2(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006507}
6508
6509 static void
6510ex_highlight(eap)
6511 exarg_T *eap;
6512{
6513 if (*eap->arg == NUL && eap->cmd[2] == '!')
6514 MSG(_("Greetings, Vim user!"));
6515 do_highlight(eap->arg, eap->forceit, FALSE);
6516}
6517
6518
6519/*
6520 * Call this function if we thought we were going to exit, but we won't
6521 * (because of an error). May need to restore the terminal mode.
6522 */
6523 void
6524not_exiting()
6525{
6526 exiting = FALSE;
6527 settmode(TMODE_RAW);
6528}
6529
6530/*
6531 * ":quit": quit current window, quit Vim if closed the last window.
6532 */
6533 static void
6534ex_quit(eap)
6535 exarg_T *eap;
6536{
6537#ifdef FEAT_CMDWIN
6538 if (cmdwin_type != 0)
6539 {
6540 cmdwin_result = Ctrl_C;
6541 return;
6542 }
6543#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006544 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006545 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006546 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006547 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006548 return;
6549 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006550#ifdef FEAT_AUTOCMD
Bram Moolenaar3b53dfb2012-06-06 18:03:07 +02006551 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01006552 /* Refuse to quit when locked or when the buffer in the last window is
Bram Moolenaar362ce482012-06-06 19:02:45 +02006553 * being closed (can only happen in autocommands). */
6554 if (curbuf_locked() || (curbuf->b_nwindows == 1 && curbuf->b_closing))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006555 return;
6556#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006557
6558#ifdef FEAT_NETBEANS_INTG
6559 netbeansForcedQuit = eap->forceit;
6560#endif
6561
6562 /*
6563 * If there are more files or windows we won't exit.
6564 */
6565 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6566 exiting = TRUE;
6567 if ((!P_HID(curbuf)
6568 && check_changed(curbuf, p_awa, FALSE, eap->forceit, FALSE))
6569 || check_more(TRUE, eap->forceit) == FAIL
6570 || (only_one_window() && check_changed_any(eap->forceit)))
6571 {
6572 not_exiting();
6573 }
6574 else
6575 {
6576#ifdef FEAT_WINDOWS
6577 if (only_one_window()) /* quit last window */
6578#endif
6579 getout(0);
6580#ifdef FEAT_WINDOWS
6581# ifdef FEAT_GUI
6582 need_mouse_correct = TRUE;
6583# endif
6584 /* close window; may free buffer */
6585 win_close(curwin, !P_HID(curwin->w_buffer) || eap->forceit);
6586#endif
6587 }
6588}
6589
6590/*
6591 * ":cquit".
6592 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006593 static void
6594ex_cquit(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006595 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006596{
6597 getout(1); /* this does not always pass on the exit code to the Manx
6598 compiler. why? */
6599}
6600
6601/*
6602 * ":qall": try to quit all windows
6603 */
6604 static void
6605ex_quit_all(eap)
6606 exarg_T *eap;
6607{
6608# ifdef FEAT_CMDWIN
6609 if (cmdwin_type != 0)
6610 {
6611 if (eap->forceit)
6612 cmdwin_result = K_XF1; /* ex_window() takes care of this */
6613 else
6614 cmdwin_result = K_XF2;
6615 return;
6616 }
6617# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006618
6619 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006620 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006621 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006622 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006623 return;
6624 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006625#ifdef FEAT_AUTOCMD
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01006626 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
6627 /* Refuse to quit when locked or when the buffer in the last window is
6628 * being closed (can only happen in autocommands). */
6629 if (curbuf_locked() || (curbuf->b_nwindows == 1 && curbuf->b_closing))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006630 return;
6631#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006632
Bram Moolenaar071d4272004-06-13 20:20:40 +00006633 exiting = TRUE;
6634 if (eap->forceit || !check_changed_any(FALSE))
6635 getout(0);
6636 not_exiting();
6637}
6638
Bram Moolenaard9967712006-03-11 21:18:15 +00006639#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006640/*
6641 * ":close": close current window, unless it is the last one
6642 */
6643 static void
6644ex_close(eap)
6645 exarg_T *eap;
6646{
6647# ifdef FEAT_CMDWIN
6648 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02006649 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006650 else
6651# endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006652 if (!text_locked()
6653#ifdef FEAT_AUTOCMD
6654 && !curbuf_locked()
6655#endif
6656 )
Bram Moolenaarf740b292006-02-16 22:11:02 +00006657 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006658}
6659
Bram Moolenaard9967712006-03-11 21:18:15 +00006660# ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006661/*
6662 * ":pclose": Close any preview window.
6663 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006664 static void
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006665ex_pclose(eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006666 exarg_T *eap;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006667{
6668 win_T *win;
6669
6670 for (win = firstwin; win != NULL; win = win->w_next)
6671 if (win->w_p_pvw)
6672 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006673 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006674 break;
6675 }
6676}
Bram Moolenaard9967712006-03-11 21:18:15 +00006677# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006678
Bram Moolenaarf740b292006-02-16 22:11:02 +00006679/*
6680 * Close window "win" and take care of handling closing the last window for a
6681 * modified buffer.
6682 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006683 static void
Bram Moolenaarf740b292006-02-16 22:11:02 +00006684ex_win_close(forceit, win, tp)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006685 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006686 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006687 tabpage_T *tp; /* NULL or the tab page "win" is in */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006688{
6689 int need_hide;
6690 buf_T *buf = win->w_buffer;
6691
6692 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006693 if (need_hide && !P_HID(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006694 {
Bram Moolenaard9967712006-03-11 21:18:15 +00006695# if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006696 if ((p_confirm || cmdmod.confirm) && p_write)
6697 {
6698 dialog_changed(buf, FALSE);
6699 if (buf_valid(buf) && bufIsChanged(buf))
6700 return;
6701 need_hide = FALSE;
6702 }
6703 else
Bram Moolenaard9967712006-03-11 21:18:15 +00006704# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006705 {
6706 EMSG(_(e_nowrtmsg));
6707 return;
6708 }
6709 }
6710
Bram Moolenaard9967712006-03-11 21:18:15 +00006711# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006712 need_mouse_correct = TRUE;
Bram Moolenaard9967712006-03-11 21:18:15 +00006713# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006714
Bram Moolenaar071d4272004-06-13 20:20:40 +00006715 /* free buffer when not hiding it or when it's a scratch buffer */
Bram Moolenaarf740b292006-02-16 22:11:02 +00006716 if (tp == NULL)
6717 win_close(win, !need_hide && !P_HID(buf));
6718 else
6719 win_close_othertab(win, !need_hide && !P_HID(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006720}
6721
Bram Moolenaar071d4272004-06-13 20:20:40 +00006722/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006723 * ":tabclose": close current tab page, unless it is the last one.
6724 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006725 */
6726 static void
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006727ex_tabclose(eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006728 exarg_T *eap;
6729{
Bram Moolenaarf740b292006-02-16 22:11:02 +00006730 tabpage_T *tp;
6731
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006732# ifdef FEAT_CMDWIN
6733 if (cmdwin_type != 0)
6734 cmdwin_result = K_IGNORE;
6735 else
6736# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006737 if (first_tabpage->tp_next == NULL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00006738 EMSG(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00006739 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006740 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006741 if (eap->addr_count > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006742 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006743 tp = find_tabpage((int)eap->line2);
6744 if (tp == NULL)
6745 {
6746 beep_flush();
6747 return;
6748 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00006749 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006750 {
6751 tabpage_close_other(tp, eap->forceit);
6752 return;
6753 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006754 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006755 if (!text_locked()
6756#ifdef FEAT_AUTOCMD
6757 && !curbuf_locked()
6758#endif
6759 )
Bram Moolenaarf740b292006-02-16 22:11:02 +00006760 tabpage_close(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006761 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006762}
6763
6764/*
6765 * ":tabonly": close all tab pages except the current one
6766 */
6767 static void
6768ex_tabonly(eap)
6769 exarg_T *eap;
6770{
6771 tabpage_T *tp;
6772 int done;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006773
6774# ifdef FEAT_CMDWIN
6775 if (cmdwin_type != 0)
6776 cmdwin_result = K_IGNORE;
6777 else
6778# endif
6779 if (first_tabpage->tp_next == NULL)
6780 MSG(_("Already only one tab page"));
6781 else
6782 {
6783 /* Repeat this up to a 1000 times, because autocommands may mess
6784 * up the lists. */
6785 for (done = 0; done < 1000; ++done)
6786 {
6787 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6788 if (tp->tp_topframe != topframe)
6789 {
6790 tabpage_close_other(tp, eap->forceit);
6791 /* if we failed to close it quit */
6792 if (valid_tabpage(tp))
6793 done = 1000;
6794 /* start over, "tp" is now invalid */
6795 break;
6796 }
6797 if (first_tabpage->tp_next == NULL)
6798 break;
6799 }
6800 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006801}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006802
6803/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006804 * Close the current tab page.
6805 */
6806 void
6807tabpage_close(forceit)
6808 int forceit;
6809{
6810 /* First close all the windows but the current one. If that worked then
6811 * close the last window in this tab, that will close it. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006812 if (lastwin != firstwin)
6813 close_others(TRUE, forceit);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006814 if (lastwin == firstwin)
6815 ex_win_close(forceit, curwin, NULL);
6816# ifdef FEAT_GUI
6817 need_mouse_correct = TRUE;
6818# endif
6819}
6820
6821/*
6822 * Close tab page "tp", which is not the current tab page.
6823 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006824 * Also takes care of the tab pages line disappearing when closing the
6825 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00006826 */
6827 void
6828tabpage_close_other(tp, forceit)
6829 tabpage_T *tp;
6830 int forceit;
6831{
6832 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006833 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006834 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006835
6836 /* Limit to 1000 windows, autocommands may add a window while we close
6837 * one. OK, so I'm paranoid... */
6838 while (++done < 1000)
6839 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006840 wp = tp->tp_firstwin;
6841 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006842
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006843 /* Autocommands may delete the tab page under our fingers and we may
6844 * fail to close a window with a modified buffer. */
6845 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006846 break;
6847 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006848
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006849 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006850 if (h != tabline_height())
6851 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006852}
6853
6854/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006855 * ":only".
6856 */
6857 static void
6858ex_only(eap)
6859 exarg_T *eap;
6860{
6861# ifdef FEAT_GUI
6862 need_mouse_correct = TRUE;
6863# endif
6864 close_others(TRUE, eap->forceit);
6865}
6866
6867/*
6868 * ":all" and ":sall".
Bram Moolenaard9967712006-03-11 21:18:15 +00006869 * Also used for ":tab drop file ..." after setting the argument list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006870 */
Bram Moolenaard9967712006-03-11 21:18:15 +00006871 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00006872ex_all(eap)
6873 exarg_T *eap;
6874{
6875 if (eap->addr_count == 0)
6876 eap->line2 = 9999;
Bram Moolenaard9967712006-03-11 21:18:15 +00006877 do_arg_all((int)eap->line2, eap->forceit, eap->cmdidx == CMD_drop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006878}
6879#endif /* FEAT_WINDOWS */
6880
6881 static void
6882ex_hide(eap)
6883 exarg_T *eap;
6884{
6885 if (*eap->arg != NUL && check_nextcmd(eap->arg) == NULL)
6886 eap->errmsg = e_invarg;
6887 else
6888 {
6889 /* ":hide" or ":hide | cmd": hide current window */
6890 eap->nextcmd = check_nextcmd(eap->arg);
6891#ifdef FEAT_WINDOWS
6892 if (!eap->skip)
6893 {
6894# ifdef FEAT_GUI
6895 need_mouse_correct = TRUE;
6896# endif
6897 win_close(curwin, FALSE); /* don't free buffer */
6898 }
6899#endif
6900 }
6901}
6902
6903/*
6904 * ":stop" and ":suspend": Suspend Vim.
6905 */
6906 static void
6907ex_stop(eap)
6908 exarg_T *eap;
6909{
6910 /*
6911 * Disallow suspending for "rvim".
6912 */
6913 if (!check_restricted()
6914#ifdef WIN3264
6915 /*
6916 * Check if external commands are allowed now.
6917 */
6918 && can_end_termcap_mode(TRUE)
6919#endif
6920 )
6921 {
6922 if (!eap->forceit)
6923 autowrite_all();
6924 windgoto((int)Rows - 1, 0);
6925 out_char('\n');
6926 out_flush();
6927 stoptermcap();
6928 out_flush(); /* needed for SUN to restore xterm buffer */
6929#ifdef FEAT_TITLE
6930 mch_restore_title(3); /* restore window titles */
6931#endif
6932 ui_suspend(); /* call machine specific function */
6933#ifdef FEAT_TITLE
6934 maketitle();
6935 resettitle(); /* force updating the title */
6936#endif
6937 starttermcap();
6938 scroll_start(); /* scroll screen before redrawing */
6939 redraw_later_clear();
6940 shell_resized(); /* may have resized window */
6941 }
6942}
6943
6944/*
6945 * ":exit", ":xit" and ":wq": Write file and exit Vim.
6946 */
6947 static void
6948ex_exit(eap)
6949 exarg_T *eap;
6950{
6951#ifdef FEAT_CMDWIN
6952 if (cmdwin_type != 0)
6953 {
6954 cmdwin_result = Ctrl_C;
6955 return;
6956 }
6957#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006958 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006959 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006960 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006961 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006962 return;
6963 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006964#ifdef FEAT_AUTOCMD
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01006965 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
6966 /* Refuse to quit when locked or when the buffer in the last window is
6967 * being closed (can only happen in autocommands). */
6968 if (curbuf_locked() || (curbuf->b_nwindows == 1 && curbuf->b_closing))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006969 return;
6970#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006971
6972 /*
6973 * if more files or windows we won't exit
6974 */
6975 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6976 exiting = TRUE;
6977 if ( ((eap->cmdidx == CMD_wq
6978 || curbufIsChanged())
6979 && do_write(eap) == FAIL)
6980 || check_more(TRUE, eap->forceit) == FAIL
6981 || (only_one_window() && check_changed_any(eap->forceit)))
6982 {
6983 not_exiting();
6984 }
6985 else
6986 {
6987#ifdef FEAT_WINDOWS
6988 if (only_one_window()) /* quit last window, exit Vim */
6989#endif
6990 getout(0);
6991#ifdef FEAT_WINDOWS
6992# ifdef FEAT_GUI
6993 need_mouse_correct = TRUE;
6994# endif
6995 /* quit current window, may free buffer */
6996 win_close(curwin, !P_HID(curwin->w_buffer));
6997#endif
6998 }
6999}
7000
7001/*
7002 * ":print", ":list", ":number".
7003 */
7004 static void
7005ex_print(eap)
7006 exarg_T *eap;
7007{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007008 if (curbuf->b_ml.ml_flags & ML_EMPTY)
7009 EMSG(_(e_emptybuf));
7010 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007011 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007012 for ( ;!got_int; ui_breakcheck())
7013 {
7014 print_line(eap->line1,
7015 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
7016 || (eap->flags & EXFLAG_NR)),
7017 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
7018 if (++eap->line1 > eap->line2)
7019 break;
7020 out_flush(); /* show one line at a time */
7021 }
7022 setpcmark();
7023 /* put cursor at last line */
7024 curwin->w_cursor.lnum = eap->line2;
7025 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007026 }
7027
Bram Moolenaar071d4272004-06-13 20:20:40 +00007028 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007029}
7030
7031#ifdef FEAT_BYTEOFF
7032 static void
7033ex_goto(eap)
7034 exarg_T *eap;
7035{
7036 goto_byte(eap->line2);
7037}
7038#endif
7039
7040/*
7041 * ":shell".
7042 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007043 static void
7044ex_shell(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007045 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007046{
7047 do_shell(NULL, 0);
7048}
7049
7050#if (defined(FEAT_WINDOWS) && defined(HAVE_DROP_FILE)) \
7051 || (defined(FEAT_GUI_GTK) && defined(FEAT_DND)) \
Bram Moolenaar371baa92005-12-29 22:43:53 +00007052 || defined(FEAT_GUI_MSWIN) \
7053 || defined(FEAT_GUI_MAC) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00007054 || defined(PROTO)
7055
7056/*
7057 * Handle a file drop. The code is here because a drop is *nearly* like an
7058 * :args command, but not quite (we have a list of exact filenames, so we
7059 * don't want to (a) parse a command line, or (b) expand wildcards. So the
7060 * code is very similar to :args and hence needs access to a lot of the static
7061 * functions in this file.
7062 *
7063 * The list should be allocated using alloc(), as should each item in the
7064 * list. This function takes over responsibility for freeing the list.
7065 *
Bram Moolenaar81870892007-11-11 18:17:28 +00007066 * XXX The list is made into the argument list. This is freed using
Bram Moolenaar071d4272004-06-13 20:20:40 +00007067 * FreeWild(), which does a series of vim_free() calls, unless the two defines
7068 * __EMX__ and __ALWAYS_HAS_TRAILING_NUL_POINTER are set. In this case, a
7069 * routine _fnexplodefree() is used. This may cause problems, but as the drop
7070 * file functionality is (currently) not in EMX this is not presently a
7071 * problem.
7072 */
7073 void
7074handle_drop(filec, filev, split)
7075 int filec; /* the number of files dropped */
7076 char_u **filev; /* the list of files dropped */
7077 int split; /* force splitting the window */
7078{
7079 exarg_T ea;
7080 int save_msg_scroll = msg_scroll;
7081
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007082 /* Postpone this while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007083 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007084 return;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007085#ifdef FEAT_AUTOCMD
7086 if (curbuf_locked())
7087 return;
7088#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00007089 /* When the screen is being updated we should not change buffers and
7090 * windows structures, it may cause freed memory to be used. */
7091 if (updating_screen)
7092 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007093
7094 /* Check whether the current buffer is changed. If so, we will need
7095 * to split the current window or data could be lost.
7096 * We don't need to check if the 'hidden' option is set, as in this
7097 * case the buffer won't be lost.
7098 */
7099 if (!P_HID(curbuf) && !split)
7100 {
7101 ++emsg_off;
7102 split = check_changed(curbuf, TRUE, FALSE, FALSE, FALSE);
7103 --emsg_off;
7104 }
7105 if (split)
7106 {
7107# ifdef FEAT_WINDOWS
7108 if (win_split(0, 0) == FAIL)
7109 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007110 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007111
7112 /* When splitting the window, create a new alist. Otherwise the
7113 * existing one is overwritten. */
7114 alist_unlink(curwin->w_alist);
7115 alist_new();
7116# else
7117 return; /* can't split, always fail */
7118# endif
7119 }
7120
7121 /*
7122 * Set up the new argument list.
7123 */
Bram Moolenaar86b68352004-12-27 21:59:20 +00007124 alist_set(ALIST(curwin), filec, filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007125
7126 /*
7127 * Move to the first file.
7128 */
7129 /* Fake up a minimal "next" command for do_argfile() */
7130 vim_memset(&ea, 0, sizeof(ea));
7131 ea.cmd = (char_u *)"next";
7132 do_argfile(&ea, 0);
7133
7134 /* do_ecmd() may set need_start_insertmode, but since we never left Insert
7135 * mode that is not needed here. */
7136 need_start_insertmode = FALSE;
7137
7138 /* Restore msg_scroll, otherwise a following command may cause scrolling
7139 * unexpectedly. The screen will be redrawn by the caller, thus
7140 * msg_scroll being set by displaying a message is irrelevant. */
7141 msg_scroll = save_msg_scroll;
7142}
7143#endif
7144
Bram Moolenaar071d4272004-06-13 20:20:40 +00007145/*
7146 * Clear an argument list: free all file names and reset it to zero entries.
7147 */
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007148 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00007149alist_clear(al)
7150 alist_T *al;
7151{
7152 while (--al->al_ga.ga_len >= 0)
7153 vim_free(AARGLIST(al)[al->al_ga.ga_len].ae_fname);
7154 ga_clear(&al->al_ga);
7155}
7156
7157/*
7158 * Init an argument list.
7159 */
7160 void
7161alist_init(al)
7162 alist_T *al;
7163{
7164 ga_init2(&al->al_ga, (int)sizeof(aentry_T), 5);
7165}
7166
7167#if defined(FEAT_WINDOWS) || defined(PROTO)
7168
7169/*
7170 * Remove a reference from an argument list.
7171 * Ignored when the argument list is the global one.
7172 * If the argument list is no longer used by any window, free it.
7173 */
7174 void
7175alist_unlink(al)
7176 alist_T *al;
7177{
7178 if (al != &global_alist && --al->al_refcount <= 0)
7179 {
7180 alist_clear(al);
7181 vim_free(al);
7182 }
7183}
7184
7185# if defined(FEAT_LISTCMDS) || defined(HAVE_DROP_FILE) || defined(PROTO)
7186/*
7187 * Create a new argument list and use it for the current window.
7188 */
7189 void
7190alist_new()
7191{
7192 curwin->w_alist = (alist_T *)alloc((unsigned)sizeof(alist_T));
7193 if (curwin->w_alist == NULL)
7194 {
7195 curwin->w_alist = &global_alist;
7196 ++global_alist.al_refcount;
7197 }
7198 else
7199 {
7200 curwin->w_alist->al_refcount = 1;
7201 alist_init(curwin->w_alist);
7202 }
7203}
7204# endif
7205#endif
7206
7207#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE) || defined(PROTO)
7208/*
7209 * Expand the file names in the global argument list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00007210 * If "fnum_list" is not NULL, use "fnum_list[fnum_len]" as a list of buffer
7211 * numbers to be re-used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007212 */
7213 void
Bram Moolenaar86b68352004-12-27 21:59:20 +00007214alist_expand(fnum_list, fnum_len)
7215 int *fnum_list;
7216 int fnum_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007217{
7218 char_u **old_arg_files;
Bram Moolenaar86b68352004-12-27 21:59:20 +00007219 int old_arg_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007220 char_u **new_arg_files;
7221 int new_arg_file_count;
7222 char_u *save_p_su = p_su;
7223 int i;
7224
7225 /* Don't use 'suffixes' here. This should work like the shell did the
7226 * expansion. Also, the vimrc file isn't read yet, thus the user
7227 * can't set the options. */
7228 p_su = empty_option;
7229 old_arg_files = (char_u **)alloc((unsigned)(sizeof(char_u *) * GARGCOUNT));
7230 if (old_arg_files != NULL)
7231 {
7232 for (i = 0; i < GARGCOUNT; ++i)
Bram Moolenaar86b68352004-12-27 21:59:20 +00007233 old_arg_files[i] = vim_strsave(GARGLIST[i].ae_fname);
7234 old_arg_count = GARGCOUNT;
7235 if (expand_wildcards(old_arg_count, old_arg_files,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007236 &new_arg_file_count, &new_arg_files,
Bram Moolenaarb5609832011-07-20 15:04:58 +02007237 EW_FILE|EW_NOTFOUND|EW_ADDSLASH|EW_NOERROR) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007238 && new_arg_file_count > 0)
7239 {
Bram Moolenaar86b68352004-12-27 21:59:20 +00007240 alist_set(&global_alist, new_arg_file_count, new_arg_files,
7241 TRUE, fnum_list, fnum_len);
7242 FreeWild(old_arg_count, old_arg_files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007243 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007244 }
7245 p_su = save_p_su;
7246}
7247#endif
7248
7249/*
7250 * Set the argument list for the current window.
7251 * Takes over the allocated files[] and the allocated fnames in it.
7252 */
7253 void
Bram Moolenaar86b68352004-12-27 21:59:20 +00007254alist_set(al, count, files, use_curbuf, fnum_list, fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007255 alist_T *al;
7256 int count;
7257 char_u **files;
7258 int use_curbuf;
Bram Moolenaar86b68352004-12-27 21:59:20 +00007259 int *fnum_list;
7260 int fnum_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007261{
7262 int i;
7263
7264 alist_clear(al);
7265 if (ga_grow(&al->al_ga, count) == OK)
7266 {
7267 for (i = 0; i < count; ++i)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007268 {
7269 if (got_int)
7270 {
7271 /* When adding many buffers this can take a long time. Allow
7272 * interrupting here. */
7273 while (i < count)
7274 vim_free(files[i++]);
7275 break;
7276 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00007277
7278 /* May set buffer name of a buffer previously used for the
7279 * argument list, so that it's re-used by alist_add. */
7280 if (fnum_list != NULL && i < fnum_len)
7281 buf_set_name(fnum_list[i], files[i]);
7282
Bram Moolenaar071d4272004-06-13 20:20:40 +00007283 alist_add(al, files[i], use_curbuf ? 2 : 1);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007284 ui_breakcheck();
7285 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007286 vim_free(files);
7287 }
7288 else
7289 FreeWild(count, files);
7290#ifdef FEAT_WINDOWS
7291 if (al == &global_alist)
7292#endif
7293 arg_had_last = FALSE;
7294}
7295
7296/*
7297 * Add file "fname" to argument list "al".
7298 * "fname" must have been allocated and "al" must have been checked for room.
7299 */
7300 void
7301alist_add(al, fname, set_fnum)
7302 alist_T *al;
7303 char_u *fname;
7304 int set_fnum; /* 1: set buffer number; 2: re-use curbuf */
7305{
7306 if (fname == NULL) /* don't add NULL file names */
7307 return;
7308#ifdef BACKSLASH_IN_FILENAME
7309 slash_adjust(fname);
7310#endif
7311 AARGLIST(al)[al->al_ga.ga_len].ae_fname = fname;
7312 if (set_fnum > 0)
7313 AARGLIST(al)[al->al_ga.ga_len].ae_fnum =
7314 buflist_add(fname, BLN_LISTED | (set_fnum == 2 ? BLN_CURBUF : 0));
7315 ++al->al_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007316}
7317
7318#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
7319/*
7320 * Adjust slashes in file names. Called after 'shellslash' was set.
7321 */
7322 void
7323alist_slash_adjust()
7324{
7325 int i;
7326# ifdef FEAT_WINDOWS
7327 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007328 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007329# endif
7330
7331 for (i = 0; i < GARGCOUNT; ++i)
7332 if (GARGLIST[i].ae_fname != NULL)
7333 slash_adjust(GARGLIST[i].ae_fname);
7334# ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00007335 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007336 if (wp->w_alist != &global_alist)
7337 for (i = 0; i < WARGCOUNT(wp); ++i)
7338 if (WARGLIST(wp)[i].ae_fname != NULL)
7339 slash_adjust(WARGLIST(wp)[i].ae_fname);
7340# endif
7341}
7342#endif
7343
7344/*
7345 * ":preserve".
7346 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007347 static void
7348ex_preserve(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007349 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007350{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007351 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007352 ml_preserve(curbuf, TRUE);
7353}
7354
7355/*
7356 * ":recover".
7357 */
7358 static void
7359ex_recover(eap)
7360 exarg_T *eap;
7361{
7362 /* Set recoverymode right away to avoid the ATTENTION prompt. */
7363 recoverymode = TRUE;
7364 if (!check_changed(curbuf, p_awa, TRUE, eap->forceit, FALSE)
7365 && (*eap->arg == NUL
7366 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
7367 ml_recover();
7368 recoverymode = FALSE;
7369}
7370
7371/*
7372 * Command modifier used in a wrong way.
7373 */
7374 static void
7375ex_wrongmodifier(eap)
7376 exarg_T *eap;
7377{
7378 eap->errmsg = e_invcmd;
7379}
7380
7381#ifdef FEAT_WINDOWS
7382/*
7383 * :sview [+command] file split window with new file, read-only
7384 * :split [[+command] file] split window with current or new file
7385 * :vsplit [[+command] file] split window vertically with current or new file
7386 * :new [[+command] file] split window with no or new file
7387 * :vnew [[+command] file] split vertically window with no or new file
7388 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007389 *
7390 * :tabedit open new Tab page with empty window
7391 * :tabedit [+command] file open new Tab page and edit "file"
7392 * :tabnew [[+command] file] just like :tabedit
7393 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007394 */
7395 void
7396ex_splitview(eap)
7397 exarg_T *eap;
7398{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007399 win_T *old_curwin = curwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007400# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007401 char_u *fname = NULL;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007402# endif
7403# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007404 int browse_flag = cmdmod.browse;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007405# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007406
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007407# ifndef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007408 if (eap->cmdidx == CMD_vsplit || eap->cmdidx == CMD_vnew)
7409 {
7410 ex_ni(eap);
7411 return;
7412 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007413# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007414
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007415# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007416 need_mouse_correct = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007417# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007418
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007419# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00007420 /* A ":split" in the quickfix window works like ":new". Don't want two
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007421 * quickfix windows. But it's OK when doing ":tab split". */
7422 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007423 {
7424 if (eap->cmdidx == CMD_split)
7425 eap->cmdidx = CMD_new;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007426# ifdef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007427 if (eap->cmdidx == CMD_vsplit)
7428 eap->cmdidx = CMD_vnew;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007429# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007430 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007431# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007432
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007433# ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007434 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007435 {
7436 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
7437 FNAME_MESS, TRUE, curbuf->b_ffname);
7438 if (fname == NULL)
7439 goto theend;
7440 eap->arg = fname;
7441 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007442# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007443 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007444# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007445# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007446# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007447 if (cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007448# ifdef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007449 && eap->cmdidx != CMD_vnew
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007450# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007451 && eap->cmdidx != CMD_new)
7452 {
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007453# ifdef FEAT_AUTOCMD
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007454 if (
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007455# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007456 !gui.in_use &&
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007457# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007458 au_has_group((char_u *)"FileExplorer"))
7459 {
7460 /* No browsing supported but we do have the file explorer:
7461 * Edit the directory. */
7462 if (*eap->arg == NUL || !mch_isdir(eap->arg))
7463 eap->arg = (char_u *)".";
7464 }
7465 else
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007466# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007467 {
7468 fname = do_browse(0, (char_u *)_("Edit File in new window"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007469 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007470 if (fname == NULL)
7471 goto theend;
7472 eap->arg = fname;
7473 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007474 }
7475 cmdmod.browse = FALSE; /* Don't browse again in do_ecmd(). */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007476# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007477
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007478 /*
7479 * Either open new tab page or split the window.
7480 */
7481 if (eap->cmdidx == CMD_tabedit
7482 || eap->cmdidx == CMD_tabfind
7483 || eap->cmdidx == CMD_tabnew)
7484 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00007485 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
7486 : eap->addr_count == 0 ? 0
7487 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007488 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00007489 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007490
7491 /* set the alternate buffer for the window we came from */
7492 if (curwin != old_curwin
7493 && win_valid(old_curwin)
7494 && old_curwin->w_buffer != curbuf
7495 && !cmdmod.keepalt)
7496 old_curwin->w_alt_fnum = curbuf->b_fnum;
7497 }
7498 }
7499 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007500 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
7501 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007502# ifdef FEAT_SCROLLBIND
Bram Moolenaar071d4272004-06-13 20:20:40 +00007503 /* Reset 'scrollbind' when editing another file, but keep it when
7504 * doing ":split" without arguments. */
7505 if (*eap->arg != NUL
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007506# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007507 || cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007508# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007509 )
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007510 {
7511 RESET_BINDING(curwin);
7512 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007513 else
7514 do_check_scrollbind(FALSE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007515# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007516 do_exedit(eap, old_curwin);
7517 }
7518
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007519# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007520 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007521# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007522
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007523# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007524theend:
7525 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007526# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007527}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007528
7529/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007530 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007531 */
7532 void
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007533tabpage_new()
7534{
7535 exarg_T ea;
7536
7537 vim_memset(&ea, 0, sizeof(ea));
7538 ea.cmdidx = CMD_tabnew;
7539 ea.cmd = (char_u *)"tabn";
7540 ea.arg = (char_u *)"";
7541 ex_splitview(&ea);
7542}
7543
7544/*
7545 * :tabnext command
7546 */
7547 static void
7548ex_tabnext(eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007549 exarg_T *eap;
7550{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007551 switch (eap->cmdidx)
7552 {
7553 case CMD_tabfirst:
7554 case CMD_tabrewind:
7555 goto_tabpage(1);
7556 break;
7557 case CMD_tablast:
7558 goto_tabpage(9999);
7559 break;
7560 case CMD_tabprevious:
7561 case CMD_tabNext:
7562 goto_tabpage(eap->addr_count == 0 ? -1 : -(int)eap->line2);
7563 break;
7564 default: /* CMD_tabnext */
7565 goto_tabpage(eap->addr_count == 0 ? 0 : (int)eap->line2);
7566 break;
7567 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007568}
7569
7570/*
7571 * :tabmove command
7572 */
7573 static void
7574ex_tabmove(eap)
7575 exarg_T *eap;
7576{
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02007577 int tab_number = 9999;
7578
7579 if (eap->arg && *eap->arg != NUL)
7580 {
7581 char_u *p = eap->arg;
7582 int relative = 0; /* argument +N/-N means: move N places to the
7583 * right/left relative to the current position. */
7584
7585 if (*eap->arg == '-')
7586 {
7587 relative = -1;
7588 p = eap->arg + 1;
7589 }
7590 else if (*eap->arg == '+')
7591 {
7592 relative = 1;
7593 p = eap->arg + 1;
7594 }
7595 else
7596 p = eap->arg;
7597
7598 if (p == skipdigits(p))
7599 {
7600 /* No numbers as argument. */
7601 eap->errmsg = e_invarg;
7602 return;
7603 }
7604
7605 tab_number = getdigits(&p);
7606 if (relative != 0)
7607 tab_number = tab_number * relative + tabpage_index(curtab) - 1;;
7608 }
7609 else if (eap->addr_count != 0)
7610 tab_number = eap->line2;
7611
7612 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007613}
7614
7615/*
7616 * :tabs command: List tabs and their contents.
7617 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007618 static void
7619ex_tabs(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007620 exarg_T *eap UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007621{
7622 tabpage_T *tp;
7623 win_T *wp;
7624 int tabcount = 1;
7625
7626 msg_start();
7627 msg_scroll = TRUE;
7628 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
7629 {
7630 msg_putchar('\n');
7631 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
7632 msg_outtrans_attr(IObuff, hl_attr(HLF_T));
7633 out_flush(); /* output one line at a time */
7634 ui_breakcheck();
7635
Bram Moolenaar030f0df2006-02-21 22:02:53 +00007636 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007637 wp = firstwin;
7638 else
7639 wp = tp->tp_firstwin;
7640 for ( ; wp != NULL && !got_int; wp = wp->w_next)
7641 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007642 msg_putchar('\n');
7643 msg_putchar(wp == curwin ? '>' : ' ');
7644 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007645 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
7646 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007647 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02007648 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007649 else
7650 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
7651 IObuff, IOSIZE, TRUE);
7652 msg_outtrans(IObuff);
7653 out_flush(); /* output one line at a time */
7654 ui_breakcheck();
7655 }
7656 }
7657}
7658
7659#endif /* FEAT_WINDOWS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007660
7661/*
7662 * ":mode": Set screen mode.
7663 * If no argument given, just get the screen size and redraw.
7664 */
7665 static void
7666ex_mode(eap)
7667 exarg_T *eap;
7668{
7669 if (*eap->arg == NUL)
7670 shell_resized();
7671 else
7672 mch_screenmode(eap->arg);
7673}
7674
7675#ifdef FEAT_WINDOWS
7676/*
7677 * ":resize".
7678 * set, increment or decrement current window height
7679 */
7680 static void
7681ex_resize(eap)
7682 exarg_T *eap;
7683{
7684 int n;
7685 win_T *wp = curwin;
7686
7687 if (eap->addr_count > 0)
7688 {
7689 n = eap->line2;
7690 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
7691 ;
7692 }
7693
7694#ifdef FEAT_GUI
7695 need_mouse_correct = TRUE;
7696#endif
7697 n = atol((char *)eap->arg);
7698#ifdef FEAT_VERTSPLIT
7699 if (cmdmod.split & WSP_VERT)
7700 {
7701 if (*eap->arg == '-' || *eap->arg == '+')
7702 n += W_WIDTH(curwin);
7703 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
7704 n = 9999;
7705 win_setwidth_win((int)n, wp);
7706 }
7707 else
7708#endif
7709 {
7710 if (*eap->arg == '-' || *eap->arg == '+')
7711 n += curwin->w_height;
7712 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
7713 n = 9999;
7714 win_setheight_win((int)n, wp);
7715 }
7716}
7717#endif
7718
7719/*
7720 * ":find [+command] <file>" command.
7721 */
7722 static void
7723ex_find(eap)
7724 exarg_T *eap;
7725{
7726#ifdef FEAT_SEARCHPATH
7727 char_u *fname;
7728 int count;
7729
7730 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
7731 TRUE, curbuf->b_ffname);
7732 if (eap->addr_count > 0)
7733 {
7734 /* Repeat finding the file "count" times. This matters when it
7735 * appears several times in the path. */
7736 count = eap->line2;
7737 while (fname != NULL && --count > 0)
7738 {
7739 vim_free(fname);
7740 fname = find_file_in_path(NULL, 0, FNAME_MESS,
7741 FALSE, curbuf->b_ffname);
7742 }
7743 }
7744
7745 if (fname != NULL)
7746 {
7747 eap->arg = fname;
7748#endif
7749 do_exedit(eap, NULL);
7750#ifdef FEAT_SEARCHPATH
7751 vim_free(fname);
7752 }
7753#endif
7754}
7755
7756/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00007757 * ":open" simulation: for now just work like ":visual".
7758 */
7759 static void
7760ex_open(eap)
7761 exarg_T *eap;
7762{
7763 regmatch_T regmatch;
7764 char_u *p;
7765
7766 curwin->w_cursor.lnum = eap->line2;
7767 beginline(BL_SOL | BL_FIX);
7768 if (*eap->arg == '/')
7769 {
7770 /* ":open /pattern/": put cursor in column found with pattern */
7771 ++eap->arg;
7772 p = skip_regexp(eap->arg, '/', p_magic, NULL);
7773 *p = NUL;
7774 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
7775 if (regmatch.regprog != NULL)
7776 {
7777 regmatch.rm_ic = p_ic;
7778 p = ml_get_curline();
7779 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007780 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007781 else
7782 EMSG(_(e_nomatch));
Bram Moolenaar473de612013-06-08 18:19:48 +02007783 vim_regfree(regmatch.regprog);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007784 }
7785 /* Move to the NUL, ignore any other arguments. */
7786 eap->arg += STRLEN(eap->arg);
7787 }
7788 check_cursor();
7789
7790 eap->cmdidx = CMD_visual;
7791 do_exedit(eap, NULL);
7792}
7793
7794/*
7795 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007796 */
7797 static void
7798ex_edit(eap)
7799 exarg_T *eap;
7800{
7801 do_exedit(eap, NULL);
7802}
7803
7804/*
7805 * ":edit <file>" command and alikes.
7806 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007807 void
7808do_exedit(eap, old_curwin)
7809 exarg_T *eap;
7810 win_T *old_curwin; /* curwin before doing a split or NULL */
7811{
7812 int n;
7813#ifdef FEAT_WINDOWS
7814 int need_hide;
7815#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00007816 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007817
7818 /*
7819 * ":vi" command ends Ex mode.
7820 */
7821 if (exmode_active && (eap->cmdidx == CMD_visual
7822 || eap->cmdidx == CMD_view))
7823 {
7824 exmode_active = FALSE;
7825 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007826 {
7827 /* Special case: ":global/pat/visual\NLvi-commands" */
7828 if (global_busy)
7829 {
7830 int rd = RedrawingDisabled;
7831 int nwr = no_wait_return;
7832 int ms = msg_scroll;
7833#ifdef FEAT_GUI
7834 int he = hold_gui_events;
7835#endif
7836
7837 if (eap->nextcmd != NULL)
7838 {
7839 stuffReadbuff(eap->nextcmd);
7840 eap->nextcmd = NULL;
7841 }
7842
7843 if (exmode_was != EXMODE_VIM)
7844 settmode(TMODE_RAW);
7845 RedrawingDisabled = 0;
7846 no_wait_return = 0;
7847 need_wait_return = FALSE;
7848 msg_scroll = 0;
7849#ifdef FEAT_GUI
7850 hold_gui_events = 0;
7851#endif
7852 must_redraw = CLEAR;
7853
7854 main_loop(FALSE, TRUE);
7855
7856 RedrawingDisabled = rd;
7857 no_wait_return = nwr;
7858 msg_scroll = ms;
7859#ifdef FEAT_GUI
7860 hold_gui_events = he;
7861#endif
7862 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007863 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007864 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007865 }
7866
7867 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007868 || eap->cmdidx == CMD_tabnew
7869 || eap->cmdidx == CMD_tabedit
Bram Moolenaar071d4272004-06-13 20:20:40 +00007870#ifdef FEAT_VERTSPLIT
7871 || eap->cmdidx == CMD_vnew
7872#endif
7873 ) && *eap->arg == NUL)
7874 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007875 /* ":new" or ":tabnew" without argument: edit an new empty buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007876 setpcmark();
7877 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007878 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
7879 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007880 }
7881 else if ((eap->cmdidx != CMD_split
7882#ifdef FEAT_VERTSPLIT
7883 && eap->cmdidx != CMD_vsplit
7884#endif
7885 )
7886 || *eap->arg != NUL
7887#ifdef FEAT_BROWSE
7888 || cmdmod.browse
7889#endif
7890 )
7891 {
Bram Moolenaar5555acc2006-04-07 21:33:12 +00007892#ifdef FEAT_AUTOCMD
7893 /* Can't edit another file when "curbuf_lock" is set. Only ":edit"
7894 * can bring us here, others are stopped earlier. */
7895 if (*eap->arg != NUL && curbuf_locked())
7896 return;
7897#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007898 n = readonlymode;
7899 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
7900 readonlymode = TRUE;
7901 else if (eap->cmdidx == CMD_enew)
7902 readonlymode = FALSE; /* 'readonly' doesn't make sense in an
7903 empty buffer */
7904 setpcmark();
7905 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
7906 NULL, eap,
7907 /* ":edit" goes to first line if Vi compatible */
7908 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
7909 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
7910 ? ECMD_ONE : eap->do_ecmd_lnum,
7911 (P_HID(curbuf) ? ECMD_HIDE : 0)
7912 + (eap->forceit ? ECMD_FORCEIT : 0)
7913#ifdef FEAT_LISTCMDS
7914 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
7915#endif
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007916 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007917 {
7918 /* Editing the file failed. If the window was split, close it. */
7919#ifdef FEAT_WINDOWS
7920 if (old_curwin != NULL)
7921 {
7922 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
7923 if (!need_hide || P_HID(curbuf))
7924 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007925# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7926 cleanup_T cs;
7927
7928 /* Reset the error/interrupt/exception state here so that
7929 * aborting() returns FALSE when closing a window. */
7930 enter_cleanup(&cs);
7931# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007932# ifdef FEAT_GUI
7933 need_mouse_correct = TRUE;
7934# endif
7935 win_close(curwin, !need_hide && !P_HID(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007936
7937# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7938 /* Restore the error/interrupt/exception state if not
7939 * discarded by a new aborting error, interrupt, or
7940 * uncaught exception. */
7941 leave_cleanup(&cs);
7942# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007943 }
7944 }
7945#endif
7946 }
7947 else if (readonlymode && curbuf->b_nwindows == 1)
7948 {
7949 /* When editing an already visited buffer, 'readonly' won't be set
7950 * but the previous value is kept. With ":view" and ":sview" we
7951 * want the file to be readonly, except when another window is
7952 * editing the same buffer. */
7953 curbuf->b_p_ro = TRUE;
7954 }
7955 readonlymode = n;
7956 }
7957 else
7958 {
7959 if (eap->do_ecmd_cmd != NULL)
7960 do_cmdline_cmd(eap->do_ecmd_cmd);
7961#ifdef FEAT_TITLE
7962 n = curwin->w_arg_idx_invalid;
7963#endif
7964 check_arg_idx(curwin);
7965#ifdef FEAT_TITLE
7966 if (n != curwin->w_arg_idx_invalid)
7967 maketitle();
7968#endif
7969 }
7970
7971#ifdef FEAT_WINDOWS
7972 /*
7973 * if ":split file" worked, set alternate file name in old window to new
7974 * file
7975 */
7976 if (old_curwin != NULL
7977 && *eap->arg != NUL
7978 && curwin != old_curwin
7979 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00007980 && old_curwin->w_buffer != curbuf
7981 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007982 old_curwin->w_alt_fnum = curbuf->b_fnum;
7983#endif
7984
7985 ex_no_reprint = TRUE;
7986}
7987
7988#ifndef FEAT_GUI
7989/*
7990 * ":gui" and ":gvim" when there is no GUI.
7991 */
7992 static void
7993ex_nogui(eap)
7994 exarg_T *eap;
7995{
7996 eap->errmsg = e_nogvim;
7997}
7998#endif
7999
8000#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
8001 static void
8002ex_tearoff(eap)
8003 exarg_T *eap;
8004{
8005 gui_make_tearoff(eap->arg);
8006}
8007#endif
8008
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008009#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008010 static void
8011ex_popup(eap)
8012 exarg_T *eap;
8013{
Bram Moolenaar97409f12005-07-08 22:17:29 +00008014 gui_make_popup(eap->arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008015}
8016#endif
8017
Bram Moolenaar071d4272004-06-13 20:20:40 +00008018 static void
8019ex_swapname(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008020 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008021{
8022 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
8023 MSG(_("No swap file"));
8024 else
8025 msg(curbuf->b_ml.ml_mfp->mf_fname);
8026}
8027
8028/*
8029 * ":syncbind" forces all 'scrollbind' windows to have the same relative
8030 * offset.
8031 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
8032 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008033 static void
8034ex_syncbind(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008035 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008036{
8037#ifdef FEAT_SCROLLBIND
8038 win_T *wp;
8039 long topline;
8040 long y;
8041 linenr_T old_linenr = curwin->w_cursor.lnum;
8042
8043 setpcmark();
8044
8045 /*
8046 * determine max topline
8047 */
8048 if (curwin->w_p_scb)
8049 {
8050 topline = curwin->w_topline;
8051 for (wp = firstwin; wp; wp = wp->w_next)
8052 {
8053 if (wp->w_p_scb && wp->w_buffer)
8054 {
8055 y = wp->w_buffer->b_ml.ml_line_count - p_so;
8056 if (topline > y)
8057 topline = y;
8058 }
8059 }
8060 if (topline < 1)
8061 topline = 1;
8062 }
8063 else
8064 {
8065 topline = 1;
8066 }
8067
8068
8069 /*
8070 * set all scrollbind windows to the same topline
8071 */
8072 wp = curwin;
8073 for (curwin = firstwin; curwin; curwin = curwin->w_next)
8074 {
8075 if (curwin->w_p_scb)
8076 {
8077 y = topline - curwin->w_topline;
8078 if (y > 0)
8079 scrollup(y, TRUE);
8080 else
8081 scrolldown(-y, TRUE);
8082 curwin->w_scbind_pos = topline;
8083 redraw_later(VALID);
8084 cursor_correct();
8085#ifdef FEAT_WINDOWS
8086 curwin->w_redr_status = TRUE;
8087#endif
8088 }
8089 }
8090 curwin = wp;
8091 if (curwin->w_p_scb)
8092 {
8093 did_syncbind = TRUE;
8094 checkpcmark();
8095 if (old_linenr != curwin->w_cursor.lnum)
8096 {
8097 char_u ctrl_o[2];
8098
8099 ctrl_o[0] = Ctrl_O;
8100 ctrl_o[1] = 0;
8101 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
8102 }
8103 }
8104#endif
8105}
8106
8107
8108 static void
8109ex_read(eap)
8110 exarg_T *eap;
8111{
Bram Moolenaardf177f62005-02-22 08:39:57 +00008112 int i;
8113 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
8114 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008115
8116 if (eap->usefilter) /* :r!cmd */
8117 do_bang(1, eap, FALSE, FALSE, TRUE);
8118 else
8119 {
8120 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
8121 return;
8122
8123#ifdef FEAT_BROWSE
8124 if (cmdmod.browse)
8125 {
8126 char_u *browseFile;
8127
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008128 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008129 NULL, NULL, NULL, curbuf);
8130 if (browseFile != NULL)
8131 {
8132 i = readfile(browseFile, NULL,
8133 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8134 vim_free(browseFile);
8135 }
8136 else
8137 i = OK;
8138 }
8139 else
8140#endif
8141 if (*eap->arg == NUL)
8142 {
8143 if (check_fname() == FAIL) /* check for no file name */
8144 return;
8145 i = readfile(curbuf->b_ffname, curbuf->b_fname,
8146 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8147 }
8148 else
8149 {
8150 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
8151 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
8152 i = readfile(eap->arg, NULL,
8153 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8154
8155 }
8156 if (i == FAIL)
8157 {
8158#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
8159 if (!aborting())
8160#endif
8161 EMSG2(_(e_notopen), eap->arg);
8162 }
8163 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008164 {
8165 if (empty && exmode_active)
8166 {
8167 /* Delete the empty line that remains. Historically ex does
8168 * this but vi doesn't. */
8169 if (eap->line2 == 0)
8170 lnum = curbuf->b_ml.ml_line_count;
8171 else
8172 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008173 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008174 {
8175 ml_delete(lnum, FALSE);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008176 if (curwin->w_cursor.lnum > 1
8177 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008178 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00008179 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008180 }
8181 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008182 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008183 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008184 }
8185}
8186
Bram Moolenaarea408852005-06-25 22:49:46 +00008187static char_u *prev_dir = NULL;
8188
8189#if defined(EXITFREE) || defined(PROTO)
8190 void
8191free_cd_dir()
8192{
8193 vim_free(prev_dir);
Bram Moolenaara0174af2008-01-02 20:08:25 +00008194 prev_dir = NULL;
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00008195
8196 vim_free(globaldir);
8197 globaldir = NULL;
Bram Moolenaarea408852005-06-25 22:49:46 +00008198}
8199#endif
8200
Bram Moolenaarf4258302013-06-02 18:20:17 +02008201/*
8202 * Deal with the side effects of changing the current directory.
8203 * When "local" is TRUE then this was after an ":lcd" command.
8204 */
8205 void
8206post_chdir(local)
8207 int local;
8208{
8209 vim_free(curwin->w_localdir);
8210 if (local)
8211 {
8212 /* If still in global directory, need to remember current
8213 * directory as global directory. */
8214 if (globaldir == NULL && prev_dir != NULL)
8215 globaldir = vim_strsave(prev_dir);
8216 /* Remember this local directory for the window. */
8217 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8218 curwin->w_localdir = vim_strsave(NameBuff);
8219 }
8220 else
8221 {
8222 /* We are now in the global directory, no need to remember its
8223 * name. */
8224 vim_free(globaldir);
8225 globaldir = NULL;
8226 curwin->w_localdir = NULL;
8227 }
8228
8229 shorten_fnames(TRUE);
8230}
8231
Bram Moolenaarea408852005-06-25 22:49:46 +00008232
Bram Moolenaar071d4272004-06-13 20:20:40 +00008233/*
8234 * ":cd", ":lcd", ":chdir" and ":lchdir".
8235 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00008236 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00008237ex_cd(eap)
8238 exarg_T *eap;
8239{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008240 char_u *new_dir;
8241 char_u *tofree;
8242
8243 new_dir = eap->arg;
8244#if !defined(UNIX) && !defined(VMS)
8245 /* for non-UNIX ":cd" means: print current directory */
8246 if (*new_dir == NUL)
8247 ex_pwd(NULL);
8248 else
8249#endif
8250 {
Bram Moolenaar8e8fe9b2009-03-11 14:37:32 +00008251#ifdef FEAT_AUTOCMD
8252 if (allbuf_locked())
8253 return;
8254#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00008255 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged()
8256 && !eap->forceit)
8257 {
Bram Moolenaar81870892007-11-11 18:17:28 +00008258 EMSG(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00008259 return;
8260 }
8261
Bram Moolenaar071d4272004-06-13 20:20:40 +00008262 /* ":cd -": Change to previous directory */
8263 if (STRCMP(new_dir, "-") == 0)
8264 {
8265 if (prev_dir == NULL)
8266 {
8267 EMSG(_("E186: No previous directory"));
8268 return;
8269 }
8270 new_dir = prev_dir;
8271 }
8272
8273 /* Save current directory for next ":cd -" */
8274 tofree = prev_dir;
8275 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8276 prev_dir = vim_strsave(NameBuff);
8277 else
8278 prev_dir = NULL;
8279
8280#if defined(UNIX) || defined(VMS)
8281 /* for UNIX ":cd" means: go to home directory */
8282 if (*new_dir == NUL)
8283 {
8284 /* use NameBuff for home directory name */
8285# ifdef VMS
8286 char_u *p;
8287
8288 p = mch_getenv((char_u *)"SYS$LOGIN");
8289 if (p == NULL || *p == NUL) /* empty is the same as not set */
8290 NameBuff[0] = NUL;
8291 else
Bram Moolenaarb6356332005-07-18 21:40:44 +00008292 vim_strncpy(NameBuff, p, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008293# else
8294 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
8295# endif
8296 new_dir = NameBuff;
8297 }
8298#endif
8299 if (new_dir == NULL || vim_chdir(new_dir))
8300 EMSG(_(e_failed));
8301 else
8302 {
Bram Moolenaarf4258302013-06-02 18:20:17 +02008303 post_chdir(eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008304
8305 /* Echo the new current directory if the command was typed. */
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00008306 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008307 ex_pwd(eap);
8308 }
8309 vim_free(tofree);
8310 }
8311}
8312
8313/*
8314 * ":pwd".
8315 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008316 static void
8317ex_pwd(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008318 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008319{
8320 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8321 {
8322#ifdef BACKSLASH_IN_FILENAME
8323 slash_adjust(NameBuff);
8324#endif
8325 msg(NameBuff);
8326 }
8327 else
8328 EMSG(_("E187: Unknown"));
8329}
8330
8331/*
8332 * ":=".
8333 */
8334 static void
8335ex_equal(eap)
8336 exarg_T *eap;
8337{
8338 smsg((char_u *)"%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008339 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008340}
8341
8342 static void
8343ex_sleep(eap)
8344 exarg_T *eap;
8345{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008346 int n;
8347 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008348
8349 if (cursor_valid())
8350 {
8351 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
8352 if (n >= 0)
8353 windgoto((int)n, curwin->w_wcol);
8354 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008355
8356 len = eap->line2;
8357 switch (*eap->arg)
8358 {
8359 case 'm': break;
8360 case NUL: len *= 1000L; break;
8361 default: EMSG2(_(e_invarg2), eap->arg); return;
8362 }
8363 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008364}
8365
8366/*
8367 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
8368 */
8369 void
8370do_sleep(msec)
8371 long msec;
8372{
8373 long done;
8374
8375 cursor_on();
8376 out_flush();
8377 for (done = 0; !got_int && done < msec; done += 1000L)
8378 {
8379 ui_delay(msec - done > 1000L ? 1000L : msec - done, TRUE);
8380 ui_breakcheck();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02008381#ifdef FEAT_NETBEANS_INTG
8382 /* Process the netbeans messages that may have been received in the
8383 * call to ui_breakcheck() when the GUI is in use. This may occur when
8384 * running a test case. */
8385 netbeans_parse_messages();
8386#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008387 }
8388}
8389
8390 static void
8391do_exmap(eap, isabbrev)
8392 exarg_T *eap;
8393 int isabbrev;
8394{
8395 int mode;
8396 char_u *cmdp;
8397
8398 cmdp = eap->cmd;
8399 mode = get_map_mode(&cmdp, eap->forceit || isabbrev);
8400
8401 switch (do_map((*cmdp == 'n') ? 2 : (*cmdp == 'u'),
8402 eap->arg, mode, isabbrev))
8403 {
8404 case 1: EMSG(_(e_invarg));
8405 break;
8406 case 2: EMSG(isabbrev ? _(e_noabbr) : _(e_nomap));
8407 break;
8408 }
8409}
8410
8411/*
8412 * ":winsize" command (obsolete).
8413 */
8414 static void
8415ex_winsize(eap)
8416 exarg_T *eap;
8417{
8418 int w, h;
8419 char_u *arg = eap->arg;
8420 char_u *p;
8421
8422 w = getdigits(&arg);
8423 arg = skipwhite(arg);
8424 p = arg;
8425 h = getdigits(&arg);
8426 if (*p != NUL && *arg == NUL)
8427 set_shellsize(w, h, TRUE);
8428 else
8429 EMSG(_("E465: :winsize requires two number arguments"));
8430}
8431
8432#ifdef FEAT_WINDOWS
8433 static void
8434ex_wincmd(eap)
8435 exarg_T *eap;
8436{
8437 int xchar = NUL;
8438 char_u *p;
8439
8440 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
8441 {
8442 /* CTRL-W g and CTRL-W CTRL-G have an extra command character */
8443 if (eap->arg[1] == NUL)
8444 {
8445 EMSG(_(e_invarg));
8446 return;
8447 }
8448 xchar = eap->arg[1];
8449 p = eap->arg + 2;
8450 }
8451 else
8452 p = eap->arg + 1;
8453
8454 eap->nextcmd = check_nextcmd(p);
8455 p = skipwhite(p);
8456 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
8457 EMSG(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02008458 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008459 {
8460 /* Pass flags on for ":vertical wincmd ]". */
8461 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008462 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008463 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
8464 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008465 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008466 }
8467}
8468#endif
8469
Bram Moolenaar843ee412004-06-30 16:16:41 +00008470#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008471/*
8472 * ":winpos".
8473 */
8474 static void
8475ex_winpos(eap)
8476 exarg_T *eap;
8477{
8478 int x, y;
8479 char_u *arg = eap->arg;
8480 char_u *p;
8481
8482 if (*arg == NUL)
8483 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00008484# if defined(FEAT_GUI) || defined(MSWIN)
8485# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008486 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00008487# else
8488 if (mch_get_winpos(&x, &y) != FAIL)
8489# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008490 {
8491 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
8492 msg(IObuff);
8493 }
8494 else
8495# endif
8496 EMSG(_("E188: Obtaining window position not implemented for this platform"));
8497 }
8498 else
8499 {
8500 x = getdigits(&arg);
8501 arg = skipwhite(arg);
8502 p = arg;
8503 y = getdigits(&arg);
8504 if (*p == NUL || *arg != NUL)
8505 {
8506 EMSG(_("E466: :winpos requires two number arguments"));
8507 return;
8508 }
8509# ifdef FEAT_GUI
8510 if (gui.in_use)
8511 gui_mch_set_winpos(x, y);
8512 else if (gui.starting)
8513 {
8514 /* Remember the coordinates for when the window is opened. */
8515 gui_win_x = x;
8516 gui_win_y = y;
8517 }
8518# ifdef HAVE_TGETENT
8519 else
8520# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00008521# else
8522# ifdef MSWIN
8523 mch_set_winpos(x, y);
8524# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008525# endif
8526# ifdef HAVE_TGETENT
8527 if (*T_CWP)
8528 term_set_winpos(x, y);
8529# endif
8530 }
8531}
8532#endif
8533
8534/*
8535 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
8536 */
8537 static void
8538ex_operators(eap)
8539 exarg_T *eap;
8540{
8541 oparg_T oa;
8542
8543 clear_oparg(&oa);
8544 oa.regname = eap->regname;
8545 oa.start.lnum = eap->line1;
8546 oa.end.lnum = eap->line2;
8547 oa.line_count = eap->line2 - eap->line1 + 1;
8548 oa.motion_type = MLINE;
8549#ifdef FEAT_VIRTUALEDIT
8550 virtual_op = FALSE;
8551#endif
8552 if (eap->cmdidx != CMD_yank) /* position cursor for undo */
8553 {
8554 setpcmark();
8555 curwin->w_cursor.lnum = eap->line1;
8556 beginline(BL_SOL | BL_FIX);
8557 }
8558
8559 switch (eap->cmdidx)
8560 {
8561 case CMD_delete:
8562 oa.op_type = OP_DELETE;
8563 op_delete(&oa);
8564 break;
8565
8566 case CMD_yank:
8567 oa.op_type = OP_YANK;
8568 (void)op_yank(&oa, FALSE, TRUE);
8569 break;
8570
8571 default: /* CMD_rshift or CMD_lshift */
Bram Moolenaar6e707362013-06-14 19:15:58 +02008572 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00008573#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02008574 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
8575#else
8576 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00008577#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02008578 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00008579 oa.op_type = OP_RSHIFT;
8580 else
8581 oa.op_type = OP_LSHIFT;
8582 op_shift(&oa, FALSE, eap->amount);
8583 break;
8584 }
8585#ifdef FEAT_VIRTUALEDIT
8586 virtual_op = MAYBE;
8587#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00008588 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008589}
8590
8591/*
8592 * ":put".
8593 */
8594 static void
8595ex_put(eap)
8596 exarg_T *eap;
8597{
8598 /* ":0put" works like ":1put!". */
8599 if (eap->line2 == 0)
8600 {
8601 eap->line2 = 1;
8602 eap->forceit = TRUE;
8603 }
8604 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008605 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
8606 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008607}
8608
8609/*
8610 * Handle ":copy" and ":move".
8611 */
8612 static void
8613ex_copymove(eap)
8614 exarg_T *eap;
8615{
8616 long n;
8617
8618 n = get_address(&eap->arg, FALSE, FALSE);
8619 if (eap->arg == NULL) /* error detected */
8620 {
8621 eap->nextcmd = NULL;
8622 return;
8623 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008624 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008625
8626 /*
8627 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
8628 */
8629 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
8630 {
8631 EMSG(_(e_invaddr));
8632 return;
8633 }
8634
8635 if (eap->cmdidx == CMD_move)
8636 {
8637 if (do_move(eap->line1, eap->line2, n) == FAIL)
8638 return;
8639 }
8640 else
8641 ex_copy(eap->line1, eap->line2, n);
8642 u_clearline();
8643 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008644 ex_may_print(eap);
8645}
8646
8647/*
8648 * Print the current line if flags were given to the Ex command.
8649 */
8650 static void
8651ex_may_print(eap)
8652 exarg_T *eap;
8653{
8654 if (eap->flags != 0)
8655 {
8656 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
8657 (eap->flags & EXFLAG_LIST));
8658 ex_no_reprint = TRUE;
8659 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008660}
8661
8662/*
8663 * ":smagic" and ":snomagic".
8664 */
8665 static void
8666ex_submagic(eap)
8667 exarg_T *eap;
8668{
8669 int magic_save = p_magic;
8670
8671 p_magic = (eap->cmdidx == CMD_smagic);
8672 do_sub(eap);
8673 p_magic = magic_save;
8674}
8675
8676/*
8677 * ":join".
8678 */
8679 static void
8680ex_join(eap)
8681 exarg_T *eap;
8682{
8683 curwin->w_cursor.lnum = eap->line1;
8684 if (eap->line1 == eap->line2)
8685 {
8686 if (eap->addr_count >= 2) /* :2,2join does nothing */
8687 return;
8688 if (eap->line2 == curbuf->b_ml.ml_line_count)
8689 {
8690 beep_flush();
8691 return;
8692 }
8693 ++eap->line2;
8694 }
Bram Moolenaar81340392012-06-06 16:12:59 +02008695 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008696 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008697 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008698}
8699
8700/*
8701 * ":[addr]@r" or ":[addr]*r": execute register
8702 */
8703 static void
8704ex_at(eap)
8705 exarg_T *eap;
8706{
8707 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00008708 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008709
8710 curwin->w_cursor.lnum = eap->line2;
8711
8712#ifdef USE_ON_FLY_SCROLL
8713 dont_scroll = TRUE; /* disallow scrolling here */
8714#endif
8715
8716 /* get the register name. No name means to use the previous one */
8717 c = *eap->arg;
8718 if (c == NUL || (c == '*' && *eap->cmd == '*'))
8719 c = '@';
Bram Moolenaard333d1e2006-11-07 17:43:47 +00008720 /* Put the register in the typeahead buffer with the "silent" flag. */
8721 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
8722 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008723 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008724 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00008725 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008726 else
8727 {
8728 int save_efr = exec_from_reg;
8729
8730 exec_from_reg = TRUE;
8731
8732 /*
8733 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00008734 * Continue until the stuff buffer is empty and all added characters
8735 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008736 */
Bram Moolenaar60462872009-11-03 11:40:19 +00008737 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008738 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
8739
8740 exec_from_reg = save_efr;
8741 }
8742}
8743
8744/*
8745 * ":!".
8746 */
8747 static void
8748ex_bang(eap)
8749 exarg_T *eap;
8750{
8751 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
8752}
8753
8754/*
8755 * ":undo".
8756 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008757 static void
8758ex_undo(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008759 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008760{
Bram Moolenaard3667a22006-03-16 21:35:52 +00008761 if (eap->addr_count == 1) /* :undo 123 */
Bram Moolenaar730cde92010-06-27 05:18:54 +02008762 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00008763 else
8764 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008765}
8766
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008767#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008768 static void
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008769ex_wundo(eap)
8770 exarg_T *eap;
8771{
8772 char_u hash[UNDO_HASH_SIZE];
8773
8774 u_compute_hash(hash);
8775 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
8776}
8777
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008778 static void
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008779ex_rundo(eap)
8780 exarg_T *eap;
8781{
8782 char_u hash[UNDO_HASH_SIZE];
8783
8784 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02008785 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008786}
8787#endif
8788
Bram Moolenaar071d4272004-06-13 20:20:40 +00008789/*
8790 * ":redo".
8791 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008792 static void
8793ex_redo(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008794 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008795{
8796 u_redo(1);
8797}
8798
8799/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008800 * ":earlier" and ":later".
8801 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008802 static void
8803ex_later(eap)
8804 exarg_T *eap;
8805{
8806 long count = 0;
8807 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008808 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008809 char_u *p = eap->arg;
8810
8811 if (*p == NUL)
8812 count = 1;
8813 else if (isdigit(*p))
8814 {
8815 count = getdigits(&p);
8816 switch (*p)
8817 {
8818 case 's': ++p; sec = TRUE; break;
8819 case 'm': ++p; sec = TRUE; count *= 60; break;
8820 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008821 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
8822 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008823 }
8824 }
8825
8826 if (*p != NUL)
8827 EMSG2(_(e_invarg2), eap->arg);
8828 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02008829 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
8830 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008831}
8832
8833/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008834 * ":redir": start/stop redirection.
8835 */
8836 static void
8837ex_redir(eap)
8838 exarg_T *eap;
8839{
8840 char *mode;
8841 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00008842 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008843
8844 if (STRICMP(eap->arg, "END") == 0)
8845 close_redir();
8846 else
8847 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008848 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008849 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008850 ++arg;
8851 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008852 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008853 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008854 mode = "a";
8855 }
8856 else
8857 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00008858 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008859
8860 close_redir();
8861
8862 /* Expand environment variables and "~/". */
Bram Moolenaarca472992005-01-21 11:46:23 +00008863 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008864 if (fname == NULL)
8865 return;
8866#ifdef FEAT_BROWSE
8867 if (cmdmod.browse)
8868 {
8869 char_u *browseFile;
8870
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008871 browseFile = do_browse(BROWSE_SAVE,
8872 (char_u *)_("Save Redirection"),
8873 fname, NULL, NULL, BROWSE_FILTER_ALL_FILES, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008874 if (browseFile == NULL)
8875 return; /* operation cancelled */
8876 vim_free(fname);
8877 fname = browseFile;
8878 eap->forceit = TRUE; /* since dialog already asked */
8879 }
8880#endif
8881
8882 redir_fd = open_exfile(fname, eap->forceit, mode);
8883 vim_free(fname);
8884 }
8885#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00008886 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008887 {
8888 /* redirect to a register a-z (resp. A-Z for appending) */
8889 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00008890 ++arg;
8891 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008892# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00008893 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00008894 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008895# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00008896 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008897 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008898 redir_reg = *arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008899 if (*arg == '>' && arg[1] == '>') /* append */
Bram Moolenaard9d30582005-05-18 22:10:28 +00008900 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008901 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008902 {
Bram Moolenaarc188b882007-10-19 14:20:54 +00008903 /* Can use both "@a" and "@a>". */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00008904 if (*arg == '>')
8905 arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008906 /* Make register empty when not using @A-@Z and the
8907 * command is valid. */
8908 if (*arg == NUL && !isupper(redir_reg))
8909 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008910 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008911 }
8912 if (*arg != NUL)
8913 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00008914 redir_reg = 0;
Bram Moolenaard9d30582005-05-18 22:10:28 +00008915 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008916 }
8917 }
8918 else if (*arg == '=' && arg[1] == '>')
8919 {
8920 int append;
8921
8922 /* redirect to a variable */
8923 close_redir();
8924 arg += 2;
8925
8926 if (*arg == '>')
8927 {
8928 ++arg;
8929 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008930 }
8931 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008932 append = FALSE;
8933
8934 if (var_redir_start(skipwhite(arg), append) == OK)
8935 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008936 }
8937#endif
8938
8939 /* TODO: redirect to a buffer */
8940
Bram Moolenaar071d4272004-06-13 20:20:40 +00008941 else
Bram Moolenaarca472992005-01-21 11:46:23 +00008942 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008943 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00008944
8945 /* Make sure redirection is not off. Can happen for cmdline completion
8946 * that indirectly invokes a command to catch its output. */
8947 if (redir_fd != NULL
8948#ifdef FEAT_EVAL
8949 || redir_reg || redir_vname
8950#endif
8951 )
8952 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008953}
8954
8955/*
8956 * ":redraw": force redraw
8957 */
8958 static void
8959ex_redraw(eap)
8960 exarg_T *eap;
8961{
8962 int r = RedrawingDisabled;
8963 int p = p_lz;
8964
8965 RedrawingDisabled = 0;
8966 p_lz = FALSE;
8967 update_topline();
8968 update_screen(eap->forceit ? CLEAR :
8969#ifdef FEAT_VISUAL
8970 VIsual_active ? INVERTED :
8971#endif
8972 0);
8973#ifdef FEAT_TITLE
8974 if (need_maketitle)
8975 maketitle();
8976#endif
8977 RedrawingDisabled = r;
8978 p_lz = p;
8979
8980 /* Reset msg_didout, so that a message that's there is overwritten. */
8981 msg_didout = FALSE;
8982 msg_col = 0;
8983
Bram Moolenaar56667a52013-07-17 11:54:28 +02008984 /* No need to wait after an intentional redraw. */
8985 need_wait_return = FALSE;
8986
Bram Moolenaar071d4272004-06-13 20:20:40 +00008987 out_flush();
8988}
8989
8990/*
8991 * ":redrawstatus": force redraw of status line(s)
8992 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008993 static void
8994ex_redrawstatus(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008995 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008996{
8997#if defined(FEAT_WINDOWS)
8998 int r = RedrawingDisabled;
8999 int p = p_lz;
9000
9001 RedrawingDisabled = 0;
9002 p_lz = FALSE;
9003 if (eap->forceit)
9004 status_redraw_all();
9005 else
9006 status_redraw_curbuf();
9007 update_screen(
9008# ifdef FEAT_VISUAL
9009 VIsual_active ? INVERTED :
9010# endif
9011 0);
9012 RedrawingDisabled = r;
9013 p_lz = p;
9014 out_flush();
9015#endif
9016}
9017
9018 static void
9019close_redir()
9020{
9021 if (redir_fd != NULL)
9022 {
9023 fclose(redir_fd);
9024 redir_fd = NULL;
9025 }
9026#ifdef FEAT_EVAL
9027 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009028 if (redir_vname)
9029 {
9030 var_redir_stop();
9031 redir_vname = 0;
9032 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009033#endif
9034}
9035
9036#if defined(FEAT_SESSION) && defined(USE_CRNL)
9037# define MKSESSION_NL
9038static int mksession_nl = FALSE; /* use NL only in put_eol() */
9039#endif
9040
9041/*
9042 * ":mkexrc", ":mkvimrc", ":mkview" and ":mksession".
9043 */
9044 static void
9045ex_mkrc(eap)
9046 exarg_T *eap;
9047{
9048 FILE *fd;
9049 int failed = FALSE;
9050 char_u *fname;
9051#ifdef FEAT_BROWSE
9052 char_u *browseFile = NULL;
9053#endif
9054#ifdef FEAT_SESSION
9055 int view_session = FALSE;
9056 int using_vdir = FALSE; /* using 'viewdir'? */
9057 char_u *viewFile = NULL;
9058 unsigned *flagp;
9059#endif
9060
9061 if (eap->cmdidx == CMD_mksession || eap->cmdidx == CMD_mkview)
9062 {
9063#ifdef FEAT_SESSION
9064 view_session = TRUE;
9065#else
9066 ex_ni(eap);
9067 return;
9068#endif
9069 }
9070
9071#ifdef FEAT_SESSION
Bram Moolenaar8d594672009-07-01 18:18:57 +00009072 /* Use the short file name until ":lcd" is used. We also don't use the
9073 * short file name when 'acd' is set, that is checked later. */
Bram Moolenaareeefcc72007-05-01 21:21:21 +00009074 did_lcd = FALSE;
9075
Bram Moolenaar071d4272004-06-13 20:20:40 +00009076 /* ":mkview" or ":mkview 9": generate file name with 'viewdir' */
9077 if (eap->cmdidx == CMD_mkview
9078 && (*eap->arg == NUL
9079 || (vim_isdigit(*eap->arg) && eap->arg[1] == NUL)))
9080 {
9081 eap->forceit = TRUE;
9082 fname = get_view_file(*eap->arg);
9083 if (fname == NULL)
9084 return;
9085 viewFile = fname;
9086 using_vdir = TRUE;
9087 }
9088 else
9089#endif
9090 if (*eap->arg != NUL)
9091 fname = eap->arg;
9092 else if (eap->cmdidx == CMD_mkvimrc)
9093 fname = (char_u *)VIMRC_FILE;
9094#ifdef FEAT_SESSION
9095 else if (eap->cmdidx == CMD_mksession)
9096 fname = (char_u *)SESSION_FILE;
9097#endif
9098 else
9099 fname = (char_u *)EXRC_FILE;
9100
9101#ifdef FEAT_BROWSE
9102 if (cmdmod.browse)
9103 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00009104 browseFile = do_browse(BROWSE_SAVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009105# ifdef FEAT_SESSION
9106 eap->cmdidx == CMD_mkview ? (char_u *)_("Save View") :
9107 eap->cmdidx == CMD_mksession ? (char_u *)_("Save Session") :
9108# endif
9109 (char_u *)_("Save Setup"),
9110 fname, (char_u *)"vim", NULL, BROWSE_FILTER_MACROS, NULL);
9111 if (browseFile == NULL)
9112 goto theend;
9113 fname = browseFile;
9114 eap->forceit = TRUE; /* since dialog already asked */
9115 }
9116#endif
9117
9118#if defined(FEAT_SESSION) && defined(vim_mkdir)
9119 /* When using 'viewdir' may have to create the directory. */
9120 if (using_vdir && !mch_isdir(p_vdir))
Bram Moolenaardf177f62005-02-22 08:39:57 +00009121 vim_mkdir_emsg(p_vdir, 0755);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009122#endif
9123
9124 fd = open_exfile(fname, eap->forceit, WRITEBIN);
9125 if (fd != NULL)
9126 {
9127#ifdef FEAT_SESSION
9128 if (eap->cmdidx == CMD_mkview)
9129 flagp = &vop_flags;
9130 else
9131 flagp = &ssop_flags;
9132#endif
9133
9134#ifdef MKSESSION_NL
9135 /* "unix" in 'sessionoptions': use NL line separator */
9136 if (view_session && (*flagp & SSOP_UNIX))
9137 mksession_nl = TRUE;
9138#endif
9139
9140 /* Write the version command for :mkvimrc */
9141 if (eap->cmdidx == CMD_mkvimrc)
9142 (void)put_line(fd, "version 6.0");
9143
9144#ifdef FEAT_SESSION
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009145 if (eap->cmdidx == CMD_mksession)
9146 {
9147 if (put_line(fd, "let SessionLoad = 1") == FAIL)
9148 failed = TRUE;
9149 }
9150
Bram Moolenaar071d4272004-06-13 20:20:40 +00009151 if (eap->cmdidx != CMD_mkview)
9152#endif
9153 {
9154 /* Write setting 'compatible' first, because it has side effects.
9155 * For that same reason only do it when needed. */
9156 if (p_cp)
9157 (void)put_line(fd, "if !&cp | set cp | endif");
9158 else
9159 (void)put_line(fd, "if &cp | set nocp | endif");
9160 }
9161
9162#ifdef FEAT_SESSION
9163 if (!view_session
9164 || (eap->cmdidx == CMD_mksession
9165 && (*flagp & SSOP_OPTIONS)))
9166#endif
9167 failed |= (makemap(fd, NULL) == FAIL
9168 || makeset(fd, OPT_GLOBAL, FALSE) == FAIL);
9169
9170#ifdef FEAT_SESSION
9171 if (!failed && view_session)
9172 {
9173 if (put_line(fd, "let s:so_save = &so | let s:siso_save = &siso | set so=0 siso=0") == FAIL)
9174 failed = TRUE;
9175 if (eap->cmdidx == CMD_mksession)
9176 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02009177 char_u *dirnow; /* current directory */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009178
Bram Moolenaard9462e32011-04-11 21:35:11 +02009179 dirnow = alloc(MAXPATHL);
9180 if (dirnow == NULL)
9181 failed = TRUE;
9182 else
9183 {
9184 /*
9185 * Change to session file's dir.
9186 */
9187 if (mch_dirname(dirnow, MAXPATHL) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00009188 || mch_chdir((char *)dirnow) != 0)
Bram Moolenaard9462e32011-04-11 21:35:11 +02009189 *dirnow = NUL;
9190 if (*dirnow != NUL && (ssop_flags & SSOP_SESDIR))
9191 {
9192 if (vim_chdirfile(fname) == OK)
9193 shorten_fnames(TRUE);
9194 }
9195 else if (*dirnow != NUL
9196 && (ssop_flags & SSOP_CURDIR) && globaldir != NULL)
9197 {
9198 if (mch_chdir((char *)globaldir) == 0)
9199 shorten_fnames(TRUE);
9200 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009201
Bram Moolenaard9462e32011-04-11 21:35:11 +02009202 failed |= (makeopens(fd, dirnow) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009203
Bram Moolenaard9462e32011-04-11 21:35:11 +02009204 /* restore original dir */
9205 if (*dirnow != NUL && ((ssop_flags & SSOP_SESDIR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009206 || ((ssop_flags & SSOP_CURDIR) && globaldir != NULL)))
Bram Moolenaard9462e32011-04-11 21:35:11 +02009207 {
9208 if (mch_chdir((char *)dirnow) != 0)
9209 EMSG(_(e_prev_dir));
9210 shorten_fnames(TRUE);
9211 }
9212 vim_free(dirnow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009213 }
9214 }
9215 else
9216 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +00009217 failed |= (put_view(fd, curwin, !using_vdir, flagp,
9218 -1) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009219 }
9220 if (put_line(fd, "let &so = s:so_save | let &siso = s:siso_save")
9221 == FAIL)
9222 failed = TRUE;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009223 if (put_line(fd, "doautoall SessionLoadPost") == FAIL)
9224 failed = TRUE;
Bram Moolenaar4770d092006-01-12 23:22:24 +00009225 if (eap->cmdidx == CMD_mksession)
9226 {
9227 if (put_line(fd, "unlet SessionLoad") == FAIL)
9228 failed = TRUE;
9229 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009230 }
9231#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009232 if (put_line(fd, "\" vim: set ft=vim :") == FAIL)
9233 failed = TRUE;
9234
Bram Moolenaar071d4272004-06-13 20:20:40 +00009235 failed |= fclose(fd);
9236
9237 if (failed)
9238 EMSG(_(e_write));
9239#if defined(FEAT_EVAL) && defined(FEAT_SESSION)
9240 else if (eap->cmdidx == CMD_mksession)
9241 {
9242 /* successful session write - set this_session var */
Bram Moolenaard9462e32011-04-11 21:35:11 +02009243 char_u *tbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009244
Bram Moolenaard9462e32011-04-11 21:35:11 +02009245 tbuf = alloc(MAXPATHL);
9246 if (tbuf != NULL)
9247 {
9248 if (vim_FullName(fname, tbuf, MAXPATHL, FALSE) == OK)
9249 set_vim_var_string(VV_THIS_SESSION, tbuf, -1);
9250 vim_free(tbuf);
9251 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009252 }
9253#endif
9254#ifdef MKSESSION_NL
9255 mksession_nl = FALSE;
9256#endif
9257 }
9258
9259#ifdef FEAT_BROWSE
9260theend:
9261 vim_free(browseFile);
9262#endif
9263#ifdef FEAT_SESSION
9264 vim_free(viewFile);
9265#endif
9266}
9267
Bram Moolenaardf177f62005-02-22 08:39:57 +00009268#if ((defined(FEAT_SESSION) || defined(FEAT_EVAL)) && defined(vim_mkdir)) \
9269 || defined(PROTO)
9270 int
9271vim_mkdir_emsg(name, prot)
9272 char_u *name;
Bram Moolenaar78a15312009-05-15 19:33:18 +00009273 int prot UNUSED;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009274{
9275 if (vim_mkdir(name, prot) != 0)
9276 {
9277 EMSG2(_("E739: Cannot create directory: %s"), name);
9278 return FAIL;
9279 }
9280 return OK;
9281}
9282#endif
9283
Bram Moolenaar071d4272004-06-13 20:20:40 +00009284/*
9285 * Open a file for writing for an Ex command, with some checks.
9286 * Return file descriptor, or NULL on failure.
9287 */
9288 FILE *
9289open_exfile(fname, forceit, mode)
9290 char_u *fname;
9291 int forceit;
9292 char *mode; /* "w" for create new file or "a" for append */
9293{
9294 FILE *fd;
9295
9296#ifdef UNIX
9297 /* with Unix it is possible to open a directory */
9298 if (mch_isdir(fname))
9299 {
9300 EMSG2(_(e_isadir2), fname);
9301 return NULL;
9302 }
9303#endif
9304 if (!forceit && *mode != 'a' && vim_fexists(fname))
9305 {
9306 EMSG2(_("E189: \"%s\" exists (add ! to override)"), fname);
9307 return NULL;
9308 }
9309
9310 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
9311 EMSG2(_("E190: Cannot open \"%s\" for writing"), fname);
9312
9313 return fd;
9314}
9315
9316/*
9317 * ":mark" and ":k".
9318 */
9319 static void
9320ex_mark(eap)
9321 exarg_T *eap;
9322{
9323 pos_T pos;
9324
9325 if (*eap->arg == NUL) /* No argument? */
9326 EMSG(_(e_argreq));
9327 else if (eap->arg[1] != NUL) /* more than one character? */
9328 EMSG(_(e_trailing));
9329 else
9330 {
9331 pos = curwin->w_cursor; /* save curwin->w_cursor */
9332 curwin->w_cursor.lnum = eap->line2;
9333 beginline(BL_WHITE | BL_FIX);
9334 if (setmark(*eap->arg) == FAIL) /* set mark */
9335 EMSG(_("E191: Argument must be a letter or forward/backward quote"));
9336 curwin->w_cursor = pos; /* restore curwin->w_cursor */
9337 }
9338}
9339
9340/*
9341 * Update w_topline, w_leftcol and the cursor position.
9342 */
9343 void
9344update_topline_cursor()
9345{
9346 check_cursor(); /* put cursor on valid line */
9347 update_topline();
9348 if (!curwin->w_p_wrap)
9349 validate_cursor();
9350 update_curswant();
9351}
9352
9353#ifdef FEAT_EX_EXTRA
9354/*
9355 * ":normal[!] {commands}": Execute normal mode commands.
9356 */
9357 static void
9358ex_normal(eap)
9359 exarg_T *eap;
9360{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009361 int save_msg_scroll = msg_scroll;
9362 int save_restart_edit = restart_edit;
9363 int save_msg_didout = msg_didout;
9364 int save_State = State;
9365 tasave_T tabuf;
9366 int save_insertmode = p_im;
9367 int save_finish_op = finish_op;
Bram Moolenaar38084112008-07-26 14:05:07 +00009368 int save_opcount = opcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009369#ifdef FEAT_MBYTE
9370 char_u *arg = NULL;
9371 int l;
9372 char_u *p;
9373#endif
9374
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00009375 if (ex_normal_lock > 0)
9376 {
9377 EMSG(_(e_secure));
9378 return;
9379 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009380 if (ex_normal_busy >= p_mmd)
9381 {
9382 EMSG(_("E192: Recursive use of :normal too deep"));
9383 return;
9384 }
9385 ++ex_normal_busy;
9386
9387 msg_scroll = FALSE; /* no msg scrolling in Normal mode */
9388 restart_edit = 0; /* don't go to Insert mode */
9389 p_im = FALSE; /* don't use 'insertmode' */
9390
9391#ifdef FEAT_MBYTE
9392 /*
9393 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
9394 * this for the K_SPECIAL leading byte, otherwise special keys will not
9395 * work.
9396 */
9397 if (has_mbyte)
9398 {
9399 int len = 0;
9400
9401 /* Count the number of characters to be escaped. */
9402 for (p = eap->arg; *p != NUL; ++p)
9403 {
9404# ifdef FEAT_GUI
9405 if (*p == CSI) /* leadbyte CSI */
9406 len += 2;
9407# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009408 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009409 if (*++p == K_SPECIAL /* trailbyte K_SPECIAL or CSI */
9410# ifdef FEAT_GUI
9411 || *p == CSI
9412# endif
9413 )
9414 len += 2;
9415 }
9416 if (len > 0)
9417 {
9418 arg = alloc((unsigned)(STRLEN(eap->arg) + len + 1));
9419 if (arg != NULL)
9420 {
9421 len = 0;
9422 for (p = eap->arg; *p != NUL; ++p)
9423 {
9424 arg[len++] = *p;
9425# ifdef FEAT_GUI
9426 if (*p == CSI)
9427 {
9428 arg[len++] = KS_EXTRA;
9429 arg[len++] = (int)KE_CSI;
9430 }
9431# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009432 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009433 {
9434 arg[len++] = *++p;
9435 if (*p == K_SPECIAL)
9436 {
9437 arg[len++] = KS_SPECIAL;
9438 arg[len++] = KE_FILLER;
9439 }
9440# ifdef FEAT_GUI
9441 else if (*p == CSI)
9442 {
9443 arg[len++] = KS_EXTRA;
9444 arg[len++] = (int)KE_CSI;
9445 }
9446# endif
9447 }
9448 arg[len] = NUL;
9449 }
9450 }
9451 }
9452 }
9453#endif
9454
9455 /*
9456 * Save the current typeahead. This is required to allow using ":normal"
9457 * from an event handler and makes sure we don't hang when the argument
9458 * ends with half a command.
9459 */
9460 save_typeahead(&tabuf);
9461 if (tabuf.typebuf_valid)
9462 {
9463 /*
9464 * Repeat the :normal command for each line in the range. When no
9465 * range given, execute it just once, without positioning the cursor
9466 * first.
9467 */
9468 do
9469 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009470 if (eap->addr_count != 0)
9471 {
9472 curwin->w_cursor.lnum = eap->line1++;
9473 curwin->w_cursor.col = 0;
9474 }
9475
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009476 exec_normal_cmd(
Bram Moolenaar071d4272004-06-13 20:20:40 +00009477#ifdef FEAT_MBYTE
9478 arg != NULL ? arg :
9479#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009480 eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009481 }
9482 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
9483 }
9484
9485 /* Might not return to the main loop when in an event handler. */
9486 update_topline_cursor();
9487
9488 /* Restore the previous typeahead. */
9489 restore_typeahead(&tabuf);
9490
9491 --ex_normal_busy;
9492 msg_scroll = save_msg_scroll;
9493 restart_edit = save_restart_edit;
9494 p_im = save_insertmode;
9495 finish_op = save_finish_op;
Bram Moolenaar38084112008-07-26 14:05:07 +00009496 opcount = save_opcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009497 msg_didout |= save_msg_didout; /* don't reset msg_didout now */
9498
9499 /* Restore the state (needed when called from a function executed for
9500 * 'indentexpr'). */
9501 State = save_State;
9502#ifdef FEAT_MBYTE
9503 vim_free(arg);
9504#endif
9505}
9506
9507/*
Bram Moolenaar64969662005-12-14 21:59:55 +00009508 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009509 */
9510 static void
9511ex_startinsert(eap)
9512 exarg_T *eap;
9513{
Bram Moolenaarfd371682005-01-14 21:42:54 +00009514 if (eap->forceit)
9515 {
9516 coladvance((colnr_T)MAXCOL);
9517 curwin->w_curswant = MAXCOL;
9518 curwin->w_set_curswant = FALSE;
9519 }
9520
Bram Moolenaara40c5002005-01-09 21:16:21 +00009521 /* Ignore the command when already in Insert mode. Inserting an
9522 * expression register that invokes a function can do this. */
9523 if (State & INSERT)
9524 return;
9525
Bram Moolenaar64969662005-12-14 21:59:55 +00009526 if (eap->cmdidx == CMD_startinsert)
9527 restart_edit = 'a';
9528 else if (eap->cmdidx == CMD_startreplace)
9529 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00009530 else
Bram Moolenaar64969662005-12-14 21:59:55 +00009531 restart_edit = 'V';
9532
9533 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009534 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009535 if (eap->cmdidx == CMD_startinsert)
9536 restart_edit = 'i';
Bram Moolenaar071d4272004-06-13 20:20:40 +00009537 curwin->w_curswant = 0; /* avoid MAXCOL */
9538 }
9539}
9540
9541/*
9542 * ":stopinsert"
9543 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009544 static void
9545ex_stopinsert(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009546 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009547{
9548 restart_edit = 0;
9549 stop_insert_mode = TRUE;
9550}
9551#endif
9552
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009553#if defined(FEAT_EX_EXTRA) || defined(FEAT_MENU) || defined(PROTO)
9554/*
9555 * Execute normal mode command "cmd".
9556 * "remap" can be REMAP_NONE or REMAP_YES.
9557 */
9558 void
9559exec_normal_cmd(cmd, remap, silent)
9560 char_u *cmd;
9561 int remap;
9562 int silent;
9563{
9564 oparg_T oa;
9565
9566 /*
9567 * Stuff the argument into the typeahead buffer.
9568 * Execute normal_cmd() until there is no typeahead left.
9569 */
9570 clear_oparg(&oa);
9571 finish_op = FALSE;
9572 ins_typebuf(cmd, remap, 0, TRUE, silent);
9573 while ((!stuff_empty() || (!typebuf_typed() && typebuf.tb_len > 0))
9574 && !got_int)
9575 {
9576 update_topline_cursor();
Bram Moolenaar48ac02c2011-01-17 19:50:06 +01009577 normal_cmd(&oa, TRUE); /* execute a Normal mode cmd */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009578 }
9579}
9580#endif
9581
Bram Moolenaar071d4272004-06-13 20:20:40 +00009582#ifdef FEAT_FIND_ID
9583 static void
9584ex_checkpath(eap)
9585 exarg_T *eap;
9586{
9587 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
9588 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
9589 (linenr_T)1, (linenr_T)MAXLNUM);
9590}
9591
9592#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
9593/*
9594 * ":psearch"
9595 */
9596 static void
9597ex_psearch(eap)
9598 exarg_T *eap;
9599{
9600 g_do_tagpreview = p_pvh;
9601 ex_findpat(eap);
9602 g_do_tagpreview = 0;
9603}
9604#endif
9605
9606 static void
9607ex_findpat(eap)
9608 exarg_T *eap;
9609{
9610 int whole = TRUE;
9611 long n;
9612 char_u *p;
9613 int action;
9614
9615 switch (cmdnames[eap->cmdidx].cmd_name[2])
9616 {
9617 case 'e': /* ":psearch", ":isearch" and ":dsearch" */
9618 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
9619 action = ACTION_GOTO;
9620 else
9621 action = ACTION_SHOW;
9622 break;
9623 case 'i': /* ":ilist" and ":dlist" */
9624 action = ACTION_SHOW_ALL;
9625 break;
9626 case 'u': /* ":ijump" and ":djump" */
9627 action = ACTION_GOTO;
9628 break;
9629 default: /* ":isplit" and ":dsplit" */
9630 action = ACTION_SPLIT;
9631 break;
9632 }
9633
9634 n = 1;
9635 if (vim_isdigit(*eap->arg)) /* get count */
9636 {
9637 n = getdigits(&eap->arg);
9638 eap->arg = skipwhite(eap->arg);
9639 }
9640 if (*eap->arg == '/') /* Match regexp, not just whole words */
9641 {
9642 whole = FALSE;
9643 ++eap->arg;
9644 p = skip_regexp(eap->arg, '/', p_magic, NULL);
9645 if (*p)
9646 {
9647 *p++ = NUL;
9648 p = skipwhite(p);
9649
9650 /* Check for trailing illegal characters */
9651 if (!ends_excmd(*p))
9652 eap->errmsg = e_trailing;
9653 else
9654 eap->nextcmd = check_nextcmd(p);
9655 }
9656 }
9657 if (!eap->skip)
9658 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
9659 whole, !eap->forceit,
9660 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
9661 n, action, eap->line1, eap->line2);
9662}
9663#endif
9664
9665#ifdef FEAT_WINDOWS
9666
9667# ifdef FEAT_QUICKFIX
9668/*
9669 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
9670 */
9671 static void
9672ex_ptag(eap)
9673 exarg_T *eap;
9674{
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01009675 g_do_tagpreview = p_pvh; /* will be reset to 0 in ex_tag_cmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009676 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9677}
9678
9679/*
9680 * ":pedit"
9681 */
9682 static void
9683ex_pedit(eap)
9684 exarg_T *eap;
9685{
9686 win_T *curwin_save = curwin;
9687
9688 g_do_tagpreview = p_pvh;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00009689 prepare_tagpreview(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009690 keep_help_flag = curwin_save->w_buffer->b_help;
9691 do_exedit(eap, NULL);
9692 keep_help_flag = FALSE;
9693 if (curwin != curwin_save && win_valid(curwin_save))
9694 {
9695 /* Return cursor to where we were */
9696 validate_cursor();
9697 redraw_later(VALID);
9698 win_enter(curwin_save, TRUE);
9699 }
9700 g_do_tagpreview = 0;
9701}
9702# endif
9703
9704/*
9705 * ":stag", ":stselect" and ":stjump".
9706 */
9707 static void
9708ex_stag(eap)
9709 exarg_T *eap;
9710{
9711 postponed_split = -1;
9712 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009713 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009714 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9715 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009716 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009717}
9718#endif
9719
9720/*
9721 * ":tag", ":tselect", ":tjump", ":tnext", etc.
9722 */
9723 static void
9724ex_tag(eap)
9725 exarg_T *eap;
9726{
9727 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
9728}
9729
9730 static void
9731ex_tag_cmd(eap, name)
9732 exarg_T *eap;
9733 char_u *name;
9734{
9735 int cmd;
9736
9737 switch (name[1])
9738 {
9739 case 'j': cmd = DT_JUMP; /* ":tjump" */
9740 break;
9741 case 's': cmd = DT_SELECT; /* ":tselect" */
9742 break;
9743 case 'p': cmd = DT_PREV; /* ":tprevious" */
9744 break;
9745 case 'N': cmd = DT_PREV; /* ":tNext" */
9746 break;
9747 case 'n': cmd = DT_NEXT; /* ":tnext" */
9748 break;
9749 case 'o': cmd = DT_POP; /* ":pop" */
9750 break;
9751 case 'f': /* ":tfirst" */
9752 case 'r': cmd = DT_FIRST; /* ":trewind" */
9753 break;
9754 case 'l': cmd = DT_LAST; /* ":tlast" */
9755 break;
9756 default: /* ":tag" */
9757#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +00009758 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009759 {
9760 do_cstag(eap);
9761 return;
9762 }
9763#endif
9764 cmd = DT_TAG;
9765 break;
9766 }
9767
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00009768 if (name[0] == 'l')
9769 {
9770#ifndef FEAT_QUICKFIX
9771 ex_ni(eap);
9772 return;
9773#else
9774 cmd = DT_LTAG;
9775#endif
9776 }
9777
Bram Moolenaar071d4272004-06-13 20:20:40 +00009778 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
9779 eap->forceit, TRUE);
9780}
9781
9782/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009783 * Check "str" for starting with a special cmdline variable.
9784 * If found return one of the SPEC_ values and set "*usedlen" to the length of
9785 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
9786 */
9787 int
9788find_cmdline_var(src, usedlen)
9789 char_u *src;
9790 int *usedlen;
9791{
9792 int len;
9793 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +00009794 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009795 "%",
9796#define SPEC_PERC 0
9797 "#",
9798#define SPEC_HASH 1
9799 "<cword>", /* cursor word */
9800#define SPEC_CWORD 2
9801 "<cWORD>", /* cursor WORD */
9802#define SPEC_CCWORD 3
9803 "<cfile>", /* cursor path name */
9804#define SPEC_CFILE 4
9805 "<sfile>", /* ":so" file name */
9806#define SPEC_SFILE 5
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009807 "<slnum>", /* ":so" file line number */
9808#define SPEC_SLNUM 6
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009809#ifdef FEAT_AUTOCMD
9810 "<afile>", /* autocommand file name */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009811# define SPEC_AFILE 7
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009812 "<abuf>", /* autocommand buffer number */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009813# define SPEC_ABUF 8
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009814 "<amatch>", /* autocommand match name */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009815# define SPEC_AMATCH 9
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009816#endif
9817#ifdef FEAT_CLIENTSERVER
9818 "<client>"
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009819# ifdef FEAT_AUTOCMD
9820# define SPEC_CLIENT 10
9821# else
9822# define SPEC_CLIENT 7
9823# endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009824#endif
9825 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009826
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009827 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009828 {
9829 len = (int)STRLEN(spec_str[i]);
9830 if (STRNCMP(src, spec_str[i], len) == 0)
9831 {
9832 *usedlen = len;
9833 return i;
9834 }
9835 }
9836 return -1;
9837}
9838
9839/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009840 * Evaluate cmdline variables.
9841 *
9842 * change '%' to curbuf->b_ffname
9843 * '#' to curwin->w_altfile
9844 * '<cword>' to word under the cursor
9845 * '<cWORD>' to WORD under the cursor
9846 * '<cfile>' to path name under the cursor
9847 * '<sfile>' to sourced file name
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009848 * '<slnum>' to sourced file line number
Bram Moolenaar071d4272004-06-13 20:20:40 +00009849 * '<afile>' to file name for autocommand
9850 * '<abuf>' to buffer number for autocommand
9851 * '<amatch>' to matching name for autocommand
9852 *
9853 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
9854 * "" for error without a message) and NULL is returned.
9855 * Returns an allocated string if a valid match was found.
9856 * Returns NULL if no match was found. "usedlen" then still contains the
9857 * number of characters to skip.
9858 */
9859 char_u *
Bram Moolenaar63b92542007-03-27 14:57:09 +00009860eval_vars(src, srcstart, usedlen, lnump, errormsg, escaped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009861 char_u *src; /* pointer into commandline */
Bram Moolenaar63b92542007-03-27 14:57:09 +00009862 char_u *srcstart; /* beginning of valid memory for src */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009863 int *usedlen; /* characters after src that are used */
9864 linenr_T *lnump; /* line number for :e command, or NULL */
9865 char_u **errormsg; /* pointer to error message */
Bram Moolenaar63b92542007-03-27 14:57:09 +00009866 int *escaped; /* return value has escaped white space (can
9867 * be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009868{
9869 int i;
9870 char_u *s;
9871 char_u *result;
9872 char_u *resultbuf = NULL;
9873 int resultlen;
9874 buf_T *buf;
9875 int valid = VALID_HEAD + VALID_PATH; /* assume valid result */
9876 int spec_idx;
9877#ifdef FEAT_MODIFY_FNAME
9878 int skip_mod = FALSE;
9879#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009880 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009881
9882 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009883 if (escaped != NULL)
9884 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009885
9886 /*
9887 * Check if there is something to do.
9888 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009889 spec_idx = find_cmdline_var(src, usedlen);
9890 if (spec_idx < 0) /* no match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009891 {
9892 *usedlen = 1;
9893 return NULL;
9894 }
9895
9896 /*
9897 * Skip when preceded with a backslash "\%" and "\#".
9898 * Note: In "\\%" the % is also not recognized!
9899 */
9900 if (src > srcstart && src[-1] == '\\')
9901 {
9902 *usedlen = 0;
Bram Moolenaara7241f52008-06-24 20:39:31 +00009903 STRMOVE(src - 1, src); /* remove backslash */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009904 return NULL;
9905 }
9906
9907 /*
9908 * word or WORD under cursor
9909 */
9910 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD)
9911 {
9912 resultlen = find_ident_under_cursor(&result, spec_idx == SPEC_CWORD ?
9913 (FIND_IDENT|FIND_STRING) : FIND_STRING);
9914 if (resultlen == 0)
9915 {
9916 *errormsg = (char_u *)"";
9917 return NULL;
9918 }
9919 }
9920
9921 /*
9922 * '#': Alternate file name
9923 * '%': Current file name
9924 * File name under the cursor
9925 * File name for autocommand
9926 * and following modifiers
9927 */
9928 else
9929 {
9930 switch (spec_idx)
9931 {
9932 case SPEC_PERC: /* '%': current file */
9933 if (curbuf->b_fname == NULL)
9934 {
9935 result = (char_u *)"";
9936 valid = 0; /* Must have ":p:h" to be valid */
9937 }
9938 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009939 result = curbuf->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009940 break;
9941
9942 case SPEC_HASH: /* '#' or "#99": alternate file */
9943 if (src[1] == '#') /* "##": the argument list */
9944 {
9945 result = arg_all();
9946 resultbuf = result;
9947 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009948 if (escaped != NULL)
9949 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009950#ifdef FEAT_MODIFY_FNAME
9951 skip_mod = TRUE;
9952#endif
9953 break;
9954 }
9955 s = src + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00009956 if (*s == '<') /* "#<99" uses v:oldfiles */
9957 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009958 i = (int)getdigits(&s);
9959 *usedlen = (int)(s - src); /* length of what we expand */
9960
Bram Moolenaard812df62008-11-09 12:46:09 +00009961 if (src[1] == '<')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009962 {
Bram Moolenaard812df62008-11-09 12:46:09 +00009963 if (*usedlen < 2)
9964 {
9965 /* Should we give an error message for #<text? */
9966 *usedlen = 1;
9967 return NULL;
9968 }
9969#ifdef FEAT_EVAL
9970 result = list_find_str(get_vim_var_list(VV_OLDFILES),
9971 (long)i);
9972 if (result == NULL)
9973 {
9974 *errormsg = (char_u *)"";
9975 return NULL;
9976 }
9977#else
9978 *errormsg = (char_u *)_("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009979 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00009980#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009981 }
9982 else
Bram Moolenaard812df62008-11-09 12:46:09 +00009983 {
9984 buf = buflist_findnr(i);
9985 if (buf == NULL)
9986 {
9987 *errormsg = (char_u *)_("E194: No alternate file name to substitute for '#'");
9988 return NULL;
9989 }
9990 if (lnump != NULL)
9991 *lnump = ECMD_LAST;
9992 if (buf->b_fname == NULL)
9993 {
9994 result = (char_u *)"";
9995 valid = 0; /* Must have ":p:h" to be valid */
9996 }
9997 else
9998 result = buf->b_fname;
9999 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010000 break;
10001
10002#ifdef FEAT_SEARCHPATH
10003 case SPEC_CFILE: /* file name under cursor */
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010004 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010005 if (result == NULL)
10006 {
10007 *errormsg = (char_u *)"";
10008 return NULL;
10009 }
10010 resultbuf = result; /* remember allocated string */
10011 break;
10012#endif
10013
10014#ifdef FEAT_AUTOCMD
10015 case SPEC_AFILE: /* file name for autocommand */
10016 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +000010017 if (result != NULL && !autocmd_fname_full)
10018 {
10019 /* Still need to turn the fname into a full path. It is
10020 * postponed to avoid a delay when <afile> is not used. */
10021 autocmd_fname_full = TRUE;
10022 result = FullName_save(autocmd_fname, FALSE);
10023 vim_free(autocmd_fname);
10024 autocmd_fname = result;
10025 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010026 if (result == NULL)
10027 {
10028 *errormsg = (char_u *)_("E495: no autocommand file name to substitute for \"<afile>\"");
10029 return NULL;
10030 }
Bram Moolenaara0174af2008-01-02 20:08:25 +000010031 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010032 break;
10033
10034 case SPEC_ABUF: /* buffer number for autocommand */
10035 if (autocmd_bufnr <= 0)
10036 {
10037 *errormsg = (char_u *)_("E496: no autocommand buffer number to substitute for \"<abuf>\"");
10038 return NULL;
10039 }
10040 sprintf((char *)strbuf, "%d", autocmd_bufnr);
10041 result = strbuf;
10042 break;
10043
10044 case SPEC_AMATCH: /* match name for autocommand */
10045 result = autocmd_match;
10046 if (result == NULL)
10047 {
10048 *errormsg = (char_u *)_("E497: no autocommand match name to substitute for \"<amatch>\"");
10049 return NULL;
10050 }
10051 break;
10052
10053#endif
10054 case SPEC_SFILE: /* file name for ":so" command */
10055 result = sourcing_name;
10056 if (result == NULL)
10057 {
10058 *errormsg = (char_u *)_("E498: no :source file name to substitute for \"<sfile>\"");
10059 return NULL;
10060 }
10061 break;
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010062 case SPEC_SLNUM: /* line in file for ":so" command */
10063 if (sourcing_name == NULL || sourcing_lnum == 0)
10064 {
10065 *errormsg = (char_u *)_("E842: no line number to use for \"<slnum>\"");
10066 return NULL;
10067 }
10068 sprintf((char *)strbuf, "%ld", (long)sourcing_lnum);
10069 result = strbuf;
10070 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010071#if defined(FEAT_CLIENTSERVER)
10072 case SPEC_CLIENT: /* Source of last submitted input */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010073 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
10074 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010075 result = strbuf;
10076 break;
10077#endif
10078 }
10079
10080 resultlen = (int)STRLEN(result); /* length of new string */
10081 if (src[*usedlen] == '<') /* remove the file name extension */
10082 {
10083 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010084 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010085 resultlen = (int)(s - result);
10086 }
10087#ifdef FEAT_MODIFY_FNAME
10088 else if (!skip_mod)
10089 {
10090 valid |= modify_fname(src, usedlen, &result, &resultbuf,
10091 &resultlen);
10092 if (result == NULL)
10093 {
10094 *errormsg = (char_u *)"";
10095 return NULL;
10096 }
10097 }
10098#endif
10099 }
10100
10101 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
10102 {
10103 if (valid != VALID_HEAD + VALID_PATH)
10104 /* xgettext:no-c-format */
10105 *errormsg = (char_u *)_("E499: Empty file name for '%' or '#', only works with \":p:h\"");
10106 else
10107 *errormsg = (char_u *)_("E500: Evaluates to an empty string");
10108 result = NULL;
10109 }
10110 else
10111 result = vim_strnsave(result, resultlen);
10112 vim_free(resultbuf);
10113 return result;
10114}
10115
10116/*
10117 * Concatenate all files in the argument list, separated by spaces, and return
10118 * it in one allocated string.
10119 * Spaces and backslashes in the file names are escaped with a backslash.
10120 * Returns NULL when out of memory.
10121 */
10122 static char_u *
10123arg_all()
10124{
10125 int len;
10126 int idx;
10127 char_u *retval = NULL;
10128 char_u *p;
10129
10130 /*
10131 * Do this loop two times:
10132 * first time: compute the total length
10133 * second time: concatenate the names
10134 */
10135 for (;;)
10136 {
10137 len = 0;
10138 for (idx = 0; idx < ARGCOUNT; ++idx)
10139 {
10140 p = alist_name(&ARGLIST[idx]);
10141 if (p != NULL)
10142 {
10143 if (len > 0)
10144 {
10145 /* insert a space in between names */
10146 if (retval != NULL)
10147 retval[len] = ' ';
10148 ++len;
10149 }
10150 for ( ; *p != NUL; ++p)
10151 {
10152 if (*p == ' ' || *p == '\\')
10153 {
10154 /* insert a backslash */
10155 if (retval != NULL)
10156 retval[len] = '\\';
10157 ++len;
10158 }
10159 if (retval != NULL)
10160 retval[len] = *p;
10161 ++len;
10162 }
10163 }
10164 }
10165
10166 /* second time: break here */
10167 if (retval != NULL)
10168 {
10169 retval[len] = NUL;
10170 break;
10171 }
10172
10173 /* allocate memory */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000010174 retval = alloc((unsigned)len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010175 if (retval == NULL)
10176 break;
10177 }
10178
10179 return retval;
10180}
10181
10182#if defined(FEAT_AUTOCMD) || defined(PROTO)
10183/*
10184 * Expand the <sfile> string in "arg".
10185 *
10186 * Returns an allocated string, or NULL for any error.
10187 */
10188 char_u *
10189expand_sfile(arg)
10190 char_u *arg;
10191{
10192 char_u *errormsg;
10193 int len;
10194 char_u *result;
10195 char_u *newres;
10196 char_u *repl;
10197 int srclen;
10198 char_u *p;
10199
10200 result = vim_strsave(arg);
10201 if (result == NULL)
10202 return NULL;
10203
10204 for (p = result; *p; )
10205 {
10206 if (STRNCMP(p, "<sfile>", 7) != 0)
10207 ++p;
10208 else
10209 {
10210 /* replace "<sfile>" with the sourced file name, and do ":" stuff */
Bram Moolenaar63b92542007-03-27 14:57:09 +000010211 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010212 if (errormsg != NULL)
10213 {
10214 if (*errormsg)
10215 emsg(errormsg);
10216 vim_free(result);
10217 return NULL;
10218 }
10219 if (repl == NULL) /* no match (cannot happen) */
10220 {
10221 p += srclen;
10222 continue;
10223 }
10224 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
10225 newres = alloc(len);
10226 if (newres == NULL)
10227 {
10228 vim_free(repl);
10229 vim_free(result);
10230 return NULL;
10231 }
10232 mch_memmove(newres, result, (size_t)(p - result));
10233 STRCPY(newres + (p - result), repl);
10234 len = (int)STRLEN(newres);
10235 STRCAT(newres, p + srclen);
10236 vim_free(repl);
10237 vim_free(result);
10238 result = newres;
10239 p = newres + len; /* continue after the match */
10240 }
10241 }
10242
10243 return result;
10244}
10245#endif
10246
10247#ifdef FEAT_SESSION
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010248static int ses_winsizes __ARGS((FILE *fd, int restore_size,
10249 win_T *tab_firstwin));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010250static int ses_win_rec __ARGS((FILE *fd, frame_T *fr));
10251static frame_T *ses_skipframe __ARGS((frame_T *fr));
10252static int ses_do_frame __ARGS((frame_T *fr));
10253static int ses_do_win __ARGS((win_T *wp));
10254static int ses_arglist __ARGS((FILE *fd, char *cmd, garray_T *gap, int fullname, unsigned *flagp));
10255static int ses_put_fname __ARGS((FILE *fd, char_u *name, unsigned *flagp));
10256static int ses_fname __ARGS((FILE *fd, buf_T *buf, unsigned *flagp));
10257
10258/*
10259 * Write openfile commands for the current buffers to an .exrc file.
10260 * Return FAIL on error, OK otherwise.
10261 */
10262 static int
10263makeopens(fd, dirnow)
10264 FILE *fd;
10265 char_u *dirnow; /* Current directory name */
10266{
10267 buf_T *buf;
10268 int only_save_windows = TRUE;
10269 int nr;
10270 int cnr = 1;
10271 int restore_size = TRUE;
10272 win_T *wp;
10273 char_u *sname;
10274 win_T *edited_win = NULL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010275 int tabnr;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010276 win_T *tab_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010277 frame_T *tab_topframe;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010278 int cur_arg_idx = 0;
Bram Moolenaar383c6f52008-01-04 15:01:07 +000010279 int next_arg_idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010280
10281 if (ssop_flags & SSOP_BUFFERS)
10282 only_save_windows = FALSE; /* Save ALL buffers */
10283
10284 /*
10285 * Begin by setting the this_session variable, and then other
10286 * sessionable variables.
10287 */
10288#ifdef FEAT_EVAL
10289 if (put_line(fd, "let v:this_session=expand(\"<sfile>:p\")") == FAIL)
10290 return FAIL;
10291 if (ssop_flags & SSOP_GLOBALS)
10292 if (store_session_globals(fd) == FAIL)
10293 return FAIL;
10294#endif
10295
10296 /*
10297 * Close all windows but one.
10298 */
10299 if (put_line(fd, "silent only") == FAIL)
10300 return FAIL;
10301
10302 /*
10303 * Now a :cd command to the session directory or the current directory
10304 */
10305 if (ssop_flags & SSOP_SESDIR)
10306 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010307 if (put_line(fd, "exe \"cd \" . escape(expand(\"<sfile>:p:h\"), ' ')")
10308 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010309 return FAIL;
10310 }
10311 else if (ssop_flags & SSOP_CURDIR)
10312 {
10313 sname = home_replace_save(NULL, globaldir != NULL ? globaldir : dirnow);
10314 if (sname == NULL
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010315 || fputs("cd ", fd) < 0
10316 || ses_put_fname(fd, sname, &ssop_flags) == FAIL
10317 || put_eol(fd) == FAIL)
10318 {
10319 vim_free(sname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010320 return FAIL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010321 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010322 vim_free(sname);
10323 }
10324
10325 /*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010326 * If there is an empty, unnamed buffer we will wipe it out later.
10327 * Remember the buffer number.
10328 */
10329 if (put_line(fd, "if expand('%') == '' && !&modified && line('$') <= 1 && getline(1) == ''") == FAIL)
10330 return FAIL;
10331 if (put_line(fd, " let s:wipebuf = bufnr('%')") == FAIL)
10332 return FAIL;
10333 if (put_line(fd, "endif") == FAIL)
10334 return FAIL;
10335
10336 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010337 * Now save the current files, current buffer first.
10338 */
10339 if (put_line(fd, "set shortmess=aoO") == FAIL)
10340 return FAIL;
10341
10342 /* Now put the other buffers into the buffer list */
10343 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
10344 {
10345 if (!(only_save_windows && buf->b_nwindows == 0)
10346 && !(buf->b_help && !(ssop_flags & SSOP_HELP))
10347 && buf->b_fname != NULL
10348 && buf->b_p_bl)
10349 {
10350 if (fprintf(fd, "badd +%ld ", buf->b_wininfo == NULL ? 1L
10351 : buf->b_wininfo->wi_fpos.lnum) < 0
10352 || ses_fname(fd, buf, &ssop_flags) == FAIL)
10353 return FAIL;
10354 }
10355 }
10356
10357 /* the global argument list */
10358 if (ses_arglist(fd, "args", &global_alist.al_ga,
10359 !(ssop_flags & SSOP_CURDIR), &ssop_flags) == FAIL)
10360 return FAIL;
10361
10362 if (ssop_flags & SSOP_RESIZE)
10363 {
10364 /* Note: after the restore we still check it worked!*/
10365 if (fprintf(fd, "set lines=%ld columns=%ld" , Rows, Columns) < 0
10366 || put_eol(fd) == FAIL)
10367 return FAIL;
10368 }
10369
10370#ifdef FEAT_GUI
10371 if (gui.in_use && (ssop_flags & SSOP_WINPOS))
10372 {
10373 int x, y;
10374
10375 if (gui_mch_get_winpos(&x, &y) == OK)
10376 {
10377 /* Note: after the restore we still check it worked!*/
10378 if (fprintf(fd, "winpos %d %d", x, y) < 0 || put_eol(fd) == FAIL)
10379 return FAIL;
10380 }
10381 }
10382#endif
10383
10384 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000010385 * May repeat putting Windows for each tab, when "tabpages" is in
10386 * 'sessionoptions'.
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010387 * Don't use goto_tabpage(), it may change directory and trigger
10388 * autocommands.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010389 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010390 tab_firstwin = firstwin; /* first window in tab page "tabnr" */
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010391 tab_topframe = topframe;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010392 for (tabnr = 1; ; ++tabnr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010393 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010394 int need_tabnew = FALSE;
10395
Bram Moolenaar18144c82006-04-12 21:52:12 +000010396 if ((ssop_flags & SSOP_TABPAGES))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010397 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010398 tabpage_T *tp = find_tabpage(tabnr);
10399
10400 if (tp == NULL)
10401 break; /* done all tab pages */
10402 if (tp == curtab)
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010403 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010404 tab_firstwin = firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010405 tab_topframe = topframe;
10406 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010407 else
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010408 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010409 tab_firstwin = tp->tp_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010410 tab_topframe = tp->tp_topframe;
10411 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010412 if (tabnr > 1)
10413 need_tabnew = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010414 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010415
Bram Moolenaar18144c82006-04-12 21:52:12 +000010416 /*
10417 * Before creating the window layout, try loading one file. If this
10418 * is aborted we don't end up with a number of useless windows.
10419 * This may have side effects! (e.g., compressed or network file).
10420 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010421 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010422 {
10423 if (ses_do_win(wp)
10424 && wp->w_buffer->b_ffname != NULL
10425 && !wp->w_buffer->b_help
10426#ifdef FEAT_QUICKFIX
10427 && !bt_nofile(wp->w_buffer)
10428#endif
10429 )
10430 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010431 if (fputs(need_tabnew ? "tabedit " : "edit ", fd) < 0
Bram Moolenaar18144c82006-04-12 21:52:12 +000010432 || ses_fname(fd, wp->w_buffer, &ssop_flags) == FAIL)
10433 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010434 need_tabnew = FALSE;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010435 if (!wp->w_arg_idx_invalid)
10436 edited_win = wp;
10437 break;
10438 }
10439 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010440
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010441 /* If no file got edited create an empty tab page. */
10442 if (need_tabnew && put_line(fd, "tabnew") == FAIL)
10443 return FAIL;
10444
Bram Moolenaar18144c82006-04-12 21:52:12 +000010445 /*
10446 * Save current window layout.
10447 */
10448 if (put_line(fd, "set splitbelow splitright") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010449 return FAIL;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010450 if (ses_win_rec(fd, tab_topframe) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010451 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010452 if (!p_sb && put_line(fd, "set nosplitbelow") == FAIL)
10453 return FAIL;
10454 if (!p_spr && put_line(fd, "set nosplitright") == FAIL)
10455 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010456
Bram Moolenaar18144c82006-04-12 21:52:12 +000010457 /*
10458 * Check if window sizes can be restored (no windows omitted).
10459 * Remember the window number of the current window after restoring.
10460 */
10461 nr = 0;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010462 for (wp = tab_firstwin; wp != NULL; wp = W_NEXT(wp))
Bram Moolenaar18144c82006-04-12 21:52:12 +000010463 {
10464 if (ses_do_win(wp))
10465 ++nr;
10466 else
10467 restore_size = FALSE;
10468 if (curwin == wp)
10469 cnr = nr;
10470 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010471
Bram Moolenaar18144c82006-04-12 21:52:12 +000010472 /* Go to the first window. */
10473 if (put_line(fd, "wincmd t") == FAIL)
10474 return FAIL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010475
Bram Moolenaar18144c82006-04-12 21:52:12 +000010476 /*
10477 * If more than one window, see if sizes can be restored.
10478 * First set 'winheight' and 'winwidth' to 1 to avoid the windows being
10479 * resized when moving between windows.
10480 * Do this before restoring the view, so that the topline and the
10481 * cursor can be set. This is done again below.
10482 */
10483 if (put_line(fd, "set winheight=1 winwidth=1") == FAIL)
10484 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010485 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010486 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010487
Bram Moolenaar18144c82006-04-12 21:52:12 +000010488 /*
10489 * Restore the view of the window (options, file, cursor, etc.).
10490 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010491 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010492 {
10493 if (!ses_do_win(wp))
10494 continue;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010495 if (put_view(fd, wp, wp != edited_win, &ssop_flags,
10496 cur_arg_idx) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010497 return FAIL;
10498 if (nr > 1 && put_line(fd, "wincmd w") == FAIL)
10499 return FAIL;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010500 next_arg_idx = wp->w_arg_idx;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010501 }
10502
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010503 /* The argument index in the first tab page is zero, need to set it in
10504 * each window. For further tab pages it's the window where we do
10505 * "tabedit". */
10506 cur_arg_idx = next_arg_idx;
10507
Bram Moolenaar18144c82006-04-12 21:52:12 +000010508 /*
10509 * Restore cursor to the current window if it's not the first one.
10510 */
10511 if (cnr > 1 && (fprintf(fd, "%dwincmd w", cnr) < 0
10512 || put_eol(fd) == FAIL))
10513 return FAIL;
10514
10515 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000010516 * Restore window sizes again after jumping around in windows, because
10517 * the current window has a minimum size while others may not.
10518 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010519 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010520 return FAIL;
10521
Bram Moolenaar18144c82006-04-12 21:52:12 +000010522 /* Don't continue in another tab page when doing only the current one
10523 * or when at the last tab page. */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010524 if (!(ssop_flags & SSOP_TABPAGES))
Bram Moolenaar18144c82006-04-12 21:52:12 +000010525 break;
10526 }
10527
10528 if (ssop_flags & SSOP_TABPAGES)
10529 {
Bram Moolenaar18144c82006-04-12 21:52:12 +000010530 if (fprintf(fd, "tabnext %d", tabpage_index(curtab)) < 0
10531 || put_eol(fd) == FAIL)
10532 return FAIL;
10533 }
10534
Bram Moolenaar9c102382006-05-03 21:26:49 +000010535 /*
10536 * Wipe out an empty unnamed buffer we started in.
10537 */
10538 if (put_line(fd, "if exists('s:wipebuf')") == FAIL)
10539 return FAIL;
Bram Moolenaarf3a67882006-05-05 21:09:41 +000010540 if (put_line(fd, " silent exe 'bwipe ' . s:wipebuf") == FAIL)
Bram Moolenaar9c102382006-05-03 21:26:49 +000010541 return FAIL;
10542 if (put_line(fd, "endif") == FAIL)
10543 return FAIL;
10544 if (put_line(fd, "unlet! s:wipebuf") == FAIL)
10545 return FAIL;
10546
10547 /* Re-apply 'winheight', 'winwidth' and 'shortmess'. */
10548 if (fprintf(fd, "set winheight=%ld winwidth=%ld shortmess=%s",
10549 p_wh, p_wiw, p_shm) < 0 || put_eol(fd) == FAIL)
10550 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010551
10552 /*
10553 * Lastly, execute the x.vim file if it exists.
10554 */
10555 if (put_line(fd, "let s:sx = expand(\"<sfile>:p:r\").\"x.vim\"") == FAIL
10556 || put_line(fd, "if file_readable(s:sx)") == FAIL
Bram Moolenaar42ba1262008-12-09 10:18:03 +000010557 || put_line(fd, " exe \"source \" . fnameescape(s:sx)") == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000010558 || put_line(fd, "endif") == FAIL)
10559 return FAIL;
10560
10561 return OK;
10562}
10563
10564 static int
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010565ses_winsizes(fd, restore_size, tab_firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010566 FILE *fd;
10567 int restore_size;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010568 win_T *tab_firstwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010569{
10570 int n = 0;
10571 win_T *wp;
10572
10573 if (restore_size && (ssop_flags & SSOP_WINSIZE))
10574 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010575 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010576 {
10577 if (!ses_do_win(wp))
10578 continue;
10579 ++n;
10580
10581 /* restore height when not full height */
10582 if (wp->w_height + wp->w_status_height < topframe->fr_height
10583 && (fprintf(fd,
10584 "exe '%dresize ' . ((&lines * %ld + %ld) / %ld)",
10585 n, (long)wp->w_height, Rows / 2, Rows) < 0
10586 || put_eol(fd) == FAIL))
10587 return FAIL;
10588
10589 /* restore width when not full width */
10590 if (wp->w_width < Columns && (fprintf(fd,
10591 "exe 'vert %dresize ' . ((&columns * %ld + %ld) / %ld)",
10592 n, (long)wp->w_width, Columns / 2, Columns) < 0
10593 || put_eol(fd) == FAIL))
10594 return FAIL;
10595 }
10596 }
10597 else
10598 {
10599 /* Just equalise window sizes */
10600 if (put_line(fd, "wincmd =") == FAIL)
10601 return FAIL;
10602 }
10603 return OK;
10604}
10605
10606/*
10607 * Write commands to "fd" to recursively create windows for frame "fr",
10608 * horizontally and vertically split.
10609 * After the commands the last window in the frame is the current window.
10610 * Returns FAIL when writing the commands to "fd" fails.
10611 */
10612 static int
10613ses_win_rec(fd, fr)
10614 FILE *fd;
10615 frame_T *fr;
10616{
10617 frame_T *frc;
10618 int count = 0;
10619
10620 if (fr->fr_layout != FR_LEAF)
10621 {
10622 /* Find first frame that's not skipped and then create a window for
10623 * each following one (first frame is already there). */
10624 frc = ses_skipframe(fr->fr_child);
10625 if (frc != NULL)
10626 while ((frc = ses_skipframe(frc->fr_next)) != NULL)
10627 {
10628 /* Make window as big as possible so that we have lots of room
10629 * to split. */
10630 if (put_line(fd, "wincmd _ | wincmd |") == FAIL
10631 || put_line(fd, fr->fr_layout == FR_COL
10632 ? "split" : "vsplit") == FAIL)
10633 return FAIL;
10634 ++count;
10635 }
10636
10637 /* Go back to the first window. */
10638 if (count > 0 && (fprintf(fd, fr->fr_layout == FR_COL
10639 ? "%dwincmd k" : "%dwincmd h", count) < 0
10640 || put_eol(fd) == FAIL))
10641 return FAIL;
10642
10643 /* Recursively create frames/windows in each window of this column or
10644 * row. */
10645 frc = ses_skipframe(fr->fr_child);
10646 while (frc != NULL)
10647 {
10648 ses_win_rec(fd, frc);
10649 frc = ses_skipframe(frc->fr_next);
10650 /* Go to next window. */
10651 if (frc != NULL && put_line(fd, "wincmd w") == FAIL)
10652 return FAIL;
10653 }
10654 }
10655 return OK;
10656}
10657
10658/*
10659 * Skip frames that don't contain windows we want to save in the Session.
10660 * Returns NULL when there none.
10661 */
10662 static frame_T *
10663ses_skipframe(fr)
10664 frame_T *fr;
10665{
10666 frame_T *frc;
10667
10668 for (frc = fr; frc != NULL; frc = frc->fr_next)
10669 if (ses_do_frame(frc))
10670 break;
10671 return frc;
10672}
10673
10674/*
10675 * Return TRUE if frame "fr" has a window somewhere that we want to save in
10676 * the Session.
10677 */
10678 static int
10679ses_do_frame(fr)
10680 frame_T *fr;
10681{
10682 frame_T *frc;
10683
10684 if (fr->fr_layout == FR_LEAF)
10685 return ses_do_win(fr->fr_win);
10686 for (frc = fr->fr_child; frc != NULL; frc = frc->fr_next)
10687 if (ses_do_frame(frc))
10688 return TRUE;
10689 return FALSE;
10690}
10691
10692/*
10693 * Return non-zero if window "wp" is to be stored in the Session.
10694 */
10695 static int
10696ses_do_win(wp)
10697 win_T *wp;
10698{
10699 if (wp->w_buffer->b_fname == NULL
10700#ifdef FEAT_QUICKFIX
10701 /* When 'buftype' is "nofile" can't restore the window contents. */
10702 || bt_nofile(wp->w_buffer)
10703#endif
10704 )
10705 return (ssop_flags & SSOP_BLANK);
10706 if (wp->w_buffer->b_help)
10707 return (ssop_flags & SSOP_HELP);
10708 return TRUE;
10709}
10710
10711/*
10712 * Write commands to "fd" to restore the view of a window.
10713 * Caller must make sure 'scrolloff' is zero.
10714 */
10715 static int
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010716put_view(fd, wp, add_edit, flagp, current_arg_idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010717 FILE *fd;
10718 win_T *wp;
10719 int add_edit; /* add ":edit" command to view */
10720 unsigned *flagp; /* vop_flags or ssop_flags */
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010721 int current_arg_idx; /* current argument index of the window, use
10722 * -1 if unknown */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010723{
10724 win_T *save_curwin;
10725 int f;
10726 int do_cursor;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010727 int did_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010728
10729 /* Always restore cursor position for ":mksession". For ":mkview" only
10730 * when 'viewoptions' contains "cursor". */
10731 do_cursor = (flagp == &ssop_flags || *flagp & SSOP_CURSOR);
10732
10733 /*
10734 * Local argument list.
10735 */
10736 if (wp->w_alist == &global_alist)
10737 {
10738 if (put_line(fd, "argglobal") == FAIL)
10739 return FAIL;
10740 }
10741 else
10742 {
10743 if (ses_arglist(fd, "arglocal", &wp->w_alist->al_ga,
10744 flagp == &vop_flags
10745 || !(*flagp & SSOP_CURDIR)
10746 || wp->w_localdir != NULL, flagp) == FAIL)
10747 return FAIL;
10748 }
10749
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010750 /* Only when part of a session: restore the argument index. Some
10751 * arguments may have been deleted, check if the index is valid. */
Bram Moolenaar51f53df2010-06-26 05:25:54 +020010752 if (wp->w_arg_idx != current_arg_idx && wp->w_arg_idx < WARGCOUNT(wp)
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010753 && flagp == &ssop_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010754 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010755 if (fprintf(fd, "%ldargu", (long)wp->w_arg_idx + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010756 || put_eol(fd) == FAIL)
10757 return FAIL;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010758 did_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010759 }
10760
10761 /* Edit the file. Skip this when ":next" already did it. */
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010762 if (add_edit && (!did_next || wp->w_arg_idx_invalid))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010763 {
10764 /*
10765 * Load the file.
10766 */
10767 if (wp->w_buffer->b_ffname != NULL
10768#ifdef FEAT_QUICKFIX
10769 && !bt_nofile(wp->w_buffer)
10770#endif
10771 )
10772 {
10773 /*
10774 * Editing a file in this buffer: use ":edit file".
10775 * This may have side effects! (e.g., compressed or network file).
10776 */
10777 if (fputs("edit ", fd) < 0
10778 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
10779 return FAIL;
10780 }
10781 else
10782 {
10783 /* No file in this buffer, just make it empty. */
10784 if (put_line(fd, "enew") == FAIL)
10785 return FAIL;
10786#ifdef FEAT_QUICKFIX
10787 if (wp->w_buffer->b_ffname != NULL)
10788 {
10789 /* The buffer does have a name, but it's not a file name. */
10790 if (fputs("file ", fd) < 0
10791 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
10792 return FAIL;
10793 }
10794#endif
10795 do_cursor = FALSE;
10796 }
10797 }
10798
10799 /*
10800 * Local mappings and abbreviations.
10801 */
10802 if ((*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
10803 && makemap(fd, wp->w_buffer) == FAIL)
10804 return FAIL;
10805
10806 /*
10807 * Local options. Need to go to the window temporarily.
10808 * Store only local values when using ":mkview" and when ":mksession" is
10809 * used and 'sessionoptions' doesn't include "options".
10810 * Some folding options are always stored when "folds" is included,
10811 * otherwise the folds would not be restored correctly.
10812 */
10813 save_curwin = curwin;
10814 curwin = wp;
10815 curbuf = curwin->w_buffer;
10816 if (*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
10817 f = makeset(fd, OPT_LOCAL,
10818 flagp == &vop_flags || !(*flagp & SSOP_OPTIONS));
10819#ifdef FEAT_FOLDING
10820 else if (*flagp & SSOP_FOLDS)
10821 f = makefoldset(fd);
10822#endif
10823 else
10824 f = OK;
10825 curwin = save_curwin;
10826 curbuf = curwin->w_buffer;
10827 if (f == FAIL)
10828 return FAIL;
10829
10830#ifdef FEAT_FOLDING
10831 /*
10832 * Save Folds when 'buftype' is empty and for help files.
10833 */
10834 if ((*flagp & SSOP_FOLDS)
10835 && wp->w_buffer->b_ffname != NULL
Bram Moolenaarb1b715d2006-01-21 22:09:43 +000010836# ifdef FEAT_QUICKFIX
10837 && (*wp->w_buffer->b_p_bt == NUL || wp->w_buffer->b_help)
10838# endif
10839 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000010840 {
10841 if (put_folds(fd, wp) == FAIL)
10842 return FAIL;
10843 }
10844#endif
10845
10846 /*
10847 * Set the cursor after creating folds, since that moves the cursor.
10848 */
10849 if (do_cursor)
10850 {
10851
10852 /* Restore the cursor line in the file and relatively in the
10853 * window. Don't use "G", it changes the jumplist. */
10854 if (fprintf(fd, "let s:l = %ld - ((%ld * winheight(0) + %ld) / %ld)",
10855 (long)wp->w_cursor.lnum,
10856 (long)(wp->w_cursor.lnum - wp->w_topline),
10857 (long)wp->w_height / 2, (long)wp->w_height) < 0
10858 || put_eol(fd) == FAIL
10859 || put_line(fd, "if s:l < 1 | let s:l = 1 | endif") == FAIL
10860 || put_line(fd, "exe s:l") == FAIL
10861 || put_line(fd, "normal! zt") == FAIL
10862 || fprintf(fd, "%ld", (long)wp->w_cursor.lnum) < 0
10863 || put_eol(fd) == FAIL)
10864 return FAIL;
10865 /* Restore the cursor column and left offset when not wrapping. */
10866 if (wp->w_cursor.col == 0)
10867 {
10868 if (put_line(fd, "normal! 0") == FAIL)
10869 return FAIL;
10870 }
10871 else
10872 {
10873 if (!wp->w_p_wrap && wp->w_leftcol > 0 && wp->w_width > 0)
10874 {
10875 if (fprintf(fd,
10876 "let s:c = %ld - ((%ld * winwidth(0) + %ld) / %ld)",
Bram Moolenaar558ddad2013-02-20 19:26:29 +010010877 (long)wp->w_virtcol + 1,
10878 (long)(wp->w_virtcol - wp->w_leftcol),
Bram Moolenaar071d4272004-06-13 20:20:40 +000010879 (long)wp->w_width / 2, (long)wp->w_width) < 0
10880 || put_eol(fd) == FAIL
10881 || put_line(fd, "if s:c > 0") == FAIL
10882 || fprintf(fd,
Bram Moolenaar558ddad2013-02-20 19:26:29 +010010883 " exe 'normal! ' . s:c . '|zs' . %ld . '|'",
10884 (long)wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010885 || put_eol(fd) == FAIL
10886 || put_line(fd, "else") == FAIL
Bram Moolenaarfdf447b2013-02-26 17:21:29 +010010887 || fprintf(fd, " normal! 0%d|", wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010888 || put_eol(fd) == FAIL
10889 || put_line(fd, "endif") == FAIL)
10890 return FAIL;
10891 }
10892 else
10893 {
Bram Moolenaar558ddad2013-02-20 19:26:29 +010010894 if (fprintf(fd, "normal! 0%d|", wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010895 || put_eol(fd) == FAIL)
10896 return FAIL;
10897 }
10898 }
10899 }
10900
10901 /*
10902 * Local directory.
10903 */
10904 if (wp->w_localdir != NULL)
10905 {
10906 if (fputs("lcd ", fd) < 0
10907 || ses_put_fname(fd, wp->w_localdir, flagp) == FAIL
10908 || put_eol(fd) == FAIL)
10909 return FAIL;
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010910 did_lcd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010911 }
10912
10913 return OK;
10914}
10915
10916/*
10917 * Write an argument list to the session file.
10918 * Returns FAIL if writing fails.
10919 */
10920 static int
10921ses_arglist(fd, cmd, gap, fullname, flagp)
10922 FILE *fd;
10923 char *cmd;
10924 garray_T *gap;
10925 int fullname; /* TRUE: use full path name */
10926 unsigned *flagp;
10927{
10928 int i;
Bram Moolenaard9462e32011-04-11 21:35:11 +020010929 char_u *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010930 char_u *s;
10931
10932 if (gap->ga_len == 0)
10933 return put_line(fd, "silent! argdel *");
10934 if (fputs(cmd, fd) < 0)
10935 return FAIL;
10936 for (i = 0; i < gap->ga_len; ++i)
10937 {
10938 /* NULL file names are skipped (only happens when out of memory). */
10939 s = alist_name(&((aentry_T *)gap->ga_data)[i]);
10940 if (s != NULL)
10941 {
10942 if (fullname)
10943 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020010944 buf = alloc(MAXPATHL);
10945 if (buf != NULL)
10946 {
10947 (void)vim_FullName(s, buf, MAXPATHL, FALSE);
10948 s = buf;
10949 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010950 }
10951 if (fputs(" ", fd) < 0 || ses_put_fname(fd, s, flagp) == FAIL)
Bram Moolenaard9462e32011-04-11 21:35:11 +020010952 {
10953 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010954 return FAIL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020010955 }
10956 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010957 }
10958 }
10959 return put_eol(fd);
10960}
10961
10962/*
10963 * Write a buffer name to the session file.
10964 * Also ends the line.
10965 * Returns FAIL if writing fails.
10966 */
10967 static int
10968ses_fname(fd, buf, flagp)
10969 FILE *fd;
10970 buf_T *buf;
10971 unsigned *flagp;
10972{
10973 char_u *name;
10974
10975 /* Use the short file name if the current directory is known at the time
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010976 * the session file will be sourced.
10977 * Don't do this for ":mkview", we don't know the current directory.
10978 * Don't do this after ":lcd", we don't keep track of what the current
10979 * directory is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010980 if (buf->b_sfname != NULL
10981 && flagp == &ssop_flags
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010982 && (ssop_flags & (SSOP_CURDIR | SSOP_SESDIR))
Bram Moolenaar8d594672009-07-01 18:18:57 +000010983#ifdef FEAT_AUTOCHDIR
10984 && !p_acd
10985#endif
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010986 && !did_lcd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010987 name = buf->b_sfname;
10988 else
10989 name = buf->b_ffname;
10990 if (ses_put_fname(fd, name, flagp) == FAIL || put_eol(fd) == FAIL)
10991 return FAIL;
10992 return OK;
10993}
10994
10995/*
10996 * Write a file name to the session file.
10997 * Takes care of the "slash" option in 'sessionoptions' and escapes special
10998 * characters.
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010999 * Returns FAIL if writing fails or out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011000 */
11001 static int
11002ses_put_fname(fd, name, flagp)
11003 FILE *fd;
11004 char_u *name;
11005 unsigned *flagp;
11006{
11007 char_u *sname;
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011008 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011009 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011010
11011 sname = home_replace_save(NULL, name);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011012 if (sname == NULL)
11013 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011014
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011015 if (*flagp & SSOP_SLASH)
11016 {
11017 /* change all backslashes to forward slashes */
11018 for (p = sname; *p != NUL; mb_ptr_adv(p))
11019 if (*p == '\\')
11020 *p = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +000011021 }
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011022
Bram Moolenaar84a05ac2013-05-06 04:24:17 +020011023 /* escape special characters */
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011024 p = vim_strsave_fnameescape(sname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011025 vim_free(sname);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011026 if (p == NULL)
11027 return FAIL;
11028
11029 /* write the result */
11030 if (fputs((char *)p, fd) < 0)
11031 retval = FAIL;
11032
11033 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011034 return retval;
11035}
11036
11037/*
11038 * ":loadview [nr]"
11039 */
11040 static void
11041ex_loadview(eap)
11042 exarg_T *eap;
11043{
11044 char_u *fname;
11045
11046 fname = get_view_file(*eap->arg);
11047 if (fname != NULL)
11048 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +000011049 do_source(fname, FALSE, DOSO_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011050 vim_free(fname);
11051 }
11052}
11053
11054/*
11055 * Get the name of the view file for the current buffer.
11056 */
11057 static char_u *
11058get_view_file(c)
11059 int c;
11060{
11061 int len = 0;
11062 char_u *p, *s;
11063 char_u *retval;
11064 char_u *sname;
11065
11066 if (curbuf->b_ffname == NULL)
11067 {
11068 EMSG(_(e_noname));
11069 return NULL;
11070 }
11071 sname = home_replace_save(NULL, curbuf->b_ffname);
11072 if (sname == NULL)
11073 return NULL;
11074
11075 /*
11076 * We want a file name without separators, because we're not going to make
11077 * a directory.
11078 * "normal" path separator -> "=+"
11079 * "=" -> "=="
11080 * ":" path separator -> "=-"
11081 */
11082 for (p = sname; *p; ++p)
11083 if (*p == '=' || vim_ispathsep(*p))
11084 ++len;
11085 retval = alloc((unsigned)(STRLEN(sname) + len + STRLEN(p_vdir) + 9));
11086 if (retval != NULL)
11087 {
11088 STRCPY(retval, p_vdir);
11089 add_pathsep(retval);
11090 s = retval + STRLEN(retval);
11091 for (p = sname; *p; ++p)
11092 {
11093 if (*p == '=')
11094 {
11095 *s++ = '=';
11096 *s++ = '=';
11097 }
11098 else if (vim_ispathsep(*p))
11099 {
11100 *s++ = '=';
Bram Moolenaare60acc12011-05-10 16:41:25 +020011101#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011102 if (*p == ':')
11103 *s++ = '-';
11104 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000011105#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000011106 *s++ = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +000011107 }
11108 else
11109 *s++ = *p;
11110 }
11111 *s++ = '=';
11112 *s++ = c;
11113 STRCPY(s, ".vim");
11114 }
11115
11116 vim_free(sname);
11117 return retval;
11118}
11119
11120#endif /* FEAT_SESSION */
11121
11122/*
11123 * Write end-of-line character(s) for ":mkexrc", ":mkvimrc" and ":mksession".
11124 * Return FAIL for a write error.
11125 */
11126 int
11127put_eol(fd)
11128 FILE *fd;
11129{
11130 if (
11131#ifdef USE_CRNL
11132 (
11133# ifdef MKSESSION_NL
11134 !mksession_nl &&
11135# endif
11136 (putc('\r', fd) < 0)) ||
11137#endif
11138 (putc('\n', fd) < 0))
11139 return FAIL;
11140 return OK;
11141}
11142
11143/*
11144 * Write a line to "fd".
11145 * Return FAIL for a write error.
11146 */
11147 int
11148put_line(fd, s)
11149 FILE *fd;
11150 char *s;
11151{
11152 if (fputs(s, fd) < 0 || put_eol(fd) == FAIL)
11153 return FAIL;
11154 return OK;
11155}
11156
11157#ifdef FEAT_VIMINFO
11158/*
11159 * ":rviminfo" and ":wviminfo".
11160 */
11161 static void
11162ex_viminfo(eap)
11163 exarg_T *eap;
11164{
11165 char_u *save_viminfo;
11166
11167 save_viminfo = p_viminfo;
11168 if (*p_viminfo == NUL)
11169 p_viminfo = (char_u *)"'100";
11170 if (eap->cmdidx == CMD_rviminfo)
11171 {
Bram Moolenaard812df62008-11-09 12:46:09 +000011172 if (read_viminfo(eap->arg, VIF_WANT_INFO | VIF_WANT_MARKS
11173 | (eap->forceit ? VIF_FORCEIT : 0)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011174 EMSG(_("E195: Cannot open viminfo file for reading"));
11175 }
11176 else
11177 write_viminfo(eap->arg, eap->forceit);
11178 p_viminfo = save_viminfo;
11179}
11180#endif
11181
11182#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +000011183/*
Bram Moolenaard9462e32011-04-11 21:35:11 +020011184 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +000011185 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +000011186 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011187 void
11188dialog_msg(buff, format, fname)
11189 char_u *buff;
11190 char *format;
11191 char_u *fname;
11192{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011193 if (fname == NULL)
11194 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +020011195 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011196}
11197#endif
11198
11199/*
11200 * ":behave {mswin,xterm}"
11201 */
11202 static void
11203ex_behave(eap)
11204 exarg_T *eap;
11205{
11206 if (STRCMP(eap->arg, "mswin") == 0)
11207 {
11208 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
11209 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
11210 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
11211 set_option_value((char_u *)"keymodel", 0L,
11212 (char_u *)"startsel,stopsel", 0);
11213 }
11214 else if (STRCMP(eap->arg, "xterm") == 0)
11215 {
11216 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
11217 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
11218 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
11219 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
11220 }
11221 else
11222 EMSG2(_(e_invarg2), eap->arg);
11223}
11224
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010011225#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
11226/*
11227 * Function given to ExpandGeneric() to obtain the possible arguments of the
11228 * ":behave {mswin,xterm}" command.
11229 */
11230 char_u *
11231get_behave_arg(xp, idx)
11232 expand_T *xp UNUSED;
11233 int idx;
11234{
11235 if (idx == 0)
11236 return (char_u *)"mswin";
11237 if (idx == 1)
11238 return (char_u *)"xterm";
11239 return NULL;
11240}
11241#endif
11242
Bram Moolenaar071d4272004-06-13 20:20:40 +000011243#ifdef FEAT_AUTOCMD
11244static int filetype_detect = FALSE;
11245static int filetype_plugin = FALSE;
11246static int filetype_indent = FALSE;
11247
11248/*
11249 * ":filetype [plugin] [indent] {on,off,detect}"
11250 * on: Load the filetype.vim file to install autocommands for file types.
11251 * off: Load the ftoff.vim file to remove all autocommands for file types.
11252 * plugin on: load filetype.vim and ftplugin.vim
11253 * plugin off: load ftplugof.vim
11254 * indent on: load filetype.vim and indent.vim
11255 * indent off: load indoff.vim
11256 */
11257 static void
11258ex_filetype(eap)
11259 exarg_T *eap;
11260{
11261 char_u *arg = eap->arg;
11262 int plugin = FALSE;
11263 int indent = FALSE;
11264
11265 if (*eap->arg == NUL)
11266 {
11267 /* Print current status. */
11268 smsg((char_u *)"filetype detection:%s plugin:%s indent:%s",
11269 filetype_detect ? "ON" : "OFF",
11270 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
11271 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
11272 return;
11273 }
11274
11275 /* Accept "plugin" and "indent" in any order. */
11276 for (;;)
11277 {
11278 if (STRNCMP(arg, "plugin", 6) == 0)
11279 {
11280 plugin = TRUE;
11281 arg = skipwhite(arg + 6);
11282 continue;
11283 }
11284 if (STRNCMP(arg, "indent", 6) == 0)
11285 {
11286 indent = TRUE;
11287 arg = skipwhite(arg + 6);
11288 continue;
11289 }
11290 break;
11291 }
11292 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
11293 {
11294 if (*arg == 'o' || !filetype_detect)
11295 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011296 source_runtime((char_u *)FILETYPE_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011297 filetype_detect = TRUE;
11298 if (plugin)
11299 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011300 source_runtime((char_u *)FTPLUGIN_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011301 filetype_plugin = TRUE;
11302 }
11303 if (indent)
11304 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011305 source_runtime((char_u *)INDENT_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011306 filetype_indent = TRUE;
11307 }
11308 }
11309 if (*arg == 'd')
11310 {
11311 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE);
Bram Moolenaara3227e22006-03-08 21:32:40 +000011312 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011313 }
11314 }
11315 else if (STRCMP(arg, "off") == 0)
11316 {
11317 if (plugin || indent)
11318 {
11319 if (plugin)
11320 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011321 source_runtime((char_u *)FTPLUGOF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011322 filetype_plugin = FALSE;
11323 }
11324 if (indent)
11325 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011326 source_runtime((char_u *)INDOFF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011327 filetype_indent = FALSE;
11328 }
11329 }
11330 else
11331 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011332 source_runtime((char_u *)FTOFF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011333 filetype_detect = FALSE;
11334 }
11335 }
11336 else
11337 EMSG2(_(e_invarg2), arg);
11338}
11339
11340/*
11341 * ":setfiletype {name}"
11342 */
11343 static void
11344ex_setfiletype(eap)
11345 exarg_T *eap;
11346{
11347 if (!did_filetype)
11348 set_option_value((char_u *)"filetype", 0L, eap->arg, OPT_LOCAL);
11349}
11350#endif
11351
Bram Moolenaar071d4272004-06-13 20:20:40 +000011352 static void
11353ex_digraphs(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +000011354 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011355{
11356#ifdef FEAT_DIGRAPHS
11357 if (*eap->arg != NUL)
11358 putdigraph(eap->arg);
11359 else
11360 listdigraphs();
11361#else
11362 EMSG(_("E196: No digraphs in this version"));
11363#endif
11364}
11365
11366 static void
11367ex_set(eap)
11368 exarg_T *eap;
11369{
11370 int flags = 0;
11371
11372 if (eap->cmdidx == CMD_setlocal)
11373 flags = OPT_LOCAL;
11374 else if (eap->cmdidx == CMD_setglobal)
11375 flags = OPT_GLOBAL;
11376#if defined(FEAT_EVAL) && defined(FEAT_AUTOCMD) && defined(FEAT_BROWSE)
11377 if (cmdmod.browse && flags == 0)
11378 ex_options(eap);
11379 else
11380#endif
11381 (void)do_set(eap->arg, flags);
11382}
11383
11384#ifdef FEAT_SEARCH_EXTRA
11385/*
11386 * ":nohlsearch"
11387 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011388 static void
11389ex_nohlsearch(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +000011390 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011391{
11392 no_hlsearch = TRUE;
Bram Moolenaarf71a3db2006-03-12 21:50:18 +000011393 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011394}
11395
11396/*
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011397 * ":[N]match {group} {pattern}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000011398 * Sets nextcmd to the start of the next command, if any. Also called when
11399 * skipping commands to find the next command.
11400 */
11401 static void
11402ex_match(eap)
11403 exarg_T *eap;
11404{
11405 char_u *p;
Bram Moolenaar52d36c82007-08-11 14:00:30 +000011406 char_u *g = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011407 char_u *end;
11408 int c;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011409 int id;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011410
11411 if (eap->line2 <= 3)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011412 id = eap->line2;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011413 else
11414 {
11415 EMSG(e_invcmd);
11416 return;
11417 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011418
11419 /* First clear any old pattern. */
11420 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011421 match_delete(curwin, id, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011422
11423 if (ends_excmd(*eap->arg))
11424 end = eap->arg;
11425 else if ((STRNICMP(eap->arg, "none", 4) == 0
11426 && (vim_iswhite(eap->arg[4]) || ends_excmd(eap->arg[4]))))
11427 end = eap->arg + 4;
11428 else
11429 {
11430 p = skiptowhite(eap->arg);
11431 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011432 g = vim_strnsave(eap->arg, (int)(p - eap->arg));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011433 p = skipwhite(p);
11434 if (*p == NUL)
11435 {
11436 /* There must be two arguments. */
11437 EMSG2(_(e_invarg2), eap->arg);
11438 return;
11439 }
11440 end = skip_regexp(p + 1, *p, TRUE, NULL);
11441 if (!eap->skip)
11442 {
11443 if (*end != NUL && !ends_excmd(*skipwhite(end + 1)))
11444 {
11445 eap->errmsg = e_trailing;
11446 return;
11447 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000011448 if (*end != *p)
11449 {
11450 EMSG2(_(e_invarg2), p);
11451 return;
11452 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011453
11454 c = *end;
11455 *end = NUL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011456 match_add(curwin, g, p + 1, 10, id);
11457 vim_free(g);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000011458 *end = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011459 }
11460 }
11461 eap->nextcmd = find_nextcmd(end);
11462}
11463#endif
11464
11465#ifdef FEAT_CRYPT
11466/*
11467 * ":X": Get crypt key
11468 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011469 static void
11470ex_X(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +000011471 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011472{
Bram Moolenaar49771f42010-07-20 17:32:38 +020011473 if (get_crypt_method(curbuf) == 0 || blowfish_self_test() == OK)
Bram Moolenaar40e6a712010-05-16 22:32:54 +020011474 (void)get_crypt_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011475}
11476#endif
11477
11478#ifdef FEAT_FOLDING
11479 static void
11480ex_fold(eap)
11481 exarg_T *eap;
11482{
11483 if (foldManualAllowed(TRUE))
11484 foldCreate(eap->line1, eap->line2);
11485}
11486
11487 static void
11488ex_foldopen(eap)
11489 exarg_T *eap;
11490{
11491 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
11492 eap->forceit, FALSE);
11493}
11494
11495 static void
11496ex_folddo(eap)
11497 exarg_T *eap;
11498{
11499 linenr_T lnum;
11500
11501 /* First set the marks for all lines closed/open. */
11502 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
11503 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
11504 ml_setmarked(lnum);
11505
11506 /* Execute the command on the marked lines. */
11507 global_exe(eap->arg);
11508 ml_clearmarked(); /* clear rest of the marks */
11509}
11510#endif