blob: 9b8bf892708faa25f06efa692cc8ee7ed6f6f689 [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 Moolenaar8a7f5a22013-06-06 14:01:46 +0200245#if !defined(FEAT_SYN_HL) || !defined(FEAT_RELTIME)
246# 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;
3123 if (!isalpha(p[j]) && j >= cmdmods[i].minlen
3124 && (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 Moolenaar071d4272004-06-13 20:20:40 +00003606 case CMD_topleft:
3607 case CMD_verbose:
3608 case CMD_vertical:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003609 case CMD_windo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610 return arg;
3611
Bram Moolenaar4f688582007-07-24 12:34:30 +00003612#ifdef FEAT_CMDL_COMPL
3613# ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614 case CMD_match:
3615 if (*arg == NUL || !ends_excmd(*arg))
3616 {
Bram Moolenaar4f688582007-07-24 12:34:30 +00003617 /* also complete "None" */
3618 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619 arg = skipwhite(skiptowhite(arg));
3620 if (*arg != NUL)
3621 {
3622 xp->xp_context = EXPAND_NOTHING;
3623 arg = skip_regexp(arg + 1, *arg, p_magic, NULL);
3624 }
3625 }
3626 return find_nextcmd(arg);
Bram Moolenaar4f688582007-07-24 12:34:30 +00003627# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629/*
3630 * All completion for the +cmdline_compl feature goes here.
3631 */
3632
3633# ifdef FEAT_USR_CMDS
3634 case CMD_command:
3635 /* Check for attributes */
3636 while (*arg == '-')
3637 {
3638 arg++; /* Skip "-" */
3639 p = skiptowhite(arg);
3640 if (*p == NUL)
3641 {
3642 /* Cursor is still in the attribute */
3643 p = vim_strchr(arg, '=');
3644 if (p == NULL)
3645 {
3646 /* No "=", so complete attribute names */
3647 xp->xp_context = EXPAND_USER_CMD_FLAGS;
3648 xp->xp_pattern = arg;
3649 return NULL;
3650 }
3651
3652 /* For the -complete and -nargs attributes, we complete
3653 * their arguments as well.
3654 */
3655 if (STRNICMP(arg, "complete", p - arg) == 0)
3656 {
3657 xp->xp_context = EXPAND_USER_COMPLETE;
3658 xp->xp_pattern = p + 1;
3659 return NULL;
3660 }
3661 else if (STRNICMP(arg, "nargs", p - arg) == 0)
3662 {
3663 xp->xp_context = EXPAND_USER_NARGS;
3664 xp->xp_pattern = p + 1;
3665 return NULL;
3666 }
3667 return NULL;
3668 }
3669 arg = skipwhite(p);
3670 }
3671
3672 /* After the attributes comes the new command name */
3673 p = skiptowhite(arg);
3674 if (*p == NUL)
3675 {
3676 xp->xp_context = EXPAND_USER_COMMANDS;
3677 xp->xp_pattern = arg;
3678 break;
3679 }
3680
3681 /* And finally comes a normal command */
3682 return skipwhite(p);
3683
3684 case CMD_delcommand:
3685 xp->xp_context = EXPAND_USER_COMMANDS;
3686 xp->xp_pattern = arg;
3687 break;
3688# endif
3689
3690 case CMD_global:
3691 case CMD_vglobal:
3692 delim = *arg; /* get the delimiter */
3693 if (delim)
3694 ++arg; /* skip delimiter if there is one */
3695
3696 while (arg[0] != NUL && arg[0] != delim)
3697 {
3698 if (arg[0] == '\\' && arg[1] != NUL)
3699 ++arg;
3700 ++arg;
3701 }
3702 if (arg[0] != NUL)
3703 return arg + 1;
3704 break;
3705 case CMD_and:
3706 case CMD_substitute:
3707 delim = *arg;
3708 if (delim)
3709 {
3710 /* skip "from" part */
3711 ++arg;
3712 arg = skip_regexp(arg, delim, p_magic, NULL);
3713 }
3714 /* skip "to" part */
3715 while (arg[0] != NUL && arg[0] != delim)
3716 {
3717 if (arg[0] == '\\' && arg[1] != NUL)
3718 ++arg;
3719 ++arg;
3720 }
3721 if (arg[0] != NUL) /* skip delimiter */
3722 ++arg;
3723 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
3724 ++arg;
3725 if (arg[0] != NUL)
3726 return arg;
3727 break;
3728 case CMD_isearch:
3729 case CMD_dsearch:
3730 case CMD_ilist:
3731 case CMD_dlist:
3732 case CMD_ijump:
3733 case CMD_psearch:
3734 case CMD_djump:
3735 case CMD_isplit:
3736 case CMD_dsplit:
3737 arg = skipwhite(skipdigits(arg)); /* skip count */
3738 if (*arg == '/') /* Match regexp, not just whole words */
3739 {
3740 for (++arg; *arg && *arg != '/'; arg++)
3741 if (*arg == '\\' && arg[1] != NUL)
3742 arg++;
3743 if (*arg)
3744 {
3745 arg = skipwhite(arg + 1);
3746
3747 /* Check for trailing illegal characters */
3748 if (*arg && vim_strchr((char_u *)"|\"\n", *arg) == NULL)
3749 xp->xp_context = EXPAND_NOTHING;
3750 else
3751 return arg;
3752 }
3753 }
3754 break;
3755#ifdef FEAT_AUTOCMD
3756 case CMD_autocmd:
3757 return set_context_in_autocmd(xp, arg, FALSE);
3758
3759 case CMD_doautocmd:
Bram Moolenaar73a9d7b2008-11-06 16:16:44 +00003760 case CMD_doautoall:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761 return set_context_in_autocmd(xp, arg, TRUE);
3762#endif
3763 case CMD_set:
3764 set_context_in_set_cmd(xp, arg, 0);
3765 break;
3766 case CMD_setglobal:
3767 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
3768 break;
3769 case CMD_setlocal:
3770 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
3771 break;
3772 case CMD_tag:
3773 case CMD_stag:
3774 case CMD_ptag:
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00003775 case CMD_ltag:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003776 case CMD_tselect:
3777 case CMD_stselect:
3778 case CMD_ptselect:
3779 case CMD_tjump:
3780 case CMD_stjump:
3781 case CMD_ptjump:
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003782 if (*p_wop != NUL)
3783 xp->xp_context = EXPAND_TAGS_LISTFILES;
3784 else
3785 xp->xp_context = EXPAND_TAGS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003786 xp->xp_pattern = arg;
3787 break;
3788 case CMD_augroup:
3789 xp->xp_context = EXPAND_AUGROUP;
3790 xp->xp_pattern = arg;
3791 break;
3792#ifdef FEAT_SYN_HL
3793 case CMD_syntax:
3794 set_context_in_syntax_cmd(xp, arg);
3795 break;
3796#endif
3797#ifdef FEAT_EVAL
3798 case CMD_let:
3799 case CMD_if:
3800 case CMD_elseif:
3801 case CMD_while:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00003802 case CMD_for:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803 case CMD_echo:
3804 case CMD_echon:
3805 case CMD_execute:
3806 case CMD_echomsg:
3807 case CMD_echoerr:
3808 case CMD_call:
3809 case CMD_return:
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003810 set_context_for_expression(xp, arg, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811 break;
3812
3813 case CMD_unlet:
3814 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3815 arg = xp->xp_pattern + 1;
3816 xp->xp_context = EXPAND_USER_VARS;
3817 xp->xp_pattern = arg;
3818 break;
3819
3820 case CMD_function:
3821 case CMD_delfunction:
3822 xp->xp_context = EXPAND_USER_FUNC;
3823 xp->xp_pattern = arg;
3824 break;
3825
3826 case CMD_echohl:
Bram Moolenaar4f688582007-07-24 12:34:30 +00003827 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003828 break;
3829#endif
3830 case CMD_highlight:
3831 set_context_in_highlight_cmd(xp, arg);
3832 break;
Bram Moolenaarf4580d82009-03-18 11:52:53 +00003833#ifdef FEAT_CSCOPE
3834 case CMD_cscope:
Bram Moolenaar7bfef802009-04-22 14:25:01 +00003835 case CMD_lcscope:
3836 case CMD_scscope:
3837 set_context_in_cscope_cmd(xp, arg, ea.cmdidx);
Bram Moolenaarf4580d82009-03-18 11:52:53 +00003838 break;
3839#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00003840#ifdef FEAT_SIGNS
3841 case CMD_sign:
3842 set_context_in_sign_cmd(xp, arg);
3843 break;
3844#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003845#ifdef FEAT_LISTCMDS
3846 case CMD_bdelete:
3847 case CMD_bwipeout:
3848 case CMD_bunload:
3849 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3850 arg = xp->xp_pattern + 1;
3851 /*FALLTHROUGH*/
3852 case CMD_buffer:
3853 case CMD_sbuffer:
3854 case CMD_checktime:
3855 xp->xp_context = EXPAND_BUFFERS;
3856 xp->xp_pattern = arg;
3857 break;
3858#endif
3859#ifdef FEAT_USR_CMDS
3860 case CMD_USER:
3861 case CMD_USER_BUF:
3862 if (compl != EXPAND_NOTHING)
3863 {
3864 /* XFILE: file names are handled above */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003865 if (!(ea.argt & XFILE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003866 {
3867# ifdef FEAT_MENU
3868 if (compl == EXPAND_MENUS)
3869 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
3870# endif
3871 if (compl == EXPAND_COMMANDS)
3872 return arg;
3873 if (compl == EXPAND_MAPPINGS)
3874 return set_context_in_map_cmd(xp, (char_u *)"map",
3875 arg, forceit, FALSE, FALSE, CMD_map);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003876 /* Find start of last argument. */
3877 p = arg;
3878 while (*p)
3879 {
3880 if (*p == ' ')
Bram Moolenaar848f8762012-07-25 17:22:23 +02003881 /* argument starts after a space */
3882 arg = p + 1;
Bram Moolenaara07c8312012-07-27 21:12:07 +02003883 else if (*p == '\\' && *(p + 1) != NUL)
3884 ++p; /* skip over escaped character */
3885 mb_ptr_adv(p);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003886 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887 xp->xp_pattern = arg;
3888 }
3889 xp->xp_context = compl;
3890 }
3891 break;
3892#endif
3893 case CMD_map: case CMD_noremap:
3894 case CMD_nmap: case CMD_nnoremap:
3895 case CMD_vmap: case CMD_vnoremap:
3896 case CMD_omap: case CMD_onoremap:
3897 case CMD_imap: case CMD_inoremap:
3898 case CMD_cmap: case CMD_cnoremap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003899 case CMD_lmap: case CMD_lnoremap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02003900 case CMD_smap: case CMD_snoremap:
3901 case CMD_xmap: case CMD_xnoremap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003902 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003903 FALSE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003904 case CMD_unmap:
3905 case CMD_nunmap:
3906 case CMD_vunmap:
3907 case CMD_ounmap:
3908 case CMD_iunmap:
3909 case CMD_cunmap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003910 case CMD_lunmap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02003911 case CMD_sunmap:
3912 case CMD_xunmap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003913 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003914 FALSE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003915 case CMD_abbreviate: case CMD_noreabbrev:
3916 case CMD_cabbrev: case CMD_cnoreabbrev:
3917 case CMD_iabbrev: case CMD_inoreabbrev:
3918 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003919 TRUE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920 case CMD_unabbreviate:
3921 case CMD_cunabbrev:
3922 case CMD_iunabbrev:
3923 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003924 TRUE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003925#ifdef FEAT_MENU
3926 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
3927 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
3928 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
3929 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
3930 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
3931 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
3932 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
3933 case CMD_tmenu: case CMD_tunmenu:
3934 case CMD_popup: case CMD_tearoff: case CMD_emenu:
3935 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
3936#endif
3937
3938 case CMD_colorscheme:
3939 xp->xp_context = EXPAND_COLORS;
3940 xp->xp_pattern = arg;
3941 break;
3942
3943 case CMD_compiler:
3944 xp->xp_context = EXPAND_COMPILER;
3945 xp->xp_pattern = arg;
3946 break;
3947
Bram Moolenaar883f5d02010-06-21 06:24:34 +02003948 case CMD_ownsyntax:
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02003949 xp->xp_context = EXPAND_OWNSYNTAX;
3950 xp->xp_pattern = arg;
3951 break;
3952
3953 case CMD_setfiletype:
Bram Moolenaar883f5d02010-06-21 06:24:34 +02003954 xp->xp_context = EXPAND_FILETYPE;
3955 xp->xp_pattern = arg;
3956 break;
3957
Bram Moolenaar071d4272004-06-13 20:20:40 +00003958#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
3959 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
3960 case CMD_language:
Bram Moolenaara660dc82011-05-25 12:51:22 +02003961 p = skiptowhite(arg);
3962 if (*p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003963 {
3964 xp->xp_context = EXPAND_LANGUAGE;
3965 xp->xp_pattern = arg;
3966 }
3967 else
Bram Moolenaara660dc82011-05-25 12:51:22 +02003968 {
3969 if ( STRNCMP(arg, "messages", p - arg) == 0
3970 || STRNCMP(arg, "ctype", p - arg) == 0
3971 || STRNCMP(arg, "time", p - arg) == 0)
3972 {
3973 xp->xp_context = EXPAND_LOCALES;
3974 xp->xp_pattern = skipwhite(p);
3975 }
3976 else
3977 xp->xp_context = EXPAND_NOTHING;
3978 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003979 break;
3980#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01003981#if defined(FEAT_PROFILE)
3982 case CMD_profile:
3983 set_context_in_profile_cmd(xp, arg);
3984 break;
3985#endif
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01003986 case CMD_behave:
3987 xp->xp_context = EXPAND_BEHAVE;
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02003988 xp->xp_pattern = arg;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01003989 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003990
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02003991#if defined(FEAT_CMDHIST)
3992 case CMD_history:
3993 xp->xp_context = EXPAND_HISTORY;
3994 xp->xp_pattern = arg;
3995 break;
3996#endif
3997
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998#endif /* FEAT_CMDL_COMPL */
3999
4000 default:
4001 break;
4002 }
4003 return NULL;
4004}
4005
4006/*
4007 * skip a range specifier of the form: addr [,addr] [;addr] ..
4008 *
4009 * Backslashed delimiters after / or ? will be skipped, and commands will
4010 * not be expanded between /'s and ?'s or after "'".
4011 *
Bram Moolenaardf177f62005-02-22 08:39:57 +00004012 * Also skip white space and ":" characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013 * Returns the "cmd" pointer advanced to beyond the range.
4014 */
4015 char_u *
4016skip_range(cmd, ctx)
4017 char_u *cmd;
4018 int *ctx; /* pointer to xp_context or NULL */
4019{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004020 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004021
Bram Moolenaardf177f62005-02-22 08:39:57 +00004022 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004023 {
4024 if (*cmd == '\'')
4025 {
4026 if (*++cmd == NUL && ctx != NULL)
4027 *ctx = EXPAND_NOTHING;
4028 }
4029 else if (*cmd == '/' || *cmd == '?')
4030 {
4031 delim = *cmd++;
4032 while (*cmd != NUL && *cmd != delim)
4033 if (*cmd++ == '\\' && *cmd != NUL)
4034 ++cmd;
4035 if (*cmd == NUL && ctx != NULL)
4036 *ctx = EXPAND_NOTHING;
4037 }
4038 if (*cmd != NUL)
4039 ++cmd;
4040 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004041
4042 /* Skip ":" and white space. */
4043 while (*cmd == ':')
4044 cmd = skipwhite(cmd + 1);
4045
Bram Moolenaar071d4272004-06-13 20:20:40 +00004046 return cmd;
4047}
4048
4049/*
4050 * get a single EX address
4051 *
4052 * Set ptr to the next character after the part that was interpreted.
4053 * Set ptr to NULL when an error is encountered.
4054 *
4055 * Return MAXLNUM when no Ex address was found.
4056 */
4057 static linenr_T
4058get_address(ptr, skip, to_other_file)
4059 char_u **ptr;
4060 int skip; /* only skip the address, don't use it */
4061 int to_other_file; /* flag: may jump to other file */
4062{
4063 int c;
4064 int i;
4065 long n;
4066 char_u *cmd;
4067 pos_T pos;
4068 pos_T *fp;
4069 linenr_T lnum;
4070
4071 cmd = skipwhite(*ptr);
4072 lnum = MAXLNUM;
4073 do
4074 {
4075 switch (*cmd)
4076 {
4077 case '.': /* '.' - Cursor position */
4078 ++cmd;
4079 lnum = curwin->w_cursor.lnum;
4080 break;
4081
4082 case '$': /* '$' - last line */
4083 ++cmd;
4084 lnum = curbuf->b_ml.ml_line_count;
4085 break;
4086
4087 case '\'': /* ''' - mark */
4088 if (*++cmd == NUL)
4089 {
4090 cmd = NULL;
4091 goto error;
4092 }
4093 if (skip)
4094 ++cmd;
4095 else
4096 {
4097 /* Only accept a mark in another file when it is
4098 * used by itself: ":'M". */
4099 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
4100 ++cmd;
4101 if (fp == (pos_T *)-1)
4102 /* Jumped to another file. */
4103 lnum = curwin->w_cursor.lnum;
4104 else
4105 {
4106 if (check_mark(fp) == FAIL)
4107 {
4108 cmd = NULL;
4109 goto error;
4110 }
4111 lnum = fp->lnum;
4112 }
4113 }
4114 break;
4115
4116 case '/':
4117 case '?': /* '/' or '?' - search */
4118 c = *cmd++;
4119 if (skip) /* skip "/pat/" */
4120 {
4121 cmd = skip_regexp(cmd, c, (int)p_magic, NULL);
4122 if (*cmd == c)
4123 ++cmd;
4124 }
4125 else
4126 {
4127 pos = curwin->w_cursor; /* save curwin->w_cursor */
4128 /*
4129 * When '/' or '?' follows another address, start
4130 * from there.
4131 */
4132 if (lnum != MAXLNUM)
4133 curwin->w_cursor.lnum = lnum;
4134 /*
4135 * Start a forward search at the end of the line.
4136 * Start a backward search at the start of the line.
4137 * This makes sure we never match in the current
4138 * line, and can match anywhere in the
4139 * next/previous line.
4140 */
4141 if (c == '/')
4142 curwin->w_cursor.col = MAXCOL;
4143 else
4144 curwin->w_cursor.col = 0;
4145 searchcmdlen = 0;
4146 if (!do_search(NULL, c, cmd, 1L,
Bram Moolenaaraad86642008-03-10 20:34:59 +00004147 SEARCH_HIS | SEARCH_MSG, NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148 {
4149 curwin->w_cursor = pos;
4150 cmd = NULL;
4151 goto error;
4152 }
4153 lnum = curwin->w_cursor.lnum;
4154 curwin->w_cursor = pos;
4155 /* adjust command string pointer */
4156 cmd += searchcmdlen;
4157 }
4158 break;
4159
4160 case '\\': /* "\?", "\/" or "\&", repeat search */
4161 ++cmd;
4162 if (*cmd == '&')
4163 i = RE_SUBST;
4164 else if (*cmd == '?' || *cmd == '/')
4165 i = RE_SEARCH;
4166 else
4167 {
4168 EMSG(_(e_backslash));
4169 cmd = NULL;
4170 goto error;
4171 }
4172
4173 if (!skip)
4174 {
4175 /*
4176 * When search follows another address, start from
4177 * there.
4178 */
4179 if (lnum != MAXLNUM)
4180 pos.lnum = lnum;
4181 else
4182 pos.lnum = curwin->w_cursor.lnum;
4183
4184 /*
4185 * Start the search just like for the above
4186 * do_search().
4187 */
4188 if (*cmd != '?')
4189 pos.col = MAXCOL;
4190 else
4191 pos.col = 0;
4192 if (searchit(curwin, curbuf, &pos,
4193 *cmd == '?' ? BACKWARD : FORWARD,
Bram Moolenaaraad86642008-03-10 20:34:59 +00004194 (char_u *)"", 1L, SEARCH_MSG,
Bram Moolenaar76929292008-01-06 19:07:36 +00004195 i, (linenr_T)0, NULL) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004196 lnum = pos.lnum;
4197 else
4198 {
4199 cmd = NULL;
4200 goto error;
4201 }
4202 }
4203 ++cmd;
4204 break;
4205
4206 default:
4207 if (VIM_ISDIGIT(*cmd)) /* absolute line number */
4208 lnum = getdigits(&cmd);
4209 }
4210
4211 for (;;)
4212 {
4213 cmd = skipwhite(cmd);
4214 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4215 break;
4216
4217 if (lnum == MAXLNUM)
4218 lnum = curwin->w_cursor.lnum; /* "+1" is same as ".+1" */
4219 if (VIM_ISDIGIT(*cmd))
4220 i = '+'; /* "number" is same as "+number" */
4221 else
4222 i = *cmd++;
4223 if (!VIM_ISDIGIT(*cmd)) /* '+' is '+1', but '+0' is not '+1' */
4224 n = 1;
4225 else
4226 n = getdigits(&cmd);
4227 if (i == '-')
4228 lnum -= n;
4229 else
4230 lnum += n;
4231 }
4232 } while (*cmd == '/' || *cmd == '?');
4233
4234error:
4235 *ptr = cmd;
4236 return lnum;
4237}
4238
4239/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004240 * Get flags from an Ex command argument.
4241 */
4242 static void
4243get_flags(eap)
4244 exarg_T *eap;
4245{
4246 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4247 {
4248 if (*eap->arg == 'l')
4249 eap->flags |= EXFLAG_LIST;
4250 else if (*eap->arg == 'p')
4251 eap->flags |= EXFLAG_PRINT;
4252 else
4253 eap->flags |= EXFLAG_NR;
4254 eap->arg = skipwhite(eap->arg + 1);
4255 }
4256}
4257
4258/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259 * Function called for command which is Not Implemented. NI!
4260 */
4261 void
4262ex_ni(eap)
4263 exarg_T *eap;
4264{
4265 if (!eap->skip)
4266 eap->errmsg = (char_u *)N_("E319: Sorry, the command is not available in this version");
4267}
4268
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004269#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004270/*
4271 * Function called for script command which is Not Implemented. NI!
4272 * Skips over ":perl <<EOF" constructs.
4273 */
4274 static void
4275ex_script_ni(eap)
4276 exarg_T *eap;
4277{
4278 if (!eap->skip)
4279 ex_ni(eap);
4280 else
4281 vim_free(script_get(eap, eap->arg));
4282}
4283#endif
4284
4285/*
4286 * Check range in Ex command for validity.
4287 * Return NULL when valid, error message when invalid.
4288 */
4289 static char_u *
4290invalid_range(eap)
4291 exarg_T *eap;
4292{
4293 if ( eap->line1 < 0
4294 || eap->line2 < 0
4295 || eap->line1 > eap->line2
4296 || ((eap->argt & RANGE)
4297 && !(eap->argt & NOTADR)
4298 && eap->line2 > curbuf->b_ml.ml_line_count
4299#ifdef FEAT_DIFF
4300 + (eap->cmdidx == CMD_diffget)
4301#endif
4302 ))
4303 return (char_u *)_(e_invrange);
4304 return NULL;
4305}
4306
4307/*
4308 * Correct the range for zero line number, if required.
4309 */
4310 static void
4311correct_range(eap)
4312 exarg_T *eap;
4313{
4314 if (!(eap->argt & ZEROR)) /* zero in range not allowed */
4315 {
4316 if (eap->line1 == 0)
4317 eap->line1 = 1;
4318 if (eap->line2 == 0)
4319 eap->line2 = 1;
4320 }
4321}
4322
Bram Moolenaar748bf032005-02-02 23:04:36 +00004323#ifdef FEAT_QUICKFIX
4324static char_u *skip_grep_pat __ARGS((exarg_T *eap));
4325
4326/*
4327 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4328 * pattern. Otherwise return eap->arg.
4329 */
4330 static char_u *
4331skip_grep_pat(eap)
4332 exarg_T *eap;
4333{
4334 char_u *p = eap->arg;
4335
Bram Moolenaara37420f2006-02-04 22:37:47 +00004336 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4337 || eap->cmdidx == CMD_vimgrepadd
4338 || eap->cmdidx == CMD_lvimgrepadd
4339 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004340 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004341 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004342 if (p == NULL)
4343 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004344 }
4345 return p;
4346}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004347
4348/*
4349 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4350 * in the command line, so that things like % get expanded.
4351 */
4352 static char_u *
4353replace_makeprg(eap, p, cmdlinep)
4354 exarg_T *eap;
4355 char_u *p;
4356 char_u **cmdlinep;
4357{
4358 char_u *new_cmdline;
4359 char_u *program;
4360 char_u *pos;
4361 char_u *ptr;
4362 int len;
4363 int i;
4364
4365 /*
4366 * Don't do it when ":vimgrep" is used for ":grep".
4367 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004368 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4369 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4370 || eap->cmdidx == CMD_grepadd
4371 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004372 && !grep_internal(eap->cmdidx))
4373 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004374 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4375 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004376 {
4377 if (*curbuf->b_p_gp == NUL)
4378 program = p_gp;
4379 else
4380 program = curbuf->b_p_gp;
4381 }
4382 else
4383 {
4384 if (*curbuf->b_p_mp == NUL)
4385 program = p_mp;
4386 else
4387 program = curbuf->b_p_mp;
4388 }
4389
4390 p = skipwhite(p);
4391
4392 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4393 {
4394 /* replace $* by given arguments */
4395 i = 1;
4396 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4397 ++i;
4398 len = (int)STRLEN(p);
4399 new_cmdline = alloc((int)(STRLEN(program) + i * (len - 2) + 1));
4400 if (new_cmdline == NULL)
4401 return NULL; /* out of memory */
4402 ptr = new_cmdline;
4403 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4404 {
4405 i = (int)(pos - program);
4406 STRNCPY(ptr, program, i);
4407 STRCPY(ptr += i, p);
4408 ptr += len;
4409 program = pos + 2;
4410 }
4411 STRCPY(ptr, program);
4412 }
4413 else
4414 {
4415 new_cmdline = alloc((int)(STRLEN(program) + STRLEN(p) + 2));
4416 if (new_cmdline == NULL)
4417 return NULL; /* out of memory */
4418 STRCPY(new_cmdline, program);
4419 STRCAT(new_cmdline, " ");
4420 STRCAT(new_cmdline, p);
4421 }
4422 msg_make(p);
4423
4424 /* 'eap->cmd' is not set here, because it is not used at CMD_make */
4425 vim_free(*cmdlinep);
4426 *cmdlinep = new_cmdline;
4427 p = new_cmdline;
4428 }
4429 return p;
4430}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004431#endif
4432
Bram Moolenaar071d4272004-06-13 20:20:40 +00004433/*
4434 * Expand file name in Ex command argument.
4435 * Return FAIL for failure, OK otherwise.
4436 */
4437 int
4438expand_filename(eap, cmdlinep, errormsgp)
4439 exarg_T *eap;
4440 char_u **cmdlinep;
4441 char_u **errormsgp;
4442{
4443 int has_wildcards; /* need to expand wildcards */
4444 char_u *repl;
4445 int srclen;
4446 char_u *p;
4447 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004448 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004449
Bram Moolenaar748bf032005-02-02 23:04:36 +00004450#ifdef FEAT_QUICKFIX
4451 /* Skip a regexp pattern for ":vimgrep[add] pat file..." */
4452 p = skip_grep_pat(eap);
4453#else
4454 p = eap->arg;
4455#endif
4456
Bram Moolenaar071d4272004-06-13 20:20:40 +00004457 /*
4458 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4459 * the file name contains a wildcard it should not cause expanding.
4460 * (it will be expanded anyway if there is a wildcard before replacing).
4461 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004462 has_wildcards = mch_has_wildcard(p);
4463 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004464 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004465#ifdef FEAT_EVAL
4466 /* Skip over `=expr`, wildcards in it are not expanded. */
4467 if (p[0] == '`' && p[1] == '=')
4468 {
4469 p += 2;
4470 (void)skip_expr(&p);
4471 if (*p == '`')
4472 ++p;
4473 continue;
4474 }
4475#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476 /*
4477 * Quick check if this cannot be the start of a special string.
4478 * Also removes backslash before '%', '#' and '<'.
4479 */
4480 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4481 {
4482 ++p;
4483 continue;
4484 }
4485
4486 /*
4487 * Try to find a match at this position.
4488 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004489 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
4490 errormsgp, &escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004491 if (*errormsgp != NULL) /* error detected */
4492 return FAIL;
4493 if (repl == NULL) /* no match found */
4494 {
4495 p += srclen;
4496 continue;
4497 }
4498
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004499 /* Wildcards won't be expanded below, the replacement is taken
4500 * literally. But do expand "~/file", "~user/file" and "$HOME/file". */
4501 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4502 {
4503 char_u *l = repl;
4504
4505 repl = expand_env_save(repl);
4506 vim_free(l);
4507 }
4508
Bram Moolenaar63b92542007-03-27 14:57:09 +00004509 /* Need to escape white space et al. with a backslash.
4510 * Don't do this for:
4511 * - replacement that already has been escaped: "##"
4512 * - shell commands (may have to use quotes instead).
4513 * - non-unix systems when there is a single argument (spaces don't
4514 * separate arguments then).
4515 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004516 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00004517 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00004518 && eap->cmdidx != CMD_bang
4519 && eap->cmdidx != CMD_make
Bram Moolenaara37420f2006-02-04 22:37:47 +00004520 && eap->cmdidx != CMD_lmake
Bram Moolenaar071d4272004-06-13 20:20:40 +00004521 && eap->cmdidx != CMD_grep
Bram Moolenaara37420f2006-02-04 22:37:47 +00004522 && eap->cmdidx != CMD_lgrep
Bram Moolenaar071d4272004-06-13 20:20:40 +00004523 && eap->cmdidx != CMD_grepadd
Bram Moolenaara37420f2006-02-04 22:37:47 +00004524 && eap->cmdidx != CMD_lgrepadd
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525#ifndef UNIX
4526 && !(eap->argt & NOSPC)
4527#endif
4528 )
4529 {
4530 char_u *l;
4531#ifdef BACKSLASH_IN_FILENAME
4532 /* Don't escape a backslash here, because rem_backslash() doesn't
4533 * remove it later. */
4534 static char_u *nobslash = (char_u *)" \t\"|";
4535# define ESCAPE_CHARS nobslash
4536#else
4537# define ESCAPE_CHARS escape_chars
4538#endif
4539
4540 for (l = repl; *l; ++l)
4541 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
4542 {
4543 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
4544 if (l != NULL)
4545 {
4546 vim_free(repl);
4547 repl = l;
4548 }
4549 break;
4550 }
4551 }
4552
4553 /* For a shell command a '!' must be escaped. */
4554 if ((eap->usefilter || eap->cmdidx == CMD_bang)
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004555 && vim_strpbrk(repl, (char_u *)"!&;()<>") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004556 {
4557 char_u *l;
4558
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004559 l = vim_strsave_escaped(repl, (char_u *)"!&;()<>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004560 if (l != NULL)
4561 {
4562 vim_free(repl);
4563 repl = l;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004564 /* For a sh-like shell escape "!" another time. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004565 if (strstr((char *)p_sh, "sh") != NULL)
4566 {
4567 l = vim_strsave_escaped(repl, (char_u *)"!");
4568 if (l != NULL)
4569 {
4570 vim_free(repl);
4571 repl = l;
4572 }
4573 }
4574 }
4575 }
4576
4577 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
4578 vim_free(repl);
4579 if (p == NULL)
4580 return FAIL;
4581 }
4582
4583 /*
4584 * One file argument: Expand wildcards.
4585 * Don't do this with ":r !command" or ":w !command".
4586 */
4587 if ((eap->argt & NOSPC) && !eap->usefilter)
4588 {
4589 /*
4590 * May do this twice:
4591 * 1. Replace environment variables.
4592 * 2. Replace any other wildcards, remove backslashes.
4593 */
4594 for (n = 1; n <= 2; ++n)
4595 {
4596 if (n == 2)
4597 {
4598#ifdef UNIX
4599 /*
4600 * Only for Unix we check for more than one file name.
4601 * For other systems spaces are considered to be part
4602 * of the file name.
4603 * Only check here if there is no wildcard, otherwise
4604 * ExpandOne() will check for errors. This allows
4605 * ":e `ls ve*.c`" on Unix.
4606 */
4607 if (!has_wildcards)
4608 for (p = eap->arg; *p; ++p)
4609 {
4610 /* skip escaped characters */
4611 if (p[1] && (*p == '\\' || *p == Ctrl_V))
4612 ++p;
4613 else if (vim_iswhite(*p))
4614 {
4615 *errormsgp = (char_u *)_("E172: Only one file name allowed");
4616 return FAIL;
4617 }
4618 }
4619#endif
4620
4621 /*
4622 * Halve the number of backslashes (this is Vi compatible).
4623 * For Unix and OS/2, when wildcards are expanded, this is
4624 * done by ExpandOne() below.
4625 */
4626#if defined(UNIX) || defined(OS2)
4627 if (!has_wildcards)
4628#endif
4629 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630 }
4631
4632 if (has_wildcards)
4633 {
4634 if (n == 1)
4635 {
4636 /*
4637 * First loop: May expand environment variables. This
4638 * can be done much faster with expand_env() than with
4639 * something else (e.g., calling a shell).
4640 * After expanding environment variables, check again
4641 * if there are still wildcards present.
4642 */
4643 if (vim_strchr(eap->arg, '$') != NULL
4644 || vim_strchr(eap->arg, '~') != NULL)
4645 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00004646 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004647 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004648 has_wildcards = mch_has_wildcard(NameBuff);
4649 p = NameBuff;
4650 }
4651 else
4652 p = NULL;
4653 }
4654 else /* n == 2 */
4655 {
4656 expand_T xpc;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004657 int options = WILD_LIST_NOTFOUND|WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004658
4659 ExpandInit(&xpc);
4660 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004661 if (p_wic)
4662 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004663 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01004664 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004665 if (p == NULL)
4666 return FAIL;
4667 }
4668 if (p != NULL)
4669 {
4670 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
4671 p, cmdlinep);
4672 if (n == 2) /* p came from ExpandOne() */
4673 vim_free(p);
4674 }
4675 }
4676 }
4677 }
4678 return OK;
4679}
4680
4681/*
4682 * Replace part of the command line, keeping eap->cmd, eap->arg and
4683 * eap->nextcmd correct.
4684 * "src" points to the part that is to be replaced, of length "srclen".
4685 * "repl" is the replacement string.
4686 * Returns a pointer to the character after the replaced string.
4687 * Returns NULL for failure.
4688 */
4689 static char_u *
4690repl_cmdline(eap, src, srclen, repl, cmdlinep)
4691 exarg_T *eap;
4692 char_u *src;
4693 int srclen;
4694 char_u *repl;
4695 char_u **cmdlinep;
4696{
4697 int len;
4698 int i;
4699 char_u *new_cmdline;
4700
4701 /*
4702 * The new command line is build in new_cmdline[].
4703 * First allocate it.
4704 * Careful: a "+cmd" argument may have been NUL terminated.
4705 */
4706 len = (int)STRLEN(repl);
4707 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004708 if (eap->nextcmd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004709 i += (int)STRLEN(eap->nextcmd);/* add space for next command */
4710 if ((new_cmdline = alloc((unsigned)i)) == NULL)
4711 return NULL; /* out of memory! */
4712
4713 /*
4714 * Copy the stuff before the expanded part.
4715 * Copy the expanded stuff.
4716 * Copy what came after the expanded part.
4717 * Copy the next commands, if there are any.
4718 */
4719 i = (int)(src - *cmdlinep); /* length of part before match */
4720 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00004721
Bram Moolenaar071d4272004-06-13 20:20:40 +00004722 mch_memmove(new_cmdline + i, repl, (size_t)len);
4723 i += len; /* remember the end of the string */
4724 STRCPY(new_cmdline + i, src + srclen);
4725 src = new_cmdline + i; /* remember where to continue */
4726
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004727 if (eap->nextcmd != NULL) /* append next command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004728 {
4729 i = (int)STRLEN(new_cmdline) + 1;
4730 STRCPY(new_cmdline + i, eap->nextcmd);
4731 eap->nextcmd = new_cmdline + i;
4732 }
4733 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
4734 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
4735 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
4736 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
4737 vim_free(*cmdlinep);
4738 *cmdlinep = new_cmdline;
4739
4740 return src;
4741}
4742
4743/*
4744 * Check for '|' to separate commands and '"' to start comments.
4745 */
4746 void
4747separate_nextcmd(eap)
4748 exarg_T *eap;
4749{
4750 char_u *p;
4751
Bram Moolenaar86b68352004-12-27 21:59:20 +00004752#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004753 p = skip_grep_pat(eap);
4754#else
4755 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004756#endif
4757
4758 for ( ; *p; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004759 {
4760 if (*p == Ctrl_V)
4761 {
4762 if (eap->argt & (USECTRLV | XFILE))
4763 ++p; /* skip CTRL-V and next char */
4764 else
Bram Moolenaarb0db5692007-08-14 20:54:49 +00004765 /* remove CTRL-V and skip next char */
Bram Moolenaara7241f52008-06-24 20:39:31 +00004766 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004767 if (*p == NUL) /* stop at NUL after CTRL-V */
4768 break;
4769 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004770
4771#ifdef FEAT_EVAL
4772 /* Skip over `=expr` when wildcards are expanded. */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004773 else if (p[0] == '`' && p[1] == '=' && (eap->argt & XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004774 {
4775 p += 2;
4776 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004777 }
4778#endif
4779
Bram Moolenaar071d4272004-06-13 20:20:40 +00004780 /* Check for '"': start of comment or '|': next command */
4781 /* :@" and :*" do not start a comment!
4782 * :redir @" doesn't either. */
4783 else if ((*p == '"' && !(eap->argt & NOTRLCOM)
4784 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
4785 || p != eap->arg)
4786 && (eap->cmdidx != CMD_redir
4787 || p != eap->arg + 1 || p[-1] != '@'))
4788 || *p == '|' || *p == '\n')
4789 {
4790 /*
4791 * We remove the '\' before the '|', unless USECTRLV is used
4792 * AND 'b' is present in 'cpoptions'.
4793 */
4794 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
4795 || !(eap->argt & USECTRLV)) && *(p - 1) == '\\')
4796 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00004797 STRMOVE(p - 1, p); /* remove the '\' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004798 --p;
4799 }
4800 else
4801 {
4802 eap->nextcmd = check_nextcmd(p);
4803 *p = NUL;
4804 break;
4805 }
4806 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004807 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004808
Bram Moolenaar071d4272004-06-13 20:20:40 +00004809 if (!(eap->argt & NOTRLCOM)) /* remove trailing spaces */
4810 del_trailing_spaces(eap->arg);
4811}
4812
4813/*
4814 * get + command from ex argument
4815 */
4816 static char_u *
4817getargcmd(argp)
4818 char_u **argp;
4819{
4820 char_u *arg = *argp;
4821 char_u *command = NULL;
4822
4823 if (*arg == '+') /* +[command] */
4824 {
4825 ++arg;
4826 if (vim_isspace(*arg))
4827 command = dollar_command;
4828 else
4829 {
4830 command = arg;
4831 arg = skip_cmd_arg(command, TRUE);
4832 if (*arg != NUL)
4833 *arg++ = NUL; /* terminate command with NUL */
4834 }
4835
4836 arg = skipwhite(arg); /* skip over spaces */
4837 *argp = arg;
4838 }
4839 return command;
4840}
4841
4842/*
4843 * Find end of "+command" argument. Skip over "\ " and "\\".
4844 */
4845 static char_u *
4846skip_cmd_arg(p, rembs)
4847 char_u *p;
4848 int rembs; /* TRUE to halve the number of backslashes */
4849{
4850 while (*p && !vim_isspace(*p))
4851 {
4852 if (*p == '\\' && p[1] != NUL)
4853 {
4854 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00004855 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004856 else
4857 ++p;
4858 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004859 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004860 }
4861 return p;
4862}
4863
4864/*
4865 * Get "++opt=arg" argument.
4866 * Return FAIL or OK.
4867 */
4868 static int
4869getargopt(eap)
4870 exarg_T *eap;
4871{
4872 char_u *arg = eap->arg + 2;
4873 int *pp = NULL;
4874#ifdef FEAT_MBYTE
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004875 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004876 char_u *p;
4877#endif
4878
4879 /* ":edit ++[no]bin[ary] file" */
4880 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
4881 {
4882 if (*arg == 'n')
4883 {
4884 arg += 2;
4885 eap->force_bin = FORCE_NOBIN;
4886 }
4887 else
4888 eap->force_bin = FORCE_BIN;
4889 if (!checkforcmd(&arg, "binary", 3))
4890 return FAIL;
4891 eap->arg = skipwhite(arg);
4892 return OK;
4893 }
4894
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004895 /* ":read ++edit file" */
4896 if (STRNCMP(arg, "edit", 4) == 0)
4897 {
4898 eap->read_edit = TRUE;
4899 eap->arg = skipwhite(arg + 4);
4900 return OK;
4901 }
4902
Bram Moolenaar071d4272004-06-13 20:20:40 +00004903 if (STRNCMP(arg, "ff", 2) == 0)
4904 {
4905 arg += 2;
4906 pp = &eap->force_ff;
4907 }
4908 else if (STRNCMP(arg, "fileformat", 10) == 0)
4909 {
4910 arg += 10;
4911 pp = &eap->force_ff;
4912 }
4913#ifdef FEAT_MBYTE
4914 else if (STRNCMP(arg, "enc", 3) == 0)
4915 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01004916 if (STRNCMP(arg, "encoding", 8) == 0)
4917 arg += 8;
4918 else
4919 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004920 pp = &eap->force_enc;
4921 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004922 else if (STRNCMP(arg, "bad", 3) == 0)
4923 {
4924 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004925 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004926 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927#endif
4928
4929 if (pp == NULL || *arg != '=')
4930 return FAIL;
4931
4932 ++arg;
4933 *pp = (int)(arg - eap->cmd);
4934 arg = skip_cmd_arg(arg, FALSE);
4935 eap->arg = skipwhite(arg);
4936 *arg = NUL;
4937
4938#ifdef FEAT_MBYTE
4939 if (pp == &eap->force_ff)
4940 {
4941#endif
4942 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
4943 return FAIL;
4944#ifdef FEAT_MBYTE
4945 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004946 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004947 {
4948 /* Make 'fileencoding' lower case. */
4949 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
4950 *p = TOLOWER_ASC(*p);
4951 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004952 else
4953 {
4954 /* Check ++bad= argument. Must be a single-byte character, "keep" or
4955 * "drop". */
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004956 p = eap->cmd + bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004957 if (STRICMP(p, "keep") == 0)
4958 eap->bad_char = BAD_KEEP;
4959 else if (STRICMP(p, "drop") == 0)
4960 eap->bad_char = BAD_DROP;
4961 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
4962 eap->bad_char = *p;
4963 else
4964 return FAIL;
4965 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004966#endif
4967
4968 return OK;
4969}
4970
4971/*
4972 * ":abbreviate" and friends.
4973 */
4974 static void
4975ex_abbreviate(eap)
4976 exarg_T *eap;
4977{
4978 do_exmap(eap, TRUE); /* almost the same as mapping */
4979}
4980
4981/*
4982 * ":map" and friends.
4983 */
4984 static void
4985ex_map(eap)
4986 exarg_T *eap;
4987{
4988 /*
4989 * If we are sourcing .exrc or .vimrc in current directory we
4990 * print the mappings for security reasons.
4991 */
4992 if (secure)
4993 {
4994 secure = 2;
4995 msg_outtrans(eap->cmd);
4996 msg_putchar('\n');
4997 }
4998 do_exmap(eap, FALSE);
4999}
5000
5001/*
5002 * ":unmap" and friends.
5003 */
5004 static void
5005ex_unmap(eap)
5006 exarg_T *eap;
5007{
5008 do_exmap(eap, FALSE);
5009}
5010
5011/*
5012 * ":mapclear" and friends.
5013 */
5014 static void
5015ex_mapclear(eap)
5016 exarg_T *eap;
5017{
5018 map_clear(eap->cmd, eap->arg, eap->forceit, FALSE);
5019}
5020
5021/*
5022 * ":abclear" and friends.
5023 */
5024 static void
5025ex_abclear(eap)
5026 exarg_T *eap;
5027{
5028 map_clear(eap->cmd, eap->arg, TRUE, TRUE);
5029}
5030
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005031#if defined(FEAT_AUTOCMD) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005032 static void
5033ex_autocmd(eap)
5034 exarg_T *eap;
5035{
5036 /*
5037 * Disallow auto commands from .exrc and .vimrc in current
5038 * directory for security reasons.
5039 */
5040 if (secure)
5041 {
5042 secure = 2;
5043 eap->errmsg = e_curdir;
5044 }
5045 else if (eap->cmdidx == CMD_autocmd)
5046 do_autocmd(eap->arg, eap->forceit);
5047 else
5048 do_augroup(eap->arg, eap->forceit);
5049}
5050
5051/*
5052 * ":doautocmd": Apply the automatic commands to the current buffer.
5053 */
5054 static void
5055ex_doautocmd(eap)
5056 exarg_T *eap;
5057{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005058 char_u *arg = eap->arg;
5059 int call_do_modelines = check_nomodeline(&arg);
5060
5061 (void)do_doautocmd(arg, TRUE);
5062 if (call_do_modelines) /* Only when there is no <nomodeline>. */
5063 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005064}
5065#endif
5066
5067#ifdef FEAT_LISTCMDS
5068/*
5069 * :[N]bunload[!] [N] [bufname] unload buffer
5070 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
5071 * :[N]bwipeout[!] [N] [bufname] delete buffer really
5072 */
5073 static void
5074ex_bunload(eap)
5075 exarg_T *eap;
5076{
5077 eap->errmsg = do_bufdel(
5078 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
5079 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
5080 : DOBUF_UNLOAD, eap->arg,
5081 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
5082}
5083
5084/*
5085 * :[N]buffer [N] to buffer N
5086 * :[N]sbuffer [N] to buffer N
5087 */
5088 static void
5089ex_buffer(eap)
5090 exarg_T *eap;
5091{
5092 if (*eap->arg)
5093 eap->errmsg = e_trailing;
5094 else
5095 {
5096 if (eap->addr_count == 0) /* default is current buffer */
5097 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
5098 else
5099 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
5100 }
5101}
5102
5103/*
5104 * :[N]bmodified [N] to next mod. buffer
5105 * :[N]sbmodified [N] to next mod. buffer
5106 */
5107 static void
5108ex_bmodified(eap)
5109 exarg_T *eap;
5110{
5111 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
5112}
5113
5114/*
5115 * :[N]bnext [N] to next buffer
5116 * :[N]sbnext [N] split and to next buffer
5117 */
5118 static void
5119ex_bnext(eap)
5120 exarg_T *eap;
5121{
5122 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
5123}
5124
5125/*
5126 * :[N]bNext [N] to previous buffer
5127 * :[N]bprevious [N] to previous buffer
5128 * :[N]sbNext [N] split and to previous buffer
5129 * :[N]sbprevious [N] split and to previous buffer
5130 */
5131 static void
5132ex_bprevious(eap)
5133 exarg_T *eap;
5134{
5135 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
5136}
5137
5138/*
5139 * :brewind to first buffer
5140 * :bfirst to first buffer
5141 * :sbrewind split and to first buffer
5142 * :sbfirst split and to first buffer
5143 */
5144 static void
5145ex_brewind(eap)
5146 exarg_T *eap;
5147{
5148 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
5149}
5150
5151/*
5152 * :blast to last buffer
5153 * :sblast split and to last buffer
5154 */
5155 static void
5156ex_blast(eap)
5157 exarg_T *eap;
5158{
5159 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
5160}
5161#endif
5162
5163 int
5164ends_excmd(c)
5165 int c;
5166{
5167 return (c == NUL || c == '|' || c == '"' || c == '\n');
5168}
5169
5170#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5171 || defined(PROTO)
5172/*
5173 * Return the next command, after the first '|' or '\n'.
5174 * Return NULL if not found.
5175 */
5176 char_u *
5177find_nextcmd(p)
5178 char_u *p;
5179{
5180 while (*p != '|' && *p != '\n')
5181 {
5182 if (*p == NUL)
5183 return NULL;
5184 ++p;
5185 }
5186 return (p + 1);
5187}
5188#endif
5189
5190/*
5191 * Check if *p is a separator between Ex commands.
5192 * Return NULL if it isn't, (p + 1) if it is.
5193 */
5194 char_u *
5195check_nextcmd(p)
5196 char_u *p;
5197{
5198 p = skipwhite(p);
5199 if (*p == '|' || *p == '\n')
5200 return (p + 1);
5201 else
5202 return NULL;
5203}
5204
5205/*
5206 * - if there are more files to edit
5207 * - and this is the last window
5208 * - and forceit not used
5209 * - and not repeated twice on a row
5210 * return FAIL and give error message if 'message' TRUE
5211 * return OK otherwise
5212 */
5213 static int
5214check_more(message, forceit)
5215 int message; /* when FALSE check only, no messages */
5216 int forceit;
5217{
5218 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5219
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005220 if (!forceit && only_one_window()
5221 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005222 {
5223 if (message)
5224 {
5225#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
5226 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
5227 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005228 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005229
5230 if (n == 1)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02005231 vim_strncpy(buff,
5232 (char_u *)_("1 more file to edit. Quit anyway?"),
Bram Moolenaard9462e32011-04-11 21:35:11 +02005233 DIALOG_MSG_SIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005234 else
Bram Moolenaard9462e32011-04-11 21:35:11 +02005235 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005236 _("%d more files to edit. Quit anyway?"), n);
5237 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5238 return OK;
5239 return FAIL;
5240 }
5241#endif
5242 if (n == 1)
5243 EMSG(_("E173: 1 more file to edit"));
5244 else
5245 EMSGN(_("E173: %ld more files to edit"), n);
5246 quitmore = 2; /* next try to quit is allowed */
5247 }
5248 return FAIL;
5249 }
5250 return OK;
5251}
5252
5253#ifdef FEAT_CMDL_COMPL
5254/*
5255 * Function given to ExpandGeneric() to obtain the list of command names.
5256 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005257 char_u *
5258get_command_name(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00005259 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005260 int idx;
5261{
5262 if (idx >= (int)CMD_SIZE)
5263# ifdef FEAT_USR_CMDS
5264 return get_user_command_name(idx);
5265# else
5266 return NULL;
5267# endif
5268 return cmdnames[idx].cmd_name;
5269}
5270#endif
5271
5272#if defined(FEAT_USR_CMDS) || defined(PROTO)
5273static 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));
5274static void uc_list __ARGS((char_u *name, size_t name_len));
5275static int uc_scan_attr __ARGS((char_u *attr, size_t len, long *argt, long *def, int *flags, int *compl, char_u **compl_arg));
5276static char_u *uc_split_args __ARGS((char_u *arg, size_t *lenp));
5277static 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));
5278
5279 static int
5280uc_add_command(name, name_len, rep, argt, def, flags, compl, compl_arg, force)
5281 char_u *name;
5282 size_t name_len;
5283 char_u *rep;
5284 long argt;
5285 long def;
5286 int flags;
5287 int compl;
5288 char_u *compl_arg;
5289 int force;
5290{
5291 ucmd_T *cmd = NULL;
5292 char_u *p;
5293 int i;
5294 int cmp = 1;
5295 char_u *rep_buf = NULL;
5296 garray_T *gap;
5297
Bram Moolenaar9c102382006-05-03 21:26:49 +00005298 replace_termcodes(rep, &rep_buf, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005299 if (rep_buf == NULL)
5300 {
5301 /* Can't replace termcodes - try using the string as is */
5302 rep_buf = vim_strsave(rep);
5303
5304 /* Give up if out of memory */
5305 if (rep_buf == NULL)
5306 return FAIL;
5307 }
5308
5309 /* get address of growarray: global or in curbuf */
5310 if (flags & UC_BUFFER)
5311 {
5312 gap = &curbuf->b_ucmds;
5313 if (gap->ga_itemsize == 0)
5314 ga_init2(gap, (int)sizeof(ucmd_T), 4);
5315 }
5316 else
5317 gap = &ucmds;
5318
5319 /* Search for the command in the already defined commands. */
5320 for (i = 0; i < gap->ga_len; ++i)
5321 {
5322 size_t len;
5323
5324 cmd = USER_CMD_GA(gap, i);
5325 len = STRLEN(cmd->uc_name);
5326 cmp = STRNCMP(name, cmd->uc_name, name_len);
5327 if (cmp == 0)
5328 {
5329 if (name_len < len)
5330 cmp = -1;
5331 else if (name_len > len)
5332 cmp = 1;
5333 }
5334
5335 if (cmp == 0)
5336 {
5337 if (!force)
5338 {
5339 EMSG(_("E174: Command already exists: add ! to replace it"));
5340 goto fail;
5341 }
5342
5343 vim_free(cmd->uc_rep);
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00005344 cmd->uc_rep = NULL;
5345#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5346 vim_free(cmd->uc_compl_arg);
5347 cmd->uc_compl_arg = NULL;
5348#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005349 break;
5350 }
5351
5352 /* Stop as soon as we pass the name to add */
5353 if (cmp < 0)
5354 break;
5355 }
5356
5357 /* Extend the array unless we're replacing an existing command */
5358 if (cmp != 0)
5359 {
5360 if (ga_grow(gap, 1) != OK)
5361 goto fail;
5362 if ((p = vim_strnsave(name, (int)name_len)) == NULL)
5363 goto fail;
5364
5365 cmd = USER_CMD_GA(gap, i);
5366 mch_memmove(cmd + 1, cmd, (gap->ga_len - i) * sizeof(ucmd_T));
5367
5368 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005369
5370 cmd->uc_name = p;
5371 }
5372
5373 cmd->uc_rep = rep_buf;
5374 cmd->uc_argt = argt;
5375 cmd->uc_def = def;
5376 cmd->uc_compl = compl;
5377#ifdef FEAT_EVAL
5378 cmd->uc_scriptID = current_SID;
5379# ifdef FEAT_CMDL_COMPL
5380 cmd->uc_compl_arg = compl_arg;
5381# endif
5382#endif
5383
5384 return OK;
5385
5386fail:
5387 vim_free(rep_buf);
5388#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5389 vim_free(compl_arg);
5390#endif
5391 return FAIL;
5392}
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005393#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005394
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005395#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005396/*
5397 * List of names for completion for ":command" with the EXPAND_ flag.
5398 * Must be alphabetical for completion.
5399 */
5400static struct
5401{
5402 int expand;
5403 char *name;
5404} command_complete[] =
5405{
5406 {EXPAND_AUGROUP, "augroup"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005407 {EXPAND_BEHAVE, "behave"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005408 {EXPAND_BUFFERS, "buffer"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005409 {EXPAND_COLORS, "color"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005410 {EXPAND_COMMANDS, "command"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005411 {EXPAND_COMPILER, "compiler"},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005412#if defined(FEAT_CSCOPE)
5413 {EXPAND_CSCOPE, "cscope"},
5414#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005415#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5416 {EXPAND_USER_DEFINED, "custom"},
Bram Moolenaara466c992005-07-09 21:03:22 +00005417 {EXPAND_USER_LIST, "customlist"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005418#endif
5419 {EXPAND_DIRECTORIES, "dir"},
5420 {EXPAND_ENV_VARS, "environment"},
5421 {EXPAND_EVENTS, "event"},
5422 {EXPAND_EXPRESSION, "expression"},
5423 {EXPAND_FILES, "file"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005424 {EXPAND_FILES_IN_PATH, "file_in_path"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005425 {EXPAND_FILETYPE, "filetype"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005426 {EXPAND_FUNCTIONS, "function"},
5427 {EXPAND_HELP, "help"},
5428 {EXPAND_HIGHLIGHT, "highlight"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005429#if defined(FEAT_CMDHIST)
5430 {EXPAND_HISTORY, "history"},
5431#endif
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005432#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005433 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005434 {EXPAND_LOCALES, "locale"},
5435#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005436 {EXPAND_MAPPINGS, "mapping"},
5437 {EXPAND_MENUS, "menu"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005438 {EXPAND_OWNSYNTAX, "syntax"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005439 {EXPAND_SETTINGS, "option"},
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005440 {EXPAND_SHELLCMD, "shellcmd"},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005441#if defined(FEAT_SIGNS)
5442 {EXPAND_SIGN, "sign"},
5443#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005444 {EXPAND_TAGS, "tag"},
5445 {EXPAND_TAGS_LISTFILES, "tag_listfiles"},
Bram Moolenaar24305862012-08-15 14:05:05 +02005446 {EXPAND_USER, "user"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005447 {EXPAND_USER_VARS, "var"},
5448 {0, NULL}
5449};
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005450#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005451
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005452#if defined(FEAT_USR_CMDS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005453 static void
5454uc_list(name, name_len)
5455 char_u *name;
5456 size_t name_len;
5457{
5458 int i, j;
5459 int found = FALSE;
5460 ucmd_T *cmd;
5461 int len;
5462 long a;
5463 garray_T *gap;
5464
5465 gap = &curbuf->b_ucmds;
5466 for (;;)
5467 {
5468 for (i = 0; i < gap->ga_len; ++i)
5469 {
5470 cmd = USER_CMD_GA(gap, i);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005471 a = (long)cmd->uc_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005472
5473 /* Skip commands which don't match the requested prefix */
5474 if (STRNCMP(name, cmd->uc_name, name_len) != 0)
5475 continue;
5476
5477 /* Put out the title first time */
5478 if (!found)
5479 MSG_PUTS_TITLE(_("\n Name Args Range Complete Definition"));
5480 found = TRUE;
5481 msg_putchar('\n');
5482 if (got_int)
5483 break;
5484
5485 /* Special cases */
5486 msg_putchar(a & BANG ? '!' : ' ');
5487 msg_putchar(a & REGSTR ? '"' : ' ');
5488 msg_putchar(gap != &ucmds ? 'b' : ' ');
5489 msg_putchar(' ');
5490
5491 msg_outtrans_attr(cmd->uc_name, hl_attr(HLF_D));
5492 len = (int)STRLEN(cmd->uc_name) + 4;
5493
5494 do {
5495 msg_putchar(' ');
5496 ++len;
5497 } while (len < 16);
5498
5499 len = 0;
5500
5501 /* Arguments */
5502 switch ((int)(a & (EXTRA|NOSPC|NEEDARG)))
5503 {
5504 case 0: IObuff[len++] = '0'; break;
5505 case (EXTRA): IObuff[len++] = '*'; break;
5506 case (EXTRA|NOSPC): IObuff[len++] = '?'; break;
5507 case (EXTRA|NEEDARG): IObuff[len++] = '+'; break;
5508 case (EXTRA|NOSPC|NEEDARG): IObuff[len++] = '1'; break;
5509 }
5510
5511 do {
5512 IObuff[len++] = ' ';
5513 } while (len < 5);
5514
5515 /* Range */
5516 if (a & (RANGE|COUNT))
5517 {
5518 if (a & COUNT)
5519 {
5520 /* -count=N */
5521 sprintf((char *)IObuff + len, "%ldc", cmd->uc_def);
5522 len += (int)STRLEN(IObuff + len);
5523 }
5524 else if (a & DFLALL)
5525 IObuff[len++] = '%';
5526 else if (cmd->uc_def >= 0)
5527 {
5528 /* -range=N */
5529 sprintf((char *)IObuff + len, "%ld", cmd->uc_def);
5530 len += (int)STRLEN(IObuff + len);
5531 }
5532 else
5533 IObuff[len++] = '.';
5534 }
5535
5536 do {
5537 IObuff[len++] = ' ';
5538 } while (len < 11);
5539
5540 /* Completion */
5541 for (j = 0; command_complete[j].expand != 0; ++j)
5542 if (command_complete[j].expand == cmd->uc_compl)
5543 {
5544 STRCPY(IObuff + len, command_complete[j].name);
5545 len += (int)STRLEN(IObuff + len);
5546 break;
5547 }
5548
5549 do {
5550 IObuff[len++] = ' ';
5551 } while (len < 21);
5552
5553 IObuff[len] = '\0';
5554 msg_outtrans(IObuff);
5555
5556 msg_outtrans_special(cmd->uc_rep, FALSE);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005557#ifdef FEAT_EVAL
5558 if (p_verbose > 0)
5559 last_set_msg(cmd->uc_scriptID);
5560#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005561 out_flush();
5562 ui_breakcheck();
5563 if (got_int)
5564 break;
5565 }
5566 if (gap == &ucmds || i < gap->ga_len)
5567 break;
5568 gap = &ucmds;
5569 }
5570
5571 if (!found)
5572 MSG(_("No user-defined commands found"));
5573}
5574
5575 static char_u *
5576uc_fun_cmd()
5577{
5578 static char_u fcmd[] = {0x84, 0xaf, 0x60, 0xb9, 0xaf, 0xb5, 0x60, 0xa4,
5579 0xa5, 0xad, 0xa1, 0xae, 0xa4, 0x60, 0xa1, 0x60,
5580 0xb3, 0xa8, 0xb2, 0xb5, 0xa2, 0xa2, 0xa5, 0xb2,
5581 0xb9, 0x7f, 0};
5582 int i;
5583
5584 for (i = 0; fcmd[i]; ++i)
5585 IObuff[i] = fcmd[i] - 0x40;
5586 IObuff[i] = 0;
5587 return IObuff;
5588}
5589
5590 static int
5591uc_scan_attr(attr, len, argt, def, flags, compl, compl_arg)
5592 char_u *attr;
5593 size_t len;
5594 long *argt;
5595 long *def;
5596 int *flags;
5597 int *compl;
5598 char_u **compl_arg;
5599{
5600 char_u *p;
5601
5602 if (len == 0)
5603 {
5604 EMSG(_("E175: No attribute specified"));
5605 return FAIL;
5606 }
5607
5608 /* First, try the simple attributes (no arguments) */
5609 if (STRNICMP(attr, "bang", len) == 0)
5610 *argt |= BANG;
5611 else if (STRNICMP(attr, "buffer", len) == 0)
5612 *flags |= UC_BUFFER;
5613 else if (STRNICMP(attr, "register", len) == 0)
5614 *argt |= REGSTR;
5615 else if (STRNICMP(attr, "bar", len) == 0)
5616 *argt |= TRLBAR;
5617 else
5618 {
5619 int i;
5620 char_u *val = NULL;
5621 size_t vallen = 0;
5622 size_t attrlen = len;
5623
5624 /* Look for the attribute name - which is the part before any '=' */
5625 for (i = 0; i < (int)len; ++i)
5626 {
5627 if (attr[i] == '=')
5628 {
5629 val = &attr[i + 1];
5630 vallen = len - i - 1;
5631 attrlen = i;
5632 break;
5633 }
5634 }
5635
5636 if (STRNICMP(attr, "nargs", attrlen) == 0)
5637 {
5638 if (vallen == 1)
5639 {
5640 if (*val == '0')
5641 /* Do nothing - this is the default */;
5642 else if (*val == '1')
5643 *argt |= (EXTRA | NOSPC | NEEDARG);
5644 else if (*val == '*')
5645 *argt |= EXTRA;
5646 else if (*val == '?')
5647 *argt |= (EXTRA | NOSPC);
5648 else if (*val == '+')
5649 *argt |= (EXTRA | NEEDARG);
5650 else
5651 goto wrong_nargs;
5652 }
5653 else
5654 {
5655wrong_nargs:
5656 EMSG(_("E176: Invalid number of arguments"));
5657 return FAIL;
5658 }
5659 }
5660 else if (STRNICMP(attr, "range", attrlen) == 0)
5661 {
5662 *argt |= RANGE;
5663 if (vallen == 1 && *val == '%')
5664 *argt |= DFLALL;
5665 else if (val != NULL)
5666 {
5667 p = val;
5668 if (*def >= 0)
5669 {
5670two_count:
5671 EMSG(_("E177: Count cannot be specified twice"));
5672 return FAIL;
5673 }
5674
5675 *def = getdigits(&p);
5676 *argt |= (ZEROR | NOTADR);
5677
5678 if (p != val + vallen || vallen == 0)
5679 {
5680invalid_count:
5681 EMSG(_("E178: Invalid default value for count"));
5682 return FAIL;
5683 }
5684 }
5685 }
5686 else if (STRNICMP(attr, "count", attrlen) == 0)
5687 {
Bram Moolenaar32e7b2d2005-02-27 22:36:47 +00005688 *argt |= (COUNT | ZEROR | RANGE | NOTADR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005689
5690 if (val != NULL)
5691 {
5692 p = val;
5693 if (*def >= 0)
5694 goto two_count;
5695
5696 *def = getdigits(&p);
5697
5698 if (p != val + vallen)
5699 goto invalid_count;
5700 }
5701
5702 if (*def < 0)
5703 *def = 0;
5704 }
5705 else if (STRNICMP(attr, "complete", attrlen) == 0)
5706 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005707 if (val == NULL)
5708 {
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005709 EMSG(_("E179: argument required for -complete"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005710 return FAIL;
5711 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005712
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005713 if (parse_compl_arg(val, (int)vallen, compl, argt, compl_arg)
5714 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005715 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005716 }
5717 else
5718 {
5719 char_u ch = attr[len];
5720 attr[len] = '\0';
5721 EMSG2(_("E181: Invalid attribute: %s"), attr);
5722 attr[len] = ch;
5723 return FAIL;
5724 }
5725 }
5726
5727 return OK;
5728}
5729
Bram Moolenaara850a712009-01-28 14:42:59 +00005730/*
5731 * ":command ..."
5732 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005733 static void
5734ex_command(eap)
5735 exarg_T *eap;
5736{
5737 char_u *name;
5738 char_u *end;
5739 char_u *p;
5740 long argt = 0;
5741 long def = -1;
5742 int flags = 0;
5743 int compl = EXPAND_NOTHING;
5744 char_u *compl_arg = NULL;
5745 int has_attr = (eap->arg[0] == '-');
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01005746 int name_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005747
5748 p = eap->arg;
5749
5750 /* Check for attributes */
5751 while (*p == '-')
5752 {
5753 ++p;
5754 end = skiptowhite(p);
5755 if (uc_scan_attr(p, end - p, &argt, &def, &flags, &compl, &compl_arg)
5756 == FAIL)
5757 return;
5758 p = skipwhite(end);
5759 }
5760
5761 /* Get the name (if any) and skip to the following argument */
5762 name = p;
5763 if (ASCII_ISALPHA(*p))
5764 while (ASCII_ISALNUM(*p))
5765 ++p;
5766 if (!ends_excmd(*p) && !vim_iswhite(*p))
5767 {
5768 EMSG(_("E182: Invalid command name"));
5769 return;
5770 }
5771 end = p;
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01005772 name_len = (int)(end - name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005773
5774 /* If there is nothing after the name, and no attributes were specified,
5775 * we are listing commands
5776 */
5777 p = skipwhite(end);
5778 if (!has_attr && ends_excmd(*p))
5779 {
5780 uc_list(name, end - name);
5781 }
5782 else if (!ASCII_ISUPPER(*name))
5783 {
5784 EMSG(_("E183: User defined commands must start with an uppercase letter"));
5785 return;
5786 }
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01005787 else if ((name_len == 1 && *name == 'X')
5788 || (name_len <= 4
5789 && STRNCMP(name, "Next", name_len > 4 ? 4 : name_len) == 0))
5790 {
5791 EMSG(_("E841: Reserved name, cannot be used for user defined command"));
5792 return;
5793 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005794 else
5795 uc_add_command(name, end - name, p, argt, def, flags, compl, compl_arg,
5796 eap->forceit);
5797}
5798
5799/*
5800 * ":comclear"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005801 * Clear all user commands, global and for current buffer.
5802 */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00005803 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005804ex_comclear(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00005805 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005806{
5807 uc_clear(&ucmds);
5808 uc_clear(&curbuf->b_ucmds);
5809}
5810
5811/*
5812 * Clear all user commands for "gap".
5813 */
5814 void
5815uc_clear(gap)
5816 garray_T *gap;
5817{
5818 int i;
5819 ucmd_T *cmd;
5820
5821 for (i = 0; i < gap->ga_len; ++i)
5822 {
5823 cmd = USER_CMD_GA(gap, i);
5824 vim_free(cmd->uc_name);
5825 vim_free(cmd->uc_rep);
5826# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5827 vim_free(cmd->uc_compl_arg);
5828# endif
5829 }
5830 ga_clear(gap);
5831}
5832
5833 static void
5834ex_delcommand(eap)
5835 exarg_T *eap;
5836{
5837 int i = 0;
5838 ucmd_T *cmd = NULL;
5839 int cmp = -1;
5840 garray_T *gap;
5841
5842 gap = &curbuf->b_ucmds;
5843 for (;;)
5844 {
5845 for (i = 0; i < gap->ga_len; ++i)
5846 {
5847 cmd = USER_CMD_GA(gap, i);
5848 cmp = STRCMP(eap->arg, cmd->uc_name);
5849 if (cmp <= 0)
5850 break;
5851 }
5852 if (gap == &ucmds || cmp == 0)
5853 break;
5854 gap = &ucmds;
5855 }
5856
5857 if (cmp != 0)
5858 {
5859 EMSG2(_("E184: No such user-defined command: %s"), eap->arg);
5860 return;
5861 }
5862
5863 vim_free(cmd->uc_name);
5864 vim_free(cmd->uc_rep);
5865# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5866 vim_free(cmd->uc_compl_arg);
5867# endif
5868
5869 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005870
5871 if (i < gap->ga_len)
5872 mch_memmove(cmd, cmd + 1, (gap->ga_len - i) * sizeof(ucmd_T));
5873}
5874
Bram Moolenaar552f8a12007-03-08 17:12:08 +00005875/*
5876 * split and quote args for <f-args>
5877 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005878 static char_u *
5879uc_split_args(arg, lenp)
5880 char_u *arg;
5881 size_t *lenp;
5882{
5883 char_u *buf;
5884 char_u *p;
5885 char_u *q;
5886 int len;
5887
5888 /* Precalculate length */
5889 p = arg;
5890 len = 2; /* Initial and final quotes */
5891
5892 while (*p)
5893 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00005894 if (p[0] == '\\' && p[1] == '\\')
5895 {
5896 len += 2;
5897 p += 2;
5898 }
5899 else if (p[0] == '\\' && vim_iswhite(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005900 {
5901 len += 1;
5902 p += 2;
5903 }
5904 else if (*p == '\\' || *p == '"')
5905 {
5906 len += 2;
5907 p += 1;
5908 }
5909 else if (vim_iswhite(*p))
5910 {
5911 p = skipwhite(p);
5912 if (*p == NUL)
5913 break;
5914 len += 3; /* "," */
5915 }
5916 else
5917 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02005918#ifdef FEAT_MBYTE
5919 int charlen = (*mb_ptr2len)(p);
5920 len += charlen;
5921 p += charlen;
5922#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005923 ++len;
5924 ++p;
Bram Moolenaardfef1542012-07-10 19:25:10 +02005925#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005926 }
5927 }
5928
5929 buf = alloc(len + 1);
5930 if (buf == NULL)
5931 {
5932 *lenp = 0;
5933 return buf;
5934 }
5935
5936 p = arg;
5937 q = buf;
5938 *q++ = '"';
5939 while (*p)
5940 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00005941 if (p[0] == '\\' && p[1] == '\\')
5942 {
5943 *q++ = '\\';
5944 *q++ = '\\';
5945 p += 2;
5946 }
5947 else if (p[0] == '\\' && vim_iswhite(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005948 {
5949 *q++ = p[1];
5950 p += 2;
5951 }
5952 else if (*p == '\\' || *p == '"')
5953 {
5954 *q++ = '\\';
5955 *q++ = *p++;
5956 }
5957 else if (vim_iswhite(*p))
5958 {
5959 p = skipwhite(p);
5960 if (*p == NUL)
5961 break;
5962 *q++ = '"';
5963 *q++ = ',';
5964 *q++ = '"';
5965 }
5966 else
5967 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02005968 MB_COPY_CHAR(p, q);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005969 }
5970 }
5971 *q++ = '"';
5972 *q = 0;
5973
5974 *lenp = len;
5975 return buf;
5976}
5977
5978/*
5979 * Check for a <> code in a user command.
5980 * "code" points to the '<'. "len" the length of the <> (inclusive).
5981 * "buf" is where the result is to be added.
5982 * "split_buf" points to a buffer used for splitting, caller should free it.
5983 * "split_len" is the length of what "split_buf" contains.
5984 * Returns the length of the replacement, which has been added to "buf".
5985 * Returns -1 if there was no match, and only the "<" has been copied.
5986 */
5987 static size_t
5988uc_check_code(code, len, buf, cmd, eap, split_buf, split_len)
5989 char_u *code;
5990 size_t len;
5991 char_u *buf;
5992 ucmd_T *cmd; /* the user command we're expanding */
5993 exarg_T *eap; /* ex arguments */
5994 char_u **split_buf;
5995 size_t *split_len;
5996{
5997 size_t result = 0;
5998 char_u *p = code + 1;
5999 size_t l = len - 2;
6000 int quote = 0;
6001 enum { ct_ARGS, ct_BANG, ct_COUNT, ct_LINE1, ct_LINE2, ct_REGISTER,
6002 ct_LT, ct_NONE } type = ct_NONE;
6003
6004 if ((vim_strchr((char_u *)"qQfF", *p) != NULL) && p[1] == '-')
6005 {
6006 quote = (*p == 'q' || *p == 'Q') ? 1 : 2;
6007 p += 2;
6008 l -= 2;
6009 }
6010
Bram Moolenaar371d5402006-03-20 21:47:49 +00006011 ++l;
6012 if (l <= 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006013 type = ct_NONE;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006014 else if (STRNICMP(p, "args>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006015 type = ct_ARGS;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006016 else if (STRNICMP(p, "bang>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006017 type = ct_BANG;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006018 else if (STRNICMP(p, "count>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006019 type = ct_COUNT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006020 else if (STRNICMP(p, "line1>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006021 type = ct_LINE1;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006022 else if (STRNICMP(p, "line2>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006023 type = ct_LINE2;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006024 else if (STRNICMP(p, "lt>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006025 type = ct_LT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006026 else if (STRNICMP(p, "reg>", l) == 0 || STRNICMP(p, "register>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006027 type = ct_REGISTER;
6028
6029 switch (type)
6030 {
6031 case ct_ARGS:
6032 /* Simple case first */
6033 if (*eap->arg == NUL)
6034 {
6035 if (quote == 1)
6036 {
6037 result = 2;
6038 if (buf != NULL)
6039 STRCPY(buf, "''");
6040 }
6041 else
6042 result = 0;
6043 break;
6044 }
6045
6046 /* When specified there is a single argument don't split it.
6047 * Works for ":Cmd %" when % is "a b c". */
6048 if ((eap->argt & NOSPC) && quote == 2)
6049 quote = 1;
6050
6051 switch (quote)
6052 {
6053 case 0: /* No quoting, no splitting */
6054 result = STRLEN(eap->arg);
6055 if (buf != NULL)
6056 STRCPY(buf, eap->arg);
6057 break;
6058 case 1: /* Quote, but don't split */
6059 result = STRLEN(eap->arg) + 2;
6060 for (p = eap->arg; *p; ++p)
6061 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006062#ifdef FEAT_MBYTE
6063 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6064 /* DBCS can contain \ in a trail byte, skip the
6065 * double-byte character. */
6066 ++p;
6067 else
6068#endif
6069 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006070 ++result;
6071 }
6072
6073 if (buf != NULL)
6074 {
6075 *buf++ = '"';
6076 for (p = eap->arg; *p; ++p)
6077 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006078#ifdef FEAT_MBYTE
6079 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6080 /* DBCS can contain \ in a trail byte, copy the
6081 * double-byte character to avoid escaping. */
6082 *buf++ = *p++;
6083 else
6084#endif
6085 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006086 *buf++ = '\\';
6087 *buf++ = *p;
6088 }
6089 *buf = '"';
6090 }
6091
6092 break;
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006093 case 2: /* Quote and split (<f-args>) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006094 /* This is hard, so only do it once, and cache the result */
6095 if (*split_buf == NULL)
6096 *split_buf = uc_split_args(eap->arg, split_len);
6097
6098 result = *split_len;
6099 if (buf != NULL && result != 0)
6100 STRCPY(buf, *split_buf);
6101
6102 break;
6103 }
6104 break;
6105
6106 case ct_BANG:
6107 result = eap->forceit ? 1 : 0;
6108 if (quote)
6109 result += 2;
6110 if (buf != NULL)
6111 {
6112 if (quote)
6113 *buf++ = '"';
6114 if (eap->forceit)
6115 *buf++ = '!';
6116 if (quote)
6117 *buf = '"';
6118 }
6119 break;
6120
6121 case ct_LINE1:
6122 case ct_LINE2:
6123 case ct_COUNT:
6124 {
6125 char num_buf[20];
6126 long num = (type == ct_LINE1) ? eap->line1 :
6127 (type == ct_LINE2) ? eap->line2 :
6128 (eap->addr_count > 0) ? eap->line2 : cmd->uc_def;
6129 size_t num_len;
6130
6131 sprintf(num_buf, "%ld", num);
6132 num_len = STRLEN(num_buf);
6133 result = num_len;
6134
6135 if (quote)
6136 result += 2;
6137
6138 if (buf != NULL)
6139 {
6140 if (quote)
6141 *buf++ = '"';
6142 STRCPY(buf, num_buf);
6143 buf += num_len;
6144 if (quote)
6145 *buf = '"';
6146 }
6147
6148 break;
6149 }
6150
6151 case ct_REGISTER:
6152 result = eap->regname ? 1 : 0;
6153 if (quote)
6154 result += 2;
6155 if (buf != NULL)
6156 {
6157 if (quote)
6158 *buf++ = '\'';
6159 if (eap->regname)
6160 *buf++ = eap->regname;
6161 if (quote)
6162 *buf = '\'';
6163 }
6164 break;
6165
6166 case ct_LT:
6167 result = 1;
6168 if (buf != NULL)
6169 *buf = '<';
6170 break;
6171
6172 default:
6173 /* Not recognized: just copy the '<' and return -1. */
6174 result = (size_t)-1;
6175 if (buf != NULL)
6176 *buf = '<';
6177 break;
6178 }
6179
6180 return result;
6181}
6182
6183 static void
6184do_ucmd(eap)
6185 exarg_T *eap;
6186{
6187 char_u *buf;
6188 char_u *p;
6189 char_u *q;
6190
6191 char_u *start;
Bram Moolenaar25648a52009-02-21 19:37:46 +00006192 char_u *end = NULL;
Bram Moolenaara850a712009-01-28 14:42:59 +00006193 char_u *ksp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006194 size_t len, totlen;
6195
6196 size_t split_len = 0;
6197 char_u *split_buf = NULL;
6198 ucmd_T *cmd;
6199#ifdef FEAT_EVAL
6200 scid_T save_current_SID = current_SID;
6201#endif
6202
6203 if (eap->cmdidx == CMD_USER)
6204 cmd = USER_CMD(eap->useridx);
6205 else
6206 cmd = USER_CMD_GA(&curbuf->b_ucmds, eap->useridx);
6207
6208 /*
6209 * Replace <> in the command by the arguments.
Bram Moolenaara850a712009-01-28 14:42:59 +00006210 * First round: "buf" is NULL, compute length, allocate "buf".
6211 * Second round: copy result into "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006212 */
6213 buf = NULL;
6214 for (;;)
6215 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006216 p = cmd->uc_rep; /* source */
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00006217 q = buf; /* destination */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006218 totlen = 0;
Bram Moolenaara850a712009-01-28 14:42:59 +00006219
6220 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006221 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006222 start = vim_strchr(p, '<');
6223 if (start != NULL)
6224 end = vim_strchr(start + 1, '>');
6225 if (buf != NULL)
6226 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006227 for (ksp = p; *ksp != NUL && *ksp != K_SPECIAL; ++ksp)
6228 ;
6229 if (*ksp == K_SPECIAL
6230 && (start == NULL || ksp < start || end == NULL)
Bram Moolenaara850a712009-01-28 14:42:59 +00006231 && ((ksp[1] == KS_SPECIAL && ksp[2] == KE_FILLER)
6232# ifdef FEAT_GUI
6233 || (ksp[1] == KS_EXTRA && ksp[2] == (int)KE_CSI)
6234# endif
6235 ))
6236 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006237 /* K_SPECIAL has been put in the buffer as K_SPECIAL
Bram Moolenaara850a712009-01-28 14:42:59 +00006238 * KS_SPECIAL KE_FILLER, like for mappings, but
6239 * do_cmdline() doesn't handle that, so convert it back.
6240 * Also change K_SPECIAL KS_EXTRA KE_CSI into CSI. */
6241 len = ksp - p;
6242 if (len > 0)
6243 {
6244 mch_memmove(q, p, len);
6245 q += len;
6246 }
6247 *q++ = ksp[1] == KS_SPECIAL ? K_SPECIAL : CSI;
6248 p = ksp + 3;
6249 continue;
6250 }
6251 }
6252
6253 /* break if there no <item> is found */
6254 if (start == NULL || end == NULL)
6255 break;
6256
Bram Moolenaar071d4272004-06-13 20:20:40 +00006257 /* Include the '>' */
6258 ++end;
6259
6260 /* Take everything up to the '<' */
6261 len = start - p;
6262 if (buf == NULL)
6263 totlen += len;
6264 else
6265 {
6266 mch_memmove(q, p, len);
6267 q += len;
6268 }
6269
6270 len = uc_check_code(start, end - start, q, cmd, eap,
6271 &split_buf, &split_len);
6272 if (len == (size_t)-1)
6273 {
6274 /* no match, continue after '<' */
6275 p = start + 1;
6276 len = 1;
6277 }
6278 else
6279 p = end;
6280 if (buf == NULL)
6281 totlen += len;
6282 else
6283 q += len;
6284 }
6285 if (buf != NULL) /* second time here, finished */
6286 {
6287 STRCPY(q, p);
6288 break;
6289 }
6290
6291 totlen += STRLEN(p); /* Add on the trailing characters */
6292 buf = alloc((unsigned)(totlen + 1));
6293 if (buf == NULL)
6294 {
6295 vim_free(split_buf);
6296 return;
6297 }
6298 }
6299
6300#ifdef FEAT_EVAL
6301 current_SID = cmd->uc_scriptID;
6302#endif
6303 (void)do_cmdline(buf, eap->getline, eap->cookie,
6304 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
6305#ifdef FEAT_EVAL
6306 current_SID = save_current_SID;
6307#endif
6308 vim_free(buf);
6309 vim_free(split_buf);
6310}
6311
6312# if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6313 static char_u *
6314get_user_command_name(idx)
6315 int idx;
6316{
6317 return get_user_commands(NULL, idx - (int)CMD_SIZE);
6318}
6319
6320/*
6321 * Function given to ExpandGeneric() to obtain the list of user command names.
6322 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006323 char_u *
6324get_user_commands(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006325 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006326 int idx;
6327{
6328 if (idx < curbuf->b_ucmds.ga_len)
6329 return USER_CMD_GA(&curbuf->b_ucmds, idx)->uc_name;
6330 idx -= curbuf->b_ucmds.ga_len;
6331 if (idx < ucmds.ga_len)
6332 return USER_CMD(idx)->uc_name;
6333 return NULL;
6334}
6335
6336/*
6337 * Function given to ExpandGeneric() to obtain the list of user command
6338 * attributes.
6339 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006340 char_u *
6341get_user_cmd_flags(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006342 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006343 int idx;
6344{
6345 static char *user_cmd_flags[] =
6346 {"bang", "bar", "buffer", "complete", "count",
6347 "nargs", "range", "register"};
6348
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00006349 if (idx >= (int)(sizeof(user_cmd_flags) / sizeof(user_cmd_flags[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006350 return NULL;
6351 return (char_u *)user_cmd_flags[idx];
6352}
6353
6354/*
6355 * Function given to ExpandGeneric() to obtain the list of values for -nargs.
6356 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006357 char_u *
6358get_user_cmd_nargs(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006359 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006360 int idx;
6361{
6362 static char *user_cmd_nargs[] = {"0", "1", "*", "?", "+"};
6363
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00006364 if (idx >= (int)(sizeof(user_cmd_nargs) / sizeof(user_cmd_nargs[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006365 return NULL;
6366 return (char_u *)user_cmd_nargs[idx];
6367}
6368
6369/*
6370 * Function given to ExpandGeneric() to obtain the list of values for -complete.
6371 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006372 char_u *
6373get_user_cmd_complete(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006374 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006375 int idx;
6376{
6377 return (char_u *)command_complete[idx].name;
6378}
6379# endif /* FEAT_CMDL_COMPL */
6380
6381#endif /* FEAT_USR_CMDS */
6382
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006383#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
6384/*
6385 * Parse a completion argument "value[vallen]".
6386 * The detected completion goes in "*complp", argument type in "*argt".
6387 * When there is an argument, for function and user defined completion, it's
6388 * copied to allocated memory and stored in "*compl_arg".
6389 * Returns FAIL if something is wrong.
6390 */
6391 int
6392parse_compl_arg(value, vallen, complp, argt, compl_arg)
6393 char_u *value;
6394 int vallen;
6395 int *complp;
6396 long *argt;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006397 char_u **compl_arg UNUSED;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006398{
6399 char_u *arg = NULL;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006400# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006401 size_t arglen = 0;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006402# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006403 int i;
6404 int valend = vallen;
6405
6406 /* Look for any argument part - which is the part after any ',' */
6407 for (i = 0; i < vallen; ++i)
6408 {
6409 if (value[i] == ',')
6410 {
6411 arg = &value[i + 1];
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006412# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006413 arglen = vallen - i - 1;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006414# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006415 valend = i;
6416 break;
6417 }
6418 }
6419
6420 for (i = 0; command_complete[i].expand != 0; ++i)
6421 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006422 if ((int)STRLEN(command_complete[i].name) == valend
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006423 && STRNCMP(value, command_complete[i].name, valend) == 0)
6424 {
6425 *complp = command_complete[i].expand;
6426 if (command_complete[i].expand == EXPAND_BUFFERS)
6427 *argt |= BUFNAME;
6428 else if (command_complete[i].expand == EXPAND_DIRECTORIES
6429 || command_complete[i].expand == EXPAND_FILES)
6430 *argt |= XFILE;
6431 break;
6432 }
6433 }
6434
6435 if (command_complete[i].expand == 0)
6436 {
6437 EMSG2(_("E180: Invalid complete value: %s"), value);
6438 return FAIL;
6439 }
6440
6441# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6442 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST
6443 && arg != NULL)
6444# else
6445 if (arg != NULL)
6446# endif
6447 {
6448 EMSG(_("E468: Completion argument only allowed for custom completion"));
6449 return FAIL;
6450 }
6451
6452# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6453 if ((*complp == EXPAND_USER_DEFINED || *complp == EXPAND_USER_LIST)
6454 && arg == NULL)
6455 {
6456 EMSG(_("E467: Custom completion requires a function argument"));
6457 return FAIL;
6458 }
6459
6460 if (arg != NULL)
6461 *compl_arg = vim_strnsave(arg, (int)arglen);
6462# endif
6463 return OK;
6464}
6465#endif
6466
Bram Moolenaar071d4272004-06-13 20:20:40 +00006467 static void
6468ex_colorscheme(eap)
6469 exarg_T *eap;
6470{
Bram Moolenaare6850792010-05-14 15:28:44 +02006471 if (*eap->arg == NUL)
6472 {
6473#ifdef FEAT_EVAL
6474 char_u *expr = vim_strsave((char_u *)"g:colors_name");
6475 char_u *p = NULL;
6476
6477 if (expr != NULL)
6478 {
6479 ++emsg_off;
6480 p = eval_to_string(expr, NULL, FALSE);
6481 --emsg_off;
6482 vim_free(expr);
6483 }
6484 if (p != NULL)
6485 {
6486 MSG(p);
6487 vim_free(p);
6488 }
6489 else
6490 MSG("default");
6491#else
6492 MSG(_("unknown"));
6493#endif
6494 }
6495 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarbe1e9e92012-09-18 16:47:07 +02006496 EMSG2(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006497}
6498
6499 static void
6500ex_highlight(eap)
6501 exarg_T *eap;
6502{
6503 if (*eap->arg == NUL && eap->cmd[2] == '!')
6504 MSG(_("Greetings, Vim user!"));
6505 do_highlight(eap->arg, eap->forceit, FALSE);
6506}
6507
6508
6509/*
6510 * Call this function if we thought we were going to exit, but we won't
6511 * (because of an error). May need to restore the terminal mode.
6512 */
6513 void
6514not_exiting()
6515{
6516 exiting = FALSE;
6517 settmode(TMODE_RAW);
6518}
6519
6520/*
6521 * ":quit": quit current window, quit Vim if closed the last window.
6522 */
6523 static void
6524ex_quit(eap)
6525 exarg_T *eap;
6526{
6527#ifdef FEAT_CMDWIN
6528 if (cmdwin_type != 0)
6529 {
6530 cmdwin_result = Ctrl_C;
6531 return;
6532 }
6533#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006534 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006535 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006536 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006537 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006538 return;
6539 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006540#ifdef FEAT_AUTOCMD
Bram Moolenaar3b53dfb2012-06-06 18:03:07 +02006541 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01006542 /* Refuse to quit when locked or when the buffer in the last window is
Bram Moolenaar362ce482012-06-06 19:02:45 +02006543 * being closed (can only happen in autocommands). */
6544 if (curbuf_locked() || (curbuf->b_nwindows == 1 && curbuf->b_closing))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006545 return;
6546#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006547
6548#ifdef FEAT_NETBEANS_INTG
6549 netbeansForcedQuit = eap->forceit;
6550#endif
6551
6552 /*
6553 * If there are more files or windows we won't exit.
6554 */
6555 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6556 exiting = TRUE;
6557 if ((!P_HID(curbuf)
6558 && check_changed(curbuf, p_awa, FALSE, eap->forceit, FALSE))
6559 || check_more(TRUE, eap->forceit) == FAIL
6560 || (only_one_window() && check_changed_any(eap->forceit)))
6561 {
6562 not_exiting();
6563 }
6564 else
6565 {
6566#ifdef FEAT_WINDOWS
6567 if (only_one_window()) /* quit last window */
6568#endif
6569 getout(0);
6570#ifdef FEAT_WINDOWS
6571# ifdef FEAT_GUI
6572 need_mouse_correct = TRUE;
6573# endif
6574 /* close window; may free buffer */
6575 win_close(curwin, !P_HID(curwin->w_buffer) || eap->forceit);
6576#endif
6577 }
6578}
6579
6580/*
6581 * ":cquit".
6582 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006583 static void
6584ex_cquit(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006585 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006586{
6587 getout(1); /* this does not always pass on the exit code to the Manx
6588 compiler. why? */
6589}
6590
6591/*
6592 * ":qall": try to quit all windows
6593 */
6594 static void
6595ex_quit_all(eap)
6596 exarg_T *eap;
6597{
6598# ifdef FEAT_CMDWIN
6599 if (cmdwin_type != 0)
6600 {
6601 if (eap->forceit)
6602 cmdwin_result = K_XF1; /* ex_window() takes care of this */
6603 else
6604 cmdwin_result = K_XF2;
6605 return;
6606 }
6607# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006608
6609 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006610 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006611 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006612 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006613 return;
6614 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006615#ifdef FEAT_AUTOCMD
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01006616 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
6617 /* Refuse to quit when locked or when the buffer in the last window is
6618 * being closed (can only happen in autocommands). */
6619 if (curbuf_locked() || (curbuf->b_nwindows == 1 && curbuf->b_closing))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006620 return;
6621#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006622
Bram Moolenaar071d4272004-06-13 20:20:40 +00006623 exiting = TRUE;
6624 if (eap->forceit || !check_changed_any(FALSE))
6625 getout(0);
6626 not_exiting();
6627}
6628
Bram Moolenaard9967712006-03-11 21:18:15 +00006629#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006630/*
6631 * ":close": close current window, unless it is the last one
6632 */
6633 static void
6634ex_close(eap)
6635 exarg_T *eap;
6636{
6637# ifdef FEAT_CMDWIN
6638 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02006639 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006640 else
6641# endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006642 if (!text_locked()
6643#ifdef FEAT_AUTOCMD
6644 && !curbuf_locked()
6645#endif
6646 )
Bram Moolenaarf740b292006-02-16 22:11:02 +00006647 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006648}
6649
Bram Moolenaard9967712006-03-11 21:18:15 +00006650# ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006651/*
6652 * ":pclose": Close any preview window.
6653 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006654 static void
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006655ex_pclose(eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006656 exarg_T *eap;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006657{
6658 win_T *win;
6659
6660 for (win = firstwin; win != NULL; win = win->w_next)
6661 if (win->w_p_pvw)
6662 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006663 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006664 break;
6665 }
6666}
Bram Moolenaard9967712006-03-11 21:18:15 +00006667# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006668
Bram Moolenaarf740b292006-02-16 22:11:02 +00006669/*
6670 * Close window "win" and take care of handling closing the last window for a
6671 * modified buffer.
6672 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006673 static void
Bram Moolenaarf740b292006-02-16 22:11:02 +00006674ex_win_close(forceit, win, tp)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006675 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006676 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006677 tabpage_T *tp; /* NULL or the tab page "win" is in */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006678{
6679 int need_hide;
6680 buf_T *buf = win->w_buffer;
6681
6682 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006683 if (need_hide && !P_HID(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006684 {
Bram Moolenaard9967712006-03-11 21:18:15 +00006685# if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006686 if ((p_confirm || cmdmod.confirm) && p_write)
6687 {
6688 dialog_changed(buf, FALSE);
6689 if (buf_valid(buf) && bufIsChanged(buf))
6690 return;
6691 need_hide = FALSE;
6692 }
6693 else
Bram Moolenaard9967712006-03-11 21:18:15 +00006694# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006695 {
6696 EMSG(_(e_nowrtmsg));
6697 return;
6698 }
6699 }
6700
Bram Moolenaard9967712006-03-11 21:18:15 +00006701# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006702 need_mouse_correct = TRUE;
Bram Moolenaard9967712006-03-11 21:18:15 +00006703# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006704
Bram Moolenaar071d4272004-06-13 20:20:40 +00006705 /* free buffer when not hiding it or when it's a scratch buffer */
Bram Moolenaarf740b292006-02-16 22:11:02 +00006706 if (tp == NULL)
6707 win_close(win, !need_hide && !P_HID(buf));
6708 else
6709 win_close_othertab(win, !need_hide && !P_HID(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006710}
6711
Bram Moolenaar071d4272004-06-13 20:20:40 +00006712/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006713 * ":tabclose": close current tab page, unless it is the last one.
6714 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006715 */
6716 static void
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006717ex_tabclose(eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006718 exarg_T *eap;
6719{
Bram Moolenaarf740b292006-02-16 22:11:02 +00006720 tabpage_T *tp;
6721
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006722# ifdef FEAT_CMDWIN
6723 if (cmdwin_type != 0)
6724 cmdwin_result = K_IGNORE;
6725 else
6726# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006727 if (first_tabpage->tp_next == NULL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00006728 EMSG(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00006729 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006730 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006731 if (eap->addr_count > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006732 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006733 tp = find_tabpage((int)eap->line2);
6734 if (tp == NULL)
6735 {
6736 beep_flush();
6737 return;
6738 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00006739 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006740 {
6741 tabpage_close_other(tp, eap->forceit);
6742 return;
6743 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006744 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006745 if (!text_locked()
6746#ifdef FEAT_AUTOCMD
6747 && !curbuf_locked()
6748#endif
6749 )
Bram Moolenaarf740b292006-02-16 22:11:02 +00006750 tabpage_close(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006751 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006752}
6753
6754/*
6755 * ":tabonly": close all tab pages except the current one
6756 */
6757 static void
6758ex_tabonly(eap)
6759 exarg_T *eap;
6760{
6761 tabpage_T *tp;
6762 int done;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006763
6764# ifdef FEAT_CMDWIN
6765 if (cmdwin_type != 0)
6766 cmdwin_result = K_IGNORE;
6767 else
6768# endif
6769 if (first_tabpage->tp_next == NULL)
6770 MSG(_("Already only one tab page"));
6771 else
6772 {
6773 /* Repeat this up to a 1000 times, because autocommands may mess
6774 * up the lists. */
6775 for (done = 0; done < 1000; ++done)
6776 {
6777 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6778 if (tp->tp_topframe != topframe)
6779 {
6780 tabpage_close_other(tp, eap->forceit);
6781 /* if we failed to close it quit */
6782 if (valid_tabpage(tp))
6783 done = 1000;
6784 /* start over, "tp" is now invalid */
6785 break;
6786 }
6787 if (first_tabpage->tp_next == NULL)
6788 break;
6789 }
6790 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006791}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006792
6793/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006794 * Close the current tab page.
6795 */
6796 void
6797tabpage_close(forceit)
6798 int forceit;
6799{
6800 /* First close all the windows but the current one. If that worked then
6801 * close the last window in this tab, that will close it. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006802 if (lastwin != firstwin)
6803 close_others(TRUE, forceit);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006804 if (lastwin == firstwin)
6805 ex_win_close(forceit, curwin, NULL);
6806# ifdef FEAT_GUI
6807 need_mouse_correct = TRUE;
6808# endif
6809}
6810
6811/*
6812 * Close tab page "tp", which is not the current tab page.
6813 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006814 * Also takes care of the tab pages line disappearing when closing the
6815 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00006816 */
6817 void
6818tabpage_close_other(tp, forceit)
6819 tabpage_T *tp;
6820 int forceit;
6821{
6822 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006823 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006824 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006825
6826 /* Limit to 1000 windows, autocommands may add a window while we close
6827 * one. OK, so I'm paranoid... */
6828 while (++done < 1000)
6829 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006830 wp = tp->tp_firstwin;
6831 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006832
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006833 /* Autocommands may delete the tab page under our fingers and we may
6834 * fail to close a window with a modified buffer. */
6835 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006836 break;
6837 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006838
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006839 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006840 if (h != tabline_height())
6841 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006842}
6843
6844/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006845 * ":only".
6846 */
6847 static void
6848ex_only(eap)
6849 exarg_T *eap;
6850{
6851# ifdef FEAT_GUI
6852 need_mouse_correct = TRUE;
6853# endif
6854 close_others(TRUE, eap->forceit);
6855}
6856
6857/*
6858 * ":all" and ":sall".
Bram Moolenaard9967712006-03-11 21:18:15 +00006859 * Also used for ":tab drop file ..." after setting the argument list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006860 */
Bram Moolenaard9967712006-03-11 21:18:15 +00006861 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00006862ex_all(eap)
6863 exarg_T *eap;
6864{
6865 if (eap->addr_count == 0)
6866 eap->line2 = 9999;
Bram Moolenaard9967712006-03-11 21:18:15 +00006867 do_arg_all((int)eap->line2, eap->forceit, eap->cmdidx == CMD_drop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006868}
6869#endif /* FEAT_WINDOWS */
6870
6871 static void
6872ex_hide(eap)
6873 exarg_T *eap;
6874{
6875 if (*eap->arg != NUL && check_nextcmd(eap->arg) == NULL)
6876 eap->errmsg = e_invarg;
6877 else
6878 {
6879 /* ":hide" or ":hide | cmd": hide current window */
6880 eap->nextcmd = check_nextcmd(eap->arg);
6881#ifdef FEAT_WINDOWS
6882 if (!eap->skip)
6883 {
6884# ifdef FEAT_GUI
6885 need_mouse_correct = TRUE;
6886# endif
6887 win_close(curwin, FALSE); /* don't free buffer */
6888 }
6889#endif
6890 }
6891}
6892
6893/*
6894 * ":stop" and ":suspend": Suspend Vim.
6895 */
6896 static void
6897ex_stop(eap)
6898 exarg_T *eap;
6899{
6900 /*
6901 * Disallow suspending for "rvim".
6902 */
6903 if (!check_restricted()
6904#ifdef WIN3264
6905 /*
6906 * Check if external commands are allowed now.
6907 */
6908 && can_end_termcap_mode(TRUE)
6909#endif
6910 )
6911 {
6912 if (!eap->forceit)
6913 autowrite_all();
6914 windgoto((int)Rows - 1, 0);
6915 out_char('\n');
6916 out_flush();
6917 stoptermcap();
6918 out_flush(); /* needed for SUN to restore xterm buffer */
6919#ifdef FEAT_TITLE
6920 mch_restore_title(3); /* restore window titles */
6921#endif
6922 ui_suspend(); /* call machine specific function */
6923#ifdef FEAT_TITLE
6924 maketitle();
6925 resettitle(); /* force updating the title */
6926#endif
6927 starttermcap();
6928 scroll_start(); /* scroll screen before redrawing */
6929 redraw_later_clear();
6930 shell_resized(); /* may have resized window */
6931 }
6932}
6933
6934/*
6935 * ":exit", ":xit" and ":wq": Write file and exit Vim.
6936 */
6937 static void
6938ex_exit(eap)
6939 exarg_T *eap;
6940{
6941#ifdef FEAT_CMDWIN
6942 if (cmdwin_type != 0)
6943 {
6944 cmdwin_result = Ctrl_C;
6945 return;
6946 }
6947#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006948 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006949 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006950 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006951 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006952 return;
6953 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006954#ifdef FEAT_AUTOCMD
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01006955 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
6956 /* Refuse to quit when locked or when the buffer in the last window is
6957 * being closed (can only happen in autocommands). */
6958 if (curbuf_locked() || (curbuf->b_nwindows == 1 && curbuf->b_closing))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006959 return;
6960#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006961
6962 /*
6963 * if more files or windows we won't exit
6964 */
6965 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6966 exiting = TRUE;
6967 if ( ((eap->cmdidx == CMD_wq
6968 || curbufIsChanged())
6969 && do_write(eap) == FAIL)
6970 || check_more(TRUE, eap->forceit) == FAIL
6971 || (only_one_window() && check_changed_any(eap->forceit)))
6972 {
6973 not_exiting();
6974 }
6975 else
6976 {
6977#ifdef FEAT_WINDOWS
6978 if (only_one_window()) /* quit last window, exit Vim */
6979#endif
6980 getout(0);
6981#ifdef FEAT_WINDOWS
6982# ifdef FEAT_GUI
6983 need_mouse_correct = TRUE;
6984# endif
6985 /* quit current window, may free buffer */
6986 win_close(curwin, !P_HID(curwin->w_buffer));
6987#endif
6988 }
6989}
6990
6991/*
6992 * ":print", ":list", ":number".
6993 */
6994 static void
6995ex_print(eap)
6996 exarg_T *eap;
6997{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006998 if (curbuf->b_ml.ml_flags & ML_EMPTY)
6999 EMSG(_(e_emptybuf));
7000 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007001 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007002 for ( ;!got_int; ui_breakcheck())
7003 {
7004 print_line(eap->line1,
7005 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
7006 || (eap->flags & EXFLAG_NR)),
7007 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
7008 if (++eap->line1 > eap->line2)
7009 break;
7010 out_flush(); /* show one line at a time */
7011 }
7012 setpcmark();
7013 /* put cursor at last line */
7014 curwin->w_cursor.lnum = eap->line2;
7015 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007016 }
7017
Bram Moolenaar071d4272004-06-13 20:20:40 +00007018 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007019}
7020
7021#ifdef FEAT_BYTEOFF
7022 static void
7023ex_goto(eap)
7024 exarg_T *eap;
7025{
7026 goto_byte(eap->line2);
7027}
7028#endif
7029
7030/*
7031 * ":shell".
7032 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007033 static void
7034ex_shell(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007035 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007036{
7037 do_shell(NULL, 0);
7038}
7039
7040#if (defined(FEAT_WINDOWS) && defined(HAVE_DROP_FILE)) \
7041 || (defined(FEAT_GUI_GTK) && defined(FEAT_DND)) \
Bram Moolenaar371baa92005-12-29 22:43:53 +00007042 || defined(FEAT_GUI_MSWIN) \
7043 || defined(FEAT_GUI_MAC) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00007044 || defined(PROTO)
7045
7046/*
7047 * Handle a file drop. The code is here because a drop is *nearly* like an
7048 * :args command, but not quite (we have a list of exact filenames, so we
7049 * don't want to (a) parse a command line, or (b) expand wildcards. So the
7050 * code is very similar to :args and hence needs access to a lot of the static
7051 * functions in this file.
7052 *
7053 * The list should be allocated using alloc(), as should each item in the
7054 * list. This function takes over responsibility for freeing the list.
7055 *
Bram Moolenaar81870892007-11-11 18:17:28 +00007056 * XXX The list is made into the argument list. This is freed using
Bram Moolenaar071d4272004-06-13 20:20:40 +00007057 * FreeWild(), which does a series of vim_free() calls, unless the two defines
7058 * __EMX__ and __ALWAYS_HAS_TRAILING_NUL_POINTER are set. In this case, a
7059 * routine _fnexplodefree() is used. This may cause problems, but as the drop
7060 * file functionality is (currently) not in EMX this is not presently a
7061 * problem.
7062 */
7063 void
7064handle_drop(filec, filev, split)
7065 int filec; /* the number of files dropped */
7066 char_u **filev; /* the list of files dropped */
7067 int split; /* force splitting the window */
7068{
7069 exarg_T ea;
7070 int save_msg_scroll = msg_scroll;
7071
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007072 /* Postpone this while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007073 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007074 return;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007075#ifdef FEAT_AUTOCMD
7076 if (curbuf_locked())
7077 return;
7078#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00007079 /* When the screen is being updated we should not change buffers and
7080 * windows structures, it may cause freed memory to be used. */
7081 if (updating_screen)
7082 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007083
7084 /* Check whether the current buffer is changed. If so, we will need
7085 * to split the current window or data could be lost.
7086 * We don't need to check if the 'hidden' option is set, as in this
7087 * case the buffer won't be lost.
7088 */
7089 if (!P_HID(curbuf) && !split)
7090 {
7091 ++emsg_off;
7092 split = check_changed(curbuf, TRUE, FALSE, FALSE, FALSE);
7093 --emsg_off;
7094 }
7095 if (split)
7096 {
7097# ifdef FEAT_WINDOWS
7098 if (win_split(0, 0) == FAIL)
7099 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007100 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007101
7102 /* When splitting the window, create a new alist. Otherwise the
7103 * existing one is overwritten. */
7104 alist_unlink(curwin->w_alist);
7105 alist_new();
7106# else
7107 return; /* can't split, always fail */
7108# endif
7109 }
7110
7111 /*
7112 * Set up the new argument list.
7113 */
Bram Moolenaar86b68352004-12-27 21:59:20 +00007114 alist_set(ALIST(curwin), filec, filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007115
7116 /*
7117 * Move to the first file.
7118 */
7119 /* Fake up a minimal "next" command for do_argfile() */
7120 vim_memset(&ea, 0, sizeof(ea));
7121 ea.cmd = (char_u *)"next";
7122 do_argfile(&ea, 0);
7123
7124 /* do_ecmd() may set need_start_insertmode, but since we never left Insert
7125 * mode that is not needed here. */
7126 need_start_insertmode = FALSE;
7127
7128 /* Restore msg_scroll, otherwise a following command may cause scrolling
7129 * unexpectedly. The screen will be redrawn by the caller, thus
7130 * msg_scroll being set by displaying a message is irrelevant. */
7131 msg_scroll = save_msg_scroll;
7132}
7133#endif
7134
Bram Moolenaar071d4272004-06-13 20:20:40 +00007135/*
7136 * Clear an argument list: free all file names and reset it to zero entries.
7137 */
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007138 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00007139alist_clear(al)
7140 alist_T *al;
7141{
7142 while (--al->al_ga.ga_len >= 0)
7143 vim_free(AARGLIST(al)[al->al_ga.ga_len].ae_fname);
7144 ga_clear(&al->al_ga);
7145}
7146
7147/*
7148 * Init an argument list.
7149 */
7150 void
7151alist_init(al)
7152 alist_T *al;
7153{
7154 ga_init2(&al->al_ga, (int)sizeof(aentry_T), 5);
7155}
7156
7157#if defined(FEAT_WINDOWS) || defined(PROTO)
7158
7159/*
7160 * Remove a reference from an argument list.
7161 * Ignored when the argument list is the global one.
7162 * If the argument list is no longer used by any window, free it.
7163 */
7164 void
7165alist_unlink(al)
7166 alist_T *al;
7167{
7168 if (al != &global_alist && --al->al_refcount <= 0)
7169 {
7170 alist_clear(al);
7171 vim_free(al);
7172 }
7173}
7174
7175# if defined(FEAT_LISTCMDS) || defined(HAVE_DROP_FILE) || defined(PROTO)
7176/*
7177 * Create a new argument list and use it for the current window.
7178 */
7179 void
7180alist_new()
7181{
7182 curwin->w_alist = (alist_T *)alloc((unsigned)sizeof(alist_T));
7183 if (curwin->w_alist == NULL)
7184 {
7185 curwin->w_alist = &global_alist;
7186 ++global_alist.al_refcount;
7187 }
7188 else
7189 {
7190 curwin->w_alist->al_refcount = 1;
7191 alist_init(curwin->w_alist);
7192 }
7193}
7194# endif
7195#endif
7196
7197#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE) || defined(PROTO)
7198/*
7199 * Expand the file names in the global argument list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00007200 * If "fnum_list" is not NULL, use "fnum_list[fnum_len]" as a list of buffer
7201 * numbers to be re-used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007202 */
7203 void
Bram Moolenaar86b68352004-12-27 21:59:20 +00007204alist_expand(fnum_list, fnum_len)
7205 int *fnum_list;
7206 int fnum_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007207{
7208 char_u **old_arg_files;
Bram Moolenaar86b68352004-12-27 21:59:20 +00007209 int old_arg_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007210 char_u **new_arg_files;
7211 int new_arg_file_count;
7212 char_u *save_p_su = p_su;
7213 int i;
7214
7215 /* Don't use 'suffixes' here. This should work like the shell did the
7216 * expansion. Also, the vimrc file isn't read yet, thus the user
7217 * can't set the options. */
7218 p_su = empty_option;
7219 old_arg_files = (char_u **)alloc((unsigned)(sizeof(char_u *) * GARGCOUNT));
7220 if (old_arg_files != NULL)
7221 {
7222 for (i = 0; i < GARGCOUNT; ++i)
Bram Moolenaar86b68352004-12-27 21:59:20 +00007223 old_arg_files[i] = vim_strsave(GARGLIST[i].ae_fname);
7224 old_arg_count = GARGCOUNT;
7225 if (expand_wildcards(old_arg_count, old_arg_files,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007226 &new_arg_file_count, &new_arg_files,
Bram Moolenaarb5609832011-07-20 15:04:58 +02007227 EW_FILE|EW_NOTFOUND|EW_ADDSLASH|EW_NOERROR) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007228 && new_arg_file_count > 0)
7229 {
Bram Moolenaar86b68352004-12-27 21:59:20 +00007230 alist_set(&global_alist, new_arg_file_count, new_arg_files,
7231 TRUE, fnum_list, fnum_len);
7232 FreeWild(old_arg_count, old_arg_files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007233 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007234 }
7235 p_su = save_p_su;
7236}
7237#endif
7238
7239/*
7240 * Set the argument list for the current window.
7241 * Takes over the allocated files[] and the allocated fnames in it.
7242 */
7243 void
Bram Moolenaar86b68352004-12-27 21:59:20 +00007244alist_set(al, count, files, use_curbuf, fnum_list, fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007245 alist_T *al;
7246 int count;
7247 char_u **files;
7248 int use_curbuf;
Bram Moolenaar86b68352004-12-27 21:59:20 +00007249 int *fnum_list;
7250 int fnum_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007251{
7252 int i;
7253
7254 alist_clear(al);
7255 if (ga_grow(&al->al_ga, count) == OK)
7256 {
7257 for (i = 0; i < count; ++i)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007258 {
7259 if (got_int)
7260 {
7261 /* When adding many buffers this can take a long time. Allow
7262 * interrupting here. */
7263 while (i < count)
7264 vim_free(files[i++]);
7265 break;
7266 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00007267
7268 /* May set buffer name of a buffer previously used for the
7269 * argument list, so that it's re-used by alist_add. */
7270 if (fnum_list != NULL && i < fnum_len)
7271 buf_set_name(fnum_list[i], files[i]);
7272
Bram Moolenaar071d4272004-06-13 20:20:40 +00007273 alist_add(al, files[i], use_curbuf ? 2 : 1);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007274 ui_breakcheck();
7275 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007276 vim_free(files);
7277 }
7278 else
7279 FreeWild(count, files);
7280#ifdef FEAT_WINDOWS
7281 if (al == &global_alist)
7282#endif
7283 arg_had_last = FALSE;
7284}
7285
7286/*
7287 * Add file "fname" to argument list "al".
7288 * "fname" must have been allocated and "al" must have been checked for room.
7289 */
7290 void
7291alist_add(al, fname, set_fnum)
7292 alist_T *al;
7293 char_u *fname;
7294 int set_fnum; /* 1: set buffer number; 2: re-use curbuf */
7295{
7296 if (fname == NULL) /* don't add NULL file names */
7297 return;
7298#ifdef BACKSLASH_IN_FILENAME
7299 slash_adjust(fname);
7300#endif
7301 AARGLIST(al)[al->al_ga.ga_len].ae_fname = fname;
7302 if (set_fnum > 0)
7303 AARGLIST(al)[al->al_ga.ga_len].ae_fnum =
7304 buflist_add(fname, BLN_LISTED | (set_fnum == 2 ? BLN_CURBUF : 0));
7305 ++al->al_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007306}
7307
7308#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
7309/*
7310 * Adjust slashes in file names. Called after 'shellslash' was set.
7311 */
7312 void
7313alist_slash_adjust()
7314{
7315 int i;
7316# ifdef FEAT_WINDOWS
7317 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007318 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007319# endif
7320
7321 for (i = 0; i < GARGCOUNT; ++i)
7322 if (GARGLIST[i].ae_fname != NULL)
7323 slash_adjust(GARGLIST[i].ae_fname);
7324# ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00007325 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007326 if (wp->w_alist != &global_alist)
7327 for (i = 0; i < WARGCOUNT(wp); ++i)
7328 if (WARGLIST(wp)[i].ae_fname != NULL)
7329 slash_adjust(WARGLIST(wp)[i].ae_fname);
7330# endif
7331}
7332#endif
7333
7334/*
7335 * ":preserve".
7336 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007337 static void
7338ex_preserve(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007339 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007340{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007341 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007342 ml_preserve(curbuf, TRUE);
7343}
7344
7345/*
7346 * ":recover".
7347 */
7348 static void
7349ex_recover(eap)
7350 exarg_T *eap;
7351{
7352 /* Set recoverymode right away to avoid the ATTENTION prompt. */
7353 recoverymode = TRUE;
7354 if (!check_changed(curbuf, p_awa, TRUE, eap->forceit, FALSE)
7355 && (*eap->arg == NUL
7356 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
7357 ml_recover();
7358 recoverymode = FALSE;
7359}
7360
7361/*
7362 * Command modifier used in a wrong way.
7363 */
7364 static void
7365ex_wrongmodifier(eap)
7366 exarg_T *eap;
7367{
7368 eap->errmsg = e_invcmd;
7369}
7370
7371#ifdef FEAT_WINDOWS
7372/*
7373 * :sview [+command] file split window with new file, read-only
7374 * :split [[+command] file] split window with current or new file
7375 * :vsplit [[+command] file] split window vertically with current or new file
7376 * :new [[+command] file] split window with no or new file
7377 * :vnew [[+command] file] split vertically window with no or new file
7378 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007379 *
7380 * :tabedit open new Tab page with empty window
7381 * :tabedit [+command] file open new Tab page and edit "file"
7382 * :tabnew [[+command] file] just like :tabedit
7383 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007384 */
7385 void
7386ex_splitview(eap)
7387 exarg_T *eap;
7388{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007389 win_T *old_curwin = curwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007390# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007391 char_u *fname = NULL;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007392# endif
7393# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007394 int browse_flag = cmdmod.browse;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007395# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007396
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007397# ifndef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007398 if (eap->cmdidx == CMD_vsplit || eap->cmdidx == CMD_vnew)
7399 {
7400 ex_ni(eap);
7401 return;
7402 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007403# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007404
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007405# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007406 need_mouse_correct = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007407# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007408
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007409# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00007410 /* A ":split" in the quickfix window works like ":new". Don't want two
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007411 * quickfix windows. But it's OK when doing ":tab split". */
7412 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007413 {
7414 if (eap->cmdidx == CMD_split)
7415 eap->cmdidx = CMD_new;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007416# ifdef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007417 if (eap->cmdidx == CMD_vsplit)
7418 eap->cmdidx = CMD_vnew;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007419# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007420 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007421# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007422
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007423# ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007424 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007425 {
7426 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
7427 FNAME_MESS, TRUE, curbuf->b_ffname);
7428 if (fname == NULL)
7429 goto theend;
7430 eap->arg = fname;
7431 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007432# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007433 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007434# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007435# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007436# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007437 if (cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007438# ifdef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007439 && eap->cmdidx != CMD_vnew
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007440# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007441 && eap->cmdidx != CMD_new)
7442 {
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007443# ifdef FEAT_AUTOCMD
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007444 if (
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007445# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007446 !gui.in_use &&
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007447# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007448 au_has_group((char_u *)"FileExplorer"))
7449 {
7450 /* No browsing supported but we do have the file explorer:
7451 * Edit the directory. */
7452 if (*eap->arg == NUL || !mch_isdir(eap->arg))
7453 eap->arg = (char_u *)".";
7454 }
7455 else
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007456# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007457 {
7458 fname = do_browse(0, (char_u *)_("Edit File in new window"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007459 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007460 if (fname == NULL)
7461 goto theend;
7462 eap->arg = fname;
7463 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007464 }
7465 cmdmod.browse = FALSE; /* Don't browse again in do_ecmd(). */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007466# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007467
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007468 /*
7469 * Either open new tab page or split the window.
7470 */
7471 if (eap->cmdidx == CMD_tabedit
7472 || eap->cmdidx == CMD_tabfind
7473 || eap->cmdidx == CMD_tabnew)
7474 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00007475 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
7476 : eap->addr_count == 0 ? 0
7477 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007478 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00007479 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007480
7481 /* set the alternate buffer for the window we came from */
7482 if (curwin != old_curwin
7483 && win_valid(old_curwin)
7484 && old_curwin->w_buffer != curbuf
7485 && !cmdmod.keepalt)
7486 old_curwin->w_alt_fnum = curbuf->b_fnum;
7487 }
7488 }
7489 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007490 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
7491 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007492# ifdef FEAT_SCROLLBIND
Bram Moolenaar071d4272004-06-13 20:20:40 +00007493 /* Reset 'scrollbind' when editing another file, but keep it when
7494 * doing ":split" without arguments. */
7495 if (*eap->arg != NUL
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007496# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007497 || cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007498# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007499 )
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007500 {
7501 RESET_BINDING(curwin);
7502 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007503 else
7504 do_check_scrollbind(FALSE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007505# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007506 do_exedit(eap, old_curwin);
7507 }
7508
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007509# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007510 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007511# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007512
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007513# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007514theend:
7515 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007516# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007517}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007518
7519/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007520 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007521 */
7522 void
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007523tabpage_new()
7524{
7525 exarg_T ea;
7526
7527 vim_memset(&ea, 0, sizeof(ea));
7528 ea.cmdidx = CMD_tabnew;
7529 ea.cmd = (char_u *)"tabn";
7530 ea.arg = (char_u *)"";
7531 ex_splitview(&ea);
7532}
7533
7534/*
7535 * :tabnext command
7536 */
7537 static void
7538ex_tabnext(eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007539 exarg_T *eap;
7540{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007541 switch (eap->cmdidx)
7542 {
7543 case CMD_tabfirst:
7544 case CMD_tabrewind:
7545 goto_tabpage(1);
7546 break;
7547 case CMD_tablast:
7548 goto_tabpage(9999);
7549 break;
7550 case CMD_tabprevious:
7551 case CMD_tabNext:
7552 goto_tabpage(eap->addr_count == 0 ? -1 : -(int)eap->line2);
7553 break;
7554 default: /* CMD_tabnext */
7555 goto_tabpage(eap->addr_count == 0 ? 0 : (int)eap->line2);
7556 break;
7557 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007558}
7559
7560/*
7561 * :tabmove command
7562 */
7563 static void
7564ex_tabmove(eap)
7565 exarg_T *eap;
7566{
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02007567 int tab_number = 9999;
7568
7569 if (eap->arg && *eap->arg != NUL)
7570 {
7571 char_u *p = eap->arg;
7572 int relative = 0; /* argument +N/-N means: move N places to the
7573 * right/left relative to the current position. */
7574
7575 if (*eap->arg == '-')
7576 {
7577 relative = -1;
7578 p = eap->arg + 1;
7579 }
7580 else if (*eap->arg == '+')
7581 {
7582 relative = 1;
7583 p = eap->arg + 1;
7584 }
7585 else
7586 p = eap->arg;
7587
7588 if (p == skipdigits(p))
7589 {
7590 /* No numbers as argument. */
7591 eap->errmsg = e_invarg;
7592 return;
7593 }
7594
7595 tab_number = getdigits(&p);
7596 if (relative != 0)
7597 tab_number = tab_number * relative + tabpage_index(curtab) - 1;;
7598 }
7599 else if (eap->addr_count != 0)
7600 tab_number = eap->line2;
7601
7602 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007603}
7604
7605/*
7606 * :tabs command: List tabs and their contents.
7607 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007608 static void
7609ex_tabs(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007610 exarg_T *eap UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007611{
7612 tabpage_T *tp;
7613 win_T *wp;
7614 int tabcount = 1;
7615
7616 msg_start();
7617 msg_scroll = TRUE;
7618 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
7619 {
7620 msg_putchar('\n');
7621 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
7622 msg_outtrans_attr(IObuff, hl_attr(HLF_T));
7623 out_flush(); /* output one line at a time */
7624 ui_breakcheck();
7625
Bram Moolenaar030f0df2006-02-21 22:02:53 +00007626 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007627 wp = firstwin;
7628 else
7629 wp = tp->tp_firstwin;
7630 for ( ; wp != NULL && !got_int; wp = wp->w_next)
7631 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007632 msg_putchar('\n');
7633 msg_putchar(wp == curwin ? '>' : ' ');
7634 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007635 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
7636 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007637 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02007638 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007639 else
7640 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
7641 IObuff, IOSIZE, TRUE);
7642 msg_outtrans(IObuff);
7643 out_flush(); /* output one line at a time */
7644 ui_breakcheck();
7645 }
7646 }
7647}
7648
7649#endif /* FEAT_WINDOWS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007650
7651/*
7652 * ":mode": Set screen mode.
7653 * If no argument given, just get the screen size and redraw.
7654 */
7655 static void
7656ex_mode(eap)
7657 exarg_T *eap;
7658{
7659 if (*eap->arg == NUL)
7660 shell_resized();
7661 else
7662 mch_screenmode(eap->arg);
7663}
7664
7665#ifdef FEAT_WINDOWS
7666/*
7667 * ":resize".
7668 * set, increment or decrement current window height
7669 */
7670 static void
7671ex_resize(eap)
7672 exarg_T *eap;
7673{
7674 int n;
7675 win_T *wp = curwin;
7676
7677 if (eap->addr_count > 0)
7678 {
7679 n = eap->line2;
7680 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
7681 ;
7682 }
7683
7684#ifdef FEAT_GUI
7685 need_mouse_correct = TRUE;
7686#endif
7687 n = atol((char *)eap->arg);
7688#ifdef FEAT_VERTSPLIT
7689 if (cmdmod.split & WSP_VERT)
7690 {
7691 if (*eap->arg == '-' || *eap->arg == '+')
7692 n += W_WIDTH(curwin);
7693 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
7694 n = 9999;
7695 win_setwidth_win((int)n, wp);
7696 }
7697 else
7698#endif
7699 {
7700 if (*eap->arg == '-' || *eap->arg == '+')
7701 n += curwin->w_height;
7702 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
7703 n = 9999;
7704 win_setheight_win((int)n, wp);
7705 }
7706}
7707#endif
7708
7709/*
7710 * ":find [+command] <file>" command.
7711 */
7712 static void
7713ex_find(eap)
7714 exarg_T *eap;
7715{
7716#ifdef FEAT_SEARCHPATH
7717 char_u *fname;
7718 int count;
7719
7720 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
7721 TRUE, curbuf->b_ffname);
7722 if (eap->addr_count > 0)
7723 {
7724 /* Repeat finding the file "count" times. This matters when it
7725 * appears several times in the path. */
7726 count = eap->line2;
7727 while (fname != NULL && --count > 0)
7728 {
7729 vim_free(fname);
7730 fname = find_file_in_path(NULL, 0, FNAME_MESS,
7731 FALSE, curbuf->b_ffname);
7732 }
7733 }
7734
7735 if (fname != NULL)
7736 {
7737 eap->arg = fname;
7738#endif
7739 do_exedit(eap, NULL);
7740#ifdef FEAT_SEARCHPATH
7741 vim_free(fname);
7742 }
7743#endif
7744}
7745
7746/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00007747 * ":open" simulation: for now just work like ":visual".
7748 */
7749 static void
7750ex_open(eap)
7751 exarg_T *eap;
7752{
7753 regmatch_T regmatch;
7754 char_u *p;
7755
7756 curwin->w_cursor.lnum = eap->line2;
7757 beginline(BL_SOL | BL_FIX);
7758 if (*eap->arg == '/')
7759 {
7760 /* ":open /pattern/": put cursor in column found with pattern */
7761 ++eap->arg;
7762 p = skip_regexp(eap->arg, '/', p_magic, NULL);
7763 *p = NUL;
7764 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
7765 if (regmatch.regprog != NULL)
7766 {
7767 regmatch.rm_ic = p_ic;
7768 p = ml_get_curline();
7769 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007770 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007771 else
7772 EMSG(_(e_nomatch));
7773 vim_free(regmatch.regprog);
7774 }
7775 /* Move to the NUL, ignore any other arguments. */
7776 eap->arg += STRLEN(eap->arg);
7777 }
7778 check_cursor();
7779
7780 eap->cmdidx = CMD_visual;
7781 do_exedit(eap, NULL);
7782}
7783
7784/*
7785 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007786 */
7787 static void
7788ex_edit(eap)
7789 exarg_T *eap;
7790{
7791 do_exedit(eap, NULL);
7792}
7793
7794/*
7795 * ":edit <file>" command and alikes.
7796 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007797 void
7798do_exedit(eap, old_curwin)
7799 exarg_T *eap;
7800 win_T *old_curwin; /* curwin before doing a split or NULL */
7801{
7802 int n;
7803#ifdef FEAT_WINDOWS
7804 int need_hide;
7805#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00007806 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007807
7808 /*
7809 * ":vi" command ends Ex mode.
7810 */
7811 if (exmode_active && (eap->cmdidx == CMD_visual
7812 || eap->cmdidx == CMD_view))
7813 {
7814 exmode_active = FALSE;
7815 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007816 {
7817 /* Special case: ":global/pat/visual\NLvi-commands" */
7818 if (global_busy)
7819 {
7820 int rd = RedrawingDisabled;
7821 int nwr = no_wait_return;
7822 int ms = msg_scroll;
7823#ifdef FEAT_GUI
7824 int he = hold_gui_events;
7825#endif
7826
7827 if (eap->nextcmd != NULL)
7828 {
7829 stuffReadbuff(eap->nextcmd);
7830 eap->nextcmd = NULL;
7831 }
7832
7833 if (exmode_was != EXMODE_VIM)
7834 settmode(TMODE_RAW);
7835 RedrawingDisabled = 0;
7836 no_wait_return = 0;
7837 need_wait_return = FALSE;
7838 msg_scroll = 0;
7839#ifdef FEAT_GUI
7840 hold_gui_events = 0;
7841#endif
7842 must_redraw = CLEAR;
7843
7844 main_loop(FALSE, TRUE);
7845
7846 RedrawingDisabled = rd;
7847 no_wait_return = nwr;
7848 msg_scroll = ms;
7849#ifdef FEAT_GUI
7850 hold_gui_events = he;
7851#endif
7852 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007853 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007854 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007855 }
7856
7857 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007858 || eap->cmdidx == CMD_tabnew
7859 || eap->cmdidx == CMD_tabedit
Bram Moolenaar071d4272004-06-13 20:20:40 +00007860#ifdef FEAT_VERTSPLIT
7861 || eap->cmdidx == CMD_vnew
7862#endif
7863 ) && *eap->arg == NUL)
7864 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007865 /* ":new" or ":tabnew" without argument: edit an new empty buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007866 setpcmark();
7867 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007868 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
7869 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007870 }
7871 else if ((eap->cmdidx != CMD_split
7872#ifdef FEAT_VERTSPLIT
7873 && eap->cmdidx != CMD_vsplit
7874#endif
7875 )
7876 || *eap->arg != NUL
7877#ifdef FEAT_BROWSE
7878 || cmdmod.browse
7879#endif
7880 )
7881 {
Bram Moolenaar5555acc2006-04-07 21:33:12 +00007882#ifdef FEAT_AUTOCMD
7883 /* Can't edit another file when "curbuf_lock" is set. Only ":edit"
7884 * can bring us here, others are stopped earlier. */
7885 if (*eap->arg != NUL && curbuf_locked())
7886 return;
7887#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007888 n = readonlymode;
7889 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
7890 readonlymode = TRUE;
7891 else if (eap->cmdidx == CMD_enew)
7892 readonlymode = FALSE; /* 'readonly' doesn't make sense in an
7893 empty buffer */
7894 setpcmark();
7895 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
7896 NULL, eap,
7897 /* ":edit" goes to first line if Vi compatible */
7898 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
7899 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
7900 ? ECMD_ONE : eap->do_ecmd_lnum,
7901 (P_HID(curbuf) ? ECMD_HIDE : 0)
7902 + (eap->forceit ? ECMD_FORCEIT : 0)
7903#ifdef FEAT_LISTCMDS
7904 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
7905#endif
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007906 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007907 {
7908 /* Editing the file failed. If the window was split, close it. */
7909#ifdef FEAT_WINDOWS
7910 if (old_curwin != NULL)
7911 {
7912 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
7913 if (!need_hide || P_HID(curbuf))
7914 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007915# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7916 cleanup_T cs;
7917
7918 /* Reset the error/interrupt/exception state here so that
7919 * aborting() returns FALSE when closing a window. */
7920 enter_cleanup(&cs);
7921# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007922# ifdef FEAT_GUI
7923 need_mouse_correct = TRUE;
7924# endif
7925 win_close(curwin, !need_hide && !P_HID(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007926
7927# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7928 /* Restore the error/interrupt/exception state if not
7929 * discarded by a new aborting error, interrupt, or
7930 * uncaught exception. */
7931 leave_cleanup(&cs);
7932# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007933 }
7934 }
7935#endif
7936 }
7937 else if (readonlymode && curbuf->b_nwindows == 1)
7938 {
7939 /* When editing an already visited buffer, 'readonly' won't be set
7940 * but the previous value is kept. With ":view" and ":sview" we
7941 * want the file to be readonly, except when another window is
7942 * editing the same buffer. */
7943 curbuf->b_p_ro = TRUE;
7944 }
7945 readonlymode = n;
7946 }
7947 else
7948 {
7949 if (eap->do_ecmd_cmd != NULL)
7950 do_cmdline_cmd(eap->do_ecmd_cmd);
7951#ifdef FEAT_TITLE
7952 n = curwin->w_arg_idx_invalid;
7953#endif
7954 check_arg_idx(curwin);
7955#ifdef FEAT_TITLE
7956 if (n != curwin->w_arg_idx_invalid)
7957 maketitle();
7958#endif
7959 }
7960
7961#ifdef FEAT_WINDOWS
7962 /*
7963 * if ":split file" worked, set alternate file name in old window to new
7964 * file
7965 */
7966 if (old_curwin != NULL
7967 && *eap->arg != NUL
7968 && curwin != old_curwin
7969 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00007970 && old_curwin->w_buffer != curbuf
7971 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007972 old_curwin->w_alt_fnum = curbuf->b_fnum;
7973#endif
7974
7975 ex_no_reprint = TRUE;
7976}
7977
7978#ifndef FEAT_GUI
7979/*
7980 * ":gui" and ":gvim" when there is no GUI.
7981 */
7982 static void
7983ex_nogui(eap)
7984 exarg_T *eap;
7985{
7986 eap->errmsg = e_nogvim;
7987}
7988#endif
7989
7990#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
7991 static void
7992ex_tearoff(eap)
7993 exarg_T *eap;
7994{
7995 gui_make_tearoff(eap->arg);
7996}
7997#endif
7998
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007999#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008000 static void
8001ex_popup(eap)
8002 exarg_T *eap;
8003{
Bram Moolenaar97409f12005-07-08 22:17:29 +00008004 gui_make_popup(eap->arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008005}
8006#endif
8007
Bram Moolenaar071d4272004-06-13 20:20:40 +00008008 static void
8009ex_swapname(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008010 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008011{
8012 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
8013 MSG(_("No swap file"));
8014 else
8015 msg(curbuf->b_ml.ml_mfp->mf_fname);
8016}
8017
8018/*
8019 * ":syncbind" forces all 'scrollbind' windows to have the same relative
8020 * offset.
8021 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
8022 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008023 static void
8024ex_syncbind(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008025 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008026{
8027#ifdef FEAT_SCROLLBIND
8028 win_T *wp;
8029 long topline;
8030 long y;
8031 linenr_T old_linenr = curwin->w_cursor.lnum;
8032
8033 setpcmark();
8034
8035 /*
8036 * determine max topline
8037 */
8038 if (curwin->w_p_scb)
8039 {
8040 topline = curwin->w_topline;
8041 for (wp = firstwin; wp; wp = wp->w_next)
8042 {
8043 if (wp->w_p_scb && wp->w_buffer)
8044 {
8045 y = wp->w_buffer->b_ml.ml_line_count - p_so;
8046 if (topline > y)
8047 topline = y;
8048 }
8049 }
8050 if (topline < 1)
8051 topline = 1;
8052 }
8053 else
8054 {
8055 topline = 1;
8056 }
8057
8058
8059 /*
8060 * set all scrollbind windows to the same topline
8061 */
8062 wp = curwin;
8063 for (curwin = firstwin; curwin; curwin = curwin->w_next)
8064 {
8065 if (curwin->w_p_scb)
8066 {
8067 y = topline - curwin->w_topline;
8068 if (y > 0)
8069 scrollup(y, TRUE);
8070 else
8071 scrolldown(-y, TRUE);
8072 curwin->w_scbind_pos = topline;
8073 redraw_later(VALID);
8074 cursor_correct();
8075#ifdef FEAT_WINDOWS
8076 curwin->w_redr_status = TRUE;
8077#endif
8078 }
8079 }
8080 curwin = wp;
8081 if (curwin->w_p_scb)
8082 {
8083 did_syncbind = TRUE;
8084 checkpcmark();
8085 if (old_linenr != curwin->w_cursor.lnum)
8086 {
8087 char_u ctrl_o[2];
8088
8089 ctrl_o[0] = Ctrl_O;
8090 ctrl_o[1] = 0;
8091 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
8092 }
8093 }
8094#endif
8095}
8096
8097
8098 static void
8099ex_read(eap)
8100 exarg_T *eap;
8101{
Bram Moolenaardf177f62005-02-22 08:39:57 +00008102 int i;
8103 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
8104 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008105
8106 if (eap->usefilter) /* :r!cmd */
8107 do_bang(1, eap, FALSE, FALSE, TRUE);
8108 else
8109 {
8110 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
8111 return;
8112
8113#ifdef FEAT_BROWSE
8114 if (cmdmod.browse)
8115 {
8116 char_u *browseFile;
8117
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008118 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008119 NULL, NULL, NULL, curbuf);
8120 if (browseFile != NULL)
8121 {
8122 i = readfile(browseFile, NULL,
8123 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8124 vim_free(browseFile);
8125 }
8126 else
8127 i = OK;
8128 }
8129 else
8130#endif
8131 if (*eap->arg == NUL)
8132 {
8133 if (check_fname() == FAIL) /* check for no file name */
8134 return;
8135 i = readfile(curbuf->b_ffname, curbuf->b_fname,
8136 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8137 }
8138 else
8139 {
8140 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
8141 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
8142 i = readfile(eap->arg, NULL,
8143 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8144
8145 }
8146 if (i == FAIL)
8147 {
8148#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
8149 if (!aborting())
8150#endif
8151 EMSG2(_(e_notopen), eap->arg);
8152 }
8153 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008154 {
8155 if (empty && exmode_active)
8156 {
8157 /* Delete the empty line that remains. Historically ex does
8158 * this but vi doesn't. */
8159 if (eap->line2 == 0)
8160 lnum = curbuf->b_ml.ml_line_count;
8161 else
8162 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008163 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008164 {
8165 ml_delete(lnum, FALSE);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008166 if (curwin->w_cursor.lnum > 1
8167 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008168 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00008169 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008170 }
8171 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008172 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008173 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008174 }
8175}
8176
Bram Moolenaarea408852005-06-25 22:49:46 +00008177static char_u *prev_dir = NULL;
8178
8179#if defined(EXITFREE) || defined(PROTO)
8180 void
8181free_cd_dir()
8182{
8183 vim_free(prev_dir);
Bram Moolenaara0174af2008-01-02 20:08:25 +00008184 prev_dir = NULL;
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00008185
8186 vim_free(globaldir);
8187 globaldir = NULL;
Bram Moolenaarea408852005-06-25 22:49:46 +00008188}
8189#endif
8190
Bram Moolenaarf4258302013-06-02 18:20:17 +02008191/*
8192 * Deal with the side effects of changing the current directory.
8193 * When "local" is TRUE then this was after an ":lcd" command.
8194 */
8195 void
8196post_chdir(local)
8197 int local;
8198{
8199 vim_free(curwin->w_localdir);
8200 if (local)
8201 {
8202 /* If still in global directory, need to remember current
8203 * directory as global directory. */
8204 if (globaldir == NULL && prev_dir != NULL)
8205 globaldir = vim_strsave(prev_dir);
8206 /* Remember this local directory for the window. */
8207 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8208 curwin->w_localdir = vim_strsave(NameBuff);
8209 }
8210 else
8211 {
8212 /* We are now in the global directory, no need to remember its
8213 * name. */
8214 vim_free(globaldir);
8215 globaldir = NULL;
8216 curwin->w_localdir = NULL;
8217 }
8218
8219 shorten_fnames(TRUE);
8220}
8221
Bram Moolenaarea408852005-06-25 22:49:46 +00008222
Bram Moolenaar071d4272004-06-13 20:20:40 +00008223/*
8224 * ":cd", ":lcd", ":chdir" and ":lchdir".
8225 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00008226 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00008227ex_cd(eap)
8228 exarg_T *eap;
8229{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008230 char_u *new_dir;
8231 char_u *tofree;
8232
8233 new_dir = eap->arg;
8234#if !defined(UNIX) && !defined(VMS)
8235 /* for non-UNIX ":cd" means: print current directory */
8236 if (*new_dir == NUL)
8237 ex_pwd(NULL);
8238 else
8239#endif
8240 {
Bram Moolenaar8e8fe9b2009-03-11 14:37:32 +00008241#ifdef FEAT_AUTOCMD
8242 if (allbuf_locked())
8243 return;
8244#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00008245 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged()
8246 && !eap->forceit)
8247 {
Bram Moolenaar81870892007-11-11 18:17:28 +00008248 EMSG(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00008249 return;
8250 }
8251
Bram Moolenaar071d4272004-06-13 20:20:40 +00008252 /* ":cd -": Change to previous directory */
8253 if (STRCMP(new_dir, "-") == 0)
8254 {
8255 if (prev_dir == NULL)
8256 {
8257 EMSG(_("E186: No previous directory"));
8258 return;
8259 }
8260 new_dir = prev_dir;
8261 }
8262
8263 /* Save current directory for next ":cd -" */
8264 tofree = prev_dir;
8265 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8266 prev_dir = vim_strsave(NameBuff);
8267 else
8268 prev_dir = NULL;
8269
8270#if defined(UNIX) || defined(VMS)
8271 /* for UNIX ":cd" means: go to home directory */
8272 if (*new_dir == NUL)
8273 {
8274 /* use NameBuff for home directory name */
8275# ifdef VMS
8276 char_u *p;
8277
8278 p = mch_getenv((char_u *)"SYS$LOGIN");
8279 if (p == NULL || *p == NUL) /* empty is the same as not set */
8280 NameBuff[0] = NUL;
8281 else
Bram Moolenaarb6356332005-07-18 21:40:44 +00008282 vim_strncpy(NameBuff, p, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008283# else
8284 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
8285# endif
8286 new_dir = NameBuff;
8287 }
8288#endif
8289 if (new_dir == NULL || vim_chdir(new_dir))
8290 EMSG(_(e_failed));
8291 else
8292 {
Bram Moolenaarf4258302013-06-02 18:20:17 +02008293 post_chdir(eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008294
8295 /* Echo the new current directory if the command was typed. */
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00008296 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008297 ex_pwd(eap);
8298 }
8299 vim_free(tofree);
8300 }
8301}
8302
8303/*
8304 * ":pwd".
8305 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008306 static void
8307ex_pwd(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008308 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008309{
8310 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8311 {
8312#ifdef BACKSLASH_IN_FILENAME
8313 slash_adjust(NameBuff);
8314#endif
8315 msg(NameBuff);
8316 }
8317 else
8318 EMSG(_("E187: Unknown"));
8319}
8320
8321/*
8322 * ":=".
8323 */
8324 static void
8325ex_equal(eap)
8326 exarg_T *eap;
8327{
8328 smsg((char_u *)"%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008329 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008330}
8331
8332 static void
8333ex_sleep(eap)
8334 exarg_T *eap;
8335{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008336 int n;
8337 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008338
8339 if (cursor_valid())
8340 {
8341 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
8342 if (n >= 0)
8343 windgoto((int)n, curwin->w_wcol);
8344 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008345
8346 len = eap->line2;
8347 switch (*eap->arg)
8348 {
8349 case 'm': break;
8350 case NUL: len *= 1000L; break;
8351 default: EMSG2(_(e_invarg2), eap->arg); return;
8352 }
8353 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008354}
8355
8356/*
8357 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
8358 */
8359 void
8360do_sleep(msec)
8361 long msec;
8362{
8363 long done;
8364
8365 cursor_on();
8366 out_flush();
8367 for (done = 0; !got_int && done < msec; done += 1000L)
8368 {
8369 ui_delay(msec - done > 1000L ? 1000L : msec - done, TRUE);
8370 ui_breakcheck();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02008371#ifdef FEAT_NETBEANS_INTG
8372 /* Process the netbeans messages that may have been received in the
8373 * call to ui_breakcheck() when the GUI is in use. This may occur when
8374 * running a test case. */
8375 netbeans_parse_messages();
8376#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008377 }
8378}
8379
8380 static void
8381do_exmap(eap, isabbrev)
8382 exarg_T *eap;
8383 int isabbrev;
8384{
8385 int mode;
8386 char_u *cmdp;
8387
8388 cmdp = eap->cmd;
8389 mode = get_map_mode(&cmdp, eap->forceit || isabbrev);
8390
8391 switch (do_map((*cmdp == 'n') ? 2 : (*cmdp == 'u'),
8392 eap->arg, mode, isabbrev))
8393 {
8394 case 1: EMSG(_(e_invarg));
8395 break;
8396 case 2: EMSG(isabbrev ? _(e_noabbr) : _(e_nomap));
8397 break;
8398 }
8399}
8400
8401/*
8402 * ":winsize" command (obsolete).
8403 */
8404 static void
8405ex_winsize(eap)
8406 exarg_T *eap;
8407{
8408 int w, h;
8409 char_u *arg = eap->arg;
8410 char_u *p;
8411
8412 w = getdigits(&arg);
8413 arg = skipwhite(arg);
8414 p = arg;
8415 h = getdigits(&arg);
8416 if (*p != NUL && *arg == NUL)
8417 set_shellsize(w, h, TRUE);
8418 else
8419 EMSG(_("E465: :winsize requires two number arguments"));
8420}
8421
8422#ifdef FEAT_WINDOWS
8423 static void
8424ex_wincmd(eap)
8425 exarg_T *eap;
8426{
8427 int xchar = NUL;
8428 char_u *p;
8429
8430 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
8431 {
8432 /* CTRL-W g and CTRL-W CTRL-G have an extra command character */
8433 if (eap->arg[1] == NUL)
8434 {
8435 EMSG(_(e_invarg));
8436 return;
8437 }
8438 xchar = eap->arg[1];
8439 p = eap->arg + 2;
8440 }
8441 else
8442 p = eap->arg + 1;
8443
8444 eap->nextcmd = check_nextcmd(p);
8445 p = skipwhite(p);
8446 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
8447 EMSG(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02008448 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008449 {
8450 /* Pass flags on for ":vertical wincmd ]". */
8451 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008452 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008453 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
8454 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008455 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008456 }
8457}
8458#endif
8459
Bram Moolenaar843ee412004-06-30 16:16:41 +00008460#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008461/*
8462 * ":winpos".
8463 */
8464 static void
8465ex_winpos(eap)
8466 exarg_T *eap;
8467{
8468 int x, y;
8469 char_u *arg = eap->arg;
8470 char_u *p;
8471
8472 if (*arg == NUL)
8473 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00008474# if defined(FEAT_GUI) || defined(MSWIN)
8475# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008476 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00008477# else
8478 if (mch_get_winpos(&x, &y) != FAIL)
8479# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008480 {
8481 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
8482 msg(IObuff);
8483 }
8484 else
8485# endif
8486 EMSG(_("E188: Obtaining window position not implemented for this platform"));
8487 }
8488 else
8489 {
8490 x = getdigits(&arg);
8491 arg = skipwhite(arg);
8492 p = arg;
8493 y = getdigits(&arg);
8494 if (*p == NUL || *arg != NUL)
8495 {
8496 EMSG(_("E466: :winpos requires two number arguments"));
8497 return;
8498 }
8499# ifdef FEAT_GUI
8500 if (gui.in_use)
8501 gui_mch_set_winpos(x, y);
8502 else if (gui.starting)
8503 {
8504 /* Remember the coordinates for when the window is opened. */
8505 gui_win_x = x;
8506 gui_win_y = y;
8507 }
8508# ifdef HAVE_TGETENT
8509 else
8510# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00008511# else
8512# ifdef MSWIN
8513 mch_set_winpos(x, y);
8514# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008515# endif
8516# ifdef HAVE_TGETENT
8517 if (*T_CWP)
8518 term_set_winpos(x, y);
8519# endif
8520 }
8521}
8522#endif
8523
8524/*
8525 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
8526 */
8527 static void
8528ex_operators(eap)
8529 exarg_T *eap;
8530{
8531 oparg_T oa;
8532
8533 clear_oparg(&oa);
8534 oa.regname = eap->regname;
8535 oa.start.lnum = eap->line1;
8536 oa.end.lnum = eap->line2;
8537 oa.line_count = eap->line2 - eap->line1 + 1;
8538 oa.motion_type = MLINE;
8539#ifdef FEAT_VIRTUALEDIT
8540 virtual_op = FALSE;
8541#endif
8542 if (eap->cmdidx != CMD_yank) /* position cursor for undo */
8543 {
8544 setpcmark();
8545 curwin->w_cursor.lnum = eap->line1;
8546 beginline(BL_SOL | BL_FIX);
8547 }
8548
8549 switch (eap->cmdidx)
8550 {
8551 case CMD_delete:
8552 oa.op_type = OP_DELETE;
8553 op_delete(&oa);
8554 break;
8555
8556 case CMD_yank:
8557 oa.op_type = OP_YANK;
8558 (void)op_yank(&oa, FALSE, TRUE);
8559 break;
8560
8561 default: /* CMD_rshift or CMD_lshift */
8562 if ((eap->cmdidx == CMD_rshift)
8563#ifdef FEAT_RIGHTLEFT
8564 ^ curwin->w_p_rl
8565#endif
8566 )
8567 oa.op_type = OP_RSHIFT;
8568 else
8569 oa.op_type = OP_LSHIFT;
8570 op_shift(&oa, FALSE, eap->amount);
8571 break;
8572 }
8573#ifdef FEAT_VIRTUALEDIT
8574 virtual_op = MAYBE;
8575#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00008576 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008577}
8578
8579/*
8580 * ":put".
8581 */
8582 static void
8583ex_put(eap)
8584 exarg_T *eap;
8585{
8586 /* ":0put" works like ":1put!". */
8587 if (eap->line2 == 0)
8588 {
8589 eap->line2 = 1;
8590 eap->forceit = TRUE;
8591 }
8592 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008593 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
8594 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008595}
8596
8597/*
8598 * Handle ":copy" and ":move".
8599 */
8600 static void
8601ex_copymove(eap)
8602 exarg_T *eap;
8603{
8604 long n;
8605
8606 n = get_address(&eap->arg, FALSE, FALSE);
8607 if (eap->arg == NULL) /* error detected */
8608 {
8609 eap->nextcmd = NULL;
8610 return;
8611 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008612 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008613
8614 /*
8615 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
8616 */
8617 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
8618 {
8619 EMSG(_(e_invaddr));
8620 return;
8621 }
8622
8623 if (eap->cmdidx == CMD_move)
8624 {
8625 if (do_move(eap->line1, eap->line2, n) == FAIL)
8626 return;
8627 }
8628 else
8629 ex_copy(eap->line1, eap->line2, n);
8630 u_clearline();
8631 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008632 ex_may_print(eap);
8633}
8634
8635/*
8636 * Print the current line if flags were given to the Ex command.
8637 */
8638 static void
8639ex_may_print(eap)
8640 exarg_T *eap;
8641{
8642 if (eap->flags != 0)
8643 {
8644 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
8645 (eap->flags & EXFLAG_LIST));
8646 ex_no_reprint = TRUE;
8647 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008648}
8649
8650/*
8651 * ":smagic" and ":snomagic".
8652 */
8653 static void
8654ex_submagic(eap)
8655 exarg_T *eap;
8656{
8657 int magic_save = p_magic;
8658
8659 p_magic = (eap->cmdidx == CMD_smagic);
8660 do_sub(eap);
8661 p_magic = magic_save;
8662}
8663
8664/*
8665 * ":join".
8666 */
8667 static void
8668ex_join(eap)
8669 exarg_T *eap;
8670{
8671 curwin->w_cursor.lnum = eap->line1;
8672 if (eap->line1 == eap->line2)
8673 {
8674 if (eap->addr_count >= 2) /* :2,2join does nothing */
8675 return;
8676 if (eap->line2 == curbuf->b_ml.ml_line_count)
8677 {
8678 beep_flush();
8679 return;
8680 }
8681 ++eap->line2;
8682 }
Bram Moolenaar81340392012-06-06 16:12:59 +02008683 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008684 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008685 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008686}
8687
8688/*
8689 * ":[addr]@r" or ":[addr]*r": execute register
8690 */
8691 static void
8692ex_at(eap)
8693 exarg_T *eap;
8694{
8695 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00008696 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008697
8698 curwin->w_cursor.lnum = eap->line2;
8699
8700#ifdef USE_ON_FLY_SCROLL
8701 dont_scroll = TRUE; /* disallow scrolling here */
8702#endif
8703
8704 /* get the register name. No name means to use the previous one */
8705 c = *eap->arg;
8706 if (c == NUL || (c == '*' && *eap->cmd == '*'))
8707 c = '@';
Bram Moolenaard333d1e2006-11-07 17:43:47 +00008708 /* Put the register in the typeahead buffer with the "silent" flag. */
8709 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
8710 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008711 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008712 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00008713 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008714 else
8715 {
8716 int save_efr = exec_from_reg;
8717
8718 exec_from_reg = TRUE;
8719
8720 /*
8721 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00008722 * Continue until the stuff buffer is empty and all added characters
8723 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008724 */
Bram Moolenaar60462872009-11-03 11:40:19 +00008725 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008726 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
8727
8728 exec_from_reg = save_efr;
8729 }
8730}
8731
8732/*
8733 * ":!".
8734 */
8735 static void
8736ex_bang(eap)
8737 exarg_T *eap;
8738{
8739 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
8740}
8741
8742/*
8743 * ":undo".
8744 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008745 static void
8746ex_undo(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008747 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008748{
Bram Moolenaard3667a22006-03-16 21:35:52 +00008749 if (eap->addr_count == 1) /* :undo 123 */
Bram Moolenaar730cde92010-06-27 05:18:54 +02008750 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00008751 else
8752 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008753}
8754
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008755#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008756 static void
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008757ex_wundo(eap)
8758 exarg_T *eap;
8759{
8760 char_u hash[UNDO_HASH_SIZE];
8761
8762 u_compute_hash(hash);
8763 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
8764}
8765
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008766 static void
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008767ex_rundo(eap)
8768 exarg_T *eap;
8769{
8770 char_u hash[UNDO_HASH_SIZE];
8771
8772 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02008773 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008774}
8775#endif
8776
Bram Moolenaar071d4272004-06-13 20:20:40 +00008777/*
8778 * ":redo".
8779 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008780 static void
8781ex_redo(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008782 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008783{
8784 u_redo(1);
8785}
8786
8787/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008788 * ":earlier" and ":later".
8789 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008790 static void
8791ex_later(eap)
8792 exarg_T *eap;
8793{
8794 long count = 0;
8795 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008796 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008797 char_u *p = eap->arg;
8798
8799 if (*p == NUL)
8800 count = 1;
8801 else if (isdigit(*p))
8802 {
8803 count = getdigits(&p);
8804 switch (*p)
8805 {
8806 case 's': ++p; sec = TRUE; break;
8807 case 'm': ++p; sec = TRUE; count *= 60; break;
8808 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008809 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
8810 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008811 }
8812 }
8813
8814 if (*p != NUL)
8815 EMSG2(_(e_invarg2), eap->arg);
8816 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02008817 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
8818 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008819}
8820
8821/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008822 * ":redir": start/stop redirection.
8823 */
8824 static void
8825ex_redir(eap)
8826 exarg_T *eap;
8827{
8828 char *mode;
8829 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00008830 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008831
8832 if (STRICMP(eap->arg, "END") == 0)
8833 close_redir();
8834 else
8835 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008836 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008837 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008838 ++arg;
8839 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008840 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008841 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008842 mode = "a";
8843 }
8844 else
8845 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00008846 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008847
8848 close_redir();
8849
8850 /* Expand environment variables and "~/". */
Bram Moolenaarca472992005-01-21 11:46:23 +00008851 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008852 if (fname == NULL)
8853 return;
8854#ifdef FEAT_BROWSE
8855 if (cmdmod.browse)
8856 {
8857 char_u *browseFile;
8858
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008859 browseFile = do_browse(BROWSE_SAVE,
8860 (char_u *)_("Save Redirection"),
8861 fname, NULL, NULL, BROWSE_FILTER_ALL_FILES, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008862 if (browseFile == NULL)
8863 return; /* operation cancelled */
8864 vim_free(fname);
8865 fname = browseFile;
8866 eap->forceit = TRUE; /* since dialog already asked */
8867 }
8868#endif
8869
8870 redir_fd = open_exfile(fname, eap->forceit, mode);
8871 vim_free(fname);
8872 }
8873#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00008874 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008875 {
8876 /* redirect to a register a-z (resp. A-Z for appending) */
8877 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00008878 ++arg;
8879 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008880# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00008881 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00008882 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008883# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00008884 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008885 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008886 redir_reg = *arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008887 if (*arg == '>' && arg[1] == '>') /* append */
Bram Moolenaard9d30582005-05-18 22:10:28 +00008888 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008889 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008890 {
Bram Moolenaarc188b882007-10-19 14:20:54 +00008891 /* Can use both "@a" and "@a>". */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00008892 if (*arg == '>')
8893 arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008894 /* Make register empty when not using @A-@Z and the
8895 * command is valid. */
8896 if (*arg == NUL && !isupper(redir_reg))
8897 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008898 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008899 }
8900 if (*arg != NUL)
8901 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00008902 redir_reg = 0;
Bram Moolenaard9d30582005-05-18 22:10:28 +00008903 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008904 }
8905 }
8906 else if (*arg == '=' && arg[1] == '>')
8907 {
8908 int append;
8909
8910 /* redirect to a variable */
8911 close_redir();
8912 arg += 2;
8913
8914 if (*arg == '>')
8915 {
8916 ++arg;
8917 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008918 }
8919 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008920 append = FALSE;
8921
8922 if (var_redir_start(skipwhite(arg), append) == OK)
8923 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008924 }
8925#endif
8926
8927 /* TODO: redirect to a buffer */
8928
Bram Moolenaar071d4272004-06-13 20:20:40 +00008929 else
Bram Moolenaarca472992005-01-21 11:46:23 +00008930 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008931 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00008932
8933 /* Make sure redirection is not off. Can happen for cmdline completion
8934 * that indirectly invokes a command to catch its output. */
8935 if (redir_fd != NULL
8936#ifdef FEAT_EVAL
8937 || redir_reg || redir_vname
8938#endif
8939 )
8940 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008941}
8942
8943/*
8944 * ":redraw": force redraw
8945 */
8946 static void
8947ex_redraw(eap)
8948 exarg_T *eap;
8949{
8950 int r = RedrawingDisabled;
8951 int p = p_lz;
8952
8953 RedrawingDisabled = 0;
8954 p_lz = FALSE;
8955 update_topline();
8956 update_screen(eap->forceit ? CLEAR :
8957#ifdef FEAT_VISUAL
8958 VIsual_active ? INVERTED :
8959#endif
8960 0);
8961#ifdef FEAT_TITLE
8962 if (need_maketitle)
8963 maketitle();
8964#endif
8965 RedrawingDisabled = r;
8966 p_lz = p;
8967
8968 /* Reset msg_didout, so that a message that's there is overwritten. */
8969 msg_didout = FALSE;
8970 msg_col = 0;
8971
8972 out_flush();
8973}
8974
8975/*
8976 * ":redrawstatus": force redraw of status line(s)
8977 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008978 static void
8979ex_redrawstatus(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008980 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008981{
8982#if defined(FEAT_WINDOWS)
8983 int r = RedrawingDisabled;
8984 int p = p_lz;
8985
8986 RedrawingDisabled = 0;
8987 p_lz = FALSE;
8988 if (eap->forceit)
8989 status_redraw_all();
8990 else
8991 status_redraw_curbuf();
8992 update_screen(
8993# ifdef FEAT_VISUAL
8994 VIsual_active ? INVERTED :
8995# endif
8996 0);
8997 RedrawingDisabled = r;
8998 p_lz = p;
8999 out_flush();
9000#endif
9001}
9002
9003 static void
9004close_redir()
9005{
9006 if (redir_fd != NULL)
9007 {
9008 fclose(redir_fd);
9009 redir_fd = NULL;
9010 }
9011#ifdef FEAT_EVAL
9012 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009013 if (redir_vname)
9014 {
9015 var_redir_stop();
9016 redir_vname = 0;
9017 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009018#endif
9019}
9020
9021#if defined(FEAT_SESSION) && defined(USE_CRNL)
9022# define MKSESSION_NL
9023static int mksession_nl = FALSE; /* use NL only in put_eol() */
9024#endif
9025
9026/*
9027 * ":mkexrc", ":mkvimrc", ":mkview" and ":mksession".
9028 */
9029 static void
9030ex_mkrc(eap)
9031 exarg_T *eap;
9032{
9033 FILE *fd;
9034 int failed = FALSE;
9035 char_u *fname;
9036#ifdef FEAT_BROWSE
9037 char_u *browseFile = NULL;
9038#endif
9039#ifdef FEAT_SESSION
9040 int view_session = FALSE;
9041 int using_vdir = FALSE; /* using 'viewdir'? */
9042 char_u *viewFile = NULL;
9043 unsigned *flagp;
9044#endif
9045
9046 if (eap->cmdidx == CMD_mksession || eap->cmdidx == CMD_mkview)
9047 {
9048#ifdef FEAT_SESSION
9049 view_session = TRUE;
9050#else
9051 ex_ni(eap);
9052 return;
9053#endif
9054 }
9055
9056#ifdef FEAT_SESSION
Bram Moolenaar8d594672009-07-01 18:18:57 +00009057 /* Use the short file name until ":lcd" is used. We also don't use the
9058 * short file name when 'acd' is set, that is checked later. */
Bram Moolenaareeefcc72007-05-01 21:21:21 +00009059 did_lcd = FALSE;
9060
Bram Moolenaar071d4272004-06-13 20:20:40 +00009061 /* ":mkview" or ":mkview 9": generate file name with 'viewdir' */
9062 if (eap->cmdidx == CMD_mkview
9063 && (*eap->arg == NUL
9064 || (vim_isdigit(*eap->arg) && eap->arg[1] == NUL)))
9065 {
9066 eap->forceit = TRUE;
9067 fname = get_view_file(*eap->arg);
9068 if (fname == NULL)
9069 return;
9070 viewFile = fname;
9071 using_vdir = TRUE;
9072 }
9073 else
9074#endif
9075 if (*eap->arg != NUL)
9076 fname = eap->arg;
9077 else if (eap->cmdidx == CMD_mkvimrc)
9078 fname = (char_u *)VIMRC_FILE;
9079#ifdef FEAT_SESSION
9080 else if (eap->cmdidx == CMD_mksession)
9081 fname = (char_u *)SESSION_FILE;
9082#endif
9083 else
9084 fname = (char_u *)EXRC_FILE;
9085
9086#ifdef FEAT_BROWSE
9087 if (cmdmod.browse)
9088 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00009089 browseFile = do_browse(BROWSE_SAVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009090# ifdef FEAT_SESSION
9091 eap->cmdidx == CMD_mkview ? (char_u *)_("Save View") :
9092 eap->cmdidx == CMD_mksession ? (char_u *)_("Save Session") :
9093# endif
9094 (char_u *)_("Save Setup"),
9095 fname, (char_u *)"vim", NULL, BROWSE_FILTER_MACROS, NULL);
9096 if (browseFile == NULL)
9097 goto theend;
9098 fname = browseFile;
9099 eap->forceit = TRUE; /* since dialog already asked */
9100 }
9101#endif
9102
9103#if defined(FEAT_SESSION) && defined(vim_mkdir)
9104 /* When using 'viewdir' may have to create the directory. */
9105 if (using_vdir && !mch_isdir(p_vdir))
Bram Moolenaardf177f62005-02-22 08:39:57 +00009106 vim_mkdir_emsg(p_vdir, 0755);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009107#endif
9108
9109 fd = open_exfile(fname, eap->forceit, WRITEBIN);
9110 if (fd != NULL)
9111 {
9112#ifdef FEAT_SESSION
9113 if (eap->cmdidx == CMD_mkview)
9114 flagp = &vop_flags;
9115 else
9116 flagp = &ssop_flags;
9117#endif
9118
9119#ifdef MKSESSION_NL
9120 /* "unix" in 'sessionoptions': use NL line separator */
9121 if (view_session && (*flagp & SSOP_UNIX))
9122 mksession_nl = TRUE;
9123#endif
9124
9125 /* Write the version command for :mkvimrc */
9126 if (eap->cmdidx == CMD_mkvimrc)
9127 (void)put_line(fd, "version 6.0");
9128
9129#ifdef FEAT_SESSION
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009130 if (eap->cmdidx == CMD_mksession)
9131 {
9132 if (put_line(fd, "let SessionLoad = 1") == FAIL)
9133 failed = TRUE;
9134 }
9135
Bram Moolenaar071d4272004-06-13 20:20:40 +00009136 if (eap->cmdidx != CMD_mkview)
9137#endif
9138 {
9139 /* Write setting 'compatible' first, because it has side effects.
9140 * For that same reason only do it when needed. */
9141 if (p_cp)
9142 (void)put_line(fd, "if !&cp | set cp | endif");
9143 else
9144 (void)put_line(fd, "if &cp | set nocp | endif");
9145 }
9146
9147#ifdef FEAT_SESSION
9148 if (!view_session
9149 || (eap->cmdidx == CMD_mksession
9150 && (*flagp & SSOP_OPTIONS)))
9151#endif
9152 failed |= (makemap(fd, NULL) == FAIL
9153 || makeset(fd, OPT_GLOBAL, FALSE) == FAIL);
9154
9155#ifdef FEAT_SESSION
9156 if (!failed && view_session)
9157 {
9158 if (put_line(fd, "let s:so_save = &so | let s:siso_save = &siso | set so=0 siso=0") == FAIL)
9159 failed = TRUE;
9160 if (eap->cmdidx == CMD_mksession)
9161 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02009162 char_u *dirnow; /* current directory */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009163
Bram Moolenaard9462e32011-04-11 21:35:11 +02009164 dirnow = alloc(MAXPATHL);
9165 if (dirnow == NULL)
9166 failed = TRUE;
9167 else
9168 {
9169 /*
9170 * Change to session file's dir.
9171 */
9172 if (mch_dirname(dirnow, MAXPATHL) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00009173 || mch_chdir((char *)dirnow) != 0)
Bram Moolenaard9462e32011-04-11 21:35:11 +02009174 *dirnow = NUL;
9175 if (*dirnow != NUL && (ssop_flags & SSOP_SESDIR))
9176 {
9177 if (vim_chdirfile(fname) == OK)
9178 shorten_fnames(TRUE);
9179 }
9180 else if (*dirnow != NUL
9181 && (ssop_flags & SSOP_CURDIR) && globaldir != NULL)
9182 {
9183 if (mch_chdir((char *)globaldir) == 0)
9184 shorten_fnames(TRUE);
9185 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009186
Bram Moolenaard9462e32011-04-11 21:35:11 +02009187 failed |= (makeopens(fd, dirnow) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009188
Bram Moolenaard9462e32011-04-11 21:35:11 +02009189 /* restore original dir */
9190 if (*dirnow != NUL && ((ssop_flags & SSOP_SESDIR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009191 || ((ssop_flags & SSOP_CURDIR) && globaldir != NULL)))
Bram Moolenaard9462e32011-04-11 21:35:11 +02009192 {
9193 if (mch_chdir((char *)dirnow) != 0)
9194 EMSG(_(e_prev_dir));
9195 shorten_fnames(TRUE);
9196 }
9197 vim_free(dirnow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009198 }
9199 }
9200 else
9201 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +00009202 failed |= (put_view(fd, curwin, !using_vdir, flagp,
9203 -1) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009204 }
9205 if (put_line(fd, "let &so = s:so_save | let &siso = s:siso_save")
9206 == FAIL)
9207 failed = TRUE;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009208 if (put_line(fd, "doautoall SessionLoadPost") == FAIL)
9209 failed = TRUE;
Bram Moolenaar4770d092006-01-12 23:22:24 +00009210 if (eap->cmdidx == CMD_mksession)
9211 {
9212 if (put_line(fd, "unlet SessionLoad") == FAIL)
9213 failed = TRUE;
9214 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009215 }
9216#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009217 if (put_line(fd, "\" vim: set ft=vim :") == FAIL)
9218 failed = TRUE;
9219
Bram Moolenaar071d4272004-06-13 20:20:40 +00009220 failed |= fclose(fd);
9221
9222 if (failed)
9223 EMSG(_(e_write));
9224#if defined(FEAT_EVAL) && defined(FEAT_SESSION)
9225 else if (eap->cmdidx == CMD_mksession)
9226 {
9227 /* successful session write - set this_session var */
Bram Moolenaard9462e32011-04-11 21:35:11 +02009228 char_u *tbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009229
Bram Moolenaard9462e32011-04-11 21:35:11 +02009230 tbuf = alloc(MAXPATHL);
9231 if (tbuf != NULL)
9232 {
9233 if (vim_FullName(fname, tbuf, MAXPATHL, FALSE) == OK)
9234 set_vim_var_string(VV_THIS_SESSION, tbuf, -1);
9235 vim_free(tbuf);
9236 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009237 }
9238#endif
9239#ifdef MKSESSION_NL
9240 mksession_nl = FALSE;
9241#endif
9242 }
9243
9244#ifdef FEAT_BROWSE
9245theend:
9246 vim_free(browseFile);
9247#endif
9248#ifdef FEAT_SESSION
9249 vim_free(viewFile);
9250#endif
9251}
9252
Bram Moolenaardf177f62005-02-22 08:39:57 +00009253#if ((defined(FEAT_SESSION) || defined(FEAT_EVAL)) && defined(vim_mkdir)) \
9254 || defined(PROTO)
9255 int
9256vim_mkdir_emsg(name, prot)
9257 char_u *name;
Bram Moolenaar78a15312009-05-15 19:33:18 +00009258 int prot UNUSED;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009259{
9260 if (vim_mkdir(name, prot) != 0)
9261 {
9262 EMSG2(_("E739: Cannot create directory: %s"), name);
9263 return FAIL;
9264 }
9265 return OK;
9266}
9267#endif
9268
Bram Moolenaar071d4272004-06-13 20:20:40 +00009269/*
9270 * Open a file for writing for an Ex command, with some checks.
9271 * Return file descriptor, or NULL on failure.
9272 */
9273 FILE *
9274open_exfile(fname, forceit, mode)
9275 char_u *fname;
9276 int forceit;
9277 char *mode; /* "w" for create new file or "a" for append */
9278{
9279 FILE *fd;
9280
9281#ifdef UNIX
9282 /* with Unix it is possible to open a directory */
9283 if (mch_isdir(fname))
9284 {
9285 EMSG2(_(e_isadir2), fname);
9286 return NULL;
9287 }
9288#endif
9289 if (!forceit && *mode != 'a' && vim_fexists(fname))
9290 {
9291 EMSG2(_("E189: \"%s\" exists (add ! to override)"), fname);
9292 return NULL;
9293 }
9294
9295 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
9296 EMSG2(_("E190: Cannot open \"%s\" for writing"), fname);
9297
9298 return fd;
9299}
9300
9301/*
9302 * ":mark" and ":k".
9303 */
9304 static void
9305ex_mark(eap)
9306 exarg_T *eap;
9307{
9308 pos_T pos;
9309
9310 if (*eap->arg == NUL) /* No argument? */
9311 EMSG(_(e_argreq));
9312 else if (eap->arg[1] != NUL) /* more than one character? */
9313 EMSG(_(e_trailing));
9314 else
9315 {
9316 pos = curwin->w_cursor; /* save curwin->w_cursor */
9317 curwin->w_cursor.lnum = eap->line2;
9318 beginline(BL_WHITE | BL_FIX);
9319 if (setmark(*eap->arg) == FAIL) /* set mark */
9320 EMSG(_("E191: Argument must be a letter or forward/backward quote"));
9321 curwin->w_cursor = pos; /* restore curwin->w_cursor */
9322 }
9323}
9324
9325/*
9326 * Update w_topline, w_leftcol and the cursor position.
9327 */
9328 void
9329update_topline_cursor()
9330{
9331 check_cursor(); /* put cursor on valid line */
9332 update_topline();
9333 if (!curwin->w_p_wrap)
9334 validate_cursor();
9335 update_curswant();
9336}
9337
9338#ifdef FEAT_EX_EXTRA
9339/*
9340 * ":normal[!] {commands}": Execute normal mode commands.
9341 */
9342 static void
9343ex_normal(eap)
9344 exarg_T *eap;
9345{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009346 int save_msg_scroll = msg_scroll;
9347 int save_restart_edit = restart_edit;
9348 int save_msg_didout = msg_didout;
9349 int save_State = State;
9350 tasave_T tabuf;
9351 int save_insertmode = p_im;
9352 int save_finish_op = finish_op;
Bram Moolenaar38084112008-07-26 14:05:07 +00009353 int save_opcount = opcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009354#ifdef FEAT_MBYTE
9355 char_u *arg = NULL;
9356 int l;
9357 char_u *p;
9358#endif
9359
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00009360 if (ex_normal_lock > 0)
9361 {
9362 EMSG(_(e_secure));
9363 return;
9364 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009365 if (ex_normal_busy >= p_mmd)
9366 {
9367 EMSG(_("E192: Recursive use of :normal too deep"));
9368 return;
9369 }
9370 ++ex_normal_busy;
9371
9372 msg_scroll = FALSE; /* no msg scrolling in Normal mode */
9373 restart_edit = 0; /* don't go to Insert mode */
9374 p_im = FALSE; /* don't use 'insertmode' */
9375
9376#ifdef FEAT_MBYTE
9377 /*
9378 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
9379 * this for the K_SPECIAL leading byte, otherwise special keys will not
9380 * work.
9381 */
9382 if (has_mbyte)
9383 {
9384 int len = 0;
9385
9386 /* Count the number of characters to be escaped. */
9387 for (p = eap->arg; *p != NUL; ++p)
9388 {
9389# ifdef FEAT_GUI
9390 if (*p == CSI) /* leadbyte CSI */
9391 len += 2;
9392# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009393 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009394 if (*++p == K_SPECIAL /* trailbyte K_SPECIAL or CSI */
9395# ifdef FEAT_GUI
9396 || *p == CSI
9397# endif
9398 )
9399 len += 2;
9400 }
9401 if (len > 0)
9402 {
9403 arg = alloc((unsigned)(STRLEN(eap->arg) + len + 1));
9404 if (arg != NULL)
9405 {
9406 len = 0;
9407 for (p = eap->arg; *p != NUL; ++p)
9408 {
9409 arg[len++] = *p;
9410# ifdef FEAT_GUI
9411 if (*p == CSI)
9412 {
9413 arg[len++] = KS_EXTRA;
9414 arg[len++] = (int)KE_CSI;
9415 }
9416# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009417 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009418 {
9419 arg[len++] = *++p;
9420 if (*p == K_SPECIAL)
9421 {
9422 arg[len++] = KS_SPECIAL;
9423 arg[len++] = KE_FILLER;
9424 }
9425# ifdef FEAT_GUI
9426 else if (*p == CSI)
9427 {
9428 arg[len++] = KS_EXTRA;
9429 arg[len++] = (int)KE_CSI;
9430 }
9431# endif
9432 }
9433 arg[len] = NUL;
9434 }
9435 }
9436 }
9437 }
9438#endif
9439
9440 /*
9441 * Save the current typeahead. This is required to allow using ":normal"
9442 * from an event handler and makes sure we don't hang when the argument
9443 * ends with half a command.
9444 */
9445 save_typeahead(&tabuf);
9446 if (tabuf.typebuf_valid)
9447 {
9448 /*
9449 * Repeat the :normal command for each line in the range. When no
9450 * range given, execute it just once, without positioning the cursor
9451 * first.
9452 */
9453 do
9454 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009455 if (eap->addr_count != 0)
9456 {
9457 curwin->w_cursor.lnum = eap->line1++;
9458 curwin->w_cursor.col = 0;
9459 }
9460
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009461 exec_normal_cmd(
Bram Moolenaar071d4272004-06-13 20:20:40 +00009462#ifdef FEAT_MBYTE
9463 arg != NULL ? arg :
9464#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009465 eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009466 }
9467 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
9468 }
9469
9470 /* Might not return to the main loop when in an event handler. */
9471 update_topline_cursor();
9472
9473 /* Restore the previous typeahead. */
9474 restore_typeahead(&tabuf);
9475
9476 --ex_normal_busy;
9477 msg_scroll = save_msg_scroll;
9478 restart_edit = save_restart_edit;
9479 p_im = save_insertmode;
9480 finish_op = save_finish_op;
Bram Moolenaar38084112008-07-26 14:05:07 +00009481 opcount = save_opcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009482 msg_didout |= save_msg_didout; /* don't reset msg_didout now */
9483
9484 /* Restore the state (needed when called from a function executed for
9485 * 'indentexpr'). */
9486 State = save_State;
9487#ifdef FEAT_MBYTE
9488 vim_free(arg);
9489#endif
9490}
9491
9492/*
Bram Moolenaar64969662005-12-14 21:59:55 +00009493 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009494 */
9495 static void
9496ex_startinsert(eap)
9497 exarg_T *eap;
9498{
Bram Moolenaarfd371682005-01-14 21:42:54 +00009499 if (eap->forceit)
9500 {
9501 coladvance((colnr_T)MAXCOL);
9502 curwin->w_curswant = MAXCOL;
9503 curwin->w_set_curswant = FALSE;
9504 }
9505
Bram Moolenaara40c5002005-01-09 21:16:21 +00009506 /* Ignore the command when already in Insert mode. Inserting an
9507 * expression register that invokes a function can do this. */
9508 if (State & INSERT)
9509 return;
9510
Bram Moolenaar64969662005-12-14 21:59:55 +00009511 if (eap->cmdidx == CMD_startinsert)
9512 restart_edit = 'a';
9513 else if (eap->cmdidx == CMD_startreplace)
9514 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00009515 else
Bram Moolenaar64969662005-12-14 21:59:55 +00009516 restart_edit = 'V';
9517
9518 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009519 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009520 if (eap->cmdidx == CMD_startinsert)
9521 restart_edit = 'i';
Bram Moolenaar071d4272004-06-13 20:20:40 +00009522 curwin->w_curswant = 0; /* avoid MAXCOL */
9523 }
9524}
9525
9526/*
9527 * ":stopinsert"
9528 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009529 static void
9530ex_stopinsert(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009531 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009532{
9533 restart_edit = 0;
9534 stop_insert_mode = TRUE;
9535}
9536#endif
9537
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009538#if defined(FEAT_EX_EXTRA) || defined(FEAT_MENU) || defined(PROTO)
9539/*
9540 * Execute normal mode command "cmd".
9541 * "remap" can be REMAP_NONE or REMAP_YES.
9542 */
9543 void
9544exec_normal_cmd(cmd, remap, silent)
9545 char_u *cmd;
9546 int remap;
9547 int silent;
9548{
9549 oparg_T oa;
9550
9551 /*
9552 * Stuff the argument into the typeahead buffer.
9553 * Execute normal_cmd() until there is no typeahead left.
9554 */
9555 clear_oparg(&oa);
9556 finish_op = FALSE;
9557 ins_typebuf(cmd, remap, 0, TRUE, silent);
9558 while ((!stuff_empty() || (!typebuf_typed() && typebuf.tb_len > 0))
9559 && !got_int)
9560 {
9561 update_topline_cursor();
Bram Moolenaar48ac02c2011-01-17 19:50:06 +01009562 normal_cmd(&oa, TRUE); /* execute a Normal mode cmd */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009563 }
9564}
9565#endif
9566
Bram Moolenaar071d4272004-06-13 20:20:40 +00009567#ifdef FEAT_FIND_ID
9568 static void
9569ex_checkpath(eap)
9570 exarg_T *eap;
9571{
9572 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
9573 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
9574 (linenr_T)1, (linenr_T)MAXLNUM);
9575}
9576
9577#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
9578/*
9579 * ":psearch"
9580 */
9581 static void
9582ex_psearch(eap)
9583 exarg_T *eap;
9584{
9585 g_do_tagpreview = p_pvh;
9586 ex_findpat(eap);
9587 g_do_tagpreview = 0;
9588}
9589#endif
9590
9591 static void
9592ex_findpat(eap)
9593 exarg_T *eap;
9594{
9595 int whole = TRUE;
9596 long n;
9597 char_u *p;
9598 int action;
9599
9600 switch (cmdnames[eap->cmdidx].cmd_name[2])
9601 {
9602 case 'e': /* ":psearch", ":isearch" and ":dsearch" */
9603 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
9604 action = ACTION_GOTO;
9605 else
9606 action = ACTION_SHOW;
9607 break;
9608 case 'i': /* ":ilist" and ":dlist" */
9609 action = ACTION_SHOW_ALL;
9610 break;
9611 case 'u': /* ":ijump" and ":djump" */
9612 action = ACTION_GOTO;
9613 break;
9614 default: /* ":isplit" and ":dsplit" */
9615 action = ACTION_SPLIT;
9616 break;
9617 }
9618
9619 n = 1;
9620 if (vim_isdigit(*eap->arg)) /* get count */
9621 {
9622 n = getdigits(&eap->arg);
9623 eap->arg = skipwhite(eap->arg);
9624 }
9625 if (*eap->arg == '/') /* Match regexp, not just whole words */
9626 {
9627 whole = FALSE;
9628 ++eap->arg;
9629 p = skip_regexp(eap->arg, '/', p_magic, NULL);
9630 if (*p)
9631 {
9632 *p++ = NUL;
9633 p = skipwhite(p);
9634
9635 /* Check for trailing illegal characters */
9636 if (!ends_excmd(*p))
9637 eap->errmsg = e_trailing;
9638 else
9639 eap->nextcmd = check_nextcmd(p);
9640 }
9641 }
9642 if (!eap->skip)
9643 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
9644 whole, !eap->forceit,
9645 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
9646 n, action, eap->line1, eap->line2);
9647}
9648#endif
9649
9650#ifdef FEAT_WINDOWS
9651
9652# ifdef FEAT_QUICKFIX
9653/*
9654 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
9655 */
9656 static void
9657ex_ptag(eap)
9658 exarg_T *eap;
9659{
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01009660 g_do_tagpreview = p_pvh; /* will be reset to 0 in ex_tag_cmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009661 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9662}
9663
9664/*
9665 * ":pedit"
9666 */
9667 static void
9668ex_pedit(eap)
9669 exarg_T *eap;
9670{
9671 win_T *curwin_save = curwin;
9672
9673 g_do_tagpreview = p_pvh;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00009674 prepare_tagpreview(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009675 keep_help_flag = curwin_save->w_buffer->b_help;
9676 do_exedit(eap, NULL);
9677 keep_help_flag = FALSE;
9678 if (curwin != curwin_save && win_valid(curwin_save))
9679 {
9680 /* Return cursor to where we were */
9681 validate_cursor();
9682 redraw_later(VALID);
9683 win_enter(curwin_save, TRUE);
9684 }
9685 g_do_tagpreview = 0;
9686}
9687# endif
9688
9689/*
9690 * ":stag", ":stselect" and ":stjump".
9691 */
9692 static void
9693ex_stag(eap)
9694 exarg_T *eap;
9695{
9696 postponed_split = -1;
9697 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009698 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009699 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9700 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009701 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009702}
9703#endif
9704
9705/*
9706 * ":tag", ":tselect", ":tjump", ":tnext", etc.
9707 */
9708 static void
9709ex_tag(eap)
9710 exarg_T *eap;
9711{
9712 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
9713}
9714
9715 static void
9716ex_tag_cmd(eap, name)
9717 exarg_T *eap;
9718 char_u *name;
9719{
9720 int cmd;
9721
9722 switch (name[1])
9723 {
9724 case 'j': cmd = DT_JUMP; /* ":tjump" */
9725 break;
9726 case 's': cmd = DT_SELECT; /* ":tselect" */
9727 break;
9728 case 'p': cmd = DT_PREV; /* ":tprevious" */
9729 break;
9730 case 'N': cmd = DT_PREV; /* ":tNext" */
9731 break;
9732 case 'n': cmd = DT_NEXT; /* ":tnext" */
9733 break;
9734 case 'o': cmd = DT_POP; /* ":pop" */
9735 break;
9736 case 'f': /* ":tfirst" */
9737 case 'r': cmd = DT_FIRST; /* ":trewind" */
9738 break;
9739 case 'l': cmd = DT_LAST; /* ":tlast" */
9740 break;
9741 default: /* ":tag" */
9742#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +00009743 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009744 {
9745 do_cstag(eap);
9746 return;
9747 }
9748#endif
9749 cmd = DT_TAG;
9750 break;
9751 }
9752
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00009753 if (name[0] == 'l')
9754 {
9755#ifndef FEAT_QUICKFIX
9756 ex_ni(eap);
9757 return;
9758#else
9759 cmd = DT_LTAG;
9760#endif
9761 }
9762
Bram Moolenaar071d4272004-06-13 20:20:40 +00009763 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
9764 eap->forceit, TRUE);
9765}
9766
9767/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009768 * Check "str" for starting with a special cmdline variable.
9769 * If found return one of the SPEC_ values and set "*usedlen" to the length of
9770 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
9771 */
9772 int
9773find_cmdline_var(src, usedlen)
9774 char_u *src;
9775 int *usedlen;
9776{
9777 int len;
9778 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +00009779 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009780 "%",
9781#define SPEC_PERC 0
9782 "#",
9783#define SPEC_HASH 1
9784 "<cword>", /* cursor word */
9785#define SPEC_CWORD 2
9786 "<cWORD>", /* cursor WORD */
9787#define SPEC_CCWORD 3
9788 "<cfile>", /* cursor path name */
9789#define SPEC_CFILE 4
9790 "<sfile>", /* ":so" file name */
9791#define SPEC_SFILE 5
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009792 "<slnum>", /* ":so" file line number */
9793#define SPEC_SLNUM 6
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009794#ifdef FEAT_AUTOCMD
9795 "<afile>", /* autocommand file name */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009796# define SPEC_AFILE 7
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009797 "<abuf>", /* autocommand buffer number */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009798# define SPEC_ABUF 8
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009799 "<amatch>", /* autocommand match name */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009800# define SPEC_AMATCH 9
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009801#endif
9802#ifdef FEAT_CLIENTSERVER
9803 "<client>"
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009804# ifdef FEAT_AUTOCMD
9805# define SPEC_CLIENT 10
9806# else
9807# define SPEC_CLIENT 7
9808# endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009809#endif
9810 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009811
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009812 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009813 {
9814 len = (int)STRLEN(spec_str[i]);
9815 if (STRNCMP(src, spec_str[i], len) == 0)
9816 {
9817 *usedlen = len;
9818 return i;
9819 }
9820 }
9821 return -1;
9822}
9823
9824/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009825 * Evaluate cmdline variables.
9826 *
9827 * change '%' to curbuf->b_ffname
9828 * '#' to curwin->w_altfile
9829 * '<cword>' to word under the cursor
9830 * '<cWORD>' to WORD under the cursor
9831 * '<cfile>' to path name under the cursor
9832 * '<sfile>' to sourced file name
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009833 * '<slnum>' to sourced file line number
Bram Moolenaar071d4272004-06-13 20:20:40 +00009834 * '<afile>' to file name for autocommand
9835 * '<abuf>' to buffer number for autocommand
9836 * '<amatch>' to matching name for autocommand
9837 *
9838 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
9839 * "" for error without a message) and NULL is returned.
9840 * Returns an allocated string if a valid match was found.
9841 * Returns NULL if no match was found. "usedlen" then still contains the
9842 * number of characters to skip.
9843 */
9844 char_u *
Bram Moolenaar63b92542007-03-27 14:57:09 +00009845eval_vars(src, srcstart, usedlen, lnump, errormsg, escaped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009846 char_u *src; /* pointer into commandline */
Bram Moolenaar63b92542007-03-27 14:57:09 +00009847 char_u *srcstart; /* beginning of valid memory for src */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009848 int *usedlen; /* characters after src that are used */
9849 linenr_T *lnump; /* line number for :e command, or NULL */
9850 char_u **errormsg; /* pointer to error message */
Bram Moolenaar63b92542007-03-27 14:57:09 +00009851 int *escaped; /* return value has escaped white space (can
9852 * be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009853{
9854 int i;
9855 char_u *s;
9856 char_u *result;
9857 char_u *resultbuf = NULL;
9858 int resultlen;
9859 buf_T *buf;
9860 int valid = VALID_HEAD + VALID_PATH; /* assume valid result */
9861 int spec_idx;
9862#ifdef FEAT_MODIFY_FNAME
9863 int skip_mod = FALSE;
9864#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009865 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009866
9867 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009868 if (escaped != NULL)
9869 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009870
9871 /*
9872 * Check if there is something to do.
9873 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009874 spec_idx = find_cmdline_var(src, usedlen);
9875 if (spec_idx < 0) /* no match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009876 {
9877 *usedlen = 1;
9878 return NULL;
9879 }
9880
9881 /*
9882 * Skip when preceded with a backslash "\%" and "\#".
9883 * Note: In "\\%" the % is also not recognized!
9884 */
9885 if (src > srcstart && src[-1] == '\\')
9886 {
9887 *usedlen = 0;
Bram Moolenaara7241f52008-06-24 20:39:31 +00009888 STRMOVE(src - 1, src); /* remove backslash */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009889 return NULL;
9890 }
9891
9892 /*
9893 * word or WORD under cursor
9894 */
9895 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD)
9896 {
9897 resultlen = find_ident_under_cursor(&result, spec_idx == SPEC_CWORD ?
9898 (FIND_IDENT|FIND_STRING) : FIND_STRING);
9899 if (resultlen == 0)
9900 {
9901 *errormsg = (char_u *)"";
9902 return NULL;
9903 }
9904 }
9905
9906 /*
9907 * '#': Alternate file name
9908 * '%': Current file name
9909 * File name under the cursor
9910 * File name for autocommand
9911 * and following modifiers
9912 */
9913 else
9914 {
9915 switch (spec_idx)
9916 {
9917 case SPEC_PERC: /* '%': current file */
9918 if (curbuf->b_fname == NULL)
9919 {
9920 result = (char_u *)"";
9921 valid = 0; /* Must have ":p:h" to be valid */
9922 }
9923 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009924 result = curbuf->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009925 break;
9926
9927 case SPEC_HASH: /* '#' or "#99": alternate file */
9928 if (src[1] == '#') /* "##": the argument list */
9929 {
9930 result = arg_all();
9931 resultbuf = result;
9932 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009933 if (escaped != NULL)
9934 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009935#ifdef FEAT_MODIFY_FNAME
9936 skip_mod = TRUE;
9937#endif
9938 break;
9939 }
9940 s = src + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00009941 if (*s == '<') /* "#<99" uses v:oldfiles */
9942 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009943 i = (int)getdigits(&s);
9944 *usedlen = (int)(s - src); /* length of what we expand */
9945
Bram Moolenaard812df62008-11-09 12:46:09 +00009946 if (src[1] == '<')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009947 {
Bram Moolenaard812df62008-11-09 12:46:09 +00009948 if (*usedlen < 2)
9949 {
9950 /* Should we give an error message for #<text? */
9951 *usedlen = 1;
9952 return NULL;
9953 }
9954#ifdef FEAT_EVAL
9955 result = list_find_str(get_vim_var_list(VV_OLDFILES),
9956 (long)i);
9957 if (result == NULL)
9958 {
9959 *errormsg = (char_u *)"";
9960 return NULL;
9961 }
9962#else
9963 *errormsg = (char_u *)_("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009964 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00009965#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009966 }
9967 else
Bram Moolenaard812df62008-11-09 12:46:09 +00009968 {
9969 buf = buflist_findnr(i);
9970 if (buf == NULL)
9971 {
9972 *errormsg = (char_u *)_("E194: No alternate file name to substitute for '#'");
9973 return NULL;
9974 }
9975 if (lnump != NULL)
9976 *lnump = ECMD_LAST;
9977 if (buf->b_fname == NULL)
9978 {
9979 result = (char_u *)"";
9980 valid = 0; /* Must have ":p:h" to be valid */
9981 }
9982 else
9983 result = buf->b_fname;
9984 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009985 break;
9986
9987#ifdef FEAT_SEARCHPATH
9988 case SPEC_CFILE: /* file name under cursor */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009989 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009990 if (result == NULL)
9991 {
9992 *errormsg = (char_u *)"";
9993 return NULL;
9994 }
9995 resultbuf = result; /* remember allocated string */
9996 break;
9997#endif
9998
9999#ifdef FEAT_AUTOCMD
10000 case SPEC_AFILE: /* file name for autocommand */
10001 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +000010002 if (result != NULL && !autocmd_fname_full)
10003 {
10004 /* Still need to turn the fname into a full path. It is
10005 * postponed to avoid a delay when <afile> is not used. */
10006 autocmd_fname_full = TRUE;
10007 result = FullName_save(autocmd_fname, FALSE);
10008 vim_free(autocmd_fname);
10009 autocmd_fname = result;
10010 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010011 if (result == NULL)
10012 {
10013 *errormsg = (char_u *)_("E495: no autocommand file name to substitute for \"<afile>\"");
10014 return NULL;
10015 }
Bram Moolenaara0174af2008-01-02 20:08:25 +000010016 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010017 break;
10018
10019 case SPEC_ABUF: /* buffer number for autocommand */
10020 if (autocmd_bufnr <= 0)
10021 {
10022 *errormsg = (char_u *)_("E496: no autocommand buffer number to substitute for \"<abuf>\"");
10023 return NULL;
10024 }
10025 sprintf((char *)strbuf, "%d", autocmd_bufnr);
10026 result = strbuf;
10027 break;
10028
10029 case SPEC_AMATCH: /* match name for autocommand */
10030 result = autocmd_match;
10031 if (result == NULL)
10032 {
10033 *errormsg = (char_u *)_("E497: no autocommand match name to substitute for \"<amatch>\"");
10034 return NULL;
10035 }
10036 break;
10037
10038#endif
10039 case SPEC_SFILE: /* file name for ":so" command */
10040 result = sourcing_name;
10041 if (result == NULL)
10042 {
10043 *errormsg = (char_u *)_("E498: no :source file name to substitute for \"<sfile>\"");
10044 return NULL;
10045 }
10046 break;
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010047 case SPEC_SLNUM: /* line in file for ":so" command */
10048 if (sourcing_name == NULL || sourcing_lnum == 0)
10049 {
10050 *errormsg = (char_u *)_("E842: no line number to use for \"<slnum>\"");
10051 return NULL;
10052 }
10053 sprintf((char *)strbuf, "%ld", (long)sourcing_lnum);
10054 result = strbuf;
10055 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010056#if defined(FEAT_CLIENTSERVER)
10057 case SPEC_CLIENT: /* Source of last submitted input */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010058 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
10059 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010060 result = strbuf;
10061 break;
10062#endif
10063 }
10064
10065 resultlen = (int)STRLEN(result); /* length of new string */
10066 if (src[*usedlen] == '<') /* remove the file name extension */
10067 {
10068 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010069 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010070 resultlen = (int)(s - result);
10071 }
10072#ifdef FEAT_MODIFY_FNAME
10073 else if (!skip_mod)
10074 {
10075 valid |= modify_fname(src, usedlen, &result, &resultbuf,
10076 &resultlen);
10077 if (result == NULL)
10078 {
10079 *errormsg = (char_u *)"";
10080 return NULL;
10081 }
10082 }
10083#endif
10084 }
10085
10086 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
10087 {
10088 if (valid != VALID_HEAD + VALID_PATH)
10089 /* xgettext:no-c-format */
10090 *errormsg = (char_u *)_("E499: Empty file name for '%' or '#', only works with \":p:h\"");
10091 else
10092 *errormsg = (char_u *)_("E500: Evaluates to an empty string");
10093 result = NULL;
10094 }
10095 else
10096 result = vim_strnsave(result, resultlen);
10097 vim_free(resultbuf);
10098 return result;
10099}
10100
10101/*
10102 * Concatenate all files in the argument list, separated by spaces, and return
10103 * it in one allocated string.
10104 * Spaces and backslashes in the file names are escaped with a backslash.
10105 * Returns NULL when out of memory.
10106 */
10107 static char_u *
10108arg_all()
10109{
10110 int len;
10111 int idx;
10112 char_u *retval = NULL;
10113 char_u *p;
10114
10115 /*
10116 * Do this loop two times:
10117 * first time: compute the total length
10118 * second time: concatenate the names
10119 */
10120 for (;;)
10121 {
10122 len = 0;
10123 for (idx = 0; idx < ARGCOUNT; ++idx)
10124 {
10125 p = alist_name(&ARGLIST[idx]);
10126 if (p != NULL)
10127 {
10128 if (len > 0)
10129 {
10130 /* insert a space in between names */
10131 if (retval != NULL)
10132 retval[len] = ' ';
10133 ++len;
10134 }
10135 for ( ; *p != NUL; ++p)
10136 {
10137 if (*p == ' ' || *p == '\\')
10138 {
10139 /* insert a backslash */
10140 if (retval != NULL)
10141 retval[len] = '\\';
10142 ++len;
10143 }
10144 if (retval != NULL)
10145 retval[len] = *p;
10146 ++len;
10147 }
10148 }
10149 }
10150
10151 /* second time: break here */
10152 if (retval != NULL)
10153 {
10154 retval[len] = NUL;
10155 break;
10156 }
10157
10158 /* allocate memory */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000010159 retval = alloc((unsigned)len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010160 if (retval == NULL)
10161 break;
10162 }
10163
10164 return retval;
10165}
10166
10167#if defined(FEAT_AUTOCMD) || defined(PROTO)
10168/*
10169 * Expand the <sfile> string in "arg".
10170 *
10171 * Returns an allocated string, or NULL for any error.
10172 */
10173 char_u *
10174expand_sfile(arg)
10175 char_u *arg;
10176{
10177 char_u *errormsg;
10178 int len;
10179 char_u *result;
10180 char_u *newres;
10181 char_u *repl;
10182 int srclen;
10183 char_u *p;
10184
10185 result = vim_strsave(arg);
10186 if (result == NULL)
10187 return NULL;
10188
10189 for (p = result; *p; )
10190 {
10191 if (STRNCMP(p, "<sfile>", 7) != 0)
10192 ++p;
10193 else
10194 {
10195 /* replace "<sfile>" with the sourced file name, and do ":" stuff */
Bram Moolenaar63b92542007-03-27 14:57:09 +000010196 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010197 if (errormsg != NULL)
10198 {
10199 if (*errormsg)
10200 emsg(errormsg);
10201 vim_free(result);
10202 return NULL;
10203 }
10204 if (repl == NULL) /* no match (cannot happen) */
10205 {
10206 p += srclen;
10207 continue;
10208 }
10209 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
10210 newres = alloc(len);
10211 if (newres == NULL)
10212 {
10213 vim_free(repl);
10214 vim_free(result);
10215 return NULL;
10216 }
10217 mch_memmove(newres, result, (size_t)(p - result));
10218 STRCPY(newres + (p - result), repl);
10219 len = (int)STRLEN(newres);
10220 STRCAT(newres, p + srclen);
10221 vim_free(repl);
10222 vim_free(result);
10223 result = newres;
10224 p = newres + len; /* continue after the match */
10225 }
10226 }
10227
10228 return result;
10229}
10230#endif
10231
10232#ifdef FEAT_SESSION
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010233static int ses_winsizes __ARGS((FILE *fd, int restore_size,
10234 win_T *tab_firstwin));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010235static int ses_win_rec __ARGS((FILE *fd, frame_T *fr));
10236static frame_T *ses_skipframe __ARGS((frame_T *fr));
10237static int ses_do_frame __ARGS((frame_T *fr));
10238static int ses_do_win __ARGS((win_T *wp));
10239static int ses_arglist __ARGS((FILE *fd, char *cmd, garray_T *gap, int fullname, unsigned *flagp));
10240static int ses_put_fname __ARGS((FILE *fd, char_u *name, unsigned *flagp));
10241static int ses_fname __ARGS((FILE *fd, buf_T *buf, unsigned *flagp));
10242
10243/*
10244 * Write openfile commands for the current buffers to an .exrc file.
10245 * Return FAIL on error, OK otherwise.
10246 */
10247 static int
10248makeopens(fd, dirnow)
10249 FILE *fd;
10250 char_u *dirnow; /* Current directory name */
10251{
10252 buf_T *buf;
10253 int only_save_windows = TRUE;
10254 int nr;
10255 int cnr = 1;
10256 int restore_size = TRUE;
10257 win_T *wp;
10258 char_u *sname;
10259 win_T *edited_win = NULL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010260 int tabnr;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010261 win_T *tab_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010262 frame_T *tab_topframe;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010263 int cur_arg_idx = 0;
Bram Moolenaar383c6f52008-01-04 15:01:07 +000010264 int next_arg_idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010265
10266 if (ssop_flags & SSOP_BUFFERS)
10267 only_save_windows = FALSE; /* Save ALL buffers */
10268
10269 /*
10270 * Begin by setting the this_session variable, and then other
10271 * sessionable variables.
10272 */
10273#ifdef FEAT_EVAL
10274 if (put_line(fd, "let v:this_session=expand(\"<sfile>:p\")") == FAIL)
10275 return FAIL;
10276 if (ssop_flags & SSOP_GLOBALS)
10277 if (store_session_globals(fd) == FAIL)
10278 return FAIL;
10279#endif
10280
10281 /*
10282 * Close all windows but one.
10283 */
10284 if (put_line(fd, "silent only") == FAIL)
10285 return FAIL;
10286
10287 /*
10288 * Now a :cd command to the session directory or the current directory
10289 */
10290 if (ssop_flags & SSOP_SESDIR)
10291 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010292 if (put_line(fd, "exe \"cd \" . escape(expand(\"<sfile>:p:h\"), ' ')")
10293 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010294 return FAIL;
10295 }
10296 else if (ssop_flags & SSOP_CURDIR)
10297 {
10298 sname = home_replace_save(NULL, globaldir != NULL ? globaldir : dirnow);
10299 if (sname == NULL
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010300 || fputs("cd ", fd) < 0
10301 || ses_put_fname(fd, sname, &ssop_flags) == FAIL
10302 || put_eol(fd) == FAIL)
10303 {
10304 vim_free(sname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010305 return FAIL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010306 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010307 vim_free(sname);
10308 }
10309
10310 /*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010311 * If there is an empty, unnamed buffer we will wipe it out later.
10312 * Remember the buffer number.
10313 */
10314 if (put_line(fd, "if expand('%') == '' && !&modified && line('$') <= 1 && getline(1) == ''") == FAIL)
10315 return FAIL;
10316 if (put_line(fd, " let s:wipebuf = bufnr('%')") == FAIL)
10317 return FAIL;
10318 if (put_line(fd, "endif") == FAIL)
10319 return FAIL;
10320
10321 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010322 * Now save the current files, current buffer first.
10323 */
10324 if (put_line(fd, "set shortmess=aoO") == FAIL)
10325 return FAIL;
10326
10327 /* Now put the other buffers into the buffer list */
10328 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
10329 {
10330 if (!(only_save_windows && buf->b_nwindows == 0)
10331 && !(buf->b_help && !(ssop_flags & SSOP_HELP))
10332 && buf->b_fname != NULL
10333 && buf->b_p_bl)
10334 {
10335 if (fprintf(fd, "badd +%ld ", buf->b_wininfo == NULL ? 1L
10336 : buf->b_wininfo->wi_fpos.lnum) < 0
10337 || ses_fname(fd, buf, &ssop_flags) == FAIL)
10338 return FAIL;
10339 }
10340 }
10341
10342 /* the global argument list */
10343 if (ses_arglist(fd, "args", &global_alist.al_ga,
10344 !(ssop_flags & SSOP_CURDIR), &ssop_flags) == FAIL)
10345 return FAIL;
10346
10347 if (ssop_flags & SSOP_RESIZE)
10348 {
10349 /* Note: after the restore we still check it worked!*/
10350 if (fprintf(fd, "set lines=%ld columns=%ld" , Rows, Columns) < 0
10351 || put_eol(fd) == FAIL)
10352 return FAIL;
10353 }
10354
10355#ifdef FEAT_GUI
10356 if (gui.in_use && (ssop_flags & SSOP_WINPOS))
10357 {
10358 int x, y;
10359
10360 if (gui_mch_get_winpos(&x, &y) == OK)
10361 {
10362 /* Note: after the restore we still check it worked!*/
10363 if (fprintf(fd, "winpos %d %d", x, y) < 0 || put_eol(fd) == FAIL)
10364 return FAIL;
10365 }
10366 }
10367#endif
10368
10369 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000010370 * May repeat putting Windows for each tab, when "tabpages" is in
10371 * 'sessionoptions'.
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010372 * Don't use goto_tabpage(), it may change directory and trigger
10373 * autocommands.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010374 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010375 tab_firstwin = firstwin; /* first window in tab page "tabnr" */
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010376 tab_topframe = topframe;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010377 for (tabnr = 1; ; ++tabnr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010378 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010379 int need_tabnew = FALSE;
10380
Bram Moolenaar18144c82006-04-12 21:52:12 +000010381 if ((ssop_flags & SSOP_TABPAGES))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010382 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010383 tabpage_T *tp = find_tabpage(tabnr);
10384
10385 if (tp == NULL)
10386 break; /* done all tab pages */
10387 if (tp == curtab)
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010388 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010389 tab_firstwin = firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010390 tab_topframe = topframe;
10391 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010392 else
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010393 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010394 tab_firstwin = tp->tp_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010395 tab_topframe = tp->tp_topframe;
10396 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010397 if (tabnr > 1)
10398 need_tabnew = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010399 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010400
Bram Moolenaar18144c82006-04-12 21:52:12 +000010401 /*
10402 * Before creating the window layout, try loading one file. If this
10403 * is aborted we don't end up with a number of useless windows.
10404 * This may have side effects! (e.g., compressed or network file).
10405 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010406 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010407 {
10408 if (ses_do_win(wp)
10409 && wp->w_buffer->b_ffname != NULL
10410 && !wp->w_buffer->b_help
10411#ifdef FEAT_QUICKFIX
10412 && !bt_nofile(wp->w_buffer)
10413#endif
10414 )
10415 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010416 if (fputs(need_tabnew ? "tabedit " : "edit ", fd) < 0
Bram Moolenaar18144c82006-04-12 21:52:12 +000010417 || ses_fname(fd, wp->w_buffer, &ssop_flags) == FAIL)
10418 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010419 need_tabnew = FALSE;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010420 if (!wp->w_arg_idx_invalid)
10421 edited_win = wp;
10422 break;
10423 }
10424 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010425
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010426 /* If no file got edited create an empty tab page. */
10427 if (need_tabnew && put_line(fd, "tabnew") == FAIL)
10428 return FAIL;
10429
Bram Moolenaar18144c82006-04-12 21:52:12 +000010430 /*
10431 * Save current window layout.
10432 */
10433 if (put_line(fd, "set splitbelow splitright") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010434 return FAIL;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010435 if (ses_win_rec(fd, tab_topframe) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010436 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010437 if (!p_sb && put_line(fd, "set nosplitbelow") == FAIL)
10438 return FAIL;
10439 if (!p_spr && put_line(fd, "set nosplitright") == FAIL)
10440 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010441
Bram Moolenaar18144c82006-04-12 21:52:12 +000010442 /*
10443 * Check if window sizes can be restored (no windows omitted).
10444 * Remember the window number of the current window after restoring.
10445 */
10446 nr = 0;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010447 for (wp = tab_firstwin; wp != NULL; wp = W_NEXT(wp))
Bram Moolenaar18144c82006-04-12 21:52:12 +000010448 {
10449 if (ses_do_win(wp))
10450 ++nr;
10451 else
10452 restore_size = FALSE;
10453 if (curwin == wp)
10454 cnr = nr;
10455 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010456
Bram Moolenaar18144c82006-04-12 21:52:12 +000010457 /* Go to the first window. */
10458 if (put_line(fd, "wincmd t") == FAIL)
10459 return FAIL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010460
Bram Moolenaar18144c82006-04-12 21:52:12 +000010461 /*
10462 * If more than one window, see if sizes can be restored.
10463 * First set 'winheight' and 'winwidth' to 1 to avoid the windows being
10464 * resized when moving between windows.
10465 * Do this before restoring the view, so that the topline and the
10466 * cursor can be set. This is done again below.
10467 */
10468 if (put_line(fd, "set winheight=1 winwidth=1") == FAIL)
10469 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010470 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010471 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010472
Bram Moolenaar18144c82006-04-12 21:52:12 +000010473 /*
10474 * Restore the view of the window (options, file, cursor, etc.).
10475 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010476 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010477 {
10478 if (!ses_do_win(wp))
10479 continue;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010480 if (put_view(fd, wp, wp != edited_win, &ssop_flags,
10481 cur_arg_idx) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010482 return FAIL;
10483 if (nr > 1 && put_line(fd, "wincmd w") == FAIL)
10484 return FAIL;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010485 next_arg_idx = wp->w_arg_idx;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010486 }
10487
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010488 /* The argument index in the first tab page is zero, need to set it in
10489 * each window. For further tab pages it's the window where we do
10490 * "tabedit". */
10491 cur_arg_idx = next_arg_idx;
10492
Bram Moolenaar18144c82006-04-12 21:52:12 +000010493 /*
10494 * Restore cursor to the current window if it's not the first one.
10495 */
10496 if (cnr > 1 && (fprintf(fd, "%dwincmd w", cnr) < 0
10497 || put_eol(fd) == FAIL))
10498 return FAIL;
10499
10500 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000010501 * Restore window sizes again after jumping around in windows, because
10502 * the current window has a minimum size while others may not.
10503 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010504 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010505 return FAIL;
10506
Bram Moolenaar18144c82006-04-12 21:52:12 +000010507 /* Don't continue in another tab page when doing only the current one
10508 * or when at the last tab page. */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010509 if (!(ssop_flags & SSOP_TABPAGES))
Bram Moolenaar18144c82006-04-12 21:52:12 +000010510 break;
10511 }
10512
10513 if (ssop_flags & SSOP_TABPAGES)
10514 {
Bram Moolenaar18144c82006-04-12 21:52:12 +000010515 if (fprintf(fd, "tabnext %d", tabpage_index(curtab)) < 0
10516 || put_eol(fd) == FAIL)
10517 return FAIL;
10518 }
10519
Bram Moolenaar9c102382006-05-03 21:26:49 +000010520 /*
10521 * Wipe out an empty unnamed buffer we started in.
10522 */
10523 if (put_line(fd, "if exists('s:wipebuf')") == FAIL)
10524 return FAIL;
Bram Moolenaarf3a67882006-05-05 21:09:41 +000010525 if (put_line(fd, " silent exe 'bwipe ' . s:wipebuf") == FAIL)
Bram Moolenaar9c102382006-05-03 21:26:49 +000010526 return FAIL;
10527 if (put_line(fd, "endif") == FAIL)
10528 return FAIL;
10529 if (put_line(fd, "unlet! s:wipebuf") == FAIL)
10530 return FAIL;
10531
10532 /* Re-apply 'winheight', 'winwidth' and 'shortmess'. */
10533 if (fprintf(fd, "set winheight=%ld winwidth=%ld shortmess=%s",
10534 p_wh, p_wiw, p_shm) < 0 || put_eol(fd) == FAIL)
10535 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010536
10537 /*
10538 * Lastly, execute the x.vim file if it exists.
10539 */
10540 if (put_line(fd, "let s:sx = expand(\"<sfile>:p:r\").\"x.vim\"") == FAIL
10541 || put_line(fd, "if file_readable(s:sx)") == FAIL
Bram Moolenaar42ba1262008-12-09 10:18:03 +000010542 || put_line(fd, " exe \"source \" . fnameescape(s:sx)") == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000010543 || put_line(fd, "endif") == FAIL)
10544 return FAIL;
10545
10546 return OK;
10547}
10548
10549 static int
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010550ses_winsizes(fd, restore_size, tab_firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010551 FILE *fd;
10552 int restore_size;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010553 win_T *tab_firstwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010554{
10555 int n = 0;
10556 win_T *wp;
10557
10558 if (restore_size && (ssop_flags & SSOP_WINSIZE))
10559 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010560 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010561 {
10562 if (!ses_do_win(wp))
10563 continue;
10564 ++n;
10565
10566 /* restore height when not full height */
10567 if (wp->w_height + wp->w_status_height < topframe->fr_height
10568 && (fprintf(fd,
10569 "exe '%dresize ' . ((&lines * %ld + %ld) / %ld)",
10570 n, (long)wp->w_height, Rows / 2, Rows) < 0
10571 || put_eol(fd) == FAIL))
10572 return FAIL;
10573
10574 /* restore width when not full width */
10575 if (wp->w_width < Columns && (fprintf(fd,
10576 "exe 'vert %dresize ' . ((&columns * %ld + %ld) / %ld)",
10577 n, (long)wp->w_width, Columns / 2, Columns) < 0
10578 || put_eol(fd) == FAIL))
10579 return FAIL;
10580 }
10581 }
10582 else
10583 {
10584 /* Just equalise window sizes */
10585 if (put_line(fd, "wincmd =") == FAIL)
10586 return FAIL;
10587 }
10588 return OK;
10589}
10590
10591/*
10592 * Write commands to "fd" to recursively create windows for frame "fr",
10593 * horizontally and vertically split.
10594 * After the commands the last window in the frame is the current window.
10595 * Returns FAIL when writing the commands to "fd" fails.
10596 */
10597 static int
10598ses_win_rec(fd, fr)
10599 FILE *fd;
10600 frame_T *fr;
10601{
10602 frame_T *frc;
10603 int count = 0;
10604
10605 if (fr->fr_layout != FR_LEAF)
10606 {
10607 /* Find first frame that's not skipped and then create a window for
10608 * each following one (first frame is already there). */
10609 frc = ses_skipframe(fr->fr_child);
10610 if (frc != NULL)
10611 while ((frc = ses_skipframe(frc->fr_next)) != NULL)
10612 {
10613 /* Make window as big as possible so that we have lots of room
10614 * to split. */
10615 if (put_line(fd, "wincmd _ | wincmd |") == FAIL
10616 || put_line(fd, fr->fr_layout == FR_COL
10617 ? "split" : "vsplit") == FAIL)
10618 return FAIL;
10619 ++count;
10620 }
10621
10622 /* Go back to the first window. */
10623 if (count > 0 && (fprintf(fd, fr->fr_layout == FR_COL
10624 ? "%dwincmd k" : "%dwincmd h", count) < 0
10625 || put_eol(fd) == FAIL))
10626 return FAIL;
10627
10628 /* Recursively create frames/windows in each window of this column or
10629 * row. */
10630 frc = ses_skipframe(fr->fr_child);
10631 while (frc != NULL)
10632 {
10633 ses_win_rec(fd, frc);
10634 frc = ses_skipframe(frc->fr_next);
10635 /* Go to next window. */
10636 if (frc != NULL && put_line(fd, "wincmd w") == FAIL)
10637 return FAIL;
10638 }
10639 }
10640 return OK;
10641}
10642
10643/*
10644 * Skip frames that don't contain windows we want to save in the Session.
10645 * Returns NULL when there none.
10646 */
10647 static frame_T *
10648ses_skipframe(fr)
10649 frame_T *fr;
10650{
10651 frame_T *frc;
10652
10653 for (frc = fr; frc != NULL; frc = frc->fr_next)
10654 if (ses_do_frame(frc))
10655 break;
10656 return frc;
10657}
10658
10659/*
10660 * Return TRUE if frame "fr" has a window somewhere that we want to save in
10661 * the Session.
10662 */
10663 static int
10664ses_do_frame(fr)
10665 frame_T *fr;
10666{
10667 frame_T *frc;
10668
10669 if (fr->fr_layout == FR_LEAF)
10670 return ses_do_win(fr->fr_win);
10671 for (frc = fr->fr_child; frc != NULL; frc = frc->fr_next)
10672 if (ses_do_frame(frc))
10673 return TRUE;
10674 return FALSE;
10675}
10676
10677/*
10678 * Return non-zero if window "wp" is to be stored in the Session.
10679 */
10680 static int
10681ses_do_win(wp)
10682 win_T *wp;
10683{
10684 if (wp->w_buffer->b_fname == NULL
10685#ifdef FEAT_QUICKFIX
10686 /* When 'buftype' is "nofile" can't restore the window contents. */
10687 || bt_nofile(wp->w_buffer)
10688#endif
10689 )
10690 return (ssop_flags & SSOP_BLANK);
10691 if (wp->w_buffer->b_help)
10692 return (ssop_flags & SSOP_HELP);
10693 return TRUE;
10694}
10695
10696/*
10697 * Write commands to "fd" to restore the view of a window.
10698 * Caller must make sure 'scrolloff' is zero.
10699 */
10700 static int
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010701put_view(fd, wp, add_edit, flagp, current_arg_idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010702 FILE *fd;
10703 win_T *wp;
10704 int add_edit; /* add ":edit" command to view */
10705 unsigned *flagp; /* vop_flags or ssop_flags */
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010706 int current_arg_idx; /* current argument index of the window, use
10707 * -1 if unknown */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010708{
10709 win_T *save_curwin;
10710 int f;
10711 int do_cursor;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010712 int did_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010713
10714 /* Always restore cursor position for ":mksession". For ":mkview" only
10715 * when 'viewoptions' contains "cursor". */
10716 do_cursor = (flagp == &ssop_flags || *flagp & SSOP_CURSOR);
10717
10718 /*
10719 * Local argument list.
10720 */
10721 if (wp->w_alist == &global_alist)
10722 {
10723 if (put_line(fd, "argglobal") == FAIL)
10724 return FAIL;
10725 }
10726 else
10727 {
10728 if (ses_arglist(fd, "arglocal", &wp->w_alist->al_ga,
10729 flagp == &vop_flags
10730 || !(*flagp & SSOP_CURDIR)
10731 || wp->w_localdir != NULL, flagp) == FAIL)
10732 return FAIL;
10733 }
10734
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010735 /* Only when part of a session: restore the argument index. Some
10736 * arguments may have been deleted, check if the index is valid. */
Bram Moolenaar51f53df2010-06-26 05:25:54 +020010737 if (wp->w_arg_idx != current_arg_idx && wp->w_arg_idx < WARGCOUNT(wp)
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010738 && flagp == &ssop_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010739 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010740 if (fprintf(fd, "%ldargu", (long)wp->w_arg_idx + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010741 || put_eol(fd) == FAIL)
10742 return FAIL;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010743 did_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010744 }
10745
10746 /* Edit the file. Skip this when ":next" already did it. */
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010747 if (add_edit && (!did_next || wp->w_arg_idx_invalid))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010748 {
10749 /*
10750 * Load the file.
10751 */
10752 if (wp->w_buffer->b_ffname != NULL
10753#ifdef FEAT_QUICKFIX
10754 && !bt_nofile(wp->w_buffer)
10755#endif
10756 )
10757 {
10758 /*
10759 * Editing a file in this buffer: use ":edit file".
10760 * This may have side effects! (e.g., compressed or network file).
10761 */
10762 if (fputs("edit ", fd) < 0
10763 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
10764 return FAIL;
10765 }
10766 else
10767 {
10768 /* No file in this buffer, just make it empty. */
10769 if (put_line(fd, "enew") == FAIL)
10770 return FAIL;
10771#ifdef FEAT_QUICKFIX
10772 if (wp->w_buffer->b_ffname != NULL)
10773 {
10774 /* The buffer does have a name, but it's not a file name. */
10775 if (fputs("file ", fd) < 0
10776 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
10777 return FAIL;
10778 }
10779#endif
10780 do_cursor = FALSE;
10781 }
10782 }
10783
10784 /*
10785 * Local mappings and abbreviations.
10786 */
10787 if ((*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
10788 && makemap(fd, wp->w_buffer) == FAIL)
10789 return FAIL;
10790
10791 /*
10792 * Local options. Need to go to the window temporarily.
10793 * Store only local values when using ":mkview" and when ":mksession" is
10794 * used and 'sessionoptions' doesn't include "options".
10795 * Some folding options are always stored when "folds" is included,
10796 * otherwise the folds would not be restored correctly.
10797 */
10798 save_curwin = curwin;
10799 curwin = wp;
10800 curbuf = curwin->w_buffer;
10801 if (*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
10802 f = makeset(fd, OPT_LOCAL,
10803 flagp == &vop_flags || !(*flagp & SSOP_OPTIONS));
10804#ifdef FEAT_FOLDING
10805 else if (*flagp & SSOP_FOLDS)
10806 f = makefoldset(fd);
10807#endif
10808 else
10809 f = OK;
10810 curwin = save_curwin;
10811 curbuf = curwin->w_buffer;
10812 if (f == FAIL)
10813 return FAIL;
10814
10815#ifdef FEAT_FOLDING
10816 /*
10817 * Save Folds when 'buftype' is empty and for help files.
10818 */
10819 if ((*flagp & SSOP_FOLDS)
10820 && wp->w_buffer->b_ffname != NULL
Bram Moolenaarb1b715d2006-01-21 22:09:43 +000010821# ifdef FEAT_QUICKFIX
10822 && (*wp->w_buffer->b_p_bt == NUL || wp->w_buffer->b_help)
10823# endif
10824 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000010825 {
10826 if (put_folds(fd, wp) == FAIL)
10827 return FAIL;
10828 }
10829#endif
10830
10831 /*
10832 * Set the cursor after creating folds, since that moves the cursor.
10833 */
10834 if (do_cursor)
10835 {
10836
10837 /* Restore the cursor line in the file and relatively in the
10838 * window. Don't use "G", it changes the jumplist. */
10839 if (fprintf(fd, "let s:l = %ld - ((%ld * winheight(0) + %ld) / %ld)",
10840 (long)wp->w_cursor.lnum,
10841 (long)(wp->w_cursor.lnum - wp->w_topline),
10842 (long)wp->w_height / 2, (long)wp->w_height) < 0
10843 || put_eol(fd) == FAIL
10844 || put_line(fd, "if s:l < 1 | let s:l = 1 | endif") == FAIL
10845 || put_line(fd, "exe s:l") == FAIL
10846 || put_line(fd, "normal! zt") == FAIL
10847 || fprintf(fd, "%ld", (long)wp->w_cursor.lnum) < 0
10848 || put_eol(fd) == FAIL)
10849 return FAIL;
10850 /* Restore the cursor column and left offset when not wrapping. */
10851 if (wp->w_cursor.col == 0)
10852 {
10853 if (put_line(fd, "normal! 0") == FAIL)
10854 return FAIL;
10855 }
10856 else
10857 {
10858 if (!wp->w_p_wrap && wp->w_leftcol > 0 && wp->w_width > 0)
10859 {
10860 if (fprintf(fd,
10861 "let s:c = %ld - ((%ld * winwidth(0) + %ld) / %ld)",
Bram Moolenaar558ddad2013-02-20 19:26:29 +010010862 (long)wp->w_virtcol + 1,
10863 (long)(wp->w_virtcol - wp->w_leftcol),
Bram Moolenaar071d4272004-06-13 20:20:40 +000010864 (long)wp->w_width / 2, (long)wp->w_width) < 0
10865 || put_eol(fd) == FAIL
10866 || put_line(fd, "if s:c > 0") == FAIL
10867 || fprintf(fd,
Bram Moolenaar558ddad2013-02-20 19:26:29 +010010868 " exe 'normal! ' . s:c . '|zs' . %ld . '|'",
10869 (long)wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010870 || put_eol(fd) == FAIL
10871 || put_line(fd, "else") == FAIL
Bram Moolenaarfdf447b2013-02-26 17:21:29 +010010872 || fprintf(fd, " normal! 0%d|", wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010873 || put_eol(fd) == FAIL
10874 || put_line(fd, "endif") == FAIL)
10875 return FAIL;
10876 }
10877 else
10878 {
Bram Moolenaar558ddad2013-02-20 19:26:29 +010010879 if (fprintf(fd, "normal! 0%d|", wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010880 || put_eol(fd) == FAIL)
10881 return FAIL;
10882 }
10883 }
10884 }
10885
10886 /*
10887 * Local directory.
10888 */
10889 if (wp->w_localdir != NULL)
10890 {
10891 if (fputs("lcd ", fd) < 0
10892 || ses_put_fname(fd, wp->w_localdir, flagp) == FAIL
10893 || put_eol(fd) == FAIL)
10894 return FAIL;
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010895 did_lcd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010896 }
10897
10898 return OK;
10899}
10900
10901/*
10902 * Write an argument list to the session file.
10903 * Returns FAIL if writing fails.
10904 */
10905 static int
10906ses_arglist(fd, cmd, gap, fullname, flagp)
10907 FILE *fd;
10908 char *cmd;
10909 garray_T *gap;
10910 int fullname; /* TRUE: use full path name */
10911 unsigned *flagp;
10912{
10913 int i;
Bram Moolenaard9462e32011-04-11 21:35:11 +020010914 char_u *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010915 char_u *s;
10916
10917 if (gap->ga_len == 0)
10918 return put_line(fd, "silent! argdel *");
10919 if (fputs(cmd, fd) < 0)
10920 return FAIL;
10921 for (i = 0; i < gap->ga_len; ++i)
10922 {
10923 /* NULL file names are skipped (only happens when out of memory). */
10924 s = alist_name(&((aentry_T *)gap->ga_data)[i]);
10925 if (s != NULL)
10926 {
10927 if (fullname)
10928 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020010929 buf = alloc(MAXPATHL);
10930 if (buf != NULL)
10931 {
10932 (void)vim_FullName(s, buf, MAXPATHL, FALSE);
10933 s = buf;
10934 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010935 }
10936 if (fputs(" ", fd) < 0 || ses_put_fname(fd, s, flagp) == FAIL)
Bram Moolenaard9462e32011-04-11 21:35:11 +020010937 {
10938 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010939 return FAIL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020010940 }
10941 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010942 }
10943 }
10944 return put_eol(fd);
10945}
10946
10947/*
10948 * Write a buffer name to the session file.
10949 * Also ends the line.
10950 * Returns FAIL if writing fails.
10951 */
10952 static int
10953ses_fname(fd, buf, flagp)
10954 FILE *fd;
10955 buf_T *buf;
10956 unsigned *flagp;
10957{
10958 char_u *name;
10959
10960 /* Use the short file name if the current directory is known at the time
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010961 * the session file will be sourced.
10962 * Don't do this for ":mkview", we don't know the current directory.
10963 * Don't do this after ":lcd", we don't keep track of what the current
10964 * directory is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010965 if (buf->b_sfname != NULL
10966 && flagp == &ssop_flags
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010967 && (ssop_flags & (SSOP_CURDIR | SSOP_SESDIR))
Bram Moolenaar8d594672009-07-01 18:18:57 +000010968#ifdef FEAT_AUTOCHDIR
10969 && !p_acd
10970#endif
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010971 && !did_lcd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010972 name = buf->b_sfname;
10973 else
10974 name = buf->b_ffname;
10975 if (ses_put_fname(fd, name, flagp) == FAIL || put_eol(fd) == FAIL)
10976 return FAIL;
10977 return OK;
10978}
10979
10980/*
10981 * Write a file name to the session file.
10982 * Takes care of the "slash" option in 'sessionoptions' and escapes special
10983 * characters.
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010984 * Returns FAIL if writing fails or out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010985 */
10986 static int
10987ses_put_fname(fd, name, flagp)
10988 FILE *fd;
10989 char_u *name;
10990 unsigned *flagp;
10991{
10992 char_u *sname;
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010993 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010994 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010995
10996 sname = home_replace_save(NULL, name);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010997 if (sname == NULL)
10998 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010999
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011000 if (*flagp & SSOP_SLASH)
11001 {
11002 /* change all backslashes to forward slashes */
11003 for (p = sname; *p != NUL; mb_ptr_adv(p))
11004 if (*p == '\\')
11005 *p = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +000011006 }
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011007
Bram Moolenaar84a05ac2013-05-06 04:24:17 +020011008 /* escape special characters */
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011009 p = vim_strsave_fnameescape(sname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011010 vim_free(sname);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011011 if (p == NULL)
11012 return FAIL;
11013
11014 /* write the result */
11015 if (fputs((char *)p, fd) < 0)
11016 retval = FAIL;
11017
11018 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011019 return retval;
11020}
11021
11022/*
11023 * ":loadview [nr]"
11024 */
11025 static void
11026ex_loadview(eap)
11027 exarg_T *eap;
11028{
11029 char_u *fname;
11030
11031 fname = get_view_file(*eap->arg);
11032 if (fname != NULL)
11033 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +000011034 do_source(fname, FALSE, DOSO_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011035 vim_free(fname);
11036 }
11037}
11038
11039/*
11040 * Get the name of the view file for the current buffer.
11041 */
11042 static char_u *
11043get_view_file(c)
11044 int c;
11045{
11046 int len = 0;
11047 char_u *p, *s;
11048 char_u *retval;
11049 char_u *sname;
11050
11051 if (curbuf->b_ffname == NULL)
11052 {
11053 EMSG(_(e_noname));
11054 return NULL;
11055 }
11056 sname = home_replace_save(NULL, curbuf->b_ffname);
11057 if (sname == NULL)
11058 return NULL;
11059
11060 /*
11061 * We want a file name without separators, because we're not going to make
11062 * a directory.
11063 * "normal" path separator -> "=+"
11064 * "=" -> "=="
11065 * ":" path separator -> "=-"
11066 */
11067 for (p = sname; *p; ++p)
11068 if (*p == '=' || vim_ispathsep(*p))
11069 ++len;
11070 retval = alloc((unsigned)(STRLEN(sname) + len + STRLEN(p_vdir) + 9));
11071 if (retval != NULL)
11072 {
11073 STRCPY(retval, p_vdir);
11074 add_pathsep(retval);
11075 s = retval + STRLEN(retval);
11076 for (p = sname; *p; ++p)
11077 {
11078 if (*p == '=')
11079 {
11080 *s++ = '=';
11081 *s++ = '=';
11082 }
11083 else if (vim_ispathsep(*p))
11084 {
11085 *s++ = '=';
Bram Moolenaare60acc12011-05-10 16:41:25 +020011086#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011087 if (*p == ':')
11088 *s++ = '-';
11089 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000011090#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000011091 *s++ = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +000011092 }
11093 else
11094 *s++ = *p;
11095 }
11096 *s++ = '=';
11097 *s++ = c;
11098 STRCPY(s, ".vim");
11099 }
11100
11101 vim_free(sname);
11102 return retval;
11103}
11104
11105#endif /* FEAT_SESSION */
11106
11107/*
11108 * Write end-of-line character(s) for ":mkexrc", ":mkvimrc" and ":mksession".
11109 * Return FAIL for a write error.
11110 */
11111 int
11112put_eol(fd)
11113 FILE *fd;
11114{
11115 if (
11116#ifdef USE_CRNL
11117 (
11118# ifdef MKSESSION_NL
11119 !mksession_nl &&
11120# endif
11121 (putc('\r', fd) < 0)) ||
11122#endif
11123 (putc('\n', fd) < 0))
11124 return FAIL;
11125 return OK;
11126}
11127
11128/*
11129 * Write a line to "fd".
11130 * Return FAIL for a write error.
11131 */
11132 int
11133put_line(fd, s)
11134 FILE *fd;
11135 char *s;
11136{
11137 if (fputs(s, fd) < 0 || put_eol(fd) == FAIL)
11138 return FAIL;
11139 return OK;
11140}
11141
11142#ifdef FEAT_VIMINFO
11143/*
11144 * ":rviminfo" and ":wviminfo".
11145 */
11146 static void
11147ex_viminfo(eap)
11148 exarg_T *eap;
11149{
11150 char_u *save_viminfo;
11151
11152 save_viminfo = p_viminfo;
11153 if (*p_viminfo == NUL)
11154 p_viminfo = (char_u *)"'100";
11155 if (eap->cmdidx == CMD_rviminfo)
11156 {
Bram Moolenaard812df62008-11-09 12:46:09 +000011157 if (read_viminfo(eap->arg, VIF_WANT_INFO | VIF_WANT_MARKS
11158 | (eap->forceit ? VIF_FORCEIT : 0)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011159 EMSG(_("E195: Cannot open viminfo file for reading"));
11160 }
11161 else
11162 write_viminfo(eap->arg, eap->forceit);
11163 p_viminfo = save_viminfo;
11164}
11165#endif
11166
11167#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +000011168/*
Bram Moolenaard9462e32011-04-11 21:35:11 +020011169 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +000011170 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +000011171 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011172 void
11173dialog_msg(buff, format, fname)
11174 char_u *buff;
11175 char *format;
11176 char_u *fname;
11177{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011178 if (fname == NULL)
11179 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +020011180 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011181}
11182#endif
11183
11184/*
11185 * ":behave {mswin,xterm}"
11186 */
11187 static void
11188ex_behave(eap)
11189 exarg_T *eap;
11190{
11191 if (STRCMP(eap->arg, "mswin") == 0)
11192 {
11193 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
11194 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
11195 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
11196 set_option_value((char_u *)"keymodel", 0L,
11197 (char_u *)"startsel,stopsel", 0);
11198 }
11199 else if (STRCMP(eap->arg, "xterm") == 0)
11200 {
11201 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
11202 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
11203 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
11204 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
11205 }
11206 else
11207 EMSG2(_(e_invarg2), eap->arg);
11208}
11209
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010011210#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
11211/*
11212 * Function given to ExpandGeneric() to obtain the possible arguments of the
11213 * ":behave {mswin,xterm}" command.
11214 */
11215 char_u *
11216get_behave_arg(xp, idx)
11217 expand_T *xp UNUSED;
11218 int idx;
11219{
11220 if (idx == 0)
11221 return (char_u *)"mswin";
11222 if (idx == 1)
11223 return (char_u *)"xterm";
11224 return NULL;
11225}
11226#endif
11227
Bram Moolenaar071d4272004-06-13 20:20:40 +000011228#ifdef FEAT_AUTOCMD
11229static int filetype_detect = FALSE;
11230static int filetype_plugin = FALSE;
11231static int filetype_indent = FALSE;
11232
11233/*
11234 * ":filetype [plugin] [indent] {on,off,detect}"
11235 * on: Load the filetype.vim file to install autocommands for file types.
11236 * off: Load the ftoff.vim file to remove all autocommands for file types.
11237 * plugin on: load filetype.vim and ftplugin.vim
11238 * plugin off: load ftplugof.vim
11239 * indent on: load filetype.vim and indent.vim
11240 * indent off: load indoff.vim
11241 */
11242 static void
11243ex_filetype(eap)
11244 exarg_T *eap;
11245{
11246 char_u *arg = eap->arg;
11247 int plugin = FALSE;
11248 int indent = FALSE;
11249
11250 if (*eap->arg == NUL)
11251 {
11252 /* Print current status. */
11253 smsg((char_u *)"filetype detection:%s plugin:%s indent:%s",
11254 filetype_detect ? "ON" : "OFF",
11255 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
11256 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
11257 return;
11258 }
11259
11260 /* Accept "plugin" and "indent" in any order. */
11261 for (;;)
11262 {
11263 if (STRNCMP(arg, "plugin", 6) == 0)
11264 {
11265 plugin = TRUE;
11266 arg = skipwhite(arg + 6);
11267 continue;
11268 }
11269 if (STRNCMP(arg, "indent", 6) == 0)
11270 {
11271 indent = TRUE;
11272 arg = skipwhite(arg + 6);
11273 continue;
11274 }
11275 break;
11276 }
11277 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
11278 {
11279 if (*arg == 'o' || !filetype_detect)
11280 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011281 source_runtime((char_u *)FILETYPE_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011282 filetype_detect = TRUE;
11283 if (plugin)
11284 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011285 source_runtime((char_u *)FTPLUGIN_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011286 filetype_plugin = TRUE;
11287 }
11288 if (indent)
11289 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011290 source_runtime((char_u *)INDENT_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011291 filetype_indent = TRUE;
11292 }
11293 }
11294 if (*arg == 'd')
11295 {
11296 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE);
Bram Moolenaara3227e22006-03-08 21:32:40 +000011297 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011298 }
11299 }
11300 else if (STRCMP(arg, "off") == 0)
11301 {
11302 if (plugin || indent)
11303 {
11304 if (plugin)
11305 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011306 source_runtime((char_u *)FTPLUGOF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011307 filetype_plugin = FALSE;
11308 }
11309 if (indent)
11310 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011311 source_runtime((char_u *)INDOFF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011312 filetype_indent = FALSE;
11313 }
11314 }
11315 else
11316 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011317 source_runtime((char_u *)FTOFF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011318 filetype_detect = FALSE;
11319 }
11320 }
11321 else
11322 EMSG2(_(e_invarg2), arg);
11323}
11324
11325/*
11326 * ":setfiletype {name}"
11327 */
11328 static void
11329ex_setfiletype(eap)
11330 exarg_T *eap;
11331{
11332 if (!did_filetype)
11333 set_option_value((char_u *)"filetype", 0L, eap->arg, OPT_LOCAL);
11334}
11335#endif
11336
Bram Moolenaar071d4272004-06-13 20:20:40 +000011337 static void
11338ex_digraphs(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +000011339 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011340{
11341#ifdef FEAT_DIGRAPHS
11342 if (*eap->arg != NUL)
11343 putdigraph(eap->arg);
11344 else
11345 listdigraphs();
11346#else
11347 EMSG(_("E196: No digraphs in this version"));
11348#endif
11349}
11350
11351 static void
11352ex_set(eap)
11353 exarg_T *eap;
11354{
11355 int flags = 0;
11356
11357 if (eap->cmdidx == CMD_setlocal)
11358 flags = OPT_LOCAL;
11359 else if (eap->cmdidx == CMD_setglobal)
11360 flags = OPT_GLOBAL;
11361#if defined(FEAT_EVAL) && defined(FEAT_AUTOCMD) && defined(FEAT_BROWSE)
11362 if (cmdmod.browse && flags == 0)
11363 ex_options(eap);
11364 else
11365#endif
11366 (void)do_set(eap->arg, flags);
11367}
11368
11369#ifdef FEAT_SEARCH_EXTRA
11370/*
11371 * ":nohlsearch"
11372 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011373 static void
11374ex_nohlsearch(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +000011375 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011376{
11377 no_hlsearch = TRUE;
Bram Moolenaarf71a3db2006-03-12 21:50:18 +000011378 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011379}
11380
11381/*
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011382 * ":[N]match {group} {pattern}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000011383 * Sets nextcmd to the start of the next command, if any. Also called when
11384 * skipping commands to find the next command.
11385 */
11386 static void
11387ex_match(eap)
11388 exarg_T *eap;
11389{
11390 char_u *p;
Bram Moolenaar52d36c82007-08-11 14:00:30 +000011391 char_u *g = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011392 char_u *end;
11393 int c;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011394 int id;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011395
11396 if (eap->line2 <= 3)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011397 id = eap->line2;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011398 else
11399 {
11400 EMSG(e_invcmd);
11401 return;
11402 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011403
11404 /* First clear any old pattern. */
11405 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011406 match_delete(curwin, id, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011407
11408 if (ends_excmd(*eap->arg))
11409 end = eap->arg;
11410 else if ((STRNICMP(eap->arg, "none", 4) == 0
11411 && (vim_iswhite(eap->arg[4]) || ends_excmd(eap->arg[4]))))
11412 end = eap->arg + 4;
11413 else
11414 {
11415 p = skiptowhite(eap->arg);
11416 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011417 g = vim_strnsave(eap->arg, (int)(p - eap->arg));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011418 p = skipwhite(p);
11419 if (*p == NUL)
11420 {
11421 /* There must be two arguments. */
11422 EMSG2(_(e_invarg2), eap->arg);
11423 return;
11424 }
11425 end = skip_regexp(p + 1, *p, TRUE, NULL);
11426 if (!eap->skip)
11427 {
11428 if (*end != NUL && !ends_excmd(*skipwhite(end + 1)))
11429 {
11430 eap->errmsg = e_trailing;
11431 return;
11432 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000011433 if (*end != *p)
11434 {
11435 EMSG2(_(e_invarg2), p);
11436 return;
11437 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011438
11439 c = *end;
11440 *end = NUL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011441 match_add(curwin, g, p + 1, 10, id);
11442 vim_free(g);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000011443 *end = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011444 }
11445 }
11446 eap->nextcmd = find_nextcmd(end);
11447}
11448#endif
11449
11450#ifdef FEAT_CRYPT
11451/*
11452 * ":X": Get crypt key
11453 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011454 static void
11455ex_X(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +000011456 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011457{
Bram Moolenaar49771f42010-07-20 17:32:38 +020011458 if (get_crypt_method(curbuf) == 0 || blowfish_self_test() == OK)
Bram Moolenaar40e6a712010-05-16 22:32:54 +020011459 (void)get_crypt_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011460}
11461#endif
11462
11463#ifdef FEAT_FOLDING
11464 static void
11465ex_fold(eap)
11466 exarg_T *eap;
11467{
11468 if (foldManualAllowed(TRUE))
11469 foldCreate(eap->line1, eap->line2);
11470}
11471
11472 static void
11473ex_foldopen(eap)
11474 exarg_T *eap;
11475{
11476 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
11477 eap->forceit, FALSE);
11478}
11479
11480 static void
11481ex_folddo(eap)
11482 exarg_T *eap;
11483{
11484 linenr_T lnum;
11485
11486 /* First set the marks for all lines closed/open. */
11487 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
11488 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
11489 ml_setmarked(lnum);
11490
11491 /* Execute the command on the marked lines. */
11492 global_exe(eap->arg);
11493 ml_clearmarked(); /* clear rest of the marks */
11494}
11495#endif