blob: 3276abfdbc48daabb53540de06eefa5d9a73439a [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 Moolenaarf1d6ccf2014-12-08 04:16:44 +010030 int uc_addr_type; /* The command's address type */
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010031# ifdef FEAT_EVAL
32 scid_T uc_scriptID; /* SID where the command was defined */
33# ifdef FEAT_CMDL_COMPL
Bram Moolenaar071d4272004-06-13 20:20:40 +000034 char_u *uc_compl_arg; /* completion argument if any */
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010035# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000036# endif
37} ucmd_T;
38
39#define UC_BUFFER 1 /* -buffer: local to current buffer */
40
Bram Moolenaar2c29bee2005-06-01 21:46:07 +000041static garray_T ucmds = {0, 0, sizeof(ucmd_T), 4, NULL};
Bram Moolenaar071d4272004-06-13 20:20:40 +000042
43#define USER_CMD(i) (&((ucmd_T *)(ucmds.ga_data))[i])
44#define USER_CMD_GA(gap, i) (&((ucmd_T *)((gap)->ga_data))[i])
45
46static void do_ucmd __ARGS((exarg_T *eap));
47static void ex_command __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +000048static void ex_delcommand __ARGS((exarg_T *eap));
49# ifdef FEAT_CMDL_COMPL
50static char_u *get_user_command_name __ARGS((int idx));
51# endif
52
Bram Moolenaar958636c2014-10-21 20:01:58 +020053/* Wether a command index indicates a user command. */
54# define IS_USER_CMDIDX(idx) ((int)(idx) < 0)
55
Bram Moolenaar071d4272004-06-13 20:20:40 +000056#else
57# define ex_command ex_ni
58# define ex_comclear ex_ni
59# define ex_delcommand ex_ni
Bram Moolenaar958636c2014-10-21 20:01:58 +020060/* Wether a command index indicates a user command. */
61# define IS_USER_CMDIDX(idx) (FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +000062#endif
63
Bram Moolenaarb96a7f32014-11-27 16:22:48 +010064static int compute_buffer_local_count __ARGS((int addr_type, int lnum, int local));
Bram Moolenaar071d4272004-06-13 20:20:40 +000065#ifdef FEAT_EVAL
Bram Moolenaar89d40322006-08-29 15:30:07 +000066static 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 +000067#else
Bram Moolenaar89d40322006-08-29 15:30:07 +000068static char_u *do_one_cmd __ARGS((char_u **, int, char_u *(*fgetline)(int, void *, int), void *cookie));
Bram Moolenaar071d4272004-06-13 20:20:40 +000069static int if_level = 0; /* depth in :if */
70#endif
Bram Moolenaara6f4d612011-09-21 19:10:46 +020071static void append_command __ARGS((char_u *cmd));
Bram Moolenaar071d4272004-06-13 20:20:40 +000072static char_u *find_command __ARGS((exarg_T *eap, int *full));
73
74static void ex_abbreviate __ARGS((exarg_T *eap));
75static void ex_map __ARGS((exarg_T *eap));
76static void ex_unmap __ARGS((exarg_T *eap));
77static void ex_mapclear __ARGS((exarg_T *eap));
78static void ex_abclear __ARGS((exarg_T *eap));
79#ifndef FEAT_MENU
80# define ex_emenu ex_ni
81# define ex_menu ex_ni
82# define ex_menutranslate ex_ni
83#endif
84#ifdef FEAT_AUTOCMD
85static void ex_autocmd __ARGS((exarg_T *eap));
86static void ex_doautocmd __ARGS((exarg_T *eap));
87#else
88# define ex_autocmd ex_ni
89# define ex_doautocmd ex_ni
90# define ex_doautoall ex_ni
91#endif
92#ifdef FEAT_LISTCMDS
93static void ex_bunload __ARGS((exarg_T *eap));
94static void ex_buffer __ARGS((exarg_T *eap));
95static void ex_bmodified __ARGS((exarg_T *eap));
96static void ex_bnext __ARGS((exarg_T *eap));
97static void ex_bprevious __ARGS((exarg_T *eap));
98static void ex_brewind __ARGS((exarg_T *eap));
99static void ex_blast __ARGS((exarg_T *eap));
100#else
101# define ex_bunload ex_ni
102# define ex_buffer ex_ni
103# define ex_bmodified ex_ni
104# define ex_bnext ex_ni
105# define ex_bprevious ex_ni
106# define ex_brewind ex_ni
107# define ex_blast ex_ni
108# define buflist_list ex_ni
109# define ex_checktime ex_ni
110#endif
111#if !defined(FEAT_LISTCMDS) || !defined(FEAT_WINDOWS)
112# define ex_buffer_all ex_ni
113#endif
114static char_u *getargcmd __ARGS((char_u **));
115static char_u *skip_cmd_arg __ARGS((char_u *p, int rembs));
116static int getargopt __ARGS((exarg_T *eap));
117#ifndef FEAT_QUICKFIX
118# define ex_make ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +0000119# define ex_cbuffer ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000120# define ex_cc ex_ni
121# define ex_cnext ex_ni
122# define ex_cfile ex_ni
123# define qf_list ex_ni
124# define qf_age ex_ni
125# define ex_helpgrep ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +0000126# define ex_vimgrep ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000127#endif
128#if !defined(FEAT_QUICKFIX) || !defined(FEAT_WINDOWS)
129# define ex_cclose ex_ni
130# define ex_copen ex_ni
131# define ex_cwindow ex_ni
132#endif
Bram Moolenaar1e015462005-09-25 22:16:38 +0000133#if !defined(FEAT_QUICKFIX) || !defined(FEAT_EVAL)
134# define ex_cexpr ex_ni
135#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000136
137static int check_more __ARGS((int, int));
Bram Moolenaarb96a7f32014-11-27 16:22:48 +0100138static linenr_T get_address __ARGS((char_u **, int addr_type, int skip, int to_other_file));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000139static void get_flags __ARGS((exarg_T *eap));
Bram Moolenaar85363ab2010-07-18 13:58:26 +0200140#if !defined(FEAT_PERL) \
141 || !defined(FEAT_PYTHON) || !defined(FEAT_PYTHON3) \
142 || !defined(FEAT_TCL) \
143 || !defined(FEAT_RUBY) \
144 || !defined(FEAT_LUA) \
145 || !defined(FEAT_MZSCHEME)
Bram Moolenaar7bb75552007-07-16 18:39:49 +0000146# define HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +0000147static void ex_script_ni __ARGS((exarg_T *eap));
148#endif
149static char_u *invalid_range __ARGS((exarg_T *eap));
150static void correct_range __ARGS((exarg_T *eap));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000151#ifdef FEAT_QUICKFIX
152static char_u *replace_makeprg __ARGS((exarg_T *eap, char_u *p, char_u **cmdlinep));
153#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000154static char_u *repl_cmdline __ARGS((exarg_T *eap, char_u *src, int srclen, char_u *repl, char_u **cmdlinep));
155static void ex_highlight __ARGS((exarg_T *eap));
156static void ex_colorscheme __ARGS((exarg_T *eap));
157static void ex_quit __ARGS((exarg_T *eap));
158static void ex_cquit __ARGS((exarg_T *eap));
159static void ex_quit_all __ARGS((exarg_T *eap));
160#ifdef FEAT_WINDOWS
161static void ex_close __ARGS((exarg_T *eap));
Bram Moolenaarf740b292006-02-16 22:11:02 +0000162static void ex_win_close __ARGS((int forceit, win_T *win, tabpage_T *tp));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000163static void ex_only __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000164static void ex_resize __ARGS((exarg_T *eap));
165static void ex_stag __ARGS((exarg_T *eap));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000166static void ex_tabclose __ARGS((exarg_T *eap));
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000167static void ex_tabonly __ARGS((exarg_T *eap));
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000168static void ex_tabnext __ARGS((exarg_T *eap));
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000169static void ex_tabmove __ARGS((exarg_T *eap));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000170static void ex_tabs __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000171#else
172# define ex_close ex_ni
173# define ex_only ex_ni
174# define ex_all ex_ni
175# define ex_resize ex_ni
176# define ex_splitview ex_ni
177# define ex_stag ex_ni
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000178# define ex_tabnext ex_ni
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000179# define ex_tabmove ex_ni
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000180# define ex_tabs ex_ni
181# define ex_tabclose ex_ni
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000182# define ex_tabonly ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000183#endif
184#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
185static void ex_pclose __ARGS((exarg_T *eap));
186static void ex_ptag __ARGS((exarg_T *eap));
187static void ex_pedit __ARGS((exarg_T *eap));
188#else
189# define ex_pclose ex_ni
190# define ex_ptag ex_ni
191# define ex_pedit ex_ni
192#endif
193static void ex_hide __ARGS((exarg_T *eap));
194static void ex_stop __ARGS((exarg_T *eap));
195static void ex_exit __ARGS((exarg_T *eap));
196static void ex_print __ARGS((exarg_T *eap));
197#ifdef FEAT_BYTEOFF
198static void ex_goto __ARGS((exarg_T *eap));
199#else
200# define ex_goto ex_ni
201#endif
202static void ex_shell __ARGS((exarg_T *eap));
203static void ex_preserve __ARGS((exarg_T *eap));
204static void ex_recover __ARGS((exarg_T *eap));
205#ifndef FEAT_LISTCMDS
206# define ex_argedit ex_ni
207# define ex_argadd ex_ni
208# define ex_argdelete ex_ni
209# define ex_listdo ex_ni
210#endif
211static void ex_mode __ARGS((exarg_T *eap));
212static void ex_wrongmodifier __ARGS((exarg_T *eap));
213static void ex_find __ARGS((exarg_T *eap));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000214static void ex_open __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000215static void ex_edit __ARGS((exarg_T *eap));
216#if !defined(FEAT_GUI) && !defined(FEAT_CLIENTSERVER)
217# define ex_drop ex_ni
218#endif
219#ifndef FEAT_GUI
220# define ex_gui ex_nogui
221static void ex_nogui __ARGS((exarg_T *eap));
222#endif
223#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
224static void ex_tearoff __ARGS((exarg_T *eap));
225#else
226# define ex_tearoff ex_ni
227#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000228#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229static void ex_popup __ARGS((exarg_T *eap));
230#else
231# define ex_popup ex_ni
232#endif
233#ifndef FEAT_GUI_MSWIN
234# define ex_simalt ex_ni
235#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000236#if !defined(FEAT_GUI_MSWIN) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000237# define gui_mch_find_dialog ex_ni
238# define gui_mch_replace_dialog ex_ni
239#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000240#if !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000241# define ex_helpfind ex_ni
242#endif
243#ifndef FEAT_CSCOPE
244# define do_cscope ex_ni
245# define do_scscope ex_ni
246# define do_cstag ex_ni
247#endif
248#ifndef FEAT_SYN_HL
249# define ex_syntax ex_ni
Bram Moolenaar860cae12010-06-05 23:22:07 +0200250# define ex_ownsyntax ex_ni
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000251#endif
Bram Moolenaarf7512552013-06-06 14:55:19 +0200252#if !defined(FEAT_SYN_HL) || !defined(FEAT_PROFILE)
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +0200253# define ex_syntime ex_ni
254#endif
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000255#ifndef FEAT_SPELL
Bram Moolenaarb765d632005-06-07 21:00:02 +0000256# define ex_spell ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000257# define ex_mkspell ex_ni
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000258# define ex_spelldump ex_ni
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000259# define ex_spellinfo ex_ni
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000260# define ex_spellrepall ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000261#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200262#ifndef FEAT_PERSISTENT_UNDO
263# define ex_rundo ex_ni
264# define ex_wundo ex_ni
265#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200266#ifndef FEAT_LUA
267# define ex_lua ex_script_ni
268# define ex_luado ex_ni
269# define ex_luafile ex_ni
270#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000271#ifndef FEAT_MZSCHEME
272# define ex_mzscheme ex_script_ni
273# define ex_mzfile ex_ni
274#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000275#ifndef FEAT_PERL
276# define ex_perl ex_script_ni
277# define ex_perldo ex_ni
278#endif
279#ifndef FEAT_PYTHON
280# define ex_python ex_script_ni
Bram Moolenaard620aa92013-05-17 16:40:06 +0200281# define ex_pydo ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000282# define ex_pyfile ex_ni
283#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200284#ifndef FEAT_PYTHON3
Bram Moolenaar368373e2010-07-19 20:46:22 +0200285# define ex_py3 ex_script_ni
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200286# define ex_py3do ex_ni
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200287# define ex_py3file ex_ni
288#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000289#ifndef FEAT_TCL
290# define ex_tcl ex_script_ni
291# define ex_tcldo ex_ni
292# define ex_tclfile ex_ni
293#endif
294#ifndef FEAT_RUBY
295# define ex_ruby ex_script_ni
296# define ex_rubydo ex_ni
297# define ex_rubyfile ex_ni
298#endif
299#ifndef FEAT_SNIFF
300# define ex_sniff ex_ni
301#endif
302#ifndef FEAT_KEYMAP
303# define ex_loadkeymap ex_ni
304#endif
305static void ex_swapname __ARGS((exarg_T *eap));
306static void ex_syncbind __ARGS((exarg_T *eap));
307static void ex_read __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000308static void ex_pwd __ARGS((exarg_T *eap));
309static void ex_equal __ARGS((exarg_T *eap));
310static void ex_sleep __ARGS((exarg_T *eap));
311static void do_exmap __ARGS((exarg_T *eap, int isabbrev));
312static void ex_winsize __ARGS((exarg_T *eap));
313#ifdef FEAT_WINDOWS
314static void ex_wincmd __ARGS((exarg_T *eap));
315#else
316# define ex_wincmd ex_ni
317#endif
Bram Moolenaar843ee412004-06-30 16:16:41 +0000318#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000319static void ex_winpos __ARGS((exarg_T *eap));
320#else
321# define ex_winpos ex_ni
322#endif
323static void ex_operators __ARGS((exarg_T *eap));
324static void ex_put __ARGS((exarg_T *eap));
325static void ex_copymove __ARGS((exarg_T *eap));
326static void ex_submagic __ARGS((exarg_T *eap));
327static void ex_join __ARGS((exarg_T *eap));
328static void ex_at __ARGS((exarg_T *eap));
329static void ex_bang __ARGS((exarg_T *eap));
330static void ex_undo __ARGS((exarg_T *eap));
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200331#ifdef FEAT_PERSISTENT_UNDO
332static void ex_wundo __ARGS((exarg_T *eap));
333static void ex_rundo __ARGS((exarg_T *eap));
334#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000335static void ex_redo __ARGS((exarg_T *eap));
Bram Moolenaarc7d89352006-03-14 22:53:34 +0000336static void ex_later __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000337static void ex_redir __ARGS((exarg_T *eap));
338static void ex_redraw __ARGS((exarg_T *eap));
339static void ex_redrawstatus __ARGS((exarg_T *eap));
340static void close_redir __ARGS((void));
341static void ex_mkrc __ARGS((exarg_T *eap));
342static void ex_mark __ARGS((exarg_T *eap));
343#ifdef FEAT_USR_CMDS
344static char_u *uc_fun_cmd __ARGS((void));
Bram Moolenaar52b4b552005-03-07 23:00:57 +0000345static 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 +0000346#endif
347#ifdef FEAT_EX_EXTRA
348static void ex_normal __ARGS((exarg_T *eap));
349static void ex_startinsert __ARGS((exarg_T *eap));
350static void ex_stopinsert __ARGS((exarg_T *eap));
351#else
352# define ex_normal ex_ni
353# define ex_align ex_ni
354# define ex_retab ex_ni
355# define ex_startinsert ex_ni
356# define ex_stopinsert ex_ni
357# define ex_helptags ex_ni
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +0000358# define ex_sort ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000359#endif
360#ifdef FEAT_FIND_ID
361static void ex_checkpath __ARGS((exarg_T *eap));
362static void ex_findpat __ARGS((exarg_T *eap));
363#else
364# define ex_findpat ex_ni
365# define ex_checkpath ex_ni
366#endif
367#if defined(FEAT_FIND_ID) && defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
368static void ex_psearch __ARGS((exarg_T *eap));
369#else
370# define ex_psearch ex_ni
371#endif
372static void ex_tag __ARGS((exarg_T *eap));
373static void ex_tag_cmd __ARGS((exarg_T *eap, char_u *name));
374#ifndef FEAT_EVAL
375# define ex_scriptnames ex_ni
376# define ex_finish ex_ni
377# define ex_echo ex_ni
378# define ex_echohl ex_ni
379# define ex_execute ex_ni
380# define ex_call ex_ni
381# define ex_if ex_ni
382# define ex_endif ex_ni
383# define ex_else ex_ni
384# define ex_while ex_ni
385# define ex_continue ex_ni
386# define ex_break ex_ni
387# define ex_endwhile ex_ni
388# define ex_throw ex_ni
389# define ex_try ex_ni
390# define ex_catch ex_ni
391# define ex_finally ex_ni
392# define ex_endtry ex_ni
393# define ex_endfunction ex_ni
394# define ex_let ex_ni
395# define ex_unlet ex_ni
Bram Moolenaar65c1b012005-01-31 19:02:28 +0000396# define ex_lockvar ex_ni
397# define ex_unlockvar ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000398# define ex_function ex_ni
399# define ex_delfunction ex_ni
400# define ex_return ex_ni
Bram Moolenaard812df62008-11-09 12:46:09 +0000401# define ex_oldfiles ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000402#endif
403static char_u *arg_all __ARGS((void));
404#ifdef FEAT_SESSION
405static int makeopens __ARGS((FILE *fd, char_u *dirnow));
Bram Moolenaarf13be0d2008-01-02 14:13:10 +0000406static 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 +0000407static void ex_loadview __ARGS((exarg_T *eap));
408static char_u *get_view_file __ARGS((int c));
Bram Moolenaareeefcc72007-05-01 21:21:21 +0000409static int did_lcd; /* whether ":lcd" was produced for a session */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000410#else
411# define ex_loadview ex_ni
412#endif
413#ifndef FEAT_EVAL
414# define ex_compiler ex_ni
415#endif
416#ifdef FEAT_VIMINFO
417static void ex_viminfo __ARGS((exarg_T *eap));
418#else
419# define ex_viminfo ex_ni
420#endif
421static void ex_behave __ARGS((exarg_T *eap));
422#ifdef FEAT_AUTOCMD
423static void ex_filetype __ARGS((exarg_T *eap));
424static void ex_setfiletype __ARGS((exarg_T *eap));
425#else
426# define ex_filetype ex_ni
427# define ex_setfiletype ex_ni
428#endif
429#ifndef FEAT_DIFF
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000430# define ex_diffoff ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000431# define ex_diffpatch ex_ni
432# define ex_diffgetput ex_ni
433# define ex_diffsplit ex_ni
434# define ex_diffthis ex_ni
435# define ex_diffupdate ex_ni
436#endif
437static void ex_digraphs __ARGS((exarg_T *eap));
438static void ex_set __ARGS((exarg_T *eap));
439#if !defined(FEAT_EVAL) || !defined(FEAT_AUTOCMD)
440# define ex_options ex_ni
441#endif
442#ifdef FEAT_SEARCH_EXTRA
443static void ex_nohlsearch __ARGS((exarg_T *eap));
444static void ex_match __ARGS((exarg_T *eap));
445#else
446# define ex_nohlsearch ex_ni
447# define ex_match ex_ni
448#endif
449#ifdef FEAT_CRYPT
450static void ex_X __ARGS((exarg_T *eap));
451#else
452# define ex_X ex_ni
453#endif
454#ifdef FEAT_FOLDING
455static void ex_fold __ARGS((exarg_T *eap));
456static void ex_foldopen __ARGS((exarg_T *eap));
457static void ex_folddo __ARGS((exarg_T *eap));
458#else
459# define ex_fold ex_ni
460# define ex_foldopen ex_ni
461# define ex_folddo ex_ni
462#endif
463#if !((defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
464 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE)))
465# define ex_language ex_ni
466#endif
467#ifndef FEAT_SIGNS
468# define ex_sign ex_ni
469#endif
470#ifndef FEAT_SUN_WORKSHOP
471# define ex_wsverb ex_ni
472#endif
Bram Moolenaar009b2592004-10-24 19:18:58 +0000473#ifndef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200474# define ex_nbclose ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000475# define ex_nbkey ex_ni
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200476# define ex_nbstart ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000477#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000478
479#ifndef FEAT_EVAL
480# define ex_debug ex_ni
481# define ex_breakadd ex_ni
482# define ex_debuggreedy ex_ni
483# define ex_breakdel ex_ni
484# define ex_breaklist ex_ni
485#endif
486
487#ifndef FEAT_CMDHIST
488# define ex_history ex_ni
489#endif
490#ifndef FEAT_JUMPLIST
491# define ex_jumps ex_ni
492# define ex_changes ex_ni
493#endif
494
Bram Moolenaar05159a02005-02-26 23:04:13 +0000495#ifndef FEAT_PROFILE
496# define ex_profile ex_ni
497#endif
498
Bram Moolenaar071d4272004-06-13 20:20:40 +0000499/*
500 * Declare cmdnames[].
501 */
502#define DO_DECLARE_EXCMD
503#include "ex_cmds.h"
504
505/*
506 * Table used to quickly search for a command, based on its first character.
507 */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +0000508static cmdidx_T cmdidxs[27] =
Bram Moolenaar071d4272004-06-13 20:20:40 +0000509{
510 CMD_append,
511 CMD_buffer,
512 CMD_change,
513 CMD_delete,
514 CMD_edit,
515 CMD_file,
516 CMD_global,
517 CMD_help,
518 CMD_insert,
519 CMD_join,
520 CMD_k,
521 CMD_list,
522 CMD_move,
523 CMD_next,
524 CMD_open,
525 CMD_print,
526 CMD_quit,
527 CMD_read,
528 CMD_substitute,
529 CMD_t,
530 CMD_undo,
531 CMD_vglobal,
532 CMD_write,
533 CMD_xit,
534 CMD_yank,
535 CMD_z,
536 CMD_bang
537};
538
539static char_u dollar_command[2] = {'$', 0};
540
541
542#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000543/* Struct for storing a line inside a while/for loop */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000544typedef struct
545{
546 char_u *line; /* command line */
547 linenr_T lnum; /* sourcing_lnum of the line */
548} wcmd_T;
549
550/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000551 * Structure used to store info for line position in a while or for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000552 * This is required, because do_one_cmd() may invoke ex_function(), which
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000553 * reads more lines that may come from the while/for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000554 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000555struct loop_cookie
Bram Moolenaar071d4272004-06-13 20:20:40 +0000556{
557 garray_T *lines_gap; /* growarray with line info */
558 int current_line; /* last read line from growarray */
559 int repeating; /* TRUE when looping a second time */
560 /* When "repeating" is FALSE use "getline" and "cookie" to get lines */
561 char_u *(*getline) __ARGS((int, void *, int));
562 void *cookie;
563};
564
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000565static char_u *get_loop_line __ARGS((int c, void *cookie, int indent));
566static int store_loop_line __ARGS((garray_T *gap, char_u *line));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000567static void free_cmdlines __ARGS((garray_T *gap));
Bram Moolenaared203462004-06-16 11:19:22 +0000568
569/* Struct to save a few things while debugging. Used in do_cmdline() only. */
570struct dbg_stuff
571{
572 int trylevel;
573 int force_abort;
574 except_T *caught_stack;
575 char_u *vv_exception;
576 char_u *vv_throwpoint;
577 int did_emsg;
578 int got_int;
579 int did_throw;
580 int need_rethrow;
581 int check_cstack;
582 except_T *current_exception;
583};
584
585static void save_dbg_stuff __ARGS((struct dbg_stuff *dsp));
586static void restore_dbg_stuff __ARGS((struct dbg_stuff *dsp));
587
588 static void
589save_dbg_stuff(dsp)
590 struct dbg_stuff *dsp;
591{
592 dsp->trylevel = trylevel; trylevel = 0;
593 dsp->force_abort = force_abort; force_abort = FALSE;
594 dsp->caught_stack = caught_stack; caught_stack = NULL;
595 dsp->vv_exception = v_exception(NULL);
596 dsp->vv_throwpoint = v_throwpoint(NULL);
597
598 /* Necessary for debugging an inactive ":catch", ":finally", ":endtry" */
599 dsp->did_emsg = did_emsg; did_emsg = FALSE;
600 dsp->got_int = got_int; got_int = FALSE;
601 dsp->did_throw = did_throw; did_throw = FALSE;
602 dsp->need_rethrow = need_rethrow; need_rethrow = FALSE;
603 dsp->check_cstack = check_cstack; check_cstack = FALSE;
604 dsp->current_exception = current_exception; current_exception = NULL;
605}
606
607 static void
608restore_dbg_stuff(dsp)
609 struct dbg_stuff *dsp;
610{
611 suppress_errthrow = FALSE;
612 trylevel = dsp->trylevel;
613 force_abort = dsp->force_abort;
614 caught_stack = dsp->caught_stack;
615 (void)v_exception(dsp->vv_exception);
616 (void)v_throwpoint(dsp->vv_throwpoint);
617 did_emsg = dsp->did_emsg;
618 got_int = dsp->got_int;
619 did_throw = dsp->did_throw;
620 need_rethrow = dsp->need_rethrow;
621 check_cstack = dsp->check_cstack;
622 current_exception = dsp->current_exception;
623}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000624#endif
625
626
627/*
628 * do_exmode(): Repeatedly get commands for the "Ex" mode, until the ":vi"
629 * command is given.
630 */
631 void
632do_exmode(improved)
633 int improved; /* TRUE for "improved Ex" mode */
634{
635 int save_msg_scroll;
636 int prev_msg_row;
637 linenr_T prev_line;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000638 int changedtick;
639
640 if (improved)
641 exmode_active = EXMODE_VIM;
642 else
643 exmode_active = EXMODE_NORMAL;
644 State = NORMAL;
645
646 /* When using ":global /pat/ visual" and then "Q" we return to continue
647 * the :global command. */
648 if (global_busy)
649 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000650
651 save_msg_scroll = msg_scroll;
652 ++RedrawingDisabled; /* don't redisplay the window */
653 ++no_wait_return; /* don't wait for return */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000654#ifdef FEAT_GUI
655 /* Ignore scrollbar and mouse events in Ex mode */
656 ++hold_gui_events;
657#endif
658#ifdef FEAT_SNIFF
659 want_sniff_request = 0; /* No K_SNIFF wanted */
660#endif
661
662 MSG(_("Entering Ex mode. Type \"visual\" to go to Normal mode."));
663 while (exmode_active)
664 {
Bram Moolenaar7c626922005-02-07 22:01:03 +0000665#ifdef FEAT_EX_EXTRA
666 /* Check for a ":normal" command and no more characters left. */
667 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
668 {
669 exmode_active = FALSE;
670 break;
671 }
672#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000673 msg_scroll = TRUE;
674 need_wait_return = FALSE;
675 ex_pressedreturn = FALSE;
676 ex_no_reprint = FALSE;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000677 changedtick = curbuf->b_changedtick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678 prev_msg_row = msg_row;
679 prev_line = curwin->w_cursor.lnum;
680#ifdef FEAT_SNIFF
681 ProcessSniffRequests();
682#endif
683 if (improved)
684 {
685 cmdline_row = msg_row;
686 do_cmdline(NULL, getexline, NULL, 0);
687 }
688 else
689 do_cmdline(NULL, getexmodeline, NULL, DOCMD_NOWAIT);
690 lines_left = Rows - 1;
691
Bram Moolenaardf177f62005-02-22 08:39:57 +0000692 if ((prev_line != curwin->w_cursor.lnum
693 || changedtick != curbuf->b_changedtick) && !ex_no_reprint)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000695 if (curbuf->b_ml.ml_flags & ML_EMPTY)
696 EMSG(_(e_emptybuf));
697 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000699 if (ex_pressedreturn)
700 {
701 /* go up one line, to overwrite the ":<CR>" line, so the
Bram Moolenaar81870892007-11-11 18:17:28 +0000702 * output doesn't contain empty lines. */
Bram Moolenaardf177f62005-02-22 08:39:57 +0000703 msg_row = prev_msg_row;
704 if (prev_msg_row == Rows - 1)
705 msg_row--;
706 }
707 msg_col = 0;
708 print_line_no_prefix(curwin->w_cursor.lnum, FALSE, FALSE);
709 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000710 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000711 }
Bram Moolenaardf177f62005-02-22 08:39:57 +0000712 else if (ex_pressedreturn && !ex_no_reprint) /* must be at EOF */
713 {
714 if (curbuf->b_ml.ml_flags & ML_EMPTY)
715 EMSG(_(e_emptybuf));
716 else
717 EMSG(_("E501: At end-of-file"));
718 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000719 }
720
721#ifdef FEAT_GUI
722 --hold_gui_events;
723#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000724 --RedrawingDisabled;
725 --no_wait_return;
726 update_screen(CLEAR);
727 need_wait_return = FALSE;
728 msg_scroll = save_msg_scroll;
729}
730
731/*
732 * Execute a simple command line. Used for translated commands like "*".
733 */
734 int
735do_cmdline_cmd(cmd)
736 char_u *cmd;
737{
738 return do_cmdline(cmd, NULL, NULL,
739 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
740}
741
742/*
743 * do_cmdline(): execute one Ex command line
744 *
745 * 1. Execute "cmdline" when it is not NULL.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100746 * If "cmdline" is NULL, or more lines are needed, fgetline() is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000747 * 2. Split up in parts separated with '|'.
748 *
749 * This function can be called recursively!
750 *
751 * flags:
752 * DOCMD_VERBOSE - The command will be included in the error message.
753 * DOCMD_NOWAIT - Don't call wait_return() and friends.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100754 * DOCMD_REPEAT - Repeat execution until fgetline() returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755 * DOCMD_KEYTYPED - Don't reset KeyTyped.
756 * DOCMD_EXCRESET - Reset the exception environment (used for debugging).
757 * DOCMD_KEEPLINE - Store first typed line (for repeating with ".").
758 *
759 * return FAIL if cmdline could not be executed, OK otherwise
760 */
761 int
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100762do_cmdline(cmdline, fgetline, cookie, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000763 char_u *cmdline;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100764 char_u *(*fgetline) __ARGS((int, void *, int));
765 void *cookie; /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000766 int flags;
767{
768 char_u *next_cmdline; /* next cmd to execute */
769 char_u *cmdline_copy = NULL; /* copy of cmd line */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100770 int used_getline = FALSE; /* used "fgetline" to obtain command */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771 static int recursive = 0; /* recursive depth */
772 int msg_didout_before_start = 0;
773 int count = 0; /* line number count */
774 int did_inc = FALSE; /* incremented RedrawingDisabled */
775 int retval = OK;
776#ifdef FEAT_EVAL
777 struct condstack cstack; /* conditional stack */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000778 garray_T lines_ga; /* keep lines for ":while"/":for" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779 int current_line = 0; /* active line in lines_ga */
780 char_u *fname = NULL; /* function or script name */
781 linenr_T *breakpoint = NULL; /* ptr to breakpoint field in cookie */
782 int *dbg_tick = NULL; /* ptr to dbg_tick field in cookie */
Bram Moolenaared203462004-06-16 11:19:22 +0000783 struct dbg_stuff debug_saved; /* saved things for debug mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784 int initial_trylevel;
785 struct msglist **saved_msg_list = NULL;
786 struct msglist *private_msg_list;
787
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100788 /* "fgetline" and "cookie" passed to do_one_cmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000789 char_u *(*cmd_getline) __ARGS((int, void *, int));
790 void *cmd_cookie;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000791 struct loop_cookie cmd_loop_cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792 void *real_cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000793 int getline_is_func;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794#else
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100795# define cmd_getline fgetline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796# define cmd_cookie cookie
797#endif
798 static int call_depth = 0; /* recursiveness */
799
800#ifdef FEAT_EVAL
801 /* For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory
802 * location for storing error messages to be converted to an exception.
Bram Moolenaarcf3630f2005-01-08 16:04:29 +0000803 * This ensures that the do_errthrow() call in do_one_cmd() does not
804 * combine the messages stored by an earlier invocation of do_one_cmd()
805 * with the command name of the later one. This would happen when
806 * BufWritePost autocommands are executed after a write error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000807 saved_msg_list = msg_list;
808 msg_list = &private_msg_list;
809 private_msg_list = NULL;
810#endif
811
812 /* It's possible to create an endless loop with ":execute", catch that
813 * here. The value of 200 allows nested function calls, ":source", etc. */
814 if (call_depth == 200)
815 {
816 EMSG(_("E169: Command too recursive"));
817#ifdef FEAT_EVAL
818 /* When converting to an exception, we do not include the command name
819 * since this is not an error of the specific command. */
820 do_errthrow((struct condstack *)NULL, (char_u *)NULL);
821 msg_list = saved_msg_list;
822#endif
823 return FAIL;
824 }
825 ++call_depth;
826
827#ifdef FEAT_EVAL
828 cstack.cs_idx = -1;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000829 cstack.cs_looplevel = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830 cstack.cs_trylevel = 0;
831 cstack.cs_emsg_silent_list = NULL;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000832 cstack.cs_lflags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000833 ga_init2(&lines_ga, (int)sizeof(wcmd_T), 10);
834
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100835 real_cookie = getline_cookie(fgetline, cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000836
837 /* Inside a function use a higher nesting level. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100838 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000839 if (getline_is_func && ex_nesting_level == func_level(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840 ++ex_nesting_level;
841
842 /* Get the function or script name and the address where the next breakpoint
843 * line and the debug tick for a function or script are stored. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000844 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000845 {
846 fname = func_name(real_cookie);
847 breakpoint = func_breakpoint(real_cookie);
848 dbg_tick = func_dbg_tick(real_cookie);
849 }
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100850 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000851 {
852 fname = sourcing_name;
853 breakpoint = source_breakpoint(real_cookie);
854 dbg_tick = source_dbg_tick(real_cookie);
855 }
856
857 /*
858 * Initialize "force_abort" and "suppress_errthrow" at the top level.
859 */
860 if (!recursive)
861 {
862 force_abort = FALSE;
863 suppress_errthrow = FALSE;
864 }
865
866 /*
867 * If requested, store and reset the global values controlling the
Bram Moolenaar89d40322006-08-29 15:30:07 +0000868 * exception handling (used when debugging). Otherwise clear it to avoid
869 * a bogus compiler warning when the optimizer uses inline functions...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000870 */
Bram Moolenaarb4872942006-05-13 10:32:52 +0000871 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +0000872 save_dbg_stuff(&debug_saved);
Bram Moolenaar89d40322006-08-29 15:30:07 +0000873 else
Bram Moolenaar7db5fc82010-05-24 11:59:29 +0200874 vim_memset(&debug_saved, 0, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000875
876 initial_trylevel = trylevel;
877
878 /*
879 * "did_throw" will be set to TRUE when an exception is being thrown.
880 */
881 did_throw = FALSE;
882#endif
883 /*
884 * "did_emsg" will be set to TRUE when emsg() is used, in which case we
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000885 * cancel the whole command line, and any if/endif or loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000886 * If force_abort is set, we cancel everything.
887 */
888 did_emsg = FALSE;
889
890 /*
891 * KeyTyped is only set when calling vgetc(). Reset it here when not
892 * calling vgetc() (sourced command lines).
893 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100894 if (!(flags & DOCMD_KEYTYPED)
895 && !getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896 KeyTyped = FALSE;
897
898 /*
899 * Continue executing command lines:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000900 * - when inside an ":if", ":while" or ":for"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901 * - for multiple commands on one line, separated with '|'
902 * - when repeating until there are no more lines (for ":source")
903 */
904 next_cmdline = cmdline;
905 do
906 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000907#ifdef FEAT_EVAL
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100908 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000909#endif
910
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000911 /* stop skipping cmds for an error msg after all endif/while/for */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000912 if (next_cmdline == NULL
913#ifdef FEAT_EVAL
914 && !force_abort
915 && cstack.cs_idx < 0
Bram Moolenaar05159a02005-02-26 23:04:13 +0000916 && !(getline_is_func && func_has_abort(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000917#endif
918 )
919 did_emsg = FALSE;
920
921 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000922 * 1. If repeating a line in a loop, get a line from lines_ga.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100923 * 2. If no line given: Get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000924 * 3. If a line is given: Make a copy, so we can mess with it.
925 */
926
927#ifdef FEAT_EVAL
928 /* 1. If repeating, get a previous line from lines_ga. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000929 if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000930 {
931 /* Each '|' separated command is stored separately in lines_ga, to
932 * be able to jump to it. Don't use next_cmdline now. */
933 vim_free(cmdline_copy);
934 cmdline_copy = NULL;
935
936 /* Check if a function has returned or, unless it has an unclosed
937 * try conditional, aborted. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000938 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000940# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000941 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000942 func_line_end(real_cookie);
943# endif
944 if (func_has_ended(real_cookie))
945 {
946 retval = FAIL;
947 break;
948 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000950#ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000951 else if (do_profiling == PROF_YES
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100952 && getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000953 script_line_end();
954#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000955
956 /* Check if a sourced file hit a ":finish" command. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100957 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000958 {
959 retval = FAIL;
960 break;
961 }
962
963 /* If breakpoints have been added/deleted need to check for it. */
964 if (breakpoint != NULL && dbg_tick != NULL
965 && *dbg_tick != debug_tick)
966 {
967 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100968 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969 fname, sourcing_lnum);
970 *dbg_tick = debug_tick;
971 }
972
973 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
974 sourcing_lnum = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
975
976 /* Did we encounter a breakpoint? */
977 if (breakpoint != NULL && *breakpoint != 0
978 && *breakpoint <= sourcing_lnum)
979 {
980 dbg_breakpoint(fname, sourcing_lnum);
981 /* Find next breakpoint. */
982 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100983 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984 fname, sourcing_lnum);
985 *dbg_tick = debug_tick;
986 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000987# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000988 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000989 {
990 if (getline_is_func)
991 func_line_start(real_cookie);
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100992 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000993 script_line_start();
994 }
995# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996 }
997
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000998 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001000 /* Inside a while/for loop we need to store the lines and use them
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001001 * again. Pass a different "fgetline" function to do_one_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002 * below, so that it stores lines in or reads them from
1003 * "lines_ga". Makes it possible to define a function inside a
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001004 * while/for loop. */
1005 cmd_getline = get_loop_line;
1006 cmd_cookie = (void *)&cmd_loop_cookie;
1007 cmd_loop_cookie.lines_gap = &lines_ga;
1008 cmd_loop_cookie.current_line = current_line;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001009 cmd_loop_cookie.getline = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001010 cmd_loop_cookie.cookie = cookie;
1011 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012 }
1013 else
1014 {
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001015 cmd_getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016 cmd_cookie = cookie;
1017 }
1018#endif
1019
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001020 /* 2. If no line given, get an allocated line with fgetline(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021 if (next_cmdline == NULL)
1022 {
1023 /*
1024 * Need to set msg_didout for the first line after an ":if",
1025 * otherwise the ":if" will be overwritten.
1026 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001027 if (count == 1 && getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028 msg_didout = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001029 if (fgetline == NULL || (next_cmdline = fgetline(':', cookie,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030#ifdef FEAT_EVAL
1031 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
1032#else
1033 0
1034#endif
1035 )) == NULL)
1036 {
1037 /* Don't call wait_return for aborted command line. The NULL
1038 * returned for the end of a sourced file or executed function
1039 * doesn't do this. */
1040 if (KeyTyped && !(flags & DOCMD_REPEAT))
1041 need_wait_return = FALSE;
1042 retval = FAIL;
1043 break;
1044 }
1045 used_getline = TRUE;
1046
1047 /*
1048 * Keep the first typed line. Clear it when more lines are typed.
1049 */
1050 if (flags & DOCMD_KEEPLINE)
1051 {
1052 vim_free(repeat_cmdline);
1053 if (count == 0)
1054 repeat_cmdline = vim_strsave(next_cmdline);
1055 else
1056 repeat_cmdline = NULL;
1057 }
1058 }
1059
1060 /* 3. Make a copy of the command so we can mess with it. */
1061 else if (cmdline_copy == NULL)
1062 {
1063 next_cmdline = vim_strsave(next_cmdline);
1064 if (next_cmdline == NULL)
1065 {
1066 EMSG(_(e_outofmem));
1067 retval = FAIL;
1068 break;
1069 }
1070 }
1071 cmdline_copy = next_cmdline;
1072
1073#ifdef FEAT_EVAL
1074 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001075 * Save the current line when inside a ":while" or ":for", and when
1076 * the command looks like a ":while" or ":for", because we may need it
1077 * later. When there is a '|' and another command, it is stored
1078 * separately, because we need to be able to jump back to it from an
1079 * :endwhile/:endfor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001081 if (current_line == lines_ga.ga_len
1082 && (cstack.cs_looplevel || has_loop_cmd(next_cmdline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001084 if (store_loop_line(&lines_ga, next_cmdline) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085 {
1086 retval = FAIL;
1087 break;
1088 }
1089 }
1090 did_endif = FALSE;
1091#endif
1092
1093 if (count++ == 0)
1094 {
1095 /*
1096 * All output from the commands is put below each other, without
1097 * waiting for a return. Don't do this when executing commands
1098 * from a script or when being called recursive (e.g. for ":e
1099 * +command file").
1100 */
1101 if (!(flags & DOCMD_NOWAIT) && !recursive)
1102 {
1103 msg_didout_before_start = msg_didout;
1104 msg_didany = FALSE; /* no output yet */
1105 msg_start();
1106 msg_scroll = TRUE; /* put messages below each other */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001107 ++no_wait_return; /* don't wait for return until finished */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001108 ++RedrawingDisabled;
1109 did_inc = TRUE;
1110 }
1111 }
1112
1113 if (p_verbose >= 15 && sourcing_name != NULL)
1114 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115 ++no_wait_return;
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001116 verbose_enter_scroll();
1117
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118 smsg((char_u *)_("line %ld: %s"),
1119 (long)sourcing_lnum, cmdline_copy);
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001120 if (msg_silent == 0)
1121 msg_puts((char_u *)"\n"); /* don't overwrite this */
1122
1123 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124 --no_wait_return;
1125 }
1126
1127 /*
1128 * 2. Execute one '|' separated command.
1129 * do_one_cmd() will return NULL if there is no trailing '|'.
1130 * "cmdline_copy" can change, e.g. for '%' and '#' expansion.
1131 */
1132 ++recursive;
1133 next_cmdline = do_one_cmd(&cmdline_copy, flags & DOCMD_VERBOSE,
1134#ifdef FEAT_EVAL
1135 &cstack,
1136#endif
1137 cmd_getline, cmd_cookie);
1138 --recursive;
1139
1140#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001141 if (cmd_cookie == (void *)&cmd_loop_cookie)
1142 /* Use "current_line" from "cmd_loop_cookie", it may have been
Bram Moolenaar071d4272004-06-13 20:20:40 +00001143 * incremented when defining a function. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001144 current_line = cmd_loop_cookie.current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145#endif
1146
1147 if (next_cmdline == NULL)
1148 {
1149 vim_free(cmdline_copy);
1150 cmdline_copy = NULL;
1151#ifdef FEAT_CMDHIST
1152 /*
1153 * If the command was typed, remember it for the ':' register.
1154 * Do this AFTER executing the command to make :@: work.
1155 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001156 if (getline_equal(fgetline, cookie, getexline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001157 && new_last_cmdline != NULL)
1158 {
1159 vim_free(last_cmdline);
1160 last_cmdline = new_last_cmdline;
1161 new_last_cmdline = NULL;
1162 }
1163#endif
1164 }
1165 else
1166 {
1167 /* need to copy the command after the '|' to cmdline_copy, for the
1168 * next do_one_cmd() */
Bram Moolenaara7241f52008-06-24 20:39:31 +00001169 STRMOVE(cmdline_copy, next_cmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001170 next_cmdline = cmdline_copy;
1171 }
1172
1173
1174#ifdef FEAT_EVAL
1175 /* reset did_emsg for a function that is not aborted by an error */
1176 if (did_emsg && !force_abort
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001177 && getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178 && !func_has_abort(real_cookie))
1179 did_emsg = FALSE;
1180
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001181 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001182 {
1183 ++current_line;
1184
1185 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001186 * An ":endwhile", ":endfor" and ":continue" is handled here.
1187 * If we were executing commands, jump back to the ":while" or
1188 * ":for".
1189 * If we were not executing commands, decrement cs_looplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001191 if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001193 cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001195 /* Jump back to the matching ":while" or ":for". Be careful
1196 * not to use a cs_line[] from an entry that isn't a ":while"
1197 * or ":for": It would make "current_line" invalid and can
1198 * cause a crash. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199 if (!did_emsg && !got_int && !did_throw
1200 && cstack.cs_idx >= 0
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001201 && (cstack.cs_flags[cstack.cs_idx]
1202 & (CSF_WHILE | CSF_FOR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203 && cstack.cs_line[cstack.cs_idx] >= 0
1204 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
1205 {
1206 current_line = cstack.cs_line[cstack.cs_idx];
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001207 /* remember we jumped there */
1208 cstack.cs_lflags |= CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209 line_breakcheck(); /* check if CTRL-C typed */
1210
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001211 /* Check for the next breakpoint at or after the ":while"
1212 * or ":for". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213 if (breakpoint != NULL)
1214 {
1215 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001216 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001217 fname,
1218 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
1219 *dbg_tick = debug_tick;
1220 }
1221 }
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001222 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001224 /* can only get here with ":endwhile" or ":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225 if (cstack.cs_idx >= 0)
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001226 rewind_conditionals(&cstack, cstack.cs_idx - 1,
1227 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001228 }
1229 }
1230
1231 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001232 * For a ":while" or ":for" we need to remember the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001234 else if (cstack.cs_lflags & CSL_HAD_LOOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001236 cstack.cs_lflags &= ~CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001237 cstack.cs_line[cstack.cs_idx] = current_line - 1;
1238 }
1239 }
1240
1241 /*
1242 * When not inside any ":while" loop, clear remembered lines.
1243 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001244 if (cstack.cs_looplevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001245 {
1246 if (lines_ga.ga_len > 0)
1247 {
1248 sourcing_lnum =
1249 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
1250 free_cmdlines(&lines_ga);
1251 }
1252 current_line = 0;
1253 }
1254
1255 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001256 * A ":finally" makes did_emsg, got_int, and did_throw pending for
1257 * being restored at the ":endtry". Reset them here and set the
1258 * ACTIVE and FINALLY flags, so that the finally clause gets executed.
1259 * This includes the case where a missing ":endif", ":endwhile" or
1260 * ":endfor" was detected by the ":finally" itself.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001262 if (cstack.cs_lflags & CSL_HAD_FINA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001263 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001264 cstack.cs_lflags &= ~CSL_HAD_FINA;
1265 report_make_pending(cstack.cs_pending[cstack.cs_idx]
1266 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267 did_throw ? (void *)current_exception : NULL);
1268 did_emsg = got_int = did_throw = FALSE;
1269 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
1270 }
1271
1272 /* Update global "trylevel" for recursive calls to do_cmdline() from
1273 * within this loop. */
1274 trylevel = initial_trylevel + cstack.cs_trylevel;
1275
1276 /*
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001277 * If the outermost try conditional (across function calls and sourced
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278 * files) is aborted because of an error, an interrupt, or an uncaught
1279 * exception, cancel everything. If it is left normally, reset
1280 * force_abort to get the non-EH compatible abortion behavior for
1281 * the rest of the script.
1282 */
1283 if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
1284 force_abort = FALSE;
1285
1286 /* Convert an interrupt to an exception if appropriate. */
1287 (void)do_intthrow(&cstack);
1288#endif /* FEAT_EVAL */
1289
1290 }
1291 /*
1292 * Continue executing command lines when:
1293 * - no CTRL-C typed, no aborting error, no exception thrown or try
1294 * conditionals need to be checked for executing finally clauses or
1295 * catching an interrupt exception
1296 * - didn't get an error message or lines are not typed
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001297 * - there is a command after '|', inside a :if, :while, :for or :try, or
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298 * looping for ":source" command or function call.
1299 */
1300 while (!((got_int
1301#ifdef FEAT_EVAL
1302 || (did_emsg && force_abort) || did_throw
1303#endif
1304 )
1305#ifdef FEAT_EVAL
1306 && cstack.cs_trylevel == 0
1307#endif
1308 )
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001309 && !(did_emsg
1310#ifdef FEAT_EVAL
1311 /* Keep going when inside try/catch, so that the error can be
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001312 * deal with, except when it is a syntax error, it may cause
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001313 * the :endtry to be missed. */
1314 && (cstack.cs_trylevel == 0 || did_emsg_syntax)
1315#endif
1316 && used_getline
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001317 && (getline_equal(fgetline, cookie, getexmodeline)
1318 || getline_equal(fgetline, cookie, getexline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001319 && (next_cmdline != NULL
1320#ifdef FEAT_EVAL
1321 || cstack.cs_idx >= 0
1322#endif
1323 || (flags & DOCMD_REPEAT)));
1324
1325 vim_free(cmdline_copy);
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001326 did_emsg_syntax = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001327#ifdef FEAT_EVAL
1328 free_cmdlines(&lines_ga);
1329 ga_clear(&lines_ga);
1330
1331 if (cstack.cs_idx >= 0)
1332 {
1333 /*
1334 * If a sourced file or executed function ran to its end, report the
1335 * unclosed conditional.
1336 */
1337 if (!got_int && !did_throw
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001338 && ((getline_equal(fgetline, cookie, getsourceline)
1339 && !source_finished(fgetline, cookie))
1340 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341 && !func_has_ended(real_cookie))))
1342 {
1343 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
1344 EMSG(_(e_endtry));
1345 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
1346 EMSG(_(e_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001347 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
1348 EMSG(_(e_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349 else
1350 EMSG(_(e_endif));
1351 }
1352
1353 /*
1354 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1355 * ":endtry" in a sourced file or executed function. If the try
1356 * conditional is in its finally clause, ignore anything pending.
1357 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001358 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359 */
1360 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001361 {
1362 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1363
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001364 if (idx >= 0)
1365 --idx; /* remove try block not in its finally clause */
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001366 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1367 &cstack.cs_looplevel);
1368 }
1369 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370 trylevel = initial_trylevel;
1371 }
1372
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001373 /* If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1374 * lack was reported above and the error message is to be converted to an
Bram Moolenaar071d4272004-06-13 20:20:40 +00001375 * exception, do this now after rewinding the cstack. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001376 do_errthrow(&cstack, getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377 ? (char_u *)"endfunction" : (char_u *)NULL);
1378
1379 if (trylevel == 0)
1380 {
1381 /*
1382 * When an exception is being thrown out of the outermost try
1383 * conditional, discard the uncaught exception, disable the conversion
1384 * of interrupts or errors to exceptions, and ensure that no more
1385 * commands are executed.
1386 */
1387 if (did_throw)
1388 {
1389 void *p = NULL;
1390 char_u *saved_sourcing_name;
1391 int saved_sourcing_lnum;
1392 struct msglist *messages = NULL, *next;
1393
1394 /*
1395 * If the uncaught exception is a user exception, report it as an
1396 * error. If it is an error exception, display the saved error
1397 * message now. For an interrupt exception, do nothing; the
1398 * interrupt message is given elsewhere.
1399 */
1400 switch (current_exception->type)
1401 {
1402 case ET_USER:
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001403 vim_snprintf((char *)IObuff, IOSIZE,
1404 _("E605: Exception not caught: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001405 current_exception->value);
1406 p = vim_strsave(IObuff);
1407 break;
1408 case ET_ERROR:
1409 messages = current_exception->messages;
1410 current_exception->messages = NULL;
1411 break;
1412 case ET_INTERRUPT:
1413 break;
1414 default:
1415 p = vim_strsave((char_u *)_(e_internal));
1416 }
1417
1418 saved_sourcing_name = sourcing_name;
1419 saved_sourcing_lnum = sourcing_lnum;
1420 sourcing_name = current_exception->throw_name;
1421 sourcing_lnum = current_exception->throw_lnum;
1422 current_exception->throw_name = NULL;
1423
1424 discard_current_exception(); /* uses IObuff if 'verbose' */
1425 suppress_errthrow = TRUE;
1426 force_abort = TRUE;
1427
1428 if (messages != NULL)
1429 {
1430 do
1431 {
1432 next = messages->next;
1433 emsg(messages->msg);
1434 vim_free(messages->msg);
1435 vim_free(messages);
1436 messages = next;
1437 }
1438 while (messages != NULL);
1439 }
1440 else if (p != NULL)
1441 {
1442 emsg(p);
1443 vim_free(p);
1444 }
1445 vim_free(sourcing_name);
1446 sourcing_name = saved_sourcing_name;
1447 sourcing_lnum = saved_sourcing_lnum;
1448 }
1449
1450 /*
1451 * On an interrupt or an aborting error not converted to an exception,
1452 * disable the conversion of errors to exceptions. (Interrupts are not
1453 * converted any more, here.) This enables also the interrupt message
1454 * when force_abort is set and did_emsg unset in case of an interrupt
1455 * from a finally clause after an error.
1456 */
1457 else if (got_int || (did_emsg && force_abort))
1458 suppress_errthrow = TRUE;
1459 }
1460
1461 /*
1462 * The current cstack will be freed when do_cmdline() returns. An uncaught
1463 * exception will have to be rethrown in the previous cstack. If a function
1464 * has just returned or a script file was just finished and the previous
1465 * cstack belongs to the same function or, respectively, script file, it
1466 * will have to be checked for finally clauses to be executed due to the
1467 * ":return" or ":finish". This is done in do_one_cmd().
1468 */
1469 if (did_throw)
1470 need_rethrow = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001471 if ((getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472 && ex_nesting_level > source_level(real_cookie))
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001473 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001474 && ex_nesting_level > func_level(real_cookie) + 1))
1475 {
1476 if (!did_throw)
1477 check_cstack = TRUE;
1478 }
1479 else
1480 {
1481 /* When leaving a function, reduce nesting level. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001482 if (getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483 --ex_nesting_level;
1484 /*
1485 * Go to debug mode when returning from a function in which we are
1486 * single-stepping.
1487 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001488 if ((getline_equal(fgetline, cookie, getsourceline)
1489 || getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490 && ex_nesting_level + 1 <= debug_break_level)
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001491 do_debug(getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001492 ? (char_u *)_("End of sourced file")
1493 : (char_u *)_("End of function"));
1494 }
1495
1496 /*
1497 * Restore the exception environment (done after returning from the
1498 * debugger).
1499 */
1500 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001501 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001502
1503 msg_list = saved_msg_list;
1504#endif /* FEAT_EVAL */
1505
1506 /*
1507 * If there was too much output to fit on the command line, ask the user to
1508 * hit return before redrawing the screen. With the ":global" command we do
1509 * this only once after the command is finished.
1510 */
1511 if (did_inc)
1512 {
1513 --RedrawingDisabled;
1514 --no_wait_return;
1515 msg_scroll = FALSE;
1516
1517 /*
1518 * When just finished an ":if"-":else" which was typed, no need to
1519 * wait for hit-return. Also for an error situation.
1520 */
1521 if (retval == FAIL
1522#ifdef FEAT_EVAL
1523 || (did_endif && KeyTyped && !did_emsg)
1524#endif
1525 )
1526 {
1527 need_wait_return = FALSE;
1528 msg_didany = FALSE; /* don't wait when restarting edit */
1529 }
1530 else if (need_wait_return)
1531 {
1532 /*
1533 * The msg_start() above clears msg_didout. The wait_return we do
1534 * here should not overwrite the command that may be shown before
1535 * doing that.
1536 */
1537 msg_didout |= msg_didout_before_start;
1538 wait_return(FALSE);
1539 }
1540 }
1541
Bram Moolenaar2a942252012-11-28 23:03:07 +01001542#ifdef FEAT_EVAL
1543 did_endif = FALSE; /* in case do_cmdline used recursively */
1544#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001545 /*
1546 * Reset if_level, in case a sourced script file contains more ":if" than
1547 * ":endif" (could be ":if x | foo | endif").
1548 */
1549 if_level = 0;
Bram Moolenaar2e18a122012-11-28 19:10:54 +01001550#endif
Bram Moolenaard4ad0d42012-11-28 17:34:48 +01001551
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552 --call_depth;
1553 return retval;
1554}
1555
1556#ifdef FEAT_EVAL
1557/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001558 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001559 */
1560 static char_u *
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001561get_loop_line(c, cookie, indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562 int c;
1563 void *cookie;
1564 int indent;
1565{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001566 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567 wcmd_T *wp;
1568 char_u *line;
1569
1570 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1571 {
1572 if (cp->repeating)
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001573 return NULL; /* trying to read past ":endwhile"/":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001574
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001575 /* First time inside the ":while"/":for": get line normally. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576 if (cp->getline == NULL)
1577 line = getcmdline(c, 0L, indent);
1578 else
1579 line = cp->getline(c, cp->cookie, indent);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001580 if (line != NULL && store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581 ++cp->current_line;
1582
1583 return line;
1584 }
1585
1586 KeyTyped = FALSE;
1587 ++cp->current_line;
1588 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
1589 sourcing_lnum = wp->lnum;
1590 return vim_strsave(wp->line);
1591}
1592
1593/*
1594 * Store a line in "gap" so that a ":while" loop can execute it again.
1595 */
1596 static int
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001597store_loop_line(gap, line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598 garray_T *gap;
1599 char_u *line;
1600{
1601 if (ga_grow(gap, 1) == FAIL)
1602 return FAIL;
1603 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
1604 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = sourcing_lnum;
1605 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606 return OK;
1607}
1608
1609/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001610 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611 */
1612 static void
1613free_cmdlines(gap)
1614 garray_T *gap;
1615{
1616 while (gap->ga_len > 0)
1617 {
1618 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1619 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620 }
1621}
1622#endif
1623
1624/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001625 * If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals
1626 * "func". * Otherwise return TRUE when "fgetline" equals "func".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001627 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628 int
Bram Moolenaar89d40322006-08-29 15:30:07 +00001629getline_equal(fgetline, cookie, func)
1630 char_u *(*fgetline) __ARGS((int, void *, int));
Bram Moolenaar78a15312009-05-15 19:33:18 +00001631 void *cookie UNUSED; /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001632 char_u *(*func) __ARGS((int, void *, int));
1633{
1634#ifdef FEAT_EVAL
1635 char_u *(*gp) __ARGS((int, void *, int));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001636 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637
Bram Moolenaar89d40322006-08-29 15:30:07 +00001638 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001639 * function that's originally used to obtain the lines. This may be
1640 * nested several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001641 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001642 cp = (struct loop_cookie *)cookie;
1643 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644 {
1645 gp = cp->getline;
1646 cp = cp->cookie;
1647 }
1648 return gp == func;
1649#else
Bram Moolenaar89d40322006-08-29 15:30:07 +00001650 return fgetline == func;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651#endif
1652}
1653
1654#if defined(FEAT_EVAL) || defined(FEAT_MBYTE) || defined(PROTO)
1655/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001656 * If "fgetline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001657 * getline function. Otherwise return "cookie".
1658 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001659 void *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001660getline_cookie(fgetline, cookie)
Bram Moolenaar78a15312009-05-15 19:33:18 +00001661 char_u *(*fgetline) __ARGS((int, void *, int)) UNUSED;
Bram Moolenaar89d40322006-08-29 15:30:07 +00001662 void *cookie; /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001663{
1664# ifdef FEAT_EVAL
1665 char_u *(*gp) __ARGS((int, void *, int));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001666 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667
Bram Moolenaar89d40322006-08-29 15:30:07 +00001668 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001669 * cookie that's originally used to obtain the lines. This may be nested
Bram Moolenaar071d4272004-06-13 20:20:40 +00001670 * several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001671 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001672 cp = (struct loop_cookie *)cookie;
1673 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001674 {
1675 gp = cp->getline;
1676 cp = cp->cookie;
1677 }
1678 return cp;
1679# else
1680 return cookie;
1681# endif
1682}
1683#endif
1684
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001685
1686/*
1687 * Helper function to apply an offset for buffer commands, i.e. ":bdelete",
1688 * ":bwipeout", etc.
1689 * Returns the buffer number.
1690 */
1691 static int
1692compute_buffer_local_count(addr_type, lnum, offset)
1693 int addr_type;
1694 int lnum;
1695 int offset;
1696{
1697 buf_T *buf;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001698 buf_T *nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001699 int count = offset;
1700
1701 buf = firstbuf;
1702 while (buf->b_next != NULL && buf->b_fnum < lnum)
1703 buf = buf->b_next;
1704 while (count != 0)
1705 {
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001706 count += (offset < 0) ? 1 : -1;
1707 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1708 if (nextbuf == NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001709 break;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001710 buf = nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001711 if (addr_type == ADDR_LOADED_BUFFERS)
1712 /* skip over unloaded buffers */
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001713 while (buf->b_ml.ml_mfp == NULL)
1714 {
1715 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1716 if (nextbuf == NULL)
1717 break;
1718 buf = nextbuf;
1719 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001720 }
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001721 /* we might have gone too far, last buffer is not loadedd */
1722 if (addr_type == ADDR_LOADED_BUFFERS)
1723 while (buf->b_ml.ml_mfp == NULL)
1724 {
1725 nextbuf = (offset >= 0) ? buf->b_prev : buf->b_next;
1726 if (nextbuf == NULL)
1727 break;
1728 buf = nextbuf;
1729 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001730 return buf->b_fnum;
1731}
1732
Bram Moolenaarf240e182014-11-27 18:33:02 +01001733#ifdef FEAT_WINDOWS
1734static int current_win_nr __ARGS((win_T *win));
1735static int current_tab_nr __ARGS((tabpage_T *tab));
1736
1737 static int
1738current_win_nr(win)
1739 win_T *win;
1740{
1741 win_T *wp;
1742 int nr = 0;
1743
1744 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1745 {
1746 ++nr;
1747 if (wp == win)
1748 break;
1749 }
1750 return nr;
1751}
1752
1753 static int
1754current_tab_nr(tab)
1755 tabpage_T *tab;
1756{
1757 tabpage_T *tp;
1758 int nr = 0;
1759
1760 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
1761 {
1762 ++nr;
1763 if (tp == tab)
1764 break;
1765 }
1766 return nr;
1767}
1768
1769# define CURRENT_WIN_NR current_win_nr(curwin)
1770# define LAST_WIN_NR current_win_nr(NULL)
1771# define CURRENT_TAB_NR current_tab_nr(curtab)
1772# define LAST_TAB_NR current_tab_nr(NULL)
1773#else
1774# define CURRENT_WIN_NR 1
1775# define LAST_WIN_NR 1
1776# define CURRENT_TAB_NR 1
1777# define LAST_TAB_NR 1
1778#endif
1779
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001780
Bram Moolenaar071d4272004-06-13 20:20:40 +00001781/*
1782 * Execute one Ex command.
1783 *
1784 * If 'sourcing' is TRUE, the command will be included in the error message.
1785 *
1786 * 1. skip comment lines and leading space
1787 * 2. handle command modifiers
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001788 * 3. find the command
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001789 * 4. parse range
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001790 * 5. Parse the command.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001791 * 6. parse arguments
1792 * 7. switch on command name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793 *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001794 * Note: "fgetline" can be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795 *
1796 * This function may be called recursively!
1797 */
1798#if (_MSC_VER == 1200)
1799/*
Bram Moolenaared203462004-06-16 11:19:22 +00001800 * Avoid optimisation bug in VC++ version 6.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801 */
Bram Moolenaar281bdce2005-01-25 21:53:18 +00001802 #pragma optimize( "g", off )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803#endif
1804 static char_u *
1805do_one_cmd(cmdlinep, sourcing,
1806#ifdef FEAT_EVAL
1807 cstack,
1808#endif
Bram Moolenaar89d40322006-08-29 15:30:07 +00001809 fgetline, cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810 char_u **cmdlinep;
1811 int sourcing;
1812#ifdef FEAT_EVAL
1813 struct condstack *cstack;
1814#endif
Bram Moolenaar89d40322006-08-29 15:30:07 +00001815 char_u *(*fgetline) __ARGS((int, void *, int));
1816 void *cookie; /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817{
1818 char_u *p;
1819 linenr_T lnum;
1820 long n;
1821 char_u *errormsg = NULL; /* error message */
1822 exarg_T ea; /* Ex command arguments */
1823 long verbose_save = -1;
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001824 int save_msg_scroll = msg_scroll;
1825 int save_msg_silent = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826 int did_esilent = 0;
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001827#ifdef HAVE_SANDBOX
1828 int did_sandbox = FALSE;
1829#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830 cmdmod_T save_cmdmod;
1831 int ni; /* set when Not Implemented */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001832 char_u *cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833
1834 vim_memset(&ea, 0, sizeof(ea));
1835 ea.line1 = 1;
1836 ea.line2 = 1;
1837#ifdef FEAT_EVAL
1838 ++ex_nesting_level;
1839#endif
1840
Bram Moolenaar21691f82012-12-05 19:13:18 +01001841 /* When the last file has not been edited :q has to be typed twice. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842 if (quitmore
1843#ifdef FEAT_EVAL
1844 /* avoid that a function call in 'statusline' does this */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001845 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001846#endif
1847#ifdef FEAT_AUTOCMD
Bram Moolenaar21691f82012-12-05 19:13:18 +01001848 /* avoid that an autocommand, e.g. QuitPre, does this */
1849 && !getline_equal(fgetline, cookie, getnextac)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001850#endif
1851 )
1852 --quitmore;
1853
1854 /*
1855 * Reset browse, confirm, etc.. They are restored when returning, for
1856 * recursive calls.
1857 */
1858 save_cmdmod = cmdmod;
1859 vim_memset(&cmdmod, 0, sizeof(cmdmod));
1860
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001861 /* "#!anything" is handled like a comment. */
1862 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1863 goto doend;
1864
Bram Moolenaar071d4272004-06-13 20:20:40 +00001865 /*
1866 * Repeat until no more command modifiers are found.
1867 */
1868 ea.cmd = *cmdlinep;
1869 for (;;)
1870 {
1871/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001872 * 1. Skip comment lines and leading white space and colons.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001873 */
1874 while (*ea.cmd == ' ' || *ea.cmd == '\t' || *ea.cmd == ':')
1875 ++ea.cmd;
1876
1877 /* in ex mode, an empty line works like :+ */
1878 if (*ea.cmd == NUL && exmode_active
Bram Moolenaar89d40322006-08-29 15:30:07 +00001879 && (getline_equal(fgetline, cookie, getexmodeline)
1880 || getline_equal(fgetline, cookie, getexline))
Bram Moolenaardf177f62005-02-22 08:39:57 +00001881 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001882 {
1883 ea.cmd = (char_u *)"+";
1884 ex_pressedreturn = TRUE;
1885 }
1886
1887 /* ignore comment and empty lines */
Bram Moolenaarf998c042007-11-20 11:31:26 +00001888 if (*ea.cmd == '"')
1889 goto doend;
1890 if (*ea.cmd == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001891 {
1892 ex_pressedreturn = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001893 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001894 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895
1896/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001897 * 2. Handle command modifiers.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898 */
1899 p = ea.cmd;
1900 if (VIM_ISDIGIT(*ea.cmd))
1901 p = skipwhite(skipdigits(ea.cmd));
1902 switch (*p)
1903 {
1904 /* When adding an entry, also modify cmd_exists(). */
1905 case 'a': if (!checkforcmd(&ea.cmd, "aboveleft", 3))
1906 break;
1907#ifdef FEAT_WINDOWS
1908 cmdmod.split |= WSP_ABOVE;
1909#endif
1910 continue;
1911
1912 case 'b': if (checkforcmd(&ea.cmd, "belowright", 3))
1913 {
1914#ifdef FEAT_WINDOWS
1915 cmdmod.split |= WSP_BELOW;
1916#endif
1917 continue;
1918 }
1919 if (checkforcmd(&ea.cmd, "browse", 3))
1920 {
Bram Moolenaard812df62008-11-09 12:46:09 +00001921#ifdef FEAT_BROWSE_CMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001922 cmdmod.browse = TRUE;
1923#endif
1924 continue;
1925 }
1926 if (!checkforcmd(&ea.cmd, "botright", 2))
1927 break;
1928#ifdef FEAT_WINDOWS
1929 cmdmod.split |= WSP_BOT;
1930#endif
1931 continue;
1932
1933 case 'c': if (!checkforcmd(&ea.cmd, "confirm", 4))
1934 break;
1935#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1936 cmdmod.confirm = TRUE;
1937#endif
1938 continue;
1939
1940 case 'k': if (checkforcmd(&ea.cmd, "keepmarks", 3))
1941 {
1942 cmdmod.keepmarks = TRUE;
1943 continue;
1944 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001945 if (checkforcmd(&ea.cmd, "keepalt", 5))
1946 {
1947 cmdmod.keepalt = TRUE;
1948 continue;
1949 }
Bram Moolenaara939e432013-11-09 05:30:26 +01001950 if (checkforcmd(&ea.cmd, "keeppatterns", 5))
1951 {
1952 cmdmod.keeppatterns = TRUE;
1953 continue;
1954 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955 if (!checkforcmd(&ea.cmd, "keepjumps", 5))
1956 break;
1957 cmdmod.keepjumps = TRUE;
1958 continue;
1959
1960 /* ":hide" and ":hide | cmd" are not modifiers */
1961 case 'h': if (p != ea.cmd || !checkforcmd(&p, "hide", 3)
1962 || *p == NUL || ends_excmd(*p))
1963 break;
1964 ea.cmd = p;
1965 cmdmod.hide = TRUE;
1966 continue;
1967
1968 case 'l': if (checkforcmd(&ea.cmd, "lockmarks", 3))
1969 {
1970 cmdmod.lockmarks = TRUE;
1971 continue;
1972 }
1973
1974 if (!checkforcmd(&ea.cmd, "leftabove", 5))
1975 break;
1976#ifdef FEAT_WINDOWS
1977 cmdmod.split |= WSP_ABOVE;
1978#endif
1979 continue;
1980
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001981 case 'n': if (checkforcmd(&ea.cmd, "noautocmd", 3))
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001982 {
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001983#ifdef FEAT_AUTOCMD
1984 if (cmdmod.save_ei == NULL)
1985 {
1986 /* Set 'eventignore' to "all". Restore the
1987 * existing option value later. */
1988 cmdmod.save_ei = vim_strsave(p_ei);
1989 set_string_option_direct((char_u *)"ei", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001990 (char_u *)"all", OPT_FREE, SID_NONE);
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001991 }
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001992#endif
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001993 continue;
1994 }
1995 if (!checkforcmd(&ea.cmd, "noswapfile", 6))
1996 break;
1997 cmdmod.noswapfile = TRUE;
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001998 continue;
1999
Bram Moolenaar071d4272004-06-13 20:20:40 +00002000 case 'r': if (!checkforcmd(&ea.cmd, "rightbelow", 6))
2001 break;
2002#ifdef FEAT_WINDOWS
2003 cmdmod.split |= WSP_BELOW;
2004#endif
2005 continue;
2006
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002007 case 's': if (checkforcmd(&ea.cmd, "sandbox", 3))
2008 {
2009#ifdef HAVE_SANDBOX
2010 if (!did_sandbox)
2011 ++sandbox;
2012 did_sandbox = TRUE;
2013#endif
2014 continue;
2015 }
2016 if (!checkforcmd(&ea.cmd, "silent", 3))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002017 break;
Bram Moolenaar8e258a42009-07-09 13:55:43 +00002018 if (save_msg_silent == -1)
2019 save_msg_silent = msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002020 ++msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002021 if (*ea.cmd == '!' && !vim_iswhite(ea.cmd[-1]))
2022 {
2023 /* ":silent!", but not "silent !cmd" */
2024 ea.cmd = skipwhite(ea.cmd + 1);
2025 ++emsg_silent;
2026 ++did_esilent;
2027 }
2028 continue;
2029
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002030 case 't': if (checkforcmd(&p, "tab", 3))
2031 {
2032#ifdef FEAT_WINDOWS
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002033 if (vim_isdigit(*ea.cmd))
2034 cmdmod.tab = atoi((char *)ea.cmd) + 1;
2035 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002036 cmdmod.tab = tabpage_index(curtab) + 1;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002037 ea.cmd = p;
2038#endif
2039 continue;
2040 }
2041 if (!checkforcmd(&ea.cmd, "topleft", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002042 break;
2043#ifdef FEAT_WINDOWS
2044 cmdmod.split |= WSP_TOP;
2045#endif
2046 continue;
2047
Bram Moolenaar8e258a42009-07-09 13:55:43 +00002048 case 'u': if (!checkforcmd(&ea.cmd, "unsilent", 3))
2049 break;
2050 if (save_msg_silent == -1)
2051 save_msg_silent = msg_silent;
2052 msg_silent = 0;
2053 continue;
2054
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055 case 'v': if (checkforcmd(&ea.cmd, "vertical", 4))
2056 {
2057#ifdef FEAT_VERTSPLIT
2058 cmdmod.split |= WSP_VERT;
2059#endif
2060 continue;
2061 }
2062 if (!checkforcmd(&p, "verbose", 4))
2063 break;
2064 if (verbose_save < 0)
2065 verbose_save = p_verbose;
Bram Moolenaared203462004-06-16 11:19:22 +00002066 if (vim_isdigit(*ea.cmd))
2067 p_verbose = atoi((char *)ea.cmd);
2068 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069 p_verbose = 1;
2070 ea.cmd = p;
2071 continue;
2072 }
2073 break;
2074 }
2075
2076#ifdef FEAT_EVAL
2077 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
2078 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
2079#else
2080 ea.skip = (if_level > 0);
2081#endif
2082
2083#ifdef FEAT_EVAL
Bram Moolenaar05159a02005-02-26 23:04:13 +00002084# ifdef FEAT_PROFILE
2085 /* Count this line for profiling if ea.skip is FALSE. */
Bram Moolenaar371d5402006-03-20 21:47:49 +00002086 if (do_profiling == PROF_YES && !ea.skip)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002087 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002088 if (getline_equal(fgetline, cookie, get_func_line))
2089 func_line_exec(getline_cookie(fgetline, cookie));
2090 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +00002091 script_line_exec();
2092 }
2093#endif
2094
Bram Moolenaar071d4272004-06-13 20:20:40 +00002095 /* May go to debug mode. If this happens and the ">quit" debug command is
2096 * used, throw an interrupt exception and skip the next command. */
2097 dbg_check_breakpoint(&ea);
2098 if (!ea.skip && got_int)
2099 {
2100 ea.skip = TRUE;
2101 (void)do_intthrow(cstack);
2102 }
2103#endif
2104
2105/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002106 * 3. Skip over the range to find the command. Let "p" point to after it.
2107 *
2108 * We need the command to know what kind of range it uses.
2109 */
2110 cmd = ea.cmd;
2111 ea.cmd = skip_range(ea.cmd, NULL);
2112 if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
2113 ea.cmd = skipwhite(ea.cmd + 1);
2114 p = find_command(&ea, NULL);
2115
2116/*
2117 * 4. parse a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118 *
2119 * where 'addr' is:
2120 *
2121 * % (entire file)
2122 * $ [+-NUM]
2123 * 'x [+-NUM] (where x denotes a currently defined mark)
2124 * . [+-NUM]
2125 * [+-NUM]..
2126 * NUM
2127 *
2128 * The ea.cmd pointer is updated to point to the first character following the
2129 * range spec. If an initial address is found, but no second, the upper bound
2130 * is equal to the lower.
2131 */
2132
Bram Moolenaarf8a447c2014-11-30 22:51:06 +01002133 if (ea.cmdidx != CMD_SIZE
2134#ifdef FEAT_USR_CMDS
2135 && ea.cmdidx != CMD_USER
Bram Moolenaarabe382c2015-01-07 13:15:45 +01002136 && ea.cmdidx != CMD_USER_BUF
Bram Moolenaarf8a447c2014-11-30 22:51:06 +01002137#endif
2138 )
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002139 ea.addr_type = cmdnames[(int)ea.cmdidx].cmd_addr_type;
2140 else
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002141#ifdef FEAT_USR_CMDS
Bram Moolenaarabe382c2015-01-07 13:15:45 +01002142 if (ea.cmdidx != CMD_USER && ea.cmdidx != CMD_USER_BUF)
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002143#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002144 ea.addr_type = ADDR_LINES;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002145 /* ea.addr_type for user commands is set by find_ucmd */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002146 ea.cmd = cmd;
2147
Bram Moolenaar071d4272004-06-13 20:20:40 +00002148 /* repeat for all ',' or ';' separated addresses */
2149 for (;;)
2150 {
2151 ea.line1 = ea.line2;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002152 switch (ea.addr_type)
2153 {
2154 case ADDR_LINES:
2155 /* default is current line number */
2156 ea.line2 = curwin->w_cursor.lnum;
2157 break;
2158 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01002159 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002160 ea.line2 = lnum;
2161 break;
2162 case ADDR_ARGUMENTS:
2163 ea.line2 = curwin->w_arg_idx + 1;
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01002164 if (ea.line2 > ARGCOUNT)
2165 ea.line2 = ARGCOUNT;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002166 break;
2167 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002168 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002169 ea.line2 = curbuf->b_fnum;
2170 break;
2171 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01002172 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002173 ea.line2 = lnum;
2174 break;
2175 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176 ea.cmd = skipwhite(ea.cmd);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002177 lnum = get_address(&ea.cmd, ea.addr_type, ea.skip, ea.addr_count == 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178 if (ea.cmd == NULL) /* error detected */
2179 goto doend;
2180 if (lnum == MAXLNUM)
2181 {
2182 if (*ea.cmd == '%') /* '%' - all lines */
2183 {
Bram Moolenaar4d84d932014-11-30 14:50:16 +01002184 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185 ++ea.cmd;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002186 switch (ea.addr_type)
2187 {
2188 case ADDR_LINES:
2189 ea.line1 = 1;
2190 ea.line2 = curbuf->b_ml.ml_line_count;
2191 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002192 case ADDR_LOADED_BUFFERS:
Bram Moolenaar4d84d932014-11-30 14:50:16 +01002193 buf = firstbuf;
2194 while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL)
2195 buf = buf->b_next;
2196 ea.line1 = buf->b_fnum;
2197 buf = lastbuf;
2198 while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL)
2199 buf = buf->b_prev;
2200 ea.line2 = buf->b_fnum;
2201 break;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002202 case ADDR_BUFFERS:
Bram Moolenaar4d84d932014-11-30 14:50:16 +01002203 ea.line1 = firstbuf->b_fnum;
2204 ea.line2 = lastbuf->b_fnum;
2205 break;
2206 case ADDR_WINDOWS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002207 case ADDR_TABS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002208 if (IS_USER_CMDIDX(ea.cmdidx))
2209 {
2210 ea.line1 = 1;
2211 ea.line2 = ea.addr_type == ADDR_WINDOWS
2212 ? LAST_WIN_NR : LAST_TAB_NR;
2213 }
2214 else
2215 {
2216 /* there is no Vim command which uses '%' and
2217 * ADDR_WINDOWS or ADDR_TABS */
2218 errormsg = (char_u *)_(e_invrange);
2219 goto doend;
2220 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002221 break;
2222 case ADDR_ARGUMENTS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002223 if (ARGCOUNT == 0)
2224 ea.line1 = ea.line2 = 0;
2225 else
2226 {
2227 ea.line1 = 1;
2228 ea.line2 = ARGCOUNT;
2229 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002230 break;
2231 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002232 ++ea.addr_count;
2233 }
2234 /* '*' - visual area */
2235 else if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
2236 {
2237 pos_T *fp;
2238
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002239 if (ea.addr_type != ADDR_LINES)
2240 {
2241 errormsg = (char_u *)_(e_invrange);
2242 goto doend;
2243 }
2244
Bram Moolenaar071d4272004-06-13 20:20:40 +00002245 ++ea.cmd;
2246 if (!ea.skip)
2247 {
2248 fp = getmark('<', FALSE);
2249 if (check_mark(fp) == FAIL)
2250 goto doend;
2251 ea.line1 = fp->lnum;
2252 fp = getmark('>', FALSE);
2253 if (check_mark(fp) == FAIL)
2254 goto doend;
2255 ea.line2 = fp->lnum;
2256 ++ea.addr_count;
2257 }
2258 }
2259 }
2260 else
2261 ea.line2 = lnum;
2262 ea.addr_count++;
2263
2264 if (*ea.cmd == ';')
2265 {
2266 if (!ea.skip)
2267 curwin->w_cursor.lnum = ea.line2;
2268 }
2269 else if (*ea.cmd != ',')
2270 break;
2271 ++ea.cmd;
2272 }
2273
2274 /* One address given: set start and end lines */
2275 if (ea.addr_count == 1)
2276 {
2277 ea.line1 = ea.line2;
2278 /* ... but only implicit: really no address given */
2279 if (lnum == MAXLNUM)
2280 ea.addr_count = 0;
2281 }
2282
2283 /* Don't leave the cursor on an illegal line (caused by ';') */
2284 check_cursor_lnum();
2285
2286/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002287 * 5. Parse the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288 */
2289
2290 /*
2291 * Skip ':' and any white space
2292 */
2293 ea.cmd = skipwhite(ea.cmd);
2294 while (*ea.cmd == ':')
2295 ea.cmd = skipwhite(ea.cmd + 1);
2296
2297 /*
2298 * If we got a line, but no command, then go to the line.
2299 * If we find a '|' or '\n' we set ea.nextcmd.
2300 */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002301 if (*ea.cmd == NUL || *ea.cmd == '"'
2302 || (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002303 {
2304 /*
2305 * strange vi behaviour:
2306 * ":3" jumps to line 3
2307 * ":3|..." prints line 3
2308 * ":|" prints current line
2309 */
2310 if (ea.skip) /* skip this if inside :if */
2311 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00002312 if (*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313 {
2314 ea.cmdidx = CMD_print;
2315 ea.argt = RANGE+COUNT+TRLBAR;
2316 if ((errormsg = invalid_range(&ea)) == NULL)
2317 {
2318 correct_range(&ea);
2319 ex_print(&ea);
2320 }
2321 }
2322 else if (ea.addr_count != 0)
2323 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002324 if (ea.line2 > curbuf->b_ml.ml_line_count)
2325 {
2326 /* With '-' in 'cpoptions' a line number past the file is an
2327 * error, otherwise put it at the end of the file. */
2328 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
2329 ea.line2 = -1;
2330 else
2331 ea.line2 = curbuf->b_ml.ml_line_count;
2332 }
2333
2334 if (ea.line2 < 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002335 errormsg = (char_u *)_(e_invrange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002336 else
2337 {
2338 if (ea.line2 == 0)
2339 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002340 else
2341 curwin->w_cursor.lnum = ea.line2;
2342 beginline(BL_SOL | BL_FIX);
2343 }
2344 }
2345 goto doend;
2346 }
2347
Bram Moolenaard5005162014-08-22 23:05:54 +02002348#ifdef FEAT_AUTOCMD
2349 /* If this looks like an undefined user command and there are CmdUndefined
2350 * autocommands defined, trigger the matching autocommands. */
2351 if (p != NULL && ea.cmdidx == CMD_SIZE && !ea.skip
2352 && ASCII_ISUPPER(*ea.cmd)
2353 && has_cmdundefined())
2354 {
Bram Moolenaard5005162014-08-22 23:05:54 +02002355 int ret;
2356
Bram Moolenaar5a31b462014-08-23 14:16:20 +02002357 p = ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02002358 while (ASCII_ISALNUM(*p))
2359 ++p;
Bram Moolenaar120f4a82014-09-09 12:22:06 +02002360 p = vim_strnsave(ea.cmd, (int)(p - ea.cmd));
Bram Moolenaard5005162014-08-22 23:05:54 +02002361 ret = apply_autocmds(EVENT_CMDUNDEFINED, p, p, TRUE, NULL);
2362 vim_free(p);
2363 if (ret && !aborting())
2364 p = find_command(&ea, NULL);
2365 }
2366#endif
2367
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368#ifdef FEAT_USR_CMDS
2369 if (p == NULL)
2370 {
2371 if (!ea.skip)
2372 errormsg = (char_u *)_("E464: Ambiguous use of user-defined command");
2373 goto doend;
2374 }
2375 /* Check for wrong commands. */
2376 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78)
2377 {
2378 errormsg = uc_fun_cmd();
2379 goto doend;
2380 }
2381#endif
2382 if (ea.cmdidx == CMD_SIZE)
2383 {
2384 if (!ea.skip)
2385 {
2386 STRCPY(IObuff, _("E492: Not an editor command"));
2387 if (!sourcing)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002388 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389 errormsg = IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02002390 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391 }
2392 goto doend;
2393 }
2394
Bram Moolenaar958636c2014-10-21 20:01:58 +02002395 ni = (!IS_USER_CMDIDX(ea.cmdidx)
2396 && (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00002397#ifdef HAVE_EX_SCRIPT_NI
2398 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
2399#endif
2400 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401
2402#ifndef FEAT_EVAL
2403 /*
2404 * When the expression evaluation is disabled, recognize the ":if" and
2405 * ":endif" commands and ignore everything in between it.
2406 */
2407 if (ea.cmdidx == CMD_if)
2408 ++if_level;
2409 if (if_level)
2410 {
2411 if (ea.cmdidx == CMD_endif)
2412 --if_level;
2413 goto doend;
2414 }
2415
2416#endif
2417
Bram Moolenaar196b3b02008-06-20 16:51:41 +00002418 /* forced commands */
2419 if (*p == '!' && ea.cmdidx != CMD_substitute
2420 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421 {
2422 ++p;
2423 ea.forceit = TRUE;
2424 }
2425 else
2426 ea.forceit = FALSE;
2427
2428/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01002429 * 6. Parse arguments.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02002431 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002432 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433
2434 if (!ea.skip)
2435 {
2436#ifdef HAVE_SANDBOX
2437 if (sandbox != 0 && !(ea.argt & SBOXOK))
2438 {
2439 /* Command not allowed in sandbox. */
2440 errormsg = (char_u *)_(e_sandbox);
2441 goto doend;
2442 }
2443#endif
2444 if (!curbuf->b_p_ma && (ea.argt & MODIFY))
2445 {
2446 /* Command not allowed in non-'modifiable' buffer */
2447 errormsg = (char_u *)_(e_modifiable);
2448 goto doend;
2449 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002450
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002451 if (text_locked() && !(ea.argt & CMDWIN)
Bram Moolenaar958636c2014-10-21 20:01:58 +02002452 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002454 /* Command not allowed when editing the command line. */
2455#ifdef FEAT_CMDWIN
2456 if (cmdwin_type != 0)
2457 errormsg = (char_u *)_(e_cmdwin);
2458 else
2459#endif
2460 errormsg = (char_u *)_(e_secure);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461 goto doend;
2462 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002463#ifdef FEAT_AUTOCMD
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002464 /* Disallow editing another buffer when "curbuf_lock" is set.
2465 * Do allow ":edit" (check for argument later).
2466 * Do allow ":checktime" (it's postponed). */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002467 if (!(ea.argt & CMDWIN)
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002468 && ea.cmdidx != CMD_edit
2469 && ea.cmdidx != CMD_checktime
Bram Moolenaar958636c2014-10-21 20:01:58 +02002470 && !IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002471 && curbuf_locked())
2472 goto doend;
2473#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474
2475 if (!ni && !(ea.argt & RANGE) && ea.addr_count > 0)
2476 {
2477 /* no range allowed */
2478 errormsg = (char_u *)_(e_norange);
2479 goto doend;
2480 }
2481 }
2482
2483 if (!ni && !(ea.argt & BANG) && ea.forceit) /* no <!> allowed */
2484 {
2485 errormsg = (char_u *)_(e_nobang);
2486 goto doend;
2487 }
2488
2489 /*
2490 * Don't complain about the range if it is not used
2491 * (could happen if line_count is accidentally set to 0).
2492 */
2493 if (!ea.skip && !ni)
2494 {
2495 /*
2496 * If the range is backwards, ask for confirmation and, if given, swap
2497 * ea.line1 & ea.line2 so it's forwards again.
2498 * When global command is busy, don't ask, will fail below.
2499 */
2500 if (!global_busy && ea.line1 > ea.line2)
2501 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002502 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002504 if (sourcing || exmode_active)
2505 {
2506 errormsg = (char_u *)_("E493: Backwards range given");
2507 goto doend;
2508 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509 if (ask_yesno((char_u *)
2510 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002511 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512 }
2513 lnum = ea.line1;
2514 ea.line1 = ea.line2;
2515 ea.line2 = lnum;
2516 }
2517 if ((errormsg = invalid_range(&ea)) != NULL)
2518 goto doend;
2519 }
2520
2521 if ((ea.argt & NOTADR) && ea.addr_count == 0) /* default is 1, not cursor */
2522 ea.line2 = 1;
2523
2524 correct_range(&ea);
2525
2526#ifdef FEAT_FOLDING
2527 if (((ea.argt & WHOLEFOLD) || ea.addr_count >= 2) && !global_busy)
2528 {
2529 /* Put the first line at the start of a closed fold, put the last line
2530 * at the end of a closed fold. */
2531 (void)hasFolding(ea.line1, &ea.line1, NULL);
2532 (void)hasFolding(ea.line2, NULL, &ea.line2);
2533 }
2534#endif
2535
2536#ifdef FEAT_QUICKFIX
2537 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002538 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002539 * option here, so things like % get expanded.
2540 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002541 p = replace_makeprg(&ea, p, cmdlinep);
2542 if (p == NULL)
2543 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544#endif
2545
2546 /*
2547 * Skip to start of argument.
2548 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2549 */
2550 if (ea.cmdidx == CMD_bang)
2551 ea.arg = p;
2552 else
2553 ea.arg = skipwhite(p);
2554
2555 /*
2556 * Check for "++opt=val" argument.
2557 * Must be first, allow ":w ++enc=utf8 !cmd"
2558 */
2559 if (ea.argt & ARGOPT)
2560 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2561 if (getargopt(&ea) == FAIL && !ni)
2562 {
2563 errormsg = (char_u *)_(e_invarg);
2564 goto doend;
2565 }
2566
2567 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2568 {
2569 if (*ea.arg == '>') /* append */
2570 {
2571 if (*++ea.arg != '>') /* typed wrong */
2572 {
2573 errormsg = (char_u *)_("E494: Use w or w>>");
2574 goto doend;
2575 }
2576 ea.arg = skipwhite(ea.arg + 1);
2577 ea.append = TRUE;
2578 }
2579 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
2580 {
2581 ++ea.arg;
2582 ea.usefilter = TRUE;
2583 }
2584 }
2585
2586 if (ea.cmdidx == CMD_read)
2587 {
2588 if (ea.forceit)
2589 {
2590 ea.usefilter = TRUE; /* :r! filter if ea.forceit */
2591 ea.forceit = FALSE;
2592 }
2593 else if (*ea.arg == '!') /* :r !filter */
2594 {
2595 ++ea.arg;
2596 ea.usefilter = TRUE;
2597 }
2598 }
2599
2600 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2601 {
2602 ea.amount = 1;
2603 while (*ea.arg == *ea.cmd) /* count number of '>' or '<' */
2604 {
2605 ++ea.arg;
2606 ++ea.amount;
2607 }
2608 ea.arg = skipwhite(ea.arg);
2609 }
2610
2611 /*
2612 * Check for "+command" argument, before checking for next command.
2613 * Don't do this for ":read !cmd" and ":write !cmd".
2614 */
2615 if ((ea.argt & EDITCMD) && !ea.usefilter)
2616 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2617
2618 /*
2619 * Check for '|' to separate commands and '"' to start comments.
2620 * Don't do this for ":read !cmd" and ":write !cmd".
2621 */
2622 if ((ea.argt & TRLBAR) && !ea.usefilter)
2623 separate_nextcmd(&ea);
2624
2625 /*
2626 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002627 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2628 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002629 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002630 else if (ea.cmdidx == CMD_bang
2631 || ea.cmdidx == CMD_global
2632 || ea.cmdidx == CMD_vglobal
2633 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002634 {
2635 for (p = ea.arg; *p; ++p)
2636 {
2637 /* Remove one backslash before a newline, so that it's possible to
2638 * pass a newline to the shell and also a newline that is preceded
2639 * with a backslash. This makes it impossible to end a shell
2640 * command in a backslash, but that doesn't appear useful.
2641 * Halving the number of backslashes is incompatible with previous
2642 * versions. */
2643 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002644 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645 else if (*p == '\n')
2646 {
2647 ea.nextcmd = p + 1;
2648 *p = NUL;
2649 break;
2650 }
2651 }
2652 }
2653
2654 if ((ea.argt & DFLALL) && ea.addr_count == 0)
2655 {
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002656 buf_T *buf;
2657
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658 ea.line1 = 1;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002659 switch (ea.addr_type)
2660 {
2661 case ADDR_LINES:
2662 ea.line2 = curbuf->b_ml.ml_line_count;
2663 break;
2664 case ADDR_LOADED_BUFFERS:
2665 buf = firstbuf;
2666 while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL)
2667 buf = buf->b_next;
2668 ea.line1 = buf->b_fnum;
2669 buf = lastbuf;
2670 while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL)
2671 buf = buf->b_prev;
2672 ea.line2 = buf->b_fnum;
2673 break;
2674 case ADDR_BUFFERS:
2675 ea.line1 = firstbuf->b_fnum;
2676 ea.line2 = lastbuf->b_fnum;
2677 break;
2678 case ADDR_WINDOWS:
2679 ea.line2 = LAST_WIN_NR;
2680 break;
2681 case ADDR_TABS:
2682 ea.line2 = LAST_TAB_NR;
2683 break;
2684 case ADDR_ARGUMENTS:
2685 if (ARGCOUNT == 0)
2686 ea.line1 = ea.line2 = 0;
2687 else
2688 ea.line2 = ARGCOUNT;
2689 break;
2690 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691 }
2692
2693 /* accept numbered register only when no count allowed (:put) */
2694 if ( (ea.argt & REGSTR)
2695 && *ea.arg != NUL
Bram Moolenaar958636c2014-10-21 20:01:58 +02002696 /* Do not allow register = for user commands */
2697 && (!IS_USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698 && !((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)))
2699 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002700#ifndef FEAT_CLIPBOARD
2701 /* check these explicitly for a more specific error message */
2702 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002703 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002704 errormsg = (char_u *)_(e_invalidreg);
2705 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706 }
2707#endif
Bram Moolenaar958636c2014-10-21 20:01:58 +02002708 if (valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2709 && !IS_USER_CMDIDX(ea.cmdidx))))
Bram Moolenaar85de2062011-05-05 14:26:41 +02002710 {
2711 ea.regname = *ea.arg++;
2712#ifdef FEAT_EVAL
2713 /* for '=' register: accept the rest of the line as an expression */
2714 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2715 {
2716 set_expr_line(vim_strsave(ea.arg));
2717 ea.arg += STRLEN(ea.arg);
2718 }
2719#endif
2720 ea.arg = skipwhite(ea.arg);
2721 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002722 }
2723
2724 /*
2725 * Check for a count. When accepting a BUFNAME, don't use "123foo" as a
2726 * count, it's a buffer name.
2727 */
2728 if ((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)
2729 && (!(ea.argt & BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
2730 || vim_iswhite(*p)))
2731 {
2732 n = getdigits(&ea.arg);
2733 ea.arg = skipwhite(ea.arg);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002734 if (n <= 0 && !ni && (ea.argt & ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002735 {
2736 errormsg = (char_u *)_(e_zerocount);
2737 goto doend;
2738 }
2739 if (ea.argt & NOTADR) /* e.g. :buffer 2, :sleep 3 */
2740 {
2741 ea.line2 = n;
2742 if (ea.addr_count == 0)
2743 ea.addr_count = 1;
2744 }
2745 else
2746 {
2747 ea.line1 = ea.line2;
2748 ea.line2 += n - 1;
2749 ++ea.addr_count;
2750 /*
2751 * Be vi compatible: no error message for out of range.
2752 */
2753 if (ea.line2 > curbuf->b_ml.ml_line_count)
2754 ea.line2 = curbuf->b_ml.ml_line_count;
2755 }
2756 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002757
2758 /*
2759 * Check for flags: 'l', 'p' and '#'.
2760 */
2761 if (ea.argt & EXFLAGS)
2762 get_flags(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763 /* no arguments allowed */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002764 if (!ni && !(ea.argt & EXTRA) && *ea.arg != NUL
Bram Moolenaara2031822006-03-07 22:29:51 +00002765 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002766 {
2767 errormsg = (char_u *)_(e_trailing);
2768 goto doend;
2769 }
2770
2771 if (!ni && (ea.argt & NEEDARG) && *ea.arg == NUL)
2772 {
2773 errormsg = (char_u *)_(e_argreq);
2774 goto doend;
2775 }
2776
2777#ifdef FEAT_EVAL
2778 /*
2779 * Skip the command when it's not going to be executed.
2780 * The commands like :if, :endif, etc. always need to be executed.
2781 * Also make an exception for commands that handle a trailing command
2782 * themselves.
2783 */
2784 if (ea.skip)
2785 {
2786 switch (ea.cmdidx)
2787 {
2788 /* commands that need evaluation */
2789 case CMD_while:
2790 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002791 case CMD_for:
2792 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002793 case CMD_if:
2794 case CMD_elseif:
2795 case CMD_else:
2796 case CMD_endif:
2797 case CMD_try:
2798 case CMD_catch:
2799 case CMD_finally:
2800 case CMD_endtry:
2801 case CMD_function:
2802 break;
2803
2804 /* Commands that handle '|' themselves. Check: A command should
2805 * either have the TRLBAR flag, appear in this list or appear in
2806 * the list at ":help :bar". */
2807 case CMD_aboveleft:
2808 case CMD_and:
2809 case CMD_belowright:
2810 case CMD_botright:
2811 case CMD_browse:
2812 case CMD_call:
2813 case CMD_confirm:
2814 case CMD_delfunction:
2815 case CMD_djump:
2816 case CMD_dlist:
2817 case CMD_dsearch:
2818 case CMD_dsplit:
2819 case CMD_echo:
2820 case CMD_echoerr:
2821 case CMD_echomsg:
2822 case CMD_echon:
2823 case CMD_execute:
2824 case CMD_help:
2825 case CMD_hide:
2826 case CMD_ijump:
2827 case CMD_ilist:
2828 case CMD_isearch:
2829 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002830 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002831 case CMD_keepjumps:
2832 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01002833 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002834 case CMD_leftabove:
2835 case CMD_let:
2836 case CMD_lockmarks:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002837 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002838 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002839 case CMD_mzscheme:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01002840 case CMD_noautocmd:
2841 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002842 case CMD_perl:
2843 case CMD_psearch:
2844 case CMD_python:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002845 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002846 case CMD_python3:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002847 case CMD_return:
2848 case CMD_rightbelow:
2849 case CMD_ruby:
2850 case CMD_silent:
2851 case CMD_smagic:
2852 case CMD_snomagic:
2853 case CMD_substitute:
2854 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002855 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002856 case CMD_tcl:
2857 case CMD_throw:
2858 case CMD_tilde:
2859 case CMD_topleft:
2860 case CMD_unlet:
2861 case CMD_verbose:
2862 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002863 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002864 break;
2865
2866 default: goto doend;
2867 }
2868 }
2869#endif
2870
2871 if (ea.argt & XFILE)
2872 {
2873 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2874 goto doend;
2875 }
2876
2877#ifdef FEAT_LISTCMDS
2878 /*
2879 * Accept buffer name. Cannot be used at the same time with a buffer
2880 * number. Don't do this for a user command.
2881 */
2882 if ((ea.argt & BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
Bram Moolenaar958636c2014-10-21 20:01:58 +02002883 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002884 {
2885 /*
2886 * :bdelete, :bwipeout and :bunload take several arguments, separated
2887 * by spaces: find next space (skipping over escaped characters).
2888 * The others take one argument: ignore trailing spaces.
2889 */
2890 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2891 || ea.cmdidx == CMD_bunload)
2892 p = skiptowhite_esc(ea.arg);
2893 else
2894 {
2895 p = ea.arg + STRLEN(ea.arg);
2896 while (p > ea.arg && vim_iswhite(p[-1]))
2897 --p;
2898 }
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002899 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & BUFUNL) != 0,
2900 FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002901 if (ea.line2 < 0) /* failed */
2902 goto doend;
2903 ea.addr_count = 1;
2904 ea.arg = skipwhite(p);
2905 }
2906#endif
2907
2908/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01002909 * 7. Switch on command name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002910 *
2911 * The "ea" structure holds the arguments that can be used.
2912 */
2913 ea.cmdlinep = cmdlinep;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002914 ea.getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002915 ea.cookie = cookie;
2916#ifdef FEAT_EVAL
2917 ea.cstack = cstack;
2918#endif
2919
2920#ifdef FEAT_USR_CMDS
Bram Moolenaar958636c2014-10-21 20:01:58 +02002921 if (IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002922 {
2923 /*
2924 * Execute a user-defined command.
2925 */
2926 do_ucmd(&ea);
2927 }
2928 else
2929#endif
2930 {
2931 /*
2932 * Call the function to execute the command.
2933 */
2934 ea.errmsg = NULL;
2935 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2936 if (ea.errmsg != NULL)
2937 errormsg = (char_u *)_(ea.errmsg);
2938 }
2939
2940#ifdef FEAT_EVAL
2941 /*
2942 * If the command just executed called do_cmdline(), any throw or ":return"
2943 * or ":finish" encountered there must also check the cstack of the still
2944 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2945 * exception, or reanimate a returned function or finished script file and
2946 * return or finish it again.
2947 */
2948 if (need_rethrow)
2949 do_throw(cstack);
2950 else if (check_cstack)
2951 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002952 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002953 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002954 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002955 && current_func_returned())
2956 do_return(&ea, TRUE, FALSE, NULL);
2957 }
2958 need_rethrow = check_cstack = FALSE;
2959#endif
2960
2961doend:
2962 if (curwin->w_cursor.lnum == 0) /* can happen with zero line number */
2963 curwin->w_cursor.lnum = 1;
2964
2965 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2966 {
2967 if (sourcing)
2968 {
2969 if (errormsg != IObuff)
2970 {
2971 STRCPY(IObuff, errormsg);
2972 errormsg = IObuff;
2973 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002974 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002975 }
2976 emsg(errormsg);
2977 }
2978#ifdef FEAT_EVAL
2979 do_errthrow(cstack,
Bram Moolenaar958636c2014-10-21 20:01:58 +02002980 (ea.cmdidx != CMD_SIZE && !IS_USER_CMDIDX(ea.cmdidx))
2981 ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002982#endif
2983
2984 if (verbose_save >= 0)
2985 p_verbose = verbose_save;
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00002986#ifdef FEAT_AUTOCMD
2987 if (cmdmod.save_ei != NULL)
2988 {
2989 /* Restore 'eventignore' to the value before ":noautocmd". */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002990 set_string_option_direct((char_u *)"ei", -1, cmdmod.save_ei,
2991 OPT_FREE, SID_NONE);
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00002992 free_string_option(cmdmod.save_ei);
2993 }
2994#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002995
2996 cmdmod = save_cmdmod;
2997
Bram Moolenaar8e258a42009-07-09 13:55:43 +00002998 if (save_msg_silent != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002999 {
3000 /* messages could be enabled for a serious error, need to check if the
3001 * counters don't become negative */
Bram Moolenaarbecf4282009-09-30 11:24:36 +00003002 if (!did_emsg || msg_silent > save_msg_silent)
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003003 msg_silent = save_msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003004 emsg_silent -= did_esilent;
3005 if (emsg_silent < 0)
3006 emsg_silent = 0;
3007 /* Restore msg_scroll, it's set by file I/O commands, even when no
3008 * message is actually displayed. */
3009 msg_scroll = save_msg_scroll;
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003010
3011 /* "silent reg" or "silent echo x" inside "redir" leaves msg_col
3012 * somewhere in the line. Put it back in the first column. */
3013 if (redirecting())
3014 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003015 }
3016
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003017#ifdef HAVE_SANDBOX
3018 if (did_sandbox)
3019 --sandbox;
3020#endif
3021
Bram Moolenaar071d4272004-06-13 20:20:40 +00003022 if (ea.nextcmd && *ea.nextcmd == NUL) /* not really a next command */
3023 ea.nextcmd = NULL;
3024
3025#ifdef FEAT_EVAL
3026 --ex_nesting_level;
3027#endif
3028
3029 return ea.nextcmd;
3030}
3031#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00003032 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003033#endif
3034
3035/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003036 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003037 * If there is a match advance "pp" to the argument and return TRUE.
3038 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003039 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00003040checkforcmd(pp, cmd, len)
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003041 char_u **pp; /* start of command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003042 char *cmd; /* name of command */
3043 int len; /* required length */
3044{
3045 int i;
3046
3047 for (i = 0; cmd[i] != NUL; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003048 if (((char_u *)cmd)[i] != (*pp)[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003049 break;
3050 if (i >= len && !isalpha((*pp)[i]))
3051 {
3052 *pp = skipwhite(*pp + i);
3053 return TRUE;
3054 }
3055 return FALSE;
3056}
3057
3058/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003059 * Append "cmd" to the error message in IObuff.
3060 * Takes care of limiting the length and handling 0xa0, which would be
3061 * invisible otherwise.
3062 */
3063 static void
3064append_command(cmd)
3065 char_u *cmd;
3066{
3067 char_u *s = cmd;
3068 char_u *d;
3069
3070 STRCAT(IObuff, ": ");
3071 d = IObuff + STRLEN(IObuff);
3072 while (*s != NUL && d - IObuff < IOSIZE - 7)
3073 {
3074 if (
3075#ifdef FEAT_MBYTE
3076 enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) :
3077#endif
3078 *s == 0xa0)
3079 {
3080 s +=
3081#ifdef FEAT_MBYTE
3082 enc_utf8 ? 2 :
3083#endif
3084 1;
3085 STRCPY(d, "<a0>");
3086 d += 4;
3087 }
3088 else
3089 MB_COPY_CHAR(s, d);
3090 }
3091 *d = NUL;
3092}
3093
3094/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003095 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003096 * Start of the name can be found at eap->cmd.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003097 * Returns pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003098 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003099 * Returns NULL for an ambiguous user command.
3100 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003101 static char_u *
3102find_command(eap, full)
3103 exarg_T *eap;
Bram Moolenaar78a15312009-05-15 19:33:18 +00003104 int *full UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003105{
3106 int len;
3107 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003108 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109
3110 /*
3111 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00003112 * Exceptions:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113 * - the 'k' command can directly be followed by any character.
3114 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003115 * but :sre[wind] is another command, as are :scr[iptnames],
Bram Moolenaar071d4272004-06-13 20:20:40 +00003116 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00003117 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118 */
3119 p = eap->cmd;
3120 if (*p == 'k')
3121 {
3122 eap->cmdidx = CMD_k;
3123 ++p;
3124 }
3125 else if (p[0] == 's'
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003126 && ((p[1] == 'c' && p[2] != 's' && p[2] != 'r'
3127 && p[3] != 'i' && p[4] != 'p')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003128 || p[1] == 'g'
3129 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
3130 || p[1] == 'I'
3131 || (p[1] == 'r' && p[2] != 'e')))
3132 {
3133 eap->cmdidx = CMD_substitute;
3134 ++p;
3135 }
3136 else
3137 {
3138 while (ASCII_ISALPHA(*p))
3139 ++p;
Bram Moolenaarb6590522010-07-21 16:00:43 +02003140 /* for python 3.x support ":py3", ":python3", ":py3file", etc. */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02003141 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaarb6590522010-07-21 16:00:43 +02003142 while (ASCII_ISALNUM(*p))
3143 ++p;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02003144
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145 /* check for non-alpha command */
3146 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3147 ++p;
3148 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003149 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
3150 {
3151 /* Check for ":dl", ":dell", etc. to ":deletel": that's
3152 * :delete with the 'l' flag. Same for 'p'. */
3153 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003154 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00003155 break;
3156 if (i == len - 1)
3157 {
3158 --len;
3159 if (p[-1] == 'l')
3160 eap->flags |= EXFLAG_LIST;
3161 else
3162 eap->flags |= EXFLAG_PRINT;
3163 }
3164 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003165
3166 if (ASCII_ISLOWER(*eap->cmd))
3167 eap->cmdidx = cmdidxs[CharOrdLow(*eap->cmd)];
3168 else
3169 eap->cmdidx = cmdidxs[26];
3170
3171 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
3172 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
3173 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
3174 (size_t)len) == 0)
3175 {
3176#ifdef FEAT_EVAL
3177 if (full != NULL
3178 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
3179 *full = TRUE;
3180#endif
3181 break;
3182 }
3183
3184#ifdef FEAT_USR_CMDS
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01003185 /* Look for a user defined command as a last resort. Let ":Print" be
3186 * overruled by a user defined command. */
3187 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
3188 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003189 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003190 /* User defined commands may contain digits. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191 while (ASCII_ISALNUM(*p))
3192 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003193 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003194 }
3195#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003196 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003197 eap->cmdidx = CMD_SIZE;
3198 }
3199
3200 return p;
3201}
3202
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003203#ifdef FEAT_USR_CMDS
3204/*
3205 * Search for a user command that matches "eap->cmd".
3206 * Return cmdidx in "eap->cmdidx", flags in "eap->argt", idx in "eap->useridx".
3207 * Return a pointer to just after the command.
3208 * Return NULL if there is no matching command.
3209 */
3210 static char_u *
3211find_ucmd(eap, p, full, xp, compl)
3212 exarg_T *eap;
3213 char_u *p; /* end of the command (possibly including count) */
3214 int *full; /* set to TRUE for a full match */
3215 expand_T *xp; /* used for completion, NULL otherwise */
3216 int *compl; /* completion flags or NULL */
3217{
3218 int len = (int)(p - eap->cmd);
3219 int j, k, matchlen = 0;
3220 ucmd_T *uc;
3221 int found = FALSE;
3222 int possible = FALSE;
3223 char_u *cp, *np; /* Point into typed cmd and test name */
3224 garray_T *gap;
3225 int amb_local = FALSE; /* Found ambiguous buffer-local command,
3226 only full match global is accepted. */
3227
3228 /*
3229 * Look for buffer-local user commands first, then global ones.
3230 */
3231 gap = &curbuf->b_ucmds;
3232 for (;;)
3233 {
3234 for (j = 0; j < gap->ga_len; ++j)
3235 {
3236 uc = USER_CMD_GA(gap, j);
3237 cp = eap->cmd;
3238 np = uc->uc_name;
3239 k = 0;
3240 while (k < len && *np != NUL && *cp++ == *np++)
3241 k++;
3242 if (k == len || (*np == NUL && vim_isdigit(eap->cmd[k])))
3243 {
3244 /* If finding a second match, the command is ambiguous. But
3245 * not if a buffer-local command wasn't a full match and a
3246 * global command is a full match. */
3247 if (k == len && found && *np != NUL)
3248 {
3249 if (gap == &ucmds)
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003250 return NULL;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003251 amb_local = TRUE;
3252 }
3253
3254 if (!found || (k == len && *np == NUL))
3255 {
3256 /* If we matched up to a digit, then there could
3257 * be another command including the digit that we
3258 * should use instead.
3259 */
3260 if (k == len)
3261 found = TRUE;
3262 else
3263 possible = TRUE;
3264
3265 if (gap == &ucmds)
3266 eap->cmdidx = CMD_USER;
3267 else
3268 eap->cmdidx = CMD_USER_BUF;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003269 eap->argt = (long)uc->uc_argt;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003270 eap->useridx = j;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003271 eap->addr_type = uc->uc_addr_type;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003272
3273# ifdef FEAT_CMDL_COMPL
3274 if (compl != NULL)
3275 *compl = uc->uc_compl;
3276# ifdef FEAT_EVAL
3277 if (xp != NULL)
3278 {
3279 xp->xp_arg = uc->uc_compl_arg;
3280 xp->xp_scriptID = uc->uc_scriptID;
3281 }
3282# endif
3283# endif
3284 /* Do not search for further abbreviations
3285 * if this is an exact match. */
3286 matchlen = k;
3287 if (k == len && *np == NUL)
3288 {
3289 if (full != NULL)
3290 *full = TRUE;
3291 amb_local = FALSE;
3292 break;
3293 }
3294 }
3295 }
3296 }
3297
3298 /* Stop if we found a full match or searched all. */
3299 if (j < gap->ga_len || gap == &ucmds)
3300 break;
3301 gap = &ucmds;
3302 }
3303
3304 /* Only found ambiguous matches. */
3305 if (amb_local)
3306 {
3307 if (xp != NULL)
3308 xp->xp_context = EXPAND_UNSUCCESSFUL;
3309 return NULL;
3310 }
3311
3312 /* The match we found may be followed immediately by a number. Move "p"
3313 * back to point to it. */
3314 if (found || possible)
3315 return p + (matchlen - len);
3316 return p;
3317}
3318#endif
3319
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003321static struct cmdmod
3322{
3323 char *name;
3324 int minlen;
3325 int has_count; /* :123verbose :3tab */
3326} cmdmods[] = {
3327 {"aboveleft", 3, FALSE},
3328 {"belowright", 3, FALSE},
3329 {"botright", 2, FALSE},
3330 {"browse", 3, FALSE},
3331 {"confirm", 4, FALSE},
3332 {"hide", 3, FALSE},
3333 {"keepalt", 5, FALSE},
3334 {"keepjumps", 5, FALSE},
3335 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01003336 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003337 {"leftabove", 5, FALSE},
3338 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003339 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003340 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003341 {"rightbelow", 6, FALSE},
3342 {"sandbox", 3, FALSE},
3343 {"silent", 3, FALSE},
3344 {"tab", 3, TRUE},
3345 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003346 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003347 {"verbose", 4, TRUE},
3348 {"vertical", 4, FALSE},
3349};
3350
3351/*
3352 * Return length of a command modifier (including optional count).
3353 * Return zero when it's not a modifier.
3354 */
3355 int
3356modifier_len(cmd)
3357 char_u *cmd;
3358{
3359 int i, j;
3360 char_u *p = cmd;
3361
3362 if (VIM_ISDIGIT(*cmd))
3363 p = skipwhite(skipdigits(cmd));
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003364 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003365 {
3366 for (j = 0; p[j] != NUL; ++j)
3367 if (p[j] != cmdmods[i].name[j])
3368 break;
Bram Moolenaar2d473ab2013-06-12 17:12:24 +02003369 if (!ASCII_ISALPHA(p[j]) && j >= cmdmods[i].minlen
Bram Moolenaared53fb92007-11-24 20:50:24 +00003370 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003371 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003372 }
3373 return 0;
3374}
3375
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376/*
3377 * Return > 0 if an Ex command "name" exists.
3378 * Return 2 if there is an exact match.
3379 * Return 3 if there is an ambiguous match.
3380 */
3381 int
3382cmd_exists(name)
3383 char_u *name;
3384{
3385 exarg_T ea;
3386 int full = FALSE;
3387 int i;
3388 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003389 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390
3391 /* Check command modifiers. */
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003392 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393 {
3394 for (j = 0; name[j] != NUL; ++j)
3395 if (name[j] != cmdmods[i].name[j])
3396 break;
3397 if (name[j] == NUL && j >= cmdmods[i].minlen)
3398 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3399 }
3400
Bram Moolenaara9587612006-05-04 21:47:50 +00003401 /* Check built-in commands and user defined commands.
3402 * For ":2match" and ":3match" we need to skip the number. */
3403 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003405 p = find_command(&ea, &full);
3406 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003408 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3409 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003410 if (*skipwhite(p) != NUL)
3411 return 0; /* trailing garbage */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3413}
3414#endif
3415
3416/*
3417 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
3418 * we don't need/want deleted. Maybe this could be done better if we didn't
3419 * repeat all this stuff. The only problem is that they may not stay
3420 * perfectly compatible with each other, but then the command line syntax
3421 * probably won't change that much -- webb.
3422 */
3423 char_u *
3424set_one_cmd_context(xp, buff)
3425 expand_T *xp;
3426 char_u *buff; /* buffer for command string */
3427{
3428 char_u *p;
3429 char_u *cmd, *arg;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003430 int len = 0;
3431 exarg_T ea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3433 int compl = EXPAND_NOTHING;
3434#endif
3435#ifdef FEAT_CMDL_COMPL
3436 int delim;
3437#endif
3438 int forceit = FALSE;
3439 int usefilter = FALSE; /* filter instead of file name */
3440
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003441 ExpandInit(xp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442 xp->xp_pattern = buff;
3443 xp->xp_context = EXPAND_COMMANDS; /* Default until we get past command */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003444 ea.argt = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445
3446/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003447 * 1. skip comment lines and leading space, colons or bars
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448 */
3449 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
3450 ;
3451 xp->xp_pattern = cmd;
3452
3453 if (*cmd == NUL)
3454 return NULL;
3455 if (*cmd == '"') /* ignore comment lines */
3456 {
3457 xp->xp_context = EXPAND_NOTHING;
3458 return NULL;
3459 }
3460
3461/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003462 * 3. Skip over the range to find the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463 */
3464 cmd = skip_range(cmd, &xp->xp_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465 xp->xp_pattern = cmd;
3466 if (*cmd == NUL)
3467 return NULL;
3468 if (*cmd == '"')
3469 {
3470 xp->xp_context = EXPAND_NOTHING;
3471 return NULL;
3472 }
3473
3474 if (*cmd == '|' || *cmd == '\n')
3475 return cmd + 1; /* There's another command */
3476
3477 /*
3478 * Isolate the command and search for it in the command table.
3479 * Exceptions:
3480 * - the 'k' command can directly be followed by any character, but
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003481 * do accept "keepmarks", "keepalt" and "keepjumps".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
3483 */
3484 if (*cmd == 'k' && cmd[1] != 'e')
3485 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003486 ea.cmdidx = CMD_k;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487 p = cmd + 1;
3488 }
3489 else
3490 {
3491 p = cmd;
3492 while (ASCII_ISALPHA(*p) || *p == '*') /* Allow * wild card */
3493 ++p;
3494 /* check for non-alpha command */
3495 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3496 ++p;
Bram Moolenaar9f5d6002013-06-02 19:22:13 +02003497 /* for python 3.x: ":py3*" commands completion */
3498 if (cmd[0] == 'p' && cmd[1] == 'y' && p == cmd + 2 && *p == '3')
Bram Moolenaar893b2d72013-12-11 17:44:38 +01003499 {
Bram Moolenaar9f5d6002013-06-02 19:22:13 +02003500 ++p;
Bram Moolenaar893b2d72013-12-11 17:44:38 +01003501 while (ASCII_ISALPHA(*p) || *p == '*')
3502 ++p;
3503 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003504 len = (int)(p - cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003506 if (len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507 {
3508 xp->xp_context = EXPAND_UNSUCCESSFUL;
3509 return NULL;
3510 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003511 for (ea.cmdidx = (cmdidx_T)0; (int)ea.cmdidx < (int)CMD_SIZE;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01003512 ea.cmdidx = (cmdidx_T)((int)ea.cmdidx + 1))
3513 if (STRNCMP(cmdnames[(int)ea.cmdidx].cmd_name, cmd,
3514 (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003515 break;
3516
3517#ifdef FEAT_USR_CMDS
3518 if (cmd[0] >= 'A' && cmd[0] <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003519 while (ASCII_ISALNUM(*p) || *p == '*') /* Allow * wild card */
3520 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521#endif
3522 }
3523
3524 /*
3525 * If the cursor is touching the command, and it ends in an alpha-numeric
3526 * character, complete the command name.
3527 */
3528 if (*p == NUL && ASCII_ISALNUM(p[-1]))
3529 return NULL;
3530
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003531 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003532 {
3533 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
3534 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003535 ea.cmdidx = CMD_substitute;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536 p = cmd + 1;
3537 }
3538#ifdef FEAT_USR_CMDS
3539 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
3540 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003541 ea.cmd = cmd;
3542 p = find_ucmd(&ea, p, NULL, xp,
3543# if defined(FEAT_CMDL_COMPL)
3544 &compl
3545# else
3546 NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547# endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003548 );
Bram Moolenaarebefac62005-12-28 22:39:57 +00003549 if (p == NULL)
3550 ea.cmdidx = CMD_SIZE; /* ambiguous user command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551 }
3552#endif
3553 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003554 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555 {
3556 /* Not still touching the command and it was an illegal one */
3557 xp->xp_context = EXPAND_UNSUCCESSFUL;
3558 return NULL;
3559 }
3560
3561 xp->xp_context = EXPAND_NOTHING; /* Default now that we're past command */
3562
3563 if (*p == '!') /* forced commands */
3564 {
3565 forceit = TRUE;
3566 ++p;
3567 }
3568
3569/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003570 * 6. parse arguments
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02003572 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003573 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574
3575 arg = skipwhite(p);
3576
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003577 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578 {
3579 if (*arg == '>') /* append */
3580 {
3581 if (*++arg == '>')
3582 ++arg;
3583 arg = skipwhite(arg);
3584 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003585 else if (*arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586 {
3587 ++arg;
3588 usefilter = TRUE;
3589 }
3590 }
3591
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003592 if (ea.cmdidx == CMD_read)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003593 {
3594 usefilter = forceit; /* :r! filter if forced */
3595 if (*arg == '!') /* :r !filter */
3596 {
3597 ++arg;
3598 usefilter = TRUE;
3599 }
3600 }
3601
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003602 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603 {
3604 while (*arg == *cmd) /* allow any number of '>' or '<' */
3605 ++arg;
3606 arg = skipwhite(arg);
3607 }
3608
3609 /* Does command allow "+command"? */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003610 if ((ea.argt & EDITCMD) && !usefilter && *arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611 {
3612 /* Check if we're in the +command */
3613 p = arg + 1;
3614 arg = skip_cmd_arg(arg, FALSE);
3615
3616 /* Still touching the command after '+'? */
3617 if (*arg == NUL)
3618 return p;
3619
3620 /* Skip space(s) after +command to get to the real argument */
3621 arg = skipwhite(arg);
3622 }
3623
3624 /*
3625 * Check for '|' to separate commands and '"' to start comments.
3626 * Don't do this for ":read !cmd" and ":write !cmd".
3627 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003628 if ((ea.argt & TRLBAR) && !usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629 {
3630 p = arg;
3631 /* ":redir @" is not the start of a comment */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003632 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633 p += 2;
3634 while (*p)
3635 {
3636 if (*p == Ctrl_V)
3637 {
3638 if (p[1] != NUL)
3639 ++p;
3640 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003641 else if ( (*p == '"' && !(ea.argt & NOTRLCOM))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642 || *p == '|' || *p == '\n')
3643 {
3644 if (*(p - 1) != '\\')
3645 {
3646 if (*p == '|' || *p == '\n')
3647 return p + 1;
3648 return NULL; /* It's a comment */
3649 }
3650 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003651 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003652 }
3653 }
3654
3655 /* no arguments allowed */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003656 if (!(ea.argt & EXTRA) && *arg != NUL &&
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657 vim_strchr((char_u *)"|\"", *arg) == NULL)
3658 return NULL;
3659
3660 /* Find start of last argument (argument just before cursor): */
Bram Moolenaar848f8762012-07-25 17:22:23 +02003661 p = buff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662 xp->xp_pattern = p;
Bram Moolenaar09168a72012-08-02 21:24:42 +02003663 len = (int)STRLEN(buff);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003664 while (*p && p < buff + len)
3665 {
3666 if (*p == ' ' || *p == TAB)
3667 {
3668 /* argument starts after a space */
3669 xp->xp_pattern = ++p;
3670 }
3671 else
3672 {
3673 if (*p == '\\' && *(p + 1) != NUL)
3674 ++p; /* skip over escaped character */
3675 mb_ptr_adv(p);
3676 }
3677 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003678
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003679 if (ea.argt & XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003680 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003681 int c;
3682 int in_quote = FALSE;
3683 char_u *bow = NULL; /* Beginning of word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003684
3685 /*
3686 * Allow spaces within back-quotes to count as part of the argument
3687 * being expanded.
3688 */
3689 xp->xp_pattern = skipwhite(arg);
Bram Moolenaar6529c102007-08-18 15:47:34 +00003690 p = xp->xp_pattern;
3691 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003692 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003693#ifdef FEAT_MBYTE
3694 if (has_mbyte)
3695 c = mb_ptr2char(p);
3696 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003697#endif
Bram Moolenaar6529c102007-08-18 15:47:34 +00003698 c = *p;
3699 if (c == '\\' && p[1] != NUL)
3700 ++p;
3701 else if (c == '`')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003702 {
3703 if (!in_quote)
3704 {
3705 xp->xp_pattern = p;
3706 bow = p + 1;
3707 }
3708 in_quote = !in_quote;
3709 }
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003710 /* An argument can contain just about everything, except
3711 * characters that end the command and white space. */
3712 else if (c == '|' || c == '\n' || c == '"' || (vim_iswhite(c)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003713#ifdef SPACE_IN_FILENAME
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003714 && (!(ea.argt & NOSPC) || usefilter)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003715#endif
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003716 ))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003717 {
Bram Moolenaarcf5a5b82008-02-26 20:30:12 +00003718 len = 0; /* avoid getting stuck when space is in 'isfname' */
Bram Moolenaar6529c102007-08-18 15:47:34 +00003719 while (*p != NUL)
3720 {
3721#ifdef FEAT_MBYTE
3722 if (has_mbyte)
3723 c = mb_ptr2char(p);
3724 else
3725#endif
3726 c = *p;
Bram Moolenaardd87969c2007-08-21 13:07:12 +00003727 if (c == '`' || vim_isfilec_or_wc(c))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003728 break;
3729#ifdef FEAT_MBYTE
3730 if (has_mbyte)
3731 len = (*mb_ptr2len)(p);
3732 else
3733#endif
3734 len = 1;
3735 mb_ptr_adv(p);
3736 }
3737 if (in_quote)
3738 bow = p;
3739 else
3740 xp->xp_pattern = p;
3741 p -= len;
3742 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003743 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744 }
3745
3746 /*
3747 * If we are still inside the quotes, and we passed a space, just
3748 * expand from there.
3749 */
3750 if (bow != NULL && in_quote)
3751 xp->xp_pattern = bow;
3752 xp->xp_context = EXPAND_FILES;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003753
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003754 /* For a shell command more chars need to be escaped. */
3755 if (usefilter || ea.cmdidx == CMD_bang)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003756 {
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003757#ifndef BACKSLASH_IN_FILENAME
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003758 xp->xp_shell = TRUE;
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003759#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003760 /* When still after the command name expand executables. */
3761 if (xp->xp_pattern == skipwhite(arg))
Bram Moolenaar5c5b0942007-05-06 12:07:59 +00003762 xp->xp_context = EXPAND_SHELLCMD;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003763 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003764
3765 /* Check for environment variable */
3766 if (*xp->xp_pattern == '$'
3767#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
3768 || *xp->xp_pattern == '%'
3769#endif
3770 )
3771 {
3772 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
3773 if (!vim_isIDc(*p))
3774 break;
3775 if (*p == NUL)
3776 {
3777 xp->xp_context = EXPAND_ENV_VARS;
3778 ++xp->xp_pattern;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003779#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3780 /* Avoid that the assignment uses EXPAND_FILES again. */
Bram Moolenaara466c992005-07-09 21:03:22 +00003781 if (compl != EXPAND_USER_DEFINED && compl != EXPAND_USER_LIST)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003782 compl = EXPAND_ENV_VARS;
3783#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003784 }
3785 }
Bram Moolenaar24305862012-08-15 14:05:05 +02003786#if defined(FEAT_CMDL_COMPL)
3787 /* Check for user names */
3788 if (*xp->xp_pattern == '~')
3789 {
3790 for (p = xp->xp_pattern + 1; *p != NUL && *p != '/'; ++p)
3791 ;
3792 /* Complete ~user only if it partially matches a user name.
3793 * A full match ~user<Tab> will be replaced by user's home
3794 * directory i.e. something like ~user<Tab> -> /home/user/ */
3795 if (*p == NUL && p > xp->xp_pattern + 1
3796 && match_user(xp->xp_pattern + 1) == 1)
3797 {
3798 xp->xp_context = EXPAND_USER;
3799 ++xp->xp_pattern;
3800 }
3801 }
3802#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803 }
3804
3805/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003806 * 6. Switch on command name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003808 switch (ea.cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003809 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003810 case CMD_find:
3811 case CMD_sfind:
3812 case CMD_tabfind:
Bram Moolenaarc24b6972010-08-16 22:34:29 +02003813 if (xp->xp_context == EXPAND_FILES)
3814 xp->xp_context = EXPAND_FILES_IN_PATH;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003815 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003816 case CMD_cd:
3817 case CMD_chdir:
3818 case CMD_lcd:
3819 case CMD_lchdir:
3820 if (xp->xp_context == EXPAND_FILES)
3821 xp->xp_context = EXPAND_DIRECTORIES;
3822 break;
3823 case CMD_help:
3824 xp->xp_context = EXPAND_HELP;
3825 xp->xp_pattern = arg;
3826 break;
3827
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003828 /* Command modifiers: return the argument.
3829 * Also for commands with an argument that is a command. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830 case CMD_aboveleft:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003831 case CMD_argdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003832 case CMD_belowright:
3833 case CMD_botright:
3834 case CMD_browse:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003835 case CMD_bufdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836 case CMD_confirm:
Bram Moolenaardf177f62005-02-22 08:39:57 +00003837 case CMD_debug:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003838 case CMD_folddoclosed:
3839 case CMD_folddoopen:
3840 case CMD_hide:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003841 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003842 case CMD_keepjumps:
3843 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01003844 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003845 case CMD_leftabove:
3846 case CMD_lockmarks:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003847 case CMD_noautocmd:
3848 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003849 case CMD_rightbelow:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003850 case CMD_sandbox:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851 case CMD_silent:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003852 case CMD_tab:
Bram Moolenaar70baa402013-06-16 16:14:03 +02003853 case CMD_tabdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854 case CMD_topleft:
3855 case CMD_verbose:
3856 case CMD_vertical:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003857 case CMD_windo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003858 return arg;
3859
Bram Moolenaar4f688582007-07-24 12:34:30 +00003860#ifdef FEAT_CMDL_COMPL
3861# ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862 case CMD_match:
3863 if (*arg == NUL || !ends_excmd(*arg))
3864 {
Bram Moolenaar4f688582007-07-24 12:34:30 +00003865 /* also complete "None" */
3866 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867 arg = skipwhite(skiptowhite(arg));
3868 if (*arg != NUL)
3869 {
3870 xp->xp_context = EXPAND_NOTHING;
3871 arg = skip_regexp(arg + 1, *arg, p_magic, NULL);
3872 }
3873 }
3874 return find_nextcmd(arg);
Bram Moolenaar4f688582007-07-24 12:34:30 +00003875# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003876
Bram Moolenaar071d4272004-06-13 20:20:40 +00003877/*
3878 * All completion for the +cmdline_compl feature goes here.
3879 */
3880
3881# ifdef FEAT_USR_CMDS
3882 case CMD_command:
3883 /* Check for attributes */
3884 while (*arg == '-')
3885 {
3886 arg++; /* Skip "-" */
3887 p = skiptowhite(arg);
3888 if (*p == NUL)
3889 {
3890 /* Cursor is still in the attribute */
3891 p = vim_strchr(arg, '=');
3892 if (p == NULL)
3893 {
3894 /* No "=", so complete attribute names */
3895 xp->xp_context = EXPAND_USER_CMD_FLAGS;
3896 xp->xp_pattern = arg;
3897 return NULL;
3898 }
3899
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003900 /* For the -complete, -nargs and -addr attributes, we complete
Bram Moolenaar071d4272004-06-13 20:20:40 +00003901 * their arguments as well.
3902 */
3903 if (STRNICMP(arg, "complete", p - arg) == 0)
3904 {
3905 xp->xp_context = EXPAND_USER_COMPLETE;
3906 xp->xp_pattern = p + 1;
3907 return NULL;
3908 }
3909 else if (STRNICMP(arg, "nargs", p - arg) == 0)
3910 {
3911 xp->xp_context = EXPAND_USER_NARGS;
3912 xp->xp_pattern = p + 1;
3913 return NULL;
3914 }
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003915 else if (STRNICMP(arg, "addr", p - arg) == 0)
3916 {
3917 xp->xp_context = EXPAND_USER_ADDR_TYPE;
3918 xp->xp_pattern = p + 1;
3919 return NULL;
3920 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003921 return NULL;
3922 }
3923 arg = skipwhite(p);
3924 }
3925
3926 /* After the attributes comes the new command name */
3927 p = skiptowhite(arg);
3928 if (*p == NUL)
3929 {
3930 xp->xp_context = EXPAND_USER_COMMANDS;
3931 xp->xp_pattern = arg;
3932 break;
3933 }
3934
3935 /* And finally comes a normal command */
3936 return skipwhite(p);
3937
3938 case CMD_delcommand:
3939 xp->xp_context = EXPAND_USER_COMMANDS;
3940 xp->xp_pattern = arg;
3941 break;
3942# endif
3943
3944 case CMD_global:
3945 case CMD_vglobal:
3946 delim = *arg; /* get the delimiter */
3947 if (delim)
3948 ++arg; /* skip delimiter if there is one */
3949
3950 while (arg[0] != NUL && arg[0] != delim)
3951 {
3952 if (arg[0] == '\\' && arg[1] != NUL)
3953 ++arg;
3954 ++arg;
3955 }
3956 if (arg[0] != NUL)
3957 return arg + 1;
3958 break;
3959 case CMD_and:
3960 case CMD_substitute:
3961 delim = *arg;
3962 if (delim)
3963 {
3964 /* skip "from" part */
3965 ++arg;
3966 arg = skip_regexp(arg, delim, p_magic, NULL);
3967 }
3968 /* skip "to" part */
3969 while (arg[0] != NUL && arg[0] != delim)
3970 {
3971 if (arg[0] == '\\' && arg[1] != NUL)
3972 ++arg;
3973 ++arg;
3974 }
3975 if (arg[0] != NUL) /* skip delimiter */
3976 ++arg;
3977 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
3978 ++arg;
3979 if (arg[0] != NUL)
3980 return arg;
3981 break;
3982 case CMD_isearch:
3983 case CMD_dsearch:
3984 case CMD_ilist:
3985 case CMD_dlist:
3986 case CMD_ijump:
3987 case CMD_psearch:
3988 case CMD_djump:
3989 case CMD_isplit:
3990 case CMD_dsplit:
3991 arg = skipwhite(skipdigits(arg)); /* skip count */
3992 if (*arg == '/') /* Match regexp, not just whole words */
3993 {
3994 for (++arg; *arg && *arg != '/'; arg++)
3995 if (*arg == '\\' && arg[1] != NUL)
3996 arg++;
3997 if (*arg)
3998 {
3999 arg = skipwhite(arg + 1);
4000
4001 /* Check for trailing illegal characters */
4002 if (*arg && vim_strchr((char_u *)"|\"\n", *arg) == NULL)
4003 xp->xp_context = EXPAND_NOTHING;
4004 else
4005 return arg;
4006 }
4007 }
4008 break;
4009#ifdef FEAT_AUTOCMD
4010 case CMD_autocmd:
4011 return set_context_in_autocmd(xp, arg, FALSE);
4012
4013 case CMD_doautocmd:
Bram Moolenaar73a9d7b2008-11-06 16:16:44 +00004014 case CMD_doautoall:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004015 return set_context_in_autocmd(xp, arg, TRUE);
4016#endif
4017 case CMD_set:
4018 set_context_in_set_cmd(xp, arg, 0);
4019 break;
4020 case CMD_setglobal:
4021 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
4022 break;
4023 case CMD_setlocal:
4024 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
4025 break;
4026 case CMD_tag:
4027 case CMD_stag:
4028 case CMD_ptag:
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00004029 case CMD_ltag:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004030 case CMD_tselect:
4031 case CMD_stselect:
4032 case CMD_ptselect:
4033 case CMD_tjump:
4034 case CMD_stjump:
4035 case CMD_ptjump:
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00004036 if (*p_wop != NUL)
4037 xp->xp_context = EXPAND_TAGS_LISTFILES;
4038 else
4039 xp->xp_context = EXPAND_TAGS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004040 xp->xp_pattern = arg;
4041 break;
4042 case CMD_augroup:
4043 xp->xp_context = EXPAND_AUGROUP;
4044 xp->xp_pattern = arg;
4045 break;
4046#ifdef FEAT_SYN_HL
4047 case CMD_syntax:
4048 set_context_in_syntax_cmd(xp, arg);
4049 break;
4050#endif
4051#ifdef FEAT_EVAL
4052 case CMD_let:
4053 case CMD_if:
4054 case CMD_elseif:
4055 case CMD_while:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00004056 case CMD_for:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004057 case CMD_echo:
4058 case CMD_echon:
4059 case CMD_execute:
4060 case CMD_echomsg:
4061 case CMD_echoerr:
4062 case CMD_call:
4063 case CMD_return:
Bram Moolenaar52b4b552005-03-07 23:00:57 +00004064 set_context_for_expression(xp, arg, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065 break;
4066
4067 case CMD_unlet:
4068 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
4069 arg = xp->xp_pattern + 1;
4070 xp->xp_context = EXPAND_USER_VARS;
4071 xp->xp_pattern = arg;
4072 break;
4073
4074 case CMD_function:
4075 case CMD_delfunction:
4076 xp->xp_context = EXPAND_USER_FUNC;
4077 xp->xp_pattern = arg;
4078 break;
4079
4080 case CMD_echohl:
Bram Moolenaar4f688582007-07-24 12:34:30 +00004081 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082 break;
4083#endif
4084 case CMD_highlight:
4085 set_context_in_highlight_cmd(xp, arg);
4086 break;
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004087#ifdef FEAT_CSCOPE
4088 case CMD_cscope:
Bram Moolenaar7bfef802009-04-22 14:25:01 +00004089 case CMD_lcscope:
4090 case CMD_scscope:
4091 set_context_in_cscope_cmd(xp, arg, ea.cmdidx);
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004092 break;
4093#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00004094#ifdef FEAT_SIGNS
4095 case CMD_sign:
4096 set_context_in_sign_cmd(xp, arg);
4097 break;
4098#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099#ifdef FEAT_LISTCMDS
4100 case CMD_bdelete:
4101 case CMD_bwipeout:
4102 case CMD_bunload:
4103 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
4104 arg = xp->xp_pattern + 1;
4105 /*FALLTHROUGH*/
4106 case CMD_buffer:
4107 case CMD_sbuffer:
4108 case CMD_checktime:
4109 xp->xp_context = EXPAND_BUFFERS;
4110 xp->xp_pattern = arg;
4111 break;
4112#endif
4113#ifdef FEAT_USR_CMDS
4114 case CMD_USER:
4115 case CMD_USER_BUF:
4116 if (compl != EXPAND_NOTHING)
4117 {
4118 /* XFILE: file names are handled above */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00004119 if (!(ea.argt & XFILE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004120 {
4121# ifdef FEAT_MENU
4122 if (compl == EXPAND_MENUS)
4123 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
4124# endif
4125 if (compl == EXPAND_COMMANDS)
4126 return arg;
4127 if (compl == EXPAND_MAPPINGS)
4128 return set_context_in_map_cmd(xp, (char_u *)"map",
4129 arg, forceit, FALSE, FALSE, CMD_map);
Bram Moolenaar848f8762012-07-25 17:22:23 +02004130 /* Find start of last argument. */
4131 p = arg;
4132 while (*p)
4133 {
4134 if (*p == ' ')
Bram Moolenaar848f8762012-07-25 17:22:23 +02004135 /* argument starts after a space */
4136 arg = p + 1;
Bram Moolenaara07c8312012-07-27 21:12:07 +02004137 else if (*p == '\\' && *(p + 1) != NUL)
4138 ++p; /* skip over escaped character */
4139 mb_ptr_adv(p);
Bram Moolenaar848f8762012-07-25 17:22:23 +02004140 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141 xp->xp_pattern = arg;
4142 }
4143 xp->xp_context = compl;
4144 }
4145 break;
4146#endif
4147 case CMD_map: case CMD_noremap:
4148 case CMD_nmap: case CMD_nnoremap:
4149 case CMD_vmap: case CMD_vnoremap:
4150 case CMD_omap: case CMD_onoremap:
4151 case CMD_imap: case CMD_inoremap:
4152 case CMD_cmap: case CMD_cnoremap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004153 case CMD_lmap: case CMD_lnoremap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004154 case CMD_smap: case CMD_snoremap:
4155 case CMD_xmap: case CMD_xnoremap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004157 FALSE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004158 case CMD_unmap:
4159 case CMD_nunmap:
4160 case CMD_vunmap:
4161 case CMD_ounmap:
4162 case CMD_iunmap:
4163 case CMD_cunmap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004164 case CMD_lunmap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004165 case CMD_sunmap:
4166 case CMD_xunmap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004168 FALSE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169 case CMD_abbreviate: case CMD_noreabbrev:
4170 case CMD_cabbrev: case CMD_cnoreabbrev:
4171 case CMD_iabbrev: case CMD_inoreabbrev:
4172 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004173 TRUE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174 case CMD_unabbreviate:
4175 case CMD_cunabbrev:
4176 case CMD_iunabbrev:
4177 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004178 TRUE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179#ifdef FEAT_MENU
4180 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
4181 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
4182 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
4183 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
4184 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
4185 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
4186 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
4187 case CMD_tmenu: case CMD_tunmenu:
4188 case CMD_popup: case CMD_tearoff: case CMD_emenu:
4189 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
4190#endif
4191
4192 case CMD_colorscheme:
4193 xp->xp_context = EXPAND_COLORS;
4194 xp->xp_pattern = arg;
4195 break;
4196
4197 case CMD_compiler:
4198 xp->xp_context = EXPAND_COMPILER;
4199 xp->xp_pattern = arg;
4200 break;
4201
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004202 case CMD_ownsyntax:
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004203 xp->xp_context = EXPAND_OWNSYNTAX;
4204 xp->xp_pattern = arg;
4205 break;
4206
4207 case CMD_setfiletype:
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004208 xp->xp_context = EXPAND_FILETYPE;
4209 xp->xp_pattern = arg;
4210 break;
4211
Bram Moolenaar071d4272004-06-13 20:20:40 +00004212#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
4213 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
4214 case CMD_language:
Bram Moolenaara660dc82011-05-25 12:51:22 +02004215 p = skiptowhite(arg);
4216 if (*p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004217 {
4218 xp->xp_context = EXPAND_LANGUAGE;
4219 xp->xp_pattern = arg;
4220 }
4221 else
Bram Moolenaara660dc82011-05-25 12:51:22 +02004222 {
4223 if ( STRNCMP(arg, "messages", p - arg) == 0
4224 || STRNCMP(arg, "ctype", p - arg) == 0
4225 || STRNCMP(arg, "time", p - arg) == 0)
4226 {
4227 xp->xp_context = EXPAND_LOCALES;
4228 xp->xp_pattern = skipwhite(p);
4229 }
4230 else
4231 xp->xp_context = EXPAND_NOTHING;
4232 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233 break;
4234#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01004235#if defined(FEAT_PROFILE)
4236 case CMD_profile:
4237 set_context_in_profile_cmd(xp, arg);
4238 break;
4239#endif
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01004240 case CMD_behave:
4241 xp->xp_context = EXPAND_BEHAVE;
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004242 xp->xp_pattern = arg;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01004243 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004245#if defined(FEAT_CMDHIST)
4246 case CMD_history:
4247 xp->xp_context = EXPAND_HISTORY;
4248 xp->xp_pattern = arg;
4249 break;
4250#endif
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02004251#if defined(FEAT_PROFILE)
4252 case CMD_syntime:
4253 xp->xp_context = EXPAND_SYNTIME;
4254 xp->xp_pattern = arg;
4255 break;
4256#endif
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004257
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258#endif /* FEAT_CMDL_COMPL */
4259
4260 default:
4261 break;
4262 }
4263 return NULL;
4264}
4265
4266/*
4267 * skip a range specifier of the form: addr [,addr] [;addr] ..
4268 *
4269 * Backslashed delimiters after / or ? will be skipped, and commands will
4270 * not be expanded between /'s and ?'s or after "'".
4271 *
Bram Moolenaardf177f62005-02-22 08:39:57 +00004272 * Also skip white space and ":" characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004273 * Returns the "cmd" pointer advanced to beyond the range.
4274 */
4275 char_u *
4276skip_range(cmd, ctx)
4277 char_u *cmd;
4278 int *ctx; /* pointer to xp_context or NULL */
4279{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004280 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281
Bram Moolenaardf177f62005-02-22 08:39:57 +00004282 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004283 {
4284 if (*cmd == '\'')
4285 {
4286 if (*++cmd == NUL && ctx != NULL)
4287 *ctx = EXPAND_NOTHING;
4288 }
4289 else if (*cmd == '/' || *cmd == '?')
4290 {
4291 delim = *cmd++;
4292 while (*cmd != NUL && *cmd != delim)
4293 if (*cmd++ == '\\' && *cmd != NUL)
4294 ++cmd;
4295 if (*cmd == NUL && ctx != NULL)
4296 *ctx = EXPAND_NOTHING;
4297 }
4298 if (*cmd != NUL)
4299 ++cmd;
4300 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004301
4302 /* Skip ":" and white space. */
4303 while (*cmd == ':')
4304 cmd = skipwhite(cmd + 1);
4305
Bram Moolenaar071d4272004-06-13 20:20:40 +00004306 return cmd;
4307}
4308
4309/*
4310 * get a single EX address
4311 *
4312 * Set ptr to the next character after the part that was interpreted.
4313 * Set ptr to NULL when an error is encountered.
4314 *
4315 * Return MAXLNUM when no Ex address was found.
4316 */
4317 static linenr_T
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004318get_address(ptr, addr_type, skip, to_other_file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319 char_u **ptr;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004320 int addr_type; /* flag: one of ADDR_LINES, ... */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004321 int skip; /* only skip the address, don't use it */
4322 int to_other_file; /* flag: may jump to other file */
4323{
4324 int c;
4325 int i;
4326 long n;
4327 char_u *cmd;
4328 pos_T pos;
4329 pos_T *fp;
4330 linenr_T lnum;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004331 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004332
4333 cmd = skipwhite(*ptr);
4334 lnum = MAXLNUM;
4335 do
4336 {
4337 switch (*cmd)
4338 {
4339 case '.': /* '.' - Cursor position */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004340 ++cmd;
4341 switch (addr_type)
4342 {
4343 case ADDR_LINES:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004344 lnum = curwin->w_cursor.lnum;
4345 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004346 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004347 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004348 break;
4349 case ADDR_ARGUMENTS:
4350 lnum = curwin->w_arg_idx + 1;
4351 break;
4352 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004353 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004354 lnum = curbuf->b_fnum;
4355 break;
4356 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004357 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004358 break;
4359 }
4360 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004361
4362 case '$': /* '$' - last line */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004363 ++cmd;
4364 switch (addr_type)
4365 {
4366 case ADDR_LINES:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004367 lnum = curbuf->b_ml.ml_line_count;
4368 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004369 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004370 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004371 break;
4372 case ADDR_ARGUMENTS:
4373 lnum = ARGCOUNT;
4374 break;
4375 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004376 buf = lastbuf;
4377 while (buf->b_ml.ml_mfp == NULL)
4378 {
4379 if (buf->b_prev == NULL)
4380 break;
4381 buf = buf->b_prev;
4382 }
4383 lnum = buf->b_fnum;
4384 break;
4385 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004386 lnum = lastbuf->b_fnum;
4387 break;
4388 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004389 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004390 break;
4391 }
4392 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004393
4394 case '\'': /* ''' - mark */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004395 if (*++cmd == NUL)
4396 {
4397 cmd = NULL;
4398 goto error;
4399 }
4400 if (addr_type != ADDR_LINES)
4401 {
4402 EMSG(_(e_invaddr));
4403 goto error;
4404 }
4405 if (skip)
4406 ++cmd;
4407 else
4408 {
4409 /* Only accept a mark in another file when it is
4410 * used by itself: ":'M". */
4411 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
4412 ++cmd;
4413 if (fp == (pos_T *)-1)
4414 /* Jumped to another file. */
4415 lnum = curwin->w_cursor.lnum;
4416 else
4417 {
4418 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004419 {
4420 cmd = NULL;
4421 goto error;
4422 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004423 lnum = fp->lnum;
4424 }
4425 }
4426 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004427
4428 case '/':
4429 case '?': /* '/' or '?' - search */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004430 c = *cmd++;
4431 if (addr_type != ADDR_LINES)
4432 {
4433 EMSG(_(e_invaddr));
4434 goto error;
4435 }
4436 if (skip) /* skip "/pat/" */
4437 {
4438 cmd = skip_regexp(cmd, c, (int)p_magic, NULL);
4439 if (*cmd == c)
4440 ++cmd;
4441 }
4442 else
4443 {
4444 pos = curwin->w_cursor; /* save curwin->w_cursor */
4445 /*
4446 * When '/' or '?' follows another address, start
4447 * from there.
4448 */
4449 if (lnum != MAXLNUM)
4450 curwin->w_cursor.lnum = lnum;
4451 /*
4452 * Start a forward search at the end of the line.
4453 * Start a backward search at the start of the line.
4454 * This makes sure we never match in the current
4455 * line, and can match anywhere in the
4456 * next/previous line.
4457 */
4458 if (c == '/')
4459 curwin->w_cursor.col = MAXCOL;
4460 else
4461 curwin->w_cursor.col = 0;
4462 searchcmdlen = 0;
4463 if (!do_search(NULL, c, cmd, 1L,
4464 SEARCH_HIS | SEARCH_MSG, NULL))
4465 {
4466 curwin->w_cursor = pos;
4467 cmd = NULL;
4468 goto error;
4469 }
4470 lnum = curwin->w_cursor.lnum;
4471 curwin->w_cursor = pos;
4472 /* adjust command string pointer */
4473 cmd += searchcmdlen;
4474 }
4475 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476
4477 case '\\': /* "\?", "\/" or "\&", repeat search */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004478 ++cmd;
4479 if (addr_type != ADDR_LINES)
4480 {
4481 EMSG(_(e_invaddr));
4482 goto error;
4483 }
4484 if (*cmd == '&')
4485 i = RE_SUBST;
4486 else if (*cmd == '?' || *cmd == '/')
4487 i = RE_SEARCH;
4488 else
4489 {
4490 EMSG(_(e_backslash));
4491 cmd = NULL;
4492 goto error;
4493 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004494
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004495 if (!skip)
4496 {
4497 /*
4498 * When search follows another address, start from
4499 * there.
4500 */
4501 if (lnum != MAXLNUM)
4502 pos.lnum = lnum;
4503 else
4504 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004505
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004506 /*
4507 * Start the search just like for the above
4508 * do_search().
4509 */
4510 if (*cmd != '?')
4511 pos.col = MAXCOL;
4512 else
4513 pos.col = 0;
4514 if (searchit(curwin, curbuf, &pos,
4515 *cmd == '?' ? BACKWARD : FORWARD,
4516 (char_u *)"", 1L, SEARCH_MSG,
4517 i, (linenr_T)0, NULL) != FAIL)
4518 lnum = pos.lnum;
4519 else
4520 {
4521 cmd = NULL;
4522 goto error;
4523 }
4524 }
4525 ++cmd;
4526 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004527
4528 default:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004529 if (VIM_ISDIGIT(*cmd)) /* absolute line number */
4530 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004531 }
4532
4533 for (;;)
4534 {
4535 cmd = skipwhite(cmd);
4536 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4537 break;
4538
4539 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004540 {
4541 switch (addr_type)
4542 {
4543 case ADDR_LINES:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004544 /* "+1" is same as ".+1" */
4545 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004546 break;
4547 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004548 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004549 break;
4550 case ADDR_ARGUMENTS:
4551 lnum = curwin->w_arg_idx + 1;
4552 break;
4553 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004554 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004555 lnum = curbuf->b_fnum;
4556 break;
4557 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004558 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004559 break;
4560 }
4561 }
4562
Bram Moolenaar071d4272004-06-13 20:20:40 +00004563 if (VIM_ISDIGIT(*cmd))
4564 i = '+'; /* "number" is same as "+number" */
4565 else
4566 i = *cmd++;
4567 if (!VIM_ISDIGIT(*cmd)) /* '+' is '+1', but '+0' is not '+1' */
4568 n = 1;
4569 else
4570 n = getdigits(&cmd);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004571 if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004572 || addr_type == ADDR_BUFFERS)
Bram Moolenaar4d84d932014-11-30 14:50:16 +01004573 lnum = compute_buffer_local_count(addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004574 else if (i == '-')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004575 lnum -= n;
4576 else
4577 lnum += n;
4578 }
4579 } while (*cmd == '/' || *cmd == '?');
4580
4581error:
4582 *ptr = cmd;
4583 return lnum;
4584}
4585
4586/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004587 * Get flags from an Ex command argument.
4588 */
4589 static void
4590get_flags(eap)
4591 exarg_T *eap;
4592{
4593 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4594 {
4595 if (*eap->arg == 'l')
4596 eap->flags |= EXFLAG_LIST;
4597 else if (*eap->arg == 'p')
4598 eap->flags |= EXFLAG_PRINT;
4599 else
4600 eap->flags |= EXFLAG_NR;
4601 eap->arg = skipwhite(eap->arg + 1);
4602 }
4603}
4604
4605/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004606 * Function called for command which is Not Implemented. NI!
4607 */
4608 void
4609ex_ni(eap)
4610 exarg_T *eap;
4611{
4612 if (!eap->skip)
4613 eap->errmsg = (char_u *)N_("E319: Sorry, the command is not available in this version");
4614}
4615
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004616#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617/*
4618 * Function called for script command which is Not Implemented. NI!
4619 * Skips over ":perl <<EOF" constructs.
4620 */
4621 static void
4622ex_script_ni(eap)
4623 exarg_T *eap;
4624{
4625 if (!eap->skip)
4626 ex_ni(eap);
4627 else
4628 vim_free(script_get(eap, eap->arg));
4629}
4630#endif
4631
4632/*
4633 * Check range in Ex command for validity.
4634 * Return NULL when valid, error message when invalid.
4635 */
4636 static char_u *
4637invalid_range(eap)
4638 exarg_T *eap;
4639{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004640 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004641 if ( eap->line1 < 0
4642 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004643 || eap->line1 > eap->line2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004644 return (char_u *)_(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004645
4646 if (eap->argt & RANGE)
4647 {
4648 switch(eap->addr_type)
4649 {
4650 case ADDR_LINES:
4651 if (!(eap->argt & NOTADR)
4652 && eap->line2 > curbuf->b_ml.ml_line_count
4653#ifdef FEAT_DIFF
4654 + (eap->cmdidx == CMD_diffget)
4655#endif
4656 )
4657 return (char_u *)_(e_invrange);
4658 break;
4659 case ADDR_ARGUMENTS:
4660 if (eap->line2 > ARGCOUNT + (!ARGCOUNT)) // add 1 if ARCOUNT is 0
4661 return (char_u *)_(e_invrange);
4662 break;
4663 case ADDR_BUFFERS:
4664 if (eap->line1 < firstbuf->b_fnum
4665 || eap->line2 > lastbuf->b_fnum)
4666 return (char_u *)_(e_invrange);
4667 break;
4668 case ADDR_LOADED_BUFFERS:
4669 buf = firstbuf;
4670 while (buf->b_ml.ml_mfp == NULL)
4671 {
4672 if (buf->b_next == NULL)
4673 return (char_u *)_(e_invrange);
4674 buf = buf->b_next;
4675 }
4676 if (eap->line1 < buf->b_fnum)
4677 return (char_u *)_(e_invrange);
4678 buf = lastbuf;
4679 while (buf->b_ml.ml_mfp == NULL)
4680 {
4681 if (buf->b_prev == NULL)
4682 return (char_u *)_(e_invrange);
4683 buf = buf->b_prev;
4684 }
4685 if (eap->line2 > buf->b_fnum)
4686 return (char_u *)_(e_invrange);
4687 break;
4688 case ADDR_WINDOWS:
4689 if (eap->line1 < 1
4690 || eap->line2 > LAST_WIN_NR)
4691 return (char_u *)_(e_invrange);
4692 break;
4693 case ADDR_TABS:
4694 if (eap->line2 > LAST_TAB_NR)
4695 return (char_u *)_(e_invrange);
4696 break;
4697 }
4698 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004699 return NULL;
4700}
4701
4702/*
4703 * Correct the range for zero line number, if required.
4704 */
4705 static void
4706correct_range(eap)
4707 exarg_T *eap;
4708{
4709 if (!(eap->argt & ZEROR)) /* zero in range not allowed */
4710 {
4711 if (eap->line1 == 0)
4712 eap->line1 = 1;
4713 if (eap->line2 == 0)
4714 eap->line2 = 1;
4715 }
4716}
4717
Bram Moolenaar748bf032005-02-02 23:04:36 +00004718#ifdef FEAT_QUICKFIX
4719static char_u *skip_grep_pat __ARGS((exarg_T *eap));
4720
4721/*
4722 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4723 * pattern. Otherwise return eap->arg.
4724 */
4725 static char_u *
4726skip_grep_pat(eap)
4727 exarg_T *eap;
4728{
4729 char_u *p = eap->arg;
4730
Bram Moolenaara37420f2006-02-04 22:37:47 +00004731 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4732 || eap->cmdidx == CMD_vimgrepadd
4733 || eap->cmdidx == CMD_lvimgrepadd
4734 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004735 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004736 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004737 if (p == NULL)
4738 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004739 }
4740 return p;
4741}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004742
4743/*
4744 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4745 * in the command line, so that things like % get expanded.
4746 */
4747 static char_u *
4748replace_makeprg(eap, p, cmdlinep)
4749 exarg_T *eap;
4750 char_u *p;
4751 char_u **cmdlinep;
4752{
4753 char_u *new_cmdline;
4754 char_u *program;
4755 char_u *pos;
4756 char_u *ptr;
4757 int len;
4758 int i;
4759
4760 /*
4761 * Don't do it when ":vimgrep" is used for ":grep".
4762 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004763 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4764 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4765 || eap->cmdidx == CMD_grepadd
4766 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004767 && !grep_internal(eap->cmdidx))
4768 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004769 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4770 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004771 {
4772 if (*curbuf->b_p_gp == NUL)
4773 program = p_gp;
4774 else
4775 program = curbuf->b_p_gp;
4776 }
4777 else
4778 {
4779 if (*curbuf->b_p_mp == NUL)
4780 program = p_mp;
4781 else
4782 program = curbuf->b_p_mp;
4783 }
4784
4785 p = skipwhite(p);
4786
4787 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4788 {
4789 /* replace $* by given arguments */
4790 i = 1;
4791 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4792 ++i;
4793 len = (int)STRLEN(p);
4794 new_cmdline = alloc((int)(STRLEN(program) + i * (len - 2) + 1));
4795 if (new_cmdline == NULL)
4796 return NULL; /* out of memory */
4797 ptr = new_cmdline;
4798 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4799 {
4800 i = (int)(pos - program);
4801 STRNCPY(ptr, program, i);
4802 STRCPY(ptr += i, p);
4803 ptr += len;
4804 program = pos + 2;
4805 }
4806 STRCPY(ptr, program);
4807 }
4808 else
4809 {
4810 new_cmdline = alloc((int)(STRLEN(program) + STRLEN(p) + 2));
4811 if (new_cmdline == NULL)
4812 return NULL; /* out of memory */
4813 STRCPY(new_cmdline, program);
4814 STRCAT(new_cmdline, " ");
4815 STRCAT(new_cmdline, p);
4816 }
4817 msg_make(p);
4818
4819 /* 'eap->cmd' is not set here, because it is not used at CMD_make */
4820 vim_free(*cmdlinep);
4821 *cmdlinep = new_cmdline;
4822 p = new_cmdline;
4823 }
4824 return p;
4825}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004826#endif
4827
Bram Moolenaar071d4272004-06-13 20:20:40 +00004828/*
4829 * Expand file name in Ex command argument.
4830 * Return FAIL for failure, OK otherwise.
4831 */
4832 int
4833expand_filename(eap, cmdlinep, errormsgp)
4834 exarg_T *eap;
4835 char_u **cmdlinep;
4836 char_u **errormsgp;
4837{
4838 int has_wildcards; /* need to expand wildcards */
4839 char_u *repl;
4840 int srclen;
4841 char_u *p;
4842 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004843 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004844
Bram Moolenaar748bf032005-02-02 23:04:36 +00004845#ifdef FEAT_QUICKFIX
4846 /* Skip a regexp pattern for ":vimgrep[add] pat file..." */
4847 p = skip_grep_pat(eap);
4848#else
4849 p = eap->arg;
4850#endif
4851
Bram Moolenaar071d4272004-06-13 20:20:40 +00004852 /*
4853 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4854 * the file name contains a wildcard it should not cause expanding.
4855 * (it will be expanded anyway if there is a wildcard before replacing).
4856 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004857 has_wildcards = mch_has_wildcard(p);
4858 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004859 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004860#ifdef FEAT_EVAL
4861 /* Skip over `=expr`, wildcards in it are not expanded. */
4862 if (p[0] == '`' && p[1] == '=')
4863 {
4864 p += 2;
4865 (void)skip_expr(&p);
4866 if (*p == '`')
4867 ++p;
4868 continue;
4869 }
4870#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004871 /*
4872 * Quick check if this cannot be the start of a special string.
4873 * Also removes backslash before '%', '#' and '<'.
4874 */
4875 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4876 {
4877 ++p;
4878 continue;
4879 }
4880
4881 /*
4882 * Try to find a match at this position.
4883 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004884 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
4885 errormsgp, &escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004886 if (*errormsgp != NULL) /* error detected */
4887 return FAIL;
4888 if (repl == NULL) /* no match found */
4889 {
4890 p += srclen;
4891 continue;
4892 }
4893
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004894 /* Wildcards won't be expanded below, the replacement is taken
4895 * literally. But do expand "~/file", "~user/file" and "$HOME/file". */
4896 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4897 {
4898 char_u *l = repl;
4899
4900 repl = expand_env_save(repl);
4901 vim_free(l);
4902 }
4903
Bram Moolenaar63b92542007-03-27 14:57:09 +00004904 /* Need to escape white space et al. with a backslash.
4905 * Don't do this for:
4906 * - replacement that already has been escaped: "##"
4907 * - shell commands (may have to use quotes instead).
4908 * - non-unix systems when there is a single argument (spaces don't
4909 * separate arguments then).
4910 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004911 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00004912 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00004913 && eap->cmdidx != CMD_bang
4914 && eap->cmdidx != CMD_make
Bram Moolenaara37420f2006-02-04 22:37:47 +00004915 && eap->cmdidx != CMD_lmake
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916 && eap->cmdidx != CMD_grep
Bram Moolenaara37420f2006-02-04 22:37:47 +00004917 && eap->cmdidx != CMD_lgrep
Bram Moolenaar071d4272004-06-13 20:20:40 +00004918 && eap->cmdidx != CMD_grepadd
Bram Moolenaara37420f2006-02-04 22:37:47 +00004919 && eap->cmdidx != CMD_lgrepadd
Bram Moolenaar071d4272004-06-13 20:20:40 +00004920#ifndef UNIX
4921 && !(eap->argt & NOSPC)
4922#endif
4923 )
4924 {
4925 char_u *l;
4926#ifdef BACKSLASH_IN_FILENAME
4927 /* Don't escape a backslash here, because rem_backslash() doesn't
4928 * remove it later. */
4929 static char_u *nobslash = (char_u *)" \t\"|";
4930# define ESCAPE_CHARS nobslash
4931#else
4932# define ESCAPE_CHARS escape_chars
4933#endif
4934
4935 for (l = repl; *l; ++l)
4936 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
4937 {
4938 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
4939 if (l != NULL)
4940 {
4941 vim_free(repl);
4942 repl = l;
4943 }
4944 break;
4945 }
4946 }
4947
4948 /* For a shell command a '!' must be escaped. */
4949 if ((eap->usefilter || eap->cmdidx == CMD_bang)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004950 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004951 {
4952 char_u *l;
4953
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004954 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004955 if (l != NULL)
4956 {
4957 vim_free(repl);
4958 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004959 }
4960 }
4961
4962 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
4963 vim_free(repl);
4964 if (p == NULL)
4965 return FAIL;
4966 }
4967
4968 /*
4969 * One file argument: Expand wildcards.
4970 * Don't do this with ":r !command" or ":w !command".
4971 */
4972 if ((eap->argt & NOSPC) && !eap->usefilter)
4973 {
4974 /*
4975 * May do this twice:
4976 * 1. Replace environment variables.
4977 * 2. Replace any other wildcards, remove backslashes.
4978 */
4979 for (n = 1; n <= 2; ++n)
4980 {
4981 if (n == 2)
4982 {
4983#ifdef UNIX
4984 /*
4985 * Only for Unix we check for more than one file name.
4986 * For other systems spaces are considered to be part
4987 * of the file name.
4988 * Only check here if there is no wildcard, otherwise
4989 * ExpandOne() will check for errors. This allows
4990 * ":e `ls ve*.c`" on Unix.
4991 */
4992 if (!has_wildcards)
4993 for (p = eap->arg; *p; ++p)
4994 {
4995 /* skip escaped characters */
4996 if (p[1] && (*p == '\\' || *p == Ctrl_V))
4997 ++p;
4998 else if (vim_iswhite(*p))
4999 {
5000 *errormsgp = (char_u *)_("E172: Only one file name allowed");
5001 return FAIL;
5002 }
5003 }
5004#endif
5005
5006 /*
5007 * Halve the number of backslashes (this is Vi compatible).
5008 * For Unix and OS/2, when wildcards are expanded, this is
5009 * done by ExpandOne() below.
5010 */
5011#if defined(UNIX) || defined(OS2)
5012 if (!has_wildcards)
5013#endif
5014 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005015 }
5016
5017 if (has_wildcards)
5018 {
5019 if (n == 1)
5020 {
5021 /*
5022 * First loop: May expand environment variables. This
5023 * can be done much faster with expand_env() than with
5024 * something else (e.g., calling a shell).
5025 * After expanding environment variables, check again
5026 * if there are still wildcards present.
5027 */
5028 if (vim_strchr(eap->arg, '$') != NULL
5029 || vim_strchr(eap->arg, '~') != NULL)
5030 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005031 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00005032 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033 has_wildcards = mch_has_wildcard(NameBuff);
5034 p = NameBuff;
5035 }
5036 else
5037 p = NULL;
5038 }
5039 else /* n == 2 */
5040 {
5041 expand_T xpc;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005042 int options = WILD_LIST_NOTFOUND|WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005043
5044 ExpandInit(&xpc);
5045 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005046 if (p_wic)
5047 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005048 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01005049 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005050 if (p == NULL)
5051 return FAIL;
5052 }
5053 if (p != NULL)
5054 {
5055 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
5056 p, cmdlinep);
5057 if (n == 2) /* p came from ExpandOne() */
5058 vim_free(p);
5059 }
5060 }
5061 }
5062 }
5063 return OK;
5064}
5065
5066/*
5067 * Replace part of the command line, keeping eap->cmd, eap->arg and
5068 * eap->nextcmd correct.
5069 * "src" points to the part that is to be replaced, of length "srclen".
5070 * "repl" is the replacement string.
5071 * Returns a pointer to the character after the replaced string.
5072 * Returns NULL for failure.
5073 */
5074 static char_u *
5075repl_cmdline(eap, src, srclen, repl, cmdlinep)
5076 exarg_T *eap;
5077 char_u *src;
5078 int srclen;
5079 char_u *repl;
5080 char_u **cmdlinep;
5081{
5082 int len;
5083 int i;
5084 char_u *new_cmdline;
5085
5086 /*
5087 * The new command line is build in new_cmdline[].
5088 * First allocate it.
5089 * Careful: a "+cmd" argument may have been NUL terminated.
5090 */
5091 len = (int)STRLEN(repl);
5092 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005093 if (eap->nextcmd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005094 i += (int)STRLEN(eap->nextcmd);/* add space for next command */
5095 if ((new_cmdline = alloc((unsigned)i)) == NULL)
5096 return NULL; /* out of memory! */
5097
5098 /*
5099 * Copy the stuff before the expanded part.
5100 * Copy the expanded stuff.
5101 * Copy what came after the expanded part.
5102 * Copy the next commands, if there are any.
5103 */
5104 i = (int)(src - *cmdlinep); /* length of part before match */
5105 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00005106
Bram Moolenaar071d4272004-06-13 20:20:40 +00005107 mch_memmove(new_cmdline + i, repl, (size_t)len);
5108 i += len; /* remember the end of the string */
5109 STRCPY(new_cmdline + i, src + srclen);
5110 src = new_cmdline + i; /* remember where to continue */
5111
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005112 if (eap->nextcmd != NULL) /* append next command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005113 {
5114 i = (int)STRLEN(new_cmdline) + 1;
5115 STRCPY(new_cmdline + i, eap->nextcmd);
5116 eap->nextcmd = new_cmdline + i;
5117 }
5118 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
5119 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
5120 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
5121 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
5122 vim_free(*cmdlinep);
5123 *cmdlinep = new_cmdline;
5124
5125 return src;
5126}
5127
5128/*
5129 * Check for '|' to separate commands and '"' to start comments.
5130 */
5131 void
5132separate_nextcmd(eap)
5133 exarg_T *eap;
5134{
5135 char_u *p;
5136
Bram Moolenaar86b68352004-12-27 21:59:20 +00005137#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00005138 p = skip_grep_pat(eap);
5139#else
5140 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005141#endif
5142
5143 for ( ; *p; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005144 {
5145 if (*p == Ctrl_V)
5146 {
5147 if (eap->argt & (USECTRLV | XFILE))
5148 ++p; /* skip CTRL-V and next char */
5149 else
Bram Moolenaarb0db5692007-08-14 20:54:49 +00005150 /* remove CTRL-V and skip next char */
Bram Moolenaara7241f52008-06-24 20:39:31 +00005151 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005152 if (*p == NUL) /* stop at NUL after CTRL-V */
5153 break;
5154 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005155
5156#ifdef FEAT_EVAL
5157 /* Skip over `=expr` when wildcards are expanded. */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005158 else if (p[0] == '`' && p[1] == '=' && (eap->argt & XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005159 {
5160 p += 2;
5161 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005162 }
5163#endif
5164
Bram Moolenaar071d4272004-06-13 20:20:40 +00005165 /* Check for '"': start of comment or '|': next command */
5166 /* :@" and :*" do not start a comment!
5167 * :redir @" doesn't either. */
5168 else if ((*p == '"' && !(eap->argt & NOTRLCOM)
5169 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
5170 || p != eap->arg)
5171 && (eap->cmdidx != CMD_redir
5172 || p != eap->arg + 1 || p[-1] != '@'))
5173 || *p == '|' || *p == '\n')
5174 {
5175 /*
5176 * We remove the '\' before the '|', unless USECTRLV is used
5177 * AND 'b' is present in 'cpoptions'.
5178 */
5179 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
5180 || !(eap->argt & USECTRLV)) && *(p - 1) == '\\')
5181 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00005182 STRMOVE(p - 1, p); /* remove the '\' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005183 --p;
5184 }
5185 else
5186 {
5187 eap->nextcmd = check_nextcmd(p);
5188 *p = NUL;
5189 break;
5190 }
5191 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005192 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005193
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194 if (!(eap->argt & NOTRLCOM)) /* remove trailing spaces */
5195 del_trailing_spaces(eap->arg);
5196}
5197
5198/*
5199 * get + command from ex argument
5200 */
5201 static char_u *
5202getargcmd(argp)
5203 char_u **argp;
5204{
5205 char_u *arg = *argp;
5206 char_u *command = NULL;
5207
5208 if (*arg == '+') /* +[command] */
5209 {
5210 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02005211 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005212 command = dollar_command;
5213 else
5214 {
5215 command = arg;
5216 arg = skip_cmd_arg(command, TRUE);
5217 if (*arg != NUL)
5218 *arg++ = NUL; /* terminate command with NUL */
5219 }
5220
5221 arg = skipwhite(arg); /* skip over spaces */
5222 *argp = arg;
5223 }
5224 return command;
5225}
5226
5227/*
5228 * Find end of "+command" argument. Skip over "\ " and "\\".
5229 */
5230 static char_u *
5231skip_cmd_arg(p, rembs)
5232 char_u *p;
5233 int rembs; /* TRUE to halve the number of backslashes */
5234{
5235 while (*p && !vim_isspace(*p))
5236 {
5237 if (*p == '\\' && p[1] != NUL)
5238 {
5239 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00005240 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005241 else
5242 ++p;
5243 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005244 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005245 }
5246 return p;
5247}
5248
5249/*
5250 * Get "++opt=arg" argument.
5251 * Return FAIL or OK.
5252 */
5253 static int
5254getargopt(eap)
5255 exarg_T *eap;
5256{
5257 char_u *arg = eap->arg + 2;
5258 int *pp = NULL;
5259#ifdef FEAT_MBYTE
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005260 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005261 char_u *p;
5262#endif
5263
5264 /* ":edit ++[no]bin[ary] file" */
5265 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
5266 {
5267 if (*arg == 'n')
5268 {
5269 arg += 2;
5270 eap->force_bin = FORCE_NOBIN;
5271 }
5272 else
5273 eap->force_bin = FORCE_BIN;
5274 if (!checkforcmd(&arg, "binary", 3))
5275 return FAIL;
5276 eap->arg = skipwhite(arg);
5277 return OK;
5278 }
5279
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005280 /* ":read ++edit file" */
5281 if (STRNCMP(arg, "edit", 4) == 0)
5282 {
5283 eap->read_edit = TRUE;
5284 eap->arg = skipwhite(arg + 4);
5285 return OK;
5286 }
5287
Bram Moolenaar071d4272004-06-13 20:20:40 +00005288 if (STRNCMP(arg, "ff", 2) == 0)
5289 {
5290 arg += 2;
5291 pp = &eap->force_ff;
5292 }
5293 else if (STRNCMP(arg, "fileformat", 10) == 0)
5294 {
5295 arg += 10;
5296 pp = &eap->force_ff;
5297 }
5298#ifdef FEAT_MBYTE
5299 else if (STRNCMP(arg, "enc", 3) == 0)
5300 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01005301 if (STRNCMP(arg, "encoding", 8) == 0)
5302 arg += 8;
5303 else
5304 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005305 pp = &eap->force_enc;
5306 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005307 else if (STRNCMP(arg, "bad", 3) == 0)
5308 {
5309 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005310 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005311 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005312#endif
5313
5314 if (pp == NULL || *arg != '=')
5315 return FAIL;
5316
5317 ++arg;
5318 *pp = (int)(arg - eap->cmd);
5319 arg = skip_cmd_arg(arg, FALSE);
5320 eap->arg = skipwhite(arg);
5321 *arg = NUL;
5322
5323#ifdef FEAT_MBYTE
5324 if (pp == &eap->force_ff)
5325 {
5326#endif
5327 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
5328 return FAIL;
5329#ifdef FEAT_MBYTE
5330 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005331 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005332 {
5333 /* Make 'fileencoding' lower case. */
5334 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
5335 *p = TOLOWER_ASC(*p);
5336 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005337 else
5338 {
5339 /* Check ++bad= argument. Must be a single-byte character, "keep" or
5340 * "drop". */
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005341 p = eap->cmd + bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005342 if (STRICMP(p, "keep") == 0)
5343 eap->bad_char = BAD_KEEP;
5344 else if (STRICMP(p, "drop") == 0)
5345 eap->bad_char = BAD_DROP;
5346 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
5347 eap->bad_char = *p;
5348 else
5349 return FAIL;
5350 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005351#endif
5352
5353 return OK;
5354}
5355
5356/*
5357 * ":abbreviate" and friends.
5358 */
5359 static void
5360ex_abbreviate(eap)
5361 exarg_T *eap;
5362{
5363 do_exmap(eap, TRUE); /* almost the same as mapping */
5364}
5365
5366/*
5367 * ":map" and friends.
5368 */
5369 static void
5370ex_map(eap)
5371 exarg_T *eap;
5372{
5373 /*
5374 * If we are sourcing .exrc or .vimrc in current directory we
5375 * print the mappings for security reasons.
5376 */
5377 if (secure)
5378 {
5379 secure = 2;
5380 msg_outtrans(eap->cmd);
5381 msg_putchar('\n');
5382 }
5383 do_exmap(eap, FALSE);
5384}
5385
5386/*
5387 * ":unmap" and friends.
5388 */
5389 static void
5390ex_unmap(eap)
5391 exarg_T *eap;
5392{
5393 do_exmap(eap, FALSE);
5394}
5395
5396/*
5397 * ":mapclear" and friends.
5398 */
5399 static void
5400ex_mapclear(eap)
5401 exarg_T *eap;
5402{
5403 map_clear(eap->cmd, eap->arg, eap->forceit, FALSE);
5404}
5405
5406/*
5407 * ":abclear" and friends.
5408 */
5409 static void
5410ex_abclear(eap)
5411 exarg_T *eap;
5412{
5413 map_clear(eap->cmd, eap->arg, TRUE, TRUE);
5414}
5415
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005416#if defined(FEAT_AUTOCMD) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005417 static void
5418ex_autocmd(eap)
5419 exarg_T *eap;
5420{
5421 /*
5422 * Disallow auto commands from .exrc and .vimrc in current
5423 * directory for security reasons.
5424 */
5425 if (secure)
5426 {
5427 secure = 2;
5428 eap->errmsg = e_curdir;
5429 }
5430 else if (eap->cmdidx == CMD_autocmd)
5431 do_autocmd(eap->arg, eap->forceit);
5432 else
5433 do_augroup(eap->arg, eap->forceit);
5434}
5435
5436/*
5437 * ":doautocmd": Apply the automatic commands to the current buffer.
5438 */
5439 static void
5440ex_doautocmd(eap)
5441 exarg_T *eap;
5442{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005443 char_u *arg = eap->arg;
5444 int call_do_modelines = check_nomodeline(&arg);
5445
5446 (void)do_doautocmd(arg, TRUE);
5447 if (call_do_modelines) /* Only when there is no <nomodeline>. */
5448 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005449}
5450#endif
5451
5452#ifdef FEAT_LISTCMDS
5453/*
5454 * :[N]bunload[!] [N] [bufname] unload buffer
5455 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
5456 * :[N]bwipeout[!] [N] [bufname] delete buffer really
5457 */
5458 static void
5459ex_bunload(eap)
5460 exarg_T *eap;
5461{
5462 eap->errmsg = do_bufdel(
5463 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
5464 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
5465 : DOBUF_UNLOAD, eap->arg,
5466 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
5467}
5468
5469/*
5470 * :[N]buffer [N] to buffer N
5471 * :[N]sbuffer [N] to buffer N
5472 */
5473 static void
5474ex_buffer(eap)
5475 exarg_T *eap;
5476{
5477 if (*eap->arg)
5478 eap->errmsg = e_trailing;
5479 else
5480 {
5481 if (eap->addr_count == 0) /* default is current buffer */
5482 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
5483 else
5484 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005485 if (eap->do_ecmd_cmd != NULL)
5486 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005487 }
5488}
5489
5490/*
5491 * :[N]bmodified [N] to next mod. buffer
5492 * :[N]sbmodified [N] to next mod. buffer
5493 */
5494 static void
5495ex_bmodified(eap)
5496 exarg_T *eap;
5497{
5498 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005499 if (eap->do_ecmd_cmd != NULL)
5500 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005501}
5502
5503/*
5504 * :[N]bnext [N] to next buffer
5505 * :[N]sbnext [N] split and to next buffer
5506 */
5507 static void
5508ex_bnext(eap)
5509 exarg_T *eap;
5510{
5511 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005512 if (eap->do_ecmd_cmd != NULL)
5513 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005514}
5515
5516/*
5517 * :[N]bNext [N] to previous buffer
5518 * :[N]bprevious [N] to previous buffer
5519 * :[N]sbNext [N] split and to previous buffer
5520 * :[N]sbprevious [N] split and to previous buffer
5521 */
5522 static void
5523ex_bprevious(eap)
5524 exarg_T *eap;
5525{
5526 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005527 if (eap->do_ecmd_cmd != NULL)
5528 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005529}
5530
5531/*
5532 * :brewind to first buffer
5533 * :bfirst to first buffer
5534 * :sbrewind split and to first buffer
5535 * :sbfirst split and to first buffer
5536 */
5537 static void
5538ex_brewind(eap)
5539 exarg_T *eap;
5540{
5541 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005542 if (eap->do_ecmd_cmd != NULL)
5543 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005544}
5545
5546/*
5547 * :blast to last buffer
5548 * :sblast split and to last buffer
5549 */
5550 static void
5551ex_blast(eap)
5552 exarg_T *eap;
5553{
5554 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005555 if (eap->do_ecmd_cmd != NULL)
5556 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005557}
5558#endif
5559
5560 int
5561ends_excmd(c)
5562 int c;
5563{
5564 return (c == NUL || c == '|' || c == '"' || c == '\n');
5565}
5566
5567#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5568 || defined(PROTO)
5569/*
5570 * Return the next command, after the first '|' or '\n'.
5571 * Return NULL if not found.
5572 */
5573 char_u *
5574find_nextcmd(p)
5575 char_u *p;
5576{
5577 while (*p != '|' && *p != '\n')
5578 {
5579 if (*p == NUL)
5580 return NULL;
5581 ++p;
5582 }
5583 return (p + 1);
5584}
5585#endif
5586
5587/*
5588 * Check if *p is a separator between Ex commands.
5589 * Return NULL if it isn't, (p + 1) if it is.
5590 */
5591 char_u *
5592check_nextcmd(p)
5593 char_u *p;
5594{
5595 p = skipwhite(p);
5596 if (*p == '|' || *p == '\n')
5597 return (p + 1);
5598 else
5599 return NULL;
5600}
5601
5602/*
5603 * - if there are more files to edit
5604 * - and this is the last window
5605 * - and forceit not used
5606 * - and not repeated twice on a row
5607 * return FAIL and give error message if 'message' TRUE
5608 * return OK otherwise
5609 */
5610 static int
5611check_more(message, forceit)
5612 int message; /* when FALSE check only, no messages */
5613 int forceit;
5614{
5615 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5616
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005617 if (!forceit && only_one_window()
5618 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005619 {
5620 if (message)
5621 {
5622#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
5623 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
5624 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005625 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005626
5627 if (n == 1)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02005628 vim_strncpy(buff,
5629 (char_u *)_("1 more file to edit. Quit anyway?"),
Bram Moolenaard9462e32011-04-11 21:35:11 +02005630 DIALOG_MSG_SIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005631 else
Bram Moolenaard9462e32011-04-11 21:35:11 +02005632 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005633 _("%d more files to edit. Quit anyway?"), n);
5634 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5635 return OK;
5636 return FAIL;
5637 }
5638#endif
5639 if (n == 1)
5640 EMSG(_("E173: 1 more file to edit"));
5641 else
5642 EMSGN(_("E173: %ld more files to edit"), n);
5643 quitmore = 2; /* next try to quit is allowed */
5644 }
5645 return FAIL;
5646 }
5647 return OK;
5648}
5649
5650#ifdef FEAT_CMDL_COMPL
5651/*
5652 * Function given to ExpandGeneric() to obtain the list of command names.
5653 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005654 char_u *
5655get_command_name(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00005656 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005657 int idx;
5658{
5659 if (idx >= (int)CMD_SIZE)
5660# ifdef FEAT_USR_CMDS
5661 return get_user_command_name(idx);
5662# else
5663 return NULL;
5664# endif
5665 return cmdnames[idx].cmd_name;
5666}
5667#endif
5668
5669#if defined(FEAT_USR_CMDS) || defined(PROTO)
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005670static 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 addr_type, int force));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005671static void uc_list __ARGS((char_u *name, size_t name_len));
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005672static int uc_scan_attr __ARGS((char_u *attr, size_t len, long *argt, long *def, int *flags, int *compl, char_u **compl_arg, int* attr_type_arg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005673static char_u *uc_split_args __ARGS((char_u *arg, size_t *lenp));
5674static 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));
5675
5676 static int
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005677uc_add_command(name, name_len, rep, argt, def, flags, compl, compl_arg, addr_type, force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005678 char_u *name;
5679 size_t name_len;
5680 char_u *rep;
5681 long argt;
5682 long def;
5683 int flags;
5684 int compl;
5685 char_u *compl_arg;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005686 int addr_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005687 int force;
5688{
5689 ucmd_T *cmd = NULL;
5690 char_u *p;
5691 int i;
5692 int cmp = 1;
5693 char_u *rep_buf = NULL;
5694 garray_T *gap;
5695
Bram Moolenaar9c102382006-05-03 21:26:49 +00005696 replace_termcodes(rep, &rep_buf, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005697 if (rep_buf == NULL)
5698 {
5699 /* Can't replace termcodes - try using the string as is */
5700 rep_buf = vim_strsave(rep);
5701
5702 /* Give up if out of memory */
5703 if (rep_buf == NULL)
5704 return FAIL;
5705 }
5706
5707 /* get address of growarray: global or in curbuf */
5708 if (flags & UC_BUFFER)
5709 {
5710 gap = &curbuf->b_ucmds;
5711 if (gap->ga_itemsize == 0)
5712 ga_init2(gap, (int)sizeof(ucmd_T), 4);
5713 }
5714 else
5715 gap = &ucmds;
5716
5717 /* Search for the command in the already defined commands. */
5718 for (i = 0; i < gap->ga_len; ++i)
5719 {
5720 size_t len;
5721
5722 cmd = USER_CMD_GA(gap, i);
5723 len = STRLEN(cmd->uc_name);
5724 cmp = STRNCMP(name, cmd->uc_name, name_len);
5725 if (cmp == 0)
5726 {
5727 if (name_len < len)
5728 cmp = -1;
5729 else if (name_len > len)
5730 cmp = 1;
5731 }
5732
5733 if (cmp == 0)
5734 {
5735 if (!force)
5736 {
5737 EMSG(_("E174: Command already exists: add ! to replace it"));
5738 goto fail;
5739 }
5740
5741 vim_free(cmd->uc_rep);
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00005742 cmd->uc_rep = NULL;
5743#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5744 vim_free(cmd->uc_compl_arg);
5745 cmd->uc_compl_arg = NULL;
5746#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005747 break;
5748 }
5749
5750 /* Stop as soon as we pass the name to add */
5751 if (cmp < 0)
5752 break;
5753 }
5754
5755 /* Extend the array unless we're replacing an existing command */
5756 if (cmp != 0)
5757 {
5758 if (ga_grow(gap, 1) != OK)
5759 goto fail;
5760 if ((p = vim_strnsave(name, (int)name_len)) == NULL)
5761 goto fail;
5762
5763 cmd = USER_CMD_GA(gap, i);
5764 mch_memmove(cmd + 1, cmd, (gap->ga_len - i) * sizeof(ucmd_T));
5765
5766 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005767
5768 cmd->uc_name = p;
5769 }
5770
5771 cmd->uc_rep = rep_buf;
5772 cmd->uc_argt = argt;
5773 cmd->uc_def = def;
5774 cmd->uc_compl = compl;
5775#ifdef FEAT_EVAL
5776 cmd->uc_scriptID = current_SID;
5777# ifdef FEAT_CMDL_COMPL
5778 cmd->uc_compl_arg = compl_arg;
5779# endif
5780#endif
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005781 cmd->uc_addr_type = addr_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005782
5783 return OK;
5784
5785fail:
5786 vim_free(rep_buf);
5787#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5788 vim_free(compl_arg);
5789#endif
5790 return FAIL;
5791}
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005792#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005793
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005794#if defined(FEAT_USR_CMDS)
5795static struct
5796{
5797 int expand;
5798 char *name;
5799} addr_type_complete[] =
5800{
5801 {ADDR_ARGUMENTS, "arguments"},
5802 {ADDR_LINES, "lines"},
5803 {ADDR_LOADED_BUFFERS, "loaded_buffers"},
5804 {ADDR_TABS, "tabs"},
5805 {ADDR_BUFFERS, "buffers"},
5806 {ADDR_WINDOWS, "windows"},
5807 {-1, NULL}
5808};
5809#endif
5810
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005811#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005812/*
5813 * List of names for completion for ":command" with the EXPAND_ flag.
5814 * Must be alphabetical for completion.
5815 */
5816static struct
5817{
5818 int expand;
5819 char *name;
5820} command_complete[] =
5821{
5822 {EXPAND_AUGROUP, "augroup"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005823 {EXPAND_BEHAVE, "behave"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005824 {EXPAND_BUFFERS, "buffer"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005825 {EXPAND_COLORS, "color"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005826 {EXPAND_COMMANDS, "command"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005827 {EXPAND_COMPILER, "compiler"},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005828#if defined(FEAT_CSCOPE)
5829 {EXPAND_CSCOPE, "cscope"},
5830#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005831#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5832 {EXPAND_USER_DEFINED, "custom"},
Bram Moolenaara466c992005-07-09 21:03:22 +00005833 {EXPAND_USER_LIST, "customlist"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005834#endif
5835 {EXPAND_DIRECTORIES, "dir"},
5836 {EXPAND_ENV_VARS, "environment"},
5837 {EXPAND_EVENTS, "event"},
5838 {EXPAND_EXPRESSION, "expression"},
5839 {EXPAND_FILES, "file"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005840 {EXPAND_FILES_IN_PATH, "file_in_path"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005841 {EXPAND_FILETYPE, "filetype"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005842 {EXPAND_FUNCTIONS, "function"},
5843 {EXPAND_HELP, "help"},
5844 {EXPAND_HIGHLIGHT, "highlight"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005845#if defined(FEAT_CMDHIST)
5846 {EXPAND_HISTORY, "history"},
5847#endif
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005848#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005849 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005850 {EXPAND_LOCALES, "locale"},
5851#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005852 {EXPAND_MAPPINGS, "mapping"},
5853 {EXPAND_MENUS, "menu"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005854 {EXPAND_OWNSYNTAX, "syntax"},
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02005855#if defined(FEAT_PROFILE)
5856 {EXPAND_SYNTIME, "syntime"},
5857#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005858 {EXPAND_SETTINGS, "option"},
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005859 {EXPAND_SHELLCMD, "shellcmd"},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005860#if defined(FEAT_SIGNS)
5861 {EXPAND_SIGN, "sign"},
5862#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005863 {EXPAND_TAGS, "tag"},
5864 {EXPAND_TAGS_LISTFILES, "tag_listfiles"},
Bram Moolenaar24305862012-08-15 14:05:05 +02005865 {EXPAND_USER, "user"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005866 {EXPAND_USER_VARS, "var"},
5867 {0, NULL}
5868};
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005869#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005870
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005871#if defined(FEAT_USR_CMDS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005872 static void
5873uc_list(name, name_len)
5874 char_u *name;
5875 size_t name_len;
5876{
5877 int i, j;
5878 int found = FALSE;
5879 ucmd_T *cmd;
5880 int len;
5881 long a;
5882 garray_T *gap;
5883
5884 gap = &curbuf->b_ucmds;
5885 for (;;)
5886 {
5887 for (i = 0; i < gap->ga_len; ++i)
5888 {
5889 cmd = USER_CMD_GA(gap, i);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005890 a = (long)cmd->uc_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005891
5892 /* Skip commands which don't match the requested prefix */
5893 if (STRNCMP(name, cmd->uc_name, name_len) != 0)
5894 continue;
5895
5896 /* Put out the title first time */
5897 if (!found)
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005898 MSG_PUTS_TITLE(_("\n Name Args Address Complete Definition"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005899 found = TRUE;
5900 msg_putchar('\n');
5901 if (got_int)
5902 break;
5903
5904 /* Special cases */
5905 msg_putchar(a & BANG ? '!' : ' ');
5906 msg_putchar(a & REGSTR ? '"' : ' ');
5907 msg_putchar(gap != &ucmds ? 'b' : ' ');
5908 msg_putchar(' ');
5909
5910 msg_outtrans_attr(cmd->uc_name, hl_attr(HLF_D));
5911 len = (int)STRLEN(cmd->uc_name) + 4;
5912
5913 do {
5914 msg_putchar(' ');
5915 ++len;
5916 } while (len < 16);
5917
5918 len = 0;
5919
5920 /* Arguments */
5921 switch ((int)(a & (EXTRA|NOSPC|NEEDARG)))
5922 {
5923 case 0: IObuff[len++] = '0'; break;
5924 case (EXTRA): IObuff[len++] = '*'; break;
5925 case (EXTRA|NOSPC): IObuff[len++] = '?'; break;
5926 case (EXTRA|NEEDARG): IObuff[len++] = '+'; break;
5927 case (EXTRA|NOSPC|NEEDARG): IObuff[len++] = '1'; break;
5928 }
5929
5930 do {
5931 IObuff[len++] = ' ';
5932 } while (len < 5);
5933
5934 /* Range */
5935 if (a & (RANGE|COUNT))
5936 {
5937 if (a & COUNT)
5938 {
5939 /* -count=N */
5940 sprintf((char *)IObuff + len, "%ldc", cmd->uc_def);
5941 len += (int)STRLEN(IObuff + len);
5942 }
5943 else if (a & DFLALL)
5944 IObuff[len++] = '%';
5945 else if (cmd->uc_def >= 0)
5946 {
5947 /* -range=N */
5948 sprintf((char *)IObuff + len, "%ld", cmd->uc_def);
5949 len += (int)STRLEN(IObuff + len);
5950 }
5951 else
5952 IObuff[len++] = '.';
5953 }
5954
5955 do {
5956 IObuff[len++] = ' ';
5957 } while (len < 11);
5958
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005959 /* Address Type */
5960 for (j = 0; addr_type_complete[j].expand != -1; ++j)
5961 if (addr_type_complete[j].expand != ADDR_LINES
5962 && addr_type_complete[j].expand == cmd->uc_addr_type)
5963 {
5964 STRCPY(IObuff + len, addr_type_complete[j].name);
5965 len += (int)STRLEN(IObuff + len);
5966 break;
5967 }
5968
5969 do {
5970 IObuff[len++] = ' ';
5971 } while (len < 21);
5972
Bram Moolenaar071d4272004-06-13 20:20:40 +00005973 /* Completion */
5974 for (j = 0; command_complete[j].expand != 0; ++j)
5975 if (command_complete[j].expand == cmd->uc_compl)
5976 {
5977 STRCPY(IObuff + len, command_complete[j].name);
5978 len += (int)STRLEN(IObuff + len);
5979 break;
5980 }
5981
5982 do {
5983 IObuff[len++] = ' ';
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005984 } while (len < 35);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005985
5986 IObuff[len] = '\0';
5987 msg_outtrans(IObuff);
5988
5989 msg_outtrans_special(cmd->uc_rep, FALSE);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005990#ifdef FEAT_EVAL
5991 if (p_verbose > 0)
5992 last_set_msg(cmd->uc_scriptID);
5993#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005994 out_flush();
5995 ui_breakcheck();
5996 if (got_int)
5997 break;
5998 }
5999 if (gap == &ucmds || i < gap->ga_len)
6000 break;
6001 gap = &ucmds;
6002 }
6003
6004 if (!found)
6005 MSG(_("No user-defined commands found"));
6006}
6007
6008 static char_u *
6009uc_fun_cmd()
6010{
6011 static char_u fcmd[] = {0x84, 0xaf, 0x60, 0xb9, 0xaf, 0xb5, 0x60, 0xa4,
6012 0xa5, 0xad, 0xa1, 0xae, 0xa4, 0x60, 0xa1, 0x60,
6013 0xb3, 0xa8, 0xb2, 0xb5, 0xa2, 0xa2, 0xa5, 0xb2,
6014 0xb9, 0x7f, 0};
6015 int i;
6016
6017 for (i = 0; fcmd[i]; ++i)
6018 IObuff[i] = fcmd[i] - 0x40;
6019 IObuff[i] = 0;
6020 return IObuff;
6021}
6022
6023 static int
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006024uc_scan_attr(attr, len, argt, def, flags, compl, compl_arg, addr_type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006025 char_u *attr;
6026 size_t len;
6027 long *argt;
6028 long *def;
6029 int *flags;
6030 int *compl;
6031 char_u **compl_arg;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006032 int *addr_type_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006033{
6034 char_u *p;
6035
6036 if (len == 0)
6037 {
6038 EMSG(_("E175: No attribute specified"));
6039 return FAIL;
6040 }
6041
6042 /* First, try the simple attributes (no arguments) */
6043 if (STRNICMP(attr, "bang", len) == 0)
6044 *argt |= BANG;
6045 else if (STRNICMP(attr, "buffer", len) == 0)
6046 *flags |= UC_BUFFER;
6047 else if (STRNICMP(attr, "register", len) == 0)
6048 *argt |= REGSTR;
6049 else if (STRNICMP(attr, "bar", len) == 0)
6050 *argt |= TRLBAR;
6051 else
6052 {
6053 int i;
6054 char_u *val = NULL;
6055 size_t vallen = 0;
6056 size_t attrlen = len;
6057
6058 /* Look for the attribute name - which is the part before any '=' */
6059 for (i = 0; i < (int)len; ++i)
6060 {
6061 if (attr[i] == '=')
6062 {
6063 val = &attr[i + 1];
6064 vallen = len - i - 1;
6065 attrlen = i;
6066 break;
6067 }
6068 }
6069
6070 if (STRNICMP(attr, "nargs", attrlen) == 0)
6071 {
6072 if (vallen == 1)
6073 {
6074 if (*val == '0')
6075 /* Do nothing - this is the default */;
6076 else if (*val == '1')
6077 *argt |= (EXTRA | NOSPC | NEEDARG);
6078 else if (*val == '*')
6079 *argt |= EXTRA;
6080 else if (*val == '?')
6081 *argt |= (EXTRA | NOSPC);
6082 else if (*val == '+')
6083 *argt |= (EXTRA | NEEDARG);
6084 else
6085 goto wrong_nargs;
6086 }
6087 else
6088 {
6089wrong_nargs:
6090 EMSG(_("E176: Invalid number of arguments"));
6091 return FAIL;
6092 }
6093 }
6094 else if (STRNICMP(attr, "range", attrlen) == 0)
6095 {
6096 *argt |= RANGE;
6097 if (vallen == 1 && *val == '%')
6098 *argt |= DFLALL;
6099 else if (val != NULL)
6100 {
6101 p = val;
6102 if (*def >= 0)
6103 {
6104two_count:
6105 EMSG(_("E177: Count cannot be specified twice"));
6106 return FAIL;
6107 }
6108
6109 *def = getdigits(&p);
6110 *argt |= (ZEROR | NOTADR);
6111
6112 if (p != val + vallen || vallen == 0)
6113 {
6114invalid_count:
6115 EMSG(_("E178: Invalid default value for count"));
6116 return FAIL;
6117 }
6118 }
6119 }
6120 else if (STRNICMP(attr, "count", attrlen) == 0)
6121 {
Bram Moolenaar32e7b2d2005-02-27 22:36:47 +00006122 *argt |= (COUNT | ZEROR | RANGE | NOTADR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006123
6124 if (val != NULL)
6125 {
6126 p = val;
6127 if (*def >= 0)
6128 goto two_count;
6129
6130 *def = getdigits(&p);
6131
6132 if (p != val + vallen)
6133 goto invalid_count;
6134 }
6135
6136 if (*def < 0)
6137 *def = 0;
6138 }
6139 else if (STRNICMP(attr, "complete", attrlen) == 0)
6140 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006141 if (val == NULL)
6142 {
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006143 EMSG(_("E179: argument required for -complete"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006144 return FAIL;
6145 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006146
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006147 if (parse_compl_arg(val, (int)vallen, compl, argt, compl_arg)
6148 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006149 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006150 }
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006151 else if (STRNICMP(attr, "addr", attrlen) == 0)
6152 {
6153 *argt |= RANGE;
6154 if (val == NULL)
6155 {
6156 EMSG(_("E179: argument required for -addr"));
6157 return FAIL;
6158 }
6159 if (parse_addr_type_arg(val, (int)vallen, argt, addr_type_arg)
6160 == FAIL)
6161 return FAIL;
6162 if (addr_type_arg != ADDR_LINES)
6163 *argt |= (ZEROR | NOTADR) ;
6164 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006165 else
6166 {
6167 char_u ch = attr[len];
6168 attr[len] = '\0';
6169 EMSG2(_("E181: Invalid attribute: %s"), attr);
6170 attr[len] = ch;
6171 return FAIL;
6172 }
6173 }
6174
6175 return OK;
6176}
6177
Bram Moolenaara850a712009-01-28 14:42:59 +00006178/*
6179 * ":command ..."
6180 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006181 static void
6182ex_command(eap)
6183 exarg_T *eap;
6184{
6185 char_u *name;
6186 char_u *end;
6187 char_u *p;
6188 long argt = 0;
6189 long def = -1;
6190 int flags = 0;
6191 int compl = EXPAND_NOTHING;
6192 char_u *compl_arg = NULL;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006193 int addr_type_arg = ADDR_LINES;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006194 int has_attr = (eap->arg[0] == '-');
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006195 int name_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006196
6197 p = eap->arg;
6198
6199 /* Check for attributes */
6200 while (*p == '-')
6201 {
6202 ++p;
6203 end = skiptowhite(p);
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006204 if (uc_scan_attr(p, end - p, &argt, &def, &flags, &compl, &compl_arg, &addr_type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006205 == FAIL)
6206 return;
6207 p = skipwhite(end);
6208 }
6209
6210 /* Get the name (if any) and skip to the following argument */
6211 name = p;
6212 if (ASCII_ISALPHA(*p))
6213 while (ASCII_ISALNUM(*p))
6214 ++p;
6215 if (!ends_excmd(*p) && !vim_iswhite(*p))
6216 {
6217 EMSG(_("E182: Invalid command name"));
6218 return;
6219 }
6220 end = p;
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006221 name_len = (int)(end - name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006222
6223 /* If there is nothing after the name, and no attributes were specified,
6224 * we are listing commands
6225 */
6226 p = skipwhite(end);
6227 if (!has_attr && ends_excmd(*p))
6228 {
6229 uc_list(name, end - name);
6230 }
6231 else if (!ASCII_ISUPPER(*name))
6232 {
6233 EMSG(_("E183: User defined commands must start with an uppercase letter"));
6234 return;
6235 }
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006236 else if ((name_len == 1 && *name == 'X')
6237 || (name_len <= 4
6238 && STRNCMP(name, "Next", name_len > 4 ? 4 : name_len) == 0))
6239 {
6240 EMSG(_("E841: Reserved name, cannot be used for user defined command"));
6241 return;
6242 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006243 else
6244 uc_add_command(name, end - name, p, argt, def, flags, compl, compl_arg,
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006245 addr_type_arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006246}
6247
6248/*
6249 * ":comclear"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006250 * Clear all user commands, global and for current buffer.
6251 */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00006252 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00006253ex_comclear(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006254 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006255{
6256 uc_clear(&ucmds);
6257 uc_clear(&curbuf->b_ucmds);
6258}
6259
6260/*
6261 * Clear all user commands for "gap".
6262 */
6263 void
6264uc_clear(gap)
6265 garray_T *gap;
6266{
6267 int i;
6268 ucmd_T *cmd;
6269
6270 for (i = 0; i < gap->ga_len; ++i)
6271 {
6272 cmd = USER_CMD_GA(gap, i);
6273 vim_free(cmd->uc_name);
6274 vim_free(cmd->uc_rep);
6275# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6276 vim_free(cmd->uc_compl_arg);
6277# endif
6278 }
6279 ga_clear(gap);
6280}
6281
6282 static void
6283ex_delcommand(eap)
6284 exarg_T *eap;
6285{
6286 int i = 0;
6287 ucmd_T *cmd = NULL;
6288 int cmp = -1;
6289 garray_T *gap;
6290
6291 gap = &curbuf->b_ucmds;
6292 for (;;)
6293 {
6294 for (i = 0; i < gap->ga_len; ++i)
6295 {
6296 cmd = USER_CMD_GA(gap, i);
6297 cmp = STRCMP(eap->arg, cmd->uc_name);
6298 if (cmp <= 0)
6299 break;
6300 }
6301 if (gap == &ucmds || cmp == 0)
6302 break;
6303 gap = &ucmds;
6304 }
6305
6306 if (cmp != 0)
6307 {
6308 EMSG2(_("E184: No such user-defined command: %s"), eap->arg);
6309 return;
6310 }
6311
6312 vim_free(cmd->uc_name);
6313 vim_free(cmd->uc_rep);
6314# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6315 vim_free(cmd->uc_compl_arg);
6316# endif
6317
6318 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006319
6320 if (i < gap->ga_len)
6321 mch_memmove(cmd, cmd + 1, (gap->ga_len - i) * sizeof(ucmd_T));
6322}
6323
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006324/*
6325 * split and quote args for <f-args>
6326 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006327 static char_u *
6328uc_split_args(arg, lenp)
6329 char_u *arg;
6330 size_t *lenp;
6331{
6332 char_u *buf;
6333 char_u *p;
6334 char_u *q;
6335 int len;
6336
6337 /* Precalculate length */
6338 p = arg;
6339 len = 2; /* Initial and final quotes */
6340
6341 while (*p)
6342 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006343 if (p[0] == '\\' && p[1] == '\\')
6344 {
6345 len += 2;
6346 p += 2;
6347 }
6348 else if (p[0] == '\\' && vim_iswhite(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006349 {
6350 len += 1;
6351 p += 2;
6352 }
6353 else if (*p == '\\' || *p == '"')
6354 {
6355 len += 2;
6356 p += 1;
6357 }
6358 else if (vim_iswhite(*p))
6359 {
6360 p = skipwhite(p);
6361 if (*p == NUL)
6362 break;
6363 len += 3; /* "," */
6364 }
6365 else
6366 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02006367#ifdef FEAT_MBYTE
6368 int charlen = (*mb_ptr2len)(p);
6369 len += charlen;
6370 p += charlen;
6371#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006372 ++len;
6373 ++p;
Bram Moolenaardfef1542012-07-10 19:25:10 +02006374#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006375 }
6376 }
6377
6378 buf = alloc(len + 1);
6379 if (buf == NULL)
6380 {
6381 *lenp = 0;
6382 return buf;
6383 }
6384
6385 p = arg;
6386 q = buf;
6387 *q++ = '"';
6388 while (*p)
6389 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006390 if (p[0] == '\\' && p[1] == '\\')
6391 {
6392 *q++ = '\\';
6393 *q++ = '\\';
6394 p += 2;
6395 }
6396 else if (p[0] == '\\' && vim_iswhite(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006397 {
6398 *q++ = p[1];
6399 p += 2;
6400 }
6401 else if (*p == '\\' || *p == '"')
6402 {
6403 *q++ = '\\';
6404 *q++ = *p++;
6405 }
6406 else if (vim_iswhite(*p))
6407 {
6408 p = skipwhite(p);
6409 if (*p == NUL)
6410 break;
6411 *q++ = '"';
6412 *q++ = ',';
6413 *q++ = '"';
6414 }
6415 else
6416 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02006417 MB_COPY_CHAR(p, q);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006418 }
6419 }
6420 *q++ = '"';
6421 *q = 0;
6422
6423 *lenp = len;
6424 return buf;
6425}
6426
6427/*
6428 * Check for a <> code in a user command.
6429 * "code" points to the '<'. "len" the length of the <> (inclusive).
6430 * "buf" is where the result is to be added.
6431 * "split_buf" points to a buffer used for splitting, caller should free it.
6432 * "split_len" is the length of what "split_buf" contains.
6433 * Returns the length of the replacement, which has been added to "buf".
6434 * Returns -1 if there was no match, and only the "<" has been copied.
6435 */
6436 static size_t
6437uc_check_code(code, len, buf, cmd, eap, split_buf, split_len)
6438 char_u *code;
6439 size_t len;
6440 char_u *buf;
6441 ucmd_T *cmd; /* the user command we're expanding */
6442 exarg_T *eap; /* ex arguments */
6443 char_u **split_buf;
6444 size_t *split_len;
6445{
6446 size_t result = 0;
6447 char_u *p = code + 1;
6448 size_t l = len - 2;
6449 int quote = 0;
6450 enum { ct_ARGS, ct_BANG, ct_COUNT, ct_LINE1, ct_LINE2, ct_REGISTER,
6451 ct_LT, ct_NONE } type = ct_NONE;
6452
6453 if ((vim_strchr((char_u *)"qQfF", *p) != NULL) && p[1] == '-')
6454 {
6455 quote = (*p == 'q' || *p == 'Q') ? 1 : 2;
6456 p += 2;
6457 l -= 2;
6458 }
6459
Bram Moolenaar371d5402006-03-20 21:47:49 +00006460 ++l;
6461 if (l <= 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006462 type = ct_NONE;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006463 else if (STRNICMP(p, "args>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006464 type = ct_ARGS;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006465 else if (STRNICMP(p, "bang>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006466 type = ct_BANG;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006467 else if (STRNICMP(p, "count>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006468 type = ct_COUNT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006469 else if (STRNICMP(p, "line1>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006470 type = ct_LINE1;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006471 else if (STRNICMP(p, "line2>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006472 type = ct_LINE2;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006473 else if (STRNICMP(p, "lt>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006474 type = ct_LT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006475 else if (STRNICMP(p, "reg>", l) == 0 || STRNICMP(p, "register>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006476 type = ct_REGISTER;
6477
6478 switch (type)
6479 {
6480 case ct_ARGS:
6481 /* Simple case first */
6482 if (*eap->arg == NUL)
6483 {
6484 if (quote == 1)
6485 {
6486 result = 2;
6487 if (buf != NULL)
6488 STRCPY(buf, "''");
6489 }
6490 else
6491 result = 0;
6492 break;
6493 }
6494
6495 /* When specified there is a single argument don't split it.
6496 * Works for ":Cmd %" when % is "a b c". */
6497 if ((eap->argt & NOSPC) && quote == 2)
6498 quote = 1;
6499
6500 switch (quote)
6501 {
6502 case 0: /* No quoting, no splitting */
6503 result = STRLEN(eap->arg);
6504 if (buf != NULL)
6505 STRCPY(buf, eap->arg);
6506 break;
6507 case 1: /* Quote, but don't split */
6508 result = STRLEN(eap->arg) + 2;
6509 for (p = eap->arg; *p; ++p)
6510 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006511#ifdef FEAT_MBYTE
6512 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6513 /* DBCS can contain \ in a trail byte, skip the
6514 * double-byte character. */
6515 ++p;
6516 else
6517#endif
6518 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006519 ++result;
6520 }
6521
6522 if (buf != NULL)
6523 {
6524 *buf++ = '"';
6525 for (p = eap->arg; *p; ++p)
6526 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006527#ifdef FEAT_MBYTE
6528 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6529 /* DBCS can contain \ in a trail byte, copy the
6530 * double-byte character to avoid escaping. */
6531 *buf++ = *p++;
6532 else
6533#endif
6534 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006535 *buf++ = '\\';
6536 *buf++ = *p;
6537 }
6538 *buf = '"';
6539 }
6540
6541 break;
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006542 case 2: /* Quote and split (<f-args>) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006543 /* This is hard, so only do it once, and cache the result */
6544 if (*split_buf == NULL)
6545 *split_buf = uc_split_args(eap->arg, split_len);
6546
6547 result = *split_len;
6548 if (buf != NULL && result != 0)
6549 STRCPY(buf, *split_buf);
6550
6551 break;
6552 }
6553 break;
6554
6555 case ct_BANG:
6556 result = eap->forceit ? 1 : 0;
6557 if (quote)
6558 result += 2;
6559 if (buf != NULL)
6560 {
6561 if (quote)
6562 *buf++ = '"';
6563 if (eap->forceit)
6564 *buf++ = '!';
6565 if (quote)
6566 *buf = '"';
6567 }
6568 break;
6569
6570 case ct_LINE1:
6571 case ct_LINE2:
6572 case ct_COUNT:
6573 {
6574 char num_buf[20];
6575 long num = (type == ct_LINE1) ? eap->line1 :
6576 (type == ct_LINE2) ? eap->line2 :
6577 (eap->addr_count > 0) ? eap->line2 : cmd->uc_def;
6578 size_t num_len;
6579
6580 sprintf(num_buf, "%ld", num);
6581 num_len = STRLEN(num_buf);
6582 result = num_len;
6583
6584 if (quote)
6585 result += 2;
6586
6587 if (buf != NULL)
6588 {
6589 if (quote)
6590 *buf++ = '"';
6591 STRCPY(buf, num_buf);
6592 buf += num_len;
6593 if (quote)
6594 *buf = '"';
6595 }
6596
6597 break;
6598 }
6599
6600 case ct_REGISTER:
6601 result = eap->regname ? 1 : 0;
6602 if (quote)
6603 result += 2;
6604 if (buf != NULL)
6605 {
6606 if (quote)
6607 *buf++ = '\'';
6608 if (eap->regname)
6609 *buf++ = eap->regname;
6610 if (quote)
6611 *buf = '\'';
6612 }
6613 break;
6614
6615 case ct_LT:
6616 result = 1;
6617 if (buf != NULL)
6618 *buf = '<';
6619 break;
6620
6621 default:
6622 /* Not recognized: just copy the '<' and return -1. */
6623 result = (size_t)-1;
6624 if (buf != NULL)
6625 *buf = '<';
6626 break;
6627 }
6628
6629 return result;
6630}
6631
6632 static void
6633do_ucmd(eap)
6634 exarg_T *eap;
6635{
6636 char_u *buf;
6637 char_u *p;
6638 char_u *q;
6639
6640 char_u *start;
Bram Moolenaar25648a52009-02-21 19:37:46 +00006641 char_u *end = NULL;
Bram Moolenaara850a712009-01-28 14:42:59 +00006642 char_u *ksp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006643 size_t len, totlen;
6644
6645 size_t split_len = 0;
6646 char_u *split_buf = NULL;
6647 ucmd_T *cmd;
6648#ifdef FEAT_EVAL
6649 scid_T save_current_SID = current_SID;
6650#endif
6651
6652 if (eap->cmdidx == CMD_USER)
6653 cmd = USER_CMD(eap->useridx);
6654 else
6655 cmd = USER_CMD_GA(&curbuf->b_ucmds, eap->useridx);
6656
6657 /*
6658 * Replace <> in the command by the arguments.
Bram Moolenaara850a712009-01-28 14:42:59 +00006659 * First round: "buf" is NULL, compute length, allocate "buf".
6660 * Second round: copy result into "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006661 */
6662 buf = NULL;
6663 for (;;)
6664 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006665 p = cmd->uc_rep; /* source */
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00006666 q = buf; /* destination */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006667 totlen = 0;
Bram Moolenaara850a712009-01-28 14:42:59 +00006668
6669 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006670 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006671 start = vim_strchr(p, '<');
6672 if (start != NULL)
6673 end = vim_strchr(start + 1, '>');
6674 if (buf != NULL)
6675 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006676 for (ksp = p; *ksp != NUL && *ksp != K_SPECIAL; ++ksp)
6677 ;
6678 if (*ksp == K_SPECIAL
6679 && (start == NULL || ksp < start || end == NULL)
Bram Moolenaara850a712009-01-28 14:42:59 +00006680 && ((ksp[1] == KS_SPECIAL && ksp[2] == KE_FILLER)
6681# ifdef FEAT_GUI
6682 || (ksp[1] == KS_EXTRA && ksp[2] == (int)KE_CSI)
6683# endif
6684 ))
6685 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006686 /* K_SPECIAL has been put in the buffer as K_SPECIAL
Bram Moolenaara850a712009-01-28 14:42:59 +00006687 * KS_SPECIAL KE_FILLER, like for mappings, but
6688 * do_cmdline() doesn't handle that, so convert it back.
6689 * Also change K_SPECIAL KS_EXTRA KE_CSI into CSI. */
6690 len = ksp - p;
6691 if (len > 0)
6692 {
6693 mch_memmove(q, p, len);
6694 q += len;
6695 }
6696 *q++ = ksp[1] == KS_SPECIAL ? K_SPECIAL : CSI;
6697 p = ksp + 3;
6698 continue;
6699 }
6700 }
6701
6702 /* break if there no <item> is found */
6703 if (start == NULL || end == NULL)
6704 break;
6705
Bram Moolenaar071d4272004-06-13 20:20:40 +00006706 /* Include the '>' */
6707 ++end;
6708
6709 /* Take everything up to the '<' */
6710 len = start - p;
6711 if (buf == NULL)
6712 totlen += len;
6713 else
6714 {
6715 mch_memmove(q, p, len);
6716 q += len;
6717 }
6718
6719 len = uc_check_code(start, end - start, q, cmd, eap,
6720 &split_buf, &split_len);
6721 if (len == (size_t)-1)
6722 {
6723 /* no match, continue after '<' */
6724 p = start + 1;
6725 len = 1;
6726 }
6727 else
6728 p = end;
6729 if (buf == NULL)
6730 totlen += len;
6731 else
6732 q += len;
6733 }
6734 if (buf != NULL) /* second time here, finished */
6735 {
6736 STRCPY(q, p);
6737 break;
6738 }
6739
6740 totlen += STRLEN(p); /* Add on the trailing characters */
6741 buf = alloc((unsigned)(totlen + 1));
6742 if (buf == NULL)
6743 {
6744 vim_free(split_buf);
6745 return;
6746 }
6747 }
6748
6749#ifdef FEAT_EVAL
6750 current_SID = cmd->uc_scriptID;
6751#endif
6752 (void)do_cmdline(buf, eap->getline, eap->cookie,
6753 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
6754#ifdef FEAT_EVAL
6755 current_SID = save_current_SID;
6756#endif
6757 vim_free(buf);
6758 vim_free(split_buf);
6759}
6760
6761# if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6762 static char_u *
6763get_user_command_name(idx)
6764 int idx;
6765{
6766 return get_user_commands(NULL, idx - (int)CMD_SIZE);
6767}
6768
6769/*
6770 * Function given to ExpandGeneric() to obtain the list of user command names.
6771 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006772 char_u *
6773get_user_commands(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006774 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006775 int idx;
6776{
6777 if (idx < curbuf->b_ucmds.ga_len)
6778 return USER_CMD_GA(&curbuf->b_ucmds, idx)->uc_name;
6779 idx -= curbuf->b_ucmds.ga_len;
6780 if (idx < ucmds.ga_len)
6781 return USER_CMD(idx)->uc_name;
6782 return NULL;
6783}
6784
6785/*
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006786 * Function given to ExpandGeneric() to obtain the list of user address type names.
6787 */
6788 char_u *
6789get_user_cmd_addr_type(xp, idx)
6790 expand_T *xp UNUSED;
6791 int idx;
6792{
6793 return (char_u *)addr_type_complete[idx].name;
6794}
6795
6796/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006797 * Function given to ExpandGeneric() to obtain the list of user command
6798 * attributes.
6799 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006800 char_u *
6801get_user_cmd_flags(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006802 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006803 int idx;
6804{
6805 static char *user_cmd_flags[] =
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006806 {"addr", "bang", "bar", "buffer", "complete",
6807 "count", "nargs", "range", "register"};
Bram Moolenaar071d4272004-06-13 20:20:40 +00006808
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00006809 if (idx >= (int)(sizeof(user_cmd_flags) / sizeof(user_cmd_flags[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006810 return NULL;
6811 return (char_u *)user_cmd_flags[idx];
6812}
6813
6814/*
6815 * Function given to ExpandGeneric() to obtain the list of values for -nargs.
6816 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006817 char_u *
6818get_user_cmd_nargs(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006819 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006820 int idx;
6821{
6822 static char *user_cmd_nargs[] = {"0", "1", "*", "?", "+"};
6823
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00006824 if (idx >= (int)(sizeof(user_cmd_nargs) / sizeof(user_cmd_nargs[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006825 return NULL;
6826 return (char_u *)user_cmd_nargs[idx];
6827}
6828
6829/*
6830 * Function given to ExpandGeneric() to obtain the list of values for -complete.
6831 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006832 char_u *
6833get_user_cmd_complete(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006834 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006835 int idx;
6836{
6837 return (char_u *)command_complete[idx].name;
6838}
6839# endif /* FEAT_CMDL_COMPL */
6840
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006841/*
6842 * Parse address type argument
6843 */
6844 int
6845parse_addr_type_arg(value, vallen, argt, addr_type_arg)
6846 char_u *value;
6847 int vallen;
6848 long *argt;
6849 int *addr_type_arg;
6850{
6851 int i, a, b;
6852 for (i = 0; addr_type_complete[i].expand != -1; ++i)
6853 {
6854 a = (int)STRLEN(addr_type_complete[i].name) == vallen;
6855 b = STRNCMP(value, addr_type_complete[i].name, vallen) == 0;
6856 if (a && b)
6857 {
6858 *addr_type_arg = addr_type_complete[i].expand;
6859 break;
6860 }
6861 }
6862
6863 if (addr_type_complete[i].expand == -1)
6864 {
6865 char_u *err = value;
6866 for (i=0; err[i] == NUL || !vim_iswhite(err[i]); i++);
6867 err[i] = NUL;
6868 EMSG2(_("E180: Invalid address type value: %s"), err);
6869 return FAIL;
6870 }
6871
6872 if (*addr_type_arg != ADDR_LINES)
6873 *argt |= NOTADR;
6874
6875 return OK;
6876}
6877
Bram Moolenaar071d4272004-06-13 20:20:40 +00006878#endif /* FEAT_USR_CMDS */
6879
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006880#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
6881/*
6882 * Parse a completion argument "value[vallen]".
6883 * The detected completion goes in "*complp", argument type in "*argt".
6884 * When there is an argument, for function and user defined completion, it's
6885 * copied to allocated memory and stored in "*compl_arg".
6886 * Returns FAIL if something is wrong.
6887 */
6888 int
6889parse_compl_arg(value, vallen, complp, argt, compl_arg)
6890 char_u *value;
6891 int vallen;
6892 int *complp;
6893 long *argt;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006894 char_u **compl_arg UNUSED;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006895{
6896 char_u *arg = NULL;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006897# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006898 size_t arglen = 0;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006899# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006900 int i;
6901 int valend = vallen;
6902
6903 /* Look for any argument part - which is the part after any ',' */
6904 for (i = 0; i < vallen; ++i)
6905 {
6906 if (value[i] == ',')
6907 {
6908 arg = &value[i + 1];
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006909# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006910 arglen = vallen - i - 1;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006911# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006912 valend = i;
6913 break;
6914 }
6915 }
6916
6917 for (i = 0; command_complete[i].expand != 0; ++i)
6918 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006919 if ((int)STRLEN(command_complete[i].name) == valend
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006920 && STRNCMP(value, command_complete[i].name, valend) == 0)
6921 {
6922 *complp = command_complete[i].expand;
6923 if (command_complete[i].expand == EXPAND_BUFFERS)
6924 *argt |= BUFNAME;
6925 else if (command_complete[i].expand == EXPAND_DIRECTORIES
6926 || command_complete[i].expand == EXPAND_FILES)
6927 *argt |= XFILE;
6928 break;
6929 }
6930 }
6931
6932 if (command_complete[i].expand == 0)
6933 {
6934 EMSG2(_("E180: Invalid complete value: %s"), value);
6935 return FAIL;
6936 }
6937
6938# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6939 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST
6940 && arg != NULL)
6941# else
6942 if (arg != NULL)
6943# endif
6944 {
6945 EMSG(_("E468: Completion argument only allowed for custom completion"));
6946 return FAIL;
6947 }
6948
6949# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6950 if ((*complp == EXPAND_USER_DEFINED || *complp == EXPAND_USER_LIST)
6951 && arg == NULL)
6952 {
6953 EMSG(_("E467: Custom completion requires a function argument"));
6954 return FAIL;
6955 }
6956
6957 if (arg != NULL)
6958 *compl_arg = vim_strnsave(arg, (int)arglen);
6959# endif
6960 return OK;
6961}
6962#endif
6963
Bram Moolenaar071d4272004-06-13 20:20:40 +00006964 static void
6965ex_colorscheme(eap)
6966 exarg_T *eap;
6967{
Bram Moolenaare6850792010-05-14 15:28:44 +02006968 if (*eap->arg == NUL)
6969 {
6970#ifdef FEAT_EVAL
6971 char_u *expr = vim_strsave((char_u *)"g:colors_name");
6972 char_u *p = NULL;
6973
6974 if (expr != NULL)
6975 {
6976 ++emsg_off;
6977 p = eval_to_string(expr, NULL, FALSE);
6978 --emsg_off;
6979 vim_free(expr);
6980 }
6981 if (p != NULL)
6982 {
6983 MSG(p);
6984 vim_free(p);
6985 }
6986 else
6987 MSG("default");
6988#else
6989 MSG(_("unknown"));
6990#endif
6991 }
6992 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarbe1e9e92012-09-18 16:47:07 +02006993 EMSG2(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006994}
6995
6996 static void
6997ex_highlight(eap)
6998 exarg_T *eap;
6999{
7000 if (*eap->arg == NUL && eap->cmd[2] == '!')
7001 MSG(_("Greetings, Vim user!"));
7002 do_highlight(eap->arg, eap->forceit, FALSE);
7003}
7004
7005
7006/*
7007 * Call this function if we thought we were going to exit, but we won't
7008 * (because of an error). May need to restore the terminal mode.
7009 */
7010 void
7011not_exiting()
7012{
7013 exiting = FALSE;
7014 settmode(TMODE_RAW);
7015}
7016
7017/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01007018 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007019 */
7020 static void
7021ex_quit(eap)
7022 exarg_T *eap;
7023{
Bram Moolenaarf240e182014-11-27 18:33:02 +01007024#if defined(FEAT_WINDOWS) || defined(FEAT_AUTOCMD)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007025 win_T *wp;
Bram Moolenaarf240e182014-11-27 18:33:02 +01007026#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007027
Bram Moolenaar071d4272004-06-13 20:20:40 +00007028#ifdef FEAT_CMDWIN
7029 if (cmdwin_type != 0)
7030 {
7031 cmdwin_result = Ctrl_C;
7032 return;
7033 }
7034#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007035 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007036 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007037 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007038 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007039 return;
7040 }
Bram Moolenaarf240e182014-11-27 18:33:02 +01007041#ifdef FEAT_WINDOWS
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007042 if (eap->addr_count > 0)
7043 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01007044 int wnr = eap->line2;
7045
7046 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
7047 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007048 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007049 }
7050 else
Bram Moolenaarf240e182014-11-27 18:33:02 +01007051#endif
7052#if defined(FEAT_WINDOWS) || defined(FEAT_AUTOCMD)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007053 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01007054#endif
7055
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007056#ifdef FEAT_AUTOCMD
Bram Moolenaar3b53dfb2012-06-06 18:03:07 +02007057 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01007058 /* Refuse to quit when locked or when the buffer in the last window is
Bram Moolenaar362ce482012-06-06 19:02:45 +02007059 * being closed (can only happen in autocommands). */
Bram Moolenaarf240e182014-11-27 18:33:02 +01007060 if (curbuf_locked() || (wp->w_buffer->b_nwindows == 1
7061 && wp->w_buffer->b_closing))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007062 return;
7063#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007064
7065#ifdef FEAT_NETBEANS_INTG
7066 netbeansForcedQuit = eap->forceit;
7067#endif
7068
7069 /*
7070 * If there are more files or windows we won't exit.
7071 */
7072 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
7073 exiting = TRUE;
7074 if ((!P_HID(curbuf)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01007075 && check_changed(curbuf, (p_awa ? CCGD_AW : 0)
7076 | (eap->forceit ? CCGD_FORCEIT : 0)
7077 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007078 || check_more(TRUE, eap->forceit) == FAIL
7079 || (only_one_window() && check_changed_any(eap->forceit)))
7080 {
7081 not_exiting();
7082 }
7083 else
7084 {
7085#ifdef FEAT_WINDOWS
7086 if (only_one_window()) /* quit last window */
7087#endif
7088 getout(0);
7089#ifdef FEAT_WINDOWS
7090# ifdef FEAT_GUI
7091 need_mouse_correct = TRUE;
7092# endif
7093 /* close window; may free buffer */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007094 win_close(wp, !P_HID(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007095#endif
7096 }
7097}
7098
7099/*
7100 * ":cquit".
7101 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007102 static void
7103ex_cquit(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007104 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007105{
7106 getout(1); /* this does not always pass on the exit code to the Manx
7107 compiler. why? */
7108}
7109
7110/*
7111 * ":qall": try to quit all windows
7112 */
7113 static void
7114ex_quit_all(eap)
7115 exarg_T *eap;
7116{
7117# ifdef FEAT_CMDWIN
7118 if (cmdwin_type != 0)
7119 {
7120 if (eap->forceit)
7121 cmdwin_result = K_XF1; /* ex_window() takes care of this */
7122 else
7123 cmdwin_result = K_XF2;
7124 return;
7125 }
7126# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007127
7128 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007129 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007130 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007131 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007132 return;
7133 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007134#ifdef FEAT_AUTOCMD
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01007135 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
7136 /* Refuse to quit when locked or when the buffer in the last window is
7137 * being closed (can only happen in autocommands). */
7138 if (curbuf_locked() || (curbuf->b_nwindows == 1 && curbuf->b_closing))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007139 return;
7140#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007141
Bram Moolenaar071d4272004-06-13 20:20:40 +00007142 exiting = TRUE;
7143 if (eap->forceit || !check_changed_any(FALSE))
7144 getout(0);
7145 not_exiting();
7146}
7147
Bram Moolenaard9967712006-03-11 21:18:15 +00007148#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007149/*
7150 * ":close": close current window, unless it is the last one
7151 */
7152 static void
7153ex_close(eap)
7154 exarg_T *eap;
7155{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007156 win_T *win;
7157 int winnr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007158# ifdef FEAT_CMDWIN
7159 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02007160 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007161 else
7162# endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007163 if (!text_locked()
7164#ifdef FEAT_AUTOCMD
7165 && !curbuf_locked()
7166#endif
7167 )
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007168 {
7169 if (eap->addr_count == 0)
7170 ex_win_close(eap->forceit, curwin, NULL);
7171 else {
7172 for (win = firstwin; win != NULL; win = win->w_next)
7173 {
7174 winnr++;
7175 if (winnr == eap->line2)
7176 break;
7177 }
7178 if (win == NULL)
7179 win = lastwin;
7180 ex_win_close(eap->forceit, win, NULL);
7181 }
7182 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007183}
7184
Bram Moolenaard9967712006-03-11 21:18:15 +00007185# ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007186/*
7187 * ":pclose": Close any preview window.
7188 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007189 static void
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007190ex_pclose(eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007191 exarg_T *eap;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007192{
7193 win_T *win;
7194
7195 for (win = firstwin; win != NULL; win = win->w_next)
7196 if (win->w_p_pvw)
7197 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00007198 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007199 break;
7200 }
7201}
Bram Moolenaard9967712006-03-11 21:18:15 +00007202# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007203
Bram Moolenaarf740b292006-02-16 22:11:02 +00007204/*
7205 * Close window "win" and take care of handling closing the last window for a
7206 * modified buffer.
7207 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007208 static void
Bram Moolenaarf740b292006-02-16 22:11:02 +00007209ex_win_close(forceit, win, tp)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007210 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007211 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007212 tabpage_T *tp; /* NULL or the tab page "win" is in */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007213{
7214 int need_hide;
7215 buf_T *buf = win->w_buffer;
7216
7217 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007218 if (need_hide && !P_HID(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007219 {
Bram Moolenaard9967712006-03-11 21:18:15 +00007220# if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007221 if ((p_confirm || cmdmod.confirm) && p_write)
7222 {
7223 dialog_changed(buf, FALSE);
7224 if (buf_valid(buf) && bufIsChanged(buf))
7225 return;
7226 need_hide = FALSE;
7227 }
7228 else
Bram Moolenaard9967712006-03-11 21:18:15 +00007229# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007230 {
7231 EMSG(_(e_nowrtmsg));
7232 return;
7233 }
7234 }
7235
Bram Moolenaard9967712006-03-11 21:18:15 +00007236# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007237 need_mouse_correct = TRUE;
Bram Moolenaard9967712006-03-11 21:18:15 +00007238# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00007239
Bram Moolenaar071d4272004-06-13 20:20:40 +00007240 /* free buffer when not hiding it or when it's a scratch buffer */
Bram Moolenaarf740b292006-02-16 22:11:02 +00007241 if (tp == NULL)
7242 win_close(win, !need_hide && !P_HID(buf));
7243 else
7244 win_close_othertab(win, !need_hide && !P_HID(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007245}
7246
Bram Moolenaar071d4272004-06-13 20:20:40 +00007247/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00007248 * ":tabclose": close current tab page, unless it is the last one.
7249 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007250 */
7251 static void
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007252ex_tabclose(eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007253 exarg_T *eap;
7254{
Bram Moolenaarf740b292006-02-16 22:11:02 +00007255 tabpage_T *tp;
7256
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007257# ifdef FEAT_CMDWIN
7258 if (cmdwin_type != 0)
7259 cmdwin_result = K_IGNORE;
7260 else
7261# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00007262 if (first_tabpage->tp_next == NULL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007263 EMSG(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00007264 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007265 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00007266 if (eap->addr_count > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007267 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00007268 tp = find_tabpage((int)eap->line2);
7269 if (tp == NULL)
7270 {
7271 beep_flush();
7272 return;
7273 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007274 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007275 {
7276 tabpage_close_other(tp, eap->forceit);
7277 return;
7278 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007279 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007280 if (!text_locked()
7281#ifdef FEAT_AUTOCMD
7282 && !curbuf_locked()
7283#endif
7284 )
Bram Moolenaarf740b292006-02-16 22:11:02 +00007285 tabpage_close(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007286 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007287}
7288
7289/*
7290 * ":tabonly": close all tab pages except the current one
7291 */
7292 static void
7293ex_tabonly(eap)
7294 exarg_T *eap;
7295{
7296 tabpage_T *tp;
7297 int done;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007298
7299# ifdef FEAT_CMDWIN
7300 if (cmdwin_type != 0)
7301 cmdwin_result = K_IGNORE;
7302 else
7303# endif
7304 if (first_tabpage->tp_next == NULL)
7305 MSG(_("Already only one tab page"));
7306 else
7307 {
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007308 if (eap->addr_count > 0)
7309 goto_tabpage(eap->line2);
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007310 /* Repeat this up to a 1000 times, because autocommands may mess
7311 * up the lists. */
7312 for (done = 0; done < 1000; ++done)
7313 {
7314 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
7315 if (tp->tp_topframe != topframe)
7316 {
7317 tabpage_close_other(tp, eap->forceit);
7318 /* if we failed to close it quit */
7319 if (valid_tabpage(tp))
7320 done = 1000;
7321 /* start over, "tp" is now invalid */
7322 break;
7323 }
7324 if (first_tabpage->tp_next == NULL)
7325 break;
7326 }
7327 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007328}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007329
7330/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00007331 * Close the current tab page.
7332 */
7333 void
7334tabpage_close(forceit)
7335 int forceit;
7336{
7337 /* First close all the windows but the current one. If that worked then
7338 * close the last window in this tab, that will close it. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007339 if (lastwin != firstwin)
7340 close_others(TRUE, forceit);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007341 if (lastwin == firstwin)
7342 ex_win_close(forceit, curwin, NULL);
7343# ifdef FEAT_GUI
7344 need_mouse_correct = TRUE;
7345# endif
7346}
7347
7348/*
7349 * Close tab page "tp", which is not the current tab page.
7350 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007351 * Also takes care of the tab pages line disappearing when closing the
7352 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00007353 */
7354 void
7355tabpage_close_other(tp, forceit)
7356 tabpage_T *tp;
7357 int forceit;
7358{
7359 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007360 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007361 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00007362
7363 /* Limit to 1000 windows, autocommands may add a window while we close
7364 * one. OK, so I'm paranoid... */
7365 while (++done < 1000)
7366 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007367 wp = tp->tp_firstwin;
7368 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007369
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007370 /* Autocommands may delete the tab page under our fingers and we may
7371 * fail to close a window with a modified buffer. */
7372 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007373 break;
7374 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007375
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007376 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007377 if (h != tabline_height())
7378 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00007379}
7380
7381/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007382 * ":only".
7383 */
7384 static void
7385ex_only(eap)
7386 exarg_T *eap;
7387{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007388 win_T *wp;
7389 int wnr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007390# ifdef FEAT_GUI
7391 need_mouse_correct = TRUE;
7392# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007393 if (eap->addr_count > 0)
7394 {
7395 wnr = eap->line2;
7396 for (wp = firstwin; --wnr > 0; )
7397 {
7398 if (wp->w_next == NULL)
7399 break;
7400 else
7401 wp = wp->w_next;
7402 }
7403 win_goto(wp);
7404 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007405 close_others(TRUE, eap->forceit);
7406}
7407
7408/*
7409 * ":all" and ":sall".
Bram Moolenaard9967712006-03-11 21:18:15 +00007410 * Also used for ":tab drop file ..." after setting the argument list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007411 */
Bram Moolenaard9967712006-03-11 21:18:15 +00007412 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00007413ex_all(eap)
7414 exarg_T *eap;
7415{
7416 if (eap->addr_count == 0)
7417 eap->line2 = 9999;
Bram Moolenaard9967712006-03-11 21:18:15 +00007418 do_arg_all((int)eap->line2, eap->forceit, eap->cmdidx == CMD_drop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007419}
7420#endif /* FEAT_WINDOWS */
7421
7422 static void
7423ex_hide(eap)
7424 exarg_T *eap;
7425{
7426 if (*eap->arg != NUL && check_nextcmd(eap->arg) == NULL)
7427 eap->errmsg = e_invarg;
7428 else
7429 {
7430 /* ":hide" or ":hide | cmd": hide current window */
7431 eap->nextcmd = check_nextcmd(eap->arg);
7432#ifdef FEAT_WINDOWS
7433 if (!eap->skip)
7434 {
7435# ifdef FEAT_GUI
7436 need_mouse_correct = TRUE;
7437# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007438 if (eap->addr_count == 0)
7439 win_close(curwin, FALSE); /* don't free buffer */
Bram Moolenaarf240e182014-11-27 18:33:02 +01007440 else
7441 {
7442 int winnr = 0;
7443 win_T *win;
7444
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007445 for (win = firstwin; win != NULL; win = win->w_next)
7446 {
7447 winnr++;
7448 if (winnr == eap->line2)
7449 break;
7450 }
7451 if (win == NULL)
7452 win = lastwin;
7453 win_close(win, FALSE);
7454 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007455 }
7456#endif
7457 }
7458}
7459
7460/*
7461 * ":stop" and ":suspend": Suspend Vim.
7462 */
7463 static void
7464ex_stop(eap)
7465 exarg_T *eap;
7466{
7467 /*
7468 * Disallow suspending for "rvim".
7469 */
7470 if (!check_restricted()
7471#ifdef WIN3264
7472 /*
7473 * Check if external commands are allowed now.
7474 */
7475 && can_end_termcap_mode(TRUE)
7476#endif
7477 )
7478 {
7479 if (!eap->forceit)
7480 autowrite_all();
7481 windgoto((int)Rows - 1, 0);
7482 out_char('\n');
7483 out_flush();
7484 stoptermcap();
7485 out_flush(); /* needed for SUN to restore xterm buffer */
7486#ifdef FEAT_TITLE
7487 mch_restore_title(3); /* restore window titles */
7488#endif
7489 ui_suspend(); /* call machine specific function */
7490#ifdef FEAT_TITLE
7491 maketitle();
7492 resettitle(); /* force updating the title */
7493#endif
7494 starttermcap();
7495 scroll_start(); /* scroll screen before redrawing */
7496 redraw_later_clear();
7497 shell_resized(); /* may have resized window */
7498 }
7499}
7500
7501/*
7502 * ":exit", ":xit" and ":wq": Write file and exit Vim.
7503 */
7504 static void
7505ex_exit(eap)
7506 exarg_T *eap;
7507{
7508#ifdef FEAT_CMDWIN
7509 if (cmdwin_type != 0)
7510 {
7511 cmdwin_result = Ctrl_C;
7512 return;
7513 }
7514#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007515 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007516 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007517 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007518 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007519 return;
7520 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007521#ifdef FEAT_AUTOCMD
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01007522 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
7523 /* Refuse to quit when locked or when the buffer in the last window is
7524 * being closed (can only happen in autocommands). */
7525 if (curbuf_locked() || (curbuf->b_nwindows == 1 && curbuf->b_closing))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007526 return;
7527#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007528
7529 /*
7530 * if more files or windows we won't exit
7531 */
7532 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
7533 exiting = TRUE;
7534 if ( ((eap->cmdidx == CMD_wq
7535 || curbufIsChanged())
7536 && do_write(eap) == FAIL)
7537 || check_more(TRUE, eap->forceit) == FAIL
7538 || (only_one_window() && check_changed_any(eap->forceit)))
7539 {
7540 not_exiting();
7541 }
7542 else
7543 {
7544#ifdef FEAT_WINDOWS
7545 if (only_one_window()) /* quit last window, exit Vim */
7546#endif
7547 getout(0);
7548#ifdef FEAT_WINDOWS
7549# ifdef FEAT_GUI
7550 need_mouse_correct = TRUE;
7551# endif
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02007552 /* Quit current window, may free the buffer. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007553 win_close(curwin, !P_HID(curwin->w_buffer));
7554#endif
7555 }
7556}
7557
7558/*
7559 * ":print", ":list", ":number".
7560 */
7561 static void
7562ex_print(eap)
7563 exarg_T *eap;
7564{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007565 if (curbuf->b_ml.ml_flags & ML_EMPTY)
7566 EMSG(_(e_emptybuf));
7567 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007568 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007569 for ( ;!got_int; ui_breakcheck())
7570 {
7571 print_line(eap->line1,
7572 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
7573 || (eap->flags & EXFLAG_NR)),
7574 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
7575 if (++eap->line1 > eap->line2)
7576 break;
7577 out_flush(); /* show one line at a time */
7578 }
7579 setpcmark();
7580 /* put cursor at last line */
7581 curwin->w_cursor.lnum = eap->line2;
7582 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007583 }
7584
Bram Moolenaar071d4272004-06-13 20:20:40 +00007585 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007586}
7587
7588#ifdef FEAT_BYTEOFF
7589 static void
7590ex_goto(eap)
7591 exarg_T *eap;
7592{
7593 goto_byte(eap->line2);
7594}
7595#endif
7596
7597/*
7598 * ":shell".
7599 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007600 static void
7601ex_shell(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007602 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007603{
7604 do_shell(NULL, 0);
7605}
7606
7607#if (defined(FEAT_WINDOWS) && defined(HAVE_DROP_FILE)) \
7608 || (defined(FEAT_GUI_GTK) && defined(FEAT_DND)) \
Bram Moolenaar371baa92005-12-29 22:43:53 +00007609 || defined(FEAT_GUI_MSWIN) \
7610 || defined(FEAT_GUI_MAC) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00007611 || defined(PROTO)
7612
7613/*
7614 * Handle a file drop. The code is here because a drop is *nearly* like an
7615 * :args command, but not quite (we have a list of exact filenames, so we
7616 * don't want to (a) parse a command line, or (b) expand wildcards. So the
7617 * code is very similar to :args and hence needs access to a lot of the static
7618 * functions in this file.
7619 *
7620 * The list should be allocated using alloc(), as should each item in the
7621 * list. This function takes over responsibility for freeing the list.
7622 *
Bram Moolenaar81870892007-11-11 18:17:28 +00007623 * XXX The list is made into the argument list. This is freed using
Bram Moolenaar071d4272004-06-13 20:20:40 +00007624 * FreeWild(), which does a series of vim_free() calls, unless the two defines
7625 * __EMX__ and __ALWAYS_HAS_TRAILING_NUL_POINTER are set. In this case, a
7626 * routine _fnexplodefree() is used. This may cause problems, but as the drop
7627 * file functionality is (currently) not in EMX this is not presently a
7628 * problem.
7629 */
7630 void
7631handle_drop(filec, filev, split)
7632 int filec; /* the number of files dropped */
7633 char_u **filev; /* the list of files dropped */
7634 int split; /* force splitting the window */
7635{
7636 exarg_T ea;
7637 int save_msg_scroll = msg_scroll;
7638
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007639 /* Postpone this while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007640 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007641 return;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007642#ifdef FEAT_AUTOCMD
7643 if (curbuf_locked())
7644 return;
7645#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00007646 /* When the screen is being updated we should not change buffers and
7647 * windows structures, it may cause freed memory to be used. */
7648 if (updating_screen)
7649 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007650
7651 /* Check whether the current buffer is changed. If so, we will need
7652 * to split the current window or data could be lost.
7653 * We don't need to check if the 'hidden' option is set, as in this
7654 * case the buffer won't be lost.
7655 */
7656 if (!P_HID(curbuf) && !split)
7657 {
7658 ++emsg_off;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01007659 split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007660 --emsg_off;
7661 }
7662 if (split)
7663 {
7664# ifdef FEAT_WINDOWS
7665 if (win_split(0, 0) == FAIL)
7666 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007667 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007668
7669 /* When splitting the window, create a new alist. Otherwise the
7670 * existing one is overwritten. */
7671 alist_unlink(curwin->w_alist);
7672 alist_new();
7673# else
7674 return; /* can't split, always fail */
7675# endif
7676 }
7677
7678 /*
7679 * Set up the new argument list.
7680 */
Bram Moolenaar86b68352004-12-27 21:59:20 +00007681 alist_set(ALIST(curwin), filec, filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007682
7683 /*
7684 * Move to the first file.
7685 */
7686 /* Fake up a minimal "next" command for do_argfile() */
7687 vim_memset(&ea, 0, sizeof(ea));
7688 ea.cmd = (char_u *)"next";
7689 do_argfile(&ea, 0);
7690
7691 /* do_ecmd() may set need_start_insertmode, but since we never left Insert
7692 * mode that is not needed here. */
7693 need_start_insertmode = FALSE;
7694
7695 /* Restore msg_scroll, otherwise a following command may cause scrolling
7696 * unexpectedly. The screen will be redrawn by the caller, thus
7697 * msg_scroll being set by displaying a message is irrelevant. */
7698 msg_scroll = save_msg_scroll;
7699}
7700#endif
7701
Bram Moolenaar071d4272004-06-13 20:20:40 +00007702/*
7703 * Clear an argument list: free all file names and reset it to zero entries.
7704 */
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007705 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00007706alist_clear(al)
7707 alist_T *al;
7708{
7709 while (--al->al_ga.ga_len >= 0)
7710 vim_free(AARGLIST(al)[al->al_ga.ga_len].ae_fname);
7711 ga_clear(&al->al_ga);
7712}
7713
7714/*
7715 * Init an argument list.
7716 */
7717 void
7718alist_init(al)
7719 alist_T *al;
7720{
7721 ga_init2(&al->al_ga, (int)sizeof(aentry_T), 5);
7722}
7723
7724#if defined(FEAT_WINDOWS) || defined(PROTO)
7725
7726/*
7727 * Remove a reference from an argument list.
7728 * Ignored when the argument list is the global one.
7729 * If the argument list is no longer used by any window, free it.
7730 */
7731 void
7732alist_unlink(al)
7733 alist_T *al;
7734{
7735 if (al != &global_alist && --al->al_refcount <= 0)
7736 {
7737 alist_clear(al);
7738 vim_free(al);
7739 }
7740}
7741
7742# if defined(FEAT_LISTCMDS) || defined(HAVE_DROP_FILE) || defined(PROTO)
7743/*
7744 * Create a new argument list and use it for the current window.
7745 */
7746 void
7747alist_new()
7748{
7749 curwin->w_alist = (alist_T *)alloc((unsigned)sizeof(alist_T));
7750 if (curwin->w_alist == NULL)
7751 {
7752 curwin->w_alist = &global_alist;
7753 ++global_alist.al_refcount;
7754 }
7755 else
7756 {
7757 curwin->w_alist->al_refcount = 1;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02007758 curwin->w_alist->id = ++max_alist_id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007759 alist_init(curwin->w_alist);
7760 }
7761}
7762# endif
7763#endif
7764
7765#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE) || defined(PROTO)
7766/*
7767 * Expand the file names in the global argument list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00007768 * If "fnum_list" is not NULL, use "fnum_list[fnum_len]" as a list of buffer
7769 * numbers to be re-used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007770 */
7771 void
Bram Moolenaar86b68352004-12-27 21:59:20 +00007772alist_expand(fnum_list, fnum_len)
7773 int *fnum_list;
7774 int fnum_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007775{
7776 char_u **old_arg_files;
Bram Moolenaar86b68352004-12-27 21:59:20 +00007777 int old_arg_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007778 char_u **new_arg_files;
7779 int new_arg_file_count;
7780 char_u *save_p_su = p_su;
7781 int i;
7782
7783 /* Don't use 'suffixes' here. This should work like the shell did the
7784 * expansion. Also, the vimrc file isn't read yet, thus the user
7785 * can't set the options. */
7786 p_su = empty_option;
7787 old_arg_files = (char_u **)alloc((unsigned)(sizeof(char_u *) * GARGCOUNT));
7788 if (old_arg_files != NULL)
7789 {
7790 for (i = 0; i < GARGCOUNT; ++i)
Bram Moolenaar86b68352004-12-27 21:59:20 +00007791 old_arg_files[i] = vim_strsave(GARGLIST[i].ae_fname);
7792 old_arg_count = GARGCOUNT;
7793 if (expand_wildcards(old_arg_count, old_arg_files,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007794 &new_arg_file_count, &new_arg_files,
Bram Moolenaarb5609832011-07-20 15:04:58 +02007795 EW_FILE|EW_NOTFOUND|EW_ADDSLASH|EW_NOERROR) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007796 && new_arg_file_count > 0)
7797 {
Bram Moolenaar86b68352004-12-27 21:59:20 +00007798 alist_set(&global_alist, new_arg_file_count, new_arg_files,
7799 TRUE, fnum_list, fnum_len);
7800 FreeWild(old_arg_count, old_arg_files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007801 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007802 }
7803 p_su = save_p_su;
7804}
7805#endif
7806
7807/*
7808 * Set the argument list for the current window.
7809 * Takes over the allocated files[] and the allocated fnames in it.
7810 */
7811 void
Bram Moolenaar86b68352004-12-27 21:59:20 +00007812alist_set(al, count, files, use_curbuf, fnum_list, fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007813 alist_T *al;
7814 int count;
7815 char_u **files;
7816 int use_curbuf;
Bram Moolenaar86b68352004-12-27 21:59:20 +00007817 int *fnum_list;
7818 int fnum_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007819{
7820 int i;
7821
7822 alist_clear(al);
7823 if (ga_grow(&al->al_ga, count) == OK)
7824 {
7825 for (i = 0; i < count; ++i)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007826 {
7827 if (got_int)
7828 {
7829 /* When adding many buffers this can take a long time. Allow
7830 * interrupting here. */
7831 while (i < count)
7832 vim_free(files[i++]);
7833 break;
7834 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00007835
7836 /* May set buffer name of a buffer previously used for the
7837 * argument list, so that it's re-used by alist_add. */
7838 if (fnum_list != NULL && i < fnum_len)
7839 buf_set_name(fnum_list[i], files[i]);
7840
Bram Moolenaar071d4272004-06-13 20:20:40 +00007841 alist_add(al, files[i], use_curbuf ? 2 : 1);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007842 ui_breakcheck();
7843 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007844 vim_free(files);
7845 }
7846 else
7847 FreeWild(count, files);
7848#ifdef FEAT_WINDOWS
7849 if (al == &global_alist)
7850#endif
7851 arg_had_last = FALSE;
7852}
7853
7854/*
7855 * Add file "fname" to argument list "al".
7856 * "fname" must have been allocated and "al" must have been checked for room.
7857 */
7858 void
7859alist_add(al, fname, set_fnum)
7860 alist_T *al;
7861 char_u *fname;
7862 int set_fnum; /* 1: set buffer number; 2: re-use curbuf */
7863{
7864 if (fname == NULL) /* don't add NULL file names */
7865 return;
7866#ifdef BACKSLASH_IN_FILENAME
7867 slash_adjust(fname);
7868#endif
7869 AARGLIST(al)[al->al_ga.ga_len].ae_fname = fname;
7870 if (set_fnum > 0)
7871 AARGLIST(al)[al->al_ga.ga_len].ae_fnum =
7872 buflist_add(fname, BLN_LISTED | (set_fnum == 2 ? BLN_CURBUF : 0));
7873 ++al->al_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007874}
7875
7876#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
7877/*
7878 * Adjust slashes in file names. Called after 'shellslash' was set.
7879 */
7880 void
7881alist_slash_adjust()
7882{
7883 int i;
7884# ifdef FEAT_WINDOWS
7885 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007886 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007887# endif
7888
7889 for (i = 0; i < GARGCOUNT; ++i)
7890 if (GARGLIST[i].ae_fname != NULL)
7891 slash_adjust(GARGLIST[i].ae_fname);
7892# ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00007893 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007894 if (wp->w_alist != &global_alist)
7895 for (i = 0; i < WARGCOUNT(wp); ++i)
7896 if (WARGLIST(wp)[i].ae_fname != NULL)
7897 slash_adjust(WARGLIST(wp)[i].ae_fname);
7898# endif
7899}
7900#endif
7901
7902/*
7903 * ":preserve".
7904 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007905 static void
7906ex_preserve(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007907 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007908{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007909 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007910 ml_preserve(curbuf, TRUE);
7911}
7912
7913/*
7914 * ":recover".
7915 */
7916 static void
7917ex_recover(eap)
7918 exarg_T *eap;
7919{
7920 /* Set recoverymode right away to avoid the ATTENTION prompt. */
7921 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01007922 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
7923 | CCGD_MULTWIN
7924 | (eap->forceit ? CCGD_FORCEIT : 0)
7925 | CCGD_EXCMD)
7926
Bram Moolenaar071d4272004-06-13 20:20:40 +00007927 && (*eap->arg == NUL
7928 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
7929 ml_recover();
7930 recoverymode = FALSE;
7931}
7932
7933/*
7934 * Command modifier used in a wrong way.
7935 */
7936 static void
7937ex_wrongmodifier(eap)
7938 exarg_T *eap;
7939{
7940 eap->errmsg = e_invcmd;
7941}
7942
7943#ifdef FEAT_WINDOWS
7944/*
7945 * :sview [+command] file split window with new file, read-only
7946 * :split [[+command] file] split window with current or new file
7947 * :vsplit [[+command] file] split window vertically with current or new file
7948 * :new [[+command] file] split window with no or new file
7949 * :vnew [[+command] file] split vertically window with no or new file
7950 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007951 *
7952 * :tabedit open new Tab page with empty window
7953 * :tabedit [+command] file open new Tab page and edit "file"
7954 * :tabnew [[+command] file] just like :tabedit
7955 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007956 */
7957 void
7958ex_splitview(eap)
7959 exarg_T *eap;
7960{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007961 win_T *old_curwin = curwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007962# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007963 char_u *fname = NULL;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007964# endif
7965# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007966 int browse_flag = cmdmod.browse;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007967# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007968
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007969# ifndef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007970 if (eap->cmdidx == CMD_vsplit || eap->cmdidx == CMD_vnew)
7971 {
7972 ex_ni(eap);
7973 return;
7974 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007975# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007976
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007977# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007978 need_mouse_correct = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007979# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007980
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007981# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00007982 /* A ":split" in the quickfix window works like ":new". Don't want two
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007983 * quickfix windows. But it's OK when doing ":tab split". */
7984 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007985 {
7986 if (eap->cmdidx == CMD_split)
7987 eap->cmdidx = CMD_new;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007988# ifdef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007989 if (eap->cmdidx == CMD_vsplit)
7990 eap->cmdidx = CMD_vnew;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007991# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007992 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007993# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007994
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007995# ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007996 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007997 {
7998 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
7999 FNAME_MESS, TRUE, curbuf->b_ffname);
8000 if (fname == NULL)
8001 goto theend;
8002 eap->arg = fname;
8003 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008004# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008005 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008006# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008007# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008008# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008009 if (cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008010# ifdef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00008011 && eap->cmdidx != CMD_vnew
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008012# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008013 && eap->cmdidx != CMD_new)
8014 {
Bram Moolenaar15bfa092008-07-24 16:45:38 +00008015# ifdef FEAT_AUTOCMD
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008016 if (
Bram Moolenaar15bfa092008-07-24 16:45:38 +00008017# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008018 !gui.in_use &&
Bram Moolenaar15bfa092008-07-24 16:45:38 +00008019# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008020 au_has_group((char_u *)"FileExplorer"))
8021 {
8022 /* No browsing supported but we do have the file explorer:
8023 * Edit the directory. */
8024 if (*eap->arg == NUL || !mch_isdir(eap->arg))
8025 eap->arg = (char_u *)".";
8026 }
8027 else
Bram Moolenaar15bfa092008-07-24 16:45:38 +00008028# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008029 {
8030 fname = do_browse(0, (char_u *)_("Edit File in new window"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00008031 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008032 if (fname == NULL)
8033 goto theend;
8034 eap->arg = fname;
8035 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008036 }
8037 cmdmod.browse = FALSE; /* Don't browse again in do_ecmd(). */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008038# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008039
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008040 /*
8041 * Either open new tab page or split the window.
8042 */
8043 if (eap->cmdidx == CMD_tabedit
8044 || eap->cmdidx == CMD_tabfind
8045 || eap->cmdidx == CMD_tabnew)
8046 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00008047 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
8048 : eap->addr_count == 0 ? 0
8049 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008050 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00008051 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008052
8053 /* set the alternate buffer for the window we came from */
8054 if (curwin != old_curwin
8055 && win_valid(old_curwin)
8056 && old_curwin->w_buffer != curbuf
8057 && !cmdmod.keepalt)
8058 old_curwin->w_alt_fnum = curbuf->b_fnum;
8059 }
8060 }
8061 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008062 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
8063 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008064# ifdef FEAT_SCROLLBIND
Bram Moolenaar071d4272004-06-13 20:20:40 +00008065 /* Reset 'scrollbind' when editing another file, but keep it when
8066 * doing ":split" without arguments. */
8067 if (*eap->arg != NUL
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008068# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008069 || cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008070# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008071 )
Bram Moolenaar3368ea22010-09-21 16:56:35 +02008072 {
8073 RESET_BINDING(curwin);
8074 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008075 else
8076 do_check_scrollbind(FALSE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008077# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008078 do_exedit(eap, old_curwin);
8079 }
8080
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008081# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008082 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008083# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008084
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008085# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008086theend:
8087 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008088# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008089}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008090
8091/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008092 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008093 */
8094 void
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008095tabpage_new()
8096{
8097 exarg_T ea;
8098
8099 vim_memset(&ea, 0, sizeof(ea));
8100 ea.cmdidx = CMD_tabnew;
8101 ea.cmd = (char_u *)"tabn";
8102 ea.arg = (char_u *)"";
8103 ex_splitview(&ea);
8104}
8105
8106/*
8107 * :tabnext command
8108 */
8109 static void
8110ex_tabnext(eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008111 exarg_T *eap;
8112{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008113 switch (eap->cmdidx)
8114 {
8115 case CMD_tabfirst:
8116 case CMD_tabrewind:
8117 goto_tabpage(1);
8118 break;
8119 case CMD_tablast:
8120 goto_tabpage(9999);
8121 break;
8122 case CMD_tabprevious:
8123 case CMD_tabNext:
8124 goto_tabpage(eap->addr_count == 0 ? -1 : -(int)eap->line2);
8125 break;
8126 default: /* CMD_tabnext */
8127 goto_tabpage(eap->addr_count == 0 ? 0 : (int)eap->line2);
8128 break;
8129 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008130}
8131
8132/*
8133 * :tabmove command
8134 */
8135 static void
8136ex_tabmove(eap)
8137 exarg_T *eap;
8138{
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02008139 int tab_number = 9999;
8140
8141 if (eap->arg && *eap->arg != NUL)
8142 {
8143 char_u *p = eap->arg;
8144 int relative = 0; /* argument +N/-N means: move N places to the
8145 * right/left relative to the current position. */
8146
8147 if (*eap->arg == '-')
8148 {
8149 relative = -1;
8150 p = eap->arg + 1;
8151 }
8152 else if (*eap->arg == '+')
8153 {
8154 relative = 1;
8155 p = eap->arg + 1;
8156 }
8157 else
8158 p = eap->arg;
8159
8160 if (p == skipdigits(p))
8161 {
8162 /* No numbers as argument. */
8163 eap->errmsg = e_invarg;
8164 return;
8165 }
8166
8167 tab_number = getdigits(&p);
8168 if (relative != 0)
8169 tab_number = tab_number * relative + tabpage_index(curtab) - 1;;
8170 }
8171 else if (eap->addr_count != 0)
8172 tab_number = eap->line2;
8173
8174 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008175}
8176
8177/*
8178 * :tabs command: List tabs and their contents.
8179 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008180 static void
8181ex_tabs(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008182 exarg_T *eap UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008183{
8184 tabpage_T *tp;
8185 win_T *wp;
8186 int tabcount = 1;
8187
8188 msg_start();
8189 msg_scroll = TRUE;
8190 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
8191 {
8192 msg_putchar('\n');
8193 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
8194 msg_outtrans_attr(IObuff, hl_attr(HLF_T));
8195 out_flush(); /* output one line at a time */
8196 ui_breakcheck();
8197
Bram Moolenaar030f0df2006-02-21 22:02:53 +00008198 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008199 wp = firstwin;
8200 else
8201 wp = tp->tp_firstwin;
8202 for ( ; wp != NULL && !got_int; wp = wp->w_next)
8203 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008204 msg_putchar('\n');
8205 msg_putchar(wp == curwin ? '>' : ' ');
8206 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00008207 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
8208 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008209 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02008210 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008211 else
8212 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
8213 IObuff, IOSIZE, TRUE);
8214 msg_outtrans(IObuff);
8215 out_flush(); /* output one line at a time */
8216 ui_breakcheck();
8217 }
8218 }
8219}
8220
8221#endif /* FEAT_WINDOWS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008222
8223/*
8224 * ":mode": Set screen mode.
8225 * If no argument given, just get the screen size and redraw.
8226 */
8227 static void
8228ex_mode(eap)
8229 exarg_T *eap;
8230{
8231 if (*eap->arg == NUL)
8232 shell_resized();
8233 else
8234 mch_screenmode(eap->arg);
8235}
8236
8237#ifdef FEAT_WINDOWS
8238/*
8239 * ":resize".
8240 * set, increment or decrement current window height
8241 */
8242 static void
8243ex_resize(eap)
8244 exarg_T *eap;
8245{
8246 int n;
8247 win_T *wp = curwin;
8248
8249 if (eap->addr_count > 0)
8250 {
8251 n = eap->line2;
8252 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
8253 ;
8254 }
8255
8256#ifdef FEAT_GUI
8257 need_mouse_correct = TRUE;
8258#endif
8259 n = atol((char *)eap->arg);
8260#ifdef FEAT_VERTSPLIT
8261 if (cmdmod.split & WSP_VERT)
8262 {
8263 if (*eap->arg == '-' || *eap->arg == '+')
8264 n += W_WIDTH(curwin);
8265 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
8266 n = 9999;
8267 win_setwidth_win((int)n, wp);
8268 }
8269 else
8270#endif
8271 {
8272 if (*eap->arg == '-' || *eap->arg == '+')
8273 n += curwin->w_height;
8274 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
8275 n = 9999;
8276 win_setheight_win((int)n, wp);
8277 }
8278}
8279#endif
8280
8281/*
8282 * ":find [+command] <file>" command.
8283 */
8284 static void
8285ex_find(eap)
8286 exarg_T *eap;
8287{
8288#ifdef FEAT_SEARCHPATH
8289 char_u *fname;
8290 int count;
8291
8292 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
8293 TRUE, curbuf->b_ffname);
8294 if (eap->addr_count > 0)
8295 {
8296 /* Repeat finding the file "count" times. This matters when it
8297 * appears several times in the path. */
8298 count = eap->line2;
8299 while (fname != NULL && --count > 0)
8300 {
8301 vim_free(fname);
8302 fname = find_file_in_path(NULL, 0, FNAME_MESS,
8303 FALSE, curbuf->b_ffname);
8304 }
8305 }
8306
8307 if (fname != NULL)
8308 {
8309 eap->arg = fname;
8310#endif
8311 do_exedit(eap, NULL);
8312#ifdef FEAT_SEARCHPATH
8313 vim_free(fname);
8314 }
8315#endif
8316}
8317
8318/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00008319 * ":open" simulation: for now just work like ":visual".
8320 */
8321 static void
8322ex_open(eap)
8323 exarg_T *eap;
8324{
8325 regmatch_T regmatch;
8326 char_u *p;
8327
8328 curwin->w_cursor.lnum = eap->line2;
8329 beginline(BL_SOL | BL_FIX);
8330 if (*eap->arg == '/')
8331 {
8332 /* ":open /pattern/": put cursor in column found with pattern */
8333 ++eap->arg;
8334 p = skip_regexp(eap->arg, '/', p_magic, NULL);
8335 *p = NUL;
8336 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
8337 if (regmatch.regprog != NULL)
8338 {
8339 regmatch.rm_ic = p_ic;
8340 p = ml_get_curline();
8341 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008342 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008343 else
8344 EMSG(_(e_nomatch));
Bram Moolenaar473de612013-06-08 18:19:48 +02008345 vim_regfree(regmatch.regprog);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008346 }
8347 /* Move to the NUL, ignore any other arguments. */
8348 eap->arg += STRLEN(eap->arg);
8349 }
8350 check_cursor();
8351
8352 eap->cmdidx = CMD_visual;
8353 do_exedit(eap, NULL);
8354}
8355
8356/*
8357 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008358 */
8359 static void
8360ex_edit(eap)
8361 exarg_T *eap;
8362{
8363 do_exedit(eap, NULL);
8364}
8365
8366/*
8367 * ":edit <file>" command and alikes.
8368 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008369 void
8370do_exedit(eap, old_curwin)
8371 exarg_T *eap;
8372 win_T *old_curwin; /* curwin before doing a split or NULL */
8373{
8374 int n;
8375#ifdef FEAT_WINDOWS
8376 int need_hide;
8377#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00008378 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008379
8380 /*
8381 * ":vi" command ends Ex mode.
8382 */
8383 if (exmode_active && (eap->cmdidx == CMD_visual
8384 || eap->cmdidx == CMD_view))
8385 {
8386 exmode_active = FALSE;
8387 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008388 {
8389 /* Special case: ":global/pat/visual\NLvi-commands" */
8390 if (global_busy)
8391 {
8392 int rd = RedrawingDisabled;
8393 int nwr = no_wait_return;
8394 int ms = msg_scroll;
8395#ifdef FEAT_GUI
8396 int he = hold_gui_events;
8397#endif
8398
8399 if (eap->nextcmd != NULL)
8400 {
8401 stuffReadbuff(eap->nextcmd);
8402 eap->nextcmd = NULL;
8403 }
8404
8405 if (exmode_was != EXMODE_VIM)
8406 settmode(TMODE_RAW);
8407 RedrawingDisabled = 0;
8408 no_wait_return = 0;
8409 need_wait_return = FALSE;
8410 msg_scroll = 0;
8411#ifdef FEAT_GUI
8412 hold_gui_events = 0;
8413#endif
8414 must_redraw = CLEAR;
8415
8416 main_loop(FALSE, TRUE);
8417
8418 RedrawingDisabled = rd;
8419 no_wait_return = nwr;
8420 msg_scroll = ms;
8421#ifdef FEAT_GUI
8422 hold_gui_events = he;
8423#endif
8424 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008425 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008426 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008427 }
8428
8429 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008430 || eap->cmdidx == CMD_tabnew
8431 || eap->cmdidx == CMD_tabedit
Bram Moolenaar071d4272004-06-13 20:20:40 +00008432#ifdef FEAT_VERTSPLIT
8433 || eap->cmdidx == CMD_vnew
8434#endif
8435 ) && *eap->arg == NUL)
8436 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008437 /* ":new" or ":tabnew" without argument: edit an new empty buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008438 setpcmark();
8439 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00008440 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
8441 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008442 }
8443 else if ((eap->cmdidx != CMD_split
8444#ifdef FEAT_VERTSPLIT
8445 && eap->cmdidx != CMD_vsplit
8446#endif
8447 )
8448 || *eap->arg != NUL
8449#ifdef FEAT_BROWSE
8450 || cmdmod.browse
8451#endif
8452 )
8453 {
Bram Moolenaar5555acc2006-04-07 21:33:12 +00008454#ifdef FEAT_AUTOCMD
8455 /* Can't edit another file when "curbuf_lock" is set. Only ":edit"
8456 * can bring us here, others are stopped earlier. */
8457 if (*eap->arg != NUL && curbuf_locked())
8458 return;
8459#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008460 n = readonlymode;
8461 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
8462 readonlymode = TRUE;
8463 else if (eap->cmdidx == CMD_enew)
8464 readonlymode = FALSE; /* 'readonly' doesn't make sense in an
8465 empty buffer */
8466 setpcmark();
8467 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
8468 NULL, eap,
8469 /* ":edit" goes to first line if Vi compatible */
8470 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
8471 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
8472 ? ECMD_ONE : eap->do_ecmd_lnum,
8473 (P_HID(curbuf) ? ECMD_HIDE : 0)
8474 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar7b449342014-03-25 13:03:48 +01008475 /* after a split we can use an existing buffer */
8476 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008477#ifdef FEAT_LISTCMDS
8478 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
8479#endif
Bram Moolenaar701f7af2008-11-15 13:12:07 +00008480 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008481 {
8482 /* Editing the file failed. If the window was split, close it. */
8483#ifdef FEAT_WINDOWS
8484 if (old_curwin != NULL)
8485 {
8486 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
8487 if (!need_hide || P_HID(curbuf))
8488 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008489# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
8490 cleanup_T cs;
8491
8492 /* Reset the error/interrupt/exception state here so that
8493 * aborting() returns FALSE when closing a window. */
8494 enter_cleanup(&cs);
8495# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008496# ifdef FEAT_GUI
8497 need_mouse_correct = TRUE;
8498# endif
8499 win_close(curwin, !need_hide && !P_HID(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008500
8501# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
8502 /* Restore the error/interrupt/exception state if not
8503 * discarded by a new aborting error, interrupt, or
8504 * uncaught exception. */
8505 leave_cleanup(&cs);
8506# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008507 }
8508 }
8509#endif
8510 }
8511 else if (readonlymode && curbuf->b_nwindows == 1)
8512 {
8513 /* When editing an already visited buffer, 'readonly' won't be set
8514 * but the previous value is kept. With ":view" and ":sview" we
8515 * want the file to be readonly, except when another window is
8516 * editing the same buffer. */
8517 curbuf->b_p_ro = TRUE;
8518 }
8519 readonlymode = n;
8520 }
8521 else
8522 {
8523 if (eap->do_ecmd_cmd != NULL)
8524 do_cmdline_cmd(eap->do_ecmd_cmd);
8525#ifdef FEAT_TITLE
8526 n = curwin->w_arg_idx_invalid;
8527#endif
8528 check_arg_idx(curwin);
8529#ifdef FEAT_TITLE
8530 if (n != curwin->w_arg_idx_invalid)
8531 maketitle();
8532#endif
8533 }
8534
8535#ifdef FEAT_WINDOWS
8536 /*
8537 * if ":split file" worked, set alternate file name in old window to new
8538 * file
8539 */
8540 if (old_curwin != NULL
8541 && *eap->arg != NUL
8542 && curwin != old_curwin
8543 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00008544 && old_curwin->w_buffer != curbuf
8545 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008546 old_curwin->w_alt_fnum = curbuf->b_fnum;
8547#endif
8548
8549 ex_no_reprint = TRUE;
8550}
8551
8552#ifndef FEAT_GUI
8553/*
8554 * ":gui" and ":gvim" when there is no GUI.
8555 */
8556 static void
8557ex_nogui(eap)
8558 exarg_T *eap;
8559{
8560 eap->errmsg = e_nogvim;
8561}
8562#endif
8563
8564#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
8565 static void
8566ex_tearoff(eap)
8567 exarg_T *eap;
8568{
8569 gui_make_tearoff(eap->arg);
8570}
8571#endif
8572
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008573#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008574 static void
8575ex_popup(eap)
8576 exarg_T *eap;
8577{
Bram Moolenaar97409f12005-07-08 22:17:29 +00008578 gui_make_popup(eap->arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008579}
8580#endif
8581
Bram Moolenaar071d4272004-06-13 20:20:40 +00008582 static void
8583ex_swapname(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008584 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008585{
8586 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
8587 MSG(_("No swap file"));
8588 else
8589 msg(curbuf->b_ml.ml_mfp->mf_fname);
8590}
8591
8592/*
8593 * ":syncbind" forces all 'scrollbind' windows to have the same relative
8594 * offset.
8595 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
8596 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008597 static void
8598ex_syncbind(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008599 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008600{
8601#ifdef FEAT_SCROLLBIND
8602 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008603 win_T *save_curwin = curwin;
8604 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008605 long topline;
8606 long y;
8607 linenr_T old_linenr = curwin->w_cursor.lnum;
8608
8609 setpcmark();
8610
8611 /*
8612 * determine max topline
8613 */
8614 if (curwin->w_p_scb)
8615 {
8616 topline = curwin->w_topline;
8617 for (wp = firstwin; wp; wp = wp->w_next)
8618 {
8619 if (wp->w_p_scb && wp->w_buffer)
8620 {
8621 y = wp->w_buffer->b_ml.ml_line_count - p_so;
8622 if (topline > y)
8623 topline = y;
8624 }
8625 }
8626 if (topline < 1)
8627 topline = 1;
8628 }
8629 else
8630 {
8631 topline = 1;
8632 }
8633
8634
8635 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008636 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008637 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008638 for (curwin = firstwin; curwin; curwin = curwin->w_next)
8639 {
8640 if (curwin->w_p_scb)
8641 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008642 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008643 y = topline - curwin->w_topline;
8644 if (y > 0)
8645 scrollup(y, TRUE);
8646 else
8647 scrolldown(-y, TRUE);
8648 curwin->w_scbind_pos = topline;
8649 redraw_later(VALID);
8650 cursor_correct();
8651#ifdef FEAT_WINDOWS
8652 curwin->w_redr_status = TRUE;
8653#endif
8654 }
8655 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008656 curwin = save_curwin;
8657 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008658 if (curwin->w_p_scb)
8659 {
8660 did_syncbind = TRUE;
8661 checkpcmark();
8662 if (old_linenr != curwin->w_cursor.lnum)
8663 {
8664 char_u ctrl_o[2];
8665
8666 ctrl_o[0] = Ctrl_O;
8667 ctrl_o[1] = 0;
8668 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
8669 }
8670 }
8671#endif
8672}
8673
8674
8675 static void
8676ex_read(eap)
8677 exarg_T *eap;
8678{
Bram Moolenaardf177f62005-02-22 08:39:57 +00008679 int i;
8680 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
8681 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008682
8683 if (eap->usefilter) /* :r!cmd */
8684 do_bang(1, eap, FALSE, FALSE, TRUE);
8685 else
8686 {
8687 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
8688 return;
8689
8690#ifdef FEAT_BROWSE
8691 if (cmdmod.browse)
8692 {
8693 char_u *browseFile;
8694
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008695 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008696 NULL, NULL, NULL, curbuf);
8697 if (browseFile != NULL)
8698 {
8699 i = readfile(browseFile, NULL,
8700 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8701 vim_free(browseFile);
8702 }
8703 else
8704 i = OK;
8705 }
8706 else
8707#endif
8708 if (*eap->arg == NUL)
8709 {
8710 if (check_fname() == FAIL) /* check for no file name */
8711 return;
8712 i = readfile(curbuf->b_ffname, curbuf->b_fname,
8713 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8714 }
8715 else
8716 {
8717 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
8718 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
8719 i = readfile(eap->arg, NULL,
8720 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8721
8722 }
8723 if (i == FAIL)
8724 {
8725#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
8726 if (!aborting())
8727#endif
8728 EMSG2(_(e_notopen), eap->arg);
8729 }
8730 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008731 {
8732 if (empty && exmode_active)
8733 {
8734 /* Delete the empty line that remains. Historically ex does
8735 * this but vi doesn't. */
8736 if (eap->line2 == 0)
8737 lnum = curbuf->b_ml.ml_line_count;
8738 else
8739 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008740 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008741 {
8742 ml_delete(lnum, FALSE);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008743 if (curwin->w_cursor.lnum > 1
8744 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008745 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00008746 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008747 }
8748 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008749 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008750 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008751 }
8752}
8753
Bram Moolenaarea408852005-06-25 22:49:46 +00008754static char_u *prev_dir = NULL;
8755
8756#if defined(EXITFREE) || defined(PROTO)
8757 void
8758free_cd_dir()
8759{
8760 vim_free(prev_dir);
Bram Moolenaara0174af2008-01-02 20:08:25 +00008761 prev_dir = NULL;
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00008762
8763 vim_free(globaldir);
8764 globaldir = NULL;
Bram Moolenaarea408852005-06-25 22:49:46 +00008765}
8766#endif
8767
Bram Moolenaarf4258302013-06-02 18:20:17 +02008768/*
8769 * Deal with the side effects of changing the current directory.
8770 * When "local" is TRUE then this was after an ":lcd" command.
8771 */
8772 void
8773post_chdir(local)
8774 int local;
8775{
8776 vim_free(curwin->w_localdir);
Bram Moolenaarbd2dc342014-01-10 15:53:13 +01008777 curwin->w_localdir = NULL;
Bram Moolenaarf4258302013-06-02 18:20:17 +02008778 if (local)
8779 {
8780 /* If still in global directory, need to remember current
8781 * directory as global directory. */
8782 if (globaldir == NULL && prev_dir != NULL)
8783 globaldir = vim_strsave(prev_dir);
8784 /* Remember this local directory for the window. */
8785 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8786 curwin->w_localdir = vim_strsave(NameBuff);
8787 }
8788 else
8789 {
8790 /* We are now in the global directory, no need to remember its
8791 * name. */
8792 vim_free(globaldir);
8793 globaldir = NULL;
Bram Moolenaarf4258302013-06-02 18:20:17 +02008794 }
8795
8796 shorten_fnames(TRUE);
8797}
8798
Bram Moolenaarea408852005-06-25 22:49:46 +00008799
Bram Moolenaar071d4272004-06-13 20:20:40 +00008800/*
8801 * ":cd", ":lcd", ":chdir" and ":lchdir".
8802 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00008803 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00008804ex_cd(eap)
8805 exarg_T *eap;
8806{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008807 char_u *new_dir;
8808 char_u *tofree;
8809
8810 new_dir = eap->arg;
8811#if !defined(UNIX) && !defined(VMS)
8812 /* for non-UNIX ":cd" means: print current directory */
8813 if (*new_dir == NUL)
8814 ex_pwd(NULL);
8815 else
8816#endif
8817 {
Bram Moolenaar8e8fe9b2009-03-11 14:37:32 +00008818#ifdef FEAT_AUTOCMD
8819 if (allbuf_locked())
8820 return;
8821#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00008822 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged()
8823 && !eap->forceit)
8824 {
Bram Moolenaar81870892007-11-11 18:17:28 +00008825 EMSG(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00008826 return;
8827 }
8828
Bram Moolenaar071d4272004-06-13 20:20:40 +00008829 /* ":cd -": Change to previous directory */
8830 if (STRCMP(new_dir, "-") == 0)
8831 {
8832 if (prev_dir == NULL)
8833 {
8834 EMSG(_("E186: No previous directory"));
8835 return;
8836 }
8837 new_dir = prev_dir;
8838 }
8839
8840 /* Save current directory for next ":cd -" */
8841 tofree = prev_dir;
8842 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8843 prev_dir = vim_strsave(NameBuff);
8844 else
8845 prev_dir = NULL;
8846
8847#if defined(UNIX) || defined(VMS)
8848 /* for UNIX ":cd" means: go to home directory */
8849 if (*new_dir == NUL)
8850 {
8851 /* use NameBuff for home directory name */
8852# ifdef VMS
8853 char_u *p;
8854
8855 p = mch_getenv((char_u *)"SYS$LOGIN");
8856 if (p == NULL || *p == NUL) /* empty is the same as not set */
8857 NameBuff[0] = NUL;
8858 else
Bram Moolenaarb6356332005-07-18 21:40:44 +00008859 vim_strncpy(NameBuff, p, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008860# else
8861 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
8862# endif
8863 new_dir = NameBuff;
8864 }
8865#endif
8866 if (new_dir == NULL || vim_chdir(new_dir))
8867 EMSG(_(e_failed));
8868 else
8869 {
Bram Moolenaarf4258302013-06-02 18:20:17 +02008870 post_chdir(eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008871
8872 /* Echo the new current directory if the command was typed. */
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00008873 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008874 ex_pwd(eap);
8875 }
8876 vim_free(tofree);
8877 }
8878}
8879
8880/*
8881 * ":pwd".
8882 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008883 static void
8884ex_pwd(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008885 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008886{
8887 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8888 {
8889#ifdef BACKSLASH_IN_FILENAME
8890 slash_adjust(NameBuff);
8891#endif
8892 msg(NameBuff);
8893 }
8894 else
8895 EMSG(_("E187: Unknown"));
8896}
8897
8898/*
8899 * ":=".
8900 */
8901 static void
8902ex_equal(eap)
8903 exarg_T *eap;
8904{
8905 smsg((char_u *)"%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008906 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008907}
8908
8909 static void
8910ex_sleep(eap)
8911 exarg_T *eap;
8912{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008913 int n;
8914 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008915
8916 if (cursor_valid())
8917 {
8918 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
8919 if (n >= 0)
Bram Moolenaar10395d82014-02-05 22:46:52 +01008920 windgoto((int)n, W_WINCOL(curwin) + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008921 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008922
8923 len = eap->line2;
8924 switch (*eap->arg)
8925 {
8926 case 'm': break;
8927 case NUL: len *= 1000L; break;
8928 default: EMSG2(_(e_invarg2), eap->arg); return;
8929 }
8930 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008931}
8932
8933/*
8934 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
8935 */
8936 void
8937do_sleep(msec)
8938 long msec;
8939{
8940 long done;
8941
8942 cursor_on();
8943 out_flush();
8944 for (done = 0; !got_int && done < msec; done += 1000L)
8945 {
8946 ui_delay(msec - done > 1000L ? 1000L : msec - done, TRUE);
8947 ui_breakcheck();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02008948#ifdef FEAT_NETBEANS_INTG
8949 /* Process the netbeans messages that may have been received in the
8950 * call to ui_breakcheck() when the GUI is in use. This may occur when
8951 * running a test case. */
8952 netbeans_parse_messages();
8953#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008954 }
8955}
8956
8957 static void
8958do_exmap(eap, isabbrev)
8959 exarg_T *eap;
8960 int isabbrev;
8961{
8962 int mode;
8963 char_u *cmdp;
8964
8965 cmdp = eap->cmd;
8966 mode = get_map_mode(&cmdp, eap->forceit || isabbrev);
8967
8968 switch (do_map((*cmdp == 'n') ? 2 : (*cmdp == 'u'),
8969 eap->arg, mode, isabbrev))
8970 {
8971 case 1: EMSG(_(e_invarg));
8972 break;
8973 case 2: EMSG(isabbrev ? _(e_noabbr) : _(e_nomap));
8974 break;
8975 }
8976}
8977
8978/*
8979 * ":winsize" command (obsolete).
8980 */
8981 static void
8982ex_winsize(eap)
8983 exarg_T *eap;
8984{
8985 int w, h;
8986 char_u *arg = eap->arg;
8987 char_u *p;
8988
8989 w = getdigits(&arg);
8990 arg = skipwhite(arg);
8991 p = arg;
8992 h = getdigits(&arg);
8993 if (*p != NUL && *arg == NUL)
8994 set_shellsize(w, h, TRUE);
8995 else
8996 EMSG(_("E465: :winsize requires two number arguments"));
8997}
8998
8999#ifdef FEAT_WINDOWS
9000 static void
9001ex_wincmd(eap)
9002 exarg_T *eap;
9003{
9004 int xchar = NUL;
9005 char_u *p;
9006
9007 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
9008 {
9009 /* CTRL-W g and CTRL-W CTRL-G have an extra command character */
9010 if (eap->arg[1] == NUL)
9011 {
9012 EMSG(_(e_invarg));
9013 return;
9014 }
9015 xchar = eap->arg[1];
9016 p = eap->arg + 2;
9017 }
9018 else
9019 p = eap->arg + 1;
9020
9021 eap->nextcmd = check_nextcmd(p);
9022 p = skipwhite(p);
9023 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
9024 EMSG(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02009025 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009026 {
9027 /* Pass flags on for ":vertical wincmd ]". */
9028 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009029 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009030 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
9031 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009032 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009033 }
9034}
9035#endif
9036
Bram Moolenaar843ee412004-06-30 16:16:41 +00009037#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009038/*
9039 * ":winpos".
9040 */
9041 static void
9042ex_winpos(eap)
9043 exarg_T *eap;
9044{
9045 int x, y;
9046 char_u *arg = eap->arg;
9047 char_u *p;
9048
9049 if (*arg == NUL)
9050 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00009051# if defined(FEAT_GUI) || defined(MSWIN)
9052# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00009053 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00009054# else
9055 if (mch_get_winpos(&x, &y) != FAIL)
9056# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009057 {
9058 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
9059 msg(IObuff);
9060 }
9061 else
9062# endif
9063 EMSG(_("E188: Obtaining window position not implemented for this platform"));
9064 }
9065 else
9066 {
9067 x = getdigits(&arg);
9068 arg = skipwhite(arg);
9069 p = arg;
9070 y = getdigits(&arg);
9071 if (*p == NUL || *arg != NUL)
9072 {
9073 EMSG(_("E466: :winpos requires two number arguments"));
9074 return;
9075 }
9076# ifdef FEAT_GUI
9077 if (gui.in_use)
9078 gui_mch_set_winpos(x, y);
9079 else if (gui.starting)
9080 {
9081 /* Remember the coordinates for when the window is opened. */
9082 gui_win_x = x;
9083 gui_win_y = y;
9084 }
9085# ifdef HAVE_TGETENT
9086 else
9087# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00009088# else
9089# ifdef MSWIN
9090 mch_set_winpos(x, y);
9091# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009092# endif
9093# ifdef HAVE_TGETENT
9094 if (*T_CWP)
9095 term_set_winpos(x, y);
9096# endif
9097 }
9098}
9099#endif
9100
9101/*
9102 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
9103 */
9104 static void
9105ex_operators(eap)
9106 exarg_T *eap;
9107{
9108 oparg_T oa;
9109
9110 clear_oparg(&oa);
9111 oa.regname = eap->regname;
9112 oa.start.lnum = eap->line1;
9113 oa.end.lnum = eap->line2;
9114 oa.line_count = eap->line2 - eap->line1 + 1;
9115 oa.motion_type = MLINE;
9116#ifdef FEAT_VIRTUALEDIT
9117 virtual_op = FALSE;
9118#endif
9119 if (eap->cmdidx != CMD_yank) /* position cursor for undo */
9120 {
9121 setpcmark();
9122 curwin->w_cursor.lnum = eap->line1;
9123 beginline(BL_SOL | BL_FIX);
9124 }
9125
Bram Moolenaard07c6e12013-11-21 14:21:40 +01009126 if (VIsual_active)
9127 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01009128
Bram Moolenaar071d4272004-06-13 20:20:40 +00009129 switch (eap->cmdidx)
9130 {
9131 case CMD_delete:
9132 oa.op_type = OP_DELETE;
9133 op_delete(&oa);
9134 break;
9135
9136 case CMD_yank:
9137 oa.op_type = OP_YANK;
9138 (void)op_yank(&oa, FALSE, TRUE);
9139 break;
9140
9141 default: /* CMD_rshift or CMD_lshift */
Bram Moolenaar6e707362013-06-14 19:15:58 +02009142 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009143#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02009144 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
9145#else
9146 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00009147#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02009148 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009149 oa.op_type = OP_RSHIFT;
9150 else
9151 oa.op_type = OP_LSHIFT;
9152 op_shift(&oa, FALSE, eap->amount);
9153 break;
9154 }
9155#ifdef FEAT_VIRTUALEDIT
9156 virtual_op = MAYBE;
9157#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00009158 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009159}
9160
9161/*
9162 * ":put".
9163 */
9164 static void
9165ex_put(eap)
9166 exarg_T *eap;
9167{
9168 /* ":0put" works like ":1put!". */
9169 if (eap->line2 == 0)
9170 {
9171 eap->line2 = 1;
9172 eap->forceit = TRUE;
9173 }
9174 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009175 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
9176 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009177}
9178
9179/*
9180 * Handle ":copy" and ":move".
9181 */
9182 static void
9183ex_copymove(eap)
9184 exarg_T *eap;
9185{
9186 long n;
9187
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01009188 n = get_address(&eap->arg, eap->addr_type, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009189 if (eap->arg == NULL) /* error detected */
9190 {
9191 eap->nextcmd = NULL;
9192 return;
9193 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00009194 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009195
9196 /*
9197 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
9198 */
9199 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
9200 {
9201 EMSG(_(e_invaddr));
9202 return;
9203 }
9204
9205 if (eap->cmdidx == CMD_move)
9206 {
9207 if (do_move(eap->line1, eap->line2, n) == FAIL)
9208 return;
9209 }
9210 else
9211 ex_copy(eap->line1, eap->line2, n);
9212 u_clearline();
9213 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009214 ex_may_print(eap);
9215}
9216
9217/*
9218 * Print the current line if flags were given to the Ex command.
9219 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02009220 void
Bram Moolenaardf177f62005-02-22 08:39:57 +00009221ex_may_print(eap)
9222 exarg_T *eap;
9223{
9224 if (eap->flags != 0)
9225 {
9226 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
9227 (eap->flags & EXFLAG_LIST));
9228 ex_no_reprint = TRUE;
9229 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009230}
9231
9232/*
9233 * ":smagic" and ":snomagic".
9234 */
9235 static void
9236ex_submagic(eap)
9237 exarg_T *eap;
9238{
9239 int magic_save = p_magic;
9240
9241 p_magic = (eap->cmdidx == CMD_smagic);
9242 do_sub(eap);
9243 p_magic = magic_save;
9244}
9245
9246/*
9247 * ":join".
9248 */
9249 static void
9250ex_join(eap)
9251 exarg_T *eap;
9252{
9253 curwin->w_cursor.lnum = eap->line1;
9254 if (eap->line1 == eap->line2)
9255 {
9256 if (eap->addr_count >= 2) /* :2,2join does nothing */
9257 return;
9258 if (eap->line2 == curbuf->b_ml.ml_line_count)
9259 {
9260 beep_flush();
9261 return;
9262 }
9263 ++eap->line2;
9264 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02009265 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009266 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009267 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009268}
9269
9270/*
9271 * ":[addr]@r" or ":[addr]*r": execute register
9272 */
9273 static void
9274ex_at(eap)
9275 exarg_T *eap;
9276{
9277 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00009278 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009279
9280 curwin->w_cursor.lnum = eap->line2;
9281
9282#ifdef USE_ON_FLY_SCROLL
9283 dont_scroll = TRUE; /* disallow scrolling here */
9284#endif
9285
9286 /* get the register name. No name means to use the previous one */
9287 c = *eap->arg;
9288 if (c == NUL || (c == '*' && *eap->cmd == '*'))
9289 c = '@';
Bram Moolenaard333d1e2006-11-07 17:43:47 +00009290 /* Put the register in the typeahead buffer with the "silent" flag. */
9291 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
9292 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009293 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009294 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00009295 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009296 else
9297 {
9298 int save_efr = exec_from_reg;
9299
9300 exec_from_reg = TRUE;
9301
9302 /*
9303 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00009304 * Continue until the stuff buffer is empty and all added characters
9305 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009306 */
Bram Moolenaar60462872009-11-03 11:40:19 +00009307 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009308 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
9309
9310 exec_from_reg = save_efr;
9311 }
9312}
9313
9314/*
9315 * ":!".
9316 */
9317 static void
9318ex_bang(eap)
9319 exarg_T *eap;
9320{
9321 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
9322}
9323
9324/*
9325 * ":undo".
9326 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009327 static void
9328ex_undo(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009329 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009330{
Bram Moolenaard3667a22006-03-16 21:35:52 +00009331 if (eap->addr_count == 1) /* :undo 123 */
Bram Moolenaar730cde92010-06-27 05:18:54 +02009332 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00009333 else
9334 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009335}
9336
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009337#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02009338 static void
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009339ex_wundo(eap)
9340 exarg_T *eap;
9341{
9342 char_u hash[UNDO_HASH_SIZE];
9343
9344 u_compute_hash(hash);
9345 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
9346}
9347
Bram Moolenaar6df6f472010-07-18 18:04:50 +02009348 static void
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009349ex_rundo(eap)
9350 exarg_T *eap;
9351{
9352 char_u hash[UNDO_HASH_SIZE];
9353
9354 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02009355 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009356}
9357#endif
9358
Bram Moolenaar071d4272004-06-13 20:20:40 +00009359/*
9360 * ":redo".
9361 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009362 static void
9363ex_redo(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009364 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009365{
9366 u_redo(1);
9367}
9368
9369/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009370 * ":earlier" and ":later".
9371 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009372 static void
9373ex_later(eap)
9374 exarg_T *eap;
9375{
9376 long count = 0;
9377 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02009378 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009379 char_u *p = eap->arg;
9380
9381 if (*p == NUL)
9382 count = 1;
9383 else if (isdigit(*p))
9384 {
9385 count = getdigits(&p);
9386 switch (*p)
9387 {
9388 case 's': ++p; sec = TRUE; break;
9389 case 'm': ++p; sec = TRUE; count *= 60; break;
9390 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02009391 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
9392 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009393 }
9394 }
9395
9396 if (*p != NUL)
9397 EMSG2(_(e_invarg2), eap->arg);
9398 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02009399 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
9400 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009401}
9402
9403/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009404 * ":redir": start/stop redirection.
9405 */
9406 static void
9407ex_redir(eap)
9408 exarg_T *eap;
9409{
9410 char *mode;
9411 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00009412 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009413
9414 if (STRICMP(eap->arg, "END") == 0)
9415 close_redir();
9416 else
9417 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009418 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009419 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009420 ++arg;
9421 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009422 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009423 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009424 mode = "a";
9425 }
9426 else
9427 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00009428 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009429
9430 close_redir();
9431
9432 /* Expand environment variables and "~/". */
Bram Moolenaarca472992005-01-21 11:46:23 +00009433 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009434 if (fname == NULL)
9435 return;
9436#ifdef FEAT_BROWSE
9437 if (cmdmod.browse)
9438 {
9439 char_u *browseFile;
9440
Bram Moolenaar7171abe2004-10-11 10:06:20 +00009441 browseFile = do_browse(BROWSE_SAVE,
9442 (char_u *)_("Save Redirection"),
9443 fname, NULL, NULL, BROWSE_FILTER_ALL_FILES, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009444 if (browseFile == NULL)
9445 return; /* operation cancelled */
9446 vim_free(fname);
9447 fname = browseFile;
9448 eap->forceit = TRUE; /* since dialog already asked */
9449 }
9450#endif
9451
9452 redir_fd = open_exfile(fname, eap->forceit, mode);
9453 vim_free(fname);
9454 }
9455#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00009456 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009457 {
9458 /* redirect to a register a-z (resp. A-Z for appending) */
9459 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00009460 ++arg;
9461 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009462# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00009463 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00009464 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00009465# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00009466 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009467 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009468 redir_reg = *arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009469 if (*arg == '>' && arg[1] == '>') /* append */
Bram Moolenaard9d30582005-05-18 22:10:28 +00009470 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009471 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009472 {
Bram Moolenaarc188b882007-10-19 14:20:54 +00009473 /* Can use both "@a" and "@a>". */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00009474 if (*arg == '>')
9475 arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009476 /* Make register empty when not using @A-@Z and the
9477 * command is valid. */
9478 if (*arg == NUL && !isupper(redir_reg))
9479 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009480 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00009481 }
9482 if (*arg != NUL)
9483 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00009484 redir_reg = 0;
Bram Moolenaard9d30582005-05-18 22:10:28 +00009485 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009486 }
9487 }
9488 else if (*arg == '=' && arg[1] == '>')
9489 {
9490 int append;
9491
9492 /* redirect to a variable */
9493 close_redir();
9494 arg += 2;
9495
9496 if (*arg == '>')
9497 {
9498 ++arg;
9499 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009500 }
9501 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00009502 append = FALSE;
9503
9504 if (var_redir_start(skipwhite(arg), append) == OK)
9505 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009506 }
9507#endif
9508
9509 /* TODO: redirect to a buffer */
9510
Bram Moolenaar071d4272004-06-13 20:20:40 +00009511 else
Bram Moolenaarca472992005-01-21 11:46:23 +00009512 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009513 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00009514
9515 /* Make sure redirection is not off. Can happen for cmdline completion
9516 * that indirectly invokes a command to catch its output. */
9517 if (redir_fd != NULL
9518#ifdef FEAT_EVAL
9519 || redir_reg || redir_vname
9520#endif
9521 )
9522 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009523}
9524
9525/*
9526 * ":redraw": force redraw
9527 */
9528 static void
9529ex_redraw(eap)
9530 exarg_T *eap;
9531{
9532 int r = RedrawingDisabled;
9533 int p = p_lz;
9534
9535 RedrawingDisabled = 0;
9536 p_lz = FALSE;
9537 update_topline();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009538 update_screen(eap->forceit ? CLEAR : VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009539#ifdef FEAT_TITLE
9540 if (need_maketitle)
9541 maketitle();
9542#endif
9543 RedrawingDisabled = r;
9544 p_lz = p;
9545
9546 /* Reset msg_didout, so that a message that's there is overwritten. */
9547 msg_didout = FALSE;
9548 msg_col = 0;
9549
Bram Moolenaar56667a52013-07-17 11:54:28 +02009550 /* No need to wait after an intentional redraw. */
9551 need_wait_return = FALSE;
9552
Bram Moolenaar071d4272004-06-13 20:20:40 +00009553 out_flush();
9554}
9555
9556/*
9557 * ":redrawstatus": force redraw of status line(s)
9558 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009559 static void
9560ex_redrawstatus(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009561 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009562{
9563#if defined(FEAT_WINDOWS)
9564 int r = RedrawingDisabled;
9565 int p = p_lz;
9566
9567 RedrawingDisabled = 0;
9568 p_lz = FALSE;
9569 if (eap->forceit)
9570 status_redraw_all();
9571 else
9572 status_redraw_curbuf();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009573 update_screen(VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009574 RedrawingDisabled = r;
9575 p_lz = p;
9576 out_flush();
9577#endif
9578}
9579
9580 static void
9581close_redir()
9582{
9583 if (redir_fd != NULL)
9584 {
9585 fclose(redir_fd);
9586 redir_fd = NULL;
9587 }
9588#ifdef FEAT_EVAL
9589 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009590 if (redir_vname)
9591 {
9592 var_redir_stop();
9593 redir_vname = 0;
9594 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009595#endif
9596}
9597
9598#if defined(FEAT_SESSION) && defined(USE_CRNL)
9599# define MKSESSION_NL
9600static int mksession_nl = FALSE; /* use NL only in put_eol() */
9601#endif
9602
9603/*
9604 * ":mkexrc", ":mkvimrc", ":mkview" and ":mksession".
9605 */
9606 static void
9607ex_mkrc(eap)
9608 exarg_T *eap;
9609{
9610 FILE *fd;
9611 int failed = FALSE;
9612 char_u *fname;
9613#ifdef FEAT_BROWSE
9614 char_u *browseFile = NULL;
9615#endif
9616#ifdef FEAT_SESSION
9617 int view_session = FALSE;
9618 int using_vdir = FALSE; /* using 'viewdir'? */
9619 char_u *viewFile = NULL;
9620 unsigned *flagp;
9621#endif
9622
9623 if (eap->cmdidx == CMD_mksession || eap->cmdidx == CMD_mkview)
9624 {
9625#ifdef FEAT_SESSION
9626 view_session = TRUE;
9627#else
9628 ex_ni(eap);
9629 return;
9630#endif
9631 }
9632
9633#ifdef FEAT_SESSION
Bram Moolenaar8d594672009-07-01 18:18:57 +00009634 /* Use the short file name until ":lcd" is used. We also don't use the
9635 * short file name when 'acd' is set, that is checked later. */
Bram Moolenaareeefcc72007-05-01 21:21:21 +00009636 did_lcd = FALSE;
9637
Bram Moolenaar071d4272004-06-13 20:20:40 +00009638 /* ":mkview" or ":mkview 9": generate file name with 'viewdir' */
9639 if (eap->cmdidx == CMD_mkview
9640 && (*eap->arg == NUL
9641 || (vim_isdigit(*eap->arg) && eap->arg[1] == NUL)))
9642 {
9643 eap->forceit = TRUE;
9644 fname = get_view_file(*eap->arg);
9645 if (fname == NULL)
9646 return;
9647 viewFile = fname;
9648 using_vdir = TRUE;
9649 }
9650 else
9651#endif
9652 if (*eap->arg != NUL)
9653 fname = eap->arg;
9654 else if (eap->cmdidx == CMD_mkvimrc)
9655 fname = (char_u *)VIMRC_FILE;
9656#ifdef FEAT_SESSION
9657 else if (eap->cmdidx == CMD_mksession)
9658 fname = (char_u *)SESSION_FILE;
9659#endif
9660 else
9661 fname = (char_u *)EXRC_FILE;
9662
9663#ifdef FEAT_BROWSE
9664 if (cmdmod.browse)
9665 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00009666 browseFile = do_browse(BROWSE_SAVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009667# ifdef FEAT_SESSION
9668 eap->cmdidx == CMD_mkview ? (char_u *)_("Save View") :
9669 eap->cmdidx == CMD_mksession ? (char_u *)_("Save Session") :
9670# endif
9671 (char_u *)_("Save Setup"),
9672 fname, (char_u *)"vim", NULL, BROWSE_FILTER_MACROS, NULL);
9673 if (browseFile == NULL)
9674 goto theend;
9675 fname = browseFile;
9676 eap->forceit = TRUE; /* since dialog already asked */
9677 }
9678#endif
9679
9680#if defined(FEAT_SESSION) && defined(vim_mkdir)
9681 /* When using 'viewdir' may have to create the directory. */
9682 if (using_vdir && !mch_isdir(p_vdir))
Bram Moolenaardf177f62005-02-22 08:39:57 +00009683 vim_mkdir_emsg(p_vdir, 0755);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009684#endif
9685
9686 fd = open_exfile(fname, eap->forceit, WRITEBIN);
9687 if (fd != NULL)
9688 {
9689#ifdef FEAT_SESSION
9690 if (eap->cmdidx == CMD_mkview)
9691 flagp = &vop_flags;
9692 else
9693 flagp = &ssop_flags;
9694#endif
9695
9696#ifdef MKSESSION_NL
9697 /* "unix" in 'sessionoptions': use NL line separator */
9698 if (view_session && (*flagp & SSOP_UNIX))
9699 mksession_nl = TRUE;
9700#endif
9701
9702 /* Write the version command for :mkvimrc */
9703 if (eap->cmdidx == CMD_mkvimrc)
9704 (void)put_line(fd, "version 6.0");
9705
9706#ifdef FEAT_SESSION
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009707 if (eap->cmdidx == CMD_mksession)
9708 {
9709 if (put_line(fd, "let SessionLoad = 1") == FAIL)
9710 failed = TRUE;
9711 }
9712
Bram Moolenaar071d4272004-06-13 20:20:40 +00009713 if (eap->cmdidx != CMD_mkview)
9714#endif
9715 {
9716 /* Write setting 'compatible' first, because it has side effects.
9717 * For that same reason only do it when needed. */
9718 if (p_cp)
9719 (void)put_line(fd, "if !&cp | set cp | endif");
9720 else
9721 (void)put_line(fd, "if &cp | set nocp | endif");
9722 }
9723
9724#ifdef FEAT_SESSION
9725 if (!view_session
9726 || (eap->cmdidx == CMD_mksession
9727 && (*flagp & SSOP_OPTIONS)))
9728#endif
9729 failed |= (makemap(fd, NULL) == FAIL
9730 || makeset(fd, OPT_GLOBAL, FALSE) == FAIL);
9731
9732#ifdef FEAT_SESSION
9733 if (!failed && view_session)
9734 {
9735 if (put_line(fd, "let s:so_save = &so | let s:siso_save = &siso | set so=0 siso=0") == FAIL)
9736 failed = TRUE;
9737 if (eap->cmdidx == CMD_mksession)
9738 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02009739 char_u *dirnow; /* current directory */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009740
Bram Moolenaard9462e32011-04-11 21:35:11 +02009741 dirnow = alloc(MAXPATHL);
9742 if (dirnow == NULL)
9743 failed = TRUE;
9744 else
9745 {
9746 /*
9747 * Change to session file's dir.
9748 */
9749 if (mch_dirname(dirnow, MAXPATHL) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00009750 || mch_chdir((char *)dirnow) != 0)
Bram Moolenaard9462e32011-04-11 21:35:11 +02009751 *dirnow = NUL;
9752 if (*dirnow != NUL && (ssop_flags & SSOP_SESDIR))
9753 {
9754 if (vim_chdirfile(fname) == OK)
9755 shorten_fnames(TRUE);
9756 }
9757 else if (*dirnow != NUL
9758 && (ssop_flags & SSOP_CURDIR) && globaldir != NULL)
9759 {
9760 if (mch_chdir((char *)globaldir) == 0)
9761 shorten_fnames(TRUE);
9762 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009763
Bram Moolenaard9462e32011-04-11 21:35:11 +02009764 failed |= (makeopens(fd, dirnow) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009765
Bram Moolenaard9462e32011-04-11 21:35:11 +02009766 /* restore original dir */
9767 if (*dirnow != NUL && ((ssop_flags & SSOP_SESDIR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009768 || ((ssop_flags & SSOP_CURDIR) && globaldir != NULL)))
Bram Moolenaard9462e32011-04-11 21:35:11 +02009769 {
9770 if (mch_chdir((char *)dirnow) != 0)
9771 EMSG(_(e_prev_dir));
9772 shorten_fnames(TRUE);
9773 }
9774 vim_free(dirnow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009775 }
9776 }
9777 else
9778 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +00009779 failed |= (put_view(fd, curwin, !using_vdir, flagp,
9780 -1) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009781 }
9782 if (put_line(fd, "let &so = s:so_save | let &siso = s:siso_save")
9783 == FAIL)
9784 failed = TRUE;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009785 if (put_line(fd, "doautoall SessionLoadPost") == FAIL)
9786 failed = TRUE;
Bram Moolenaar4770d092006-01-12 23:22:24 +00009787 if (eap->cmdidx == CMD_mksession)
9788 {
9789 if (put_line(fd, "unlet SessionLoad") == FAIL)
9790 failed = TRUE;
9791 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009792 }
9793#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009794 if (put_line(fd, "\" vim: set ft=vim :") == FAIL)
9795 failed = TRUE;
9796
Bram Moolenaar071d4272004-06-13 20:20:40 +00009797 failed |= fclose(fd);
9798
9799 if (failed)
9800 EMSG(_(e_write));
9801#if defined(FEAT_EVAL) && defined(FEAT_SESSION)
9802 else if (eap->cmdidx == CMD_mksession)
9803 {
9804 /* successful session write - set this_session var */
Bram Moolenaard9462e32011-04-11 21:35:11 +02009805 char_u *tbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009806
Bram Moolenaard9462e32011-04-11 21:35:11 +02009807 tbuf = alloc(MAXPATHL);
9808 if (tbuf != NULL)
9809 {
9810 if (vim_FullName(fname, tbuf, MAXPATHL, FALSE) == OK)
9811 set_vim_var_string(VV_THIS_SESSION, tbuf, -1);
9812 vim_free(tbuf);
9813 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009814 }
9815#endif
9816#ifdef MKSESSION_NL
9817 mksession_nl = FALSE;
9818#endif
9819 }
9820
9821#ifdef FEAT_BROWSE
9822theend:
9823 vim_free(browseFile);
9824#endif
9825#ifdef FEAT_SESSION
9826 vim_free(viewFile);
9827#endif
9828}
9829
Bram Moolenaardf177f62005-02-22 08:39:57 +00009830#if ((defined(FEAT_SESSION) || defined(FEAT_EVAL)) && defined(vim_mkdir)) \
9831 || defined(PROTO)
9832 int
9833vim_mkdir_emsg(name, prot)
9834 char_u *name;
Bram Moolenaar78a15312009-05-15 19:33:18 +00009835 int prot UNUSED;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009836{
9837 if (vim_mkdir(name, prot) != 0)
9838 {
9839 EMSG2(_("E739: Cannot create directory: %s"), name);
9840 return FAIL;
9841 }
9842 return OK;
9843}
9844#endif
9845
Bram Moolenaar071d4272004-06-13 20:20:40 +00009846/*
9847 * Open a file for writing for an Ex command, with some checks.
9848 * Return file descriptor, or NULL on failure.
9849 */
9850 FILE *
9851open_exfile(fname, forceit, mode)
9852 char_u *fname;
9853 int forceit;
9854 char *mode; /* "w" for create new file or "a" for append */
9855{
9856 FILE *fd;
9857
9858#ifdef UNIX
9859 /* with Unix it is possible to open a directory */
9860 if (mch_isdir(fname))
9861 {
9862 EMSG2(_(e_isadir2), fname);
9863 return NULL;
9864 }
9865#endif
9866 if (!forceit && *mode != 'a' && vim_fexists(fname))
9867 {
9868 EMSG2(_("E189: \"%s\" exists (add ! to override)"), fname);
9869 return NULL;
9870 }
9871
9872 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
9873 EMSG2(_("E190: Cannot open \"%s\" for writing"), fname);
9874
9875 return fd;
9876}
9877
9878/*
9879 * ":mark" and ":k".
9880 */
9881 static void
9882ex_mark(eap)
9883 exarg_T *eap;
9884{
9885 pos_T pos;
9886
9887 if (*eap->arg == NUL) /* No argument? */
9888 EMSG(_(e_argreq));
9889 else if (eap->arg[1] != NUL) /* more than one character? */
9890 EMSG(_(e_trailing));
9891 else
9892 {
9893 pos = curwin->w_cursor; /* save curwin->w_cursor */
9894 curwin->w_cursor.lnum = eap->line2;
9895 beginline(BL_WHITE | BL_FIX);
9896 if (setmark(*eap->arg) == FAIL) /* set mark */
9897 EMSG(_("E191: Argument must be a letter or forward/backward quote"));
9898 curwin->w_cursor = pos; /* restore curwin->w_cursor */
9899 }
9900}
9901
9902/*
9903 * Update w_topline, w_leftcol and the cursor position.
9904 */
9905 void
9906update_topline_cursor()
9907{
9908 check_cursor(); /* put cursor on valid line */
9909 update_topline();
9910 if (!curwin->w_p_wrap)
9911 validate_cursor();
9912 update_curswant();
9913}
9914
9915#ifdef FEAT_EX_EXTRA
9916/*
9917 * ":normal[!] {commands}": Execute normal mode commands.
9918 */
9919 static void
9920ex_normal(eap)
9921 exarg_T *eap;
9922{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009923 int save_msg_scroll = msg_scroll;
9924 int save_restart_edit = restart_edit;
9925 int save_msg_didout = msg_didout;
9926 int save_State = State;
9927 tasave_T tabuf;
9928 int save_insertmode = p_im;
9929 int save_finish_op = finish_op;
Bram Moolenaar38084112008-07-26 14:05:07 +00009930 int save_opcount = opcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009931#ifdef FEAT_MBYTE
9932 char_u *arg = NULL;
9933 int l;
9934 char_u *p;
9935#endif
9936
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00009937 if (ex_normal_lock > 0)
9938 {
9939 EMSG(_(e_secure));
9940 return;
9941 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009942 if (ex_normal_busy >= p_mmd)
9943 {
9944 EMSG(_("E192: Recursive use of :normal too deep"));
9945 return;
9946 }
9947 ++ex_normal_busy;
9948
9949 msg_scroll = FALSE; /* no msg scrolling in Normal mode */
9950 restart_edit = 0; /* don't go to Insert mode */
9951 p_im = FALSE; /* don't use 'insertmode' */
9952
9953#ifdef FEAT_MBYTE
9954 /*
9955 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
9956 * this for the K_SPECIAL leading byte, otherwise special keys will not
9957 * work.
9958 */
9959 if (has_mbyte)
9960 {
9961 int len = 0;
9962
9963 /* Count the number of characters to be escaped. */
9964 for (p = eap->arg; *p != NUL; ++p)
9965 {
9966# ifdef FEAT_GUI
9967 if (*p == CSI) /* leadbyte CSI */
9968 len += 2;
9969# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009970 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009971 if (*++p == K_SPECIAL /* trailbyte K_SPECIAL or CSI */
9972# ifdef FEAT_GUI
9973 || *p == CSI
9974# endif
9975 )
9976 len += 2;
9977 }
9978 if (len > 0)
9979 {
9980 arg = alloc((unsigned)(STRLEN(eap->arg) + len + 1));
9981 if (arg != NULL)
9982 {
9983 len = 0;
9984 for (p = eap->arg; *p != NUL; ++p)
9985 {
9986 arg[len++] = *p;
9987# ifdef FEAT_GUI
9988 if (*p == CSI)
9989 {
9990 arg[len++] = KS_EXTRA;
9991 arg[len++] = (int)KE_CSI;
9992 }
9993# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009994 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009995 {
9996 arg[len++] = *++p;
9997 if (*p == K_SPECIAL)
9998 {
9999 arg[len++] = KS_SPECIAL;
10000 arg[len++] = KE_FILLER;
10001 }
10002# ifdef FEAT_GUI
10003 else if (*p == CSI)
10004 {
10005 arg[len++] = KS_EXTRA;
10006 arg[len++] = (int)KE_CSI;
10007 }
10008# endif
10009 }
10010 arg[len] = NUL;
10011 }
10012 }
10013 }
10014 }
10015#endif
10016
10017 /*
10018 * Save the current typeahead. This is required to allow using ":normal"
10019 * from an event handler and makes sure we don't hang when the argument
10020 * ends with half a command.
10021 */
10022 save_typeahead(&tabuf);
10023 if (tabuf.typebuf_valid)
10024 {
10025 /*
10026 * Repeat the :normal command for each line in the range. When no
10027 * range given, execute it just once, without positioning the cursor
10028 * first.
10029 */
10030 do
10031 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010032 if (eap->addr_count != 0)
10033 {
10034 curwin->w_cursor.lnum = eap->line1++;
10035 curwin->w_cursor.col = 0;
10036 }
10037
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010038 exec_normal_cmd(
Bram Moolenaar071d4272004-06-13 20:20:40 +000010039#ifdef FEAT_MBYTE
10040 arg != NULL ? arg :
10041#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010042 eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010043 }
10044 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
10045 }
10046
10047 /* Might not return to the main loop when in an event handler. */
10048 update_topline_cursor();
10049
10050 /* Restore the previous typeahead. */
10051 restore_typeahead(&tabuf);
10052
10053 --ex_normal_busy;
10054 msg_scroll = save_msg_scroll;
10055 restart_edit = save_restart_edit;
10056 p_im = save_insertmode;
10057 finish_op = save_finish_op;
Bram Moolenaar38084112008-07-26 14:05:07 +000010058 opcount = save_opcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010059 msg_didout |= save_msg_didout; /* don't reset msg_didout now */
10060
10061 /* Restore the state (needed when called from a function executed for
Bram Moolenaareda73602014-11-05 09:53:23 +010010062 * 'indentexpr'). Update the mouse and cursor, they may have changed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010063 State = save_State;
Bram Moolenaareda73602014-11-05 09:53:23 +010010064#ifdef FEAT_MOUSE
10065 setmouse();
10066#endif
10067#ifdef CURSOR_SHAPE
10068 ui_cursor_shape(); /* may show different cursor shape */
10069#endif
10070
Bram Moolenaar071d4272004-06-13 20:20:40 +000010071#ifdef FEAT_MBYTE
10072 vim_free(arg);
10073#endif
10074}
10075
10076/*
Bram Moolenaar64969662005-12-14 21:59:55 +000010077 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010078 */
10079 static void
10080ex_startinsert(eap)
10081 exarg_T *eap;
10082{
Bram Moolenaarfd371682005-01-14 21:42:54 +000010083 if (eap->forceit)
10084 {
10085 coladvance((colnr_T)MAXCOL);
10086 curwin->w_curswant = MAXCOL;
10087 curwin->w_set_curswant = FALSE;
10088 }
10089
Bram Moolenaara40c5002005-01-09 21:16:21 +000010090 /* Ignore the command when already in Insert mode. Inserting an
10091 * expression register that invokes a function can do this. */
10092 if (State & INSERT)
10093 return;
10094
Bram Moolenaar64969662005-12-14 21:59:55 +000010095 if (eap->cmdidx == CMD_startinsert)
10096 restart_edit = 'a';
10097 else if (eap->cmdidx == CMD_startreplace)
10098 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010099 else
Bram Moolenaar64969662005-12-14 21:59:55 +000010100 restart_edit = 'V';
10101
10102 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010103 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010104 if (eap->cmdidx == CMD_startinsert)
10105 restart_edit = 'i';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010106 curwin->w_curswant = 0; /* avoid MAXCOL */
10107 }
10108}
10109
10110/*
10111 * ":stopinsert"
10112 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010113 static void
10114ex_stopinsert(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010115 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010116{
10117 restart_edit = 0;
10118 stop_insert_mode = TRUE;
10119}
10120#endif
10121
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010122#if defined(FEAT_EX_EXTRA) || defined(FEAT_MENU) || defined(PROTO)
10123/*
10124 * Execute normal mode command "cmd".
10125 * "remap" can be REMAP_NONE or REMAP_YES.
10126 */
10127 void
10128exec_normal_cmd(cmd, remap, silent)
10129 char_u *cmd;
10130 int remap;
10131 int silent;
10132{
10133 oparg_T oa;
10134
10135 /*
10136 * Stuff the argument into the typeahead buffer.
10137 * Execute normal_cmd() until there is no typeahead left.
10138 */
10139 clear_oparg(&oa);
10140 finish_op = FALSE;
10141 ins_typebuf(cmd, remap, 0, TRUE, silent);
10142 while ((!stuff_empty() || (!typebuf_typed() && typebuf.tb_len > 0))
10143 && !got_int)
10144 {
10145 update_topline_cursor();
Bram Moolenaar48ac02c2011-01-17 19:50:06 +010010146 normal_cmd(&oa, TRUE); /* execute a Normal mode cmd */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010147 }
10148}
10149#endif
10150
Bram Moolenaar071d4272004-06-13 20:20:40 +000010151#ifdef FEAT_FIND_ID
10152 static void
10153ex_checkpath(eap)
10154 exarg_T *eap;
10155{
10156 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
10157 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
10158 (linenr_T)1, (linenr_T)MAXLNUM);
10159}
10160
10161#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
10162/*
10163 * ":psearch"
10164 */
10165 static void
10166ex_psearch(eap)
10167 exarg_T *eap;
10168{
10169 g_do_tagpreview = p_pvh;
10170 ex_findpat(eap);
10171 g_do_tagpreview = 0;
10172}
10173#endif
10174
10175 static void
10176ex_findpat(eap)
10177 exarg_T *eap;
10178{
10179 int whole = TRUE;
10180 long n;
10181 char_u *p;
10182 int action;
10183
10184 switch (cmdnames[eap->cmdidx].cmd_name[2])
10185 {
10186 case 'e': /* ":psearch", ":isearch" and ":dsearch" */
10187 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
10188 action = ACTION_GOTO;
10189 else
10190 action = ACTION_SHOW;
10191 break;
10192 case 'i': /* ":ilist" and ":dlist" */
10193 action = ACTION_SHOW_ALL;
10194 break;
10195 case 'u': /* ":ijump" and ":djump" */
10196 action = ACTION_GOTO;
10197 break;
10198 default: /* ":isplit" and ":dsplit" */
10199 action = ACTION_SPLIT;
10200 break;
10201 }
10202
10203 n = 1;
10204 if (vim_isdigit(*eap->arg)) /* get count */
10205 {
10206 n = getdigits(&eap->arg);
10207 eap->arg = skipwhite(eap->arg);
10208 }
10209 if (*eap->arg == '/') /* Match regexp, not just whole words */
10210 {
10211 whole = FALSE;
10212 ++eap->arg;
10213 p = skip_regexp(eap->arg, '/', p_magic, NULL);
10214 if (*p)
10215 {
10216 *p++ = NUL;
10217 p = skipwhite(p);
10218
10219 /* Check for trailing illegal characters */
10220 if (!ends_excmd(*p))
10221 eap->errmsg = e_trailing;
10222 else
10223 eap->nextcmd = check_nextcmd(p);
10224 }
10225 }
10226 if (!eap->skip)
10227 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
10228 whole, !eap->forceit,
10229 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
10230 n, action, eap->line1, eap->line2);
10231}
10232#endif
10233
10234#ifdef FEAT_WINDOWS
10235
10236# ifdef FEAT_QUICKFIX
10237/*
10238 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
10239 */
10240 static void
10241ex_ptag(eap)
10242 exarg_T *eap;
10243{
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +010010244 g_do_tagpreview = p_pvh; /* will be reset to 0 in ex_tag_cmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010245 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
10246}
10247
10248/*
10249 * ":pedit"
10250 */
10251 static void
10252ex_pedit(eap)
10253 exarg_T *eap;
10254{
10255 win_T *curwin_save = curwin;
10256
10257 g_do_tagpreview = p_pvh;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +000010258 prepare_tagpreview(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010259 keep_help_flag = curwin_save->w_buffer->b_help;
10260 do_exedit(eap, NULL);
10261 keep_help_flag = FALSE;
10262 if (curwin != curwin_save && win_valid(curwin_save))
10263 {
10264 /* Return cursor to where we were */
10265 validate_cursor();
10266 redraw_later(VALID);
10267 win_enter(curwin_save, TRUE);
10268 }
10269 g_do_tagpreview = 0;
10270}
10271# endif
10272
10273/*
10274 * ":stag", ":stselect" and ":stjump".
10275 */
10276 static void
10277ex_stag(eap)
10278 exarg_T *eap;
10279{
10280 postponed_split = -1;
10281 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +000010282 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010283 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
10284 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +000010285 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010286}
10287#endif
10288
10289/*
10290 * ":tag", ":tselect", ":tjump", ":tnext", etc.
10291 */
10292 static void
10293ex_tag(eap)
10294 exarg_T *eap;
10295{
10296 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
10297}
10298
10299 static void
10300ex_tag_cmd(eap, name)
10301 exarg_T *eap;
10302 char_u *name;
10303{
10304 int cmd;
10305
10306 switch (name[1])
10307 {
10308 case 'j': cmd = DT_JUMP; /* ":tjump" */
10309 break;
10310 case 's': cmd = DT_SELECT; /* ":tselect" */
10311 break;
10312 case 'p': cmd = DT_PREV; /* ":tprevious" */
10313 break;
10314 case 'N': cmd = DT_PREV; /* ":tNext" */
10315 break;
10316 case 'n': cmd = DT_NEXT; /* ":tnext" */
10317 break;
10318 case 'o': cmd = DT_POP; /* ":pop" */
10319 break;
10320 case 'f': /* ":tfirst" */
10321 case 'r': cmd = DT_FIRST; /* ":trewind" */
10322 break;
10323 case 'l': cmd = DT_LAST; /* ":tlast" */
10324 break;
10325 default: /* ":tag" */
10326#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +000010327 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010328 {
10329 do_cstag(eap);
10330 return;
10331 }
10332#endif
10333 cmd = DT_TAG;
10334 break;
10335 }
10336
Bram Moolenaarb8a7b562006-02-01 21:47:16 +000010337 if (name[0] == 'l')
10338 {
10339#ifndef FEAT_QUICKFIX
10340 ex_ni(eap);
10341 return;
10342#else
10343 cmd = DT_LTAG;
10344#endif
10345 }
10346
Bram Moolenaar071d4272004-06-13 20:20:40 +000010347 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
10348 eap->forceit, TRUE);
10349}
10350
10351/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010352 * Check "str" for starting with a special cmdline variable.
10353 * If found return one of the SPEC_ values and set "*usedlen" to the length of
10354 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
10355 */
10356 int
10357find_cmdline_var(src, usedlen)
10358 char_u *src;
10359 int *usedlen;
10360{
10361 int len;
10362 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +000010363 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010364 "%",
10365#define SPEC_PERC 0
10366 "#",
10367#define SPEC_HASH 1
10368 "<cword>", /* cursor word */
10369#define SPEC_CWORD 2
10370 "<cWORD>", /* cursor WORD */
10371#define SPEC_CCWORD 3
10372 "<cfile>", /* cursor path name */
10373#define SPEC_CFILE 4
10374 "<sfile>", /* ":so" file name */
10375#define SPEC_SFILE 5
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010376 "<slnum>", /* ":so" file line number */
10377#define SPEC_SLNUM 6
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010378#ifdef FEAT_AUTOCMD
10379 "<afile>", /* autocommand file name */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010380# define SPEC_AFILE 7
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010381 "<abuf>", /* autocommand buffer number */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010382# define SPEC_ABUF 8
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010383 "<amatch>", /* autocommand match name */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010384# define SPEC_AMATCH 9
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010385#endif
10386#ifdef FEAT_CLIENTSERVER
10387 "<client>"
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010388# ifdef FEAT_AUTOCMD
10389# define SPEC_CLIENT 10
10390# else
10391# define SPEC_CLIENT 7
10392# endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010393#endif
10394 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010395
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000010396 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010397 {
10398 len = (int)STRLEN(spec_str[i]);
10399 if (STRNCMP(src, spec_str[i], len) == 0)
10400 {
10401 *usedlen = len;
10402 return i;
10403 }
10404 }
10405 return -1;
10406}
10407
10408/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010409 * Evaluate cmdline variables.
10410 *
10411 * change '%' to curbuf->b_ffname
10412 * '#' to curwin->w_altfile
10413 * '<cword>' to word under the cursor
10414 * '<cWORD>' to WORD under the cursor
10415 * '<cfile>' to path name under the cursor
10416 * '<sfile>' to sourced file name
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010417 * '<slnum>' to sourced file line number
Bram Moolenaar071d4272004-06-13 20:20:40 +000010418 * '<afile>' to file name for autocommand
10419 * '<abuf>' to buffer number for autocommand
10420 * '<amatch>' to matching name for autocommand
10421 *
10422 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
10423 * "" for error without a message) and NULL is returned.
10424 * Returns an allocated string if a valid match was found.
10425 * Returns NULL if no match was found. "usedlen" then still contains the
10426 * number of characters to skip.
10427 */
10428 char_u *
Bram Moolenaar63b92542007-03-27 14:57:09 +000010429eval_vars(src, srcstart, usedlen, lnump, errormsg, escaped)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010430 char_u *src; /* pointer into commandline */
Bram Moolenaar63b92542007-03-27 14:57:09 +000010431 char_u *srcstart; /* beginning of valid memory for src */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010432 int *usedlen; /* characters after src that are used */
10433 linenr_T *lnump; /* line number for :e command, or NULL */
10434 char_u **errormsg; /* pointer to error message */
Bram Moolenaar63b92542007-03-27 14:57:09 +000010435 int *escaped; /* return value has escaped white space (can
10436 * be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010437{
10438 int i;
10439 char_u *s;
10440 char_u *result;
10441 char_u *resultbuf = NULL;
10442 int resultlen;
10443 buf_T *buf;
10444 int valid = VALID_HEAD + VALID_PATH; /* assume valid result */
10445 int spec_idx;
10446#ifdef FEAT_MODIFY_FNAME
10447 int skip_mod = FALSE;
10448#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010449 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010450
10451 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +000010452 if (escaped != NULL)
10453 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010454
10455 /*
10456 * Check if there is something to do.
10457 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010458 spec_idx = find_cmdline_var(src, usedlen);
10459 if (spec_idx < 0) /* no match */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010460 {
10461 *usedlen = 1;
10462 return NULL;
10463 }
10464
10465 /*
10466 * Skip when preceded with a backslash "\%" and "\#".
10467 * Note: In "\\%" the % is also not recognized!
10468 */
10469 if (src > srcstart && src[-1] == '\\')
10470 {
10471 *usedlen = 0;
Bram Moolenaara7241f52008-06-24 20:39:31 +000010472 STRMOVE(src - 1, src); /* remove backslash */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010473 return NULL;
10474 }
10475
10476 /*
10477 * word or WORD under cursor
10478 */
10479 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD)
10480 {
10481 resultlen = find_ident_under_cursor(&result, spec_idx == SPEC_CWORD ?
10482 (FIND_IDENT|FIND_STRING) : FIND_STRING);
10483 if (resultlen == 0)
10484 {
10485 *errormsg = (char_u *)"";
10486 return NULL;
10487 }
10488 }
10489
10490 /*
10491 * '#': Alternate file name
10492 * '%': Current file name
10493 * File name under the cursor
10494 * File name for autocommand
10495 * and following modifiers
10496 */
10497 else
10498 {
10499 switch (spec_idx)
10500 {
10501 case SPEC_PERC: /* '%': current file */
10502 if (curbuf->b_fname == NULL)
10503 {
10504 result = (char_u *)"";
10505 valid = 0; /* Must have ":p:h" to be valid */
10506 }
10507 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010508 result = curbuf->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010509 break;
10510
10511 case SPEC_HASH: /* '#' or "#99": alternate file */
10512 if (src[1] == '#') /* "##": the argument list */
10513 {
10514 result = arg_all();
10515 resultbuf = result;
10516 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +000010517 if (escaped != NULL)
10518 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010519#ifdef FEAT_MODIFY_FNAME
10520 skip_mod = TRUE;
10521#endif
10522 break;
10523 }
10524 s = src + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +000010525 if (*s == '<') /* "#<99" uses v:oldfiles */
10526 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010527 i = (int)getdigits(&s);
10528 *usedlen = (int)(s - src); /* length of what we expand */
10529
Bram Moolenaard812df62008-11-09 12:46:09 +000010530 if (src[1] == '<')
Bram Moolenaar071d4272004-06-13 20:20:40 +000010531 {
Bram Moolenaard812df62008-11-09 12:46:09 +000010532 if (*usedlen < 2)
10533 {
10534 /* Should we give an error message for #<text? */
10535 *usedlen = 1;
10536 return NULL;
10537 }
10538#ifdef FEAT_EVAL
10539 result = list_find_str(get_vim_var_list(VV_OLDFILES),
10540 (long)i);
10541 if (result == NULL)
10542 {
10543 *errormsg = (char_u *)"";
10544 return NULL;
10545 }
10546#else
10547 *errormsg = (char_u *)_("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +000010548 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +000010549#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010550 }
10551 else
Bram Moolenaard812df62008-11-09 12:46:09 +000010552 {
10553 buf = buflist_findnr(i);
10554 if (buf == NULL)
10555 {
10556 *errormsg = (char_u *)_("E194: No alternate file name to substitute for '#'");
10557 return NULL;
10558 }
10559 if (lnump != NULL)
10560 *lnump = ECMD_LAST;
10561 if (buf->b_fname == NULL)
10562 {
10563 result = (char_u *)"";
10564 valid = 0; /* Must have ":p:h" to be valid */
10565 }
10566 else
10567 result = buf->b_fname;
10568 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010569 break;
10570
10571#ifdef FEAT_SEARCHPATH
10572 case SPEC_CFILE: /* file name under cursor */
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010573 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010574 if (result == NULL)
10575 {
10576 *errormsg = (char_u *)"";
10577 return NULL;
10578 }
10579 resultbuf = result; /* remember allocated string */
10580 break;
10581#endif
10582
10583#ifdef FEAT_AUTOCMD
10584 case SPEC_AFILE: /* file name for autocommand */
10585 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +000010586 if (result != NULL && !autocmd_fname_full)
10587 {
10588 /* Still need to turn the fname into a full path. It is
10589 * postponed to avoid a delay when <afile> is not used. */
10590 autocmd_fname_full = TRUE;
10591 result = FullName_save(autocmd_fname, FALSE);
10592 vim_free(autocmd_fname);
10593 autocmd_fname = result;
10594 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010595 if (result == NULL)
10596 {
10597 *errormsg = (char_u *)_("E495: no autocommand file name to substitute for \"<afile>\"");
10598 return NULL;
10599 }
Bram Moolenaara0174af2008-01-02 20:08:25 +000010600 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010601 break;
10602
10603 case SPEC_ABUF: /* buffer number for autocommand */
10604 if (autocmd_bufnr <= 0)
10605 {
10606 *errormsg = (char_u *)_("E496: no autocommand buffer number to substitute for \"<abuf>\"");
10607 return NULL;
10608 }
10609 sprintf((char *)strbuf, "%d", autocmd_bufnr);
10610 result = strbuf;
10611 break;
10612
10613 case SPEC_AMATCH: /* match name for autocommand */
10614 result = autocmd_match;
10615 if (result == NULL)
10616 {
10617 *errormsg = (char_u *)_("E497: no autocommand match name to substitute for \"<amatch>\"");
10618 return NULL;
10619 }
10620 break;
10621
10622#endif
10623 case SPEC_SFILE: /* file name for ":so" command */
10624 result = sourcing_name;
10625 if (result == NULL)
10626 {
10627 *errormsg = (char_u *)_("E498: no :source file name to substitute for \"<sfile>\"");
10628 return NULL;
10629 }
10630 break;
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010631 case SPEC_SLNUM: /* line in file for ":so" command */
10632 if (sourcing_name == NULL || sourcing_lnum == 0)
10633 {
10634 *errormsg = (char_u *)_("E842: no line number to use for \"<slnum>\"");
10635 return NULL;
10636 }
10637 sprintf((char *)strbuf, "%ld", (long)sourcing_lnum);
10638 result = strbuf;
10639 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010640#if defined(FEAT_CLIENTSERVER)
10641 case SPEC_CLIENT: /* Source of last submitted input */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010642 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
10643 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010644 result = strbuf;
10645 break;
10646#endif
10647 }
10648
10649 resultlen = (int)STRLEN(result); /* length of new string */
10650 if (src[*usedlen] == '<') /* remove the file name extension */
10651 {
10652 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010653 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010654 resultlen = (int)(s - result);
10655 }
10656#ifdef FEAT_MODIFY_FNAME
10657 else if (!skip_mod)
10658 {
10659 valid |= modify_fname(src, usedlen, &result, &resultbuf,
10660 &resultlen);
10661 if (result == NULL)
10662 {
10663 *errormsg = (char_u *)"";
10664 return NULL;
10665 }
10666 }
10667#endif
10668 }
10669
10670 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
10671 {
10672 if (valid != VALID_HEAD + VALID_PATH)
10673 /* xgettext:no-c-format */
10674 *errormsg = (char_u *)_("E499: Empty file name for '%' or '#', only works with \":p:h\"");
10675 else
10676 *errormsg = (char_u *)_("E500: Evaluates to an empty string");
10677 result = NULL;
10678 }
10679 else
10680 result = vim_strnsave(result, resultlen);
10681 vim_free(resultbuf);
10682 return result;
10683}
10684
10685/*
10686 * Concatenate all files in the argument list, separated by spaces, and return
10687 * it in one allocated string.
10688 * Spaces and backslashes in the file names are escaped with a backslash.
10689 * Returns NULL when out of memory.
10690 */
10691 static char_u *
10692arg_all()
10693{
10694 int len;
10695 int idx;
10696 char_u *retval = NULL;
10697 char_u *p;
10698
10699 /*
10700 * Do this loop two times:
10701 * first time: compute the total length
10702 * second time: concatenate the names
10703 */
10704 for (;;)
10705 {
10706 len = 0;
10707 for (idx = 0; idx < ARGCOUNT; ++idx)
10708 {
10709 p = alist_name(&ARGLIST[idx]);
10710 if (p != NULL)
10711 {
10712 if (len > 0)
10713 {
10714 /* insert a space in between names */
10715 if (retval != NULL)
10716 retval[len] = ' ';
10717 ++len;
10718 }
10719 for ( ; *p != NUL; ++p)
10720 {
10721 if (*p == ' ' || *p == '\\')
10722 {
10723 /* insert a backslash */
10724 if (retval != NULL)
10725 retval[len] = '\\';
10726 ++len;
10727 }
10728 if (retval != NULL)
10729 retval[len] = *p;
10730 ++len;
10731 }
10732 }
10733 }
10734
10735 /* second time: break here */
10736 if (retval != NULL)
10737 {
10738 retval[len] = NUL;
10739 break;
10740 }
10741
10742 /* allocate memory */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000010743 retval = alloc((unsigned)len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010744 if (retval == NULL)
10745 break;
10746 }
10747
10748 return retval;
10749}
10750
10751#if defined(FEAT_AUTOCMD) || defined(PROTO)
10752/*
10753 * Expand the <sfile> string in "arg".
10754 *
10755 * Returns an allocated string, or NULL for any error.
10756 */
10757 char_u *
10758expand_sfile(arg)
10759 char_u *arg;
10760{
10761 char_u *errormsg;
10762 int len;
10763 char_u *result;
10764 char_u *newres;
10765 char_u *repl;
10766 int srclen;
10767 char_u *p;
10768
10769 result = vim_strsave(arg);
10770 if (result == NULL)
10771 return NULL;
10772
10773 for (p = result; *p; )
10774 {
10775 if (STRNCMP(p, "<sfile>", 7) != 0)
10776 ++p;
10777 else
10778 {
10779 /* replace "<sfile>" with the sourced file name, and do ":" stuff */
Bram Moolenaar63b92542007-03-27 14:57:09 +000010780 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010781 if (errormsg != NULL)
10782 {
10783 if (*errormsg)
10784 emsg(errormsg);
10785 vim_free(result);
10786 return NULL;
10787 }
10788 if (repl == NULL) /* no match (cannot happen) */
10789 {
10790 p += srclen;
10791 continue;
10792 }
10793 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
10794 newres = alloc(len);
10795 if (newres == NULL)
10796 {
10797 vim_free(repl);
10798 vim_free(result);
10799 return NULL;
10800 }
10801 mch_memmove(newres, result, (size_t)(p - result));
10802 STRCPY(newres + (p - result), repl);
10803 len = (int)STRLEN(newres);
10804 STRCAT(newres, p + srclen);
10805 vim_free(repl);
10806 vim_free(result);
10807 result = newres;
10808 p = newres + len; /* continue after the match */
10809 }
10810 }
10811
10812 return result;
10813}
10814#endif
10815
10816#ifdef FEAT_SESSION
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010817static int ses_winsizes __ARGS((FILE *fd, int restore_size,
10818 win_T *tab_firstwin));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010819static int ses_win_rec __ARGS((FILE *fd, frame_T *fr));
10820static frame_T *ses_skipframe __ARGS((frame_T *fr));
10821static int ses_do_frame __ARGS((frame_T *fr));
10822static int ses_do_win __ARGS((win_T *wp));
10823static int ses_arglist __ARGS((FILE *fd, char *cmd, garray_T *gap, int fullname, unsigned *flagp));
10824static int ses_put_fname __ARGS((FILE *fd, char_u *name, unsigned *flagp));
10825static int ses_fname __ARGS((FILE *fd, buf_T *buf, unsigned *flagp));
10826
10827/*
10828 * Write openfile commands for the current buffers to an .exrc file.
10829 * Return FAIL on error, OK otherwise.
10830 */
10831 static int
10832makeopens(fd, dirnow)
10833 FILE *fd;
10834 char_u *dirnow; /* Current directory name */
10835{
10836 buf_T *buf;
10837 int only_save_windows = TRUE;
10838 int nr;
10839 int cnr = 1;
10840 int restore_size = TRUE;
10841 win_T *wp;
10842 char_u *sname;
10843 win_T *edited_win = NULL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010844 int tabnr;
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020010845 int restore_stal = FALSE;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010846 win_T *tab_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010847 frame_T *tab_topframe;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010848 int cur_arg_idx = 0;
Bram Moolenaar383c6f52008-01-04 15:01:07 +000010849 int next_arg_idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010850
10851 if (ssop_flags & SSOP_BUFFERS)
10852 only_save_windows = FALSE; /* Save ALL buffers */
10853
10854 /*
10855 * Begin by setting the this_session variable, and then other
10856 * sessionable variables.
10857 */
10858#ifdef FEAT_EVAL
10859 if (put_line(fd, "let v:this_session=expand(\"<sfile>:p\")") == FAIL)
10860 return FAIL;
10861 if (ssop_flags & SSOP_GLOBALS)
10862 if (store_session_globals(fd) == FAIL)
10863 return FAIL;
10864#endif
10865
10866 /*
10867 * Close all windows but one.
10868 */
10869 if (put_line(fd, "silent only") == FAIL)
10870 return FAIL;
10871
10872 /*
10873 * Now a :cd command to the session directory or the current directory
10874 */
10875 if (ssop_flags & SSOP_SESDIR)
10876 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010877 if (put_line(fd, "exe \"cd \" . escape(expand(\"<sfile>:p:h\"), ' ')")
10878 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010879 return FAIL;
10880 }
10881 else if (ssop_flags & SSOP_CURDIR)
10882 {
10883 sname = home_replace_save(NULL, globaldir != NULL ? globaldir : dirnow);
10884 if (sname == NULL
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010885 || fputs("cd ", fd) < 0
10886 || ses_put_fname(fd, sname, &ssop_flags) == FAIL
10887 || put_eol(fd) == FAIL)
10888 {
10889 vim_free(sname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010890 return FAIL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010891 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010892 vim_free(sname);
10893 }
10894
10895 /*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010896 * If there is an empty, unnamed buffer we will wipe it out later.
10897 * Remember the buffer number.
10898 */
10899 if (put_line(fd, "if expand('%') == '' && !&modified && line('$') <= 1 && getline(1) == ''") == FAIL)
10900 return FAIL;
10901 if (put_line(fd, " let s:wipebuf = bufnr('%')") == FAIL)
10902 return FAIL;
10903 if (put_line(fd, "endif") == FAIL)
10904 return FAIL;
10905
10906 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010907 * Now save the current files, current buffer first.
10908 */
10909 if (put_line(fd, "set shortmess=aoO") == FAIL)
10910 return FAIL;
10911
10912 /* Now put the other buffers into the buffer list */
10913 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
10914 {
10915 if (!(only_save_windows && buf->b_nwindows == 0)
10916 && !(buf->b_help && !(ssop_flags & SSOP_HELP))
10917 && buf->b_fname != NULL
10918 && buf->b_p_bl)
10919 {
10920 if (fprintf(fd, "badd +%ld ", buf->b_wininfo == NULL ? 1L
10921 : buf->b_wininfo->wi_fpos.lnum) < 0
10922 || ses_fname(fd, buf, &ssop_flags) == FAIL)
10923 return FAIL;
10924 }
10925 }
10926
10927 /* the global argument list */
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010010928 if (ses_arglist(fd, "argglobal", &global_alist.al_ga,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010929 !(ssop_flags & SSOP_CURDIR), &ssop_flags) == FAIL)
10930 return FAIL;
10931
10932 if (ssop_flags & SSOP_RESIZE)
10933 {
10934 /* Note: after the restore we still check it worked!*/
10935 if (fprintf(fd, "set lines=%ld columns=%ld" , Rows, Columns) < 0
10936 || put_eol(fd) == FAIL)
10937 return FAIL;
10938 }
10939
10940#ifdef FEAT_GUI
10941 if (gui.in_use && (ssop_flags & SSOP_WINPOS))
10942 {
10943 int x, y;
10944
10945 if (gui_mch_get_winpos(&x, &y) == OK)
10946 {
10947 /* Note: after the restore we still check it worked!*/
10948 if (fprintf(fd, "winpos %d %d", x, y) < 0 || put_eol(fd) == FAIL)
10949 return FAIL;
10950 }
10951 }
10952#endif
10953
10954 /*
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020010955 * When there are two or more tabpages and 'showtabline' is 1 the tabline
10956 * will be displayed when creating the next tab. That resizes the windows
10957 * in the first tab, which may cause problems. Set 'showtabline' to 2
10958 * temporarily to avoid that.
10959 */
10960 if (p_stal == 1 && first_tabpage->tp_next != NULL)
10961 {
10962 if (put_line(fd, "set stal=2") == FAIL)
10963 return FAIL;
10964 restore_stal = TRUE;
10965 }
10966
10967 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000010968 * May repeat putting Windows for each tab, when "tabpages" is in
10969 * 'sessionoptions'.
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010970 * Don't use goto_tabpage(), it may change directory and trigger
10971 * autocommands.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010972 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010973 tab_firstwin = firstwin; /* first window in tab page "tabnr" */
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010974 tab_topframe = topframe;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010975 for (tabnr = 1; ; ++tabnr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010976 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010977 int need_tabnew = FALSE;
10978
Bram Moolenaar18144c82006-04-12 21:52:12 +000010979 if ((ssop_flags & SSOP_TABPAGES))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010980 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010981 tabpage_T *tp = find_tabpage(tabnr);
10982
10983 if (tp == NULL)
10984 break; /* done all tab pages */
10985 if (tp == curtab)
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010986 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010987 tab_firstwin = firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010988 tab_topframe = topframe;
10989 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010990 else
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010991 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010992 tab_firstwin = tp->tp_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010993 tab_topframe = tp->tp_topframe;
10994 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010995 if (tabnr > 1)
10996 need_tabnew = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010997 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010998
Bram Moolenaar18144c82006-04-12 21:52:12 +000010999 /*
11000 * Before creating the window layout, try loading one file. If this
11001 * is aborted we don't end up with a number of useless windows.
11002 * This may have side effects! (e.g., compressed or network file).
11003 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011004 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011005 {
11006 if (ses_do_win(wp)
11007 && wp->w_buffer->b_ffname != NULL
11008 && !wp->w_buffer->b_help
11009#ifdef FEAT_QUICKFIX
11010 && !bt_nofile(wp->w_buffer)
11011#endif
11012 )
11013 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011014 if (fputs(need_tabnew ? "tabedit " : "edit ", fd) < 0
Bram Moolenaar18144c82006-04-12 21:52:12 +000011015 || ses_fname(fd, wp->w_buffer, &ssop_flags) == FAIL)
11016 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011017 need_tabnew = FALSE;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011018 if (!wp->w_arg_idx_invalid)
11019 edited_win = wp;
11020 break;
11021 }
11022 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011023
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011024 /* If no file got edited create an empty tab page. */
11025 if (need_tabnew && put_line(fd, "tabnew") == FAIL)
11026 return FAIL;
11027
Bram Moolenaar18144c82006-04-12 21:52:12 +000011028 /*
11029 * Save current window layout.
11030 */
11031 if (put_line(fd, "set splitbelow splitright") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011032 return FAIL;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011033 if (ses_win_rec(fd, tab_topframe) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011034 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011035 if (!p_sb && put_line(fd, "set nosplitbelow") == FAIL)
11036 return FAIL;
11037 if (!p_spr && put_line(fd, "set nosplitright") == FAIL)
11038 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011039
Bram Moolenaar18144c82006-04-12 21:52:12 +000011040 /*
11041 * Check if window sizes can be restored (no windows omitted).
11042 * Remember the window number of the current window after restoring.
11043 */
11044 nr = 0;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011045 for (wp = tab_firstwin; wp != NULL; wp = W_NEXT(wp))
Bram Moolenaar18144c82006-04-12 21:52:12 +000011046 {
11047 if (ses_do_win(wp))
11048 ++nr;
11049 else
11050 restore_size = FALSE;
11051 if (curwin == wp)
11052 cnr = nr;
11053 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011054
Bram Moolenaar18144c82006-04-12 21:52:12 +000011055 /* Go to the first window. */
11056 if (put_line(fd, "wincmd t") == FAIL)
11057 return FAIL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011058
Bram Moolenaar18144c82006-04-12 21:52:12 +000011059 /*
11060 * If more than one window, see if sizes can be restored.
11061 * First set 'winheight' and 'winwidth' to 1 to avoid the windows being
11062 * resized when moving between windows.
11063 * Do this before restoring the view, so that the topline and the
11064 * cursor can be set. This is done again below.
11065 */
11066 if (put_line(fd, "set winheight=1 winwidth=1") == FAIL)
11067 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011068 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011069 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011070
Bram Moolenaar18144c82006-04-12 21:52:12 +000011071 /*
11072 * Restore the view of the window (options, file, cursor, etc.).
11073 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011074 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011075 {
11076 if (!ses_do_win(wp))
11077 continue;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011078 if (put_view(fd, wp, wp != edited_win, &ssop_flags,
11079 cur_arg_idx) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011080 return FAIL;
11081 if (nr > 1 && put_line(fd, "wincmd w") == FAIL)
11082 return FAIL;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011083 next_arg_idx = wp->w_arg_idx;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011084 }
11085
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011086 /* The argument index in the first tab page is zero, need to set it in
11087 * each window. For further tab pages it's the window where we do
11088 * "tabedit". */
11089 cur_arg_idx = next_arg_idx;
11090
Bram Moolenaar18144c82006-04-12 21:52:12 +000011091 /*
11092 * Restore cursor to the current window if it's not the first one.
11093 */
11094 if (cnr > 1 && (fprintf(fd, "%dwincmd w", cnr) < 0
11095 || put_eol(fd) == FAIL))
11096 return FAIL;
11097
11098 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000011099 * Restore window sizes again after jumping around in windows, because
11100 * the current window has a minimum size while others may not.
11101 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011102 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011103 return FAIL;
11104
Bram Moolenaar18144c82006-04-12 21:52:12 +000011105 /* Don't continue in another tab page when doing only the current one
11106 * or when at the last tab page. */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011107 if (!(ssop_flags & SSOP_TABPAGES))
Bram Moolenaar18144c82006-04-12 21:52:12 +000011108 break;
11109 }
11110
11111 if (ssop_flags & SSOP_TABPAGES)
11112 {
Bram Moolenaar18144c82006-04-12 21:52:12 +000011113 if (fprintf(fd, "tabnext %d", tabpage_index(curtab)) < 0
11114 || put_eol(fd) == FAIL)
11115 return FAIL;
11116 }
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020011117 if (restore_stal && put_line(fd, "set stal=1") == FAIL)
11118 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011119
Bram Moolenaar9c102382006-05-03 21:26:49 +000011120 /*
11121 * Wipe out an empty unnamed buffer we started in.
11122 */
11123 if (put_line(fd, "if exists('s:wipebuf')") == FAIL)
11124 return FAIL;
Bram Moolenaarf3a67882006-05-05 21:09:41 +000011125 if (put_line(fd, " silent exe 'bwipe ' . s:wipebuf") == FAIL)
Bram Moolenaar9c102382006-05-03 21:26:49 +000011126 return FAIL;
11127 if (put_line(fd, "endif") == FAIL)
11128 return FAIL;
11129 if (put_line(fd, "unlet! s:wipebuf") == FAIL)
11130 return FAIL;
11131
11132 /* Re-apply 'winheight', 'winwidth' and 'shortmess'. */
11133 if (fprintf(fd, "set winheight=%ld winwidth=%ld shortmess=%s",
11134 p_wh, p_wiw, p_shm) < 0 || put_eol(fd) == FAIL)
11135 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011136
11137 /*
11138 * Lastly, execute the x.vim file if it exists.
11139 */
11140 if (put_line(fd, "let s:sx = expand(\"<sfile>:p:r\").\"x.vim\"") == FAIL
11141 || put_line(fd, "if file_readable(s:sx)") == FAIL
Bram Moolenaar42ba1262008-12-09 10:18:03 +000011142 || put_line(fd, " exe \"source \" . fnameescape(s:sx)") == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000011143 || put_line(fd, "endif") == FAIL)
11144 return FAIL;
11145
11146 return OK;
11147}
11148
11149 static int
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011150ses_winsizes(fd, restore_size, tab_firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011151 FILE *fd;
11152 int restore_size;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011153 win_T *tab_firstwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011154{
11155 int n = 0;
11156 win_T *wp;
11157
11158 if (restore_size && (ssop_flags & SSOP_WINSIZE))
11159 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011160 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011161 {
11162 if (!ses_do_win(wp))
11163 continue;
11164 ++n;
11165
11166 /* restore height when not full height */
11167 if (wp->w_height + wp->w_status_height < topframe->fr_height
11168 && (fprintf(fd,
11169 "exe '%dresize ' . ((&lines * %ld + %ld) / %ld)",
11170 n, (long)wp->w_height, Rows / 2, Rows) < 0
11171 || put_eol(fd) == FAIL))
11172 return FAIL;
11173
11174 /* restore width when not full width */
11175 if (wp->w_width < Columns && (fprintf(fd,
11176 "exe 'vert %dresize ' . ((&columns * %ld + %ld) / %ld)",
11177 n, (long)wp->w_width, Columns / 2, Columns) < 0
11178 || put_eol(fd) == FAIL))
11179 return FAIL;
11180 }
11181 }
11182 else
11183 {
11184 /* Just equalise window sizes */
11185 if (put_line(fd, "wincmd =") == FAIL)
11186 return FAIL;
11187 }
11188 return OK;
11189}
11190
11191/*
11192 * Write commands to "fd" to recursively create windows for frame "fr",
11193 * horizontally and vertically split.
11194 * After the commands the last window in the frame is the current window.
11195 * Returns FAIL when writing the commands to "fd" fails.
11196 */
11197 static int
11198ses_win_rec(fd, fr)
11199 FILE *fd;
11200 frame_T *fr;
11201{
11202 frame_T *frc;
11203 int count = 0;
11204
11205 if (fr->fr_layout != FR_LEAF)
11206 {
11207 /* Find first frame that's not skipped and then create a window for
11208 * each following one (first frame is already there). */
11209 frc = ses_skipframe(fr->fr_child);
11210 if (frc != NULL)
11211 while ((frc = ses_skipframe(frc->fr_next)) != NULL)
11212 {
11213 /* Make window as big as possible so that we have lots of room
11214 * to split. */
11215 if (put_line(fd, "wincmd _ | wincmd |") == FAIL
11216 || put_line(fd, fr->fr_layout == FR_COL
11217 ? "split" : "vsplit") == FAIL)
11218 return FAIL;
11219 ++count;
11220 }
11221
11222 /* Go back to the first window. */
11223 if (count > 0 && (fprintf(fd, fr->fr_layout == FR_COL
11224 ? "%dwincmd k" : "%dwincmd h", count) < 0
11225 || put_eol(fd) == FAIL))
11226 return FAIL;
11227
11228 /* Recursively create frames/windows in each window of this column or
11229 * row. */
11230 frc = ses_skipframe(fr->fr_child);
11231 while (frc != NULL)
11232 {
11233 ses_win_rec(fd, frc);
11234 frc = ses_skipframe(frc->fr_next);
11235 /* Go to next window. */
11236 if (frc != NULL && put_line(fd, "wincmd w") == FAIL)
11237 return FAIL;
11238 }
11239 }
11240 return OK;
11241}
11242
11243/*
11244 * Skip frames that don't contain windows we want to save in the Session.
11245 * Returns NULL when there none.
11246 */
11247 static frame_T *
11248ses_skipframe(fr)
11249 frame_T *fr;
11250{
11251 frame_T *frc;
11252
11253 for (frc = fr; frc != NULL; frc = frc->fr_next)
11254 if (ses_do_frame(frc))
11255 break;
11256 return frc;
11257}
11258
11259/*
11260 * Return TRUE if frame "fr" has a window somewhere that we want to save in
11261 * the Session.
11262 */
11263 static int
11264ses_do_frame(fr)
11265 frame_T *fr;
11266{
11267 frame_T *frc;
11268
11269 if (fr->fr_layout == FR_LEAF)
11270 return ses_do_win(fr->fr_win);
11271 for (frc = fr->fr_child; frc != NULL; frc = frc->fr_next)
11272 if (ses_do_frame(frc))
11273 return TRUE;
11274 return FALSE;
11275}
11276
11277/*
11278 * Return non-zero if window "wp" is to be stored in the Session.
11279 */
11280 static int
11281ses_do_win(wp)
11282 win_T *wp;
11283{
11284 if (wp->w_buffer->b_fname == NULL
11285#ifdef FEAT_QUICKFIX
11286 /* When 'buftype' is "nofile" can't restore the window contents. */
11287 || bt_nofile(wp->w_buffer)
11288#endif
11289 )
11290 return (ssop_flags & SSOP_BLANK);
11291 if (wp->w_buffer->b_help)
11292 return (ssop_flags & SSOP_HELP);
11293 return TRUE;
11294}
11295
11296/*
11297 * Write commands to "fd" to restore the view of a window.
11298 * Caller must make sure 'scrolloff' is zero.
11299 */
11300 static int
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011301put_view(fd, wp, add_edit, flagp, current_arg_idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011302 FILE *fd;
11303 win_T *wp;
11304 int add_edit; /* add ":edit" command to view */
11305 unsigned *flagp; /* vop_flags or ssop_flags */
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011306 int current_arg_idx; /* current argument index of the window, use
11307 * -1 if unknown */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011308{
11309 win_T *save_curwin;
11310 int f;
11311 int do_cursor;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011312 int did_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011313
11314 /* Always restore cursor position for ":mksession". For ":mkview" only
11315 * when 'viewoptions' contains "cursor". */
11316 do_cursor = (flagp == &ssop_flags || *flagp & SSOP_CURSOR);
11317
11318 /*
11319 * Local argument list.
11320 */
11321 if (wp->w_alist == &global_alist)
11322 {
11323 if (put_line(fd, "argglobal") == FAIL)
11324 return FAIL;
11325 }
11326 else
11327 {
11328 if (ses_arglist(fd, "arglocal", &wp->w_alist->al_ga,
11329 flagp == &vop_flags
11330 || !(*flagp & SSOP_CURDIR)
11331 || wp->w_localdir != NULL, flagp) == FAIL)
11332 return FAIL;
11333 }
11334
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011335 /* Only when part of a session: restore the argument index. Some
11336 * arguments may have been deleted, check if the index is valid. */
Bram Moolenaar51f53df2010-06-26 05:25:54 +020011337 if (wp->w_arg_idx != current_arg_idx && wp->w_arg_idx < WARGCOUNT(wp)
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011338 && flagp == &ssop_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011339 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011340 if (fprintf(fd, "%ldargu", (long)wp->w_arg_idx + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011341 || put_eol(fd) == FAIL)
11342 return FAIL;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011343 did_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011344 }
11345
11346 /* Edit the file. Skip this when ":next" already did it. */
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011347 if (add_edit && (!did_next || wp->w_arg_idx_invalid))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011348 {
11349 /*
11350 * Load the file.
11351 */
11352 if (wp->w_buffer->b_ffname != NULL
11353#ifdef FEAT_QUICKFIX
11354 && !bt_nofile(wp->w_buffer)
11355#endif
11356 )
11357 {
11358 /*
11359 * Editing a file in this buffer: use ":edit file".
11360 * This may have side effects! (e.g., compressed or network file).
11361 */
11362 if (fputs("edit ", fd) < 0
11363 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
11364 return FAIL;
11365 }
11366 else
11367 {
11368 /* No file in this buffer, just make it empty. */
11369 if (put_line(fd, "enew") == FAIL)
11370 return FAIL;
11371#ifdef FEAT_QUICKFIX
11372 if (wp->w_buffer->b_ffname != NULL)
11373 {
11374 /* The buffer does have a name, but it's not a file name. */
11375 if (fputs("file ", fd) < 0
11376 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
11377 return FAIL;
11378 }
11379#endif
11380 do_cursor = FALSE;
11381 }
11382 }
11383
11384 /*
11385 * Local mappings and abbreviations.
11386 */
11387 if ((*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
11388 && makemap(fd, wp->w_buffer) == FAIL)
11389 return FAIL;
11390
11391 /*
11392 * Local options. Need to go to the window temporarily.
11393 * Store only local values when using ":mkview" and when ":mksession" is
11394 * used and 'sessionoptions' doesn't include "options".
11395 * Some folding options are always stored when "folds" is included,
11396 * otherwise the folds would not be restored correctly.
11397 */
11398 save_curwin = curwin;
11399 curwin = wp;
11400 curbuf = curwin->w_buffer;
11401 if (*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
11402 f = makeset(fd, OPT_LOCAL,
11403 flagp == &vop_flags || !(*flagp & SSOP_OPTIONS));
11404#ifdef FEAT_FOLDING
11405 else if (*flagp & SSOP_FOLDS)
11406 f = makefoldset(fd);
11407#endif
11408 else
11409 f = OK;
11410 curwin = save_curwin;
11411 curbuf = curwin->w_buffer;
11412 if (f == FAIL)
11413 return FAIL;
11414
11415#ifdef FEAT_FOLDING
11416 /*
11417 * Save Folds when 'buftype' is empty and for help files.
11418 */
11419 if ((*flagp & SSOP_FOLDS)
11420 && wp->w_buffer->b_ffname != NULL
Bram Moolenaarb1b715d2006-01-21 22:09:43 +000011421# ifdef FEAT_QUICKFIX
11422 && (*wp->w_buffer->b_p_bt == NUL || wp->w_buffer->b_help)
11423# endif
11424 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000011425 {
11426 if (put_folds(fd, wp) == FAIL)
11427 return FAIL;
11428 }
11429#endif
11430
11431 /*
11432 * Set the cursor after creating folds, since that moves the cursor.
11433 */
11434 if (do_cursor)
11435 {
11436
11437 /* Restore the cursor line in the file and relatively in the
11438 * window. Don't use "G", it changes the jumplist. */
11439 if (fprintf(fd, "let s:l = %ld - ((%ld * winheight(0) + %ld) / %ld)",
11440 (long)wp->w_cursor.lnum,
11441 (long)(wp->w_cursor.lnum - wp->w_topline),
11442 (long)wp->w_height / 2, (long)wp->w_height) < 0
11443 || put_eol(fd) == FAIL
11444 || put_line(fd, "if s:l < 1 | let s:l = 1 | endif") == FAIL
11445 || put_line(fd, "exe s:l") == FAIL
11446 || put_line(fd, "normal! zt") == FAIL
11447 || fprintf(fd, "%ld", (long)wp->w_cursor.lnum) < 0
11448 || put_eol(fd) == FAIL)
11449 return FAIL;
11450 /* Restore the cursor column and left offset when not wrapping. */
11451 if (wp->w_cursor.col == 0)
11452 {
11453 if (put_line(fd, "normal! 0") == FAIL)
11454 return FAIL;
11455 }
11456 else
11457 {
11458 if (!wp->w_p_wrap && wp->w_leftcol > 0 && wp->w_width > 0)
11459 {
11460 if (fprintf(fd,
11461 "let s:c = %ld - ((%ld * winwidth(0) + %ld) / %ld)",
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011462 (long)wp->w_virtcol + 1,
11463 (long)(wp->w_virtcol - wp->w_leftcol),
Bram Moolenaar071d4272004-06-13 20:20:40 +000011464 (long)wp->w_width / 2, (long)wp->w_width) < 0
11465 || put_eol(fd) == FAIL
11466 || put_line(fd, "if s:c > 0") == FAIL
11467 || fprintf(fd,
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011468 " exe 'normal! ' . s:c . '|zs' . %ld . '|'",
11469 (long)wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011470 || put_eol(fd) == FAIL
11471 || put_line(fd, "else") == FAIL
Bram Moolenaarfdf447b2013-02-26 17:21:29 +010011472 || fprintf(fd, " normal! 0%d|", wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011473 || put_eol(fd) == FAIL
11474 || put_line(fd, "endif") == FAIL)
11475 return FAIL;
11476 }
11477 else
11478 {
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011479 if (fprintf(fd, "normal! 0%d|", wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011480 || put_eol(fd) == FAIL)
11481 return FAIL;
11482 }
11483 }
11484 }
11485
11486 /*
11487 * Local directory.
11488 */
11489 if (wp->w_localdir != NULL)
11490 {
11491 if (fputs("lcd ", fd) < 0
11492 || ses_put_fname(fd, wp->w_localdir, flagp) == FAIL
11493 || put_eol(fd) == FAIL)
11494 return FAIL;
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011495 did_lcd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011496 }
11497
11498 return OK;
11499}
11500
11501/*
11502 * Write an argument list to the session file.
11503 * Returns FAIL if writing fails.
11504 */
11505 static int
11506ses_arglist(fd, cmd, gap, fullname, flagp)
11507 FILE *fd;
11508 char *cmd;
11509 garray_T *gap;
11510 int fullname; /* TRUE: use full path name */
11511 unsigned *flagp;
11512{
11513 int i;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011514 char_u *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011515 char_u *s;
11516
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011517 if (fputs(cmd, fd) < 0 || put_eol(fd) == FAIL)
11518 return FAIL;
11519 if (put_line(fd, "silent! argdel *") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011520 return FAIL;
11521 for (i = 0; i < gap->ga_len; ++i)
11522 {
11523 /* NULL file names are skipped (only happens when out of memory). */
11524 s = alist_name(&((aentry_T *)gap->ga_data)[i]);
11525 if (s != NULL)
11526 {
11527 if (fullname)
11528 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020011529 buf = alloc(MAXPATHL);
11530 if (buf != NULL)
11531 {
11532 (void)vim_FullName(s, buf, MAXPATHL, FALSE);
11533 s = buf;
11534 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011535 }
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011536 if (fputs("argadd ", fd) < 0
11537 || ses_put_fname(fd, s, flagp) == FAIL
11538 || put_eol(fd) == FAIL)
Bram Moolenaard9462e32011-04-11 21:35:11 +020011539 {
11540 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011541 return FAIL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011542 }
11543 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011544 }
11545 }
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011546 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011547}
11548
11549/*
11550 * Write a buffer name to the session file.
11551 * Also ends the line.
11552 * Returns FAIL if writing fails.
11553 */
11554 static int
11555ses_fname(fd, buf, flagp)
11556 FILE *fd;
11557 buf_T *buf;
11558 unsigned *flagp;
11559{
11560 char_u *name;
11561
11562 /* Use the short file name if the current directory is known at the time
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011563 * the session file will be sourced.
11564 * Don't do this for ":mkview", we don't know the current directory.
11565 * Don't do this after ":lcd", we don't keep track of what the current
11566 * directory is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011567 if (buf->b_sfname != NULL
11568 && flagp == &ssop_flags
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011569 && (ssop_flags & (SSOP_CURDIR | SSOP_SESDIR))
Bram Moolenaar8d594672009-07-01 18:18:57 +000011570#ifdef FEAT_AUTOCHDIR
11571 && !p_acd
11572#endif
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011573 && !did_lcd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011574 name = buf->b_sfname;
11575 else
11576 name = buf->b_ffname;
11577 if (ses_put_fname(fd, name, flagp) == FAIL || put_eol(fd) == FAIL)
11578 return FAIL;
11579 return OK;
11580}
11581
11582/*
11583 * Write a file name to the session file.
11584 * Takes care of the "slash" option in 'sessionoptions' and escapes special
11585 * characters.
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011586 * Returns FAIL if writing fails or out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011587 */
11588 static int
11589ses_put_fname(fd, name, flagp)
11590 FILE *fd;
11591 char_u *name;
11592 unsigned *flagp;
11593{
11594 char_u *sname;
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011595 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011596 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011597
11598 sname = home_replace_save(NULL, name);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011599 if (sname == NULL)
11600 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011601
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011602 if (*flagp & SSOP_SLASH)
11603 {
11604 /* change all backslashes to forward slashes */
11605 for (p = sname; *p != NUL; mb_ptr_adv(p))
11606 if (*p == '\\')
11607 *p = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +000011608 }
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011609
Bram Moolenaar84a05ac2013-05-06 04:24:17 +020011610 /* escape special characters */
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011611 p = vim_strsave_fnameescape(sname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011612 vim_free(sname);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011613 if (p == NULL)
11614 return FAIL;
11615
11616 /* write the result */
11617 if (fputs((char *)p, fd) < 0)
11618 retval = FAIL;
11619
11620 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011621 return retval;
11622}
11623
11624/*
11625 * ":loadview [nr]"
11626 */
11627 static void
11628ex_loadview(eap)
11629 exarg_T *eap;
11630{
11631 char_u *fname;
11632
11633 fname = get_view_file(*eap->arg);
11634 if (fname != NULL)
11635 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +000011636 do_source(fname, FALSE, DOSO_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011637 vim_free(fname);
11638 }
11639}
11640
11641/*
11642 * Get the name of the view file for the current buffer.
11643 */
11644 static char_u *
11645get_view_file(c)
11646 int c;
11647{
11648 int len = 0;
11649 char_u *p, *s;
11650 char_u *retval;
11651 char_u *sname;
11652
11653 if (curbuf->b_ffname == NULL)
11654 {
11655 EMSG(_(e_noname));
11656 return NULL;
11657 }
11658 sname = home_replace_save(NULL, curbuf->b_ffname);
11659 if (sname == NULL)
11660 return NULL;
11661
11662 /*
11663 * We want a file name without separators, because we're not going to make
11664 * a directory.
11665 * "normal" path separator -> "=+"
11666 * "=" -> "=="
11667 * ":" path separator -> "=-"
11668 */
11669 for (p = sname; *p; ++p)
11670 if (*p == '=' || vim_ispathsep(*p))
11671 ++len;
11672 retval = alloc((unsigned)(STRLEN(sname) + len + STRLEN(p_vdir) + 9));
11673 if (retval != NULL)
11674 {
11675 STRCPY(retval, p_vdir);
11676 add_pathsep(retval);
11677 s = retval + STRLEN(retval);
11678 for (p = sname; *p; ++p)
11679 {
11680 if (*p == '=')
11681 {
11682 *s++ = '=';
11683 *s++ = '=';
11684 }
11685 else if (vim_ispathsep(*p))
11686 {
11687 *s++ = '=';
Bram Moolenaare60acc12011-05-10 16:41:25 +020011688#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011689 if (*p == ':')
11690 *s++ = '-';
11691 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000011692#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000011693 *s++ = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +000011694 }
11695 else
11696 *s++ = *p;
11697 }
11698 *s++ = '=';
11699 *s++ = c;
11700 STRCPY(s, ".vim");
11701 }
11702
11703 vim_free(sname);
11704 return retval;
11705}
11706
11707#endif /* FEAT_SESSION */
11708
11709/*
11710 * Write end-of-line character(s) for ":mkexrc", ":mkvimrc" and ":mksession".
11711 * Return FAIL for a write error.
11712 */
11713 int
11714put_eol(fd)
11715 FILE *fd;
11716{
11717 if (
11718#ifdef USE_CRNL
11719 (
11720# ifdef MKSESSION_NL
11721 !mksession_nl &&
11722# endif
11723 (putc('\r', fd) < 0)) ||
11724#endif
11725 (putc('\n', fd) < 0))
11726 return FAIL;
11727 return OK;
11728}
11729
11730/*
11731 * Write a line to "fd".
11732 * Return FAIL for a write error.
11733 */
11734 int
11735put_line(fd, s)
11736 FILE *fd;
11737 char *s;
11738{
11739 if (fputs(s, fd) < 0 || put_eol(fd) == FAIL)
11740 return FAIL;
11741 return OK;
11742}
11743
11744#ifdef FEAT_VIMINFO
11745/*
11746 * ":rviminfo" and ":wviminfo".
11747 */
11748 static void
11749ex_viminfo(eap)
11750 exarg_T *eap;
11751{
11752 char_u *save_viminfo;
11753
11754 save_viminfo = p_viminfo;
11755 if (*p_viminfo == NUL)
11756 p_viminfo = (char_u *)"'100";
11757 if (eap->cmdidx == CMD_rviminfo)
11758 {
Bram Moolenaard812df62008-11-09 12:46:09 +000011759 if (read_viminfo(eap->arg, VIF_WANT_INFO | VIF_WANT_MARKS
11760 | (eap->forceit ? VIF_FORCEIT : 0)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011761 EMSG(_("E195: Cannot open viminfo file for reading"));
11762 }
11763 else
11764 write_viminfo(eap->arg, eap->forceit);
11765 p_viminfo = save_viminfo;
11766}
11767#endif
11768
11769#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +000011770/*
Bram Moolenaard9462e32011-04-11 21:35:11 +020011771 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +000011772 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +000011773 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011774 void
11775dialog_msg(buff, format, fname)
11776 char_u *buff;
11777 char *format;
11778 char_u *fname;
11779{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011780 if (fname == NULL)
11781 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +020011782 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011783}
11784#endif
11785
11786/*
11787 * ":behave {mswin,xterm}"
11788 */
11789 static void
11790ex_behave(eap)
11791 exarg_T *eap;
11792{
11793 if (STRCMP(eap->arg, "mswin") == 0)
11794 {
11795 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
11796 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
11797 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
11798 set_option_value((char_u *)"keymodel", 0L,
11799 (char_u *)"startsel,stopsel", 0);
11800 }
11801 else if (STRCMP(eap->arg, "xterm") == 0)
11802 {
11803 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
11804 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
11805 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
11806 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
11807 }
11808 else
11809 EMSG2(_(e_invarg2), eap->arg);
11810}
11811
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010011812#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
11813/*
11814 * Function given to ExpandGeneric() to obtain the possible arguments of the
11815 * ":behave {mswin,xterm}" command.
11816 */
11817 char_u *
11818get_behave_arg(xp, idx)
11819 expand_T *xp UNUSED;
11820 int idx;
11821{
11822 if (idx == 0)
11823 return (char_u *)"mswin";
11824 if (idx == 1)
11825 return (char_u *)"xterm";
11826 return NULL;
11827}
11828#endif
11829
Bram Moolenaar071d4272004-06-13 20:20:40 +000011830#ifdef FEAT_AUTOCMD
11831static int filetype_detect = FALSE;
11832static int filetype_plugin = FALSE;
11833static int filetype_indent = FALSE;
11834
11835/*
11836 * ":filetype [plugin] [indent] {on,off,detect}"
11837 * on: Load the filetype.vim file to install autocommands for file types.
11838 * off: Load the ftoff.vim file to remove all autocommands for file types.
11839 * plugin on: load filetype.vim and ftplugin.vim
11840 * plugin off: load ftplugof.vim
11841 * indent on: load filetype.vim and indent.vim
11842 * indent off: load indoff.vim
11843 */
11844 static void
11845ex_filetype(eap)
11846 exarg_T *eap;
11847{
11848 char_u *arg = eap->arg;
11849 int plugin = FALSE;
11850 int indent = FALSE;
11851
11852 if (*eap->arg == NUL)
11853 {
11854 /* Print current status. */
11855 smsg((char_u *)"filetype detection:%s plugin:%s indent:%s",
11856 filetype_detect ? "ON" : "OFF",
11857 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
11858 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
11859 return;
11860 }
11861
11862 /* Accept "plugin" and "indent" in any order. */
11863 for (;;)
11864 {
11865 if (STRNCMP(arg, "plugin", 6) == 0)
11866 {
11867 plugin = TRUE;
11868 arg = skipwhite(arg + 6);
11869 continue;
11870 }
11871 if (STRNCMP(arg, "indent", 6) == 0)
11872 {
11873 indent = TRUE;
11874 arg = skipwhite(arg + 6);
11875 continue;
11876 }
11877 break;
11878 }
11879 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
11880 {
11881 if (*arg == 'o' || !filetype_detect)
11882 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011883 source_runtime((char_u *)FILETYPE_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011884 filetype_detect = TRUE;
11885 if (plugin)
11886 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011887 source_runtime((char_u *)FTPLUGIN_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011888 filetype_plugin = TRUE;
11889 }
11890 if (indent)
11891 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011892 source_runtime((char_u *)INDENT_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011893 filetype_indent = TRUE;
11894 }
11895 }
11896 if (*arg == 'd')
11897 {
11898 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE);
Bram Moolenaara3227e22006-03-08 21:32:40 +000011899 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011900 }
11901 }
11902 else if (STRCMP(arg, "off") == 0)
11903 {
11904 if (plugin || indent)
11905 {
11906 if (plugin)
11907 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011908 source_runtime((char_u *)FTPLUGOF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011909 filetype_plugin = FALSE;
11910 }
11911 if (indent)
11912 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011913 source_runtime((char_u *)INDOFF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011914 filetype_indent = FALSE;
11915 }
11916 }
11917 else
11918 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011919 source_runtime((char_u *)FTOFF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011920 filetype_detect = FALSE;
11921 }
11922 }
11923 else
11924 EMSG2(_(e_invarg2), arg);
11925}
11926
11927/*
11928 * ":setfiletype {name}"
11929 */
11930 static void
11931ex_setfiletype(eap)
11932 exarg_T *eap;
11933{
11934 if (!did_filetype)
11935 set_option_value((char_u *)"filetype", 0L, eap->arg, OPT_LOCAL);
11936}
11937#endif
11938
Bram Moolenaar071d4272004-06-13 20:20:40 +000011939 static void
11940ex_digraphs(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +000011941 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011942{
11943#ifdef FEAT_DIGRAPHS
11944 if (*eap->arg != NUL)
11945 putdigraph(eap->arg);
11946 else
11947 listdigraphs();
11948#else
11949 EMSG(_("E196: No digraphs in this version"));
11950#endif
11951}
11952
11953 static void
11954ex_set(eap)
11955 exarg_T *eap;
11956{
11957 int flags = 0;
11958
11959 if (eap->cmdidx == CMD_setlocal)
11960 flags = OPT_LOCAL;
11961 else if (eap->cmdidx == CMD_setglobal)
11962 flags = OPT_GLOBAL;
11963#if defined(FEAT_EVAL) && defined(FEAT_AUTOCMD) && defined(FEAT_BROWSE)
11964 if (cmdmod.browse && flags == 0)
11965 ex_options(eap);
11966 else
11967#endif
11968 (void)do_set(eap->arg, flags);
11969}
11970
11971#ifdef FEAT_SEARCH_EXTRA
11972/*
11973 * ":nohlsearch"
11974 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011975 static void
11976ex_nohlsearch(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +000011977 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011978{
Bram Moolenaar8050efa2013-11-08 04:30:20 +010011979 SET_NO_HLSEARCH(TRUE);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +000011980 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011981}
11982
11983/*
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011984 * ":[N]match {group} {pattern}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000011985 * Sets nextcmd to the start of the next command, if any. Also called when
11986 * skipping commands to find the next command.
11987 */
11988 static void
11989ex_match(eap)
11990 exarg_T *eap;
11991{
11992 char_u *p;
Bram Moolenaar52d36c82007-08-11 14:00:30 +000011993 char_u *g = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011994 char_u *end;
11995 int c;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011996 int id;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011997
11998 if (eap->line2 <= 3)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011999 id = eap->line2;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000012000 else
12001 {
12002 EMSG(e_invcmd);
12003 return;
12004 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012005
12006 /* First clear any old pattern. */
12007 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012008 match_delete(curwin, id, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012009
12010 if (ends_excmd(*eap->arg))
12011 end = eap->arg;
12012 else if ((STRNICMP(eap->arg, "none", 4) == 0
12013 && (vim_iswhite(eap->arg[4]) || ends_excmd(eap->arg[4]))))
12014 end = eap->arg + 4;
12015 else
12016 {
12017 p = skiptowhite(eap->arg);
12018 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012019 g = vim_strnsave(eap->arg, (int)(p - eap->arg));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012020 p = skipwhite(p);
12021 if (*p == NUL)
12022 {
12023 /* There must be two arguments. */
12024 EMSG2(_(e_invarg2), eap->arg);
12025 return;
12026 }
12027 end = skip_regexp(p + 1, *p, TRUE, NULL);
12028 if (!eap->skip)
12029 {
12030 if (*end != NUL && !ends_excmd(*skipwhite(end + 1)))
12031 {
12032 eap->errmsg = e_trailing;
12033 return;
12034 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000012035 if (*end != *p)
12036 {
12037 EMSG2(_(e_invarg2), p);
12038 return;
12039 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012040
12041 c = *end;
12042 *end = NUL;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012043 match_add(curwin, g, p + 1, 10, id, NULL);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012044 vim_free(g);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012045 *end = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012046 }
12047 }
12048 eap->nextcmd = find_nextcmd(end);
12049}
12050#endif
12051
12052#ifdef FEAT_CRYPT
12053/*
12054 * ":X": Get crypt key
12055 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012056 static void
12057ex_X(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +000012058 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012059{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +010012060 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +020012061 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012062}
12063#endif
12064
12065#ifdef FEAT_FOLDING
12066 static void
12067ex_fold(eap)
12068 exarg_T *eap;
12069{
12070 if (foldManualAllowed(TRUE))
12071 foldCreate(eap->line1, eap->line2);
12072}
12073
12074 static void
12075ex_foldopen(eap)
12076 exarg_T *eap;
12077{
12078 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
12079 eap->forceit, FALSE);
12080}
12081
12082 static void
12083ex_folddo(eap)
12084 exarg_T *eap;
12085{
12086 linenr_T lnum;
12087
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020012088#ifdef FEAT_CLIPBOARD
12089 start_global_changes();
12090#endif
12091
Bram Moolenaar071d4272004-06-13 20:20:40 +000012092 /* First set the marks for all lines closed/open. */
12093 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
12094 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
12095 ml_setmarked(lnum);
12096
12097 /* Execute the command on the marked lines. */
12098 global_exe(eap->arg);
12099 ml_clearmarked(); /* clear rest of the marks */
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020012100#ifdef FEAT_CLIPBOARD
12101 end_global_changes();
12102#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012103}
12104#endif