blob: a59bbd1bbd3e5fd8f451343a130d6cabad3e5a2a [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 Moolenaarf1d6ccf2014-12-08 04:16:44 +01002133 /* ea.addr_type for user commands is set by find_ucmd */
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01002134 if (!IS_USER_CMDIDX(ea.cmdidx))
2135 {
2136 if (ea.cmdidx != CMD_SIZE)
2137 ea.addr_type = cmdnames[(int)ea.cmdidx].cmd_addr_type;
2138 else
2139 ea.addr_type = ADDR_LINES;
2140
2141#ifdef FEAT_WINDOWS
2142 /* :wincmd range depends on the argument. */
Bram Moolenaar164f3262015-01-14 21:22:01 +01002143 if (ea.cmdidx == CMD_wincmd && p != NULL)
2144 get_wincmd_addr_type(skipwhite(p), &ea);
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01002145#endif
2146 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002147
Bram Moolenaar071d4272004-06-13 20:20:40 +00002148 /* repeat for all ',' or ';' separated addresses */
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01002149 ea.cmd = cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002150 for (;;)
2151 {
2152 ea.line1 = ea.line2;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002153 switch (ea.addr_type)
2154 {
2155 case ADDR_LINES:
2156 /* default is current line number */
2157 ea.line2 = curwin->w_cursor.lnum;
2158 break;
2159 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01002160 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002161 ea.line2 = lnum;
2162 break;
2163 case ADDR_ARGUMENTS:
2164 ea.line2 = curwin->w_arg_idx + 1;
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01002165 if (ea.line2 > ARGCOUNT)
2166 ea.line2 = ARGCOUNT;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002167 break;
2168 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002169 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002170 ea.line2 = curbuf->b_fnum;
2171 break;
2172 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01002173 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002174 ea.line2 = lnum;
2175 break;
2176 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177 ea.cmd = skipwhite(ea.cmd);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002178 lnum = get_address(&ea.cmd, ea.addr_type, ea.skip, ea.addr_count == 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002179 if (ea.cmd == NULL) /* error detected */
2180 goto doend;
2181 if (lnum == MAXLNUM)
2182 {
2183 if (*ea.cmd == '%') /* '%' - all lines */
2184 {
2185 ++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 Moolenaar84c8e5a2015-01-14 15:47:36 +01002193 {
2194 buf_T *buf = firstbuf;
2195
2196 while (buf->b_next != NULL
2197 && buf->b_ml.ml_mfp == NULL)
2198 buf = buf->b_next;
2199 ea.line1 = buf->b_fnum;
2200 buf = lastbuf;
2201 while (buf->b_prev != NULL
2202 && buf->b_ml.ml_mfp == NULL)
2203 buf = buf->b_prev;
2204 ea.line2 = buf->b_fnum;
2205 break;
2206 }
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002207 case ADDR_BUFFERS:
Bram Moolenaar4d84d932014-11-30 14:50:16 +01002208 ea.line1 = firstbuf->b_fnum;
2209 ea.line2 = lastbuf->b_fnum;
2210 break;
2211 case ADDR_WINDOWS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002212 case ADDR_TABS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002213 if (IS_USER_CMDIDX(ea.cmdidx))
2214 {
2215 ea.line1 = 1;
2216 ea.line2 = ea.addr_type == ADDR_WINDOWS
2217 ? LAST_WIN_NR : LAST_TAB_NR;
2218 }
2219 else
2220 {
2221 /* there is no Vim command which uses '%' and
2222 * ADDR_WINDOWS or ADDR_TABS */
2223 errormsg = (char_u *)_(e_invrange);
2224 goto doend;
2225 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002226 break;
2227 case ADDR_ARGUMENTS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002228 if (ARGCOUNT == 0)
2229 ea.line1 = ea.line2 = 0;
2230 else
2231 {
2232 ea.line1 = 1;
2233 ea.line2 = ARGCOUNT;
2234 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002235 break;
2236 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002237 ++ea.addr_count;
2238 }
2239 /* '*' - visual area */
2240 else if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
2241 {
2242 pos_T *fp;
2243
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002244 if (ea.addr_type != ADDR_LINES)
2245 {
2246 errormsg = (char_u *)_(e_invrange);
2247 goto doend;
2248 }
2249
Bram Moolenaar071d4272004-06-13 20:20:40 +00002250 ++ea.cmd;
2251 if (!ea.skip)
2252 {
2253 fp = getmark('<', FALSE);
2254 if (check_mark(fp) == FAIL)
2255 goto doend;
2256 ea.line1 = fp->lnum;
2257 fp = getmark('>', FALSE);
2258 if (check_mark(fp) == FAIL)
2259 goto doend;
2260 ea.line2 = fp->lnum;
2261 ++ea.addr_count;
2262 }
2263 }
2264 }
2265 else
2266 ea.line2 = lnum;
2267 ea.addr_count++;
2268
2269 if (*ea.cmd == ';')
2270 {
2271 if (!ea.skip)
2272 curwin->w_cursor.lnum = ea.line2;
2273 }
2274 else if (*ea.cmd != ',')
2275 break;
2276 ++ea.cmd;
2277 }
2278
2279 /* One address given: set start and end lines */
2280 if (ea.addr_count == 1)
2281 {
2282 ea.line1 = ea.line2;
2283 /* ... but only implicit: really no address given */
2284 if (lnum == MAXLNUM)
2285 ea.addr_count = 0;
2286 }
2287
2288 /* Don't leave the cursor on an illegal line (caused by ';') */
2289 check_cursor_lnum();
2290
2291/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002292 * 5. Parse the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002293 */
2294
2295 /*
2296 * Skip ':' and any white space
2297 */
2298 ea.cmd = skipwhite(ea.cmd);
2299 while (*ea.cmd == ':')
2300 ea.cmd = skipwhite(ea.cmd + 1);
2301
2302 /*
2303 * If we got a line, but no command, then go to the line.
2304 * If we find a '|' or '\n' we set ea.nextcmd.
2305 */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002306 if (*ea.cmd == NUL || *ea.cmd == '"'
2307 || (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308 {
2309 /*
2310 * strange vi behaviour:
2311 * ":3" jumps to line 3
2312 * ":3|..." prints line 3
2313 * ":|" prints current line
2314 */
2315 if (ea.skip) /* skip this if inside :if */
2316 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00002317 if (*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318 {
2319 ea.cmdidx = CMD_print;
2320 ea.argt = RANGE+COUNT+TRLBAR;
2321 if ((errormsg = invalid_range(&ea)) == NULL)
2322 {
2323 correct_range(&ea);
2324 ex_print(&ea);
2325 }
2326 }
2327 else if (ea.addr_count != 0)
2328 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002329 if (ea.line2 > curbuf->b_ml.ml_line_count)
2330 {
2331 /* With '-' in 'cpoptions' a line number past the file is an
2332 * error, otherwise put it at the end of the file. */
2333 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
2334 ea.line2 = -1;
2335 else
2336 ea.line2 = curbuf->b_ml.ml_line_count;
2337 }
2338
2339 if (ea.line2 < 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002340 errormsg = (char_u *)_(e_invrange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341 else
2342 {
2343 if (ea.line2 == 0)
2344 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345 else
2346 curwin->w_cursor.lnum = ea.line2;
2347 beginline(BL_SOL | BL_FIX);
2348 }
2349 }
2350 goto doend;
2351 }
2352
Bram Moolenaard5005162014-08-22 23:05:54 +02002353#ifdef FEAT_AUTOCMD
2354 /* If this looks like an undefined user command and there are CmdUndefined
2355 * autocommands defined, trigger the matching autocommands. */
2356 if (p != NULL && ea.cmdidx == CMD_SIZE && !ea.skip
2357 && ASCII_ISUPPER(*ea.cmd)
2358 && has_cmdundefined())
2359 {
Bram Moolenaard5005162014-08-22 23:05:54 +02002360 int ret;
2361
Bram Moolenaar5a31b462014-08-23 14:16:20 +02002362 p = ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02002363 while (ASCII_ISALNUM(*p))
2364 ++p;
Bram Moolenaar120f4a82014-09-09 12:22:06 +02002365 p = vim_strnsave(ea.cmd, (int)(p - ea.cmd));
Bram Moolenaard5005162014-08-22 23:05:54 +02002366 ret = apply_autocmds(EVENT_CMDUNDEFINED, p, p, TRUE, NULL);
2367 vim_free(p);
2368 if (ret && !aborting())
2369 p = find_command(&ea, NULL);
2370 }
2371#endif
2372
Bram Moolenaar071d4272004-06-13 20:20:40 +00002373#ifdef FEAT_USR_CMDS
2374 if (p == NULL)
2375 {
2376 if (!ea.skip)
2377 errormsg = (char_u *)_("E464: Ambiguous use of user-defined command");
2378 goto doend;
2379 }
2380 /* Check for wrong commands. */
2381 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78)
2382 {
2383 errormsg = uc_fun_cmd();
2384 goto doend;
2385 }
2386#endif
2387 if (ea.cmdidx == CMD_SIZE)
2388 {
2389 if (!ea.skip)
2390 {
2391 STRCPY(IObuff, _("E492: Not an editor command"));
2392 if (!sourcing)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002393 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002394 errormsg = IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02002395 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002396 }
2397 goto doend;
2398 }
2399
Bram Moolenaar958636c2014-10-21 20:01:58 +02002400 ni = (!IS_USER_CMDIDX(ea.cmdidx)
2401 && (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00002402#ifdef HAVE_EX_SCRIPT_NI
2403 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
2404#endif
2405 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406
2407#ifndef FEAT_EVAL
2408 /*
2409 * When the expression evaluation is disabled, recognize the ":if" and
2410 * ":endif" commands and ignore everything in between it.
2411 */
2412 if (ea.cmdidx == CMD_if)
2413 ++if_level;
2414 if (if_level)
2415 {
2416 if (ea.cmdidx == CMD_endif)
2417 --if_level;
2418 goto doend;
2419 }
2420
2421#endif
2422
Bram Moolenaar196b3b02008-06-20 16:51:41 +00002423 /* forced commands */
2424 if (*p == '!' && ea.cmdidx != CMD_substitute
2425 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002426 {
2427 ++p;
2428 ea.forceit = TRUE;
2429 }
2430 else
2431 ea.forceit = FALSE;
2432
2433/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01002434 * 6. Parse arguments.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02002436 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002437 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438
2439 if (!ea.skip)
2440 {
2441#ifdef HAVE_SANDBOX
2442 if (sandbox != 0 && !(ea.argt & SBOXOK))
2443 {
2444 /* Command not allowed in sandbox. */
2445 errormsg = (char_u *)_(e_sandbox);
2446 goto doend;
2447 }
2448#endif
2449 if (!curbuf->b_p_ma && (ea.argt & MODIFY))
2450 {
2451 /* Command not allowed in non-'modifiable' buffer */
2452 errormsg = (char_u *)_(e_modifiable);
2453 goto doend;
2454 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002455
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002456 if (text_locked() && !(ea.argt & CMDWIN)
Bram Moolenaar958636c2014-10-21 20:01:58 +02002457 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002458 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002459 /* Command not allowed when editing the command line. */
2460#ifdef FEAT_CMDWIN
2461 if (cmdwin_type != 0)
2462 errormsg = (char_u *)_(e_cmdwin);
2463 else
2464#endif
2465 errormsg = (char_u *)_(e_secure);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466 goto doend;
2467 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002468#ifdef FEAT_AUTOCMD
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002469 /* Disallow editing another buffer when "curbuf_lock" is set.
2470 * Do allow ":edit" (check for argument later).
2471 * Do allow ":checktime" (it's postponed). */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002472 if (!(ea.argt & CMDWIN)
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002473 && ea.cmdidx != CMD_edit
2474 && ea.cmdidx != CMD_checktime
Bram Moolenaar958636c2014-10-21 20:01:58 +02002475 && !IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002476 && curbuf_locked())
2477 goto doend;
2478#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002479
2480 if (!ni && !(ea.argt & RANGE) && ea.addr_count > 0)
2481 {
2482 /* no range allowed */
2483 errormsg = (char_u *)_(e_norange);
2484 goto doend;
2485 }
2486 }
2487
2488 if (!ni && !(ea.argt & BANG) && ea.forceit) /* no <!> allowed */
2489 {
2490 errormsg = (char_u *)_(e_nobang);
2491 goto doend;
2492 }
2493
2494 /*
2495 * Don't complain about the range if it is not used
2496 * (could happen if line_count is accidentally set to 0).
2497 */
2498 if (!ea.skip && !ni)
2499 {
2500 /*
2501 * If the range is backwards, ask for confirmation and, if given, swap
2502 * ea.line1 & ea.line2 so it's forwards again.
2503 * When global command is busy, don't ask, will fail below.
2504 */
2505 if (!global_busy && ea.line1 > ea.line2)
2506 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002507 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002508 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002509 if (sourcing || exmode_active)
2510 {
2511 errormsg = (char_u *)_("E493: Backwards range given");
2512 goto doend;
2513 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514 if (ask_yesno((char_u *)
2515 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002516 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002517 }
2518 lnum = ea.line1;
2519 ea.line1 = ea.line2;
2520 ea.line2 = lnum;
2521 }
2522 if ((errormsg = invalid_range(&ea)) != NULL)
2523 goto doend;
2524 }
2525
2526 if ((ea.argt & NOTADR) && ea.addr_count == 0) /* default is 1, not cursor */
2527 ea.line2 = 1;
2528
2529 correct_range(&ea);
2530
2531#ifdef FEAT_FOLDING
2532 if (((ea.argt & WHOLEFOLD) || ea.addr_count >= 2) && !global_busy)
2533 {
2534 /* Put the first line at the start of a closed fold, put the last line
2535 * at the end of a closed fold. */
2536 (void)hasFolding(ea.line1, &ea.line1, NULL);
2537 (void)hasFolding(ea.line2, NULL, &ea.line2);
2538 }
2539#endif
2540
2541#ifdef FEAT_QUICKFIX
2542 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002543 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544 * option here, so things like % get expanded.
2545 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002546 p = replace_makeprg(&ea, p, cmdlinep);
2547 if (p == NULL)
2548 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002549#endif
2550
2551 /*
2552 * Skip to start of argument.
2553 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2554 */
2555 if (ea.cmdidx == CMD_bang)
2556 ea.arg = p;
2557 else
2558 ea.arg = skipwhite(p);
2559
2560 /*
2561 * Check for "++opt=val" argument.
2562 * Must be first, allow ":w ++enc=utf8 !cmd"
2563 */
2564 if (ea.argt & ARGOPT)
2565 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2566 if (getargopt(&ea) == FAIL && !ni)
2567 {
2568 errormsg = (char_u *)_(e_invarg);
2569 goto doend;
2570 }
2571
2572 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2573 {
2574 if (*ea.arg == '>') /* append */
2575 {
2576 if (*++ea.arg != '>') /* typed wrong */
2577 {
2578 errormsg = (char_u *)_("E494: Use w or w>>");
2579 goto doend;
2580 }
2581 ea.arg = skipwhite(ea.arg + 1);
2582 ea.append = TRUE;
2583 }
2584 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
2585 {
2586 ++ea.arg;
2587 ea.usefilter = TRUE;
2588 }
2589 }
2590
2591 if (ea.cmdidx == CMD_read)
2592 {
2593 if (ea.forceit)
2594 {
2595 ea.usefilter = TRUE; /* :r! filter if ea.forceit */
2596 ea.forceit = FALSE;
2597 }
2598 else if (*ea.arg == '!') /* :r !filter */
2599 {
2600 ++ea.arg;
2601 ea.usefilter = TRUE;
2602 }
2603 }
2604
2605 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2606 {
2607 ea.amount = 1;
2608 while (*ea.arg == *ea.cmd) /* count number of '>' or '<' */
2609 {
2610 ++ea.arg;
2611 ++ea.amount;
2612 }
2613 ea.arg = skipwhite(ea.arg);
2614 }
2615
2616 /*
2617 * Check for "+command" argument, before checking for next command.
2618 * Don't do this for ":read !cmd" and ":write !cmd".
2619 */
2620 if ((ea.argt & EDITCMD) && !ea.usefilter)
2621 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2622
2623 /*
2624 * Check for '|' to separate commands and '"' to start comments.
2625 * Don't do this for ":read !cmd" and ":write !cmd".
2626 */
2627 if ((ea.argt & TRLBAR) && !ea.usefilter)
2628 separate_nextcmd(&ea);
2629
2630 /*
2631 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002632 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2633 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002634 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002635 else if (ea.cmdidx == CMD_bang
2636 || ea.cmdidx == CMD_global
2637 || ea.cmdidx == CMD_vglobal
2638 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639 {
2640 for (p = ea.arg; *p; ++p)
2641 {
2642 /* Remove one backslash before a newline, so that it's possible to
2643 * pass a newline to the shell and also a newline that is preceded
2644 * with a backslash. This makes it impossible to end a shell
2645 * command in a backslash, but that doesn't appear useful.
2646 * Halving the number of backslashes is incompatible with previous
2647 * versions. */
2648 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002649 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650 else if (*p == '\n')
2651 {
2652 ea.nextcmd = p + 1;
2653 *p = NUL;
2654 break;
2655 }
2656 }
2657 }
2658
2659 if ((ea.argt & DFLALL) && ea.addr_count == 0)
2660 {
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002661 buf_T *buf;
2662
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663 ea.line1 = 1;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002664 switch (ea.addr_type)
2665 {
2666 case ADDR_LINES:
2667 ea.line2 = curbuf->b_ml.ml_line_count;
2668 break;
2669 case ADDR_LOADED_BUFFERS:
2670 buf = firstbuf;
2671 while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL)
2672 buf = buf->b_next;
2673 ea.line1 = buf->b_fnum;
2674 buf = lastbuf;
2675 while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL)
2676 buf = buf->b_prev;
2677 ea.line2 = buf->b_fnum;
2678 break;
2679 case ADDR_BUFFERS:
2680 ea.line1 = firstbuf->b_fnum;
2681 ea.line2 = lastbuf->b_fnum;
2682 break;
2683 case ADDR_WINDOWS:
2684 ea.line2 = LAST_WIN_NR;
2685 break;
2686 case ADDR_TABS:
2687 ea.line2 = LAST_TAB_NR;
2688 break;
2689 case ADDR_ARGUMENTS:
2690 if (ARGCOUNT == 0)
2691 ea.line1 = ea.line2 = 0;
2692 else
2693 ea.line2 = ARGCOUNT;
2694 break;
2695 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696 }
2697
2698 /* accept numbered register only when no count allowed (:put) */
2699 if ( (ea.argt & REGSTR)
2700 && *ea.arg != NUL
Bram Moolenaar958636c2014-10-21 20:01:58 +02002701 /* Do not allow register = for user commands */
2702 && (!IS_USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002703 && !((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)))
2704 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002705#ifndef FEAT_CLIPBOARD
2706 /* check these explicitly for a more specific error message */
2707 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002709 errormsg = (char_u *)_(e_invalidreg);
2710 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002711 }
2712#endif
Bram Moolenaar958636c2014-10-21 20:01:58 +02002713 if (valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2714 && !IS_USER_CMDIDX(ea.cmdidx))))
Bram Moolenaar85de2062011-05-05 14:26:41 +02002715 {
2716 ea.regname = *ea.arg++;
2717#ifdef FEAT_EVAL
2718 /* for '=' register: accept the rest of the line as an expression */
2719 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2720 {
2721 set_expr_line(vim_strsave(ea.arg));
2722 ea.arg += STRLEN(ea.arg);
2723 }
2724#endif
2725 ea.arg = skipwhite(ea.arg);
2726 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727 }
2728
2729 /*
2730 * Check for a count. When accepting a BUFNAME, don't use "123foo" as a
2731 * count, it's a buffer name.
2732 */
2733 if ((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)
2734 && (!(ea.argt & BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
2735 || vim_iswhite(*p)))
2736 {
2737 n = getdigits(&ea.arg);
2738 ea.arg = skipwhite(ea.arg);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002739 if (n <= 0 && !ni && (ea.argt & ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002740 {
2741 errormsg = (char_u *)_(e_zerocount);
2742 goto doend;
2743 }
2744 if (ea.argt & NOTADR) /* e.g. :buffer 2, :sleep 3 */
2745 {
2746 ea.line2 = n;
2747 if (ea.addr_count == 0)
2748 ea.addr_count = 1;
2749 }
2750 else
2751 {
2752 ea.line1 = ea.line2;
2753 ea.line2 += n - 1;
2754 ++ea.addr_count;
2755 /*
2756 * Be vi compatible: no error message for out of range.
2757 */
2758 if (ea.line2 > curbuf->b_ml.ml_line_count)
2759 ea.line2 = curbuf->b_ml.ml_line_count;
2760 }
2761 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002762
2763 /*
2764 * Check for flags: 'l', 'p' and '#'.
2765 */
2766 if (ea.argt & EXFLAGS)
2767 get_flags(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002768 /* no arguments allowed */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002769 if (!ni && !(ea.argt & EXTRA) && *ea.arg != NUL
Bram Moolenaara2031822006-03-07 22:29:51 +00002770 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002771 {
2772 errormsg = (char_u *)_(e_trailing);
2773 goto doend;
2774 }
2775
2776 if (!ni && (ea.argt & NEEDARG) && *ea.arg == NUL)
2777 {
2778 errormsg = (char_u *)_(e_argreq);
2779 goto doend;
2780 }
2781
2782#ifdef FEAT_EVAL
2783 /*
2784 * Skip the command when it's not going to be executed.
2785 * The commands like :if, :endif, etc. always need to be executed.
2786 * Also make an exception for commands that handle a trailing command
2787 * themselves.
2788 */
2789 if (ea.skip)
2790 {
2791 switch (ea.cmdidx)
2792 {
2793 /* commands that need evaluation */
2794 case CMD_while:
2795 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002796 case CMD_for:
2797 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002798 case CMD_if:
2799 case CMD_elseif:
2800 case CMD_else:
2801 case CMD_endif:
2802 case CMD_try:
2803 case CMD_catch:
2804 case CMD_finally:
2805 case CMD_endtry:
2806 case CMD_function:
2807 break;
2808
2809 /* Commands that handle '|' themselves. Check: A command should
2810 * either have the TRLBAR flag, appear in this list or appear in
2811 * the list at ":help :bar". */
2812 case CMD_aboveleft:
2813 case CMD_and:
2814 case CMD_belowright:
2815 case CMD_botright:
2816 case CMD_browse:
2817 case CMD_call:
2818 case CMD_confirm:
2819 case CMD_delfunction:
2820 case CMD_djump:
2821 case CMD_dlist:
2822 case CMD_dsearch:
2823 case CMD_dsplit:
2824 case CMD_echo:
2825 case CMD_echoerr:
2826 case CMD_echomsg:
2827 case CMD_echon:
2828 case CMD_execute:
2829 case CMD_help:
2830 case CMD_hide:
2831 case CMD_ijump:
2832 case CMD_ilist:
2833 case CMD_isearch:
2834 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002835 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002836 case CMD_keepjumps:
2837 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01002838 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002839 case CMD_leftabove:
2840 case CMD_let:
2841 case CMD_lockmarks:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002842 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002843 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002844 case CMD_mzscheme:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01002845 case CMD_noautocmd:
2846 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002847 case CMD_perl:
2848 case CMD_psearch:
2849 case CMD_python:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002850 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002851 case CMD_python3:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852 case CMD_return:
2853 case CMD_rightbelow:
2854 case CMD_ruby:
2855 case CMD_silent:
2856 case CMD_smagic:
2857 case CMD_snomagic:
2858 case CMD_substitute:
2859 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002860 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002861 case CMD_tcl:
2862 case CMD_throw:
2863 case CMD_tilde:
2864 case CMD_topleft:
2865 case CMD_unlet:
2866 case CMD_verbose:
2867 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002868 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002869 break;
2870
2871 default: goto doend;
2872 }
2873 }
2874#endif
2875
2876 if (ea.argt & XFILE)
2877 {
2878 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2879 goto doend;
2880 }
2881
2882#ifdef FEAT_LISTCMDS
2883 /*
2884 * Accept buffer name. Cannot be used at the same time with a buffer
2885 * number. Don't do this for a user command.
2886 */
2887 if ((ea.argt & BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
Bram Moolenaar958636c2014-10-21 20:01:58 +02002888 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002889 {
2890 /*
2891 * :bdelete, :bwipeout and :bunload take several arguments, separated
2892 * by spaces: find next space (skipping over escaped characters).
2893 * The others take one argument: ignore trailing spaces.
2894 */
2895 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2896 || ea.cmdidx == CMD_bunload)
2897 p = skiptowhite_esc(ea.arg);
2898 else
2899 {
2900 p = ea.arg + STRLEN(ea.arg);
2901 while (p > ea.arg && vim_iswhite(p[-1]))
2902 --p;
2903 }
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002904 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & BUFUNL) != 0,
2905 FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002906 if (ea.line2 < 0) /* failed */
2907 goto doend;
2908 ea.addr_count = 1;
2909 ea.arg = skipwhite(p);
2910 }
2911#endif
2912
2913/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01002914 * 7. Switch on command name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002915 *
2916 * The "ea" structure holds the arguments that can be used.
2917 */
2918 ea.cmdlinep = cmdlinep;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002919 ea.getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002920 ea.cookie = cookie;
2921#ifdef FEAT_EVAL
2922 ea.cstack = cstack;
2923#endif
2924
2925#ifdef FEAT_USR_CMDS
Bram Moolenaar958636c2014-10-21 20:01:58 +02002926 if (IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002927 {
2928 /*
2929 * Execute a user-defined command.
2930 */
2931 do_ucmd(&ea);
2932 }
2933 else
2934#endif
2935 {
2936 /*
2937 * Call the function to execute the command.
2938 */
2939 ea.errmsg = NULL;
2940 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2941 if (ea.errmsg != NULL)
2942 errormsg = (char_u *)_(ea.errmsg);
2943 }
2944
2945#ifdef FEAT_EVAL
2946 /*
2947 * If the command just executed called do_cmdline(), any throw or ":return"
2948 * or ":finish" encountered there must also check the cstack of the still
2949 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2950 * exception, or reanimate a returned function or finished script file and
2951 * return or finish it again.
2952 */
2953 if (need_rethrow)
2954 do_throw(cstack);
2955 else if (check_cstack)
2956 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002957 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002958 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002959 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002960 && current_func_returned())
2961 do_return(&ea, TRUE, FALSE, NULL);
2962 }
2963 need_rethrow = check_cstack = FALSE;
2964#endif
2965
2966doend:
2967 if (curwin->w_cursor.lnum == 0) /* can happen with zero line number */
2968 curwin->w_cursor.lnum = 1;
2969
2970 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2971 {
2972 if (sourcing)
2973 {
2974 if (errormsg != IObuff)
2975 {
2976 STRCPY(IObuff, errormsg);
2977 errormsg = IObuff;
2978 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002979 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002980 }
2981 emsg(errormsg);
2982 }
2983#ifdef FEAT_EVAL
2984 do_errthrow(cstack,
Bram Moolenaar958636c2014-10-21 20:01:58 +02002985 (ea.cmdidx != CMD_SIZE && !IS_USER_CMDIDX(ea.cmdidx))
2986 ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002987#endif
2988
2989 if (verbose_save >= 0)
2990 p_verbose = verbose_save;
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00002991#ifdef FEAT_AUTOCMD
2992 if (cmdmod.save_ei != NULL)
2993 {
2994 /* Restore 'eventignore' to the value before ":noautocmd". */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002995 set_string_option_direct((char_u *)"ei", -1, cmdmod.save_ei,
2996 OPT_FREE, SID_NONE);
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00002997 free_string_option(cmdmod.save_ei);
2998 }
2999#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003000
3001 cmdmod = save_cmdmod;
3002
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003003 if (save_msg_silent != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003004 {
3005 /* messages could be enabled for a serious error, need to check if the
3006 * counters don't become negative */
Bram Moolenaarbecf4282009-09-30 11:24:36 +00003007 if (!did_emsg || msg_silent > save_msg_silent)
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003008 msg_silent = save_msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003009 emsg_silent -= did_esilent;
3010 if (emsg_silent < 0)
3011 emsg_silent = 0;
3012 /* Restore msg_scroll, it's set by file I/O commands, even when no
3013 * message is actually displayed. */
3014 msg_scroll = save_msg_scroll;
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003015
3016 /* "silent reg" or "silent echo x" inside "redir" leaves msg_col
3017 * somewhere in the line. Put it back in the first column. */
3018 if (redirecting())
3019 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003020 }
3021
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003022#ifdef HAVE_SANDBOX
3023 if (did_sandbox)
3024 --sandbox;
3025#endif
3026
Bram Moolenaar071d4272004-06-13 20:20:40 +00003027 if (ea.nextcmd && *ea.nextcmd == NUL) /* not really a next command */
3028 ea.nextcmd = NULL;
3029
3030#ifdef FEAT_EVAL
3031 --ex_nesting_level;
3032#endif
3033
3034 return ea.nextcmd;
3035}
3036#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00003037 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003038#endif
3039
3040/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003041 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003042 * If there is a match advance "pp" to the argument and return TRUE.
3043 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003044 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00003045checkforcmd(pp, cmd, len)
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003046 char_u **pp; /* start of command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003047 char *cmd; /* name of command */
3048 int len; /* required length */
3049{
3050 int i;
3051
3052 for (i = 0; cmd[i] != NUL; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003053 if (((char_u *)cmd)[i] != (*pp)[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003054 break;
3055 if (i >= len && !isalpha((*pp)[i]))
3056 {
3057 *pp = skipwhite(*pp + i);
3058 return TRUE;
3059 }
3060 return FALSE;
3061}
3062
3063/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003064 * Append "cmd" to the error message in IObuff.
3065 * Takes care of limiting the length and handling 0xa0, which would be
3066 * invisible otherwise.
3067 */
3068 static void
3069append_command(cmd)
3070 char_u *cmd;
3071{
3072 char_u *s = cmd;
3073 char_u *d;
3074
3075 STRCAT(IObuff, ": ");
3076 d = IObuff + STRLEN(IObuff);
3077 while (*s != NUL && d - IObuff < IOSIZE - 7)
3078 {
3079 if (
3080#ifdef FEAT_MBYTE
3081 enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) :
3082#endif
3083 *s == 0xa0)
3084 {
3085 s +=
3086#ifdef FEAT_MBYTE
3087 enc_utf8 ? 2 :
3088#endif
3089 1;
3090 STRCPY(d, "<a0>");
3091 d += 4;
3092 }
3093 else
3094 MB_COPY_CHAR(s, d);
3095 }
3096 *d = NUL;
3097}
3098
3099/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003100 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003101 * Start of the name can be found at eap->cmd.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003102 * Returns pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003103 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003104 * Returns NULL for an ambiguous user command.
3105 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003106 static char_u *
3107find_command(eap, full)
3108 exarg_T *eap;
Bram Moolenaar78a15312009-05-15 19:33:18 +00003109 int *full UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003110{
3111 int len;
3112 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003113 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114
3115 /*
3116 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00003117 * Exceptions:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118 * - the 'k' command can directly be followed by any character.
3119 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003120 * but :sre[wind] is another command, as are :scr[iptnames],
Bram Moolenaar071d4272004-06-13 20:20:40 +00003121 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00003122 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003123 */
3124 p = eap->cmd;
3125 if (*p == 'k')
3126 {
3127 eap->cmdidx = CMD_k;
3128 ++p;
3129 }
3130 else if (p[0] == 's'
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003131 && ((p[1] == 'c' && p[2] != 's' && p[2] != 'r'
3132 && p[3] != 'i' && p[4] != 'p')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133 || p[1] == 'g'
3134 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
3135 || p[1] == 'I'
3136 || (p[1] == 'r' && p[2] != 'e')))
3137 {
3138 eap->cmdidx = CMD_substitute;
3139 ++p;
3140 }
3141 else
3142 {
3143 while (ASCII_ISALPHA(*p))
3144 ++p;
Bram Moolenaarb6590522010-07-21 16:00:43 +02003145 /* for python 3.x support ":py3", ":python3", ":py3file", etc. */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02003146 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaarb6590522010-07-21 16:00:43 +02003147 while (ASCII_ISALNUM(*p))
3148 ++p;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02003149
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150 /* check for non-alpha command */
3151 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3152 ++p;
3153 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003154 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
3155 {
3156 /* Check for ":dl", ":dell", etc. to ":deletel": that's
3157 * :delete with the 'l' flag. Same for 'p'. */
3158 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003159 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00003160 break;
3161 if (i == len - 1)
3162 {
3163 --len;
3164 if (p[-1] == 'l')
3165 eap->flags |= EXFLAG_LIST;
3166 else
3167 eap->flags |= EXFLAG_PRINT;
3168 }
3169 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003170
3171 if (ASCII_ISLOWER(*eap->cmd))
3172 eap->cmdidx = cmdidxs[CharOrdLow(*eap->cmd)];
3173 else
3174 eap->cmdidx = cmdidxs[26];
3175
3176 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
3177 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
3178 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
3179 (size_t)len) == 0)
3180 {
3181#ifdef FEAT_EVAL
3182 if (full != NULL
3183 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
3184 *full = TRUE;
3185#endif
3186 break;
3187 }
3188
3189#ifdef FEAT_USR_CMDS
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01003190 /* Look for a user defined command as a last resort. Let ":Print" be
3191 * overruled by a user defined command. */
3192 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
3193 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003194 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003195 /* User defined commands may contain digits. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003196 while (ASCII_ISALNUM(*p))
3197 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003198 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003199 }
3200#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003201 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003202 eap->cmdidx = CMD_SIZE;
3203 }
3204
3205 return p;
3206}
3207
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003208#ifdef FEAT_USR_CMDS
3209/*
3210 * Search for a user command that matches "eap->cmd".
3211 * Return cmdidx in "eap->cmdidx", flags in "eap->argt", idx in "eap->useridx".
3212 * Return a pointer to just after the command.
3213 * Return NULL if there is no matching command.
3214 */
3215 static char_u *
3216find_ucmd(eap, p, full, xp, compl)
3217 exarg_T *eap;
3218 char_u *p; /* end of the command (possibly including count) */
3219 int *full; /* set to TRUE for a full match */
3220 expand_T *xp; /* used for completion, NULL otherwise */
3221 int *compl; /* completion flags or NULL */
3222{
3223 int len = (int)(p - eap->cmd);
3224 int j, k, matchlen = 0;
3225 ucmd_T *uc;
3226 int found = FALSE;
3227 int possible = FALSE;
3228 char_u *cp, *np; /* Point into typed cmd and test name */
3229 garray_T *gap;
3230 int amb_local = FALSE; /* Found ambiguous buffer-local command,
3231 only full match global is accepted. */
3232
3233 /*
3234 * Look for buffer-local user commands first, then global ones.
3235 */
3236 gap = &curbuf->b_ucmds;
3237 for (;;)
3238 {
3239 for (j = 0; j < gap->ga_len; ++j)
3240 {
3241 uc = USER_CMD_GA(gap, j);
3242 cp = eap->cmd;
3243 np = uc->uc_name;
3244 k = 0;
3245 while (k < len && *np != NUL && *cp++ == *np++)
3246 k++;
3247 if (k == len || (*np == NUL && vim_isdigit(eap->cmd[k])))
3248 {
3249 /* If finding a second match, the command is ambiguous. But
3250 * not if a buffer-local command wasn't a full match and a
3251 * global command is a full match. */
3252 if (k == len && found && *np != NUL)
3253 {
3254 if (gap == &ucmds)
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003255 return NULL;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003256 amb_local = TRUE;
3257 }
3258
3259 if (!found || (k == len && *np == NUL))
3260 {
3261 /* If we matched up to a digit, then there could
3262 * be another command including the digit that we
3263 * should use instead.
3264 */
3265 if (k == len)
3266 found = TRUE;
3267 else
3268 possible = TRUE;
3269
3270 if (gap == &ucmds)
3271 eap->cmdidx = CMD_USER;
3272 else
3273 eap->cmdidx = CMD_USER_BUF;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003274 eap->argt = (long)uc->uc_argt;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003275 eap->useridx = j;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003276 eap->addr_type = uc->uc_addr_type;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003277
3278# ifdef FEAT_CMDL_COMPL
3279 if (compl != NULL)
3280 *compl = uc->uc_compl;
3281# ifdef FEAT_EVAL
3282 if (xp != NULL)
3283 {
3284 xp->xp_arg = uc->uc_compl_arg;
3285 xp->xp_scriptID = uc->uc_scriptID;
3286 }
3287# endif
3288# endif
3289 /* Do not search for further abbreviations
3290 * if this is an exact match. */
3291 matchlen = k;
3292 if (k == len && *np == NUL)
3293 {
3294 if (full != NULL)
3295 *full = TRUE;
3296 amb_local = FALSE;
3297 break;
3298 }
3299 }
3300 }
3301 }
3302
3303 /* Stop if we found a full match or searched all. */
3304 if (j < gap->ga_len || gap == &ucmds)
3305 break;
3306 gap = &ucmds;
3307 }
3308
3309 /* Only found ambiguous matches. */
3310 if (amb_local)
3311 {
3312 if (xp != NULL)
3313 xp->xp_context = EXPAND_UNSUCCESSFUL;
3314 return NULL;
3315 }
3316
3317 /* The match we found may be followed immediately by a number. Move "p"
3318 * back to point to it. */
3319 if (found || possible)
3320 return p + (matchlen - len);
3321 return p;
3322}
3323#endif
3324
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003326static struct cmdmod
3327{
3328 char *name;
3329 int minlen;
3330 int has_count; /* :123verbose :3tab */
3331} cmdmods[] = {
3332 {"aboveleft", 3, FALSE},
3333 {"belowright", 3, FALSE},
3334 {"botright", 2, FALSE},
3335 {"browse", 3, FALSE},
3336 {"confirm", 4, FALSE},
3337 {"hide", 3, FALSE},
3338 {"keepalt", 5, FALSE},
3339 {"keepjumps", 5, FALSE},
3340 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01003341 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003342 {"leftabove", 5, FALSE},
3343 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003344 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003345 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003346 {"rightbelow", 6, FALSE},
3347 {"sandbox", 3, FALSE},
3348 {"silent", 3, FALSE},
3349 {"tab", 3, TRUE},
3350 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003351 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003352 {"verbose", 4, TRUE},
3353 {"vertical", 4, FALSE},
3354};
3355
3356/*
3357 * Return length of a command modifier (including optional count).
3358 * Return zero when it's not a modifier.
3359 */
3360 int
3361modifier_len(cmd)
3362 char_u *cmd;
3363{
3364 int i, j;
3365 char_u *p = cmd;
3366
3367 if (VIM_ISDIGIT(*cmd))
3368 p = skipwhite(skipdigits(cmd));
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003369 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003370 {
3371 for (j = 0; p[j] != NUL; ++j)
3372 if (p[j] != cmdmods[i].name[j])
3373 break;
Bram Moolenaar2d473ab2013-06-12 17:12:24 +02003374 if (!ASCII_ISALPHA(p[j]) && j >= cmdmods[i].minlen
Bram Moolenaared53fb92007-11-24 20:50:24 +00003375 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003376 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003377 }
3378 return 0;
3379}
3380
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381/*
3382 * Return > 0 if an Ex command "name" exists.
3383 * Return 2 if there is an exact match.
3384 * Return 3 if there is an ambiguous match.
3385 */
3386 int
3387cmd_exists(name)
3388 char_u *name;
3389{
3390 exarg_T ea;
3391 int full = FALSE;
3392 int i;
3393 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003394 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395
3396 /* Check command modifiers. */
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003397 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398 {
3399 for (j = 0; name[j] != NUL; ++j)
3400 if (name[j] != cmdmods[i].name[j])
3401 break;
3402 if (name[j] == NUL && j >= cmdmods[i].minlen)
3403 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3404 }
3405
Bram Moolenaara9587612006-05-04 21:47:50 +00003406 /* Check built-in commands and user defined commands.
3407 * For ":2match" and ":3match" we need to skip the number. */
3408 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003409 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003410 p = find_command(&ea, &full);
3411 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003413 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3414 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003415 if (*skipwhite(p) != NUL)
3416 return 0; /* trailing garbage */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003417 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3418}
3419#endif
3420
3421/*
3422 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
3423 * we don't need/want deleted. Maybe this could be done better if we didn't
3424 * repeat all this stuff. The only problem is that they may not stay
3425 * perfectly compatible with each other, but then the command line syntax
3426 * probably won't change that much -- webb.
3427 */
3428 char_u *
3429set_one_cmd_context(xp, buff)
3430 expand_T *xp;
3431 char_u *buff; /* buffer for command string */
3432{
3433 char_u *p;
3434 char_u *cmd, *arg;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003435 int len = 0;
3436 exarg_T ea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3438 int compl = EXPAND_NOTHING;
3439#endif
3440#ifdef FEAT_CMDL_COMPL
3441 int delim;
3442#endif
3443 int forceit = FALSE;
3444 int usefilter = FALSE; /* filter instead of file name */
3445
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003446 ExpandInit(xp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447 xp->xp_pattern = buff;
3448 xp->xp_context = EXPAND_COMMANDS; /* Default until we get past command */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003449 ea.argt = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450
3451/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003452 * 1. skip comment lines and leading space, colons or bars
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453 */
3454 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
3455 ;
3456 xp->xp_pattern = cmd;
3457
3458 if (*cmd == NUL)
3459 return NULL;
3460 if (*cmd == '"') /* ignore comment lines */
3461 {
3462 xp->xp_context = EXPAND_NOTHING;
3463 return NULL;
3464 }
3465
3466/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003467 * 3. Skip over the range to find the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468 */
3469 cmd = skip_range(cmd, &xp->xp_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470 xp->xp_pattern = cmd;
3471 if (*cmd == NUL)
3472 return NULL;
3473 if (*cmd == '"')
3474 {
3475 xp->xp_context = EXPAND_NOTHING;
3476 return NULL;
3477 }
3478
3479 if (*cmd == '|' || *cmd == '\n')
3480 return cmd + 1; /* There's another command */
3481
3482 /*
3483 * Isolate the command and search for it in the command table.
3484 * Exceptions:
3485 * - the 'k' command can directly be followed by any character, but
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003486 * do accept "keepmarks", "keepalt" and "keepjumps".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
3488 */
3489 if (*cmd == 'k' && cmd[1] != 'e')
3490 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003491 ea.cmdidx = CMD_k;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492 p = cmd + 1;
3493 }
3494 else
3495 {
3496 p = cmd;
3497 while (ASCII_ISALPHA(*p) || *p == '*') /* Allow * wild card */
3498 ++p;
3499 /* check for non-alpha command */
3500 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3501 ++p;
Bram Moolenaar9f5d6002013-06-02 19:22:13 +02003502 /* for python 3.x: ":py3*" commands completion */
3503 if (cmd[0] == 'p' && cmd[1] == 'y' && p == cmd + 2 && *p == '3')
Bram Moolenaar893b2d72013-12-11 17:44:38 +01003504 {
Bram Moolenaar9f5d6002013-06-02 19:22:13 +02003505 ++p;
Bram Moolenaar893b2d72013-12-11 17:44:38 +01003506 while (ASCII_ISALPHA(*p) || *p == '*')
3507 ++p;
3508 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003509 len = (int)(p - cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003511 if (len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512 {
3513 xp->xp_context = EXPAND_UNSUCCESSFUL;
3514 return NULL;
3515 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003516 for (ea.cmdidx = (cmdidx_T)0; (int)ea.cmdidx < (int)CMD_SIZE;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01003517 ea.cmdidx = (cmdidx_T)((int)ea.cmdidx + 1))
3518 if (STRNCMP(cmdnames[(int)ea.cmdidx].cmd_name, cmd,
3519 (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520 break;
3521
3522#ifdef FEAT_USR_CMDS
3523 if (cmd[0] >= 'A' && cmd[0] <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524 while (ASCII_ISALNUM(*p) || *p == '*') /* Allow * wild card */
3525 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526#endif
3527 }
3528
3529 /*
3530 * If the cursor is touching the command, and it ends in an alpha-numeric
3531 * character, complete the command name.
3532 */
3533 if (*p == NUL && ASCII_ISALNUM(p[-1]))
3534 return NULL;
3535
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003536 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537 {
3538 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
3539 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003540 ea.cmdidx = CMD_substitute;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541 p = cmd + 1;
3542 }
3543#ifdef FEAT_USR_CMDS
3544 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
3545 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003546 ea.cmd = cmd;
3547 p = find_ucmd(&ea, p, NULL, xp,
3548# if defined(FEAT_CMDL_COMPL)
3549 &compl
3550# else
3551 NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003552# endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003553 );
Bram Moolenaarebefac62005-12-28 22:39:57 +00003554 if (p == NULL)
3555 ea.cmdidx = CMD_SIZE; /* ambiguous user command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556 }
3557#endif
3558 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003559 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560 {
3561 /* Not still touching the command and it was an illegal one */
3562 xp->xp_context = EXPAND_UNSUCCESSFUL;
3563 return NULL;
3564 }
3565
3566 xp->xp_context = EXPAND_NOTHING; /* Default now that we're past command */
3567
3568 if (*p == '!') /* forced commands */
3569 {
3570 forceit = TRUE;
3571 ++p;
3572 }
3573
3574/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003575 * 6. parse arguments
Bram Moolenaar071d4272004-06-13 20:20:40 +00003576 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02003577 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003578 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579
3580 arg = skipwhite(p);
3581
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003582 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583 {
3584 if (*arg == '>') /* append */
3585 {
3586 if (*++arg == '>')
3587 ++arg;
3588 arg = skipwhite(arg);
3589 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003590 else if (*arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591 {
3592 ++arg;
3593 usefilter = TRUE;
3594 }
3595 }
3596
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003597 if (ea.cmdidx == CMD_read)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598 {
3599 usefilter = forceit; /* :r! filter if forced */
3600 if (*arg == '!') /* :r !filter */
3601 {
3602 ++arg;
3603 usefilter = TRUE;
3604 }
3605 }
3606
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003607 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608 {
3609 while (*arg == *cmd) /* allow any number of '>' or '<' */
3610 ++arg;
3611 arg = skipwhite(arg);
3612 }
3613
3614 /* Does command allow "+command"? */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003615 if ((ea.argt & EDITCMD) && !usefilter && *arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616 {
3617 /* Check if we're in the +command */
3618 p = arg + 1;
3619 arg = skip_cmd_arg(arg, FALSE);
3620
3621 /* Still touching the command after '+'? */
3622 if (*arg == NUL)
3623 return p;
3624
3625 /* Skip space(s) after +command to get to the real argument */
3626 arg = skipwhite(arg);
3627 }
3628
3629 /*
3630 * Check for '|' to separate commands and '"' to start comments.
3631 * Don't do this for ":read !cmd" and ":write !cmd".
3632 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003633 if ((ea.argt & TRLBAR) && !usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634 {
3635 p = arg;
3636 /* ":redir @" is not the start of a comment */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003637 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003638 p += 2;
3639 while (*p)
3640 {
3641 if (*p == Ctrl_V)
3642 {
3643 if (p[1] != NUL)
3644 ++p;
3645 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003646 else if ( (*p == '"' && !(ea.argt & NOTRLCOM))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647 || *p == '|' || *p == '\n')
3648 {
3649 if (*(p - 1) != '\\')
3650 {
3651 if (*p == '|' || *p == '\n')
3652 return p + 1;
3653 return NULL; /* It's a comment */
3654 }
3655 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003656 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657 }
3658 }
3659
3660 /* no arguments allowed */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003661 if (!(ea.argt & EXTRA) && *arg != NUL &&
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662 vim_strchr((char_u *)"|\"", *arg) == NULL)
3663 return NULL;
3664
3665 /* Find start of last argument (argument just before cursor): */
Bram Moolenaar848f8762012-07-25 17:22:23 +02003666 p = buff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667 xp->xp_pattern = p;
Bram Moolenaar09168a72012-08-02 21:24:42 +02003668 len = (int)STRLEN(buff);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003669 while (*p && p < buff + len)
3670 {
3671 if (*p == ' ' || *p == TAB)
3672 {
3673 /* argument starts after a space */
3674 xp->xp_pattern = ++p;
3675 }
3676 else
3677 {
3678 if (*p == '\\' && *(p + 1) != NUL)
3679 ++p; /* skip over escaped character */
3680 mb_ptr_adv(p);
3681 }
3682 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003683
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003684 if (ea.argt & XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003685 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003686 int c;
3687 int in_quote = FALSE;
3688 char_u *bow = NULL; /* Beginning of word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003689
3690 /*
3691 * Allow spaces within back-quotes to count as part of the argument
3692 * being expanded.
3693 */
3694 xp->xp_pattern = skipwhite(arg);
Bram Moolenaar6529c102007-08-18 15:47:34 +00003695 p = xp->xp_pattern;
3696 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003697 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003698#ifdef FEAT_MBYTE
3699 if (has_mbyte)
3700 c = mb_ptr2char(p);
3701 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003702#endif
Bram Moolenaar6529c102007-08-18 15:47:34 +00003703 c = *p;
3704 if (c == '\\' && p[1] != NUL)
3705 ++p;
3706 else if (c == '`')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003707 {
3708 if (!in_quote)
3709 {
3710 xp->xp_pattern = p;
3711 bow = p + 1;
3712 }
3713 in_quote = !in_quote;
3714 }
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003715 /* An argument can contain just about everything, except
3716 * characters that end the command and white space. */
3717 else if (c == '|' || c == '\n' || c == '"' || (vim_iswhite(c)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003718#ifdef SPACE_IN_FILENAME
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003719 && (!(ea.argt & NOSPC) || usefilter)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003720#endif
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003721 ))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003722 {
Bram Moolenaarcf5a5b82008-02-26 20:30:12 +00003723 len = 0; /* avoid getting stuck when space is in 'isfname' */
Bram Moolenaar6529c102007-08-18 15:47:34 +00003724 while (*p != NUL)
3725 {
3726#ifdef FEAT_MBYTE
3727 if (has_mbyte)
3728 c = mb_ptr2char(p);
3729 else
3730#endif
3731 c = *p;
Bram Moolenaardd87969c2007-08-21 13:07:12 +00003732 if (c == '`' || vim_isfilec_or_wc(c))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003733 break;
3734#ifdef FEAT_MBYTE
3735 if (has_mbyte)
3736 len = (*mb_ptr2len)(p);
3737 else
3738#endif
3739 len = 1;
3740 mb_ptr_adv(p);
3741 }
3742 if (in_quote)
3743 bow = p;
3744 else
3745 xp->xp_pattern = p;
3746 p -= len;
3747 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003748 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749 }
3750
3751 /*
3752 * If we are still inside the quotes, and we passed a space, just
3753 * expand from there.
3754 */
3755 if (bow != NULL && in_quote)
3756 xp->xp_pattern = bow;
3757 xp->xp_context = EXPAND_FILES;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003758
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003759 /* For a shell command more chars need to be escaped. */
3760 if (usefilter || ea.cmdidx == CMD_bang)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003761 {
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003762#ifndef BACKSLASH_IN_FILENAME
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003763 xp->xp_shell = TRUE;
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003764#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003765 /* When still after the command name expand executables. */
3766 if (xp->xp_pattern == skipwhite(arg))
Bram Moolenaar5c5b0942007-05-06 12:07:59 +00003767 xp->xp_context = EXPAND_SHELLCMD;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003768 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003769
3770 /* Check for environment variable */
3771 if (*xp->xp_pattern == '$'
3772#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
3773 || *xp->xp_pattern == '%'
3774#endif
3775 )
3776 {
3777 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
3778 if (!vim_isIDc(*p))
3779 break;
3780 if (*p == NUL)
3781 {
3782 xp->xp_context = EXPAND_ENV_VARS;
3783 ++xp->xp_pattern;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003784#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3785 /* Avoid that the assignment uses EXPAND_FILES again. */
Bram Moolenaara466c992005-07-09 21:03:22 +00003786 if (compl != EXPAND_USER_DEFINED && compl != EXPAND_USER_LIST)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003787 compl = EXPAND_ENV_VARS;
3788#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003789 }
3790 }
Bram Moolenaar24305862012-08-15 14:05:05 +02003791#if defined(FEAT_CMDL_COMPL)
3792 /* Check for user names */
3793 if (*xp->xp_pattern == '~')
3794 {
3795 for (p = xp->xp_pattern + 1; *p != NUL && *p != '/'; ++p)
3796 ;
3797 /* Complete ~user only if it partially matches a user name.
3798 * A full match ~user<Tab> will be replaced by user's home
3799 * directory i.e. something like ~user<Tab> -> /home/user/ */
3800 if (*p == NUL && p > xp->xp_pattern + 1
3801 && match_user(xp->xp_pattern + 1) == 1)
3802 {
3803 xp->xp_context = EXPAND_USER;
3804 ++xp->xp_pattern;
3805 }
3806 }
3807#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003808 }
3809
3810/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003811 * 6. Switch on command name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003812 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003813 switch (ea.cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003815 case CMD_find:
3816 case CMD_sfind:
3817 case CMD_tabfind:
Bram Moolenaarc24b6972010-08-16 22:34:29 +02003818 if (xp->xp_context == EXPAND_FILES)
3819 xp->xp_context = EXPAND_FILES_IN_PATH;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003820 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003821 case CMD_cd:
3822 case CMD_chdir:
3823 case CMD_lcd:
3824 case CMD_lchdir:
3825 if (xp->xp_context == EXPAND_FILES)
3826 xp->xp_context = EXPAND_DIRECTORIES;
3827 break;
3828 case CMD_help:
3829 xp->xp_context = EXPAND_HELP;
3830 xp->xp_pattern = arg;
3831 break;
3832
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003833 /* Command modifiers: return the argument.
3834 * Also for commands with an argument that is a command. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003835 case CMD_aboveleft:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003836 case CMD_argdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003837 case CMD_belowright:
3838 case CMD_botright:
3839 case CMD_browse:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003840 case CMD_bufdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841 case CMD_confirm:
Bram Moolenaardf177f62005-02-22 08:39:57 +00003842 case CMD_debug:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003843 case CMD_folddoclosed:
3844 case CMD_folddoopen:
3845 case CMD_hide:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003846 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003847 case CMD_keepjumps:
3848 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01003849 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003850 case CMD_leftabove:
3851 case CMD_lockmarks:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003852 case CMD_noautocmd:
3853 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854 case CMD_rightbelow:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003855 case CMD_sandbox:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003856 case CMD_silent:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003857 case CMD_tab:
Bram Moolenaar70baa402013-06-16 16:14:03 +02003858 case CMD_tabdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859 case CMD_topleft:
3860 case CMD_verbose:
3861 case CMD_vertical:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003862 case CMD_windo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003863 return arg;
3864
Bram Moolenaar4f688582007-07-24 12:34:30 +00003865#ifdef FEAT_CMDL_COMPL
3866# ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867 case CMD_match:
3868 if (*arg == NUL || !ends_excmd(*arg))
3869 {
Bram Moolenaar4f688582007-07-24 12:34:30 +00003870 /* also complete "None" */
3871 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003872 arg = skipwhite(skiptowhite(arg));
3873 if (*arg != NUL)
3874 {
3875 xp->xp_context = EXPAND_NOTHING;
3876 arg = skip_regexp(arg + 1, *arg, p_magic, NULL);
3877 }
3878 }
3879 return find_nextcmd(arg);
Bram Moolenaar4f688582007-07-24 12:34:30 +00003880# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003881
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882/*
3883 * All completion for the +cmdline_compl feature goes here.
3884 */
3885
3886# ifdef FEAT_USR_CMDS
3887 case CMD_command:
3888 /* Check for attributes */
3889 while (*arg == '-')
3890 {
3891 arg++; /* Skip "-" */
3892 p = skiptowhite(arg);
3893 if (*p == NUL)
3894 {
3895 /* Cursor is still in the attribute */
3896 p = vim_strchr(arg, '=');
3897 if (p == NULL)
3898 {
3899 /* No "=", so complete attribute names */
3900 xp->xp_context = EXPAND_USER_CMD_FLAGS;
3901 xp->xp_pattern = arg;
3902 return NULL;
3903 }
3904
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003905 /* For the -complete, -nargs and -addr attributes, we complete
Bram Moolenaar071d4272004-06-13 20:20:40 +00003906 * their arguments as well.
3907 */
3908 if (STRNICMP(arg, "complete", p - arg) == 0)
3909 {
3910 xp->xp_context = EXPAND_USER_COMPLETE;
3911 xp->xp_pattern = p + 1;
3912 return NULL;
3913 }
3914 else if (STRNICMP(arg, "nargs", p - arg) == 0)
3915 {
3916 xp->xp_context = EXPAND_USER_NARGS;
3917 xp->xp_pattern = p + 1;
3918 return NULL;
3919 }
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003920 else if (STRNICMP(arg, "addr", p - arg) == 0)
3921 {
3922 xp->xp_context = EXPAND_USER_ADDR_TYPE;
3923 xp->xp_pattern = p + 1;
3924 return NULL;
3925 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926 return NULL;
3927 }
3928 arg = skipwhite(p);
3929 }
3930
3931 /* After the attributes comes the new command name */
3932 p = skiptowhite(arg);
3933 if (*p == NUL)
3934 {
3935 xp->xp_context = EXPAND_USER_COMMANDS;
3936 xp->xp_pattern = arg;
3937 break;
3938 }
3939
3940 /* And finally comes a normal command */
3941 return skipwhite(p);
3942
3943 case CMD_delcommand:
3944 xp->xp_context = EXPAND_USER_COMMANDS;
3945 xp->xp_pattern = arg;
3946 break;
3947# endif
3948
3949 case CMD_global:
3950 case CMD_vglobal:
3951 delim = *arg; /* get the delimiter */
3952 if (delim)
3953 ++arg; /* skip delimiter if there is one */
3954
3955 while (arg[0] != NUL && arg[0] != delim)
3956 {
3957 if (arg[0] == '\\' && arg[1] != NUL)
3958 ++arg;
3959 ++arg;
3960 }
3961 if (arg[0] != NUL)
3962 return arg + 1;
3963 break;
3964 case CMD_and:
3965 case CMD_substitute:
3966 delim = *arg;
3967 if (delim)
3968 {
3969 /* skip "from" part */
3970 ++arg;
3971 arg = skip_regexp(arg, delim, p_magic, NULL);
3972 }
3973 /* skip "to" part */
3974 while (arg[0] != NUL && arg[0] != delim)
3975 {
3976 if (arg[0] == '\\' && arg[1] != NUL)
3977 ++arg;
3978 ++arg;
3979 }
3980 if (arg[0] != NUL) /* skip delimiter */
3981 ++arg;
3982 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
3983 ++arg;
3984 if (arg[0] != NUL)
3985 return arg;
3986 break;
3987 case CMD_isearch:
3988 case CMD_dsearch:
3989 case CMD_ilist:
3990 case CMD_dlist:
3991 case CMD_ijump:
3992 case CMD_psearch:
3993 case CMD_djump:
3994 case CMD_isplit:
3995 case CMD_dsplit:
3996 arg = skipwhite(skipdigits(arg)); /* skip count */
3997 if (*arg == '/') /* Match regexp, not just whole words */
3998 {
3999 for (++arg; *arg && *arg != '/'; arg++)
4000 if (*arg == '\\' && arg[1] != NUL)
4001 arg++;
4002 if (*arg)
4003 {
4004 arg = skipwhite(arg + 1);
4005
4006 /* Check for trailing illegal characters */
4007 if (*arg && vim_strchr((char_u *)"|\"\n", *arg) == NULL)
4008 xp->xp_context = EXPAND_NOTHING;
4009 else
4010 return arg;
4011 }
4012 }
4013 break;
4014#ifdef FEAT_AUTOCMD
4015 case CMD_autocmd:
4016 return set_context_in_autocmd(xp, arg, FALSE);
4017
4018 case CMD_doautocmd:
Bram Moolenaar73a9d7b2008-11-06 16:16:44 +00004019 case CMD_doautoall:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004020 return set_context_in_autocmd(xp, arg, TRUE);
4021#endif
4022 case CMD_set:
4023 set_context_in_set_cmd(xp, arg, 0);
4024 break;
4025 case CMD_setglobal:
4026 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
4027 break;
4028 case CMD_setlocal:
4029 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
4030 break;
4031 case CMD_tag:
4032 case CMD_stag:
4033 case CMD_ptag:
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00004034 case CMD_ltag:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004035 case CMD_tselect:
4036 case CMD_stselect:
4037 case CMD_ptselect:
4038 case CMD_tjump:
4039 case CMD_stjump:
4040 case CMD_ptjump:
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00004041 if (*p_wop != NUL)
4042 xp->xp_context = EXPAND_TAGS_LISTFILES;
4043 else
4044 xp->xp_context = EXPAND_TAGS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004045 xp->xp_pattern = arg;
4046 break;
4047 case CMD_augroup:
4048 xp->xp_context = EXPAND_AUGROUP;
4049 xp->xp_pattern = arg;
4050 break;
4051#ifdef FEAT_SYN_HL
4052 case CMD_syntax:
4053 set_context_in_syntax_cmd(xp, arg);
4054 break;
4055#endif
4056#ifdef FEAT_EVAL
4057 case CMD_let:
4058 case CMD_if:
4059 case CMD_elseif:
4060 case CMD_while:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00004061 case CMD_for:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004062 case CMD_echo:
4063 case CMD_echon:
4064 case CMD_execute:
4065 case CMD_echomsg:
4066 case CMD_echoerr:
4067 case CMD_call:
4068 case CMD_return:
Bram Moolenaar52b4b552005-03-07 23:00:57 +00004069 set_context_for_expression(xp, arg, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070 break;
4071
4072 case CMD_unlet:
4073 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
4074 arg = xp->xp_pattern + 1;
4075 xp->xp_context = EXPAND_USER_VARS;
4076 xp->xp_pattern = arg;
4077 break;
4078
4079 case CMD_function:
4080 case CMD_delfunction:
4081 xp->xp_context = EXPAND_USER_FUNC;
4082 xp->xp_pattern = arg;
4083 break;
4084
4085 case CMD_echohl:
Bram Moolenaar4f688582007-07-24 12:34:30 +00004086 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087 break;
4088#endif
4089 case CMD_highlight:
4090 set_context_in_highlight_cmd(xp, arg);
4091 break;
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004092#ifdef FEAT_CSCOPE
4093 case CMD_cscope:
Bram Moolenaar7bfef802009-04-22 14:25:01 +00004094 case CMD_lcscope:
4095 case CMD_scscope:
4096 set_context_in_cscope_cmd(xp, arg, ea.cmdidx);
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004097 break;
4098#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00004099#ifdef FEAT_SIGNS
4100 case CMD_sign:
4101 set_context_in_sign_cmd(xp, arg);
4102 break;
4103#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004104#ifdef FEAT_LISTCMDS
4105 case CMD_bdelete:
4106 case CMD_bwipeout:
4107 case CMD_bunload:
4108 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
4109 arg = xp->xp_pattern + 1;
4110 /*FALLTHROUGH*/
4111 case CMD_buffer:
4112 case CMD_sbuffer:
4113 case CMD_checktime:
4114 xp->xp_context = EXPAND_BUFFERS;
4115 xp->xp_pattern = arg;
4116 break;
4117#endif
4118#ifdef FEAT_USR_CMDS
4119 case CMD_USER:
4120 case CMD_USER_BUF:
4121 if (compl != EXPAND_NOTHING)
4122 {
4123 /* XFILE: file names are handled above */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00004124 if (!(ea.argt & XFILE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004125 {
4126# ifdef FEAT_MENU
4127 if (compl == EXPAND_MENUS)
4128 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
4129# endif
4130 if (compl == EXPAND_COMMANDS)
4131 return arg;
4132 if (compl == EXPAND_MAPPINGS)
4133 return set_context_in_map_cmd(xp, (char_u *)"map",
4134 arg, forceit, FALSE, FALSE, CMD_map);
Bram Moolenaar848f8762012-07-25 17:22:23 +02004135 /* Find start of last argument. */
4136 p = arg;
4137 while (*p)
4138 {
4139 if (*p == ' ')
Bram Moolenaar848f8762012-07-25 17:22:23 +02004140 /* argument starts after a space */
4141 arg = p + 1;
Bram Moolenaara07c8312012-07-27 21:12:07 +02004142 else if (*p == '\\' && *(p + 1) != NUL)
4143 ++p; /* skip over escaped character */
4144 mb_ptr_adv(p);
Bram Moolenaar848f8762012-07-25 17:22:23 +02004145 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004146 xp->xp_pattern = arg;
4147 }
4148 xp->xp_context = compl;
4149 }
4150 break;
4151#endif
4152 case CMD_map: case CMD_noremap:
4153 case CMD_nmap: case CMD_nnoremap:
4154 case CMD_vmap: case CMD_vnoremap:
4155 case CMD_omap: case CMD_onoremap:
4156 case CMD_imap: case CMD_inoremap:
4157 case CMD_cmap: case CMD_cnoremap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004158 case CMD_lmap: case CMD_lnoremap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004159 case CMD_smap: case CMD_snoremap:
4160 case CMD_xmap: case CMD_xnoremap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004161 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004162 FALSE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004163 case CMD_unmap:
4164 case CMD_nunmap:
4165 case CMD_vunmap:
4166 case CMD_ounmap:
4167 case CMD_iunmap:
4168 case CMD_cunmap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004169 case CMD_lunmap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004170 case CMD_sunmap:
4171 case CMD_xunmap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004173 FALSE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174 case CMD_abbreviate: case CMD_noreabbrev:
4175 case CMD_cabbrev: case CMD_cnoreabbrev:
4176 case CMD_iabbrev: case CMD_inoreabbrev:
4177 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004178 TRUE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179 case CMD_unabbreviate:
4180 case CMD_cunabbrev:
4181 case CMD_iunabbrev:
4182 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004183 TRUE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184#ifdef FEAT_MENU
4185 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
4186 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
4187 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
4188 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
4189 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
4190 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
4191 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
4192 case CMD_tmenu: case CMD_tunmenu:
4193 case CMD_popup: case CMD_tearoff: case CMD_emenu:
4194 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
4195#endif
4196
4197 case CMD_colorscheme:
4198 xp->xp_context = EXPAND_COLORS;
4199 xp->xp_pattern = arg;
4200 break;
4201
4202 case CMD_compiler:
4203 xp->xp_context = EXPAND_COMPILER;
4204 xp->xp_pattern = arg;
4205 break;
4206
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004207 case CMD_ownsyntax:
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004208 xp->xp_context = EXPAND_OWNSYNTAX;
4209 xp->xp_pattern = arg;
4210 break;
4211
4212 case CMD_setfiletype:
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004213 xp->xp_context = EXPAND_FILETYPE;
4214 xp->xp_pattern = arg;
4215 break;
4216
Bram Moolenaar071d4272004-06-13 20:20:40 +00004217#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
4218 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
4219 case CMD_language:
Bram Moolenaara660dc82011-05-25 12:51:22 +02004220 p = skiptowhite(arg);
4221 if (*p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222 {
4223 xp->xp_context = EXPAND_LANGUAGE;
4224 xp->xp_pattern = arg;
4225 }
4226 else
Bram Moolenaara660dc82011-05-25 12:51:22 +02004227 {
4228 if ( STRNCMP(arg, "messages", p - arg) == 0
4229 || STRNCMP(arg, "ctype", p - arg) == 0
4230 || STRNCMP(arg, "time", p - arg) == 0)
4231 {
4232 xp->xp_context = EXPAND_LOCALES;
4233 xp->xp_pattern = skipwhite(p);
4234 }
4235 else
4236 xp->xp_context = EXPAND_NOTHING;
4237 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004238 break;
4239#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01004240#if defined(FEAT_PROFILE)
4241 case CMD_profile:
4242 set_context_in_profile_cmd(xp, arg);
4243 break;
4244#endif
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01004245 case CMD_behave:
4246 xp->xp_context = EXPAND_BEHAVE;
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004247 xp->xp_pattern = arg;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01004248 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004250#if defined(FEAT_CMDHIST)
4251 case CMD_history:
4252 xp->xp_context = EXPAND_HISTORY;
4253 xp->xp_pattern = arg;
4254 break;
4255#endif
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02004256#if defined(FEAT_PROFILE)
4257 case CMD_syntime:
4258 xp->xp_context = EXPAND_SYNTIME;
4259 xp->xp_pattern = arg;
4260 break;
4261#endif
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004262
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263#endif /* FEAT_CMDL_COMPL */
4264
4265 default:
4266 break;
4267 }
4268 return NULL;
4269}
4270
4271/*
4272 * skip a range specifier of the form: addr [,addr] [;addr] ..
4273 *
4274 * Backslashed delimiters after / or ? will be skipped, and commands will
4275 * not be expanded between /'s and ?'s or after "'".
4276 *
Bram Moolenaardf177f62005-02-22 08:39:57 +00004277 * Also skip white space and ":" characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004278 * Returns the "cmd" pointer advanced to beyond the range.
4279 */
4280 char_u *
4281skip_range(cmd, ctx)
4282 char_u *cmd;
4283 int *ctx; /* pointer to xp_context or NULL */
4284{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004285 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004286
Bram Moolenaardf177f62005-02-22 08:39:57 +00004287 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004288 {
4289 if (*cmd == '\'')
4290 {
4291 if (*++cmd == NUL && ctx != NULL)
4292 *ctx = EXPAND_NOTHING;
4293 }
4294 else if (*cmd == '/' || *cmd == '?')
4295 {
4296 delim = *cmd++;
4297 while (*cmd != NUL && *cmd != delim)
4298 if (*cmd++ == '\\' && *cmd != NUL)
4299 ++cmd;
4300 if (*cmd == NUL && ctx != NULL)
4301 *ctx = EXPAND_NOTHING;
4302 }
4303 if (*cmd != NUL)
4304 ++cmd;
4305 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004306
4307 /* Skip ":" and white space. */
4308 while (*cmd == ':')
4309 cmd = skipwhite(cmd + 1);
4310
Bram Moolenaar071d4272004-06-13 20:20:40 +00004311 return cmd;
4312}
4313
4314/*
4315 * get a single EX address
4316 *
4317 * Set ptr to the next character after the part that was interpreted.
4318 * Set ptr to NULL when an error is encountered.
4319 *
4320 * Return MAXLNUM when no Ex address was found.
4321 */
4322 static linenr_T
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004323get_address(ptr, addr_type, skip, to_other_file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004324 char_u **ptr;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004325 int addr_type; /* flag: one of ADDR_LINES, ... */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004326 int skip; /* only skip the address, don't use it */
4327 int to_other_file; /* flag: may jump to other file */
4328{
4329 int c;
4330 int i;
4331 long n;
4332 char_u *cmd;
4333 pos_T pos;
4334 pos_T *fp;
4335 linenr_T lnum;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004336 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004337
4338 cmd = skipwhite(*ptr);
4339 lnum = MAXLNUM;
4340 do
4341 {
4342 switch (*cmd)
4343 {
4344 case '.': /* '.' - Cursor position */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004345 ++cmd;
4346 switch (addr_type)
4347 {
4348 case ADDR_LINES:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004349 lnum = curwin->w_cursor.lnum;
4350 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004351 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004352 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004353 break;
4354 case ADDR_ARGUMENTS:
4355 lnum = curwin->w_arg_idx + 1;
4356 break;
4357 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004358 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004359 lnum = curbuf->b_fnum;
4360 break;
4361 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004362 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004363 break;
4364 }
4365 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004366
4367 case '$': /* '$' - last line */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004368 ++cmd;
4369 switch (addr_type)
4370 {
4371 case ADDR_LINES:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004372 lnum = curbuf->b_ml.ml_line_count;
4373 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004374 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004375 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004376 break;
4377 case ADDR_ARGUMENTS:
4378 lnum = ARGCOUNT;
4379 break;
4380 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004381 buf = lastbuf;
4382 while (buf->b_ml.ml_mfp == NULL)
4383 {
4384 if (buf->b_prev == NULL)
4385 break;
4386 buf = buf->b_prev;
4387 }
4388 lnum = buf->b_fnum;
4389 break;
4390 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004391 lnum = lastbuf->b_fnum;
4392 break;
4393 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004394 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004395 break;
4396 }
4397 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004398
4399 case '\'': /* ''' - mark */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004400 if (*++cmd == NUL)
4401 {
4402 cmd = NULL;
4403 goto error;
4404 }
4405 if (addr_type != ADDR_LINES)
4406 {
4407 EMSG(_(e_invaddr));
4408 goto error;
4409 }
4410 if (skip)
4411 ++cmd;
4412 else
4413 {
4414 /* Only accept a mark in another file when it is
4415 * used by itself: ":'M". */
4416 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
4417 ++cmd;
4418 if (fp == (pos_T *)-1)
4419 /* Jumped to another file. */
4420 lnum = curwin->w_cursor.lnum;
4421 else
4422 {
4423 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004424 {
4425 cmd = NULL;
4426 goto error;
4427 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004428 lnum = fp->lnum;
4429 }
4430 }
4431 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004432
4433 case '/':
4434 case '?': /* '/' or '?' - search */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004435 c = *cmd++;
4436 if (addr_type != ADDR_LINES)
4437 {
4438 EMSG(_(e_invaddr));
4439 goto error;
4440 }
4441 if (skip) /* skip "/pat/" */
4442 {
4443 cmd = skip_regexp(cmd, c, (int)p_magic, NULL);
4444 if (*cmd == c)
4445 ++cmd;
4446 }
4447 else
4448 {
4449 pos = curwin->w_cursor; /* save curwin->w_cursor */
4450 /*
4451 * When '/' or '?' follows another address, start
4452 * from there.
4453 */
4454 if (lnum != MAXLNUM)
4455 curwin->w_cursor.lnum = lnum;
4456 /*
4457 * Start a forward search at the end of the line.
4458 * Start a backward search at the start of the line.
4459 * This makes sure we never match in the current
4460 * line, and can match anywhere in the
4461 * next/previous line.
4462 */
4463 if (c == '/')
4464 curwin->w_cursor.col = MAXCOL;
4465 else
4466 curwin->w_cursor.col = 0;
4467 searchcmdlen = 0;
4468 if (!do_search(NULL, c, cmd, 1L,
4469 SEARCH_HIS | SEARCH_MSG, NULL))
4470 {
4471 curwin->w_cursor = pos;
4472 cmd = NULL;
4473 goto error;
4474 }
4475 lnum = curwin->w_cursor.lnum;
4476 curwin->w_cursor = pos;
4477 /* adjust command string pointer */
4478 cmd += searchcmdlen;
4479 }
4480 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004481
4482 case '\\': /* "\?", "\/" or "\&", repeat search */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004483 ++cmd;
4484 if (addr_type != ADDR_LINES)
4485 {
4486 EMSG(_(e_invaddr));
4487 goto error;
4488 }
4489 if (*cmd == '&')
4490 i = RE_SUBST;
4491 else if (*cmd == '?' || *cmd == '/')
4492 i = RE_SEARCH;
4493 else
4494 {
4495 EMSG(_(e_backslash));
4496 cmd = NULL;
4497 goto error;
4498 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004499
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004500 if (!skip)
4501 {
4502 /*
4503 * When search follows another address, start from
4504 * there.
4505 */
4506 if (lnum != MAXLNUM)
4507 pos.lnum = lnum;
4508 else
4509 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004510
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004511 /*
4512 * Start the search just like for the above
4513 * do_search().
4514 */
4515 if (*cmd != '?')
4516 pos.col = MAXCOL;
4517 else
4518 pos.col = 0;
4519 if (searchit(curwin, curbuf, &pos,
4520 *cmd == '?' ? BACKWARD : FORWARD,
4521 (char_u *)"", 1L, SEARCH_MSG,
4522 i, (linenr_T)0, NULL) != FAIL)
4523 lnum = pos.lnum;
4524 else
4525 {
4526 cmd = NULL;
4527 goto error;
4528 }
4529 }
4530 ++cmd;
4531 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004532
4533 default:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004534 if (VIM_ISDIGIT(*cmd)) /* absolute line number */
4535 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004536 }
4537
4538 for (;;)
4539 {
4540 cmd = skipwhite(cmd);
4541 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4542 break;
4543
4544 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004545 {
4546 switch (addr_type)
4547 {
4548 case ADDR_LINES:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004549 /* "+1" is same as ".+1" */
4550 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004551 break;
4552 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004553 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004554 break;
4555 case ADDR_ARGUMENTS:
4556 lnum = curwin->w_arg_idx + 1;
4557 break;
4558 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004559 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004560 lnum = curbuf->b_fnum;
4561 break;
4562 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004563 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004564 break;
4565 }
4566 }
4567
Bram Moolenaar071d4272004-06-13 20:20:40 +00004568 if (VIM_ISDIGIT(*cmd))
4569 i = '+'; /* "number" is same as "+number" */
4570 else
4571 i = *cmd++;
4572 if (!VIM_ISDIGIT(*cmd)) /* '+' is '+1', but '+0' is not '+1' */
4573 n = 1;
4574 else
4575 n = getdigits(&cmd);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004576 if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004577 || addr_type == ADDR_BUFFERS)
Bram Moolenaar4d84d932014-11-30 14:50:16 +01004578 lnum = compute_buffer_local_count(addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004579 else if (i == '-')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004580 lnum -= n;
4581 else
4582 lnum += n;
4583 }
4584 } while (*cmd == '/' || *cmd == '?');
4585
4586error:
4587 *ptr = cmd;
4588 return lnum;
4589}
4590
4591/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004592 * Get flags from an Ex command argument.
4593 */
4594 static void
4595get_flags(eap)
4596 exarg_T *eap;
4597{
4598 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4599 {
4600 if (*eap->arg == 'l')
4601 eap->flags |= EXFLAG_LIST;
4602 else if (*eap->arg == 'p')
4603 eap->flags |= EXFLAG_PRINT;
4604 else
4605 eap->flags |= EXFLAG_NR;
4606 eap->arg = skipwhite(eap->arg + 1);
4607 }
4608}
4609
4610/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004611 * Function called for command which is Not Implemented. NI!
4612 */
4613 void
4614ex_ni(eap)
4615 exarg_T *eap;
4616{
4617 if (!eap->skip)
4618 eap->errmsg = (char_u *)N_("E319: Sorry, the command is not available in this version");
4619}
4620
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004621#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004622/*
4623 * Function called for script command which is Not Implemented. NI!
4624 * Skips over ":perl <<EOF" constructs.
4625 */
4626 static void
4627ex_script_ni(eap)
4628 exarg_T *eap;
4629{
4630 if (!eap->skip)
4631 ex_ni(eap);
4632 else
4633 vim_free(script_get(eap, eap->arg));
4634}
4635#endif
4636
4637/*
4638 * Check range in Ex command for validity.
4639 * Return NULL when valid, error message when invalid.
4640 */
4641 static char_u *
4642invalid_range(eap)
4643 exarg_T *eap;
4644{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004645 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004646 if ( eap->line1 < 0
4647 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004648 || eap->line1 > eap->line2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004649 return (char_u *)_(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004650
4651 if (eap->argt & RANGE)
4652 {
4653 switch(eap->addr_type)
4654 {
4655 case ADDR_LINES:
4656 if (!(eap->argt & NOTADR)
4657 && eap->line2 > curbuf->b_ml.ml_line_count
4658#ifdef FEAT_DIFF
4659 + (eap->cmdidx == CMD_diffget)
4660#endif
4661 )
4662 return (char_u *)_(e_invrange);
4663 break;
4664 case ADDR_ARGUMENTS:
4665 if (eap->line2 > ARGCOUNT + (!ARGCOUNT)) // add 1 if ARCOUNT is 0
4666 return (char_u *)_(e_invrange);
4667 break;
4668 case ADDR_BUFFERS:
4669 if (eap->line1 < firstbuf->b_fnum
4670 || eap->line2 > lastbuf->b_fnum)
4671 return (char_u *)_(e_invrange);
4672 break;
4673 case ADDR_LOADED_BUFFERS:
4674 buf = firstbuf;
4675 while (buf->b_ml.ml_mfp == NULL)
4676 {
4677 if (buf->b_next == NULL)
4678 return (char_u *)_(e_invrange);
4679 buf = buf->b_next;
4680 }
4681 if (eap->line1 < buf->b_fnum)
4682 return (char_u *)_(e_invrange);
4683 buf = lastbuf;
4684 while (buf->b_ml.ml_mfp == NULL)
4685 {
4686 if (buf->b_prev == NULL)
4687 return (char_u *)_(e_invrange);
4688 buf = buf->b_prev;
4689 }
4690 if (eap->line2 > buf->b_fnum)
4691 return (char_u *)_(e_invrange);
4692 break;
4693 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01004694 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004695 return (char_u *)_(e_invrange);
4696 break;
4697 case ADDR_TABS:
4698 if (eap->line2 > LAST_TAB_NR)
4699 return (char_u *)_(e_invrange);
4700 break;
4701 }
4702 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004703 return NULL;
4704}
4705
4706/*
4707 * Correct the range for zero line number, if required.
4708 */
4709 static void
4710correct_range(eap)
4711 exarg_T *eap;
4712{
4713 if (!(eap->argt & ZEROR)) /* zero in range not allowed */
4714 {
4715 if (eap->line1 == 0)
4716 eap->line1 = 1;
4717 if (eap->line2 == 0)
4718 eap->line2 = 1;
4719 }
4720}
4721
Bram Moolenaar748bf032005-02-02 23:04:36 +00004722#ifdef FEAT_QUICKFIX
4723static char_u *skip_grep_pat __ARGS((exarg_T *eap));
4724
4725/*
4726 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4727 * pattern. Otherwise return eap->arg.
4728 */
4729 static char_u *
4730skip_grep_pat(eap)
4731 exarg_T *eap;
4732{
4733 char_u *p = eap->arg;
4734
Bram Moolenaara37420f2006-02-04 22:37:47 +00004735 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4736 || eap->cmdidx == CMD_vimgrepadd
4737 || eap->cmdidx == CMD_lvimgrepadd
4738 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004739 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004740 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004741 if (p == NULL)
4742 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004743 }
4744 return p;
4745}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004746
4747/*
4748 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4749 * in the command line, so that things like % get expanded.
4750 */
4751 static char_u *
4752replace_makeprg(eap, p, cmdlinep)
4753 exarg_T *eap;
4754 char_u *p;
4755 char_u **cmdlinep;
4756{
4757 char_u *new_cmdline;
4758 char_u *program;
4759 char_u *pos;
4760 char_u *ptr;
4761 int len;
4762 int i;
4763
4764 /*
4765 * Don't do it when ":vimgrep" is used for ":grep".
4766 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004767 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4768 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4769 || eap->cmdidx == CMD_grepadd
4770 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004771 && !grep_internal(eap->cmdidx))
4772 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004773 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4774 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004775 {
4776 if (*curbuf->b_p_gp == NUL)
4777 program = p_gp;
4778 else
4779 program = curbuf->b_p_gp;
4780 }
4781 else
4782 {
4783 if (*curbuf->b_p_mp == NUL)
4784 program = p_mp;
4785 else
4786 program = curbuf->b_p_mp;
4787 }
4788
4789 p = skipwhite(p);
4790
4791 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4792 {
4793 /* replace $* by given arguments */
4794 i = 1;
4795 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4796 ++i;
4797 len = (int)STRLEN(p);
4798 new_cmdline = alloc((int)(STRLEN(program) + i * (len - 2) + 1));
4799 if (new_cmdline == NULL)
4800 return NULL; /* out of memory */
4801 ptr = new_cmdline;
4802 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4803 {
4804 i = (int)(pos - program);
4805 STRNCPY(ptr, program, i);
4806 STRCPY(ptr += i, p);
4807 ptr += len;
4808 program = pos + 2;
4809 }
4810 STRCPY(ptr, program);
4811 }
4812 else
4813 {
4814 new_cmdline = alloc((int)(STRLEN(program) + STRLEN(p) + 2));
4815 if (new_cmdline == NULL)
4816 return NULL; /* out of memory */
4817 STRCPY(new_cmdline, program);
4818 STRCAT(new_cmdline, " ");
4819 STRCAT(new_cmdline, p);
4820 }
4821 msg_make(p);
4822
4823 /* 'eap->cmd' is not set here, because it is not used at CMD_make */
4824 vim_free(*cmdlinep);
4825 *cmdlinep = new_cmdline;
4826 p = new_cmdline;
4827 }
4828 return p;
4829}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004830#endif
4831
Bram Moolenaar071d4272004-06-13 20:20:40 +00004832/*
4833 * Expand file name in Ex command argument.
4834 * Return FAIL for failure, OK otherwise.
4835 */
4836 int
4837expand_filename(eap, cmdlinep, errormsgp)
4838 exarg_T *eap;
4839 char_u **cmdlinep;
4840 char_u **errormsgp;
4841{
4842 int has_wildcards; /* need to expand wildcards */
4843 char_u *repl;
4844 int srclen;
4845 char_u *p;
4846 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004847 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004848
Bram Moolenaar748bf032005-02-02 23:04:36 +00004849#ifdef FEAT_QUICKFIX
4850 /* Skip a regexp pattern for ":vimgrep[add] pat file..." */
4851 p = skip_grep_pat(eap);
4852#else
4853 p = eap->arg;
4854#endif
4855
Bram Moolenaar071d4272004-06-13 20:20:40 +00004856 /*
4857 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4858 * the file name contains a wildcard it should not cause expanding.
4859 * (it will be expanded anyway if there is a wildcard before replacing).
4860 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004861 has_wildcards = mch_has_wildcard(p);
4862 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004863 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004864#ifdef FEAT_EVAL
4865 /* Skip over `=expr`, wildcards in it are not expanded. */
4866 if (p[0] == '`' && p[1] == '=')
4867 {
4868 p += 2;
4869 (void)skip_expr(&p);
4870 if (*p == '`')
4871 ++p;
4872 continue;
4873 }
4874#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004875 /*
4876 * Quick check if this cannot be the start of a special string.
4877 * Also removes backslash before '%', '#' and '<'.
4878 */
4879 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4880 {
4881 ++p;
4882 continue;
4883 }
4884
4885 /*
4886 * Try to find a match at this position.
4887 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004888 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
4889 errormsgp, &escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004890 if (*errormsgp != NULL) /* error detected */
4891 return FAIL;
4892 if (repl == NULL) /* no match found */
4893 {
4894 p += srclen;
4895 continue;
4896 }
4897
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004898 /* Wildcards won't be expanded below, the replacement is taken
4899 * literally. But do expand "~/file", "~user/file" and "$HOME/file". */
4900 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4901 {
4902 char_u *l = repl;
4903
4904 repl = expand_env_save(repl);
4905 vim_free(l);
4906 }
4907
Bram Moolenaar63b92542007-03-27 14:57:09 +00004908 /* Need to escape white space et al. with a backslash.
4909 * Don't do this for:
4910 * - replacement that already has been escaped: "##"
4911 * - shell commands (may have to use quotes instead).
4912 * - non-unix systems when there is a single argument (spaces don't
4913 * separate arguments then).
4914 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004915 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00004916 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917 && eap->cmdidx != CMD_bang
4918 && eap->cmdidx != CMD_make
Bram Moolenaara37420f2006-02-04 22:37:47 +00004919 && eap->cmdidx != CMD_lmake
Bram Moolenaar071d4272004-06-13 20:20:40 +00004920 && eap->cmdidx != CMD_grep
Bram Moolenaara37420f2006-02-04 22:37:47 +00004921 && eap->cmdidx != CMD_lgrep
Bram Moolenaar071d4272004-06-13 20:20:40 +00004922 && eap->cmdidx != CMD_grepadd
Bram Moolenaara37420f2006-02-04 22:37:47 +00004923 && eap->cmdidx != CMD_lgrepadd
Bram Moolenaar071d4272004-06-13 20:20:40 +00004924#ifndef UNIX
4925 && !(eap->argt & NOSPC)
4926#endif
4927 )
4928 {
4929 char_u *l;
4930#ifdef BACKSLASH_IN_FILENAME
4931 /* Don't escape a backslash here, because rem_backslash() doesn't
4932 * remove it later. */
4933 static char_u *nobslash = (char_u *)" \t\"|";
4934# define ESCAPE_CHARS nobslash
4935#else
4936# define ESCAPE_CHARS escape_chars
4937#endif
4938
4939 for (l = repl; *l; ++l)
4940 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
4941 {
4942 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
4943 if (l != NULL)
4944 {
4945 vim_free(repl);
4946 repl = l;
4947 }
4948 break;
4949 }
4950 }
4951
4952 /* For a shell command a '!' must be escaped. */
4953 if ((eap->usefilter || eap->cmdidx == CMD_bang)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004954 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004955 {
4956 char_u *l;
4957
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004958 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004959 if (l != NULL)
4960 {
4961 vim_free(repl);
4962 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004963 }
4964 }
4965
4966 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
4967 vim_free(repl);
4968 if (p == NULL)
4969 return FAIL;
4970 }
4971
4972 /*
4973 * One file argument: Expand wildcards.
4974 * Don't do this with ":r !command" or ":w !command".
4975 */
4976 if ((eap->argt & NOSPC) && !eap->usefilter)
4977 {
4978 /*
4979 * May do this twice:
4980 * 1. Replace environment variables.
4981 * 2. Replace any other wildcards, remove backslashes.
4982 */
4983 for (n = 1; n <= 2; ++n)
4984 {
4985 if (n == 2)
4986 {
4987#ifdef UNIX
4988 /*
4989 * Only for Unix we check for more than one file name.
4990 * For other systems spaces are considered to be part
4991 * of the file name.
4992 * Only check here if there is no wildcard, otherwise
4993 * ExpandOne() will check for errors. This allows
4994 * ":e `ls ve*.c`" on Unix.
4995 */
4996 if (!has_wildcards)
4997 for (p = eap->arg; *p; ++p)
4998 {
4999 /* skip escaped characters */
5000 if (p[1] && (*p == '\\' || *p == Ctrl_V))
5001 ++p;
5002 else if (vim_iswhite(*p))
5003 {
5004 *errormsgp = (char_u *)_("E172: Only one file name allowed");
5005 return FAIL;
5006 }
5007 }
5008#endif
5009
5010 /*
5011 * Halve the number of backslashes (this is Vi compatible).
5012 * For Unix and OS/2, when wildcards are expanded, this is
5013 * done by ExpandOne() below.
5014 */
5015#if defined(UNIX) || defined(OS2)
5016 if (!has_wildcards)
5017#endif
5018 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019 }
5020
5021 if (has_wildcards)
5022 {
5023 if (n == 1)
5024 {
5025 /*
5026 * First loop: May expand environment variables. This
5027 * can be done much faster with expand_env() than with
5028 * something else (e.g., calling a shell).
5029 * After expanding environment variables, check again
5030 * if there are still wildcards present.
5031 */
5032 if (vim_strchr(eap->arg, '$') != NULL
5033 || vim_strchr(eap->arg, '~') != NULL)
5034 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005035 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00005036 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005037 has_wildcards = mch_has_wildcard(NameBuff);
5038 p = NameBuff;
5039 }
5040 else
5041 p = NULL;
5042 }
5043 else /* n == 2 */
5044 {
5045 expand_T xpc;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005046 int options = WILD_LIST_NOTFOUND|WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047
5048 ExpandInit(&xpc);
5049 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005050 if (p_wic)
5051 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01005053 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054 if (p == NULL)
5055 return FAIL;
5056 }
5057 if (p != NULL)
5058 {
5059 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
5060 p, cmdlinep);
5061 if (n == 2) /* p came from ExpandOne() */
5062 vim_free(p);
5063 }
5064 }
5065 }
5066 }
5067 return OK;
5068}
5069
5070/*
5071 * Replace part of the command line, keeping eap->cmd, eap->arg and
5072 * eap->nextcmd correct.
5073 * "src" points to the part that is to be replaced, of length "srclen".
5074 * "repl" is the replacement string.
5075 * Returns a pointer to the character after the replaced string.
5076 * Returns NULL for failure.
5077 */
5078 static char_u *
5079repl_cmdline(eap, src, srclen, repl, cmdlinep)
5080 exarg_T *eap;
5081 char_u *src;
5082 int srclen;
5083 char_u *repl;
5084 char_u **cmdlinep;
5085{
5086 int len;
5087 int i;
5088 char_u *new_cmdline;
5089
5090 /*
5091 * The new command line is build in new_cmdline[].
5092 * First allocate it.
5093 * Careful: a "+cmd" argument may have been NUL terminated.
5094 */
5095 len = (int)STRLEN(repl);
5096 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005097 if (eap->nextcmd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005098 i += (int)STRLEN(eap->nextcmd);/* add space for next command */
5099 if ((new_cmdline = alloc((unsigned)i)) == NULL)
5100 return NULL; /* out of memory! */
5101
5102 /*
5103 * Copy the stuff before the expanded part.
5104 * Copy the expanded stuff.
5105 * Copy what came after the expanded part.
5106 * Copy the next commands, if there are any.
5107 */
5108 i = (int)(src - *cmdlinep); /* length of part before match */
5109 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00005110
Bram Moolenaar071d4272004-06-13 20:20:40 +00005111 mch_memmove(new_cmdline + i, repl, (size_t)len);
5112 i += len; /* remember the end of the string */
5113 STRCPY(new_cmdline + i, src + srclen);
5114 src = new_cmdline + i; /* remember where to continue */
5115
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005116 if (eap->nextcmd != NULL) /* append next command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005117 {
5118 i = (int)STRLEN(new_cmdline) + 1;
5119 STRCPY(new_cmdline + i, eap->nextcmd);
5120 eap->nextcmd = new_cmdline + i;
5121 }
5122 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
5123 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
5124 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
5125 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
5126 vim_free(*cmdlinep);
5127 *cmdlinep = new_cmdline;
5128
5129 return src;
5130}
5131
5132/*
5133 * Check for '|' to separate commands and '"' to start comments.
5134 */
5135 void
5136separate_nextcmd(eap)
5137 exarg_T *eap;
5138{
5139 char_u *p;
5140
Bram Moolenaar86b68352004-12-27 21:59:20 +00005141#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00005142 p = skip_grep_pat(eap);
5143#else
5144 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005145#endif
5146
5147 for ( ; *p; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005148 {
5149 if (*p == Ctrl_V)
5150 {
5151 if (eap->argt & (USECTRLV | XFILE))
5152 ++p; /* skip CTRL-V and next char */
5153 else
Bram Moolenaarb0db5692007-08-14 20:54:49 +00005154 /* remove CTRL-V and skip next char */
Bram Moolenaara7241f52008-06-24 20:39:31 +00005155 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005156 if (*p == NUL) /* stop at NUL after CTRL-V */
5157 break;
5158 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005159
5160#ifdef FEAT_EVAL
5161 /* Skip over `=expr` when wildcards are expanded. */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005162 else if (p[0] == '`' && p[1] == '=' && (eap->argt & XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005163 {
5164 p += 2;
5165 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005166 }
5167#endif
5168
Bram Moolenaar071d4272004-06-13 20:20:40 +00005169 /* Check for '"': start of comment or '|': next command */
5170 /* :@" and :*" do not start a comment!
5171 * :redir @" doesn't either. */
5172 else if ((*p == '"' && !(eap->argt & NOTRLCOM)
5173 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
5174 || p != eap->arg)
5175 && (eap->cmdidx != CMD_redir
5176 || p != eap->arg + 1 || p[-1] != '@'))
5177 || *p == '|' || *p == '\n')
5178 {
5179 /*
5180 * We remove the '\' before the '|', unless USECTRLV is used
5181 * AND 'b' is present in 'cpoptions'.
5182 */
5183 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
5184 || !(eap->argt & USECTRLV)) && *(p - 1) == '\\')
5185 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00005186 STRMOVE(p - 1, p); /* remove the '\' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005187 --p;
5188 }
5189 else
5190 {
5191 eap->nextcmd = check_nextcmd(p);
5192 *p = NUL;
5193 break;
5194 }
5195 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005196 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005197
Bram Moolenaar071d4272004-06-13 20:20:40 +00005198 if (!(eap->argt & NOTRLCOM)) /* remove trailing spaces */
5199 del_trailing_spaces(eap->arg);
5200}
5201
5202/*
5203 * get + command from ex argument
5204 */
5205 static char_u *
5206getargcmd(argp)
5207 char_u **argp;
5208{
5209 char_u *arg = *argp;
5210 char_u *command = NULL;
5211
5212 if (*arg == '+') /* +[command] */
5213 {
5214 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02005215 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005216 command = dollar_command;
5217 else
5218 {
5219 command = arg;
5220 arg = skip_cmd_arg(command, TRUE);
5221 if (*arg != NUL)
5222 *arg++ = NUL; /* terminate command with NUL */
5223 }
5224
5225 arg = skipwhite(arg); /* skip over spaces */
5226 *argp = arg;
5227 }
5228 return command;
5229}
5230
5231/*
5232 * Find end of "+command" argument. Skip over "\ " and "\\".
5233 */
5234 static char_u *
5235skip_cmd_arg(p, rembs)
5236 char_u *p;
5237 int rembs; /* TRUE to halve the number of backslashes */
5238{
5239 while (*p && !vim_isspace(*p))
5240 {
5241 if (*p == '\\' && p[1] != NUL)
5242 {
5243 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00005244 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005245 else
5246 ++p;
5247 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005248 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005249 }
5250 return p;
5251}
5252
5253/*
5254 * Get "++opt=arg" argument.
5255 * Return FAIL or OK.
5256 */
5257 static int
5258getargopt(eap)
5259 exarg_T *eap;
5260{
5261 char_u *arg = eap->arg + 2;
5262 int *pp = NULL;
5263#ifdef FEAT_MBYTE
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005264 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005265 char_u *p;
5266#endif
5267
5268 /* ":edit ++[no]bin[ary] file" */
5269 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
5270 {
5271 if (*arg == 'n')
5272 {
5273 arg += 2;
5274 eap->force_bin = FORCE_NOBIN;
5275 }
5276 else
5277 eap->force_bin = FORCE_BIN;
5278 if (!checkforcmd(&arg, "binary", 3))
5279 return FAIL;
5280 eap->arg = skipwhite(arg);
5281 return OK;
5282 }
5283
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005284 /* ":read ++edit file" */
5285 if (STRNCMP(arg, "edit", 4) == 0)
5286 {
5287 eap->read_edit = TRUE;
5288 eap->arg = skipwhite(arg + 4);
5289 return OK;
5290 }
5291
Bram Moolenaar071d4272004-06-13 20:20:40 +00005292 if (STRNCMP(arg, "ff", 2) == 0)
5293 {
5294 arg += 2;
5295 pp = &eap->force_ff;
5296 }
5297 else if (STRNCMP(arg, "fileformat", 10) == 0)
5298 {
5299 arg += 10;
5300 pp = &eap->force_ff;
5301 }
5302#ifdef FEAT_MBYTE
5303 else if (STRNCMP(arg, "enc", 3) == 0)
5304 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01005305 if (STRNCMP(arg, "encoding", 8) == 0)
5306 arg += 8;
5307 else
5308 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005309 pp = &eap->force_enc;
5310 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005311 else if (STRNCMP(arg, "bad", 3) == 0)
5312 {
5313 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005314 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005315 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005316#endif
5317
5318 if (pp == NULL || *arg != '=')
5319 return FAIL;
5320
5321 ++arg;
5322 *pp = (int)(arg - eap->cmd);
5323 arg = skip_cmd_arg(arg, FALSE);
5324 eap->arg = skipwhite(arg);
5325 *arg = NUL;
5326
5327#ifdef FEAT_MBYTE
5328 if (pp == &eap->force_ff)
5329 {
5330#endif
5331 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
5332 return FAIL;
5333#ifdef FEAT_MBYTE
5334 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005335 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005336 {
5337 /* Make 'fileencoding' lower case. */
5338 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
5339 *p = TOLOWER_ASC(*p);
5340 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005341 else
5342 {
5343 /* Check ++bad= argument. Must be a single-byte character, "keep" or
5344 * "drop". */
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005345 p = eap->cmd + bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005346 if (STRICMP(p, "keep") == 0)
5347 eap->bad_char = BAD_KEEP;
5348 else if (STRICMP(p, "drop") == 0)
5349 eap->bad_char = BAD_DROP;
5350 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
5351 eap->bad_char = *p;
5352 else
5353 return FAIL;
5354 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005355#endif
5356
5357 return OK;
5358}
5359
5360/*
5361 * ":abbreviate" and friends.
5362 */
5363 static void
5364ex_abbreviate(eap)
5365 exarg_T *eap;
5366{
5367 do_exmap(eap, TRUE); /* almost the same as mapping */
5368}
5369
5370/*
5371 * ":map" and friends.
5372 */
5373 static void
5374ex_map(eap)
5375 exarg_T *eap;
5376{
5377 /*
5378 * If we are sourcing .exrc or .vimrc in current directory we
5379 * print the mappings for security reasons.
5380 */
5381 if (secure)
5382 {
5383 secure = 2;
5384 msg_outtrans(eap->cmd);
5385 msg_putchar('\n');
5386 }
5387 do_exmap(eap, FALSE);
5388}
5389
5390/*
5391 * ":unmap" and friends.
5392 */
5393 static void
5394ex_unmap(eap)
5395 exarg_T *eap;
5396{
5397 do_exmap(eap, FALSE);
5398}
5399
5400/*
5401 * ":mapclear" and friends.
5402 */
5403 static void
5404ex_mapclear(eap)
5405 exarg_T *eap;
5406{
5407 map_clear(eap->cmd, eap->arg, eap->forceit, FALSE);
5408}
5409
5410/*
5411 * ":abclear" and friends.
5412 */
5413 static void
5414ex_abclear(eap)
5415 exarg_T *eap;
5416{
5417 map_clear(eap->cmd, eap->arg, TRUE, TRUE);
5418}
5419
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005420#if defined(FEAT_AUTOCMD) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005421 static void
5422ex_autocmd(eap)
5423 exarg_T *eap;
5424{
5425 /*
5426 * Disallow auto commands from .exrc and .vimrc in current
5427 * directory for security reasons.
5428 */
5429 if (secure)
5430 {
5431 secure = 2;
5432 eap->errmsg = e_curdir;
5433 }
5434 else if (eap->cmdidx == CMD_autocmd)
5435 do_autocmd(eap->arg, eap->forceit);
5436 else
5437 do_augroup(eap->arg, eap->forceit);
5438}
5439
5440/*
5441 * ":doautocmd": Apply the automatic commands to the current buffer.
5442 */
5443 static void
5444ex_doautocmd(eap)
5445 exarg_T *eap;
5446{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005447 char_u *arg = eap->arg;
5448 int call_do_modelines = check_nomodeline(&arg);
5449
5450 (void)do_doautocmd(arg, TRUE);
5451 if (call_do_modelines) /* Only when there is no <nomodeline>. */
5452 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005453}
5454#endif
5455
5456#ifdef FEAT_LISTCMDS
5457/*
5458 * :[N]bunload[!] [N] [bufname] unload buffer
5459 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
5460 * :[N]bwipeout[!] [N] [bufname] delete buffer really
5461 */
5462 static void
5463ex_bunload(eap)
5464 exarg_T *eap;
5465{
5466 eap->errmsg = do_bufdel(
5467 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
5468 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
5469 : DOBUF_UNLOAD, eap->arg,
5470 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
5471}
5472
5473/*
5474 * :[N]buffer [N] to buffer N
5475 * :[N]sbuffer [N] to buffer N
5476 */
5477 static void
5478ex_buffer(eap)
5479 exarg_T *eap;
5480{
5481 if (*eap->arg)
5482 eap->errmsg = e_trailing;
5483 else
5484 {
5485 if (eap->addr_count == 0) /* default is current buffer */
5486 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
5487 else
5488 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005489 if (eap->do_ecmd_cmd != NULL)
5490 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005491 }
5492}
5493
5494/*
5495 * :[N]bmodified [N] to next mod. buffer
5496 * :[N]sbmodified [N] to next mod. buffer
5497 */
5498 static void
5499ex_bmodified(eap)
5500 exarg_T *eap;
5501{
5502 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005503 if (eap->do_ecmd_cmd != NULL)
5504 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005505}
5506
5507/*
5508 * :[N]bnext [N] to next buffer
5509 * :[N]sbnext [N] split and to next buffer
5510 */
5511 static void
5512ex_bnext(eap)
5513 exarg_T *eap;
5514{
5515 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005516 if (eap->do_ecmd_cmd != NULL)
5517 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005518}
5519
5520/*
5521 * :[N]bNext [N] to previous buffer
5522 * :[N]bprevious [N] to previous buffer
5523 * :[N]sbNext [N] split and to previous buffer
5524 * :[N]sbprevious [N] split and to previous buffer
5525 */
5526 static void
5527ex_bprevious(eap)
5528 exarg_T *eap;
5529{
5530 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005531 if (eap->do_ecmd_cmd != NULL)
5532 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005533}
5534
5535/*
5536 * :brewind to first buffer
5537 * :bfirst to first buffer
5538 * :sbrewind split and to first buffer
5539 * :sbfirst split and to first buffer
5540 */
5541 static void
5542ex_brewind(eap)
5543 exarg_T *eap;
5544{
5545 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005546 if (eap->do_ecmd_cmd != NULL)
5547 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005548}
5549
5550/*
5551 * :blast to last buffer
5552 * :sblast split and to last buffer
5553 */
5554 static void
5555ex_blast(eap)
5556 exarg_T *eap;
5557{
5558 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005559 if (eap->do_ecmd_cmd != NULL)
5560 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005561}
5562#endif
5563
5564 int
5565ends_excmd(c)
5566 int c;
5567{
5568 return (c == NUL || c == '|' || c == '"' || c == '\n');
5569}
5570
5571#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5572 || defined(PROTO)
5573/*
5574 * Return the next command, after the first '|' or '\n'.
5575 * Return NULL if not found.
5576 */
5577 char_u *
5578find_nextcmd(p)
5579 char_u *p;
5580{
5581 while (*p != '|' && *p != '\n')
5582 {
5583 if (*p == NUL)
5584 return NULL;
5585 ++p;
5586 }
5587 return (p + 1);
5588}
5589#endif
5590
5591/*
5592 * Check if *p is a separator between Ex commands.
5593 * Return NULL if it isn't, (p + 1) if it is.
5594 */
5595 char_u *
5596check_nextcmd(p)
5597 char_u *p;
5598{
5599 p = skipwhite(p);
5600 if (*p == '|' || *p == '\n')
5601 return (p + 1);
5602 else
5603 return NULL;
5604}
5605
5606/*
5607 * - if there are more files to edit
5608 * - and this is the last window
5609 * - and forceit not used
5610 * - and not repeated twice on a row
5611 * return FAIL and give error message if 'message' TRUE
5612 * return OK otherwise
5613 */
5614 static int
5615check_more(message, forceit)
5616 int message; /* when FALSE check only, no messages */
5617 int forceit;
5618{
5619 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5620
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005621 if (!forceit && only_one_window()
5622 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005623 {
5624 if (message)
5625 {
5626#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
5627 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
5628 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005629 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005630
5631 if (n == 1)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02005632 vim_strncpy(buff,
5633 (char_u *)_("1 more file to edit. Quit anyway?"),
Bram Moolenaard9462e32011-04-11 21:35:11 +02005634 DIALOG_MSG_SIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005635 else
Bram Moolenaard9462e32011-04-11 21:35:11 +02005636 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005637 _("%d more files to edit. Quit anyway?"), n);
5638 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5639 return OK;
5640 return FAIL;
5641 }
5642#endif
5643 if (n == 1)
5644 EMSG(_("E173: 1 more file to edit"));
5645 else
5646 EMSGN(_("E173: %ld more files to edit"), n);
5647 quitmore = 2; /* next try to quit is allowed */
5648 }
5649 return FAIL;
5650 }
5651 return OK;
5652}
5653
5654#ifdef FEAT_CMDL_COMPL
5655/*
5656 * Function given to ExpandGeneric() to obtain the list of command names.
5657 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005658 char_u *
5659get_command_name(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00005660 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005661 int idx;
5662{
5663 if (idx >= (int)CMD_SIZE)
5664# ifdef FEAT_USR_CMDS
5665 return get_user_command_name(idx);
5666# else
5667 return NULL;
5668# endif
5669 return cmdnames[idx].cmd_name;
5670}
5671#endif
5672
5673#if defined(FEAT_USR_CMDS) || defined(PROTO)
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005674static 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 +00005675static void uc_list __ARGS((char_u *name, size_t name_len));
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005676static 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 +00005677static char_u *uc_split_args __ARGS((char_u *arg, size_t *lenp));
5678static 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));
5679
5680 static int
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005681uc_add_command(name, name_len, rep, argt, def, flags, compl, compl_arg, addr_type, force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005682 char_u *name;
5683 size_t name_len;
5684 char_u *rep;
5685 long argt;
5686 long def;
5687 int flags;
5688 int compl;
5689 char_u *compl_arg;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005690 int addr_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005691 int force;
5692{
5693 ucmd_T *cmd = NULL;
5694 char_u *p;
5695 int i;
5696 int cmp = 1;
5697 char_u *rep_buf = NULL;
5698 garray_T *gap;
5699
Bram Moolenaar9c102382006-05-03 21:26:49 +00005700 replace_termcodes(rep, &rep_buf, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005701 if (rep_buf == NULL)
5702 {
5703 /* Can't replace termcodes - try using the string as is */
5704 rep_buf = vim_strsave(rep);
5705
5706 /* Give up if out of memory */
5707 if (rep_buf == NULL)
5708 return FAIL;
5709 }
5710
5711 /* get address of growarray: global or in curbuf */
5712 if (flags & UC_BUFFER)
5713 {
5714 gap = &curbuf->b_ucmds;
5715 if (gap->ga_itemsize == 0)
5716 ga_init2(gap, (int)sizeof(ucmd_T), 4);
5717 }
5718 else
5719 gap = &ucmds;
5720
5721 /* Search for the command in the already defined commands. */
5722 for (i = 0; i < gap->ga_len; ++i)
5723 {
5724 size_t len;
5725
5726 cmd = USER_CMD_GA(gap, i);
5727 len = STRLEN(cmd->uc_name);
5728 cmp = STRNCMP(name, cmd->uc_name, name_len);
5729 if (cmp == 0)
5730 {
5731 if (name_len < len)
5732 cmp = -1;
5733 else if (name_len > len)
5734 cmp = 1;
5735 }
5736
5737 if (cmp == 0)
5738 {
5739 if (!force)
5740 {
5741 EMSG(_("E174: Command already exists: add ! to replace it"));
5742 goto fail;
5743 }
5744
5745 vim_free(cmd->uc_rep);
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00005746 cmd->uc_rep = NULL;
5747#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5748 vim_free(cmd->uc_compl_arg);
5749 cmd->uc_compl_arg = NULL;
5750#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005751 break;
5752 }
5753
5754 /* Stop as soon as we pass the name to add */
5755 if (cmp < 0)
5756 break;
5757 }
5758
5759 /* Extend the array unless we're replacing an existing command */
5760 if (cmp != 0)
5761 {
5762 if (ga_grow(gap, 1) != OK)
5763 goto fail;
5764 if ((p = vim_strnsave(name, (int)name_len)) == NULL)
5765 goto fail;
5766
5767 cmd = USER_CMD_GA(gap, i);
5768 mch_memmove(cmd + 1, cmd, (gap->ga_len - i) * sizeof(ucmd_T));
5769
5770 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005771
5772 cmd->uc_name = p;
5773 }
5774
5775 cmd->uc_rep = rep_buf;
5776 cmd->uc_argt = argt;
5777 cmd->uc_def = def;
5778 cmd->uc_compl = compl;
5779#ifdef FEAT_EVAL
5780 cmd->uc_scriptID = current_SID;
5781# ifdef FEAT_CMDL_COMPL
5782 cmd->uc_compl_arg = compl_arg;
5783# endif
5784#endif
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005785 cmd->uc_addr_type = addr_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005786
5787 return OK;
5788
5789fail:
5790 vim_free(rep_buf);
5791#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5792 vim_free(compl_arg);
5793#endif
5794 return FAIL;
5795}
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005796#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005797
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005798#if defined(FEAT_USR_CMDS)
5799static struct
5800{
5801 int expand;
5802 char *name;
5803} addr_type_complete[] =
5804{
5805 {ADDR_ARGUMENTS, "arguments"},
5806 {ADDR_LINES, "lines"},
5807 {ADDR_LOADED_BUFFERS, "loaded_buffers"},
5808 {ADDR_TABS, "tabs"},
5809 {ADDR_BUFFERS, "buffers"},
5810 {ADDR_WINDOWS, "windows"},
5811 {-1, NULL}
5812};
5813#endif
5814
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005815#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005816/*
5817 * List of names for completion for ":command" with the EXPAND_ flag.
5818 * Must be alphabetical for completion.
5819 */
5820static struct
5821{
5822 int expand;
5823 char *name;
5824} command_complete[] =
5825{
5826 {EXPAND_AUGROUP, "augroup"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005827 {EXPAND_BEHAVE, "behave"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005828 {EXPAND_BUFFERS, "buffer"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005829 {EXPAND_COLORS, "color"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005830 {EXPAND_COMMANDS, "command"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005831 {EXPAND_COMPILER, "compiler"},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005832#if defined(FEAT_CSCOPE)
5833 {EXPAND_CSCOPE, "cscope"},
5834#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005835#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5836 {EXPAND_USER_DEFINED, "custom"},
Bram Moolenaara466c992005-07-09 21:03:22 +00005837 {EXPAND_USER_LIST, "customlist"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005838#endif
5839 {EXPAND_DIRECTORIES, "dir"},
5840 {EXPAND_ENV_VARS, "environment"},
5841 {EXPAND_EVENTS, "event"},
5842 {EXPAND_EXPRESSION, "expression"},
5843 {EXPAND_FILES, "file"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005844 {EXPAND_FILES_IN_PATH, "file_in_path"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005845 {EXPAND_FILETYPE, "filetype"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005846 {EXPAND_FUNCTIONS, "function"},
5847 {EXPAND_HELP, "help"},
5848 {EXPAND_HIGHLIGHT, "highlight"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005849#if defined(FEAT_CMDHIST)
5850 {EXPAND_HISTORY, "history"},
5851#endif
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005852#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005853 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005854 {EXPAND_LOCALES, "locale"},
5855#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005856 {EXPAND_MAPPINGS, "mapping"},
5857 {EXPAND_MENUS, "menu"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005858 {EXPAND_OWNSYNTAX, "syntax"},
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02005859#if defined(FEAT_PROFILE)
5860 {EXPAND_SYNTIME, "syntime"},
5861#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005862 {EXPAND_SETTINGS, "option"},
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005863 {EXPAND_SHELLCMD, "shellcmd"},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005864#if defined(FEAT_SIGNS)
5865 {EXPAND_SIGN, "sign"},
5866#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005867 {EXPAND_TAGS, "tag"},
5868 {EXPAND_TAGS_LISTFILES, "tag_listfiles"},
Bram Moolenaar24305862012-08-15 14:05:05 +02005869 {EXPAND_USER, "user"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005870 {EXPAND_USER_VARS, "var"},
5871 {0, NULL}
5872};
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005873#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005874
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005875#if defined(FEAT_USR_CMDS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005876 static void
5877uc_list(name, name_len)
5878 char_u *name;
5879 size_t name_len;
5880{
5881 int i, j;
5882 int found = FALSE;
5883 ucmd_T *cmd;
5884 int len;
5885 long a;
5886 garray_T *gap;
5887
5888 gap = &curbuf->b_ucmds;
5889 for (;;)
5890 {
5891 for (i = 0; i < gap->ga_len; ++i)
5892 {
5893 cmd = USER_CMD_GA(gap, i);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005894 a = (long)cmd->uc_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005895
5896 /* Skip commands which don't match the requested prefix */
5897 if (STRNCMP(name, cmd->uc_name, name_len) != 0)
5898 continue;
5899
5900 /* Put out the title first time */
5901 if (!found)
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005902 MSG_PUTS_TITLE(_("\n Name Args Address Complete Definition"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005903 found = TRUE;
5904 msg_putchar('\n');
5905 if (got_int)
5906 break;
5907
5908 /* Special cases */
5909 msg_putchar(a & BANG ? '!' : ' ');
5910 msg_putchar(a & REGSTR ? '"' : ' ');
5911 msg_putchar(gap != &ucmds ? 'b' : ' ');
5912 msg_putchar(' ');
5913
5914 msg_outtrans_attr(cmd->uc_name, hl_attr(HLF_D));
5915 len = (int)STRLEN(cmd->uc_name) + 4;
5916
5917 do {
5918 msg_putchar(' ');
5919 ++len;
5920 } while (len < 16);
5921
5922 len = 0;
5923
5924 /* Arguments */
5925 switch ((int)(a & (EXTRA|NOSPC|NEEDARG)))
5926 {
5927 case 0: IObuff[len++] = '0'; break;
5928 case (EXTRA): IObuff[len++] = '*'; break;
5929 case (EXTRA|NOSPC): IObuff[len++] = '?'; break;
5930 case (EXTRA|NEEDARG): IObuff[len++] = '+'; break;
5931 case (EXTRA|NOSPC|NEEDARG): IObuff[len++] = '1'; break;
5932 }
5933
5934 do {
5935 IObuff[len++] = ' ';
5936 } while (len < 5);
5937
5938 /* Range */
5939 if (a & (RANGE|COUNT))
5940 {
5941 if (a & COUNT)
5942 {
5943 /* -count=N */
5944 sprintf((char *)IObuff + len, "%ldc", cmd->uc_def);
5945 len += (int)STRLEN(IObuff + len);
5946 }
5947 else if (a & DFLALL)
5948 IObuff[len++] = '%';
5949 else if (cmd->uc_def >= 0)
5950 {
5951 /* -range=N */
5952 sprintf((char *)IObuff + len, "%ld", cmd->uc_def);
5953 len += (int)STRLEN(IObuff + len);
5954 }
5955 else
5956 IObuff[len++] = '.';
5957 }
5958
5959 do {
5960 IObuff[len++] = ' ';
5961 } while (len < 11);
5962
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005963 /* Address Type */
5964 for (j = 0; addr_type_complete[j].expand != -1; ++j)
5965 if (addr_type_complete[j].expand != ADDR_LINES
5966 && addr_type_complete[j].expand == cmd->uc_addr_type)
5967 {
5968 STRCPY(IObuff + len, addr_type_complete[j].name);
5969 len += (int)STRLEN(IObuff + len);
5970 break;
5971 }
5972
5973 do {
5974 IObuff[len++] = ' ';
5975 } while (len < 21);
5976
Bram Moolenaar071d4272004-06-13 20:20:40 +00005977 /* Completion */
5978 for (j = 0; command_complete[j].expand != 0; ++j)
5979 if (command_complete[j].expand == cmd->uc_compl)
5980 {
5981 STRCPY(IObuff + len, command_complete[j].name);
5982 len += (int)STRLEN(IObuff + len);
5983 break;
5984 }
5985
5986 do {
5987 IObuff[len++] = ' ';
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005988 } while (len < 35);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005989
5990 IObuff[len] = '\0';
5991 msg_outtrans(IObuff);
5992
5993 msg_outtrans_special(cmd->uc_rep, FALSE);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005994#ifdef FEAT_EVAL
5995 if (p_verbose > 0)
5996 last_set_msg(cmd->uc_scriptID);
5997#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005998 out_flush();
5999 ui_breakcheck();
6000 if (got_int)
6001 break;
6002 }
6003 if (gap == &ucmds || i < gap->ga_len)
6004 break;
6005 gap = &ucmds;
6006 }
6007
6008 if (!found)
6009 MSG(_("No user-defined commands found"));
6010}
6011
6012 static char_u *
6013uc_fun_cmd()
6014{
6015 static char_u fcmd[] = {0x84, 0xaf, 0x60, 0xb9, 0xaf, 0xb5, 0x60, 0xa4,
6016 0xa5, 0xad, 0xa1, 0xae, 0xa4, 0x60, 0xa1, 0x60,
6017 0xb3, 0xa8, 0xb2, 0xb5, 0xa2, 0xa2, 0xa5, 0xb2,
6018 0xb9, 0x7f, 0};
6019 int i;
6020
6021 for (i = 0; fcmd[i]; ++i)
6022 IObuff[i] = fcmd[i] - 0x40;
6023 IObuff[i] = 0;
6024 return IObuff;
6025}
6026
6027 static int
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006028uc_scan_attr(attr, len, argt, def, flags, compl, compl_arg, addr_type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006029 char_u *attr;
6030 size_t len;
6031 long *argt;
6032 long *def;
6033 int *flags;
6034 int *compl;
6035 char_u **compl_arg;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006036 int *addr_type_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006037{
6038 char_u *p;
6039
6040 if (len == 0)
6041 {
6042 EMSG(_("E175: No attribute specified"));
6043 return FAIL;
6044 }
6045
6046 /* First, try the simple attributes (no arguments) */
6047 if (STRNICMP(attr, "bang", len) == 0)
6048 *argt |= BANG;
6049 else if (STRNICMP(attr, "buffer", len) == 0)
6050 *flags |= UC_BUFFER;
6051 else if (STRNICMP(attr, "register", len) == 0)
6052 *argt |= REGSTR;
6053 else if (STRNICMP(attr, "bar", len) == 0)
6054 *argt |= TRLBAR;
6055 else
6056 {
6057 int i;
6058 char_u *val = NULL;
6059 size_t vallen = 0;
6060 size_t attrlen = len;
6061
6062 /* Look for the attribute name - which is the part before any '=' */
6063 for (i = 0; i < (int)len; ++i)
6064 {
6065 if (attr[i] == '=')
6066 {
6067 val = &attr[i + 1];
6068 vallen = len - i - 1;
6069 attrlen = i;
6070 break;
6071 }
6072 }
6073
6074 if (STRNICMP(attr, "nargs", attrlen) == 0)
6075 {
6076 if (vallen == 1)
6077 {
6078 if (*val == '0')
6079 /* Do nothing - this is the default */;
6080 else if (*val == '1')
6081 *argt |= (EXTRA | NOSPC | NEEDARG);
6082 else if (*val == '*')
6083 *argt |= EXTRA;
6084 else if (*val == '?')
6085 *argt |= (EXTRA | NOSPC);
6086 else if (*val == '+')
6087 *argt |= (EXTRA | NEEDARG);
6088 else
6089 goto wrong_nargs;
6090 }
6091 else
6092 {
6093wrong_nargs:
6094 EMSG(_("E176: Invalid number of arguments"));
6095 return FAIL;
6096 }
6097 }
6098 else if (STRNICMP(attr, "range", attrlen) == 0)
6099 {
6100 *argt |= RANGE;
6101 if (vallen == 1 && *val == '%')
6102 *argt |= DFLALL;
6103 else if (val != NULL)
6104 {
6105 p = val;
6106 if (*def >= 0)
6107 {
6108two_count:
6109 EMSG(_("E177: Count cannot be specified twice"));
6110 return FAIL;
6111 }
6112
6113 *def = getdigits(&p);
6114 *argt |= (ZEROR | NOTADR);
6115
6116 if (p != val + vallen || vallen == 0)
6117 {
6118invalid_count:
6119 EMSG(_("E178: Invalid default value for count"));
6120 return FAIL;
6121 }
6122 }
6123 }
6124 else if (STRNICMP(attr, "count", attrlen) == 0)
6125 {
Bram Moolenaar32e7b2d2005-02-27 22:36:47 +00006126 *argt |= (COUNT | ZEROR | RANGE | NOTADR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006127
6128 if (val != NULL)
6129 {
6130 p = val;
6131 if (*def >= 0)
6132 goto two_count;
6133
6134 *def = getdigits(&p);
6135
6136 if (p != val + vallen)
6137 goto invalid_count;
6138 }
6139
6140 if (*def < 0)
6141 *def = 0;
6142 }
6143 else if (STRNICMP(attr, "complete", attrlen) == 0)
6144 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006145 if (val == NULL)
6146 {
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006147 EMSG(_("E179: argument required for -complete"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006148 return FAIL;
6149 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006150
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006151 if (parse_compl_arg(val, (int)vallen, compl, argt, compl_arg)
6152 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006153 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006154 }
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006155 else if (STRNICMP(attr, "addr", attrlen) == 0)
6156 {
6157 *argt |= RANGE;
6158 if (val == NULL)
6159 {
6160 EMSG(_("E179: argument required for -addr"));
6161 return FAIL;
6162 }
6163 if (parse_addr_type_arg(val, (int)vallen, argt, addr_type_arg)
6164 == FAIL)
6165 return FAIL;
6166 if (addr_type_arg != ADDR_LINES)
6167 *argt |= (ZEROR | NOTADR) ;
6168 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006169 else
6170 {
6171 char_u ch = attr[len];
6172 attr[len] = '\0';
6173 EMSG2(_("E181: Invalid attribute: %s"), attr);
6174 attr[len] = ch;
6175 return FAIL;
6176 }
6177 }
6178
6179 return OK;
6180}
6181
Bram Moolenaara850a712009-01-28 14:42:59 +00006182/*
6183 * ":command ..."
6184 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006185 static void
6186ex_command(eap)
6187 exarg_T *eap;
6188{
6189 char_u *name;
6190 char_u *end;
6191 char_u *p;
6192 long argt = 0;
6193 long def = -1;
6194 int flags = 0;
6195 int compl = EXPAND_NOTHING;
6196 char_u *compl_arg = NULL;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006197 int addr_type_arg = ADDR_LINES;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006198 int has_attr = (eap->arg[0] == '-');
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006199 int name_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006200
6201 p = eap->arg;
6202
6203 /* Check for attributes */
6204 while (*p == '-')
6205 {
6206 ++p;
6207 end = skiptowhite(p);
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006208 if (uc_scan_attr(p, end - p, &argt, &def, &flags, &compl, &compl_arg, &addr_type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006209 == FAIL)
6210 return;
6211 p = skipwhite(end);
6212 }
6213
6214 /* Get the name (if any) and skip to the following argument */
6215 name = p;
6216 if (ASCII_ISALPHA(*p))
6217 while (ASCII_ISALNUM(*p))
6218 ++p;
6219 if (!ends_excmd(*p) && !vim_iswhite(*p))
6220 {
6221 EMSG(_("E182: Invalid command name"));
6222 return;
6223 }
6224 end = p;
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006225 name_len = (int)(end - name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006226
6227 /* If there is nothing after the name, and no attributes were specified,
6228 * we are listing commands
6229 */
6230 p = skipwhite(end);
6231 if (!has_attr && ends_excmd(*p))
6232 {
6233 uc_list(name, end - name);
6234 }
6235 else if (!ASCII_ISUPPER(*name))
6236 {
6237 EMSG(_("E183: User defined commands must start with an uppercase letter"));
6238 return;
6239 }
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006240 else if ((name_len == 1 && *name == 'X')
6241 || (name_len <= 4
6242 && STRNCMP(name, "Next", name_len > 4 ? 4 : name_len) == 0))
6243 {
6244 EMSG(_("E841: Reserved name, cannot be used for user defined command"));
6245 return;
6246 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006247 else
6248 uc_add_command(name, end - name, p, argt, def, flags, compl, compl_arg,
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006249 addr_type_arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006250}
6251
6252/*
6253 * ":comclear"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006254 * Clear all user commands, global and for current buffer.
6255 */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00006256 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00006257ex_comclear(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006258 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006259{
6260 uc_clear(&ucmds);
6261 uc_clear(&curbuf->b_ucmds);
6262}
6263
6264/*
6265 * Clear all user commands for "gap".
6266 */
6267 void
6268uc_clear(gap)
6269 garray_T *gap;
6270{
6271 int i;
6272 ucmd_T *cmd;
6273
6274 for (i = 0; i < gap->ga_len; ++i)
6275 {
6276 cmd = USER_CMD_GA(gap, i);
6277 vim_free(cmd->uc_name);
6278 vim_free(cmd->uc_rep);
6279# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6280 vim_free(cmd->uc_compl_arg);
6281# endif
6282 }
6283 ga_clear(gap);
6284}
6285
6286 static void
6287ex_delcommand(eap)
6288 exarg_T *eap;
6289{
6290 int i = 0;
6291 ucmd_T *cmd = NULL;
6292 int cmp = -1;
6293 garray_T *gap;
6294
6295 gap = &curbuf->b_ucmds;
6296 for (;;)
6297 {
6298 for (i = 0; i < gap->ga_len; ++i)
6299 {
6300 cmd = USER_CMD_GA(gap, i);
6301 cmp = STRCMP(eap->arg, cmd->uc_name);
6302 if (cmp <= 0)
6303 break;
6304 }
6305 if (gap == &ucmds || cmp == 0)
6306 break;
6307 gap = &ucmds;
6308 }
6309
6310 if (cmp != 0)
6311 {
6312 EMSG2(_("E184: No such user-defined command: %s"), eap->arg);
6313 return;
6314 }
6315
6316 vim_free(cmd->uc_name);
6317 vim_free(cmd->uc_rep);
6318# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6319 vim_free(cmd->uc_compl_arg);
6320# endif
6321
6322 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006323
6324 if (i < gap->ga_len)
6325 mch_memmove(cmd, cmd + 1, (gap->ga_len - i) * sizeof(ucmd_T));
6326}
6327
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006328/*
6329 * split and quote args for <f-args>
6330 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006331 static char_u *
6332uc_split_args(arg, lenp)
6333 char_u *arg;
6334 size_t *lenp;
6335{
6336 char_u *buf;
6337 char_u *p;
6338 char_u *q;
6339 int len;
6340
6341 /* Precalculate length */
6342 p = arg;
6343 len = 2; /* Initial and final quotes */
6344
6345 while (*p)
6346 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006347 if (p[0] == '\\' && p[1] == '\\')
6348 {
6349 len += 2;
6350 p += 2;
6351 }
6352 else if (p[0] == '\\' && vim_iswhite(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006353 {
6354 len += 1;
6355 p += 2;
6356 }
6357 else if (*p == '\\' || *p == '"')
6358 {
6359 len += 2;
6360 p += 1;
6361 }
6362 else if (vim_iswhite(*p))
6363 {
6364 p = skipwhite(p);
6365 if (*p == NUL)
6366 break;
6367 len += 3; /* "," */
6368 }
6369 else
6370 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02006371#ifdef FEAT_MBYTE
6372 int charlen = (*mb_ptr2len)(p);
6373 len += charlen;
6374 p += charlen;
6375#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006376 ++len;
6377 ++p;
Bram Moolenaardfef1542012-07-10 19:25:10 +02006378#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006379 }
6380 }
6381
6382 buf = alloc(len + 1);
6383 if (buf == NULL)
6384 {
6385 *lenp = 0;
6386 return buf;
6387 }
6388
6389 p = arg;
6390 q = buf;
6391 *q++ = '"';
6392 while (*p)
6393 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006394 if (p[0] == '\\' && p[1] == '\\')
6395 {
6396 *q++ = '\\';
6397 *q++ = '\\';
6398 p += 2;
6399 }
6400 else if (p[0] == '\\' && vim_iswhite(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006401 {
6402 *q++ = p[1];
6403 p += 2;
6404 }
6405 else if (*p == '\\' || *p == '"')
6406 {
6407 *q++ = '\\';
6408 *q++ = *p++;
6409 }
6410 else if (vim_iswhite(*p))
6411 {
6412 p = skipwhite(p);
6413 if (*p == NUL)
6414 break;
6415 *q++ = '"';
6416 *q++ = ',';
6417 *q++ = '"';
6418 }
6419 else
6420 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02006421 MB_COPY_CHAR(p, q);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006422 }
6423 }
6424 *q++ = '"';
6425 *q = 0;
6426
6427 *lenp = len;
6428 return buf;
6429}
6430
6431/*
6432 * Check for a <> code in a user command.
6433 * "code" points to the '<'. "len" the length of the <> (inclusive).
6434 * "buf" is where the result is to be added.
6435 * "split_buf" points to a buffer used for splitting, caller should free it.
6436 * "split_len" is the length of what "split_buf" contains.
6437 * Returns the length of the replacement, which has been added to "buf".
6438 * Returns -1 if there was no match, and only the "<" has been copied.
6439 */
6440 static size_t
6441uc_check_code(code, len, buf, cmd, eap, split_buf, split_len)
6442 char_u *code;
6443 size_t len;
6444 char_u *buf;
6445 ucmd_T *cmd; /* the user command we're expanding */
6446 exarg_T *eap; /* ex arguments */
6447 char_u **split_buf;
6448 size_t *split_len;
6449{
6450 size_t result = 0;
6451 char_u *p = code + 1;
6452 size_t l = len - 2;
6453 int quote = 0;
6454 enum { ct_ARGS, ct_BANG, ct_COUNT, ct_LINE1, ct_LINE2, ct_REGISTER,
6455 ct_LT, ct_NONE } type = ct_NONE;
6456
6457 if ((vim_strchr((char_u *)"qQfF", *p) != NULL) && p[1] == '-')
6458 {
6459 quote = (*p == 'q' || *p == 'Q') ? 1 : 2;
6460 p += 2;
6461 l -= 2;
6462 }
6463
Bram Moolenaar371d5402006-03-20 21:47:49 +00006464 ++l;
6465 if (l <= 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006466 type = ct_NONE;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006467 else if (STRNICMP(p, "args>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006468 type = ct_ARGS;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006469 else if (STRNICMP(p, "bang>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006470 type = ct_BANG;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006471 else if (STRNICMP(p, "count>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006472 type = ct_COUNT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006473 else if (STRNICMP(p, "line1>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006474 type = ct_LINE1;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006475 else if (STRNICMP(p, "line2>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006476 type = ct_LINE2;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006477 else if (STRNICMP(p, "lt>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006478 type = ct_LT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006479 else if (STRNICMP(p, "reg>", l) == 0 || STRNICMP(p, "register>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006480 type = ct_REGISTER;
6481
6482 switch (type)
6483 {
6484 case ct_ARGS:
6485 /* Simple case first */
6486 if (*eap->arg == NUL)
6487 {
6488 if (quote == 1)
6489 {
6490 result = 2;
6491 if (buf != NULL)
6492 STRCPY(buf, "''");
6493 }
6494 else
6495 result = 0;
6496 break;
6497 }
6498
6499 /* When specified there is a single argument don't split it.
6500 * Works for ":Cmd %" when % is "a b c". */
6501 if ((eap->argt & NOSPC) && quote == 2)
6502 quote = 1;
6503
6504 switch (quote)
6505 {
6506 case 0: /* No quoting, no splitting */
6507 result = STRLEN(eap->arg);
6508 if (buf != NULL)
6509 STRCPY(buf, eap->arg);
6510 break;
6511 case 1: /* Quote, but don't split */
6512 result = STRLEN(eap->arg) + 2;
6513 for (p = eap->arg; *p; ++p)
6514 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006515#ifdef FEAT_MBYTE
6516 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6517 /* DBCS can contain \ in a trail byte, skip the
6518 * double-byte character. */
6519 ++p;
6520 else
6521#endif
6522 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006523 ++result;
6524 }
6525
6526 if (buf != NULL)
6527 {
6528 *buf++ = '"';
6529 for (p = eap->arg; *p; ++p)
6530 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006531#ifdef FEAT_MBYTE
6532 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6533 /* DBCS can contain \ in a trail byte, copy the
6534 * double-byte character to avoid escaping. */
6535 *buf++ = *p++;
6536 else
6537#endif
6538 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006539 *buf++ = '\\';
6540 *buf++ = *p;
6541 }
6542 *buf = '"';
6543 }
6544
6545 break;
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006546 case 2: /* Quote and split (<f-args>) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006547 /* This is hard, so only do it once, and cache the result */
6548 if (*split_buf == NULL)
6549 *split_buf = uc_split_args(eap->arg, split_len);
6550
6551 result = *split_len;
6552 if (buf != NULL && result != 0)
6553 STRCPY(buf, *split_buf);
6554
6555 break;
6556 }
6557 break;
6558
6559 case ct_BANG:
6560 result = eap->forceit ? 1 : 0;
6561 if (quote)
6562 result += 2;
6563 if (buf != NULL)
6564 {
6565 if (quote)
6566 *buf++ = '"';
6567 if (eap->forceit)
6568 *buf++ = '!';
6569 if (quote)
6570 *buf = '"';
6571 }
6572 break;
6573
6574 case ct_LINE1:
6575 case ct_LINE2:
6576 case ct_COUNT:
6577 {
6578 char num_buf[20];
6579 long num = (type == ct_LINE1) ? eap->line1 :
6580 (type == ct_LINE2) ? eap->line2 :
6581 (eap->addr_count > 0) ? eap->line2 : cmd->uc_def;
6582 size_t num_len;
6583
6584 sprintf(num_buf, "%ld", num);
6585 num_len = STRLEN(num_buf);
6586 result = num_len;
6587
6588 if (quote)
6589 result += 2;
6590
6591 if (buf != NULL)
6592 {
6593 if (quote)
6594 *buf++ = '"';
6595 STRCPY(buf, num_buf);
6596 buf += num_len;
6597 if (quote)
6598 *buf = '"';
6599 }
6600
6601 break;
6602 }
6603
6604 case ct_REGISTER:
6605 result = eap->regname ? 1 : 0;
6606 if (quote)
6607 result += 2;
6608 if (buf != NULL)
6609 {
6610 if (quote)
6611 *buf++ = '\'';
6612 if (eap->regname)
6613 *buf++ = eap->regname;
6614 if (quote)
6615 *buf = '\'';
6616 }
6617 break;
6618
6619 case ct_LT:
6620 result = 1;
6621 if (buf != NULL)
6622 *buf = '<';
6623 break;
6624
6625 default:
6626 /* Not recognized: just copy the '<' and return -1. */
6627 result = (size_t)-1;
6628 if (buf != NULL)
6629 *buf = '<';
6630 break;
6631 }
6632
6633 return result;
6634}
6635
6636 static void
6637do_ucmd(eap)
6638 exarg_T *eap;
6639{
6640 char_u *buf;
6641 char_u *p;
6642 char_u *q;
6643
6644 char_u *start;
Bram Moolenaar25648a52009-02-21 19:37:46 +00006645 char_u *end = NULL;
Bram Moolenaara850a712009-01-28 14:42:59 +00006646 char_u *ksp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006647 size_t len, totlen;
6648
6649 size_t split_len = 0;
6650 char_u *split_buf = NULL;
6651 ucmd_T *cmd;
6652#ifdef FEAT_EVAL
6653 scid_T save_current_SID = current_SID;
6654#endif
6655
6656 if (eap->cmdidx == CMD_USER)
6657 cmd = USER_CMD(eap->useridx);
6658 else
6659 cmd = USER_CMD_GA(&curbuf->b_ucmds, eap->useridx);
6660
6661 /*
6662 * Replace <> in the command by the arguments.
Bram Moolenaara850a712009-01-28 14:42:59 +00006663 * First round: "buf" is NULL, compute length, allocate "buf".
6664 * Second round: copy result into "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006665 */
6666 buf = NULL;
6667 for (;;)
6668 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006669 p = cmd->uc_rep; /* source */
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00006670 q = buf; /* destination */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006671 totlen = 0;
Bram Moolenaara850a712009-01-28 14:42:59 +00006672
6673 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006674 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006675 start = vim_strchr(p, '<');
6676 if (start != NULL)
6677 end = vim_strchr(start + 1, '>');
6678 if (buf != NULL)
6679 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006680 for (ksp = p; *ksp != NUL && *ksp != K_SPECIAL; ++ksp)
6681 ;
6682 if (*ksp == K_SPECIAL
6683 && (start == NULL || ksp < start || end == NULL)
Bram Moolenaara850a712009-01-28 14:42:59 +00006684 && ((ksp[1] == KS_SPECIAL && ksp[2] == KE_FILLER)
6685# ifdef FEAT_GUI
6686 || (ksp[1] == KS_EXTRA && ksp[2] == (int)KE_CSI)
6687# endif
6688 ))
6689 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006690 /* K_SPECIAL has been put in the buffer as K_SPECIAL
Bram Moolenaara850a712009-01-28 14:42:59 +00006691 * KS_SPECIAL KE_FILLER, like for mappings, but
6692 * do_cmdline() doesn't handle that, so convert it back.
6693 * Also change K_SPECIAL KS_EXTRA KE_CSI into CSI. */
6694 len = ksp - p;
6695 if (len > 0)
6696 {
6697 mch_memmove(q, p, len);
6698 q += len;
6699 }
6700 *q++ = ksp[1] == KS_SPECIAL ? K_SPECIAL : CSI;
6701 p = ksp + 3;
6702 continue;
6703 }
6704 }
6705
6706 /* break if there no <item> is found */
6707 if (start == NULL || end == NULL)
6708 break;
6709
Bram Moolenaar071d4272004-06-13 20:20:40 +00006710 /* Include the '>' */
6711 ++end;
6712
6713 /* Take everything up to the '<' */
6714 len = start - p;
6715 if (buf == NULL)
6716 totlen += len;
6717 else
6718 {
6719 mch_memmove(q, p, len);
6720 q += len;
6721 }
6722
6723 len = uc_check_code(start, end - start, q, cmd, eap,
6724 &split_buf, &split_len);
6725 if (len == (size_t)-1)
6726 {
6727 /* no match, continue after '<' */
6728 p = start + 1;
6729 len = 1;
6730 }
6731 else
6732 p = end;
6733 if (buf == NULL)
6734 totlen += len;
6735 else
6736 q += len;
6737 }
6738 if (buf != NULL) /* second time here, finished */
6739 {
6740 STRCPY(q, p);
6741 break;
6742 }
6743
6744 totlen += STRLEN(p); /* Add on the trailing characters */
6745 buf = alloc((unsigned)(totlen + 1));
6746 if (buf == NULL)
6747 {
6748 vim_free(split_buf);
6749 return;
6750 }
6751 }
6752
6753#ifdef FEAT_EVAL
6754 current_SID = cmd->uc_scriptID;
6755#endif
6756 (void)do_cmdline(buf, eap->getline, eap->cookie,
6757 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
6758#ifdef FEAT_EVAL
6759 current_SID = save_current_SID;
6760#endif
6761 vim_free(buf);
6762 vim_free(split_buf);
6763}
6764
6765# if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6766 static char_u *
6767get_user_command_name(idx)
6768 int idx;
6769{
6770 return get_user_commands(NULL, idx - (int)CMD_SIZE);
6771}
6772
6773/*
6774 * Function given to ExpandGeneric() to obtain the list of user command names.
6775 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006776 char_u *
6777get_user_commands(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006778 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006779 int idx;
6780{
6781 if (idx < curbuf->b_ucmds.ga_len)
6782 return USER_CMD_GA(&curbuf->b_ucmds, idx)->uc_name;
6783 idx -= curbuf->b_ucmds.ga_len;
6784 if (idx < ucmds.ga_len)
6785 return USER_CMD(idx)->uc_name;
6786 return NULL;
6787}
6788
6789/*
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006790 * Function given to ExpandGeneric() to obtain the list of user address type names.
6791 */
6792 char_u *
6793get_user_cmd_addr_type(xp, idx)
6794 expand_T *xp UNUSED;
6795 int idx;
6796{
6797 return (char_u *)addr_type_complete[idx].name;
6798}
6799
6800/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006801 * Function given to ExpandGeneric() to obtain the list of user command
6802 * attributes.
6803 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006804 char_u *
6805get_user_cmd_flags(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006806 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006807 int idx;
6808{
6809 static char *user_cmd_flags[] =
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006810 {"addr", "bang", "bar", "buffer", "complete",
6811 "count", "nargs", "range", "register"};
Bram Moolenaar071d4272004-06-13 20:20:40 +00006812
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00006813 if (idx >= (int)(sizeof(user_cmd_flags) / sizeof(user_cmd_flags[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006814 return NULL;
6815 return (char_u *)user_cmd_flags[idx];
6816}
6817
6818/*
6819 * Function given to ExpandGeneric() to obtain the list of values for -nargs.
6820 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006821 char_u *
6822get_user_cmd_nargs(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006823 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006824 int idx;
6825{
6826 static char *user_cmd_nargs[] = {"0", "1", "*", "?", "+"};
6827
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00006828 if (idx >= (int)(sizeof(user_cmd_nargs) / sizeof(user_cmd_nargs[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006829 return NULL;
6830 return (char_u *)user_cmd_nargs[idx];
6831}
6832
6833/*
6834 * Function given to ExpandGeneric() to obtain the list of values for -complete.
6835 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006836 char_u *
6837get_user_cmd_complete(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006838 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006839 int idx;
6840{
6841 return (char_u *)command_complete[idx].name;
6842}
6843# endif /* FEAT_CMDL_COMPL */
6844
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006845/*
6846 * Parse address type argument
6847 */
6848 int
6849parse_addr_type_arg(value, vallen, argt, addr_type_arg)
6850 char_u *value;
6851 int vallen;
6852 long *argt;
6853 int *addr_type_arg;
6854{
6855 int i, a, b;
6856 for (i = 0; addr_type_complete[i].expand != -1; ++i)
6857 {
6858 a = (int)STRLEN(addr_type_complete[i].name) == vallen;
6859 b = STRNCMP(value, addr_type_complete[i].name, vallen) == 0;
6860 if (a && b)
6861 {
6862 *addr_type_arg = addr_type_complete[i].expand;
6863 break;
6864 }
6865 }
6866
6867 if (addr_type_complete[i].expand == -1)
6868 {
6869 char_u *err = value;
6870 for (i=0; err[i] == NUL || !vim_iswhite(err[i]); i++);
6871 err[i] = NUL;
6872 EMSG2(_("E180: Invalid address type value: %s"), err);
6873 return FAIL;
6874 }
6875
6876 if (*addr_type_arg != ADDR_LINES)
6877 *argt |= NOTADR;
6878
6879 return OK;
6880}
6881
Bram Moolenaar071d4272004-06-13 20:20:40 +00006882#endif /* FEAT_USR_CMDS */
6883
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006884#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
6885/*
6886 * Parse a completion argument "value[vallen]".
6887 * The detected completion goes in "*complp", argument type in "*argt".
6888 * When there is an argument, for function and user defined completion, it's
6889 * copied to allocated memory and stored in "*compl_arg".
6890 * Returns FAIL if something is wrong.
6891 */
6892 int
6893parse_compl_arg(value, vallen, complp, argt, compl_arg)
6894 char_u *value;
6895 int vallen;
6896 int *complp;
6897 long *argt;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006898 char_u **compl_arg UNUSED;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006899{
6900 char_u *arg = NULL;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006901# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006902 size_t arglen = 0;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006903# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006904 int i;
6905 int valend = vallen;
6906
6907 /* Look for any argument part - which is the part after any ',' */
6908 for (i = 0; i < vallen; ++i)
6909 {
6910 if (value[i] == ',')
6911 {
6912 arg = &value[i + 1];
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006913# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006914 arglen = vallen - i - 1;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006915# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006916 valend = i;
6917 break;
6918 }
6919 }
6920
6921 for (i = 0; command_complete[i].expand != 0; ++i)
6922 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006923 if ((int)STRLEN(command_complete[i].name) == valend
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006924 && STRNCMP(value, command_complete[i].name, valend) == 0)
6925 {
6926 *complp = command_complete[i].expand;
6927 if (command_complete[i].expand == EXPAND_BUFFERS)
6928 *argt |= BUFNAME;
6929 else if (command_complete[i].expand == EXPAND_DIRECTORIES
6930 || command_complete[i].expand == EXPAND_FILES)
6931 *argt |= XFILE;
6932 break;
6933 }
6934 }
6935
6936 if (command_complete[i].expand == 0)
6937 {
6938 EMSG2(_("E180: Invalid complete value: %s"), value);
6939 return FAIL;
6940 }
6941
6942# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6943 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST
6944 && arg != NULL)
6945# else
6946 if (arg != NULL)
6947# endif
6948 {
6949 EMSG(_("E468: Completion argument only allowed for custom completion"));
6950 return FAIL;
6951 }
6952
6953# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6954 if ((*complp == EXPAND_USER_DEFINED || *complp == EXPAND_USER_LIST)
6955 && arg == NULL)
6956 {
6957 EMSG(_("E467: Custom completion requires a function argument"));
6958 return FAIL;
6959 }
6960
6961 if (arg != NULL)
6962 *compl_arg = vim_strnsave(arg, (int)arglen);
6963# endif
6964 return OK;
6965}
6966#endif
6967
Bram Moolenaar071d4272004-06-13 20:20:40 +00006968 static void
6969ex_colorscheme(eap)
6970 exarg_T *eap;
6971{
Bram Moolenaare6850792010-05-14 15:28:44 +02006972 if (*eap->arg == NUL)
6973 {
6974#ifdef FEAT_EVAL
6975 char_u *expr = vim_strsave((char_u *)"g:colors_name");
6976 char_u *p = NULL;
6977
6978 if (expr != NULL)
6979 {
6980 ++emsg_off;
6981 p = eval_to_string(expr, NULL, FALSE);
6982 --emsg_off;
6983 vim_free(expr);
6984 }
6985 if (p != NULL)
6986 {
6987 MSG(p);
6988 vim_free(p);
6989 }
6990 else
6991 MSG("default");
6992#else
6993 MSG(_("unknown"));
6994#endif
6995 }
6996 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarbe1e9e92012-09-18 16:47:07 +02006997 EMSG2(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006998}
6999
7000 static void
7001ex_highlight(eap)
7002 exarg_T *eap;
7003{
7004 if (*eap->arg == NUL && eap->cmd[2] == '!')
7005 MSG(_("Greetings, Vim user!"));
7006 do_highlight(eap->arg, eap->forceit, FALSE);
7007}
7008
7009
7010/*
7011 * Call this function if we thought we were going to exit, but we won't
7012 * (because of an error). May need to restore the terminal mode.
7013 */
7014 void
7015not_exiting()
7016{
7017 exiting = FALSE;
7018 settmode(TMODE_RAW);
7019}
7020
7021/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01007022 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007023 */
7024 static void
7025ex_quit(eap)
7026 exarg_T *eap;
7027{
Bram Moolenaarf240e182014-11-27 18:33:02 +01007028#if defined(FEAT_WINDOWS) || defined(FEAT_AUTOCMD)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007029 win_T *wp;
Bram Moolenaarf240e182014-11-27 18:33:02 +01007030#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007031
Bram Moolenaar071d4272004-06-13 20:20:40 +00007032#ifdef FEAT_CMDWIN
7033 if (cmdwin_type != 0)
7034 {
7035 cmdwin_result = Ctrl_C;
7036 return;
7037 }
7038#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007039 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007040 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007041 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007042 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007043 return;
7044 }
Bram Moolenaarf240e182014-11-27 18:33:02 +01007045#ifdef FEAT_WINDOWS
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007046 if (eap->addr_count > 0)
7047 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01007048 int wnr = eap->line2;
7049
7050 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
7051 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007052 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007053 }
7054 else
Bram Moolenaarf240e182014-11-27 18:33:02 +01007055#endif
7056#if defined(FEAT_WINDOWS) || defined(FEAT_AUTOCMD)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007057 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01007058#endif
7059
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007060#ifdef FEAT_AUTOCMD
Bram Moolenaar3b53dfb2012-06-06 18:03:07 +02007061 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01007062 /* Refuse to quit when locked or when the buffer in the last window is
Bram Moolenaar362ce482012-06-06 19:02:45 +02007063 * being closed (can only happen in autocommands). */
Bram Moolenaarf240e182014-11-27 18:33:02 +01007064 if (curbuf_locked() || (wp->w_buffer->b_nwindows == 1
7065 && wp->w_buffer->b_closing))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007066 return;
7067#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007068
7069#ifdef FEAT_NETBEANS_INTG
7070 netbeansForcedQuit = eap->forceit;
7071#endif
7072
7073 /*
7074 * If there are more files or windows we won't exit.
7075 */
7076 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
7077 exiting = TRUE;
7078 if ((!P_HID(curbuf)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01007079 && check_changed(curbuf, (p_awa ? CCGD_AW : 0)
7080 | (eap->forceit ? CCGD_FORCEIT : 0)
7081 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007082 || check_more(TRUE, eap->forceit) == FAIL
7083 || (only_one_window() && check_changed_any(eap->forceit)))
7084 {
7085 not_exiting();
7086 }
7087 else
7088 {
7089#ifdef FEAT_WINDOWS
7090 if (only_one_window()) /* quit last window */
7091#endif
7092 getout(0);
7093#ifdef FEAT_WINDOWS
7094# ifdef FEAT_GUI
7095 need_mouse_correct = TRUE;
7096# endif
7097 /* close window; may free buffer */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007098 win_close(wp, !P_HID(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007099#endif
7100 }
7101}
7102
7103/*
7104 * ":cquit".
7105 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007106 static void
7107ex_cquit(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007108 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007109{
7110 getout(1); /* this does not always pass on the exit code to the Manx
7111 compiler. why? */
7112}
7113
7114/*
7115 * ":qall": try to quit all windows
7116 */
7117 static void
7118ex_quit_all(eap)
7119 exarg_T *eap;
7120{
7121# ifdef FEAT_CMDWIN
7122 if (cmdwin_type != 0)
7123 {
7124 if (eap->forceit)
7125 cmdwin_result = K_XF1; /* ex_window() takes care of this */
7126 else
7127 cmdwin_result = K_XF2;
7128 return;
7129 }
7130# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007131
7132 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007133 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007134 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007135 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007136 return;
7137 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007138#ifdef FEAT_AUTOCMD
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01007139 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
7140 /* Refuse to quit when locked or when the buffer in the last window is
7141 * being closed (can only happen in autocommands). */
7142 if (curbuf_locked() || (curbuf->b_nwindows == 1 && curbuf->b_closing))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007143 return;
7144#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007145
Bram Moolenaar071d4272004-06-13 20:20:40 +00007146 exiting = TRUE;
7147 if (eap->forceit || !check_changed_any(FALSE))
7148 getout(0);
7149 not_exiting();
7150}
7151
Bram Moolenaard9967712006-03-11 21:18:15 +00007152#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007153/*
7154 * ":close": close current window, unless it is the last one
7155 */
7156 static void
7157ex_close(eap)
7158 exarg_T *eap;
7159{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007160 win_T *win;
7161 int winnr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007162# ifdef FEAT_CMDWIN
7163 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02007164 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007165 else
7166# endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007167 if (!text_locked()
7168#ifdef FEAT_AUTOCMD
7169 && !curbuf_locked()
7170#endif
7171 )
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007172 {
7173 if (eap->addr_count == 0)
7174 ex_win_close(eap->forceit, curwin, NULL);
7175 else {
7176 for (win = firstwin; win != NULL; win = win->w_next)
7177 {
7178 winnr++;
7179 if (winnr == eap->line2)
7180 break;
7181 }
7182 if (win == NULL)
7183 win = lastwin;
7184 ex_win_close(eap->forceit, win, NULL);
7185 }
7186 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007187}
7188
Bram Moolenaard9967712006-03-11 21:18:15 +00007189# ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007190/*
7191 * ":pclose": Close any preview window.
7192 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007193 static void
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007194ex_pclose(eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007195 exarg_T *eap;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007196{
7197 win_T *win;
7198
7199 for (win = firstwin; win != NULL; win = win->w_next)
7200 if (win->w_p_pvw)
7201 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00007202 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007203 break;
7204 }
7205}
Bram Moolenaard9967712006-03-11 21:18:15 +00007206# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007207
Bram Moolenaarf740b292006-02-16 22:11:02 +00007208/*
7209 * Close window "win" and take care of handling closing the last window for a
7210 * modified buffer.
7211 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007212 static void
Bram Moolenaarf740b292006-02-16 22:11:02 +00007213ex_win_close(forceit, win, tp)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007214 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007215 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007216 tabpage_T *tp; /* NULL or the tab page "win" is in */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007217{
7218 int need_hide;
7219 buf_T *buf = win->w_buffer;
7220
7221 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007222 if (need_hide && !P_HID(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007223 {
Bram Moolenaard9967712006-03-11 21:18:15 +00007224# if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007225 if ((p_confirm || cmdmod.confirm) && p_write)
7226 {
7227 dialog_changed(buf, FALSE);
7228 if (buf_valid(buf) && bufIsChanged(buf))
7229 return;
7230 need_hide = FALSE;
7231 }
7232 else
Bram Moolenaard9967712006-03-11 21:18:15 +00007233# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007234 {
7235 EMSG(_(e_nowrtmsg));
7236 return;
7237 }
7238 }
7239
Bram Moolenaard9967712006-03-11 21:18:15 +00007240# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007241 need_mouse_correct = TRUE;
Bram Moolenaard9967712006-03-11 21:18:15 +00007242# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00007243
Bram Moolenaar071d4272004-06-13 20:20:40 +00007244 /* free buffer when not hiding it or when it's a scratch buffer */
Bram Moolenaarf740b292006-02-16 22:11:02 +00007245 if (tp == NULL)
7246 win_close(win, !need_hide && !P_HID(buf));
7247 else
7248 win_close_othertab(win, !need_hide && !P_HID(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007249}
7250
Bram Moolenaar071d4272004-06-13 20:20:40 +00007251/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00007252 * ":tabclose": close current tab page, unless it is the last one.
7253 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007254 */
7255 static void
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007256ex_tabclose(eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007257 exarg_T *eap;
7258{
Bram Moolenaarf740b292006-02-16 22:11:02 +00007259 tabpage_T *tp;
7260
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007261# ifdef FEAT_CMDWIN
7262 if (cmdwin_type != 0)
7263 cmdwin_result = K_IGNORE;
7264 else
7265# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00007266 if (first_tabpage->tp_next == NULL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007267 EMSG(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00007268 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007269 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00007270 if (eap->addr_count > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007271 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00007272 tp = find_tabpage((int)eap->line2);
7273 if (tp == NULL)
7274 {
7275 beep_flush();
7276 return;
7277 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007278 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007279 {
7280 tabpage_close_other(tp, eap->forceit);
7281 return;
7282 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007283 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007284 if (!text_locked()
7285#ifdef FEAT_AUTOCMD
7286 && !curbuf_locked()
7287#endif
7288 )
Bram Moolenaarf740b292006-02-16 22:11:02 +00007289 tabpage_close(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007290 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007291}
7292
7293/*
7294 * ":tabonly": close all tab pages except the current one
7295 */
7296 static void
7297ex_tabonly(eap)
7298 exarg_T *eap;
7299{
7300 tabpage_T *tp;
7301 int done;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007302
7303# ifdef FEAT_CMDWIN
7304 if (cmdwin_type != 0)
7305 cmdwin_result = K_IGNORE;
7306 else
7307# endif
7308 if (first_tabpage->tp_next == NULL)
7309 MSG(_("Already only one tab page"));
7310 else
7311 {
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007312 if (eap->addr_count > 0)
7313 goto_tabpage(eap->line2);
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007314 /* Repeat this up to a 1000 times, because autocommands may mess
7315 * up the lists. */
7316 for (done = 0; done < 1000; ++done)
7317 {
7318 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
7319 if (tp->tp_topframe != topframe)
7320 {
7321 tabpage_close_other(tp, eap->forceit);
7322 /* if we failed to close it quit */
7323 if (valid_tabpage(tp))
7324 done = 1000;
7325 /* start over, "tp" is now invalid */
7326 break;
7327 }
7328 if (first_tabpage->tp_next == NULL)
7329 break;
7330 }
7331 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007332}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007333
7334/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00007335 * Close the current tab page.
7336 */
7337 void
7338tabpage_close(forceit)
7339 int forceit;
7340{
7341 /* First close all the windows but the current one. If that worked then
7342 * close the last window in this tab, that will close it. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007343 if (lastwin != firstwin)
7344 close_others(TRUE, forceit);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007345 if (lastwin == firstwin)
7346 ex_win_close(forceit, curwin, NULL);
7347# ifdef FEAT_GUI
7348 need_mouse_correct = TRUE;
7349# endif
7350}
7351
7352/*
7353 * Close tab page "tp", which is not the current tab page.
7354 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007355 * Also takes care of the tab pages line disappearing when closing the
7356 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00007357 */
7358 void
7359tabpage_close_other(tp, forceit)
7360 tabpage_T *tp;
7361 int forceit;
7362{
7363 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007364 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007365 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00007366
7367 /* Limit to 1000 windows, autocommands may add a window while we close
7368 * one. OK, so I'm paranoid... */
7369 while (++done < 1000)
7370 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007371 wp = tp->tp_firstwin;
7372 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007373
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007374 /* Autocommands may delete the tab page under our fingers and we may
7375 * fail to close a window with a modified buffer. */
7376 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007377 break;
7378 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007379
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007380 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007381 if (h != tabline_height())
7382 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00007383}
7384
7385/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007386 * ":only".
7387 */
7388 static void
7389ex_only(eap)
7390 exarg_T *eap;
7391{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007392 win_T *wp;
7393 int wnr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007394# ifdef FEAT_GUI
7395 need_mouse_correct = TRUE;
7396# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007397 if (eap->addr_count > 0)
7398 {
7399 wnr = eap->line2;
7400 for (wp = firstwin; --wnr > 0; )
7401 {
7402 if (wp->w_next == NULL)
7403 break;
7404 else
7405 wp = wp->w_next;
7406 }
7407 win_goto(wp);
7408 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007409 close_others(TRUE, eap->forceit);
7410}
7411
7412/*
7413 * ":all" and ":sall".
Bram Moolenaard9967712006-03-11 21:18:15 +00007414 * Also used for ":tab drop file ..." after setting the argument list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007415 */
Bram Moolenaard9967712006-03-11 21:18:15 +00007416 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00007417ex_all(eap)
7418 exarg_T *eap;
7419{
7420 if (eap->addr_count == 0)
7421 eap->line2 = 9999;
Bram Moolenaard9967712006-03-11 21:18:15 +00007422 do_arg_all((int)eap->line2, eap->forceit, eap->cmdidx == CMD_drop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007423}
7424#endif /* FEAT_WINDOWS */
7425
7426 static void
7427ex_hide(eap)
7428 exarg_T *eap;
7429{
7430 if (*eap->arg != NUL && check_nextcmd(eap->arg) == NULL)
7431 eap->errmsg = e_invarg;
7432 else
7433 {
7434 /* ":hide" or ":hide | cmd": hide current window */
7435 eap->nextcmd = check_nextcmd(eap->arg);
7436#ifdef FEAT_WINDOWS
7437 if (!eap->skip)
7438 {
7439# ifdef FEAT_GUI
7440 need_mouse_correct = TRUE;
7441# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007442 if (eap->addr_count == 0)
7443 win_close(curwin, FALSE); /* don't free buffer */
Bram Moolenaarf240e182014-11-27 18:33:02 +01007444 else
7445 {
7446 int winnr = 0;
7447 win_T *win;
7448
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007449 for (win = firstwin; win != NULL; win = win->w_next)
7450 {
7451 winnr++;
7452 if (winnr == eap->line2)
7453 break;
7454 }
7455 if (win == NULL)
7456 win = lastwin;
7457 win_close(win, FALSE);
7458 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007459 }
7460#endif
7461 }
7462}
7463
7464/*
7465 * ":stop" and ":suspend": Suspend Vim.
7466 */
7467 static void
7468ex_stop(eap)
7469 exarg_T *eap;
7470{
7471 /*
7472 * Disallow suspending for "rvim".
7473 */
7474 if (!check_restricted()
7475#ifdef WIN3264
7476 /*
7477 * Check if external commands are allowed now.
7478 */
7479 && can_end_termcap_mode(TRUE)
7480#endif
7481 )
7482 {
7483 if (!eap->forceit)
7484 autowrite_all();
7485 windgoto((int)Rows - 1, 0);
7486 out_char('\n');
7487 out_flush();
7488 stoptermcap();
7489 out_flush(); /* needed for SUN to restore xterm buffer */
7490#ifdef FEAT_TITLE
7491 mch_restore_title(3); /* restore window titles */
7492#endif
7493 ui_suspend(); /* call machine specific function */
7494#ifdef FEAT_TITLE
7495 maketitle();
7496 resettitle(); /* force updating the title */
7497#endif
7498 starttermcap();
7499 scroll_start(); /* scroll screen before redrawing */
7500 redraw_later_clear();
7501 shell_resized(); /* may have resized window */
7502 }
7503}
7504
7505/*
7506 * ":exit", ":xit" and ":wq": Write file and exit Vim.
7507 */
7508 static void
7509ex_exit(eap)
7510 exarg_T *eap;
7511{
7512#ifdef FEAT_CMDWIN
7513 if (cmdwin_type != 0)
7514 {
7515 cmdwin_result = Ctrl_C;
7516 return;
7517 }
7518#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007519 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007520 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007521 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007522 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007523 return;
7524 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007525#ifdef FEAT_AUTOCMD
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01007526 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
7527 /* Refuse to quit when locked or when the buffer in the last window is
7528 * being closed (can only happen in autocommands). */
7529 if (curbuf_locked() || (curbuf->b_nwindows == 1 && curbuf->b_closing))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007530 return;
7531#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007532
7533 /*
7534 * if more files or windows we won't exit
7535 */
7536 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
7537 exiting = TRUE;
7538 if ( ((eap->cmdidx == CMD_wq
7539 || curbufIsChanged())
7540 && do_write(eap) == FAIL)
7541 || check_more(TRUE, eap->forceit) == FAIL
7542 || (only_one_window() && check_changed_any(eap->forceit)))
7543 {
7544 not_exiting();
7545 }
7546 else
7547 {
7548#ifdef FEAT_WINDOWS
7549 if (only_one_window()) /* quit last window, exit Vim */
7550#endif
7551 getout(0);
7552#ifdef FEAT_WINDOWS
7553# ifdef FEAT_GUI
7554 need_mouse_correct = TRUE;
7555# endif
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02007556 /* Quit current window, may free the buffer. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007557 win_close(curwin, !P_HID(curwin->w_buffer));
7558#endif
7559 }
7560}
7561
7562/*
7563 * ":print", ":list", ":number".
7564 */
7565 static void
7566ex_print(eap)
7567 exarg_T *eap;
7568{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007569 if (curbuf->b_ml.ml_flags & ML_EMPTY)
7570 EMSG(_(e_emptybuf));
7571 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007572 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007573 for ( ;!got_int; ui_breakcheck())
7574 {
7575 print_line(eap->line1,
7576 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
7577 || (eap->flags & EXFLAG_NR)),
7578 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
7579 if (++eap->line1 > eap->line2)
7580 break;
7581 out_flush(); /* show one line at a time */
7582 }
7583 setpcmark();
7584 /* put cursor at last line */
7585 curwin->w_cursor.lnum = eap->line2;
7586 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007587 }
7588
Bram Moolenaar071d4272004-06-13 20:20:40 +00007589 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007590}
7591
7592#ifdef FEAT_BYTEOFF
7593 static void
7594ex_goto(eap)
7595 exarg_T *eap;
7596{
7597 goto_byte(eap->line2);
7598}
7599#endif
7600
7601/*
7602 * ":shell".
7603 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007604 static void
7605ex_shell(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007606 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007607{
7608 do_shell(NULL, 0);
7609}
7610
7611#if (defined(FEAT_WINDOWS) && defined(HAVE_DROP_FILE)) \
7612 || (defined(FEAT_GUI_GTK) && defined(FEAT_DND)) \
Bram Moolenaar371baa92005-12-29 22:43:53 +00007613 || defined(FEAT_GUI_MSWIN) \
7614 || defined(FEAT_GUI_MAC) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00007615 || defined(PROTO)
7616
7617/*
7618 * Handle a file drop. The code is here because a drop is *nearly* like an
7619 * :args command, but not quite (we have a list of exact filenames, so we
7620 * don't want to (a) parse a command line, or (b) expand wildcards. So the
7621 * code is very similar to :args and hence needs access to a lot of the static
7622 * functions in this file.
7623 *
7624 * The list should be allocated using alloc(), as should each item in the
7625 * list. This function takes over responsibility for freeing the list.
7626 *
Bram Moolenaar81870892007-11-11 18:17:28 +00007627 * XXX The list is made into the argument list. This is freed using
Bram Moolenaar071d4272004-06-13 20:20:40 +00007628 * FreeWild(), which does a series of vim_free() calls, unless the two defines
7629 * __EMX__ and __ALWAYS_HAS_TRAILING_NUL_POINTER are set. In this case, a
7630 * routine _fnexplodefree() is used. This may cause problems, but as the drop
7631 * file functionality is (currently) not in EMX this is not presently a
7632 * problem.
7633 */
7634 void
7635handle_drop(filec, filev, split)
7636 int filec; /* the number of files dropped */
7637 char_u **filev; /* the list of files dropped */
7638 int split; /* force splitting the window */
7639{
7640 exarg_T ea;
7641 int save_msg_scroll = msg_scroll;
7642
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007643 /* Postpone this while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007644 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007645 return;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007646#ifdef FEAT_AUTOCMD
7647 if (curbuf_locked())
7648 return;
7649#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00007650 /* When the screen is being updated we should not change buffers and
7651 * windows structures, it may cause freed memory to be used. */
7652 if (updating_screen)
7653 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007654
7655 /* Check whether the current buffer is changed. If so, we will need
7656 * to split the current window or data could be lost.
7657 * We don't need to check if the 'hidden' option is set, as in this
7658 * case the buffer won't be lost.
7659 */
7660 if (!P_HID(curbuf) && !split)
7661 {
7662 ++emsg_off;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01007663 split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007664 --emsg_off;
7665 }
7666 if (split)
7667 {
7668# ifdef FEAT_WINDOWS
7669 if (win_split(0, 0) == FAIL)
7670 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007671 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007672
7673 /* When splitting the window, create a new alist. Otherwise the
7674 * existing one is overwritten. */
7675 alist_unlink(curwin->w_alist);
7676 alist_new();
7677# else
7678 return; /* can't split, always fail */
7679# endif
7680 }
7681
7682 /*
7683 * Set up the new argument list.
7684 */
Bram Moolenaar86b68352004-12-27 21:59:20 +00007685 alist_set(ALIST(curwin), filec, filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007686
7687 /*
7688 * Move to the first file.
7689 */
7690 /* Fake up a minimal "next" command for do_argfile() */
7691 vim_memset(&ea, 0, sizeof(ea));
7692 ea.cmd = (char_u *)"next";
7693 do_argfile(&ea, 0);
7694
7695 /* do_ecmd() may set need_start_insertmode, but since we never left Insert
7696 * mode that is not needed here. */
7697 need_start_insertmode = FALSE;
7698
7699 /* Restore msg_scroll, otherwise a following command may cause scrolling
7700 * unexpectedly. The screen will be redrawn by the caller, thus
7701 * msg_scroll being set by displaying a message is irrelevant. */
7702 msg_scroll = save_msg_scroll;
7703}
7704#endif
7705
Bram Moolenaar071d4272004-06-13 20:20:40 +00007706/*
7707 * Clear an argument list: free all file names and reset it to zero entries.
7708 */
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007709 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00007710alist_clear(al)
7711 alist_T *al;
7712{
7713 while (--al->al_ga.ga_len >= 0)
7714 vim_free(AARGLIST(al)[al->al_ga.ga_len].ae_fname);
7715 ga_clear(&al->al_ga);
7716}
7717
7718/*
7719 * Init an argument list.
7720 */
7721 void
7722alist_init(al)
7723 alist_T *al;
7724{
7725 ga_init2(&al->al_ga, (int)sizeof(aentry_T), 5);
7726}
7727
7728#if defined(FEAT_WINDOWS) || defined(PROTO)
7729
7730/*
7731 * Remove a reference from an argument list.
7732 * Ignored when the argument list is the global one.
7733 * If the argument list is no longer used by any window, free it.
7734 */
7735 void
7736alist_unlink(al)
7737 alist_T *al;
7738{
7739 if (al != &global_alist && --al->al_refcount <= 0)
7740 {
7741 alist_clear(al);
7742 vim_free(al);
7743 }
7744}
7745
7746# if defined(FEAT_LISTCMDS) || defined(HAVE_DROP_FILE) || defined(PROTO)
7747/*
7748 * Create a new argument list and use it for the current window.
7749 */
7750 void
7751alist_new()
7752{
7753 curwin->w_alist = (alist_T *)alloc((unsigned)sizeof(alist_T));
7754 if (curwin->w_alist == NULL)
7755 {
7756 curwin->w_alist = &global_alist;
7757 ++global_alist.al_refcount;
7758 }
7759 else
7760 {
7761 curwin->w_alist->al_refcount = 1;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02007762 curwin->w_alist->id = ++max_alist_id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007763 alist_init(curwin->w_alist);
7764 }
7765}
7766# endif
7767#endif
7768
7769#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE) || defined(PROTO)
7770/*
7771 * Expand the file names in the global argument list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00007772 * If "fnum_list" is not NULL, use "fnum_list[fnum_len]" as a list of buffer
7773 * numbers to be re-used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007774 */
7775 void
Bram Moolenaar86b68352004-12-27 21:59:20 +00007776alist_expand(fnum_list, fnum_len)
7777 int *fnum_list;
7778 int fnum_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007779{
7780 char_u **old_arg_files;
Bram Moolenaar86b68352004-12-27 21:59:20 +00007781 int old_arg_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007782 char_u **new_arg_files;
7783 int new_arg_file_count;
7784 char_u *save_p_su = p_su;
7785 int i;
7786
7787 /* Don't use 'suffixes' here. This should work like the shell did the
7788 * expansion. Also, the vimrc file isn't read yet, thus the user
7789 * can't set the options. */
7790 p_su = empty_option;
7791 old_arg_files = (char_u **)alloc((unsigned)(sizeof(char_u *) * GARGCOUNT));
7792 if (old_arg_files != NULL)
7793 {
7794 for (i = 0; i < GARGCOUNT; ++i)
Bram Moolenaar86b68352004-12-27 21:59:20 +00007795 old_arg_files[i] = vim_strsave(GARGLIST[i].ae_fname);
7796 old_arg_count = GARGCOUNT;
7797 if (expand_wildcards(old_arg_count, old_arg_files,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007798 &new_arg_file_count, &new_arg_files,
Bram Moolenaarb5609832011-07-20 15:04:58 +02007799 EW_FILE|EW_NOTFOUND|EW_ADDSLASH|EW_NOERROR) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007800 && new_arg_file_count > 0)
7801 {
Bram Moolenaar86b68352004-12-27 21:59:20 +00007802 alist_set(&global_alist, new_arg_file_count, new_arg_files,
7803 TRUE, fnum_list, fnum_len);
7804 FreeWild(old_arg_count, old_arg_files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007805 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007806 }
7807 p_su = save_p_su;
7808}
7809#endif
7810
7811/*
7812 * Set the argument list for the current window.
7813 * Takes over the allocated files[] and the allocated fnames in it.
7814 */
7815 void
Bram Moolenaar86b68352004-12-27 21:59:20 +00007816alist_set(al, count, files, use_curbuf, fnum_list, fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007817 alist_T *al;
7818 int count;
7819 char_u **files;
7820 int use_curbuf;
Bram Moolenaar86b68352004-12-27 21:59:20 +00007821 int *fnum_list;
7822 int fnum_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007823{
7824 int i;
7825
7826 alist_clear(al);
7827 if (ga_grow(&al->al_ga, count) == OK)
7828 {
7829 for (i = 0; i < count; ++i)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007830 {
7831 if (got_int)
7832 {
7833 /* When adding many buffers this can take a long time. Allow
7834 * interrupting here. */
7835 while (i < count)
7836 vim_free(files[i++]);
7837 break;
7838 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00007839
7840 /* May set buffer name of a buffer previously used for the
7841 * argument list, so that it's re-used by alist_add. */
7842 if (fnum_list != NULL && i < fnum_len)
7843 buf_set_name(fnum_list[i], files[i]);
7844
Bram Moolenaar071d4272004-06-13 20:20:40 +00007845 alist_add(al, files[i], use_curbuf ? 2 : 1);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007846 ui_breakcheck();
7847 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007848 vim_free(files);
7849 }
7850 else
7851 FreeWild(count, files);
7852#ifdef FEAT_WINDOWS
7853 if (al == &global_alist)
7854#endif
7855 arg_had_last = FALSE;
7856}
7857
7858/*
7859 * Add file "fname" to argument list "al".
7860 * "fname" must have been allocated and "al" must have been checked for room.
7861 */
7862 void
7863alist_add(al, fname, set_fnum)
7864 alist_T *al;
7865 char_u *fname;
7866 int set_fnum; /* 1: set buffer number; 2: re-use curbuf */
7867{
7868 if (fname == NULL) /* don't add NULL file names */
7869 return;
7870#ifdef BACKSLASH_IN_FILENAME
7871 slash_adjust(fname);
7872#endif
7873 AARGLIST(al)[al->al_ga.ga_len].ae_fname = fname;
7874 if (set_fnum > 0)
7875 AARGLIST(al)[al->al_ga.ga_len].ae_fnum =
7876 buflist_add(fname, BLN_LISTED | (set_fnum == 2 ? BLN_CURBUF : 0));
7877 ++al->al_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007878}
7879
7880#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
7881/*
7882 * Adjust slashes in file names. Called after 'shellslash' was set.
7883 */
7884 void
7885alist_slash_adjust()
7886{
7887 int i;
7888# ifdef FEAT_WINDOWS
7889 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007890 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007891# endif
7892
7893 for (i = 0; i < GARGCOUNT; ++i)
7894 if (GARGLIST[i].ae_fname != NULL)
7895 slash_adjust(GARGLIST[i].ae_fname);
7896# ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00007897 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007898 if (wp->w_alist != &global_alist)
7899 for (i = 0; i < WARGCOUNT(wp); ++i)
7900 if (WARGLIST(wp)[i].ae_fname != NULL)
7901 slash_adjust(WARGLIST(wp)[i].ae_fname);
7902# endif
7903}
7904#endif
7905
7906/*
7907 * ":preserve".
7908 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007909 static void
7910ex_preserve(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007911 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007912{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007913 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007914 ml_preserve(curbuf, TRUE);
7915}
7916
7917/*
7918 * ":recover".
7919 */
7920 static void
7921ex_recover(eap)
7922 exarg_T *eap;
7923{
7924 /* Set recoverymode right away to avoid the ATTENTION prompt. */
7925 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01007926 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
7927 | CCGD_MULTWIN
7928 | (eap->forceit ? CCGD_FORCEIT : 0)
7929 | CCGD_EXCMD)
7930
Bram Moolenaar071d4272004-06-13 20:20:40 +00007931 && (*eap->arg == NUL
7932 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
7933 ml_recover();
7934 recoverymode = FALSE;
7935}
7936
7937/*
7938 * Command modifier used in a wrong way.
7939 */
7940 static void
7941ex_wrongmodifier(eap)
7942 exarg_T *eap;
7943{
7944 eap->errmsg = e_invcmd;
7945}
7946
7947#ifdef FEAT_WINDOWS
7948/*
7949 * :sview [+command] file split window with new file, read-only
7950 * :split [[+command] file] split window with current or new file
7951 * :vsplit [[+command] file] split window vertically with current or new file
7952 * :new [[+command] file] split window with no or new file
7953 * :vnew [[+command] file] split vertically window with no or new file
7954 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007955 *
7956 * :tabedit open new Tab page with empty window
7957 * :tabedit [+command] file open new Tab page and edit "file"
7958 * :tabnew [[+command] file] just like :tabedit
7959 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007960 */
7961 void
7962ex_splitview(eap)
7963 exarg_T *eap;
7964{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007965 win_T *old_curwin = curwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007966# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007967 char_u *fname = NULL;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007968# endif
7969# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007970 int browse_flag = cmdmod.browse;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007971# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007972
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007973# ifndef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007974 if (eap->cmdidx == CMD_vsplit || eap->cmdidx == CMD_vnew)
7975 {
7976 ex_ni(eap);
7977 return;
7978 }
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_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007982 need_mouse_correct = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007983# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007984
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007985# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00007986 /* A ":split" in the quickfix window works like ":new". Don't want two
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007987 * quickfix windows. But it's OK when doing ":tab split". */
7988 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007989 {
7990 if (eap->cmdidx == CMD_split)
7991 eap->cmdidx = CMD_new;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007992# ifdef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007993 if (eap->cmdidx == CMD_vsplit)
7994 eap->cmdidx = CMD_vnew;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007995# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007996 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007997# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007998
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007999# ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008000 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008001 {
8002 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
8003 FNAME_MESS, TRUE, curbuf->b_ffname);
8004 if (fname == NULL)
8005 goto theend;
8006 eap->arg = fname;
8007 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008008# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008009 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008010# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008011# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008012# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008013 if (cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008014# ifdef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00008015 && eap->cmdidx != CMD_vnew
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008016# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008017 && eap->cmdidx != CMD_new)
8018 {
Bram Moolenaar15bfa092008-07-24 16:45:38 +00008019# ifdef FEAT_AUTOCMD
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008020 if (
Bram Moolenaar15bfa092008-07-24 16:45:38 +00008021# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008022 !gui.in_use &&
Bram Moolenaar15bfa092008-07-24 16:45:38 +00008023# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008024 au_has_group((char_u *)"FileExplorer"))
8025 {
8026 /* No browsing supported but we do have the file explorer:
8027 * Edit the directory. */
8028 if (*eap->arg == NUL || !mch_isdir(eap->arg))
8029 eap->arg = (char_u *)".";
8030 }
8031 else
Bram Moolenaar15bfa092008-07-24 16:45:38 +00008032# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008033 {
8034 fname = do_browse(0, (char_u *)_("Edit File in new window"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00008035 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008036 if (fname == NULL)
8037 goto theend;
8038 eap->arg = fname;
8039 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008040 }
8041 cmdmod.browse = FALSE; /* Don't browse again in do_ecmd(). */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008042# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008043
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008044 /*
8045 * Either open new tab page or split the window.
8046 */
8047 if (eap->cmdidx == CMD_tabedit
8048 || eap->cmdidx == CMD_tabfind
8049 || eap->cmdidx == CMD_tabnew)
8050 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00008051 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
8052 : eap->addr_count == 0 ? 0
8053 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008054 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00008055 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008056
8057 /* set the alternate buffer for the window we came from */
8058 if (curwin != old_curwin
8059 && win_valid(old_curwin)
8060 && old_curwin->w_buffer != curbuf
8061 && !cmdmod.keepalt)
8062 old_curwin->w_alt_fnum = curbuf->b_fnum;
8063 }
8064 }
8065 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008066 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
8067 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008068# ifdef FEAT_SCROLLBIND
Bram Moolenaar071d4272004-06-13 20:20:40 +00008069 /* Reset 'scrollbind' when editing another file, but keep it when
8070 * doing ":split" without arguments. */
8071 if (*eap->arg != NUL
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008072# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008073 || cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008074# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008075 )
Bram Moolenaar3368ea22010-09-21 16:56:35 +02008076 {
8077 RESET_BINDING(curwin);
8078 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008079 else
8080 do_check_scrollbind(FALSE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008081# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008082 do_exedit(eap, old_curwin);
8083 }
8084
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008085# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008086 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008087# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008088
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008089# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008090theend:
8091 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008092# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008093}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008094
8095/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008096 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008097 */
8098 void
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008099tabpage_new()
8100{
8101 exarg_T ea;
8102
8103 vim_memset(&ea, 0, sizeof(ea));
8104 ea.cmdidx = CMD_tabnew;
8105 ea.cmd = (char_u *)"tabn";
8106 ea.arg = (char_u *)"";
8107 ex_splitview(&ea);
8108}
8109
8110/*
8111 * :tabnext command
8112 */
8113 static void
8114ex_tabnext(eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008115 exarg_T *eap;
8116{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008117 switch (eap->cmdidx)
8118 {
8119 case CMD_tabfirst:
8120 case CMD_tabrewind:
8121 goto_tabpage(1);
8122 break;
8123 case CMD_tablast:
8124 goto_tabpage(9999);
8125 break;
8126 case CMD_tabprevious:
8127 case CMD_tabNext:
8128 goto_tabpage(eap->addr_count == 0 ? -1 : -(int)eap->line2);
8129 break;
8130 default: /* CMD_tabnext */
8131 goto_tabpage(eap->addr_count == 0 ? 0 : (int)eap->line2);
8132 break;
8133 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008134}
8135
8136/*
8137 * :tabmove command
8138 */
8139 static void
8140ex_tabmove(eap)
8141 exarg_T *eap;
8142{
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02008143 int tab_number = 9999;
8144
8145 if (eap->arg && *eap->arg != NUL)
8146 {
8147 char_u *p = eap->arg;
8148 int relative = 0; /* argument +N/-N means: move N places to the
8149 * right/left relative to the current position. */
8150
8151 if (*eap->arg == '-')
8152 {
8153 relative = -1;
8154 p = eap->arg + 1;
8155 }
8156 else if (*eap->arg == '+')
8157 {
8158 relative = 1;
8159 p = eap->arg + 1;
8160 }
8161 else
8162 p = eap->arg;
8163
8164 if (p == skipdigits(p))
8165 {
8166 /* No numbers as argument. */
8167 eap->errmsg = e_invarg;
8168 return;
8169 }
8170
8171 tab_number = getdigits(&p);
8172 if (relative != 0)
8173 tab_number = tab_number * relative + tabpage_index(curtab) - 1;;
8174 }
8175 else if (eap->addr_count != 0)
8176 tab_number = eap->line2;
8177
8178 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008179}
8180
8181/*
8182 * :tabs command: List tabs and their contents.
8183 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008184 static void
8185ex_tabs(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008186 exarg_T *eap UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008187{
8188 tabpage_T *tp;
8189 win_T *wp;
8190 int tabcount = 1;
8191
8192 msg_start();
8193 msg_scroll = TRUE;
8194 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
8195 {
8196 msg_putchar('\n');
8197 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
8198 msg_outtrans_attr(IObuff, hl_attr(HLF_T));
8199 out_flush(); /* output one line at a time */
8200 ui_breakcheck();
8201
Bram Moolenaar030f0df2006-02-21 22:02:53 +00008202 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008203 wp = firstwin;
8204 else
8205 wp = tp->tp_firstwin;
8206 for ( ; wp != NULL && !got_int; wp = wp->w_next)
8207 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008208 msg_putchar('\n');
8209 msg_putchar(wp == curwin ? '>' : ' ');
8210 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00008211 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
8212 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008213 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02008214 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008215 else
8216 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
8217 IObuff, IOSIZE, TRUE);
8218 msg_outtrans(IObuff);
8219 out_flush(); /* output one line at a time */
8220 ui_breakcheck();
8221 }
8222 }
8223}
8224
8225#endif /* FEAT_WINDOWS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008226
8227/*
8228 * ":mode": Set screen mode.
8229 * If no argument given, just get the screen size and redraw.
8230 */
8231 static void
8232ex_mode(eap)
8233 exarg_T *eap;
8234{
8235 if (*eap->arg == NUL)
8236 shell_resized();
8237 else
8238 mch_screenmode(eap->arg);
8239}
8240
8241#ifdef FEAT_WINDOWS
8242/*
8243 * ":resize".
8244 * set, increment or decrement current window height
8245 */
8246 static void
8247ex_resize(eap)
8248 exarg_T *eap;
8249{
8250 int n;
8251 win_T *wp = curwin;
8252
8253 if (eap->addr_count > 0)
8254 {
8255 n = eap->line2;
8256 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
8257 ;
8258 }
8259
8260#ifdef FEAT_GUI
8261 need_mouse_correct = TRUE;
8262#endif
8263 n = atol((char *)eap->arg);
8264#ifdef FEAT_VERTSPLIT
8265 if (cmdmod.split & WSP_VERT)
8266 {
8267 if (*eap->arg == '-' || *eap->arg == '+')
8268 n += W_WIDTH(curwin);
8269 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
8270 n = 9999;
8271 win_setwidth_win((int)n, wp);
8272 }
8273 else
8274#endif
8275 {
8276 if (*eap->arg == '-' || *eap->arg == '+')
8277 n += curwin->w_height;
8278 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
8279 n = 9999;
8280 win_setheight_win((int)n, wp);
8281 }
8282}
8283#endif
8284
8285/*
8286 * ":find [+command] <file>" command.
8287 */
8288 static void
8289ex_find(eap)
8290 exarg_T *eap;
8291{
8292#ifdef FEAT_SEARCHPATH
8293 char_u *fname;
8294 int count;
8295
8296 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
8297 TRUE, curbuf->b_ffname);
8298 if (eap->addr_count > 0)
8299 {
8300 /* Repeat finding the file "count" times. This matters when it
8301 * appears several times in the path. */
8302 count = eap->line2;
8303 while (fname != NULL && --count > 0)
8304 {
8305 vim_free(fname);
8306 fname = find_file_in_path(NULL, 0, FNAME_MESS,
8307 FALSE, curbuf->b_ffname);
8308 }
8309 }
8310
8311 if (fname != NULL)
8312 {
8313 eap->arg = fname;
8314#endif
8315 do_exedit(eap, NULL);
8316#ifdef FEAT_SEARCHPATH
8317 vim_free(fname);
8318 }
8319#endif
8320}
8321
8322/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00008323 * ":open" simulation: for now just work like ":visual".
8324 */
8325 static void
8326ex_open(eap)
8327 exarg_T *eap;
8328{
8329 regmatch_T regmatch;
8330 char_u *p;
8331
8332 curwin->w_cursor.lnum = eap->line2;
8333 beginline(BL_SOL | BL_FIX);
8334 if (*eap->arg == '/')
8335 {
8336 /* ":open /pattern/": put cursor in column found with pattern */
8337 ++eap->arg;
8338 p = skip_regexp(eap->arg, '/', p_magic, NULL);
8339 *p = NUL;
8340 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
8341 if (regmatch.regprog != NULL)
8342 {
8343 regmatch.rm_ic = p_ic;
8344 p = ml_get_curline();
8345 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008346 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008347 else
8348 EMSG(_(e_nomatch));
Bram Moolenaar473de612013-06-08 18:19:48 +02008349 vim_regfree(regmatch.regprog);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008350 }
8351 /* Move to the NUL, ignore any other arguments. */
8352 eap->arg += STRLEN(eap->arg);
8353 }
8354 check_cursor();
8355
8356 eap->cmdidx = CMD_visual;
8357 do_exedit(eap, NULL);
8358}
8359
8360/*
8361 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008362 */
8363 static void
8364ex_edit(eap)
8365 exarg_T *eap;
8366{
8367 do_exedit(eap, NULL);
8368}
8369
8370/*
8371 * ":edit <file>" command and alikes.
8372 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008373 void
8374do_exedit(eap, old_curwin)
8375 exarg_T *eap;
8376 win_T *old_curwin; /* curwin before doing a split or NULL */
8377{
8378 int n;
8379#ifdef FEAT_WINDOWS
8380 int need_hide;
8381#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00008382 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008383
8384 /*
8385 * ":vi" command ends Ex mode.
8386 */
8387 if (exmode_active && (eap->cmdidx == CMD_visual
8388 || eap->cmdidx == CMD_view))
8389 {
8390 exmode_active = FALSE;
8391 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008392 {
8393 /* Special case: ":global/pat/visual\NLvi-commands" */
8394 if (global_busy)
8395 {
8396 int rd = RedrawingDisabled;
8397 int nwr = no_wait_return;
8398 int ms = msg_scroll;
8399#ifdef FEAT_GUI
8400 int he = hold_gui_events;
8401#endif
8402
8403 if (eap->nextcmd != NULL)
8404 {
8405 stuffReadbuff(eap->nextcmd);
8406 eap->nextcmd = NULL;
8407 }
8408
8409 if (exmode_was != EXMODE_VIM)
8410 settmode(TMODE_RAW);
8411 RedrawingDisabled = 0;
8412 no_wait_return = 0;
8413 need_wait_return = FALSE;
8414 msg_scroll = 0;
8415#ifdef FEAT_GUI
8416 hold_gui_events = 0;
8417#endif
8418 must_redraw = CLEAR;
8419
8420 main_loop(FALSE, TRUE);
8421
8422 RedrawingDisabled = rd;
8423 no_wait_return = nwr;
8424 msg_scroll = ms;
8425#ifdef FEAT_GUI
8426 hold_gui_events = he;
8427#endif
8428 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008429 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008430 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008431 }
8432
8433 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008434 || eap->cmdidx == CMD_tabnew
8435 || eap->cmdidx == CMD_tabedit
Bram Moolenaar071d4272004-06-13 20:20:40 +00008436#ifdef FEAT_VERTSPLIT
8437 || eap->cmdidx == CMD_vnew
8438#endif
8439 ) && *eap->arg == NUL)
8440 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008441 /* ":new" or ":tabnew" without argument: edit an new empty buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008442 setpcmark();
8443 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00008444 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
8445 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008446 }
8447 else if ((eap->cmdidx != CMD_split
8448#ifdef FEAT_VERTSPLIT
8449 && eap->cmdidx != CMD_vsplit
8450#endif
8451 )
8452 || *eap->arg != NUL
8453#ifdef FEAT_BROWSE
8454 || cmdmod.browse
8455#endif
8456 )
8457 {
Bram Moolenaar5555acc2006-04-07 21:33:12 +00008458#ifdef FEAT_AUTOCMD
8459 /* Can't edit another file when "curbuf_lock" is set. Only ":edit"
8460 * can bring us here, others are stopped earlier. */
8461 if (*eap->arg != NUL && curbuf_locked())
8462 return;
8463#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008464 n = readonlymode;
8465 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
8466 readonlymode = TRUE;
8467 else if (eap->cmdidx == CMD_enew)
8468 readonlymode = FALSE; /* 'readonly' doesn't make sense in an
8469 empty buffer */
8470 setpcmark();
8471 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
8472 NULL, eap,
8473 /* ":edit" goes to first line if Vi compatible */
8474 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
8475 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
8476 ? ECMD_ONE : eap->do_ecmd_lnum,
8477 (P_HID(curbuf) ? ECMD_HIDE : 0)
8478 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar7b449342014-03-25 13:03:48 +01008479 /* after a split we can use an existing buffer */
8480 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008481#ifdef FEAT_LISTCMDS
8482 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
8483#endif
Bram Moolenaar701f7af2008-11-15 13:12:07 +00008484 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008485 {
8486 /* Editing the file failed. If the window was split, close it. */
8487#ifdef FEAT_WINDOWS
8488 if (old_curwin != NULL)
8489 {
8490 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
8491 if (!need_hide || P_HID(curbuf))
8492 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008493# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
8494 cleanup_T cs;
8495
8496 /* Reset the error/interrupt/exception state here so that
8497 * aborting() returns FALSE when closing a window. */
8498 enter_cleanup(&cs);
8499# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008500# ifdef FEAT_GUI
8501 need_mouse_correct = TRUE;
8502# endif
8503 win_close(curwin, !need_hide && !P_HID(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008504
8505# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
8506 /* Restore the error/interrupt/exception state if not
8507 * discarded by a new aborting error, interrupt, or
8508 * uncaught exception. */
8509 leave_cleanup(&cs);
8510# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008511 }
8512 }
8513#endif
8514 }
8515 else if (readonlymode && curbuf->b_nwindows == 1)
8516 {
8517 /* When editing an already visited buffer, 'readonly' won't be set
8518 * but the previous value is kept. With ":view" and ":sview" we
8519 * want the file to be readonly, except when another window is
8520 * editing the same buffer. */
8521 curbuf->b_p_ro = TRUE;
8522 }
8523 readonlymode = n;
8524 }
8525 else
8526 {
8527 if (eap->do_ecmd_cmd != NULL)
8528 do_cmdline_cmd(eap->do_ecmd_cmd);
8529#ifdef FEAT_TITLE
8530 n = curwin->w_arg_idx_invalid;
8531#endif
8532 check_arg_idx(curwin);
8533#ifdef FEAT_TITLE
8534 if (n != curwin->w_arg_idx_invalid)
8535 maketitle();
8536#endif
8537 }
8538
8539#ifdef FEAT_WINDOWS
8540 /*
8541 * if ":split file" worked, set alternate file name in old window to new
8542 * file
8543 */
8544 if (old_curwin != NULL
8545 && *eap->arg != NUL
8546 && curwin != old_curwin
8547 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00008548 && old_curwin->w_buffer != curbuf
8549 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008550 old_curwin->w_alt_fnum = curbuf->b_fnum;
8551#endif
8552
8553 ex_no_reprint = TRUE;
8554}
8555
8556#ifndef FEAT_GUI
8557/*
8558 * ":gui" and ":gvim" when there is no GUI.
8559 */
8560 static void
8561ex_nogui(eap)
8562 exarg_T *eap;
8563{
8564 eap->errmsg = e_nogvim;
8565}
8566#endif
8567
8568#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
8569 static void
8570ex_tearoff(eap)
8571 exarg_T *eap;
8572{
8573 gui_make_tearoff(eap->arg);
8574}
8575#endif
8576
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008577#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008578 static void
8579ex_popup(eap)
8580 exarg_T *eap;
8581{
Bram Moolenaar97409f12005-07-08 22:17:29 +00008582 gui_make_popup(eap->arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008583}
8584#endif
8585
Bram Moolenaar071d4272004-06-13 20:20:40 +00008586 static void
8587ex_swapname(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008588 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008589{
8590 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
8591 MSG(_("No swap file"));
8592 else
8593 msg(curbuf->b_ml.ml_mfp->mf_fname);
8594}
8595
8596/*
8597 * ":syncbind" forces all 'scrollbind' windows to have the same relative
8598 * offset.
8599 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
8600 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008601 static void
8602ex_syncbind(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008603 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008604{
8605#ifdef FEAT_SCROLLBIND
8606 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008607 win_T *save_curwin = curwin;
8608 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008609 long topline;
8610 long y;
8611 linenr_T old_linenr = curwin->w_cursor.lnum;
8612
8613 setpcmark();
8614
8615 /*
8616 * determine max topline
8617 */
8618 if (curwin->w_p_scb)
8619 {
8620 topline = curwin->w_topline;
8621 for (wp = firstwin; wp; wp = wp->w_next)
8622 {
8623 if (wp->w_p_scb && wp->w_buffer)
8624 {
8625 y = wp->w_buffer->b_ml.ml_line_count - p_so;
8626 if (topline > y)
8627 topline = y;
8628 }
8629 }
8630 if (topline < 1)
8631 topline = 1;
8632 }
8633 else
8634 {
8635 topline = 1;
8636 }
8637
8638
8639 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008640 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008641 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008642 for (curwin = firstwin; curwin; curwin = curwin->w_next)
8643 {
8644 if (curwin->w_p_scb)
8645 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008646 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008647 y = topline - curwin->w_topline;
8648 if (y > 0)
8649 scrollup(y, TRUE);
8650 else
8651 scrolldown(-y, TRUE);
8652 curwin->w_scbind_pos = topline;
8653 redraw_later(VALID);
8654 cursor_correct();
8655#ifdef FEAT_WINDOWS
8656 curwin->w_redr_status = TRUE;
8657#endif
8658 }
8659 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008660 curwin = save_curwin;
8661 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008662 if (curwin->w_p_scb)
8663 {
8664 did_syncbind = TRUE;
8665 checkpcmark();
8666 if (old_linenr != curwin->w_cursor.lnum)
8667 {
8668 char_u ctrl_o[2];
8669
8670 ctrl_o[0] = Ctrl_O;
8671 ctrl_o[1] = 0;
8672 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
8673 }
8674 }
8675#endif
8676}
8677
8678
8679 static void
8680ex_read(eap)
8681 exarg_T *eap;
8682{
Bram Moolenaardf177f62005-02-22 08:39:57 +00008683 int i;
8684 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
8685 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008686
8687 if (eap->usefilter) /* :r!cmd */
8688 do_bang(1, eap, FALSE, FALSE, TRUE);
8689 else
8690 {
8691 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
8692 return;
8693
8694#ifdef FEAT_BROWSE
8695 if (cmdmod.browse)
8696 {
8697 char_u *browseFile;
8698
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008699 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008700 NULL, NULL, NULL, curbuf);
8701 if (browseFile != NULL)
8702 {
8703 i = readfile(browseFile, NULL,
8704 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8705 vim_free(browseFile);
8706 }
8707 else
8708 i = OK;
8709 }
8710 else
8711#endif
8712 if (*eap->arg == NUL)
8713 {
8714 if (check_fname() == FAIL) /* check for no file name */
8715 return;
8716 i = readfile(curbuf->b_ffname, curbuf->b_fname,
8717 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8718 }
8719 else
8720 {
8721 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
8722 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
8723 i = readfile(eap->arg, NULL,
8724 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8725
8726 }
8727 if (i == FAIL)
8728 {
8729#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
8730 if (!aborting())
8731#endif
8732 EMSG2(_(e_notopen), eap->arg);
8733 }
8734 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008735 {
8736 if (empty && exmode_active)
8737 {
8738 /* Delete the empty line that remains. Historically ex does
8739 * this but vi doesn't. */
8740 if (eap->line2 == 0)
8741 lnum = curbuf->b_ml.ml_line_count;
8742 else
8743 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008744 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008745 {
8746 ml_delete(lnum, FALSE);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008747 if (curwin->w_cursor.lnum > 1
8748 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008749 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00008750 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008751 }
8752 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008753 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008754 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008755 }
8756}
8757
Bram Moolenaarea408852005-06-25 22:49:46 +00008758static char_u *prev_dir = NULL;
8759
8760#if defined(EXITFREE) || defined(PROTO)
8761 void
8762free_cd_dir()
8763{
8764 vim_free(prev_dir);
Bram Moolenaara0174af2008-01-02 20:08:25 +00008765 prev_dir = NULL;
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00008766
8767 vim_free(globaldir);
8768 globaldir = NULL;
Bram Moolenaarea408852005-06-25 22:49:46 +00008769}
8770#endif
8771
Bram Moolenaarf4258302013-06-02 18:20:17 +02008772/*
8773 * Deal with the side effects of changing the current directory.
8774 * When "local" is TRUE then this was after an ":lcd" command.
8775 */
8776 void
8777post_chdir(local)
8778 int local;
8779{
8780 vim_free(curwin->w_localdir);
Bram Moolenaarbd2dc342014-01-10 15:53:13 +01008781 curwin->w_localdir = NULL;
Bram Moolenaarf4258302013-06-02 18:20:17 +02008782 if (local)
8783 {
8784 /* If still in global directory, need to remember current
8785 * directory as global directory. */
8786 if (globaldir == NULL && prev_dir != NULL)
8787 globaldir = vim_strsave(prev_dir);
8788 /* Remember this local directory for the window. */
8789 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8790 curwin->w_localdir = vim_strsave(NameBuff);
8791 }
8792 else
8793 {
8794 /* We are now in the global directory, no need to remember its
8795 * name. */
8796 vim_free(globaldir);
8797 globaldir = NULL;
Bram Moolenaarf4258302013-06-02 18:20:17 +02008798 }
8799
8800 shorten_fnames(TRUE);
8801}
8802
Bram Moolenaarea408852005-06-25 22:49:46 +00008803
Bram Moolenaar071d4272004-06-13 20:20:40 +00008804/*
8805 * ":cd", ":lcd", ":chdir" and ":lchdir".
8806 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00008807 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00008808ex_cd(eap)
8809 exarg_T *eap;
8810{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008811 char_u *new_dir;
8812 char_u *tofree;
8813
8814 new_dir = eap->arg;
8815#if !defined(UNIX) && !defined(VMS)
8816 /* for non-UNIX ":cd" means: print current directory */
8817 if (*new_dir == NUL)
8818 ex_pwd(NULL);
8819 else
8820#endif
8821 {
Bram Moolenaar8e8fe9b2009-03-11 14:37:32 +00008822#ifdef FEAT_AUTOCMD
8823 if (allbuf_locked())
8824 return;
8825#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00008826 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged()
8827 && !eap->forceit)
8828 {
Bram Moolenaar81870892007-11-11 18:17:28 +00008829 EMSG(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00008830 return;
8831 }
8832
Bram Moolenaar071d4272004-06-13 20:20:40 +00008833 /* ":cd -": Change to previous directory */
8834 if (STRCMP(new_dir, "-") == 0)
8835 {
8836 if (prev_dir == NULL)
8837 {
8838 EMSG(_("E186: No previous directory"));
8839 return;
8840 }
8841 new_dir = prev_dir;
8842 }
8843
8844 /* Save current directory for next ":cd -" */
8845 tofree = prev_dir;
8846 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8847 prev_dir = vim_strsave(NameBuff);
8848 else
8849 prev_dir = NULL;
8850
8851#if defined(UNIX) || defined(VMS)
8852 /* for UNIX ":cd" means: go to home directory */
8853 if (*new_dir == NUL)
8854 {
8855 /* use NameBuff for home directory name */
8856# ifdef VMS
8857 char_u *p;
8858
8859 p = mch_getenv((char_u *)"SYS$LOGIN");
8860 if (p == NULL || *p == NUL) /* empty is the same as not set */
8861 NameBuff[0] = NUL;
8862 else
Bram Moolenaarb6356332005-07-18 21:40:44 +00008863 vim_strncpy(NameBuff, p, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008864# else
8865 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
8866# endif
8867 new_dir = NameBuff;
8868 }
8869#endif
8870 if (new_dir == NULL || vim_chdir(new_dir))
8871 EMSG(_(e_failed));
8872 else
8873 {
Bram Moolenaarf4258302013-06-02 18:20:17 +02008874 post_chdir(eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008875
8876 /* Echo the new current directory if the command was typed. */
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00008877 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008878 ex_pwd(eap);
8879 }
8880 vim_free(tofree);
8881 }
8882}
8883
8884/*
8885 * ":pwd".
8886 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008887 static void
8888ex_pwd(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008889 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008890{
8891 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8892 {
8893#ifdef BACKSLASH_IN_FILENAME
8894 slash_adjust(NameBuff);
8895#endif
8896 msg(NameBuff);
8897 }
8898 else
8899 EMSG(_("E187: Unknown"));
8900}
8901
8902/*
8903 * ":=".
8904 */
8905 static void
8906ex_equal(eap)
8907 exarg_T *eap;
8908{
8909 smsg((char_u *)"%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008910 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008911}
8912
8913 static void
8914ex_sleep(eap)
8915 exarg_T *eap;
8916{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008917 int n;
8918 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008919
8920 if (cursor_valid())
8921 {
8922 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
8923 if (n >= 0)
Bram Moolenaar10395d82014-02-05 22:46:52 +01008924 windgoto((int)n, W_WINCOL(curwin) + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008925 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008926
8927 len = eap->line2;
8928 switch (*eap->arg)
8929 {
8930 case 'm': break;
8931 case NUL: len *= 1000L; break;
8932 default: EMSG2(_(e_invarg2), eap->arg); return;
8933 }
8934 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008935}
8936
8937/*
8938 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
8939 */
8940 void
8941do_sleep(msec)
8942 long msec;
8943{
8944 long done;
8945
8946 cursor_on();
8947 out_flush();
8948 for (done = 0; !got_int && done < msec; done += 1000L)
8949 {
8950 ui_delay(msec - done > 1000L ? 1000L : msec - done, TRUE);
8951 ui_breakcheck();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02008952#ifdef FEAT_NETBEANS_INTG
8953 /* Process the netbeans messages that may have been received in the
8954 * call to ui_breakcheck() when the GUI is in use. This may occur when
8955 * running a test case. */
8956 netbeans_parse_messages();
8957#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008958 }
8959}
8960
8961 static void
8962do_exmap(eap, isabbrev)
8963 exarg_T *eap;
8964 int isabbrev;
8965{
8966 int mode;
8967 char_u *cmdp;
8968
8969 cmdp = eap->cmd;
8970 mode = get_map_mode(&cmdp, eap->forceit || isabbrev);
8971
8972 switch (do_map((*cmdp == 'n') ? 2 : (*cmdp == 'u'),
8973 eap->arg, mode, isabbrev))
8974 {
8975 case 1: EMSG(_(e_invarg));
8976 break;
8977 case 2: EMSG(isabbrev ? _(e_noabbr) : _(e_nomap));
8978 break;
8979 }
8980}
8981
8982/*
8983 * ":winsize" command (obsolete).
8984 */
8985 static void
8986ex_winsize(eap)
8987 exarg_T *eap;
8988{
8989 int w, h;
8990 char_u *arg = eap->arg;
8991 char_u *p;
8992
8993 w = getdigits(&arg);
8994 arg = skipwhite(arg);
8995 p = arg;
8996 h = getdigits(&arg);
8997 if (*p != NUL && *arg == NUL)
8998 set_shellsize(w, h, TRUE);
8999 else
9000 EMSG(_("E465: :winsize requires two number arguments"));
9001}
9002
9003#ifdef FEAT_WINDOWS
9004 static void
9005ex_wincmd(eap)
9006 exarg_T *eap;
9007{
9008 int xchar = NUL;
9009 char_u *p;
9010
9011 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
9012 {
9013 /* CTRL-W g and CTRL-W CTRL-G have an extra command character */
9014 if (eap->arg[1] == NUL)
9015 {
9016 EMSG(_(e_invarg));
9017 return;
9018 }
9019 xchar = eap->arg[1];
9020 p = eap->arg + 2;
9021 }
9022 else
9023 p = eap->arg + 1;
9024
9025 eap->nextcmd = check_nextcmd(p);
9026 p = skipwhite(p);
9027 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
9028 EMSG(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02009029 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009030 {
9031 /* Pass flags on for ":vertical wincmd ]". */
9032 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009033 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009034 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
9035 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009036 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009037 }
9038}
9039#endif
9040
Bram Moolenaar843ee412004-06-30 16:16:41 +00009041#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009042/*
9043 * ":winpos".
9044 */
9045 static void
9046ex_winpos(eap)
9047 exarg_T *eap;
9048{
9049 int x, y;
9050 char_u *arg = eap->arg;
9051 char_u *p;
9052
9053 if (*arg == NUL)
9054 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00009055# if defined(FEAT_GUI) || defined(MSWIN)
9056# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00009057 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00009058# else
9059 if (mch_get_winpos(&x, &y) != FAIL)
9060# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009061 {
9062 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
9063 msg(IObuff);
9064 }
9065 else
9066# endif
9067 EMSG(_("E188: Obtaining window position not implemented for this platform"));
9068 }
9069 else
9070 {
9071 x = getdigits(&arg);
9072 arg = skipwhite(arg);
9073 p = arg;
9074 y = getdigits(&arg);
9075 if (*p == NUL || *arg != NUL)
9076 {
9077 EMSG(_("E466: :winpos requires two number arguments"));
9078 return;
9079 }
9080# ifdef FEAT_GUI
9081 if (gui.in_use)
9082 gui_mch_set_winpos(x, y);
9083 else if (gui.starting)
9084 {
9085 /* Remember the coordinates for when the window is opened. */
9086 gui_win_x = x;
9087 gui_win_y = y;
9088 }
9089# ifdef HAVE_TGETENT
9090 else
9091# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00009092# else
9093# ifdef MSWIN
9094 mch_set_winpos(x, y);
9095# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009096# endif
9097# ifdef HAVE_TGETENT
9098 if (*T_CWP)
9099 term_set_winpos(x, y);
9100# endif
9101 }
9102}
9103#endif
9104
9105/*
9106 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
9107 */
9108 static void
9109ex_operators(eap)
9110 exarg_T *eap;
9111{
9112 oparg_T oa;
9113
9114 clear_oparg(&oa);
9115 oa.regname = eap->regname;
9116 oa.start.lnum = eap->line1;
9117 oa.end.lnum = eap->line2;
9118 oa.line_count = eap->line2 - eap->line1 + 1;
9119 oa.motion_type = MLINE;
9120#ifdef FEAT_VIRTUALEDIT
9121 virtual_op = FALSE;
9122#endif
9123 if (eap->cmdidx != CMD_yank) /* position cursor for undo */
9124 {
9125 setpcmark();
9126 curwin->w_cursor.lnum = eap->line1;
9127 beginline(BL_SOL | BL_FIX);
9128 }
9129
Bram Moolenaard07c6e12013-11-21 14:21:40 +01009130 if (VIsual_active)
9131 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01009132
Bram Moolenaar071d4272004-06-13 20:20:40 +00009133 switch (eap->cmdidx)
9134 {
9135 case CMD_delete:
9136 oa.op_type = OP_DELETE;
9137 op_delete(&oa);
9138 break;
9139
9140 case CMD_yank:
9141 oa.op_type = OP_YANK;
9142 (void)op_yank(&oa, FALSE, TRUE);
9143 break;
9144
9145 default: /* CMD_rshift or CMD_lshift */
Bram Moolenaar6e707362013-06-14 19:15:58 +02009146 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009147#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02009148 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
9149#else
9150 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00009151#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02009152 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009153 oa.op_type = OP_RSHIFT;
9154 else
9155 oa.op_type = OP_LSHIFT;
9156 op_shift(&oa, FALSE, eap->amount);
9157 break;
9158 }
9159#ifdef FEAT_VIRTUALEDIT
9160 virtual_op = MAYBE;
9161#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00009162 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009163}
9164
9165/*
9166 * ":put".
9167 */
9168 static void
9169ex_put(eap)
9170 exarg_T *eap;
9171{
9172 /* ":0put" works like ":1put!". */
9173 if (eap->line2 == 0)
9174 {
9175 eap->line2 = 1;
9176 eap->forceit = TRUE;
9177 }
9178 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009179 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
9180 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009181}
9182
9183/*
9184 * Handle ":copy" and ":move".
9185 */
9186 static void
9187ex_copymove(eap)
9188 exarg_T *eap;
9189{
9190 long n;
9191
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01009192 n = get_address(&eap->arg, eap->addr_type, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009193 if (eap->arg == NULL) /* error detected */
9194 {
9195 eap->nextcmd = NULL;
9196 return;
9197 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00009198 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009199
9200 /*
9201 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
9202 */
9203 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
9204 {
9205 EMSG(_(e_invaddr));
9206 return;
9207 }
9208
9209 if (eap->cmdidx == CMD_move)
9210 {
9211 if (do_move(eap->line1, eap->line2, n) == FAIL)
9212 return;
9213 }
9214 else
9215 ex_copy(eap->line1, eap->line2, n);
9216 u_clearline();
9217 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009218 ex_may_print(eap);
9219}
9220
9221/*
9222 * Print the current line if flags were given to the Ex command.
9223 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02009224 void
Bram Moolenaardf177f62005-02-22 08:39:57 +00009225ex_may_print(eap)
9226 exarg_T *eap;
9227{
9228 if (eap->flags != 0)
9229 {
9230 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
9231 (eap->flags & EXFLAG_LIST));
9232 ex_no_reprint = TRUE;
9233 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009234}
9235
9236/*
9237 * ":smagic" and ":snomagic".
9238 */
9239 static void
9240ex_submagic(eap)
9241 exarg_T *eap;
9242{
9243 int magic_save = p_magic;
9244
9245 p_magic = (eap->cmdidx == CMD_smagic);
9246 do_sub(eap);
9247 p_magic = magic_save;
9248}
9249
9250/*
9251 * ":join".
9252 */
9253 static void
9254ex_join(eap)
9255 exarg_T *eap;
9256{
9257 curwin->w_cursor.lnum = eap->line1;
9258 if (eap->line1 == eap->line2)
9259 {
9260 if (eap->addr_count >= 2) /* :2,2join does nothing */
9261 return;
9262 if (eap->line2 == curbuf->b_ml.ml_line_count)
9263 {
9264 beep_flush();
9265 return;
9266 }
9267 ++eap->line2;
9268 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02009269 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009270 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009271 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009272}
9273
9274/*
9275 * ":[addr]@r" or ":[addr]*r": execute register
9276 */
9277 static void
9278ex_at(eap)
9279 exarg_T *eap;
9280{
9281 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00009282 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009283
9284 curwin->w_cursor.lnum = eap->line2;
9285
9286#ifdef USE_ON_FLY_SCROLL
9287 dont_scroll = TRUE; /* disallow scrolling here */
9288#endif
9289
9290 /* get the register name. No name means to use the previous one */
9291 c = *eap->arg;
9292 if (c == NUL || (c == '*' && *eap->cmd == '*'))
9293 c = '@';
Bram Moolenaard333d1e2006-11-07 17:43:47 +00009294 /* Put the register in the typeahead buffer with the "silent" flag. */
9295 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
9296 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009297 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009298 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00009299 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009300 else
9301 {
9302 int save_efr = exec_from_reg;
9303
9304 exec_from_reg = TRUE;
9305
9306 /*
9307 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00009308 * Continue until the stuff buffer is empty and all added characters
9309 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009310 */
Bram Moolenaar60462872009-11-03 11:40:19 +00009311 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009312 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
9313
9314 exec_from_reg = save_efr;
9315 }
9316}
9317
9318/*
9319 * ":!".
9320 */
9321 static void
9322ex_bang(eap)
9323 exarg_T *eap;
9324{
9325 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
9326}
9327
9328/*
9329 * ":undo".
9330 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009331 static void
9332ex_undo(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009333 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009334{
Bram Moolenaard3667a22006-03-16 21:35:52 +00009335 if (eap->addr_count == 1) /* :undo 123 */
Bram Moolenaar730cde92010-06-27 05:18:54 +02009336 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00009337 else
9338 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009339}
9340
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009341#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02009342 static void
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009343ex_wundo(eap)
9344 exarg_T *eap;
9345{
9346 char_u hash[UNDO_HASH_SIZE];
9347
9348 u_compute_hash(hash);
9349 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
9350}
9351
Bram Moolenaar6df6f472010-07-18 18:04:50 +02009352 static void
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009353ex_rundo(eap)
9354 exarg_T *eap;
9355{
9356 char_u hash[UNDO_HASH_SIZE];
9357
9358 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02009359 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009360}
9361#endif
9362
Bram Moolenaar071d4272004-06-13 20:20:40 +00009363/*
9364 * ":redo".
9365 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009366 static void
9367ex_redo(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009368 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009369{
9370 u_redo(1);
9371}
9372
9373/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009374 * ":earlier" and ":later".
9375 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009376 static void
9377ex_later(eap)
9378 exarg_T *eap;
9379{
9380 long count = 0;
9381 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02009382 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009383 char_u *p = eap->arg;
9384
9385 if (*p == NUL)
9386 count = 1;
9387 else if (isdigit(*p))
9388 {
9389 count = getdigits(&p);
9390 switch (*p)
9391 {
9392 case 's': ++p; sec = TRUE; break;
9393 case 'm': ++p; sec = TRUE; count *= 60; break;
9394 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02009395 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
9396 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009397 }
9398 }
9399
9400 if (*p != NUL)
9401 EMSG2(_(e_invarg2), eap->arg);
9402 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02009403 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
9404 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009405}
9406
9407/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009408 * ":redir": start/stop redirection.
9409 */
9410 static void
9411ex_redir(eap)
9412 exarg_T *eap;
9413{
9414 char *mode;
9415 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00009416 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009417
9418 if (STRICMP(eap->arg, "END") == 0)
9419 close_redir();
9420 else
9421 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009422 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009423 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009424 ++arg;
9425 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009426 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009427 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009428 mode = "a";
9429 }
9430 else
9431 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00009432 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009433
9434 close_redir();
9435
9436 /* Expand environment variables and "~/". */
Bram Moolenaarca472992005-01-21 11:46:23 +00009437 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009438 if (fname == NULL)
9439 return;
9440#ifdef FEAT_BROWSE
9441 if (cmdmod.browse)
9442 {
9443 char_u *browseFile;
9444
Bram Moolenaar7171abe2004-10-11 10:06:20 +00009445 browseFile = do_browse(BROWSE_SAVE,
9446 (char_u *)_("Save Redirection"),
9447 fname, NULL, NULL, BROWSE_FILTER_ALL_FILES, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009448 if (browseFile == NULL)
9449 return; /* operation cancelled */
9450 vim_free(fname);
9451 fname = browseFile;
9452 eap->forceit = TRUE; /* since dialog already asked */
9453 }
9454#endif
9455
9456 redir_fd = open_exfile(fname, eap->forceit, mode);
9457 vim_free(fname);
9458 }
9459#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00009460 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009461 {
9462 /* redirect to a register a-z (resp. A-Z for appending) */
9463 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00009464 ++arg;
9465 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009466# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00009467 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00009468 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00009469# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00009470 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009471 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009472 redir_reg = *arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009473 if (*arg == '>' && arg[1] == '>') /* append */
Bram Moolenaard9d30582005-05-18 22:10:28 +00009474 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009475 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009476 {
Bram Moolenaarc188b882007-10-19 14:20:54 +00009477 /* Can use both "@a" and "@a>". */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00009478 if (*arg == '>')
9479 arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009480 /* Make register empty when not using @A-@Z and the
9481 * command is valid. */
9482 if (*arg == NUL && !isupper(redir_reg))
9483 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009484 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00009485 }
9486 if (*arg != NUL)
9487 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00009488 redir_reg = 0;
Bram Moolenaard9d30582005-05-18 22:10:28 +00009489 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009490 }
9491 }
9492 else if (*arg == '=' && arg[1] == '>')
9493 {
9494 int append;
9495
9496 /* redirect to a variable */
9497 close_redir();
9498 arg += 2;
9499
9500 if (*arg == '>')
9501 {
9502 ++arg;
9503 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009504 }
9505 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00009506 append = FALSE;
9507
9508 if (var_redir_start(skipwhite(arg), append) == OK)
9509 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009510 }
9511#endif
9512
9513 /* TODO: redirect to a buffer */
9514
Bram Moolenaar071d4272004-06-13 20:20:40 +00009515 else
Bram Moolenaarca472992005-01-21 11:46:23 +00009516 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009517 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00009518
9519 /* Make sure redirection is not off. Can happen for cmdline completion
9520 * that indirectly invokes a command to catch its output. */
9521 if (redir_fd != NULL
9522#ifdef FEAT_EVAL
9523 || redir_reg || redir_vname
9524#endif
9525 )
9526 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009527}
9528
9529/*
9530 * ":redraw": force redraw
9531 */
9532 static void
9533ex_redraw(eap)
9534 exarg_T *eap;
9535{
9536 int r = RedrawingDisabled;
9537 int p = p_lz;
9538
9539 RedrawingDisabled = 0;
9540 p_lz = FALSE;
9541 update_topline();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009542 update_screen(eap->forceit ? CLEAR : VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009543#ifdef FEAT_TITLE
9544 if (need_maketitle)
9545 maketitle();
9546#endif
9547 RedrawingDisabled = r;
9548 p_lz = p;
9549
9550 /* Reset msg_didout, so that a message that's there is overwritten. */
9551 msg_didout = FALSE;
9552 msg_col = 0;
9553
Bram Moolenaar56667a52013-07-17 11:54:28 +02009554 /* No need to wait after an intentional redraw. */
9555 need_wait_return = FALSE;
9556
Bram Moolenaar071d4272004-06-13 20:20:40 +00009557 out_flush();
9558}
9559
9560/*
9561 * ":redrawstatus": force redraw of status line(s)
9562 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009563 static void
9564ex_redrawstatus(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009565 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009566{
9567#if defined(FEAT_WINDOWS)
9568 int r = RedrawingDisabled;
9569 int p = p_lz;
9570
9571 RedrawingDisabled = 0;
9572 p_lz = FALSE;
9573 if (eap->forceit)
9574 status_redraw_all();
9575 else
9576 status_redraw_curbuf();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009577 update_screen(VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009578 RedrawingDisabled = r;
9579 p_lz = p;
9580 out_flush();
9581#endif
9582}
9583
9584 static void
9585close_redir()
9586{
9587 if (redir_fd != NULL)
9588 {
9589 fclose(redir_fd);
9590 redir_fd = NULL;
9591 }
9592#ifdef FEAT_EVAL
9593 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009594 if (redir_vname)
9595 {
9596 var_redir_stop();
9597 redir_vname = 0;
9598 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009599#endif
9600}
9601
9602#if defined(FEAT_SESSION) && defined(USE_CRNL)
9603# define MKSESSION_NL
9604static int mksession_nl = FALSE; /* use NL only in put_eol() */
9605#endif
9606
9607/*
9608 * ":mkexrc", ":mkvimrc", ":mkview" and ":mksession".
9609 */
9610 static void
9611ex_mkrc(eap)
9612 exarg_T *eap;
9613{
9614 FILE *fd;
9615 int failed = FALSE;
9616 char_u *fname;
9617#ifdef FEAT_BROWSE
9618 char_u *browseFile = NULL;
9619#endif
9620#ifdef FEAT_SESSION
9621 int view_session = FALSE;
9622 int using_vdir = FALSE; /* using 'viewdir'? */
9623 char_u *viewFile = NULL;
9624 unsigned *flagp;
9625#endif
9626
9627 if (eap->cmdidx == CMD_mksession || eap->cmdidx == CMD_mkview)
9628 {
9629#ifdef FEAT_SESSION
9630 view_session = TRUE;
9631#else
9632 ex_ni(eap);
9633 return;
9634#endif
9635 }
9636
9637#ifdef FEAT_SESSION
Bram Moolenaar8d594672009-07-01 18:18:57 +00009638 /* Use the short file name until ":lcd" is used. We also don't use the
9639 * short file name when 'acd' is set, that is checked later. */
Bram Moolenaareeefcc72007-05-01 21:21:21 +00009640 did_lcd = FALSE;
9641
Bram Moolenaar071d4272004-06-13 20:20:40 +00009642 /* ":mkview" or ":mkview 9": generate file name with 'viewdir' */
9643 if (eap->cmdidx == CMD_mkview
9644 && (*eap->arg == NUL
9645 || (vim_isdigit(*eap->arg) && eap->arg[1] == NUL)))
9646 {
9647 eap->forceit = TRUE;
9648 fname = get_view_file(*eap->arg);
9649 if (fname == NULL)
9650 return;
9651 viewFile = fname;
9652 using_vdir = TRUE;
9653 }
9654 else
9655#endif
9656 if (*eap->arg != NUL)
9657 fname = eap->arg;
9658 else if (eap->cmdidx == CMD_mkvimrc)
9659 fname = (char_u *)VIMRC_FILE;
9660#ifdef FEAT_SESSION
9661 else if (eap->cmdidx == CMD_mksession)
9662 fname = (char_u *)SESSION_FILE;
9663#endif
9664 else
9665 fname = (char_u *)EXRC_FILE;
9666
9667#ifdef FEAT_BROWSE
9668 if (cmdmod.browse)
9669 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00009670 browseFile = do_browse(BROWSE_SAVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009671# ifdef FEAT_SESSION
9672 eap->cmdidx == CMD_mkview ? (char_u *)_("Save View") :
9673 eap->cmdidx == CMD_mksession ? (char_u *)_("Save Session") :
9674# endif
9675 (char_u *)_("Save Setup"),
9676 fname, (char_u *)"vim", NULL, BROWSE_FILTER_MACROS, NULL);
9677 if (browseFile == NULL)
9678 goto theend;
9679 fname = browseFile;
9680 eap->forceit = TRUE; /* since dialog already asked */
9681 }
9682#endif
9683
9684#if defined(FEAT_SESSION) && defined(vim_mkdir)
9685 /* When using 'viewdir' may have to create the directory. */
9686 if (using_vdir && !mch_isdir(p_vdir))
Bram Moolenaardf177f62005-02-22 08:39:57 +00009687 vim_mkdir_emsg(p_vdir, 0755);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009688#endif
9689
9690 fd = open_exfile(fname, eap->forceit, WRITEBIN);
9691 if (fd != NULL)
9692 {
9693#ifdef FEAT_SESSION
9694 if (eap->cmdidx == CMD_mkview)
9695 flagp = &vop_flags;
9696 else
9697 flagp = &ssop_flags;
9698#endif
9699
9700#ifdef MKSESSION_NL
9701 /* "unix" in 'sessionoptions': use NL line separator */
9702 if (view_session && (*flagp & SSOP_UNIX))
9703 mksession_nl = TRUE;
9704#endif
9705
9706 /* Write the version command for :mkvimrc */
9707 if (eap->cmdidx == CMD_mkvimrc)
9708 (void)put_line(fd, "version 6.0");
9709
9710#ifdef FEAT_SESSION
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009711 if (eap->cmdidx == CMD_mksession)
9712 {
9713 if (put_line(fd, "let SessionLoad = 1") == FAIL)
9714 failed = TRUE;
9715 }
9716
Bram Moolenaar071d4272004-06-13 20:20:40 +00009717 if (eap->cmdidx != CMD_mkview)
9718#endif
9719 {
9720 /* Write setting 'compatible' first, because it has side effects.
9721 * For that same reason only do it when needed. */
9722 if (p_cp)
9723 (void)put_line(fd, "if !&cp | set cp | endif");
9724 else
9725 (void)put_line(fd, "if &cp | set nocp | endif");
9726 }
9727
9728#ifdef FEAT_SESSION
9729 if (!view_session
9730 || (eap->cmdidx == CMD_mksession
9731 && (*flagp & SSOP_OPTIONS)))
9732#endif
9733 failed |= (makemap(fd, NULL) == FAIL
9734 || makeset(fd, OPT_GLOBAL, FALSE) == FAIL);
9735
9736#ifdef FEAT_SESSION
9737 if (!failed && view_session)
9738 {
9739 if (put_line(fd, "let s:so_save = &so | let s:siso_save = &siso | set so=0 siso=0") == FAIL)
9740 failed = TRUE;
9741 if (eap->cmdidx == CMD_mksession)
9742 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02009743 char_u *dirnow; /* current directory */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009744
Bram Moolenaard9462e32011-04-11 21:35:11 +02009745 dirnow = alloc(MAXPATHL);
9746 if (dirnow == NULL)
9747 failed = TRUE;
9748 else
9749 {
9750 /*
9751 * Change to session file's dir.
9752 */
9753 if (mch_dirname(dirnow, MAXPATHL) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00009754 || mch_chdir((char *)dirnow) != 0)
Bram Moolenaard9462e32011-04-11 21:35:11 +02009755 *dirnow = NUL;
9756 if (*dirnow != NUL && (ssop_flags & SSOP_SESDIR))
9757 {
9758 if (vim_chdirfile(fname) == OK)
9759 shorten_fnames(TRUE);
9760 }
9761 else if (*dirnow != NUL
9762 && (ssop_flags & SSOP_CURDIR) && globaldir != NULL)
9763 {
9764 if (mch_chdir((char *)globaldir) == 0)
9765 shorten_fnames(TRUE);
9766 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009767
Bram Moolenaard9462e32011-04-11 21:35:11 +02009768 failed |= (makeopens(fd, dirnow) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009769
Bram Moolenaard9462e32011-04-11 21:35:11 +02009770 /* restore original dir */
9771 if (*dirnow != NUL && ((ssop_flags & SSOP_SESDIR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009772 || ((ssop_flags & SSOP_CURDIR) && globaldir != NULL)))
Bram Moolenaard9462e32011-04-11 21:35:11 +02009773 {
9774 if (mch_chdir((char *)dirnow) != 0)
9775 EMSG(_(e_prev_dir));
9776 shorten_fnames(TRUE);
9777 }
9778 vim_free(dirnow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009779 }
9780 }
9781 else
9782 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +00009783 failed |= (put_view(fd, curwin, !using_vdir, flagp,
9784 -1) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009785 }
9786 if (put_line(fd, "let &so = s:so_save | let &siso = s:siso_save")
9787 == FAIL)
9788 failed = TRUE;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009789 if (put_line(fd, "doautoall SessionLoadPost") == FAIL)
9790 failed = TRUE;
Bram Moolenaar4770d092006-01-12 23:22:24 +00009791 if (eap->cmdidx == CMD_mksession)
9792 {
9793 if (put_line(fd, "unlet SessionLoad") == FAIL)
9794 failed = TRUE;
9795 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009796 }
9797#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009798 if (put_line(fd, "\" vim: set ft=vim :") == FAIL)
9799 failed = TRUE;
9800
Bram Moolenaar071d4272004-06-13 20:20:40 +00009801 failed |= fclose(fd);
9802
9803 if (failed)
9804 EMSG(_(e_write));
9805#if defined(FEAT_EVAL) && defined(FEAT_SESSION)
9806 else if (eap->cmdidx == CMD_mksession)
9807 {
9808 /* successful session write - set this_session var */
Bram Moolenaard9462e32011-04-11 21:35:11 +02009809 char_u *tbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009810
Bram Moolenaard9462e32011-04-11 21:35:11 +02009811 tbuf = alloc(MAXPATHL);
9812 if (tbuf != NULL)
9813 {
9814 if (vim_FullName(fname, tbuf, MAXPATHL, FALSE) == OK)
9815 set_vim_var_string(VV_THIS_SESSION, tbuf, -1);
9816 vim_free(tbuf);
9817 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009818 }
9819#endif
9820#ifdef MKSESSION_NL
9821 mksession_nl = FALSE;
9822#endif
9823 }
9824
9825#ifdef FEAT_BROWSE
9826theend:
9827 vim_free(browseFile);
9828#endif
9829#ifdef FEAT_SESSION
9830 vim_free(viewFile);
9831#endif
9832}
9833
Bram Moolenaardf177f62005-02-22 08:39:57 +00009834#if ((defined(FEAT_SESSION) || defined(FEAT_EVAL)) && defined(vim_mkdir)) \
9835 || defined(PROTO)
9836 int
9837vim_mkdir_emsg(name, prot)
9838 char_u *name;
Bram Moolenaar78a15312009-05-15 19:33:18 +00009839 int prot UNUSED;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009840{
9841 if (vim_mkdir(name, prot) != 0)
9842 {
9843 EMSG2(_("E739: Cannot create directory: %s"), name);
9844 return FAIL;
9845 }
9846 return OK;
9847}
9848#endif
9849
Bram Moolenaar071d4272004-06-13 20:20:40 +00009850/*
9851 * Open a file for writing for an Ex command, with some checks.
9852 * Return file descriptor, or NULL on failure.
9853 */
9854 FILE *
9855open_exfile(fname, forceit, mode)
9856 char_u *fname;
9857 int forceit;
9858 char *mode; /* "w" for create new file or "a" for append */
9859{
9860 FILE *fd;
9861
9862#ifdef UNIX
9863 /* with Unix it is possible to open a directory */
9864 if (mch_isdir(fname))
9865 {
9866 EMSG2(_(e_isadir2), fname);
9867 return NULL;
9868 }
9869#endif
9870 if (!forceit && *mode != 'a' && vim_fexists(fname))
9871 {
9872 EMSG2(_("E189: \"%s\" exists (add ! to override)"), fname);
9873 return NULL;
9874 }
9875
9876 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
9877 EMSG2(_("E190: Cannot open \"%s\" for writing"), fname);
9878
9879 return fd;
9880}
9881
9882/*
9883 * ":mark" and ":k".
9884 */
9885 static void
9886ex_mark(eap)
9887 exarg_T *eap;
9888{
9889 pos_T pos;
9890
9891 if (*eap->arg == NUL) /* No argument? */
9892 EMSG(_(e_argreq));
9893 else if (eap->arg[1] != NUL) /* more than one character? */
9894 EMSG(_(e_trailing));
9895 else
9896 {
9897 pos = curwin->w_cursor; /* save curwin->w_cursor */
9898 curwin->w_cursor.lnum = eap->line2;
9899 beginline(BL_WHITE | BL_FIX);
9900 if (setmark(*eap->arg) == FAIL) /* set mark */
9901 EMSG(_("E191: Argument must be a letter or forward/backward quote"));
9902 curwin->w_cursor = pos; /* restore curwin->w_cursor */
9903 }
9904}
9905
9906/*
9907 * Update w_topline, w_leftcol and the cursor position.
9908 */
9909 void
9910update_topline_cursor()
9911{
9912 check_cursor(); /* put cursor on valid line */
9913 update_topline();
9914 if (!curwin->w_p_wrap)
9915 validate_cursor();
9916 update_curswant();
9917}
9918
9919#ifdef FEAT_EX_EXTRA
9920/*
9921 * ":normal[!] {commands}": Execute normal mode commands.
9922 */
9923 static void
9924ex_normal(eap)
9925 exarg_T *eap;
9926{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009927 int save_msg_scroll = msg_scroll;
9928 int save_restart_edit = restart_edit;
9929 int save_msg_didout = msg_didout;
9930 int save_State = State;
9931 tasave_T tabuf;
9932 int save_insertmode = p_im;
9933 int save_finish_op = finish_op;
Bram Moolenaar38084112008-07-26 14:05:07 +00009934 int save_opcount = opcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009935#ifdef FEAT_MBYTE
9936 char_u *arg = NULL;
9937 int l;
9938 char_u *p;
9939#endif
9940
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00009941 if (ex_normal_lock > 0)
9942 {
9943 EMSG(_(e_secure));
9944 return;
9945 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009946 if (ex_normal_busy >= p_mmd)
9947 {
9948 EMSG(_("E192: Recursive use of :normal too deep"));
9949 return;
9950 }
9951 ++ex_normal_busy;
9952
9953 msg_scroll = FALSE; /* no msg scrolling in Normal mode */
9954 restart_edit = 0; /* don't go to Insert mode */
9955 p_im = FALSE; /* don't use 'insertmode' */
9956
9957#ifdef FEAT_MBYTE
9958 /*
9959 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
9960 * this for the K_SPECIAL leading byte, otherwise special keys will not
9961 * work.
9962 */
9963 if (has_mbyte)
9964 {
9965 int len = 0;
9966
9967 /* Count the number of characters to be escaped. */
9968 for (p = eap->arg; *p != NUL; ++p)
9969 {
9970# ifdef FEAT_GUI
9971 if (*p == CSI) /* leadbyte CSI */
9972 len += 2;
9973# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009974 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009975 if (*++p == K_SPECIAL /* trailbyte K_SPECIAL or CSI */
9976# ifdef FEAT_GUI
9977 || *p == CSI
9978# endif
9979 )
9980 len += 2;
9981 }
9982 if (len > 0)
9983 {
9984 arg = alloc((unsigned)(STRLEN(eap->arg) + len + 1));
9985 if (arg != NULL)
9986 {
9987 len = 0;
9988 for (p = eap->arg; *p != NUL; ++p)
9989 {
9990 arg[len++] = *p;
9991# ifdef FEAT_GUI
9992 if (*p == CSI)
9993 {
9994 arg[len++] = KS_EXTRA;
9995 arg[len++] = (int)KE_CSI;
9996 }
9997# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009998 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009999 {
10000 arg[len++] = *++p;
10001 if (*p == K_SPECIAL)
10002 {
10003 arg[len++] = KS_SPECIAL;
10004 arg[len++] = KE_FILLER;
10005 }
10006# ifdef FEAT_GUI
10007 else if (*p == CSI)
10008 {
10009 arg[len++] = KS_EXTRA;
10010 arg[len++] = (int)KE_CSI;
10011 }
10012# endif
10013 }
10014 arg[len] = NUL;
10015 }
10016 }
10017 }
10018 }
10019#endif
10020
10021 /*
10022 * Save the current typeahead. This is required to allow using ":normal"
10023 * from an event handler and makes sure we don't hang when the argument
10024 * ends with half a command.
10025 */
10026 save_typeahead(&tabuf);
10027 if (tabuf.typebuf_valid)
10028 {
10029 /*
10030 * Repeat the :normal command for each line in the range. When no
10031 * range given, execute it just once, without positioning the cursor
10032 * first.
10033 */
10034 do
10035 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010036 if (eap->addr_count != 0)
10037 {
10038 curwin->w_cursor.lnum = eap->line1++;
10039 curwin->w_cursor.col = 0;
10040 }
10041
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010042 exec_normal_cmd(
Bram Moolenaar071d4272004-06-13 20:20:40 +000010043#ifdef FEAT_MBYTE
10044 arg != NULL ? arg :
10045#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010046 eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010047 }
10048 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
10049 }
10050
10051 /* Might not return to the main loop when in an event handler. */
10052 update_topline_cursor();
10053
10054 /* Restore the previous typeahead. */
10055 restore_typeahead(&tabuf);
10056
10057 --ex_normal_busy;
10058 msg_scroll = save_msg_scroll;
10059 restart_edit = save_restart_edit;
10060 p_im = save_insertmode;
10061 finish_op = save_finish_op;
Bram Moolenaar38084112008-07-26 14:05:07 +000010062 opcount = save_opcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010063 msg_didout |= save_msg_didout; /* don't reset msg_didout now */
10064
10065 /* Restore the state (needed when called from a function executed for
Bram Moolenaareda73602014-11-05 09:53:23 +010010066 * 'indentexpr'). Update the mouse and cursor, they may have changed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010067 State = save_State;
Bram Moolenaareda73602014-11-05 09:53:23 +010010068#ifdef FEAT_MOUSE
10069 setmouse();
10070#endif
10071#ifdef CURSOR_SHAPE
10072 ui_cursor_shape(); /* may show different cursor shape */
10073#endif
10074
Bram Moolenaar071d4272004-06-13 20:20:40 +000010075#ifdef FEAT_MBYTE
10076 vim_free(arg);
10077#endif
10078}
10079
10080/*
Bram Moolenaar64969662005-12-14 21:59:55 +000010081 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010082 */
10083 static void
10084ex_startinsert(eap)
10085 exarg_T *eap;
10086{
Bram Moolenaarfd371682005-01-14 21:42:54 +000010087 if (eap->forceit)
10088 {
10089 coladvance((colnr_T)MAXCOL);
10090 curwin->w_curswant = MAXCOL;
10091 curwin->w_set_curswant = FALSE;
10092 }
10093
Bram Moolenaara40c5002005-01-09 21:16:21 +000010094 /* Ignore the command when already in Insert mode. Inserting an
10095 * expression register that invokes a function can do this. */
10096 if (State & INSERT)
10097 return;
10098
Bram Moolenaar64969662005-12-14 21:59:55 +000010099 if (eap->cmdidx == CMD_startinsert)
10100 restart_edit = 'a';
10101 else if (eap->cmdidx == CMD_startreplace)
10102 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010103 else
Bram Moolenaar64969662005-12-14 21:59:55 +000010104 restart_edit = 'V';
10105
10106 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010107 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010108 if (eap->cmdidx == CMD_startinsert)
10109 restart_edit = 'i';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010110 curwin->w_curswant = 0; /* avoid MAXCOL */
10111 }
10112}
10113
10114/*
10115 * ":stopinsert"
10116 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010117 static void
10118ex_stopinsert(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010119 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010120{
10121 restart_edit = 0;
10122 stop_insert_mode = TRUE;
10123}
10124#endif
10125
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010126#if defined(FEAT_EX_EXTRA) || defined(FEAT_MENU) || defined(PROTO)
10127/*
10128 * Execute normal mode command "cmd".
10129 * "remap" can be REMAP_NONE or REMAP_YES.
10130 */
10131 void
10132exec_normal_cmd(cmd, remap, silent)
10133 char_u *cmd;
10134 int remap;
10135 int silent;
10136{
10137 oparg_T oa;
10138
10139 /*
10140 * Stuff the argument into the typeahead buffer.
10141 * Execute normal_cmd() until there is no typeahead left.
10142 */
10143 clear_oparg(&oa);
10144 finish_op = FALSE;
10145 ins_typebuf(cmd, remap, 0, TRUE, silent);
10146 while ((!stuff_empty() || (!typebuf_typed() && typebuf.tb_len > 0))
10147 && !got_int)
10148 {
10149 update_topline_cursor();
Bram Moolenaar48ac02c2011-01-17 19:50:06 +010010150 normal_cmd(&oa, TRUE); /* execute a Normal mode cmd */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010151 }
10152}
10153#endif
10154
Bram Moolenaar071d4272004-06-13 20:20:40 +000010155#ifdef FEAT_FIND_ID
10156 static void
10157ex_checkpath(eap)
10158 exarg_T *eap;
10159{
10160 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
10161 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
10162 (linenr_T)1, (linenr_T)MAXLNUM);
10163}
10164
10165#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
10166/*
10167 * ":psearch"
10168 */
10169 static void
10170ex_psearch(eap)
10171 exarg_T *eap;
10172{
10173 g_do_tagpreview = p_pvh;
10174 ex_findpat(eap);
10175 g_do_tagpreview = 0;
10176}
10177#endif
10178
10179 static void
10180ex_findpat(eap)
10181 exarg_T *eap;
10182{
10183 int whole = TRUE;
10184 long n;
10185 char_u *p;
10186 int action;
10187
10188 switch (cmdnames[eap->cmdidx].cmd_name[2])
10189 {
10190 case 'e': /* ":psearch", ":isearch" and ":dsearch" */
10191 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
10192 action = ACTION_GOTO;
10193 else
10194 action = ACTION_SHOW;
10195 break;
10196 case 'i': /* ":ilist" and ":dlist" */
10197 action = ACTION_SHOW_ALL;
10198 break;
10199 case 'u': /* ":ijump" and ":djump" */
10200 action = ACTION_GOTO;
10201 break;
10202 default: /* ":isplit" and ":dsplit" */
10203 action = ACTION_SPLIT;
10204 break;
10205 }
10206
10207 n = 1;
10208 if (vim_isdigit(*eap->arg)) /* get count */
10209 {
10210 n = getdigits(&eap->arg);
10211 eap->arg = skipwhite(eap->arg);
10212 }
10213 if (*eap->arg == '/') /* Match regexp, not just whole words */
10214 {
10215 whole = FALSE;
10216 ++eap->arg;
10217 p = skip_regexp(eap->arg, '/', p_magic, NULL);
10218 if (*p)
10219 {
10220 *p++ = NUL;
10221 p = skipwhite(p);
10222
10223 /* Check for trailing illegal characters */
10224 if (!ends_excmd(*p))
10225 eap->errmsg = e_trailing;
10226 else
10227 eap->nextcmd = check_nextcmd(p);
10228 }
10229 }
10230 if (!eap->skip)
10231 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
10232 whole, !eap->forceit,
10233 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
10234 n, action, eap->line1, eap->line2);
10235}
10236#endif
10237
10238#ifdef FEAT_WINDOWS
10239
10240# ifdef FEAT_QUICKFIX
10241/*
10242 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
10243 */
10244 static void
10245ex_ptag(eap)
10246 exarg_T *eap;
10247{
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +010010248 g_do_tagpreview = p_pvh; /* will be reset to 0 in ex_tag_cmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010249 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
10250}
10251
10252/*
10253 * ":pedit"
10254 */
10255 static void
10256ex_pedit(eap)
10257 exarg_T *eap;
10258{
10259 win_T *curwin_save = curwin;
10260
10261 g_do_tagpreview = p_pvh;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +000010262 prepare_tagpreview(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010263 keep_help_flag = curwin_save->w_buffer->b_help;
10264 do_exedit(eap, NULL);
10265 keep_help_flag = FALSE;
10266 if (curwin != curwin_save && win_valid(curwin_save))
10267 {
10268 /* Return cursor to where we were */
10269 validate_cursor();
10270 redraw_later(VALID);
10271 win_enter(curwin_save, TRUE);
10272 }
10273 g_do_tagpreview = 0;
10274}
10275# endif
10276
10277/*
10278 * ":stag", ":stselect" and ":stjump".
10279 */
10280 static void
10281ex_stag(eap)
10282 exarg_T *eap;
10283{
10284 postponed_split = -1;
10285 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +000010286 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010287 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
10288 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +000010289 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010290}
10291#endif
10292
10293/*
10294 * ":tag", ":tselect", ":tjump", ":tnext", etc.
10295 */
10296 static void
10297ex_tag(eap)
10298 exarg_T *eap;
10299{
10300 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
10301}
10302
10303 static void
10304ex_tag_cmd(eap, name)
10305 exarg_T *eap;
10306 char_u *name;
10307{
10308 int cmd;
10309
10310 switch (name[1])
10311 {
10312 case 'j': cmd = DT_JUMP; /* ":tjump" */
10313 break;
10314 case 's': cmd = DT_SELECT; /* ":tselect" */
10315 break;
10316 case 'p': cmd = DT_PREV; /* ":tprevious" */
10317 break;
10318 case 'N': cmd = DT_PREV; /* ":tNext" */
10319 break;
10320 case 'n': cmd = DT_NEXT; /* ":tnext" */
10321 break;
10322 case 'o': cmd = DT_POP; /* ":pop" */
10323 break;
10324 case 'f': /* ":tfirst" */
10325 case 'r': cmd = DT_FIRST; /* ":trewind" */
10326 break;
10327 case 'l': cmd = DT_LAST; /* ":tlast" */
10328 break;
10329 default: /* ":tag" */
10330#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +000010331 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010332 {
10333 do_cstag(eap);
10334 return;
10335 }
10336#endif
10337 cmd = DT_TAG;
10338 break;
10339 }
10340
Bram Moolenaarb8a7b562006-02-01 21:47:16 +000010341 if (name[0] == 'l')
10342 {
10343#ifndef FEAT_QUICKFIX
10344 ex_ni(eap);
10345 return;
10346#else
10347 cmd = DT_LTAG;
10348#endif
10349 }
10350
Bram Moolenaar071d4272004-06-13 20:20:40 +000010351 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
10352 eap->forceit, TRUE);
10353}
10354
10355/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010356 * Check "str" for starting with a special cmdline variable.
10357 * If found return one of the SPEC_ values and set "*usedlen" to the length of
10358 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
10359 */
10360 int
10361find_cmdline_var(src, usedlen)
10362 char_u *src;
10363 int *usedlen;
10364{
10365 int len;
10366 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +000010367 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010368 "%",
10369#define SPEC_PERC 0
10370 "#",
10371#define SPEC_HASH 1
10372 "<cword>", /* cursor word */
10373#define SPEC_CWORD 2
10374 "<cWORD>", /* cursor WORD */
10375#define SPEC_CCWORD 3
10376 "<cfile>", /* cursor path name */
10377#define SPEC_CFILE 4
10378 "<sfile>", /* ":so" file name */
10379#define SPEC_SFILE 5
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010380 "<slnum>", /* ":so" file line number */
10381#define SPEC_SLNUM 6
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010382#ifdef FEAT_AUTOCMD
10383 "<afile>", /* autocommand file name */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010384# define SPEC_AFILE 7
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010385 "<abuf>", /* autocommand buffer number */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010386# define SPEC_ABUF 8
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010387 "<amatch>", /* autocommand match name */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010388# define SPEC_AMATCH 9
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010389#endif
10390#ifdef FEAT_CLIENTSERVER
10391 "<client>"
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010392# ifdef FEAT_AUTOCMD
10393# define SPEC_CLIENT 10
10394# else
10395# define SPEC_CLIENT 7
10396# endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010397#endif
10398 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010399
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000010400 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010401 {
10402 len = (int)STRLEN(spec_str[i]);
10403 if (STRNCMP(src, spec_str[i], len) == 0)
10404 {
10405 *usedlen = len;
10406 return i;
10407 }
10408 }
10409 return -1;
10410}
10411
10412/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010413 * Evaluate cmdline variables.
10414 *
10415 * change '%' to curbuf->b_ffname
10416 * '#' to curwin->w_altfile
10417 * '<cword>' to word under the cursor
10418 * '<cWORD>' to WORD under the cursor
10419 * '<cfile>' to path name under the cursor
10420 * '<sfile>' to sourced file name
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010421 * '<slnum>' to sourced file line number
Bram Moolenaar071d4272004-06-13 20:20:40 +000010422 * '<afile>' to file name for autocommand
10423 * '<abuf>' to buffer number for autocommand
10424 * '<amatch>' to matching name for autocommand
10425 *
10426 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
10427 * "" for error without a message) and NULL is returned.
10428 * Returns an allocated string if a valid match was found.
10429 * Returns NULL if no match was found. "usedlen" then still contains the
10430 * number of characters to skip.
10431 */
10432 char_u *
Bram Moolenaar63b92542007-03-27 14:57:09 +000010433eval_vars(src, srcstart, usedlen, lnump, errormsg, escaped)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010434 char_u *src; /* pointer into commandline */
Bram Moolenaar63b92542007-03-27 14:57:09 +000010435 char_u *srcstart; /* beginning of valid memory for src */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010436 int *usedlen; /* characters after src that are used */
10437 linenr_T *lnump; /* line number for :e command, or NULL */
10438 char_u **errormsg; /* pointer to error message */
Bram Moolenaar63b92542007-03-27 14:57:09 +000010439 int *escaped; /* return value has escaped white space (can
10440 * be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010441{
10442 int i;
10443 char_u *s;
10444 char_u *result;
10445 char_u *resultbuf = NULL;
10446 int resultlen;
10447 buf_T *buf;
10448 int valid = VALID_HEAD + VALID_PATH; /* assume valid result */
10449 int spec_idx;
10450#ifdef FEAT_MODIFY_FNAME
10451 int skip_mod = FALSE;
10452#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010453 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010454
10455 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +000010456 if (escaped != NULL)
10457 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010458
10459 /*
10460 * Check if there is something to do.
10461 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010462 spec_idx = find_cmdline_var(src, usedlen);
10463 if (spec_idx < 0) /* no match */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010464 {
10465 *usedlen = 1;
10466 return NULL;
10467 }
10468
10469 /*
10470 * Skip when preceded with a backslash "\%" and "\#".
10471 * Note: In "\\%" the % is also not recognized!
10472 */
10473 if (src > srcstart && src[-1] == '\\')
10474 {
10475 *usedlen = 0;
Bram Moolenaara7241f52008-06-24 20:39:31 +000010476 STRMOVE(src - 1, src); /* remove backslash */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010477 return NULL;
10478 }
10479
10480 /*
10481 * word or WORD under cursor
10482 */
10483 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD)
10484 {
10485 resultlen = find_ident_under_cursor(&result, spec_idx == SPEC_CWORD ?
10486 (FIND_IDENT|FIND_STRING) : FIND_STRING);
10487 if (resultlen == 0)
10488 {
10489 *errormsg = (char_u *)"";
10490 return NULL;
10491 }
10492 }
10493
10494 /*
10495 * '#': Alternate file name
10496 * '%': Current file name
10497 * File name under the cursor
10498 * File name for autocommand
10499 * and following modifiers
10500 */
10501 else
10502 {
10503 switch (spec_idx)
10504 {
10505 case SPEC_PERC: /* '%': current file */
10506 if (curbuf->b_fname == NULL)
10507 {
10508 result = (char_u *)"";
10509 valid = 0; /* Must have ":p:h" to be valid */
10510 }
10511 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010512 result = curbuf->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010513 break;
10514
10515 case SPEC_HASH: /* '#' or "#99": alternate file */
10516 if (src[1] == '#') /* "##": the argument list */
10517 {
10518 result = arg_all();
10519 resultbuf = result;
10520 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +000010521 if (escaped != NULL)
10522 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010523#ifdef FEAT_MODIFY_FNAME
10524 skip_mod = TRUE;
10525#endif
10526 break;
10527 }
10528 s = src + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +000010529 if (*s == '<') /* "#<99" uses v:oldfiles */
10530 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010531 i = (int)getdigits(&s);
10532 *usedlen = (int)(s - src); /* length of what we expand */
10533
Bram Moolenaard812df62008-11-09 12:46:09 +000010534 if (src[1] == '<')
Bram Moolenaar071d4272004-06-13 20:20:40 +000010535 {
Bram Moolenaard812df62008-11-09 12:46:09 +000010536 if (*usedlen < 2)
10537 {
10538 /* Should we give an error message for #<text? */
10539 *usedlen = 1;
10540 return NULL;
10541 }
10542#ifdef FEAT_EVAL
10543 result = list_find_str(get_vim_var_list(VV_OLDFILES),
10544 (long)i);
10545 if (result == NULL)
10546 {
10547 *errormsg = (char_u *)"";
10548 return NULL;
10549 }
10550#else
10551 *errormsg = (char_u *)_("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +000010552 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +000010553#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010554 }
10555 else
Bram Moolenaard812df62008-11-09 12:46:09 +000010556 {
10557 buf = buflist_findnr(i);
10558 if (buf == NULL)
10559 {
10560 *errormsg = (char_u *)_("E194: No alternate file name to substitute for '#'");
10561 return NULL;
10562 }
10563 if (lnump != NULL)
10564 *lnump = ECMD_LAST;
10565 if (buf->b_fname == NULL)
10566 {
10567 result = (char_u *)"";
10568 valid = 0; /* Must have ":p:h" to be valid */
10569 }
10570 else
10571 result = buf->b_fname;
10572 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010573 break;
10574
10575#ifdef FEAT_SEARCHPATH
10576 case SPEC_CFILE: /* file name under cursor */
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010577 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010578 if (result == NULL)
10579 {
10580 *errormsg = (char_u *)"";
10581 return NULL;
10582 }
10583 resultbuf = result; /* remember allocated string */
10584 break;
10585#endif
10586
10587#ifdef FEAT_AUTOCMD
10588 case SPEC_AFILE: /* file name for autocommand */
10589 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +000010590 if (result != NULL && !autocmd_fname_full)
10591 {
10592 /* Still need to turn the fname into a full path. It is
10593 * postponed to avoid a delay when <afile> is not used. */
10594 autocmd_fname_full = TRUE;
10595 result = FullName_save(autocmd_fname, FALSE);
10596 vim_free(autocmd_fname);
10597 autocmd_fname = result;
10598 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010599 if (result == NULL)
10600 {
10601 *errormsg = (char_u *)_("E495: no autocommand file name to substitute for \"<afile>\"");
10602 return NULL;
10603 }
Bram Moolenaara0174af2008-01-02 20:08:25 +000010604 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010605 break;
10606
10607 case SPEC_ABUF: /* buffer number for autocommand */
10608 if (autocmd_bufnr <= 0)
10609 {
10610 *errormsg = (char_u *)_("E496: no autocommand buffer number to substitute for \"<abuf>\"");
10611 return NULL;
10612 }
10613 sprintf((char *)strbuf, "%d", autocmd_bufnr);
10614 result = strbuf;
10615 break;
10616
10617 case SPEC_AMATCH: /* match name for autocommand */
10618 result = autocmd_match;
10619 if (result == NULL)
10620 {
10621 *errormsg = (char_u *)_("E497: no autocommand match name to substitute for \"<amatch>\"");
10622 return NULL;
10623 }
10624 break;
10625
10626#endif
10627 case SPEC_SFILE: /* file name for ":so" command */
10628 result = sourcing_name;
10629 if (result == NULL)
10630 {
10631 *errormsg = (char_u *)_("E498: no :source file name to substitute for \"<sfile>\"");
10632 return NULL;
10633 }
10634 break;
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010635 case SPEC_SLNUM: /* line in file for ":so" command */
10636 if (sourcing_name == NULL || sourcing_lnum == 0)
10637 {
10638 *errormsg = (char_u *)_("E842: no line number to use for \"<slnum>\"");
10639 return NULL;
10640 }
10641 sprintf((char *)strbuf, "%ld", (long)sourcing_lnum);
10642 result = strbuf;
10643 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010644#if defined(FEAT_CLIENTSERVER)
10645 case SPEC_CLIENT: /* Source of last submitted input */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010646 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
10647 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010648 result = strbuf;
10649 break;
10650#endif
10651 }
10652
10653 resultlen = (int)STRLEN(result); /* length of new string */
10654 if (src[*usedlen] == '<') /* remove the file name extension */
10655 {
10656 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010657 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010658 resultlen = (int)(s - result);
10659 }
10660#ifdef FEAT_MODIFY_FNAME
10661 else if (!skip_mod)
10662 {
10663 valid |= modify_fname(src, usedlen, &result, &resultbuf,
10664 &resultlen);
10665 if (result == NULL)
10666 {
10667 *errormsg = (char_u *)"";
10668 return NULL;
10669 }
10670 }
10671#endif
10672 }
10673
10674 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
10675 {
10676 if (valid != VALID_HEAD + VALID_PATH)
10677 /* xgettext:no-c-format */
10678 *errormsg = (char_u *)_("E499: Empty file name for '%' or '#', only works with \":p:h\"");
10679 else
10680 *errormsg = (char_u *)_("E500: Evaluates to an empty string");
10681 result = NULL;
10682 }
10683 else
10684 result = vim_strnsave(result, resultlen);
10685 vim_free(resultbuf);
10686 return result;
10687}
10688
10689/*
10690 * Concatenate all files in the argument list, separated by spaces, and return
10691 * it in one allocated string.
10692 * Spaces and backslashes in the file names are escaped with a backslash.
10693 * Returns NULL when out of memory.
10694 */
10695 static char_u *
10696arg_all()
10697{
10698 int len;
10699 int idx;
10700 char_u *retval = NULL;
10701 char_u *p;
10702
10703 /*
10704 * Do this loop two times:
10705 * first time: compute the total length
10706 * second time: concatenate the names
10707 */
10708 for (;;)
10709 {
10710 len = 0;
10711 for (idx = 0; idx < ARGCOUNT; ++idx)
10712 {
10713 p = alist_name(&ARGLIST[idx]);
10714 if (p != NULL)
10715 {
10716 if (len > 0)
10717 {
10718 /* insert a space in between names */
10719 if (retval != NULL)
10720 retval[len] = ' ';
10721 ++len;
10722 }
10723 for ( ; *p != NUL; ++p)
10724 {
10725 if (*p == ' ' || *p == '\\')
10726 {
10727 /* insert a backslash */
10728 if (retval != NULL)
10729 retval[len] = '\\';
10730 ++len;
10731 }
10732 if (retval != NULL)
10733 retval[len] = *p;
10734 ++len;
10735 }
10736 }
10737 }
10738
10739 /* second time: break here */
10740 if (retval != NULL)
10741 {
10742 retval[len] = NUL;
10743 break;
10744 }
10745
10746 /* allocate memory */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000010747 retval = alloc((unsigned)len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010748 if (retval == NULL)
10749 break;
10750 }
10751
10752 return retval;
10753}
10754
10755#if defined(FEAT_AUTOCMD) || defined(PROTO)
10756/*
10757 * Expand the <sfile> string in "arg".
10758 *
10759 * Returns an allocated string, or NULL for any error.
10760 */
10761 char_u *
10762expand_sfile(arg)
10763 char_u *arg;
10764{
10765 char_u *errormsg;
10766 int len;
10767 char_u *result;
10768 char_u *newres;
10769 char_u *repl;
10770 int srclen;
10771 char_u *p;
10772
10773 result = vim_strsave(arg);
10774 if (result == NULL)
10775 return NULL;
10776
10777 for (p = result; *p; )
10778 {
10779 if (STRNCMP(p, "<sfile>", 7) != 0)
10780 ++p;
10781 else
10782 {
10783 /* replace "<sfile>" with the sourced file name, and do ":" stuff */
Bram Moolenaar63b92542007-03-27 14:57:09 +000010784 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010785 if (errormsg != NULL)
10786 {
10787 if (*errormsg)
10788 emsg(errormsg);
10789 vim_free(result);
10790 return NULL;
10791 }
10792 if (repl == NULL) /* no match (cannot happen) */
10793 {
10794 p += srclen;
10795 continue;
10796 }
10797 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
10798 newres = alloc(len);
10799 if (newres == NULL)
10800 {
10801 vim_free(repl);
10802 vim_free(result);
10803 return NULL;
10804 }
10805 mch_memmove(newres, result, (size_t)(p - result));
10806 STRCPY(newres + (p - result), repl);
10807 len = (int)STRLEN(newres);
10808 STRCAT(newres, p + srclen);
10809 vim_free(repl);
10810 vim_free(result);
10811 result = newres;
10812 p = newres + len; /* continue after the match */
10813 }
10814 }
10815
10816 return result;
10817}
10818#endif
10819
10820#ifdef FEAT_SESSION
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010821static int ses_winsizes __ARGS((FILE *fd, int restore_size,
10822 win_T *tab_firstwin));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010823static int ses_win_rec __ARGS((FILE *fd, frame_T *fr));
10824static frame_T *ses_skipframe __ARGS((frame_T *fr));
10825static int ses_do_frame __ARGS((frame_T *fr));
10826static int ses_do_win __ARGS((win_T *wp));
10827static int ses_arglist __ARGS((FILE *fd, char *cmd, garray_T *gap, int fullname, unsigned *flagp));
10828static int ses_put_fname __ARGS((FILE *fd, char_u *name, unsigned *flagp));
10829static int ses_fname __ARGS((FILE *fd, buf_T *buf, unsigned *flagp));
10830
10831/*
10832 * Write openfile commands for the current buffers to an .exrc file.
10833 * Return FAIL on error, OK otherwise.
10834 */
10835 static int
10836makeopens(fd, dirnow)
10837 FILE *fd;
10838 char_u *dirnow; /* Current directory name */
10839{
10840 buf_T *buf;
10841 int only_save_windows = TRUE;
10842 int nr;
10843 int cnr = 1;
10844 int restore_size = TRUE;
10845 win_T *wp;
10846 char_u *sname;
10847 win_T *edited_win = NULL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010848 int tabnr;
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020010849 int restore_stal = FALSE;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010850 win_T *tab_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010851 frame_T *tab_topframe;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010852 int cur_arg_idx = 0;
Bram Moolenaar383c6f52008-01-04 15:01:07 +000010853 int next_arg_idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010854
10855 if (ssop_flags & SSOP_BUFFERS)
10856 only_save_windows = FALSE; /* Save ALL buffers */
10857
10858 /*
10859 * Begin by setting the this_session variable, and then other
10860 * sessionable variables.
10861 */
10862#ifdef FEAT_EVAL
10863 if (put_line(fd, "let v:this_session=expand(\"<sfile>:p\")") == FAIL)
10864 return FAIL;
10865 if (ssop_flags & SSOP_GLOBALS)
10866 if (store_session_globals(fd) == FAIL)
10867 return FAIL;
10868#endif
10869
10870 /*
10871 * Close all windows but one.
10872 */
10873 if (put_line(fd, "silent only") == FAIL)
10874 return FAIL;
10875
10876 /*
10877 * Now a :cd command to the session directory or the current directory
10878 */
10879 if (ssop_flags & SSOP_SESDIR)
10880 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010881 if (put_line(fd, "exe \"cd \" . escape(expand(\"<sfile>:p:h\"), ' ')")
10882 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010883 return FAIL;
10884 }
10885 else if (ssop_flags & SSOP_CURDIR)
10886 {
10887 sname = home_replace_save(NULL, globaldir != NULL ? globaldir : dirnow);
10888 if (sname == NULL
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010889 || fputs("cd ", fd) < 0
10890 || ses_put_fname(fd, sname, &ssop_flags) == FAIL
10891 || put_eol(fd) == FAIL)
10892 {
10893 vim_free(sname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010894 return FAIL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010895 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010896 vim_free(sname);
10897 }
10898
10899 /*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010900 * If there is an empty, unnamed buffer we will wipe it out later.
10901 * Remember the buffer number.
10902 */
10903 if (put_line(fd, "if expand('%') == '' && !&modified && line('$') <= 1 && getline(1) == ''") == FAIL)
10904 return FAIL;
10905 if (put_line(fd, " let s:wipebuf = bufnr('%')") == FAIL)
10906 return FAIL;
10907 if (put_line(fd, "endif") == FAIL)
10908 return FAIL;
10909
10910 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010911 * Now save the current files, current buffer first.
10912 */
10913 if (put_line(fd, "set shortmess=aoO") == FAIL)
10914 return FAIL;
10915
10916 /* Now put the other buffers into the buffer list */
10917 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
10918 {
10919 if (!(only_save_windows && buf->b_nwindows == 0)
10920 && !(buf->b_help && !(ssop_flags & SSOP_HELP))
10921 && buf->b_fname != NULL
10922 && buf->b_p_bl)
10923 {
10924 if (fprintf(fd, "badd +%ld ", buf->b_wininfo == NULL ? 1L
10925 : buf->b_wininfo->wi_fpos.lnum) < 0
10926 || ses_fname(fd, buf, &ssop_flags) == FAIL)
10927 return FAIL;
10928 }
10929 }
10930
10931 /* the global argument list */
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010010932 if (ses_arglist(fd, "argglobal", &global_alist.al_ga,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010933 !(ssop_flags & SSOP_CURDIR), &ssop_flags) == FAIL)
10934 return FAIL;
10935
10936 if (ssop_flags & SSOP_RESIZE)
10937 {
10938 /* Note: after the restore we still check it worked!*/
10939 if (fprintf(fd, "set lines=%ld columns=%ld" , Rows, Columns) < 0
10940 || put_eol(fd) == FAIL)
10941 return FAIL;
10942 }
10943
10944#ifdef FEAT_GUI
10945 if (gui.in_use && (ssop_flags & SSOP_WINPOS))
10946 {
10947 int x, y;
10948
10949 if (gui_mch_get_winpos(&x, &y) == OK)
10950 {
10951 /* Note: after the restore we still check it worked!*/
10952 if (fprintf(fd, "winpos %d %d", x, y) < 0 || put_eol(fd) == FAIL)
10953 return FAIL;
10954 }
10955 }
10956#endif
10957
10958 /*
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020010959 * When there are two or more tabpages and 'showtabline' is 1 the tabline
10960 * will be displayed when creating the next tab. That resizes the windows
10961 * in the first tab, which may cause problems. Set 'showtabline' to 2
10962 * temporarily to avoid that.
10963 */
10964 if (p_stal == 1 && first_tabpage->tp_next != NULL)
10965 {
10966 if (put_line(fd, "set stal=2") == FAIL)
10967 return FAIL;
10968 restore_stal = TRUE;
10969 }
10970
10971 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000010972 * May repeat putting Windows for each tab, when "tabpages" is in
10973 * 'sessionoptions'.
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010974 * Don't use goto_tabpage(), it may change directory and trigger
10975 * autocommands.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010976 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010977 tab_firstwin = firstwin; /* first window in tab page "tabnr" */
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010978 tab_topframe = topframe;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010979 for (tabnr = 1; ; ++tabnr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010980 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010981 int need_tabnew = FALSE;
10982
Bram Moolenaar18144c82006-04-12 21:52:12 +000010983 if ((ssop_flags & SSOP_TABPAGES))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010984 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010985 tabpage_T *tp = find_tabpage(tabnr);
10986
10987 if (tp == NULL)
10988 break; /* done all tab pages */
10989 if (tp == curtab)
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010990 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010991 tab_firstwin = firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010992 tab_topframe = topframe;
10993 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010994 else
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010995 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010996 tab_firstwin = tp->tp_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010997 tab_topframe = tp->tp_topframe;
10998 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010999 if (tabnr > 1)
11000 need_tabnew = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011001 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011002
Bram Moolenaar18144c82006-04-12 21:52:12 +000011003 /*
11004 * Before creating the window layout, try loading one file. If this
11005 * is aborted we don't end up with a number of useless windows.
11006 * This may have side effects! (e.g., compressed or network file).
11007 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011008 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011009 {
11010 if (ses_do_win(wp)
11011 && wp->w_buffer->b_ffname != NULL
11012 && !wp->w_buffer->b_help
11013#ifdef FEAT_QUICKFIX
11014 && !bt_nofile(wp->w_buffer)
11015#endif
11016 )
11017 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011018 if (fputs(need_tabnew ? "tabedit " : "edit ", fd) < 0
Bram Moolenaar18144c82006-04-12 21:52:12 +000011019 || ses_fname(fd, wp->w_buffer, &ssop_flags) == FAIL)
11020 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011021 need_tabnew = FALSE;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011022 if (!wp->w_arg_idx_invalid)
11023 edited_win = wp;
11024 break;
11025 }
11026 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011027
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011028 /* If no file got edited create an empty tab page. */
11029 if (need_tabnew && put_line(fd, "tabnew") == FAIL)
11030 return FAIL;
11031
Bram Moolenaar18144c82006-04-12 21:52:12 +000011032 /*
11033 * Save current window layout.
11034 */
11035 if (put_line(fd, "set splitbelow splitright") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011036 return FAIL;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011037 if (ses_win_rec(fd, tab_topframe) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011038 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011039 if (!p_sb && put_line(fd, "set nosplitbelow") == FAIL)
11040 return FAIL;
11041 if (!p_spr && put_line(fd, "set nosplitright") == FAIL)
11042 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011043
Bram Moolenaar18144c82006-04-12 21:52:12 +000011044 /*
11045 * Check if window sizes can be restored (no windows omitted).
11046 * Remember the window number of the current window after restoring.
11047 */
11048 nr = 0;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011049 for (wp = tab_firstwin; wp != NULL; wp = W_NEXT(wp))
Bram Moolenaar18144c82006-04-12 21:52:12 +000011050 {
11051 if (ses_do_win(wp))
11052 ++nr;
11053 else
11054 restore_size = FALSE;
11055 if (curwin == wp)
11056 cnr = nr;
11057 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011058
Bram Moolenaar18144c82006-04-12 21:52:12 +000011059 /* Go to the first window. */
11060 if (put_line(fd, "wincmd t") == FAIL)
11061 return FAIL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011062
Bram Moolenaar18144c82006-04-12 21:52:12 +000011063 /*
11064 * If more than one window, see if sizes can be restored.
11065 * First set 'winheight' and 'winwidth' to 1 to avoid the windows being
11066 * resized when moving between windows.
11067 * Do this before restoring the view, so that the topline and the
11068 * cursor can be set. This is done again below.
11069 */
11070 if (put_line(fd, "set winheight=1 winwidth=1") == FAIL)
11071 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011072 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011073 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011074
Bram Moolenaar18144c82006-04-12 21:52:12 +000011075 /*
11076 * Restore the view of the window (options, file, cursor, etc.).
11077 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011078 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011079 {
11080 if (!ses_do_win(wp))
11081 continue;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011082 if (put_view(fd, wp, wp != edited_win, &ssop_flags,
11083 cur_arg_idx) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011084 return FAIL;
11085 if (nr > 1 && put_line(fd, "wincmd w") == FAIL)
11086 return FAIL;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011087 next_arg_idx = wp->w_arg_idx;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011088 }
11089
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011090 /* The argument index in the first tab page is zero, need to set it in
11091 * each window. For further tab pages it's the window where we do
11092 * "tabedit". */
11093 cur_arg_idx = next_arg_idx;
11094
Bram Moolenaar18144c82006-04-12 21:52:12 +000011095 /*
11096 * Restore cursor to the current window if it's not the first one.
11097 */
11098 if (cnr > 1 && (fprintf(fd, "%dwincmd w", cnr) < 0
11099 || put_eol(fd) == FAIL))
11100 return FAIL;
11101
11102 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000011103 * Restore window sizes again after jumping around in windows, because
11104 * the current window has a minimum size while others may not.
11105 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011106 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011107 return FAIL;
11108
Bram Moolenaar18144c82006-04-12 21:52:12 +000011109 /* Don't continue in another tab page when doing only the current one
11110 * or when at the last tab page. */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011111 if (!(ssop_flags & SSOP_TABPAGES))
Bram Moolenaar18144c82006-04-12 21:52:12 +000011112 break;
11113 }
11114
11115 if (ssop_flags & SSOP_TABPAGES)
11116 {
Bram Moolenaar18144c82006-04-12 21:52:12 +000011117 if (fprintf(fd, "tabnext %d", tabpage_index(curtab)) < 0
11118 || put_eol(fd) == FAIL)
11119 return FAIL;
11120 }
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020011121 if (restore_stal && put_line(fd, "set stal=1") == FAIL)
11122 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011123
Bram Moolenaar9c102382006-05-03 21:26:49 +000011124 /*
11125 * Wipe out an empty unnamed buffer we started in.
11126 */
11127 if (put_line(fd, "if exists('s:wipebuf')") == FAIL)
11128 return FAIL;
Bram Moolenaarf3a67882006-05-05 21:09:41 +000011129 if (put_line(fd, " silent exe 'bwipe ' . s:wipebuf") == FAIL)
Bram Moolenaar9c102382006-05-03 21:26:49 +000011130 return FAIL;
11131 if (put_line(fd, "endif") == FAIL)
11132 return FAIL;
11133 if (put_line(fd, "unlet! s:wipebuf") == FAIL)
11134 return FAIL;
11135
11136 /* Re-apply 'winheight', 'winwidth' and 'shortmess'. */
11137 if (fprintf(fd, "set winheight=%ld winwidth=%ld shortmess=%s",
11138 p_wh, p_wiw, p_shm) < 0 || put_eol(fd) == FAIL)
11139 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011140
11141 /*
11142 * Lastly, execute the x.vim file if it exists.
11143 */
11144 if (put_line(fd, "let s:sx = expand(\"<sfile>:p:r\").\"x.vim\"") == FAIL
11145 || put_line(fd, "if file_readable(s:sx)") == FAIL
Bram Moolenaar42ba1262008-12-09 10:18:03 +000011146 || put_line(fd, " exe \"source \" . fnameescape(s:sx)") == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000011147 || put_line(fd, "endif") == FAIL)
11148 return FAIL;
11149
11150 return OK;
11151}
11152
11153 static int
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011154ses_winsizes(fd, restore_size, tab_firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011155 FILE *fd;
11156 int restore_size;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011157 win_T *tab_firstwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011158{
11159 int n = 0;
11160 win_T *wp;
11161
11162 if (restore_size && (ssop_flags & SSOP_WINSIZE))
11163 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011164 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011165 {
11166 if (!ses_do_win(wp))
11167 continue;
11168 ++n;
11169
11170 /* restore height when not full height */
11171 if (wp->w_height + wp->w_status_height < topframe->fr_height
11172 && (fprintf(fd,
11173 "exe '%dresize ' . ((&lines * %ld + %ld) / %ld)",
11174 n, (long)wp->w_height, Rows / 2, Rows) < 0
11175 || put_eol(fd) == FAIL))
11176 return FAIL;
11177
11178 /* restore width when not full width */
11179 if (wp->w_width < Columns && (fprintf(fd,
11180 "exe 'vert %dresize ' . ((&columns * %ld + %ld) / %ld)",
11181 n, (long)wp->w_width, Columns / 2, Columns) < 0
11182 || put_eol(fd) == FAIL))
11183 return FAIL;
11184 }
11185 }
11186 else
11187 {
11188 /* Just equalise window sizes */
11189 if (put_line(fd, "wincmd =") == FAIL)
11190 return FAIL;
11191 }
11192 return OK;
11193}
11194
11195/*
11196 * Write commands to "fd" to recursively create windows for frame "fr",
11197 * horizontally and vertically split.
11198 * After the commands the last window in the frame is the current window.
11199 * Returns FAIL when writing the commands to "fd" fails.
11200 */
11201 static int
11202ses_win_rec(fd, fr)
11203 FILE *fd;
11204 frame_T *fr;
11205{
11206 frame_T *frc;
11207 int count = 0;
11208
11209 if (fr->fr_layout != FR_LEAF)
11210 {
11211 /* Find first frame that's not skipped and then create a window for
11212 * each following one (first frame is already there). */
11213 frc = ses_skipframe(fr->fr_child);
11214 if (frc != NULL)
11215 while ((frc = ses_skipframe(frc->fr_next)) != NULL)
11216 {
11217 /* Make window as big as possible so that we have lots of room
11218 * to split. */
11219 if (put_line(fd, "wincmd _ | wincmd |") == FAIL
11220 || put_line(fd, fr->fr_layout == FR_COL
11221 ? "split" : "vsplit") == FAIL)
11222 return FAIL;
11223 ++count;
11224 }
11225
11226 /* Go back to the first window. */
11227 if (count > 0 && (fprintf(fd, fr->fr_layout == FR_COL
11228 ? "%dwincmd k" : "%dwincmd h", count) < 0
11229 || put_eol(fd) == FAIL))
11230 return FAIL;
11231
11232 /* Recursively create frames/windows in each window of this column or
11233 * row. */
11234 frc = ses_skipframe(fr->fr_child);
11235 while (frc != NULL)
11236 {
11237 ses_win_rec(fd, frc);
11238 frc = ses_skipframe(frc->fr_next);
11239 /* Go to next window. */
11240 if (frc != NULL && put_line(fd, "wincmd w") == FAIL)
11241 return FAIL;
11242 }
11243 }
11244 return OK;
11245}
11246
11247/*
11248 * Skip frames that don't contain windows we want to save in the Session.
11249 * Returns NULL when there none.
11250 */
11251 static frame_T *
11252ses_skipframe(fr)
11253 frame_T *fr;
11254{
11255 frame_T *frc;
11256
11257 for (frc = fr; frc != NULL; frc = frc->fr_next)
11258 if (ses_do_frame(frc))
11259 break;
11260 return frc;
11261}
11262
11263/*
11264 * Return TRUE if frame "fr" has a window somewhere that we want to save in
11265 * the Session.
11266 */
11267 static int
11268ses_do_frame(fr)
11269 frame_T *fr;
11270{
11271 frame_T *frc;
11272
11273 if (fr->fr_layout == FR_LEAF)
11274 return ses_do_win(fr->fr_win);
11275 for (frc = fr->fr_child; frc != NULL; frc = frc->fr_next)
11276 if (ses_do_frame(frc))
11277 return TRUE;
11278 return FALSE;
11279}
11280
11281/*
11282 * Return non-zero if window "wp" is to be stored in the Session.
11283 */
11284 static int
11285ses_do_win(wp)
11286 win_T *wp;
11287{
11288 if (wp->w_buffer->b_fname == NULL
11289#ifdef FEAT_QUICKFIX
11290 /* When 'buftype' is "nofile" can't restore the window contents. */
11291 || bt_nofile(wp->w_buffer)
11292#endif
11293 )
11294 return (ssop_flags & SSOP_BLANK);
11295 if (wp->w_buffer->b_help)
11296 return (ssop_flags & SSOP_HELP);
11297 return TRUE;
11298}
11299
11300/*
11301 * Write commands to "fd" to restore the view of a window.
11302 * Caller must make sure 'scrolloff' is zero.
11303 */
11304 static int
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011305put_view(fd, wp, add_edit, flagp, current_arg_idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011306 FILE *fd;
11307 win_T *wp;
11308 int add_edit; /* add ":edit" command to view */
11309 unsigned *flagp; /* vop_flags or ssop_flags */
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011310 int current_arg_idx; /* current argument index of the window, use
11311 * -1 if unknown */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011312{
11313 win_T *save_curwin;
11314 int f;
11315 int do_cursor;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011316 int did_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011317
11318 /* Always restore cursor position for ":mksession". For ":mkview" only
11319 * when 'viewoptions' contains "cursor". */
11320 do_cursor = (flagp == &ssop_flags || *flagp & SSOP_CURSOR);
11321
11322 /*
11323 * Local argument list.
11324 */
11325 if (wp->w_alist == &global_alist)
11326 {
11327 if (put_line(fd, "argglobal") == FAIL)
11328 return FAIL;
11329 }
11330 else
11331 {
11332 if (ses_arglist(fd, "arglocal", &wp->w_alist->al_ga,
11333 flagp == &vop_flags
11334 || !(*flagp & SSOP_CURDIR)
11335 || wp->w_localdir != NULL, flagp) == FAIL)
11336 return FAIL;
11337 }
11338
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011339 /* Only when part of a session: restore the argument index. Some
11340 * arguments may have been deleted, check if the index is valid. */
Bram Moolenaar51f53df2010-06-26 05:25:54 +020011341 if (wp->w_arg_idx != current_arg_idx && wp->w_arg_idx < WARGCOUNT(wp)
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011342 && flagp == &ssop_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011343 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011344 if (fprintf(fd, "%ldargu", (long)wp->w_arg_idx + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011345 || put_eol(fd) == FAIL)
11346 return FAIL;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011347 did_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011348 }
11349
11350 /* Edit the file. Skip this when ":next" already did it. */
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011351 if (add_edit && (!did_next || wp->w_arg_idx_invalid))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011352 {
11353 /*
11354 * Load the file.
11355 */
11356 if (wp->w_buffer->b_ffname != NULL
11357#ifdef FEAT_QUICKFIX
11358 && !bt_nofile(wp->w_buffer)
11359#endif
11360 )
11361 {
11362 /*
11363 * Editing a file in this buffer: use ":edit file".
11364 * This may have side effects! (e.g., compressed or network file).
11365 */
11366 if (fputs("edit ", fd) < 0
11367 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
11368 return FAIL;
11369 }
11370 else
11371 {
11372 /* No file in this buffer, just make it empty. */
11373 if (put_line(fd, "enew") == FAIL)
11374 return FAIL;
11375#ifdef FEAT_QUICKFIX
11376 if (wp->w_buffer->b_ffname != NULL)
11377 {
11378 /* The buffer does have a name, but it's not a file name. */
11379 if (fputs("file ", fd) < 0
11380 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
11381 return FAIL;
11382 }
11383#endif
11384 do_cursor = FALSE;
11385 }
11386 }
11387
11388 /*
11389 * Local mappings and abbreviations.
11390 */
11391 if ((*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
11392 && makemap(fd, wp->w_buffer) == FAIL)
11393 return FAIL;
11394
11395 /*
11396 * Local options. Need to go to the window temporarily.
11397 * Store only local values when using ":mkview" and when ":mksession" is
11398 * used and 'sessionoptions' doesn't include "options".
11399 * Some folding options are always stored when "folds" is included,
11400 * otherwise the folds would not be restored correctly.
11401 */
11402 save_curwin = curwin;
11403 curwin = wp;
11404 curbuf = curwin->w_buffer;
11405 if (*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
11406 f = makeset(fd, OPT_LOCAL,
11407 flagp == &vop_flags || !(*flagp & SSOP_OPTIONS));
11408#ifdef FEAT_FOLDING
11409 else if (*flagp & SSOP_FOLDS)
11410 f = makefoldset(fd);
11411#endif
11412 else
11413 f = OK;
11414 curwin = save_curwin;
11415 curbuf = curwin->w_buffer;
11416 if (f == FAIL)
11417 return FAIL;
11418
11419#ifdef FEAT_FOLDING
11420 /*
11421 * Save Folds when 'buftype' is empty and for help files.
11422 */
11423 if ((*flagp & SSOP_FOLDS)
11424 && wp->w_buffer->b_ffname != NULL
Bram Moolenaarb1b715d2006-01-21 22:09:43 +000011425# ifdef FEAT_QUICKFIX
11426 && (*wp->w_buffer->b_p_bt == NUL || wp->w_buffer->b_help)
11427# endif
11428 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000011429 {
11430 if (put_folds(fd, wp) == FAIL)
11431 return FAIL;
11432 }
11433#endif
11434
11435 /*
11436 * Set the cursor after creating folds, since that moves the cursor.
11437 */
11438 if (do_cursor)
11439 {
11440
11441 /* Restore the cursor line in the file and relatively in the
11442 * window. Don't use "G", it changes the jumplist. */
11443 if (fprintf(fd, "let s:l = %ld - ((%ld * winheight(0) + %ld) / %ld)",
11444 (long)wp->w_cursor.lnum,
11445 (long)(wp->w_cursor.lnum - wp->w_topline),
11446 (long)wp->w_height / 2, (long)wp->w_height) < 0
11447 || put_eol(fd) == FAIL
11448 || put_line(fd, "if s:l < 1 | let s:l = 1 | endif") == FAIL
11449 || put_line(fd, "exe s:l") == FAIL
11450 || put_line(fd, "normal! zt") == FAIL
11451 || fprintf(fd, "%ld", (long)wp->w_cursor.lnum) < 0
11452 || put_eol(fd) == FAIL)
11453 return FAIL;
11454 /* Restore the cursor column and left offset when not wrapping. */
11455 if (wp->w_cursor.col == 0)
11456 {
11457 if (put_line(fd, "normal! 0") == FAIL)
11458 return FAIL;
11459 }
11460 else
11461 {
11462 if (!wp->w_p_wrap && wp->w_leftcol > 0 && wp->w_width > 0)
11463 {
11464 if (fprintf(fd,
11465 "let s:c = %ld - ((%ld * winwidth(0) + %ld) / %ld)",
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011466 (long)wp->w_virtcol + 1,
11467 (long)(wp->w_virtcol - wp->w_leftcol),
Bram Moolenaar071d4272004-06-13 20:20:40 +000011468 (long)wp->w_width / 2, (long)wp->w_width) < 0
11469 || put_eol(fd) == FAIL
11470 || put_line(fd, "if s:c > 0") == FAIL
11471 || fprintf(fd,
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011472 " exe 'normal! ' . s:c . '|zs' . %ld . '|'",
11473 (long)wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011474 || put_eol(fd) == FAIL
11475 || put_line(fd, "else") == FAIL
Bram Moolenaarfdf447b2013-02-26 17:21:29 +010011476 || fprintf(fd, " normal! 0%d|", wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011477 || put_eol(fd) == FAIL
11478 || put_line(fd, "endif") == FAIL)
11479 return FAIL;
11480 }
11481 else
11482 {
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011483 if (fprintf(fd, "normal! 0%d|", wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011484 || put_eol(fd) == FAIL)
11485 return FAIL;
11486 }
11487 }
11488 }
11489
11490 /*
11491 * Local directory.
11492 */
11493 if (wp->w_localdir != NULL)
11494 {
11495 if (fputs("lcd ", fd) < 0
11496 || ses_put_fname(fd, wp->w_localdir, flagp) == FAIL
11497 || put_eol(fd) == FAIL)
11498 return FAIL;
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011499 did_lcd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011500 }
11501
11502 return OK;
11503}
11504
11505/*
11506 * Write an argument list to the session file.
11507 * Returns FAIL if writing fails.
11508 */
11509 static int
11510ses_arglist(fd, cmd, gap, fullname, flagp)
11511 FILE *fd;
11512 char *cmd;
11513 garray_T *gap;
11514 int fullname; /* TRUE: use full path name */
11515 unsigned *flagp;
11516{
11517 int i;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011518 char_u *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011519 char_u *s;
11520
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011521 if (fputs(cmd, fd) < 0 || put_eol(fd) == FAIL)
11522 return FAIL;
11523 if (put_line(fd, "silent! argdel *") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011524 return FAIL;
11525 for (i = 0; i < gap->ga_len; ++i)
11526 {
11527 /* NULL file names are skipped (only happens when out of memory). */
11528 s = alist_name(&((aentry_T *)gap->ga_data)[i]);
11529 if (s != NULL)
11530 {
11531 if (fullname)
11532 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020011533 buf = alloc(MAXPATHL);
11534 if (buf != NULL)
11535 {
11536 (void)vim_FullName(s, buf, MAXPATHL, FALSE);
11537 s = buf;
11538 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011539 }
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011540 if (fputs("argadd ", fd) < 0
11541 || ses_put_fname(fd, s, flagp) == FAIL
11542 || put_eol(fd) == FAIL)
Bram Moolenaard9462e32011-04-11 21:35:11 +020011543 {
11544 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011545 return FAIL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011546 }
11547 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011548 }
11549 }
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011550 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011551}
11552
11553/*
11554 * Write a buffer name to the session file.
11555 * Also ends the line.
11556 * Returns FAIL if writing fails.
11557 */
11558 static int
11559ses_fname(fd, buf, flagp)
11560 FILE *fd;
11561 buf_T *buf;
11562 unsigned *flagp;
11563{
11564 char_u *name;
11565
11566 /* Use the short file name if the current directory is known at the time
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011567 * the session file will be sourced.
11568 * Don't do this for ":mkview", we don't know the current directory.
11569 * Don't do this after ":lcd", we don't keep track of what the current
11570 * directory is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011571 if (buf->b_sfname != NULL
11572 && flagp == &ssop_flags
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011573 && (ssop_flags & (SSOP_CURDIR | SSOP_SESDIR))
Bram Moolenaar8d594672009-07-01 18:18:57 +000011574#ifdef FEAT_AUTOCHDIR
11575 && !p_acd
11576#endif
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011577 && !did_lcd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011578 name = buf->b_sfname;
11579 else
11580 name = buf->b_ffname;
11581 if (ses_put_fname(fd, name, flagp) == FAIL || put_eol(fd) == FAIL)
11582 return FAIL;
11583 return OK;
11584}
11585
11586/*
11587 * Write a file name to the session file.
11588 * Takes care of the "slash" option in 'sessionoptions' and escapes special
11589 * characters.
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011590 * Returns FAIL if writing fails or out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011591 */
11592 static int
11593ses_put_fname(fd, name, flagp)
11594 FILE *fd;
11595 char_u *name;
11596 unsigned *flagp;
11597{
11598 char_u *sname;
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011599 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011600 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011601
11602 sname = home_replace_save(NULL, name);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011603 if (sname == NULL)
11604 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011605
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011606 if (*flagp & SSOP_SLASH)
11607 {
11608 /* change all backslashes to forward slashes */
11609 for (p = sname; *p != NUL; mb_ptr_adv(p))
11610 if (*p == '\\')
11611 *p = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +000011612 }
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011613
Bram Moolenaar84a05ac2013-05-06 04:24:17 +020011614 /* escape special characters */
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011615 p = vim_strsave_fnameescape(sname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011616 vim_free(sname);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011617 if (p == NULL)
11618 return FAIL;
11619
11620 /* write the result */
11621 if (fputs((char *)p, fd) < 0)
11622 retval = FAIL;
11623
11624 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011625 return retval;
11626}
11627
11628/*
11629 * ":loadview [nr]"
11630 */
11631 static void
11632ex_loadview(eap)
11633 exarg_T *eap;
11634{
11635 char_u *fname;
11636
11637 fname = get_view_file(*eap->arg);
11638 if (fname != NULL)
11639 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +000011640 do_source(fname, FALSE, DOSO_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011641 vim_free(fname);
11642 }
11643}
11644
11645/*
11646 * Get the name of the view file for the current buffer.
11647 */
11648 static char_u *
11649get_view_file(c)
11650 int c;
11651{
11652 int len = 0;
11653 char_u *p, *s;
11654 char_u *retval;
11655 char_u *sname;
11656
11657 if (curbuf->b_ffname == NULL)
11658 {
11659 EMSG(_(e_noname));
11660 return NULL;
11661 }
11662 sname = home_replace_save(NULL, curbuf->b_ffname);
11663 if (sname == NULL)
11664 return NULL;
11665
11666 /*
11667 * We want a file name without separators, because we're not going to make
11668 * a directory.
11669 * "normal" path separator -> "=+"
11670 * "=" -> "=="
11671 * ":" path separator -> "=-"
11672 */
11673 for (p = sname; *p; ++p)
11674 if (*p == '=' || vim_ispathsep(*p))
11675 ++len;
11676 retval = alloc((unsigned)(STRLEN(sname) + len + STRLEN(p_vdir) + 9));
11677 if (retval != NULL)
11678 {
11679 STRCPY(retval, p_vdir);
11680 add_pathsep(retval);
11681 s = retval + STRLEN(retval);
11682 for (p = sname; *p; ++p)
11683 {
11684 if (*p == '=')
11685 {
11686 *s++ = '=';
11687 *s++ = '=';
11688 }
11689 else if (vim_ispathsep(*p))
11690 {
11691 *s++ = '=';
Bram Moolenaare60acc12011-05-10 16:41:25 +020011692#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011693 if (*p == ':')
11694 *s++ = '-';
11695 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000011696#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000011697 *s++ = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +000011698 }
11699 else
11700 *s++ = *p;
11701 }
11702 *s++ = '=';
11703 *s++ = c;
11704 STRCPY(s, ".vim");
11705 }
11706
11707 vim_free(sname);
11708 return retval;
11709}
11710
11711#endif /* FEAT_SESSION */
11712
11713/*
11714 * Write end-of-line character(s) for ":mkexrc", ":mkvimrc" and ":mksession".
11715 * Return FAIL for a write error.
11716 */
11717 int
11718put_eol(fd)
11719 FILE *fd;
11720{
11721 if (
11722#ifdef USE_CRNL
11723 (
11724# ifdef MKSESSION_NL
11725 !mksession_nl &&
11726# endif
11727 (putc('\r', fd) < 0)) ||
11728#endif
11729 (putc('\n', fd) < 0))
11730 return FAIL;
11731 return OK;
11732}
11733
11734/*
11735 * Write a line to "fd".
11736 * Return FAIL for a write error.
11737 */
11738 int
11739put_line(fd, s)
11740 FILE *fd;
11741 char *s;
11742{
11743 if (fputs(s, fd) < 0 || put_eol(fd) == FAIL)
11744 return FAIL;
11745 return OK;
11746}
11747
11748#ifdef FEAT_VIMINFO
11749/*
11750 * ":rviminfo" and ":wviminfo".
11751 */
11752 static void
11753ex_viminfo(eap)
11754 exarg_T *eap;
11755{
11756 char_u *save_viminfo;
11757
11758 save_viminfo = p_viminfo;
11759 if (*p_viminfo == NUL)
11760 p_viminfo = (char_u *)"'100";
11761 if (eap->cmdidx == CMD_rviminfo)
11762 {
Bram Moolenaard812df62008-11-09 12:46:09 +000011763 if (read_viminfo(eap->arg, VIF_WANT_INFO | VIF_WANT_MARKS
11764 | (eap->forceit ? VIF_FORCEIT : 0)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011765 EMSG(_("E195: Cannot open viminfo file for reading"));
11766 }
11767 else
11768 write_viminfo(eap->arg, eap->forceit);
11769 p_viminfo = save_viminfo;
11770}
11771#endif
11772
11773#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +000011774/*
Bram Moolenaard9462e32011-04-11 21:35:11 +020011775 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +000011776 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +000011777 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011778 void
11779dialog_msg(buff, format, fname)
11780 char_u *buff;
11781 char *format;
11782 char_u *fname;
11783{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011784 if (fname == NULL)
11785 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +020011786 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011787}
11788#endif
11789
11790/*
11791 * ":behave {mswin,xterm}"
11792 */
11793 static void
11794ex_behave(eap)
11795 exarg_T *eap;
11796{
11797 if (STRCMP(eap->arg, "mswin") == 0)
11798 {
11799 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
11800 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
11801 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
11802 set_option_value((char_u *)"keymodel", 0L,
11803 (char_u *)"startsel,stopsel", 0);
11804 }
11805 else if (STRCMP(eap->arg, "xterm") == 0)
11806 {
11807 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
11808 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
11809 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
11810 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
11811 }
11812 else
11813 EMSG2(_(e_invarg2), eap->arg);
11814}
11815
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010011816#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
11817/*
11818 * Function given to ExpandGeneric() to obtain the possible arguments of the
11819 * ":behave {mswin,xterm}" command.
11820 */
11821 char_u *
11822get_behave_arg(xp, idx)
11823 expand_T *xp UNUSED;
11824 int idx;
11825{
11826 if (idx == 0)
11827 return (char_u *)"mswin";
11828 if (idx == 1)
11829 return (char_u *)"xterm";
11830 return NULL;
11831}
11832#endif
11833
Bram Moolenaar071d4272004-06-13 20:20:40 +000011834#ifdef FEAT_AUTOCMD
11835static int filetype_detect = FALSE;
11836static int filetype_plugin = FALSE;
11837static int filetype_indent = FALSE;
11838
11839/*
11840 * ":filetype [plugin] [indent] {on,off,detect}"
11841 * on: Load the filetype.vim file to install autocommands for file types.
11842 * off: Load the ftoff.vim file to remove all autocommands for file types.
11843 * plugin on: load filetype.vim and ftplugin.vim
11844 * plugin off: load ftplugof.vim
11845 * indent on: load filetype.vim and indent.vim
11846 * indent off: load indoff.vim
11847 */
11848 static void
11849ex_filetype(eap)
11850 exarg_T *eap;
11851{
11852 char_u *arg = eap->arg;
11853 int plugin = FALSE;
11854 int indent = FALSE;
11855
11856 if (*eap->arg == NUL)
11857 {
11858 /* Print current status. */
11859 smsg((char_u *)"filetype detection:%s plugin:%s indent:%s",
11860 filetype_detect ? "ON" : "OFF",
11861 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
11862 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
11863 return;
11864 }
11865
11866 /* Accept "plugin" and "indent" in any order. */
11867 for (;;)
11868 {
11869 if (STRNCMP(arg, "plugin", 6) == 0)
11870 {
11871 plugin = TRUE;
11872 arg = skipwhite(arg + 6);
11873 continue;
11874 }
11875 if (STRNCMP(arg, "indent", 6) == 0)
11876 {
11877 indent = TRUE;
11878 arg = skipwhite(arg + 6);
11879 continue;
11880 }
11881 break;
11882 }
11883 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
11884 {
11885 if (*arg == 'o' || !filetype_detect)
11886 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011887 source_runtime((char_u *)FILETYPE_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011888 filetype_detect = TRUE;
11889 if (plugin)
11890 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011891 source_runtime((char_u *)FTPLUGIN_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011892 filetype_plugin = TRUE;
11893 }
11894 if (indent)
11895 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011896 source_runtime((char_u *)INDENT_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011897 filetype_indent = TRUE;
11898 }
11899 }
11900 if (*arg == 'd')
11901 {
11902 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE);
Bram Moolenaara3227e22006-03-08 21:32:40 +000011903 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011904 }
11905 }
11906 else if (STRCMP(arg, "off") == 0)
11907 {
11908 if (plugin || indent)
11909 {
11910 if (plugin)
11911 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011912 source_runtime((char_u *)FTPLUGOF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011913 filetype_plugin = FALSE;
11914 }
11915 if (indent)
11916 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011917 source_runtime((char_u *)INDOFF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011918 filetype_indent = FALSE;
11919 }
11920 }
11921 else
11922 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011923 source_runtime((char_u *)FTOFF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011924 filetype_detect = FALSE;
11925 }
11926 }
11927 else
11928 EMSG2(_(e_invarg2), arg);
11929}
11930
11931/*
11932 * ":setfiletype {name}"
11933 */
11934 static void
11935ex_setfiletype(eap)
11936 exarg_T *eap;
11937{
11938 if (!did_filetype)
11939 set_option_value((char_u *)"filetype", 0L, eap->arg, OPT_LOCAL);
11940}
11941#endif
11942
Bram Moolenaar071d4272004-06-13 20:20:40 +000011943 static void
11944ex_digraphs(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +000011945 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011946{
11947#ifdef FEAT_DIGRAPHS
11948 if (*eap->arg != NUL)
11949 putdigraph(eap->arg);
11950 else
11951 listdigraphs();
11952#else
11953 EMSG(_("E196: No digraphs in this version"));
11954#endif
11955}
11956
11957 static void
11958ex_set(eap)
11959 exarg_T *eap;
11960{
11961 int flags = 0;
11962
11963 if (eap->cmdidx == CMD_setlocal)
11964 flags = OPT_LOCAL;
11965 else if (eap->cmdidx == CMD_setglobal)
11966 flags = OPT_GLOBAL;
11967#if defined(FEAT_EVAL) && defined(FEAT_AUTOCMD) && defined(FEAT_BROWSE)
11968 if (cmdmod.browse && flags == 0)
11969 ex_options(eap);
11970 else
11971#endif
11972 (void)do_set(eap->arg, flags);
11973}
11974
11975#ifdef FEAT_SEARCH_EXTRA
11976/*
11977 * ":nohlsearch"
11978 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011979 static void
11980ex_nohlsearch(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +000011981 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011982{
Bram Moolenaar8050efa2013-11-08 04:30:20 +010011983 SET_NO_HLSEARCH(TRUE);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +000011984 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011985}
11986
11987/*
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011988 * ":[N]match {group} {pattern}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000011989 * Sets nextcmd to the start of the next command, if any. Also called when
11990 * skipping commands to find the next command.
11991 */
11992 static void
11993ex_match(eap)
11994 exarg_T *eap;
11995{
11996 char_u *p;
Bram Moolenaar52d36c82007-08-11 14:00:30 +000011997 char_u *g = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011998 char_u *end;
11999 int c;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012000 int id;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000012001
12002 if (eap->line2 <= 3)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012003 id = eap->line2;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000012004 else
12005 {
12006 EMSG(e_invcmd);
12007 return;
12008 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012009
12010 /* First clear any old pattern. */
12011 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012012 match_delete(curwin, id, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012013
12014 if (ends_excmd(*eap->arg))
12015 end = eap->arg;
12016 else if ((STRNICMP(eap->arg, "none", 4) == 0
12017 && (vim_iswhite(eap->arg[4]) || ends_excmd(eap->arg[4]))))
12018 end = eap->arg + 4;
12019 else
12020 {
12021 p = skiptowhite(eap->arg);
12022 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012023 g = vim_strnsave(eap->arg, (int)(p - eap->arg));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012024 p = skipwhite(p);
12025 if (*p == NUL)
12026 {
12027 /* There must be two arguments. */
12028 EMSG2(_(e_invarg2), eap->arg);
12029 return;
12030 }
12031 end = skip_regexp(p + 1, *p, TRUE, NULL);
12032 if (!eap->skip)
12033 {
12034 if (*end != NUL && !ends_excmd(*skipwhite(end + 1)))
12035 {
12036 eap->errmsg = e_trailing;
12037 return;
12038 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000012039 if (*end != *p)
12040 {
12041 EMSG2(_(e_invarg2), p);
12042 return;
12043 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012044
12045 c = *end;
12046 *end = NUL;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012047 match_add(curwin, g, p + 1, 10, id, NULL);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012048 vim_free(g);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012049 *end = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012050 }
12051 }
12052 eap->nextcmd = find_nextcmd(end);
12053}
12054#endif
12055
12056#ifdef FEAT_CRYPT
12057/*
12058 * ":X": Get crypt key
12059 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012060 static void
12061ex_X(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +000012062 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012063{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +010012064 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +020012065 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012066}
12067#endif
12068
12069#ifdef FEAT_FOLDING
12070 static void
12071ex_fold(eap)
12072 exarg_T *eap;
12073{
12074 if (foldManualAllowed(TRUE))
12075 foldCreate(eap->line1, eap->line2);
12076}
12077
12078 static void
12079ex_foldopen(eap)
12080 exarg_T *eap;
12081{
12082 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
12083 eap->forceit, FALSE);
12084}
12085
12086 static void
12087ex_folddo(eap)
12088 exarg_T *eap;
12089{
12090 linenr_T lnum;
12091
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020012092#ifdef FEAT_CLIPBOARD
12093 start_global_changes();
12094#endif
12095
Bram Moolenaar071d4272004-06-13 20:20:40 +000012096 /* First set the marks for all lines closed/open. */
12097 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
12098 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
12099 ml_setmarked(lnum);
12100
12101 /* Execute the command on the marked lines. */
12102 global_exe(eap->arg);
12103 ml_clearmarked(); /* clear rest of the marks */
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020012104#ifdef FEAT_CLIPBOARD
12105 end_global_changes();
12106#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012107}
12108#endif