blob: e2c972403b60ab5ffc281894e663d8531fc36920 [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 Moolenaarcdbac1e2005-12-11 21:27:22 +00001877 case 'n': if (!checkforcmd(&ea.cmd, "noautocmd", 3))
1878 break;
1879#ifdef FEAT_AUTOCMD
1880 if (cmdmod.save_ei == NULL)
1881 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001882 /* Set 'eventignore' to "all". Restore the
1883 * existing option value later. */
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001884 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 Moolenaarcdbac1e2005-12-11 21:27:22 +00001887 }
1888#endif
1889 continue;
1890
Bram Moolenaar071d4272004-06-13 20:20:40 +00001891 case 'r': if (!checkforcmd(&ea.cmd, "rightbelow", 6))
1892 break;
1893#ifdef FEAT_WINDOWS
1894 cmdmod.split |= WSP_BELOW;
1895#endif
1896 continue;
1897
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001898 case 's': if (checkforcmd(&ea.cmd, "sandbox", 3))
1899 {
1900#ifdef HAVE_SANDBOX
1901 if (!did_sandbox)
1902 ++sandbox;
1903 did_sandbox = TRUE;
1904#endif
1905 continue;
1906 }
1907 if (!checkforcmd(&ea.cmd, "silent", 3))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001908 break;
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001909 if (save_msg_silent == -1)
1910 save_msg_silent = msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001911 ++msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912 if (*ea.cmd == '!' && !vim_iswhite(ea.cmd[-1]))
1913 {
1914 /* ":silent!", but not "silent !cmd" */
1915 ea.cmd = skipwhite(ea.cmd + 1);
1916 ++emsg_silent;
1917 ++did_esilent;
1918 }
1919 continue;
1920
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001921 case 't': if (checkforcmd(&p, "tab", 3))
1922 {
1923#ifdef FEAT_WINDOWS
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001924 if (vim_isdigit(*ea.cmd))
1925 cmdmod.tab = atoi((char *)ea.cmd) + 1;
1926 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001927 cmdmod.tab = tabpage_index(curtab) + 1;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001928 ea.cmd = p;
1929#endif
1930 continue;
1931 }
1932 if (!checkforcmd(&ea.cmd, "topleft", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001933 break;
1934#ifdef FEAT_WINDOWS
1935 cmdmod.split |= WSP_TOP;
1936#endif
1937 continue;
1938
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001939 case 'u': if (!checkforcmd(&ea.cmd, "unsilent", 3))
1940 break;
1941 if (save_msg_silent == -1)
1942 save_msg_silent = msg_silent;
1943 msg_silent = 0;
1944 continue;
1945
Bram Moolenaar071d4272004-06-13 20:20:40 +00001946 case 'v': if (checkforcmd(&ea.cmd, "vertical", 4))
1947 {
1948#ifdef FEAT_VERTSPLIT
1949 cmdmod.split |= WSP_VERT;
1950#endif
1951 continue;
1952 }
1953 if (!checkforcmd(&p, "verbose", 4))
1954 break;
1955 if (verbose_save < 0)
1956 verbose_save = p_verbose;
Bram Moolenaared203462004-06-16 11:19:22 +00001957 if (vim_isdigit(*ea.cmd))
1958 p_verbose = atoi((char *)ea.cmd);
1959 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001960 p_verbose = 1;
1961 ea.cmd = p;
1962 continue;
1963 }
1964 break;
1965 }
1966
1967#ifdef FEAT_EVAL
1968 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
1969 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
1970#else
1971 ea.skip = (if_level > 0);
1972#endif
1973
1974#ifdef FEAT_EVAL
Bram Moolenaar05159a02005-02-26 23:04:13 +00001975# ifdef FEAT_PROFILE
1976 /* Count this line for profiling if ea.skip is FALSE. */
Bram Moolenaar371d5402006-03-20 21:47:49 +00001977 if (do_profiling == PROF_YES && !ea.skip)
Bram Moolenaar05159a02005-02-26 23:04:13 +00001978 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001979 if (getline_equal(fgetline, cookie, get_func_line))
1980 func_line_exec(getline_cookie(fgetline, cookie));
1981 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +00001982 script_line_exec();
1983 }
1984#endif
1985
Bram Moolenaar071d4272004-06-13 20:20:40 +00001986 /* May go to debug mode. If this happens and the ">quit" debug command is
1987 * used, throw an interrupt exception and skip the next command. */
1988 dbg_check_breakpoint(&ea);
1989 if (!ea.skip && got_int)
1990 {
1991 ea.skip = TRUE;
1992 (void)do_intthrow(cstack);
1993 }
1994#endif
1995
1996/*
1997 * 3. parse a range specifier of the form: addr [,addr] [;addr] ..
1998 *
1999 * where 'addr' is:
2000 *
2001 * % (entire file)
2002 * $ [+-NUM]
2003 * 'x [+-NUM] (where x denotes a currently defined mark)
2004 * . [+-NUM]
2005 * [+-NUM]..
2006 * NUM
2007 *
2008 * The ea.cmd pointer is updated to point to the first character following the
2009 * range spec. If an initial address is found, but no second, the upper bound
2010 * is equal to the lower.
2011 */
2012
2013 /* repeat for all ',' or ';' separated addresses */
2014 for (;;)
2015 {
2016 ea.line1 = ea.line2;
2017 ea.line2 = curwin->w_cursor.lnum; /* default is current line number */
2018 ea.cmd = skipwhite(ea.cmd);
2019 lnum = get_address(&ea.cmd, ea.skip, ea.addr_count == 0);
2020 if (ea.cmd == NULL) /* error detected */
2021 goto doend;
2022 if (lnum == MAXLNUM)
2023 {
2024 if (*ea.cmd == '%') /* '%' - all lines */
2025 {
2026 ++ea.cmd;
2027 ea.line1 = 1;
2028 ea.line2 = curbuf->b_ml.ml_line_count;
2029 ++ea.addr_count;
2030 }
2031 /* '*' - visual area */
2032 else if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
2033 {
2034 pos_T *fp;
2035
2036 ++ea.cmd;
2037 if (!ea.skip)
2038 {
2039 fp = getmark('<', FALSE);
2040 if (check_mark(fp) == FAIL)
2041 goto doend;
2042 ea.line1 = fp->lnum;
2043 fp = getmark('>', FALSE);
2044 if (check_mark(fp) == FAIL)
2045 goto doend;
2046 ea.line2 = fp->lnum;
2047 ++ea.addr_count;
2048 }
2049 }
2050 }
2051 else
2052 ea.line2 = lnum;
2053 ea.addr_count++;
2054
2055 if (*ea.cmd == ';')
2056 {
2057 if (!ea.skip)
2058 curwin->w_cursor.lnum = ea.line2;
2059 }
2060 else if (*ea.cmd != ',')
2061 break;
2062 ++ea.cmd;
2063 }
2064
2065 /* One address given: set start and end lines */
2066 if (ea.addr_count == 1)
2067 {
2068 ea.line1 = ea.line2;
2069 /* ... but only implicit: really no address given */
2070 if (lnum == MAXLNUM)
2071 ea.addr_count = 0;
2072 }
2073
2074 /* Don't leave the cursor on an illegal line (caused by ';') */
2075 check_cursor_lnum();
2076
2077/*
2078 * 4. parse command
2079 */
2080
2081 /*
2082 * Skip ':' and any white space
2083 */
2084 ea.cmd = skipwhite(ea.cmd);
2085 while (*ea.cmd == ':')
2086 ea.cmd = skipwhite(ea.cmd + 1);
2087
2088 /*
2089 * If we got a line, but no command, then go to the line.
2090 * If we find a '|' or '\n' we set ea.nextcmd.
2091 */
2092 if (*ea.cmd == NUL || *ea.cmd == '"' ||
2093 (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
2094 {
2095 /*
2096 * strange vi behaviour:
2097 * ":3" jumps to line 3
2098 * ":3|..." prints line 3
2099 * ":|" prints current line
2100 */
2101 if (ea.skip) /* skip this if inside :if */
2102 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00002103 if (*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002104 {
2105 ea.cmdidx = CMD_print;
2106 ea.argt = RANGE+COUNT+TRLBAR;
2107 if ((errormsg = invalid_range(&ea)) == NULL)
2108 {
2109 correct_range(&ea);
2110 ex_print(&ea);
2111 }
2112 }
2113 else if (ea.addr_count != 0)
2114 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002115 if (ea.line2 > curbuf->b_ml.ml_line_count)
2116 {
2117 /* With '-' in 'cpoptions' a line number past the file is an
2118 * error, otherwise put it at the end of the file. */
2119 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
2120 ea.line2 = -1;
2121 else
2122 ea.line2 = curbuf->b_ml.ml_line_count;
2123 }
2124
2125 if (ea.line2 < 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002126 errormsg = (char_u *)_(e_invrange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002127 else
2128 {
2129 if (ea.line2 == 0)
2130 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002131 else
2132 curwin->w_cursor.lnum = ea.line2;
2133 beginline(BL_SOL | BL_FIX);
2134 }
2135 }
2136 goto doend;
2137 }
2138
2139 /* Find the command and let "p" point to after it. */
2140 p = find_command(&ea, NULL);
2141
2142#ifdef FEAT_USR_CMDS
2143 if (p == NULL)
2144 {
2145 if (!ea.skip)
2146 errormsg = (char_u *)_("E464: Ambiguous use of user-defined command");
2147 goto doend;
2148 }
2149 /* Check for wrong commands. */
2150 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78)
2151 {
2152 errormsg = uc_fun_cmd();
2153 goto doend;
2154 }
2155#endif
2156 if (ea.cmdidx == CMD_SIZE)
2157 {
2158 if (!ea.skip)
2159 {
2160 STRCPY(IObuff, _("E492: Not an editor command"));
2161 if (!sourcing)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002162 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163 errormsg = IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02002164 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165 }
2166 goto doend;
2167 }
2168
2169 ni = (
2170#ifdef FEAT_USR_CMDS
2171 !USER_CMDIDX(ea.cmdidx) &&
2172#endif
Bram Moolenaar3ebc1e52007-07-05 07:54:17 +00002173 (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00002174#ifdef HAVE_EX_SCRIPT_NI
2175 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
2176#endif
2177 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178
2179#ifndef FEAT_EVAL
2180 /*
2181 * When the expression evaluation is disabled, recognize the ":if" and
2182 * ":endif" commands and ignore everything in between it.
2183 */
2184 if (ea.cmdidx == CMD_if)
2185 ++if_level;
2186 if (if_level)
2187 {
2188 if (ea.cmdidx == CMD_endif)
2189 --if_level;
2190 goto doend;
2191 }
2192
2193#endif
2194
Bram Moolenaar196b3b02008-06-20 16:51:41 +00002195 /* forced commands */
2196 if (*p == '!' && ea.cmdidx != CMD_substitute
2197 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002198 {
2199 ++p;
2200 ea.forceit = TRUE;
2201 }
2202 else
2203 ea.forceit = FALSE;
2204
2205/*
2206 * 5. parse arguments
2207 */
2208#ifdef FEAT_USR_CMDS
2209 if (!USER_CMDIDX(ea.cmdidx))
2210#endif
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002211 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002212
2213 if (!ea.skip)
2214 {
2215#ifdef HAVE_SANDBOX
2216 if (sandbox != 0 && !(ea.argt & SBOXOK))
2217 {
2218 /* Command not allowed in sandbox. */
2219 errormsg = (char_u *)_(e_sandbox);
2220 goto doend;
2221 }
2222#endif
2223 if (!curbuf->b_p_ma && (ea.argt & MODIFY))
2224 {
2225 /* Command not allowed in non-'modifiable' buffer */
2226 errormsg = (char_u *)_(e_modifiable);
2227 goto doend;
2228 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002229
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002230 if (text_locked() && !(ea.argt & CMDWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231# ifdef FEAT_USR_CMDS
2232 && !USER_CMDIDX(ea.cmdidx)
2233# endif
2234 )
2235 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002236 /* Command not allowed when editing the command line. */
2237#ifdef FEAT_CMDWIN
2238 if (cmdwin_type != 0)
2239 errormsg = (char_u *)_(e_cmdwin);
2240 else
2241#endif
2242 errormsg = (char_u *)_(e_secure);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002243 goto doend;
2244 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002245#ifdef FEAT_AUTOCMD
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002246 /* Disallow editing another buffer when "curbuf_lock" is set.
2247 * Do allow ":edit" (check for argument later).
2248 * Do allow ":checktime" (it's postponed). */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002249 if (!(ea.argt & CMDWIN)
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002250 && ea.cmdidx != CMD_edit
2251 && ea.cmdidx != CMD_checktime
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002252# ifdef FEAT_USR_CMDS
2253 && !USER_CMDIDX(ea.cmdidx)
2254# endif
2255 && curbuf_locked())
2256 goto doend;
2257#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258
2259 if (!ni && !(ea.argt & RANGE) && ea.addr_count > 0)
2260 {
2261 /* no range allowed */
2262 errormsg = (char_u *)_(e_norange);
2263 goto doend;
2264 }
2265 }
2266
2267 if (!ni && !(ea.argt & BANG) && ea.forceit) /* no <!> allowed */
2268 {
2269 errormsg = (char_u *)_(e_nobang);
2270 goto doend;
2271 }
2272
2273 /*
2274 * Don't complain about the range if it is not used
2275 * (could happen if line_count is accidentally set to 0).
2276 */
2277 if (!ea.skip && !ni)
2278 {
2279 /*
2280 * If the range is backwards, ask for confirmation and, if given, swap
2281 * ea.line1 & ea.line2 so it's forwards again.
2282 * When global command is busy, don't ask, will fail below.
2283 */
2284 if (!global_busy && ea.line1 > ea.line2)
2285 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002286 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002287 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002288 if (sourcing || exmode_active)
2289 {
2290 errormsg = (char_u *)_("E493: Backwards range given");
2291 goto doend;
2292 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002293 if (ask_yesno((char_u *)
2294 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002295 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002296 }
2297 lnum = ea.line1;
2298 ea.line1 = ea.line2;
2299 ea.line2 = lnum;
2300 }
2301 if ((errormsg = invalid_range(&ea)) != NULL)
2302 goto doend;
2303 }
2304
2305 if ((ea.argt & NOTADR) && ea.addr_count == 0) /* default is 1, not cursor */
2306 ea.line2 = 1;
2307
2308 correct_range(&ea);
2309
2310#ifdef FEAT_FOLDING
2311 if (((ea.argt & WHOLEFOLD) || ea.addr_count >= 2) && !global_busy)
2312 {
2313 /* Put the first line at the start of a closed fold, put the last line
2314 * at the end of a closed fold. */
2315 (void)hasFolding(ea.line1, &ea.line1, NULL);
2316 (void)hasFolding(ea.line2, NULL, &ea.line2);
2317 }
2318#endif
2319
2320#ifdef FEAT_QUICKFIX
2321 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002322 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323 * option here, so things like % get expanded.
2324 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002325 p = replace_makeprg(&ea, p, cmdlinep);
2326 if (p == NULL)
2327 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002328#endif
2329
2330 /*
2331 * Skip to start of argument.
2332 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2333 */
2334 if (ea.cmdidx == CMD_bang)
2335 ea.arg = p;
2336 else
2337 ea.arg = skipwhite(p);
2338
2339 /*
2340 * Check for "++opt=val" argument.
2341 * Must be first, allow ":w ++enc=utf8 !cmd"
2342 */
2343 if (ea.argt & ARGOPT)
2344 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2345 if (getargopt(&ea) == FAIL && !ni)
2346 {
2347 errormsg = (char_u *)_(e_invarg);
2348 goto doend;
2349 }
2350
2351 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2352 {
2353 if (*ea.arg == '>') /* append */
2354 {
2355 if (*++ea.arg != '>') /* typed wrong */
2356 {
2357 errormsg = (char_u *)_("E494: Use w or w>>");
2358 goto doend;
2359 }
2360 ea.arg = skipwhite(ea.arg + 1);
2361 ea.append = TRUE;
2362 }
2363 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
2364 {
2365 ++ea.arg;
2366 ea.usefilter = TRUE;
2367 }
2368 }
2369
2370 if (ea.cmdidx == CMD_read)
2371 {
2372 if (ea.forceit)
2373 {
2374 ea.usefilter = TRUE; /* :r! filter if ea.forceit */
2375 ea.forceit = FALSE;
2376 }
2377 else if (*ea.arg == '!') /* :r !filter */
2378 {
2379 ++ea.arg;
2380 ea.usefilter = TRUE;
2381 }
2382 }
2383
2384 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2385 {
2386 ea.amount = 1;
2387 while (*ea.arg == *ea.cmd) /* count number of '>' or '<' */
2388 {
2389 ++ea.arg;
2390 ++ea.amount;
2391 }
2392 ea.arg = skipwhite(ea.arg);
2393 }
2394
2395 /*
2396 * Check for "+command" argument, before checking for next command.
2397 * Don't do this for ":read !cmd" and ":write !cmd".
2398 */
2399 if ((ea.argt & EDITCMD) && !ea.usefilter)
2400 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2401
2402 /*
2403 * Check for '|' to separate commands and '"' to start comments.
2404 * Don't do this for ":read !cmd" and ":write !cmd".
2405 */
2406 if ((ea.argt & TRLBAR) && !ea.usefilter)
2407 separate_nextcmd(&ea);
2408
2409 /*
2410 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002411 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2412 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002414 else if (ea.cmdidx == CMD_bang
2415 || ea.cmdidx == CMD_global
2416 || ea.cmdidx == CMD_vglobal
2417 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002418 {
2419 for (p = ea.arg; *p; ++p)
2420 {
2421 /* Remove one backslash before a newline, so that it's possible to
2422 * pass a newline to the shell and also a newline that is preceded
2423 * with a backslash. This makes it impossible to end a shell
2424 * command in a backslash, but that doesn't appear useful.
2425 * Halving the number of backslashes is incompatible with previous
2426 * versions. */
2427 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002428 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429 else if (*p == '\n')
2430 {
2431 ea.nextcmd = p + 1;
2432 *p = NUL;
2433 break;
2434 }
2435 }
2436 }
2437
2438 if ((ea.argt & DFLALL) && ea.addr_count == 0)
2439 {
2440 ea.line1 = 1;
2441 ea.line2 = curbuf->b_ml.ml_line_count;
2442 }
2443
2444 /* accept numbered register only when no count allowed (:put) */
2445 if ( (ea.argt & REGSTR)
2446 && *ea.arg != NUL
2447#ifdef FEAT_USR_CMDS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448 /* Do not allow register = for user commands */
2449 && (!USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002450#endif
2451 && !((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)))
2452 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002453#ifndef FEAT_CLIPBOARD
2454 /* check these explicitly for a more specific error message */
2455 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002457 errormsg = (char_u *)_(e_invalidreg);
2458 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459 }
2460#endif
Bram Moolenaar85de2062011-05-05 14:26:41 +02002461 if (
2462#ifdef FEAT_USR_CMDS
2463 valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2464 && USER_CMDIDX(ea.cmdidx)))
2465#else
2466 valid_yank_reg(*ea.arg, ea.cmdidx != CMD_put)
2467#endif
2468 )
2469 {
2470 ea.regname = *ea.arg++;
2471#ifdef FEAT_EVAL
2472 /* for '=' register: accept the rest of the line as an expression */
2473 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2474 {
2475 set_expr_line(vim_strsave(ea.arg));
2476 ea.arg += STRLEN(ea.arg);
2477 }
2478#endif
2479 ea.arg = skipwhite(ea.arg);
2480 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481 }
2482
2483 /*
2484 * Check for a count. When accepting a BUFNAME, don't use "123foo" as a
2485 * count, it's a buffer name.
2486 */
2487 if ((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)
2488 && (!(ea.argt & BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
2489 || vim_iswhite(*p)))
2490 {
2491 n = getdigits(&ea.arg);
2492 ea.arg = skipwhite(ea.arg);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002493 if (n <= 0 && !ni && (ea.argt & ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494 {
2495 errormsg = (char_u *)_(e_zerocount);
2496 goto doend;
2497 }
2498 if (ea.argt & NOTADR) /* e.g. :buffer 2, :sleep 3 */
2499 {
2500 ea.line2 = n;
2501 if (ea.addr_count == 0)
2502 ea.addr_count = 1;
2503 }
2504 else
2505 {
2506 ea.line1 = ea.line2;
2507 ea.line2 += n - 1;
2508 ++ea.addr_count;
2509 /*
2510 * Be vi compatible: no error message for out of range.
2511 */
2512 if (ea.line2 > curbuf->b_ml.ml_line_count)
2513 ea.line2 = curbuf->b_ml.ml_line_count;
2514 }
2515 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002516
2517 /*
2518 * Check for flags: 'l', 'p' and '#'.
2519 */
2520 if (ea.argt & EXFLAGS)
2521 get_flags(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522 /* no arguments allowed */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002523 if (!ni && !(ea.argt & EXTRA) && *ea.arg != NUL
Bram Moolenaara2031822006-03-07 22:29:51 +00002524 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002525 {
2526 errormsg = (char_u *)_(e_trailing);
2527 goto doend;
2528 }
2529
2530 if (!ni && (ea.argt & NEEDARG) && *ea.arg == NUL)
2531 {
2532 errormsg = (char_u *)_(e_argreq);
2533 goto doend;
2534 }
2535
2536#ifdef FEAT_EVAL
2537 /*
2538 * Skip the command when it's not going to be executed.
2539 * The commands like :if, :endif, etc. always need to be executed.
2540 * Also make an exception for commands that handle a trailing command
2541 * themselves.
2542 */
2543 if (ea.skip)
2544 {
2545 switch (ea.cmdidx)
2546 {
2547 /* commands that need evaluation */
2548 case CMD_while:
2549 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002550 case CMD_for:
2551 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002552 case CMD_if:
2553 case CMD_elseif:
2554 case CMD_else:
2555 case CMD_endif:
2556 case CMD_try:
2557 case CMD_catch:
2558 case CMD_finally:
2559 case CMD_endtry:
2560 case CMD_function:
2561 break;
2562
2563 /* Commands that handle '|' themselves. Check: A command should
2564 * either have the TRLBAR flag, appear in this list or appear in
2565 * the list at ":help :bar". */
2566 case CMD_aboveleft:
2567 case CMD_and:
2568 case CMD_belowright:
2569 case CMD_botright:
2570 case CMD_browse:
2571 case CMD_call:
2572 case CMD_confirm:
2573 case CMD_delfunction:
2574 case CMD_djump:
2575 case CMD_dlist:
2576 case CMD_dsearch:
2577 case CMD_dsplit:
2578 case CMD_echo:
2579 case CMD_echoerr:
2580 case CMD_echomsg:
2581 case CMD_echon:
2582 case CMD_execute:
2583 case CMD_help:
2584 case CMD_hide:
2585 case CMD_ijump:
2586 case CMD_ilist:
2587 case CMD_isearch:
2588 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002589 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002590 case CMD_keepjumps:
2591 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01002592 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593 case CMD_leftabove:
2594 case CMD_let:
2595 case CMD_lockmarks:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002596 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002597 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002598 case CMD_mzscheme:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599 case CMD_perl:
2600 case CMD_psearch:
2601 case CMD_python:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002602 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002603 case CMD_python3:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604 case CMD_return:
2605 case CMD_rightbelow:
2606 case CMD_ruby:
2607 case CMD_silent:
2608 case CMD_smagic:
2609 case CMD_snomagic:
2610 case CMD_substitute:
2611 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002612 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002613 case CMD_tcl:
2614 case CMD_throw:
2615 case CMD_tilde:
2616 case CMD_topleft:
2617 case CMD_unlet:
2618 case CMD_verbose:
2619 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002620 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002621 break;
2622
2623 default: goto doend;
2624 }
2625 }
2626#endif
2627
2628 if (ea.argt & XFILE)
2629 {
2630 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2631 goto doend;
2632 }
2633
2634#ifdef FEAT_LISTCMDS
2635 /*
2636 * Accept buffer name. Cannot be used at the same time with a buffer
2637 * number. Don't do this for a user command.
2638 */
2639 if ((ea.argt & BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
2640# ifdef FEAT_USR_CMDS
2641 && !USER_CMDIDX(ea.cmdidx)
2642# endif
2643 )
2644 {
2645 /*
2646 * :bdelete, :bwipeout and :bunload take several arguments, separated
2647 * by spaces: find next space (skipping over escaped characters).
2648 * The others take one argument: ignore trailing spaces.
2649 */
2650 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2651 || ea.cmdidx == CMD_bunload)
2652 p = skiptowhite_esc(ea.arg);
2653 else
2654 {
2655 p = ea.arg + STRLEN(ea.arg);
2656 while (p > ea.arg && vim_iswhite(p[-1]))
2657 --p;
2658 }
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002659 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & BUFUNL) != 0,
2660 FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661 if (ea.line2 < 0) /* failed */
2662 goto doend;
2663 ea.addr_count = 1;
2664 ea.arg = skipwhite(p);
2665 }
2666#endif
2667
2668/*
2669 * 6. switch on command name
2670 *
2671 * The "ea" structure holds the arguments that can be used.
2672 */
2673 ea.cmdlinep = cmdlinep;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002674 ea.getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675 ea.cookie = cookie;
2676#ifdef FEAT_EVAL
2677 ea.cstack = cstack;
2678#endif
2679
2680#ifdef FEAT_USR_CMDS
2681 if (USER_CMDIDX(ea.cmdidx))
2682 {
2683 /*
2684 * Execute a user-defined command.
2685 */
2686 do_ucmd(&ea);
2687 }
2688 else
2689#endif
2690 {
2691 /*
2692 * Call the function to execute the command.
2693 */
2694 ea.errmsg = NULL;
2695 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2696 if (ea.errmsg != NULL)
2697 errormsg = (char_u *)_(ea.errmsg);
2698 }
2699
2700#ifdef FEAT_EVAL
2701 /*
2702 * If the command just executed called do_cmdline(), any throw or ":return"
2703 * or ":finish" encountered there must also check the cstack of the still
2704 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2705 * exception, or reanimate a returned function or finished script file and
2706 * return or finish it again.
2707 */
2708 if (need_rethrow)
2709 do_throw(cstack);
2710 else if (check_cstack)
2711 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002712 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002714 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715 && current_func_returned())
2716 do_return(&ea, TRUE, FALSE, NULL);
2717 }
2718 need_rethrow = check_cstack = FALSE;
2719#endif
2720
2721doend:
2722 if (curwin->w_cursor.lnum == 0) /* can happen with zero line number */
2723 curwin->w_cursor.lnum = 1;
2724
2725 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2726 {
2727 if (sourcing)
2728 {
2729 if (errormsg != IObuff)
2730 {
2731 STRCPY(IObuff, errormsg);
2732 errormsg = IObuff;
2733 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002734 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002735 }
2736 emsg(errormsg);
2737 }
2738#ifdef FEAT_EVAL
2739 do_errthrow(cstack,
2740 (ea.cmdidx != CMD_SIZE
2741# ifdef FEAT_USR_CMDS
2742 && !USER_CMDIDX(ea.cmdidx)
2743# endif
2744 ) ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
2745#endif
2746
2747 if (verbose_save >= 0)
2748 p_verbose = verbose_save;
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00002749#ifdef FEAT_AUTOCMD
2750 if (cmdmod.save_ei != NULL)
2751 {
2752 /* Restore 'eventignore' to the value before ":noautocmd". */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002753 set_string_option_direct((char_u *)"ei", -1, cmdmod.save_ei,
2754 OPT_FREE, SID_NONE);
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00002755 free_string_option(cmdmod.save_ei);
2756 }
2757#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002758
2759 cmdmod = save_cmdmod;
2760
Bram Moolenaar8e258a42009-07-09 13:55:43 +00002761 if (save_msg_silent != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002762 {
2763 /* messages could be enabled for a serious error, need to check if the
2764 * counters don't become negative */
Bram Moolenaarbecf4282009-09-30 11:24:36 +00002765 if (!did_emsg || msg_silent > save_msg_silent)
Bram Moolenaar8e258a42009-07-09 13:55:43 +00002766 msg_silent = save_msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002767 emsg_silent -= did_esilent;
2768 if (emsg_silent < 0)
2769 emsg_silent = 0;
2770 /* Restore msg_scroll, it's set by file I/O commands, even when no
2771 * message is actually displayed. */
2772 msg_scroll = save_msg_scroll;
Bram Moolenaar77ab2802009-04-22 12:44:48 +00002773
2774 /* "silent reg" or "silent echo x" inside "redir" leaves msg_col
2775 * somewhere in the line. Put it back in the first column. */
2776 if (redirecting())
2777 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002778 }
2779
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002780#ifdef HAVE_SANDBOX
2781 if (did_sandbox)
2782 --sandbox;
2783#endif
2784
Bram Moolenaar071d4272004-06-13 20:20:40 +00002785 if (ea.nextcmd && *ea.nextcmd == NUL) /* not really a next command */
2786 ea.nextcmd = NULL;
2787
2788#ifdef FEAT_EVAL
2789 --ex_nesting_level;
2790#endif
2791
2792 return ea.nextcmd;
2793}
2794#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00002795 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002796#endif
2797
2798/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002799 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002800 * If there is a match advance "pp" to the argument and return TRUE.
2801 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002802 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00002803checkforcmd(pp, cmd, len)
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002804 char_u **pp; /* start of command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002805 char *cmd; /* name of command */
2806 int len; /* required length */
2807{
2808 int i;
2809
2810 for (i = 0; cmd[i] != NUL; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00002811 if (((char_u *)cmd)[i] != (*pp)[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00002812 break;
2813 if (i >= len && !isalpha((*pp)[i]))
2814 {
2815 *pp = skipwhite(*pp + i);
2816 return TRUE;
2817 }
2818 return FALSE;
2819}
2820
2821/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002822 * Append "cmd" to the error message in IObuff.
2823 * Takes care of limiting the length and handling 0xa0, which would be
2824 * invisible otherwise.
2825 */
2826 static void
2827append_command(cmd)
2828 char_u *cmd;
2829{
2830 char_u *s = cmd;
2831 char_u *d;
2832
2833 STRCAT(IObuff, ": ");
2834 d = IObuff + STRLEN(IObuff);
2835 while (*s != NUL && d - IObuff < IOSIZE - 7)
2836 {
2837 if (
2838#ifdef FEAT_MBYTE
2839 enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) :
2840#endif
2841 *s == 0xa0)
2842 {
2843 s +=
2844#ifdef FEAT_MBYTE
2845 enc_utf8 ? 2 :
2846#endif
2847 1;
2848 STRCPY(d, "<a0>");
2849 d += 4;
2850 }
2851 else
2852 MB_COPY_CHAR(s, d);
2853 }
2854 *d = NUL;
2855}
2856
2857/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002859 * Start of the name can be found at eap->cmd.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002860 * Returns pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002861 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002862 * Returns NULL for an ambiguous user command.
2863 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002864 static char_u *
2865find_command(eap, full)
2866 exarg_T *eap;
Bram Moolenaar78a15312009-05-15 19:33:18 +00002867 int *full UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002868{
2869 int len;
2870 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00002871 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002872
2873 /*
2874 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00002875 * Exceptions:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002876 * - the 'k' command can directly be followed by any character.
2877 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
2878 * but :sre[wind] is another command, as are :scrip[tnames],
2879 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00002880 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881 */
2882 p = eap->cmd;
2883 if (*p == 'k')
2884 {
2885 eap->cmdidx = CMD_k;
2886 ++p;
2887 }
2888 else if (p[0] == 's'
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002889 && ((p[1] == 'c' && p[2] != 's' && p[2] != 'r'
2890 && p[3] != 'i' && p[4] != 'p')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002891 || p[1] == 'g'
2892 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
2893 || p[1] == 'I'
2894 || (p[1] == 'r' && p[2] != 'e')))
2895 {
2896 eap->cmdidx = CMD_substitute;
2897 ++p;
2898 }
2899 else
2900 {
2901 while (ASCII_ISALPHA(*p))
2902 ++p;
Bram Moolenaarb6590522010-07-21 16:00:43 +02002903 /* for python 3.x support ":py3", ":python3", ":py3file", etc. */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002904 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaarb6590522010-07-21 16:00:43 +02002905 while (ASCII_ISALNUM(*p))
2906 ++p;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02002907
Bram Moolenaar071d4272004-06-13 20:20:40 +00002908 /* check for non-alpha command */
2909 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
2910 ++p;
2911 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00002912 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
2913 {
2914 /* Check for ":dl", ":dell", etc. to ":deletel": that's
2915 * :delete with the 'l' flag. Same for 'p'. */
2916 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00002917 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00002918 break;
2919 if (i == len - 1)
2920 {
2921 --len;
2922 if (p[-1] == 'l')
2923 eap->flags |= EXFLAG_LIST;
2924 else
2925 eap->flags |= EXFLAG_PRINT;
2926 }
2927 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002928
2929 if (ASCII_ISLOWER(*eap->cmd))
2930 eap->cmdidx = cmdidxs[CharOrdLow(*eap->cmd)];
2931 else
2932 eap->cmdidx = cmdidxs[26];
2933
2934 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
2935 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
2936 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
2937 (size_t)len) == 0)
2938 {
2939#ifdef FEAT_EVAL
2940 if (full != NULL
2941 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
2942 *full = TRUE;
2943#endif
2944 break;
2945 }
2946
2947#ifdef FEAT_USR_CMDS
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01002948 /* Look for a user defined command as a last resort. Let ":Print" be
2949 * overruled by a user defined command. */
2950 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
2951 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002952 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002953 /* User defined commands may contain digits. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002954 while (ASCII_ISALNUM(*p))
2955 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002956 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002957 }
2958#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002959 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002960 eap->cmdidx = CMD_SIZE;
2961 }
2962
2963 return p;
2964}
2965
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002966#ifdef FEAT_USR_CMDS
2967/*
2968 * Search for a user command that matches "eap->cmd".
2969 * Return cmdidx in "eap->cmdidx", flags in "eap->argt", idx in "eap->useridx".
2970 * Return a pointer to just after the command.
2971 * Return NULL if there is no matching command.
2972 */
2973 static char_u *
2974find_ucmd(eap, p, full, xp, compl)
2975 exarg_T *eap;
2976 char_u *p; /* end of the command (possibly including count) */
2977 int *full; /* set to TRUE for a full match */
2978 expand_T *xp; /* used for completion, NULL otherwise */
2979 int *compl; /* completion flags or NULL */
2980{
2981 int len = (int)(p - eap->cmd);
2982 int j, k, matchlen = 0;
2983 ucmd_T *uc;
2984 int found = FALSE;
2985 int possible = FALSE;
2986 char_u *cp, *np; /* Point into typed cmd and test name */
2987 garray_T *gap;
2988 int amb_local = FALSE; /* Found ambiguous buffer-local command,
2989 only full match global is accepted. */
2990
2991 /*
2992 * Look for buffer-local user commands first, then global ones.
2993 */
2994 gap = &curbuf->b_ucmds;
2995 for (;;)
2996 {
2997 for (j = 0; j < gap->ga_len; ++j)
2998 {
2999 uc = USER_CMD_GA(gap, j);
3000 cp = eap->cmd;
3001 np = uc->uc_name;
3002 k = 0;
3003 while (k < len && *np != NUL && *cp++ == *np++)
3004 k++;
3005 if (k == len || (*np == NUL && vim_isdigit(eap->cmd[k])))
3006 {
3007 /* If finding a second match, the command is ambiguous. But
3008 * not if a buffer-local command wasn't a full match and a
3009 * global command is a full match. */
3010 if (k == len && found && *np != NUL)
3011 {
3012 if (gap == &ucmds)
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003013 return NULL;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003014 amb_local = TRUE;
3015 }
3016
3017 if (!found || (k == len && *np == NUL))
3018 {
3019 /* If we matched up to a digit, then there could
3020 * be another command including the digit that we
3021 * should use instead.
3022 */
3023 if (k == len)
3024 found = TRUE;
3025 else
3026 possible = TRUE;
3027
3028 if (gap == &ucmds)
3029 eap->cmdidx = CMD_USER;
3030 else
3031 eap->cmdidx = CMD_USER_BUF;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003032 eap->argt = (long)uc->uc_argt;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003033 eap->useridx = j;
3034
3035# ifdef FEAT_CMDL_COMPL
3036 if (compl != NULL)
3037 *compl = uc->uc_compl;
3038# ifdef FEAT_EVAL
3039 if (xp != NULL)
3040 {
3041 xp->xp_arg = uc->uc_compl_arg;
3042 xp->xp_scriptID = uc->uc_scriptID;
3043 }
3044# endif
3045# endif
3046 /* Do not search for further abbreviations
3047 * if this is an exact match. */
3048 matchlen = k;
3049 if (k == len && *np == NUL)
3050 {
3051 if (full != NULL)
3052 *full = TRUE;
3053 amb_local = FALSE;
3054 break;
3055 }
3056 }
3057 }
3058 }
3059
3060 /* Stop if we found a full match or searched all. */
3061 if (j < gap->ga_len || gap == &ucmds)
3062 break;
3063 gap = &ucmds;
3064 }
3065
3066 /* Only found ambiguous matches. */
3067 if (amb_local)
3068 {
3069 if (xp != NULL)
3070 xp->xp_context = EXPAND_UNSUCCESSFUL;
3071 return NULL;
3072 }
3073
3074 /* The match we found may be followed immediately by a number. Move "p"
3075 * back to point to it. */
3076 if (found || possible)
3077 return p + (matchlen - len);
3078 return p;
3079}
3080#endif
3081
Bram Moolenaar071d4272004-06-13 20:20:40 +00003082#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003083static struct cmdmod
3084{
3085 char *name;
3086 int minlen;
3087 int has_count; /* :123verbose :3tab */
3088} cmdmods[] = {
3089 {"aboveleft", 3, FALSE},
3090 {"belowright", 3, FALSE},
3091 {"botright", 2, FALSE},
3092 {"browse", 3, FALSE},
3093 {"confirm", 4, FALSE},
3094 {"hide", 3, FALSE},
3095 {"keepalt", 5, FALSE},
3096 {"keepjumps", 5, FALSE},
3097 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01003098 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003099 {"leftabove", 5, FALSE},
3100 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003101 {"noautocmd", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003102 {"rightbelow", 6, FALSE},
3103 {"sandbox", 3, FALSE},
3104 {"silent", 3, FALSE},
3105 {"tab", 3, TRUE},
3106 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003107 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003108 {"verbose", 4, TRUE},
3109 {"vertical", 4, FALSE},
3110};
3111
3112/*
3113 * Return length of a command modifier (including optional count).
3114 * Return zero when it's not a modifier.
3115 */
3116 int
3117modifier_len(cmd)
3118 char_u *cmd;
3119{
3120 int i, j;
3121 char_u *p = cmd;
3122
3123 if (VIM_ISDIGIT(*cmd))
3124 p = skipwhite(skipdigits(cmd));
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003125 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003126 {
3127 for (j = 0; p[j] != NUL; ++j)
3128 if (p[j] != cmdmods[i].name[j])
3129 break;
Bram Moolenaar2d473ab2013-06-12 17:12:24 +02003130 if (!ASCII_ISALPHA(p[j]) && j >= cmdmods[i].minlen
Bram Moolenaared53fb92007-11-24 20:50:24 +00003131 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003132 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003133 }
3134 return 0;
3135}
3136
Bram Moolenaar071d4272004-06-13 20:20:40 +00003137/*
3138 * Return > 0 if an Ex command "name" exists.
3139 * Return 2 if there is an exact match.
3140 * Return 3 if there is an ambiguous match.
3141 */
3142 int
3143cmd_exists(name)
3144 char_u *name;
3145{
3146 exarg_T ea;
3147 int full = FALSE;
3148 int i;
3149 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003150 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003151
3152 /* Check command modifiers. */
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003153 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154 {
3155 for (j = 0; name[j] != NUL; ++j)
3156 if (name[j] != cmdmods[i].name[j])
3157 break;
3158 if (name[j] == NUL && j >= cmdmods[i].minlen)
3159 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3160 }
3161
Bram Moolenaara9587612006-05-04 21:47:50 +00003162 /* Check built-in commands and user defined commands.
3163 * For ":2match" and ":3match" we need to skip the number. */
3164 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003165 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003166 p = find_command(&ea, &full);
3167 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003168 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003169 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3170 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003171 if (*skipwhite(p) != NUL)
3172 return 0; /* trailing garbage */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3174}
3175#endif
3176
3177/*
3178 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
3179 * we don't need/want deleted. Maybe this could be done better if we didn't
3180 * repeat all this stuff. The only problem is that they may not stay
3181 * perfectly compatible with each other, but then the command line syntax
3182 * probably won't change that much -- webb.
3183 */
3184 char_u *
3185set_one_cmd_context(xp, buff)
3186 expand_T *xp;
3187 char_u *buff; /* buffer for command string */
3188{
3189 char_u *p;
3190 char_u *cmd, *arg;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003191 int len = 0;
3192 exarg_T ea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3194 int compl = EXPAND_NOTHING;
3195#endif
3196#ifdef FEAT_CMDL_COMPL
3197 int delim;
3198#endif
3199 int forceit = FALSE;
3200 int usefilter = FALSE; /* filter instead of file name */
3201
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003202 ExpandInit(xp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203 xp->xp_pattern = buff;
3204 xp->xp_context = EXPAND_COMMANDS; /* Default until we get past command */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003205 ea.argt = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206
3207/*
3208 * 2. skip comment lines and leading space, colons or bars
3209 */
3210 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
3211 ;
3212 xp->xp_pattern = cmd;
3213
3214 if (*cmd == NUL)
3215 return NULL;
3216 if (*cmd == '"') /* ignore comment lines */
3217 {
3218 xp->xp_context = EXPAND_NOTHING;
3219 return NULL;
3220 }
3221
3222/*
3223 * 3. parse a range specifier of the form: addr [,addr] [;addr] ..
3224 */
3225 cmd = skip_range(cmd, &xp->xp_context);
3226
3227/*
3228 * 4. parse command
3229 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230 xp->xp_pattern = cmd;
3231 if (*cmd == NUL)
3232 return NULL;
3233 if (*cmd == '"')
3234 {
3235 xp->xp_context = EXPAND_NOTHING;
3236 return NULL;
3237 }
3238
3239 if (*cmd == '|' || *cmd == '\n')
3240 return cmd + 1; /* There's another command */
3241
3242 /*
3243 * Isolate the command and search for it in the command table.
3244 * Exceptions:
3245 * - the 'k' command can directly be followed by any character, but
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003246 * do accept "keepmarks", "keepalt" and "keepjumps".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003247 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
3248 */
3249 if (*cmd == 'k' && cmd[1] != 'e')
3250 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003251 ea.cmdidx = CMD_k;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003252 p = cmd + 1;
3253 }
3254 else
3255 {
3256 p = cmd;
3257 while (ASCII_ISALPHA(*p) || *p == '*') /* Allow * wild card */
3258 ++p;
3259 /* check for non-alpha command */
3260 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3261 ++p;
Bram Moolenaar9f5d6002013-06-02 19:22:13 +02003262 /* for python 3.x: ":py3*" commands completion */
3263 if (cmd[0] == 'p' && cmd[1] == 'y' && p == cmd + 2 && *p == '3')
Bram Moolenaar893b2d72013-12-11 17:44:38 +01003264 {
Bram Moolenaar9f5d6002013-06-02 19:22:13 +02003265 ++p;
Bram Moolenaar893b2d72013-12-11 17:44:38 +01003266 while (ASCII_ISALPHA(*p) || *p == '*')
3267 ++p;
3268 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003269 len = (int)(p - cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003271 if (len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272 {
3273 xp->xp_context = EXPAND_UNSUCCESSFUL;
3274 return NULL;
3275 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003276 for (ea.cmdidx = (cmdidx_T)0; (int)ea.cmdidx < (int)CMD_SIZE;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01003277 ea.cmdidx = (cmdidx_T)((int)ea.cmdidx + 1))
3278 if (STRNCMP(cmdnames[(int)ea.cmdidx].cmd_name, cmd,
3279 (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280 break;
3281
3282#ifdef FEAT_USR_CMDS
3283 if (cmd[0] >= 'A' && cmd[0] <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003284 while (ASCII_ISALNUM(*p) || *p == '*') /* Allow * wild card */
3285 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003286#endif
3287 }
3288
3289 /*
3290 * If the cursor is touching the command, and it ends in an alpha-numeric
3291 * character, complete the command name.
3292 */
3293 if (*p == NUL && ASCII_ISALNUM(p[-1]))
3294 return NULL;
3295
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003296 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297 {
3298 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
3299 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003300 ea.cmdidx = CMD_substitute;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003301 p = cmd + 1;
3302 }
3303#ifdef FEAT_USR_CMDS
3304 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
3305 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003306 ea.cmd = cmd;
3307 p = find_ucmd(&ea, p, NULL, xp,
3308# if defined(FEAT_CMDL_COMPL)
3309 &compl
3310# else
3311 NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312# endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003313 );
Bram Moolenaarebefac62005-12-28 22:39:57 +00003314 if (p == NULL)
3315 ea.cmdidx = CMD_SIZE; /* ambiguous user command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003316 }
3317#endif
3318 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003319 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320 {
3321 /* Not still touching the command and it was an illegal one */
3322 xp->xp_context = EXPAND_UNSUCCESSFUL;
3323 return NULL;
3324 }
3325
3326 xp->xp_context = EXPAND_NOTHING; /* Default now that we're past command */
3327
3328 if (*p == '!') /* forced commands */
3329 {
3330 forceit = TRUE;
3331 ++p;
3332 }
3333
3334/*
3335 * 5. parse arguments
3336 */
3337#ifdef FEAT_USR_CMDS
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003338 if (!USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003339#endif
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003340 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003341
3342 arg = skipwhite(p);
3343
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003344 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345 {
3346 if (*arg == '>') /* append */
3347 {
3348 if (*++arg == '>')
3349 ++arg;
3350 arg = skipwhite(arg);
3351 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003352 else if (*arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353 {
3354 ++arg;
3355 usefilter = TRUE;
3356 }
3357 }
3358
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003359 if (ea.cmdidx == CMD_read)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360 {
3361 usefilter = forceit; /* :r! filter if forced */
3362 if (*arg == '!') /* :r !filter */
3363 {
3364 ++arg;
3365 usefilter = TRUE;
3366 }
3367 }
3368
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003369 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370 {
3371 while (*arg == *cmd) /* allow any number of '>' or '<' */
3372 ++arg;
3373 arg = skipwhite(arg);
3374 }
3375
3376 /* Does command allow "+command"? */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003377 if ((ea.argt & EDITCMD) && !usefilter && *arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378 {
3379 /* Check if we're in the +command */
3380 p = arg + 1;
3381 arg = skip_cmd_arg(arg, FALSE);
3382
3383 /* Still touching the command after '+'? */
3384 if (*arg == NUL)
3385 return p;
3386
3387 /* Skip space(s) after +command to get to the real argument */
3388 arg = skipwhite(arg);
3389 }
3390
3391 /*
3392 * Check for '|' to separate commands and '"' to start comments.
3393 * Don't do this for ":read !cmd" and ":write !cmd".
3394 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003395 if ((ea.argt & TRLBAR) && !usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396 {
3397 p = arg;
3398 /* ":redir @" is not the start of a comment */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003399 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400 p += 2;
3401 while (*p)
3402 {
3403 if (*p == Ctrl_V)
3404 {
3405 if (p[1] != NUL)
3406 ++p;
3407 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003408 else if ( (*p == '"' && !(ea.argt & NOTRLCOM))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003409 || *p == '|' || *p == '\n')
3410 {
3411 if (*(p - 1) != '\\')
3412 {
3413 if (*p == '|' || *p == '\n')
3414 return p + 1;
3415 return NULL; /* It's a comment */
3416 }
3417 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003418 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419 }
3420 }
3421
3422 /* no arguments allowed */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003423 if (!(ea.argt & EXTRA) && *arg != NUL &&
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424 vim_strchr((char_u *)"|\"", *arg) == NULL)
3425 return NULL;
3426
3427 /* Find start of last argument (argument just before cursor): */
Bram Moolenaar848f8762012-07-25 17:22:23 +02003428 p = buff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429 xp->xp_pattern = p;
Bram Moolenaar09168a72012-08-02 21:24:42 +02003430 len = (int)STRLEN(buff);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003431 while (*p && p < buff + len)
3432 {
3433 if (*p == ' ' || *p == TAB)
3434 {
3435 /* argument starts after a space */
3436 xp->xp_pattern = ++p;
3437 }
3438 else
3439 {
3440 if (*p == '\\' && *(p + 1) != NUL)
3441 ++p; /* skip over escaped character */
3442 mb_ptr_adv(p);
3443 }
3444 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003446 if (ea.argt & XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003448 int c;
3449 int in_quote = FALSE;
3450 char_u *bow = NULL; /* Beginning of word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451
3452 /*
3453 * Allow spaces within back-quotes to count as part of the argument
3454 * being expanded.
3455 */
3456 xp->xp_pattern = skipwhite(arg);
Bram Moolenaar6529c102007-08-18 15:47:34 +00003457 p = xp->xp_pattern;
3458 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003460#ifdef FEAT_MBYTE
3461 if (has_mbyte)
3462 c = mb_ptr2char(p);
3463 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464#endif
Bram Moolenaar6529c102007-08-18 15:47:34 +00003465 c = *p;
3466 if (c == '\\' && p[1] != NUL)
3467 ++p;
3468 else if (c == '`')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469 {
3470 if (!in_quote)
3471 {
3472 xp->xp_pattern = p;
3473 bow = p + 1;
3474 }
3475 in_quote = !in_quote;
3476 }
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003477 /* An argument can contain just about everything, except
3478 * characters that end the command and white space. */
3479 else if (c == '|' || c == '\n' || c == '"' || (vim_iswhite(c)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003480#ifdef SPACE_IN_FILENAME
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003481 && (!(ea.argt & NOSPC) || usefilter)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003482#endif
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003483 ))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003484 {
Bram Moolenaarcf5a5b82008-02-26 20:30:12 +00003485 len = 0; /* avoid getting stuck when space is in 'isfname' */
Bram Moolenaar6529c102007-08-18 15:47:34 +00003486 while (*p != NUL)
3487 {
3488#ifdef FEAT_MBYTE
3489 if (has_mbyte)
3490 c = mb_ptr2char(p);
3491 else
3492#endif
3493 c = *p;
Bram Moolenaardd87969c2007-08-21 13:07:12 +00003494 if (c == '`' || vim_isfilec_or_wc(c))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003495 break;
3496#ifdef FEAT_MBYTE
3497 if (has_mbyte)
3498 len = (*mb_ptr2len)(p);
3499 else
3500#endif
3501 len = 1;
3502 mb_ptr_adv(p);
3503 }
3504 if (in_quote)
3505 bow = p;
3506 else
3507 xp->xp_pattern = p;
3508 p -= len;
3509 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003510 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511 }
3512
3513 /*
3514 * If we are still inside the quotes, and we passed a space, just
3515 * expand from there.
3516 */
3517 if (bow != NULL && in_quote)
3518 xp->xp_pattern = bow;
3519 xp->xp_context = EXPAND_FILES;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003520
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003521 /* For a shell command more chars need to be escaped. */
3522 if (usefilter || ea.cmdidx == CMD_bang)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003523 {
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003524#ifndef BACKSLASH_IN_FILENAME
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003525 xp->xp_shell = TRUE;
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003526#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003527 /* When still after the command name expand executables. */
3528 if (xp->xp_pattern == skipwhite(arg))
Bram Moolenaar5c5b0942007-05-06 12:07:59 +00003529 xp->xp_context = EXPAND_SHELLCMD;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003530 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531
3532 /* Check for environment variable */
3533 if (*xp->xp_pattern == '$'
3534#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
3535 || *xp->xp_pattern == '%'
3536#endif
3537 )
3538 {
3539 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
3540 if (!vim_isIDc(*p))
3541 break;
3542 if (*p == NUL)
3543 {
3544 xp->xp_context = EXPAND_ENV_VARS;
3545 ++xp->xp_pattern;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003546#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3547 /* Avoid that the assignment uses EXPAND_FILES again. */
Bram Moolenaara466c992005-07-09 21:03:22 +00003548 if (compl != EXPAND_USER_DEFINED && compl != EXPAND_USER_LIST)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003549 compl = EXPAND_ENV_VARS;
3550#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551 }
3552 }
Bram Moolenaar24305862012-08-15 14:05:05 +02003553#if defined(FEAT_CMDL_COMPL)
3554 /* Check for user names */
3555 if (*xp->xp_pattern == '~')
3556 {
3557 for (p = xp->xp_pattern + 1; *p != NUL && *p != '/'; ++p)
3558 ;
3559 /* Complete ~user only if it partially matches a user name.
3560 * A full match ~user<Tab> will be replaced by user's home
3561 * directory i.e. something like ~user<Tab> -> /home/user/ */
3562 if (*p == NUL && p > xp->xp_pattern + 1
3563 && match_user(xp->xp_pattern + 1) == 1)
3564 {
3565 xp->xp_context = EXPAND_USER;
3566 ++xp->xp_pattern;
3567 }
3568 }
3569#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570 }
3571
3572/*
3573 * 6. switch on command name
3574 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003575 switch (ea.cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003576 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003577 case CMD_find:
3578 case CMD_sfind:
3579 case CMD_tabfind:
Bram Moolenaarc24b6972010-08-16 22:34:29 +02003580 if (xp->xp_context == EXPAND_FILES)
3581 xp->xp_context = EXPAND_FILES_IN_PATH;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003582 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583 case CMD_cd:
3584 case CMD_chdir:
3585 case CMD_lcd:
3586 case CMD_lchdir:
3587 if (xp->xp_context == EXPAND_FILES)
3588 xp->xp_context = EXPAND_DIRECTORIES;
3589 break;
3590 case CMD_help:
3591 xp->xp_context = EXPAND_HELP;
3592 xp->xp_pattern = arg;
3593 break;
3594
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003595 /* Command modifiers: return the argument.
3596 * Also for commands with an argument that is a command. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597 case CMD_aboveleft:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003598 case CMD_argdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599 case CMD_belowright:
3600 case CMD_botright:
3601 case CMD_browse:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003602 case CMD_bufdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603 case CMD_confirm:
Bram Moolenaardf177f62005-02-22 08:39:57 +00003604 case CMD_debug:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605 case CMD_folddoclosed:
3606 case CMD_folddoopen:
3607 case CMD_hide:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003608 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003609 case CMD_keepjumps:
3610 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01003611 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612 case CMD_leftabove:
3613 case CMD_lockmarks:
3614 case CMD_rightbelow:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003615 case CMD_sandbox:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616 case CMD_silent:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003617 case CMD_tab:
Bram Moolenaar70baa402013-06-16 16:14:03 +02003618 case CMD_tabdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619 case CMD_topleft:
3620 case CMD_verbose:
3621 case CMD_vertical:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003622 case CMD_windo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623 return arg;
3624
Bram Moolenaar4f688582007-07-24 12:34:30 +00003625#ifdef FEAT_CMDL_COMPL
3626# ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar071d4272004-06-13 20:20:40 +00003627 case CMD_match:
3628 if (*arg == NUL || !ends_excmd(*arg))
3629 {
Bram Moolenaar4f688582007-07-24 12:34:30 +00003630 /* also complete "None" */
3631 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003632 arg = skipwhite(skiptowhite(arg));
3633 if (*arg != NUL)
3634 {
3635 xp->xp_context = EXPAND_NOTHING;
3636 arg = skip_regexp(arg + 1, *arg, p_magic, NULL);
3637 }
3638 }
3639 return find_nextcmd(arg);
Bram Moolenaar4f688582007-07-24 12:34:30 +00003640# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642/*
3643 * All completion for the +cmdline_compl feature goes here.
3644 */
3645
3646# ifdef FEAT_USR_CMDS
3647 case CMD_command:
3648 /* Check for attributes */
3649 while (*arg == '-')
3650 {
3651 arg++; /* Skip "-" */
3652 p = skiptowhite(arg);
3653 if (*p == NUL)
3654 {
3655 /* Cursor is still in the attribute */
3656 p = vim_strchr(arg, '=');
3657 if (p == NULL)
3658 {
3659 /* No "=", so complete attribute names */
3660 xp->xp_context = EXPAND_USER_CMD_FLAGS;
3661 xp->xp_pattern = arg;
3662 return NULL;
3663 }
3664
3665 /* For the -complete and -nargs attributes, we complete
3666 * their arguments as well.
3667 */
3668 if (STRNICMP(arg, "complete", p - arg) == 0)
3669 {
3670 xp->xp_context = EXPAND_USER_COMPLETE;
3671 xp->xp_pattern = p + 1;
3672 return NULL;
3673 }
3674 else if (STRNICMP(arg, "nargs", p - arg) == 0)
3675 {
3676 xp->xp_context = EXPAND_USER_NARGS;
3677 xp->xp_pattern = p + 1;
3678 return NULL;
3679 }
3680 return NULL;
3681 }
3682 arg = skipwhite(p);
3683 }
3684
3685 /* After the attributes comes the new command name */
3686 p = skiptowhite(arg);
3687 if (*p == NUL)
3688 {
3689 xp->xp_context = EXPAND_USER_COMMANDS;
3690 xp->xp_pattern = arg;
3691 break;
3692 }
3693
3694 /* And finally comes a normal command */
3695 return skipwhite(p);
3696
3697 case CMD_delcommand:
3698 xp->xp_context = EXPAND_USER_COMMANDS;
3699 xp->xp_pattern = arg;
3700 break;
3701# endif
3702
3703 case CMD_global:
3704 case CMD_vglobal:
3705 delim = *arg; /* get the delimiter */
3706 if (delim)
3707 ++arg; /* skip delimiter if there is one */
3708
3709 while (arg[0] != NUL && arg[0] != delim)
3710 {
3711 if (arg[0] == '\\' && arg[1] != NUL)
3712 ++arg;
3713 ++arg;
3714 }
3715 if (arg[0] != NUL)
3716 return arg + 1;
3717 break;
3718 case CMD_and:
3719 case CMD_substitute:
3720 delim = *arg;
3721 if (delim)
3722 {
3723 /* skip "from" part */
3724 ++arg;
3725 arg = skip_regexp(arg, delim, p_magic, NULL);
3726 }
3727 /* skip "to" part */
3728 while (arg[0] != NUL && arg[0] != delim)
3729 {
3730 if (arg[0] == '\\' && arg[1] != NUL)
3731 ++arg;
3732 ++arg;
3733 }
3734 if (arg[0] != NUL) /* skip delimiter */
3735 ++arg;
3736 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
3737 ++arg;
3738 if (arg[0] != NUL)
3739 return arg;
3740 break;
3741 case CMD_isearch:
3742 case CMD_dsearch:
3743 case CMD_ilist:
3744 case CMD_dlist:
3745 case CMD_ijump:
3746 case CMD_psearch:
3747 case CMD_djump:
3748 case CMD_isplit:
3749 case CMD_dsplit:
3750 arg = skipwhite(skipdigits(arg)); /* skip count */
3751 if (*arg == '/') /* Match regexp, not just whole words */
3752 {
3753 for (++arg; *arg && *arg != '/'; arg++)
3754 if (*arg == '\\' && arg[1] != NUL)
3755 arg++;
3756 if (*arg)
3757 {
3758 arg = skipwhite(arg + 1);
3759
3760 /* Check for trailing illegal characters */
3761 if (*arg && vim_strchr((char_u *)"|\"\n", *arg) == NULL)
3762 xp->xp_context = EXPAND_NOTHING;
3763 else
3764 return arg;
3765 }
3766 }
3767 break;
3768#ifdef FEAT_AUTOCMD
3769 case CMD_autocmd:
3770 return set_context_in_autocmd(xp, arg, FALSE);
3771
3772 case CMD_doautocmd:
Bram Moolenaar73a9d7b2008-11-06 16:16:44 +00003773 case CMD_doautoall:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003774 return set_context_in_autocmd(xp, arg, TRUE);
3775#endif
3776 case CMD_set:
3777 set_context_in_set_cmd(xp, arg, 0);
3778 break;
3779 case CMD_setglobal:
3780 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
3781 break;
3782 case CMD_setlocal:
3783 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
3784 break;
3785 case CMD_tag:
3786 case CMD_stag:
3787 case CMD_ptag:
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00003788 case CMD_ltag:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003789 case CMD_tselect:
3790 case CMD_stselect:
3791 case CMD_ptselect:
3792 case CMD_tjump:
3793 case CMD_stjump:
3794 case CMD_ptjump:
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003795 if (*p_wop != NUL)
3796 xp->xp_context = EXPAND_TAGS_LISTFILES;
3797 else
3798 xp->xp_context = EXPAND_TAGS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003799 xp->xp_pattern = arg;
3800 break;
3801 case CMD_augroup:
3802 xp->xp_context = EXPAND_AUGROUP;
3803 xp->xp_pattern = arg;
3804 break;
3805#ifdef FEAT_SYN_HL
3806 case CMD_syntax:
3807 set_context_in_syntax_cmd(xp, arg);
3808 break;
3809#endif
3810#ifdef FEAT_EVAL
3811 case CMD_let:
3812 case CMD_if:
3813 case CMD_elseif:
3814 case CMD_while:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00003815 case CMD_for:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003816 case CMD_echo:
3817 case CMD_echon:
3818 case CMD_execute:
3819 case CMD_echomsg:
3820 case CMD_echoerr:
3821 case CMD_call:
3822 case CMD_return:
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003823 set_context_for_expression(xp, arg, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003824 break;
3825
3826 case CMD_unlet:
3827 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3828 arg = xp->xp_pattern + 1;
3829 xp->xp_context = EXPAND_USER_VARS;
3830 xp->xp_pattern = arg;
3831 break;
3832
3833 case CMD_function:
3834 case CMD_delfunction:
3835 xp->xp_context = EXPAND_USER_FUNC;
3836 xp->xp_pattern = arg;
3837 break;
3838
3839 case CMD_echohl:
Bram Moolenaar4f688582007-07-24 12:34:30 +00003840 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841 break;
3842#endif
3843 case CMD_highlight:
3844 set_context_in_highlight_cmd(xp, arg);
3845 break;
Bram Moolenaarf4580d82009-03-18 11:52:53 +00003846#ifdef FEAT_CSCOPE
3847 case CMD_cscope:
Bram Moolenaar7bfef802009-04-22 14:25:01 +00003848 case CMD_lcscope:
3849 case CMD_scscope:
3850 set_context_in_cscope_cmd(xp, arg, ea.cmdidx);
Bram Moolenaarf4580d82009-03-18 11:52:53 +00003851 break;
3852#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00003853#ifdef FEAT_SIGNS
3854 case CMD_sign:
3855 set_context_in_sign_cmd(xp, arg);
3856 break;
3857#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003858#ifdef FEAT_LISTCMDS
3859 case CMD_bdelete:
3860 case CMD_bwipeout:
3861 case CMD_bunload:
3862 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3863 arg = xp->xp_pattern + 1;
3864 /*FALLTHROUGH*/
3865 case CMD_buffer:
3866 case CMD_sbuffer:
3867 case CMD_checktime:
3868 xp->xp_context = EXPAND_BUFFERS;
3869 xp->xp_pattern = arg;
3870 break;
3871#endif
3872#ifdef FEAT_USR_CMDS
3873 case CMD_USER:
3874 case CMD_USER_BUF:
3875 if (compl != EXPAND_NOTHING)
3876 {
3877 /* XFILE: file names are handled above */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003878 if (!(ea.argt & XFILE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003879 {
3880# ifdef FEAT_MENU
3881 if (compl == EXPAND_MENUS)
3882 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
3883# endif
3884 if (compl == EXPAND_COMMANDS)
3885 return arg;
3886 if (compl == EXPAND_MAPPINGS)
3887 return set_context_in_map_cmd(xp, (char_u *)"map",
3888 arg, forceit, FALSE, FALSE, CMD_map);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003889 /* Find start of last argument. */
3890 p = arg;
3891 while (*p)
3892 {
3893 if (*p == ' ')
Bram Moolenaar848f8762012-07-25 17:22:23 +02003894 /* argument starts after a space */
3895 arg = p + 1;
Bram Moolenaara07c8312012-07-27 21:12:07 +02003896 else if (*p == '\\' && *(p + 1) != NUL)
3897 ++p; /* skip over escaped character */
3898 mb_ptr_adv(p);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003899 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900 xp->xp_pattern = arg;
3901 }
3902 xp->xp_context = compl;
3903 }
3904 break;
3905#endif
3906 case CMD_map: case CMD_noremap:
3907 case CMD_nmap: case CMD_nnoremap:
3908 case CMD_vmap: case CMD_vnoremap:
3909 case CMD_omap: case CMD_onoremap:
3910 case CMD_imap: case CMD_inoremap:
3911 case CMD_cmap: case CMD_cnoremap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003912 case CMD_lmap: case CMD_lnoremap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02003913 case CMD_smap: case CMD_snoremap:
3914 case CMD_xmap: case CMD_xnoremap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003915 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003916 FALSE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003917 case CMD_unmap:
3918 case CMD_nunmap:
3919 case CMD_vunmap:
3920 case CMD_ounmap:
3921 case CMD_iunmap:
3922 case CMD_cunmap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003923 case CMD_lunmap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02003924 case CMD_sunmap:
3925 case CMD_xunmap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003927 FALSE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003928 case CMD_abbreviate: case CMD_noreabbrev:
3929 case CMD_cabbrev: case CMD_cnoreabbrev:
3930 case CMD_iabbrev: case CMD_inoreabbrev:
3931 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003932 TRUE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933 case CMD_unabbreviate:
3934 case CMD_cunabbrev:
3935 case CMD_iunabbrev:
3936 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003937 TRUE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938#ifdef FEAT_MENU
3939 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
3940 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
3941 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
3942 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
3943 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
3944 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
3945 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
3946 case CMD_tmenu: case CMD_tunmenu:
3947 case CMD_popup: case CMD_tearoff: case CMD_emenu:
3948 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
3949#endif
3950
3951 case CMD_colorscheme:
3952 xp->xp_context = EXPAND_COLORS;
3953 xp->xp_pattern = arg;
3954 break;
3955
3956 case CMD_compiler:
3957 xp->xp_context = EXPAND_COMPILER;
3958 xp->xp_pattern = arg;
3959 break;
3960
Bram Moolenaar883f5d02010-06-21 06:24:34 +02003961 case CMD_ownsyntax:
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02003962 xp->xp_context = EXPAND_OWNSYNTAX;
3963 xp->xp_pattern = arg;
3964 break;
3965
3966 case CMD_setfiletype:
Bram Moolenaar883f5d02010-06-21 06:24:34 +02003967 xp->xp_context = EXPAND_FILETYPE;
3968 xp->xp_pattern = arg;
3969 break;
3970
Bram Moolenaar071d4272004-06-13 20:20:40 +00003971#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
3972 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
3973 case CMD_language:
Bram Moolenaara660dc82011-05-25 12:51:22 +02003974 p = skiptowhite(arg);
3975 if (*p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003976 {
3977 xp->xp_context = EXPAND_LANGUAGE;
3978 xp->xp_pattern = arg;
3979 }
3980 else
Bram Moolenaara660dc82011-05-25 12:51:22 +02003981 {
3982 if ( STRNCMP(arg, "messages", p - arg) == 0
3983 || STRNCMP(arg, "ctype", p - arg) == 0
3984 || STRNCMP(arg, "time", p - arg) == 0)
3985 {
3986 xp->xp_context = EXPAND_LOCALES;
3987 xp->xp_pattern = skipwhite(p);
3988 }
3989 else
3990 xp->xp_context = EXPAND_NOTHING;
3991 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992 break;
3993#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01003994#if defined(FEAT_PROFILE)
3995 case CMD_profile:
3996 set_context_in_profile_cmd(xp, arg);
3997 break;
3998#endif
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01003999 case CMD_behave:
4000 xp->xp_context = EXPAND_BEHAVE;
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004001 xp->xp_pattern = arg;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01004002 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004003
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004004#if defined(FEAT_CMDHIST)
4005 case CMD_history:
4006 xp->xp_context = EXPAND_HISTORY;
4007 xp->xp_pattern = arg;
4008 break;
4009#endif
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02004010#if defined(FEAT_PROFILE)
4011 case CMD_syntime:
4012 xp->xp_context = EXPAND_SYNTIME;
4013 xp->xp_pattern = arg;
4014 break;
4015#endif
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004016
Bram Moolenaar071d4272004-06-13 20:20:40 +00004017#endif /* FEAT_CMDL_COMPL */
4018
4019 default:
4020 break;
4021 }
4022 return NULL;
4023}
4024
4025/*
4026 * skip a range specifier of the form: addr [,addr] [;addr] ..
4027 *
4028 * Backslashed delimiters after / or ? will be skipped, and commands will
4029 * not be expanded between /'s and ?'s or after "'".
4030 *
Bram Moolenaardf177f62005-02-22 08:39:57 +00004031 * Also skip white space and ":" characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004032 * Returns the "cmd" pointer advanced to beyond the range.
4033 */
4034 char_u *
4035skip_range(cmd, ctx)
4036 char_u *cmd;
4037 int *ctx; /* pointer to xp_context or NULL */
4038{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004039 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004040
Bram Moolenaardf177f62005-02-22 08:39:57 +00004041 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042 {
4043 if (*cmd == '\'')
4044 {
4045 if (*++cmd == NUL && ctx != NULL)
4046 *ctx = EXPAND_NOTHING;
4047 }
4048 else if (*cmd == '/' || *cmd == '?')
4049 {
4050 delim = *cmd++;
4051 while (*cmd != NUL && *cmd != delim)
4052 if (*cmd++ == '\\' && *cmd != NUL)
4053 ++cmd;
4054 if (*cmd == NUL && ctx != NULL)
4055 *ctx = EXPAND_NOTHING;
4056 }
4057 if (*cmd != NUL)
4058 ++cmd;
4059 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004060
4061 /* Skip ":" and white space. */
4062 while (*cmd == ':')
4063 cmd = skipwhite(cmd + 1);
4064
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065 return cmd;
4066}
4067
4068/*
4069 * get a single EX address
4070 *
4071 * Set ptr to the next character after the part that was interpreted.
4072 * Set ptr to NULL when an error is encountered.
4073 *
4074 * Return MAXLNUM when no Ex address was found.
4075 */
4076 static linenr_T
4077get_address(ptr, skip, to_other_file)
4078 char_u **ptr;
4079 int skip; /* only skip the address, don't use it */
4080 int to_other_file; /* flag: may jump to other file */
4081{
4082 int c;
4083 int i;
4084 long n;
4085 char_u *cmd;
4086 pos_T pos;
4087 pos_T *fp;
4088 linenr_T lnum;
4089
4090 cmd = skipwhite(*ptr);
4091 lnum = MAXLNUM;
4092 do
4093 {
4094 switch (*cmd)
4095 {
4096 case '.': /* '.' - Cursor position */
4097 ++cmd;
4098 lnum = curwin->w_cursor.lnum;
4099 break;
4100
4101 case '$': /* '$' - last line */
4102 ++cmd;
4103 lnum = curbuf->b_ml.ml_line_count;
4104 break;
4105
4106 case '\'': /* ''' - mark */
4107 if (*++cmd == NUL)
4108 {
4109 cmd = NULL;
4110 goto error;
4111 }
4112 if (skip)
4113 ++cmd;
4114 else
4115 {
4116 /* Only accept a mark in another file when it is
4117 * used by itself: ":'M". */
4118 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
4119 ++cmd;
4120 if (fp == (pos_T *)-1)
4121 /* Jumped to another file. */
4122 lnum = curwin->w_cursor.lnum;
4123 else
4124 {
4125 if (check_mark(fp) == FAIL)
4126 {
4127 cmd = NULL;
4128 goto error;
4129 }
4130 lnum = fp->lnum;
4131 }
4132 }
4133 break;
4134
4135 case '/':
4136 case '?': /* '/' or '?' - search */
4137 c = *cmd++;
4138 if (skip) /* skip "/pat/" */
4139 {
4140 cmd = skip_regexp(cmd, c, (int)p_magic, NULL);
4141 if (*cmd == c)
4142 ++cmd;
4143 }
4144 else
4145 {
4146 pos = curwin->w_cursor; /* save curwin->w_cursor */
4147 /*
4148 * When '/' or '?' follows another address, start
4149 * from there.
4150 */
4151 if (lnum != MAXLNUM)
4152 curwin->w_cursor.lnum = lnum;
4153 /*
4154 * Start a forward search at the end of the line.
4155 * Start a backward search at the start of the line.
4156 * This makes sure we never match in the current
4157 * line, and can match anywhere in the
4158 * next/previous line.
4159 */
4160 if (c == '/')
4161 curwin->w_cursor.col = MAXCOL;
4162 else
4163 curwin->w_cursor.col = 0;
4164 searchcmdlen = 0;
4165 if (!do_search(NULL, c, cmd, 1L,
Bram Moolenaaraad86642008-03-10 20:34:59 +00004166 SEARCH_HIS | SEARCH_MSG, NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167 {
4168 curwin->w_cursor = pos;
4169 cmd = NULL;
4170 goto error;
4171 }
4172 lnum = curwin->w_cursor.lnum;
4173 curwin->w_cursor = pos;
4174 /* adjust command string pointer */
4175 cmd += searchcmdlen;
4176 }
4177 break;
4178
4179 case '\\': /* "\?", "\/" or "\&", repeat search */
4180 ++cmd;
4181 if (*cmd == '&')
4182 i = RE_SUBST;
4183 else if (*cmd == '?' || *cmd == '/')
4184 i = RE_SEARCH;
4185 else
4186 {
4187 EMSG(_(e_backslash));
4188 cmd = NULL;
4189 goto error;
4190 }
4191
4192 if (!skip)
4193 {
4194 /*
4195 * When search follows another address, start from
4196 * there.
4197 */
4198 if (lnum != MAXLNUM)
4199 pos.lnum = lnum;
4200 else
4201 pos.lnum = curwin->w_cursor.lnum;
4202
4203 /*
4204 * Start the search just like for the above
4205 * do_search().
4206 */
4207 if (*cmd != '?')
4208 pos.col = MAXCOL;
4209 else
4210 pos.col = 0;
4211 if (searchit(curwin, curbuf, &pos,
4212 *cmd == '?' ? BACKWARD : FORWARD,
Bram Moolenaaraad86642008-03-10 20:34:59 +00004213 (char_u *)"", 1L, SEARCH_MSG,
Bram Moolenaar76929292008-01-06 19:07:36 +00004214 i, (linenr_T)0, NULL) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004215 lnum = pos.lnum;
4216 else
4217 {
4218 cmd = NULL;
4219 goto error;
4220 }
4221 }
4222 ++cmd;
4223 break;
4224
4225 default:
4226 if (VIM_ISDIGIT(*cmd)) /* absolute line number */
4227 lnum = getdigits(&cmd);
4228 }
4229
4230 for (;;)
4231 {
4232 cmd = skipwhite(cmd);
4233 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4234 break;
4235
4236 if (lnum == MAXLNUM)
4237 lnum = curwin->w_cursor.lnum; /* "+1" is same as ".+1" */
4238 if (VIM_ISDIGIT(*cmd))
4239 i = '+'; /* "number" is same as "+number" */
4240 else
4241 i = *cmd++;
4242 if (!VIM_ISDIGIT(*cmd)) /* '+' is '+1', but '+0' is not '+1' */
4243 n = 1;
4244 else
4245 n = getdigits(&cmd);
4246 if (i == '-')
4247 lnum -= n;
4248 else
4249 lnum += n;
4250 }
4251 } while (*cmd == '/' || *cmd == '?');
4252
4253error:
4254 *ptr = cmd;
4255 return lnum;
4256}
4257
4258/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004259 * Get flags from an Ex command argument.
4260 */
4261 static void
4262get_flags(eap)
4263 exarg_T *eap;
4264{
4265 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4266 {
4267 if (*eap->arg == 'l')
4268 eap->flags |= EXFLAG_LIST;
4269 else if (*eap->arg == 'p')
4270 eap->flags |= EXFLAG_PRINT;
4271 else
4272 eap->flags |= EXFLAG_NR;
4273 eap->arg = skipwhite(eap->arg + 1);
4274 }
4275}
4276
4277/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004278 * Function called for command which is Not Implemented. NI!
4279 */
4280 void
4281ex_ni(eap)
4282 exarg_T *eap;
4283{
4284 if (!eap->skip)
4285 eap->errmsg = (char_u *)N_("E319: Sorry, the command is not available in this version");
4286}
4287
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004288#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004289/*
4290 * Function called for script command which is Not Implemented. NI!
4291 * Skips over ":perl <<EOF" constructs.
4292 */
4293 static void
4294ex_script_ni(eap)
4295 exarg_T *eap;
4296{
4297 if (!eap->skip)
4298 ex_ni(eap);
4299 else
4300 vim_free(script_get(eap, eap->arg));
4301}
4302#endif
4303
4304/*
4305 * Check range in Ex command for validity.
4306 * Return NULL when valid, error message when invalid.
4307 */
4308 static char_u *
4309invalid_range(eap)
4310 exarg_T *eap;
4311{
4312 if ( eap->line1 < 0
4313 || eap->line2 < 0
4314 || eap->line1 > eap->line2
4315 || ((eap->argt & RANGE)
4316 && !(eap->argt & NOTADR)
4317 && eap->line2 > curbuf->b_ml.ml_line_count
4318#ifdef FEAT_DIFF
4319 + (eap->cmdidx == CMD_diffget)
4320#endif
4321 ))
4322 return (char_u *)_(e_invrange);
4323 return NULL;
4324}
4325
4326/*
4327 * Correct the range for zero line number, if required.
4328 */
4329 static void
4330correct_range(eap)
4331 exarg_T *eap;
4332{
4333 if (!(eap->argt & ZEROR)) /* zero in range not allowed */
4334 {
4335 if (eap->line1 == 0)
4336 eap->line1 = 1;
4337 if (eap->line2 == 0)
4338 eap->line2 = 1;
4339 }
4340}
4341
Bram Moolenaar748bf032005-02-02 23:04:36 +00004342#ifdef FEAT_QUICKFIX
4343static char_u *skip_grep_pat __ARGS((exarg_T *eap));
4344
4345/*
4346 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4347 * pattern. Otherwise return eap->arg.
4348 */
4349 static char_u *
4350skip_grep_pat(eap)
4351 exarg_T *eap;
4352{
4353 char_u *p = eap->arg;
4354
Bram Moolenaara37420f2006-02-04 22:37:47 +00004355 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4356 || eap->cmdidx == CMD_vimgrepadd
4357 || eap->cmdidx == CMD_lvimgrepadd
4358 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004359 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004360 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004361 if (p == NULL)
4362 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004363 }
4364 return p;
4365}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004366
4367/*
4368 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4369 * in the command line, so that things like % get expanded.
4370 */
4371 static char_u *
4372replace_makeprg(eap, p, cmdlinep)
4373 exarg_T *eap;
4374 char_u *p;
4375 char_u **cmdlinep;
4376{
4377 char_u *new_cmdline;
4378 char_u *program;
4379 char_u *pos;
4380 char_u *ptr;
4381 int len;
4382 int i;
4383
4384 /*
4385 * Don't do it when ":vimgrep" is used for ":grep".
4386 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004387 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4388 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4389 || eap->cmdidx == CMD_grepadd
4390 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004391 && !grep_internal(eap->cmdidx))
4392 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004393 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4394 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004395 {
4396 if (*curbuf->b_p_gp == NUL)
4397 program = p_gp;
4398 else
4399 program = curbuf->b_p_gp;
4400 }
4401 else
4402 {
4403 if (*curbuf->b_p_mp == NUL)
4404 program = p_mp;
4405 else
4406 program = curbuf->b_p_mp;
4407 }
4408
4409 p = skipwhite(p);
4410
4411 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4412 {
4413 /* replace $* by given arguments */
4414 i = 1;
4415 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4416 ++i;
4417 len = (int)STRLEN(p);
4418 new_cmdline = alloc((int)(STRLEN(program) + i * (len - 2) + 1));
4419 if (new_cmdline == NULL)
4420 return NULL; /* out of memory */
4421 ptr = new_cmdline;
4422 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4423 {
4424 i = (int)(pos - program);
4425 STRNCPY(ptr, program, i);
4426 STRCPY(ptr += i, p);
4427 ptr += len;
4428 program = pos + 2;
4429 }
4430 STRCPY(ptr, program);
4431 }
4432 else
4433 {
4434 new_cmdline = alloc((int)(STRLEN(program) + STRLEN(p) + 2));
4435 if (new_cmdline == NULL)
4436 return NULL; /* out of memory */
4437 STRCPY(new_cmdline, program);
4438 STRCAT(new_cmdline, " ");
4439 STRCAT(new_cmdline, p);
4440 }
4441 msg_make(p);
4442
4443 /* 'eap->cmd' is not set here, because it is not used at CMD_make */
4444 vim_free(*cmdlinep);
4445 *cmdlinep = new_cmdline;
4446 p = new_cmdline;
4447 }
4448 return p;
4449}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004450#endif
4451
Bram Moolenaar071d4272004-06-13 20:20:40 +00004452/*
4453 * Expand file name in Ex command argument.
4454 * Return FAIL for failure, OK otherwise.
4455 */
4456 int
4457expand_filename(eap, cmdlinep, errormsgp)
4458 exarg_T *eap;
4459 char_u **cmdlinep;
4460 char_u **errormsgp;
4461{
4462 int has_wildcards; /* need to expand wildcards */
4463 char_u *repl;
4464 int srclen;
4465 char_u *p;
4466 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004467 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004468
Bram Moolenaar748bf032005-02-02 23:04:36 +00004469#ifdef FEAT_QUICKFIX
4470 /* Skip a regexp pattern for ":vimgrep[add] pat file..." */
4471 p = skip_grep_pat(eap);
4472#else
4473 p = eap->arg;
4474#endif
4475
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476 /*
4477 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4478 * the file name contains a wildcard it should not cause expanding.
4479 * (it will be expanded anyway if there is a wildcard before replacing).
4480 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004481 has_wildcards = mch_has_wildcard(p);
4482 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004483 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004484#ifdef FEAT_EVAL
4485 /* Skip over `=expr`, wildcards in it are not expanded. */
4486 if (p[0] == '`' && p[1] == '=')
4487 {
4488 p += 2;
4489 (void)skip_expr(&p);
4490 if (*p == '`')
4491 ++p;
4492 continue;
4493 }
4494#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004495 /*
4496 * Quick check if this cannot be the start of a special string.
4497 * Also removes backslash before '%', '#' and '<'.
4498 */
4499 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4500 {
4501 ++p;
4502 continue;
4503 }
4504
4505 /*
4506 * Try to find a match at this position.
4507 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004508 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
4509 errormsgp, &escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004510 if (*errormsgp != NULL) /* error detected */
4511 return FAIL;
4512 if (repl == NULL) /* no match found */
4513 {
4514 p += srclen;
4515 continue;
4516 }
4517
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004518 /* Wildcards won't be expanded below, the replacement is taken
4519 * literally. But do expand "~/file", "~user/file" and "$HOME/file". */
4520 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4521 {
4522 char_u *l = repl;
4523
4524 repl = expand_env_save(repl);
4525 vim_free(l);
4526 }
4527
Bram Moolenaar63b92542007-03-27 14:57:09 +00004528 /* Need to escape white space et al. with a backslash.
4529 * Don't do this for:
4530 * - replacement that already has been escaped: "##"
4531 * - shell commands (may have to use quotes instead).
4532 * - non-unix systems when there is a single argument (spaces don't
4533 * separate arguments then).
4534 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004535 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00004536 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00004537 && eap->cmdidx != CMD_bang
4538 && eap->cmdidx != CMD_make
Bram Moolenaara37420f2006-02-04 22:37:47 +00004539 && eap->cmdidx != CMD_lmake
Bram Moolenaar071d4272004-06-13 20:20:40 +00004540 && eap->cmdidx != CMD_grep
Bram Moolenaara37420f2006-02-04 22:37:47 +00004541 && eap->cmdidx != CMD_lgrep
Bram Moolenaar071d4272004-06-13 20:20:40 +00004542 && eap->cmdidx != CMD_grepadd
Bram Moolenaara37420f2006-02-04 22:37:47 +00004543 && eap->cmdidx != CMD_lgrepadd
Bram Moolenaar071d4272004-06-13 20:20:40 +00004544#ifndef UNIX
4545 && !(eap->argt & NOSPC)
4546#endif
4547 )
4548 {
4549 char_u *l;
4550#ifdef BACKSLASH_IN_FILENAME
4551 /* Don't escape a backslash here, because rem_backslash() doesn't
4552 * remove it later. */
4553 static char_u *nobslash = (char_u *)" \t\"|";
4554# define ESCAPE_CHARS nobslash
4555#else
4556# define ESCAPE_CHARS escape_chars
4557#endif
4558
4559 for (l = repl; *l; ++l)
4560 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
4561 {
4562 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
4563 if (l != NULL)
4564 {
4565 vim_free(repl);
4566 repl = l;
4567 }
4568 break;
4569 }
4570 }
4571
4572 /* For a shell command a '!' must be escaped. */
4573 if ((eap->usefilter || eap->cmdidx == CMD_bang)
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004574 && vim_strpbrk(repl, (char_u *)"!&;()<>") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004575 {
4576 char_u *l;
4577
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004578 l = vim_strsave_escaped(repl, (char_u *)"!&;()<>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004579 if (l != NULL)
4580 {
4581 vim_free(repl);
4582 repl = l;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004583 /* For a sh-like shell escape "!" another time. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004584 if (strstr((char *)p_sh, "sh") != NULL)
4585 {
4586 l = vim_strsave_escaped(repl, (char_u *)"!");
4587 if (l != NULL)
4588 {
4589 vim_free(repl);
4590 repl = l;
4591 }
4592 }
4593 }
4594 }
4595
4596 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
4597 vim_free(repl);
4598 if (p == NULL)
4599 return FAIL;
4600 }
4601
4602 /*
4603 * One file argument: Expand wildcards.
4604 * Don't do this with ":r !command" or ":w !command".
4605 */
4606 if ((eap->argt & NOSPC) && !eap->usefilter)
4607 {
4608 /*
4609 * May do this twice:
4610 * 1. Replace environment variables.
4611 * 2. Replace any other wildcards, remove backslashes.
4612 */
4613 for (n = 1; n <= 2; ++n)
4614 {
4615 if (n == 2)
4616 {
4617#ifdef UNIX
4618 /*
4619 * Only for Unix we check for more than one file name.
4620 * For other systems spaces are considered to be part
4621 * of the file name.
4622 * Only check here if there is no wildcard, otherwise
4623 * ExpandOne() will check for errors. This allows
4624 * ":e `ls ve*.c`" on Unix.
4625 */
4626 if (!has_wildcards)
4627 for (p = eap->arg; *p; ++p)
4628 {
4629 /* skip escaped characters */
4630 if (p[1] && (*p == '\\' || *p == Ctrl_V))
4631 ++p;
4632 else if (vim_iswhite(*p))
4633 {
4634 *errormsgp = (char_u *)_("E172: Only one file name allowed");
4635 return FAIL;
4636 }
4637 }
4638#endif
4639
4640 /*
4641 * Halve the number of backslashes (this is Vi compatible).
4642 * For Unix and OS/2, when wildcards are expanded, this is
4643 * done by ExpandOne() below.
4644 */
4645#if defined(UNIX) || defined(OS2)
4646 if (!has_wildcards)
4647#endif
4648 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004649 }
4650
4651 if (has_wildcards)
4652 {
4653 if (n == 1)
4654 {
4655 /*
4656 * First loop: May expand environment variables. This
4657 * can be done much faster with expand_env() than with
4658 * something else (e.g., calling a shell).
4659 * After expanding environment variables, check again
4660 * if there are still wildcards present.
4661 */
4662 if (vim_strchr(eap->arg, '$') != NULL
4663 || vim_strchr(eap->arg, '~') != NULL)
4664 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00004665 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004666 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004667 has_wildcards = mch_has_wildcard(NameBuff);
4668 p = NameBuff;
4669 }
4670 else
4671 p = NULL;
4672 }
4673 else /* n == 2 */
4674 {
4675 expand_T xpc;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004676 int options = WILD_LIST_NOTFOUND|WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004677
4678 ExpandInit(&xpc);
4679 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004680 if (p_wic)
4681 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004682 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01004683 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004684 if (p == NULL)
4685 return FAIL;
4686 }
4687 if (p != NULL)
4688 {
4689 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
4690 p, cmdlinep);
4691 if (n == 2) /* p came from ExpandOne() */
4692 vim_free(p);
4693 }
4694 }
4695 }
4696 }
4697 return OK;
4698}
4699
4700/*
4701 * Replace part of the command line, keeping eap->cmd, eap->arg and
4702 * eap->nextcmd correct.
4703 * "src" points to the part that is to be replaced, of length "srclen".
4704 * "repl" is the replacement string.
4705 * Returns a pointer to the character after the replaced string.
4706 * Returns NULL for failure.
4707 */
4708 static char_u *
4709repl_cmdline(eap, src, srclen, repl, cmdlinep)
4710 exarg_T *eap;
4711 char_u *src;
4712 int srclen;
4713 char_u *repl;
4714 char_u **cmdlinep;
4715{
4716 int len;
4717 int i;
4718 char_u *new_cmdline;
4719
4720 /*
4721 * The new command line is build in new_cmdline[].
4722 * First allocate it.
4723 * Careful: a "+cmd" argument may have been NUL terminated.
4724 */
4725 len = (int)STRLEN(repl);
4726 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004727 if (eap->nextcmd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004728 i += (int)STRLEN(eap->nextcmd);/* add space for next command */
4729 if ((new_cmdline = alloc((unsigned)i)) == NULL)
4730 return NULL; /* out of memory! */
4731
4732 /*
4733 * Copy the stuff before the expanded part.
4734 * Copy the expanded stuff.
4735 * Copy what came after the expanded part.
4736 * Copy the next commands, if there are any.
4737 */
4738 i = (int)(src - *cmdlinep); /* length of part before match */
4739 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00004740
Bram Moolenaar071d4272004-06-13 20:20:40 +00004741 mch_memmove(new_cmdline + i, repl, (size_t)len);
4742 i += len; /* remember the end of the string */
4743 STRCPY(new_cmdline + i, src + srclen);
4744 src = new_cmdline + i; /* remember where to continue */
4745
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004746 if (eap->nextcmd != NULL) /* append next command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004747 {
4748 i = (int)STRLEN(new_cmdline) + 1;
4749 STRCPY(new_cmdline + i, eap->nextcmd);
4750 eap->nextcmd = new_cmdline + i;
4751 }
4752 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
4753 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
4754 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
4755 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
4756 vim_free(*cmdlinep);
4757 *cmdlinep = new_cmdline;
4758
4759 return src;
4760}
4761
4762/*
4763 * Check for '|' to separate commands and '"' to start comments.
4764 */
4765 void
4766separate_nextcmd(eap)
4767 exarg_T *eap;
4768{
4769 char_u *p;
4770
Bram Moolenaar86b68352004-12-27 21:59:20 +00004771#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004772 p = skip_grep_pat(eap);
4773#else
4774 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004775#endif
4776
4777 for ( ; *p; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004778 {
4779 if (*p == Ctrl_V)
4780 {
4781 if (eap->argt & (USECTRLV | XFILE))
4782 ++p; /* skip CTRL-V and next char */
4783 else
Bram Moolenaarb0db5692007-08-14 20:54:49 +00004784 /* remove CTRL-V and skip next char */
Bram Moolenaara7241f52008-06-24 20:39:31 +00004785 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004786 if (*p == NUL) /* stop at NUL after CTRL-V */
4787 break;
4788 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004789
4790#ifdef FEAT_EVAL
4791 /* Skip over `=expr` when wildcards are expanded. */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004792 else if (p[0] == '`' && p[1] == '=' && (eap->argt & XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004793 {
4794 p += 2;
4795 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004796 }
4797#endif
4798
Bram Moolenaar071d4272004-06-13 20:20:40 +00004799 /* Check for '"': start of comment or '|': next command */
4800 /* :@" and :*" do not start a comment!
4801 * :redir @" doesn't either. */
4802 else if ((*p == '"' && !(eap->argt & NOTRLCOM)
4803 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
4804 || p != eap->arg)
4805 && (eap->cmdidx != CMD_redir
4806 || p != eap->arg + 1 || p[-1] != '@'))
4807 || *p == '|' || *p == '\n')
4808 {
4809 /*
4810 * We remove the '\' before the '|', unless USECTRLV is used
4811 * AND 'b' is present in 'cpoptions'.
4812 */
4813 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
4814 || !(eap->argt & USECTRLV)) && *(p - 1) == '\\')
4815 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00004816 STRMOVE(p - 1, p); /* remove the '\' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004817 --p;
4818 }
4819 else
4820 {
4821 eap->nextcmd = check_nextcmd(p);
4822 *p = NUL;
4823 break;
4824 }
4825 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004826 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004827
Bram Moolenaar071d4272004-06-13 20:20:40 +00004828 if (!(eap->argt & NOTRLCOM)) /* remove trailing spaces */
4829 del_trailing_spaces(eap->arg);
4830}
4831
4832/*
4833 * get + command from ex argument
4834 */
4835 static char_u *
4836getargcmd(argp)
4837 char_u **argp;
4838{
4839 char_u *arg = *argp;
4840 char_u *command = NULL;
4841
4842 if (*arg == '+') /* +[command] */
4843 {
4844 ++arg;
4845 if (vim_isspace(*arg))
4846 command = dollar_command;
4847 else
4848 {
4849 command = arg;
4850 arg = skip_cmd_arg(command, TRUE);
4851 if (*arg != NUL)
4852 *arg++ = NUL; /* terminate command with NUL */
4853 }
4854
4855 arg = skipwhite(arg); /* skip over spaces */
4856 *argp = arg;
4857 }
4858 return command;
4859}
4860
4861/*
4862 * Find end of "+command" argument. Skip over "\ " and "\\".
4863 */
4864 static char_u *
4865skip_cmd_arg(p, rembs)
4866 char_u *p;
4867 int rembs; /* TRUE to halve the number of backslashes */
4868{
4869 while (*p && !vim_isspace(*p))
4870 {
4871 if (*p == '\\' && p[1] != NUL)
4872 {
4873 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00004874 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004875 else
4876 ++p;
4877 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004878 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004879 }
4880 return p;
4881}
4882
4883/*
4884 * Get "++opt=arg" argument.
4885 * Return FAIL or OK.
4886 */
4887 static int
4888getargopt(eap)
4889 exarg_T *eap;
4890{
4891 char_u *arg = eap->arg + 2;
4892 int *pp = NULL;
4893#ifdef FEAT_MBYTE
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004894 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004895 char_u *p;
4896#endif
4897
4898 /* ":edit ++[no]bin[ary] file" */
4899 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
4900 {
4901 if (*arg == 'n')
4902 {
4903 arg += 2;
4904 eap->force_bin = FORCE_NOBIN;
4905 }
4906 else
4907 eap->force_bin = FORCE_BIN;
4908 if (!checkforcmd(&arg, "binary", 3))
4909 return FAIL;
4910 eap->arg = skipwhite(arg);
4911 return OK;
4912 }
4913
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004914 /* ":read ++edit file" */
4915 if (STRNCMP(arg, "edit", 4) == 0)
4916 {
4917 eap->read_edit = TRUE;
4918 eap->arg = skipwhite(arg + 4);
4919 return OK;
4920 }
4921
Bram Moolenaar071d4272004-06-13 20:20:40 +00004922 if (STRNCMP(arg, "ff", 2) == 0)
4923 {
4924 arg += 2;
4925 pp = &eap->force_ff;
4926 }
4927 else if (STRNCMP(arg, "fileformat", 10) == 0)
4928 {
4929 arg += 10;
4930 pp = &eap->force_ff;
4931 }
4932#ifdef FEAT_MBYTE
4933 else if (STRNCMP(arg, "enc", 3) == 0)
4934 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01004935 if (STRNCMP(arg, "encoding", 8) == 0)
4936 arg += 8;
4937 else
4938 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004939 pp = &eap->force_enc;
4940 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004941 else if (STRNCMP(arg, "bad", 3) == 0)
4942 {
4943 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004944 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004945 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004946#endif
4947
4948 if (pp == NULL || *arg != '=')
4949 return FAIL;
4950
4951 ++arg;
4952 *pp = (int)(arg - eap->cmd);
4953 arg = skip_cmd_arg(arg, FALSE);
4954 eap->arg = skipwhite(arg);
4955 *arg = NUL;
4956
4957#ifdef FEAT_MBYTE
4958 if (pp == &eap->force_ff)
4959 {
4960#endif
4961 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
4962 return FAIL;
4963#ifdef FEAT_MBYTE
4964 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004965 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004966 {
4967 /* Make 'fileencoding' lower case. */
4968 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
4969 *p = TOLOWER_ASC(*p);
4970 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004971 else
4972 {
4973 /* Check ++bad= argument. Must be a single-byte character, "keep" or
4974 * "drop". */
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004975 p = eap->cmd + bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004976 if (STRICMP(p, "keep") == 0)
4977 eap->bad_char = BAD_KEEP;
4978 else if (STRICMP(p, "drop") == 0)
4979 eap->bad_char = BAD_DROP;
4980 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
4981 eap->bad_char = *p;
4982 else
4983 return FAIL;
4984 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985#endif
4986
4987 return OK;
4988}
4989
4990/*
4991 * ":abbreviate" and friends.
4992 */
4993 static void
4994ex_abbreviate(eap)
4995 exarg_T *eap;
4996{
4997 do_exmap(eap, TRUE); /* almost the same as mapping */
4998}
4999
5000/*
5001 * ":map" and friends.
5002 */
5003 static void
5004ex_map(eap)
5005 exarg_T *eap;
5006{
5007 /*
5008 * If we are sourcing .exrc or .vimrc in current directory we
5009 * print the mappings for security reasons.
5010 */
5011 if (secure)
5012 {
5013 secure = 2;
5014 msg_outtrans(eap->cmd);
5015 msg_putchar('\n');
5016 }
5017 do_exmap(eap, FALSE);
5018}
5019
5020/*
5021 * ":unmap" and friends.
5022 */
5023 static void
5024ex_unmap(eap)
5025 exarg_T *eap;
5026{
5027 do_exmap(eap, FALSE);
5028}
5029
5030/*
5031 * ":mapclear" and friends.
5032 */
5033 static void
5034ex_mapclear(eap)
5035 exarg_T *eap;
5036{
5037 map_clear(eap->cmd, eap->arg, eap->forceit, FALSE);
5038}
5039
5040/*
5041 * ":abclear" and friends.
5042 */
5043 static void
5044ex_abclear(eap)
5045 exarg_T *eap;
5046{
5047 map_clear(eap->cmd, eap->arg, TRUE, TRUE);
5048}
5049
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005050#if defined(FEAT_AUTOCMD) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005051 static void
5052ex_autocmd(eap)
5053 exarg_T *eap;
5054{
5055 /*
5056 * Disallow auto commands from .exrc and .vimrc in current
5057 * directory for security reasons.
5058 */
5059 if (secure)
5060 {
5061 secure = 2;
5062 eap->errmsg = e_curdir;
5063 }
5064 else if (eap->cmdidx == CMD_autocmd)
5065 do_autocmd(eap->arg, eap->forceit);
5066 else
5067 do_augroup(eap->arg, eap->forceit);
5068}
5069
5070/*
5071 * ":doautocmd": Apply the automatic commands to the current buffer.
5072 */
5073 static void
5074ex_doautocmd(eap)
5075 exarg_T *eap;
5076{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005077 char_u *arg = eap->arg;
5078 int call_do_modelines = check_nomodeline(&arg);
5079
5080 (void)do_doautocmd(arg, TRUE);
5081 if (call_do_modelines) /* Only when there is no <nomodeline>. */
5082 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005083}
5084#endif
5085
5086#ifdef FEAT_LISTCMDS
5087/*
5088 * :[N]bunload[!] [N] [bufname] unload buffer
5089 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
5090 * :[N]bwipeout[!] [N] [bufname] delete buffer really
5091 */
5092 static void
5093ex_bunload(eap)
5094 exarg_T *eap;
5095{
5096 eap->errmsg = do_bufdel(
5097 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
5098 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
5099 : DOBUF_UNLOAD, eap->arg,
5100 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
5101}
5102
5103/*
5104 * :[N]buffer [N] to buffer N
5105 * :[N]sbuffer [N] to buffer N
5106 */
5107 static void
5108ex_buffer(eap)
5109 exarg_T *eap;
5110{
5111 if (*eap->arg)
5112 eap->errmsg = e_trailing;
5113 else
5114 {
5115 if (eap->addr_count == 0) /* default is current buffer */
5116 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
5117 else
5118 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
5119 }
5120}
5121
5122/*
5123 * :[N]bmodified [N] to next mod. buffer
5124 * :[N]sbmodified [N] to next mod. buffer
5125 */
5126 static void
5127ex_bmodified(eap)
5128 exarg_T *eap;
5129{
5130 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
5131}
5132
5133/*
5134 * :[N]bnext [N] to next buffer
5135 * :[N]sbnext [N] split and to next buffer
5136 */
5137 static void
5138ex_bnext(eap)
5139 exarg_T *eap;
5140{
5141 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
5142}
5143
5144/*
5145 * :[N]bNext [N] to previous buffer
5146 * :[N]bprevious [N] to previous buffer
5147 * :[N]sbNext [N] split and to previous buffer
5148 * :[N]sbprevious [N] split and to previous buffer
5149 */
5150 static void
5151ex_bprevious(eap)
5152 exarg_T *eap;
5153{
5154 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
5155}
5156
5157/*
5158 * :brewind to first buffer
5159 * :bfirst to first buffer
5160 * :sbrewind split and to first buffer
5161 * :sbfirst split and to first buffer
5162 */
5163 static void
5164ex_brewind(eap)
5165 exarg_T *eap;
5166{
5167 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
5168}
5169
5170/*
5171 * :blast to last buffer
5172 * :sblast split and to last buffer
5173 */
5174 static void
5175ex_blast(eap)
5176 exarg_T *eap;
5177{
5178 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
5179}
5180#endif
5181
5182 int
5183ends_excmd(c)
5184 int c;
5185{
5186 return (c == NUL || c == '|' || c == '"' || c == '\n');
5187}
5188
5189#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5190 || defined(PROTO)
5191/*
5192 * Return the next command, after the first '|' or '\n'.
5193 * Return NULL if not found.
5194 */
5195 char_u *
5196find_nextcmd(p)
5197 char_u *p;
5198{
5199 while (*p != '|' && *p != '\n')
5200 {
5201 if (*p == NUL)
5202 return NULL;
5203 ++p;
5204 }
5205 return (p + 1);
5206}
5207#endif
5208
5209/*
5210 * Check if *p is a separator between Ex commands.
5211 * Return NULL if it isn't, (p + 1) if it is.
5212 */
5213 char_u *
5214check_nextcmd(p)
5215 char_u *p;
5216{
5217 p = skipwhite(p);
5218 if (*p == '|' || *p == '\n')
5219 return (p + 1);
5220 else
5221 return NULL;
5222}
5223
5224/*
5225 * - if there are more files to edit
5226 * - and this is the last window
5227 * - and forceit not used
5228 * - and not repeated twice on a row
5229 * return FAIL and give error message if 'message' TRUE
5230 * return OK otherwise
5231 */
5232 static int
5233check_more(message, forceit)
5234 int message; /* when FALSE check only, no messages */
5235 int forceit;
5236{
5237 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5238
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005239 if (!forceit && only_one_window()
5240 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005241 {
5242 if (message)
5243 {
5244#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
5245 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
5246 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005247 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005248
5249 if (n == 1)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02005250 vim_strncpy(buff,
5251 (char_u *)_("1 more file to edit. Quit anyway?"),
Bram Moolenaard9462e32011-04-11 21:35:11 +02005252 DIALOG_MSG_SIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005253 else
Bram Moolenaard9462e32011-04-11 21:35:11 +02005254 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005255 _("%d more files to edit. Quit anyway?"), n);
5256 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5257 return OK;
5258 return FAIL;
5259 }
5260#endif
5261 if (n == 1)
5262 EMSG(_("E173: 1 more file to edit"));
5263 else
5264 EMSGN(_("E173: %ld more files to edit"), n);
5265 quitmore = 2; /* next try to quit is allowed */
5266 }
5267 return FAIL;
5268 }
5269 return OK;
5270}
5271
5272#ifdef FEAT_CMDL_COMPL
5273/*
5274 * Function given to ExpandGeneric() to obtain the list of command names.
5275 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005276 char_u *
5277get_command_name(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00005278 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005279 int idx;
5280{
5281 if (idx >= (int)CMD_SIZE)
5282# ifdef FEAT_USR_CMDS
5283 return get_user_command_name(idx);
5284# else
5285 return NULL;
5286# endif
5287 return cmdnames[idx].cmd_name;
5288}
5289#endif
5290
5291#if defined(FEAT_USR_CMDS) || defined(PROTO)
5292static 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));
5293static void uc_list __ARGS((char_u *name, size_t name_len));
5294static int uc_scan_attr __ARGS((char_u *attr, size_t len, long *argt, long *def, int *flags, int *compl, char_u **compl_arg));
5295static char_u *uc_split_args __ARGS((char_u *arg, size_t *lenp));
5296static 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));
5297
5298 static int
5299uc_add_command(name, name_len, rep, argt, def, flags, compl, compl_arg, force)
5300 char_u *name;
5301 size_t name_len;
5302 char_u *rep;
5303 long argt;
5304 long def;
5305 int flags;
5306 int compl;
5307 char_u *compl_arg;
5308 int force;
5309{
5310 ucmd_T *cmd = NULL;
5311 char_u *p;
5312 int i;
5313 int cmp = 1;
5314 char_u *rep_buf = NULL;
5315 garray_T *gap;
5316
Bram Moolenaar9c102382006-05-03 21:26:49 +00005317 replace_termcodes(rep, &rep_buf, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005318 if (rep_buf == NULL)
5319 {
5320 /* Can't replace termcodes - try using the string as is */
5321 rep_buf = vim_strsave(rep);
5322
5323 /* Give up if out of memory */
5324 if (rep_buf == NULL)
5325 return FAIL;
5326 }
5327
5328 /* get address of growarray: global or in curbuf */
5329 if (flags & UC_BUFFER)
5330 {
5331 gap = &curbuf->b_ucmds;
5332 if (gap->ga_itemsize == 0)
5333 ga_init2(gap, (int)sizeof(ucmd_T), 4);
5334 }
5335 else
5336 gap = &ucmds;
5337
5338 /* Search for the command in the already defined commands. */
5339 for (i = 0; i < gap->ga_len; ++i)
5340 {
5341 size_t len;
5342
5343 cmd = USER_CMD_GA(gap, i);
5344 len = STRLEN(cmd->uc_name);
5345 cmp = STRNCMP(name, cmd->uc_name, name_len);
5346 if (cmp == 0)
5347 {
5348 if (name_len < len)
5349 cmp = -1;
5350 else if (name_len > len)
5351 cmp = 1;
5352 }
5353
5354 if (cmp == 0)
5355 {
5356 if (!force)
5357 {
5358 EMSG(_("E174: Command already exists: add ! to replace it"));
5359 goto fail;
5360 }
5361
5362 vim_free(cmd->uc_rep);
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00005363 cmd->uc_rep = NULL;
5364#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5365 vim_free(cmd->uc_compl_arg);
5366 cmd->uc_compl_arg = NULL;
5367#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005368 break;
5369 }
5370
5371 /* Stop as soon as we pass the name to add */
5372 if (cmp < 0)
5373 break;
5374 }
5375
5376 /* Extend the array unless we're replacing an existing command */
5377 if (cmp != 0)
5378 {
5379 if (ga_grow(gap, 1) != OK)
5380 goto fail;
5381 if ((p = vim_strnsave(name, (int)name_len)) == NULL)
5382 goto fail;
5383
5384 cmd = USER_CMD_GA(gap, i);
5385 mch_memmove(cmd + 1, cmd, (gap->ga_len - i) * sizeof(ucmd_T));
5386
5387 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005388
5389 cmd->uc_name = p;
5390 }
5391
5392 cmd->uc_rep = rep_buf;
5393 cmd->uc_argt = argt;
5394 cmd->uc_def = def;
5395 cmd->uc_compl = compl;
5396#ifdef FEAT_EVAL
5397 cmd->uc_scriptID = current_SID;
5398# ifdef FEAT_CMDL_COMPL
5399 cmd->uc_compl_arg = compl_arg;
5400# endif
5401#endif
5402
5403 return OK;
5404
5405fail:
5406 vim_free(rep_buf);
5407#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5408 vim_free(compl_arg);
5409#endif
5410 return FAIL;
5411}
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005412#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005413
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005414#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005415/*
5416 * List of names for completion for ":command" with the EXPAND_ flag.
5417 * Must be alphabetical for completion.
5418 */
5419static struct
5420{
5421 int expand;
5422 char *name;
5423} command_complete[] =
5424{
5425 {EXPAND_AUGROUP, "augroup"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005426 {EXPAND_BEHAVE, "behave"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005427 {EXPAND_BUFFERS, "buffer"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005428 {EXPAND_COLORS, "color"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005429 {EXPAND_COMMANDS, "command"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005430 {EXPAND_COMPILER, "compiler"},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005431#if defined(FEAT_CSCOPE)
5432 {EXPAND_CSCOPE, "cscope"},
5433#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005434#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5435 {EXPAND_USER_DEFINED, "custom"},
Bram Moolenaara466c992005-07-09 21:03:22 +00005436 {EXPAND_USER_LIST, "customlist"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005437#endif
5438 {EXPAND_DIRECTORIES, "dir"},
5439 {EXPAND_ENV_VARS, "environment"},
5440 {EXPAND_EVENTS, "event"},
5441 {EXPAND_EXPRESSION, "expression"},
5442 {EXPAND_FILES, "file"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005443 {EXPAND_FILES_IN_PATH, "file_in_path"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005444 {EXPAND_FILETYPE, "filetype"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005445 {EXPAND_FUNCTIONS, "function"},
5446 {EXPAND_HELP, "help"},
5447 {EXPAND_HIGHLIGHT, "highlight"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005448#if defined(FEAT_CMDHIST)
5449 {EXPAND_HISTORY, "history"},
5450#endif
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005451#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005452 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005453 {EXPAND_LOCALES, "locale"},
5454#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005455 {EXPAND_MAPPINGS, "mapping"},
5456 {EXPAND_MENUS, "menu"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005457 {EXPAND_OWNSYNTAX, "syntax"},
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02005458#if defined(FEAT_PROFILE)
5459 {EXPAND_SYNTIME, "syntime"},
5460#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005461 {EXPAND_SETTINGS, "option"},
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005462 {EXPAND_SHELLCMD, "shellcmd"},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005463#if defined(FEAT_SIGNS)
5464 {EXPAND_SIGN, "sign"},
5465#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005466 {EXPAND_TAGS, "tag"},
5467 {EXPAND_TAGS_LISTFILES, "tag_listfiles"},
Bram Moolenaar24305862012-08-15 14:05:05 +02005468 {EXPAND_USER, "user"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005469 {EXPAND_USER_VARS, "var"},
5470 {0, NULL}
5471};
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005472#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005473
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005474#if defined(FEAT_USR_CMDS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005475 static void
5476uc_list(name, name_len)
5477 char_u *name;
5478 size_t name_len;
5479{
5480 int i, j;
5481 int found = FALSE;
5482 ucmd_T *cmd;
5483 int len;
5484 long a;
5485 garray_T *gap;
5486
5487 gap = &curbuf->b_ucmds;
5488 for (;;)
5489 {
5490 for (i = 0; i < gap->ga_len; ++i)
5491 {
5492 cmd = USER_CMD_GA(gap, i);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005493 a = (long)cmd->uc_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005494
5495 /* Skip commands which don't match the requested prefix */
5496 if (STRNCMP(name, cmd->uc_name, name_len) != 0)
5497 continue;
5498
5499 /* Put out the title first time */
5500 if (!found)
5501 MSG_PUTS_TITLE(_("\n Name Args Range Complete Definition"));
5502 found = TRUE;
5503 msg_putchar('\n');
5504 if (got_int)
5505 break;
5506
5507 /* Special cases */
5508 msg_putchar(a & BANG ? '!' : ' ');
5509 msg_putchar(a & REGSTR ? '"' : ' ');
5510 msg_putchar(gap != &ucmds ? 'b' : ' ');
5511 msg_putchar(' ');
5512
5513 msg_outtrans_attr(cmd->uc_name, hl_attr(HLF_D));
5514 len = (int)STRLEN(cmd->uc_name) + 4;
5515
5516 do {
5517 msg_putchar(' ');
5518 ++len;
5519 } while (len < 16);
5520
5521 len = 0;
5522
5523 /* Arguments */
5524 switch ((int)(a & (EXTRA|NOSPC|NEEDARG)))
5525 {
5526 case 0: IObuff[len++] = '0'; break;
5527 case (EXTRA): IObuff[len++] = '*'; break;
5528 case (EXTRA|NOSPC): IObuff[len++] = '?'; break;
5529 case (EXTRA|NEEDARG): IObuff[len++] = '+'; break;
5530 case (EXTRA|NOSPC|NEEDARG): IObuff[len++] = '1'; break;
5531 }
5532
5533 do {
5534 IObuff[len++] = ' ';
5535 } while (len < 5);
5536
5537 /* Range */
5538 if (a & (RANGE|COUNT))
5539 {
5540 if (a & COUNT)
5541 {
5542 /* -count=N */
5543 sprintf((char *)IObuff + len, "%ldc", cmd->uc_def);
5544 len += (int)STRLEN(IObuff + len);
5545 }
5546 else if (a & DFLALL)
5547 IObuff[len++] = '%';
5548 else if (cmd->uc_def >= 0)
5549 {
5550 /* -range=N */
5551 sprintf((char *)IObuff + len, "%ld", cmd->uc_def);
5552 len += (int)STRLEN(IObuff + len);
5553 }
5554 else
5555 IObuff[len++] = '.';
5556 }
5557
5558 do {
5559 IObuff[len++] = ' ';
5560 } while (len < 11);
5561
5562 /* Completion */
5563 for (j = 0; command_complete[j].expand != 0; ++j)
5564 if (command_complete[j].expand == cmd->uc_compl)
5565 {
5566 STRCPY(IObuff + len, command_complete[j].name);
5567 len += (int)STRLEN(IObuff + len);
5568 break;
5569 }
5570
5571 do {
5572 IObuff[len++] = ' ';
5573 } while (len < 21);
5574
5575 IObuff[len] = '\0';
5576 msg_outtrans(IObuff);
5577
5578 msg_outtrans_special(cmd->uc_rep, FALSE);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005579#ifdef FEAT_EVAL
5580 if (p_verbose > 0)
5581 last_set_msg(cmd->uc_scriptID);
5582#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005583 out_flush();
5584 ui_breakcheck();
5585 if (got_int)
5586 break;
5587 }
5588 if (gap == &ucmds || i < gap->ga_len)
5589 break;
5590 gap = &ucmds;
5591 }
5592
5593 if (!found)
5594 MSG(_("No user-defined commands found"));
5595}
5596
5597 static char_u *
5598uc_fun_cmd()
5599{
5600 static char_u fcmd[] = {0x84, 0xaf, 0x60, 0xb9, 0xaf, 0xb5, 0x60, 0xa4,
5601 0xa5, 0xad, 0xa1, 0xae, 0xa4, 0x60, 0xa1, 0x60,
5602 0xb3, 0xa8, 0xb2, 0xb5, 0xa2, 0xa2, 0xa5, 0xb2,
5603 0xb9, 0x7f, 0};
5604 int i;
5605
5606 for (i = 0; fcmd[i]; ++i)
5607 IObuff[i] = fcmd[i] - 0x40;
5608 IObuff[i] = 0;
5609 return IObuff;
5610}
5611
5612 static int
5613uc_scan_attr(attr, len, argt, def, flags, compl, compl_arg)
5614 char_u *attr;
5615 size_t len;
5616 long *argt;
5617 long *def;
5618 int *flags;
5619 int *compl;
5620 char_u **compl_arg;
5621{
5622 char_u *p;
5623
5624 if (len == 0)
5625 {
5626 EMSG(_("E175: No attribute specified"));
5627 return FAIL;
5628 }
5629
5630 /* First, try the simple attributes (no arguments) */
5631 if (STRNICMP(attr, "bang", len) == 0)
5632 *argt |= BANG;
5633 else if (STRNICMP(attr, "buffer", len) == 0)
5634 *flags |= UC_BUFFER;
5635 else if (STRNICMP(attr, "register", len) == 0)
5636 *argt |= REGSTR;
5637 else if (STRNICMP(attr, "bar", len) == 0)
5638 *argt |= TRLBAR;
5639 else
5640 {
5641 int i;
5642 char_u *val = NULL;
5643 size_t vallen = 0;
5644 size_t attrlen = len;
5645
5646 /* Look for the attribute name - which is the part before any '=' */
5647 for (i = 0; i < (int)len; ++i)
5648 {
5649 if (attr[i] == '=')
5650 {
5651 val = &attr[i + 1];
5652 vallen = len - i - 1;
5653 attrlen = i;
5654 break;
5655 }
5656 }
5657
5658 if (STRNICMP(attr, "nargs", attrlen) == 0)
5659 {
5660 if (vallen == 1)
5661 {
5662 if (*val == '0')
5663 /* Do nothing - this is the default */;
5664 else if (*val == '1')
5665 *argt |= (EXTRA | NOSPC | NEEDARG);
5666 else if (*val == '*')
5667 *argt |= EXTRA;
5668 else if (*val == '?')
5669 *argt |= (EXTRA | NOSPC);
5670 else if (*val == '+')
5671 *argt |= (EXTRA | NEEDARG);
5672 else
5673 goto wrong_nargs;
5674 }
5675 else
5676 {
5677wrong_nargs:
5678 EMSG(_("E176: Invalid number of arguments"));
5679 return FAIL;
5680 }
5681 }
5682 else if (STRNICMP(attr, "range", attrlen) == 0)
5683 {
5684 *argt |= RANGE;
5685 if (vallen == 1 && *val == '%')
5686 *argt |= DFLALL;
5687 else if (val != NULL)
5688 {
5689 p = val;
5690 if (*def >= 0)
5691 {
5692two_count:
5693 EMSG(_("E177: Count cannot be specified twice"));
5694 return FAIL;
5695 }
5696
5697 *def = getdigits(&p);
5698 *argt |= (ZEROR | NOTADR);
5699
5700 if (p != val + vallen || vallen == 0)
5701 {
5702invalid_count:
5703 EMSG(_("E178: Invalid default value for count"));
5704 return FAIL;
5705 }
5706 }
5707 }
5708 else if (STRNICMP(attr, "count", attrlen) == 0)
5709 {
Bram Moolenaar32e7b2d2005-02-27 22:36:47 +00005710 *argt |= (COUNT | ZEROR | RANGE | NOTADR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005711
5712 if (val != NULL)
5713 {
5714 p = val;
5715 if (*def >= 0)
5716 goto two_count;
5717
5718 *def = getdigits(&p);
5719
5720 if (p != val + vallen)
5721 goto invalid_count;
5722 }
5723
5724 if (*def < 0)
5725 *def = 0;
5726 }
5727 else if (STRNICMP(attr, "complete", attrlen) == 0)
5728 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005729 if (val == NULL)
5730 {
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005731 EMSG(_("E179: argument required for -complete"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005732 return FAIL;
5733 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005734
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005735 if (parse_compl_arg(val, (int)vallen, compl, argt, compl_arg)
5736 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005737 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005738 }
5739 else
5740 {
5741 char_u ch = attr[len];
5742 attr[len] = '\0';
5743 EMSG2(_("E181: Invalid attribute: %s"), attr);
5744 attr[len] = ch;
5745 return FAIL;
5746 }
5747 }
5748
5749 return OK;
5750}
5751
Bram Moolenaara850a712009-01-28 14:42:59 +00005752/*
5753 * ":command ..."
5754 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005755 static void
5756ex_command(eap)
5757 exarg_T *eap;
5758{
5759 char_u *name;
5760 char_u *end;
5761 char_u *p;
5762 long argt = 0;
5763 long def = -1;
5764 int flags = 0;
5765 int compl = EXPAND_NOTHING;
5766 char_u *compl_arg = NULL;
5767 int has_attr = (eap->arg[0] == '-');
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01005768 int name_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005769
5770 p = eap->arg;
5771
5772 /* Check for attributes */
5773 while (*p == '-')
5774 {
5775 ++p;
5776 end = skiptowhite(p);
5777 if (uc_scan_attr(p, end - p, &argt, &def, &flags, &compl, &compl_arg)
5778 == FAIL)
5779 return;
5780 p = skipwhite(end);
5781 }
5782
5783 /* Get the name (if any) and skip to the following argument */
5784 name = p;
5785 if (ASCII_ISALPHA(*p))
5786 while (ASCII_ISALNUM(*p))
5787 ++p;
5788 if (!ends_excmd(*p) && !vim_iswhite(*p))
5789 {
5790 EMSG(_("E182: Invalid command name"));
5791 return;
5792 }
5793 end = p;
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01005794 name_len = (int)(end - name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005795
5796 /* If there is nothing after the name, and no attributes were specified,
5797 * we are listing commands
5798 */
5799 p = skipwhite(end);
5800 if (!has_attr && ends_excmd(*p))
5801 {
5802 uc_list(name, end - name);
5803 }
5804 else if (!ASCII_ISUPPER(*name))
5805 {
5806 EMSG(_("E183: User defined commands must start with an uppercase letter"));
5807 return;
5808 }
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01005809 else if ((name_len == 1 && *name == 'X')
5810 || (name_len <= 4
5811 && STRNCMP(name, "Next", name_len > 4 ? 4 : name_len) == 0))
5812 {
5813 EMSG(_("E841: Reserved name, cannot be used for user defined command"));
5814 return;
5815 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005816 else
5817 uc_add_command(name, end - name, p, argt, def, flags, compl, compl_arg,
5818 eap->forceit);
5819}
5820
5821/*
5822 * ":comclear"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005823 * Clear all user commands, global and for current buffer.
5824 */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00005825 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005826ex_comclear(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00005827 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005828{
5829 uc_clear(&ucmds);
5830 uc_clear(&curbuf->b_ucmds);
5831}
5832
5833/*
5834 * Clear all user commands for "gap".
5835 */
5836 void
5837uc_clear(gap)
5838 garray_T *gap;
5839{
5840 int i;
5841 ucmd_T *cmd;
5842
5843 for (i = 0; i < gap->ga_len; ++i)
5844 {
5845 cmd = USER_CMD_GA(gap, i);
5846 vim_free(cmd->uc_name);
5847 vim_free(cmd->uc_rep);
5848# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5849 vim_free(cmd->uc_compl_arg);
5850# endif
5851 }
5852 ga_clear(gap);
5853}
5854
5855 static void
5856ex_delcommand(eap)
5857 exarg_T *eap;
5858{
5859 int i = 0;
5860 ucmd_T *cmd = NULL;
5861 int cmp = -1;
5862 garray_T *gap;
5863
5864 gap = &curbuf->b_ucmds;
5865 for (;;)
5866 {
5867 for (i = 0; i < gap->ga_len; ++i)
5868 {
5869 cmd = USER_CMD_GA(gap, i);
5870 cmp = STRCMP(eap->arg, cmd->uc_name);
5871 if (cmp <= 0)
5872 break;
5873 }
5874 if (gap == &ucmds || cmp == 0)
5875 break;
5876 gap = &ucmds;
5877 }
5878
5879 if (cmp != 0)
5880 {
5881 EMSG2(_("E184: No such user-defined command: %s"), eap->arg);
5882 return;
5883 }
5884
5885 vim_free(cmd->uc_name);
5886 vim_free(cmd->uc_rep);
5887# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5888 vim_free(cmd->uc_compl_arg);
5889# endif
5890
5891 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005892
5893 if (i < gap->ga_len)
5894 mch_memmove(cmd, cmd + 1, (gap->ga_len - i) * sizeof(ucmd_T));
5895}
5896
Bram Moolenaar552f8a12007-03-08 17:12:08 +00005897/*
5898 * split and quote args for <f-args>
5899 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005900 static char_u *
5901uc_split_args(arg, lenp)
5902 char_u *arg;
5903 size_t *lenp;
5904{
5905 char_u *buf;
5906 char_u *p;
5907 char_u *q;
5908 int len;
5909
5910 /* Precalculate length */
5911 p = arg;
5912 len = 2; /* Initial and final quotes */
5913
5914 while (*p)
5915 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00005916 if (p[0] == '\\' && p[1] == '\\')
5917 {
5918 len += 2;
5919 p += 2;
5920 }
5921 else if (p[0] == '\\' && vim_iswhite(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005922 {
5923 len += 1;
5924 p += 2;
5925 }
5926 else if (*p == '\\' || *p == '"')
5927 {
5928 len += 2;
5929 p += 1;
5930 }
5931 else if (vim_iswhite(*p))
5932 {
5933 p = skipwhite(p);
5934 if (*p == NUL)
5935 break;
5936 len += 3; /* "," */
5937 }
5938 else
5939 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02005940#ifdef FEAT_MBYTE
5941 int charlen = (*mb_ptr2len)(p);
5942 len += charlen;
5943 p += charlen;
5944#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005945 ++len;
5946 ++p;
Bram Moolenaardfef1542012-07-10 19:25:10 +02005947#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005948 }
5949 }
5950
5951 buf = alloc(len + 1);
5952 if (buf == NULL)
5953 {
5954 *lenp = 0;
5955 return buf;
5956 }
5957
5958 p = arg;
5959 q = buf;
5960 *q++ = '"';
5961 while (*p)
5962 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00005963 if (p[0] == '\\' && p[1] == '\\')
5964 {
5965 *q++ = '\\';
5966 *q++ = '\\';
5967 p += 2;
5968 }
5969 else if (p[0] == '\\' && vim_iswhite(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005970 {
5971 *q++ = p[1];
5972 p += 2;
5973 }
5974 else if (*p == '\\' || *p == '"')
5975 {
5976 *q++ = '\\';
5977 *q++ = *p++;
5978 }
5979 else if (vim_iswhite(*p))
5980 {
5981 p = skipwhite(p);
5982 if (*p == NUL)
5983 break;
5984 *q++ = '"';
5985 *q++ = ',';
5986 *q++ = '"';
5987 }
5988 else
5989 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02005990 MB_COPY_CHAR(p, q);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005991 }
5992 }
5993 *q++ = '"';
5994 *q = 0;
5995
5996 *lenp = len;
5997 return buf;
5998}
5999
6000/*
6001 * Check for a <> code in a user command.
6002 * "code" points to the '<'. "len" the length of the <> (inclusive).
6003 * "buf" is where the result is to be added.
6004 * "split_buf" points to a buffer used for splitting, caller should free it.
6005 * "split_len" is the length of what "split_buf" contains.
6006 * Returns the length of the replacement, which has been added to "buf".
6007 * Returns -1 if there was no match, and only the "<" has been copied.
6008 */
6009 static size_t
6010uc_check_code(code, len, buf, cmd, eap, split_buf, split_len)
6011 char_u *code;
6012 size_t len;
6013 char_u *buf;
6014 ucmd_T *cmd; /* the user command we're expanding */
6015 exarg_T *eap; /* ex arguments */
6016 char_u **split_buf;
6017 size_t *split_len;
6018{
6019 size_t result = 0;
6020 char_u *p = code + 1;
6021 size_t l = len - 2;
6022 int quote = 0;
6023 enum { ct_ARGS, ct_BANG, ct_COUNT, ct_LINE1, ct_LINE2, ct_REGISTER,
6024 ct_LT, ct_NONE } type = ct_NONE;
6025
6026 if ((vim_strchr((char_u *)"qQfF", *p) != NULL) && p[1] == '-')
6027 {
6028 quote = (*p == 'q' || *p == 'Q') ? 1 : 2;
6029 p += 2;
6030 l -= 2;
6031 }
6032
Bram Moolenaar371d5402006-03-20 21:47:49 +00006033 ++l;
6034 if (l <= 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006035 type = ct_NONE;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006036 else if (STRNICMP(p, "args>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006037 type = ct_ARGS;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006038 else if (STRNICMP(p, "bang>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006039 type = ct_BANG;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006040 else if (STRNICMP(p, "count>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006041 type = ct_COUNT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006042 else if (STRNICMP(p, "line1>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006043 type = ct_LINE1;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006044 else if (STRNICMP(p, "line2>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006045 type = ct_LINE2;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006046 else if (STRNICMP(p, "lt>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006047 type = ct_LT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006048 else if (STRNICMP(p, "reg>", l) == 0 || STRNICMP(p, "register>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006049 type = ct_REGISTER;
6050
6051 switch (type)
6052 {
6053 case ct_ARGS:
6054 /* Simple case first */
6055 if (*eap->arg == NUL)
6056 {
6057 if (quote == 1)
6058 {
6059 result = 2;
6060 if (buf != NULL)
6061 STRCPY(buf, "''");
6062 }
6063 else
6064 result = 0;
6065 break;
6066 }
6067
6068 /* When specified there is a single argument don't split it.
6069 * Works for ":Cmd %" when % is "a b c". */
6070 if ((eap->argt & NOSPC) && quote == 2)
6071 quote = 1;
6072
6073 switch (quote)
6074 {
6075 case 0: /* No quoting, no splitting */
6076 result = STRLEN(eap->arg);
6077 if (buf != NULL)
6078 STRCPY(buf, eap->arg);
6079 break;
6080 case 1: /* Quote, but don't split */
6081 result = STRLEN(eap->arg) + 2;
6082 for (p = eap->arg; *p; ++p)
6083 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006084#ifdef FEAT_MBYTE
6085 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6086 /* DBCS can contain \ in a trail byte, skip the
6087 * double-byte character. */
6088 ++p;
6089 else
6090#endif
6091 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006092 ++result;
6093 }
6094
6095 if (buf != NULL)
6096 {
6097 *buf++ = '"';
6098 for (p = eap->arg; *p; ++p)
6099 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006100#ifdef FEAT_MBYTE
6101 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6102 /* DBCS can contain \ in a trail byte, copy the
6103 * double-byte character to avoid escaping. */
6104 *buf++ = *p++;
6105 else
6106#endif
6107 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006108 *buf++ = '\\';
6109 *buf++ = *p;
6110 }
6111 *buf = '"';
6112 }
6113
6114 break;
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006115 case 2: /* Quote and split (<f-args>) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006116 /* This is hard, so only do it once, and cache the result */
6117 if (*split_buf == NULL)
6118 *split_buf = uc_split_args(eap->arg, split_len);
6119
6120 result = *split_len;
6121 if (buf != NULL && result != 0)
6122 STRCPY(buf, *split_buf);
6123
6124 break;
6125 }
6126 break;
6127
6128 case ct_BANG:
6129 result = eap->forceit ? 1 : 0;
6130 if (quote)
6131 result += 2;
6132 if (buf != NULL)
6133 {
6134 if (quote)
6135 *buf++ = '"';
6136 if (eap->forceit)
6137 *buf++ = '!';
6138 if (quote)
6139 *buf = '"';
6140 }
6141 break;
6142
6143 case ct_LINE1:
6144 case ct_LINE2:
6145 case ct_COUNT:
6146 {
6147 char num_buf[20];
6148 long num = (type == ct_LINE1) ? eap->line1 :
6149 (type == ct_LINE2) ? eap->line2 :
6150 (eap->addr_count > 0) ? eap->line2 : cmd->uc_def;
6151 size_t num_len;
6152
6153 sprintf(num_buf, "%ld", num);
6154 num_len = STRLEN(num_buf);
6155 result = num_len;
6156
6157 if (quote)
6158 result += 2;
6159
6160 if (buf != NULL)
6161 {
6162 if (quote)
6163 *buf++ = '"';
6164 STRCPY(buf, num_buf);
6165 buf += num_len;
6166 if (quote)
6167 *buf = '"';
6168 }
6169
6170 break;
6171 }
6172
6173 case ct_REGISTER:
6174 result = eap->regname ? 1 : 0;
6175 if (quote)
6176 result += 2;
6177 if (buf != NULL)
6178 {
6179 if (quote)
6180 *buf++ = '\'';
6181 if (eap->regname)
6182 *buf++ = eap->regname;
6183 if (quote)
6184 *buf = '\'';
6185 }
6186 break;
6187
6188 case ct_LT:
6189 result = 1;
6190 if (buf != NULL)
6191 *buf = '<';
6192 break;
6193
6194 default:
6195 /* Not recognized: just copy the '<' and return -1. */
6196 result = (size_t)-1;
6197 if (buf != NULL)
6198 *buf = '<';
6199 break;
6200 }
6201
6202 return result;
6203}
6204
6205 static void
6206do_ucmd(eap)
6207 exarg_T *eap;
6208{
6209 char_u *buf;
6210 char_u *p;
6211 char_u *q;
6212
6213 char_u *start;
Bram Moolenaar25648a52009-02-21 19:37:46 +00006214 char_u *end = NULL;
Bram Moolenaara850a712009-01-28 14:42:59 +00006215 char_u *ksp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006216 size_t len, totlen;
6217
6218 size_t split_len = 0;
6219 char_u *split_buf = NULL;
6220 ucmd_T *cmd;
6221#ifdef FEAT_EVAL
6222 scid_T save_current_SID = current_SID;
6223#endif
6224
6225 if (eap->cmdidx == CMD_USER)
6226 cmd = USER_CMD(eap->useridx);
6227 else
6228 cmd = USER_CMD_GA(&curbuf->b_ucmds, eap->useridx);
6229
6230 /*
6231 * Replace <> in the command by the arguments.
Bram Moolenaara850a712009-01-28 14:42:59 +00006232 * First round: "buf" is NULL, compute length, allocate "buf".
6233 * Second round: copy result into "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006234 */
6235 buf = NULL;
6236 for (;;)
6237 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006238 p = cmd->uc_rep; /* source */
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00006239 q = buf; /* destination */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006240 totlen = 0;
Bram Moolenaara850a712009-01-28 14:42:59 +00006241
6242 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006243 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006244 start = vim_strchr(p, '<');
6245 if (start != NULL)
6246 end = vim_strchr(start + 1, '>');
6247 if (buf != NULL)
6248 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006249 for (ksp = p; *ksp != NUL && *ksp != K_SPECIAL; ++ksp)
6250 ;
6251 if (*ksp == K_SPECIAL
6252 && (start == NULL || ksp < start || end == NULL)
Bram Moolenaara850a712009-01-28 14:42:59 +00006253 && ((ksp[1] == KS_SPECIAL && ksp[2] == KE_FILLER)
6254# ifdef FEAT_GUI
6255 || (ksp[1] == KS_EXTRA && ksp[2] == (int)KE_CSI)
6256# endif
6257 ))
6258 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006259 /* K_SPECIAL has been put in the buffer as K_SPECIAL
Bram Moolenaara850a712009-01-28 14:42:59 +00006260 * KS_SPECIAL KE_FILLER, like for mappings, but
6261 * do_cmdline() doesn't handle that, so convert it back.
6262 * Also change K_SPECIAL KS_EXTRA KE_CSI into CSI. */
6263 len = ksp - p;
6264 if (len > 0)
6265 {
6266 mch_memmove(q, p, len);
6267 q += len;
6268 }
6269 *q++ = ksp[1] == KS_SPECIAL ? K_SPECIAL : CSI;
6270 p = ksp + 3;
6271 continue;
6272 }
6273 }
6274
6275 /* break if there no <item> is found */
6276 if (start == NULL || end == NULL)
6277 break;
6278
Bram Moolenaar071d4272004-06-13 20:20:40 +00006279 /* Include the '>' */
6280 ++end;
6281
6282 /* Take everything up to the '<' */
6283 len = start - p;
6284 if (buf == NULL)
6285 totlen += len;
6286 else
6287 {
6288 mch_memmove(q, p, len);
6289 q += len;
6290 }
6291
6292 len = uc_check_code(start, end - start, q, cmd, eap,
6293 &split_buf, &split_len);
6294 if (len == (size_t)-1)
6295 {
6296 /* no match, continue after '<' */
6297 p = start + 1;
6298 len = 1;
6299 }
6300 else
6301 p = end;
6302 if (buf == NULL)
6303 totlen += len;
6304 else
6305 q += len;
6306 }
6307 if (buf != NULL) /* second time here, finished */
6308 {
6309 STRCPY(q, p);
6310 break;
6311 }
6312
6313 totlen += STRLEN(p); /* Add on the trailing characters */
6314 buf = alloc((unsigned)(totlen + 1));
6315 if (buf == NULL)
6316 {
6317 vim_free(split_buf);
6318 return;
6319 }
6320 }
6321
6322#ifdef FEAT_EVAL
6323 current_SID = cmd->uc_scriptID;
6324#endif
6325 (void)do_cmdline(buf, eap->getline, eap->cookie,
6326 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
6327#ifdef FEAT_EVAL
6328 current_SID = save_current_SID;
6329#endif
6330 vim_free(buf);
6331 vim_free(split_buf);
6332}
6333
6334# if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6335 static char_u *
6336get_user_command_name(idx)
6337 int idx;
6338{
6339 return get_user_commands(NULL, idx - (int)CMD_SIZE);
6340}
6341
6342/*
6343 * Function given to ExpandGeneric() to obtain the list of user command names.
6344 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006345 char_u *
6346get_user_commands(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006347 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006348 int idx;
6349{
6350 if (idx < curbuf->b_ucmds.ga_len)
6351 return USER_CMD_GA(&curbuf->b_ucmds, idx)->uc_name;
6352 idx -= curbuf->b_ucmds.ga_len;
6353 if (idx < ucmds.ga_len)
6354 return USER_CMD(idx)->uc_name;
6355 return NULL;
6356}
6357
6358/*
6359 * Function given to ExpandGeneric() to obtain the list of user command
6360 * attributes.
6361 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006362 char_u *
6363get_user_cmd_flags(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006364 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006365 int idx;
6366{
6367 static char *user_cmd_flags[] =
6368 {"bang", "bar", "buffer", "complete", "count",
6369 "nargs", "range", "register"};
6370
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00006371 if (idx >= (int)(sizeof(user_cmd_flags) / sizeof(user_cmd_flags[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006372 return NULL;
6373 return (char_u *)user_cmd_flags[idx];
6374}
6375
6376/*
6377 * Function given to ExpandGeneric() to obtain the list of values for -nargs.
6378 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006379 char_u *
6380get_user_cmd_nargs(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006381 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006382 int idx;
6383{
6384 static char *user_cmd_nargs[] = {"0", "1", "*", "?", "+"};
6385
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00006386 if (idx >= (int)(sizeof(user_cmd_nargs) / sizeof(user_cmd_nargs[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006387 return NULL;
6388 return (char_u *)user_cmd_nargs[idx];
6389}
6390
6391/*
6392 * Function given to ExpandGeneric() to obtain the list of values for -complete.
6393 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006394 char_u *
6395get_user_cmd_complete(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006396 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006397 int idx;
6398{
6399 return (char_u *)command_complete[idx].name;
6400}
6401# endif /* FEAT_CMDL_COMPL */
6402
6403#endif /* FEAT_USR_CMDS */
6404
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006405#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
6406/*
6407 * Parse a completion argument "value[vallen]".
6408 * The detected completion goes in "*complp", argument type in "*argt".
6409 * When there is an argument, for function and user defined completion, it's
6410 * copied to allocated memory and stored in "*compl_arg".
6411 * Returns FAIL if something is wrong.
6412 */
6413 int
6414parse_compl_arg(value, vallen, complp, argt, compl_arg)
6415 char_u *value;
6416 int vallen;
6417 int *complp;
6418 long *argt;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006419 char_u **compl_arg UNUSED;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006420{
6421 char_u *arg = NULL;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006422# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006423 size_t arglen = 0;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006424# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006425 int i;
6426 int valend = vallen;
6427
6428 /* Look for any argument part - which is the part after any ',' */
6429 for (i = 0; i < vallen; ++i)
6430 {
6431 if (value[i] == ',')
6432 {
6433 arg = &value[i + 1];
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006434# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006435 arglen = vallen - i - 1;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006436# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006437 valend = i;
6438 break;
6439 }
6440 }
6441
6442 for (i = 0; command_complete[i].expand != 0; ++i)
6443 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006444 if ((int)STRLEN(command_complete[i].name) == valend
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006445 && STRNCMP(value, command_complete[i].name, valend) == 0)
6446 {
6447 *complp = command_complete[i].expand;
6448 if (command_complete[i].expand == EXPAND_BUFFERS)
6449 *argt |= BUFNAME;
6450 else if (command_complete[i].expand == EXPAND_DIRECTORIES
6451 || command_complete[i].expand == EXPAND_FILES)
6452 *argt |= XFILE;
6453 break;
6454 }
6455 }
6456
6457 if (command_complete[i].expand == 0)
6458 {
6459 EMSG2(_("E180: Invalid complete value: %s"), value);
6460 return FAIL;
6461 }
6462
6463# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6464 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST
6465 && arg != NULL)
6466# else
6467 if (arg != NULL)
6468# endif
6469 {
6470 EMSG(_("E468: Completion argument only allowed for custom completion"));
6471 return FAIL;
6472 }
6473
6474# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6475 if ((*complp == EXPAND_USER_DEFINED || *complp == EXPAND_USER_LIST)
6476 && arg == NULL)
6477 {
6478 EMSG(_("E467: Custom completion requires a function argument"));
6479 return FAIL;
6480 }
6481
6482 if (arg != NULL)
6483 *compl_arg = vim_strnsave(arg, (int)arglen);
6484# endif
6485 return OK;
6486}
6487#endif
6488
Bram Moolenaar071d4272004-06-13 20:20:40 +00006489 static void
6490ex_colorscheme(eap)
6491 exarg_T *eap;
6492{
Bram Moolenaare6850792010-05-14 15:28:44 +02006493 if (*eap->arg == NUL)
6494 {
6495#ifdef FEAT_EVAL
6496 char_u *expr = vim_strsave((char_u *)"g:colors_name");
6497 char_u *p = NULL;
6498
6499 if (expr != NULL)
6500 {
6501 ++emsg_off;
6502 p = eval_to_string(expr, NULL, FALSE);
6503 --emsg_off;
6504 vim_free(expr);
6505 }
6506 if (p != NULL)
6507 {
6508 MSG(p);
6509 vim_free(p);
6510 }
6511 else
6512 MSG("default");
6513#else
6514 MSG(_("unknown"));
6515#endif
6516 }
6517 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarbe1e9e92012-09-18 16:47:07 +02006518 EMSG2(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006519}
6520
6521 static void
6522ex_highlight(eap)
6523 exarg_T *eap;
6524{
6525 if (*eap->arg == NUL && eap->cmd[2] == '!')
6526 MSG(_("Greetings, Vim user!"));
6527 do_highlight(eap->arg, eap->forceit, FALSE);
6528}
6529
6530
6531/*
6532 * Call this function if we thought we were going to exit, but we won't
6533 * (because of an error). May need to restore the terminal mode.
6534 */
6535 void
6536not_exiting()
6537{
6538 exiting = FALSE;
6539 settmode(TMODE_RAW);
6540}
6541
6542/*
6543 * ":quit": quit current window, quit Vim if closed the last window.
6544 */
6545 static void
6546ex_quit(eap)
6547 exarg_T *eap;
6548{
6549#ifdef FEAT_CMDWIN
6550 if (cmdwin_type != 0)
6551 {
6552 cmdwin_result = Ctrl_C;
6553 return;
6554 }
6555#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006556 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006557 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006558 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006559 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006560 return;
6561 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006562#ifdef FEAT_AUTOCMD
Bram Moolenaar3b53dfb2012-06-06 18:03:07 +02006563 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01006564 /* Refuse to quit when locked or when the buffer in the last window is
Bram Moolenaar362ce482012-06-06 19:02:45 +02006565 * being closed (can only happen in autocommands). */
6566 if (curbuf_locked() || (curbuf->b_nwindows == 1 && curbuf->b_closing))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006567 return;
6568#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006569
6570#ifdef FEAT_NETBEANS_INTG
6571 netbeansForcedQuit = eap->forceit;
6572#endif
6573
6574 /*
6575 * If there are more files or windows we won't exit.
6576 */
6577 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6578 exiting = TRUE;
6579 if ((!P_HID(curbuf)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01006580 && check_changed(curbuf, (p_awa ? CCGD_AW : 0)
6581 | (eap->forceit ? CCGD_FORCEIT : 0)
6582 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006583 || check_more(TRUE, eap->forceit) == FAIL
6584 || (only_one_window() && check_changed_any(eap->forceit)))
6585 {
6586 not_exiting();
6587 }
6588 else
6589 {
6590#ifdef FEAT_WINDOWS
6591 if (only_one_window()) /* quit last window */
6592#endif
6593 getout(0);
6594#ifdef FEAT_WINDOWS
6595# ifdef FEAT_GUI
6596 need_mouse_correct = TRUE;
6597# endif
6598 /* close window; may free buffer */
6599 win_close(curwin, !P_HID(curwin->w_buffer) || eap->forceit);
6600#endif
6601 }
6602}
6603
6604/*
6605 * ":cquit".
6606 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006607 static void
6608ex_cquit(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006609 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006610{
6611 getout(1); /* this does not always pass on the exit code to the Manx
6612 compiler. why? */
6613}
6614
6615/*
6616 * ":qall": try to quit all windows
6617 */
6618 static void
6619ex_quit_all(eap)
6620 exarg_T *eap;
6621{
6622# ifdef FEAT_CMDWIN
6623 if (cmdwin_type != 0)
6624 {
6625 if (eap->forceit)
6626 cmdwin_result = K_XF1; /* ex_window() takes care of this */
6627 else
6628 cmdwin_result = K_XF2;
6629 return;
6630 }
6631# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006632
6633 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006634 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006635 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006636 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006637 return;
6638 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006639#ifdef FEAT_AUTOCMD
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01006640 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
6641 /* Refuse to quit when locked or when the buffer in the last window is
6642 * being closed (can only happen in autocommands). */
6643 if (curbuf_locked() || (curbuf->b_nwindows == 1 && curbuf->b_closing))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006644 return;
6645#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006646
Bram Moolenaar071d4272004-06-13 20:20:40 +00006647 exiting = TRUE;
6648 if (eap->forceit || !check_changed_any(FALSE))
6649 getout(0);
6650 not_exiting();
6651}
6652
Bram Moolenaard9967712006-03-11 21:18:15 +00006653#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006654/*
6655 * ":close": close current window, unless it is the last one
6656 */
6657 static void
6658ex_close(eap)
6659 exarg_T *eap;
6660{
6661# ifdef FEAT_CMDWIN
6662 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02006663 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006664 else
6665# endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006666 if (!text_locked()
6667#ifdef FEAT_AUTOCMD
6668 && !curbuf_locked()
6669#endif
6670 )
Bram Moolenaarf740b292006-02-16 22:11:02 +00006671 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006672}
6673
Bram Moolenaard9967712006-03-11 21:18:15 +00006674# ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006675/*
6676 * ":pclose": Close any preview window.
6677 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006678 static void
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006679ex_pclose(eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006680 exarg_T *eap;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006681{
6682 win_T *win;
6683
6684 for (win = firstwin; win != NULL; win = win->w_next)
6685 if (win->w_p_pvw)
6686 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006687 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006688 break;
6689 }
6690}
Bram Moolenaard9967712006-03-11 21:18:15 +00006691# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006692
Bram Moolenaarf740b292006-02-16 22:11:02 +00006693/*
6694 * Close window "win" and take care of handling closing the last window for a
6695 * modified buffer.
6696 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006697 static void
Bram Moolenaarf740b292006-02-16 22:11:02 +00006698ex_win_close(forceit, win, tp)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006699 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006700 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006701 tabpage_T *tp; /* NULL or the tab page "win" is in */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006702{
6703 int need_hide;
6704 buf_T *buf = win->w_buffer;
6705
6706 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006707 if (need_hide && !P_HID(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006708 {
Bram Moolenaard9967712006-03-11 21:18:15 +00006709# if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006710 if ((p_confirm || cmdmod.confirm) && p_write)
6711 {
6712 dialog_changed(buf, FALSE);
6713 if (buf_valid(buf) && bufIsChanged(buf))
6714 return;
6715 need_hide = FALSE;
6716 }
6717 else
Bram Moolenaard9967712006-03-11 21:18:15 +00006718# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006719 {
6720 EMSG(_(e_nowrtmsg));
6721 return;
6722 }
6723 }
6724
Bram Moolenaard9967712006-03-11 21:18:15 +00006725# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006726 need_mouse_correct = TRUE;
Bram Moolenaard9967712006-03-11 21:18:15 +00006727# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006728
Bram Moolenaar071d4272004-06-13 20:20:40 +00006729 /* free buffer when not hiding it or when it's a scratch buffer */
Bram Moolenaarf740b292006-02-16 22:11:02 +00006730 if (tp == NULL)
6731 win_close(win, !need_hide && !P_HID(buf));
6732 else
6733 win_close_othertab(win, !need_hide && !P_HID(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006734}
6735
Bram Moolenaar071d4272004-06-13 20:20:40 +00006736/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006737 * ":tabclose": close current tab page, unless it is the last one.
6738 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006739 */
6740 static void
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006741ex_tabclose(eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006742 exarg_T *eap;
6743{
Bram Moolenaarf740b292006-02-16 22:11:02 +00006744 tabpage_T *tp;
6745
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006746# ifdef FEAT_CMDWIN
6747 if (cmdwin_type != 0)
6748 cmdwin_result = K_IGNORE;
6749 else
6750# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006751 if (first_tabpage->tp_next == NULL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00006752 EMSG(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00006753 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006754 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006755 if (eap->addr_count > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006756 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006757 tp = find_tabpage((int)eap->line2);
6758 if (tp == NULL)
6759 {
6760 beep_flush();
6761 return;
6762 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00006763 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006764 {
6765 tabpage_close_other(tp, eap->forceit);
6766 return;
6767 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006768 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006769 if (!text_locked()
6770#ifdef FEAT_AUTOCMD
6771 && !curbuf_locked()
6772#endif
6773 )
Bram Moolenaarf740b292006-02-16 22:11:02 +00006774 tabpage_close(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006775 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006776}
6777
6778/*
6779 * ":tabonly": close all tab pages except the current one
6780 */
6781 static void
6782ex_tabonly(eap)
6783 exarg_T *eap;
6784{
6785 tabpage_T *tp;
6786 int done;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006787
6788# ifdef FEAT_CMDWIN
6789 if (cmdwin_type != 0)
6790 cmdwin_result = K_IGNORE;
6791 else
6792# endif
6793 if (first_tabpage->tp_next == NULL)
6794 MSG(_("Already only one tab page"));
6795 else
6796 {
6797 /* Repeat this up to a 1000 times, because autocommands may mess
6798 * up the lists. */
6799 for (done = 0; done < 1000; ++done)
6800 {
6801 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6802 if (tp->tp_topframe != topframe)
6803 {
6804 tabpage_close_other(tp, eap->forceit);
6805 /* if we failed to close it quit */
6806 if (valid_tabpage(tp))
6807 done = 1000;
6808 /* start over, "tp" is now invalid */
6809 break;
6810 }
6811 if (first_tabpage->tp_next == NULL)
6812 break;
6813 }
6814 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006815}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006816
6817/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006818 * Close the current tab page.
6819 */
6820 void
6821tabpage_close(forceit)
6822 int forceit;
6823{
6824 /* First close all the windows but the current one. If that worked then
6825 * close the last window in this tab, that will close it. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006826 if (lastwin != firstwin)
6827 close_others(TRUE, forceit);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006828 if (lastwin == firstwin)
6829 ex_win_close(forceit, curwin, NULL);
6830# ifdef FEAT_GUI
6831 need_mouse_correct = TRUE;
6832# endif
6833}
6834
6835/*
6836 * Close tab page "tp", which is not the current tab page.
6837 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006838 * Also takes care of the tab pages line disappearing when closing the
6839 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00006840 */
6841 void
6842tabpage_close_other(tp, forceit)
6843 tabpage_T *tp;
6844 int forceit;
6845{
6846 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006847 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006848 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006849
6850 /* Limit to 1000 windows, autocommands may add a window while we close
6851 * one. OK, so I'm paranoid... */
6852 while (++done < 1000)
6853 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006854 wp = tp->tp_firstwin;
6855 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006856
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006857 /* Autocommands may delete the tab page under our fingers and we may
6858 * fail to close a window with a modified buffer. */
6859 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006860 break;
6861 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006862
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006863 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006864 if (h != tabline_height())
6865 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006866}
6867
6868/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006869 * ":only".
6870 */
6871 static void
6872ex_only(eap)
6873 exarg_T *eap;
6874{
6875# ifdef FEAT_GUI
6876 need_mouse_correct = TRUE;
6877# endif
6878 close_others(TRUE, eap->forceit);
6879}
6880
6881/*
6882 * ":all" and ":sall".
Bram Moolenaard9967712006-03-11 21:18:15 +00006883 * Also used for ":tab drop file ..." after setting the argument list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006884 */
Bram Moolenaard9967712006-03-11 21:18:15 +00006885 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00006886ex_all(eap)
6887 exarg_T *eap;
6888{
6889 if (eap->addr_count == 0)
6890 eap->line2 = 9999;
Bram Moolenaard9967712006-03-11 21:18:15 +00006891 do_arg_all((int)eap->line2, eap->forceit, eap->cmdidx == CMD_drop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006892}
6893#endif /* FEAT_WINDOWS */
6894
6895 static void
6896ex_hide(eap)
6897 exarg_T *eap;
6898{
6899 if (*eap->arg != NUL && check_nextcmd(eap->arg) == NULL)
6900 eap->errmsg = e_invarg;
6901 else
6902 {
6903 /* ":hide" or ":hide | cmd": hide current window */
6904 eap->nextcmd = check_nextcmd(eap->arg);
6905#ifdef FEAT_WINDOWS
6906 if (!eap->skip)
6907 {
6908# ifdef FEAT_GUI
6909 need_mouse_correct = TRUE;
6910# endif
6911 win_close(curwin, FALSE); /* don't free buffer */
6912 }
6913#endif
6914 }
6915}
6916
6917/*
6918 * ":stop" and ":suspend": Suspend Vim.
6919 */
6920 static void
6921ex_stop(eap)
6922 exarg_T *eap;
6923{
6924 /*
6925 * Disallow suspending for "rvim".
6926 */
6927 if (!check_restricted()
6928#ifdef WIN3264
6929 /*
6930 * Check if external commands are allowed now.
6931 */
6932 && can_end_termcap_mode(TRUE)
6933#endif
6934 )
6935 {
6936 if (!eap->forceit)
6937 autowrite_all();
6938 windgoto((int)Rows - 1, 0);
6939 out_char('\n');
6940 out_flush();
6941 stoptermcap();
6942 out_flush(); /* needed for SUN to restore xterm buffer */
6943#ifdef FEAT_TITLE
6944 mch_restore_title(3); /* restore window titles */
6945#endif
6946 ui_suspend(); /* call machine specific function */
6947#ifdef FEAT_TITLE
6948 maketitle();
6949 resettitle(); /* force updating the title */
6950#endif
6951 starttermcap();
6952 scroll_start(); /* scroll screen before redrawing */
6953 redraw_later_clear();
6954 shell_resized(); /* may have resized window */
6955 }
6956}
6957
6958/*
6959 * ":exit", ":xit" and ":wq": Write file and exit Vim.
6960 */
6961 static void
6962ex_exit(eap)
6963 exarg_T *eap;
6964{
6965#ifdef FEAT_CMDWIN
6966 if (cmdwin_type != 0)
6967 {
6968 cmdwin_result = Ctrl_C;
6969 return;
6970 }
6971#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006972 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006973 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006974 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006975 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006976 return;
6977 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006978#ifdef FEAT_AUTOCMD
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01006979 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
6980 /* Refuse to quit when locked or when the buffer in the last window is
6981 * being closed (can only happen in autocommands). */
6982 if (curbuf_locked() || (curbuf->b_nwindows == 1 && curbuf->b_closing))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006983 return;
6984#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006985
6986 /*
6987 * if more files or windows we won't exit
6988 */
6989 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6990 exiting = TRUE;
6991 if ( ((eap->cmdidx == CMD_wq
6992 || curbufIsChanged())
6993 && do_write(eap) == FAIL)
6994 || check_more(TRUE, eap->forceit) == FAIL
6995 || (only_one_window() && check_changed_any(eap->forceit)))
6996 {
6997 not_exiting();
6998 }
6999 else
7000 {
7001#ifdef FEAT_WINDOWS
7002 if (only_one_window()) /* quit last window, exit Vim */
7003#endif
7004 getout(0);
7005#ifdef FEAT_WINDOWS
7006# ifdef FEAT_GUI
7007 need_mouse_correct = TRUE;
7008# endif
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02007009 /* Quit current window, may free the buffer. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007010 win_close(curwin, !P_HID(curwin->w_buffer));
7011#endif
7012 }
7013}
7014
7015/*
7016 * ":print", ":list", ":number".
7017 */
7018 static void
7019ex_print(eap)
7020 exarg_T *eap;
7021{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007022 if (curbuf->b_ml.ml_flags & ML_EMPTY)
7023 EMSG(_(e_emptybuf));
7024 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007025 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007026 for ( ;!got_int; ui_breakcheck())
7027 {
7028 print_line(eap->line1,
7029 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
7030 || (eap->flags & EXFLAG_NR)),
7031 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
7032 if (++eap->line1 > eap->line2)
7033 break;
7034 out_flush(); /* show one line at a time */
7035 }
7036 setpcmark();
7037 /* put cursor at last line */
7038 curwin->w_cursor.lnum = eap->line2;
7039 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007040 }
7041
Bram Moolenaar071d4272004-06-13 20:20:40 +00007042 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007043}
7044
7045#ifdef FEAT_BYTEOFF
7046 static void
7047ex_goto(eap)
7048 exarg_T *eap;
7049{
7050 goto_byte(eap->line2);
7051}
7052#endif
7053
7054/*
7055 * ":shell".
7056 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007057 static void
7058ex_shell(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007059 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007060{
7061 do_shell(NULL, 0);
7062}
7063
7064#if (defined(FEAT_WINDOWS) && defined(HAVE_DROP_FILE)) \
7065 || (defined(FEAT_GUI_GTK) && defined(FEAT_DND)) \
Bram Moolenaar371baa92005-12-29 22:43:53 +00007066 || defined(FEAT_GUI_MSWIN) \
7067 || defined(FEAT_GUI_MAC) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00007068 || defined(PROTO)
7069
7070/*
7071 * Handle a file drop. The code is here because a drop is *nearly* like an
7072 * :args command, but not quite (we have a list of exact filenames, so we
7073 * don't want to (a) parse a command line, or (b) expand wildcards. So the
7074 * code is very similar to :args and hence needs access to a lot of the static
7075 * functions in this file.
7076 *
7077 * The list should be allocated using alloc(), as should each item in the
7078 * list. This function takes over responsibility for freeing the list.
7079 *
Bram Moolenaar81870892007-11-11 18:17:28 +00007080 * XXX The list is made into the argument list. This is freed using
Bram Moolenaar071d4272004-06-13 20:20:40 +00007081 * FreeWild(), which does a series of vim_free() calls, unless the two defines
7082 * __EMX__ and __ALWAYS_HAS_TRAILING_NUL_POINTER are set. In this case, a
7083 * routine _fnexplodefree() is used. This may cause problems, but as the drop
7084 * file functionality is (currently) not in EMX this is not presently a
7085 * problem.
7086 */
7087 void
7088handle_drop(filec, filev, split)
7089 int filec; /* the number of files dropped */
7090 char_u **filev; /* the list of files dropped */
7091 int split; /* force splitting the window */
7092{
7093 exarg_T ea;
7094 int save_msg_scroll = msg_scroll;
7095
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007096 /* Postpone this while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007097 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007098 return;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007099#ifdef FEAT_AUTOCMD
7100 if (curbuf_locked())
7101 return;
7102#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00007103 /* When the screen is being updated we should not change buffers and
7104 * windows structures, it may cause freed memory to be used. */
7105 if (updating_screen)
7106 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007107
7108 /* Check whether the current buffer is changed. If so, we will need
7109 * to split the current window or data could be lost.
7110 * We don't need to check if the 'hidden' option is set, as in this
7111 * case the buffer won't be lost.
7112 */
7113 if (!P_HID(curbuf) && !split)
7114 {
7115 ++emsg_off;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01007116 split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007117 --emsg_off;
7118 }
7119 if (split)
7120 {
7121# ifdef FEAT_WINDOWS
7122 if (win_split(0, 0) == FAIL)
7123 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007124 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007125
7126 /* When splitting the window, create a new alist. Otherwise the
7127 * existing one is overwritten. */
7128 alist_unlink(curwin->w_alist);
7129 alist_new();
7130# else
7131 return; /* can't split, always fail */
7132# endif
7133 }
7134
7135 /*
7136 * Set up the new argument list.
7137 */
Bram Moolenaar86b68352004-12-27 21:59:20 +00007138 alist_set(ALIST(curwin), filec, filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007139
7140 /*
7141 * Move to the first file.
7142 */
7143 /* Fake up a minimal "next" command for do_argfile() */
7144 vim_memset(&ea, 0, sizeof(ea));
7145 ea.cmd = (char_u *)"next";
7146 do_argfile(&ea, 0);
7147
7148 /* do_ecmd() may set need_start_insertmode, but since we never left Insert
7149 * mode that is not needed here. */
7150 need_start_insertmode = FALSE;
7151
7152 /* Restore msg_scroll, otherwise a following command may cause scrolling
7153 * unexpectedly. The screen will be redrawn by the caller, thus
7154 * msg_scroll being set by displaying a message is irrelevant. */
7155 msg_scroll = save_msg_scroll;
7156}
7157#endif
7158
Bram Moolenaar071d4272004-06-13 20:20:40 +00007159/*
7160 * Clear an argument list: free all file names and reset it to zero entries.
7161 */
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007162 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00007163alist_clear(al)
7164 alist_T *al;
7165{
7166 while (--al->al_ga.ga_len >= 0)
7167 vim_free(AARGLIST(al)[al->al_ga.ga_len].ae_fname);
7168 ga_clear(&al->al_ga);
7169}
7170
7171/*
7172 * Init an argument list.
7173 */
7174 void
7175alist_init(al)
7176 alist_T *al;
7177{
7178 ga_init2(&al->al_ga, (int)sizeof(aentry_T), 5);
7179}
7180
7181#if defined(FEAT_WINDOWS) || defined(PROTO)
7182
7183/*
7184 * Remove a reference from an argument list.
7185 * Ignored when the argument list is the global one.
7186 * If the argument list is no longer used by any window, free it.
7187 */
7188 void
7189alist_unlink(al)
7190 alist_T *al;
7191{
7192 if (al != &global_alist && --al->al_refcount <= 0)
7193 {
7194 alist_clear(al);
7195 vim_free(al);
7196 }
7197}
7198
7199# if defined(FEAT_LISTCMDS) || defined(HAVE_DROP_FILE) || defined(PROTO)
7200/*
7201 * Create a new argument list and use it for the current window.
7202 */
7203 void
7204alist_new()
7205{
7206 curwin->w_alist = (alist_T *)alloc((unsigned)sizeof(alist_T));
7207 if (curwin->w_alist == NULL)
7208 {
7209 curwin->w_alist = &global_alist;
7210 ++global_alist.al_refcount;
7211 }
7212 else
7213 {
7214 curwin->w_alist->al_refcount = 1;
7215 alist_init(curwin->w_alist);
7216 }
7217}
7218# endif
7219#endif
7220
7221#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE) || defined(PROTO)
7222/*
7223 * Expand the file names in the global argument list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00007224 * If "fnum_list" is not NULL, use "fnum_list[fnum_len]" as a list of buffer
7225 * numbers to be re-used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007226 */
7227 void
Bram Moolenaar86b68352004-12-27 21:59:20 +00007228alist_expand(fnum_list, fnum_len)
7229 int *fnum_list;
7230 int fnum_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007231{
7232 char_u **old_arg_files;
Bram Moolenaar86b68352004-12-27 21:59:20 +00007233 int old_arg_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007234 char_u **new_arg_files;
7235 int new_arg_file_count;
7236 char_u *save_p_su = p_su;
7237 int i;
7238
7239 /* Don't use 'suffixes' here. This should work like the shell did the
7240 * expansion. Also, the vimrc file isn't read yet, thus the user
7241 * can't set the options. */
7242 p_su = empty_option;
7243 old_arg_files = (char_u **)alloc((unsigned)(sizeof(char_u *) * GARGCOUNT));
7244 if (old_arg_files != NULL)
7245 {
7246 for (i = 0; i < GARGCOUNT; ++i)
Bram Moolenaar86b68352004-12-27 21:59:20 +00007247 old_arg_files[i] = vim_strsave(GARGLIST[i].ae_fname);
7248 old_arg_count = GARGCOUNT;
7249 if (expand_wildcards(old_arg_count, old_arg_files,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007250 &new_arg_file_count, &new_arg_files,
Bram Moolenaarb5609832011-07-20 15:04:58 +02007251 EW_FILE|EW_NOTFOUND|EW_ADDSLASH|EW_NOERROR) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007252 && new_arg_file_count > 0)
7253 {
Bram Moolenaar86b68352004-12-27 21:59:20 +00007254 alist_set(&global_alist, new_arg_file_count, new_arg_files,
7255 TRUE, fnum_list, fnum_len);
7256 FreeWild(old_arg_count, old_arg_files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007257 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007258 }
7259 p_su = save_p_su;
7260}
7261#endif
7262
7263/*
7264 * Set the argument list for the current window.
7265 * Takes over the allocated files[] and the allocated fnames in it.
7266 */
7267 void
Bram Moolenaar86b68352004-12-27 21:59:20 +00007268alist_set(al, count, files, use_curbuf, fnum_list, fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007269 alist_T *al;
7270 int count;
7271 char_u **files;
7272 int use_curbuf;
Bram Moolenaar86b68352004-12-27 21:59:20 +00007273 int *fnum_list;
7274 int fnum_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007275{
7276 int i;
7277
7278 alist_clear(al);
7279 if (ga_grow(&al->al_ga, count) == OK)
7280 {
7281 for (i = 0; i < count; ++i)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007282 {
7283 if (got_int)
7284 {
7285 /* When adding many buffers this can take a long time. Allow
7286 * interrupting here. */
7287 while (i < count)
7288 vim_free(files[i++]);
7289 break;
7290 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00007291
7292 /* May set buffer name of a buffer previously used for the
7293 * argument list, so that it's re-used by alist_add. */
7294 if (fnum_list != NULL && i < fnum_len)
7295 buf_set_name(fnum_list[i], files[i]);
7296
Bram Moolenaar071d4272004-06-13 20:20:40 +00007297 alist_add(al, files[i], use_curbuf ? 2 : 1);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007298 ui_breakcheck();
7299 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007300 vim_free(files);
7301 }
7302 else
7303 FreeWild(count, files);
7304#ifdef FEAT_WINDOWS
7305 if (al == &global_alist)
7306#endif
7307 arg_had_last = FALSE;
7308}
7309
7310/*
7311 * Add file "fname" to argument list "al".
7312 * "fname" must have been allocated and "al" must have been checked for room.
7313 */
7314 void
7315alist_add(al, fname, set_fnum)
7316 alist_T *al;
7317 char_u *fname;
7318 int set_fnum; /* 1: set buffer number; 2: re-use curbuf */
7319{
7320 if (fname == NULL) /* don't add NULL file names */
7321 return;
7322#ifdef BACKSLASH_IN_FILENAME
7323 slash_adjust(fname);
7324#endif
7325 AARGLIST(al)[al->al_ga.ga_len].ae_fname = fname;
7326 if (set_fnum > 0)
7327 AARGLIST(al)[al->al_ga.ga_len].ae_fnum =
7328 buflist_add(fname, BLN_LISTED | (set_fnum == 2 ? BLN_CURBUF : 0));
7329 ++al->al_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007330}
7331
7332#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
7333/*
7334 * Adjust slashes in file names. Called after 'shellslash' was set.
7335 */
7336 void
7337alist_slash_adjust()
7338{
7339 int i;
7340# ifdef FEAT_WINDOWS
7341 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007342 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007343# endif
7344
7345 for (i = 0; i < GARGCOUNT; ++i)
7346 if (GARGLIST[i].ae_fname != NULL)
7347 slash_adjust(GARGLIST[i].ae_fname);
7348# ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00007349 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007350 if (wp->w_alist != &global_alist)
7351 for (i = 0; i < WARGCOUNT(wp); ++i)
7352 if (WARGLIST(wp)[i].ae_fname != NULL)
7353 slash_adjust(WARGLIST(wp)[i].ae_fname);
7354# endif
7355}
7356#endif
7357
7358/*
7359 * ":preserve".
7360 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007361 static void
7362ex_preserve(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007363 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007364{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007365 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007366 ml_preserve(curbuf, TRUE);
7367}
7368
7369/*
7370 * ":recover".
7371 */
7372 static void
7373ex_recover(eap)
7374 exarg_T *eap;
7375{
7376 /* Set recoverymode right away to avoid the ATTENTION prompt. */
7377 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01007378 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
7379 | CCGD_MULTWIN
7380 | (eap->forceit ? CCGD_FORCEIT : 0)
7381 | CCGD_EXCMD)
7382
Bram Moolenaar071d4272004-06-13 20:20:40 +00007383 && (*eap->arg == NUL
7384 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
7385 ml_recover();
7386 recoverymode = FALSE;
7387}
7388
7389/*
7390 * Command modifier used in a wrong way.
7391 */
7392 static void
7393ex_wrongmodifier(eap)
7394 exarg_T *eap;
7395{
7396 eap->errmsg = e_invcmd;
7397}
7398
7399#ifdef FEAT_WINDOWS
7400/*
7401 * :sview [+command] file split window with new file, read-only
7402 * :split [[+command] file] split window with current or new file
7403 * :vsplit [[+command] file] split window vertically with current or new file
7404 * :new [[+command] file] split window with no or new file
7405 * :vnew [[+command] file] split vertically window with no or new file
7406 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007407 *
7408 * :tabedit open new Tab page with empty window
7409 * :tabedit [+command] file open new Tab page and edit "file"
7410 * :tabnew [[+command] file] just like :tabedit
7411 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007412 */
7413 void
7414ex_splitview(eap)
7415 exarg_T *eap;
7416{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007417 win_T *old_curwin = curwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007418# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007419 char_u *fname = NULL;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007420# endif
7421# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007422 int browse_flag = cmdmod.browse;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007423# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007424
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007425# ifndef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007426 if (eap->cmdidx == CMD_vsplit || eap->cmdidx == CMD_vnew)
7427 {
7428 ex_ni(eap);
7429 return;
7430 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007431# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007432
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007433# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007434 need_mouse_correct = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007435# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007436
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007437# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00007438 /* A ":split" in the quickfix window works like ":new". Don't want two
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007439 * quickfix windows. But it's OK when doing ":tab split". */
7440 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007441 {
7442 if (eap->cmdidx == CMD_split)
7443 eap->cmdidx = CMD_new;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007444# ifdef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007445 if (eap->cmdidx == CMD_vsplit)
7446 eap->cmdidx = CMD_vnew;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007447# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007448 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007449# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007450
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007451# ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007452 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007453 {
7454 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
7455 FNAME_MESS, TRUE, curbuf->b_ffname);
7456 if (fname == NULL)
7457 goto theend;
7458 eap->arg = fname;
7459 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007460# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007461 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007462# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007463# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007464# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007465 if (cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007466# ifdef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007467 && eap->cmdidx != CMD_vnew
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007468# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007469 && eap->cmdidx != CMD_new)
7470 {
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007471# ifdef FEAT_AUTOCMD
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007472 if (
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007473# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007474 !gui.in_use &&
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007475# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007476 au_has_group((char_u *)"FileExplorer"))
7477 {
7478 /* No browsing supported but we do have the file explorer:
7479 * Edit the directory. */
7480 if (*eap->arg == NUL || !mch_isdir(eap->arg))
7481 eap->arg = (char_u *)".";
7482 }
7483 else
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007484# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007485 {
7486 fname = do_browse(0, (char_u *)_("Edit File in new window"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007487 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007488 if (fname == NULL)
7489 goto theend;
7490 eap->arg = fname;
7491 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007492 }
7493 cmdmod.browse = FALSE; /* Don't browse again in do_ecmd(). */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007494# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007495
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007496 /*
7497 * Either open new tab page or split the window.
7498 */
7499 if (eap->cmdidx == CMD_tabedit
7500 || eap->cmdidx == CMD_tabfind
7501 || eap->cmdidx == CMD_tabnew)
7502 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00007503 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
7504 : eap->addr_count == 0 ? 0
7505 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007506 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00007507 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007508
7509 /* set the alternate buffer for the window we came from */
7510 if (curwin != old_curwin
7511 && win_valid(old_curwin)
7512 && old_curwin->w_buffer != curbuf
7513 && !cmdmod.keepalt)
7514 old_curwin->w_alt_fnum = curbuf->b_fnum;
7515 }
7516 }
7517 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007518 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
7519 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007520# ifdef FEAT_SCROLLBIND
Bram Moolenaar071d4272004-06-13 20:20:40 +00007521 /* Reset 'scrollbind' when editing another file, but keep it when
7522 * doing ":split" without arguments. */
7523 if (*eap->arg != NUL
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007524# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007525 || cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007526# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007527 )
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007528 {
7529 RESET_BINDING(curwin);
7530 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007531 else
7532 do_check_scrollbind(FALSE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007533# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007534 do_exedit(eap, old_curwin);
7535 }
7536
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007537# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007538 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007539# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007540
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007541# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007542theend:
7543 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007544# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007545}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007546
7547/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007548 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007549 */
7550 void
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007551tabpage_new()
7552{
7553 exarg_T ea;
7554
7555 vim_memset(&ea, 0, sizeof(ea));
7556 ea.cmdidx = CMD_tabnew;
7557 ea.cmd = (char_u *)"tabn";
7558 ea.arg = (char_u *)"";
7559 ex_splitview(&ea);
7560}
7561
7562/*
7563 * :tabnext command
7564 */
7565 static void
7566ex_tabnext(eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007567 exarg_T *eap;
7568{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007569 switch (eap->cmdidx)
7570 {
7571 case CMD_tabfirst:
7572 case CMD_tabrewind:
7573 goto_tabpage(1);
7574 break;
7575 case CMD_tablast:
7576 goto_tabpage(9999);
7577 break;
7578 case CMD_tabprevious:
7579 case CMD_tabNext:
7580 goto_tabpage(eap->addr_count == 0 ? -1 : -(int)eap->line2);
7581 break;
7582 default: /* CMD_tabnext */
7583 goto_tabpage(eap->addr_count == 0 ? 0 : (int)eap->line2);
7584 break;
7585 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007586}
7587
7588/*
7589 * :tabmove command
7590 */
7591 static void
7592ex_tabmove(eap)
7593 exarg_T *eap;
7594{
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02007595 int tab_number = 9999;
7596
7597 if (eap->arg && *eap->arg != NUL)
7598 {
7599 char_u *p = eap->arg;
7600 int relative = 0; /* argument +N/-N means: move N places to the
7601 * right/left relative to the current position. */
7602
7603 if (*eap->arg == '-')
7604 {
7605 relative = -1;
7606 p = eap->arg + 1;
7607 }
7608 else if (*eap->arg == '+')
7609 {
7610 relative = 1;
7611 p = eap->arg + 1;
7612 }
7613 else
7614 p = eap->arg;
7615
7616 if (p == skipdigits(p))
7617 {
7618 /* No numbers as argument. */
7619 eap->errmsg = e_invarg;
7620 return;
7621 }
7622
7623 tab_number = getdigits(&p);
7624 if (relative != 0)
7625 tab_number = tab_number * relative + tabpage_index(curtab) - 1;;
7626 }
7627 else if (eap->addr_count != 0)
7628 tab_number = eap->line2;
7629
7630 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007631}
7632
7633/*
7634 * :tabs command: List tabs and their contents.
7635 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007636 static void
7637ex_tabs(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007638 exarg_T *eap UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007639{
7640 tabpage_T *tp;
7641 win_T *wp;
7642 int tabcount = 1;
7643
7644 msg_start();
7645 msg_scroll = TRUE;
7646 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
7647 {
7648 msg_putchar('\n');
7649 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
7650 msg_outtrans_attr(IObuff, hl_attr(HLF_T));
7651 out_flush(); /* output one line at a time */
7652 ui_breakcheck();
7653
Bram Moolenaar030f0df2006-02-21 22:02:53 +00007654 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007655 wp = firstwin;
7656 else
7657 wp = tp->tp_firstwin;
7658 for ( ; wp != NULL && !got_int; wp = wp->w_next)
7659 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007660 msg_putchar('\n');
7661 msg_putchar(wp == curwin ? '>' : ' ');
7662 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007663 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
7664 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007665 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02007666 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007667 else
7668 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
7669 IObuff, IOSIZE, TRUE);
7670 msg_outtrans(IObuff);
7671 out_flush(); /* output one line at a time */
7672 ui_breakcheck();
7673 }
7674 }
7675}
7676
7677#endif /* FEAT_WINDOWS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007678
7679/*
7680 * ":mode": Set screen mode.
7681 * If no argument given, just get the screen size and redraw.
7682 */
7683 static void
7684ex_mode(eap)
7685 exarg_T *eap;
7686{
7687 if (*eap->arg == NUL)
7688 shell_resized();
7689 else
7690 mch_screenmode(eap->arg);
7691}
7692
7693#ifdef FEAT_WINDOWS
7694/*
7695 * ":resize".
7696 * set, increment or decrement current window height
7697 */
7698 static void
7699ex_resize(eap)
7700 exarg_T *eap;
7701{
7702 int n;
7703 win_T *wp = curwin;
7704
7705 if (eap->addr_count > 0)
7706 {
7707 n = eap->line2;
7708 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
7709 ;
7710 }
7711
7712#ifdef FEAT_GUI
7713 need_mouse_correct = TRUE;
7714#endif
7715 n = atol((char *)eap->arg);
7716#ifdef FEAT_VERTSPLIT
7717 if (cmdmod.split & WSP_VERT)
7718 {
7719 if (*eap->arg == '-' || *eap->arg == '+')
7720 n += W_WIDTH(curwin);
7721 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
7722 n = 9999;
7723 win_setwidth_win((int)n, wp);
7724 }
7725 else
7726#endif
7727 {
7728 if (*eap->arg == '-' || *eap->arg == '+')
7729 n += curwin->w_height;
7730 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
7731 n = 9999;
7732 win_setheight_win((int)n, wp);
7733 }
7734}
7735#endif
7736
7737/*
7738 * ":find [+command] <file>" command.
7739 */
7740 static void
7741ex_find(eap)
7742 exarg_T *eap;
7743{
7744#ifdef FEAT_SEARCHPATH
7745 char_u *fname;
7746 int count;
7747
7748 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
7749 TRUE, curbuf->b_ffname);
7750 if (eap->addr_count > 0)
7751 {
7752 /* Repeat finding the file "count" times. This matters when it
7753 * appears several times in the path. */
7754 count = eap->line2;
7755 while (fname != NULL && --count > 0)
7756 {
7757 vim_free(fname);
7758 fname = find_file_in_path(NULL, 0, FNAME_MESS,
7759 FALSE, curbuf->b_ffname);
7760 }
7761 }
7762
7763 if (fname != NULL)
7764 {
7765 eap->arg = fname;
7766#endif
7767 do_exedit(eap, NULL);
7768#ifdef FEAT_SEARCHPATH
7769 vim_free(fname);
7770 }
7771#endif
7772}
7773
7774/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00007775 * ":open" simulation: for now just work like ":visual".
7776 */
7777 static void
7778ex_open(eap)
7779 exarg_T *eap;
7780{
7781 regmatch_T regmatch;
7782 char_u *p;
7783
7784 curwin->w_cursor.lnum = eap->line2;
7785 beginline(BL_SOL | BL_FIX);
7786 if (*eap->arg == '/')
7787 {
7788 /* ":open /pattern/": put cursor in column found with pattern */
7789 ++eap->arg;
7790 p = skip_regexp(eap->arg, '/', p_magic, NULL);
7791 *p = NUL;
7792 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
7793 if (regmatch.regprog != NULL)
7794 {
7795 regmatch.rm_ic = p_ic;
7796 p = ml_get_curline();
7797 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007798 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007799 else
7800 EMSG(_(e_nomatch));
Bram Moolenaar473de612013-06-08 18:19:48 +02007801 vim_regfree(regmatch.regprog);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007802 }
7803 /* Move to the NUL, ignore any other arguments. */
7804 eap->arg += STRLEN(eap->arg);
7805 }
7806 check_cursor();
7807
7808 eap->cmdidx = CMD_visual;
7809 do_exedit(eap, NULL);
7810}
7811
7812/*
7813 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007814 */
7815 static void
7816ex_edit(eap)
7817 exarg_T *eap;
7818{
7819 do_exedit(eap, NULL);
7820}
7821
7822/*
7823 * ":edit <file>" command and alikes.
7824 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007825 void
7826do_exedit(eap, old_curwin)
7827 exarg_T *eap;
7828 win_T *old_curwin; /* curwin before doing a split or NULL */
7829{
7830 int n;
7831#ifdef FEAT_WINDOWS
7832 int need_hide;
7833#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00007834 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007835
7836 /*
7837 * ":vi" command ends Ex mode.
7838 */
7839 if (exmode_active && (eap->cmdidx == CMD_visual
7840 || eap->cmdidx == CMD_view))
7841 {
7842 exmode_active = FALSE;
7843 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007844 {
7845 /* Special case: ":global/pat/visual\NLvi-commands" */
7846 if (global_busy)
7847 {
7848 int rd = RedrawingDisabled;
7849 int nwr = no_wait_return;
7850 int ms = msg_scroll;
7851#ifdef FEAT_GUI
7852 int he = hold_gui_events;
7853#endif
7854
7855 if (eap->nextcmd != NULL)
7856 {
7857 stuffReadbuff(eap->nextcmd);
7858 eap->nextcmd = NULL;
7859 }
7860
7861 if (exmode_was != EXMODE_VIM)
7862 settmode(TMODE_RAW);
7863 RedrawingDisabled = 0;
7864 no_wait_return = 0;
7865 need_wait_return = FALSE;
7866 msg_scroll = 0;
7867#ifdef FEAT_GUI
7868 hold_gui_events = 0;
7869#endif
7870 must_redraw = CLEAR;
7871
7872 main_loop(FALSE, TRUE);
7873
7874 RedrawingDisabled = rd;
7875 no_wait_return = nwr;
7876 msg_scroll = ms;
7877#ifdef FEAT_GUI
7878 hold_gui_events = he;
7879#endif
7880 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007881 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007882 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007883 }
7884
7885 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007886 || eap->cmdidx == CMD_tabnew
7887 || eap->cmdidx == CMD_tabedit
Bram Moolenaar071d4272004-06-13 20:20:40 +00007888#ifdef FEAT_VERTSPLIT
7889 || eap->cmdidx == CMD_vnew
7890#endif
7891 ) && *eap->arg == NUL)
7892 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007893 /* ":new" or ":tabnew" without argument: edit an new empty buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007894 setpcmark();
7895 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007896 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
7897 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007898 }
7899 else if ((eap->cmdidx != CMD_split
7900#ifdef FEAT_VERTSPLIT
7901 && eap->cmdidx != CMD_vsplit
7902#endif
7903 )
7904 || *eap->arg != NUL
7905#ifdef FEAT_BROWSE
7906 || cmdmod.browse
7907#endif
7908 )
7909 {
Bram Moolenaar5555acc2006-04-07 21:33:12 +00007910#ifdef FEAT_AUTOCMD
7911 /* Can't edit another file when "curbuf_lock" is set. Only ":edit"
7912 * can bring us here, others are stopped earlier. */
7913 if (*eap->arg != NUL && curbuf_locked())
7914 return;
7915#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007916 n = readonlymode;
7917 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
7918 readonlymode = TRUE;
7919 else if (eap->cmdidx == CMD_enew)
7920 readonlymode = FALSE; /* 'readonly' doesn't make sense in an
7921 empty buffer */
7922 setpcmark();
7923 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
7924 NULL, eap,
7925 /* ":edit" goes to first line if Vi compatible */
7926 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
7927 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
7928 ? ECMD_ONE : eap->do_ecmd_lnum,
7929 (P_HID(curbuf) ? ECMD_HIDE : 0)
7930 + (eap->forceit ? ECMD_FORCEIT : 0)
7931#ifdef FEAT_LISTCMDS
7932 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
7933#endif
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007934 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007935 {
7936 /* Editing the file failed. If the window was split, close it. */
7937#ifdef FEAT_WINDOWS
7938 if (old_curwin != NULL)
7939 {
7940 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
7941 if (!need_hide || P_HID(curbuf))
7942 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007943# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7944 cleanup_T cs;
7945
7946 /* Reset the error/interrupt/exception state here so that
7947 * aborting() returns FALSE when closing a window. */
7948 enter_cleanup(&cs);
7949# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007950# ifdef FEAT_GUI
7951 need_mouse_correct = TRUE;
7952# endif
7953 win_close(curwin, !need_hide && !P_HID(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007954
7955# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7956 /* Restore the error/interrupt/exception state if not
7957 * discarded by a new aborting error, interrupt, or
7958 * uncaught exception. */
7959 leave_cleanup(&cs);
7960# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007961 }
7962 }
7963#endif
7964 }
7965 else if (readonlymode && curbuf->b_nwindows == 1)
7966 {
7967 /* When editing an already visited buffer, 'readonly' won't be set
7968 * but the previous value is kept. With ":view" and ":sview" we
7969 * want the file to be readonly, except when another window is
7970 * editing the same buffer. */
7971 curbuf->b_p_ro = TRUE;
7972 }
7973 readonlymode = n;
7974 }
7975 else
7976 {
7977 if (eap->do_ecmd_cmd != NULL)
7978 do_cmdline_cmd(eap->do_ecmd_cmd);
7979#ifdef FEAT_TITLE
7980 n = curwin->w_arg_idx_invalid;
7981#endif
7982 check_arg_idx(curwin);
7983#ifdef FEAT_TITLE
7984 if (n != curwin->w_arg_idx_invalid)
7985 maketitle();
7986#endif
7987 }
7988
7989#ifdef FEAT_WINDOWS
7990 /*
7991 * if ":split file" worked, set alternate file name in old window to new
7992 * file
7993 */
7994 if (old_curwin != NULL
7995 && *eap->arg != NUL
7996 && curwin != old_curwin
7997 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00007998 && old_curwin->w_buffer != curbuf
7999 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008000 old_curwin->w_alt_fnum = curbuf->b_fnum;
8001#endif
8002
8003 ex_no_reprint = TRUE;
8004}
8005
8006#ifndef FEAT_GUI
8007/*
8008 * ":gui" and ":gvim" when there is no GUI.
8009 */
8010 static void
8011ex_nogui(eap)
8012 exarg_T *eap;
8013{
8014 eap->errmsg = e_nogvim;
8015}
8016#endif
8017
8018#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
8019 static void
8020ex_tearoff(eap)
8021 exarg_T *eap;
8022{
8023 gui_make_tearoff(eap->arg);
8024}
8025#endif
8026
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008027#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008028 static void
8029ex_popup(eap)
8030 exarg_T *eap;
8031{
Bram Moolenaar97409f12005-07-08 22:17:29 +00008032 gui_make_popup(eap->arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008033}
8034#endif
8035
Bram Moolenaar071d4272004-06-13 20:20:40 +00008036 static void
8037ex_swapname(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008038 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008039{
8040 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
8041 MSG(_("No swap file"));
8042 else
8043 msg(curbuf->b_ml.ml_mfp->mf_fname);
8044}
8045
8046/*
8047 * ":syncbind" forces all 'scrollbind' windows to have the same relative
8048 * offset.
8049 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
8050 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008051 static void
8052ex_syncbind(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008053 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008054{
8055#ifdef FEAT_SCROLLBIND
8056 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008057 win_T *save_curwin = curwin;
8058 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008059 long topline;
8060 long y;
8061 linenr_T old_linenr = curwin->w_cursor.lnum;
8062
8063 setpcmark();
8064
8065 /*
8066 * determine max topline
8067 */
8068 if (curwin->w_p_scb)
8069 {
8070 topline = curwin->w_topline;
8071 for (wp = firstwin; wp; wp = wp->w_next)
8072 {
8073 if (wp->w_p_scb && wp->w_buffer)
8074 {
8075 y = wp->w_buffer->b_ml.ml_line_count - p_so;
8076 if (topline > y)
8077 topline = y;
8078 }
8079 }
8080 if (topline < 1)
8081 topline = 1;
8082 }
8083 else
8084 {
8085 topline = 1;
8086 }
8087
8088
8089 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008090 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008091 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008092 for (curwin = firstwin; curwin; curwin = curwin->w_next)
8093 {
8094 if (curwin->w_p_scb)
8095 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008096 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008097 y = topline - curwin->w_topline;
8098 if (y > 0)
8099 scrollup(y, TRUE);
8100 else
8101 scrolldown(-y, TRUE);
8102 curwin->w_scbind_pos = topline;
8103 redraw_later(VALID);
8104 cursor_correct();
8105#ifdef FEAT_WINDOWS
8106 curwin->w_redr_status = TRUE;
8107#endif
8108 }
8109 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008110 curwin = save_curwin;
8111 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008112 if (curwin->w_p_scb)
8113 {
8114 did_syncbind = TRUE;
8115 checkpcmark();
8116 if (old_linenr != curwin->w_cursor.lnum)
8117 {
8118 char_u ctrl_o[2];
8119
8120 ctrl_o[0] = Ctrl_O;
8121 ctrl_o[1] = 0;
8122 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
8123 }
8124 }
8125#endif
8126}
8127
8128
8129 static void
8130ex_read(eap)
8131 exarg_T *eap;
8132{
Bram Moolenaardf177f62005-02-22 08:39:57 +00008133 int i;
8134 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
8135 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008136
8137 if (eap->usefilter) /* :r!cmd */
8138 do_bang(1, eap, FALSE, FALSE, TRUE);
8139 else
8140 {
8141 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
8142 return;
8143
8144#ifdef FEAT_BROWSE
8145 if (cmdmod.browse)
8146 {
8147 char_u *browseFile;
8148
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008149 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008150 NULL, NULL, NULL, curbuf);
8151 if (browseFile != NULL)
8152 {
8153 i = readfile(browseFile, NULL,
8154 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8155 vim_free(browseFile);
8156 }
8157 else
8158 i = OK;
8159 }
8160 else
8161#endif
8162 if (*eap->arg == NUL)
8163 {
8164 if (check_fname() == FAIL) /* check for no file name */
8165 return;
8166 i = readfile(curbuf->b_ffname, curbuf->b_fname,
8167 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8168 }
8169 else
8170 {
8171 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
8172 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
8173 i = readfile(eap->arg, NULL,
8174 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8175
8176 }
8177 if (i == FAIL)
8178 {
8179#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
8180 if (!aborting())
8181#endif
8182 EMSG2(_(e_notopen), eap->arg);
8183 }
8184 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008185 {
8186 if (empty && exmode_active)
8187 {
8188 /* Delete the empty line that remains. Historically ex does
8189 * this but vi doesn't. */
8190 if (eap->line2 == 0)
8191 lnum = curbuf->b_ml.ml_line_count;
8192 else
8193 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008194 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008195 {
8196 ml_delete(lnum, FALSE);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008197 if (curwin->w_cursor.lnum > 1
8198 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008199 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00008200 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008201 }
8202 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008203 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008204 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008205 }
8206}
8207
Bram Moolenaarea408852005-06-25 22:49:46 +00008208static char_u *prev_dir = NULL;
8209
8210#if defined(EXITFREE) || defined(PROTO)
8211 void
8212free_cd_dir()
8213{
8214 vim_free(prev_dir);
Bram Moolenaara0174af2008-01-02 20:08:25 +00008215 prev_dir = NULL;
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00008216
8217 vim_free(globaldir);
8218 globaldir = NULL;
Bram Moolenaarea408852005-06-25 22:49:46 +00008219}
8220#endif
8221
Bram Moolenaarf4258302013-06-02 18:20:17 +02008222/*
8223 * Deal with the side effects of changing the current directory.
8224 * When "local" is TRUE then this was after an ":lcd" command.
8225 */
8226 void
8227post_chdir(local)
8228 int local;
8229{
8230 vim_free(curwin->w_localdir);
Bram Moolenaarbd2dc342014-01-10 15:53:13 +01008231 curwin->w_localdir = NULL;
Bram Moolenaarf4258302013-06-02 18:20:17 +02008232 if (local)
8233 {
8234 /* If still in global directory, need to remember current
8235 * directory as global directory. */
8236 if (globaldir == NULL && prev_dir != NULL)
8237 globaldir = vim_strsave(prev_dir);
8238 /* Remember this local directory for the window. */
8239 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8240 curwin->w_localdir = vim_strsave(NameBuff);
8241 }
8242 else
8243 {
8244 /* We are now in the global directory, no need to remember its
8245 * name. */
8246 vim_free(globaldir);
8247 globaldir = NULL;
Bram Moolenaarf4258302013-06-02 18:20:17 +02008248 }
8249
8250 shorten_fnames(TRUE);
8251}
8252
Bram Moolenaarea408852005-06-25 22:49:46 +00008253
Bram Moolenaar071d4272004-06-13 20:20:40 +00008254/*
8255 * ":cd", ":lcd", ":chdir" and ":lchdir".
8256 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00008257 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00008258ex_cd(eap)
8259 exarg_T *eap;
8260{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008261 char_u *new_dir;
8262 char_u *tofree;
8263
8264 new_dir = eap->arg;
8265#if !defined(UNIX) && !defined(VMS)
8266 /* for non-UNIX ":cd" means: print current directory */
8267 if (*new_dir == NUL)
8268 ex_pwd(NULL);
8269 else
8270#endif
8271 {
Bram Moolenaar8e8fe9b2009-03-11 14:37:32 +00008272#ifdef FEAT_AUTOCMD
8273 if (allbuf_locked())
8274 return;
8275#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00008276 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged()
8277 && !eap->forceit)
8278 {
Bram Moolenaar81870892007-11-11 18:17:28 +00008279 EMSG(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00008280 return;
8281 }
8282
Bram Moolenaar071d4272004-06-13 20:20:40 +00008283 /* ":cd -": Change to previous directory */
8284 if (STRCMP(new_dir, "-") == 0)
8285 {
8286 if (prev_dir == NULL)
8287 {
8288 EMSG(_("E186: No previous directory"));
8289 return;
8290 }
8291 new_dir = prev_dir;
8292 }
8293
8294 /* Save current directory for next ":cd -" */
8295 tofree = prev_dir;
8296 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8297 prev_dir = vim_strsave(NameBuff);
8298 else
8299 prev_dir = NULL;
8300
8301#if defined(UNIX) || defined(VMS)
8302 /* for UNIX ":cd" means: go to home directory */
8303 if (*new_dir == NUL)
8304 {
8305 /* use NameBuff for home directory name */
8306# ifdef VMS
8307 char_u *p;
8308
8309 p = mch_getenv((char_u *)"SYS$LOGIN");
8310 if (p == NULL || *p == NUL) /* empty is the same as not set */
8311 NameBuff[0] = NUL;
8312 else
Bram Moolenaarb6356332005-07-18 21:40:44 +00008313 vim_strncpy(NameBuff, p, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008314# else
8315 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
8316# endif
8317 new_dir = NameBuff;
8318 }
8319#endif
8320 if (new_dir == NULL || vim_chdir(new_dir))
8321 EMSG(_(e_failed));
8322 else
8323 {
Bram Moolenaarf4258302013-06-02 18:20:17 +02008324 post_chdir(eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008325
8326 /* Echo the new current directory if the command was typed. */
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00008327 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008328 ex_pwd(eap);
8329 }
8330 vim_free(tofree);
8331 }
8332}
8333
8334/*
8335 * ":pwd".
8336 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008337 static void
8338ex_pwd(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008339 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008340{
8341 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8342 {
8343#ifdef BACKSLASH_IN_FILENAME
8344 slash_adjust(NameBuff);
8345#endif
8346 msg(NameBuff);
8347 }
8348 else
8349 EMSG(_("E187: Unknown"));
8350}
8351
8352/*
8353 * ":=".
8354 */
8355 static void
8356ex_equal(eap)
8357 exarg_T *eap;
8358{
8359 smsg((char_u *)"%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008360 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008361}
8362
8363 static void
8364ex_sleep(eap)
8365 exarg_T *eap;
8366{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008367 int n;
8368 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008369
8370 if (cursor_valid())
8371 {
8372 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
8373 if (n >= 0)
Bram Moolenaar10395d82014-02-05 22:46:52 +01008374 windgoto((int)n, W_WINCOL(curwin) + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008375 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008376
8377 len = eap->line2;
8378 switch (*eap->arg)
8379 {
8380 case 'm': break;
8381 case NUL: len *= 1000L; break;
8382 default: EMSG2(_(e_invarg2), eap->arg); return;
8383 }
8384 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008385}
8386
8387/*
8388 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
8389 */
8390 void
8391do_sleep(msec)
8392 long msec;
8393{
8394 long done;
8395
8396 cursor_on();
8397 out_flush();
8398 for (done = 0; !got_int && done < msec; done += 1000L)
8399 {
8400 ui_delay(msec - done > 1000L ? 1000L : msec - done, TRUE);
8401 ui_breakcheck();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02008402#ifdef FEAT_NETBEANS_INTG
8403 /* Process the netbeans messages that may have been received in the
8404 * call to ui_breakcheck() when the GUI is in use. This may occur when
8405 * running a test case. */
8406 netbeans_parse_messages();
8407#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008408 }
8409}
8410
8411 static void
8412do_exmap(eap, isabbrev)
8413 exarg_T *eap;
8414 int isabbrev;
8415{
8416 int mode;
8417 char_u *cmdp;
8418
8419 cmdp = eap->cmd;
8420 mode = get_map_mode(&cmdp, eap->forceit || isabbrev);
8421
8422 switch (do_map((*cmdp == 'n') ? 2 : (*cmdp == 'u'),
8423 eap->arg, mode, isabbrev))
8424 {
8425 case 1: EMSG(_(e_invarg));
8426 break;
8427 case 2: EMSG(isabbrev ? _(e_noabbr) : _(e_nomap));
8428 break;
8429 }
8430}
8431
8432/*
8433 * ":winsize" command (obsolete).
8434 */
8435 static void
8436ex_winsize(eap)
8437 exarg_T *eap;
8438{
8439 int w, h;
8440 char_u *arg = eap->arg;
8441 char_u *p;
8442
8443 w = getdigits(&arg);
8444 arg = skipwhite(arg);
8445 p = arg;
8446 h = getdigits(&arg);
8447 if (*p != NUL && *arg == NUL)
8448 set_shellsize(w, h, TRUE);
8449 else
8450 EMSG(_("E465: :winsize requires two number arguments"));
8451}
8452
8453#ifdef FEAT_WINDOWS
8454 static void
8455ex_wincmd(eap)
8456 exarg_T *eap;
8457{
8458 int xchar = NUL;
8459 char_u *p;
8460
8461 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
8462 {
8463 /* CTRL-W g and CTRL-W CTRL-G have an extra command character */
8464 if (eap->arg[1] == NUL)
8465 {
8466 EMSG(_(e_invarg));
8467 return;
8468 }
8469 xchar = eap->arg[1];
8470 p = eap->arg + 2;
8471 }
8472 else
8473 p = eap->arg + 1;
8474
8475 eap->nextcmd = check_nextcmd(p);
8476 p = skipwhite(p);
8477 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
8478 EMSG(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02008479 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008480 {
8481 /* Pass flags on for ":vertical wincmd ]". */
8482 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008483 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008484 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
8485 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008486 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008487 }
8488}
8489#endif
8490
Bram Moolenaar843ee412004-06-30 16:16:41 +00008491#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008492/*
8493 * ":winpos".
8494 */
8495 static void
8496ex_winpos(eap)
8497 exarg_T *eap;
8498{
8499 int x, y;
8500 char_u *arg = eap->arg;
8501 char_u *p;
8502
8503 if (*arg == NUL)
8504 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00008505# if defined(FEAT_GUI) || defined(MSWIN)
8506# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008507 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00008508# else
8509 if (mch_get_winpos(&x, &y) != FAIL)
8510# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008511 {
8512 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
8513 msg(IObuff);
8514 }
8515 else
8516# endif
8517 EMSG(_("E188: Obtaining window position not implemented for this platform"));
8518 }
8519 else
8520 {
8521 x = getdigits(&arg);
8522 arg = skipwhite(arg);
8523 p = arg;
8524 y = getdigits(&arg);
8525 if (*p == NUL || *arg != NUL)
8526 {
8527 EMSG(_("E466: :winpos requires two number arguments"));
8528 return;
8529 }
8530# ifdef FEAT_GUI
8531 if (gui.in_use)
8532 gui_mch_set_winpos(x, y);
8533 else if (gui.starting)
8534 {
8535 /* Remember the coordinates for when the window is opened. */
8536 gui_win_x = x;
8537 gui_win_y = y;
8538 }
8539# ifdef HAVE_TGETENT
8540 else
8541# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00008542# else
8543# ifdef MSWIN
8544 mch_set_winpos(x, y);
8545# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008546# endif
8547# ifdef HAVE_TGETENT
8548 if (*T_CWP)
8549 term_set_winpos(x, y);
8550# endif
8551 }
8552}
8553#endif
8554
8555/*
8556 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
8557 */
8558 static void
8559ex_operators(eap)
8560 exarg_T *eap;
8561{
8562 oparg_T oa;
8563
8564 clear_oparg(&oa);
8565 oa.regname = eap->regname;
8566 oa.start.lnum = eap->line1;
8567 oa.end.lnum = eap->line2;
8568 oa.line_count = eap->line2 - eap->line1 + 1;
8569 oa.motion_type = MLINE;
8570#ifdef FEAT_VIRTUALEDIT
8571 virtual_op = FALSE;
8572#endif
8573 if (eap->cmdidx != CMD_yank) /* position cursor for undo */
8574 {
8575 setpcmark();
8576 curwin->w_cursor.lnum = eap->line1;
8577 beginline(BL_SOL | BL_FIX);
8578 }
8579
Bram Moolenaard07c6e12013-11-21 14:21:40 +01008580#if defined(FEAT_VISUAL)
8581 if (VIsual_active)
8582 end_visual_mode();
8583#endif
8584
Bram Moolenaar071d4272004-06-13 20:20:40 +00008585 switch (eap->cmdidx)
8586 {
8587 case CMD_delete:
8588 oa.op_type = OP_DELETE;
8589 op_delete(&oa);
8590 break;
8591
8592 case CMD_yank:
8593 oa.op_type = OP_YANK;
8594 (void)op_yank(&oa, FALSE, TRUE);
8595 break;
8596
8597 default: /* CMD_rshift or CMD_lshift */
Bram Moolenaar6e707362013-06-14 19:15:58 +02008598 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00008599#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02008600 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
8601#else
8602 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00008603#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02008604 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00008605 oa.op_type = OP_RSHIFT;
8606 else
8607 oa.op_type = OP_LSHIFT;
8608 op_shift(&oa, FALSE, eap->amount);
8609 break;
8610 }
8611#ifdef FEAT_VIRTUALEDIT
8612 virtual_op = MAYBE;
8613#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00008614 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008615}
8616
8617/*
8618 * ":put".
8619 */
8620 static void
8621ex_put(eap)
8622 exarg_T *eap;
8623{
8624 /* ":0put" works like ":1put!". */
8625 if (eap->line2 == 0)
8626 {
8627 eap->line2 = 1;
8628 eap->forceit = TRUE;
8629 }
8630 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008631 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
8632 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008633}
8634
8635/*
8636 * Handle ":copy" and ":move".
8637 */
8638 static void
8639ex_copymove(eap)
8640 exarg_T *eap;
8641{
8642 long n;
8643
8644 n = get_address(&eap->arg, FALSE, FALSE);
8645 if (eap->arg == NULL) /* error detected */
8646 {
8647 eap->nextcmd = NULL;
8648 return;
8649 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008650 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008651
8652 /*
8653 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
8654 */
8655 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
8656 {
8657 EMSG(_(e_invaddr));
8658 return;
8659 }
8660
8661 if (eap->cmdidx == CMD_move)
8662 {
8663 if (do_move(eap->line1, eap->line2, n) == FAIL)
8664 return;
8665 }
8666 else
8667 ex_copy(eap->line1, eap->line2, n);
8668 u_clearline();
8669 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008670 ex_may_print(eap);
8671}
8672
8673/*
8674 * Print the current line if flags were given to the Ex command.
8675 */
8676 static void
8677ex_may_print(eap)
8678 exarg_T *eap;
8679{
8680 if (eap->flags != 0)
8681 {
8682 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
8683 (eap->flags & EXFLAG_LIST));
8684 ex_no_reprint = TRUE;
8685 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008686}
8687
8688/*
8689 * ":smagic" and ":snomagic".
8690 */
8691 static void
8692ex_submagic(eap)
8693 exarg_T *eap;
8694{
8695 int magic_save = p_magic;
8696
8697 p_magic = (eap->cmdidx == CMD_smagic);
8698 do_sub(eap);
8699 p_magic = magic_save;
8700}
8701
8702/*
8703 * ":join".
8704 */
8705 static void
8706ex_join(eap)
8707 exarg_T *eap;
8708{
8709 curwin->w_cursor.lnum = eap->line1;
8710 if (eap->line1 == eap->line2)
8711 {
8712 if (eap->addr_count >= 2) /* :2,2join does nothing */
8713 return;
8714 if (eap->line2 == curbuf->b_ml.ml_line_count)
8715 {
8716 beep_flush();
8717 return;
8718 }
8719 ++eap->line2;
8720 }
Bram Moolenaar81340392012-06-06 16:12:59 +02008721 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008722 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008723 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008724}
8725
8726/*
8727 * ":[addr]@r" or ":[addr]*r": execute register
8728 */
8729 static void
8730ex_at(eap)
8731 exarg_T *eap;
8732{
8733 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00008734 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008735
8736 curwin->w_cursor.lnum = eap->line2;
8737
8738#ifdef USE_ON_FLY_SCROLL
8739 dont_scroll = TRUE; /* disallow scrolling here */
8740#endif
8741
8742 /* get the register name. No name means to use the previous one */
8743 c = *eap->arg;
8744 if (c == NUL || (c == '*' && *eap->cmd == '*'))
8745 c = '@';
Bram Moolenaard333d1e2006-11-07 17:43:47 +00008746 /* Put the register in the typeahead buffer with the "silent" flag. */
8747 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
8748 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008749 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008750 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00008751 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008752 else
8753 {
8754 int save_efr = exec_from_reg;
8755
8756 exec_from_reg = TRUE;
8757
8758 /*
8759 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00008760 * Continue until the stuff buffer is empty and all added characters
8761 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008762 */
Bram Moolenaar60462872009-11-03 11:40:19 +00008763 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008764 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
8765
8766 exec_from_reg = save_efr;
8767 }
8768}
8769
8770/*
8771 * ":!".
8772 */
8773 static void
8774ex_bang(eap)
8775 exarg_T *eap;
8776{
8777 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
8778}
8779
8780/*
8781 * ":undo".
8782 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008783 static void
8784ex_undo(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008785 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008786{
Bram Moolenaard3667a22006-03-16 21:35:52 +00008787 if (eap->addr_count == 1) /* :undo 123 */
Bram Moolenaar730cde92010-06-27 05:18:54 +02008788 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00008789 else
8790 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008791}
8792
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008793#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008794 static void
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008795ex_wundo(eap)
8796 exarg_T *eap;
8797{
8798 char_u hash[UNDO_HASH_SIZE];
8799
8800 u_compute_hash(hash);
8801 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
8802}
8803
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008804 static void
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008805ex_rundo(eap)
8806 exarg_T *eap;
8807{
8808 char_u hash[UNDO_HASH_SIZE];
8809
8810 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02008811 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008812}
8813#endif
8814
Bram Moolenaar071d4272004-06-13 20:20:40 +00008815/*
8816 * ":redo".
8817 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008818 static void
8819ex_redo(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008820 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008821{
8822 u_redo(1);
8823}
8824
8825/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008826 * ":earlier" and ":later".
8827 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008828 static void
8829ex_later(eap)
8830 exarg_T *eap;
8831{
8832 long count = 0;
8833 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008834 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008835 char_u *p = eap->arg;
8836
8837 if (*p == NUL)
8838 count = 1;
8839 else if (isdigit(*p))
8840 {
8841 count = getdigits(&p);
8842 switch (*p)
8843 {
8844 case 's': ++p; sec = TRUE; break;
8845 case 'm': ++p; sec = TRUE; count *= 60; break;
8846 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008847 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
8848 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008849 }
8850 }
8851
8852 if (*p != NUL)
8853 EMSG2(_(e_invarg2), eap->arg);
8854 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02008855 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
8856 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008857}
8858
8859/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008860 * ":redir": start/stop redirection.
8861 */
8862 static void
8863ex_redir(eap)
8864 exarg_T *eap;
8865{
8866 char *mode;
8867 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00008868 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008869
8870 if (STRICMP(eap->arg, "END") == 0)
8871 close_redir();
8872 else
8873 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008874 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008875 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008876 ++arg;
8877 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008878 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008879 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008880 mode = "a";
8881 }
8882 else
8883 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00008884 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008885
8886 close_redir();
8887
8888 /* Expand environment variables and "~/". */
Bram Moolenaarca472992005-01-21 11:46:23 +00008889 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008890 if (fname == NULL)
8891 return;
8892#ifdef FEAT_BROWSE
8893 if (cmdmod.browse)
8894 {
8895 char_u *browseFile;
8896
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008897 browseFile = do_browse(BROWSE_SAVE,
8898 (char_u *)_("Save Redirection"),
8899 fname, NULL, NULL, BROWSE_FILTER_ALL_FILES, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008900 if (browseFile == NULL)
8901 return; /* operation cancelled */
8902 vim_free(fname);
8903 fname = browseFile;
8904 eap->forceit = TRUE; /* since dialog already asked */
8905 }
8906#endif
8907
8908 redir_fd = open_exfile(fname, eap->forceit, mode);
8909 vim_free(fname);
8910 }
8911#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00008912 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008913 {
8914 /* redirect to a register a-z (resp. A-Z for appending) */
8915 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00008916 ++arg;
8917 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008918# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00008919 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00008920 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008921# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00008922 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008923 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008924 redir_reg = *arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008925 if (*arg == '>' && arg[1] == '>') /* append */
Bram Moolenaard9d30582005-05-18 22:10:28 +00008926 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008927 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008928 {
Bram Moolenaarc188b882007-10-19 14:20:54 +00008929 /* Can use both "@a" and "@a>". */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00008930 if (*arg == '>')
8931 arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008932 /* Make register empty when not using @A-@Z and the
8933 * command is valid. */
8934 if (*arg == NUL && !isupper(redir_reg))
8935 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008936 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008937 }
8938 if (*arg != NUL)
8939 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00008940 redir_reg = 0;
Bram Moolenaard9d30582005-05-18 22:10:28 +00008941 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008942 }
8943 }
8944 else if (*arg == '=' && arg[1] == '>')
8945 {
8946 int append;
8947
8948 /* redirect to a variable */
8949 close_redir();
8950 arg += 2;
8951
8952 if (*arg == '>')
8953 {
8954 ++arg;
8955 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008956 }
8957 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008958 append = FALSE;
8959
8960 if (var_redir_start(skipwhite(arg), append) == OK)
8961 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008962 }
8963#endif
8964
8965 /* TODO: redirect to a buffer */
8966
Bram Moolenaar071d4272004-06-13 20:20:40 +00008967 else
Bram Moolenaarca472992005-01-21 11:46:23 +00008968 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008969 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00008970
8971 /* Make sure redirection is not off. Can happen for cmdline completion
8972 * that indirectly invokes a command to catch its output. */
8973 if (redir_fd != NULL
8974#ifdef FEAT_EVAL
8975 || redir_reg || redir_vname
8976#endif
8977 )
8978 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008979}
8980
8981/*
8982 * ":redraw": force redraw
8983 */
8984 static void
8985ex_redraw(eap)
8986 exarg_T *eap;
8987{
8988 int r = RedrawingDisabled;
8989 int p = p_lz;
8990
8991 RedrawingDisabled = 0;
8992 p_lz = FALSE;
8993 update_topline();
8994 update_screen(eap->forceit ? CLEAR :
8995#ifdef FEAT_VISUAL
8996 VIsual_active ? INVERTED :
8997#endif
8998 0);
8999#ifdef FEAT_TITLE
9000 if (need_maketitle)
9001 maketitle();
9002#endif
9003 RedrawingDisabled = r;
9004 p_lz = p;
9005
9006 /* Reset msg_didout, so that a message that's there is overwritten. */
9007 msg_didout = FALSE;
9008 msg_col = 0;
9009
Bram Moolenaar56667a52013-07-17 11:54:28 +02009010 /* No need to wait after an intentional redraw. */
9011 need_wait_return = FALSE;
9012
Bram Moolenaar071d4272004-06-13 20:20:40 +00009013 out_flush();
9014}
9015
9016/*
9017 * ":redrawstatus": force redraw of status line(s)
9018 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009019 static void
9020ex_redrawstatus(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009021 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009022{
9023#if defined(FEAT_WINDOWS)
9024 int r = RedrawingDisabled;
9025 int p = p_lz;
9026
9027 RedrawingDisabled = 0;
9028 p_lz = FALSE;
9029 if (eap->forceit)
9030 status_redraw_all();
9031 else
9032 status_redraw_curbuf();
9033 update_screen(
9034# ifdef FEAT_VISUAL
9035 VIsual_active ? INVERTED :
9036# endif
9037 0);
9038 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