blob: 95c699d9891f4b1b7b32c36f4141f6303bcac5fa [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * ex_docmd.c: functions for executing an Ex command line.
12 */
13
14#include "vim.h"
15
Bram Moolenaar071d4272004-06-13 20:20:40 +000016static int quitmore = 0;
17static int ex_pressedreturn = FALSE;
18#ifndef FEAT_PRINTER
19# define ex_hardcopy ex_ni
20#endif
21
22#ifdef FEAT_USR_CMDS
23typedef struct ucmd
24{
25 char_u *uc_name; /* The command name */
26 long_u uc_argt; /* The argument type */
27 char_u *uc_rep; /* The command's replacement string */
28 long uc_def; /* The default value for a range/count */
Bram Moolenaar071d4272004-06-13 20:20:40 +000029 int uc_compl; /* completion type */
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010030# ifdef FEAT_EVAL
31 scid_T uc_scriptID; /* SID where the command was defined */
32# ifdef FEAT_CMDL_COMPL
Bram Moolenaar071d4272004-06-13 20:20:40 +000033 char_u *uc_compl_arg; /* completion argument if any */
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010034# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000035# endif
36} ucmd_T;
37
38#define UC_BUFFER 1 /* -buffer: local to current buffer */
39
Bram Moolenaar2c29bee2005-06-01 21:46:07 +000040static garray_T ucmds = {0, 0, sizeof(ucmd_T), 4, NULL};
Bram Moolenaar071d4272004-06-13 20:20:40 +000041
42#define USER_CMD(i) (&((ucmd_T *)(ucmds.ga_data))[i])
43#define USER_CMD_GA(gap, i) (&((ucmd_T *)((gap)->ga_data))[i])
44
45static void do_ucmd __ARGS((exarg_T *eap));
46static void ex_command __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +000047static void ex_delcommand __ARGS((exarg_T *eap));
48# ifdef FEAT_CMDL_COMPL
49static char_u *get_user_command_name __ARGS((int idx));
50# endif
51
52#else
53# define ex_command ex_ni
54# define ex_comclear ex_ni
55# define ex_delcommand ex_ni
56#endif
57
58#ifdef FEAT_EVAL
Bram Moolenaar89d40322006-08-29 15:30:07 +000059static char_u *do_one_cmd __ARGS((char_u **, int, struct condstack *, char_u *(*fgetline)(int, void *, int), void *cookie));
Bram Moolenaar071d4272004-06-13 20:20:40 +000060#else
Bram Moolenaar89d40322006-08-29 15:30:07 +000061static char_u *do_one_cmd __ARGS((char_u **, int, char_u *(*fgetline)(int, void *, int), void *cookie));
Bram Moolenaar071d4272004-06-13 20:20:40 +000062static int if_level = 0; /* depth in :if */
63#endif
Bram Moolenaara6f4d612011-09-21 19:10:46 +020064static void append_command __ARGS((char_u *cmd));
Bram Moolenaar071d4272004-06-13 20:20:40 +000065static char_u *find_command __ARGS((exarg_T *eap, int *full));
66
67static void ex_abbreviate __ARGS((exarg_T *eap));
68static void ex_map __ARGS((exarg_T *eap));
69static void ex_unmap __ARGS((exarg_T *eap));
70static void ex_mapclear __ARGS((exarg_T *eap));
71static void ex_abclear __ARGS((exarg_T *eap));
72#ifndef FEAT_MENU
73# define ex_emenu ex_ni
74# define ex_menu ex_ni
75# define ex_menutranslate ex_ni
76#endif
77#ifdef FEAT_AUTOCMD
78static void ex_autocmd __ARGS((exarg_T *eap));
79static void ex_doautocmd __ARGS((exarg_T *eap));
80#else
81# define ex_autocmd ex_ni
82# define ex_doautocmd ex_ni
83# define ex_doautoall ex_ni
84#endif
85#ifdef FEAT_LISTCMDS
86static void ex_bunload __ARGS((exarg_T *eap));
87static void ex_buffer __ARGS((exarg_T *eap));
88static void ex_bmodified __ARGS((exarg_T *eap));
89static void ex_bnext __ARGS((exarg_T *eap));
90static void ex_bprevious __ARGS((exarg_T *eap));
91static void ex_brewind __ARGS((exarg_T *eap));
92static void ex_blast __ARGS((exarg_T *eap));
93#else
94# define ex_bunload ex_ni
95# define ex_buffer ex_ni
96# define ex_bmodified ex_ni
97# define ex_bnext ex_ni
98# define ex_bprevious ex_ni
99# define ex_brewind ex_ni
100# define ex_blast ex_ni
101# define buflist_list ex_ni
102# define ex_checktime ex_ni
103#endif
104#if !defined(FEAT_LISTCMDS) || !defined(FEAT_WINDOWS)
105# define ex_buffer_all ex_ni
106#endif
107static char_u *getargcmd __ARGS((char_u **));
108static char_u *skip_cmd_arg __ARGS((char_u *p, int rembs));
109static int getargopt __ARGS((exarg_T *eap));
110#ifndef FEAT_QUICKFIX
111# define ex_make ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +0000112# define ex_cbuffer ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000113# define ex_cc ex_ni
114# define ex_cnext ex_ni
115# define ex_cfile ex_ni
116# define qf_list ex_ni
117# define qf_age ex_ni
118# define ex_helpgrep ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +0000119# define ex_vimgrep ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000120#endif
121#if !defined(FEAT_QUICKFIX) || !defined(FEAT_WINDOWS)
122# define ex_cclose ex_ni
123# define ex_copen ex_ni
124# define ex_cwindow ex_ni
125#endif
Bram Moolenaar1e015462005-09-25 22:16:38 +0000126#if !defined(FEAT_QUICKFIX) || !defined(FEAT_EVAL)
127# define ex_cexpr ex_ni
128#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000129
130static int check_more __ARGS((int, int));
131static linenr_T get_address __ARGS((char_u **, int skip, int to_other_file));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000132static void get_flags __ARGS((exarg_T *eap));
Bram Moolenaar85363ab2010-07-18 13:58:26 +0200133#if !defined(FEAT_PERL) \
134 || !defined(FEAT_PYTHON) || !defined(FEAT_PYTHON3) \
135 || !defined(FEAT_TCL) \
136 || !defined(FEAT_RUBY) \
137 || !defined(FEAT_LUA) \
138 || !defined(FEAT_MZSCHEME)
Bram Moolenaar7bb75552007-07-16 18:39:49 +0000139# define HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +0000140static void ex_script_ni __ARGS((exarg_T *eap));
141#endif
142static char_u *invalid_range __ARGS((exarg_T *eap));
143static void correct_range __ARGS((exarg_T *eap));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000144#ifdef FEAT_QUICKFIX
145static char_u *replace_makeprg __ARGS((exarg_T *eap, char_u *p, char_u **cmdlinep));
146#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000147static char_u *repl_cmdline __ARGS((exarg_T *eap, char_u *src, int srclen, char_u *repl, char_u **cmdlinep));
148static void ex_highlight __ARGS((exarg_T *eap));
149static void ex_colorscheme __ARGS((exarg_T *eap));
150static void ex_quit __ARGS((exarg_T *eap));
151static void ex_cquit __ARGS((exarg_T *eap));
152static void ex_quit_all __ARGS((exarg_T *eap));
153#ifdef FEAT_WINDOWS
154static void ex_close __ARGS((exarg_T *eap));
Bram Moolenaarf740b292006-02-16 22:11:02 +0000155static void ex_win_close __ARGS((int forceit, win_T *win, tabpage_T *tp));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000156static void ex_only __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000157static void ex_resize __ARGS((exarg_T *eap));
158static void ex_stag __ARGS((exarg_T *eap));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000159static void ex_tabclose __ARGS((exarg_T *eap));
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000160static void ex_tabonly __ARGS((exarg_T *eap));
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000161static void ex_tabnext __ARGS((exarg_T *eap));
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000162static void ex_tabmove __ARGS((exarg_T *eap));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000163static void ex_tabs __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000164#else
165# define ex_close ex_ni
166# define ex_only ex_ni
167# define ex_all ex_ni
168# define ex_resize ex_ni
169# define ex_splitview ex_ni
170# define ex_stag ex_ni
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000171# define ex_tabnext ex_ni
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000172# define ex_tabmove ex_ni
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000173# define ex_tabs ex_ni
174# define ex_tabclose ex_ni
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000175# define ex_tabonly ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000176#endif
177#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
178static void ex_pclose __ARGS((exarg_T *eap));
179static void ex_ptag __ARGS((exarg_T *eap));
180static void ex_pedit __ARGS((exarg_T *eap));
181#else
182# define ex_pclose ex_ni
183# define ex_ptag ex_ni
184# define ex_pedit ex_ni
185#endif
186static void ex_hide __ARGS((exarg_T *eap));
187static void ex_stop __ARGS((exarg_T *eap));
188static void ex_exit __ARGS((exarg_T *eap));
189static void ex_print __ARGS((exarg_T *eap));
190#ifdef FEAT_BYTEOFF
191static void ex_goto __ARGS((exarg_T *eap));
192#else
193# define ex_goto ex_ni
194#endif
195static void ex_shell __ARGS((exarg_T *eap));
196static void ex_preserve __ARGS((exarg_T *eap));
197static void ex_recover __ARGS((exarg_T *eap));
198#ifndef FEAT_LISTCMDS
199# define ex_argedit ex_ni
200# define ex_argadd ex_ni
201# define ex_argdelete ex_ni
202# define ex_listdo ex_ni
203#endif
204static void ex_mode __ARGS((exarg_T *eap));
205static void ex_wrongmodifier __ARGS((exarg_T *eap));
206static void ex_find __ARGS((exarg_T *eap));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000207static void ex_open __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000208static void ex_edit __ARGS((exarg_T *eap));
209#if !defined(FEAT_GUI) && !defined(FEAT_CLIENTSERVER)
210# define ex_drop ex_ni
211#endif
212#ifndef FEAT_GUI
213# define ex_gui ex_nogui
214static void ex_nogui __ARGS((exarg_T *eap));
215#endif
216#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
217static void ex_tearoff __ARGS((exarg_T *eap));
218#else
219# define ex_tearoff ex_ni
220#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000221#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000222static void ex_popup __ARGS((exarg_T *eap));
223#else
224# define ex_popup ex_ni
225#endif
226#ifndef FEAT_GUI_MSWIN
227# define ex_simalt ex_ni
228#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000229#if !defined(FEAT_GUI_MSWIN) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000230# define gui_mch_find_dialog ex_ni
231# define gui_mch_replace_dialog ex_ni
232#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000233#if !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000234# define ex_helpfind ex_ni
235#endif
236#ifndef FEAT_CSCOPE
237# define do_cscope ex_ni
238# define do_scscope ex_ni
239# define do_cstag ex_ni
240#endif
241#ifndef FEAT_SYN_HL
242# define ex_syntax ex_ni
Bram Moolenaar860cae12010-06-05 23:22:07 +0200243# define ex_ownsyntax ex_ni
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000244#endif
Bram Moolenaarf7512552013-06-06 14:55:19 +0200245#if !defined(FEAT_SYN_HL) || !defined(FEAT_PROFILE)
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +0200246# define ex_syntime ex_ni
247#endif
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000248#ifndef FEAT_SPELL
Bram Moolenaarb765d632005-06-07 21:00:02 +0000249# define ex_spell ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000250# define ex_mkspell ex_ni
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000251# define ex_spelldump ex_ni
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000252# define ex_spellinfo ex_ni
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000253# define ex_spellrepall ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000254#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200255#ifndef FEAT_PERSISTENT_UNDO
256# define ex_rundo ex_ni
257# define ex_wundo ex_ni
258#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200259#ifndef FEAT_LUA
260# define ex_lua ex_script_ni
261# define ex_luado ex_ni
262# define ex_luafile ex_ni
263#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000264#ifndef FEAT_MZSCHEME
265# define ex_mzscheme ex_script_ni
266# define ex_mzfile ex_ni
267#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000268#ifndef FEAT_PERL
269# define ex_perl ex_script_ni
270# define ex_perldo ex_ni
271#endif
272#ifndef FEAT_PYTHON
273# define ex_python ex_script_ni
Bram Moolenaard620aa92013-05-17 16:40:06 +0200274# define ex_pydo ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000275# define ex_pyfile ex_ni
276#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200277#ifndef FEAT_PYTHON3
Bram Moolenaar368373e2010-07-19 20:46:22 +0200278# define ex_py3 ex_script_ni
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200279# define ex_py3do ex_ni
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200280# define ex_py3file ex_ni
281#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000282#ifndef FEAT_TCL
283# define ex_tcl ex_script_ni
284# define ex_tcldo ex_ni
285# define ex_tclfile ex_ni
286#endif
287#ifndef FEAT_RUBY
288# define ex_ruby ex_script_ni
289# define ex_rubydo ex_ni
290# define ex_rubyfile ex_ni
291#endif
292#ifndef FEAT_SNIFF
293# define ex_sniff ex_ni
294#endif
295#ifndef FEAT_KEYMAP
296# define ex_loadkeymap ex_ni
297#endif
298static void ex_swapname __ARGS((exarg_T *eap));
299static void ex_syncbind __ARGS((exarg_T *eap));
300static void ex_read __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000301static void ex_pwd __ARGS((exarg_T *eap));
302static void ex_equal __ARGS((exarg_T *eap));
303static void ex_sleep __ARGS((exarg_T *eap));
304static void do_exmap __ARGS((exarg_T *eap, int isabbrev));
305static void ex_winsize __ARGS((exarg_T *eap));
306#ifdef FEAT_WINDOWS
307static void ex_wincmd __ARGS((exarg_T *eap));
308#else
309# define ex_wincmd ex_ni
310#endif
Bram Moolenaar843ee412004-06-30 16:16:41 +0000311#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000312static void ex_winpos __ARGS((exarg_T *eap));
313#else
314# define ex_winpos ex_ni
315#endif
316static void ex_operators __ARGS((exarg_T *eap));
317static void ex_put __ARGS((exarg_T *eap));
318static void ex_copymove __ARGS((exarg_T *eap));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000319static void ex_may_print __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000320static void ex_submagic __ARGS((exarg_T *eap));
321static void ex_join __ARGS((exarg_T *eap));
322static void ex_at __ARGS((exarg_T *eap));
323static void ex_bang __ARGS((exarg_T *eap));
324static void ex_undo __ARGS((exarg_T *eap));
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200325#ifdef FEAT_PERSISTENT_UNDO
326static void ex_wundo __ARGS((exarg_T *eap));
327static void ex_rundo __ARGS((exarg_T *eap));
328#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000329static void ex_redo __ARGS((exarg_T *eap));
Bram Moolenaarc7d89352006-03-14 22:53:34 +0000330static void ex_later __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000331static void ex_redir __ARGS((exarg_T *eap));
332static void ex_redraw __ARGS((exarg_T *eap));
333static void ex_redrawstatus __ARGS((exarg_T *eap));
334static void close_redir __ARGS((void));
335static void ex_mkrc __ARGS((exarg_T *eap));
336static void ex_mark __ARGS((exarg_T *eap));
337#ifdef FEAT_USR_CMDS
338static char_u *uc_fun_cmd __ARGS((void));
Bram Moolenaar52b4b552005-03-07 23:00:57 +0000339static char_u *find_ucmd __ARGS((exarg_T *eap, char_u *p, int *full, expand_T *xp, int *compl));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000340#endif
341#ifdef FEAT_EX_EXTRA
342static void ex_normal __ARGS((exarg_T *eap));
343static void ex_startinsert __ARGS((exarg_T *eap));
344static void ex_stopinsert __ARGS((exarg_T *eap));
345#else
346# define ex_normal ex_ni
347# define ex_align ex_ni
348# define ex_retab ex_ni
349# define ex_startinsert ex_ni
350# define ex_stopinsert ex_ni
351# define ex_helptags ex_ni
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +0000352# define ex_sort ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000353#endif
354#ifdef FEAT_FIND_ID
355static void ex_checkpath __ARGS((exarg_T *eap));
356static void ex_findpat __ARGS((exarg_T *eap));
357#else
358# define ex_findpat ex_ni
359# define ex_checkpath ex_ni
360#endif
361#if defined(FEAT_FIND_ID) && defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
362static void ex_psearch __ARGS((exarg_T *eap));
363#else
364# define ex_psearch ex_ni
365#endif
366static void ex_tag __ARGS((exarg_T *eap));
367static void ex_tag_cmd __ARGS((exarg_T *eap, char_u *name));
368#ifndef FEAT_EVAL
369# define ex_scriptnames ex_ni
370# define ex_finish ex_ni
371# define ex_echo ex_ni
372# define ex_echohl ex_ni
373# define ex_execute ex_ni
374# define ex_call ex_ni
375# define ex_if ex_ni
376# define ex_endif ex_ni
377# define ex_else ex_ni
378# define ex_while ex_ni
379# define ex_continue ex_ni
380# define ex_break ex_ni
381# define ex_endwhile ex_ni
382# define ex_throw ex_ni
383# define ex_try ex_ni
384# define ex_catch ex_ni
385# define ex_finally ex_ni
386# define ex_endtry ex_ni
387# define ex_endfunction ex_ni
388# define ex_let ex_ni
389# define ex_unlet ex_ni
Bram Moolenaar65c1b012005-01-31 19:02:28 +0000390# define ex_lockvar ex_ni
391# define ex_unlockvar ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000392# define ex_function ex_ni
393# define ex_delfunction ex_ni
394# define ex_return ex_ni
Bram Moolenaard812df62008-11-09 12:46:09 +0000395# define ex_oldfiles ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000396#endif
397static char_u *arg_all __ARGS((void));
398#ifdef FEAT_SESSION
399static int makeopens __ARGS((FILE *fd, char_u *dirnow));
Bram Moolenaarf13be0d2008-01-02 14:13:10 +0000400static int put_view __ARGS((FILE *fd, win_T *wp, int add_edit, unsigned *flagp, int current_arg_idx));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000401static void ex_loadview __ARGS((exarg_T *eap));
402static char_u *get_view_file __ARGS((int c));
Bram Moolenaareeefcc72007-05-01 21:21:21 +0000403static int did_lcd; /* whether ":lcd" was produced for a session */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000404#else
405# define ex_loadview ex_ni
406#endif
407#ifndef FEAT_EVAL
408# define ex_compiler ex_ni
409#endif
410#ifdef FEAT_VIMINFO
411static void ex_viminfo __ARGS((exarg_T *eap));
412#else
413# define ex_viminfo ex_ni
414#endif
415static void ex_behave __ARGS((exarg_T *eap));
416#ifdef FEAT_AUTOCMD
417static void ex_filetype __ARGS((exarg_T *eap));
418static void ex_setfiletype __ARGS((exarg_T *eap));
419#else
420# define ex_filetype ex_ni
421# define ex_setfiletype ex_ni
422#endif
423#ifndef FEAT_DIFF
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000424# define ex_diffoff ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000425# define ex_diffpatch ex_ni
426# define ex_diffgetput ex_ni
427# define ex_diffsplit ex_ni
428# define ex_diffthis ex_ni
429# define ex_diffupdate ex_ni
430#endif
431static void ex_digraphs __ARGS((exarg_T *eap));
432static void ex_set __ARGS((exarg_T *eap));
433#if !defined(FEAT_EVAL) || !defined(FEAT_AUTOCMD)
434# define ex_options ex_ni
435#endif
436#ifdef FEAT_SEARCH_EXTRA
437static void ex_nohlsearch __ARGS((exarg_T *eap));
438static void ex_match __ARGS((exarg_T *eap));
439#else
440# define ex_nohlsearch ex_ni
441# define ex_match ex_ni
442#endif
443#ifdef FEAT_CRYPT
444static void ex_X __ARGS((exarg_T *eap));
445#else
446# define ex_X ex_ni
447#endif
448#ifdef FEAT_FOLDING
449static void ex_fold __ARGS((exarg_T *eap));
450static void ex_foldopen __ARGS((exarg_T *eap));
451static void ex_folddo __ARGS((exarg_T *eap));
452#else
453# define ex_fold ex_ni
454# define ex_foldopen ex_ni
455# define ex_folddo ex_ni
456#endif
457#if !((defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
458 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE)))
459# define ex_language ex_ni
460#endif
461#ifndef FEAT_SIGNS
462# define ex_sign ex_ni
463#endif
464#ifndef FEAT_SUN_WORKSHOP
465# define ex_wsverb ex_ni
466#endif
Bram Moolenaar009b2592004-10-24 19:18:58 +0000467#ifndef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200468# define ex_nbclose ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000469# define ex_nbkey ex_ni
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200470# define ex_nbstart ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000471#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000472
473#ifndef FEAT_EVAL
474# define ex_debug ex_ni
475# define ex_breakadd ex_ni
476# define ex_debuggreedy ex_ni
477# define ex_breakdel ex_ni
478# define ex_breaklist ex_ni
479#endif
480
481#ifndef FEAT_CMDHIST
482# define ex_history ex_ni
483#endif
484#ifndef FEAT_JUMPLIST
485# define ex_jumps ex_ni
486# define ex_changes ex_ni
487#endif
488
Bram Moolenaar05159a02005-02-26 23:04:13 +0000489#ifndef FEAT_PROFILE
490# define ex_profile ex_ni
491#endif
492
Bram Moolenaar071d4272004-06-13 20:20:40 +0000493/*
494 * Declare cmdnames[].
495 */
496#define DO_DECLARE_EXCMD
497#include "ex_cmds.h"
498
499/*
500 * Table used to quickly search for a command, based on its first character.
501 */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +0000502static cmdidx_T cmdidxs[27] =
Bram Moolenaar071d4272004-06-13 20:20:40 +0000503{
504 CMD_append,
505 CMD_buffer,
506 CMD_change,
507 CMD_delete,
508 CMD_edit,
509 CMD_file,
510 CMD_global,
511 CMD_help,
512 CMD_insert,
513 CMD_join,
514 CMD_k,
515 CMD_list,
516 CMD_move,
517 CMD_next,
518 CMD_open,
519 CMD_print,
520 CMD_quit,
521 CMD_read,
522 CMD_substitute,
523 CMD_t,
524 CMD_undo,
525 CMD_vglobal,
526 CMD_write,
527 CMD_xit,
528 CMD_yank,
529 CMD_z,
530 CMD_bang
531};
532
533static char_u dollar_command[2] = {'$', 0};
534
535
536#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000537/* Struct for storing a line inside a while/for loop */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000538typedef struct
539{
540 char_u *line; /* command line */
541 linenr_T lnum; /* sourcing_lnum of the line */
542} wcmd_T;
543
544/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000545 * Structure used to store info for line position in a while or for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000546 * This is required, because do_one_cmd() may invoke ex_function(), which
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000547 * reads more lines that may come from the while/for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000548 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000549struct loop_cookie
Bram Moolenaar071d4272004-06-13 20:20:40 +0000550{
551 garray_T *lines_gap; /* growarray with line info */
552 int current_line; /* last read line from growarray */
553 int repeating; /* TRUE when looping a second time */
554 /* When "repeating" is FALSE use "getline" and "cookie" to get lines */
555 char_u *(*getline) __ARGS((int, void *, int));
556 void *cookie;
557};
558
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000559static char_u *get_loop_line __ARGS((int c, void *cookie, int indent));
560static int store_loop_line __ARGS((garray_T *gap, char_u *line));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000561static void free_cmdlines __ARGS((garray_T *gap));
Bram Moolenaared203462004-06-16 11:19:22 +0000562
563/* Struct to save a few things while debugging. Used in do_cmdline() only. */
564struct dbg_stuff
565{
566 int trylevel;
567 int force_abort;
568 except_T *caught_stack;
569 char_u *vv_exception;
570 char_u *vv_throwpoint;
571 int did_emsg;
572 int got_int;
573 int did_throw;
574 int need_rethrow;
575 int check_cstack;
576 except_T *current_exception;
577};
578
579static void save_dbg_stuff __ARGS((struct dbg_stuff *dsp));
580static void restore_dbg_stuff __ARGS((struct dbg_stuff *dsp));
581
582 static void
583save_dbg_stuff(dsp)
584 struct dbg_stuff *dsp;
585{
586 dsp->trylevel = trylevel; trylevel = 0;
587 dsp->force_abort = force_abort; force_abort = FALSE;
588 dsp->caught_stack = caught_stack; caught_stack = NULL;
589 dsp->vv_exception = v_exception(NULL);
590 dsp->vv_throwpoint = v_throwpoint(NULL);
591
592 /* Necessary for debugging an inactive ":catch", ":finally", ":endtry" */
593 dsp->did_emsg = did_emsg; did_emsg = FALSE;
594 dsp->got_int = got_int; got_int = FALSE;
595 dsp->did_throw = did_throw; did_throw = FALSE;
596 dsp->need_rethrow = need_rethrow; need_rethrow = FALSE;
597 dsp->check_cstack = check_cstack; check_cstack = FALSE;
598 dsp->current_exception = current_exception; current_exception = NULL;
599}
600
601 static void
602restore_dbg_stuff(dsp)
603 struct dbg_stuff *dsp;
604{
605 suppress_errthrow = FALSE;
606 trylevel = dsp->trylevel;
607 force_abort = dsp->force_abort;
608 caught_stack = dsp->caught_stack;
609 (void)v_exception(dsp->vv_exception);
610 (void)v_throwpoint(dsp->vv_throwpoint);
611 did_emsg = dsp->did_emsg;
612 got_int = dsp->got_int;
613 did_throw = dsp->did_throw;
614 need_rethrow = dsp->need_rethrow;
615 check_cstack = dsp->check_cstack;
616 current_exception = dsp->current_exception;
617}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000618#endif
619
620
621/*
622 * do_exmode(): Repeatedly get commands for the "Ex" mode, until the ":vi"
623 * command is given.
624 */
625 void
626do_exmode(improved)
627 int improved; /* TRUE for "improved Ex" mode */
628{
629 int save_msg_scroll;
630 int prev_msg_row;
631 linenr_T prev_line;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000632 int changedtick;
633
634 if (improved)
635 exmode_active = EXMODE_VIM;
636 else
637 exmode_active = EXMODE_NORMAL;
638 State = NORMAL;
639
640 /* When using ":global /pat/ visual" and then "Q" we return to continue
641 * the :global command. */
642 if (global_busy)
643 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000644
645 save_msg_scroll = msg_scroll;
646 ++RedrawingDisabled; /* don't redisplay the window */
647 ++no_wait_return; /* don't wait for return */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000648#ifdef FEAT_GUI
649 /* Ignore scrollbar and mouse events in Ex mode */
650 ++hold_gui_events;
651#endif
652#ifdef FEAT_SNIFF
653 want_sniff_request = 0; /* No K_SNIFF wanted */
654#endif
655
656 MSG(_("Entering Ex mode. Type \"visual\" to go to Normal mode."));
657 while (exmode_active)
658 {
Bram Moolenaar7c626922005-02-07 22:01:03 +0000659#ifdef FEAT_EX_EXTRA
660 /* Check for a ":normal" command and no more characters left. */
661 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
662 {
663 exmode_active = FALSE;
664 break;
665 }
666#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667 msg_scroll = TRUE;
668 need_wait_return = FALSE;
669 ex_pressedreturn = FALSE;
670 ex_no_reprint = FALSE;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000671 changedtick = curbuf->b_changedtick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000672 prev_msg_row = msg_row;
673 prev_line = curwin->w_cursor.lnum;
674#ifdef FEAT_SNIFF
675 ProcessSniffRequests();
676#endif
677 if (improved)
678 {
679 cmdline_row = msg_row;
680 do_cmdline(NULL, getexline, NULL, 0);
681 }
682 else
683 do_cmdline(NULL, getexmodeline, NULL, DOCMD_NOWAIT);
684 lines_left = Rows - 1;
685
Bram Moolenaardf177f62005-02-22 08:39:57 +0000686 if ((prev_line != curwin->w_cursor.lnum
687 || changedtick != curbuf->b_changedtick) && !ex_no_reprint)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000689 if (curbuf->b_ml.ml_flags & ML_EMPTY)
690 EMSG(_(e_emptybuf));
691 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000693 if (ex_pressedreturn)
694 {
695 /* go up one line, to overwrite the ":<CR>" line, so the
Bram Moolenaar81870892007-11-11 18:17:28 +0000696 * output doesn't contain empty lines. */
Bram Moolenaardf177f62005-02-22 08:39:57 +0000697 msg_row = prev_msg_row;
698 if (prev_msg_row == Rows - 1)
699 msg_row--;
700 }
701 msg_col = 0;
702 print_line_no_prefix(curwin->w_cursor.lnum, FALSE, FALSE);
703 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000705 }
Bram Moolenaardf177f62005-02-22 08:39:57 +0000706 else if (ex_pressedreturn && !ex_no_reprint) /* must be at EOF */
707 {
708 if (curbuf->b_ml.ml_flags & ML_EMPTY)
709 EMSG(_(e_emptybuf));
710 else
711 EMSG(_("E501: At end-of-file"));
712 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000713 }
714
715#ifdef FEAT_GUI
716 --hold_gui_events;
717#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000718 --RedrawingDisabled;
719 --no_wait_return;
720 update_screen(CLEAR);
721 need_wait_return = FALSE;
722 msg_scroll = save_msg_scroll;
723}
724
725/*
726 * Execute a simple command line. Used for translated commands like "*".
727 */
728 int
729do_cmdline_cmd(cmd)
730 char_u *cmd;
731{
732 return do_cmdline(cmd, NULL, NULL,
733 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
734}
735
736/*
737 * do_cmdline(): execute one Ex command line
738 *
739 * 1. Execute "cmdline" when it is not NULL.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100740 * If "cmdline" is NULL, or more lines are needed, fgetline() is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000741 * 2. Split up in parts separated with '|'.
742 *
743 * This function can be called recursively!
744 *
745 * flags:
746 * DOCMD_VERBOSE - The command will be included in the error message.
747 * DOCMD_NOWAIT - Don't call wait_return() and friends.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100748 * DOCMD_REPEAT - Repeat execution until fgetline() returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000749 * DOCMD_KEYTYPED - Don't reset KeyTyped.
750 * DOCMD_EXCRESET - Reset the exception environment (used for debugging).
751 * DOCMD_KEEPLINE - Store first typed line (for repeating with ".").
752 *
753 * return FAIL if cmdline could not be executed, OK otherwise
754 */
755 int
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100756do_cmdline(cmdline, fgetline, cookie, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000757 char_u *cmdline;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100758 char_u *(*fgetline) __ARGS((int, void *, int));
759 void *cookie; /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000760 int flags;
761{
762 char_u *next_cmdline; /* next cmd to execute */
763 char_u *cmdline_copy = NULL; /* copy of cmd line */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100764 int used_getline = FALSE; /* used "fgetline" to obtain command */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765 static int recursive = 0; /* recursive depth */
766 int msg_didout_before_start = 0;
767 int count = 0; /* line number count */
768 int did_inc = FALSE; /* incremented RedrawingDisabled */
769 int retval = OK;
770#ifdef FEAT_EVAL
771 struct condstack cstack; /* conditional stack */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000772 garray_T lines_ga; /* keep lines for ":while"/":for" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773 int current_line = 0; /* active line in lines_ga */
774 char_u *fname = NULL; /* function or script name */
775 linenr_T *breakpoint = NULL; /* ptr to breakpoint field in cookie */
776 int *dbg_tick = NULL; /* ptr to dbg_tick field in cookie */
Bram Moolenaared203462004-06-16 11:19:22 +0000777 struct dbg_stuff debug_saved; /* saved things for debug mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778 int initial_trylevel;
779 struct msglist **saved_msg_list = NULL;
780 struct msglist *private_msg_list;
781
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100782 /* "fgetline" and "cookie" passed to do_one_cmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783 char_u *(*cmd_getline) __ARGS((int, void *, int));
784 void *cmd_cookie;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000785 struct loop_cookie cmd_loop_cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786 void *real_cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000787 int getline_is_func;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788#else
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100789# define cmd_getline fgetline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790# define cmd_cookie cookie
791#endif
792 static int call_depth = 0; /* recursiveness */
793
794#ifdef FEAT_EVAL
795 /* For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory
796 * location for storing error messages to be converted to an exception.
Bram Moolenaarcf3630f2005-01-08 16:04:29 +0000797 * This ensures that the do_errthrow() call in do_one_cmd() does not
798 * combine the messages stored by an earlier invocation of do_one_cmd()
799 * with the command name of the later one. This would happen when
800 * BufWritePost autocommands are executed after a write error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801 saved_msg_list = msg_list;
802 msg_list = &private_msg_list;
803 private_msg_list = NULL;
804#endif
805
806 /* It's possible to create an endless loop with ":execute", catch that
807 * here. The value of 200 allows nested function calls, ":source", etc. */
808 if (call_depth == 200)
809 {
810 EMSG(_("E169: Command too recursive"));
811#ifdef FEAT_EVAL
812 /* When converting to an exception, we do not include the command name
813 * since this is not an error of the specific command. */
814 do_errthrow((struct condstack *)NULL, (char_u *)NULL);
815 msg_list = saved_msg_list;
816#endif
817 return FAIL;
818 }
819 ++call_depth;
820
821#ifdef FEAT_EVAL
822 cstack.cs_idx = -1;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000823 cstack.cs_looplevel = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824 cstack.cs_trylevel = 0;
825 cstack.cs_emsg_silent_list = NULL;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000826 cstack.cs_lflags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827 ga_init2(&lines_ga, (int)sizeof(wcmd_T), 10);
828
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100829 real_cookie = getline_cookie(fgetline, cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830
831 /* Inside a function use a higher nesting level. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100832 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000833 if (getline_is_func && ex_nesting_level == func_level(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834 ++ex_nesting_level;
835
836 /* Get the function or script name and the address where the next breakpoint
837 * line and the debug tick for a function or script are stored. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000838 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839 {
840 fname = func_name(real_cookie);
841 breakpoint = func_breakpoint(real_cookie);
842 dbg_tick = func_dbg_tick(real_cookie);
843 }
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100844 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000845 {
846 fname = sourcing_name;
847 breakpoint = source_breakpoint(real_cookie);
848 dbg_tick = source_dbg_tick(real_cookie);
849 }
850
851 /*
852 * Initialize "force_abort" and "suppress_errthrow" at the top level.
853 */
854 if (!recursive)
855 {
856 force_abort = FALSE;
857 suppress_errthrow = FALSE;
858 }
859
860 /*
861 * If requested, store and reset the global values controlling the
Bram Moolenaar89d40322006-08-29 15:30:07 +0000862 * exception handling (used when debugging). Otherwise clear it to avoid
863 * a bogus compiler warning when the optimizer uses inline functions...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000864 */
Bram Moolenaarb4872942006-05-13 10:32:52 +0000865 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +0000866 save_dbg_stuff(&debug_saved);
Bram Moolenaar89d40322006-08-29 15:30:07 +0000867 else
Bram Moolenaar7db5fc82010-05-24 11:59:29 +0200868 vim_memset(&debug_saved, 0, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000869
870 initial_trylevel = trylevel;
871
872 /*
873 * "did_throw" will be set to TRUE when an exception is being thrown.
874 */
875 did_throw = FALSE;
876#endif
877 /*
878 * "did_emsg" will be set to TRUE when emsg() is used, in which case we
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000879 * cancel the whole command line, and any if/endif or loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000880 * If force_abort is set, we cancel everything.
881 */
882 did_emsg = FALSE;
883
884 /*
885 * KeyTyped is only set when calling vgetc(). Reset it here when not
886 * calling vgetc() (sourced command lines).
887 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100888 if (!(flags & DOCMD_KEYTYPED)
889 && !getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000890 KeyTyped = FALSE;
891
892 /*
893 * Continue executing command lines:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000894 * - when inside an ":if", ":while" or ":for"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000895 * - for multiple commands on one line, separated with '|'
896 * - when repeating until there are no more lines (for ":source")
897 */
898 next_cmdline = cmdline;
899 do
900 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000901#ifdef FEAT_EVAL
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100902 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000903#endif
904
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000905 /* stop skipping cmds for an error msg after all endif/while/for */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906 if (next_cmdline == NULL
907#ifdef FEAT_EVAL
908 && !force_abort
909 && cstack.cs_idx < 0
Bram Moolenaar05159a02005-02-26 23:04:13 +0000910 && !(getline_is_func && func_has_abort(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000911#endif
912 )
913 did_emsg = FALSE;
914
915 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000916 * 1. If repeating a line in a loop, get a line from lines_ga.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100917 * 2. If no line given: Get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000918 * 3. If a line is given: Make a copy, so we can mess with it.
919 */
920
921#ifdef FEAT_EVAL
922 /* 1. If repeating, get a previous line from lines_ga. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000923 if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000924 {
925 /* Each '|' separated command is stored separately in lines_ga, to
926 * be able to jump to it. Don't use next_cmdline now. */
927 vim_free(cmdline_copy);
928 cmdline_copy = NULL;
929
930 /* Check if a function has returned or, unless it has an unclosed
931 * try conditional, aborted. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000932 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000933 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000934# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000935 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000936 func_line_end(real_cookie);
937# endif
938 if (func_has_ended(real_cookie))
939 {
940 retval = FAIL;
941 break;
942 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000943 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000944#ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000945 else if (do_profiling == PROF_YES
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100946 && getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000947 script_line_end();
948#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949
950 /* Check if a sourced file hit a ":finish" command. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100951 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000952 {
953 retval = FAIL;
954 break;
955 }
956
957 /* If breakpoints have been added/deleted need to check for it. */
958 if (breakpoint != NULL && dbg_tick != NULL
959 && *dbg_tick != debug_tick)
960 {
961 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100962 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000963 fname, sourcing_lnum);
964 *dbg_tick = debug_tick;
965 }
966
967 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
968 sourcing_lnum = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
969
970 /* Did we encounter a breakpoint? */
971 if (breakpoint != NULL && *breakpoint != 0
972 && *breakpoint <= sourcing_lnum)
973 {
974 dbg_breakpoint(fname, sourcing_lnum);
975 /* Find next breakpoint. */
976 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100977 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978 fname, sourcing_lnum);
979 *dbg_tick = debug_tick;
980 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000981# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000982 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000983 {
984 if (getline_is_func)
985 func_line_start(real_cookie);
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100986 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000987 script_line_start();
988 }
989# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990 }
991
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000992 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000994 /* Inside a while/for loop we need to store the lines and use them
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100995 * again. Pass a different "fgetline" function to do_one_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996 * below, so that it stores lines in or reads them from
997 * "lines_ga". Makes it possible to define a function inside a
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000998 * while/for loop. */
999 cmd_getline = get_loop_line;
1000 cmd_cookie = (void *)&cmd_loop_cookie;
1001 cmd_loop_cookie.lines_gap = &lines_ga;
1002 cmd_loop_cookie.current_line = current_line;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001003 cmd_loop_cookie.getline = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001004 cmd_loop_cookie.cookie = cookie;
1005 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006 }
1007 else
1008 {
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001009 cmd_getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010 cmd_cookie = cookie;
1011 }
1012#endif
1013
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001014 /* 2. If no line given, get an allocated line with fgetline(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015 if (next_cmdline == NULL)
1016 {
1017 /*
1018 * Need to set msg_didout for the first line after an ":if",
1019 * otherwise the ":if" will be overwritten.
1020 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001021 if (count == 1 && getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022 msg_didout = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001023 if (fgetline == NULL || (next_cmdline = fgetline(':', cookie,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001024#ifdef FEAT_EVAL
1025 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
1026#else
1027 0
1028#endif
1029 )) == NULL)
1030 {
1031 /* Don't call wait_return for aborted command line. The NULL
1032 * returned for the end of a sourced file or executed function
1033 * doesn't do this. */
1034 if (KeyTyped && !(flags & DOCMD_REPEAT))
1035 need_wait_return = FALSE;
1036 retval = FAIL;
1037 break;
1038 }
1039 used_getline = TRUE;
1040
1041 /*
1042 * Keep the first typed line. Clear it when more lines are typed.
1043 */
1044 if (flags & DOCMD_KEEPLINE)
1045 {
1046 vim_free(repeat_cmdline);
1047 if (count == 0)
1048 repeat_cmdline = vim_strsave(next_cmdline);
1049 else
1050 repeat_cmdline = NULL;
1051 }
1052 }
1053
1054 /* 3. Make a copy of the command so we can mess with it. */
1055 else if (cmdline_copy == NULL)
1056 {
1057 next_cmdline = vim_strsave(next_cmdline);
1058 if (next_cmdline == NULL)
1059 {
1060 EMSG(_(e_outofmem));
1061 retval = FAIL;
1062 break;
1063 }
1064 }
1065 cmdline_copy = next_cmdline;
1066
1067#ifdef FEAT_EVAL
1068 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001069 * Save the current line when inside a ":while" or ":for", and when
1070 * the command looks like a ":while" or ":for", because we may need it
1071 * later. When there is a '|' and another command, it is stored
1072 * separately, because we need to be able to jump back to it from an
1073 * :endwhile/:endfor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001075 if (current_line == lines_ga.ga_len
1076 && (cstack.cs_looplevel || has_loop_cmd(next_cmdline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001078 if (store_loop_line(&lines_ga, next_cmdline) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079 {
1080 retval = FAIL;
1081 break;
1082 }
1083 }
1084 did_endif = FALSE;
1085#endif
1086
1087 if (count++ == 0)
1088 {
1089 /*
1090 * All output from the commands is put below each other, without
1091 * waiting for a return. Don't do this when executing commands
1092 * from a script or when being called recursive (e.g. for ":e
1093 * +command file").
1094 */
1095 if (!(flags & DOCMD_NOWAIT) && !recursive)
1096 {
1097 msg_didout_before_start = msg_didout;
1098 msg_didany = FALSE; /* no output yet */
1099 msg_start();
1100 msg_scroll = TRUE; /* put messages below each other */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001101 ++no_wait_return; /* don't wait for return until finished */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102 ++RedrawingDisabled;
1103 did_inc = TRUE;
1104 }
1105 }
1106
1107 if (p_verbose >= 15 && sourcing_name != NULL)
1108 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109 ++no_wait_return;
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001110 verbose_enter_scroll();
1111
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112 smsg((char_u *)_("line %ld: %s"),
1113 (long)sourcing_lnum, cmdline_copy);
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001114 if (msg_silent == 0)
1115 msg_puts((char_u *)"\n"); /* don't overwrite this */
1116
1117 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118 --no_wait_return;
1119 }
1120
1121 /*
1122 * 2. Execute one '|' separated command.
1123 * do_one_cmd() will return NULL if there is no trailing '|'.
1124 * "cmdline_copy" can change, e.g. for '%' and '#' expansion.
1125 */
1126 ++recursive;
1127 next_cmdline = do_one_cmd(&cmdline_copy, flags & DOCMD_VERBOSE,
1128#ifdef FEAT_EVAL
1129 &cstack,
1130#endif
1131 cmd_getline, cmd_cookie);
1132 --recursive;
1133
1134#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001135 if (cmd_cookie == (void *)&cmd_loop_cookie)
1136 /* Use "current_line" from "cmd_loop_cookie", it may have been
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137 * incremented when defining a function. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001138 current_line = cmd_loop_cookie.current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139#endif
1140
1141 if (next_cmdline == NULL)
1142 {
1143 vim_free(cmdline_copy);
1144 cmdline_copy = NULL;
1145#ifdef FEAT_CMDHIST
1146 /*
1147 * If the command was typed, remember it for the ':' register.
1148 * Do this AFTER executing the command to make :@: work.
1149 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001150 if (getline_equal(fgetline, cookie, getexline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001151 && new_last_cmdline != NULL)
1152 {
1153 vim_free(last_cmdline);
1154 last_cmdline = new_last_cmdline;
1155 new_last_cmdline = NULL;
1156 }
1157#endif
1158 }
1159 else
1160 {
1161 /* need to copy the command after the '|' to cmdline_copy, for the
1162 * next do_one_cmd() */
Bram Moolenaara7241f52008-06-24 20:39:31 +00001163 STRMOVE(cmdline_copy, next_cmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001164 next_cmdline = cmdline_copy;
1165 }
1166
1167
1168#ifdef FEAT_EVAL
1169 /* reset did_emsg for a function that is not aborted by an error */
1170 if (did_emsg && !force_abort
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001171 && getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172 && !func_has_abort(real_cookie))
1173 did_emsg = FALSE;
1174
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001175 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001176 {
1177 ++current_line;
1178
1179 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001180 * An ":endwhile", ":endfor" and ":continue" is handled here.
1181 * If we were executing commands, jump back to the ":while" or
1182 * ":for".
1183 * If we were not executing commands, decrement cs_looplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001185 if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001187 cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001189 /* Jump back to the matching ":while" or ":for". Be careful
1190 * not to use a cs_line[] from an entry that isn't a ":while"
1191 * or ":for": It would make "current_line" invalid and can
1192 * cause a crash. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193 if (!did_emsg && !got_int && !did_throw
1194 && cstack.cs_idx >= 0
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001195 && (cstack.cs_flags[cstack.cs_idx]
1196 & (CSF_WHILE | CSF_FOR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197 && cstack.cs_line[cstack.cs_idx] >= 0
1198 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
1199 {
1200 current_line = cstack.cs_line[cstack.cs_idx];
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001201 /* remember we jumped there */
1202 cstack.cs_lflags |= CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203 line_breakcheck(); /* check if CTRL-C typed */
1204
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001205 /* Check for the next breakpoint at or after the ":while"
1206 * or ":for". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207 if (breakpoint != NULL)
1208 {
1209 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001210 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211 fname,
1212 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
1213 *dbg_tick = debug_tick;
1214 }
1215 }
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001216 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001217 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001218 /* can only get here with ":endwhile" or ":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219 if (cstack.cs_idx >= 0)
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001220 rewind_conditionals(&cstack, cstack.cs_idx - 1,
1221 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222 }
1223 }
1224
1225 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001226 * For a ":while" or ":for" we need to remember the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001227 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001228 else if (cstack.cs_lflags & CSL_HAD_LOOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001229 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001230 cstack.cs_lflags &= ~CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001231 cstack.cs_line[cstack.cs_idx] = current_line - 1;
1232 }
1233 }
1234
1235 /*
1236 * When not inside any ":while" loop, clear remembered lines.
1237 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001238 if (cstack.cs_looplevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239 {
1240 if (lines_ga.ga_len > 0)
1241 {
1242 sourcing_lnum =
1243 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
1244 free_cmdlines(&lines_ga);
1245 }
1246 current_line = 0;
1247 }
1248
1249 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001250 * A ":finally" makes did_emsg, got_int, and did_throw pending for
1251 * being restored at the ":endtry". Reset them here and set the
1252 * ACTIVE and FINALLY flags, so that the finally clause gets executed.
1253 * This includes the case where a missing ":endif", ":endwhile" or
1254 * ":endfor" was detected by the ":finally" itself.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001256 if (cstack.cs_lflags & CSL_HAD_FINA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001257 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001258 cstack.cs_lflags &= ~CSL_HAD_FINA;
1259 report_make_pending(cstack.cs_pending[cstack.cs_idx]
1260 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261 did_throw ? (void *)current_exception : NULL);
1262 did_emsg = got_int = did_throw = FALSE;
1263 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
1264 }
1265
1266 /* Update global "trylevel" for recursive calls to do_cmdline() from
1267 * within this loop. */
1268 trylevel = initial_trylevel + cstack.cs_trylevel;
1269
1270 /*
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001271 * If the outermost try conditional (across function calls and sourced
Bram Moolenaar071d4272004-06-13 20:20:40 +00001272 * files) is aborted because of an error, an interrupt, or an uncaught
1273 * exception, cancel everything. If it is left normally, reset
1274 * force_abort to get the non-EH compatible abortion behavior for
1275 * the rest of the script.
1276 */
1277 if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
1278 force_abort = FALSE;
1279
1280 /* Convert an interrupt to an exception if appropriate. */
1281 (void)do_intthrow(&cstack);
1282#endif /* FEAT_EVAL */
1283
1284 }
1285 /*
1286 * Continue executing command lines when:
1287 * - no CTRL-C typed, no aborting error, no exception thrown or try
1288 * conditionals need to be checked for executing finally clauses or
1289 * catching an interrupt exception
1290 * - didn't get an error message or lines are not typed
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001291 * - there is a command after '|', inside a :if, :while, :for or :try, or
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292 * looping for ":source" command or function call.
1293 */
1294 while (!((got_int
1295#ifdef FEAT_EVAL
1296 || (did_emsg && force_abort) || did_throw
1297#endif
1298 )
1299#ifdef FEAT_EVAL
1300 && cstack.cs_trylevel == 0
1301#endif
1302 )
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001303 && !(did_emsg
1304#ifdef FEAT_EVAL
1305 /* Keep going when inside try/catch, so that the error can be
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001306 * deal with, except when it is a syntax error, it may cause
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001307 * the :endtry to be missed. */
1308 && (cstack.cs_trylevel == 0 || did_emsg_syntax)
1309#endif
1310 && used_getline
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001311 && (getline_equal(fgetline, cookie, getexmodeline)
1312 || getline_equal(fgetline, cookie, getexline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313 && (next_cmdline != NULL
1314#ifdef FEAT_EVAL
1315 || cstack.cs_idx >= 0
1316#endif
1317 || (flags & DOCMD_REPEAT)));
1318
1319 vim_free(cmdline_copy);
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001320 did_emsg_syntax = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321#ifdef FEAT_EVAL
1322 free_cmdlines(&lines_ga);
1323 ga_clear(&lines_ga);
1324
1325 if (cstack.cs_idx >= 0)
1326 {
1327 /*
1328 * If a sourced file or executed function ran to its end, report the
1329 * unclosed conditional.
1330 */
1331 if (!got_int && !did_throw
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001332 && ((getline_equal(fgetline, cookie, getsourceline)
1333 && !source_finished(fgetline, cookie))
1334 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335 && !func_has_ended(real_cookie))))
1336 {
1337 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
1338 EMSG(_(e_endtry));
1339 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
1340 EMSG(_(e_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001341 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
1342 EMSG(_(e_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343 else
1344 EMSG(_(e_endif));
1345 }
1346
1347 /*
1348 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1349 * ":endtry" in a sourced file or executed function. If the try
1350 * conditional is in its finally clause, ignore anything pending.
1351 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001352 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353 */
1354 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001355 {
1356 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1357
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001358 if (idx >= 0)
1359 --idx; /* remove try block not in its finally clause */
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001360 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1361 &cstack.cs_looplevel);
1362 }
1363 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364 trylevel = initial_trylevel;
1365 }
1366
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001367 /* If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1368 * lack was reported above and the error message is to be converted to an
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369 * exception, do this now after rewinding the cstack. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001370 do_errthrow(&cstack, getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371 ? (char_u *)"endfunction" : (char_u *)NULL);
1372
1373 if (trylevel == 0)
1374 {
1375 /*
1376 * When an exception is being thrown out of the outermost try
1377 * conditional, discard the uncaught exception, disable the conversion
1378 * of interrupts or errors to exceptions, and ensure that no more
1379 * commands are executed.
1380 */
1381 if (did_throw)
1382 {
1383 void *p = NULL;
1384 char_u *saved_sourcing_name;
1385 int saved_sourcing_lnum;
1386 struct msglist *messages = NULL, *next;
1387
1388 /*
1389 * If the uncaught exception is a user exception, report it as an
1390 * error. If it is an error exception, display the saved error
1391 * message now. For an interrupt exception, do nothing; the
1392 * interrupt message is given elsewhere.
1393 */
1394 switch (current_exception->type)
1395 {
1396 case ET_USER:
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001397 vim_snprintf((char *)IObuff, IOSIZE,
1398 _("E605: Exception not caught: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001399 current_exception->value);
1400 p = vim_strsave(IObuff);
1401 break;
1402 case ET_ERROR:
1403 messages = current_exception->messages;
1404 current_exception->messages = NULL;
1405 break;
1406 case ET_INTERRUPT:
1407 break;
1408 default:
1409 p = vim_strsave((char_u *)_(e_internal));
1410 }
1411
1412 saved_sourcing_name = sourcing_name;
1413 saved_sourcing_lnum = sourcing_lnum;
1414 sourcing_name = current_exception->throw_name;
1415 sourcing_lnum = current_exception->throw_lnum;
1416 current_exception->throw_name = NULL;
1417
1418 discard_current_exception(); /* uses IObuff if 'verbose' */
1419 suppress_errthrow = TRUE;
1420 force_abort = TRUE;
1421
1422 if (messages != NULL)
1423 {
1424 do
1425 {
1426 next = messages->next;
1427 emsg(messages->msg);
1428 vim_free(messages->msg);
1429 vim_free(messages);
1430 messages = next;
1431 }
1432 while (messages != NULL);
1433 }
1434 else if (p != NULL)
1435 {
1436 emsg(p);
1437 vim_free(p);
1438 }
1439 vim_free(sourcing_name);
1440 sourcing_name = saved_sourcing_name;
1441 sourcing_lnum = saved_sourcing_lnum;
1442 }
1443
1444 /*
1445 * On an interrupt or an aborting error not converted to an exception,
1446 * disable the conversion of errors to exceptions. (Interrupts are not
1447 * converted any more, here.) This enables also the interrupt message
1448 * when force_abort is set and did_emsg unset in case of an interrupt
1449 * from a finally clause after an error.
1450 */
1451 else if (got_int || (did_emsg && force_abort))
1452 suppress_errthrow = TRUE;
1453 }
1454
1455 /*
1456 * The current cstack will be freed when do_cmdline() returns. An uncaught
1457 * exception will have to be rethrown in the previous cstack. If a function
1458 * has just returned or a script file was just finished and the previous
1459 * cstack belongs to the same function or, respectively, script file, it
1460 * will have to be checked for finally clauses to be executed due to the
1461 * ":return" or ":finish". This is done in do_one_cmd().
1462 */
1463 if (did_throw)
1464 need_rethrow = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001465 if ((getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466 && ex_nesting_level > source_level(real_cookie))
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001467 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001468 && ex_nesting_level > func_level(real_cookie) + 1))
1469 {
1470 if (!did_throw)
1471 check_cstack = TRUE;
1472 }
1473 else
1474 {
1475 /* When leaving a function, reduce nesting level. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001476 if (getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001477 --ex_nesting_level;
1478 /*
1479 * Go to debug mode when returning from a function in which we are
1480 * single-stepping.
1481 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001482 if ((getline_equal(fgetline, cookie, getsourceline)
1483 || getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484 && ex_nesting_level + 1 <= debug_break_level)
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001485 do_debug(getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001486 ? (char_u *)_("End of sourced file")
1487 : (char_u *)_("End of function"));
1488 }
1489
1490 /*
1491 * Restore the exception environment (done after returning from the
1492 * debugger).
1493 */
1494 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001495 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001496
1497 msg_list = saved_msg_list;
1498#endif /* FEAT_EVAL */
1499
1500 /*
1501 * If there was too much output to fit on the command line, ask the user to
1502 * hit return before redrawing the screen. With the ":global" command we do
1503 * this only once after the command is finished.
1504 */
1505 if (did_inc)
1506 {
1507 --RedrawingDisabled;
1508 --no_wait_return;
1509 msg_scroll = FALSE;
1510
1511 /*
1512 * When just finished an ":if"-":else" which was typed, no need to
1513 * wait for hit-return. Also for an error situation.
1514 */
1515 if (retval == FAIL
1516#ifdef FEAT_EVAL
1517 || (did_endif && KeyTyped && !did_emsg)
1518#endif
1519 )
1520 {
1521 need_wait_return = FALSE;
1522 msg_didany = FALSE; /* don't wait when restarting edit */
1523 }
1524 else if (need_wait_return)
1525 {
1526 /*
1527 * The msg_start() above clears msg_didout. The wait_return we do
1528 * here should not overwrite the command that may be shown before
1529 * doing that.
1530 */
1531 msg_didout |= msg_didout_before_start;
1532 wait_return(FALSE);
1533 }
1534 }
1535
Bram Moolenaar2a942252012-11-28 23:03:07 +01001536#ifdef FEAT_EVAL
1537 did_endif = FALSE; /* in case do_cmdline used recursively */
1538#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001539 /*
1540 * Reset if_level, in case a sourced script file contains more ":if" than
1541 * ":endif" (could be ":if x | foo | endif").
1542 */
1543 if_level = 0;
Bram Moolenaar2e18a122012-11-28 19:10:54 +01001544#endif
Bram Moolenaard4ad0d42012-11-28 17:34:48 +01001545
Bram Moolenaar071d4272004-06-13 20:20:40 +00001546 --call_depth;
1547 return retval;
1548}
1549
1550#ifdef FEAT_EVAL
1551/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001552 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001553 */
1554 static char_u *
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001555get_loop_line(c, cookie, indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001556 int c;
1557 void *cookie;
1558 int indent;
1559{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001560 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561 wcmd_T *wp;
1562 char_u *line;
1563
1564 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1565 {
1566 if (cp->repeating)
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001567 return NULL; /* trying to read past ":endwhile"/":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001568
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001569 /* First time inside the ":while"/":for": get line normally. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570 if (cp->getline == NULL)
1571 line = getcmdline(c, 0L, indent);
1572 else
1573 line = cp->getline(c, cp->cookie, indent);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001574 if (line != NULL && store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575 ++cp->current_line;
1576
1577 return line;
1578 }
1579
1580 KeyTyped = FALSE;
1581 ++cp->current_line;
1582 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
1583 sourcing_lnum = wp->lnum;
1584 return vim_strsave(wp->line);
1585}
1586
1587/*
1588 * Store a line in "gap" so that a ":while" loop can execute it again.
1589 */
1590 static int
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001591store_loop_line(gap, line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592 garray_T *gap;
1593 char_u *line;
1594{
1595 if (ga_grow(gap, 1) == FAIL)
1596 return FAIL;
1597 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
1598 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = sourcing_lnum;
1599 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001600 return OK;
1601}
1602
1603/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001604 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001605 */
1606 static void
1607free_cmdlines(gap)
1608 garray_T *gap;
1609{
1610 while (gap->ga_len > 0)
1611 {
1612 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1613 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001614 }
1615}
1616#endif
1617
1618/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001619 * If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals
1620 * "func". * Otherwise return TRUE when "fgetline" equals "func".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001622 int
Bram Moolenaar89d40322006-08-29 15:30:07 +00001623getline_equal(fgetline, cookie, func)
1624 char_u *(*fgetline) __ARGS((int, void *, int));
Bram Moolenaar78a15312009-05-15 19:33:18 +00001625 void *cookie UNUSED; /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626 char_u *(*func) __ARGS((int, void *, int));
1627{
1628#ifdef FEAT_EVAL
1629 char_u *(*gp) __ARGS((int, void *, int));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001630 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631
Bram Moolenaar89d40322006-08-29 15:30:07 +00001632 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001633 * function that's originally used to obtain the lines. This may be
1634 * nested several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001635 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001636 cp = (struct loop_cookie *)cookie;
1637 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638 {
1639 gp = cp->getline;
1640 cp = cp->cookie;
1641 }
1642 return gp == func;
1643#else
Bram Moolenaar89d40322006-08-29 15:30:07 +00001644 return fgetline == func;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001645#endif
1646}
1647
1648#if defined(FEAT_EVAL) || defined(FEAT_MBYTE) || defined(PROTO)
1649/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001650 * If "fgetline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651 * getline function. Otherwise return "cookie".
1652 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653 void *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001654getline_cookie(fgetline, cookie)
Bram Moolenaar78a15312009-05-15 19:33:18 +00001655 char_u *(*fgetline) __ARGS((int, void *, int)) UNUSED;
Bram Moolenaar89d40322006-08-29 15:30:07 +00001656 void *cookie; /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001657{
1658# ifdef FEAT_EVAL
1659 char_u *(*gp) __ARGS((int, void *, int));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001660 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661
Bram Moolenaar89d40322006-08-29 15:30:07 +00001662 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001663 * cookie that's originally used to obtain the lines. This may be nested
Bram Moolenaar071d4272004-06-13 20:20:40 +00001664 * several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001665 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001666 cp = (struct loop_cookie *)cookie;
1667 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001668 {
1669 gp = cp->getline;
1670 cp = cp->cookie;
1671 }
1672 return cp;
1673# else
1674 return cookie;
1675# endif
1676}
1677#endif
1678
1679/*
1680 * Execute one Ex command.
1681 *
1682 * If 'sourcing' is TRUE, the command will be included in the error message.
1683 *
1684 * 1. skip comment lines and leading space
1685 * 2. handle command modifiers
1686 * 3. parse range
1687 * 4. parse command
1688 * 5. parse arguments
1689 * 6. switch on command name
1690 *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001691 * Note: "fgetline" can be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001692 *
1693 * This function may be called recursively!
1694 */
1695#if (_MSC_VER == 1200)
1696/*
Bram Moolenaared203462004-06-16 11:19:22 +00001697 * Avoid optimisation bug in VC++ version 6.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001698 */
Bram Moolenaar281bdce2005-01-25 21:53:18 +00001699 #pragma optimize( "g", off )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001700#endif
1701 static char_u *
1702do_one_cmd(cmdlinep, sourcing,
1703#ifdef FEAT_EVAL
1704 cstack,
1705#endif
Bram Moolenaar89d40322006-08-29 15:30:07 +00001706 fgetline, cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001707 char_u **cmdlinep;
1708 int sourcing;
1709#ifdef FEAT_EVAL
1710 struct condstack *cstack;
1711#endif
Bram Moolenaar89d40322006-08-29 15:30:07 +00001712 char_u *(*fgetline) __ARGS((int, void *, int));
1713 void *cookie; /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714{
1715 char_u *p;
1716 linenr_T lnum;
1717 long n;
1718 char_u *errormsg = NULL; /* error message */
1719 exarg_T ea; /* Ex command arguments */
1720 long verbose_save = -1;
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001721 int save_msg_scroll = msg_scroll;
1722 int save_msg_silent = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723 int did_esilent = 0;
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001724#ifdef HAVE_SANDBOX
1725 int did_sandbox = FALSE;
1726#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727 cmdmod_T save_cmdmod;
1728 int ni; /* set when Not Implemented */
1729
1730 vim_memset(&ea, 0, sizeof(ea));
1731 ea.line1 = 1;
1732 ea.line2 = 1;
1733#ifdef FEAT_EVAL
1734 ++ex_nesting_level;
1735#endif
1736
Bram Moolenaar21691f82012-12-05 19:13:18 +01001737 /* When the last file has not been edited :q has to be typed twice. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738 if (quitmore
1739#ifdef FEAT_EVAL
1740 /* avoid that a function call in 'statusline' does this */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001741 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001742#endif
1743#ifdef FEAT_AUTOCMD
Bram Moolenaar21691f82012-12-05 19:13:18 +01001744 /* avoid that an autocommand, e.g. QuitPre, does this */
1745 && !getline_equal(fgetline, cookie, getnextac)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001746#endif
1747 )
1748 --quitmore;
1749
1750 /*
1751 * Reset browse, confirm, etc.. They are restored when returning, for
1752 * recursive calls.
1753 */
1754 save_cmdmod = cmdmod;
1755 vim_memset(&cmdmod, 0, sizeof(cmdmod));
1756
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001757 /* "#!anything" is handled like a comment. */
1758 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1759 goto doend;
1760
Bram Moolenaar071d4272004-06-13 20:20:40 +00001761 /*
1762 * Repeat until no more command modifiers are found.
1763 */
1764 ea.cmd = *cmdlinep;
1765 for (;;)
1766 {
1767/*
1768 * 1. skip comment lines and leading white space and colons
1769 */
1770 while (*ea.cmd == ' ' || *ea.cmd == '\t' || *ea.cmd == ':')
1771 ++ea.cmd;
1772
1773 /* in ex mode, an empty line works like :+ */
1774 if (*ea.cmd == NUL && exmode_active
Bram Moolenaar89d40322006-08-29 15:30:07 +00001775 && (getline_equal(fgetline, cookie, getexmodeline)
1776 || getline_equal(fgetline, cookie, getexline))
Bram Moolenaardf177f62005-02-22 08:39:57 +00001777 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778 {
1779 ea.cmd = (char_u *)"+";
1780 ex_pressedreturn = TRUE;
1781 }
1782
1783 /* ignore comment and empty lines */
Bram Moolenaarf998c042007-11-20 11:31:26 +00001784 if (*ea.cmd == '"')
1785 goto doend;
1786 if (*ea.cmd == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001787 {
1788 ex_pressedreturn = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001790 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791
1792/*
1793 * 2. handle command modifiers.
1794 */
1795 p = ea.cmd;
1796 if (VIM_ISDIGIT(*ea.cmd))
1797 p = skipwhite(skipdigits(ea.cmd));
1798 switch (*p)
1799 {
1800 /* When adding an entry, also modify cmd_exists(). */
1801 case 'a': if (!checkforcmd(&ea.cmd, "aboveleft", 3))
1802 break;
1803#ifdef FEAT_WINDOWS
1804 cmdmod.split |= WSP_ABOVE;
1805#endif
1806 continue;
1807
1808 case 'b': if (checkforcmd(&ea.cmd, "belowright", 3))
1809 {
1810#ifdef FEAT_WINDOWS
1811 cmdmod.split |= WSP_BELOW;
1812#endif
1813 continue;
1814 }
1815 if (checkforcmd(&ea.cmd, "browse", 3))
1816 {
Bram Moolenaard812df62008-11-09 12:46:09 +00001817#ifdef FEAT_BROWSE_CMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818 cmdmod.browse = TRUE;
1819#endif
1820 continue;
1821 }
1822 if (!checkforcmd(&ea.cmd, "botright", 2))
1823 break;
1824#ifdef FEAT_WINDOWS
1825 cmdmod.split |= WSP_BOT;
1826#endif
1827 continue;
1828
1829 case 'c': if (!checkforcmd(&ea.cmd, "confirm", 4))
1830 break;
1831#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1832 cmdmod.confirm = TRUE;
1833#endif
1834 continue;
1835
1836 case 'k': if (checkforcmd(&ea.cmd, "keepmarks", 3))
1837 {
1838 cmdmod.keepmarks = TRUE;
1839 continue;
1840 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001841 if (checkforcmd(&ea.cmd, "keepalt", 5))
1842 {
1843 cmdmod.keepalt = TRUE;
1844 continue;
1845 }
Bram Moolenaara939e432013-11-09 05:30:26 +01001846 if (checkforcmd(&ea.cmd, "keeppatterns", 5))
1847 {
1848 cmdmod.keeppatterns = TRUE;
1849 continue;
1850 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851 if (!checkforcmd(&ea.cmd, "keepjumps", 5))
1852 break;
1853 cmdmod.keepjumps = TRUE;
1854 continue;
1855
1856 /* ":hide" and ":hide | cmd" are not modifiers */
1857 case 'h': if (p != ea.cmd || !checkforcmd(&p, "hide", 3)
1858 || *p == NUL || ends_excmd(*p))
1859 break;
1860 ea.cmd = p;
1861 cmdmod.hide = TRUE;
1862 continue;
1863
1864 case 'l': if (checkforcmd(&ea.cmd, "lockmarks", 3))
1865 {
1866 cmdmod.lockmarks = TRUE;
1867 continue;
1868 }
1869
1870 if (!checkforcmd(&ea.cmd, "leftabove", 5))
1871 break;
1872#ifdef FEAT_WINDOWS
1873 cmdmod.split |= WSP_ABOVE;
1874#endif
1875 continue;
1876
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001877 case 'n': if (checkforcmd(&ea.cmd, "noautocmd", 3))
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001878 {
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001879#ifdef FEAT_AUTOCMD
1880 if (cmdmod.save_ei == NULL)
1881 {
1882 /* Set 'eventignore' to "all". Restore the
1883 * existing option value later. */
1884 cmdmod.save_ei = vim_strsave(p_ei);
1885 set_string_option_direct((char_u *)"ei", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001886 (char_u *)"all", OPT_FREE, SID_NONE);
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001887 }
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001888#endif
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001889 continue;
1890 }
1891 if (!checkforcmd(&ea.cmd, "noswapfile", 6))
1892 break;
1893 cmdmod.noswapfile = TRUE;
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001894 continue;
1895
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896 case 'r': if (!checkforcmd(&ea.cmd, "rightbelow", 6))
1897 break;
1898#ifdef FEAT_WINDOWS
1899 cmdmod.split |= WSP_BELOW;
1900#endif
1901 continue;
1902
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001903 case 's': if (checkforcmd(&ea.cmd, "sandbox", 3))
1904 {
1905#ifdef HAVE_SANDBOX
1906 if (!did_sandbox)
1907 ++sandbox;
1908 did_sandbox = TRUE;
1909#endif
1910 continue;
1911 }
1912 if (!checkforcmd(&ea.cmd, "silent", 3))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001913 break;
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001914 if (save_msg_silent == -1)
1915 save_msg_silent = msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001916 ++msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917 if (*ea.cmd == '!' && !vim_iswhite(ea.cmd[-1]))
1918 {
1919 /* ":silent!", but not "silent !cmd" */
1920 ea.cmd = skipwhite(ea.cmd + 1);
1921 ++emsg_silent;
1922 ++did_esilent;
1923 }
1924 continue;
1925
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001926 case 't': if (checkforcmd(&p, "tab", 3))
1927 {
1928#ifdef FEAT_WINDOWS
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001929 if (vim_isdigit(*ea.cmd))
1930 cmdmod.tab = atoi((char *)ea.cmd) + 1;
1931 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001932 cmdmod.tab = tabpage_index(curtab) + 1;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001933 ea.cmd = p;
1934#endif
1935 continue;
1936 }
1937 if (!checkforcmd(&ea.cmd, "topleft", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938 break;
1939#ifdef FEAT_WINDOWS
1940 cmdmod.split |= WSP_TOP;
1941#endif
1942 continue;
1943
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001944 case 'u': if (!checkforcmd(&ea.cmd, "unsilent", 3))
1945 break;
1946 if (save_msg_silent == -1)
1947 save_msg_silent = msg_silent;
1948 msg_silent = 0;
1949 continue;
1950
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951 case 'v': if (checkforcmd(&ea.cmd, "vertical", 4))
1952 {
1953#ifdef FEAT_VERTSPLIT
1954 cmdmod.split |= WSP_VERT;
1955#endif
1956 continue;
1957 }
1958 if (!checkforcmd(&p, "verbose", 4))
1959 break;
1960 if (verbose_save < 0)
1961 verbose_save = p_verbose;
Bram Moolenaared203462004-06-16 11:19:22 +00001962 if (vim_isdigit(*ea.cmd))
1963 p_verbose = atoi((char *)ea.cmd);
1964 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001965 p_verbose = 1;
1966 ea.cmd = p;
1967 continue;
1968 }
1969 break;
1970 }
1971
1972#ifdef FEAT_EVAL
1973 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
1974 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
1975#else
1976 ea.skip = (if_level > 0);
1977#endif
1978
1979#ifdef FEAT_EVAL
Bram Moolenaar05159a02005-02-26 23:04:13 +00001980# ifdef FEAT_PROFILE
1981 /* Count this line for profiling if ea.skip is FALSE. */
Bram Moolenaar371d5402006-03-20 21:47:49 +00001982 if (do_profiling == PROF_YES && !ea.skip)
Bram Moolenaar05159a02005-02-26 23:04:13 +00001983 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001984 if (getline_equal(fgetline, cookie, get_func_line))
1985 func_line_exec(getline_cookie(fgetline, cookie));
1986 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +00001987 script_line_exec();
1988 }
1989#endif
1990
Bram Moolenaar071d4272004-06-13 20:20:40 +00001991 /* May go to debug mode. If this happens and the ">quit" debug command is
1992 * used, throw an interrupt exception and skip the next command. */
1993 dbg_check_breakpoint(&ea);
1994 if (!ea.skip && got_int)
1995 {
1996 ea.skip = TRUE;
1997 (void)do_intthrow(cstack);
1998 }
1999#endif
2000
2001/*
2002 * 3. parse a range specifier of the form: addr [,addr] [;addr] ..
2003 *
2004 * where 'addr' is:
2005 *
2006 * % (entire file)
2007 * $ [+-NUM]
2008 * 'x [+-NUM] (where x denotes a currently defined mark)
2009 * . [+-NUM]
2010 * [+-NUM]..
2011 * NUM
2012 *
2013 * The ea.cmd pointer is updated to point to the first character following the
2014 * range spec. If an initial address is found, but no second, the upper bound
2015 * is equal to the lower.
2016 */
2017
2018 /* repeat for all ',' or ';' separated addresses */
2019 for (;;)
2020 {
2021 ea.line1 = ea.line2;
2022 ea.line2 = curwin->w_cursor.lnum; /* default is current line number */
2023 ea.cmd = skipwhite(ea.cmd);
2024 lnum = get_address(&ea.cmd, ea.skip, ea.addr_count == 0);
2025 if (ea.cmd == NULL) /* error detected */
2026 goto doend;
2027 if (lnum == MAXLNUM)
2028 {
2029 if (*ea.cmd == '%') /* '%' - all lines */
2030 {
2031 ++ea.cmd;
2032 ea.line1 = 1;
2033 ea.line2 = curbuf->b_ml.ml_line_count;
2034 ++ea.addr_count;
2035 }
2036 /* '*' - visual area */
2037 else if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
2038 {
2039 pos_T *fp;
2040
2041 ++ea.cmd;
2042 if (!ea.skip)
2043 {
2044 fp = getmark('<', FALSE);
2045 if (check_mark(fp) == FAIL)
2046 goto doend;
2047 ea.line1 = fp->lnum;
2048 fp = getmark('>', FALSE);
2049 if (check_mark(fp) == FAIL)
2050 goto doend;
2051 ea.line2 = fp->lnum;
2052 ++ea.addr_count;
2053 }
2054 }
2055 }
2056 else
2057 ea.line2 = lnum;
2058 ea.addr_count++;
2059
2060 if (*ea.cmd == ';')
2061 {
2062 if (!ea.skip)
2063 curwin->w_cursor.lnum = ea.line2;
2064 }
2065 else if (*ea.cmd != ',')
2066 break;
2067 ++ea.cmd;
2068 }
2069
2070 /* One address given: set start and end lines */
2071 if (ea.addr_count == 1)
2072 {
2073 ea.line1 = ea.line2;
2074 /* ... but only implicit: really no address given */
2075 if (lnum == MAXLNUM)
2076 ea.addr_count = 0;
2077 }
2078
2079 /* Don't leave the cursor on an illegal line (caused by ';') */
2080 check_cursor_lnum();
2081
2082/*
2083 * 4. parse command
2084 */
2085
2086 /*
2087 * Skip ':' and any white space
2088 */
2089 ea.cmd = skipwhite(ea.cmd);
2090 while (*ea.cmd == ':')
2091 ea.cmd = skipwhite(ea.cmd + 1);
2092
2093 /*
2094 * If we got a line, but no command, then go to the line.
2095 * If we find a '|' or '\n' we set ea.nextcmd.
2096 */
2097 if (*ea.cmd == NUL || *ea.cmd == '"' ||
2098 (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
2099 {
2100 /*
2101 * strange vi behaviour:
2102 * ":3" jumps to line 3
2103 * ":3|..." prints line 3
2104 * ":|" prints current line
2105 */
2106 if (ea.skip) /* skip this if inside :if */
2107 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00002108 if (*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002109 {
2110 ea.cmdidx = CMD_print;
2111 ea.argt = RANGE+COUNT+TRLBAR;
2112 if ((errormsg = invalid_range(&ea)) == NULL)
2113 {
2114 correct_range(&ea);
2115 ex_print(&ea);
2116 }
2117 }
2118 else if (ea.addr_count != 0)
2119 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002120 if (ea.line2 > curbuf->b_ml.ml_line_count)
2121 {
2122 /* With '-' in 'cpoptions' a line number past the file is an
2123 * error, otherwise put it at the end of the file. */
2124 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
2125 ea.line2 = -1;
2126 else
2127 ea.line2 = curbuf->b_ml.ml_line_count;
2128 }
2129
2130 if (ea.line2 < 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002131 errormsg = (char_u *)_(e_invrange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132 else
2133 {
2134 if (ea.line2 == 0)
2135 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002136 else
2137 curwin->w_cursor.lnum = ea.line2;
2138 beginline(BL_SOL | BL_FIX);
2139 }
2140 }
2141 goto doend;
2142 }
2143
2144 /* Find the command and let "p" point to after it. */
2145 p = find_command(&ea, NULL);
2146
2147#ifdef FEAT_USR_CMDS
2148 if (p == NULL)
2149 {
2150 if (!ea.skip)
2151 errormsg = (char_u *)_("E464: Ambiguous use of user-defined command");
2152 goto doend;
2153 }
2154 /* Check for wrong commands. */
2155 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78)
2156 {
2157 errormsg = uc_fun_cmd();
2158 goto doend;
2159 }
2160#endif
2161 if (ea.cmdidx == CMD_SIZE)
2162 {
2163 if (!ea.skip)
2164 {
2165 STRCPY(IObuff, _("E492: Not an editor command"));
2166 if (!sourcing)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002167 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168 errormsg = IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02002169 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002170 }
2171 goto doend;
2172 }
2173
2174 ni = (
2175#ifdef FEAT_USR_CMDS
2176 !USER_CMDIDX(ea.cmdidx) &&
2177#endif
Bram Moolenaar3ebc1e52007-07-05 07:54:17 +00002178 (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00002179#ifdef HAVE_EX_SCRIPT_NI
2180 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
2181#endif
2182 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002183
2184#ifndef FEAT_EVAL
2185 /*
2186 * When the expression evaluation is disabled, recognize the ":if" and
2187 * ":endif" commands and ignore everything in between it.
2188 */
2189 if (ea.cmdidx == CMD_if)
2190 ++if_level;
2191 if (if_level)
2192 {
2193 if (ea.cmdidx == CMD_endif)
2194 --if_level;
2195 goto doend;
2196 }
2197
2198#endif
2199
Bram Moolenaar196b3b02008-06-20 16:51:41 +00002200 /* forced commands */
2201 if (*p == '!' && ea.cmdidx != CMD_substitute
2202 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002203 {
2204 ++p;
2205 ea.forceit = TRUE;
2206 }
2207 else
2208 ea.forceit = FALSE;
2209
2210/*
2211 * 5. parse arguments
2212 */
2213#ifdef FEAT_USR_CMDS
2214 if (!USER_CMDIDX(ea.cmdidx))
2215#endif
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002216 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217
2218 if (!ea.skip)
2219 {
2220#ifdef HAVE_SANDBOX
2221 if (sandbox != 0 && !(ea.argt & SBOXOK))
2222 {
2223 /* Command not allowed in sandbox. */
2224 errormsg = (char_u *)_(e_sandbox);
2225 goto doend;
2226 }
2227#endif
2228 if (!curbuf->b_p_ma && (ea.argt & MODIFY))
2229 {
2230 /* Command not allowed in non-'modifiable' buffer */
2231 errormsg = (char_u *)_(e_modifiable);
2232 goto doend;
2233 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002234
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002235 if (text_locked() && !(ea.argt & CMDWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002236# ifdef FEAT_USR_CMDS
2237 && !USER_CMDIDX(ea.cmdidx)
2238# endif
2239 )
2240 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002241 /* Command not allowed when editing the command line. */
2242#ifdef FEAT_CMDWIN
2243 if (cmdwin_type != 0)
2244 errormsg = (char_u *)_(e_cmdwin);
2245 else
2246#endif
2247 errormsg = (char_u *)_(e_secure);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002248 goto doend;
2249 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002250#ifdef FEAT_AUTOCMD
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002251 /* Disallow editing another buffer when "curbuf_lock" is set.
2252 * Do allow ":edit" (check for argument later).
2253 * Do allow ":checktime" (it's postponed). */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002254 if (!(ea.argt & CMDWIN)
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002255 && ea.cmdidx != CMD_edit
2256 && ea.cmdidx != CMD_checktime
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002257# ifdef FEAT_USR_CMDS
2258 && !USER_CMDIDX(ea.cmdidx)
2259# endif
2260 && curbuf_locked())
2261 goto doend;
2262#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002263
2264 if (!ni && !(ea.argt & RANGE) && ea.addr_count > 0)
2265 {
2266 /* no range allowed */
2267 errormsg = (char_u *)_(e_norange);
2268 goto doend;
2269 }
2270 }
2271
2272 if (!ni && !(ea.argt & BANG) && ea.forceit) /* no <!> allowed */
2273 {
2274 errormsg = (char_u *)_(e_nobang);
2275 goto doend;
2276 }
2277
2278 /*
2279 * Don't complain about the range if it is not used
2280 * (could happen if line_count is accidentally set to 0).
2281 */
2282 if (!ea.skip && !ni)
2283 {
2284 /*
2285 * If the range is backwards, ask for confirmation and, if given, swap
2286 * ea.line1 & ea.line2 so it's forwards again.
2287 * When global command is busy, don't ask, will fail below.
2288 */
2289 if (!global_busy && ea.line1 > ea.line2)
2290 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002291 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002293 if (sourcing || exmode_active)
2294 {
2295 errormsg = (char_u *)_("E493: Backwards range given");
2296 goto doend;
2297 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002298 if (ask_yesno((char_u *)
2299 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002300 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301 }
2302 lnum = ea.line1;
2303 ea.line1 = ea.line2;
2304 ea.line2 = lnum;
2305 }
2306 if ((errormsg = invalid_range(&ea)) != NULL)
2307 goto doend;
2308 }
2309
2310 if ((ea.argt & NOTADR) && ea.addr_count == 0) /* default is 1, not cursor */
2311 ea.line2 = 1;
2312
2313 correct_range(&ea);
2314
2315#ifdef FEAT_FOLDING
2316 if (((ea.argt & WHOLEFOLD) || ea.addr_count >= 2) && !global_busy)
2317 {
2318 /* Put the first line at the start of a closed fold, put the last line
2319 * at the end of a closed fold. */
2320 (void)hasFolding(ea.line1, &ea.line1, NULL);
2321 (void)hasFolding(ea.line2, NULL, &ea.line2);
2322 }
2323#endif
2324
2325#ifdef FEAT_QUICKFIX
2326 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002327 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002328 * option here, so things like % get expanded.
2329 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002330 p = replace_makeprg(&ea, p, cmdlinep);
2331 if (p == NULL)
2332 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002333#endif
2334
2335 /*
2336 * Skip to start of argument.
2337 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2338 */
2339 if (ea.cmdidx == CMD_bang)
2340 ea.arg = p;
2341 else
2342 ea.arg = skipwhite(p);
2343
2344 /*
2345 * Check for "++opt=val" argument.
2346 * Must be first, allow ":w ++enc=utf8 !cmd"
2347 */
2348 if (ea.argt & ARGOPT)
2349 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2350 if (getargopt(&ea) == FAIL && !ni)
2351 {
2352 errormsg = (char_u *)_(e_invarg);
2353 goto doend;
2354 }
2355
2356 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2357 {
2358 if (*ea.arg == '>') /* append */
2359 {
2360 if (*++ea.arg != '>') /* typed wrong */
2361 {
2362 errormsg = (char_u *)_("E494: Use w or w>>");
2363 goto doend;
2364 }
2365 ea.arg = skipwhite(ea.arg + 1);
2366 ea.append = TRUE;
2367 }
2368 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
2369 {
2370 ++ea.arg;
2371 ea.usefilter = TRUE;
2372 }
2373 }
2374
2375 if (ea.cmdidx == CMD_read)
2376 {
2377 if (ea.forceit)
2378 {
2379 ea.usefilter = TRUE; /* :r! filter if ea.forceit */
2380 ea.forceit = FALSE;
2381 }
2382 else if (*ea.arg == '!') /* :r !filter */
2383 {
2384 ++ea.arg;
2385 ea.usefilter = TRUE;
2386 }
2387 }
2388
2389 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2390 {
2391 ea.amount = 1;
2392 while (*ea.arg == *ea.cmd) /* count number of '>' or '<' */
2393 {
2394 ++ea.arg;
2395 ++ea.amount;
2396 }
2397 ea.arg = skipwhite(ea.arg);
2398 }
2399
2400 /*
2401 * Check for "+command" argument, before checking for next command.
2402 * Don't do this for ":read !cmd" and ":write !cmd".
2403 */
2404 if ((ea.argt & EDITCMD) && !ea.usefilter)
2405 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2406
2407 /*
2408 * Check for '|' to separate commands and '"' to start comments.
2409 * Don't do this for ":read !cmd" and ":write !cmd".
2410 */
2411 if ((ea.argt & TRLBAR) && !ea.usefilter)
2412 separate_nextcmd(&ea);
2413
2414 /*
2415 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002416 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2417 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002418 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002419 else if (ea.cmdidx == CMD_bang
2420 || ea.cmdidx == CMD_global
2421 || ea.cmdidx == CMD_vglobal
2422 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423 {
2424 for (p = ea.arg; *p; ++p)
2425 {
2426 /* Remove one backslash before a newline, so that it's possible to
2427 * pass a newline to the shell and also a newline that is preceded
2428 * with a backslash. This makes it impossible to end a shell
2429 * command in a backslash, but that doesn't appear useful.
2430 * Halving the number of backslashes is incompatible with previous
2431 * versions. */
2432 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002433 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434 else if (*p == '\n')
2435 {
2436 ea.nextcmd = p + 1;
2437 *p = NUL;
2438 break;
2439 }
2440 }
2441 }
2442
2443 if ((ea.argt & DFLALL) && ea.addr_count == 0)
2444 {
2445 ea.line1 = 1;
2446 ea.line2 = curbuf->b_ml.ml_line_count;
2447 }
2448
2449 /* accept numbered register only when no count allowed (:put) */
2450 if ( (ea.argt & REGSTR)
2451 && *ea.arg != NUL
2452#ifdef FEAT_USR_CMDS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453 /* Do not allow register = for user commands */
2454 && (!USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002455#endif
2456 && !((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)))
2457 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002458#ifndef FEAT_CLIPBOARD
2459 /* check these explicitly for a more specific error message */
2460 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002462 errormsg = (char_u *)_(e_invalidreg);
2463 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002464 }
2465#endif
Bram Moolenaar85de2062011-05-05 14:26:41 +02002466 if (
2467#ifdef FEAT_USR_CMDS
2468 valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2469 && USER_CMDIDX(ea.cmdidx)))
2470#else
2471 valid_yank_reg(*ea.arg, ea.cmdidx != CMD_put)
2472#endif
2473 )
2474 {
2475 ea.regname = *ea.arg++;
2476#ifdef FEAT_EVAL
2477 /* for '=' register: accept the rest of the line as an expression */
2478 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2479 {
2480 set_expr_line(vim_strsave(ea.arg));
2481 ea.arg += STRLEN(ea.arg);
2482 }
2483#endif
2484 ea.arg = skipwhite(ea.arg);
2485 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486 }
2487
2488 /*
2489 * Check for a count. When accepting a BUFNAME, don't use "123foo" as a
2490 * count, it's a buffer name.
2491 */
2492 if ((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)
2493 && (!(ea.argt & BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
2494 || vim_iswhite(*p)))
2495 {
2496 n = getdigits(&ea.arg);
2497 ea.arg = skipwhite(ea.arg);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002498 if (n <= 0 && !ni && (ea.argt & ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002499 {
2500 errormsg = (char_u *)_(e_zerocount);
2501 goto doend;
2502 }
2503 if (ea.argt & NOTADR) /* e.g. :buffer 2, :sleep 3 */
2504 {
2505 ea.line2 = n;
2506 if (ea.addr_count == 0)
2507 ea.addr_count = 1;
2508 }
2509 else
2510 {
2511 ea.line1 = ea.line2;
2512 ea.line2 += n - 1;
2513 ++ea.addr_count;
2514 /*
2515 * Be vi compatible: no error message for out of range.
2516 */
2517 if (ea.line2 > curbuf->b_ml.ml_line_count)
2518 ea.line2 = curbuf->b_ml.ml_line_count;
2519 }
2520 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002521
2522 /*
2523 * Check for flags: 'l', 'p' and '#'.
2524 */
2525 if (ea.argt & EXFLAGS)
2526 get_flags(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527 /* no arguments allowed */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002528 if (!ni && !(ea.argt & EXTRA) && *ea.arg != NUL
Bram Moolenaara2031822006-03-07 22:29:51 +00002529 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002530 {
2531 errormsg = (char_u *)_(e_trailing);
2532 goto doend;
2533 }
2534
2535 if (!ni && (ea.argt & NEEDARG) && *ea.arg == NUL)
2536 {
2537 errormsg = (char_u *)_(e_argreq);
2538 goto doend;
2539 }
2540
2541#ifdef FEAT_EVAL
2542 /*
2543 * Skip the command when it's not going to be executed.
2544 * The commands like :if, :endif, etc. always need to be executed.
2545 * Also make an exception for commands that handle a trailing command
2546 * themselves.
2547 */
2548 if (ea.skip)
2549 {
2550 switch (ea.cmdidx)
2551 {
2552 /* commands that need evaluation */
2553 case CMD_while:
2554 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002555 case CMD_for:
2556 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557 case CMD_if:
2558 case CMD_elseif:
2559 case CMD_else:
2560 case CMD_endif:
2561 case CMD_try:
2562 case CMD_catch:
2563 case CMD_finally:
2564 case CMD_endtry:
2565 case CMD_function:
2566 break;
2567
2568 /* Commands that handle '|' themselves. Check: A command should
2569 * either have the TRLBAR flag, appear in this list or appear in
2570 * the list at ":help :bar". */
2571 case CMD_aboveleft:
2572 case CMD_and:
2573 case CMD_belowright:
2574 case CMD_botright:
2575 case CMD_browse:
2576 case CMD_call:
2577 case CMD_confirm:
2578 case CMD_delfunction:
2579 case CMD_djump:
2580 case CMD_dlist:
2581 case CMD_dsearch:
2582 case CMD_dsplit:
2583 case CMD_echo:
2584 case CMD_echoerr:
2585 case CMD_echomsg:
2586 case CMD_echon:
2587 case CMD_execute:
2588 case CMD_help:
2589 case CMD_hide:
2590 case CMD_ijump:
2591 case CMD_ilist:
2592 case CMD_isearch:
2593 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002594 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595 case CMD_keepjumps:
2596 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01002597 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002598 case CMD_leftabove:
2599 case CMD_let:
2600 case CMD_lockmarks:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002601 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002603 case CMD_mzscheme:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01002604 case CMD_noautocmd:
2605 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606 case CMD_perl:
2607 case CMD_psearch:
2608 case CMD_python:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002609 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002610 case CMD_python3:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002611 case CMD_return:
2612 case CMD_rightbelow:
2613 case CMD_ruby:
2614 case CMD_silent:
2615 case CMD_smagic:
2616 case CMD_snomagic:
2617 case CMD_substitute:
2618 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002619 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002620 case CMD_tcl:
2621 case CMD_throw:
2622 case CMD_tilde:
2623 case CMD_topleft:
2624 case CMD_unlet:
2625 case CMD_verbose:
2626 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002627 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002628 break;
2629
2630 default: goto doend;
2631 }
2632 }
2633#endif
2634
2635 if (ea.argt & XFILE)
2636 {
2637 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2638 goto doend;
2639 }
2640
2641#ifdef FEAT_LISTCMDS
2642 /*
2643 * Accept buffer name. Cannot be used at the same time with a buffer
2644 * number. Don't do this for a user command.
2645 */
2646 if ((ea.argt & BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
2647# ifdef FEAT_USR_CMDS
2648 && !USER_CMDIDX(ea.cmdidx)
2649# endif
2650 )
2651 {
2652 /*
2653 * :bdelete, :bwipeout and :bunload take several arguments, separated
2654 * by spaces: find next space (skipping over escaped characters).
2655 * The others take one argument: ignore trailing spaces.
2656 */
2657 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2658 || ea.cmdidx == CMD_bunload)
2659 p = skiptowhite_esc(ea.arg);
2660 else
2661 {
2662 p = ea.arg + STRLEN(ea.arg);
2663 while (p > ea.arg && vim_iswhite(p[-1]))
2664 --p;
2665 }
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002666 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & BUFUNL) != 0,
2667 FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668 if (ea.line2 < 0) /* failed */
2669 goto doend;
2670 ea.addr_count = 1;
2671 ea.arg = skipwhite(p);
2672 }
2673#endif
2674
2675/*
2676 * 6. switch on command name
2677 *
2678 * The "ea" structure holds the arguments that can be used.
2679 */
2680 ea.cmdlinep = cmdlinep;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002681 ea.getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682 ea.cookie = cookie;
2683#ifdef FEAT_EVAL
2684 ea.cstack = cstack;
2685#endif
2686
2687#ifdef FEAT_USR_CMDS
2688 if (USER_CMDIDX(ea.cmdidx))
2689 {
2690 /*
2691 * Execute a user-defined command.
2692 */
2693 do_ucmd(&ea);
2694 }
2695 else
2696#endif
2697 {
2698 /*
2699 * Call the function to execute the command.
2700 */
2701 ea.errmsg = NULL;
2702 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2703 if (ea.errmsg != NULL)
2704 errormsg = (char_u *)_(ea.errmsg);
2705 }
2706
2707#ifdef FEAT_EVAL
2708 /*
2709 * If the command just executed called do_cmdline(), any throw or ":return"
2710 * or ":finish" encountered there must also check the cstack of the still
2711 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2712 * exception, or reanimate a returned function or finished script file and
2713 * return or finish it again.
2714 */
2715 if (need_rethrow)
2716 do_throw(cstack);
2717 else if (check_cstack)
2718 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002719 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002721 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002722 && current_func_returned())
2723 do_return(&ea, TRUE, FALSE, NULL);
2724 }
2725 need_rethrow = check_cstack = FALSE;
2726#endif
2727
2728doend:
2729 if (curwin->w_cursor.lnum == 0) /* can happen with zero line number */
2730 curwin->w_cursor.lnum = 1;
2731
2732 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2733 {
2734 if (sourcing)
2735 {
2736 if (errormsg != IObuff)
2737 {
2738 STRCPY(IObuff, errormsg);
2739 errormsg = IObuff;
2740 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002741 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002742 }
2743 emsg(errormsg);
2744 }
2745#ifdef FEAT_EVAL
2746 do_errthrow(cstack,
2747 (ea.cmdidx != CMD_SIZE
2748# ifdef FEAT_USR_CMDS
2749 && !USER_CMDIDX(ea.cmdidx)
2750# endif
2751 ) ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
2752#endif
2753
2754 if (verbose_save >= 0)
2755 p_verbose = verbose_save;
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00002756#ifdef FEAT_AUTOCMD
2757 if (cmdmod.save_ei != NULL)
2758 {
2759 /* Restore 'eventignore' to the value before ":noautocmd". */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002760 set_string_option_direct((char_u *)"ei", -1, cmdmod.save_ei,
2761 OPT_FREE, SID_NONE);
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00002762 free_string_option(cmdmod.save_ei);
2763 }
2764#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002765
2766 cmdmod = save_cmdmod;
2767
Bram Moolenaar8e258a42009-07-09 13:55:43 +00002768 if (save_msg_silent != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002769 {
2770 /* messages could be enabled for a serious error, need to check if the
2771 * counters don't become negative */
Bram Moolenaarbecf4282009-09-30 11:24:36 +00002772 if (!did_emsg || msg_silent > save_msg_silent)
Bram Moolenaar8e258a42009-07-09 13:55:43 +00002773 msg_silent = save_msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774 emsg_silent -= did_esilent;
2775 if (emsg_silent < 0)
2776 emsg_silent = 0;
2777 /* Restore msg_scroll, it's set by file I/O commands, even when no
2778 * message is actually displayed. */
2779 msg_scroll = save_msg_scroll;
Bram Moolenaar77ab2802009-04-22 12:44:48 +00002780
2781 /* "silent reg" or "silent echo x" inside "redir" leaves msg_col
2782 * somewhere in the line. Put it back in the first column. */
2783 if (redirecting())
2784 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002785 }
2786
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002787#ifdef HAVE_SANDBOX
2788 if (did_sandbox)
2789 --sandbox;
2790#endif
2791
Bram Moolenaar071d4272004-06-13 20:20:40 +00002792 if (ea.nextcmd && *ea.nextcmd == NUL) /* not really a next command */
2793 ea.nextcmd = NULL;
2794
2795#ifdef FEAT_EVAL
2796 --ex_nesting_level;
2797#endif
2798
2799 return ea.nextcmd;
2800}
2801#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00002802 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002803#endif
2804
2805/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002806 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002807 * If there is a match advance "pp" to the argument and return TRUE.
2808 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002809 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00002810checkforcmd(pp, cmd, len)
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002811 char_u **pp; /* start of command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002812 char *cmd; /* name of command */
2813 int len; /* required length */
2814{
2815 int i;
2816
2817 for (i = 0; cmd[i] != NUL; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00002818 if (((char_u *)cmd)[i] != (*pp)[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00002819 break;
2820 if (i >= len && !isalpha((*pp)[i]))
2821 {
2822 *pp = skipwhite(*pp + i);
2823 return TRUE;
2824 }
2825 return FALSE;
2826}
2827
2828/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002829 * Append "cmd" to the error message in IObuff.
2830 * Takes care of limiting the length and handling 0xa0, which would be
2831 * invisible otherwise.
2832 */
2833 static void
2834append_command(cmd)
2835 char_u *cmd;
2836{
2837 char_u *s = cmd;
2838 char_u *d;
2839
2840 STRCAT(IObuff, ": ");
2841 d = IObuff + STRLEN(IObuff);
2842 while (*s != NUL && d - IObuff < IOSIZE - 7)
2843 {
2844 if (
2845#ifdef FEAT_MBYTE
2846 enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) :
2847#endif
2848 *s == 0xa0)
2849 {
2850 s +=
2851#ifdef FEAT_MBYTE
2852 enc_utf8 ? 2 :
2853#endif
2854 1;
2855 STRCPY(d, "<a0>");
2856 d += 4;
2857 }
2858 else
2859 MB_COPY_CHAR(s, d);
2860 }
2861 *d = NUL;
2862}
2863
2864/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002865 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002866 * Start of the name can be found at eap->cmd.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002867 * Returns pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002868 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002869 * Returns NULL for an ambiguous user command.
2870 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002871 static char_u *
2872find_command(eap, full)
2873 exarg_T *eap;
Bram Moolenaar78a15312009-05-15 19:33:18 +00002874 int *full UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002875{
2876 int len;
2877 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00002878 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002879
2880 /*
2881 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00002882 * Exceptions:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002883 * - the 'k' command can directly be followed by any character.
2884 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
2885 * but :sre[wind] is another command, as are :scrip[tnames],
2886 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00002887 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002888 */
2889 p = eap->cmd;
2890 if (*p == 'k')
2891 {
2892 eap->cmdidx = CMD_k;
2893 ++p;
2894 }
2895 else if (p[0] == 's'
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002896 && ((p[1] == 'c' && p[2] != 's' && p[2] != 'r'
2897 && p[3] != 'i' && p[4] != 'p')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002898 || p[1] == 'g'
2899 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
2900 || p[1] == 'I'
2901 || (p[1] == 'r' && p[2] != 'e')))
2902 {
2903 eap->cmdidx = CMD_substitute;
2904 ++p;
2905 }
2906 else
2907 {
2908 while (ASCII_ISALPHA(*p))
2909 ++p;
Bram Moolenaarb6590522010-07-21 16:00:43 +02002910 /* for python 3.x support ":py3", ":python3", ":py3file", etc. */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002911 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaarb6590522010-07-21 16:00:43 +02002912 while (ASCII_ISALNUM(*p))
2913 ++p;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02002914
Bram Moolenaar071d4272004-06-13 20:20:40 +00002915 /* check for non-alpha command */
2916 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
2917 ++p;
2918 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00002919 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
2920 {
2921 /* Check for ":dl", ":dell", etc. to ":deletel": that's
2922 * :delete with the 'l' flag. Same for 'p'. */
2923 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00002924 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00002925 break;
2926 if (i == len - 1)
2927 {
2928 --len;
2929 if (p[-1] == 'l')
2930 eap->flags |= EXFLAG_LIST;
2931 else
2932 eap->flags |= EXFLAG_PRINT;
2933 }
2934 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002935
2936 if (ASCII_ISLOWER(*eap->cmd))
2937 eap->cmdidx = cmdidxs[CharOrdLow(*eap->cmd)];
2938 else
2939 eap->cmdidx = cmdidxs[26];
2940
2941 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
2942 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
2943 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
2944 (size_t)len) == 0)
2945 {
2946#ifdef FEAT_EVAL
2947 if (full != NULL
2948 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
2949 *full = TRUE;
2950#endif
2951 break;
2952 }
2953
2954#ifdef FEAT_USR_CMDS
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01002955 /* Look for a user defined command as a last resort. Let ":Print" be
2956 * overruled by a user defined command. */
2957 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
2958 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002959 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002960 /* User defined commands may contain digits. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002961 while (ASCII_ISALNUM(*p))
2962 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002963 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002964 }
2965#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002966 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002967 eap->cmdidx = CMD_SIZE;
2968 }
2969
2970 return p;
2971}
2972
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002973#ifdef FEAT_USR_CMDS
2974/*
2975 * Search for a user command that matches "eap->cmd".
2976 * Return cmdidx in "eap->cmdidx", flags in "eap->argt", idx in "eap->useridx".
2977 * Return a pointer to just after the command.
2978 * Return NULL if there is no matching command.
2979 */
2980 static char_u *
2981find_ucmd(eap, p, full, xp, compl)
2982 exarg_T *eap;
2983 char_u *p; /* end of the command (possibly including count) */
2984 int *full; /* set to TRUE for a full match */
2985 expand_T *xp; /* used for completion, NULL otherwise */
2986 int *compl; /* completion flags or NULL */
2987{
2988 int len = (int)(p - eap->cmd);
2989 int j, k, matchlen = 0;
2990 ucmd_T *uc;
2991 int found = FALSE;
2992 int possible = FALSE;
2993 char_u *cp, *np; /* Point into typed cmd and test name */
2994 garray_T *gap;
2995 int amb_local = FALSE; /* Found ambiguous buffer-local command,
2996 only full match global is accepted. */
2997
2998 /*
2999 * Look for buffer-local user commands first, then global ones.
3000 */
3001 gap = &curbuf->b_ucmds;
3002 for (;;)
3003 {
3004 for (j = 0; j < gap->ga_len; ++j)
3005 {
3006 uc = USER_CMD_GA(gap, j);
3007 cp = eap->cmd;
3008 np = uc->uc_name;
3009 k = 0;
3010 while (k < len && *np != NUL && *cp++ == *np++)
3011 k++;
3012 if (k == len || (*np == NUL && vim_isdigit(eap->cmd[k])))
3013 {
3014 /* If finding a second match, the command is ambiguous. But
3015 * not if a buffer-local command wasn't a full match and a
3016 * global command is a full match. */
3017 if (k == len && found && *np != NUL)
3018 {
3019 if (gap == &ucmds)
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003020 return NULL;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003021 amb_local = TRUE;
3022 }
3023
3024 if (!found || (k == len && *np == NUL))
3025 {
3026 /* If we matched up to a digit, then there could
3027 * be another command including the digit that we
3028 * should use instead.
3029 */
3030 if (k == len)
3031 found = TRUE;
3032 else
3033 possible = TRUE;
3034
3035 if (gap == &ucmds)
3036 eap->cmdidx = CMD_USER;
3037 else
3038 eap->cmdidx = CMD_USER_BUF;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003039 eap->argt = (long)uc->uc_argt;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003040 eap->useridx = j;
3041
3042# ifdef FEAT_CMDL_COMPL
3043 if (compl != NULL)
3044 *compl = uc->uc_compl;
3045# ifdef FEAT_EVAL
3046 if (xp != NULL)
3047 {
3048 xp->xp_arg = uc->uc_compl_arg;
3049 xp->xp_scriptID = uc->uc_scriptID;
3050 }
3051# endif
3052# endif
3053 /* Do not search for further abbreviations
3054 * if this is an exact match. */
3055 matchlen = k;
3056 if (k == len && *np == NUL)
3057 {
3058 if (full != NULL)
3059 *full = TRUE;
3060 amb_local = FALSE;
3061 break;
3062 }
3063 }
3064 }
3065 }
3066
3067 /* Stop if we found a full match or searched all. */
3068 if (j < gap->ga_len || gap == &ucmds)
3069 break;
3070 gap = &ucmds;
3071 }
3072
3073 /* Only found ambiguous matches. */
3074 if (amb_local)
3075 {
3076 if (xp != NULL)
3077 xp->xp_context = EXPAND_UNSUCCESSFUL;
3078 return NULL;
3079 }
3080
3081 /* The match we found may be followed immediately by a number. Move "p"
3082 * back to point to it. */
3083 if (found || possible)
3084 return p + (matchlen - len);
3085 return p;
3086}
3087#endif
3088
Bram Moolenaar071d4272004-06-13 20:20:40 +00003089#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003090static struct cmdmod
3091{
3092 char *name;
3093 int minlen;
3094 int has_count; /* :123verbose :3tab */
3095} cmdmods[] = {
3096 {"aboveleft", 3, FALSE},
3097 {"belowright", 3, FALSE},
3098 {"botright", 2, FALSE},
3099 {"browse", 3, FALSE},
3100 {"confirm", 4, FALSE},
3101 {"hide", 3, FALSE},
3102 {"keepalt", 5, FALSE},
3103 {"keepjumps", 5, FALSE},
3104 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01003105 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003106 {"leftabove", 5, FALSE},
3107 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003108 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003109 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003110 {"rightbelow", 6, FALSE},
3111 {"sandbox", 3, FALSE},
3112 {"silent", 3, FALSE},
3113 {"tab", 3, TRUE},
3114 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003115 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003116 {"verbose", 4, TRUE},
3117 {"vertical", 4, FALSE},
3118};
3119
3120/*
3121 * Return length of a command modifier (including optional count).
3122 * Return zero when it's not a modifier.
3123 */
3124 int
3125modifier_len(cmd)
3126 char_u *cmd;
3127{
3128 int i, j;
3129 char_u *p = cmd;
3130
3131 if (VIM_ISDIGIT(*cmd))
3132 p = skipwhite(skipdigits(cmd));
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003133 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003134 {
3135 for (j = 0; p[j] != NUL; ++j)
3136 if (p[j] != cmdmods[i].name[j])
3137 break;
Bram Moolenaar2d473ab2013-06-12 17:12:24 +02003138 if (!ASCII_ISALPHA(p[j]) && j >= cmdmods[i].minlen
Bram Moolenaared53fb92007-11-24 20:50:24 +00003139 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003140 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003141 }
3142 return 0;
3143}
3144
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145/*
3146 * Return > 0 if an Ex command "name" exists.
3147 * Return 2 if there is an exact match.
3148 * Return 3 if there is an ambiguous match.
3149 */
3150 int
3151cmd_exists(name)
3152 char_u *name;
3153{
3154 exarg_T ea;
3155 int full = FALSE;
3156 int i;
3157 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003158 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003159
3160 /* Check command modifiers. */
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003161 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003162 {
3163 for (j = 0; name[j] != NUL; ++j)
3164 if (name[j] != cmdmods[i].name[j])
3165 break;
3166 if (name[j] == NUL && j >= cmdmods[i].minlen)
3167 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3168 }
3169
Bram Moolenaara9587612006-05-04 21:47:50 +00003170 /* Check built-in commands and user defined commands.
3171 * For ":2match" and ":3match" we need to skip the number. */
3172 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003174 p = find_command(&ea, &full);
3175 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003176 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003177 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3178 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003179 if (*skipwhite(p) != NUL)
3180 return 0; /* trailing garbage */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003181 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3182}
3183#endif
3184
3185/*
3186 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
3187 * we don't need/want deleted. Maybe this could be done better if we didn't
3188 * repeat all this stuff. The only problem is that they may not stay
3189 * perfectly compatible with each other, but then the command line syntax
3190 * probably won't change that much -- webb.
3191 */
3192 char_u *
3193set_one_cmd_context(xp, buff)
3194 expand_T *xp;
3195 char_u *buff; /* buffer for command string */
3196{
3197 char_u *p;
3198 char_u *cmd, *arg;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003199 int len = 0;
3200 exarg_T ea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3202 int compl = EXPAND_NOTHING;
3203#endif
3204#ifdef FEAT_CMDL_COMPL
3205 int delim;
3206#endif
3207 int forceit = FALSE;
3208 int usefilter = FALSE; /* filter instead of file name */
3209
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003210 ExpandInit(xp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211 xp->xp_pattern = buff;
3212 xp->xp_context = EXPAND_COMMANDS; /* Default until we get past command */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003213 ea.argt = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003214
3215/*
3216 * 2. skip comment lines and leading space, colons or bars
3217 */
3218 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
3219 ;
3220 xp->xp_pattern = cmd;
3221
3222 if (*cmd == NUL)
3223 return NULL;
3224 if (*cmd == '"') /* ignore comment lines */
3225 {
3226 xp->xp_context = EXPAND_NOTHING;
3227 return NULL;
3228 }
3229
3230/*
3231 * 3. parse a range specifier of the form: addr [,addr] [;addr] ..
3232 */
3233 cmd = skip_range(cmd, &xp->xp_context);
3234
3235/*
3236 * 4. parse command
3237 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238 xp->xp_pattern = cmd;
3239 if (*cmd == NUL)
3240 return NULL;
3241 if (*cmd == '"')
3242 {
3243 xp->xp_context = EXPAND_NOTHING;
3244 return NULL;
3245 }
3246
3247 if (*cmd == '|' || *cmd == '\n')
3248 return cmd + 1; /* There's another command */
3249
3250 /*
3251 * Isolate the command and search for it in the command table.
3252 * Exceptions:
3253 * - the 'k' command can directly be followed by any character, but
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003254 * do accept "keepmarks", "keepalt" and "keepjumps".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
3256 */
3257 if (*cmd == 'k' && cmd[1] != 'e')
3258 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003259 ea.cmdidx = CMD_k;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003260 p = cmd + 1;
3261 }
3262 else
3263 {
3264 p = cmd;
3265 while (ASCII_ISALPHA(*p) || *p == '*') /* Allow * wild card */
3266 ++p;
3267 /* check for non-alpha command */
3268 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3269 ++p;
Bram Moolenaar9f5d6002013-06-02 19:22:13 +02003270 /* for python 3.x: ":py3*" commands completion */
3271 if (cmd[0] == 'p' && cmd[1] == 'y' && p == cmd + 2 && *p == '3')
Bram Moolenaar893b2d72013-12-11 17:44:38 +01003272 {
Bram Moolenaar9f5d6002013-06-02 19:22:13 +02003273 ++p;
Bram Moolenaar893b2d72013-12-11 17:44:38 +01003274 while (ASCII_ISALPHA(*p) || *p == '*')
3275 ++p;
3276 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003277 len = (int)(p - cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003279 if (len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280 {
3281 xp->xp_context = EXPAND_UNSUCCESSFUL;
3282 return NULL;
3283 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003284 for (ea.cmdidx = (cmdidx_T)0; (int)ea.cmdidx < (int)CMD_SIZE;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01003285 ea.cmdidx = (cmdidx_T)((int)ea.cmdidx + 1))
3286 if (STRNCMP(cmdnames[(int)ea.cmdidx].cmd_name, cmd,
3287 (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003288 break;
3289
3290#ifdef FEAT_USR_CMDS
3291 if (cmd[0] >= 'A' && cmd[0] <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292 while (ASCII_ISALNUM(*p) || *p == '*') /* Allow * wild card */
3293 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003294#endif
3295 }
3296
3297 /*
3298 * If the cursor is touching the command, and it ends in an alpha-numeric
3299 * character, complete the command name.
3300 */
3301 if (*p == NUL && ASCII_ISALNUM(p[-1]))
3302 return NULL;
3303
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003304 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305 {
3306 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
3307 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003308 ea.cmdidx = CMD_substitute;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309 p = cmd + 1;
3310 }
3311#ifdef FEAT_USR_CMDS
3312 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
3313 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003314 ea.cmd = cmd;
3315 p = find_ucmd(&ea, p, NULL, xp,
3316# if defined(FEAT_CMDL_COMPL)
3317 &compl
3318# else
3319 NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320# endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003321 );
Bram Moolenaarebefac62005-12-28 22:39:57 +00003322 if (p == NULL)
3323 ea.cmdidx = CMD_SIZE; /* ambiguous user command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324 }
3325#endif
3326 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003327 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328 {
3329 /* Not still touching the command and it was an illegal one */
3330 xp->xp_context = EXPAND_UNSUCCESSFUL;
3331 return NULL;
3332 }
3333
3334 xp->xp_context = EXPAND_NOTHING; /* Default now that we're past command */
3335
3336 if (*p == '!') /* forced commands */
3337 {
3338 forceit = TRUE;
3339 ++p;
3340 }
3341
3342/*
3343 * 5. parse arguments
3344 */
3345#ifdef FEAT_USR_CMDS
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003346 if (!USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003347#endif
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003348 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349
3350 arg = skipwhite(p);
3351
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003352 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353 {
3354 if (*arg == '>') /* append */
3355 {
3356 if (*++arg == '>')
3357 ++arg;
3358 arg = skipwhite(arg);
3359 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003360 else if (*arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361 {
3362 ++arg;
3363 usefilter = TRUE;
3364 }
3365 }
3366
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003367 if (ea.cmdidx == CMD_read)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368 {
3369 usefilter = forceit; /* :r! filter if forced */
3370 if (*arg == '!') /* :r !filter */
3371 {
3372 ++arg;
3373 usefilter = TRUE;
3374 }
3375 }
3376
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003377 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378 {
3379 while (*arg == *cmd) /* allow any number of '>' or '<' */
3380 ++arg;
3381 arg = skipwhite(arg);
3382 }
3383
3384 /* Does command allow "+command"? */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003385 if ((ea.argt & EDITCMD) && !usefilter && *arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386 {
3387 /* Check if we're in the +command */
3388 p = arg + 1;
3389 arg = skip_cmd_arg(arg, FALSE);
3390
3391 /* Still touching the command after '+'? */
3392 if (*arg == NUL)
3393 return p;
3394
3395 /* Skip space(s) after +command to get to the real argument */
3396 arg = skipwhite(arg);
3397 }
3398
3399 /*
3400 * Check for '|' to separate commands and '"' to start comments.
3401 * Don't do this for ":read !cmd" and ":write !cmd".
3402 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003403 if ((ea.argt & TRLBAR) && !usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404 {
3405 p = arg;
3406 /* ":redir @" is not the start of a comment */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003407 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408 p += 2;
3409 while (*p)
3410 {
3411 if (*p == Ctrl_V)
3412 {
3413 if (p[1] != NUL)
3414 ++p;
3415 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003416 else if ( (*p == '"' && !(ea.argt & NOTRLCOM))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003417 || *p == '|' || *p == '\n')
3418 {
3419 if (*(p - 1) != '\\')
3420 {
3421 if (*p == '|' || *p == '\n')
3422 return p + 1;
3423 return NULL; /* It's a comment */
3424 }
3425 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003426 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427 }
3428 }
3429
3430 /* no arguments allowed */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003431 if (!(ea.argt & EXTRA) && *arg != NUL &&
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432 vim_strchr((char_u *)"|\"", *arg) == NULL)
3433 return NULL;
3434
3435 /* Find start of last argument (argument just before cursor): */
Bram Moolenaar848f8762012-07-25 17:22:23 +02003436 p = buff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437 xp->xp_pattern = p;
Bram Moolenaar09168a72012-08-02 21:24:42 +02003438 len = (int)STRLEN(buff);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003439 while (*p && p < buff + len)
3440 {
3441 if (*p == ' ' || *p == TAB)
3442 {
3443 /* argument starts after a space */
3444 xp->xp_pattern = ++p;
3445 }
3446 else
3447 {
3448 if (*p == '\\' && *(p + 1) != NUL)
3449 ++p; /* skip over escaped character */
3450 mb_ptr_adv(p);
3451 }
3452 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003454 if (ea.argt & XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003456 int c;
3457 int in_quote = FALSE;
3458 char_u *bow = NULL; /* Beginning of word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459
3460 /*
3461 * Allow spaces within back-quotes to count as part of the argument
3462 * being expanded.
3463 */
3464 xp->xp_pattern = skipwhite(arg);
Bram Moolenaar6529c102007-08-18 15:47:34 +00003465 p = xp->xp_pattern;
3466 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003468#ifdef FEAT_MBYTE
3469 if (has_mbyte)
3470 c = mb_ptr2char(p);
3471 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472#endif
Bram Moolenaar6529c102007-08-18 15:47:34 +00003473 c = *p;
3474 if (c == '\\' && p[1] != NUL)
3475 ++p;
3476 else if (c == '`')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477 {
3478 if (!in_quote)
3479 {
3480 xp->xp_pattern = p;
3481 bow = p + 1;
3482 }
3483 in_quote = !in_quote;
3484 }
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003485 /* An argument can contain just about everything, except
3486 * characters that end the command and white space. */
3487 else if (c == '|' || c == '\n' || c == '"' || (vim_iswhite(c)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003488#ifdef SPACE_IN_FILENAME
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003489 && (!(ea.argt & NOSPC) || usefilter)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003490#endif
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003491 ))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003492 {
Bram Moolenaarcf5a5b82008-02-26 20:30:12 +00003493 len = 0; /* avoid getting stuck when space is in 'isfname' */
Bram Moolenaar6529c102007-08-18 15:47:34 +00003494 while (*p != NUL)
3495 {
3496#ifdef FEAT_MBYTE
3497 if (has_mbyte)
3498 c = mb_ptr2char(p);
3499 else
3500#endif
3501 c = *p;
Bram Moolenaardd87969c2007-08-21 13:07:12 +00003502 if (c == '`' || vim_isfilec_or_wc(c))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003503 break;
3504#ifdef FEAT_MBYTE
3505 if (has_mbyte)
3506 len = (*mb_ptr2len)(p);
3507 else
3508#endif
3509 len = 1;
3510 mb_ptr_adv(p);
3511 }
3512 if (in_quote)
3513 bow = p;
3514 else
3515 xp->xp_pattern = p;
3516 p -= len;
3517 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003518 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003519 }
3520
3521 /*
3522 * If we are still inside the quotes, and we passed a space, just
3523 * expand from there.
3524 */
3525 if (bow != NULL && in_quote)
3526 xp->xp_pattern = bow;
3527 xp->xp_context = EXPAND_FILES;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003528
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003529 /* For a shell command more chars need to be escaped. */
3530 if (usefilter || ea.cmdidx == CMD_bang)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003531 {
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003532#ifndef BACKSLASH_IN_FILENAME
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003533 xp->xp_shell = TRUE;
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003534#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003535 /* When still after the command name expand executables. */
3536 if (xp->xp_pattern == skipwhite(arg))
Bram Moolenaar5c5b0942007-05-06 12:07:59 +00003537 xp->xp_context = EXPAND_SHELLCMD;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003538 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003539
3540 /* Check for environment variable */
3541 if (*xp->xp_pattern == '$'
3542#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
3543 || *xp->xp_pattern == '%'
3544#endif
3545 )
3546 {
3547 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
3548 if (!vim_isIDc(*p))
3549 break;
3550 if (*p == NUL)
3551 {
3552 xp->xp_context = EXPAND_ENV_VARS;
3553 ++xp->xp_pattern;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003554#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3555 /* Avoid that the assignment uses EXPAND_FILES again. */
Bram Moolenaara466c992005-07-09 21:03:22 +00003556 if (compl != EXPAND_USER_DEFINED && compl != EXPAND_USER_LIST)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003557 compl = EXPAND_ENV_VARS;
3558#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559 }
3560 }
Bram Moolenaar24305862012-08-15 14:05:05 +02003561#if defined(FEAT_CMDL_COMPL)
3562 /* Check for user names */
3563 if (*xp->xp_pattern == '~')
3564 {
3565 for (p = xp->xp_pattern + 1; *p != NUL && *p != '/'; ++p)
3566 ;
3567 /* Complete ~user only if it partially matches a user name.
3568 * A full match ~user<Tab> will be replaced by user's home
3569 * directory i.e. something like ~user<Tab> -> /home/user/ */
3570 if (*p == NUL && p > xp->xp_pattern + 1
3571 && match_user(xp->xp_pattern + 1) == 1)
3572 {
3573 xp->xp_context = EXPAND_USER;
3574 ++xp->xp_pattern;
3575 }
3576 }
3577#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578 }
3579
3580/*
3581 * 6. switch on command name
3582 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003583 switch (ea.cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003584 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003585 case CMD_find:
3586 case CMD_sfind:
3587 case CMD_tabfind:
Bram Moolenaarc24b6972010-08-16 22:34:29 +02003588 if (xp->xp_context == EXPAND_FILES)
3589 xp->xp_context = EXPAND_FILES_IN_PATH;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003590 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591 case CMD_cd:
3592 case CMD_chdir:
3593 case CMD_lcd:
3594 case CMD_lchdir:
3595 if (xp->xp_context == EXPAND_FILES)
3596 xp->xp_context = EXPAND_DIRECTORIES;
3597 break;
3598 case CMD_help:
3599 xp->xp_context = EXPAND_HELP;
3600 xp->xp_pattern = arg;
3601 break;
3602
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003603 /* Command modifiers: return the argument.
3604 * Also for commands with an argument that is a command. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605 case CMD_aboveleft:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003606 case CMD_argdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607 case CMD_belowright:
3608 case CMD_botright:
3609 case CMD_browse:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003610 case CMD_bufdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611 case CMD_confirm:
Bram Moolenaardf177f62005-02-22 08:39:57 +00003612 case CMD_debug:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613 case CMD_folddoclosed:
3614 case CMD_folddoopen:
3615 case CMD_hide:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003616 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003617 case CMD_keepjumps:
3618 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01003619 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003620 case CMD_leftabove:
3621 case CMD_lockmarks:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003622 case CMD_noautocmd:
3623 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624 case CMD_rightbelow:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003625 case CMD_sandbox:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626 case CMD_silent:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003627 case CMD_tab:
Bram Moolenaar70baa402013-06-16 16:14:03 +02003628 case CMD_tabdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629 case CMD_topleft:
3630 case CMD_verbose:
3631 case CMD_vertical:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003632 case CMD_windo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633 return arg;
3634
Bram Moolenaar4f688582007-07-24 12:34:30 +00003635#ifdef FEAT_CMDL_COMPL
3636# ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar071d4272004-06-13 20:20:40 +00003637 case CMD_match:
3638 if (*arg == NUL || !ends_excmd(*arg))
3639 {
Bram Moolenaar4f688582007-07-24 12:34:30 +00003640 /* also complete "None" */
3641 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642 arg = skipwhite(skiptowhite(arg));
3643 if (*arg != NUL)
3644 {
3645 xp->xp_context = EXPAND_NOTHING;
3646 arg = skip_regexp(arg + 1, *arg, p_magic, NULL);
3647 }
3648 }
3649 return find_nextcmd(arg);
Bram Moolenaar4f688582007-07-24 12:34:30 +00003650# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651
Bram Moolenaar071d4272004-06-13 20:20:40 +00003652/*
3653 * All completion for the +cmdline_compl feature goes here.
3654 */
3655
3656# ifdef FEAT_USR_CMDS
3657 case CMD_command:
3658 /* Check for attributes */
3659 while (*arg == '-')
3660 {
3661 arg++; /* Skip "-" */
3662 p = skiptowhite(arg);
3663 if (*p == NUL)
3664 {
3665 /* Cursor is still in the attribute */
3666 p = vim_strchr(arg, '=');
3667 if (p == NULL)
3668 {
3669 /* No "=", so complete attribute names */
3670 xp->xp_context = EXPAND_USER_CMD_FLAGS;
3671 xp->xp_pattern = arg;
3672 return NULL;
3673 }
3674
3675 /* For the -complete and -nargs attributes, we complete
3676 * their arguments as well.
3677 */
3678 if (STRNICMP(arg, "complete", p - arg) == 0)
3679 {
3680 xp->xp_context = EXPAND_USER_COMPLETE;
3681 xp->xp_pattern = p + 1;
3682 return NULL;
3683 }
3684 else if (STRNICMP(arg, "nargs", p - arg) == 0)
3685 {
3686 xp->xp_context = EXPAND_USER_NARGS;
3687 xp->xp_pattern = p + 1;
3688 return NULL;
3689 }
3690 return NULL;
3691 }
3692 arg = skipwhite(p);
3693 }
3694
3695 /* After the attributes comes the new command name */
3696 p = skiptowhite(arg);
3697 if (*p == NUL)
3698 {
3699 xp->xp_context = EXPAND_USER_COMMANDS;
3700 xp->xp_pattern = arg;
3701 break;
3702 }
3703
3704 /* And finally comes a normal command */
3705 return skipwhite(p);
3706
3707 case CMD_delcommand:
3708 xp->xp_context = EXPAND_USER_COMMANDS;
3709 xp->xp_pattern = arg;
3710 break;
3711# endif
3712
3713 case CMD_global:
3714 case CMD_vglobal:
3715 delim = *arg; /* get the delimiter */
3716 if (delim)
3717 ++arg; /* skip delimiter if there is one */
3718
3719 while (arg[0] != NUL && arg[0] != delim)
3720 {
3721 if (arg[0] == '\\' && arg[1] != NUL)
3722 ++arg;
3723 ++arg;
3724 }
3725 if (arg[0] != NUL)
3726 return arg + 1;
3727 break;
3728 case CMD_and:
3729 case CMD_substitute:
3730 delim = *arg;
3731 if (delim)
3732 {
3733 /* skip "from" part */
3734 ++arg;
3735 arg = skip_regexp(arg, delim, p_magic, NULL);
3736 }
3737 /* skip "to" part */
3738 while (arg[0] != NUL && arg[0] != delim)
3739 {
3740 if (arg[0] == '\\' && arg[1] != NUL)
3741 ++arg;
3742 ++arg;
3743 }
3744 if (arg[0] != NUL) /* skip delimiter */
3745 ++arg;
3746 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
3747 ++arg;
3748 if (arg[0] != NUL)
3749 return arg;
3750 break;
3751 case CMD_isearch:
3752 case CMD_dsearch:
3753 case CMD_ilist:
3754 case CMD_dlist:
3755 case CMD_ijump:
3756 case CMD_psearch:
3757 case CMD_djump:
3758 case CMD_isplit:
3759 case CMD_dsplit:
3760 arg = skipwhite(skipdigits(arg)); /* skip count */
3761 if (*arg == '/') /* Match regexp, not just whole words */
3762 {
3763 for (++arg; *arg && *arg != '/'; arg++)
3764 if (*arg == '\\' && arg[1] != NUL)
3765 arg++;
3766 if (*arg)
3767 {
3768 arg = skipwhite(arg + 1);
3769
3770 /* Check for trailing illegal characters */
3771 if (*arg && vim_strchr((char_u *)"|\"\n", *arg) == NULL)
3772 xp->xp_context = EXPAND_NOTHING;
3773 else
3774 return arg;
3775 }
3776 }
3777 break;
3778#ifdef FEAT_AUTOCMD
3779 case CMD_autocmd:
3780 return set_context_in_autocmd(xp, arg, FALSE);
3781
3782 case CMD_doautocmd:
Bram Moolenaar73a9d7b2008-11-06 16:16:44 +00003783 case CMD_doautoall:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003784 return set_context_in_autocmd(xp, arg, TRUE);
3785#endif
3786 case CMD_set:
3787 set_context_in_set_cmd(xp, arg, 0);
3788 break;
3789 case CMD_setglobal:
3790 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
3791 break;
3792 case CMD_setlocal:
3793 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
3794 break;
3795 case CMD_tag:
3796 case CMD_stag:
3797 case CMD_ptag:
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00003798 case CMD_ltag:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003799 case CMD_tselect:
3800 case CMD_stselect:
3801 case CMD_ptselect:
3802 case CMD_tjump:
3803 case CMD_stjump:
3804 case CMD_ptjump:
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003805 if (*p_wop != NUL)
3806 xp->xp_context = EXPAND_TAGS_LISTFILES;
3807 else
3808 xp->xp_context = EXPAND_TAGS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003809 xp->xp_pattern = arg;
3810 break;
3811 case CMD_augroup:
3812 xp->xp_context = EXPAND_AUGROUP;
3813 xp->xp_pattern = arg;
3814 break;
3815#ifdef FEAT_SYN_HL
3816 case CMD_syntax:
3817 set_context_in_syntax_cmd(xp, arg);
3818 break;
3819#endif
3820#ifdef FEAT_EVAL
3821 case CMD_let:
3822 case CMD_if:
3823 case CMD_elseif:
3824 case CMD_while:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00003825 case CMD_for:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003826 case CMD_echo:
3827 case CMD_echon:
3828 case CMD_execute:
3829 case CMD_echomsg:
3830 case CMD_echoerr:
3831 case CMD_call:
3832 case CMD_return:
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003833 set_context_for_expression(xp, arg, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003834 break;
3835
3836 case CMD_unlet:
3837 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3838 arg = xp->xp_pattern + 1;
3839 xp->xp_context = EXPAND_USER_VARS;
3840 xp->xp_pattern = arg;
3841 break;
3842
3843 case CMD_function:
3844 case CMD_delfunction:
3845 xp->xp_context = EXPAND_USER_FUNC;
3846 xp->xp_pattern = arg;
3847 break;
3848
3849 case CMD_echohl:
Bram Moolenaar4f688582007-07-24 12:34:30 +00003850 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851 break;
3852#endif
3853 case CMD_highlight:
3854 set_context_in_highlight_cmd(xp, arg);
3855 break;
Bram Moolenaarf4580d82009-03-18 11:52:53 +00003856#ifdef FEAT_CSCOPE
3857 case CMD_cscope:
Bram Moolenaar7bfef802009-04-22 14:25:01 +00003858 case CMD_lcscope:
3859 case CMD_scscope:
3860 set_context_in_cscope_cmd(xp, arg, ea.cmdidx);
Bram Moolenaarf4580d82009-03-18 11:52:53 +00003861 break;
3862#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00003863#ifdef FEAT_SIGNS
3864 case CMD_sign:
3865 set_context_in_sign_cmd(xp, arg);
3866 break;
3867#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003868#ifdef FEAT_LISTCMDS
3869 case CMD_bdelete:
3870 case CMD_bwipeout:
3871 case CMD_bunload:
3872 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3873 arg = xp->xp_pattern + 1;
3874 /*FALLTHROUGH*/
3875 case CMD_buffer:
3876 case CMD_sbuffer:
3877 case CMD_checktime:
3878 xp->xp_context = EXPAND_BUFFERS;
3879 xp->xp_pattern = arg;
3880 break;
3881#endif
3882#ifdef FEAT_USR_CMDS
3883 case CMD_USER:
3884 case CMD_USER_BUF:
3885 if (compl != EXPAND_NOTHING)
3886 {
3887 /* XFILE: file names are handled above */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003888 if (!(ea.argt & XFILE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889 {
3890# ifdef FEAT_MENU
3891 if (compl == EXPAND_MENUS)
3892 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
3893# endif
3894 if (compl == EXPAND_COMMANDS)
3895 return arg;
3896 if (compl == EXPAND_MAPPINGS)
3897 return set_context_in_map_cmd(xp, (char_u *)"map",
3898 arg, forceit, FALSE, FALSE, CMD_map);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003899 /* Find start of last argument. */
3900 p = arg;
3901 while (*p)
3902 {
3903 if (*p == ' ')
Bram Moolenaar848f8762012-07-25 17:22:23 +02003904 /* argument starts after a space */
3905 arg = p + 1;
Bram Moolenaara07c8312012-07-27 21:12:07 +02003906 else if (*p == '\\' && *(p + 1) != NUL)
3907 ++p; /* skip over escaped character */
3908 mb_ptr_adv(p);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003909 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003910 xp->xp_pattern = arg;
3911 }
3912 xp->xp_context = compl;
3913 }
3914 break;
3915#endif
3916 case CMD_map: case CMD_noremap:
3917 case CMD_nmap: case CMD_nnoremap:
3918 case CMD_vmap: case CMD_vnoremap:
3919 case CMD_omap: case CMD_onoremap:
3920 case CMD_imap: case CMD_inoremap:
3921 case CMD_cmap: case CMD_cnoremap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003922 case CMD_lmap: case CMD_lnoremap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02003923 case CMD_smap: case CMD_snoremap:
3924 case CMD_xmap: case CMD_xnoremap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003925 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003926 FALSE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927 case CMD_unmap:
3928 case CMD_nunmap:
3929 case CMD_vunmap:
3930 case CMD_ounmap:
3931 case CMD_iunmap:
3932 case CMD_cunmap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003933 case CMD_lunmap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02003934 case CMD_sunmap:
3935 case CMD_xunmap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003936 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003937 FALSE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938 case CMD_abbreviate: case CMD_noreabbrev:
3939 case CMD_cabbrev: case CMD_cnoreabbrev:
3940 case CMD_iabbrev: case CMD_inoreabbrev:
3941 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003942 TRUE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943 case CMD_unabbreviate:
3944 case CMD_cunabbrev:
3945 case CMD_iunabbrev:
3946 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003947 TRUE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003948#ifdef FEAT_MENU
3949 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
3950 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
3951 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
3952 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
3953 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
3954 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
3955 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
3956 case CMD_tmenu: case CMD_tunmenu:
3957 case CMD_popup: case CMD_tearoff: case CMD_emenu:
3958 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
3959#endif
3960
3961 case CMD_colorscheme:
3962 xp->xp_context = EXPAND_COLORS;
3963 xp->xp_pattern = arg;
3964 break;
3965
3966 case CMD_compiler:
3967 xp->xp_context = EXPAND_COMPILER;
3968 xp->xp_pattern = arg;
3969 break;
3970
Bram Moolenaar883f5d02010-06-21 06:24:34 +02003971 case CMD_ownsyntax:
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02003972 xp->xp_context = EXPAND_OWNSYNTAX;
3973 xp->xp_pattern = arg;
3974 break;
3975
3976 case CMD_setfiletype:
Bram Moolenaar883f5d02010-06-21 06:24:34 +02003977 xp->xp_context = EXPAND_FILETYPE;
3978 xp->xp_pattern = arg;
3979 break;
3980
Bram Moolenaar071d4272004-06-13 20:20:40 +00003981#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
3982 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
3983 case CMD_language:
Bram Moolenaara660dc82011-05-25 12:51:22 +02003984 p = skiptowhite(arg);
3985 if (*p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003986 {
3987 xp->xp_context = EXPAND_LANGUAGE;
3988 xp->xp_pattern = arg;
3989 }
3990 else
Bram Moolenaara660dc82011-05-25 12:51:22 +02003991 {
3992 if ( STRNCMP(arg, "messages", p - arg) == 0
3993 || STRNCMP(arg, "ctype", p - arg) == 0
3994 || STRNCMP(arg, "time", p - arg) == 0)
3995 {
3996 xp->xp_context = EXPAND_LOCALES;
3997 xp->xp_pattern = skipwhite(p);
3998 }
3999 else
4000 xp->xp_context = EXPAND_NOTHING;
4001 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002 break;
4003#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01004004#if defined(FEAT_PROFILE)
4005 case CMD_profile:
4006 set_context_in_profile_cmd(xp, arg);
4007 break;
4008#endif
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01004009 case CMD_behave:
4010 xp->xp_context = EXPAND_BEHAVE;
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004011 xp->xp_pattern = arg;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01004012 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004014#if defined(FEAT_CMDHIST)
4015 case CMD_history:
4016 xp->xp_context = EXPAND_HISTORY;
4017 xp->xp_pattern = arg;
4018 break;
4019#endif
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02004020#if defined(FEAT_PROFILE)
4021 case CMD_syntime:
4022 xp->xp_context = EXPAND_SYNTIME;
4023 xp->xp_pattern = arg;
4024 break;
4025#endif
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004026
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027#endif /* FEAT_CMDL_COMPL */
4028
4029 default:
4030 break;
4031 }
4032 return NULL;
4033}
4034
4035/*
4036 * skip a range specifier of the form: addr [,addr] [;addr] ..
4037 *
4038 * Backslashed delimiters after / or ? will be skipped, and commands will
4039 * not be expanded between /'s and ?'s or after "'".
4040 *
Bram Moolenaardf177f62005-02-22 08:39:57 +00004041 * Also skip white space and ":" characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042 * Returns the "cmd" pointer advanced to beyond the range.
4043 */
4044 char_u *
4045skip_range(cmd, ctx)
4046 char_u *cmd;
4047 int *ctx; /* pointer to xp_context or NULL */
4048{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004049 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004050
Bram Moolenaardf177f62005-02-22 08:39:57 +00004051 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052 {
4053 if (*cmd == '\'')
4054 {
4055 if (*++cmd == NUL && ctx != NULL)
4056 *ctx = EXPAND_NOTHING;
4057 }
4058 else if (*cmd == '/' || *cmd == '?')
4059 {
4060 delim = *cmd++;
4061 while (*cmd != NUL && *cmd != delim)
4062 if (*cmd++ == '\\' && *cmd != NUL)
4063 ++cmd;
4064 if (*cmd == NUL && ctx != NULL)
4065 *ctx = EXPAND_NOTHING;
4066 }
4067 if (*cmd != NUL)
4068 ++cmd;
4069 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004070
4071 /* Skip ":" and white space. */
4072 while (*cmd == ':')
4073 cmd = skipwhite(cmd + 1);
4074
Bram Moolenaar071d4272004-06-13 20:20:40 +00004075 return cmd;
4076}
4077
4078/*
4079 * get a single EX address
4080 *
4081 * Set ptr to the next character after the part that was interpreted.
4082 * Set ptr to NULL when an error is encountered.
4083 *
4084 * Return MAXLNUM when no Ex address was found.
4085 */
4086 static linenr_T
4087get_address(ptr, skip, to_other_file)
4088 char_u **ptr;
4089 int skip; /* only skip the address, don't use it */
4090 int to_other_file; /* flag: may jump to other file */
4091{
4092 int c;
4093 int i;
4094 long n;
4095 char_u *cmd;
4096 pos_T pos;
4097 pos_T *fp;
4098 linenr_T lnum;
4099
4100 cmd = skipwhite(*ptr);
4101 lnum = MAXLNUM;
4102 do
4103 {
4104 switch (*cmd)
4105 {
4106 case '.': /* '.' - Cursor position */
4107 ++cmd;
4108 lnum = curwin->w_cursor.lnum;
4109 break;
4110
4111 case '$': /* '$' - last line */
4112 ++cmd;
4113 lnum = curbuf->b_ml.ml_line_count;
4114 break;
4115
4116 case '\'': /* ''' - mark */
4117 if (*++cmd == NUL)
4118 {
4119 cmd = NULL;
4120 goto error;
4121 }
4122 if (skip)
4123 ++cmd;
4124 else
4125 {
4126 /* Only accept a mark in another file when it is
4127 * used by itself: ":'M". */
4128 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
4129 ++cmd;
4130 if (fp == (pos_T *)-1)
4131 /* Jumped to another file. */
4132 lnum = curwin->w_cursor.lnum;
4133 else
4134 {
4135 if (check_mark(fp) == FAIL)
4136 {
4137 cmd = NULL;
4138 goto error;
4139 }
4140 lnum = fp->lnum;
4141 }
4142 }
4143 break;
4144
4145 case '/':
4146 case '?': /* '/' or '?' - search */
4147 c = *cmd++;
4148 if (skip) /* skip "/pat/" */
4149 {
4150 cmd = skip_regexp(cmd, c, (int)p_magic, NULL);
4151 if (*cmd == c)
4152 ++cmd;
4153 }
4154 else
4155 {
4156 pos = curwin->w_cursor; /* save curwin->w_cursor */
4157 /*
4158 * When '/' or '?' follows another address, start
4159 * from there.
4160 */
4161 if (lnum != MAXLNUM)
4162 curwin->w_cursor.lnum = lnum;
4163 /*
4164 * Start a forward search at the end of the line.
4165 * Start a backward search at the start of the line.
4166 * This makes sure we never match in the current
4167 * line, and can match anywhere in the
4168 * next/previous line.
4169 */
4170 if (c == '/')
4171 curwin->w_cursor.col = MAXCOL;
4172 else
4173 curwin->w_cursor.col = 0;
4174 searchcmdlen = 0;
4175 if (!do_search(NULL, c, cmd, 1L,
Bram Moolenaaraad86642008-03-10 20:34:59 +00004176 SEARCH_HIS | SEARCH_MSG, NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177 {
4178 curwin->w_cursor = pos;
4179 cmd = NULL;
4180 goto error;
4181 }
4182 lnum = curwin->w_cursor.lnum;
4183 curwin->w_cursor = pos;
4184 /* adjust command string pointer */
4185 cmd += searchcmdlen;
4186 }
4187 break;
4188
4189 case '\\': /* "\?", "\/" or "\&", repeat search */
4190 ++cmd;
4191 if (*cmd == '&')
4192 i = RE_SUBST;
4193 else if (*cmd == '?' || *cmd == '/')
4194 i = RE_SEARCH;
4195 else
4196 {
4197 EMSG(_(e_backslash));
4198 cmd = NULL;
4199 goto error;
4200 }
4201
4202 if (!skip)
4203 {
4204 /*
4205 * When search follows another address, start from
4206 * there.
4207 */
4208 if (lnum != MAXLNUM)
4209 pos.lnum = lnum;
4210 else
4211 pos.lnum = curwin->w_cursor.lnum;
4212
4213 /*
4214 * Start the search just like for the above
4215 * do_search().
4216 */
4217 if (*cmd != '?')
4218 pos.col = MAXCOL;
4219 else
4220 pos.col = 0;
4221 if (searchit(curwin, curbuf, &pos,
4222 *cmd == '?' ? BACKWARD : FORWARD,
Bram Moolenaaraad86642008-03-10 20:34:59 +00004223 (char_u *)"", 1L, SEARCH_MSG,
Bram Moolenaar76929292008-01-06 19:07:36 +00004224 i, (linenr_T)0, NULL) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225 lnum = pos.lnum;
4226 else
4227 {
4228 cmd = NULL;
4229 goto error;
4230 }
4231 }
4232 ++cmd;
4233 break;
4234
4235 default:
4236 if (VIM_ISDIGIT(*cmd)) /* absolute line number */
4237 lnum = getdigits(&cmd);
4238 }
4239
4240 for (;;)
4241 {
4242 cmd = skipwhite(cmd);
4243 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4244 break;
4245
4246 if (lnum == MAXLNUM)
4247 lnum = curwin->w_cursor.lnum; /* "+1" is same as ".+1" */
4248 if (VIM_ISDIGIT(*cmd))
4249 i = '+'; /* "number" is same as "+number" */
4250 else
4251 i = *cmd++;
4252 if (!VIM_ISDIGIT(*cmd)) /* '+' is '+1', but '+0' is not '+1' */
4253 n = 1;
4254 else
4255 n = getdigits(&cmd);
4256 if (i == '-')
4257 lnum -= n;
4258 else
4259 lnum += n;
4260 }
4261 } while (*cmd == '/' || *cmd == '?');
4262
4263error:
4264 *ptr = cmd;
4265 return lnum;
4266}
4267
4268/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004269 * Get flags from an Ex command argument.
4270 */
4271 static void
4272get_flags(eap)
4273 exarg_T *eap;
4274{
4275 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4276 {
4277 if (*eap->arg == 'l')
4278 eap->flags |= EXFLAG_LIST;
4279 else if (*eap->arg == 'p')
4280 eap->flags |= EXFLAG_PRINT;
4281 else
4282 eap->flags |= EXFLAG_NR;
4283 eap->arg = skipwhite(eap->arg + 1);
4284 }
4285}
4286
4287/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004288 * Function called for command which is Not Implemented. NI!
4289 */
4290 void
4291ex_ni(eap)
4292 exarg_T *eap;
4293{
4294 if (!eap->skip)
4295 eap->errmsg = (char_u *)N_("E319: Sorry, the command is not available in this version");
4296}
4297
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004298#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004299/*
4300 * Function called for script command which is Not Implemented. NI!
4301 * Skips over ":perl <<EOF" constructs.
4302 */
4303 static void
4304ex_script_ni(eap)
4305 exarg_T *eap;
4306{
4307 if (!eap->skip)
4308 ex_ni(eap);
4309 else
4310 vim_free(script_get(eap, eap->arg));
4311}
4312#endif
4313
4314/*
4315 * Check range in Ex command for validity.
4316 * Return NULL when valid, error message when invalid.
4317 */
4318 static char_u *
4319invalid_range(eap)
4320 exarg_T *eap;
4321{
4322 if ( eap->line1 < 0
4323 || eap->line2 < 0
4324 || eap->line1 > eap->line2
4325 || ((eap->argt & RANGE)
4326 && !(eap->argt & NOTADR)
4327 && eap->line2 > curbuf->b_ml.ml_line_count
4328#ifdef FEAT_DIFF
4329 + (eap->cmdidx == CMD_diffget)
4330#endif
4331 ))
4332 return (char_u *)_(e_invrange);
4333 return NULL;
4334}
4335
4336/*
4337 * Correct the range for zero line number, if required.
4338 */
4339 static void
4340correct_range(eap)
4341 exarg_T *eap;
4342{
4343 if (!(eap->argt & ZEROR)) /* zero in range not allowed */
4344 {
4345 if (eap->line1 == 0)
4346 eap->line1 = 1;
4347 if (eap->line2 == 0)
4348 eap->line2 = 1;
4349 }
4350}
4351
Bram Moolenaar748bf032005-02-02 23:04:36 +00004352#ifdef FEAT_QUICKFIX
4353static char_u *skip_grep_pat __ARGS((exarg_T *eap));
4354
4355/*
4356 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4357 * pattern. Otherwise return eap->arg.
4358 */
4359 static char_u *
4360skip_grep_pat(eap)
4361 exarg_T *eap;
4362{
4363 char_u *p = eap->arg;
4364
Bram Moolenaara37420f2006-02-04 22:37:47 +00004365 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4366 || eap->cmdidx == CMD_vimgrepadd
4367 || eap->cmdidx == CMD_lvimgrepadd
4368 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004369 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004370 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004371 if (p == NULL)
4372 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004373 }
4374 return p;
4375}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004376
4377/*
4378 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4379 * in the command line, so that things like % get expanded.
4380 */
4381 static char_u *
4382replace_makeprg(eap, p, cmdlinep)
4383 exarg_T *eap;
4384 char_u *p;
4385 char_u **cmdlinep;
4386{
4387 char_u *new_cmdline;
4388 char_u *program;
4389 char_u *pos;
4390 char_u *ptr;
4391 int len;
4392 int i;
4393
4394 /*
4395 * Don't do it when ":vimgrep" is used for ":grep".
4396 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004397 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4398 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4399 || eap->cmdidx == CMD_grepadd
4400 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004401 && !grep_internal(eap->cmdidx))
4402 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004403 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4404 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004405 {
4406 if (*curbuf->b_p_gp == NUL)
4407 program = p_gp;
4408 else
4409 program = curbuf->b_p_gp;
4410 }
4411 else
4412 {
4413 if (*curbuf->b_p_mp == NUL)
4414 program = p_mp;
4415 else
4416 program = curbuf->b_p_mp;
4417 }
4418
4419 p = skipwhite(p);
4420
4421 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4422 {
4423 /* replace $* by given arguments */
4424 i = 1;
4425 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4426 ++i;
4427 len = (int)STRLEN(p);
4428 new_cmdline = alloc((int)(STRLEN(program) + i * (len - 2) + 1));
4429 if (new_cmdline == NULL)
4430 return NULL; /* out of memory */
4431 ptr = new_cmdline;
4432 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4433 {
4434 i = (int)(pos - program);
4435 STRNCPY(ptr, program, i);
4436 STRCPY(ptr += i, p);
4437 ptr += len;
4438 program = pos + 2;
4439 }
4440 STRCPY(ptr, program);
4441 }
4442 else
4443 {
4444 new_cmdline = alloc((int)(STRLEN(program) + STRLEN(p) + 2));
4445 if (new_cmdline == NULL)
4446 return NULL; /* out of memory */
4447 STRCPY(new_cmdline, program);
4448 STRCAT(new_cmdline, " ");
4449 STRCAT(new_cmdline, p);
4450 }
4451 msg_make(p);
4452
4453 /* 'eap->cmd' is not set here, because it is not used at CMD_make */
4454 vim_free(*cmdlinep);
4455 *cmdlinep = new_cmdline;
4456 p = new_cmdline;
4457 }
4458 return p;
4459}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004460#endif
4461
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462/*
4463 * Expand file name in Ex command argument.
4464 * Return FAIL for failure, OK otherwise.
4465 */
4466 int
4467expand_filename(eap, cmdlinep, errormsgp)
4468 exarg_T *eap;
4469 char_u **cmdlinep;
4470 char_u **errormsgp;
4471{
4472 int has_wildcards; /* need to expand wildcards */
4473 char_u *repl;
4474 int srclen;
4475 char_u *p;
4476 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004477 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004478
Bram Moolenaar748bf032005-02-02 23:04:36 +00004479#ifdef FEAT_QUICKFIX
4480 /* Skip a regexp pattern for ":vimgrep[add] pat file..." */
4481 p = skip_grep_pat(eap);
4482#else
4483 p = eap->arg;
4484#endif
4485
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486 /*
4487 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4488 * the file name contains a wildcard it should not cause expanding.
4489 * (it will be expanded anyway if there is a wildcard before replacing).
4490 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004491 has_wildcards = mch_has_wildcard(p);
4492 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004494#ifdef FEAT_EVAL
4495 /* Skip over `=expr`, wildcards in it are not expanded. */
4496 if (p[0] == '`' && p[1] == '=')
4497 {
4498 p += 2;
4499 (void)skip_expr(&p);
4500 if (*p == '`')
4501 ++p;
4502 continue;
4503 }
4504#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004505 /*
4506 * Quick check if this cannot be the start of a special string.
4507 * Also removes backslash before '%', '#' and '<'.
4508 */
4509 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4510 {
4511 ++p;
4512 continue;
4513 }
4514
4515 /*
4516 * Try to find a match at this position.
4517 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004518 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
4519 errormsgp, &escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004520 if (*errormsgp != NULL) /* error detected */
4521 return FAIL;
4522 if (repl == NULL) /* no match found */
4523 {
4524 p += srclen;
4525 continue;
4526 }
4527
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004528 /* Wildcards won't be expanded below, the replacement is taken
4529 * literally. But do expand "~/file", "~user/file" and "$HOME/file". */
4530 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4531 {
4532 char_u *l = repl;
4533
4534 repl = expand_env_save(repl);
4535 vim_free(l);
4536 }
4537
Bram Moolenaar63b92542007-03-27 14:57:09 +00004538 /* Need to escape white space et al. with a backslash.
4539 * Don't do this for:
4540 * - replacement that already has been escaped: "##"
4541 * - shell commands (may have to use quotes instead).
4542 * - non-unix systems when there is a single argument (spaces don't
4543 * separate arguments then).
4544 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004545 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00004546 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00004547 && eap->cmdidx != CMD_bang
4548 && eap->cmdidx != CMD_make
Bram Moolenaara37420f2006-02-04 22:37:47 +00004549 && eap->cmdidx != CMD_lmake
Bram Moolenaar071d4272004-06-13 20:20:40 +00004550 && eap->cmdidx != CMD_grep
Bram Moolenaara37420f2006-02-04 22:37:47 +00004551 && eap->cmdidx != CMD_lgrep
Bram Moolenaar071d4272004-06-13 20:20:40 +00004552 && eap->cmdidx != CMD_grepadd
Bram Moolenaara37420f2006-02-04 22:37:47 +00004553 && eap->cmdidx != CMD_lgrepadd
Bram Moolenaar071d4272004-06-13 20:20:40 +00004554#ifndef UNIX
4555 && !(eap->argt & NOSPC)
4556#endif
4557 )
4558 {
4559 char_u *l;
4560#ifdef BACKSLASH_IN_FILENAME
4561 /* Don't escape a backslash here, because rem_backslash() doesn't
4562 * remove it later. */
4563 static char_u *nobslash = (char_u *)" \t\"|";
4564# define ESCAPE_CHARS nobslash
4565#else
4566# define ESCAPE_CHARS escape_chars
4567#endif
4568
4569 for (l = repl; *l; ++l)
4570 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
4571 {
4572 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
4573 if (l != NULL)
4574 {
4575 vim_free(repl);
4576 repl = l;
4577 }
4578 break;
4579 }
4580 }
4581
4582 /* For a shell command a '!' must be escaped. */
4583 if ((eap->usefilter || eap->cmdidx == CMD_bang)
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004584 && vim_strpbrk(repl, (char_u *)"!&;()<>") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004585 {
4586 char_u *l;
4587
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004588 l = vim_strsave_escaped(repl, (char_u *)"!&;()<>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004589 if (l != NULL)
4590 {
4591 vim_free(repl);
4592 repl = l;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004593 /* For a sh-like shell escape "!" another time. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004594 if (strstr((char *)p_sh, "sh") != NULL)
4595 {
4596 l = vim_strsave_escaped(repl, (char_u *)"!");
4597 if (l != NULL)
4598 {
4599 vim_free(repl);
4600 repl = l;
4601 }
4602 }
4603 }
4604 }
4605
4606 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
4607 vim_free(repl);
4608 if (p == NULL)
4609 return FAIL;
4610 }
4611
4612 /*
4613 * One file argument: Expand wildcards.
4614 * Don't do this with ":r !command" or ":w !command".
4615 */
4616 if ((eap->argt & NOSPC) && !eap->usefilter)
4617 {
4618 /*
4619 * May do this twice:
4620 * 1. Replace environment variables.
4621 * 2. Replace any other wildcards, remove backslashes.
4622 */
4623 for (n = 1; n <= 2; ++n)
4624 {
4625 if (n == 2)
4626 {
4627#ifdef UNIX
4628 /*
4629 * Only for Unix we check for more than one file name.
4630 * For other systems spaces are considered to be part
4631 * of the file name.
4632 * Only check here if there is no wildcard, otherwise
4633 * ExpandOne() will check for errors. This allows
4634 * ":e `ls ve*.c`" on Unix.
4635 */
4636 if (!has_wildcards)
4637 for (p = eap->arg; *p; ++p)
4638 {
4639 /* skip escaped characters */
4640 if (p[1] && (*p == '\\' || *p == Ctrl_V))
4641 ++p;
4642 else if (vim_iswhite(*p))
4643 {
4644 *errormsgp = (char_u *)_("E172: Only one file name allowed");
4645 return FAIL;
4646 }
4647 }
4648#endif
4649
4650 /*
4651 * Halve the number of backslashes (this is Vi compatible).
4652 * For Unix and OS/2, when wildcards are expanded, this is
4653 * done by ExpandOne() below.
4654 */
4655#if defined(UNIX) || defined(OS2)
4656 if (!has_wildcards)
4657#endif
4658 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004659 }
4660
4661 if (has_wildcards)
4662 {
4663 if (n == 1)
4664 {
4665 /*
4666 * First loop: May expand environment variables. This
4667 * can be done much faster with expand_env() than with
4668 * something else (e.g., calling a shell).
4669 * After expanding environment variables, check again
4670 * if there are still wildcards present.
4671 */
4672 if (vim_strchr(eap->arg, '$') != NULL
4673 || vim_strchr(eap->arg, '~') != NULL)
4674 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00004675 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004676 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004677 has_wildcards = mch_has_wildcard(NameBuff);
4678 p = NameBuff;
4679 }
4680 else
4681 p = NULL;
4682 }
4683 else /* n == 2 */
4684 {
4685 expand_T xpc;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004686 int options = WILD_LIST_NOTFOUND|WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004687
4688 ExpandInit(&xpc);
4689 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004690 if (p_wic)
4691 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004692 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01004693 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694 if (p == NULL)
4695 return FAIL;
4696 }
4697 if (p != NULL)
4698 {
4699 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
4700 p, cmdlinep);
4701 if (n == 2) /* p came from ExpandOne() */
4702 vim_free(p);
4703 }
4704 }
4705 }
4706 }
4707 return OK;
4708}
4709
4710/*
4711 * Replace part of the command line, keeping eap->cmd, eap->arg and
4712 * eap->nextcmd correct.
4713 * "src" points to the part that is to be replaced, of length "srclen".
4714 * "repl" is the replacement string.
4715 * Returns a pointer to the character after the replaced string.
4716 * Returns NULL for failure.
4717 */
4718 static char_u *
4719repl_cmdline(eap, src, srclen, repl, cmdlinep)
4720 exarg_T *eap;
4721 char_u *src;
4722 int srclen;
4723 char_u *repl;
4724 char_u **cmdlinep;
4725{
4726 int len;
4727 int i;
4728 char_u *new_cmdline;
4729
4730 /*
4731 * The new command line is build in new_cmdline[].
4732 * First allocate it.
4733 * Careful: a "+cmd" argument may have been NUL terminated.
4734 */
4735 len = (int)STRLEN(repl);
4736 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004737 if (eap->nextcmd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004738 i += (int)STRLEN(eap->nextcmd);/* add space for next command */
4739 if ((new_cmdline = alloc((unsigned)i)) == NULL)
4740 return NULL; /* out of memory! */
4741
4742 /*
4743 * Copy the stuff before the expanded part.
4744 * Copy the expanded stuff.
4745 * Copy what came after the expanded part.
4746 * Copy the next commands, if there are any.
4747 */
4748 i = (int)(src - *cmdlinep); /* length of part before match */
4749 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00004750
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751 mch_memmove(new_cmdline + i, repl, (size_t)len);
4752 i += len; /* remember the end of the string */
4753 STRCPY(new_cmdline + i, src + srclen);
4754 src = new_cmdline + i; /* remember where to continue */
4755
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004756 if (eap->nextcmd != NULL) /* append next command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004757 {
4758 i = (int)STRLEN(new_cmdline) + 1;
4759 STRCPY(new_cmdline + i, eap->nextcmd);
4760 eap->nextcmd = new_cmdline + i;
4761 }
4762 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
4763 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
4764 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
4765 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
4766 vim_free(*cmdlinep);
4767 *cmdlinep = new_cmdline;
4768
4769 return src;
4770}
4771
4772/*
4773 * Check for '|' to separate commands and '"' to start comments.
4774 */
4775 void
4776separate_nextcmd(eap)
4777 exarg_T *eap;
4778{
4779 char_u *p;
4780
Bram Moolenaar86b68352004-12-27 21:59:20 +00004781#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004782 p = skip_grep_pat(eap);
4783#else
4784 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004785#endif
4786
4787 for ( ; *p; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004788 {
4789 if (*p == Ctrl_V)
4790 {
4791 if (eap->argt & (USECTRLV | XFILE))
4792 ++p; /* skip CTRL-V and next char */
4793 else
Bram Moolenaarb0db5692007-08-14 20:54:49 +00004794 /* remove CTRL-V and skip next char */
Bram Moolenaara7241f52008-06-24 20:39:31 +00004795 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004796 if (*p == NUL) /* stop at NUL after CTRL-V */
4797 break;
4798 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004799
4800#ifdef FEAT_EVAL
4801 /* Skip over `=expr` when wildcards are expanded. */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004802 else if (p[0] == '`' && p[1] == '=' && (eap->argt & XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004803 {
4804 p += 2;
4805 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004806 }
4807#endif
4808
Bram Moolenaar071d4272004-06-13 20:20:40 +00004809 /* Check for '"': start of comment or '|': next command */
4810 /* :@" and :*" do not start a comment!
4811 * :redir @" doesn't either. */
4812 else if ((*p == '"' && !(eap->argt & NOTRLCOM)
4813 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
4814 || p != eap->arg)
4815 && (eap->cmdidx != CMD_redir
4816 || p != eap->arg + 1 || p[-1] != '@'))
4817 || *p == '|' || *p == '\n')
4818 {
4819 /*
4820 * We remove the '\' before the '|', unless USECTRLV is used
4821 * AND 'b' is present in 'cpoptions'.
4822 */
4823 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
4824 || !(eap->argt & USECTRLV)) && *(p - 1) == '\\')
4825 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00004826 STRMOVE(p - 1, p); /* remove the '\' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004827 --p;
4828 }
4829 else
4830 {
4831 eap->nextcmd = check_nextcmd(p);
4832 *p = NUL;
4833 break;
4834 }
4835 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004836 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004837
Bram Moolenaar071d4272004-06-13 20:20:40 +00004838 if (!(eap->argt & NOTRLCOM)) /* remove trailing spaces */
4839 del_trailing_spaces(eap->arg);
4840}
4841
4842/*
4843 * get + command from ex argument
4844 */
4845 static char_u *
4846getargcmd(argp)
4847 char_u **argp;
4848{
4849 char_u *arg = *argp;
4850 char_u *command = NULL;
4851
4852 if (*arg == '+') /* +[command] */
4853 {
4854 ++arg;
4855 if (vim_isspace(*arg))
4856 command = dollar_command;
4857 else
4858 {
4859 command = arg;
4860 arg = skip_cmd_arg(command, TRUE);
4861 if (*arg != NUL)
4862 *arg++ = NUL; /* terminate command with NUL */
4863 }
4864
4865 arg = skipwhite(arg); /* skip over spaces */
4866 *argp = arg;
4867 }
4868 return command;
4869}
4870
4871/*
4872 * Find end of "+command" argument. Skip over "\ " and "\\".
4873 */
4874 static char_u *
4875skip_cmd_arg(p, rembs)
4876 char_u *p;
4877 int rembs; /* TRUE to halve the number of backslashes */
4878{
4879 while (*p && !vim_isspace(*p))
4880 {
4881 if (*p == '\\' && p[1] != NUL)
4882 {
4883 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00004884 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004885 else
4886 ++p;
4887 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004888 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004889 }
4890 return p;
4891}
4892
4893/*
4894 * Get "++opt=arg" argument.
4895 * Return FAIL or OK.
4896 */
4897 static int
4898getargopt(eap)
4899 exarg_T *eap;
4900{
4901 char_u *arg = eap->arg + 2;
4902 int *pp = NULL;
4903#ifdef FEAT_MBYTE
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004904 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004905 char_u *p;
4906#endif
4907
4908 /* ":edit ++[no]bin[ary] file" */
4909 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
4910 {
4911 if (*arg == 'n')
4912 {
4913 arg += 2;
4914 eap->force_bin = FORCE_NOBIN;
4915 }
4916 else
4917 eap->force_bin = FORCE_BIN;
4918 if (!checkforcmd(&arg, "binary", 3))
4919 return FAIL;
4920 eap->arg = skipwhite(arg);
4921 return OK;
4922 }
4923
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004924 /* ":read ++edit file" */
4925 if (STRNCMP(arg, "edit", 4) == 0)
4926 {
4927 eap->read_edit = TRUE;
4928 eap->arg = skipwhite(arg + 4);
4929 return OK;
4930 }
4931
Bram Moolenaar071d4272004-06-13 20:20:40 +00004932 if (STRNCMP(arg, "ff", 2) == 0)
4933 {
4934 arg += 2;
4935 pp = &eap->force_ff;
4936 }
4937 else if (STRNCMP(arg, "fileformat", 10) == 0)
4938 {
4939 arg += 10;
4940 pp = &eap->force_ff;
4941 }
4942#ifdef FEAT_MBYTE
4943 else if (STRNCMP(arg, "enc", 3) == 0)
4944 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01004945 if (STRNCMP(arg, "encoding", 8) == 0)
4946 arg += 8;
4947 else
4948 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004949 pp = &eap->force_enc;
4950 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004951 else if (STRNCMP(arg, "bad", 3) == 0)
4952 {
4953 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004954 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004955 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004956#endif
4957
4958 if (pp == NULL || *arg != '=')
4959 return FAIL;
4960
4961 ++arg;
4962 *pp = (int)(arg - eap->cmd);
4963 arg = skip_cmd_arg(arg, FALSE);
4964 eap->arg = skipwhite(arg);
4965 *arg = NUL;
4966
4967#ifdef FEAT_MBYTE
4968 if (pp == &eap->force_ff)
4969 {
4970#endif
4971 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
4972 return FAIL;
4973#ifdef FEAT_MBYTE
4974 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004975 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004976 {
4977 /* Make 'fileencoding' lower case. */
4978 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
4979 *p = TOLOWER_ASC(*p);
4980 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004981 else
4982 {
4983 /* Check ++bad= argument. Must be a single-byte character, "keep" or
4984 * "drop". */
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004985 p = eap->cmd + bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004986 if (STRICMP(p, "keep") == 0)
4987 eap->bad_char = BAD_KEEP;
4988 else if (STRICMP(p, "drop") == 0)
4989 eap->bad_char = BAD_DROP;
4990 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
4991 eap->bad_char = *p;
4992 else
4993 return FAIL;
4994 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004995#endif
4996
4997 return OK;
4998}
4999
5000/*
5001 * ":abbreviate" and friends.
5002 */
5003 static void
5004ex_abbreviate(eap)
5005 exarg_T *eap;
5006{
5007 do_exmap(eap, TRUE); /* almost the same as mapping */
5008}
5009
5010/*
5011 * ":map" and friends.
5012 */
5013 static void
5014ex_map(eap)
5015 exarg_T *eap;
5016{
5017 /*
5018 * If we are sourcing .exrc or .vimrc in current directory we
5019 * print the mappings for security reasons.
5020 */
5021 if (secure)
5022 {
5023 secure = 2;
5024 msg_outtrans(eap->cmd);
5025 msg_putchar('\n');
5026 }
5027 do_exmap(eap, FALSE);
5028}
5029
5030/*
5031 * ":unmap" and friends.
5032 */
5033 static void
5034ex_unmap(eap)
5035 exarg_T *eap;
5036{
5037 do_exmap(eap, FALSE);
5038}
5039
5040/*
5041 * ":mapclear" and friends.
5042 */
5043 static void
5044ex_mapclear(eap)
5045 exarg_T *eap;
5046{
5047 map_clear(eap->cmd, eap->arg, eap->forceit, FALSE);
5048}
5049
5050/*
5051 * ":abclear" and friends.
5052 */
5053 static void
5054ex_abclear(eap)
5055 exarg_T *eap;
5056{
5057 map_clear(eap->cmd, eap->arg, TRUE, TRUE);
5058}
5059
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005060#if defined(FEAT_AUTOCMD) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005061 static void
5062ex_autocmd(eap)
5063 exarg_T *eap;
5064{
5065 /*
5066 * Disallow auto commands from .exrc and .vimrc in current
5067 * directory for security reasons.
5068 */
5069 if (secure)
5070 {
5071 secure = 2;
5072 eap->errmsg = e_curdir;
5073 }
5074 else if (eap->cmdidx == CMD_autocmd)
5075 do_autocmd(eap->arg, eap->forceit);
5076 else
5077 do_augroup(eap->arg, eap->forceit);
5078}
5079
5080/*
5081 * ":doautocmd": Apply the automatic commands to the current buffer.
5082 */
5083 static void
5084ex_doautocmd(eap)
5085 exarg_T *eap;
5086{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005087 char_u *arg = eap->arg;
5088 int call_do_modelines = check_nomodeline(&arg);
5089
5090 (void)do_doautocmd(arg, TRUE);
5091 if (call_do_modelines) /* Only when there is no <nomodeline>. */
5092 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005093}
5094#endif
5095
5096#ifdef FEAT_LISTCMDS
5097/*
5098 * :[N]bunload[!] [N] [bufname] unload buffer
5099 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
5100 * :[N]bwipeout[!] [N] [bufname] delete buffer really
5101 */
5102 static void
5103ex_bunload(eap)
5104 exarg_T *eap;
5105{
5106 eap->errmsg = do_bufdel(
5107 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
5108 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
5109 : DOBUF_UNLOAD, eap->arg,
5110 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
5111}
5112
5113/*
5114 * :[N]buffer [N] to buffer N
5115 * :[N]sbuffer [N] to buffer N
5116 */
5117 static void
5118ex_buffer(eap)
5119 exarg_T *eap;
5120{
5121 if (*eap->arg)
5122 eap->errmsg = e_trailing;
5123 else
5124 {
5125 if (eap->addr_count == 0) /* default is current buffer */
5126 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
5127 else
5128 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
5129 }
5130}
5131
5132/*
5133 * :[N]bmodified [N] to next mod. buffer
5134 * :[N]sbmodified [N] to next mod. buffer
5135 */
5136 static void
5137ex_bmodified(eap)
5138 exarg_T *eap;
5139{
5140 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
5141}
5142
5143/*
5144 * :[N]bnext [N] to next buffer
5145 * :[N]sbnext [N] split and to next buffer
5146 */
5147 static void
5148ex_bnext(eap)
5149 exarg_T *eap;
5150{
5151 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
5152}
5153
5154/*
5155 * :[N]bNext [N] to previous buffer
5156 * :[N]bprevious [N] to previous buffer
5157 * :[N]sbNext [N] split and to previous buffer
5158 * :[N]sbprevious [N] split and to previous buffer
5159 */
5160 static void
5161ex_bprevious(eap)
5162 exarg_T *eap;
5163{
5164 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
5165}
5166
5167/*
5168 * :brewind to first buffer
5169 * :bfirst to first buffer
5170 * :sbrewind split and to first buffer
5171 * :sbfirst split and to first buffer
5172 */
5173 static void
5174ex_brewind(eap)
5175 exarg_T *eap;
5176{
5177 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
5178}
5179
5180/*
5181 * :blast to last buffer
5182 * :sblast split and to last buffer
5183 */
5184 static void
5185ex_blast(eap)
5186 exarg_T *eap;
5187{
5188 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
5189}
5190#endif
5191
5192 int
5193ends_excmd(c)
5194 int c;
5195{
5196 return (c == NUL || c == '|' || c == '"' || c == '\n');
5197}
5198
5199#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5200 || defined(PROTO)
5201/*
5202 * Return the next command, after the first '|' or '\n'.
5203 * Return NULL if not found.
5204 */
5205 char_u *
5206find_nextcmd(p)
5207 char_u *p;
5208{
5209 while (*p != '|' && *p != '\n')
5210 {
5211 if (*p == NUL)
5212 return NULL;
5213 ++p;
5214 }
5215 return (p + 1);
5216}
5217#endif
5218
5219/*
5220 * Check if *p is a separator between Ex commands.
5221 * Return NULL if it isn't, (p + 1) if it is.
5222 */
5223 char_u *
5224check_nextcmd(p)
5225 char_u *p;
5226{
5227 p = skipwhite(p);
5228 if (*p == '|' || *p == '\n')
5229 return (p + 1);
5230 else
5231 return NULL;
5232}
5233
5234/*
5235 * - if there are more files to edit
5236 * - and this is the last window
5237 * - and forceit not used
5238 * - and not repeated twice on a row
5239 * return FAIL and give error message if 'message' TRUE
5240 * return OK otherwise
5241 */
5242 static int
5243check_more(message, forceit)
5244 int message; /* when FALSE check only, no messages */
5245 int forceit;
5246{
5247 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5248
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005249 if (!forceit && only_one_window()
5250 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005251 {
5252 if (message)
5253 {
5254#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
5255 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
5256 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005257 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005258
5259 if (n == 1)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02005260 vim_strncpy(buff,
5261 (char_u *)_("1 more file to edit. Quit anyway?"),
Bram Moolenaard9462e32011-04-11 21:35:11 +02005262 DIALOG_MSG_SIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005263 else
Bram Moolenaard9462e32011-04-11 21:35:11 +02005264 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005265 _("%d more files to edit. Quit anyway?"), n);
5266 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5267 return OK;
5268 return FAIL;
5269 }
5270#endif
5271 if (n == 1)
5272 EMSG(_("E173: 1 more file to edit"));
5273 else
5274 EMSGN(_("E173: %ld more files to edit"), n);
5275 quitmore = 2; /* next try to quit is allowed */
5276 }
5277 return FAIL;
5278 }
5279 return OK;
5280}
5281
5282#ifdef FEAT_CMDL_COMPL
5283/*
5284 * Function given to ExpandGeneric() to obtain the list of command names.
5285 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005286 char_u *
5287get_command_name(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00005288 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005289 int idx;
5290{
5291 if (idx >= (int)CMD_SIZE)
5292# ifdef FEAT_USR_CMDS
5293 return get_user_command_name(idx);
5294# else
5295 return NULL;
5296# endif
5297 return cmdnames[idx].cmd_name;
5298}
5299#endif
5300
5301#if defined(FEAT_USR_CMDS) || defined(PROTO)
5302static 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));
5303static void uc_list __ARGS((char_u *name, size_t name_len));
5304static int uc_scan_attr __ARGS((char_u *attr, size_t len, long *argt, long *def, int *flags, int *compl, char_u **compl_arg));
5305static char_u *uc_split_args __ARGS((char_u *arg, size_t *lenp));
5306static 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));
5307
5308 static int
5309uc_add_command(name, name_len, rep, argt, def, flags, compl, compl_arg, force)
5310 char_u *name;
5311 size_t name_len;
5312 char_u *rep;
5313 long argt;
5314 long def;
5315 int flags;
5316 int compl;
5317 char_u *compl_arg;
5318 int force;
5319{
5320 ucmd_T *cmd = NULL;
5321 char_u *p;
5322 int i;
5323 int cmp = 1;
5324 char_u *rep_buf = NULL;
5325 garray_T *gap;
5326
Bram Moolenaar9c102382006-05-03 21:26:49 +00005327 replace_termcodes(rep, &rep_buf, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005328 if (rep_buf == NULL)
5329 {
5330 /* Can't replace termcodes - try using the string as is */
5331 rep_buf = vim_strsave(rep);
5332
5333 /* Give up if out of memory */
5334 if (rep_buf == NULL)
5335 return FAIL;
5336 }
5337
5338 /* get address of growarray: global or in curbuf */
5339 if (flags & UC_BUFFER)
5340 {
5341 gap = &curbuf->b_ucmds;
5342 if (gap->ga_itemsize == 0)
5343 ga_init2(gap, (int)sizeof(ucmd_T), 4);
5344 }
5345 else
5346 gap = &ucmds;
5347
5348 /* Search for the command in the already defined commands. */
5349 for (i = 0; i < gap->ga_len; ++i)
5350 {
5351 size_t len;
5352
5353 cmd = USER_CMD_GA(gap, i);
5354 len = STRLEN(cmd->uc_name);
5355 cmp = STRNCMP(name, cmd->uc_name, name_len);
5356 if (cmp == 0)
5357 {
5358 if (name_len < len)
5359 cmp = -1;
5360 else if (name_len > len)
5361 cmp = 1;
5362 }
5363
5364 if (cmp == 0)
5365 {
5366 if (!force)
5367 {
5368 EMSG(_("E174: Command already exists: add ! to replace it"));
5369 goto fail;
5370 }
5371
5372 vim_free(cmd->uc_rep);
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00005373 cmd->uc_rep = NULL;
5374#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5375 vim_free(cmd->uc_compl_arg);
5376 cmd->uc_compl_arg = NULL;
5377#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005378 break;
5379 }
5380
5381 /* Stop as soon as we pass the name to add */
5382 if (cmp < 0)
5383 break;
5384 }
5385
5386 /* Extend the array unless we're replacing an existing command */
5387 if (cmp != 0)
5388 {
5389 if (ga_grow(gap, 1) != OK)
5390 goto fail;
5391 if ((p = vim_strnsave(name, (int)name_len)) == NULL)
5392 goto fail;
5393
5394 cmd = USER_CMD_GA(gap, i);
5395 mch_memmove(cmd + 1, cmd, (gap->ga_len - i) * sizeof(ucmd_T));
5396
5397 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005398
5399 cmd->uc_name = p;
5400 }
5401
5402 cmd->uc_rep = rep_buf;
5403 cmd->uc_argt = argt;
5404 cmd->uc_def = def;
5405 cmd->uc_compl = compl;
5406#ifdef FEAT_EVAL
5407 cmd->uc_scriptID = current_SID;
5408# ifdef FEAT_CMDL_COMPL
5409 cmd->uc_compl_arg = compl_arg;
5410# endif
5411#endif
5412
5413 return OK;
5414
5415fail:
5416 vim_free(rep_buf);
5417#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5418 vim_free(compl_arg);
5419#endif
5420 return FAIL;
5421}
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005422#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005423
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005424#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005425/*
5426 * List of names for completion for ":command" with the EXPAND_ flag.
5427 * Must be alphabetical for completion.
5428 */
5429static struct
5430{
5431 int expand;
5432 char *name;
5433} command_complete[] =
5434{
5435 {EXPAND_AUGROUP, "augroup"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005436 {EXPAND_BEHAVE, "behave"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005437 {EXPAND_BUFFERS, "buffer"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005438 {EXPAND_COLORS, "color"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005439 {EXPAND_COMMANDS, "command"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005440 {EXPAND_COMPILER, "compiler"},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005441#if defined(FEAT_CSCOPE)
5442 {EXPAND_CSCOPE, "cscope"},
5443#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005444#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5445 {EXPAND_USER_DEFINED, "custom"},
Bram Moolenaara466c992005-07-09 21:03:22 +00005446 {EXPAND_USER_LIST, "customlist"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005447#endif
5448 {EXPAND_DIRECTORIES, "dir"},
5449 {EXPAND_ENV_VARS, "environment"},
5450 {EXPAND_EVENTS, "event"},
5451 {EXPAND_EXPRESSION, "expression"},
5452 {EXPAND_FILES, "file"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005453 {EXPAND_FILES_IN_PATH, "file_in_path"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005454 {EXPAND_FILETYPE, "filetype"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005455 {EXPAND_FUNCTIONS, "function"},
5456 {EXPAND_HELP, "help"},
5457 {EXPAND_HIGHLIGHT, "highlight"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005458#if defined(FEAT_CMDHIST)
5459 {EXPAND_HISTORY, "history"},
5460#endif
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005461#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005462 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005463 {EXPAND_LOCALES, "locale"},
5464#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005465 {EXPAND_MAPPINGS, "mapping"},
5466 {EXPAND_MENUS, "menu"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005467 {EXPAND_OWNSYNTAX, "syntax"},
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02005468#if defined(FEAT_PROFILE)
5469 {EXPAND_SYNTIME, "syntime"},
5470#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005471 {EXPAND_SETTINGS, "option"},
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005472 {EXPAND_SHELLCMD, "shellcmd"},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005473#if defined(FEAT_SIGNS)
5474 {EXPAND_SIGN, "sign"},
5475#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005476 {EXPAND_TAGS, "tag"},
5477 {EXPAND_TAGS_LISTFILES, "tag_listfiles"},
Bram Moolenaar24305862012-08-15 14:05:05 +02005478 {EXPAND_USER, "user"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005479 {EXPAND_USER_VARS, "var"},
5480 {0, NULL}
5481};
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005482#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005483
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005484#if defined(FEAT_USR_CMDS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005485 static void
5486uc_list(name, name_len)
5487 char_u *name;
5488 size_t name_len;
5489{
5490 int i, j;
5491 int found = FALSE;
5492 ucmd_T *cmd;
5493 int len;
5494 long a;
5495 garray_T *gap;
5496
5497 gap = &curbuf->b_ucmds;
5498 for (;;)
5499 {
5500 for (i = 0; i < gap->ga_len; ++i)
5501 {
5502 cmd = USER_CMD_GA(gap, i);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005503 a = (long)cmd->uc_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005504
5505 /* Skip commands which don't match the requested prefix */
5506 if (STRNCMP(name, cmd->uc_name, name_len) != 0)
5507 continue;
5508
5509 /* Put out the title first time */
5510 if (!found)
5511 MSG_PUTS_TITLE(_("\n Name Args Range Complete Definition"));
5512 found = TRUE;
5513 msg_putchar('\n');
5514 if (got_int)
5515 break;
5516
5517 /* Special cases */
5518 msg_putchar(a & BANG ? '!' : ' ');
5519 msg_putchar(a & REGSTR ? '"' : ' ');
5520 msg_putchar(gap != &ucmds ? 'b' : ' ');
5521 msg_putchar(' ');
5522
5523 msg_outtrans_attr(cmd->uc_name, hl_attr(HLF_D));
5524 len = (int)STRLEN(cmd->uc_name) + 4;
5525
5526 do {
5527 msg_putchar(' ');
5528 ++len;
5529 } while (len < 16);
5530
5531 len = 0;
5532
5533 /* Arguments */
5534 switch ((int)(a & (EXTRA|NOSPC|NEEDARG)))
5535 {
5536 case 0: IObuff[len++] = '0'; break;
5537 case (EXTRA): IObuff[len++] = '*'; break;
5538 case (EXTRA|NOSPC): IObuff[len++] = '?'; break;
5539 case (EXTRA|NEEDARG): IObuff[len++] = '+'; break;
5540 case (EXTRA|NOSPC|NEEDARG): IObuff[len++] = '1'; break;
5541 }
5542
5543 do {
5544 IObuff[len++] = ' ';
5545 } while (len < 5);
5546
5547 /* Range */
5548 if (a & (RANGE|COUNT))
5549 {
5550 if (a & COUNT)
5551 {
5552 /* -count=N */
5553 sprintf((char *)IObuff + len, "%ldc", cmd->uc_def);
5554 len += (int)STRLEN(IObuff + len);
5555 }
5556 else if (a & DFLALL)
5557 IObuff[len++] = '%';
5558 else if (cmd->uc_def >= 0)
5559 {
5560 /* -range=N */
5561 sprintf((char *)IObuff + len, "%ld", cmd->uc_def);
5562 len += (int)STRLEN(IObuff + len);
5563 }
5564 else
5565 IObuff[len++] = '.';
5566 }
5567
5568 do {
5569 IObuff[len++] = ' ';
5570 } while (len < 11);
5571
5572 /* Completion */
5573 for (j = 0; command_complete[j].expand != 0; ++j)
5574 if (command_complete[j].expand == cmd->uc_compl)
5575 {
5576 STRCPY(IObuff + len, command_complete[j].name);
5577 len += (int)STRLEN(IObuff + len);
5578 break;
5579 }
5580
5581 do {
5582 IObuff[len++] = ' ';
5583 } while (len < 21);
5584
5585 IObuff[len] = '\0';
5586 msg_outtrans(IObuff);
5587
5588 msg_outtrans_special(cmd->uc_rep, FALSE);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005589#ifdef FEAT_EVAL
5590 if (p_verbose > 0)
5591 last_set_msg(cmd->uc_scriptID);
5592#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005593 out_flush();
5594 ui_breakcheck();
5595 if (got_int)
5596 break;
5597 }
5598 if (gap == &ucmds || i < gap->ga_len)
5599 break;
5600 gap = &ucmds;
5601 }
5602
5603 if (!found)
5604 MSG(_("No user-defined commands found"));
5605}
5606
5607 static char_u *
5608uc_fun_cmd()
5609{
5610 static char_u fcmd[] = {0x84, 0xaf, 0x60, 0xb9, 0xaf, 0xb5, 0x60, 0xa4,
5611 0xa5, 0xad, 0xa1, 0xae, 0xa4, 0x60, 0xa1, 0x60,
5612 0xb3, 0xa8, 0xb2, 0xb5, 0xa2, 0xa2, 0xa5, 0xb2,
5613 0xb9, 0x7f, 0};
5614 int i;
5615
5616 for (i = 0; fcmd[i]; ++i)
5617 IObuff[i] = fcmd[i] - 0x40;
5618 IObuff[i] = 0;
5619 return IObuff;
5620}
5621
5622 static int
5623uc_scan_attr(attr, len, argt, def, flags, compl, compl_arg)
5624 char_u *attr;
5625 size_t len;
5626 long *argt;
5627 long *def;
5628 int *flags;
5629 int *compl;
5630 char_u **compl_arg;
5631{
5632 char_u *p;
5633
5634 if (len == 0)
5635 {
5636 EMSG(_("E175: No attribute specified"));
5637 return FAIL;
5638 }
5639
5640 /* First, try the simple attributes (no arguments) */
5641 if (STRNICMP(attr, "bang", len) == 0)
5642 *argt |= BANG;
5643 else if (STRNICMP(attr, "buffer", len) == 0)
5644 *flags |= UC_BUFFER;
5645 else if (STRNICMP(attr, "register", len) == 0)
5646 *argt |= REGSTR;
5647 else if (STRNICMP(attr, "bar", len) == 0)
5648 *argt |= TRLBAR;
5649 else
5650 {
5651 int i;
5652 char_u *val = NULL;
5653 size_t vallen = 0;
5654 size_t attrlen = len;
5655
5656 /* Look for the attribute name - which is the part before any '=' */
5657 for (i = 0; i < (int)len; ++i)
5658 {
5659 if (attr[i] == '=')
5660 {
5661 val = &attr[i + 1];
5662 vallen = len - i - 1;
5663 attrlen = i;
5664 break;
5665 }
5666 }
5667
5668 if (STRNICMP(attr, "nargs", attrlen) == 0)
5669 {
5670 if (vallen == 1)
5671 {
5672 if (*val == '0')
5673 /* Do nothing - this is the default */;
5674 else if (*val == '1')
5675 *argt |= (EXTRA | NOSPC | NEEDARG);
5676 else if (*val == '*')
5677 *argt |= EXTRA;
5678 else if (*val == '?')
5679 *argt |= (EXTRA | NOSPC);
5680 else if (*val == '+')
5681 *argt |= (EXTRA | NEEDARG);
5682 else
5683 goto wrong_nargs;
5684 }
5685 else
5686 {
5687wrong_nargs:
5688 EMSG(_("E176: Invalid number of arguments"));
5689 return FAIL;
5690 }
5691 }
5692 else if (STRNICMP(attr, "range", attrlen) == 0)
5693 {
5694 *argt |= RANGE;
5695 if (vallen == 1 && *val == '%')
5696 *argt |= DFLALL;
5697 else if (val != NULL)
5698 {
5699 p = val;
5700 if (*def >= 0)
5701 {
5702two_count:
5703 EMSG(_("E177: Count cannot be specified twice"));
5704 return FAIL;
5705 }
5706
5707 *def = getdigits(&p);
5708 *argt |= (ZEROR | NOTADR);
5709
5710 if (p != val + vallen || vallen == 0)
5711 {
5712invalid_count:
5713 EMSG(_("E178: Invalid default value for count"));
5714 return FAIL;
5715 }
5716 }
5717 }
5718 else if (STRNICMP(attr, "count", attrlen) == 0)
5719 {
Bram Moolenaar32e7b2d2005-02-27 22:36:47 +00005720 *argt |= (COUNT | ZEROR | RANGE | NOTADR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005721
5722 if (val != NULL)
5723 {
5724 p = val;
5725 if (*def >= 0)
5726 goto two_count;
5727
5728 *def = getdigits(&p);
5729
5730 if (p != val + vallen)
5731 goto invalid_count;
5732 }
5733
5734 if (*def < 0)
5735 *def = 0;
5736 }
5737 else if (STRNICMP(attr, "complete", attrlen) == 0)
5738 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005739 if (val == NULL)
5740 {
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005741 EMSG(_("E179: argument required for -complete"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005742 return FAIL;
5743 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005744
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005745 if (parse_compl_arg(val, (int)vallen, compl, argt, compl_arg)
5746 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005747 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005748 }
5749 else
5750 {
5751 char_u ch = attr[len];
5752 attr[len] = '\0';
5753 EMSG2(_("E181: Invalid attribute: %s"), attr);
5754 attr[len] = ch;
5755 return FAIL;
5756 }
5757 }
5758
5759 return OK;
5760}
5761
Bram Moolenaara850a712009-01-28 14:42:59 +00005762/*
5763 * ":command ..."
5764 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005765 static void
5766ex_command(eap)
5767 exarg_T *eap;
5768{
5769 char_u *name;
5770 char_u *end;
5771 char_u *p;
5772 long argt = 0;
5773 long def = -1;
5774 int flags = 0;
5775 int compl = EXPAND_NOTHING;
5776 char_u *compl_arg = NULL;
5777 int has_attr = (eap->arg[0] == '-');
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01005778 int name_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005779
5780 p = eap->arg;
5781
5782 /* Check for attributes */
5783 while (*p == '-')
5784 {
5785 ++p;
5786 end = skiptowhite(p);
5787 if (uc_scan_attr(p, end - p, &argt, &def, &flags, &compl, &compl_arg)
5788 == FAIL)
5789 return;
5790 p = skipwhite(end);
5791 }
5792
5793 /* Get the name (if any) and skip to the following argument */
5794 name = p;
5795 if (ASCII_ISALPHA(*p))
5796 while (ASCII_ISALNUM(*p))
5797 ++p;
5798 if (!ends_excmd(*p) && !vim_iswhite(*p))
5799 {
5800 EMSG(_("E182: Invalid command name"));
5801 return;
5802 }
5803 end = p;
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01005804 name_len = (int)(end - name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005805
5806 /* If there is nothing after the name, and no attributes were specified,
5807 * we are listing commands
5808 */
5809 p = skipwhite(end);
5810 if (!has_attr && ends_excmd(*p))
5811 {
5812 uc_list(name, end - name);
5813 }
5814 else if (!ASCII_ISUPPER(*name))
5815 {
5816 EMSG(_("E183: User defined commands must start with an uppercase letter"));
5817 return;
5818 }
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01005819 else if ((name_len == 1 && *name == 'X')
5820 || (name_len <= 4
5821 && STRNCMP(name, "Next", name_len > 4 ? 4 : name_len) == 0))
5822 {
5823 EMSG(_("E841: Reserved name, cannot be used for user defined command"));
5824 return;
5825 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005826 else
5827 uc_add_command(name, end - name, p, argt, def, flags, compl, compl_arg,
5828 eap->forceit);
5829}
5830
5831/*
5832 * ":comclear"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005833 * Clear all user commands, global and for current buffer.
5834 */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00005835 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005836ex_comclear(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00005837 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005838{
5839 uc_clear(&ucmds);
5840 uc_clear(&curbuf->b_ucmds);
5841}
5842
5843/*
5844 * Clear all user commands for "gap".
5845 */
5846 void
5847uc_clear(gap)
5848 garray_T *gap;
5849{
5850 int i;
5851 ucmd_T *cmd;
5852
5853 for (i = 0; i < gap->ga_len; ++i)
5854 {
5855 cmd = USER_CMD_GA(gap, i);
5856 vim_free(cmd->uc_name);
5857 vim_free(cmd->uc_rep);
5858# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5859 vim_free(cmd->uc_compl_arg);
5860# endif
5861 }
5862 ga_clear(gap);
5863}
5864
5865 static void
5866ex_delcommand(eap)
5867 exarg_T *eap;
5868{
5869 int i = 0;
5870 ucmd_T *cmd = NULL;
5871 int cmp = -1;
5872 garray_T *gap;
5873
5874 gap = &curbuf->b_ucmds;
5875 for (;;)
5876 {
5877 for (i = 0; i < gap->ga_len; ++i)
5878 {
5879 cmd = USER_CMD_GA(gap, i);
5880 cmp = STRCMP(eap->arg, cmd->uc_name);
5881 if (cmp <= 0)
5882 break;
5883 }
5884 if (gap == &ucmds || cmp == 0)
5885 break;
5886 gap = &ucmds;
5887 }
5888
5889 if (cmp != 0)
5890 {
5891 EMSG2(_("E184: No such user-defined command: %s"), eap->arg);
5892 return;
5893 }
5894
5895 vim_free(cmd->uc_name);
5896 vim_free(cmd->uc_rep);
5897# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5898 vim_free(cmd->uc_compl_arg);
5899# endif
5900
5901 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005902
5903 if (i < gap->ga_len)
5904 mch_memmove(cmd, cmd + 1, (gap->ga_len - i) * sizeof(ucmd_T));
5905}
5906
Bram Moolenaar552f8a12007-03-08 17:12:08 +00005907/*
5908 * split and quote args for <f-args>
5909 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005910 static char_u *
5911uc_split_args(arg, lenp)
5912 char_u *arg;
5913 size_t *lenp;
5914{
5915 char_u *buf;
5916 char_u *p;
5917 char_u *q;
5918 int len;
5919
5920 /* Precalculate length */
5921 p = arg;
5922 len = 2; /* Initial and final quotes */
5923
5924 while (*p)
5925 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00005926 if (p[0] == '\\' && p[1] == '\\')
5927 {
5928 len += 2;
5929 p += 2;
5930 }
5931 else if (p[0] == '\\' && vim_iswhite(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005932 {
5933 len += 1;
5934 p += 2;
5935 }
5936 else if (*p == '\\' || *p == '"')
5937 {
5938 len += 2;
5939 p += 1;
5940 }
5941 else if (vim_iswhite(*p))
5942 {
5943 p = skipwhite(p);
5944 if (*p == NUL)
5945 break;
5946 len += 3; /* "," */
5947 }
5948 else
5949 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02005950#ifdef FEAT_MBYTE
5951 int charlen = (*mb_ptr2len)(p);
5952 len += charlen;
5953 p += charlen;
5954#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005955 ++len;
5956 ++p;
Bram Moolenaardfef1542012-07-10 19:25:10 +02005957#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005958 }
5959 }
5960
5961 buf = alloc(len + 1);
5962 if (buf == NULL)
5963 {
5964 *lenp = 0;
5965 return buf;
5966 }
5967
5968 p = arg;
5969 q = buf;
5970 *q++ = '"';
5971 while (*p)
5972 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00005973 if (p[0] == '\\' && p[1] == '\\')
5974 {
5975 *q++ = '\\';
5976 *q++ = '\\';
5977 p += 2;
5978 }
5979 else if (p[0] == '\\' && vim_iswhite(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005980 {
5981 *q++ = p[1];
5982 p += 2;
5983 }
5984 else if (*p == '\\' || *p == '"')
5985 {
5986 *q++ = '\\';
5987 *q++ = *p++;
5988 }
5989 else if (vim_iswhite(*p))
5990 {
5991 p = skipwhite(p);
5992 if (*p == NUL)
5993 break;
5994 *q++ = '"';
5995 *q++ = ',';
5996 *q++ = '"';
5997 }
5998 else
5999 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02006000 MB_COPY_CHAR(p, q);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006001 }
6002 }
6003 *q++ = '"';
6004 *q = 0;
6005
6006 *lenp = len;
6007 return buf;
6008}
6009
6010/*
6011 * Check for a <> code in a user command.
6012 * "code" points to the '<'. "len" the length of the <> (inclusive).
6013 * "buf" is where the result is to be added.
6014 * "split_buf" points to a buffer used for splitting, caller should free it.
6015 * "split_len" is the length of what "split_buf" contains.
6016 * Returns the length of the replacement, which has been added to "buf".
6017 * Returns -1 if there was no match, and only the "<" has been copied.
6018 */
6019 static size_t
6020uc_check_code(code, len, buf, cmd, eap, split_buf, split_len)
6021 char_u *code;
6022 size_t len;
6023 char_u *buf;
6024 ucmd_T *cmd; /* the user command we're expanding */
6025 exarg_T *eap; /* ex arguments */
6026 char_u **split_buf;
6027 size_t *split_len;
6028{
6029 size_t result = 0;
6030 char_u *p = code + 1;
6031 size_t l = len - 2;
6032 int quote = 0;
6033 enum { ct_ARGS, ct_BANG, ct_COUNT, ct_LINE1, ct_LINE2, ct_REGISTER,
6034 ct_LT, ct_NONE } type = ct_NONE;
6035
6036 if ((vim_strchr((char_u *)"qQfF", *p) != NULL) && p[1] == '-')
6037 {
6038 quote = (*p == 'q' || *p == 'Q') ? 1 : 2;
6039 p += 2;
6040 l -= 2;
6041 }
6042
Bram Moolenaar371d5402006-03-20 21:47:49 +00006043 ++l;
6044 if (l <= 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006045 type = ct_NONE;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006046 else if (STRNICMP(p, "args>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006047 type = ct_ARGS;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006048 else if (STRNICMP(p, "bang>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006049 type = ct_BANG;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006050 else if (STRNICMP(p, "count>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006051 type = ct_COUNT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006052 else if (STRNICMP(p, "line1>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006053 type = ct_LINE1;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006054 else if (STRNICMP(p, "line2>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006055 type = ct_LINE2;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006056 else if (STRNICMP(p, "lt>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006057 type = ct_LT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006058 else if (STRNICMP(p, "reg>", l) == 0 || STRNICMP(p, "register>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006059 type = ct_REGISTER;
6060
6061 switch (type)
6062 {
6063 case ct_ARGS:
6064 /* Simple case first */
6065 if (*eap->arg == NUL)
6066 {
6067 if (quote == 1)
6068 {
6069 result = 2;
6070 if (buf != NULL)
6071 STRCPY(buf, "''");
6072 }
6073 else
6074 result = 0;
6075 break;
6076 }
6077
6078 /* When specified there is a single argument don't split it.
6079 * Works for ":Cmd %" when % is "a b c". */
6080 if ((eap->argt & NOSPC) && quote == 2)
6081 quote = 1;
6082
6083 switch (quote)
6084 {
6085 case 0: /* No quoting, no splitting */
6086 result = STRLEN(eap->arg);
6087 if (buf != NULL)
6088 STRCPY(buf, eap->arg);
6089 break;
6090 case 1: /* Quote, but don't split */
6091 result = STRLEN(eap->arg) + 2;
6092 for (p = eap->arg; *p; ++p)
6093 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006094#ifdef FEAT_MBYTE
6095 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6096 /* DBCS can contain \ in a trail byte, skip the
6097 * double-byte character. */
6098 ++p;
6099 else
6100#endif
6101 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006102 ++result;
6103 }
6104
6105 if (buf != NULL)
6106 {
6107 *buf++ = '"';
6108 for (p = eap->arg; *p; ++p)
6109 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006110#ifdef FEAT_MBYTE
6111 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6112 /* DBCS can contain \ in a trail byte, copy the
6113 * double-byte character to avoid escaping. */
6114 *buf++ = *p++;
6115 else
6116#endif
6117 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006118 *buf++ = '\\';
6119 *buf++ = *p;
6120 }
6121 *buf = '"';
6122 }
6123
6124 break;
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006125 case 2: /* Quote and split (<f-args>) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006126 /* This is hard, so only do it once, and cache the result */
6127 if (*split_buf == NULL)
6128 *split_buf = uc_split_args(eap->arg, split_len);
6129
6130 result = *split_len;
6131 if (buf != NULL && result != 0)
6132 STRCPY(buf, *split_buf);
6133
6134 break;
6135 }
6136 break;
6137
6138 case ct_BANG:
6139 result = eap->forceit ? 1 : 0;
6140 if (quote)
6141 result += 2;
6142 if (buf != NULL)
6143 {
6144 if (quote)
6145 *buf++ = '"';
6146 if (eap->forceit)
6147 *buf++ = '!';
6148 if (quote)
6149 *buf = '"';
6150 }
6151 break;
6152
6153 case ct_LINE1:
6154 case ct_LINE2:
6155 case ct_COUNT:
6156 {
6157 char num_buf[20];
6158 long num = (type == ct_LINE1) ? eap->line1 :
6159 (type == ct_LINE2) ? eap->line2 :
6160 (eap->addr_count > 0) ? eap->line2 : cmd->uc_def;
6161 size_t num_len;
6162
6163 sprintf(num_buf, "%ld", num);
6164 num_len = STRLEN(num_buf);
6165 result = num_len;
6166
6167 if (quote)
6168 result += 2;
6169
6170 if (buf != NULL)
6171 {
6172 if (quote)
6173 *buf++ = '"';
6174 STRCPY(buf, num_buf);
6175 buf += num_len;
6176 if (quote)
6177 *buf = '"';
6178 }
6179
6180 break;
6181 }
6182
6183 case ct_REGISTER:
6184 result = eap->regname ? 1 : 0;
6185 if (quote)
6186 result += 2;
6187 if (buf != NULL)
6188 {
6189 if (quote)
6190 *buf++ = '\'';
6191 if (eap->regname)
6192 *buf++ = eap->regname;
6193 if (quote)
6194 *buf = '\'';
6195 }
6196 break;
6197
6198 case ct_LT:
6199 result = 1;
6200 if (buf != NULL)
6201 *buf = '<';
6202 break;
6203
6204 default:
6205 /* Not recognized: just copy the '<' and return -1. */
6206 result = (size_t)-1;
6207 if (buf != NULL)
6208 *buf = '<';
6209 break;
6210 }
6211
6212 return result;
6213}
6214
6215 static void
6216do_ucmd(eap)
6217 exarg_T *eap;
6218{
6219 char_u *buf;
6220 char_u *p;
6221 char_u *q;
6222
6223 char_u *start;
Bram Moolenaar25648a52009-02-21 19:37:46 +00006224 char_u *end = NULL;
Bram Moolenaara850a712009-01-28 14:42:59 +00006225 char_u *ksp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006226 size_t len, totlen;
6227
6228 size_t split_len = 0;
6229 char_u *split_buf = NULL;
6230 ucmd_T *cmd;
6231#ifdef FEAT_EVAL
6232 scid_T save_current_SID = current_SID;
6233#endif
6234
6235 if (eap->cmdidx == CMD_USER)
6236 cmd = USER_CMD(eap->useridx);
6237 else
6238 cmd = USER_CMD_GA(&curbuf->b_ucmds, eap->useridx);
6239
6240 /*
6241 * Replace <> in the command by the arguments.
Bram Moolenaara850a712009-01-28 14:42:59 +00006242 * First round: "buf" is NULL, compute length, allocate "buf".
6243 * Second round: copy result into "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006244 */
6245 buf = NULL;
6246 for (;;)
6247 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006248 p = cmd->uc_rep; /* source */
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00006249 q = buf; /* destination */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006250 totlen = 0;
Bram Moolenaara850a712009-01-28 14:42:59 +00006251
6252 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006253 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006254 start = vim_strchr(p, '<');
6255 if (start != NULL)
6256 end = vim_strchr(start + 1, '>');
6257 if (buf != NULL)
6258 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006259 for (ksp = p; *ksp != NUL && *ksp != K_SPECIAL; ++ksp)
6260 ;
6261 if (*ksp == K_SPECIAL
6262 && (start == NULL || ksp < start || end == NULL)
Bram Moolenaara850a712009-01-28 14:42:59 +00006263 && ((ksp[1] == KS_SPECIAL && ksp[2] == KE_FILLER)
6264# ifdef FEAT_GUI
6265 || (ksp[1] == KS_EXTRA && ksp[2] == (int)KE_CSI)
6266# endif
6267 ))
6268 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006269 /* K_SPECIAL has been put in the buffer as K_SPECIAL
Bram Moolenaara850a712009-01-28 14:42:59 +00006270 * KS_SPECIAL KE_FILLER, like for mappings, but
6271 * do_cmdline() doesn't handle that, so convert it back.
6272 * Also change K_SPECIAL KS_EXTRA KE_CSI into CSI. */
6273 len = ksp - p;
6274 if (len > 0)
6275 {
6276 mch_memmove(q, p, len);
6277 q += len;
6278 }
6279 *q++ = ksp[1] == KS_SPECIAL ? K_SPECIAL : CSI;
6280 p = ksp + 3;
6281 continue;
6282 }
6283 }
6284
6285 /* break if there no <item> is found */
6286 if (start == NULL || end == NULL)
6287 break;
6288
Bram Moolenaar071d4272004-06-13 20:20:40 +00006289 /* Include the '>' */
6290 ++end;
6291
6292 /* Take everything up to the '<' */
6293 len = start - p;
6294 if (buf == NULL)
6295 totlen += len;
6296 else
6297 {
6298 mch_memmove(q, p, len);
6299 q += len;
6300 }
6301
6302 len = uc_check_code(start, end - start, q, cmd, eap,
6303 &split_buf, &split_len);
6304 if (len == (size_t)-1)
6305 {
6306 /* no match, continue after '<' */
6307 p = start + 1;
6308 len = 1;
6309 }
6310 else
6311 p = end;
6312 if (buf == NULL)
6313 totlen += len;
6314 else
6315 q += len;
6316 }
6317 if (buf != NULL) /* second time here, finished */
6318 {
6319 STRCPY(q, p);
6320 break;
6321 }
6322
6323 totlen += STRLEN(p); /* Add on the trailing characters */
6324 buf = alloc((unsigned)(totlen + 1));
6325 if (buf == NULL)
6326 {
6327 vim_free(split_buf);
6328 return;
6329 }
6330 }
6331
6332#ifdef FEAT_EVAL
6333 current_SID = cmd->uc_scriptID;
6334#endif
6335 (void)do_cmdline(buf, eap->getline, eap->cookie,
6336 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
6337#ifdef FEAT_EVAL
6338 current_SID = save_current_SID;
6339#endif
6340 vim_free(buf);
6341 vim_free(split_buf);
6342}
6343
6344# if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6345 static char_u *
6346get_user_command_name(idx)
6347 int idx;
6348{
6349 return get_user_commands(NULL, idx - (int)CMD_SIZE);
6350}
6351
6352/*
6353 * Function given to ExpandGeneric() to obtain the list of user command names.
6354 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006355 char_u *
6356get_user_commands(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006357 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006358 int idx;
6359{
6360 if (idx < curbuf->b_ucmds.ga_len)
6361 return USER_CMD_GA(&curbuf->b_ucmds, idx)->uc_name;
6362 idx -= curbuf->b_ucmds.ga_len;
6363 if (idx < ucmds.ga_len)
6364 return USER_CMD(idx)->uc_name;
6365 return NULL;
6366}
6367
6368/*
6369 * Function given to ExpandGeneric() to obtain the list of user command
6370 * attributes.
6371 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006372 char_u *
6373get_user_cmd_flags(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 static char *user_cmd_flags[] =
6378 {"bang", "bar", "buffer", "complete", "count",
6379 "nargs", "range", "register"};
6380
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00006381 if (idx >= (int)(sizeof(user_cmd_flags) / sizeof(user_cmd_flags[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006382 return NULL;
6383 return (char_u *)user_cmd_flags[idx];
6384}
6385
6386/*
6387 * Function given to ExpandGeneric() to obtain the list of values for -nargs.
6388 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006389 char_u *
6390get_user_cmd_nargs(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006391 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006392 int idx;
6393{
6394 static char *user_cmd_nargs[] = {"0", "1", "*", "?", "+"};
6395
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00006396 if (idx >= (int)(sizeof(user_cmd_nargs) / sizeof(user_cmd_nargs[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006397 return NULL;
6398 return (char_u *)user_cmd_nargs[idx];
6399}
6400
6401/*
6402 * Function given to ExpandGeneric() to obtain the list of values for -complete.
6403 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006404 char_u *
6405get_user_cmd_complete(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006406 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006407 int idx;
6408{
6409 return (char_u *)command_complete[idx].name;
6410}
6411# endif /* FEAT_CMDL_COMPL */
6412
6413#endif /* FEAT_USR_CMDS */
6414
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006415#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
6416/*
6417 * Parse a completion argument "value[vallen]".
6418 * The detected completion goes in "*complp", argument type in "*argt".
6419 * When there is an argument, for function and user defined completion, it's
6420 * copied to allocated memory and stored in "*compl_arg".
6421 * Returns FAIL if something is wrong.
6422 */
6423 int
6424parse_compl_arg(value, vallen, complp, argt, compl_arg)
6425 char_u *value;
6426 int vallen;
6427 int *complp;
6428 long *argt;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006429 char_u **compl_arg UNUSED;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006430{
6431 char_u *arg = NULL;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006432# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006433 size_t arglen = 0;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006434# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006435 int i;
6436 int valend = vallen;
6437
6438 /* Look for any argument part - which is the part after any ',' */
6439 for (i = 0; i < vallen; ++i)
6440 {
6441 if (value[i] == ',')
6442 {
6443 arg = &value[i + 1];
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006444# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006445 arglen = vallen - i - 1;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006446# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006447 valend = i;
6448 break;
6449 }
6450 }
6451
6452 for (i = 0; command_complete[i].expand != 0; ++i)
6453 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006454 if ((int)STRLEN(command_complete[i].name) == valend
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006455 && STRNCMP(value, command_complete[i].name, valend) == 0)
6456 {
6457 *complp = command_complete[i].expand;
6458 if (command_complete[i].expand == EXPAND_BUFFERS)
6459 *argt |= BUFNAME;
6460 else if (command_complete[i].expand == EXPAND_DIRECTORIES
6461 || command_complete[i].expand == EXPAND_FILES)
6462 *argt |= XFILE;
6463 break;
6464 }
6465 }
6466
6467 if (command_complete[i].expand == 0)
6468 {
6469 EMSG2(_("E180: Invalid complete value: %s"), value);
6470 return FAIL;
6471 }
6472
6473# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6474 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST
6475 && arg != NULL)
6476# else
6477 if (arg != NULL)
6478# endif
6479 {
6480 EMSG(_("E468: Completion argument only allowed for custom completion"));
6481 return FAIL;
6482 }
6483
6484# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6485 if ((*complp == EXPAND_USER_DEFINED || *complp == EXPAND_USER_LIST)
6486 && arg == NULL)
6487 {
6488 EMSG(_("E467: Custom completion requires a function argument"));
6489 return FAIL;
6490 }
6491
6492 if (arg != NULL)
6493 *compl_arg = vim_strnsave(arg, (int)arglen);
6494# endif
6495 return OK;
6496}
6497#endif
6498
Bram Moolenaar071d4272004-06-13 20:20:40 +00006499 static void
6500ex_colorscheme(eap)
6501 exarg_T *eap;
6502{
Bram Moolenaare6850792010-05-14 15:28:44 +02006503 if (*eap->arg == NUL)
6504 {
6505#ifdef FEAT_EVAL
6506 char_u *expr = vim_strsave((char_u *)"g:colors_name");
6507 char_u *p = NULL;
6508
6509 if (expr != NULL)
6510 {
6511 ++emsg_off;
6512 p = eval_to_string(expr, NULL, FALSE);
6513 --emsg_off;
6514 vim_free(expr);
6515 }
6516 if (p != NULL)
6517 {
6518 MSG(p);
6519 vim_free(p);
6520 }
6521 else
6522 MSG("default");
6523#else
6524 MSG(_("unknown"));
6525#endif
6526 }
6527 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarbe1e9e92012-09-18 16:47:07 +02006528 EMSG2(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006529}
6530
6531 static void
6532ex_highlight(eap)
6533 exarg_T *eap;
6534{
6535 if (*eap->arg == NUL && eap->cmd[2] == '!')
6536 MSG(_("Greetings, Vim user!"));
6537 do_highlight(eap->arg, eap->forceit, FALSE);
6538}
6539
6540
6541/*
6542 * Call this function if we thought we were going to exit, but we won't
6543 * (because of an error). May need to restore the terminal mode.
6544 */
6545 void
6546not_exiting()
6547{
6548 exiting = FALSE;
6549 settmode(TMODE_RAW);
6550}
6551
6552/*
6553 * ":quit": quit current window, quit Vim if closed the last window.
6554 */
6555 static void
6556ex_quit(eap)
6557 exarg_T *eap;
6558{
6559#ifdef FEAT_CMDWIN
6560 if (cmdwin_type != 0)
6561 {
6562 cmdwin_result = Ctrl_C;
6563 return;
6564 }
6565#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006566 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006567 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006568 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006569 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006570 return;
6571 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006572#ifdef FEAT_AUTOCMD
Bram Moolenaar3b53dfb2012-06-06 18:03:07 +02006573 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01006574 /* Refuse to quit when locked or when the buffer in the last window is
Bram Moolenaar362ce482012-06-06 19:02:45 +02006575 * being closed (can only happen in autocommands). */
6576 if (curbuf_locked() || (curbuf->b_nwindows == 1 && curbuf->b_closing))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006577 return;
6578#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006579
6580#ifdef FEAT_NETBEANS_INTG
6581 netbeansForcedQuit = eap->forceit;
6582#endif
6583
6584 /*
6585 * If there are more files or windows we won't exit.
6586 */
6587 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6588 exiting = TRUE;
6589 if ((!P_HID(curbuf)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01006590 && check_changed(curbuf, (p_awa ? CCGD_AW : 0)
6591 | (eap->forceit ? CCGD_FORCEIT : 0)
6592 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006593 || check_more(TRUE, eap->forceit) == FAIL
6594 || (only_one_window() && check_changed_any(eap->forceit)))
6595 {
6596 not_exiting();
6597 }
6598 else
6599 {
6600#ifdef FEAT_WINDOWS
6601 if (only_one_window()) /* quit last window */
6602#endif
6603 getout(0);
6604#ifdef FEAT_WINDOWS
6605# ifdef FEAT_GUI
6606 need_mouse_correct = TRUE;
6607# endif
6608 /* close window; may free buffer */
6609 win_close(curwin, !P_HID(curwin->w_buffer) || eap->forceit);
6610#endif
6611 }
6612}
6613
6614/*
6615 * ":cquit".
6616 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006617 static void
6618ex_cquit(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006619 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006620{
6621 getout(1); /* this does not always pass on the exit code to the Manx
6622 compiler. why? */
6623}
6624
6625/*
6626 * ":qall": try to quit all windows
6627 */
6628 static void
6629ex_quit_all(eap)
6630 exarg_T *eap;
6631{
6632# ifdef FEAT_CMDWIN
6633 if (cmdwin_type != 0)
6634 {
6635 if (eap->forceit)
6636 cmdwin_result = K_XF1; /* ex_window() takes care of this */
6637 else
6638 cmdwin_result = K_XF2;
6639 return;
6640 }
6641# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006642
6643 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006644 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006645 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006646 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006647 return;
6648 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006649#ifdef FEAT_AUTOCMD
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01006650 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
6651 /* Refuse to quit when locked or when the buffer in the last window is
6652 * being closed (can only happen in autocommands). */
6653 if (curbuf_locked() || (curbuf->b_nwindows == 1 && curbuf->b_closing))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006654 return;
6655#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006656
Bram Moolenaar071d4272004-06-13 20:20:40 +00006657 exiting = TRUE;
6658 if (eap->forceit || !check_changed_any(FALSE))
6659 getout(0);
6660 not_exiting();
6661}
6662
Bram Moolenaard9967712006-03-11 21:18:15 +00006663#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006664/*
6665 * ":close": close current window, unless it is the last one
6666 */
6667 static void
6668ex_close(eap)
6669 exarg_T *eap;
6670{
6671# ifdef FEAT_CMDWIN
6672 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02006673 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006674 else
6675# endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006676 if (!text_locked()
6677#ifdef FEAT_AUTOCMD
6678 && !curbuf_locked()
6679#endif
6680 )
Bram Moolenaarf740b292006-02-16 22:11:02 +00006681 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006682}
6683
Bram Moolenaard9967712006-03-11 21:18:15 +00006684# ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006685/*
6686 * ":pclose": Close any preview window.
6687 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006688 static void
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006689ex_pclose(eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006690 exarg_T *eap;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006691{
6692 win_T *win;
6693
6694 for (win = firstwin; win != NULL; win = win->w_next)
6695 if (win->w_p_pvw)
6696 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006697 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006698 break;
6699 }
6700}
Bram Moolenaard9967712006-03-11 21:18:15 +00006701# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006702
Bram Moolenaarf740b292006-02-16 22:11:02 +00006703/*
6704 * Close window "win" and take care of handling closing the last window for a
6705 * modified buffer.
6706 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006707 static void
Bram Moolenaarf740b292006-02-16 22:11:02 +00006708ex_win_close(forceit, win, tp)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006709 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006710 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006711 tabpage_T *tp; /* NULL or the tab page "win" is in */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006712{
6713 int need_hide;
6714 buf_T *buf = win->w_buffer;
6715
6716 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006717 if (need_hide && !P_HID(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006718 {
Bram Moolenaard9967712006-03-11 21:18:15 +00006719# if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006720 if ((p_confirm || cmdmod.confirm) && p_write)
6721 {
6722 dialog_changed(buf, FALSE);
6723 if (buf_valid(buf) && bufIsChanged(buf))
6724 return;
6725 need_hide = FALSE;
6726 }
6727 else
Bram Moolenaard9967712006-03-11 21:18:15 +00006728# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006729 {
6730 EMSG(_(e_nowrtmsg));
6731 return;
6732 }
6733 }
6734
Bram Moolenaard9967712006-03-11 21:18:15 +00006735# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006736 need_mouse_correct = TRUE;
Bram Moolenaard9967712006-03-11 21:18:15 +00006737# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006738
Bram Moolenaar071d4272004-06-13 20:20:40 +00006739 /* free buffer when not hiding it or when it's a scratch buffer */
Bram Moolenaarf740b292006-02-16 22:11:02 +00006740 if (tp == NULL)
6741 win_close(win, !need_hide && !P_HID(buf));
6742 else
6743 win_close_othertab(win, !need_hide && !P_HID(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006744}
6745
Bram Moolenaar071d4272004-06-13 20:20:40 +00006746/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006747 * ":tabclose": close current tab page, unless it is the last one.
6748 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006749 */
6750 static void
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006751ex_tabclose(eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006752 exarg_T *eap;
6753{
Bram Moolenaarf740b292006-02-16 22:11:02 +00006754 tabpage_T *tp;
6755
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006756# ifdef FEAT_CMDWIN
6757 if (cmdwin_type != 0)
6758 cmdwin_result = K_IGNORE;
6759 else
6760# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006761 if (first_tabpage->tp_next == NULL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00006762 EMSG(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00006763 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006764 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006765 if (eap->addr_count > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006766 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006767 tp = find_tabpage((int)eap->line2);
6768 if (tp == NULL)
6769 {
6770 beep_flush();
6771 return;
6772 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00006773 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006774 {
6775 tabpage_close_other(tp, eap->forceit);
6776 return;
6777 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006778 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006779 if (!text_locked()
6780#ifdef FEAT_AUTOCMD
6781 && !curbuf_locked()
6782#endif
6783 )
Bram Moolenaarf740b292006-02-16 22:11:02 +00006784 tabpage_close(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006785 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006786}
6787
6788/*
6789 * ":tabonly": close all tab pages except the current one
6790 */
6791 static void
6792ex_tabonly(eap)
6793 exarg_T *eap;
6794{
6795 tabpage_T *tp;
6796 int done;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006797
6798# ifdef FEAT_CMDWIN
6799 if (cmdwin_type != 0)
6800 cmdwin_result = K_IGNORE;
6801 else
6802# endif
6803 if (first_tabpage->tp_next == NULL)
6804 MSG(_("Already only one tab page"));
6805 else
6806 {
6807 /* Repeat this up to a 1000 times, because autocommands may mess
6808 * up the lists. */
6809 for (done = 0; done < 1000; ++done)
6810 {
6811 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6812 if (tp->tp_topframe != topframe)
6813 {
6814 tabpage_close_other(tp, eap->forceit);
6815 /* if we failed to close it quit */
6816 if (valid_tabpage(tp))
6817 done = 1000;
6818 /* start over, "tp" is now invalid */
6819 break;
6820 }
6821 if (first_tabpage->tp_next == NULL)
6822 break;
6823 }
6824 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006825}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006826
6827/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006828 * Close the current tab page.
6829 */
6830 void
6831tabpage_close(forceit)
6832 int forceit;
6833{
6834 /* First close all the windows but the current one. If that worked then
6835 * close the last window in this tab, that will close it. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006836 if (lastwin != firstwin)
6837 close_others(TRUE, forceit);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006838 if (lastwin == firstwin)
6839 ex_win_close(forceit, curwin, NULL);
6840# ifdef FEAT_GUI
6841 need_mouse_correct = TRUE;
6842# endif
6843}
6844
6845/*
6846 * Close tab page "tp", which is not the current tab page.
6847 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006848 * Also takes care of the tab pages line disappearing when closing the
6849 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00006850 */
6851 void
6852tabpage_close_other(tp, forceit)
6853 tabpage_T *tp;
6854 int forceit;
6855{
6856 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006857 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006858 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006859
6860 /* Limit to 1000 windows, autocommands may add a window while we close
6861 * one. OK, so I'm paranoid... */
6862 while (++done < 1000)
6863 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006864 wp = tp->tp_firstwin;
6865 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006866
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006867 /* Autocommands may delete the tab page under our fingers and we may
6868 * fail to close a window with a modified buffer. */
6869 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006870 break;
6871 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006872
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006873 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006874 if (h != tabline_height())
6875 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006876}
6877
6878/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006879 * ":only".
6880 */
6881 static void
6882ex_only(eap)
6883 exarg_T *eap;
6884{
6885# ifdef FEAT_GUI
6886 need_mouse_correct = TRUE;
6887# endif
6888 close_others(TRUE, eap->forceit);
6889}
6890
6891/*
6892 * ":all" and ":sall".
Bram Moolenaard9967712006-03-11 21:18:15 +00006893 * Also used for ":tab drop file ..." after setting the argument list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006894 */
Bram Moolenaard9967712006-03-11 21:18:15 +00006895 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00006896ex_all(eap)
6897 exarg_T *eap;
6898{
6899 if (eap->addr_count == 0)
6900 eap->line2 = 9999;
Bram Moolenaard9967712006-03-11 21:18:15 +00006901 do_arg_all((int)eap->line2, eap->forceit, eap->cmdidx == CMD_drop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006902}
6903#endif /* FEAT_WINDOWS */
6904
6905 static void
6906ex_hide(eap)
6907 exarg_T *eap;
6908{
6909 if (*eap->arg != NUL && check_nextcmd(eap->arg) == NULL)
6910 eap->errmsg = e_invarg;
6911 else
6912 {
6913 /* ":hide" or ":hide | cmd": hide current window */
6914 eap->nextcmd = check_nextcmd(eap->arg);
6915#ifdef FEAT_WINDOWS
6916 if (!eap->skip)
6917 {
6918# ifdef FEAT_GUI
6919 need_mouse_correct = TRUE;
6920# endif
6921 win_close(curwin, FALSE); /* don't free buffer */
6922 }
6923#endif
6924 }
6925}
6926
6927/*
6928 * ":stop" and ":suspend": Suspend Vim.
6929 */
6930 static void
6931ex_stop(eap)
6932 exarg_T *eap;
6933{
6934 /*
6935 * Disallow suspending for "rvim".
6936 */
6937 if (!check_restricted()
6938#ifdef WIN3264
6939 /*
6940 * Check if external commands are allowed now.
6941 */
6942 && can_end_termcap_mode(TRUE)
6943#endif
6944 )
6945 {
6946 if (!eap->forceit)
6947 autowrite_all();
6948 windgoto((int)Rows - 1, 0);
6949 out_char('\n');
6950 out_flush();
6951 stoptermcap();
6952 out_flush(); /* needed for SUN to restore xterm buffer */
6953#ifdef FEAT_TITLE
6954 mch_restore_title(3); /* restore window titles */
6955#endif
6956 ui_suspend(); /* call machine specific function */
6957#ifdef FEAT_TITLE
6958 maketitle();
6959 resettitle(); /* force updating the title */
6960#endif
6961 starttermcap();
6962 scroll_start(); /* scroll screen before redrawing */
6963 redraw_later_clear();
6964 shell_resized(); /* may have resized window */
6965 }
6966}
6967
6968/*
6969 * ":exit", ":xit" and ":wq": Write file and exit Vim.
6970 */
6971 static void
6972ex_exit(eap)
6973 exarg_T *eap;
6974{
6975#ifdef FEAT_CMDWIN
6976 if (cmdwin_type != 0)
6977 {
6978 cmdwin_result = Ctrl_C;
6979 return;
6980 }
6981#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006982 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006983 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006984 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006985 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006986 return;
6987 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006988#ifdef FEAT_AUTOCMD
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01006989 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
6990 /* Refuse to quit when locked or when the buffer in the last window is
6991 * being closed (can only happen in autocommands). */
6992 if (curbuf_locked() || (curbuf->b_nwindows == 1 && curbuf->b_closing))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006993 return;
6994#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006995
6996 /*
6997 * if more files or windows we won't exit
6998 */
6999 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
7000 exiting = TRUE;
7001 if ( ((eap->cmdidx == CMD_wq
7002 || curbufIsChanged())
7003 && do_write(eap) == FAIL)
7004 || check_more(TRUE, eap->forceit) == FAIL
7005 || (only_one_window() && check_changed_any(eap->forceit)))
7006 {
7007 not_exiting();
7008 }
7009 else
7010 {
7011#ifdef FEAT_WINDOWS
7012 if (only_one_window()) /* quit last window, exit Vim */
7013#endif
7014 getout(0);
7015#ifdef FEAT_WINDOWS
7016# ifdef FEAT_GUI
7017 need_mouse_correct = TRUE;
7018# endif
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02007019 /* Quit current window, may free the buffer. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007020 win_close(curwin, !P_HID(curwin->w_buffer));
7021#endif
7022 }
7023}
7024
7025/*
7026 * ":print", ":list", ":number".
7027 */
7028 static void
7029ex_print(eap)
7030 exarg_T *eap;
7031{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007032 if (curbuf->b_ml.ml_flags & ML_EMPTY)
7033 EMSG(_(e_emptybuf));
7034 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007035 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007036 for ( ;!got_int; ui_breakcheck())
7037 {
7038 print_line(eap->line1,
7039 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
7040 || (eap->flags & EXFLAG_NR)),
7041 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
7042 if (++eap->line1 > eap->line2)
7043 break;
7044 out_flush(); /* show one line at a time */
7045 }
7046 setpcmark();
7047 /* put cursor at last line */
7048 curwin->w_cursor.lnum = eap->line2;
7049 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007050 }
7051
Bram Moolenaar071d4272004-06-13 20:20:40 +00007052 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007053}
7054
7055#ifdef FEAT_BYTEOFF
7056 static void
7057ex_goto(eap)
7058 exarg_T *eap;
7059{
7060 goto_byte(eap->line2);
7061}
7062#endif
7063
7064/*
7065 * ":shell".
7066 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007067 static void
7068ex_shell(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007069 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007070{
7071 do_shell(NULL, 0);
7072}
7073
7074#if (defined(FEAT_WINDOWS) && defined(HAVE_DROP_FILE)) \
7075 || (defined(FEAT_GUI_GTK) && defined(FEAT_DND)) \
Bram Moolenaar371baa92005-12-29 22:43:53 +00007076 || defined(FEAT_GUI_MSWIN) \
7077 || defined(FEAT_GUI_MAC) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00007078 || defined(PROTO)
7079
7080/*
7081 * Handle a file drop. The code is here because a drop is *nearly* like an
7082 * :args command, but not quite (we have a list of exact filenames, so we
7083 * don't want to (a) parse a command line, or (b) expand wildcards. So the
7084 * code is very similar to :args and hence needs access to a lot of the static
7085 * functions in this file.
7086 *
7087 * The list should be allocated using alloc(), as should each item in the
7088 * list. This function takes over responsibility for freeing the list.
7089 *
Bram Moolenaar81870892007-11-11 18:17:28 +00007090 * XXX The list is made into the argument list. This is freed using
Bram Moolenaar071d4272004-06-13 20:20:40 +00007091 * FreeWild(), which does a series of vim_free() calls, unless the two defines
7092 * __EMX__ and __ALWAYS_HAS_TRAILING_NUL_POINTER are set. In this case, a
7093 * routine _fnexplodefree() is used. This may cause problems, but as the drop
7094 * file functionality is (currently) not in EMX this is not presently a
7095 * problem.
7096 */
7097 void
7098handle_drop(filec, filev, split)
7099 int filec; /* the number of files dropped */
7100 char_u **filev; /* the list of files dropped */
7101 int split; /* force splitting the window */
7102{
7103 exarg_T ea;
7104 int save_msg_scroll = msg_scroll;
7105
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007106 /* Postpone this while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007107 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007108 return;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007109#ifdef FEAT_AUTOCMD
7110 if (curbuf_locked())
7111 return;
7112#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00007113 /* When the screen is being updated we should not change buffers and
7114 * windows structures, it may cause freed memory to be used. */
7115 if (updating_screen)
7116 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007117
7118 /* Check whether the current buffer is changed. If so, we will need
7119 * to split the current window or data could be lost.
7120 * We don't need to check if the 'hidden' option is set, as in this
7121 * case the buffer won't be lost.
7122 */
7123 if (!P_HID(curbuf) && !split)
7124 {
7125 ++emsg_off;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01007126 split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007127 --emsg_off;
7128 }
7129 if (split)
7130 {
7131# ifdef FEAT_WINDOWS
7132 if (win_split(0, 0) == FAIL)
7133 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007134 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007135
7136 /* When splitting the window, create a new alist. Otherwise the
7137 * existing one is overwritten. */
7138 alist_unlink(curwin->w_alist);
7139 alist_new();
7140# else
7141 return; /* can't split, always fail */
7142# endif
7143 }
7144
7145 /*
7146 * Set up the new argument list.
7147 */
Bram Moolenaar86b68352004-12-27 21:59:20 +00007148 alist_set(ALIST(curwin), filec, filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007149
7150 /*
7151 * Move to the first file.
7152 */
7153 /* Fake up a minimal "next" command for do_argfile() */
7154 vim_memset(&ea, 0, sizeof(ea));
7155 ea.cmd = (char_u *)"next";
7156 do_argfile(&ea, 0);
7157
7158 /* do_ecmd() may set need_start_insertmode, but since we never left Insert
7159 * mode that is not needed here. */
7160 need_start_insertmode = FALSE;
7161
7162 /* Restore msg_scroll, otherwise a following command may cause scrolling
7163 * unexpectedly. The screen will be redrawn by the caller, thus
7164 * msg_scroll being set by displaying a message is irrelevant. */
7165 msg_scroll = save_msg_scroll;
7166}
7167#endif
7168
Bram Moolenaar071d4272004-06-13 20:20:40 +00007169/*
7170 * Clear an argument list: free all file names and reset it to zero entries.
7171 */
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007172 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00007173alist_clear(al)
7174 alist_T *al;
7175{
7176 while (--al->al_ga.ga_len >= 0)
7177 vim_free(AARGLIST(al)[al->al_ga.ga_len].ae_fname);
7178 ga_clear(&al->al_ga);
7179}
7180
7181/*
7182 * Init an argument list.
7183 */
7184 void
7185alist_init(al)
7186 alist_T *al;
7187{
7188 ga_init2(&al->al_ga, (int)sizeof(aentry_T), 5);
7189}
7190
7191#if defined(FEAT_WINDOWS) || defined(PROTO)
7192
7193/*
7194 * Remove a reference from an argument list.
7195 * Ignored when the argument list is the global one.
7196 * If the argument list is no longer used by any window, free it.
7197 */
7198 void
7199alist_unlink(al)
7200 alist_T *al;
7201{
7202 if (al != &global_alist && --al->al_refcount <= 0)
7203 {
7204 alist_clear(al);
7205 vim_free(al);
7206 }
7207}
7208
7209# if defined(FEAT_LISTCMDS) || defined(HAVE_DROP_FILE) || defined(PROTO)
7210/*
7211 * Create a new argument list and use it for the current window.
7212 */
7213 void
7214alist_new()
7215{
7216 curwin->w_alist = (alist_T *)alloc((unsigned)sizeof(alist_T));
7217 if (curwin->w_alist == NULL)
7218 {
7219 curwin->w_alist = &global_alist;
7220 ++global_alist.al_refcount;
7221 }
7222 else
7223 {
7224 curwin->w_alist->al_refcount = 1;
7225 alist_init(curwin->w_alist);
7226 }
7227}
7228# endif
7229#endif
7230
7231#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE) || defined(PROTO)
7232/*
7233 * Expand the file names in the global argument list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00007234 * If "fnum_list" is not NULL, use "fnum_list[fnum_len]" as a list of buffer
7235 * numbers to be re-used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007236 */
7237 void
Bram Moolenaar86b68352004-12-27 21:59:20 +00007238alist_expand(fnum_list, fnum_len)
7239 int *fnum_list;
7240 int fnum_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007241{
7242 char_u **old_arg_files;
Bram Moolenaar86b68352004-12-27 21:59:20 +00007243 int old_arg_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007244 char_u **new_arg_files;
7245 int new_arg_file_count;
7246 char_u *save_p_su = p_su;
7247 int i;
7248
7249 /* Don't use 'suffixes' here. This should work like the shell did the
7250 * expansion. Also, the vimrc file isn't read yet, thus the user
7251 * can't set the options. */
7252 p_su = empty_option;
7253 old_arg_files = (char_u **)alloc((unsigned)(sizeof(char_u *) * GARGCOUNT));
7254 if (old_arg_files != NULL)
7255 {
7256 for (i = 0; i < GARGCOUNT; ++i)
Bram Moolenaar86b68352004-12-27 21:59:20 +00007257 old_arg_files[i] = vim_strsave(GARGLIST[i].ae_fname);
7258 old_arg_count = GARGCOUNT;
7259 if (expand_wildcards(old_arg_count, old_arg_files,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007260 &new_arg_file_count, &new_arg_files,
Bram Moolenaarb5609832011-07-20 15:04:58 +02007261 EW_FILE|EW_NOTFOUND|EW_ADDSLASH|EW_NOERROR) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007262 && new_arg_file_count > 0)
7263 {
Bram Moolenaar86b68352004-12-27 21:59:20 +00007264 alist_set(&global_alist, new_arg_file_count, new_arg_files,
7265 TRUE, fnum_list, fnum_len);
7266 FreeWild(old_arg_count, old_arg_files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007267 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007268 }
7269 p_su = save_p_su;
7270}
7271#endif
7272
7273/*
7274 * Set the argument list for the current window.
7275 * Takes over the allocated files[] and the allocated fnames in it.
7276 */
7277 void
Bram Moolenaar86b68352004-12-27 21:59:20 +00007278alist_set(al, count, files, use_curbuf, fnum_list, fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007279 alist_T *al;
7280 int count;
7281 char_u **files;
7282 int use_curbuf;
Bram Moolenaar86b68352004-12-27 21:59:20 +00007283 int *fnum_list;
7284 int fnum_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007285{
7286 int i;
7287
7288 alist_clear(al);
7289 if (ga_grow(&al->al_ga, count) == OK)
7290 {
7291 for (i = 0; i < count; ++i)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007292 {
7293 if (got_int)
7294 {
7295 /* When adding many buffers this can take a long time. Allow
7296 * interrupting here. */
7297 while (i < count)
7298 vim_free(files[i++]);
7299 break;
7300 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00007301
7302 /* May set buffer name of a buffer previously used for the
7303 * argument list, so that it's re-used by alist_add. */
7304 if (fnum_list != NULL && i < fnum_len)
7305 buf_set_name(fnum_list[i], files[i]);
7306
Bram Moolenaar071d4272004-06-13 20:20:40 +00007307 alist_add(al, files[i], use_curbuf ? 2 : 1);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007308 ui_breakcheck();
7309 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007310 vim_free(files);
7311 }
7312 else
7313 FreeWild(count, files);
7314#ifdef FEAT_WINDOWS
7315 if (al == &global_alist)
7316#endif
7317 arg_had_last = FALSE;
7318}
7319
7320/*
7321 * Add file "fname" to argument list "al".
7322 * "fname" must have been allocated and "al" must have been checked for room.
7323 */
7324 void
7325alist_add(al, fname, set_fnum)
7326 alist_T *al;
7327 char_u *fname;
7328 int set_fnum; /* 1: set buffer number; 2: re-use curbuf */
7329{
7330 if (fname == NULL) /* don't add NULL file names */
7331 return;
7332#ifdef BACKSLASH_IN_FILENAME
7333 slash_adjust(fname);
7334#endif
7335 AARGLIST(al)[al->al_ga.ga_len].ae_fname = fname;
7336 if (set_fnum > 0)
7337 AARGLIST(al)[al->al_ga.ga_len].ae_fnum =
7338 buflist_add(fname, BLN_LISTED | (set_fnum == 2 ? BLN_CURBUF : 0));
7339 ++al->al_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007340}
7341
7342#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
7343/*
7344 * Adjust slashes in file names. Called after 'shellslash' was set.
7345 */
7346 void
7347alist_slash_adjust()
7348{
7349 int i;
7350# ifdef FEAT_WINDOWS
7351 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007352 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007353# endif
7354
7355 for (i = 0; i < GARGCOUNT; ++i)
7356 if (GARGLIST[i].ae_fname != NULL)
7357 slash_adjust(GARGLIST[i].ae_fname);
7358# ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00007359 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007360 if (wp->w_alist != &global_alist)
7361 for (i = 0; i < WARGCOUNT(wp); ++i)
7362 if (WARGLIST(wp)[i].ae_fname != NULL)
7363 slash_adjust(WARGLIST(wp)[i].ae_fname);
7364# endif
7365}
7366#endif
7367
7368/*
7369 * ":preserve".
7370 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007371 static void
7372ex_preserve(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007373 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007374{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007375 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007376 ml_preserve(curbuf, TRUE);
7377}
7378
7379/*
7380 * ":recover".
7381 */
7382 static void
7383ex_recover(eap)
7384 exarg_T *eap;
7385{
7386 /* Set recoverymode right away to avoid the ATTENTION prompt. */
7387 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01007388 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
7389 | CCGD_MULTWIN
7390 | (eap->forceit ? CCGD_FORCEIT : 0)
7391 | CCGD_EXCMD)
7392
Bram Moolenaar071d4272004-06-13 20:20:40 +00007393 && (*eap->arg == NUL
7394 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
7395 ml_recover();
7396 recoverymode = FALSE;
7397}
7398
7399/*
7400 * Command modifier used in a wrong way.
7401 */
7402 static void
7403ex_wrongmodifier(eap)
7404 exarg_T *eap;
7405{
7406 eap->errmsg = e_invcmd;
7407}
7408
7409#ifdef FEAT_WINDOWS
7410/*
7411 * :sview [+command] file split window with new file, read-only
7412 * :split [[+command] file] split window with current or new file
7413 * :vsplit [[+command] file] split window vertically with current or new file
7414 * :new [[+command] file] split window with no or new file
7415 * :vnew [[+command] file] split vertically window with no or new file
7416 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007417 *
7418 * :tabedit open new Tab page with empty window
7419 * :tabedit [+command] file open new Tab page and edit "file"
7420 * :tabnew [[+command] file] just like :tabedit
7421 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007422 */
7423 void
7424ex_splitview(eap)
7425 exarg_T *eap;
7426{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007427 win_T *old_curwin = curwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007428# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007429 char_u *fname = NULL;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007430# endif
7431# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007432 int browse_flag = cmdmod.browse;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007433# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007434
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007435# ifndef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007436 if (eap->cmdidx == CMD_vsplit || eap->cmdidx == CMD_vnew)
7437 {
7438 ex_ni(eap);
7439 return;
7440 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007441# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007442
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007443# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007444 need_mouse_correct = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007445# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007446
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007447# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00007448 /* A ":split" in the quickfix window works like ":new". Don't want two
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007449 * quickfix windows. But it's OK when doing ":tab split". */
7450 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007451 {
7452 if (eap->cmdidx == CMD_split)
7453 eap->cmdidx = CMD_new;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007454# ifdef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007455 if (eap->cmdidx == CMD_vsplit)
7456 eap->cmdidx = CMD_vnew;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007457# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007458 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007459# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007460
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007461# ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007462 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007463 {
7464 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
7465 FNAME_MESS, TRUE, curbuf->b_ffname);
7466 if (fname == NULL)
7467 goto theend;
7468 eap->arg = fname;
7469 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007470# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007471 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007472# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007473# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007474# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007475 if (cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007476# ifdef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007477 && eap->cmdidx != CMD_vnew
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007478# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007479 && eap->cmdidx != CMD_new)
7480 {
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007481# ifdef FEAT_AUTOCMD
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007482 if (
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007483# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007484 !gui.in_use &&
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007485# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007486 au_has_group((char_u *)"FileExplorer"))
7487 {
7488 /* No browsing supported but we do have the file explorer:
7489 * Edit the directory. */
7490 if (*eap->arg == NUL || !mch_isdir(eap->arg))
7491 eap->arg = (char_u *)".";
7492 }
7493 else
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007494# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007495 {
7496 fname = do_browse(0, (char_u *)_("Edit File in new window"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007497 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007498 if (fname == NULL)
7499 goto theend;
7500 eap->arg = fname;
7501 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007502 }
7503 cmdmod.browse = FALSE; /* Don't browse again in do_ecmd(). */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007504# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007505
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007506 /*
7507 * Either open new tab page or split the window.
7508 */
7509 if (eap->cmdidx == CMD_tabedit
7510 || eap->cmdidx == CMD_tabfind
7511 || eap->cmdidx == CMD_tabnew)
7512 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00007513 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
7514 : eap->addr_count == 0 ? 0
7515 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007516 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00007517 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007518
7519 /* set the alternate buffer for the window we came from */
7520 if (curwin != old_curwin
7521 && win_valid(old_curwin)
7522 && old_curwin->w_buffer != curbuf
7523 && !cmdmod.keepalt)
7524 old_curwin->w_alt_fnum = curbuf->b_fnum;
7525 }
7526 }
7527 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007528 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
7529 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007530# ifdef FEAT_SCROLLBIND
Bram Moolenaar071d4272004-06-13 20:20:40 +00007531 /* Reset 'scrollbind' when editing another file, but keep it when
7532 * doing ":split" without arguments. */
7533 if (*eap->arg != NUL
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007534# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007535 || cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007536# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007537 )
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007538 {
7539 RESET_BINDING(curwin);
7540 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007541 else
7542 do_check_scrollbind(FALSE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007543# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007544 do_exedit(eap, old_curwin);
7545 }
7546
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007547# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007548 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007549# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007550
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007551# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007552theend:
7553 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007554# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007555}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007556
7557/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007558 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007559 */
7560 void
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007561tabpage_new()
7562{
7563 exarg_T ea;
7564
7565 vim_memset(&ea, 0, sizeof(ea));
7566 ea.cmdidx = CMD_tabnew;
7567 ea.cmd = (char_u *)"tabn";
7568 ea.arg = (char_u *)"";
7569 ex_splitview(&ea);
7570}
7571
7572/*
7573 * :tabnext command
7574 */
7575 static void
7576ex_tabnext(eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007577 exarg_T *eap;
7578{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007579 switch (eap->cmdidx)
7580 {
7581 case CMD_tabfirst:
7582 case CMD_tabrewind:
7583 goto_tabpage(1);
7584 break;
7585 case CMD_tablast:
7586 goto_tabpage(9999);
7587 break;
7588 case CMD_tabprevious:
7589 case CMD_tabNext:
7590 goto_tabpage(eap->addr_count == 0 ? -1 : -(int)eap->line2);
7591 break;
7592 default: /* CMD_tabnext */
7593 goto_tabpage(eap->addr_count == 0 ? 0 : (int)eap->line2);
7594 break;
7595 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007596}
7597
7598/*
7599 * :tabmove command
7600 */
7601 static void
7602ex_tabmove(eap)
7603 exarg_T *eap;
7604{
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02007605 int tab_number = 9999;
7606
7607 if (eap->arg && *eap->arg != NUL)
7608 {
7609 char_u *p = eap->arg;
7610 int relative = 0; /* argument +N/-N means: move N places to the
7611 * right/left relative to the current position. */
7612
7613 if (*eap->arg == '-')
7614 {
7615 relative = -1;
7616 p = eap->arg + 1;
7617 }
7618 else if (*eap->arg == '+')
7619 {
7620 relative = 1;
7621 p = eap->arg + 1;
7622 }
7623 else
7624 p = eap->arg;
7625
7626 if (p == skipdigits(p))
7627 {
7628 /* No numbers as argument. */
7629 eap->errmsg = e_invarg;
7630 return;
7631 }
7632
7633 tab_number = getdigits(&p);
7634 if (relative != 0)
7635 tab_number = tab_number * relative + tabpage_index(curtab) - 1;;
7636 }
7637 else if (eap->addr_count != 0)
7638 tab_number = eap->line2;
7639
7640 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007641}
7642
7643/*
7644 * :tabs command: List tabs and their contents.
7645 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007646 static void
7647ex_tabs(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007648 exarg_T *eap UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007649{
7650 tabpage_T *tp;
7651 win_T *wp;
7652 int tabcount = 1;
7653
7654 msg_start();
7655 msg_scroll = TRUE;
7656 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
7657 {
7658 msg_putchar('\n');
7659 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
7660 msg_outtrans_attr(IObuff, hl_attr(HLF_T));
7661 out_flush(); /* output one line at a time */
7662 ui_breakcheck();
7663
Bram Moolenaar030f0df2006-02-21 22:02:53 +00007664 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007665 wp = firstwin;
7666 else
7667 wp = tp->tp_firstwin;
7668 for ( ; wp != NULL && !got_int; wp = wp->w_next)
7669 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007670 msg_putchar('\n');
7671 msg_putchar(wp == curwin ? '>' : ' ');
7672 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007673 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
7674 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007675 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02007676 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007677 else
7678 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
7679 IObuff, IOSIZE, TRUE);
7680 msg_outtrans(IObuff);
7681 out_flush(); /* output one line at a time */
7682 ui_breakcheck();
7683 }
7684 }
7685}
7686
7687#endif /* FEAT_WINDOWS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007688
7689/*
7690 * ":mode": Set screen mode.
7691 * If no argument given, just get the screen size and redraw.
7692 */
7693 static void
7694ex_mode(eap)
7695 exarg_T *eap;
7696{
7697 if (*eap->arg == NUL)
7698 shell_resized();
7699 else
7700 mch_screenmode(eap->arg);
7701}
7702
7703#ifdef FEAT_WINDOWS
7704/*
7705 * ":resize".
7706 * set, increment or decrement current window height
7707 */
7708 static void
7709ex_resize(eap)
7710 exarg_T *eap;
7711{
7712 int n;
7713 win_T *wp = curwin;
7714
7715 if (eap->addr_count > 0)
7716 {
7717 n = eap->line2;
7718 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
7719 ;
7720 }
7721
7722#ifdef FEAT_GUI
7723 need_mouse_correct = TRUE;
7724#endif
7725 n = atol((char *)eap->arg);
7726#ifdef FEAT_VERTSPLIT
7727 if (cmdmod.split & WSP_VERT)
7728 {
7729 if (*eap->arg == '-' || *eap->arg == '+')
7730 n += W_WIDTH(curwin);
7731 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
7732 n = 9999;
7733 win_setwidth_win((int)n, wp);
7734 }
7735 else
7736#endif
7737 {
7738 if (*eap->arg == '-' || *eap->arg == '+')
7739 n += curwin->w_height;
7740 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
7741 n = 9999;
7742 win_setheight_win((int)n, wp);
7743 }
7744}
7745#endif
7746
7747/*
7748 * ":find [+command] <file>" command.
7749 */
7750 static void
7751ex_find(eap)
7752 exarg_T *eap;
7753{
7754#ifdef FEAT_SEARCHPATH
7755 char_u *fname;
7756 int count;
7757
7758 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
7759 TRUE, curbuf->b_ffname);
7760 if (eap->addr_count > 0)
7761 {
7762 /* Repeat finding the file "count" times. This matters when it
7763 * appears several times in the path. */
7764 count = eap->line2;
7765 while (fname != NULL && --count > 0)
7766 {
7767 vim_free(fname);
7768 fname = find_file_in_path(NULL, 0, FNAME_MESS,
7769 FALSE, curbuf->b_ffname);
7770 }
7771 }
7772
7773 if (fname != NULL)
7774 {
7775 eap->arg = fname;
7776#endif
7777 do_exedit(eap, NULL);
7778#ifdef FEAT_SEARCHPATH
7779 vim_free(fname);
7780 }
7781#endif
7782}
7783
7784/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00007785 * ":open" simulation: for now just work like ":visual".
7786 */
7787 static void
7788ex_open(eap)
7789 exarg_T *eap;
7790{
7791 regmatch_T regmatch;
7792 char_u *p;
7793
7794 curwin->w_cursor.lnum = eap->line2;
7795 beginline(BL_SOL | BL_FIX);
7796 if (*eap->arg == '/')
7797 {
7798 /* ":open /pattern/": put cursor in column found with pattern */
7799 ++eap->arg;
7800 p = skip_regexp(eap->arg, '/', p_magic, NULL);
7801 *p = NUL;
7802 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
7803 if (regmatch.regprog != NULL)
7804 {
7805 regmatch.rm_ic = p_ic;
7806 p = ml_get_curline();
7807 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007808 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007809 else
7810 EMSG(_(e_nomatch));
Bram Moolenaar473de612013-06-08 18:19:48 +02007811 vim_regfree(regmatch.regprog);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007812 }
7813 /* Move to the NUL, ignore any other arguments. */
7814 eap->arg += STRLEN(eap->arg);
7815 }
7816 check_cursor();
7817
7818 eap->cmdidx = CMD_visual;
7819 do_exedit(eap, NULL);
7820}
7821
7822/*
7823 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007824 */
7825 static void
7826ex_edit(eap)
7827 exarg_T *eap;
7828{
7829 do_exedit(eap, NULL);
7830}
7831
7832/*
7833 * ":edit <file>" command and alikes.
7834 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007835 void
7836do_exedit(eap, old_curwin)
7837 exarg_T *eap;
7838 win_T *old_curwin; /* curwin before doing a split or NULL */
7839{
7840 int n;
7841#ifdef FEAT_WINDOWS
7842 int need_hide;
7843#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00007844 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007845
7846 /*
7847 * ":vi" command ends Ex mode.
7848 */
7849 if (exmode_active && (eap->cmdidx == CMD_visual
7850 || eap->cmdidx == CMD_view))
7851 {
7852 exmode_active = FALSE;
7853 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007854 {
7855 /* Special case: ":global/pat/visual\NLvi-commands" */
7856 if (global_busy)
7857 {
7858 int rd = RedrawingDisabled;
7859 int nwr = no_wait_return;
7860 int ms = msg_scroll;
7861#ifdef FEAT_GUI
7862 int he = hold_gui_events;
7863#endif
7864
7865 if (eap->nextcmd != NULL)
7866 {
7867 stuffReadbuff(eap->nextcmd);
7868 eap->nextcmd = NULL;
7869 }
7870
7871 if (exmode_was != EXMODE_VIM)
7872 settmode(TMODE_RAW);
7873 RedrawingDisabled = 0;
7874 no_wait_return = 0;
7875 need_wait_return = FALSE;
7876 msg_scroll = 0;
7877#ifdef FEAT_GUI
7878 hold_gui_events = 0;
7879#endif
7880 must_redraw = CLEAR;
7881
7882 main_loop(FALSE, TRUE);
7883
7884 RedrawingDisabled = rd;
7885 no_wait_return = nwr;
7886 msg_scroll = ms;
7887#ifdef FEAT_GUI
7888 hold_gui_events = he;
7889#endif
7890 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007891 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007892 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007893 }
7894
7895 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007896 || eap->cmdidx == CMD_tabnew
7897 || eap->cmdidx == CMD_tabedit
Bram Moolenaar071d4272004-06-13 20:20:40 +00007898#ifdef FEAT_VERTSPLIT
7899 || eap->cmdidx == CMD_vnew
7900#endif
7901 ) && *eap->arg == NUL)
7902 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007903 /* ":new" or ":tabnew" without argument: edit an new empty buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007904 setpcmark();
7905 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007906 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
7907 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007908 }
7909 else if ((eap->cmdidx != CMD_split
7910#ifdef FEAT_VERTSPLIT
7911 && eap->cmdidx != CMD_vsplit
7912#endif
7913 )
7914 || *eap->arg != NUL
7915#ifdef FEAT_BROWSE
7916 || cmdmod.browse
7917#endif
7918 )
7919 {
Bram Moolenaar5555acc2006-04-07 21:33:12 +00007920#ifdef FEAT_AUTOCMD
7921 /* Can't edit another file when "curbuf_lock" is set. Only ":edit"
7922 * can bring us here, others are stopped earlier. */
7923 if (*eap->arg != NUL && curbuf_locked())
7924 return;
7925#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007926 n = readonlymode;
7927 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
7928 readonlymode = TRUE;
7929 else if (eap->cmdidx == CMD_enew)
7930 readonlymode = FALSE; /* 'readonly' doesn't make sense in an
7931 empty buffer */
7932 setpcmark();
7933 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
7934 NULL, eap,
7935 /* ":edit" goes to first line if Vi compatible */
7936 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
7937 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
7938 ? ECMD_ONE : eap->do_ecmd_lnum,
7939 (P_HID(curbuf) ? ECMD_HIDE : 0)
7940 + (eap->forceit ? ECMD_FORCEIT : 0)
7941#ifdef FEAT_LISTCMDS
7942 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
7943#endif
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007944 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007945 {
7946 /* Editing the file failed. If the window was split, close it. */
7947#ifdef FEAT_WINDOWS
7948 if (old_curwin != NULL)
7949 {
7950 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
7951 if (!need_hide || P_HID(curbuf))
7952 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007953# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7954 cleanup_T cs;
7955
7956 /* Reset the error/interrupt/exception state here so that
7957 * aborting() returns FALSE when closing a window. */
7958 enter_cleanup(&cs);
7959# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007960# ifdef FEAT_GUI
7961 need_mouse_correct = TRUE;
7962# endif
7963 win_close(curwin, !need_hide && !P_HID(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007964
7965# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7966 /* Restore the error/interrupt/exception state if not
7967 * discarded by a new aborting error, interrupt, or
7968 * uncaught exception. */
7969 leave_cleanup(&cs);
7970# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007971 }
7972 }
7973#endif
7974 }
7975 else if (readonlymode && curbuf->b_nwindows == 1)
7976 {
7977 /* When editing an already visited buffer, 'readonly' won't be set
7978 * but the previous value is kept. With ":view" and ":sview" we
7979 * want the file to be readonly, except when another window is
7980 * editing the same buffer. */
7981 curbuf->b_p_ro = TRUE;
7982 }
7983 readonlymode = n;
7984 }
7985 else
7986 {
7987 if (eap->do_ecmd_cmd != NULL)
7988 do_cmdline_cmd(eap->do_ecmd_cmd);
7989#ifdef FEAT_TITLE
7990 n = curwin->w_arg_idx_invalid;
7991#endif
7992 check_arg_idx(curwin);
7993#ifdef FEAT_TITLE
7994 if (n != curwin->w_arg_idx_invalid)
7995 maketitle();
7996#endif
7997 }
7998
7999#ifdef FEAT_WINDOWS
8000 /*
8001 * if ":split file" worked, set alternate file name in old window to new
8002 * file
8003 */
8004 if (old_curwin != NULL
8005 && *eap->arg != NUL
8006 && curwin != old_curwin
8007 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00008008 && old_curwin->w_buffer != curbuf
8009 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008010 old_curwin->w_alt_fnum = curbuf->b_fnum;
8011#endif
8012
8013 ex_no_reprint = TRUE;
8014}
8015
8016#ifndef FEAT_GUI
8017/*
8018 * ":gui" and ":gvim" when there is no GUI.
8019 */
8020 static void
8021ex_nogui(eap)
8022 exarg_T *eap;
8023{
8024 eap->errmsg = e_nogvim;
8025}
8026#endif
8027
8028#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
8029 static void
8030ex_tearoff(eap)
8031 exarg_T *eap;
8032{
8033 gui_make_tearoff(eap->arg);
8034}
8035#endif
8036
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008037#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008038 static void
8039ex_popup(eap)
8040 exarg_T *eap;
8041{
Bram Moolenaar97409f12005-07-08 22:17:29 +00008042 gui_make_popup(eap->arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008043}
8044#endif
8045
Bram Moolenaar071d4272004-06-13 20:20:40 +00008046 static void
8047ex_swapname(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008048 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008049{
8050 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
8051 MSG(_("No swap file"));
8052 else
8053 msg(curbuf->b_ml.ml_mfp->mf_fname);
8054}
8055
8056/*
8057 * ":syncbind" forces all 'scrollbind' windows to have the same relative
8058 * offset.
8059 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
8060 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008061 static void
8062ex_syncbind(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008063 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008064{
8065#ifdef FEAT_SCROLLBIND
8066 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008067 win_T *save_curwin = curwin;
8068 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008069 long topline;
8070 long y;
8071 linenr_T old_linenr = curwin->w_cursor.lnum;
8072
8073 setpcmark();
8074
8075 /*
8076 * determine max topline
8077 */
8078 if (curwin->w_p_scb)
8079 {
8080 topline = curwin->w_topline;
8081 for (wp = firstwin; wp; wp = wp->w_next)
8082 {
8083 if (wp->w_p_scb && wp->w_buffer)
8084 {
8085 y = wp->w_buffer->b_ml.ml_line_count - p_so;
8086 if (topline > y)
8087 topline = y;
8088 }
8089 }
8090 if (topline < 1)
8091 topline = 1;
8092 }
8093 else
8094 {
8095 topline = 1;
8096 }
8097
8098
8099 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008100 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008101 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008102 for (curwin = firstwin; curwin; curwin = curwin->w_next)
8103 {
8104 if (curwin->w_p_scb)
8105 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008106 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008107 y = topline - curwin->w_topline;
8108 if (y > 0)
8109 scrollup(y, TRUE);
8110 else
8111 scrolldown(-y, TRUE);
8112 curwin->w_scbind_pos = topline;
8113 redraw_later(VALID);
8114 cursor_correct();
8115#ifdef FEAT_WINDOWS
8116 curwin->w_redr_status = TRUE;
8117#endif
8118 }
8119 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008120 curwin = save_curwin;
8121 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008122 if (curwin->w_p_scb)
8123 {
8124 did_syncbind = TRUE;
8125 checkpcmark();
8126 if (old_linenr != curwin->w_cursor.lnum)
8127 {
8128 char_u ctrl_o[2];
8129
8130 ctrl_o[0] = Ctrl_O;
8131 ctrl_o[1] = 0;
8132 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
8133 }
8134 }
8135#endif
8136}
8137
8138
8139 static void
8140ex_read(eap)
8141 exarg_T *eap;
8142{
Bram Moolenaardf177f62005-02-22 08:39:57 +00008143 int i;
8144 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
8145 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008146
8147 if (eap->usefilter) /* :r!cmd */
8148 do_bang(1, eap, FALSE, FALSE, TRUE);
8149 else
8150 {
8151 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
8152 return;
8153
8154#ifdef FEAT_BROWSE
8155 if (cmdmod.browse)
8156 {
8157 char_u *browseFile;
8158
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008159 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008160 NULL, NULL, NULL, curbuf);
8161 if (browseFile != NULL)
8162 {
8163 i = readfile(browseFile, NULL,
8164 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8165 vim_free(browseFile);
8166 }
8167 else
8168 i = OK;
8169 }
8170 else
8171#endif
8172 if (*eap->arg == NUL)
8173 {
8174 if (check_fname() == FAIL) /* check for no file name */
8175 return;
8176 i = readfile(curbuf->b_ffname, curbuf->b_fname,
8177 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8178 }
8179 else
8180 {
8181 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
8182 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
8183 i = readfile(eap->arg, NULL,
8184 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8185
8186 }
8187 if (i == FAIL)
8188 {
8189#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
8190 if (!aborting())
8191#endif
8192 EMSG2(_(e_notopen), eap->arg);
8193 }
8194 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008195 {
8196 if (empty && exmode_active)
8197 {
8198 /* Delete the empty line that remains. Historically ex does
8199 * this but vi doesn't. */
8200 if (eap->line2 == 0)
8201 lnum = curbuf->b_ml.ml_line_count;
8202 else
8203 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008204 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008205 {
8206 ml_delete(lnum, FALSE);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008207 if (curwin->w_cursor.lnum > 1
8208 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008209 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00008210 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008211 }
8212 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008213 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008214 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008215 }
8216}
8217
Bram Moolenaarea408852005-06-25 22:49:46 +00008218static char_u *prev_dir = NULL;
8219
8220#if defined(EXITFREE) || defined(PROTO)
8221 void
8222free_cd_dir()
8223{
8224 vim_free(prev_dir);
Bram Moolenaara0174af2008-01-02 20:08:25 +00008225 prev_dir = NULL;
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00008226
8227 vim_free(globaldir);
8228 globaldir = NULL;
Bram Moolenaarea408852005-06-25 22:49:46 +00008229}
8230#endif
8231
Bram Moolenaarf4258302013-06-02 18:20:17 +02008232/*
8233 * Deal with the side effects of changing the current directory.
8234 * When "local" is TRUE then this was after an ":lcd" command.
8235 */
8236 void
8237post_chdir(local)
8238 int local;
8239{
8240 vim_free(curwin->w_localdir);
Bram Moolenaarbd2dc342014-01-10 15:53:13 +01008241 curwin->w_localdir = NULL;
Bram Moolenaarf4258302013-06-02 18:20:17 +02008242 if (local)
8243 {
8244 /* If still in global directory, need to remember current
8245 * directory as global directory. */
8246 if (globaldir == NULL && prev_dir != NULL)
8247 globaldir = vim_strsave(prev_dir);
8248 /* Remember this local directory for the window. */
8249 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8250 curwin->w_localdir = vim_strsave(NameBuff);
8251 }
8252 else
8253 {
8254 /* We are now in the global directory, no need to remember its
8255 * name. */
8256 vim_free(globaldir);
8257 globaldir = NULL;
Bram Moolenaarf4258302013-06-02 18:20:17 +02008258 }
8259
8260 shorten_fnames(TRUE);
8261}
8262
Bram Moolenaarea408852005-06-25 22:49:46 +00008263
Bram Moolenaar071d4272004-06-13 20:20:40 +00008264/*
8265 * ":cd", ":lcd", ":chdir" and ":lchdir".
8266 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00008267 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00008268ex_cd(eap)
8269 exarg_T *eap;
8270{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008271 char_u *new_dir;
8272 char_u *tofree;
8273
8274 new_dir = eap->arg;
8275#if !defined(UNIX) && !defined(VMS)
8276 /* for non-UNIX ":cd" means: print current directory */
8277 if (*new_dir == NUL)
8278 ex_pwd(NULL);
8279 else
8280#endif
8281 {
Bram Moolenaar8e8fe9b2009-03-11 14:37:32 +00008282#ifdef FEAT_AUTOCMD
8283 if (allbuf_locked())
8284 return;
8285#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00008286 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged()
8287 && !eap->forceit)
8288 {
Bram Moolenaar81870892007-11-11 18:17:28 +00008289 EMSG(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00008290 return;
8291 }
8292
Bram Moolenaar071d4272004-06-13 20:20:40 +00008293 /* ":cd -": Change to previous directory */
8294 if (STRCMP(new_dir, "-") == 0)
8295 {
8296 if (prev_dir == NULL)
8297 {
8298 EMSG(_("E186: No previous directory"));
8299 return;
8300 }
8301 new_dir = prev_dir;
8302 }
8303
8304 /* Save current directory for next ":cd -" */
8305 tofree = prev_dir;
8306 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8307 prev_dir = vim_strsave(NameBuff);
8308 else
8309 prev_dir = NULL;
8310
8311#if defined(UNIX) || defined(VMS)
8312 /* for UNIX ":cd" means: go to home directory */
8313 if (*new_dir == NUL)
8314 {
8315 /* use NameBuff for home directory name */
8316# ifdef VMS
8317 char_u *p;
8318
8319 p = mch_getenv((char_u *)"SYS$LOGIN");
8320 if (p == NULL || *p == NUL) /* empty is the same as not set */
8321 NameBuff[0] = NUL;
8322 else
Bram Moolenaarb6356332005-07-18 21:40:44 +00008323 vim_strncpy(NameBuff, p, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008324# else
8325 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
8326# endif
8327 new_dir = NameBuff;
8328 }
8329#endif
8330 if (new_dir == NULL || vim_chdir(new_dir))
8331 EMSG(_(e_failed));
8332 else
8333 {
Bram Moolenaarf4258302013-06-02 18:20:17 +02008334 post_chdir(eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008335
8336 /* Echo the new current directory if the command was typed. */
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00008337 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008338 ex_pwd(eap);
8339 }
8340 vim_free(tofree);
8341 }
8342}
8343
8344/*
8345 * ":pwd".
8346 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008347 static void
8348ex_pwd(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008349 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008350{
8351 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8352 {
8353#ifdef BACKSLASH_IN_FILENAME
8354 slash_adjust(NameBuff);
8355#endif
8356 msg(NameBuff);
8357 }
8358 else
8359 EMSG(_("E187: Unknown"));
8360}
8361
8362/*
8363 * ":=".
8364 */
8365 static void
8366ex_equal(eap)
8367 exarg_T *eap;
8368{
8369 smsg((char_u *)"%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008370 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008371}
8372
8373 static void
8374ex_sleep(eap)
8375 exarg_T *eap;
8376{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008377 int n;
8378 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008379
8380 if (cursor_valid())
8381 {
8382 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
8383 if (n >= 0)
Bram Moolenaar10395d82014-02-05 22:46:52 +01008384 windgoto((int)n, W_WINCOL(curwin) + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008385 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008386
8387 len = eap->line2;
8388 switch (*eap->arg)
8389 {
8390 case 'm': break;
8391 case NUL: len *= 1000L; break;
8392 default: EMSG2(_(e_invarg2), eap->arg); return;
8393 }
8394 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008395}
8396
8397/*
8398 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
8399 */
8400 void
8401do_sleep(msec)
8402 long msec;
8403{
8404 long done;
8405
8406 cursor_on();
8407 out_flush();
8408 for (done = 0; !got_int && done < msec; done += 1000L)
8409 {
8410 ui_delay(msec - done > 1000L ? 1000L : msec - done, TRUE);
8411 ui_breakcheck();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02008412#ifdef FEAT_NETBEANS_INTG
8413 /* Process the netbeans messages that may have been received in the
8414 * call to ui_breakcheck() when the GUI is in use. This may occur when
8415 * running a test case. */
8416 netbeans_parse_messages();
8417#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008418 }
8419}
8420
8421 static void
8422do_exmap(eap, isabbrev)
8423 exarg_T *eap;
8424 int isabbrev;
8425{
8426 int mode;
8427 char_u *cmdp;
8428
8429 cmdp = eap->cmd;
8430 mode = get_map_mode(&cmdp, eap->forceit || isabbrev);
8431
8432 switch (do_map((*cmdp == 'n') ? 2 : (*cmdp == 'u'),
8433 eap->arg, mode, isabbrev))
8434 {
8435 case 1: EMSG(_(e_invarg));
8436 break;
8437 case 2: EMSG(isabbrev ? _(e_noabbr) : _(e_nomap));
8438 break;
8439 }
8440}
8441
8442/*
8443 * ":winsize" command (obsolete).
8444 */
8445 static void
8446ex_winsize(eap)
8447 exarg_T *eap;
8448{
8449 int w, h;
8450 char_u *arg = eap->arg;
8451 char_u *p;
8452
8453 w = getdigits(&arg);
8454 arg = skipwhite(arg);
8455 p = arg;
8456 h = getdigits(&arg);
8457 if (*p != NUL && *arg == NUL)
8458 set_shellsize(w, h, TRUE);
8459 else
8460 EMSG(_("E465: :winsize requires two number arguments"));
8461}
8462
8463#ifdef FEAT_WINDOWS
8464 static void
8465ex_wincmd(eap)
8466 exarg_T *eap;
8467{
8468 int xchar = NUL;
8469 char_u *p;
8470
8471 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
8472 {
8473 /* CTRL-W g and CTRL-W CTRL-G have an extra command character */
8474 if (eap->arg[1] == NUL)
8475 {
8476 EMSG(_(e_invarg));
8477 return;
8478 }
8479 xchar = eap->arg[1];
8480 p = eap->arg + 2;
8481 }
8482 else
8483 p = eap->arg + 1;
8484
8485 eap->nextcmd = check_nextcmd(p);
8486 p = skipwhite(p);
8487 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
8488 EMSG(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02008489 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008490 {
8491 /* Pass flags on for ":vertical wincmd ]". */
8492 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008493 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008494 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
8495 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008496 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008497 }
8498}
8499#endif
8500
Bram Moolenaar843ee412004-06-30 16:16:41 +00008501#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008502/*
8503 * ":winpos".
8504 */
8505 static void
8506ex_winpos(eap)
8507 exarg_T *eap;
8508{
8509 int x, y;
8510 char_u *arg = eap->arg;
8511 char_u *p;
8512
8513 if (*arg == NUL)
8514 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00008515# if defined(FEAT_GUI) || defined(MSWIN)
8516# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008517 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00008518# else
8519 if (mch_get_winpos(&x, &y) != FAIL)
8520# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008521 {
8522 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
8523 msg(IObuff);
8524 }
8525 else
8526# endif
8527 EMSG(_("E188: Obtaining window position not implemented for this platform"));
8528 }
8529 else
8530 {
8531 x = getdigits(&arg);
8532 arg = skipwhite(arg);
8533 p = arg;
8534 y = getdigits(&arg);
8535 if (*p == NUL || *arg != NUL)
8536 {
8537 EMSG(_("E466: :winpos requires two number arguments"));
8538 return;
8539 }
8540# ifdef FEAT_GUI
8541 if (gui.in_use)
8542 gui_mch_set_winpos(x, y);
8543 else if (gui.starting)
8544 {
8545 /* Remember the coordinates for when the window is opened. */
8546 gui_win_x = x;
8547 gui_win_y = y;
8548 }
8549# ifdef HAVE_TGETENT
8550 else
8551# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00008552# else
8553# ifdef MSWIN
8554 mch_set_winpos(x, y);
8555# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008556# endif
8557# ifdef HAVE_TGETENT
8558 if (*T_CWP)
8559 term_set_winpos(x, y);
8560# endif
8561 }
8562}
8563#endif
8564
8565/*
8566 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
8567 */
8568 static void
8569ex_operators(eap)
8570 exarg_T *eap;
8571{
8572 oparg_T oa;
8573
8574 clear_oparg(&oa);
8575 oa.regname = eap->regname;
8576 oa.start.lnum = eap->line1;
8577 oa.end.lnum = eap->line2;
8578 oa.line_count = eap->line2 - eap->line1 + 1;
8579 oa.motion_type = MLINE;
8580#ifdef FEAT_VIRTUALEDIT
8581 virtual_op = FALSE;
8582#endif
8583 if (eap->cmdidx != CMD_yank) /* position cursor for undo */
8584 {
8585 setpcmark();
8586 curwin->w_cursor.lnum = eap->line1;
8587 beginline(BL_SOL | BL_FIX);
8588 }
8589
Bram Moolenaard07c6e12013-11-21 14:21:40 +01008590 if (VIsual_active)
8591 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01008592
Bram Moolenaar071d4272004-06-13 20:20:40 +00008593 switch (eap->cmdidx)
8594 {
8595 case CMD_delete:
8596 oa.op_type = OP_DELETE;
8597 op_delete(&oa);
8598 break;
8599
8600 case CMD_yank:
8601 oa.op_type = OP_YANK;
8602 (void)op_yank(&oa, FALSE, TRUE);
8603 break;
8604
8605 default: /* CMD_rshift or CMD_lshift */
Bram Moolenaar6e707362013-06-14 19:15:58 +02008606 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00008607#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02008608 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
8609#else
8610 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00008611#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02008612 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00008613 oa.op_type = OP_RSHIFT;
8614 else
8615 oa.op_type = OP_LSHIFT;
8616 op_shift(&oa, FALSE, eap->amount);
8617 break;
8618 }
8619#ifdef FEAT_VIRTUALEDIT
8620 virtual_op = MAYBE;
8621#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00008622 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008623}
8624
8625/*
8626 * ":put".
8627 */
8628 static void
8629ex_put(eap)
8630 exarg_T *eap;
8631{
8632 /* ":0put" works like ":1put!". */
8633 if (eap->line2 == 0)
8634 {
8635 eap->line2 = 1;
8636 eap->forceit = TRUE;
8637 }
8638 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008639 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
8640 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008641}
8642
8643/*
8644 * Handle ":copy" and ":move".
8645 */
8646 static void
8647ex_copymove(eap)
8648 exarg_T *eap;
8649{
8650 long n;
8651
8652 n = get_address(&eap->arg, FALSE, FALSE);
8653 if (eap->arg == NULL) /* error detected */
8654 {
8655 eap->nextcmd = NULL;
8656 return;
8657 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008658 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008659
8660 /*
8661 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
8662 */
8663 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
8664 {
8665 EMSG(_(e_invaddr));
8666 return;
8667 }
8668
8669 if (eap->cmdidx == CMD_move)
8670 {
8671 if (do_move(eap->line1, eap->line2, n) == FAIL)
8672 return;
8673 }
8674 else
8675 ex_copy(eap->line1, eap->line2, n);
8676 u_clearline();
8677 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008678 ex_may_print(eap);
8679}
8680
8681/*
8682 * Print the current line if flags were given to the Ex command.
8683 */
8684 static void
8685ex_may_print(eap)
8686 exarg_T *eap;
8687{
8688 if (eap->flags != 0)
8689 {
8690 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
8691 (eap->flags & EXFLAG_LIST));
8692 ex_no_reprint = TRUE;
8693 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008694}
8695
8696/*
8697 * ":smagic" and ":snomagic".
8698 */
8699 static void
8700ex_submagic(eap)
8701 exarg_T *eap;
8702{
8703 int magic_save = p_magic;
8704
8705 p_magic = (eap->cmdidx == CMD_smagic);
8706 do_sub(eap);
8707 p_magic = magic_save;
8708}
8709
8710/*
8711 * ":join".
8712 */
8713 static void
8714ex_join(eap)
8715 exarg_T *eap;
8716{
8717 curwin->w_cursor.lnum = eap->line1;
8718 if (eap->line1 == eap->line2)
8719 {
8720 if (eap->addr_count >= 2) /* :2,2join does nothing */
8721 return;
8722 if (eap->line2 == curbuf->b_ml.ml_line_count)
8723 {
8724 beep_flush();
8725 return;
8726 }
8727 ++eap->line2;
8728 }
Bram Moolenaar81340392012-06-06 16:12:59 +02008729 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008730 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008731 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008732}
8733
8734/*
8735 * ":[addr]@r" or ":[addr]*r": execute register
8736 */
8737 static void
8738ex_at(eap)
8739 exarg_T *eap;
8740{
8741 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00008742 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008743
8744 curwin->w_cursor.lnum = eap->line2;
8745
8746#ifdef USE_ON_FLY_SCROLL
8747 dont_scroll = TRUE; /* disallow scrolling here */
8748#endif
8749
8750 /* get the register name. No name means to use the previous one */
8751 c = *eap->arg;
8752 if (c == NUL || (c == '*' && *eap->cmd == '*'))
8753 c = '@';
Bram Moolenaard333d1e2006-11-07 17:43:47 +00008754 /* Put the register in the typeahead buffer with the "silent" flag. */
8755 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
8756 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008757 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008758 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00008759 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008760 else
8761 {
8762 int save_efr = exec_from_reg;
8763
8764 exec_from_reg = TRUE;
8765
8766 /*
8767 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00008768 * Continue until the stuff buffer is empty and all added characters
8769 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008770 */
Bram Moolenaar60462872009-11-03 11:40:19 +00008771 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008772 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
8773
8774 exec_from_reg = save_efr;
8775 }
8776}
8777
8778/*
8779 * ":!".
8780 */
8781 static void
8782ex_bang(eap)
8783 exarg_T *eap;
8784{
8785 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
8786}
8787
8788/*
8789 * ":undo".
8790 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008791 static void
8792ex_undo(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008793 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008794{
Bram Moolenaard3667a22006-03-16 21:35:52 +00008795 if (eap->addr_count == 1) /* :undo 123 */
Bram Moolenaar730cde92010-06-27 05:18:54 +02008796 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00008797 else
8798 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008799}
8800
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008801#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008802 static void
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008803ex_wundo(eap)
8804 exarg_T *eap;
8805{
8806 char_u hash[UNDO_HASH_SIZE];
8807
8808 u_compute_hash(hash);
8809 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
8810}
8811
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008812 static void
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008813ex_rundo(eap)
8814 exarg_T *eap;
8815{
8816 char_u hash[UNDO_HASH_SIZE];
8817
8818 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02008819 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008820}
8821#endif
8822
Bram Moolenaar071d4272004-06-13 20:20:40 +00008823/*
8824 * ":redo".
8825 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008826 static void
8827ex_redo(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008828 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008829{
8830 u_redo(1);
8831}
8832
8833/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008834 * ":earlier" and ":later".
8835 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008836 static void
8837ex_later(eap)
8838 exarg_T *eap;
8839{
8840 long count = 0;
8841 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008842 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008843 char_u *p = eap->arg;
8844
8845 if (*p == NUL)
8846 count = 1;
8847 else if (isdigit(*p))
8848 {
8849 count = getdigits(&p);
8850 switch (*p)
8851 {
8852 case 's': ++p; sec = TRUE; break;
8853 case 'm': ++p; sec = TRUE; count *= 60; break;
8854 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008855 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
8856 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008857 }
8858 }
8859
8860 if (*p != NUL)
8861 EMSG2(_(e_invarg2), eap->arg);
8862 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02008863 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
8864 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008865}
8866
8867/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008868 * ":redir": start/stop redirection.
8869 */
8870 static void
8871ex_redir(eap)
8872 exarg_T *eap;
8873{
8874 char *mode;
8875 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00008876 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008877
8878 if (STRICMP(eap->arg, "END") == 0)
8879 close_redir();
8880 else
8881 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008882 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008883 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008884 ++arg;
8885 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008886 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008887 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008888 mode = "a";
8889 }
8890 else
8891 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00008892 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008893
8894 close_redir();
8895
8896 /* Expand environment variables and "~/". */
Bram Moolenaarca472992005-01-21 11:46:23 +00008897 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008898 if (fname == NULL)
8899 return;
8900#ifdef FEAT_BROWSE
8901 if (cmdmod.browse)
8902 {
8903 char_u *browseFile;
8904
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008905 browseFile = do_browse(BROWSE_SAVE,
8906 (char_u *)_("Save Redirection"),
8907 fname, NULL, NULL, BROWSE_FILTER_ALL_FILES, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008908 if (browseFile == NULL)
8909 return; /* operation cancelled */
8910 vim_free(fname);
8911 fname = browseFile;
8912 eap->forceit = TRUE; /* since dialog already asked */
8913 }
8914#endif
8915
8916 redir_fd = open_exfile(fname, eap->forceit, mode);
8917 vim_free(fname);
8918 }
8919#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00008920 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008921 {
8922 /* redirect to a register a-z (resp. A-Z for appending) */
8923 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00008924 ++arg;
8925 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008926# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00008927 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00008928 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008929# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00008930 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008931 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008932 redir_reg = *arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008933 if (*arg == '>' && arg[1] == '>') /* append */
Bram Moolenaard9d30582005-05-18 22:10:28 +00008934 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008935 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008936 {
Bram Moolenaarc188b882007-10-19 14:20:54 +00008937 /* Can use both "@a" and "@a>". */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00008938 if (*arg == '>')
8939 arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008940 /* Make register empty when not using @A-@Z and the
8941 * command is valid. */
8942 if (*arg == NUL && !isupper(redir_reg))
8943 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008944 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008945 }
8946 if (*arg != NUL)
8947 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00008948 redir_reg = 0;
Bram Moolenaard9d30582005-05-18 22:10:28 +00008949 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008950 }
8951 }
8952 else if (*arg == '=' && arg[1] == '>')
8953 {
8954 int append;
8955
8956 /* redirect to a variable */
8957 close_redir();
8958 arg += 2;
8959
8960 if (*arg == '>')
8961 {
8962 ++arg;
8963 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008964 }
8965 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008966 append = FALSE;
8967
8968 if (var_redir_start(skipwhite(arg), append) == OK)
8969 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008970 }
8971#endif
8972
8973 /* TODO: redirect to a buffer */
8974
Bram Moolenaar071d4272004-06-13 20:20:40 +00008975 else
Bram Moolenaarca472992005-01-21 11:46:23 +00008976 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008977 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00008978
8979 /* Make sure redirection is not off. Can happen for cmdline completion
8980 * that indirectly invokes a command to catch its output. */
8981 if (redir_fd != NULL
8982#ifdef FEAT_EVAL
8983 || redir_reg || redir_vname
8984#endif
8985 )
8986 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008987}
8988
8989/*
8990 * ":redraw": force redraw
8991 */
8992 static void
8993ex_redraw(eap)
8994 exarg_T *eap;
8995{
8996 int r = RedrawingDisabled;
8997 int p = p_lz;
8998
8999 RedrawingDisabled = 0;
9000 p_lz = FALSE;
9001 update_topline();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009002 update_screen(eap->forceit ? CLEAR : VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009003#ifdef FEAT_TITLE
9004 if (need_maketitle)
9005 maketitle();
9006#endif
9007 RedrawingDisabled = r;
9008 p_lz = p;
9009
9010 /* Reset msg_didout, so that a message that's there is overwritten. */
9011 msg_didout = FALSE;
9012 msg_col = 0;
9013
Bram Moolenaar56667a52013-07-17 11:54:28 +02009014 /* No need to wait after an intentional redraw. */
9015 need_wait_return = FALSE;
9016
Bram Moolenaar071d4272004-06-13 20:20:40 +00009017 out_flush();
9018}
9019
9020/*
9021 * ":redrawstatus": force redraw of status line(s)
9022 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009023 static void
9024ex_redrawstatus(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009025 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009026{
9027#if defined(FEAT_WINDOWS)
9028 int r = RedrawingDisabled;
9029 int p = p_lz;
9030
9031 RedrawingDisabled = 0;
9032 p_lz = FALSE;
9033 if (eap->forceit)
9034 status_redraw_all();
9035 else
9036 status_redraw_curbuf();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009037 update_screen(VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009038 RedrawingDisabled = r;
9039 p_lz = p;
9040 out_flush();
9041#endif
9042}
9043
9044 static void
9045close_redir()
9046{
9047 if (redir_fd != NULL)
9048 {
9049 fclose(redir_fd);
9050 redir_fd = NULL;
9051 }
9052#ifdef FEAT_EVAL
9053 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009054 if (redir_vname)
9055 {
9056 var_redir_stop();
9057 redir_vname = 0;
9058 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009059#endif
9060}
9061
9062#if defined(FEAT_SESSION) && defined(USE_CRNL)
9063# define MKSESSION_NL
9064static int mksession_nl = FALSE; /* use NL only in put_eol() */
9065#endif
9066
9067/*
9068 * ":mkexrc", ":mkvimrc", ":mkview" and ":mksession".
9069 */
9070 static void
9071ex_mkrc(eap)
9072 exarg_T *eap;
9073{
9074 FILE *fd;
9075 int failed = FALSE;
9076 char_u *fname;
9077#ifdef FEAT_BROWSE
9078 char_u *browseFile = NULL;
9079#endif
9080#ifdef FEAT_SESSION
9081 int view_session = FALSE;
9082 int using_vdir = FALSE; /* using 'viewdir'? */
9083 char_u *viewFile = NULL;
9084 unsigned *flagp;
9085#endif
9086
9087 if (eap->cmdidx == CMD_mksession || eap->cmdidx == CMD_mkview)
9088 {
9089#ifdef FEAT_SESSION
9090 view_session = TRUE;
9091#else
9092 ex_ni(eap);
9093 return;
9094#endif
9095 }
9096
9097#ifdef FEAT_SESSION
Bram Moolenaar8d594672009-07-01 18:18:57 +00009098 /* Use the short file name until ":lcd" is used. We also don't use the
9099 * short file name when 'acd' is set, that is checked later. */
Bram Moolenaareeefcc72007-05-01 21:21:21 +00009100 did_lcd = FALSE;
9101
Bram Moolenaar071d4272004-06-13 20:20:40 +00009102 /* ":mkview" or ":mkview 9": generate file name with 'viewdir' */
9103 if (eap->cmdidx == CMD_mkview
9104 && (*eap->arg == NUL
9105 || (vim_isdigit(*eap->arg) && eap->arg[1] == NUL)))
9106 {
9107 eap->forceit = TRUE;
9108 fname = get_view_file(*eap->arg);
9109 if (fname == NULL)
9110 return;
9111 viewFile = fname;
9112 using_vdir = TRUE;
9113 }
9114 else
9115#endif
9116 if (*eap->arg != NUL)
9117 fname = eap->arg;
9118 else if (eap->cmdidx == CMD_mkvimrc)
9119 fname = (char_u *)VIMRC_FILE;
9120#ifdef FEAT_SESSION
9121 else if (eap->cmdidx == CMD_mksession)
9122 fname = (char_u *)SESSION_FILE;
9123#endif
9124 else
9125 fname = (char_u *)EXRC_FILE;
9126
9127#ifdef FEAT_BROWSE
9128 if (cmdmod.browse)
9129 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00009130 browseFile = do_browse(BROWSE_SAVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009131# ifdef FEAT_SESSION
9132 eap->cmdidx == CMD_mkview ? (char_u *)_("Save View") :
9133 eap->cmdidx == CMD_mksession ? (char_u *)_("Save Session") :
9134# endif
9135 (char_u *)_("Save Setup"),
9136 fname, (char_u *)"vim", NULL, BROWSE_FILTER_MACROS, NULL);
9137 if (browseFile == NULL)
9138 goto theend;
9139 fname = browseFile;
9140 eap->forceit = TRUE; /* since dialog already asked */
9141 }
9142#endif
9143
9144#if defined(FEAT_SESSION) && defined(vim_mkdir)
9145 /* When using 'viewdir' may have to create the directory. */
9146 if (using_vdir && !mch_isdir(p_vdir))
Bram Moolenaardf177f62005-02-22 08:39:57 +00009147 vim_mkdir_emsg(p_vdir, 0755);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009148#endif
9149
9150 fd = open_exfile(fname, eap->forceit, WRITEBIN);
9151 if (fd != NULL)
9152 {
9153#ifdef FEAT_SESSION
9154 if (eap->cmdidx == CMD_mkview)
9155 flagp = &vop_flags;
9156 else
9157 flagp = &ssop_flags;
9158#endif
9159
9160#ifdef MKSESSION_NL
9161 /* "unix" in 'sessionoptions': use NL line separator */
9162 if (view_session && (*flagp & SSOP_UNIX))
9163 mksession_nl = TRUE;
9164#endif
9165
9166 /* Write the version command for :mkvimrc */
9167 if (eap->cmdidx == CMD_mkvimrc)
9168 (void)put_line(fd, "version 6.0");
9169
9170#ifdef FEAT_SESSION
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009171 if (eap->cmdidx == CMD_mksession)
9172 {
9173 if (put_line(fd, "let SessionLoad = 1") == FAIL)
9174 failed = TRUE;
9175 }
9176
Bram Moolenaar071d4272004-06-13 20:20:40 +00009177 if (eap->cmdidx != CMD_mkview)
9178#endif
9179 {
9180 /* Write setting 'compatible' first, because it has side effects.
9181 * For that same reason only do it when needed. */
9182 if (p_cp)
9183 (void)put_line(fd, "if !&cp | set cp | endif");
9184 else
9185 (void)put_line(fd, "if &cp | set nocp | endif");
9186 }
9187
9188#ifdef FEAT_SESSION
9189 if (!view_session
9190 || (eap->cmdidx == CMD_mksession
9191 && (*flagp & SSOP_OPTIONS)))
9192#endif
9193 failed |= (makemap(fd, NULL) == FAIL
9194 || makeset(fd, OPT_GLOBAL, FALSE) == FAIL);
9195
9196#ifdef FEAT_SESSION
9197 if (!failed && view_session)
9198 {
9199 if (put_line(fd, "let s:so_save = &so | let s:siso_save = &siso | set so=0 siso=0") == FAIL)
9200 failed = TRUE;
9201 if (eap->cmdidx == CMD_mksession)
9202 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02009203 char_u *dirnow; /* current directory */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009204
Bram Moolenaard9462e32011-04-11 21:35:11 +02009205 dirnow = alloc(MAXPATHL);
9206 if (dirnow == NULL)
9207 failed = TRUE;
9208 else
9209 {
9210 /*
9211 * Change to session file's dir.
9212 */
9213 if (mch_dirname(dirnow, MAXPATHL) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00009214 || mch_chdir((char *)dirnow) != 0)
Bram Moolenaard9462e32011-04-11 21:35:11 +02009215 *dirnow = NUL;
9216 if (*dirnow != NUL && (ssop_flags & SSOP_SESDIR))
9217 {
9218 if (vim_chdirfile(fname) == OK)
9219 shorten_fnames(TRUE);
9220 }
9221 else if (*dirnow != NUL
9222 && (ssop_flags & SSOP_CURDIR) && globaldir != NULL)
9223 {
9224 if (mch_chdir((char *)globaldir) == 0)
9225 shorten_fnames(TRUE);
9226 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009227
Bram Moolenaard9462e32011-04-11 21:35:11 +02009228 failed |= (makeopens(fd, dirnow) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009229
Bram Moolenaard9462e32011-04-11 21:35:11 +02009230 /* restore original dir */
9231 if (*dirnow != NUL && ((ssop_flags & SSOP_SESDIR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009232 || ((ssop_flags & SSOP_CURDIR) && globaldir != NULL)))
Bram Moolenaard9462e32011-04-11 21:35:11 +02009233 {
9234 if (mch_chdir((char *)dirnow) != 0)
9235 EMSG(_(e_prev_dir));
9236 shorten_fnames(TRUE);
9237 }
9238 vim_free(dirnow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009239 }
9240 }
9241 else
9242 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +00009243 failed |= (put_view(fd, curwin, !using_vdir, flagp,
9244 -1) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009245 }
9246 if (put_line(fd, "let &so = s:so_save | let &siso = s:siso_save")
9247 == FAIL)
9248 failed = TRUE;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009249 if (put_line(fd, "doautoall SessionLoadPost") == FAIL)
9250 failed = TRUE;
Bram Moolenaar4770d092006-01-12 23:22:24 +00009251 if (eap->cmdidx == CMD_mksession)
9252 {
9253 if (put_line(fd, "unlet SessionLoad") == FAIL)
9254 failed = TRUE;
9255 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009256 }
9257#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009258 if (put_line(fd, "\" vim: set ft=vim :") == FAIL)
9259 failed = TRUE;
9260
Bram Moolenaar071d4272004-06-13 20:20:40 +00009261 failed |= fclose(fd);
9262
9263 if (failed)
9264 EMSG(_(e_write));
9265#if defined(FEAT_EVAL) && defined(FEAT_SESSION)
9266 else if (eap->cmdidx == CMD_mksession)
9267 {
9268 /* successful session write - set this_session var */
Bram Moolenaard9462e32011-04-11 21:35:11 +02009269 char_u *tbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009270
Bram Moolenaard9462e32011-04-11 21:35:11 +02009271 tbuf = alloc(MAXPATHL);
9272 if (tbuf != NULL)
9273 {
9274 if (vim_FullName(fname, tbuf, MAXPATHL, FALSE) == OK)
9275 set_vim_var_string(VV_THIS_SESSION, tbuf, -1);
9276 vim_free(tbuf);
9277 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009278 }
9279#endif
9280#ifdef MKSESSION_NL
9281 mksession_nl = FALSE;
9282#endif
9283 }
9284
9285#ifdef FEAT_BROWSE
9286theend:
9287 vim_free(browseFile);
9288#endif
9289#ifdef FEAT_SESSION
9290 vim_free(viewFile);
9291#endif
9292}
9293
Bram Moolenaardf177f62005-02-22 08:39:57 +00009294#if ((defined(FEAT_SESSION) || defined(FEAT_EVAL)) && defined(vim_mkdir)) \
9295 || defined(PROTO)
9296 int
9297vim_mkdir_emsg(name, prot)
9298 char_u *name;
Bram Moolenaar78a15312009-05-15 19:33:18 +00009299 int prot UNUSED;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009300{
9301 if (vim_mkdir(name, prot) != 0)
9302 {
9303 EMSG2(_("E739: Cannot create directory: %s"), name);
9304 return FAIL;
9305 }
9306 return OK;
9307}
9308#endif
9309
Bram Moolenaar071d4272004-06-13 20:20:40 +00009310/*
9311 * Open a file for writing for an Ex command, with some checks.
9312 * Return file descriptor, or NULL on failure.
9313 */
9314 FILE *
9315open_exfile(fname, forceit, mode)
9316 char_u *fname;
9317 int forceit;
9318 char *mode; /* "w" for create new file or "a" for append */
9319{
9320 FILE *fd;
9321
9322#ifdef UNIX
9323 /* with Unix it is possible to open a directory */
9324 if (mch_isdir(fname))
9325 {
9326 EMSG2(_(e_isadir2), fname);
9327 return NULL;
9328 }
9329#endif
9330 if (!forceit && *mode != 'a' && vim_fexists(fname))
9331 {
9332 EMSG2(_("E189: \"%s\" exists (add ! to override)"), fname);
9333 return NULL;
9334 }
9335
9336 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
9337 EMSG2(_("E190: Cannot open \"%s\" for writing"), fname);
9338
9339 return fd;
9340}
9341
9342/*
9343 * ":mark" and ":k".
9344 */
9345 static void
9346ex_mark(eap)
9347 exarg_T *eap;
9348{
9349 pos_T pos;
9350
9351 if (*eap->arg == NUL) /* No argument? */
9352 EMSG(_(e_argreq));
9353 else if (eap->arg[1] != NUL) /* more than one character? */
9354 EMSG(_(e_trailing));
9355 else
9356 {
9357 pos = curwin->w_cursor; /* save curwin->w_cursor */
9358 curwin->w_cursor.lnum = eap->line2;
9359 beginline(BL_WHITE | BL_FIX);
9360 if (setmark(*eap->arg) == FAIL) /* set mark */
9361 EMSG(_("E191: Argument must be a letter or forward/backward quote"));
9362 curwin->w_cursor = pos; /* restore curwin->w_cursor */
9363 }
9364}
9365
9366/*
9367 * Update w_topline, w_leftcol and the cursor position.
9368 */
9369 void
9370update_topline_cursor()
9371{
9372 check_cursor(); /* put cursor on valid line */
9373 update_topline();
9374 if (!curwin->w_p_wrap)
9375 validate_cursor();
9376 update_curswant();
9377}
9378
9379#ifdef FEAT_EX_EXTRA
9380/*
9381 * ":normal[!] {commands}": Execute normal mode commands.
9382 */
9383 static void
9384ex_normal(eap)
9385 exarg_T *eap;
9386{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009387 int save_msg_scroll = msg_scroll;
9388 int save_restart_edit = restart_edit;
9389 int save_msg_didout = msg_didout;
9390 int save_State = State;
9391 tasave_T tabuf;
9392 int save_insertmode = p_im;
9393 int save_finish_op = finish_op;
Bram Moolenaar38084112008-07-26 14:05:07 +00009394 int save_opcount = opcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009395#ifdef FEAT_MBYTE
9396 char_u *arg = NULL;
9397 int l;
9398 char_u *p;
9399#endif
9400
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00009401 if (ex_normal_lock > 0)
9402 {
9403 EMSG(_(e_secure));
9404 return;
9405 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009406 if (ex_normal_busy >= p_mmd)
9407 {
9408 EMSG(_("E192: Recursive use of :normal too deep"));
9409 return;
9410 }
9411 ++ex_normal_busy;
9412
9413 msg_scroll = FALSE; /* no msg scrolling in Normal mode */
9414 restart_edit = 0; /* don't go to Insert mode */
9415 p_im = FALSE; /* don't use 'insertmode' */
9416
9417#ifdef FEAT_MBYTE
9418 /*
9419 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
9420 * this for the K_SPECIAL leading byte, otherwise special keys will not
9421 * work.
9422 */
9423 if (has_mbyte)
9424 {
9425 int len = 0;
9426
9427 /* Count the number of characters to be escaped. */
9428 for (p = eap->arg; *p != NUL; ++p)
9429 {
9430# ifdef FEAT_GUI
9431 if (*p == CSI) /* leadbyte CSI */
9432 len += 2;
9433# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009434 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009435 if (*++p == K_SPECIAL /* trailbyte K_SPECIAL or CSI */
9436# ifdef FEAT_GUI
9437 || *p == CSI
9438# endif
9439 )
9440 len += 2;
9441 }
9442 if (len > 0)
9443 {
9444 arg = alloc((unsigned)(STRLEN(eap->arg) + len + 1));
9445 if (arg != NULL)
9446 {
9447 len = 0;
9448 for (p = eap->arg; *p != NUL; ++p)
9449 {
9450 arg[len++] = *p;
9451# ifdef FEAT_GUI
9452 if (*p == CSI)
9453 {
9454 arg[len++] = KS_EXTRA;
9455 arg[len++] = (int)KE_CSI;
9456 }
9457# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009458 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009459 {
9460 arg[len++] = *++p;
9461 if (*p == K_SPECIAL)
9462 {
9463 arg[len++] = KS_SPECIAL;
9464 arg[len++] = KE_FILLER;
9465 }
9466# ifdef FEAT_GUI
9467 else if (*p == CSI)
9468 {
9469 arg[len++] = KS_EXTRA;
9470 arg[len++] = (int)KE_CSI;
9471 }
9472# endif
9473 }
9474 arg[len] = NUL;
9475 }
9476 }
9477 }
9478 }
9479#endif
9480
9481 /*
9482 * Save the current typeahead. This is required to allow using ":normal"
9483 * from an event handler and makes sure we don't hang when the argument
9484 * ends with half a command.
9485 */
9486 save_typeahead(&tabuf);
9487 if (tabuf.typebuf_valid)
9488 {
9489 /*
9490 * Repeat the :normal command for each line in the range. When no
9491 * range given, execute it just once, without positioning the cursor
9492 * first.
9493 */
9494 do
9495 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009496 if (eap->addr_count != 0)
9497 {
9498 curwin->w_cursor.lnum = eap->line1++;
9499 curwin->w_cursor.col = 0;
9500 }
9501
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009502 exec_normal_cmd(
Bram Moolenaar071d4272004-06-13 20:20:40 +00009503#ifdef FEAT_MBYTE
9504 arg != NULL ? arg :
9505#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009506 eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009507 }
9508 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
9509 }
9510
9511 /* Might not return to the main loop when in an event handler. */
9512 update_topline_cursor();
9513
9514 /* Restore the previous typeahead. */
9515 restore_typeahead(&tabuf);
9516
9517 --ex_normal_busy;
9518 msg_scroll = save_msg_scroll;
9519 restart_edit = save_restart_edit;
9520 p_im = save_insertmode;
9521 finish_op = save_finish_op;
Bram Moolenaar38084112008-07-26 14:05:07 +00009522 opcount = save_opcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009523 msg_didout |= save_msg_didout; /* don't reset msg_didout now */
9524
9525 /* Restore the state (needed when called from a function executed for
9526 * 'indentexpr'). */
9527 State = save_State;
9528#ifdef FEAT_MBYTE
9529 vim_free(arg);
9530#endif
9531}
9532
9533/*
Bram Moolenaar64969662005-12-14 21:59:55 +00009534 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009535 */
9536 static void
9537ex_startinsert(eap)
9538 exarg_T *eap;
9539{
Bram Moolenaarfd371682005-01-14 21:42:54 +00009540 if (eap->forceit)
9541 {
9542 coladvance((colnr_T)MAXCOL);
9543 curwin->w_curswant = MAXCOL;
9544 curwin->w_set_curswant = FALSE;
9545 }
9546
Bram Moolenaara40c5002005-01-09 21:16:21 +00009547 /* Ignore the command when already in Insert mode. Inserting an
9548 * expression register that invokes a function can do this. */
9549 if (State & INSERT)
9550 return;
9551
Bram Moolenaar64969662005-12-14 21:59:55 +00009552 if (eap->cmdidx == CMD_startinsert)
9553 restart_edit = 'a';
9554 else if (eap->cmdidx == CMD_startreplace)
9555 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00009556 else
Bram Moolenaar64969662005-12-14 21:59:55 +00009557 restart_edit = 'V';
9558
9559 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009560 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009561 if (eap->cmdidx == CMD_startinsert)
9562 restart_edit = 'i';
Bram Moolenaar071d4272004-06-13 20:20:40 +00009563 curwin->w_curswant = 0; /* avoid MAXCOL */
9564 }
9565}
9566
9567/*
9568 * ":stopinsert"
9569 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009570 static void
9571ex_stopinsert(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009572 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009573{
9574 restart_edit = 0;
9575 stop_insert_mode = TRUE;
9576}
9577#endif
9578
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009579#if defined(FEAT_EX_EXTRA) || defined(FEAT_MENU) || defined(PROTO)
9580/*
9581 * Execute normal mode command "cmd".
9582 * "remap" can be REMAP_NONE or REMAP_YES.
9583 */
9584 void
9585exec_normal_cmd(cmd, remap, silent)
9586 char_u *cmd;
9587 int remap;
9588 int silent;
9589{
9590 oparg_T oa;
9591
9592 /*
9593 * Stuff the argument into the typeahead buffer.
9594 * Execute normal_cmd() until there is no typeahead left.
9595 */
9596 clear_oparg(&oa);
9597 finish_op = FALSE;
9598 ins_typebuf(cmd, remap, 0, TRUE, silent);
9599 while ((!stuff_empty() || (!typebuf_typed() && typebuf.tb_len > 0))
9600 && !got_int)
9601 {
9602 update_topline_cursor();
Bram Moolenaar48ac02c2011-01-17 19:50:06 +01009603 normal_cmd(&oa, TRUE); /* execute a Normal mode cmd */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009604 }
9605}
9606#endif
9607
Bram Moolenaar071d4272004-06-13 20:20:40 +00009608#ifdef FEAT_FIND_ID
9609 static void
9610ex_checkpath(eap)
9611 exarg_T *eap;
9612{
9613 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
9614 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
9615 (linenr_T)1, (linenr_T)MAXLNUM);
9616}
9617
9618#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
9619/*
9620 * ":psearch"
9621 */
9622 static void
9623ex_psearch(eap)
9624 exarg_T *eap;
9625{
9626 g_do_tagpreview = p_pvh;
9627 ex_findpat(eap);
9628 g_do_tagpreview = 0;
9629}
9630#endif
9631
9632 static void
9633ex_findpat(eap)
9634 exarg_T *eap;
9635{
9636 int whole = TRUE;
9637 long n;
9638 char_u *p;
9639 int action;
9640
9641 switch (cmdnames[eap->cmdidx].cmd_name[2])
9642 {
9643 case 'e': /* ":psearch", ":isearch" and ":dsearch" */
9644 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
9645 action = ACTION_GOTO;
9646 else
9647 action = ACTION_SHOW;
9648 break;
9649 case 'i': /* ":ilist" and ":dlist" */
9650 action = ACTION_SHOW_ALL;
9651 break;
9652 case 'u': /* ":ijump" and ":djump" */
9653 action = ACTION_GOTO;
9654 break;
9655 default: /* ":isplit" and ":dsplit" */
9656 action = ACTION_SPLIT;
9657 break;
9658 }
9659
9660 n = 1;
9661 if (vim_isdigit(*eap->arg)) /* get count */
9662 {
9663 n = getdigits(&eap->arg);
9664 eap->arg = skipwhite(eap->arg);
9665 }
9666 if (*eap->arg == '/') /* Match regexp, not just whole words */
9667 {
9668 whole = FALSE;
9669 ++eap->arg;
9670 p = skip_regexp(eap->arg, '/', p_magic, NULL);
9671 if (*p)
9672 {
9673 *p++ = NUL;
9674 p = skipwhite(p);
9675
9676 /* Check for trailing illegal characters */
9677 if (!ends_excmd(*p))
9678 eap->errmsg = e_trailing;
9679 else
9680 eap->nextcmd = check_nextcmd(p);
9681 }
9682 }
9683 if (!eap->skip)
9684 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
9685 whole, !eap->forceit,
9686 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
9687 n, action, eap->line1, eap->line2);
9688}
9689#endif
9690
9691#ifdef FEAT_WINDOWS
9692
9693# ifdef FEAT_QUICKFIX
9694/*
9695 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
9696 */
9697 static void
9698ex_ptag(eap)
9699 exarg_T *eap;
9700{
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01009701 g_do_tagpreview = p_pvh; /* will be reset to 0 in ex_tag_cmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009702 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9703}
9704
9705/*
9706 * ":pedit"
9707 */
9708 static void
9709ex_pedit(eap)
9710 exarg_T *eap;
9711{
9712 win_T *curwin_save = curwin;
9713
9714 g_do_tagpreview = p_pvh;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00009715 prepare_tagpreview(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009716 keep_help_flag = curwin_save->w_buffer->b_help;
9717 do_exedit(eap, NULL);
9718 keep_help_flag = FALSE;
9719 if (curwin != curwin_save && win_valid(curwin_save))
9720 {
9721 /* Return cursor to where we were */
9722 validate_cursor();
9723 redraw_later(VALID);
9724 win_enter(curwin_save, TRUE);
9725 }
9726 g_do_tagpreview = 0;
9727}
9728# endif
9729
9730/*
9731 * ":stag", ":stselect" and ":stjump".
9732 */
9733 static void
9734ex_stag(eap)
9735 exarg_T *eap;
9736{
9737 postponed_split = -1;
9738 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009739 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009740 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9741 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009742 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009743}
9744#endif
9745
9746/*
9747 * ":tag", ":tselect", ":tjump", ":tnext", etc.
9748 */
9749 static void
9750ex_tag(eap)
9751 exarg_T *eap;
9752{
9753 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
9754}
9755
9756 static void
9757ex_tag_cmd(eap, name)
9758 exarg_T *eap;
9759 char_u *name;
9760{
9761 int cmd;
9762
9763 switch (name[1])
9764 {
9765 case 'j': cmd = DT_JUMP; /* ":tjump" */
9766 break;
9767 case 's': cmd = DT_SELECT; /* ":tselect" */
9768 break;
9769 case 'p': cmd = DT_PREV; /* ":tprevious" */
9770 break;
9771 case 'N': cmd = DT_PREV; /* ":tNext" */
9772 break;
9773 case 'n': cmd = DT_NEXT; /* ":tnext" */
9774 break;
9775 case 'o': cmd = DT_POP; /* ":pop" */
9776 break;
9777 case 'f': /* ":tfirst" */
9778 case 'r': cmd = DT_FIRST; /* ":trewind" */
9779 break;
9780 case 'l': cmd = DT_LAST; /* ":tlast" */
9781 break;
9782 default: /* ":tag" */
9783#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +00009784 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009785 {
9786 do_cstag(eap);
9787 return;
9788 }
9789#endif
9790 cmd = DT_TAG;
9791 break;
9792 }
9793
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00009794 if (name[0] == 'l')
9795 {
9796#ifndef FEAT_QUICKFIX
9797 ex_ni(eap);
9798 return;
9799#else
9800 cmd = DT_LTAG;
9801#endif
9802 }
9803
Bram Moolenaar071d4272004-06-13 20:20:40 +00009804 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
9805 eap->forceit, TRUE);
9806}
9807
9808/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009809 * Check "str" for starting with a special cmdline variable.
9810 * If found return one of the SPEC_ values and set "*usedlen" to the length of
9811 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
9812 */
9813 int
9814find_cmdline_var(src, usedlen)
9815 char_u *src;
9816 int *usedlen;
9817{
9818 int len;
9819 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +00009820 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009821 "%",
9822#define SPEC_PERC 0
9823 "#",
9824#define SPEC_HASH 1
9825 "<cword>", /* cursor word */
9826#define SPEC_CWORD 2
9827 "<cWORD>", /* cursor WORD */
9828#define SPEC_CCWORD 3
9829 "<cfile>", /* cursor path name */
9830#define SPEC_CFILE 4
9831 "<sfile>", /* ":so" file name */
9832#define SPEC_SFILE 5
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009833 "<slnum>", /* ":so" file line number */
9834#define SPEC_SLNUM 6
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009835#ifdef FEAT_AUTOCMD
9836 "<afile>", /* autocommand file name */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009837# define SPEC_AFILE 7
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009838 "<abuf>", /* autocommand buffer number */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009839# define SPEC_ABUF 8
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009840 "<amatch>", /* autocommand match name */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009841# define SPEC_AMATCH 9
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009842#endif
9843#ifdef FEAT_CLIENTSERVER
9844 "<client>"
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009845# ifdef FEAT_AUTOCMD
9846# define SPEC_CLIENT 10
9847# else
9848# define SPEC_CLIENT 7
9849# endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009850#endif
9851 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009852
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009853 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009854 {
9855 len = (int)STRLEN(spec_str[i]);
9856 if (STRNCMP(src, spec_str[i], len) == 0)
9857 {
9858 *usedlen = len;
9859 return i;
9860 }
9861 }
9862 return -1;
9863}
9864
9865/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009866 * Evaluate cmdline variables.
9867 *
9868 * change '%' to curbuf->b_ffname
9869 * '#' to curwin->w_altfile
9870 * '<cword>' to word under the cursor
9871 * '<cWORD>' to WORD under the cursor
9872 * '<cfile>' to path name under the cursor
9873 * '<sfile>' to sourced file name
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009874 * '<slnum>' to sourced file line number
Bram Moolenaar071d4272004-06-13 20:20:40 +00009875 * '<afile>' to file name for autocommand
9876 * '<abuf>' to buffer number for autocommand
9877 * '<amatch>' to matching name for autocommand
9878 *
9879 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
9880 * "" for error without a message) and NULL is returned.
9881 * Returns an allocated string if a valid match was found.
9882 * Returns NULL if no match was found. "usedlen" then still contains the
9883 * number of characters to skip.
9884 */
9885 char_u *
Bram Moolenaar63b92542007-03-27 14:57:09 +00009886eval_vars(src, srcstart, usedlen, lnump, errormsg, escaped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009887 char_u *src; /* pointer into commandline */
Bram Moolenaar63b92542007-03-27 14:57:09 +00009888 char_u *srcstart; /* beginning of valid memory for src */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009889 int *usedlen; /* characters after src that are used */
9890 linenr_T *lnump; /* line number for :e command, or NULL */
9891 char_u **errormsg; /* pointer to error message */
Bram Moolenaar63b92542007-03-27 14:57:09 +00009892 int *escaped; /* return value has escaped white space (can
9893 * be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009894{
9895 int i;
9896 char_u *s;
9897 char_u *result;
9898 char_u *resultbuf = NULL;
9899 int resultlen;
9900 buf_T *buf;
9901 int valid = VALID_HEAD + VALID_PATH; /* assume valid result */
9902 int spec_idx;
9903#ifdef FEAT_MODIFY_FNAME
9904 int skip_mod = FALSE;
9905#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009906 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009907
9908 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009909 if (escaped != NULL)
9910 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009911
9912 /*
9913 * Check if there is something to do.
9914 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009915 spec_idx = find_cmdline_var(src, usedlen);
9916 if (spec_idx < 0) /* no match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009917 {
9918 *usedlen = 1;
9919 return NULL;
9920 }
9921
9922 /*
9923 * Skip when preceded with a backslash "\%" and "\#".
9924 * Note: In "\\%" the % is also not recognized!
9925 */
9926 if (src > srcstart && src[-1] == '\\')
9927 {
9928 *usedlen = 0;
Bram Moolenaara7241f52008-06-24 20:39:31 +00009929 STRMOVE(src - 1, src); /* remove backslash */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009930 return NULL;
9931 }
9932
9933 /*
9934 * word or WORD under cursor
9935 */
9936 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD)
9937 {
9938 resultlen = find_ident_under_cursor(&result, spec_idx == SPEC_CWORD ?
9939 (FIND_IDENT|FIND_STRING) : FIND_STRING);
9940 if (resultlen == 0)
9941 {
9942 *errormsg = (char_u *)"";
9943 return NULL;
9944 }
9945 }
9946
9947 /*
9948 * '#': Alternate file name
9949 * '%': Current file name
9950 * File name under the cursor
9951 * File name for autocommand
9952 * and following modifiers
9953 */
9954 else
9955 {
9956 switch (spec_idx)
9957 {
9958 case SPEC_PERC: /* '%': current file */
9959 if (curbuf->b_fname == NULL)
9960 {
9961 result = (char_u *)"";
9962 valid = 0; /* Must have ":p:h" to be valid */
9963 }
9964 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009965 result = curbuf->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009966 break;
9967
9968 case SPEC_HASH: /* '#' or "#99": alternate file */
9969 if (src[1] == '#') /* "##": the argument list */
9970 {
9971 result = arg_all();
9972 resultbuf = result;
9973 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009974 if (escaped != NULL)
9975 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009976#ifdef FEAT_MODIFY_FNAME
9977 skip_mod = TRUE;
9978#endif
9979 break;
9980 }
9981 s = src + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00009982 if (*s == '<') /* "#<99" uses v:oldfiles */
9983 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009984 i = (int)getdigits(&s);
9985 *usedlen = (int)(s - src); /* length of what we expand */
9986
Bram Moolenaard812df62008-11-09 12:46:09 +00009987 if (src[1] == '<')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009988 {
Bram Moolenaard812df62008-11-09 12:46:09 +00009989 if (*usedlen < 2)
9990 {
9991 /* Should we give an error message for #<text? */
9992 *usedlen = 1;
9993 return NULL;
9994 }
9995#ifdef FEAT_EVAL
9996 result = list_find_str(get_vim_var_list(VV_OLDFILES),
9997 (long)i);
9998 if (result == NULL)
9999 {
10000 *errormsg = (char_u *)"";
10001 return NULL;
10002 }
10003#else
10004 *errormsg = (char_u *)_("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +000010005 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +000010006#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010007 }
10008 else
Bram Moolenaard812df62008-11-09 12:46:09 +000010009 {
10010 buf = buflist_findnr(i);
10011 if (buf == NULL)
10012 {
10013 *errormsg = (char_u *)_("E194: No alternate file name to substitute for '#'");
10014 return NULL;
10015 }
10016 if (lnump != NULL)
10017 *lnump = ECMD_LAST;
10018 if (buf->b_fname == NULL)
10019 {
10020 result = (char_u *)"";
10021 valid = 0; /* Must have ":p:h" to be valid */
10022 }
10023 else
10024 result = buf->b_fname;
10025 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010026 break;
10027
10028#ifdef FEAT_SEARCHPATH
10029 case SPEC_CFILE: /* file name under cursor */
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010030 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010031 if (result == NULL)
10032 {
10033 *errormsg = (char_u *)"";
10034 return NULL;
10035 }
10036 resultbuf = result; /* remember allocated string */
10037 break;
10038#endif
10039
10040#ifdef FEAT_AUTOCMD
10041 case SPEC_AFILE: /* file name for autocommand */
10042 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +000010043 if (result != NULL && !autocmd_fname_full)
10044 {
10045 /* Still need to turn the fname into a full path. It is
10046 * postponed to avoid a delay when <afile> is not used. */
10047 autocmd_fname_full = TRUE;
10048 result = FullName_save(autocmd_fname, FALSE);
10049 vim_free(autocmd_fname);
10050 autocmd_fname = result;
10051 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010052 if (result == NULL)
10053 {
10054 *errormsg = (char_u *)_("E495: no autocommand file name to substitute for \"<afile>\"");
10055 return NULL;
10056 }
Bram Moolenaara0174af2008-01-02 20:08:25 +000010057 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010058 break;
10059
10060 case SPEC_ABUF: /* buffer number for autocommand */
10061 if (autocmd_bufnr <= 0)
10062 {
10063 *errormsg = (char_u *)_("E496: no autocommand buffer number to substitute for \"<abuf>\"");
10064 return NULL;
10065 }
10066 sprintf((char *)strbuf, "%d", autocmd_bufnr);
10067 result = strbuf;
10068 break;
10069
10070 case SPEC_AMATCH: /* match name for autocommand */
10071 result = autocmd_match;
10072 if (result == NULL)
10073 {
10074 *errormsg = (char_u *)_("E497: no autocommand match name to substitute for \"<amatch>\"");
10075 return NULL;
10076 }
10077 break;
10078
10079#endif
10080 case SPEC_SFILE: /* file name for ":so" command */
10081 result = sourcing_name;
10082 if (result == NULL)
10083 {
10084 *errormsg = (char_u *)_("E498: no :source file name to substitute for \"<sfile>\"");
10085 return NULL;
10086 }
10087 break;
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010088 case SPEC_SLNUM: /* line in file for ":so" command */
10089 if (sourcing_name == NULL || sourcing_lnum == 0)
10090 {
10091 *errormsg = (char_u *)_("E842: no line number to use for \"<slnum>\"");
10092 return NULL;
10093 }
10094 sprintf((char *)strbuf, "%ld", (long)sourcing_lnum);
10095 result = strbuf;
10096 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010097#if defined(FEAT_CLIENTSERVER)
10098 case SPEC_CLIENT: /* Source of last submitted input */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010099 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
10100 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010101 result = strbuf;
10102 break;
10103#endif
10104 }
10105
10106 resultlen = (int)STRLEN(result); /* length of new string */
10107 if (src[*usedlen] == '<') /* remove the file name extension */
10108 {
10109 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010110 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010111 resultlen = (int)(s - result);
10112 }
10113#ifdef FEAT_MODIFY_FNAME
10114 else if (!skip_mod)
10115 {
10116 valid |= modify_fname(src, usedlen, &result, &resultbuf,
10117 &resultlen);
10118 if (result == NULL)
10119 {
10120 *errormsg = (char_u *)"";
10121 return NULL;
10122 }
10123 }
10124#endif
10125 }
10126
10127 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
10128 {
10129 if (valid != VALID_HEAD + VALID_PATH)
10130 /* xgettext:no-c-format */
10131 *errormsg = (char_u *)_("E499: Empty file name for '%' or '#', only works with \":p:h\"");
10132 else
10133 *errormsg = (char_u *)_("E500: Evaluates to an empty string");
10134 result = NULL;
10135 }
10136 else
10137 result = vim_strnsave(result, resultlen);
10138 vim_free(resultbuf);
10139 return result;
10140}
10141
10142/*
10143 * Concatenate all files in the argument list, separated by spaces, and return
10144 * it in one allocated string.
10145 * Spaces and backslashes in the file names are escaped with a backslash.
10146 * Returns NULL when out of memory.
10147 */
10148 static char_u *
10149arg_all()
10150{
10151 int len;
10152 int idx;
10153 char_u *retval = NULL;
10154 char_u *p;
10155
10156 /*
10157 * Do this loop two times:
10158 * first time: compute the total length
10159 * second time: concatenate the names
10160 */
10161 for (;;)
10162 {
10163 len = 0;
10164 for (idx = 0; idx < ARGCOUNT; ++idx)
10165 {
10166 p = alist_name(&ARGLIST[idx]);
10167 if (p != NULL)
10168 {
10169 if (len > 0)
10170 {
10171 /* insert a space in between names */
10172 if (retval != NULL)
10173 retval[len] = ' ';
10174 ++len;
10175 }
10176 for ( ; *p != NUL; ++p)
10177 {
10178 if (*p == ' ' || *p == '\\')
10179 {
10180 /* insert a backslash */
10181 if (retval != NULL)
10182 retval[len] = '\\';
10183 ++len;
10184 }
10185 if (retval != NULL)
10186 retval[len] = *p;
10187 ++len;
10188 }
10189 }
10190 }
10191
10192 /* second time: break here */
10193 if (retval != NULL)
10194 {
10195 retval[len] = NUL;
10196 break;
10197 }
10198
10199 /* allocate memory */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000010200 retval = alloc((unsigned)len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010201 if (retval == NULL)
10202 break;
10203 }
10204
10205 return retval;
10206}
10207
10208#if defined(FEAT_AUTOCMD) || defined(PROTO)
10209/*
10210 * Expand the <sfile> string in "arg".
10211 *
10212 * Returns an allocated string, or NULL for any error.
10213 */
10214 char_u *
10215expand_sfile(arg)
10216 char_u *arg;
10217{
10218 char_u *errormsg;
10219 int len;
10220 char_u *result;
10221 char_u *newres;
10222 char_u *repl;
10223 int srclen;
10224 char_u *p;
10225
10226 result = vim_strsave(arg);
10227 if (result == NULL)
10228 return NULL;
10229
10230 for (p = result; *p; )
10231 {
10232 if (STRNCMP(p, "<sfile>", 7) != 0)
10233 ++p;
10234 else
10235 {
10236 /* replace "<sfile>" with the sourced file name, and do ":" stuff */
Bram Moolenaar63b92542007-03-27 14:57:09 +000010237 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010238 if (errormsg != NULL)
10239 {
10240 if (*errormsg)
10241 emsg(errormsg);
10242 vim_free(result);
10243 return NULL;
10244 }
10245 if (repl == NULL) /* no match (cannot happen) */
10246 {
10247 p += srclen;
10248 continue;
10249 }
10250 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
10251 newres = alloc(len);
10252 if (newres == NULL)
10253 {
10254 vim_free(repl);
10255 vim_free(result);
10256 return NULL;
10257 }
10258 mch_memmove(newres, result, (size_t)(p - result));
10259 STRCPY(newres + (p - result), repl);
10260 len = (int)STRLEN(newres);
10261 STRCAT(newres, p + srclen);
10262 vim_free(repl);
10263 vim_free(result);
10264 result = newres;
10265 p = newres + len; /* continue after the match */
10266 }
10267 }
10268
10269 return result;
10270}
10271#endif
10272
10273#ifdef FEAT_SESSION
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010274static int ses_winsizes __ARGS((FILE *fd, int restore_size,
10275 win_T *tab_firstwin));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010276static int ses_win_rec __ARGS((FILE *fd, frame_T *fr));
10277static frame_T *ses_skipframe __ARGS((frame_T *fr));
10278static int ses_do_frame __ARGS((frame_T *fr));
10279static int ses_do_win __ARGS((win_T *wp));
10280static int ses_arglist __ARGS((FILE *fd, char *cmd, garray_T *gap, int fullname, unsigned *flagp));
10281static int ses_put_fname __ARGS((FILE *fd, char_u *name, unsigned *flagp));
10282static int ses_fname __ARGS((FILE *fd, buf_T *buf, unsigned *flagp));
10283
10284/*
10285 * Write openfile commands for the current buffers to an .exrc file.
10286 * Return FAIL on error, OK otherwise.
10287 */
10288 static int
10289makeopens(fd, dirnow)
10290 FILE *fd;
10291 char_u *dirnow; /* Current directory name */
10292{
10293 buf_T *buf;
10294 int only_save_windows = TRUE;
10295 int nr;
10296 int cnr = 1;
10297 int restore_size = TRUE;
10298 win_T *wp;
10299 char_u *sname;
10300 win_T *edited_win = NULL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010301 int tabnr;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010302 win_T *tab_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010303 frame_T *tab_topframe;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010304 int cur_arg_idx = 0;
Bram Moolenaar383c6f52008-01-04 15:01:07 +000010305 int next_arg_idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010306
10307 if (ssop_flags & SSOP_BUFFERS)
10308 only_save_windows = FALSE; /* Save ALL buffers */
10309
10310 /*
10311 * Begin by setting the this_session variable, and then other
10312 * sessionable variables.
10313 */
10314#ifdef FEAT_EVAL
10315 if (put_line(fd, "let v:this_session=expand(\"<sfile>:p\")") == FAIL)
10316 return FAIL;
10317 if (ssop_flags & SSOP_GLOBALS)
10318 if (store_session_globals(fd) == FAIL)
10319 return FAIL;
10320#endif
10321
10322 /*
10323 * Close all windows but one.
10324 */
10325 if (put_line(fd, "silent only") == FAIL)
10326 return FAIL;
10327
10328 /*
10329 * Now a :cd command to the session directory or the current directory
10330 */
10331 if (ssop_flags & SSOP_SESDIR)
10332 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010333 if (put_line(fd, "exe \"cd \" . escape(expand(\"<sfile>:p:h\"), ' ')")
10334 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010335 return FAIL;
10336 }
10337 else if (ssop_flags & SSOP_CURDIR)
10338 {
10339 sname = home_replace_save(NULL, globaldir != NULL ? globaldir : dirnow);
10340 if (sname == NULL
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010341 || fputs("cd ", fd) < 0
10342 || ses_put_fname(fd, sname, &ssop_flags) == FAIL
10343 || put_eol(fd) == FAIL)
10344 {
10345 vim_free(sname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010346 return FAIL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010347 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010348 vim_free(sname);
10349 }
10350
10351 /*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010352 * If there is an empty, unnamed buffer we will wipe it out later.
10353 * Remember the buffer number.
10354 */
10355 if (put_line(fd, "if expand('%') == '' && !&modified && line('$') <= 1 && getline(1) == ''") == FAIL)
10356 return FAIL;
10357 if (put_line(fd, " let s:wipebuf = bufnr('%')") == FAIL)
10358 return FAIL;
10359 if (put_line(fd, "endif") == FAIL)
10360 return FAIL;
10361
10362 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010363 * Now save the current files, current buffer first.
10364 */
10365 if (put_line(fd, "set shortmess=aoO") == FAIL)
10366 return FAIL;
10367
10368 /* Now put the other buffers into the buffer list */
10369 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
10370 {
10371 if (!(only_save_windows && buf->b_nwindows == 0)
10372 && !(buf->b_help && !(ssop_flags & SSOP_HELP))
10373 && buf->b_fname != NULL
10374 && buf->b_p_bl)
10375 {
10376 if (fprintf(fd, "badd +%ld ", buf->b_wininfo == NULL ? 1L
10377 : buf->b_wininfo->wi_fpos.lnum) < 0
10378 || ses_fname(fd, buf, &ssop_flags) == FAIL)
10379 return FAIL;
10380 }
10381 }
10382
10383 /* the global argument list */
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010010384 if (ses_arglist(fd, "argglobal", &global_alist.al_ga,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010385 !(ssop_flags & SSOP_CURDIR), &ssop_flags) == FAIL)
10386 return FAIL;
10387
10388 if (ssop_flags & SSOP_RESIZE)
10389 {
10390 /* Note: after the restore we still check it worked!*/
10391 if (fprintf(fd, "set lines=%ld columns=%ld" , Rows, Columns) < 0
10392 || put_eol(fd) == FAIL)
10393 return FAIL;
10394 }
10395
10396#ifdef FEAT_GUI
10397 if (gui.in_use && (ssop_flags & SSOP_WINPOS))
10398 {
10399 int x, y;
10400
10401 if (gui_mch_get_winpos(&x, &y) == OK)
10402 {
10403 /* Note: after the restore we still check it worked!*/
10404 if (fprintf(fd, "winpos %d %d", x, y) < 0 || put_eol(fd) == FAIL)
10405 return FAIL;
10406 }
10407 }
10408#endif
10409
10410 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000010411 * May repeat putting Windows for each tab, when "tabpages" is in
10412 * 'sessionoptions'.
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010413 * Don't use goto_tabpage(), it may change directory and trigger
10414 * autocommands.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010415 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010416 tab_firstwin = firstwin; /* first window in tab page "tabnr" */
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010417 tab_topframe = topframe;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010418 for (tabnr = 1; ; ++tabnr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010419 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010420 int need_tabnew = FALSE;
10421
Bram Moolenaar18144c82006-04-12 21:52:12 +000010422 if ((ssop_flags & SSOP_TABPAGES))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010423 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010424 tabpage_T *tp = find_tabpage(tabnr);
10425
10426 if (tp == NULL)
10427 break; /* done all tab pages */
10428 if (tp == curtab)
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010429 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010430 tab_firstwin = firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010431 tab_topframe = topframe;
10432 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010433 else
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010434 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010435 tab_firstwin = tp->tp_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010436 tab_topframe = tp->tp_topframe;
10437 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010438 if (tabnr > 1)
10439 need_tabnew = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010440 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010441
Bram Moolenaar18144c82006-04-12 21:52:12 +000010442 /*
10443 * Before creating the window layout, try loading one file. If this
10444 * is aborted we don't end up with a number of useless windows.
10445 * This may have side effects! (e.g., compressed or network file).
10446 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010447 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010448 {
10449 if (ses_do_win(wp)
10450 && wp->w_buffer->b_ffname != NULL
10451 && !wp->w_buffer->b_help
10452#ifdef FEAT_QUICKFIX
10453 && !bt_nofile(wp->w_buffer)
10454#endif
10455 )
10456 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010457 if (fputs(need_tabnew ? "tabedit " : "edit ", fd) < 0
Bram Moolenaar18144c82006-04-12 21:52:12 +000010458 || ses_fname(fd, wp->w_buffer, &ssop_flags) == FAIL)
10459 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010460 need_tabnew = FALSE;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010461 if (!wp->w_arg_idx_invalid)
10462 edited_win = wp;
10463 break;
10464 }
10465 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010466
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010467 /* If no file got edited create an empty tab page. */
10468 if (need_tabnew && put_line(fd, "tabnew") == FAIL)
10469 return FAIL;
10470
Bram Moolenaar18144c82006-04-12 21:52:12 +000010471 /*
10472 * Save current window layout.
10473 */
10474 if (put_line(fd, "set splitbelow splitright") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010475 return FAIL;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010476 if (ses_win_rec(fd, tab_topframe) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010477 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010478 if (!p_sb && put_line(fd, "set nosplitbelow") == FAIL)
10479 return FAIL;
10480 if (!p_spr && put_line(fd, "set nosplitright") == FAIL)
10481 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010482
Bram Moolenaar18144c82006-04-12 21:52:12 +000010483 /*
10484 * Check if window sizes can be restored (no windows omitted).
10485 * Remember the window number of the current window after restoring.
10486 */
10487 nr = 0;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010488 for (wp = tab_firstwin; wp != NULL; wp = W_NEXT(wp))
Bram Moolenaar18144c82006-04-12 21:52:12 +000010489 {
10490 if (ses_do_win(wp))
10491 ++nr;
10492 else
10493 restore_size = FALSE;
10494 if (curwin == wp)
10495 cnr = nr;
10496 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010497
Bram Moolenaar18144c82006-04-12 21:52:12 +000010498 /* Go to the first window. */
10499 if (put_line(fd, "wincmd t") == FAIL)
10500 return FAIL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010501
Bram Moolenaar18144c82006-04-12 21:52:12 +000010502 /*
10503 * If more than one window, see if sizes can be restored.
10504 * First set 'winheight' and 'winwidth' to 1 to avoid the windows being
10505 * resized when moving between windows.
10506 * Do this before restoring the view, so that the topline and the
10507 * cursor can be set. This is done again below.
10508 */
10509 if (put_line(fd, "set winheight=1 winwidth=1") == FAIL)
10510 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010511 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010512 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010513
Bram Moolenaar18144c82006-04-12 21:52:12 +000010514 /*
10515 * Restore the view of the window (options, file, cursor, etc.).
10516 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010517 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010518 {
10519 if (!ses_do_win(wp))
10520 continue;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010521 if (put_view(fd, wp, wp != edited_win, &ssop_flags,
10522 cur_arg_idx) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010523 return FAIL;
10524 if (nr > 1 && put_line(fd, "wincmd w") == FAIL)
10525 return FAIL;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010526 next_arg_idx = wp->w_arg_idx;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010527 }
10528
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010529 /* The argument index in the first tab page is zero, need to set it in
10530 * each window. For further tab pages it's the window where we do
10531 * "tabedit". */
10532 cur_arg_idx = next_arg_idx;
10533
Bram Moolenaar18144c82006-04-12 21:52:12 +000010534 /*
10535 * Restore cursor to the current window if it's not the first one.
10536 */
10537 if (cnr > 1 && (fprintf(fd, "%dwincmd w", cnr) < 0
10538 || put_eol(fd) == FAIL))
10539 return FAIL;
10540
10541 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000010542 * Restore window sizes again after jumping around in windows, because
10543 * the current window has a minimum size while others may not.
10544 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010545 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010546 return FAIL;
10547
Bram Moolenaar18144c82006-04-12 21:52:12 +000010548 /* Don't continue in another tab page when doing only the current one
10549 * or when at the last tab page. */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010550 if (!(ssop_flags & SSOP_TABPAGES))
Bram Moolenaar18144c82006-04-12 21:52:12 +000010551 break;
10552 }
10553
10554 if (ssop_flags & SSOP_TABPAGES)
10555 {
Bram Moolenaar18144c82006-04-12 21:52:12 +000010556 if (fprintf(fd, "tabnext %d", tabpage_index(curtab)) < 0
10557 || put_eol(fd) == FAIL)
10558 return FAIL;
10559 }
10560
Bram Moolenaar9c102382006-05-03 21:26:49 +000010561 /*
10562 * Wipe out an empty unnamed buffer we started in.
10563 */
10564 if (put_line(fd, "if exists('s:wipebuf')") == FAIL)
10565 return FAIL;
Bram Moolenaarf3a67882006-05-05 21:09:41 +000010566 if (put_line(fd, " silent exe 'bwipe ' . s:wipebuf") == FAIL)
Bram Moolenaar9c102382006-05-03 21:26:49 +000010567 return FAIL;
10568 if (put_line(fd, "endif") == FAIL)
10569 return FAIL;
10570 if (put_line(fd, "unlet! s:wipebuf") == FAIL)
10571 return FAIL;
10572
10573 /* Re-apply 'winheight', 'winwidth' and 'shortmess'. */
10574 if (fprintf(fd, "set winheight=%ld winwidth=%ld shortmess=%s",
10575 p_wh, p_wiw, p_shm) < 0 || put_eol(fd) == FAIL)
10576 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010577
10578 /*
10579 * Lastly, execute the x.vim file if it exists.
10580 */
10581 if (put_line(fd, "let s:sx = expand(\"<sfile>:p:r\").\"x.vim\"") == FAIL
10582 || put_line(fd, "if file_readable(s:sx)") == FAIL
Bram Moolenaar42ba1262008-12-09 10:18:03 +000010583 || put_line(fd, " exe \"source \" . fnameescape(s:sx)") == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000010584 || put_line(fd, "endif") == FAIL)
10585 return FAIL;
10586
10587 return OK;
10588}
10589
10590 static int
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010591ses_winsizes(fd, restore_size, tab_firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010592 FILE *fd;
10593 int restore_size;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010594 win_T *tab_firstwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010595{
10596 int n = 0;
10597 win_T *wp;
10598
10599 if (restore_size && (ssop_flags & SSOP_WINSIZE))
10600 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010601 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010602 {
10603 if (!ses_do_win(wp))
10604 continue;
10605 ++n;
10606
10607 /* restore height when not full height */
10608 if (wp->w_height + wp->w_status_height < topframe->fr_height
10609 && (fprintf(fd,
10610 "exe '%dresize ' . ((&lines * %ld + %ld) / %ld)",
10611 n, (long)wp->w_height, Rows / 2, Rows) < 0
10612 || put_eol(fd) == FAIL))
10613 return FAIL;
10614
10615 /* restore width when not full width */
10616 if (wp->w_width < Columns && (fprintf(fd,
10617 "exe 'vert %dresize ' . ((&columns * %ld + %ld) / %ld)",
10618 n, (long)wp->w_width, Columns / 2, Columns) < 0
10619 || put_eol(fd) == FAIL))
10620 return FAIL;
10621 }
10622 }
10623 else
10624 {
10625 /* Just equalise window sizes */
10626 if (put_line(fd, "wincmd =") == FAIL)
10627 return FAIL;
10628 }
10629 return OK;
10630}
10631
10632/*
10633 * Write commands to "fd" to recursively create windows for frame "fr",
10634 * horizontally and vertically split.
10635 * After the commands the last window in the frame is the current window.
10636 * Returns FAIL when writing the commands to "fd" fails.
10637 */
10638 static int
10639ses_win_rec(fd, fr)
10640 FILE *fd;
10641 frame_T *fr;
10642{
10643 frame_T *frc;
10644 int count = 0;
10645
10646 if (fr->fr_layout != FR_LEAF)
10647 {
10648 /* Find first frame that's not skipped and then create a window for
10649 * each following one (first frame is already there). */
10650 frc = ses_skipframe(fr->fr_child);
10651 if (frc != NULL)
10652 while ((frc = ses_skipframe(frc->fr_next)) != NULL)
10653 {
10654 /* Make window as big as possible so that we have lots of room
10655 * to split. */
10656 if (put_line(fd, "wincmd _ | wincmd |") == FAIL
10657 || put_line(fd, fr->fr_layout == FR_COL
10658 ? "split" : "vsplit") == FAIL)
10659 return FAIL;
10660 ++count;
10661 }
10662
10663 /* Go back to the first window. */
10664 if (count > 0 && (fprintf(fd, fr->fr_layout == FR_COL
10665 ? "%dwincmd k" : "%dwincmd h", count) < 0
10666 || put_eol(fd) == FAIL))
10667 return FAIL;
10668
10669 /* Recursively create frames/windows in each window of this column or
10670 * row. */
10671 frc = ses_skipframe(fr->fr_child);
10672 while (frc != NULL)
10673 {
10674 ses_win_rec(fd, frc);
10675 frc = ses_skipframe(frc->fr_next);
10676 /* Go to next window. */
10677 if (frc != NULL && put_line(fd, "wincmd w") == FAIL)
10678 return FAIL;
10679 }
10680 }
10681 return OK;
10682}
10683
10684/*
10685 * Skip frames that don't contain windows we want to save in the Session.
10686 * Returns NULL when there none.
10687 */
10688 static frame_T *
10689ses_skipframe(fr)
10690 frame_T *fr;
10691{
10692 frame_T *frc;
10693
10694 for (frc = fr; frc != NULL; frc = frc->fr_next)
10695 if (ses_do_frame(frc))
10696 break;
10697 return frc;
10698}
10699
10700/*
10701 * Return TRUE if frame "fr" has a window somewhere that we want to save in
10702 * the Session.
10703 */
10704 static int
10705ses_do_frame(fr)
10706 frame_T *fr;
10707{
10708 frame_T *frc;
10709
10710 if (fr->fr_layout == FR_LEAF)
10711 return ses_do_win(fr->fr_win);
10712 for (frc = fr->fr_child; frc != NULL; frc = frc->fr_next)
10713 if (ses_do_frame(frc))
10714 return TRUE;
10715 return FALSE;
10716}
10717
10718/*
10719 * Return non-zero if window "wp" is to be stored in the Session.
10720 */
10721 static int
10722ses_do_win(wp)
10723 win_T *wp;
10724{
10725 if (wp->w_buffer->b_fname == NULL
10726#ifdef FEAT_QUICKFIX
10727 /* When 'buftype' is "nofile" can't restore the window contents. */
10728 || bt_nofile(wp->w_buffer)
10729#endif
10730 )
10731 return (ssop_flags & SSOP_BLANK);
10732 if (wp->w_buffer->b_help)
10733 return (ssop_flags & SSOP_HELP);
10734 return TRUE;
10735}
10736
10737/*
10738 * Write commands to "fd" to restore the view of a window.
10739 * Caller must make sure 'scrolloff' is zero.
10740 */
10741 static int
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010742put_view(fd, wp, add_edit, flagp, current_arg_idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010743 FILE *fd;
10744 win_T *wp;
10745 int add_edit; /* add ":edit" command to view */
10746 unsigned *flagp; /* vop_flags or ssop_flags */
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010747 int current_arg_idx; /* current argument index of the window, use
10748 * -1 if unknown */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010749{
10750 win_T *save_curwin;
10751 int f;
10752 int do_cursor;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010753 int did_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010754
10755 /* Always restore cursor position for ":mksession". For ":mkview" only
10756 * when 'viewoptions' contains "cursor". */
10757 do_cursor = (flagp == &ssop_flags || *flagp & SSOP_CURSOR);
10758
10759 /*
10760 * Local argument list.
10761 */
10762 if (wp->w_alist == &global_alist)
10763 {
10764 if (put_line(fd, "argglobal") == FAIL)
10765 return FAIL;
10766 }
10767 else
10768 {
10769 if (ses_arglist(fd, "arglocal", &wp->w_alist->al_ga,
10770 flagp == &vop_flags
10771 || !(*flagp & SSOP_CURDIR)
10772 || wp->w_localdir != NULL, flagp) == FAIL)
10773 return FAIL;
10774 }
10775
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010776 /* Only when part of a session: restore the argument index. Some
10777 * arguments may have been deleted, check if the index is valid. */
Bram Moolenaar51f53df2010-06-26 05:25:54 +020010778 if (wp->w_arg_idx != current_arg_idx && wp->w_arg_idx < WARGCOUNT(wp)
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010779 && flagp == &ssop_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010780 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010781 if (fprintf(fd, "%ldargu", (long)wp->w_arg_idx + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010782 || put_eol(fd) == FAIL)
10783 return FAIL;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010784 did_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010785 }
10786
10787 /* Edit the file. Skip this when ":next" already did it. */
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010788 if (add_edit && (!did_next || wp->w_arg_idx_invalid))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010789 {
10790 /*
10791 * Load the file.
10792 */
10793 if (wp->w_buffer->b_ffname != NULL
10794#ifdef FEAT_QUICKFIX
10795 && !bt_nofile(wp->w_buffer)
10796#endif
10797 )
10798 {
10799 /*
10800 * Editing a file in this buffer: use ":edit file".
10801 * This may have side effects! (e.g., compressed or network file).
10802 */
10803 if (fputs("edit ", fd) < 0
10804 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
10805 return FAIL;
10806 }
10807 else
10808 {
10809 /* No file in this buffer, just make it empty. */
10810 if (put_line(fd, "enew") == FAIL)
10811 return FAIL;
10812#ifdef FEAT_QUICKFIX
10813 if (wp->w_buffer->b_ffname != NULL)
10814 {
10815 /* The buffer does have a name, but it's not a file name. */
10816 if (fputs("file ", fd) < 0
10817 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
10818 return FAIL;
10819 }
10820#endif
10821 do_cursor = FALSE;
10822 }
10823 }
10824
10825 /*
10826 * Local mappings and abbreviations.
10827 */
10828 if ((*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
10829 && makemap(fd, wp->w_buffer) == FAIL)
10830 return FAIL;
10831
10832 /*
10833 * Local options. Need to go to the window temporarily.
10834 * Store only local values when using ":mkview" and when ":mksession" is
10835 * used and 'sessionoptions' doesn't include "options".
10836 * Some folding options are always stored when "folds" is included,
10837 * otherwise the folds would not be restored correctly.
10838 */
10839 save_curwin = curwin;
10840 curwin = wp;
10841 curbuf = curwin->w_buffer;
10842 if (*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
10843 f = makeset(fd, OPT_LOCAL,
10844 flagp == &vop_flags || !(*flagp & SSOP_OPTIONS));
10845#ifdef FEAT_FOLDING
10846 else if (*flagp & SSOP_FOLDS)
10847 f = makefoldset(fd);
10848#endif
10849 else
10850 f = OK;
10851 curwin = save_curwin;
10852 curbuf = curwin->w_buffer;
10853 if (f == FAIL)
10854 return FAIL;
10855
10856#ifdef FEAT_FOLDING
10857 /*
10858 * Save Folds when 'buftype' is empty and for help files.
10859 */
10860 if ((*flagp & SSOP_FOLDS)
10861 && wp->w_buffer->b_ffname != NULL
Bram Moolenaarb1b715d2006-01-21 22:09:43 +000010862# ifdef FEAT_QUICKFIX
10863 && (*wp->w_buffer->b_p_bt == NUL || wp->w_buffer->b_help)
10864# endif
10865 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000010866 {
10867 if (put_folds(fd, wp) == FAIL)
10868 return FAIL;
10869 }
10870#endif
10871
10872 /*
10873 * Set the cursor after creating folds, since that moves the cursor.
10874 */
10875 if (do_cursor)
10876 {
10877
10878 /* Restore the cursor line in the file and relatively in the
10879 * window. Don't use "G", it changes the jumplist. */
10880 if (fprintf(fd, "let s:l = %ld - ((%ld * winheight(0) + %ld) / %ld)",
10881 (long)wp->w_cursor.lnum,
10882 (long)(wp->w_cursor.lnum - wp->w_topline),
10883 (long)wp->w_height / 2, (long)wp->w_height) < 0
10884 || put_eol(fd) == FAIL
10885 || put_line(fd, "if s:l < 1 | let s:l = 1 | endif") == FAIL
10886 || put_line(fd, "exe s:l") == FAIL
10887 || put_line(fd, "normal! zt") == FAIL
10888 || fprintf(fd, "%ld", (long)wp->w_cursor.lnum) < 0
10889 || put_eol(fd) == FAIL)
10890 return FAIL;
10891 /* Restore the cursor column and left offset when not wrapping. */
10892 if (wp->w_cursor.col == 0)
10893 {
10894 if (put_line(fd, "normal! 0") == FAIL)
10895 return FAIL;
10896 }
10897 else
10898 {
10899 if (!wp->w_p_wrap && wp->w_leftcol > 0 && wp->w_width > 0)
10900 {
10901 if (fprintf(fd,
10902 "let s:c = %ld - ((%ld * winwidth(0) + %ld) / %ld)",
Bram Moolenaar558ddad2013-02-20 19:26:29 +010010903 (long)wp->w_virtcol + 1,
10904 (long)(wp->w_virtcol - wp->w_leftcol),
Bram Moolenaar071d4272004-06-13 20:20:40 +000010905 (long)wp->w_width / 2, (long)wp->w_width) < 0
10906 || put_eol(fd) == FAIL
10907 || put_line(fd, "if s:c > 0") == FAIL
10908 || fprintf(fd,
Bram Moolenaar558ddad2013-02-20 19:26:29 +010010909 " exe 'normal! ' . s:c . '|zs' . %ld . '|'",
10910 (long)wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010911 || put_eol(fd) == FAIL
10912 || put_line(fd, "else") == FAIL
Bram Moolenaarfdf447b2013-02-26 17:21:29 +010010913 || fprintf(fd, " normal! 0%d|", wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010914 || put_eol(fd) == FAIL
10915 || put_line(fd, "endif") == FAIL)
10916 return FAIL;
10917 }
10918 else
10919 {
Bram Moolenaar558ddad2013-02-20 19:26:29 +010010920 if (fprintf(fd, "normal! 0%d|", wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010921 || put_eol(fd) == FAIL)
10922 return FAIL;
10923 }
10924 }
10925 }
10926
10927 /*
10928 * Local directory.
10929 */
10930 if (wp->w_localdir != NULL)
10931 {
10932 if (fputs("lcd ", fd) < 0
10933 || ses_put_fname(fd, wp->w_localdir, flagp) == FAIL
10934 || put_eol(fd) == FAIL)
10935 return FAIL;
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010936 did_lcd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010937 }
10938
10939 return OK;
10940}
10941
10942/*
10943 * Write an argument list to the session file.
10944 * Returns FAIL if writing fails.
10945 */
10946 static int
10947ses_arglist(fd, cmd, gap, fullname, flagp)
10948 FILE *fd;
10949 char *cmd;
10950 garray_T *gap;
10951 int fullname; /* TRUE: use full path name */
10952 unsigned *flagp;
10953{
10954 int i;
Bram Moolenaard9462e32011-04-11 21:35:11 +020010955 char_u *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010956 char_u *s;
10957
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010010958 if (fputs(cmd, fd) < 0 || put_eol(fd) == FAIL)
10959 return FAIL;
10960 if (put_line(fd, "silent! argdel *") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010961 return FAIL;
10962 for (i = 0; i < gap->ga_len; ++i)
10963 {
10964 /* NULL file names are skipped (only happens when out of memory). */
10965 s = alist_name(&((aentry_T *)gap->ga_data)[i]);
10966 if (s != NULL)
10967 {
10968 if (fullname)
10969 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020010970 buf = alloc(MAXPATHL);
10971 if (buf != NULL)
10972 {
10973 (void)vim_FullName(s, buf, MAXPATHL, FALSE);
10974 s = buf;
10975 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010976 }
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010010977 if (fputs("argadd ", fd) < 0
10978 || ses_put_fname(fd, s, flagp) == FAIL
10979 || put_eol(fd) == FAIL)
Bram Moolenaard9462e32011-04-11 21:35:11 +020010980 {
10981 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010982 return FAIL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020010983 }
10984 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010985 }
10986 }
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010010987 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010988}
10989
10990/*
10991 * Write a buffer name to the session file.
10992 * Also ends the line.
10993 * Returns FAIL if writing fails.
10994 */
10995 static int
10996ses_fname(fd, buf, flagp)
10997 FILE *fd;
10998 buf_T *buf;
10999 unsigned *flagp;
11000{
11001 char_u *name;
11002
11003 /* Use the short file name if the current directory is known at the time
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011004 * the session file will be sourced.
11005 * Don't do this for ":mkview", we don't know the current directory.
11006 * Don't do this after ":lcd", we don't keep track of what the current
11007 * directory is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011008 if (buf->b_sfname != NULL
11009 && flagp == &ssop_flags
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011010 && (ssop_flags & (SSOP_CURDIR | SSOP_SESDIR))
Bram Moolenaar8d594672009-07-01 18:18:57 +000011011#ifdef FEAT_AUTOCHDIR
11012 && !p_acd
11013#endif
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011014 && !did_lcd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011015 name = buf->b_sfname;
11016 else
11017 name = buf->b_ffname;
11018 if (ses_put_fname(fd, name, flagp) == FAIL || put_eol(fd) == FAIL)
11019 return FAIL;
11020 return OK;
11021}
11022
11023/*
11024 * Write a file name to the session file.
11025 * Takes care of the "slash" option in 'sessionoptions' and escapes special
11026 * characters.
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011027 * Returns FAIL if writing fails or out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011028 */
11029 static int
11030ses_put_fname(fd, name, flagp)
11031 FILE *fd;
11032 char_u *name;
11033 unsigned *flagp;
11034{
11035 char_u *sname;
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011036 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011037 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011038
11039 sname = home_replace_save(NULL, name);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011040 if (sname == NULL)
11041 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011042
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011043 if (*flagp & SSOP_SLASH)
11044 {
11045 /* change all backslashes to forward slashes */
11046 for (p = sname; *p != NUL; mb_ptr_adv(p))
11047 if (*p == '\\')
11048 *p = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +000011049 }
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011050
Bram Moolenaar84a05ac2013-05-06 04:24:17 +020011051 /* escape special characters */
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011052 p = vim_strsave_fnameescape(sname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011053 vim_free(sname);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011054 if (p == NULL)
11055 return FAIL;
11056
11057 /* write the result */
11058 if (fputs((char *)p, fd) < 0)
11059 retval = FAIL;
11060
11061 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011062 return retval;
11063}
11064
11065/*
11066 * ":loadview [nr]"
11067 */
11068 static void
11069ex_loadview(eap)
11070 exarg_T *eap;
11071{
11072 char_u *fname;
11073
11074 fname = get_view_file(*eap->arg);
11075 if (fname != NULL)
11076 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +000011077 do_source(fname, FALSE, DOSO_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011078 vim_free(fname);
11079 }
11080}
11081
11082/*
11083 * Get the name of the view file for the current buffer.
11084 */
11085 static char_u *
11086get_view_file(c)
11087 int c;
11088{
11089 int len = 0;
11090 char_u *p, *s;
11091 char_u *retval;
11092 char_u *sname;
11093
11094 if (curbuf->b_ffname == NULL)
11095 {
11096 EMSG(_(e_noname));
11097 return NULL;
11098 }
11099 sname = home_replace_save(NULL, curbuf->b_ffname);
11100 if (sname == NULL)
11101 return NULL;
11102
11103 /*
11104 * We want a file name without separators, because we're not going to make
11105 * a directory.
11106 * "normal" path separator -> "=+"
11107 * "=" -> "=="
11108 * ":" path separator -> "=-"
11109 */
11110 for (p = sname; *p; ++p)
11111 if (*p == '=' || vim_ispathsep(*p))
11112 ++len;
11113 retval = alloc((unsigned)(STRLEN(sname) + len + STRLEN(p_vdir) + 9));
11114 if (retval != NULL)
11115 {
11116 STRCPY(retval, p_vdir);
11117 add_pathsep(retval);
11118 s = retval + STRLEN(retval);
11119 for (p = sname; *p; ++p)
11120 {
11121 if (*p == '=')
11122 {
11123 *s++ = '=';
11124 *s++ = '=';
11125 }
11126 else if (vim_ispathsep(*p))
11127 {
11128 *s++ = '=';
Bram Moolenaare60acc12011-05-10 16:41:25 +020011129#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011130 if (*p == ':')
11131 *s++ = '-';
11132 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000011133#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000011134 *s++ = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +000011135 }
11136 else
11137 *s++ = *p;
11138 }
11139 *s++ = '=';
11140 *s++ = c;
11141 STRCPY(s, ".vim");
11142 }
11143
11144 vim_free(sname);
11145 return retval;
11146}
11147
11148#endif /* FEAT_SESSION */
11149
11150/*
11151 * Write end-of-line character(s) for ":mkexrc", ":mkvimrc" and ":mksession".
11152 * Return FAIL for a write error.
11153 */
11154 int
11155put_eol(fd)
11156 FILE *fd;
11157{
11158 if (
11159#ifdef USE_CRNL
11160 (
11161# ifdef MKSESSION_NL
11162 !mksession_nl &&
11163# endif
11164 (putc('\r', fd) < 0)) ||
11165#endif
11166 (putc('\n', fd) < 0))
11167 return FAIL;
11168 return OK;
11169}
11170
11171/*
11172 * Write a line to "fd".
11173 * Return FAIL for a write error.
11174 */
11175 int
11176put_line(fd, s)
11177 FILE *fd;
11178 char *s;
11179{
11180 if (fputs(s, fd) < 0 || put_eol(fd) == FAIL)
11181 return FAIL;
11182 return OK;
11183}
11184
11185#ifdef FEAT_VIMINFO
11186/*
11187 * ":rviminfo" and ":wviminfo".
11188 */
11189 static void
11190ex_viminfo(eap)
11191 exarg_T *eap;
11192{
11193 char_u *save_viminfo;
11194
11195 save_viminfo = p_viminfo;
11196 if (*p_viminfo == NUL)
11197 p_viminfo = (char_u *)"'100";
11198 if (eap->cmdidx == CMD_rviminfo)
11199 {
Bram Moolenaard812df62008-11-09 12:46:09 +000011200 if (read_viminfo(eap->arg, VIF_WANT_INFO | VIF_WANT_MARKS
11201 | (eap->forceit ? VIF_FORCEIT : 0)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011202 EMSG(_("E195: Cannot open viminfo file for reading"));
11203 }
11204 else
11205 write_viminfo(eap->arg, eap->forceit);
11206 p_viminfo = save_viminfo;
11207}
11208#endif
11209
11210#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +000011211/*
Bram Moolenaard9462e32011-04-11 21:35:11 +020011212 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +000011213 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +000011214 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011215 void
11216dialog_msg(buff, format, fname)
11217 char_u *buff;
11218 char *format;
11219 char_u *fname;
11220{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011221 if (fname == NULL)
11222 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +020011223 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011224}
11225#endif
11226
11227/*
11228 * ":behave {mswin,xterm}"
11229 */
11230 static void
11231ex_behave(eap)
11232 exarg_T *eap;
11233{
11234 if (STRCMP(eap->arg, "mswin") == 0)
11235 {
11236 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
11237 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
11238 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
11239 set_option_value((char_u *)"keymodel", 0L,
11240 (char_u *)"startsel,stopsel", 0);
11241 }
11242 else if (STRCMP(eap->arg, "xterm") == 0)
11243 {
11244 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
11245 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
11246 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
11247 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
11248 }
11249 else
11250 EMSG2(_(e_invarg2), eap->arg);
11251}
11252
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010011253#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
11254/*
11255 * Function given to ExpandGeneric() to obtain the possible arguments of the
11256 * ":behave {mswin,xterm}" command.
11257 */
11258 char_u *
11259get_behave_arg(xp, idx)
11260 expand_T *xp UNUSED;
11261 int idx;
11262{
11263 if (idx == 0)
11264 return (char_u *)"mswin";
11265 if (idx == 1)
11266 return (char_u *)"xterm";
11267 return NULL;
11268}
11269#endif
11270
Bram Moolenaar071d4272004-06-13 20:20:40 +000011271#ifdef FEAT_AUTOCMD
11272static int filetype_detect = FALSE;
11273static int filetype_plugin = FALSE;
11274static int filetype_indent = FALSE;
11275
11276/*
11277 * ":filetype [plugin] [indent] {on,off,detect}"
11278 * on: Load the filetype.vim file to install autocommands for file types.
11279 * off: Load the ftoff.vim file to remove all autocommands for file types.
11280 * plugin on: load filetype.vim and ftplugin.vim
11281 * plugin off: load ftplugof.vim
11282 * indent on: load filetype.vim and indent.vim
11283 * indent off: load indoff.vim
11284 */
11285 static void
11286ex_filetype(eap)
11287 exarg_T *eap;
11288{
11289 char_u *arg = eap->arg;
11290 int plugin = FALSE;
11291 int indent = FALSE;
11292
11293 if (*eap->arg == NUL)
11294 {
11295 /* Print current status. */
11296 smsg((char_u *)"filetype detection:%s plugin:%s indent:%s",
11297 filetype_detect ? "ON" : "OFF",
11298 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
11299 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
11300 return;
11301 }
11302
11303 /* Accept "plugin" and "indent" in any order. */
11304 for (;;)
11305 {
11306 if (STRNCMP(arg, "plugin", 6) == 0)
11307 {
11308 plugin = TRUE;
11309 arg = skipwhite(arg + 6);
11310 continue;
11311 }
11312 if (STRNCMP(arg, "indent", 6) == 0)
11313 {
11314 indent = TRUE;
11315 arg = skipwhite(arg + 6);
11316 continue;
11317 }
11318 break;
11319 }
11320 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
11321 {
11322 if (*arg == 'o' || !filetype_detect)
11323 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011324 source_runtime((char_u *)FILETYPE_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011325 filetype_detect = TRUE;
11326 if (plugin)
11327 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011328 source_runtime((char_u *)FTPLUGIN_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011329 filetype_plugin = TRUE;
11330 }
11331 if (indent)
11332 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011333 source_runtime((char_u *)INDENT_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011334 filetype_indent = TRUE;
11335 }
11336 }
11337 if (*arg == 'd')
11338 {
11339 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE);
Bram Moolenaara3227e22006-03-08 21:32:40 +000011340 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011341 }
11342 }
11343 else if (STRCMP(arg, "off") == 0)
11344 {
11345 if (plugin || indent)
11346 {
11347 if (plugin)
11348 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011349 source_runtime((char_u *)FTPLUGOF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011350 filetype_plugin = FALSE;
11351 }
11352 if (indent)
11353 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011354 source_runtime((char_u *)INDOFF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011355 filetype_indent = FALSE;
11356 }
11357 }
11358 else
11359 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011360 source_runtime((char_u *)FTOFF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011361 filetype_detect = FALSE;
11362 }
11363 }
11364 else
11365 EMSG2(_(e_invarg2), arg);
11366}
11367
11368/*
11369 * ":setfiletype {name}"
11370 */
11371 static void
11372ex_setfiletype(eap)
11373 exarg_T *eap;
11374{
11375 if (!did_filetype)
11376 set_option_value((char_u *)"filetype", 0L, eap->arg, OPT_LOCAL);
11377}
11378#endif
11379
Bram Moolenaar071d4272004-06-13 20:20:40 +000011380 static void
11381ex_digraphs(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +000011382 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011383{
11384#ifdef FEAT_DIGRAPHS
11385 if (*eap->arg != NUL)
11386 putdigraph(eap->arg);
11387 else
11388 listdigraphs();
11389#else
11390 EMSG(_("E196: No digraphs in this version"));
11391#endif
11392}
11393
11394 static void
11395ex_set(eap)
11396 exarg_T *eap;
11397{
11398 int flags = 0;
11399
11400 if (eap->cmdidx == CMD_setlocal)
11401 flags = OPT_LOCAL;
11402 else if (eap->cmdidx == CMD_setglobal)
11403 flags = OPT_GLOBAL;
11404#if defined(FEAT_EVAL) && defined(FEAT_AUTOCMD) && defined(FEAT_BROWSE)
11405 if (cmdmod.browse && flags == 0)
11406 ex_options(eap);
11407 else
11408#endif
11409 (void)do_set(eap->arg, flags);
11410}
11411
11412#ifdef FEAT_SEARCH_EXTRA
11413/*
11414 * ":nohlsearch"
11415 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011416 static void
11417ex_nohlsearch(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +000011418 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011419{
Bram Moolenaar8050efa2013-11-08 04:30:20 +010011420 SET_NO_HLSEARCH(TRUE);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +000011421 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011422}
11423
11424/*
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011425 * ":[N]match {group} {pattern}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000011426 * Sets nextcmd to the start of the next command, if any. Also called when
11427 * skipping commands to find the next command.
11428 */
11429 static void
11430ex_match(eap)
11431 exarg_T *eap;
11432{
11433 char_u *p;
Bram Moolenaar52d36c82007-08-11 14:00:30 +000011434 char_u *g = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011435 char_u *end;
11436 int c;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011437 int id;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011438
11439 if (eap->line2 <= 3)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011440 id = eap->line2;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011441 else
11442 {
11443 EMSG(e_invcmd);
11444 return;
11445 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011446
11447 /* First clear any old pattern. */
11448 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011449 match_delete(curwin, id, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011450
11451 if (ends_excmd(*eap->arg))
11452 end = eap->arg;
11453 else if ((STRNICMP(eap->arg, "none", 4) == 0
11454 && (vim_iswhite(eap->arg[4]) || ends_excmd(eap->arg[4]))))
11455 end = eap->arg + 4;
11456 else
11457 {
11458 p = skiptowhite(eap->arg);
11459 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011460 g = vim_strnsave(eap->arg, (int)(p - eap->arg));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011461 p = skipwhite(p);
11462 if (*p == NUL)
11463 {
11464 /* There must be two arguments. */
11465 EMSG2(_(e_invarg2), eap->arg);
11466 return;
11467 }
11468 end = skip_regexp(p + 1, *p, TRUE, NULL);
11469 if (!eap->skip)
11470 {
11471 if (*end != NUL && !ends_excmd(*skipwhite(end + 1)))
11472 {
11473 eap->errmsg = e_trailing;
11474 return;
11475 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000011476 if (*end != *p)
11477 {
11478 EMSG2(_(e_invarg2), p);
11479 return;
11480 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011481
11482 c = *end;
11483 *end = NUL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011484 match_add(curwin, g, p + 1, 10, id);
11485 vim_free(g);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000011486 *end = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011487 }
11488 }
11489 eap->nextcmd = find_nextcmd(end);
11490}
11491#endif
11492
11493#ifdef FEAT_CRYPT
11494/*
11495 * ":X": Get crypt key
11496 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011497 static void
11498ex_X(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +000011499 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011500{
Bram Moolenaar49771f42010-07-20 17:32:38 +020011501 if (get_crypt_method(curbuf) == 0 || blowfish_self_test() == OK)
Bram Moolenaar40e6a712010-05-16 22:32:54 +020011502 (void)get_crypt_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011503}
11504#endif
11505
11506#ifdef FEAT_FOLDING
11507 static void
11508ex_fold(eap)
11509 exarg_T *eap;
11510{
11511 if (foldManualAllowed(TRUE))
11512 foldCreate(eap->line1, eap->line2);
11513}
11514
11515 static void
11516ex_foldopen(eap)
11517 exarg_T *eap;
11518{
11519 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
11520 eap->forceit, FALSE);
11521}
11522
11523 static void
11524ex_folddo(eap)
11525 exarg_T *eap;
11526{
11527 linenr_T lnum;
11528
11529 /* First set the marks for all lines closed/open. */
11530 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
11531 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
11532 ml_setmarked(lnum);
11533
11534 /* Execute the command on the marked lines. */
11535 global_exe(eap->arg);
11536 ml_clearmarked(); /* clear rest of the marks */
11537}
11538#endif