blob: 453d83e8d63763399ff85c608d9ab47d01d1ab54 [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));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004408 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004409 goto error;
4410 }
4411 if (skip)
4412 ++cmd;
4413 else
4414 {
4415 /* Only accept a mark in another file when it is
4416 * used by itself: ":'M". */
4417 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
4418 ++cmd;
4419 if (fp == (pos_T *)-1)
4420 /* Jumped to another file. */
4421 lnum = curwin->w_cursor.lnum;
4422 else
4423 {
4424 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425 {
4426 cmd = NULL;
4427 goto error;
4428 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004429 lnum = fp->lnum;
4430 }
4431 }
4432 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004433
4434 case '/':
4435 case '?': /* '/' or '?' - search */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004436 c = *cmd++;
4437 if (addr_type != ADDR_LINES)
4438 {
4439 EMSG(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004440 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004441 goto error;
4442 }
4443 if (skip) /* skip "/pat/" */
4444 {
4445 cmd = skip_regexp(cmd, c, (int)p_magic, NULL);
4446 if (*cmd == c)
4447 ++cmd;
4448 }
4449 else
4450 {
4451 pos = curwin->w_cursor; /* save curwin->w_cursor */
4452 /*
4453 * When '/' or '?' follows another address, start
4454 * from there.
4455 */
4456 if (lnum != MAXLNUM)
4457 curwin->w_cursor.lnum = lnum;
4458 /*
4459 * Start a forward search at the end of the line.
4460 * Start a backward search at the start of the line.
4461 * This makes sure we never match in the current
4462 * line, and can match anywhere in the
4463 * next/previous line.
4464 */
4465 if (c == '/')
4466 curwin->w_cursor.col = MAXCOL;
4467 else
4468 curwin->w_cursor.col = 0;
4469 searchcmdlen = 0;
4470 if (!do_search(NULL, c, cmd, 1L,
4471 SEARCH_HIS | SEARCH_MSG, NULL))
4472 {
4473 curwin->w_cursor = pos;
4474 cmd = NULL;
4475 goto error;
4476 }
4477 lnum = curwin->w_cursor.lnum;
4478 curwin->w_cursor = pos;
4479 /* adjust command string pointer */
4480 cmd += searchcmdlen;
4481 }
4482 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004483
4484 case '\\': /* "\?", "\/" or "\&", repeat search */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004485 ++cmd;
4486 if (addr_type != ADDR_LINES)
4487 {
4488 EMSG(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004489 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004490 goto error;
4491 }
4492 if (*cmd == '&')
4493 i = RE_SUBST;
4494 else if (*cmd == '?' || *cmd == '/')
4495 i = RE_SEARCH;
4496 else
4497 {
4498 EMSG(_(e_backslash));
4499 cmd = NULL;
4500 goto error;
4501 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004502
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004503 if (!skip)
4504 {
4505 /*
4506 * When search follows another address, start from
4507 * there.
4508 */
4509 if (lnum != MAXLNUM)
4510 pos.lnum = lnum;
4511 else
4512 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004513
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004514 /*
4515 * Start the search just like for the above
4516 * do_search().
4517 */
4518 if (*cmd != '?')
4519 pos.col = MAXCOL;
4520 else
4521 pos.col = 0;
4522 if (searchit(curwin, curbuf, &pos,
4523 *cmd == '?' ? BACKWARD : FORWARD,
4524 (char_u *)"", 1L, SEARCH_MSG,
4525 i, (linenr_T)0, NULL) != FAIL)
4526 lnum = pos.lnum;
4527 else
4528 {
4529 cmd = NULL;
4530 goto error;
4531 }
4532 }
4533 ++cmd;
4534 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004535
4536 default:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004537 if (VIM_ISDIGIT(*cmd)) /* absolute line number */
4538 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004539 }
4540
4541 for (;;)
4542 {
4543 cmd = skipwhite(cmd);
4544 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4545 break;
4546
4547 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004548 {
4549 switch (addr_type)
4550 {
4551 case ADDR_LINES:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004552 /* "+1" is same as ".+1" */
4553 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004554 break;
4555 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004556 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004557 break;
4558 case ADDR_ARGUMENTS:
4559 lnum = curwin->w_arg_idx + 1;
4560 break;
4561 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004562 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004563 lnum = curbuf->b_fnum;
4564 break;
4565 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004566 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004567 break;
4568 }
4569 }
4570
Bram Moolenaar071d4272004-06-13 20:20:40 +00004571 if (VIM_ISDIGIT(*cmd))
4572 i = '+'; /* "number" is same as "+number" */
4573 else
4574 i = *cmd++;
4575 if (!VIM_ISDIGIT(*cmd)) /* '+' is '+1', but '+0' is not '+1' */
4576 n = 1;
4577 else
4578 n = getdigits(&cmd);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004579 if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004580 || addr_type == ADDR_BUFFERS)
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004581 lnum = compute_buffer_local_count(
4582 addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004583 else if (i == '-')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004584 lnum -= n;
4585 else
4586 lnum += n;
4587 }
4588 } while (*cmd == '/' || *cmd == '?');
4589
4590error:
4591 *ptr = cmd;
4592 return lnum;
4593}
4594
4595/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004596 * Get flags from an Ex command argument.
4597 */
4598 static void
4599get_flags(eap)
4600 exarg_T *eap;
4601{
4602 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4603 {
4604 if (*eap->arg == 'l')
4605 eap->flags |= EXFLAG_LIST;
4606 else if (*eap->arg == 'p')
4607 eap->flags |= EXFLAG_PRINT;
4608 else
4609 eap->flags |= EXFLAG_NR;
4610 eap->arg = skipwhite(eap->arg + 1);
4611 }
4612}
4613
4614/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004615 * Function called for command which is Not Implemented. NI!
4616 */
4617 void
4618ex_ni(eap)
4619 exarg_T *eap;
4620{
4621 if (!eap->skip)
4622 eap->errmsg = (char_u *)N_("E319: Sorry, the command is not available in this version");
4623}
4624
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004625#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004626/*
4627 * Function called for script command which is Not Implemented. NI!
4628 * Skips over ":perl <<EOF" constructs.
4629 */
4630 static void
4631ex_script_ni(eap)
4632 exarg_T *eap;
4633{
4634 if (!eap->skip)
4635 ex_ni(eap);
4636 else
4637 vim_free(script_get(eap, eap->arg));
4638}
4639#endif
4640
4641/*
4642 * Check range in Ex command for validity.
4643 * Return NULL when valid, error message when invalid.
4644 */
4645 static char_u *
4646invalid_range(eap)
4647 exarg_T *eap;
4648{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004649 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004650 if ( eap->line1 < 0
4651 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004652 || eap->line1 > eap->line2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004653 return (char_u *)_(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004654
4655 if (eap->argt & RANGE)
4656 {
4657 switch(eap->addr_type)
4658 {
4659 case ADDR_LINES:
4660 if (!(eap->argt & NOTADR)
4661 && eap->line2 > curbuf->b_ml.ml_line_count
4662#ifdef FEAT_DIFF
4663 + (eap->cmdidx == CMD_diffget)
4664#endif
4665 )
4666 return (char_u *)_(e_invrange);
4667 break;
4668 case ADDR_ARGUMENTS:
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004669 /* add 1 if ARGCOUNT is 0 */
4670 if (eap->line2 > ARGCOUNT + (!ARGCOUNT))
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004671 return (char_u *)_(e_invrange);
4672 break;
4673 case ADDR_BUFFERS:
4674 if (eap->line1 < firstbuf->b_fnum
4675 || eap->line2 > lastbuf->b_fnum)
4676 return (char_u *)_(e_invrange);
4677 break;
4678 case ADDR_LOADED_BUFFERS:
4679 buf = firstbuf;
4680 while (buf->b_ml.ml_mfp == NULL)
4681 {
4682 if (buf->b_next == NULL)
4683 return (char_u *)_(e_invrange);
4684 buf = buf->b_next;
4685 }
4686 if (eap->line1 < buf->b_fnum)
4687 return (char_u *)_(e_invrange);
4688 buf = lastbuf;
4689 while (buf->b_ml.ml_mfp == NULL)
4690 {
4691 if (buf->b_prev == NULL)
4692 return (char_u *)_(e_invrange);
4693 buf = buf->b_prev;
4694 }
4695 if (eap->line2 > buf->b_fnum)
4696 return (char_u *)_(e_invrange);
4697 break;
4698 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01004699 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004700 return (char_u *)_(e_invrange);
4701 break;
4702 case ADDR_TABS:
4703 if (eap->line2 > LAST_TAB_NR)
4704 return (char_u *)_(e_invrange);
4705 break;
4706 }
4707 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004708 return NULL;
4709}
4710
4711/*
4712 * Correct the range for zero line number, if required.
4713 */
4714 static void
4715correct_range(eap)
4716 exarg_T *eap;
4717{
4718 if (!(eap->argt & ZEROR)) /* zero in range not allowed */
4719 {
4720 if (eap->line1 == 0)
4721 eap->line1 = 1;
4722 if (eap->line2 == 0)
4723 eap->line2 = 1;
4724 }
4725}
4726
Bram Moolenaar748bf032005-02-02 23:04:36 +00004727#ifdef FEAT_QUICKFIX
4728static char_u *skip_grep_pat __ARGS((exarg_T *eap));
4729
4730/*
4731 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4732 * pattern. Otherwise return eap->arg.
4733 */
4734 static char_u *
4735skip_grep_pat(eap)
4736 exarg_T *eap;
4737{
4738 char_u *p = eap->arg;
4739
Bram Moolenaara37420f2006-02-04 22:37:47 +00004740 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4741 || eap->cmdidx == CMD_vimgrepadd
4742 || eap->cmdidx == CMD_lvimgrepadd
4743 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004744 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004745 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004746 if (p == NULL)
4747 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004748 }
4749 return p;
4750}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004751
4752/*
4753 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4754 * in the command line, so that things like % get expanded.
4755 */
4756 static char_u *
4757replace_makeprg(eap, p, cmdlinep)
4758 exarg_T *eap;
4759 char_u *p;
4760 char_u **cmdlinep;
4761{
4762 char_u *new_cmdline;
4763 char_u *program;
4764 char_u *pos;
4765 char_u *ptr;
4766 int len;
4767 int i;
4768
4769 /*
4770 * Don't do it when ":vimgrep" is used for ":grep".
4771 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004772 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4773 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4774 || eap->cmdidx == CMD_grepadd
4775 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004776 && !grep_internal(eap->cmdidx))
4777 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004778 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4779 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004780 {
4781 if (*curbuf->b_p_gp == NUL)
4782 program = p_gp;
4783 else
4784 program = curbuf->b_p_gp;
4785 }
4786 else
4787 {
4788 if (*curbuf->b_p_mp == NUL)
4789 program = p_mp;
4790 else
4791 program = curbuf->b_p_mp;
4792 }
4793
4794 p = skipwhite(p);
4795
4796 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4797 {
4798 /* replace $* by given arguments */
4799 i = 1;
4800 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4801 ++i;
4802 len = (int)STRLEN(p);
4803 new_cmdline = alloc((int)(STRLEN(program) + i * (len - 2) + 1));
4804 if (new_cmdline == NULL)
4805 return NULL; /* out of memory */
4806 ptr = new_cmdline;
4807 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4808 {
4809 i = (int)(pos - program);
4810 STRNCPY(ptr, program, i);
4811 STRCPY(ptr += i, p);
4812 ptr += len;
4813 program = pos + 2;
4814 }
4815 STRCPY(ptr, program);
4816 }
4817 else
4818 {
4819 new_cmdline = alloc((int)(STRLEN(program) + STRLEN(p) + 2));
4820 if (new_cmdline == NULL)
4821 return NULL; /* out of memory */
4822 STRCPY(new_cmdline, program);
4823 STRCAT(new_cmdline, " ");
4824 STRCAT(new_cmdline, p);
4825 }
4826 msg_make(p);
4827
4828 /* 'eap->cmd' is not set here, because it is not used at CMD_make */
4829 vim_free(*cmdlinep);
4830 *cmdlinep = new_cmdline;
4831 p = new_cmdline;
4832 }
4833 return p;
4834}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004835#endif
4836
Bram Moolenaar071d4272004-06-13 20:20:40 +00004837/*
4838 * Expand file name in Ex command argument.
4839 * Return FAIL for failure, OK otherwise.
4840 */
4841 int
4842expand_filename(eap, cmdlinep, errormsgp)
4843 exarg_T *eap;
4844 char_u **cmdlinep;
4845 char_u **errormsgp;
4846{
4847 int has_wildcards; /* need to expand wildcards */
4848 char_u *repl;
4849 int srclen;
4850 char_u *p;
4851 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004852 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853
Bram Moolenaar748bf032005-02-02 23:04:36 +00004854#ifdef FEAT_QUICKFIX
4855 /* Skip a regexp pattern for ":vimgrep[add] pat file..." */
4856 p = skip_grep_pat(eap);
4857#else
4858 p = eap->arg;
4859#endif
4860
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861 /*
4862 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4863 * the file name contains a wildcard it should not cause expanding.
4864 * (it will be expanded anyway if there is a wildcard before replacing).
4865 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004866 has_wildcards = mch_has_wildcard(p);
4867 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004868 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004869#ifdef FEAT_EVAL
4870 /* Skip over `=expr`, wildcards in it are not expanded. */
4871 if (p[0] == '`' && p[1] == '=')
4872 {
4873 p += 2;
4874 (void)skip_expr(&p);
4875 if (*p == '`')
4876 ++p;
4877 continue;
4878 }
4879#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004880 /*
4881 * Quick check if this cannot be the start of a special string.
4882 * Also removes backslash before '%', '#' and '<'.
4883 */
4884 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4885 {
4886 ++p;
4887 continue;
4888 }
4889
4890 /*
4891 * Try to find a match at this position.
4892 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004893 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
4894 errormsgp, &escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004895 if (*errormsgp != NULL) /* error detected */
4896 return FAIL;
4897 if (repl == NULL) /* no match found */
4898 {
4899 p += srclen;
4900 continue;
4901 }
4902
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004903 /* Wildcards won't be expanded below, the replacement is taken
4904 * literally. But do expand "~/file", "~user/file" and "$HOME/file". */
4905 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4906 {
4907 char_u *l = repl;
4908
4909 repl = expand_env_save(repl);
4910 vim_free(l);
4911 }
4912
Bram Moolenaar63b92542007-03-27 14:57:09 +00004913 /* Need to escape white space et al. with a backslash.
4914 * Don't do this for:
4915 * - replacement that already has been escaped: "##"
4916 * - shell commands (may have to use quotes instead).
4917 * - non-unix systems when there is a single argument (spaces don't
4918 * separate arguments then).
4919 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004920 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00004921 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00004922 && eap->cmdidx != CMD_bang
4923 && eap->cmdidx != CMD_make
Bram Moolenaara37420f2006-02-04 22:37:47 +00004924 && eap->cmdidx != CMD_lmake
Bram Moolenaar071d4272004-06-13 20:20:40 +00004925 && eap->cmdidx != CMD_grep
Bram Moolenaara37420f2006-02-04 22:37:47 +00004926 && eap->cmdidx != CMD_lgrep
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927 && eap->cmdidx != CMD_grepadd
Bram Moolenaara37420f2006-02-04 22:37:47 +00004928 && eap->cmdidx != CMD_lgrepadd
Bram Moolenaar071d4272004-06-13 20:20:40 +00004929#ifndef UNIX
4930 && !(eap->argt & NOSPC)
4931#endif
4932 )
4933 {
4934 char_u *l;
4935#ifdef BACKSLASH_IN_FILENAME
4936 /* Don't escape a backslash here, because rem_backslash() doesn't
4937 * remove it later. */
4938 static char_u *nobslash = (char_u *)" \t\"|";
4939# define ESCAPE_CHARS nobslash
4940#else
4941# define ESCAPE_CHARS escape_chars
4942#endif
4943
4944 for (l = repl; *l; ++l)
4945 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
4946 {
4947 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
4948 if (l != NULL)
4949 {
4950 vim_free(repl);
4951 repl = l;
4952 }
4953 break;
4954 }
4955 }
4956
4957 /* For a shell command a '!' must be escaped. */
4958 if ((eap->usefilter || eap->cmdidx == CMD_bang)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004959 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004960 {
4961 char_u *l;
4962
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004963 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004964 if (l != NULL)
4965 {
4966 vim_free(repl);
4967 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004968 }
4969 }
4970
4971 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
4972 vim_free(repl);
4973 if (p == NULL)
4974 return FAIL;
4975 }
4976
4977 /*
4978 * One file argument: Expand wildcards.
4979 * Don't do this with ":r !command" or ":w !command".
4980 */
4981 if ((eap->argt & NOSPC) && !eap->usefilter)
4982 {
4983 /*
4984 * May do this twice:
4985 * 1. Replace environment variables.
4986 * 2. Replace any other wildcards, remove backslashes.
4987 */
4988 for (n = 1; n <= 2; ++n)
4989 {
4990 if (n == 2)
4991 {
4992#ifdef UNIX
4993 /*
4994 * Only for Unix we check for more than one file name.
4995 * For other systems spaces are considered to be part
4996 * of the file name.
4997 * Only check here if there is no wildcard, otherwise
4998 * ExpandOne() will check for errors. This allows
4999 * ":e `ls ve*.c`" on Unix.
5000 */
5001 if (!has_wildcards)
5002 for (p = eap->arg; *p; ++p)
5003 {
5004 /* skip escaped characters */
5005 if (p[1] && (*p == '\\' || *p == Ctrl_V))
5006 ++p;
5007 else if (vim_iswhite(*p))
5008 {
5009 *errormsgp = (char_u *)_("E172: Only one file name allowed");
5010 return FAIL;
5011 }
5012 }
5013#endif
5014
5015 /*
5016 * Halve the number of backslashes (this is Vi compatible).
5017 * For Unix and OS/2, when wildcards are expanded, this is
5018 * done by ExpandOne() below.
5019 */
5020#if defined(UNIX) || defined(OS2)
5021 if (!has_wildcards)
5022#endif
5023 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005024 }
5025
5026 if (has_wildcards)
5027 {
5028 if (n == 1)
5029 {
5030 /*
5031 * First loop: May expand environment variables. This
5032 * can be done much faster with expand_env() than with
5033 * something else (e.g., calling a shell).
5034 * After expanding environment variables, check again
5035 * if there are still wildcards present.
5036 */
5037 if (vim_strchr(eap->arg, '$') != NULL
5038 || vim_strchr(eap->arg, '~') != NULL)
5039 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005040 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00005041 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005042 has_wildcards = mch_has_wildcard(NameBuff);
5043 p = NameBuff;
5044 }
5045 else
5046 p = NULL;
5047 }
5048 else /* n == 2 */
5049 {
5050 expand_T xpc;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005051 int options = WILD_LIST_NOTFOUND|WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052
5053 ExpandInit(&xpc);
5054 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005055 if (p_wic)
5056 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01005058 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059 if (p == NULL)
5060 return FAIL;
5061 }
5062 if (p != NULL)
5063 {
5064 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
5065 p, cmdlinep);
5066 if (n == 2) /* p came from ExpandOne() */
5067 vim_free(p);
5068 }
5069 }
5070 }
5071 }
5072 return OK;
5073}
5074
5075/*
5076 * Replace part of the command line, keeping eap->cmd, eap->arg and
5077 * eap->nextcmd correct.
5078 * "src" points to the part that is to be replaced, of length "srclen".
5079 * "repl" is the replacement string.
5080 * Returns a pointer to the character after the replaced string.
5081 * Returns NULL for failure.
5082 */
5083 static char_u *
5084repl_cmdline(eap, src, srclen, repl, cmdlinep)
5085 exarg_T *eap;
5086 char_u *src;
5087 int srclen;
5088 char_u *repl;
5089 char_u **cmdlinep;
5090{
5091 int len;
5092 int i;
5093 char_u *new_cmdline;
5094
5095 /*
5096 * The new command line is build in new_cmdline[].
5097 * First allocate it.
5098 * Careful: a "+cmd" argument may have been NUL terminated.
5099 */
5100 len = (int)STRLEN(repl);
5101 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005102 if (eap->nextcmd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005103 i += (int)STRLEN(eap->nextcmd);/* add space for next command */
5104 if ((new_cmdline = alloc((unsigned)i)) == NULL)
5105 return NULL; /* out of memory! */
5106
5107 /*
5108 * Copy the stuff before the expanded part.
5109 * Copy the expanded stuff.
5110 * Copy what came after the expanded part.
5111 * Copy the next commands, if there are any.
5112 */
5113 i = (int)(src - *cmdlinep); /* length of part before match */
5114 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00005115
Bram Moolenaar071d4272004-06-13 20:20:40 +00005116 mch_memmove(new_cmdline + i, repl, (size_t)len);
5117 i += len; /* remember the end of the string */
5118 STRCPY(new_cmdline + i, src + srclen);
5119 src = new_cmdline + i; /* remember where to continue */
5120
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005121 if (eap->nextcmd != NULL) /* append next command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005122 {
5123 i = (int)STRLEN(new_cmdline) + 1;
5124 STRCPY(new_cmdline + i, eap->nextcmd);
5125 eap->nextcmd = new_cmdline + i;
5126 }
5127 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
5128 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
5129 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
5130 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
5131 vim_free(*cmdlinep);
5132 *cmdlinep = new_cmdline;
5133
5134 return src;
5135}
5136
5137/*
5138 * Check for '|' to separate commands and '"' to start comments.
5139 */
5140 void
5141separate_nextcmd(eap)
5142 exarg_T *eap;
5143{
5144 char_u *p;
5145
Bram Moolenaar86b68352004-12-27 21:59:20 +00005146#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00005147 p = skip_grep_pat(eap);
5148#else
5149 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005150#endif
5151
5152 for ( ; *p; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153 {
5154 if (*p == Ctrl_V)
5155 {
5156 if (eap->argt & (USECTRLV | XFILE))
5157 ++p; /* skip CTRL-V and next char */
5158 else
Bram Moolenaarb0db5692007-08-14 20:54:49 +00005159 /* remove CTRL-V and skip next char */
Bram Moolenaara7241f52008-06-24 20:39:31 +00005160 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005161 if (*p == NUL) /* stop at NUL after CTRL-V */
5162 break;
5163 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005164
5165#ifdef FEAT_EVAL
5166 /* Skip over `=expr` when wildcards are expanded. */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005167 else if (p[0] == '`' && p[1] == '=' && (eap->argt & XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005168 {
5169 p += 2;
5170 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005171 }
5172#endif
5173
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174 /* Check for '"': start of comment or '|': next command */
5175 /* :@" and :*" do not start a comment!
5176 * :redir @" doesn't either. */
5177 else if ((*p == '"' && !(eap->argt & NOTRLCOM)
5178 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
5179 || p != eap->arg)
5180 && (eap->cmdidx != CMD_redir
5181 || p != eap->arg + 1 || p[-1] != '@'))
5182 || *p == '|' || *p == '\n')
5183 {
5184 /*
5185 * We remove the '\' before the '|', unless USECTRLV is used
5186 * AND 'b' is present in 'cpoptions'.
5187 */
5188 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
5189 || !(eap->argt & USECTRLV)) && *(p - 1) == '\\')
5190 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00005191 STRMOVE(p - 1, p); /* remove the '\' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005192 --p;
5193 }
5194 else
5195 {
5196 eap->nextcmd = check_nextcmd(p);
5197 *p = NUL;
5198 break;
5199 }
5200 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005202
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203 if (!(eap->argt & NOTRLCOM)) /* remove trailing spaces */
5204 del_trailing_spaces(eap->arg);
5205}
5206
5207/*
5208 * get + command from ex argument
5209 */
5210 static char_u *
5211getargcmd(argp)
5212 char_u **argp;
5213{
5214 char_u *arg = *argp;
5215 char_u *command = NULL;
5216
5217 if (*arg == '+') /* +[command] */
5218 {
5219 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02005220 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005221 command = dollar_command;
5222 else
5223 {
5224 command = arg;
5225 arg = skip_cmd_arg(command, TRUE);
5226 if (*arg != NUL)
5227 *arg++ = NUL; /* terminate command with NUL */
5228 }
5229
5230 arg = skipwhite(arg); /* skip over spaces */
5231 *argp = arg;
5232 }
5233 return command;
5234}
5235
5236/*
5237 * Find end of "+command" argument. Skip over "\ " and "\\".
5238 */
5239 static char_u *
5240skip_cmd_arg(p, rembs)
5241 char_u *p;
5242 int rembs; /* TRUE to halve the number of backslashes */
5243{
5244 while (*p && !vim_isspace(*p))
5245 {
5246 if (*p == '\\' && p[1] != NUL)
5247 {
5248 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00005249 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005250 else
5251 ++p;
5252 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005253 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005254 }
5255 return p;
5256}
5257
5258/*
5259 * Get "++opt=arg" argument.
5260 * Return FAIL or OK.
5261 */
5262 static int
5263getargopt(eap)
5264 exarg_T *eap;
5265{
5266 char_u *arg = eap->arg + 2;
5267 int *pp = NULL;
5268#ifdef FEAT_MBYTE
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005269 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005270 char_u *p;
5271#endif
5272
5273 /* ":edit ++[no]bin[ary] file" */
5274 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
5275 {
5276 if (*arg == 'n')
5277 {
5278 arg += 2;
5279 eap->force_bin = FORCE_NOBIN;
5280 }
5281 else
5282 eap->force_bin = FORCE_BIN;
5283 if (!checkforcmd(&arg, "binary", 3))
5284 return FAIL;
5285 eap->arg = skipwhite(arg);
5286 return OK;
5287 }
5288
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005289 /* ":read ++edit file" */
5290 if (STRNCMP(arg, "edit", 4) == 0)
5291 {
5292 eap->read_edit = TRUE;
5293 eap->arg = skipwhite(arg + 4);
5294 return OK;
5295 }
5296
Bram Moolenaar071d4272004-06-13 20:20:40 +00005297 if (STRNCMP(arg, "ff", 2) == 0)
5298 {
5299 arg += 2;
5300 pp = &eap->force_ff;
5301 }
5302 else if (STRNCMP(arg, "fileformat", 10) == 0)
5303 {
5304 arg += 10;
5305 pp = &eap->force_ff;
5306 }
5307#ifdef FEAT_MBYTE
5308 else if (STRNCMP(arg, "enc", 3) == 0)
5309 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01005310 if (STRNCMP(arg, "encoding", 8) == 0)
5311 arg += 8;
5312 else
5313 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005314 pp = &eap->force_enc;
5315 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005316 else if (STRNCMP(arg, "bad", 3) == 0)
5317 {
5318 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005319 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005320 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005321#endif
5322
5323 if (pp == NULL || *arg != '=')
5324 return FAIL;
5325
5326 ++arg;
5327 *pp = (int)(arg - eap->cmd);
5328 arg = skip_cmd_arg(arg, FALSE);
5329 eap->arg = skipwhite(arg);
5330 *arg = NUL;
5331
5332#ifdef FEAT_MBYTE
5333 if (pp == &eap->force_ff)
5334 {
5335#endif
5336 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
5337 return FAIL;
5338#ifdef FEAT_MBYTE
5339 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005340 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005341 {
5342 /* Make 'fileencoding' lower case. */
5343 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
5344 *p = TOLOWER_ASC(*p);
5345 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005346 else
5347 {
5348 /* Check ++bad= argument. Must be a single-byte character, "keep" or
5349 * "drop". */
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005350 p = eap->cmd + bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005351 if (STRICMP(p, "keep") == 0)
5352 eap->bad_char = BAD_KEEP;
5353 else if (STRICMP(p, "drop") == 0)
5354 eap->bad_char = BAD_DROP;
5355 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
5356 eap->bad_char = *p;
5357 else
5358 return FAIL;
5359 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005360#endif
5361
5362 return OK;
5363}
5364
5365/*
5366 * ":abbreviate" and friends.
5367 */
5368 static void
5369ex_abbreviate(eap)
5370 exarg_T *eap;
5371{
5372 do_exmap(eap, TRUE); /* almost the same as mapping */
5373}
5374
5375/*
5376 * ":map" and friends.
5377 */
5378 static void
5379ex_map(eap)
5380 exarg_T *eap;
5381{
5382 /*
5383 * If we are sourcing .exrc or .vimrc in current directory we
5384 * print the mappings for security reasons.
5385 */
5386 if (secure)
5387 {
5388 secure = 2;
5389 msg_outtrans(eap->cmd);
5390 msg_putchar('\n');
5391 }
5392 do_exmap(eap, FALSE);
5393}
5394
5395/*
5396 * ":unmap" and friends.
5397 */
5398 static void
5399ex_unmap(eap)
5400 exarg_T *eap;
5401{
5402 do_exmap(eap, FALSE);
5403}
5404
5405/*
5406 * ":mapclear" and friends.
5407 */
5408 static void
5409ex_mapclear(eap)
5410 exarg_T *eap;
5411{
5412 map_clear(eap->cmd, eap->arg, eap->forceit, FALSE);
5413}
5414
5415/*
5416 * ":abclear" and friends.
5417 */
5418 static void
5419ex_abclear(eap)
5420 exarg_T *eap;
5421{
5422 map_clear(eap->cmd, eap->arg, TRUE, TRUE);
5423}
5424
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005425#if defined(FEAT_AUTOCMD) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005426 static void
5427ex_autocmd(eap)
5428 exarg_T *eap;
5429{
5430 /*
5431 * Disallow auto commands from .exrc and .vimrc in current
5432 * directory for security reasons.
5433 */
5434 if (secure)
5435 {
5436 secure = 2;
5437 eap->errmsg = e_curdir;
5438 }
5439 else if (eap->cmdidx == CMD_autocmd)
5440 do_autocmd(eap->arg, eap->forceit);
5441 else
5442 do_augroup(eap->arg, eap->forceit);
5443}
5444
5445/*
5446 * ":doautocmd": Apply the automatic commands to the current buffer.
5447 */
5448 static void
5449ex_doautocmd(eap)
5450 exarg_T *eap;
5451{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005452 char_u *arg = eap->arg;
5453 int call_do_modelines = check_nomodeline(&arg);
5454
5455 (void)do_doautocmd(arg, TRUE);
5456 if (call_do_modelines) /* Only when there is no <nomodeline>. */
5457 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005458}
5459#endif
5460
5461#ifdef FEAT_LISTCMDS
5462/*
5463 * :[N]bunload[!] [N] [bufname] unload buffer
5464 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
5465 * :[N]bwipeout[!] [N] [bufname] delete buffer really
5466 */
5467 static void
5468ex_bunload(eap)
5469 exarg_T *eap;
5470{
5471 eap->errmsg = do_bufdel(
5472 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
5473 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
5474 : DOBUF_UNLOAD, eap->arg,
5475 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
5476}
5477
5478/*
5479 * :[N]buffer [N] to buffer N
5480 * :[N]sbuffer [N] to buffer N
5481 */
5482 static void
5483ex_buffer(eap)
5484 exarg_T *eap;
5485{
5486 if (*eap->arg)
5487 eap->errmsg = e_trailing;
5488 else
5489 {
5490 if (eap->addr_count == 0) /* default is current buffer */
5491 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
5492 else
5493 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005494 if (eap->do_ecmd_cmd != NULL)
5495 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005496 }
5497}
5498
5499/*
5500 * :[N]bmodified [N] to next mod. buffer
5501 * :[N]sbmodified [N] to next mod. buffer
5502 */
5503 static void
5504ex_bmodified(eap)
5505 exarg_T *eap;
5506{
5507 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005508 if (eap->do_ecmd_cmd != NULL)
5509 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005510}
5511
5512/*
5513 * :[N]bnext [N] to next buffer
5514 * :[N]sbnext [N] split and to next buffer
5515 */
5516 static void
5517ex_bnext(eap)
5518 exarg_T *eap;
5519{
5520 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005521 if (eap->do_ecmd_cmd != NULL)
5522 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005523}
5524
5525/*
5526 * :[N]bNext [N] to previous buffer
5527 * :[N]bprevious [N] to previous buffer
5528 * :[N]sbNext [N] split and to previous buffer
5529 * :[N]sbprevious [N] split and to previous buffer
5530 */
5531 static void
5532ex_bprevious(eap)
5533 exarg_T *eap;
5534{
5535 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005536 if (eap->do_ecmd_cmd != NULL)
5537 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005538}
5539
5540/*
5541 * :brewind to first buffer
5542 * :bfirst to first buffer
5543 * :sbrewind split and to first buffer
5544 * :sbfirst split and to first buffer
5545 */
5546 static void
5547ex_brewind(eap)
5548 exarg_T *eap;
5549{
5550 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005551 if (eap->do_ecmd_cmd != NULL)
5552 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005553}
5554
5555/*
5556 * :blast to last buffer
5557 * :sblast split and to last buffer
5558 */
5559 static void
5560ex_blast(eap)
5561 exarg_T *eap;
5562{
5563 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005564 if (eap->do_ecmd_cmd != NULL)
5565 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005566}
5567#endif
5568
5569 int
5570ends_excmd(c)
5571 int c;
5572{
5573 return (c == NUL || c == '|' || c == '"' || c == '\n');
5574}
5575
5576#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5577 || defined(PROTO)
5578/*
5579 * Return the next command, after the first '|' or '\n'.
5580 * Return NULL if not found.
5581 */
5582 char_u *
5583find_nextcmd(p)
5584 char_u *p;
5585{
5586 while (*p != '|' && *p != '\n')
5587 {
5588 if (*p == NUL)
5589 return NULL;
5590 ++p;
5591 }
5592 return (p + 1);
5593}
5594#endif
5595
5596/*
5597 * Check if *p is a separator between Ex commands.
5598 * Return NULL if it isn't, (p + 1) if it is.
5599 */
5600 char_u *
5601check_nextcmd(p)
5602 char_u *p;
5603{
5604 p = skipwhite(p);
5605 if (*p == '|' || *p == '\n')
5606 return (p + 1);
5607 else
5608 return NULL;
5609}
5610
5611/*
5612 * - if there are more files to edit
5613 * - and this is the last window
5614 * - and forceit not used
5615 * - and not repeated twice on a row
5616 * return FAIL and give error message if 'message' TRUE
5617 * return OK otherwise
5618 */
5619 static int
5620check_more(message, forceit)
5621 int message; /* when FALSE check only, no messages */
5622 int forceit;
5623{
5624 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5625
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005626 if (!forceit && only_one_window()
5627 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005628 {
5629 if (message)
5630 {
5631#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
5632 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
5633 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005634 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005635
5636 if (n == 1)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02005637 vim_strncpy(buff,
5638 (char_u *)_("1 more file to edit. Quit anyway?"),
Bram Moolenaard9462e32011-04-11 21:35:11 +02005639 DIALOG_MSG_SIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005640 else
Bram Moolenaard9462e32011-04-11 21:35:11 +02005641 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005642 _("%d more files to edit. Quit anyway?"), n);
5643 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5644 return OK;
5645 return FAIL;
5646 }
5647#endif
5648 if (n == 1)
5649 EMSG(_("E173: 1 more file to edit"));
5650 else
5651 EMSGN(_("E173: %ld more files to edit"), n);
5652 quitmore = 2; /* next try to quit is allowed */
5653 }
5654 return FAIL;
5655 }
5656 return OK;
5657}
5658
5659#ifdef FEAT_CMDL_COMPL
5660/*
5661 * Function given to ExpandGeneric() to obtain the list of command names.
5662 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005663 char_u *
5664get_command_name(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00005665 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005666 int idx;
5667{
5668 if (idx >= (int)CMD_SIZE)
5669# ifdef FEAT_USR_CMDS
5670 return get_user_command_name(idx);
5671# else
5672 return NULL;
5673# endif
5674 return cmdnames[idx].cmd_name;
5675}
5676#endif
5677
5678#if defined(FEAT_USR_CMDS) || defined(PROTO)
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005679static 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 +00005680static void uc_list __ARGS((char_u *name, size_t name_len));
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005681static 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 +00005682static char_u *uc_split_args __ARGS((char_u *arg, size_t *lenp));
5683static 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));
5684
5685 static int
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005686uc_add_command(name, name_len, rep, argt, def, flags, compl, compl_arg, addr_type, force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005687 char_u *name;
5688 size_t name_len;
5689 char_u *rep;
5690 long argt;
5691 long def;
5692 int flags;
5693 int compl;
5694 char_u *compl_arg;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005695 int addr_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005696 int force;
5697{
5698 ucmd_T *cmd = NULL;
5699 char_u *p;
5700 int i;
5701 int cmp = 1;
5702 char_u *rep_buf = NULL;
5703 garray_T *gap;
5704
Bram Moolenaar9c102382006-05-03 21:26:49 +00005705 replace_termcodes(rep, &rep_buf, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005706 if (rep_buf == NULL)
5707 {
5708 /* Can't replace termcodes - try using the string as is */
5709 rep_buf = vim_strsave(rep);
5710
5711 /* Give up if out of memory */
5712 if (rep_buf == NULL)
5713 return FAIL;
5714 }
5715
5716 /* get address of growarray: global or in curbuf */
5717 if (flags & UC_BUFFER)
5718 {
5719 gap = &curbuf->b_ucmds;
5720 if (gap->ga_itemsize == 0)
5721 ga_init2(gap, (int)sizeof(ucmd_T), 4);
5722 }
5723 else
5724 gap = &ucmds;
5725
5726 /* Search for the command in the already defined commands. */
5727 for (i = 0; i < gap->ga_len; ++i)
5728 {
5729 size_t len;
5730
5731 cmd = USER_CMD_GA(gap, i);
5732 len = STRLEN(cmd->uc_name);
5733 cmp = STRNCMP(name, cmd->uc_name, name_len);
5734 if (cmp == 0)
5735 {
5736 if (name_len < len)
5737 cmp = -1;
5738 else if (name_len > len)
5739 cmp = 1;
5740 }
5741
5742 if (cmp == 0)
5743 {
5744 if (!force)
5745 {
5746 EMSG(_("E174: Command already exists: add ! to replace it"));
5747 goto fail;
5748 }
5749
5750 vim_free(cmd->uc_rep);
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00005751 cmd->uc_rep = NULL;
5752#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5753 vim_free(cmd->uc_compl_arg);
5754 cmd->uc_compl_arg = NULL;
5755#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005756 break;
5757 }
5758
5759 /* Stop as soon as we pass the name to add */
5760 if (cmp < 0)
5761 break;
5762 }
5763
5764 /* Extend the array unless we're replacing an existing command */
5765 if (cmp != 0)
5766 {
5767 if (ga_grow(gap, 1) != OK)
5768 goto fail;
5769 if ((p = vim_strnsave(name, (int)name_len)) == NULL)
5770 goto fail;
5771
5772 cmd = USER_CMD_GA(gap, i);
5773 mch_memmove(cmd + 1, cmd, (gap->ga_len - i) * sizeof(ucmd_T));
5774
5775 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005776
5777 cmd->uc_name = p;
5778 }
5779
5780 cmd->uc_rep = rep_buf;
5781 cmd->uc_argt = argt;
5782 cmd->uc_def = def;
5783 cmd->uc_compl = compl;
5784#ifdef FEAT_EVAL
5785 cmd->uc_scriptID = current_SID;
5786# ifdef FEAT_CMDL_COMPL
5787 cmd->uc_compl_arg = compl_arg;
5788# endif
5789#endif
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005790 cmd->uc_addr_type = addr_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005791
5792 return OK;
5793
5794fail:
5795 vim_free(rep_buf);
5796#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5797 vim_free(compl_arg);
5798#endif
5799 return FAIL;
5800}
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005801#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005802
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005803#if defined(FEAT_USR_CMDS)
5804static struct
5805{
5806 int expand;
5807 char *name;
5808} addr_type_complete[] =
5809{
5810 {ADDR_ARGUMENTS, "arguments"},
5811 {ADDR_LINES, "lines"},
5812 {ADDR_LOADED_BUFFERS, "loaded_buffers"},
5813 {ADDR_TABS, "tabs"},
5814 {ADDR_BUFFERS, "buffers"},
5815 {ADDR_WINDOWS, "windows"},
5816 {-1, NULL}
5817};
5818#endif
5819
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005820#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005821/*
5822 * List of names for completion for ":command" with the EXPAND_ flag.
5823 * Must be alphabetical for completion.
5824 */
5825static struct
5826{
5827 int expand;
5828 char *name;
5829} command_complete[] =
5830{
5831 {EXPAND_AUGROUP, "augroup"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005832 {EXPAND_BEHAVE, "behave"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005833 {EXPAND_BUFFERS, "buffer"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005834 {EXPAND_COLORS, "color"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005835 {EXPAND_COMMANDS, "command"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005836 {EXPAND_COMPILER, "compiler"},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005837#if defined(FEAT_CSCOPE)
5838 {EXPAND_CSCOPE, "cscope"},
5839#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005840#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5841 {EXPAND_USER_DEFINED, "custom"},
Bram Moolenaara466c992005-07-09 21:03:22 +00005842 {EXPAND_USER_LIST, "customlist"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005843#endif
5844 {EXPAND_DIRECTORIES, "dir"},
5845 {EXPAND_ENV_VARS, "environment"},
5846 {EXPAND_EVENTS, "event"},
5847 {EXPAND_EXPRESSION, "expression"},
5848 {EXPAND_FILES, "file"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005849 {EXPAND_FILES_IN_PATH, "file_in_path"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005850 {EXPAND_FILETYPE, "filetype"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005851 {EXPAND_FUNCTIONS, "function"},
5852 {EXPAND_HELP, "help"},
5853 {EXPAND_HIGHLIGHT, "highlight"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005854#if defined(FEAT_CMDHIST)
5855 {EXPAND_HISTORY, "history"},
5856#endif
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005857#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005858 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005859 {EXPAND_LOCALES, "locale"},
5860#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005861 {EXPAND_MAPPINGS, "mapping"},
5862 {EXPAND_MENUS, "menu"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005863 {EXPAND_OWNSYNTAX, "syntax"},
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02005864#if defined(FEAT_PROFILE)
5865 {EXPAND_SYNTIME, "syntime"},
5866#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005867 {EXPAND_SETTINGS, "option"},
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005868 {EXPAND_SHELLCMD, "shellcmd"},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005869#if defined(FEAT_SIGNS)
5870 {EXPAND_SIGN, "sign"},
5871#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005872 {EXPAND_TAGS, "tag"},
5873 {EXPAND_TAGS_LISTFILES, "tag_listfiles"},
Bram Moolenaar24305862012-08-15 14:05:05 +02005874 {EXPAND_USER, "user"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005875 {EXPAND_USER_VARS, "var"},
5876 {0, NULL}
5877};
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005878#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005879
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005880#if defined(FEAT_USR_CMDS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005881 static void
5882uc_list(name, name_len)
5883 char_u *name;
5884 size_t name_len;
5885{
5886 int i, j;
5887 int found = FALSE;
5888 ucmd_T *cmd;
5889 int len;
5890 long a;
5891 garray_T *gap;
5892
5893 gap = &curbuf->b_ucmds;
5894 for (;;)
5895 {
5896 for (i = 0; i < gap->ga_len; ++i)
5897 {
5898 cmd = USER_CMD_GA(gap, i);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005899 a = (long)cmd->uc_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005900
5901 /* Skip commands which don't match the requested prefix */
5902 if (STRNCMP(name, cmd->uc_name, name_len) != 0)
5903 continue;
5904
5905 /* Put out the title first time */
5906 if (!found)
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005907 MSG_PUTS_TITLE(_("\n Name Args Address Complete Definition"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005908 found = TRUE;
5909 msg_putchar('\n');
5910 if (got_int)
5911 break;
5912
5913 /* Special cases */
5914 msg_putchar(a & BANG ? '!' : ' ');
5915 msg_putchar(a & REGSTR ? '"' : ' ');
5916 msg_putchar(gap != &ucmds ? 'b' : ' ');
5917 msg_putchar(' ');
5918
5919 msg_outtrans_attr(cmd->uc_name, hl_attr(HLF_D));
5920 len = (int)STRLEN(cmd->uc_name) + 4;
5921
5922 do {
5923 msg_putchar(' ');
5924 ++len;
5925 } while (len < 16);
5926
5927 len = 0;
5928
5929 /* Arguments */
5930 switch ((int)(a & (EXTRA|NOSPC|NEEDARG)))
5931 {
5932 case 0: IObuff[len++] = '0'; break;
5933 case (EXTRA): IObuff[len++] = '*'; break;
5934 case (EXTRA|NOSPC): IObuff[len++] = '?'; break;
5935 case (EXTRA|NEEDARG): IObuff[len++] = '+'; break;
5936 case (EXTRA|NOSPC|NEEDARG): IObuff[len++] = '1'; break;
5937 }
5938
5939 do {
5940 IObuff[len++] = ' ';
5941 } while (len < 5);
5942
5943 /* Range */
5944 if (a & (RANGE|COUNT))
5945 {
5946 if (a & COUNT)
5947 {
5948 /* -count=N */
5949 sprintf((char *)IObuff + len, "%ldc", cmd->uc_def);
5950 len += (int)STRLEN(IObuff + len);
5951 }
5952 else if (a & DFLALL)
5953 IObuff[len++] = '%';
5954 else if (cmd->uc_def >= 0)
5955 {
5956 /* -range=N */
5957 sprintf((char *)IObuff + len, "%ld", cmd->uc_def);
5958 len += (int)STRLEN(IObuff + len);
5959 }
5960 else
5961 IObuff[len++] = '.';
5962 }
5963
5964 do {
5965 IObuff[len++] = ' ';
5966 } while (len < 11);
5967
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005968 /* Address Type */
5969 for (j = 0; addr_type_complete[j].expand != -1; ++j)
5970 if (addr_type_complete[j].expand != ADDR_LINES
5971 && addr_type_complete[j].expand == cmd->uc_addr_type)
5972 {
5973 STRCPY(IObuff + len, addr_type_complete[j].name);
5974 len += (int)STRLEN(IObuff + len);
5975 break;
5976 }
5977
5978 do {
5979 IObuff[len++] = ' ';
5980 } while (len < 21);
5981
Bram Moolenaar071d4272004-06-13 20:20:40 +00005982 /* Completion */
5983 for (j = 0; command_complete[j].expand != 0; ++j)
5984 if (command_complete[j].expand == cmd->uc_compl)
5985 {
5986 STRCPY(IObuff + len, command_complete[j].name);
5987 len += (int)STRLEN(IObuff + len);
5988 break;
5989 }
5990
5991 do {
5992 IObuff[len++] = ' ';
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005993 } while (len < 35);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005994
5995 IObuff[len] = '\0';
5996 msg_outtrans(IObuff);
5997
5998 msg_outtrans_special(cmd->uc_rep, FALSE);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005999#ifdef FEAT_EVAL
6000 if (p_verbose > 0)
6001 last_set_msg(cmd->uc_scriptID);
6002#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006003 out_flush();
6004 ui_breakcheck();
6005 if (got_int)
6006 break;
6007 }
6008 if (gap == &ucmds || i < gap->ga_len)
6009 break;
6010 gap = &ucmds;
6011 }
6012
6013 if (!found)
6014 MSG(_("No user-defined commands found"));
6015}
6016
6017 static char_u *
6018uc_fun_cmd()
6019{
6020 static char_u fcmd[] = {0x84, 0xaf, 0x60, 0xb9, 0xaf, 0xb5, 0x60, 0xa4,
6021 0xa5, 0xad, 0xa1, 0xae, 0xa4, 0x60, 0xa1, 0x60,
6022 0xb3, 0xa8, 0xb2, 0xb5, 0xa2, 0xa2, 0xa5, 0xb2,
6023 0xb9, 0x7f, 0};
6024 int i;
6025
6026 for (i = 0; fcmd[i]; ++i)
6027 IObuff[i] = fcmd[i] - 0x40;
6028 IObuff[i] = 0;
6029 return IObuff;
6030}
6031
6032 static int
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006033uc_scan_attr(attr, len, argt, def, flags, compl, compl_arg, addr_type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006034 char_u *attr;
6035 size_t len;
6036 long *argt;
6037 long *def;
6038 int *flags;
6039 int *compl;
6040 char_u **compl_arg;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006041 int *addr_type_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006042{
6043 char_u *p;
6044
6045 if (len == 0)
6046 {
6047 EMSG(_("E175: No attribute specified"));
6048 return FAIL;
6049 }
6050
6051 /* First, try the simple attributes (no arguments) */
6052 if (STRNICMP(attr, "bang", len) == 0)
6053 *argt |= BANG;
6054 else if (STRNICMP(attr, "buffer", len) == 0)
6055 *flags |= UC_BUFFER;
6056 else if (STRNICMP(attr, "register", len) == 0)
6057 *argt |= REGSTR;
6058 else if (STRNICMP(attr, "bar", len) == 0)
6059 *argt |= TRLBAR;
6060 else
6061 {
6062 int i;
6063 char_u *val = NULL;
6064 size_t vallen = 0;
6065 size_t attrlen = len;
6066
6067 /* Look for the attribute name - which is the part before any '=' */
6068 for (i = 0; i < (int)len; ++i)
6069 {
6070 if (attr[i] == '=')
6071 {
6072 val = &attr[i + 1];
6073 vallen = len - i - 1;
6074 attrlen = i;
6075 break;
6076 }
6077 }
6078
6079 if (STRNICMP(attr, "nargs", attrlen) == 0)
6080 {
6081 if (vallen == 1)
6082 {
6083 if (*val == '0')
6084 /* Do nothing - this is the default */;
6085 else if (*val == '1')
6086 *argt |= (EXTRA | NOSPC | NEEDARG);
6087 else if (*val == '*')
6088 *argt |= EXTRA;
6089 else if (*val == '?')
6090 *argt |= (EXTRA | NOSPC);
6091 else if (*val == '+')
6092 *argt |= (EXTRA | NEEDARG);
6093 else
6094 goto wrong_nargs;
6095 }
6096 else
6097 {
6098wrong_nargs:
6099 EMSG(_("E176: Invalid number of arguments"));
6100 return FAIL;
6101 }
6102 }
6103 else if (STRNICMP(attr, "range", attrlen) == 0)
6104 {
6105 *argt |= RANGE;
6106 if (vallen == 1 && *val == '%')
6107 *argt |= DFLALL;
6108 else if (val != NULL)
6109 {
6110 p = val;
6111 if (*def >= 0)
6112 {
6113two_count:
6114 EMSG(_("E177: Count cannot be specified twice"));
6115 return FAIL;
6116 }
6117
6118 *def = getdigits(&p);
6119 *argt |= (ZEROR | NOTADR);
6120
6121 if (p != val + vallen || vallen == 0)
6122 {
6123invalid_count:
6124 EMSG(_("E178: Invalid default value for count"));
6125 return FAIL;
6126 }
6127 }
6128 }
6129 else if (STRNICMP(attr, "count", attrlen) == 0)
6130 {
Bram Moolenaar32e7b2d2005-02-27 22:36:47 +00006131 *argt |= (COUNT | ZEROR | RANGE | NOTADR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006132
6133 if (val != NULL)
6134 {
6135 p = val;
6136 if (*def >= 0)
6137 goto two_count;
6138
6139 *def = getdigits(&p);
6140
6141 if (p != val + vallen)
6142 goto invalid_count;
6143 }
6144
6145 if (*def < 0)
6146 *def = 0;
6147 }
6148 else if (STRNICMP(attr, "complete", attrlen) == 0)
6149 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006150 if (val == NULL)
6151 {
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006152 EMSG(_("E179: argument required for -complete"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006153 return FAIL;
6154 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006155
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006156 if (parse_compl_arg(val, (int)vallen, compl, argt, compl_arg)
6157 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006158 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006159 }
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006160 else if (STRNICMP(attr, "addr", attrlen) == 0)
6161 {
6162 *argt |= RANGE;
6163 if (val == NULL)
6164 {
6165 EMSG(_("E179: argument required for -addr"));
6166 return FAIL;
6167 }
6168 if (parse_addr_type_arg(val, (int)vallen, argt, addr_type_arg)
6169 == FAIL)
6170 return FAIL;
6171 if (addr_type_arg != ADDR_LINES)
6172 *argt |= (ZEROR | NOTADR) ;
6173 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006174 else
6175 {
6176 char_u ch = attr[len];
6177 attr[len] = '\0';
6178 EMSG2(_("E181: Invalid attribute: %s"), attr);
6179 attr[len] = ch;
6180 return FAIL;
6181 }
6182 }
6183
6184 return OK;
6185}
6186
Bram Moolenaara850a712009-01-28 14:42:59 +00006187/*
6188 * ":command ..."
6189 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006190 static void
6191ex_command(eap)
6192 exarg_T *eap;
6193{
6194 char_u *name;
6195 char_u *end;
6196 char_u *p;
6197 long argt = 0;
6198 long def = -1;
6199 int flags = 0;
6200 int compl = EXPAND_NOTHING;
6201 char_u *compl_arg = NULL;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006202 int addr_type_arg = ADDR_LINES;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006203 int has_attr = (eap->arg[0] == '-');
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006204 int name_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006205
6206 p = eap->arg;
6207
6208 /* Check for attributes */
6209 while (*p == '-')
6210 {
6211 ++p;
6212 end = skiptowhite(p);
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006213 if (uc_scan_attr(p, end - p, &argt, &def, &flags, &compl, &compl_arg, &addr_type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006214 == FAIL)
6215 return;
6216 p = skipwhite(end);
6217 }
6218
6219 /* Get the name (if any) and skip to the following argument */
6220 name = p;
6221 if (ASCII_ISALPHA(*p))
6222 while (ASCII_ISALNUM(*p))
6223 ++p;
6224 if (!ends_excmd(*p) && !vim_iswhite(*p))
6225 {
6226 EMSG(_("E182: Invalid command name"));
6227 return;
6228 }
6229 end = p;
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006230 name_len = (int)(end - name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006231
6232 /* If there is nothing after the name, and no attributes were specified,
6233 * we are listing commands
6234 */
6235 p = skipwhite(end);
6236 if (!has_attr && ends_excmd(*p))
6237 {
6238 uc_list(name, end - name);
6239 }
6240 else if (!ASCII_ISUPPER(*name))
6241 {
6242 EMSG(_("E183: User defined commands must start with an uppercase letter"));
6243 return;
6244 }
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006245 else if ((name_len == 1 && *name == 'X')
6246 || (name_len <= 4
6247 && STRNCMP(name, "Next", name_len > 4 ? 4 : name_len) == 0))
6248 {
6249 EMSG(_("E841: Reserved name, cannot be used for user defined command"));
6250 return;
6251 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006252 else
6253 uc_add_command(name, end - name, p, argt, def, flags, compl, compl_arg,
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006254 addr_type_arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006255}
6256
6257/*
6258 * ":comclear"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006259 * Clear all user commands, global and for current buffer.
6260 */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00006261 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00006262ex_comclear(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006263 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006264{
6265 uc_clear(&ucmds);
6266 uc_clear(&curbuf->b_ucmds);
6267}
6268
6269/*
6270 * Clear all user commands for "gap".
6271 */
6272 void
6273uc_clear(gap)
6274 garray_T *gap;
6275{
6276 int i;
6277 ucmd_T *cmd;
6278
6279 for (i = 0; i < gap->ga_len; ++i)
6280 {
6281 cmd = USER_CMD_GA(gap, i);
6282 vim_free(cmd->uc_name);
6283 vim_free(cmd->uc_rep);
6284# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6285 vim_free(cmd->uc_compl_arg);
6286# endif
6287 }
6288 ga_clear(gap);
6289}
6290
6291 static void
6292ex_delcommand(eap)
6293 exarg_T *eap;
6294{
6295 int i = 0;
6296 ucmd_T *cmd = NULL;
6297 int cmp = -1;
6298 garray_T *gap;
6299
6300 gap = &curbuf->b_ucmds;
6301 for (;;)
6302 {
6303 for (i = 0; i < gap->ga_len; ++i)
6304 {
6305 cmd = USER_CMD_GA(gap, i);
6306 cmp = STRCMP(eap->arg, cmd->uc_name);
6307 if (cmp <= 0)
6308 break;
6309 }
6310 if (gap == &ucmds || cmp == 0)
6311 break;
6312 gap = &ucmds;
6313 }
6314
6315 if (cmp != 0)
6316 {
6317 EMSG2(_("E184: No such user-defined command: %s"), eap->arg);
6318 return;
6319 }
6320
6321 vim_free(cmd->uc_name);
6322 vim_free(cmd->uc_rep);
6323# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6324 vim_free(cmd->uc_compl_arg);
6325# endif
6326
6327 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006328
6329 if (i < gap->ga_len)
6330 mch_memmove(cmd, cmd + 1, (gap->ga_len - i) * sizeof(ucmd_T));
6331}
6332
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006333/*
6334 * split and quote args for <f-args>
6335 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006336 static char_u *
6337uc_split_args(arg, lenp)
6338 char_u *arg;
6339 size_t *lenp;
6340{
6341 char_u *buf;
6342 char_u *p;
6343 char_u *q;
6344 int len;
6345
6346 /* Precalculate length */
6347 p = arg;
6348 len = 2; /* Initial and final quotes */
6349
6350 while (*p)
6351 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006352 if (p[0] == '\\' && p[1] == '\\')
6353 {
6354 len += 2;
6355 p += 2;
6356 }
6357 else if (p[0] == '\\' && vim_iswhite(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006358 {
6359 len += 1;
6360 p += 2;
6361 }
6362 else if (*p == '\\' || *p == '"')
6363 {
6364 len += 2;
6365 p += 1;
6366 }
6367 else if (vim_iswhite(*p))
6368 {
6369 p = skipwhite(p);
6370 if (*p == NUL)
6371 break;
6372 len += 3; /* "," */
6373 }
6374 else
6375 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02006376#ifdef FEAT_MBYTE
6377 int charlen = (*mb_ptr2len)(p);
6378 len += charlen;
6379 p += charlen;
6380#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006381 ++len;
6382 ++p;
Bram Moolenaardfef1542012-07-10 19:25:10 +02006383#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006384 }
6385 }
6386
6387 buf = alloc(len + 1);
6388 if (buf == NULL)
6389 {
6390 *lenp = 0;
6391 return buf;
6392 }
6393
6394 p = arg;
6395 q = buf;
6396 *q++ = '"';
6397 while (*p)
6398 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006399 if (p[0] == '\\' && p[1] == '\\')
6400 {
6401 *q++ = '\\';
6402 *q++ = '\\';
6403 p += 2;
6404 }
6405 else if (p[0] == '\\' && vim_iswhite(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006406 {
6407 *q++ = p[1];
6408 p += 2;
6409 }
6410 else if (*p == '\\' || *p == '"')
6411 {
6412 *q++ = '\\';
6413 *q++ = *p++;
6414 }
6415 else if (vim_iswhite(*p))
6416 {
6417 p = skipwhite(p);
6418 if (*p == NUL)
6419 break;
6420 *q++ = '"';
6421 *q++ = ',';
6422 *q++ = '"';
6423 }
6424 else
6425 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02006426 MB_COPY_CHAR(p, q);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006427 }
6428 }
6429 *q++ = '"';
6430 *q = 0;
6431
6432 *lenp = len;
6433 return buf;
6434}
6435
6436/*
6437 * Check for a <> code in a user command.
6438 * "code" points to the '<'. "len" the length of the <> (inclusive).
6439 * "buf" is where the result is to be added.
6440 * "split_buf" points to a buffer used for splitting, caller should free it.
6441 * "split_len" is the length of what "split_buf" contains.
6442 * Returns the length of the replacement, which has been added to "buf".
6443 * Returns -1 if there was no match, and only the "<" has been copied.
6444 */
6445 static size_t
6446uc_check_code(code, len, buf, cmd, eap, split_buf, split_len)
6447 char_u *code;
6448 size_t len;
6449 char_u *buf;
6450 ucmd_T *cmd; /* the user command we're expanding */
6451 exarg_T *eap; /* ex arguments */
6452 char_u **split_buf;
6453 size_t *split_len;
6454{
6455 size_t result = 0;
6456 char_u *p = code + 1;
6457 size_t l = len - 2;
6458 int quote = 0;
6459 enum { ct_ARGS, ct_BANG, ct_COUNT, ct_LINE1, ct_LINE2, ct_REGISTER,
6460 ct_LT, ct_NONE } type = ct_NONE;
6461
6462 if ((vim_strchr((char_u *)"qQfF", *p) != NULL) && p[1] == '-')
6463 {
6464 quote = (*p == 'q' || *p == 'Q') ? 1 : 2;
6465 p += 2;
6466 l -= 2;
6467 }
6468
Bram Moolenaar371d5402006-03-20 21:47:49 +00006469 ++l;
6470 if (l <= 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006471 type = ct_NONE;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006472 else if (STRNICMP(p, "args>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006473 type = ct_ARGS;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006474 else if (STRNICMP(p, "bang>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006475 type = ct_BANG;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006476 else if (STRNICMP(p, "count>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006477 type = ct_COUNT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006478 else if (STRNICMP(p, "line1>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006479 type = ct_LINE1;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006480 else if (STRNICMP(p, "line2>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006481 type = ct_LINE2;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006482 else if (STRNICMP(p, "lt>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006483 type = ct_LT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006484 else if (STRNICMP(p, "reg>", l) == 0 || STRNICMP(p, "register>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006485 type = ct_REGISTER;
6486
6487 switch (type)
6488 {
6489 case ct_ARGS:
6490 /* Simple case first */
6491 if (*eap->arg == NUL)
6492 {
6493 if (quote == 1)
6494 {
6495 result = 2;
6496 if (buf != NULL)
6497 STRCPY(buf, "''");
6498 }
6499 else
6500 result = 0;
6501 break;
6502 }
6503
6504 /* When specified there is a single argument don't split it.
6505 * Works for ":Cmd %" when % is "a b c". */
6506 if ((eap->argt & NOSPC) && quote == 2)
6507 quote = 1;
6508
6509 switch (quote)
6510 {
6511 case 0: /* No quoting, no splitting */
6512 result = STRLEN(eap->arg);
6513 if (buf != NULL)
6514 STRCPY(buf, eap->arg);
6515 break;
6516 case 1: /* Quote, but don't split */
6517 result = STRLEN(eap->arg) + 2;
6518 for (p = eap->arg; *p; ++p)
6519 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006520#ifdef FEAT_MBYTE
6521 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6522 /* DBCS can contain \ in a trail byte, skip the
6523 * double-byte character. */
6524 ++p;
6525 else
6526#endif
6527 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006528 ++result;
6529 }
6530
6531 if (buf != NULL)
6532 {
6533 *buf++ = '"';
6534 for (p = eap->arg; *p; ++p)
6535 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006536#ifdef FEAT_MBYTE
6537 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6538 /* DBCS can contain \ in a trail byte, copy the
6539 * double-byte character to avoid escaping. */
6540 *buf++ = *p++;
6541 else
6542#endif
6543 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006544 *buf++ = '\\';
6545 *buf++ = *p;
6546 }
6547 *buf = '"';
6548 }
6549
6550 break;
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006551 case 2: /* Quote and split (<f-args>) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006552 /* This is hard, so only do it once, and cache the result */
6553 if (*split_buf == NULL)
6554 *split_buf = uc_split_args(eap->arg, split_len);
6555
6556 result = *split_len;
6557 if (buf != NULL && result != 0)
6558 STRCPY(buf, *split_buf);
6559
6560 break;
6561 }
6562 break;
6563
6564 case ct_BANG:
6565 result = eap->forceit ? 1 : 0;
6566 if (quote)
6567 result += 2;
6568 if (buf != NULL)
6569 {
6570 if (quote)
6571 *buf++ = '"';
6572 if (eap->forceit)
6573 *buf++ = '!';
6574 if (quote)
6575 *buf = '"';
6576 }
6577 break;
6578
6579 case ct_LINE1:
6580 case ct_LINE2:
6581 case ct_COUNT:
6582 {
6583 char num_buf[20];
6584 long num = (type == ct_LINE1) ? eap->line1 :
6585 (type == ct_LINE2) ? eap->line2 :
6586 (eap->addr_count > 0) ? eap->line2 : cmd->uc_def;
6587 size_t num_len;
6588
6589 sprintf(num_buf, "%ld", num);
6590 num_len = STRLEN(num_buf);
6591 result = num_len;
6592
6593 if (quote)
6594 result += 2;
6595
6596 if (buf != NULL)
6597 {
6598 if (quote)
6599 *buf++ = '"';
6600 STRCPY(buf, num_buf);
6601 buf += num_len;
6602 if (quote)
6603 *buf = '"';
6604 }
6605
6606 break;
6607 }
6608
6609 case ct_REGISTER:
6610 result = eap->regname ? 1 : 0;
6611 if (quote)
6612 result += 2;
6613 if (buf != NULL)
6614 {
6615 if (quote)
6616 *buf++ = '\'';
6617 if (eap->regname)
6618 *buf++ = eap->regname;
6619 if (quote)
6620 *buf = '\'';
6621 }
6622 break;
6623
6624 case ct_LT:
6625 result = 1;
6626 if (buf != NULL)
6627 *buf = '<';
6628 break;
6629
6630 default:
6631 /* Not recognized: just copy the '<' and return -1. */
6632 result = (size_t)-1;
6633 if (buf != NULL)
6634 *buf = '<';
6635 break;
6636 }
6637
6638 return result;
6639}
6640
6641 static void
6642do_ucmd(eap)
6643 exarg_T *eap;
6644{
6645 char_u *buf;
6646 char_u *p;
6647 char_u *q;
6648
6649 char_u *start;
Bram Moolenaar25648a52009-02-21 19:37:46 +00006650 char_u *end = NULL;
Bram Moolenaara850a712009-01-28 14:42:59 +00006651 char_u *ksp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006652 size_t len, totlen;
6653
6654 size_t split_len = 0;
6655 char_u *split_buf = NULL;
6656 ucmd_T *cmd;
6657#ifdef FEAT_EVAL
6658 scid_T save_current_SID = current_SID;
6659#endif
6660
6661 if (eap->cmdidx == CMD_USER)
6662 cmd = USER_CMD(eap->useridx);
6663 else
6664 cmd = USER_CMD_GA(&curbuf->b_ucmds, eap->useridx);
6665
6666 /*
6667 * Replace <> in the command by the arguments.
Bram Moolenaara850a712009-01-28 14:42:59 +00006668 * First round: "buf" is NULL, compute length, allocate "buf".
6669 * Second round: copy result into "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006670 */
6671 buf = NULL;
6672 for (;;)
6673 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006674 p = cmd->uc_rep; /* source */
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00006675 q = buf; /* destination */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006676 totlen = 0;
Bram Moolenaara850a712009-01-28 14:42:59 +00006677
6678 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006679 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006680 start = vim_strchr(p, '<');
6681 if (start != NULL)
6682 end = vim_strchr(start + 1, '>');
6683 if (buf != NULL)
6684 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006685 for (ksp = p; *ksp != NUL && *ksp != K_SPECIAL; ++ksp)
6686 ;
6687 if (*ksp == K_SPECIAL
6688 && (start == NULL || ksp < start || end == NULL)
Bram Moolenaara850a712009-01-28 14:42:59 +00006689 && ((ksp[1] == KS_SPECIAL && ksp[2] == KE_FILLER)
6690# ifdef FEAT_GUI
6691 || (ksp[1] == KS_EXTRA && ksp[2] == (int)KE_CSI)
6692# endif
6693 ))
6694 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006695 /* K_SPECIAL has been put in the buffer as K_SPECIAL
Bram Moolenaara850a712009-01-28 14:42:59 +00006696 * KS_SPECIAL KE_FILLER, like for mappings, but
6697 * do_cmdline() doesn't handle that, so convert it back.
6698 * Also change K_SPECIAL KS_EXTRA KE_CSI into CSI. */
6699 len = ksp - p;
6700 if (len > 0)
6701 {
6702 mch_memmove(q, p, len);
6703 q += len;
6704 }
6705 *q++ = ksp[1] == KS_SPECIAL ? K_SPECIAL : CSI;
6706 p = ksp + 3;
6707 continue;
6708 }
6709 }
6710
6711 /* break if there no <item> is found */
6712 if (start == NULL || end == NULL)
6713 break;
6714
Bram Moolenaar071d4272004-06-13 20:20:40 +00006715 /* Include the '>' */
6716 ++end;
6717
6718 /* Take everything up to the '<' */
6719 len = start - p;
6720 if (buf == NULL)
6721 totlen += len;
6722 else
6723 {
6724 mch_memmove(q, p, len);
6725 q += len;
6726 }
6727
6728 len = uc_check_code(start, end - start, q, cmd, eap,
6729 &split_buf, &split_len);
6730 if (len == (size_t)-1)
6731 {
6732 /* no match, continue after '<' */
6733 p = start + 1;
6734 len = 1;
6735 }
6736 else
6737 p = end;
6738 if (buf == NULL)
6739 totlen += len;
6740 else
6741 q += len;
6742 }
6743 if (buf != NULL) /* second time here, finished */
6744 {
6745 STRCPY(q, p);
6746 break;
6747 }
6748
6749 totlen += STRLEN(p); /* Add on the trailing characters */
6750 buf = alloc((unsigned)(totlen + 1));
6751 if (buf == NULL)
6752 {
6753 vim_free(split_buf);
6754 return;
6755 }
6756 }
6757
6758#ifdef FEAT_EVAL
6759 current_SID = cmd->uc_scriptID;
6760#endif
6761 (void)do_cmdline(buf, eap->getline, eap->cookie,
6762 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
6763#ifdef FEAT_EVAL
6764 current_SID = save_current_SID;
6765#endif
6766 vim_free(buf);
6767 vim_free(split_buf);
6768}
6769
6770# if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6771 static char_u *
6772get_user_command_name(idx)
6773 int idx;
6774{
6775 return get_user_commands(NULL, idx - (int)CMD_SIZE);
6776}
6777
6778/*
6779 * Function given to ExpandGeneric() to obtain the list of user command names.
6780 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006781 char_u *
6782get_user_commands(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006783 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006784 int idx;
6785{
6786 if (idx < curbuf->b_ucmds.ga_len)
6787 return USER_CMD_GA(&curbuf->b_ucmds, idx)->uc_name;
6788 idx -= curbuf->b_ucmds.ga_len;
6789 if (idx < ucmds.ga_len)
6790 return USER_CMD(idx)->uc_name;
6791 return NULL;
6792}
6793
6794/*
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006795 * Function given to ExpandGeneric() to obtain the list of user address type names.
6796 */
6797 char_u *
6798get_user_cmd_addr_type(xp, idx)
6799 expand_T *xp UNUSED;
6800 int idx;
6801{
6802 return (char_u *)addr_type_complete[idx].name;
6803}
6804
6805/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006806 * Function given to ExpandGeneric() to obtain the list of user command
6807 * attributes.
6808 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006809 char_u *
6810get_user_cmd_flags(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006811 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006812 int idx;
6813{
6814 static char *user_cmd_flags[] =
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006815 {"addr", "bang", "bar", "buffer", "complete",
6816 "count", "nargs", "range", "register"};
Bram Moolenaar071d4272004-06-13 20:20:40 +00006817
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00006818 if (idx >= (int)(sizeof(user_cmd_flags) / sizeof(user_cmd_flags[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006819 return NULL;
6820 return (char_u *)user_cmd_flags[idx];
6821}
6822
6823/*
6824 * Function given to ExpandGeneric() to obtain the list of values for -nargs.
6825 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006826 char_u *
6827get_user_cmd_nargs(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006828 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006829 int idx;
6830{
6831 static char *user_cmd_nargs[] = {"0", "1", "*", "?", "+"};
6832
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00006833 if (idx >= (int)(sizeof(user_cmd_nargs) / sizeof(user_cmd_nargs[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006834 return NULL;
6835 return (char_u *)user_cmd_nargs[idx];
6836}
6837
6838/*
6839 * Function given to ExpandGeneric() to obtain the list of values for -complete.
6840 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006841 char_u *
6842get_user_cmd_complete(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006843 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006844 int idx;
6845{
6846 return (char_u *)command_complete[idx].name;
6847}
6848# endif /* FEAT_CMDL_COMPL */
6849
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006850/*
6851 * Parse address type argument
6852 */
6853 int
6854parse_addr_type_arg(value, vallen, argt, addr_type_arg)
6855 char_u *value;
6856 int vallen;
6857 long *argt;
6858 int *addr_type_arg;
6859{
6860 int i, a, b;
6861 for (i = 0; addr_type_complete[i].expand != -1; ++i)
6862 {
6863 a = (int)STRLEN(addr_type_complete[i].name) == vallen;
6864 b = STRNCMP(value, addr_type_complete[i].name, vallen) == 0;
6865 if (a && b)
6866 {
6867 *addr_type_arg = addr_type_complete[i].expand;
6868 break;
6869 }
6870 }
6871
6872 if (addr_type_complete[i].expand == -1)
6873 {
6874 char_u *err = value;
6875 for (i=0; err[i] == NUL || !vim_iswhite(err[i]); i++);
6876 err[i] = NUL;
6877 EMSG2(_("E180: Invalid address type value: %s"), err);
6878 return FAIL;
6879 }
6880
6881 if (*addr_type_arg != ADDR_LINES)
6882 *argt |= NOTADR;
6883
6884 return OK;
6885}
6886
Bram Moolenaar071d4272004-06-13 20:20:40 +00006887#endif /* FEAT_USR_CMDS */
6888
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006889#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
6890/*
6891 * Parse a completion argument "value[vallen]".
6892 * The detected completion goes in "*complp", argument type in "*argt".
6893 * When there is an argument, for function and user defined completion, it's
6894 * copied to allocated memory and stored in "*compl_arg".
6895 * Returns FAIL if something is wrong.
6896 */
6897 int
6898parse_compl_arg(value, vallen, complp, argt, compl_arg)
6899 char_u *value;
6900 int vallen;
6901 int *complp;
6902 long *argt;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006903 char_u **compl_arg UNUSED;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006904{
6905 char_u *arg = NULL;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006906# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006907 size_t arglen = 0;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006908# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006909 int i;
6910 int valend = vallen;
6911
6912 /* Look for any argument part - which is the part after any ',' */
6913 for (i = 0; i < vallen; ++i)
6914 {
6915 if (value[i] == ',')
6916 {
6917 arg = &value[i + 1];
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006918# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006919 arglen = vallen - i - 1;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006920# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006921 valend = i;
6922 break;
6923 }
6924 }
6925
6926 for (i = 0; command_complete[i].expand != 0; ++i)
6927 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006928 if ((int)STRLEN(command_complete[i].name) == valend
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006929 && STRNCMP(value, command_complete[i].name, valend) == 0)
6930 {
6931 *complp = command_complete[i].expand;
6932 if (command_complete[i].expand == EXPAND_BUFFERS)
6933 *argt |= BUFNAME;
6934 else if (command_complete[i].expand == EXPAND_DIRECTORIES
6935 || command_complete[i].expand == EXPAND_FILES)
6936 *argt |= XFILE;
6937 break;
6938 }
6939 }
6940
6941 if (command_complete[i].expand == 0)
6942 {
6943 EMSG2(_("E180: Invalid complete value: %s"), value);
6944 return FAIL;
6945 }
6946
6947# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6948 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST
6949 && arg != NULL)
6950# else
6951 if (arg != NULL)
6952# endif
6953 {
6954 EMSG(_("E468: Completion argument only allowed for custom completion"));
6955 return FAIL;
6956 }
6957
6958# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6959 if ((*complp == EXPAND_USER_DEFINED || *complp == EXPAND_USER_LIST)
6960 && arg == NULL)
6961 {
6962 EMSG(_("E467: Custom completion requires a function argument"));
6963 return FAIL;
6964 }
6965
6966 if (arg != NULL)
6967 *compl_arg = vim_strnsave(arg, (int)arglen);
6968# endif
6969 return OK;
6970}
6971#endif
6972
Bram Moolenaar071d4272004-06-13 20:20:40 +00006973 static void
6974ex_colorscheme(eap)
6975 exarg_T *eap;
6976{
Bram Moolenaare6850792010-05-14 15:28:44 +02006977 if (*eap->arg == NUL)
6978 {
6979#ifdef FEAT_EVAL
6980 char_u *expr = vim_strsave((char_u *)"g:colors_name");
6981 char_u *p = NULL;
6982
6983 if (expr != NULL)
6984 {
6985 ++emsg_off;
6986 p = eval_to_string(expr, NULL, FALSE);
6987 --emsg_off;
6988 vim_free(expr);
6989 }
6990 if (p != NULL)
6991 {
6992 MSG(p);
6993 vim_free(p);
6994 }
6995 else
6996 MSG("default");
6997#else
6998 MSG(_("unknown"));
6999#endif
7000 }
7001 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarbe1e9e92012-09-18 16:47:07 +02007002 EMSG2(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007003}
7004
7005 static void
7006ex_highlight(eap)
7007 exarg_T *eap;
7008{
7009 if (*eap->arg == NUL && eap->cmd[2] == '!')
7010 MSG(_("Greetings, Vim user!"));
7011 do_highlight(eap->arg, eap->forceit, FALSE);
7012}
7013
7014
7015/*
7016 * Call this function if we thought we were going to exit, but we won't
7017 * (because of an error). May need to restore the terminal mode.
7018 */
7019 void
7020not_exiting()
7021{
7022 exiting = FALSE;
7023 settmode(TMODE_RAW);
7024}
7025
7026/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01007027 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007028 */
7029 static void
7030ex_quit(eap)
7031 exarg_T *eap;
7032{
Bram Moolenaarf240e182014-11-27 18:33:02 +01007033#if defined(FEAT_WINDOWS) || defined(FEAT_AUTOCMD)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007034 win_T *wp;
Bram Moolenaarf240e182014-11-27 18:33:02 +01007035#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007036
Bram Moolenaar071d4272004-06-13 20:20:40 +00007037#ifdef FEAT_CMDWIN
7038 if (cmdwin_type != 0)
7039 {
7040 cmdwin_result = Ctrl_C;
7041 return;
7042 }
7043#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007044 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007045 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007046 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007047 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007048 return;
7049 }
Bram Moolenaarf240e182014-11-27 18:33:02 +01007050#ifdef FEAT_WINDOWS
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007051 if (eap->addr_count > 0)
7052 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01007053 int wnr = eap->line2;
7054
7055 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
7056 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007057 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007058 }
7059 else
Bram Moolenaarf240e182014-11-27 18:33:02 +01007060#endif
7061#if defined(FEAT_WINDOWS) || defined(FEAT_AUTOCMD)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007062 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01007063#endif
7064
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007065#ifdef FEAT_AUTOCMD
Bram Moolenaar3b53dfb2012-06-06 18:03:07 +02007066 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01007067 /* Refuse to quit when locked or when the buffer in the last window is
Bram Moolenaar362ce482012-06-06 19:02:45 +02007068 * being closed (can only happen in autocommands). */
Bram Moolenaarf240e182014-11-27 18:33:02 +01007069 if (curbuf_locked() || (wp->w_buffer->b_nwindows == 1
7070 && wp->w_buffer->b_closing))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007071 return;
7072#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007073
7074#ifdef FEAT_NETBEANS_INTG
7075 netbeansForcedQuit = eap->forceit;
7076#endif
7077
7078 /*
7079 * If there are more files or windows we won't exit.
7080 */
7081 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
7082 exiting = TRUE;
7083 if ((!P_HID(curbuf)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01007084 && check_changed(curbuf, (p_awa ? CCGD_AW : 0)
7085 | (eap->forceit ? CCGD_FORCEIT : 0)
7086 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007087 || check_more(TRUE, eap->forceit) == FAIL
7088 || (only_one_window() && check_changed_any(eap->forceit)))
7089 {
7090 not_exiting();
7091 }
7092 else
7093 {
7094#ifdef FEAT_WINDOWS
7095 if (only_one_window()) /* quit last window */
7096#endif
7097 getout(0);
7098#ifdef FEAT_WINDOWS
7099# ifdef FEAT_GUI
7100 need_mouse_correct = TRUE;
7101# endif
7102 /* close window; may free buffer */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007103 win_close(wp, !P_HID(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007104#endif
7105 }
7106}
7107
7108/*
7109 * ":cquit".
7110 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007111 static void
7112ex_cquit(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007113 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007114{
7115 getout(1); /* this does not always pass on the exit code to the Manx
7116 compiler. why? */
7117}
7118
7119/*
7120 * ":qall": try to quit all windows
7121 */
7122 static void
7123ex_quit_all(eap)
7124 exarg_T *eap;
7125{
7126# ifdef FEAT_CMDWIN
7127 if (cmdwin_type != 0)
7128 {
7129 if (eap->forceit)
7130 cmdwin_result = K_XF1; /* ex_window() takes care of this */
7131 else
7132 cmdwin_result = K_XF2;
7133 return;
7134 }
7135# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007136
7137 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007138 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007139 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007140 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007141 return;
7142 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007143#ifdef FEAT_AUTOCMD
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01007144 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
7145 /* Refuse to quit when locked or when the buffer in the last window is
7146 * being closed (can only happen in autocommands). */
7147 if (curbuf_locked() || (curbuf->b_nwindows == 1 && curbuf->b_closing))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007148 return;
7149#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007150
Bram Moolenaar071d4272004-06-13 20:20:40 +00007151 exiting = TRUE;
7152 if (eap->forceit || !check_changed_any(FALSE))
7153 getout(0);
7154 not_exiting();
7155}
7156
Bram Moolenaard9967712006-03-11 21:18:15 +00007157#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007158/*
7159 * ":close": close current window, unless it is the last one
7160 */
7161 static void
7162ex_close(eap)
7163 exarg_T *eap;
7164{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007165 win_T *win;
7166 int winnr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007167# ifdef FEAT_CMDWIN
7168 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02007169 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007170 else
7171# endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007172 if (!text_locked()
7173#ifdef FEAT_AUTOCMD
7174 && !curbuf_locked()
7175#endif
7176 )
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007177 {
7178 if (eap->addr_count == 0)
7179 ex_win_close(eap->forceit, curwin, NULL);
7180 else {
7181 for (win = firstwin; win != NULL; win = win->w_next)
7182 {
7183 winnr++;
7184 if (winnr == eap->line2)
7185 break;
7186 }
7187 if (win == NULL)
7188 win = lastwin;
7189 ex_win_close(eap->forceit, win, NULL);
7190 }
7191 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007192}
7193
Bram Moolenaard9967712006-03-11 21:18:15 +00007194# ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007195/*
7196 * ":pclose": Close any preview window.
7197 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007198 static void
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007199ex_pclose(eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007200 exarg_T *eap;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007201{
7202 win_T *win;
7203
7204 for (win = firstwin; win != NULL; win = win->w_next)
7205 if (win->w_p_pvw)
7206 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00007207 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007208 break;
7209 }
7210}
Bram Moolenaard9967712006-03-11 21:18:15 +00007211# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007212
Bram Moolenaarf740b292006-02-16 22:11:02 +00007213/*
7214 * Close window "win" and take care of handling closing the last window for a
7215 * modified buffer.
7216 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007217 static void
Bram Moolenaarf740b292006-02-16 22:11:02 +00007218ex_win_close(forceit, win, tp)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007219 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007220 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007221 tabpage_T *tp; /* NULL or the tab page "win" is in */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007222{
7223 int need_hide;
7224 buf_T *buf = win->w_buffer;
7225
7226 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007227 if (need_hide && !P_HID(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007228 {
Bram Moolenaard9967712006-03-11 21:18:15 +00007229# if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007230 if ((p_confirm || cmdmod.confirm) && p_write)
7231 {
7232 dialog_changed(buf, FALSE);
7233 if (buf_valid(buf) && bufIsChanged(buf))
7234 return;
7235 need_hide = FALSE;
7236 }
7237 else
Bram Moolenaard9967712006-03-11 21:18:15 +00007238# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007239 {
7240 EMSG(_(e_nowrtmsg));
7241 return;
7242 }
7243 }
7244
Bram Moolenaard9967712006-03-11 21:18:15 +00007245# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007246 need_mouse_correct = TRUE;
Bram Moolenaard9967712006-03-11 21:18:15 +00007247# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00007248
Bram Moolenaar071d4272004-06-13 20:20:40 +00007249 /* free buffer when not hiding it or when it's a scratch buffer */
Bram Moolenaarf740b292006-02-16 22:11:02 +00007250 if (tp == NULL)
7251 win_close(win, !need_hide && !P_HID(buf));
7252 else
7253 win_close_othertab(win, !need_hide && !P_HID(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007254}
7255
Bram Moolenaar071d4272004-06-13 20:20:40 +00007256/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00007257 * ":tabclose": close current tab page, unless it is the last one.
7258 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007259 */
7260 static void
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007261ex_tabclose(eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007262 exarg_T *eap;
7263{
Bram Moolenaarf740b292006-02-16 22:11:02 +00007264 tabpage_T *tp;
7265
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007266# ifdef FEAT_CMDWIN
7267 if (cmdwin_type != 0)
7268 cmdwin_result = K_IGNORE;
7269 else
7270# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00007271 if (first_tabpage->tp_next == NULL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007272 EMSG(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00007273 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007274 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00007275 if (eap->addr_count > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007276 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00007277 tp = find_tabpage((int)eap->line2);
7278 if (tp == NULL)
7279 {
7280 beep_flush();
7281 return;
7282 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007283 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007284 {
7285 tabpage_close_other(tp, eap->forceit);
7286 return;
7287 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007288 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007289 if (!text_locked()
7290#ifdef FEAT_AUTOCMD
7291 && !curbuf_locked()
7292#endif
7293 )
Bram Moolenaarf740b292006-02-16 22:11:02 +00007294 tabpage_close(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007295 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007296}
7297
7298/*
7299 * ":tabonly": close all tab pages except the current one
7300 */
7301 static void
7302ex_tabonly(eap)
7303 exarg_T *eap;
7304{
7305 tabpage_T *tp;
7306 int done;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007307
7308# ifdef FEAT_CMDWIN
7309 if (cmdwin_type != 0)
7310 cmdwin_result = K_IGNORE;
7311 else
7312# endif
7313 if (first_tabpage->tp_next == NULL)
7314 MSG(_("Already only one tab page"));
7315 else
7316 {
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007317 if (eap->addr_count > 0)
7318 goto_tabpage(eap->line2);
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007319 /* Repeat this up to a 1000 times, because autocommands may mess
7320 * up the lists. */
7321 for (done = 0; done < 1000; ++done)
7322 {
7323 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
7324 if (tp->tp_topframe != topframe)
7325 {
7326 tabpage_close_other(tp, eap->forceit);
7327 /* if we failed to close it quit */
7328 if (valid_tabpage(tp))
7329 done = 1000;
7330 /* start over, "tp" is now invalid */
7331 break;
7332 }
7333 if (first_tabpage->tp_next == NULL)
7334 break;
7335 }
7336 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007337}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007338
7339/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00007340 * Close the current tab page.
7341 */
7342 void
7343tabpage_close(forceit)
7344 int forceit;
7345{
7346 /* First close all the windows but the current one. If that worked then
7347 * close the last window in this tab, that will close it. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007348 if (lastwin != firstwin)
7349 close_others(TRUE, forceit);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007350 if (lastwin == firstwin)
7351 ex_win_close(forceit, curwin, NULL);
7352# ifdef FEAT_GUI
7353 need_mouse_correct = TRUE;
7354# endif
7355}
7356
7357/*
7358 * Close tab page "tp", which is not the current tab page.
7359 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007360 * Also takes care of the tab pages line disappearing when closing the
7361 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00007362 */
7363 void
7364tabpage_close_other(tp, forceit)
7365 tabpage_T *tp;
7366 int forceit;
7367{
7368 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007369 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007370 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00007371
7372 /* Limit to 1000 windows, autocommands may add a window while we close
7373 * one. OK, so I'm paranoid... */
7374 while (++done < 1000)
7375 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007376 wp = tp->tp_firstwin;
7377 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007378
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007379 /* Autocommands may delete the tab page under our fingers and we may
7380 * fail to close a window with a modified buffer. */
7381 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007382 break;
7383 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007384
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007385 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007386 if (h != tabline_height())
7387 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00007388}
7389
7390/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007391 * ":only".
7392 */
7393 static void
7394ex_only(eap)
7395 exarg_T *eap;
7396{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007397 win_T *wp;
7398 int wnr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007399# ifdef FEAT_GUI
7400 need_mouse_correct = TRUE;
7401# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007402 if (eap->addr_count > 0)
7403 {
7404 wnr = eap->line2;
7405 for (wp = firstwin; --wnr > 0; )
7406 {
7407 if (wp->w_next == NULL)
7408 break;
7409 else
7410 wp = wp->w_next;
7411 }
7412 win_goto(wp);
7413 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007414 close_others(TRUE, eap->forceit);
7415}
7416
7417/*
7418 * ":all" and ":sall".
Bram Moolenaard9967712006-03-11 21:18:15 +00007419 * Also used for ":tab drop file ..." after setting the argument list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007420 */
Bram Moolenaard9967712006-03-11 21:18:15 +00007421 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00007422ex_all(eap)
7423 exarg_T *eap;
7424{
7425 if (eap->addr_count == 0)
7426 eap->line2 = 9999;
Bram Moolenaard9967712006-03-11 21:18:15 +00007427 do_arg_all((int)eap->line2, eap->forceit, eap->cmdidx == CMD_drop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007428}
7429#endif /* FEAT_WINDOWS */
7430
7431 static void
7432ex_hide(eap)
7433 exarg_T *eap;
7434{
7435 if (*eap->arg != NUL && check_nextcmd(eap->arg) == NULL)
7436 eap->errmsg = e_invarg;
7437 else
7438 {
7439 /* ":hide" or ":hide | cmd": hide current window */
7440 eap->nextcmd = check_nextcmd(eap->arg);
7441#ifdef FEAT_WINDOWS
7442 if (!eap->skip)
7443 {
7444# ifdef FEAT_GUI
7445 need_mouse_correct = TRUE;
7446# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007447 if (eap->addr_count == 0)
7448 win_close(curwin, FALSE); /* don't free buffer */
Bram Moolenaarf240e182014-11-27 18:33:02 +01007449 else
7450 {
7451 int winnr = 0;
7452 win_T *win;
7453
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007454 for (win = firstwin; win != NULL; win = win->w_next)
7455 {
7456 winnr++;
7457 if (winnr == eap->line2)
7458 break;
7459 }
7460 if (win == NULL)
7461 win = lastwin;
7462 win_close(win, FALSE);
7463 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007464 }
7465#endif
7466 }
7467}
7468
7469/*
7470 * ":stop" and ":suspend": Suspend Vim.
7471 */
7472 static void
7473ex_stop(eap)
7474 exarg_T *eap;
7475{
7476 /*
7477 * Disallow suspending for "rvim".
7478 */
7479 if (!check_restricted()
7480#ifdef WIN3264
7481 /*
7482 * Check if external commands are allowed now.
7483 */
7484 && can_end_termcap_mode(TRUE)
7485#endif
7486 )
7487 {
7488 if (!eap->forceit)
7489 autowrite_all();
7490 windgoto((int)Rows - 1, 0);
7491 out_char('\n');
7492 out_flush();
7493 stoptermcap();
7494 out_flush(); /* needed for SUN to restore xterm buffer */
7495#ifdef FEAT_TITLE
7496 mch_restore_title(3); /* restore window titles */
7497#endif
7498 ui_suspend(); /* call machine specific function */
7499#ifdef FEAT_TITLE
7500 maketitle();
7501 resettitle(); /* force updating the title */
7502#endif
7503 starttermcap();
7504 scroll_start(); /* scroll screen before redrawing */
7505 redraw_later_clear();
7506 shell_resized(); /* may have resized window */
7507 }
7508}
7509
7510/*
7511 * ":exit", ":xit" and ":wq": Write file and exit Vim.
7512 */
7513 static void
7514ex_exit(eap)
7515 exarg_T *eap;
7516{
7517#ifdef FEAT_CMDWIN
7518 if (cmdwin_type != 0)
7519 {
7520 cmdwin_result = Ctrl_C;
7521 return;
7522 }
7523#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007524 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007525 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007526 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007527 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007528 return;
7529 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007530#ifdef FEAT_AUTOCMD
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01007531 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
7532 /* Refuse to quit when locked or when the buffer in the last window is
7533 * being closed (can only happen in autocommands). */
7534 if (curbuf_locked() || (curbuf->b_nwindows == 1 && curbuf->b_closing))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007535 return;
7536#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007537
7538 /*
7539 * if more files or windows we won't exit
7540 */
7541 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
7542 exiting = TRUE;
7543 if ( ((eap->cmdidx == CMD_wq
7544 || curbufIsChanged())
7545 && do_write(eap) == FAIL)
7546 || check_more(TRUE, eap->forceit) == FAIL
7547 || (only_one_window() && check_changed_any(eap->forceit)))
7548 {
7549 not_exiting();
7550 }
7551 else
7552 {
7553#ifdef FEAT_WINDOWS
7554 if (only_one_window()) /* quit last window, exit Vim */
7555#endif
7556 getout(0);
7557#ifdef FEAT_WINDOWS
7558# ifdef FEAT_GUI
7559 need_mouse_correct = TRUE;
7560# endif
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02007561 /* Quit current window, may free the buffer. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007562 win_close(curwin, !P_HID(curwin->w_buffer));
7563#endif
7564 }
7565}
7566
7567/*
7568 * ":print", ":list", ":number".
7569 */
7570 static void
7571ex_print(eap)
7572 exarg_T *eap;
7573{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007574 if (curbuf->b_ml.ml_flags & ML_EMPTY)
7575 EMSG(_(e_emptybuf));
7576 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007577 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007578 for ( ;!got_int; ui_breakcheck())
7579 {
7580 print_line(eap->line1,
7581 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
7582 || (eap->flags & EXFLAG_NR)),
7583 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
7584 if (++eap->line1 > eap->line2)
7585 break;
7586 out_flush(); /* show one line at a time */
7587 }
7588 setpcmark();
7589 /* put cursor at last line */
7590 curwin->w_cursor.lnum = eap->line2;
7591 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007592 }
7593
Bram Moolenaar071d4272004-06-13 20:20:40 +00007594 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007595}
7596
7597#ifdef FEAT_BYTEOFF
7598 static void
7599ex_goto(eap)
7600 exarg_T *eap;
7601{
7602 goto_byte(eap->line2);
7603}
7604#endif
7605
7606/*
7607 * ":shell".
7608 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007609 static void
7610ex_shell(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007611 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007612{
7613 do_shell(NULL, 0);
7614}
7615
7616#if (defined(FEAT_WINDOWS) && defined(HAVE_DROP_FILE)) \
7617 || (defined(FEAT_GUI_GTK) && defined(FEAT_DND)) \
Bram Moolenaar371baa92005-12-29 22:43:53 +00007618 || defined(FEAT_GUI_MSWIN) \
7619 || defined(FEAT_GUI_MAC) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00007620 || defined(PROTO)
7621
7622/*
7623 * Handle a file drop. The code is here because a drop is *nearly* like an
7624 * :args command, but not quite (we have a list of exact filenames, so we
7625 * don't want to (a) parse a command line, or (b) expand wildcards. So the
7626 * code is very similar to :args and hence needs access to a lot of the static
7627 * functions in this file.
7628 *
7629 * The list should be allocated using alloc(), as should each item in the
7630 * list. This function takes over responsibility for freeing the list.
7631 *
Bram Moolenaar81870892007-11-11 18:17:28 +00007632 * XXX The list is made into the argument list. This is freed using
Bram Moolenaar071d4272004-06-13 20:20:40 +00007633 * FreeWild(), which does a series of vim_free() calls, unless the two defines
7634 * __EMX__ and __ALWAYS_HAS_TRAILING_NUL_POINTER are set. In this case, a
7635 * routine _fnexplodefree() is used. This may cause problems, but as the drop
7636 * file functionality is (currently) not in EMX this is not presently a
7637 * problem.
7638 */
7639 void
7640handle_drop(filec, filev, split)
7641 int filec; /* the number of files dropped */
7642 char_u **filev; /* the list of files dropped */
7643 int split; /* force splitting the window */
7644{
7645 exarg_T ea;
7646 int save_msg_scroll = msg_scroll;
7647
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007648 /* Postpone this while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007649 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007650 return;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007651#ifdef FEAT_AUTOCMD
7652 if (curbuf_locked())
7653 return;
7654#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00007655 /* When the screen is being updated we should not change buffers and
7656 * windows structures, it may cause freed memory to be used. */
7657 if (updating_screen)
7658 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007659
7660 /* Check whether the current buffer is changed. If so, we will need
7661 * to split the current window or data could be lost.
7662 * We don't need to check if the 'hidden' option is set, as in this
7663 * case the buffer won't be lost.
7664 */
7665 if (!P_HID(curbuf) && !split)
7666 {
7667 ++emsg_off;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01007668 split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007669 --emsg_off;
7670 }
7671 if (split)
7672 {
7673# ifdef FEAT_WINDOWS
7674 if (win_split(0, 0) == FAIL)
7675 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007676 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007677
7678 /* When splitting the window, create a new alist. Otherwise the
7679 * existing one is overwritten. */
7680 alist_unlink(curwin->w_alist);
7681 alist_new();
7682# else
7683 return; /* can't split, always fail */
7684# endif
7685 }
7686
7687 /*
7688 * Set up the new argument list.
7689 */
Bram Moolenaar86b68352004-12-27 21:59:20 +00007690 alist_set(ALIST(curwin), filec, filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007691
7692 /*
7693 * Move to the first file.
7694 */
7695 /* Fake up a minimal "next" command for do_argfile() */
7696 vim_memset(&ea, 0, sizeof(ea));
7697 ea.cmd = (char_u *)"next";
7698 do_argfile(&ea, 0);
7699
7700 /* do_ecmd() may set need_start_insertmode, but since we never left Insert
7701 * mode that is not needed here. */
7702 need_start_insertmode = FALSE;
7703
7704 /* Restore msg_scroll, otherwise a following command may cause scrolling
7705 * unexpectedly. The screen will be redrawn by the caller, thus
7706 * msg_scroll being set by displaying a message is irrelevant. */
7707 msg_scroll = save_msg_scroll;
7708}
7709#endif
7710
Bram Moolenaar071d4272004-06-13 20:20:40 +00007711/*
7712 * Clear an argument list: free all file names and reset it to zero entries.
7713 */
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007714 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00007715alist_clear(al)
7716 alist_T *al;
7717{
7718 while (--al->al_ga.ga_len >= 0)
7719 vim_free(AARGLIST(al)[al->al_ga.ga_len].ae_fname);
7720 ga_clear(&al->al_ga);
7721}
7722
7723/*
7724 * Init an argument list.
7725 */
7726 void
7727alist_init(al)
7728 alist_T *al;
7729{
7730 ga_init2(&al->al_ga, (int)sizeof(aentry_T), 5);
7731}
7732
7733#if defined(FEAT_WINDOWS) || defined(PROTO)
7734
7735/*
7736 * Remove a reference from an argument list.
7737 * Ignored when the argument list is the global one.
7738 * If the argument list is no longer used by any window, free it.
7739 */
7740 void
7741alist_unlink(al)
7742 alist_T *al;
7743{
7744 if (al != &global_alist && --al->al_refcount <= 0)
7745 {
7746 alist_clear(al);
7747 vim_free(al);
7748 }
7749}
7750
7751# if defined(FEAT_LISTCMDS) || defined(HAVE_DROP_FILE) || defined(PROTO)
7752/*
7753 * Create a new argument list and use it for the current window.
7754 */
7755 void
7756alist_new()
7757{
7758 curwin->w_alist = (alist_T *)alloc((unsigned)sizeof(alist_T));
7759 if (curwin->w_alist == NULL)
7760 {
7761 curwin->w_alist = &global_alist;
7762 ++global_alist.al_refcount;
7763 }
7764 else
7765 {
7766 curwin->w_alist->al_refcount = 1;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02007767 curwin->w_alist->id = ++max_alist_id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007768 alist_init(curwin->w_alist);
7769 }
7770}
7771# endif
7772#endif
7773
7774#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE) || defined(PROTO)
7775/*
7776 * Expand the file names in the global argument list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00007777 * If "fnum_list" is not NULL, use "fnum_list[fnum_len]" as a list of buffer
7778 * numbers to be re-used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007779 */
7780 void
Bram Moolenaar86b68352004-12-27 21:59:20 +00007781alist_expand(fnum_list, fnum_len)
7782 int *fnum_list;
7783 int fnum_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007784{
7785 char_u **old_arg_files;
Bram Moolenaar86b68352004-12-27 21:59:20 +00007786 int old_arg_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007787 char_u **new_arg_files;
7788 int new_arg_file_count;
7789 char_u *save_p_su = p_su;
7790 int i;
7791
7792 /* Don't use 'suffixes' here. This should work like the shell did the
7793 * expansion. Also, the vimrc file isn't read yet, thus the user
7794 * can't set the options. */
7795 p_su = empty_option;
7796 old_arg_files = (char_u **)alloc((unsigned)(sizeof(char_u *) * GARGCOUNT));
7797 if (old_arg_files != NULL)
7798 {
7799 for (i = 0; i < GARGCOUNT; ++i)
Bram Moolenaar86b68352004-12-27 21:59:20 +00007800 old_arg_files[i] = vim_strsave(GARGLIST[i].ae_fname);
7801 old_arg_count = GARGCOUNT;
7802 if (expand_wildcards(old_arg_count, old_arg_files,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007803 &new_arg_file_count, &new_arg_files,
Bram Moolenaarb5609832011-07-20 15:04:58 +02007804 EW_FILE|EW_NOTFOUND|EW_ADDSLASH|EW_NOERROR) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007805 && new_arg_file_count > 0)
7806 {
Bram Moolenaar86b68352004-12-27 21:59:20 +00007807 alist_set(&global_alist, new_arg_file_count, new_arg_files,
7808 TRUE, fnum_list, fnum_len);
7809 FreeWild(old_arg_count, old_arg_files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007810 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007811 }
7812 p_su = save_p_su;
7813}
7814#endif
7815
7816/*
7817 * Set the argument list for the current window.
7818 * Takes over the allocated files[] and the allocated fnames in it.
7819 */
7820 void
Bram Moolenaar86b68352004-12-27 21:59:20 +00007821alist_set(al, count, files, use_curbuf, fnum_list, fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007822 alist_T *al;
7823 int count;
7824 char_u **files;
7825 int use_curbuf;
Bram Moolenaar86b68352004-12-27 21:59:20 +00007826 int *fnum_list;
7827 int fnum_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007828{
7829 int i;
7830
7831 alist_clear(al);
7832 if (ga_grow(&al->al_ga, count) == OK)
7833 {
7834 for (i = 0; i < count; ++i)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007835 {
7836 if (got_int)
7837 {
7838 /* When adding many buffers this can take a long time. Allow
7839 * interrupting here. */
7840 while (i < count)
7841 vim_free(files[i++]);
7842 break;
7843 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00007844
7845 /* May set buffer name of a buffer previously used for the
7846 * argument list, so that it's re-used by alist_add. */
7847 if (fnum_list != NULL && i < fnum_len)
7848 buf_set_name(fnum_list[i], files[i]);
7849
Bram Moolenaar071d4272004-06-13 20:20:40 +00007850 alist_add(al, files[i], use_curbuf ? 2 : 1);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007851 ui_breakcheck();
7852 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007853 vim_free(files);
7854 }
7855 else
7856 FreeWild(count, files);
7857#ifdef FEAT_WINDOWS
7858 if (al == &global_alist)
7859#endif
7860 arg_had_last = FALSE;
7861}
7862
7863/*
7864 * Add file "fname" to argument list "al".
7865 * "fname" must have been allocated and "al" must have been checked for room.
7866 */
7867 void
7868alist_add(al, fname, set_fnum)
7869 alist_T *al;
7870 char_u *fname;
7871 int set_fnum; /* 1: set buffer number; 2: re-use curbuf */
7872{
7873 if (fname == NULL) /* don't add NULL file names */
7874 return;
7875#ifdef BACKSLASH_IN_FILENAME
7876 slash_adjust(fname);
7877#endif
7878 AARGLIST(al)[al->al_ga.ga_len].ae_fname = fname;
7879 if (set_fnum > 0)
7880 AARGLIST(al)[al->al_ga.ga_len].ae_fnum =
7881 buflist_add(fname, BLN_LISTED | (set_fnum == 2 ? BLN_CURBUF : 0));
7882 ++al->al_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007883}
7884
7885#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
7886/*
7887 * Adjust slashes in file names. Called after 'shellslash' was set.
7888 */
7889 void
7890alist_slash_adjust()
7891{
7892 int i;
7893# ifdef FEAT_WINDOWS
7894 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007895 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007896# endif
7897
7898 for (i = 0; i < GARGCOUNT; ++i)
7899 if (GARGLIST[i].ae_fname != NULL)
7900 slash_adjust(GARGLIST[i].ae_fname);
7901# ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00007902 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007903 if (wp->w_alist != &global_alist)
7904 for (i = 0; i < WARGCOUNT(wp); ++i)
7905 if (WARGLIST(wp)[i].ae_fname != NULL)
7906 slash_adjust(WARGLIST(wp)[i].ae_fname);
7907# endif
7908}
7909#endif
7910
7911/*
7912 * ":preserve".
7913 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007914 static void
7915ex_preserve(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007916 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007917{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007918 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007919 ml_preserve(curbuf, TRUE);
7920}
7921
7922/*
7923 * ":recover".
7924 */
7925 static void
7926ex_recover(eap)
7927 exarg_T *eap;
7928{
7929 /* Set recoverymode right away to avoid the ATTENTION prompt. */
7930 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01007931 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
7932 | CCGD_MULTWIN
7933 | (eap->forceit ? CCGD_FORCEIT : 0)
7934 | CCGD_EXCMD)
7935
Bram Moolenaar071d4272004-06-13 20:20:40 +00007936 && (*eap->arg == NUL
7937 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
7938 ml_recover();
7939 recoverymode = FALSE;
7940}
7941
7942/*
7943 * Command modifier used in a wrong way.
7944 */
7945 static void
7946ex_wrongmodifier(eap)
7947 exarg_T *eap;
7948{
7949 eap->errmsg = e_invcmd;
7950}
7951
7952#ifdef FEAT_WINDOWS
7953/*
7954 * :sview [+command] file split window with new file, read-only
7955 * :split [[+command] file] split window with current or new file
7956 * :vsplit [[+command] file] split window vertically with current or new file
7957 * :new [[+command] file] split window with no or new file
7958 * :vnew [[+command] file] split vertically window with no or new file
7959 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007960 *
7961 * :tabedit open new Tab page with empty window
7962 * :tabedit [+command] file open new Tab page and edit "file"
7963 * :tabnew [[+command] file] just like :tabedit
7964 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007965 */
7966 void
7967ex_splitview(eap)
7968 exarg_T *eap;
7969{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007970 win_T *old_curwin = curwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007971# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007972 char_u *fname = NULL;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007973# endif
7974# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007975 int browse_flag = cmdmod.browse;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007976# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007977
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007978# ifndef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007979 if (eap->cmdidx == CMD_vsplit || eap->cmdidx == CMD_vnew)
7980 {
7981 ex_ni(eap);
7982 return;
7983 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007984# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007985
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007986# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007987 need_mouse_correct = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007988# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007989
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007990# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00007991 /* A ":split" in the quickfix window works like ":new". Don't want two
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007992 * quickfix windows. But it's OK when doing ":tab split". */
7993 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007994 {
7995 if (eap->cmdidx == CMD_split)
7996 eap->cmdidx = CMD_new;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007997# ifdef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007998 if (eap->cmdidx == CMD_vsplit)
7999 eap->cmdidx = CMD_vnew;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008000# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008001 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008002# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008003
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008004# ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008005 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008006 {
8007 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
8008 FNAME_MESS, TRUE, curbuf->b_ffname);
8009 if (fname == NULL)
8010 goto theend;
8011 eap->arg = fname;
8012 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008013# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008014 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008015# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008016# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008017# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008018 if (cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008019# ifdef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00008020 && eap->cmdidx != CMD_vnew
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008021# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008022 && eap->cmdidx != CMD_new)
8023 {
Bram Moolenaar15bfa092008-07-24 16:45:38 +00008024# ifdef FEAT_AUTOCMD
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008025 if (
Bram Moolenaar15bfa092008-07-24 16:45:38 +00008026# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008027 !gui.in_use &&
Bram Moolenaar15bfa092008-07-24 16:45:38 +00008028# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008029 au_has_group((char_u *)"FileExplorer"))
8030 {
8031 /* No browsing supported but we do have the file explorer:
8032 * Edit the directory. */
8033 if (*eap->arg == NUL || !mch_isdir(eap->arg))
8034 eap->arg = (char_u *)".";
8035 }
8036 else
Bram Moolenaar15bfa092008-07-24 16:45:38 +00008037# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008038 {
8039 fname = do_browse(0, (char_u *)_("Edit File in new window"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00008040 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008041 if (fname == NULL)
8042 goto theend;
8043 eap->arg = fname;
8044 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008045 }
8046 cmdmod.browse = FALSE; /* Don't browse again in do_ecmd(). */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008047# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008048
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008049 /*
8050 * Either open new tab page or split the window.
8051 */
8052 if (eap->cmdidx == CMD_tabedit
8053 || eap->cmdidx == CMD_tabfind
8054 || eap->cmdidx == CMD_tabnew)
8055 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00008056 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
8057 : eap->addr_count == 0 ? 0
8058 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008059 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00008060 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008061
8062 /* set the alternate buffer for the window we came from */
8063 if (curwin != old_curwin
8064 && win_valid(old_curwin)
8065 && old_curwin->w_buffer != curbuf
8066 && !cmdmod.keepalt)
8067 old_curwin->w_alt_fnum = curbuf->b_fnum;
8068 }
8069 }
8070 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008071 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
8072 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008073# ifdef FEAT_SCROLLBIND
Bram Moolenaar071d4272004-06-13 20:20:40 +00008074 /* Reset 'scrollbind' when editing another file, but keep it when
8075 * doing ":split" without arguments. */
8076 if (*eap->arg != NUL
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008077# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008078 || cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008079# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008080 )
Bram Moolenaar3368ea22010-09-21 16:56:35 +02008081 {
8082 RESET_BINDING(curwin);
8083 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008084 else
8085 do_check_scrollbind(FALSE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008086# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008087 do_exedit(eap, old_curwin);
8088 }
8089
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008090# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008091 cmdmod.browse = browse_flag;
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# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008095theend:
8096 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008097# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008098}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008099
8100/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008101 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008102 */
8103 void
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008104tabpage_new()
8105{
8106 exarg_T ea;
8107
8108 vim_memset(&ea, 0, sizeof(ea));
8109 ea.cmdidx = CMD_tabnew;
8110 ea.cmd = (char_u *)"tabn";
8111 ea.arg = (char_u *)"";
8112 ex_splitview(&ea);
8113}
8114
8115/*
8116 * :tabnext command
8117 */
8118 static void
8119ex_tabnext(eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008120 exarg_T *eap;
8121{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008122 switch (eap->cmdidx)
8123 {
8124 case CMD_tabfirst:
8125 case CMD_tabrewind:
8126 goto_tabpage(1);
8127 break;
8128 case CMD_tablast:
8129 goto_tabpage(9999);
8130 break;
8131 case CMD_tabprevious:
8132 case CMD_tabNext:
8133 goto_tabpage(eap->addr_count == 0 ? -1 : -(int)eap->line2);
8134 break;
8135 default: /* CMD_tabnext */
8136 goto_tabpage(eap->addr_count == 0 ? 0 : (int)eap->line2);
8137 break;
8138 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008139}
8140
8141/*
8142 * :tabmove command
8143 */
8144 static void
8145ex_tabmove(eap)
8146 exarg_T *eap;
8147{
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02008148 int tab_number = 9999;
8149
8150 if (eap->arg && *eap->arg != NUL)
8151 {
8152 char_u *p = eap->arg;
8153 int relative = 0; /* argument +N/-N means: move N places to the
8154 * right/left relative to the current position. */
8155
8156 if (*eap->arg == '-')
8157 {
8158 relative = -1;
8159 p = eap->arg + 1;
8160 }
8161 else if (*eap->arg == '+')
8162 {
8163 relative = 1;
8164 p = eap->arg + 1;
8165 }
8166 else
8167 p = eap->arg;
8168
8169 if (p == skipdigits(p))
8170 {
8171 /* No numbers as argument. */
8172 eap->errmsg = e_invarg;
8173 return;
8174 }
8175
8176 tab_number = getdigits(&p);
8177 if (relative != 0)
8178 tab_number = tab_number * relative + tabpage_index(curtab) - 1;;
8179 }
8180 else if (eap->addr_count != 0)
8181 tab_number = eap->line2;
8182
8183 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008184}
8185
8186/*
8187 * :tabs command: List tabs and their contents.
8188 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008189 static void
8190ex_tabs(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008191 exarg_T *eap UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008192{
8193 tabpage_T *tp;
8194 win_T *wp;
8195 int tabcount = 1;
8196
8197 msg_start();
8198 msg_scroll = TRUE;
8199 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
8200 {
8201 msg_putchar('\n');
8202 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
8203 msg_outtrans_attr(IObuff, hl_attr(HLF_T));
8204 out_flush(); /* output one line at a time */
8205 ui_breakcheck();
8206
Bram Moolenaar030f0df2006-02-21 22:02:53 +00008207 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008208 wp = firstwin;
8209 else
8210 wp = tp->tp_firstwin;
8211 for ( ; wp != NULL && !got_int; wp = wp->w_next)
8212 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008213 msg_putchar('\n');
8214 msg_putchar(wp == curwin ? '>' : ' ');
8215 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00008216 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
8217 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008218 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02008219 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008220 else
8221 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
8222 IObuff, IOSIZE, TRUE);
8223 msg_outtrans(IObuff);
8224 out_flush(); /* output one line at a time */
8225 ui_breakcheck();
8226 }
8227 }
8228}
8229
8230#endif /* FEAT_WINDOWS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008231
8232/*
8233 * ":mode": Set screen mode.
8234 * If no argument given, just get the screen size and redraw.
8235 */
8236 static void
8237ex_mode(eap)
8238 exarg_T *eap;
8239{
8240 if (*eap->arg == NUL)
8241 shell_resized();
8242 else
8243 mch_screenmode(eap->arg);
8244}
8245
8246#ifdef FEAT_WINDOWS
8247/*
8248 * ":resize".
8249 * set, increment or decrement current window height
8250 */
8251 static void
8252ex_resize(eap)
8253 exarg_T *eap;
8254{
8255 int n;
8256 win_T *wp = curwin;
8257
8258 if (eap->addr_count > 0)
8259 {
8260 n = eap->line2;
8261 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
8262 ;
8263 }
8264
8265#ifdef FEAT_GUI
8266 need_mouse_correct = TRUE;
8267#endif
8268 n = atol((char *)eap->arg);
8269#ifdef FEAT_VERTSPLIT
8270 if (cmdmod.split & WSP_VERT)
8271 {
8272 if (*eap->arg == '-' || *eap->arg == '+')
8273 n += W_WIDTH(curwin);
8274 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
8275 n = 9999;
8276 win_setwidth_win((int)n, wp);
8277 }
8278 else
8279#endif
8280 {
8281 if (*eap->arg == '-' || *eap->arg == '+')
8282 n += curwin->w_height;
8283 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
8284 n = 9999;
8285 win_setheight_win((int)n, wp);
8286 }
8287}
8288#endif
8289
8290/*
8291 * ":find [+command] <file>" command.
8292 */
8293 static void
8294ex_find(eap)
8295 exarg_T *eap;
8296{
8297#ifdef FEAT_SEARCHPATH
8298 char_u *fname;
8299 int count;
8300
8301 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
8302 TRUE, curbuf->b_ffname);
8303 if (eap->addr_count > 0)
8304 {
8305 /* Repeat finding the file "count" times. This matters when it
8306 * appears several times in the path. */
8307 count = eap->line2;
8308 while (fname != NULL && --count > 0)
8309 {
8310 vim_free(fname);
8311 fname = find_file_in_path(NULL, 0, FNAME_MESS,
8312 FALSE, curbuf->b_ffname);
8313 }
8314 }
8315
8316 if (fname != NULL)
8317 {
8318 eap->arg = fname;
8319#endif
8320 do_exedit(eap, NULL);
8321#ifdef FEAT_SEARCHPATH
8322 vim_free(fname);
8323 }
8324#endif
8325}
8326
8327/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00008328 * ":open" simulation: for now just work like ":visual".
8329 */
8330 static void
8331ex_open(eap)
8332 exarg_T *eap;
8333{
8334 regmatch_T regmatch;
8335 char_u *p;
8336
8337 curwin->w_cursor.lnum = eap->line2;
8338 beginline(BL_SOL | BL_FIX);
8339 if (*eap->arg == '/')
8340 {
8341 /* ":open /pattern/": put cursor in column found with pattern */
8342 ++eap->arg;
8343 p = skip_regexp(eap->arg, '/', p_magic, NULL);
8344 *p = NUL;
8345 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
8346 if (regmatch.regprog != NULL)
8347 {
8348 regmatch.rm_ic = p_ic;
8349 p = ml_get_curline();
8350 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008351 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008352 else
8353 EMSG(_(e_nomatch));
Bram Moolenaar473de612013-06-08 18:19:48 +02008354 vim_regfree(regmatch.regprog);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008355 }
8356 /* Move to the NUL, ignore any other arguments. */
8357 eap->arg += STRLEN(eap->arg);
8358 }
8359 check_cursor();
8360
8361 eap->cmdidx = CMD_visual;
8362 do_exedit(eap, NULL);
8363}
8364
8365/*
8366 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008367 */
8368 static void
8369ex_edit(eap)
8370 exarg_T *eap;
8371{
8372 do_exedit(eap, NULL);
8373}
8374
8375/*
8376 * ":edit <file>" command and alikes.
8377 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008378 void
8379do_exedit(eap, old_curwin)
8380 exarg_T *eap;
8381 win_T *old_curwin; /* curwin before doing a split or NULL */
8382{
8383 int n;
8384#ifdef FEAT_WINDOWS
8385 int need_hide;
8386#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00008387 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008388
8389 /*
8390 * ":vi" command ends Ex mode.
8391 */
8392 if (exmode_active && (eap->cmdidx == CMD_visual
8393 || eap->cmdidx == CMD_view))
8394 {
8395 exmode_active = FALSE;
8396 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008397 {
8398 /* Special case: ":global/pat/visual\NLvi-commands" */
8399 if (global_busy)
8400 {
8401 int rd = RedrawingDisabled;
8402 int nwr = no_wait_return;
8403 int ms = msg_scroll;
8404#ifdef FEAT_GUI
8405 int he = hold_gui_events;
8406#endif
8407
8408 if (eap->nextcmd != NULL)
8409 {
8410 stuffReadbuff(eap->nextcmd);
8411 eap->nextcmd = NULL;
8412 }
8413
8414 if (exmode_was != EXMODE_VIM)
8415 settmode(TMODE_RAW);
8416 RedrawingDisabled = 0;
8417 no_wait_return = 0;
8418 need_wait_return = FALSE;
8419 msg_scroll = 0;
8420#ifdef FEAT_GUI
8421 hold_gui_events = 0;
8422#endif
8423 must_redraw = CLEAR;
8424
8425 main_loop(FALSE, TRUE);
8426
8427 RedrawingDisabled = rd;
8428 no_wait_return = nwr;
8429 msg_scroll = ms;
8430#ifdef FEAT_GUI
8431 hold_gui_events = he;
8432#endif
8433 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008434 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008435 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008436 }
8437
8438 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008439 || eap->cmdidx == CMD_tabnew
8440 || eap->cmdidx == CMD_tabedit
Bram Moolenaar071d4272004-06-13 20:20:40 +00008441#ifdef FEAT_VERTSPLIT
8442 || eap->cmdidx == CMD_vnew
8443#endif
8444 ) && *eap->arg == NUL)
8445 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008446 /* ":new" or ":tabnew" without argument: edit an new empty buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008447 setpcmark();
8448 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00008449 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
8450 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008451 }
8452 else if ((eap->cmdidx != CMD_split
8453#ifdef FEAT_VERTSPLIT
8454 && eap->cmdidx != CMD_vsplit
8455#endif
8456 )
8457 || *eap->arg != NUL
8458#ifdef FEAT_BROWSE
8459 || cmdmod.browse
8460#endif
8461 )
8462 {
Bram Moolenaar5555acc2006-04-07 21:33:12 +00008463#ifdef FEAT_AUTOCMD
8464 /* Can't edit another file when "curbuf_lock" is set. Only ":edit"
8465 * can bring us here, others are stopped earlier. */
8466 if (*eap->arg != NUL && curbuf_locked())
8467 return;
8468#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008469 n = readonlymode;
8470 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
8471 readonlymode = TRUE;
8472 else if (eap->cmdidx == CMD_enew)
8473 readonlymode = FALSE; /* 'readonly' doesn't make sense in an
8474 empty buffer */
8475 setpcmark();
8476 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
8477 NULL, eap,
8478 /* ":edit" goes to first line if Vi compatible */
8479 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
8480 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
8481 ? ECMD_ONE : eap->do_ecmd_lnum,
8482 (P_HID(curbuf) ? ECMD_HIDE : 0)
8483 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar7b449342014-03-25 13:03:48 +01008484 /* after a split we can use an existing buffer */
8485 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008486#ifdef FEAT_LISTCMDS
8487 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
8488#endif
Bram Moolenaar701f7af2008-11-15 13:12:07 +00008489 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008490 {
8491 /* Editing the file failed. If the window was split, close it. */
8492#ifdef FEAT_WINDOWS
8493 if (old_curwin != NULL)
8494 {
8495 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
8496 if (!need_hide || P_HID(curbuf))
8497 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008498# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
8499 cleanup_T cs;
8500
8501 /* Reset the error/interrupt/exception state here so that
8502 * aborting() returns FALSE when closing a window. */
8503 enter_cleanup(&cs);
8504# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008505# ifdef FEAT_GUI
8506 need_mouse_correct = TRUE;
8507# endif
8508 win_close(curwin, !need_hide && !P_HID(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008509
8510# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
8511 /* Restore the error/interrupt/exception state if not
8512 * discarded by a new aborting error, interrupt, or
8513 * uncaught exception. */
8514 leave_cleanup(&cs);
8515# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008516 }
8517 }
8518#endif
8519 }
8520 else if (readonlymode && curbuf->b_nwindows == 1)
8521 {
8522 /* When editing an already visited buffer, 'readonly' won't be set
8523 * but the previous value is kept. With ":view" and ":sview" we
8524 * want the file to be readonly, except when another window is
8525 * editing the same buffer. */
8526 curbuf->b_p_ro = TRUE;
8527 }
8528 readonlymode = n;
8529 }
8530 else
8531 {
8532 if (eap->do_ecmd_cmd != NULL)
8533 do_cmdline_cmd(eap->do_ecmd_cmd);
8534#ifdef FEAT_TITLE
8535 n = curwin->w_arg_idx_invalid;
8536#endif
8537 check_arg_idx(curwin);
8538#ifdef FEAT_TITLE
8539 if (n != curwin->w_arg_idx_invalid)
8540 maketitle();
8541#endif
8542 }
8543
8544#ifdef FEAT_WINDOWS
8545 /*
8546 * if ":split file" worked, set alternate file name in old window to new
8547 * file
8548 */
8549 if (old_curwin != NULL
8550 && *eap->arg != NUL
8551 && curwin != old_curwin
8552 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00008553 && old_curwin->w_buffer != curbuf
8554 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008555 old_curwin->w_alt_fnum = curbuf->b_fnum;
8556#endif
8557
8558 ex_no_reprint = TRUE;
8559}
8560
8561#ifndef FEAT_GUI
8562/*
8563 * ":gui" and ":gvim" when there is no GUI.
8564 */
8565 static void
8566ex_nogui(eap)
8567 exarg_T *eap;
8568{
8569 eap->errmsg = e_nogvim;
8570}
8571#endif
8572
8573#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
8574 static void
8575ex_tearoff(eap)
8576 exarg_T *eap;
8577{
8578 gui_make_tearoff(eap->arg);
8579}
8580#endif
8581
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008582#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008583 static void
8584ex_popup(eap)
8585 exarg_T *eap;
8586{
Bram Moolenaar97409f12005-07-08 22:17:29 +00008587 gui_make_popup(eap->arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008588}
8589#endif
8590
Bram Moolenaar071d4272004-06-13 20:20:40 +00008591 static void
8592ex_swapname(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008593 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008594{
8595 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
8596 MSG(_("No swap file"));
8597 else
8598 msg(curbuf->b_ml.ml_mfp->mf_fname);
8599}
8600
8601/*
8602 * ":syncbind" forces all 'scrollbind' windows to have the same relative
8603 * offset.
8604 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
8605 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008606 static void
8607ex_syncbind(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008608 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008609{
8610#ifdef FEAT_SCROLLBIND
8611 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008612 win_T *save_curwin = curwin;
8613 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008614 long topline;
8615 long y;
8616 linenr_T old_linenr = curwin->w_cursor.lnum;
8617
8618 setpcmark();
8619
8620 /*
8621 * determine max topline
8622 */
8623 if (curwin->w_p_scb)
8624 {
8625 topline = curwin->w_topline;
8626 for (wp = firstwin; wp; wp = wp->w_next)
8627 {
8628 if (wp->w_p_scb && wp->w_buffer)
8629 {
8630 y = wp->w_buffer->b_ml.ml_line_count - p_so;
8631 if (topline > y)
8632 topline = y;
8633 }
8634 }
8635 if (topline < 1)
8636 topline = 1;
8637 }
8638 else
8639 {
8640 topline = 1;
8641 }
8642
8643
8644 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008645 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008646 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008647 for (curwin = firstwin; curwin; curwin = curwin->w_next)
8648 {
8649 if (curwin->w_p_scb)
8650 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008651 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008652 y = topline - curwin->w_topline;
8653 if (y > 0)
8654 scrollup(y, TRUE);
8655 else
8656 scrolldown(-y, TRUE);
8657 curwin->w_scbind_pos = topline;
8658 redraw_later(VALID);
8659 cursor_correct();
8660#ifdef FEAT_WINDOWS
8661 curwin->w_redr_status = TRUE;
8662#endif
8663 }
8664 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008665 curwin = save_curwin;
8666 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008667 if (curwin->w_p_scb)
8668 {
8669 did_syncbind = TRUE;
8670 checkpcmark();
8671 if (old_linenr != curwin->w_cursor.lnum)
8672 {
8673 char_u ctrl_o[2];
8674
8675 ctrl_o[0] = Ctrl_O;
8676 ctrl_o[1] = 0;
8677 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
8678 }
8679 }
8680#endif
8681}
8682
8683
8684 static void
8685ex_read(eap)
8686 exarg_T *eap;
8687{
Bram Moolenaardf177f62005-02-22 08:39:57 +00008688 int i;
8689 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
8690 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008691
8692 if (eap->usefilter) /* :r!cmd */
8693 do_bang(1, eap, FALSE, FALSE, TRUE);
8694 else
8695 {
8696 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
8697 return;
8698
8699#ifdef FEAT_BROWSE
8700 if (cmdmod.browse)
8701 {
8702 char_u *browseFile;
8703
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008704 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008705 NULL, NULL, NULL, curbuf);
8706 if (browseFile != NULL)
8707 {
8708 i = readfile(browseFile, NULL,
8709 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8710 vim_free(browseFile);
8711 }
8712 else
8713 i = OK;
8714 }
8715 else
8716#endif
8717 if (*eap->arg == NUL)
8718 {
8719 if (check_fname() == FAIL) /* check for no file name */
8720 return;
8721 i = readfile(curbuf->b_ffname, curbuf->b_fname,
8722 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8723 }
8724 else
8725 {
8726 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
8727 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
8728 i = readfile(eap->arg, NULL,
8729 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8730
8731 }
8732 if (i == FAIL)
8733 {
8734#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
8735 if (!aborting())
8736#endif
8737 EMSG2(_(e_notopen), eap->arg);
8738 }
8739 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008740 {
8741 if (empty && exmode_active)
8742 {
8743 /* Delete the empty line that remains. Historically ex does
8744 * this but vi doesn't. */
8745 if (eap->line2 == 0)
8746 lnum = curbuf->b_ml.ml_line_count;
8747 else
8748 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008749 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008750 {
8751 ml_delete(lnum, FALSE);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008752 if (curwin->w_cursor.lnum > 1
8753 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008754 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00008755 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008756 }
8757 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008758 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008759 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008760 }
8761}
8762
Bram Moolenaarea408852005-06-25 22:49:46 +00008763static char_u *prev_dir = NULL;
8764
8765#if defined(EXITFREE) || defined(PROTO)
8766 void
8767free_cd_dir()
8768{
8769 vim_free(prev_dir);
Bram Moolenaara0174af2008-01-02 20:08:25 +00008770 prev_dir = NULL;
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00008771
8772 vim_free(globaldir);
8773 globaldir = NULL;
Bram Moolenaarea408852005-06-25 22:49:46 +00008774}
8775#endif
8776
Bram Moolenaarf4258302013-06-02 18:20:17 +02008777/*
8778 * Deal with the side effects of changing the current directory.
8779 * When "local" is TRUE then this was after an ":lcd" command.
8780 */
8781 void
8782post_chdir(local)
8783 int local;
8784{
8785 vim_free(curwin->w_localdir);
Bram Moolenaarbd2dc342014-01-10 15:53:13 +01008786 curwin->w_localdir = NULL;
Bram Moolenaarf4258302013-06-02 18:20:17 +02008787 if (local)
8788 {
8789 /* If still in global directory, need to remember current
8790 * directory as global directory. */
8791 if (globaldir == NULL && prev_dir != NULL)
8792 globaldir = vim_strsave(prev_dir);
8793 /* Remember this local directory for the window. */
8794 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8795 curwin->w_localdir = vim_strsave(NameBuff);
8796 }
8797 else
8798 {
8799 /* We are now in the global directory, no need to remember its
8800 * name. */
8801 vim_free(globaldir);
8802 globaldir = NULL;
Bram Moolenaarf4258302013-06-02 18:20:17 +02008803 }
8804
8805 shorten_fnames(TRUE);
8806}
8807
Bram Moolenaarea408852005-06-25 22:49:46 +00008808
Bram Moolenaar071d4272004-06-13 20:20:40 +00008809/*
8810 * ":cd", ":lcd", ":chdir" and ":lchdir".
8811 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00008812 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00008813ex_cd(eap)
8814 exarg_T *eap;
8815{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008816 char_u *new_dir;
8817 char_u *tofree;
8818
8819 new_dir = eap->arg;
8820#if !defined(UNIX) && !defined(VMS)
8821 /* for non-UNIX ":cd" means: print current directory */
8822 if (*new_dir == NUL)
8823 ex_pwd(NULL);
8824 else
8825#endif
8826 {
Bram Moolenaar8e8fe9b2009-03-11 14:37:32 +00008827#ifdef FEAT_AUTOCMD
8828 if (allbuf_locked())
8829 return;
8830#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00008831 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged()
8832 && !eap->forceit)
8833 {
Bram Moolenaar81870892007-11-11 18:17:28 +00008834 EMSG(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00008835 return;
8836 }
8837
Bram Moolenaar071d4272004-06-13 20:20:40 +00008838 /* ":cd -": Change to previous directory */
8839 if (STRCMP(new_dir, "-") == 0)
8840 {
8841 if (prev_dir == NULL)
8842 {
8843 EMSG(_("E186: No previous directory"));
8844 return;
8845 }
8846 new_dir = prev_dir;
8847 }
8848
8849 /* Save current directory for next ":cd -" */
8850 tofree = prev_dir;
8851 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8852 prev_dir = vim_strsave(NameBuff);
8853 else
8854 prev_dir = NULL;
8855
8856#if defined(UNIX) || defined(VMS)
8857 /* for UNIX ":cd" means: go to home directory */
8858 if (*new_dir == NUL)
8859 {
8860 /* use NameBuff for home directory name */
8861# ifdef VMS
8862 char_u *p;
8863
8864 p = mch_getenv((char_u *)"SYS$LOGIN");
8865 if (p == NULL || *p == NUL) /* empty is the same as not set */
8866 NameBuff[0] = NUL;
8867 else
Bram Moolenaarb6356332005-07-18 21:40:44 +00008868 vim_strncpy(NameBuff, p, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008869# else
8870 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
8871# endif
8872 new_dir = NameBuff;
8873 }
8874#endif
8875 if (new_dir == NULL || vim_chdir(new_dir))
8876 EMSG(_(e_failed));
8877 else
8878 {
Bram Moolenaarf4258302013-06-02 18:20:17 +02008879 post_chdir(eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008880
8881 /* Echo the new current directory if the command was typed. */
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00008882 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008883 ex_pwd(eap);
8884 }
8885 vim_free(tofree);
8886 }
8887}
8888
8889/*
8890 * ":pwd".
8891 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008892 static void
8893ex_pwd(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008894 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008895{
8896 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8897 {
8898#ifdef BACKSLASH_IN_FILENAME
8899 slash_adjust(NameBuff);
8900#endif
8901 msg(NameBuff);
8902 }
8903 else
8904 EMSG(_("E187: Unknown"));
8905}
8906
8907/*
8908 * ":=".
8909 */
8910 static void
8911ex_equal(eap)
8912 exarg_T *eap;
8913{
8914 smsg((char_u *)"%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008915 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008916}
8917
8918 static void
8919ex_sleep(eap)
8920 exarg_T *eap;
8921{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008922 int n;
8923 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008924
8925 if (cursor_valid())
8926 {
8927 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
8928 if (n >= 0)
Bram Moolenaar10395d82014-02-05 22:46:52 +01008929 windgoto((int)n, W_WINCOL(curwin) + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008930 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008931
8932 len = eap->line2;
8933 switch (*eap->arg)
8934 {
8935 case 'm': break;
8936 case NUL: len *= 1000L; break;
8937 default: EMSG2(_(e_invarg2), eap->arg); return;
8938 }
8939 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008940}
8941
8942/*
8943 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
8944 */
8945 void
8946do_sleep(msec)
8947 long msec;
8948{
8949 long done;
8950
8951 cursor_on();
8952 out_flush();
8953 for (done = 0; !got_int && done < msec; done += 1000L)
8954 {
8955 ui_delay(msec - done > 1000L ? 1000L : msec - done, TRUE);
8956 ui_breakcheck();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02008957#ifdef FEAT_NETBEANS_INTG
8958 /* Process the netbeans messages that may have been received in the
8959 * call to ui_breakcheck() when the GUI is in use. This may occur when
8960 * running a test case. */
8961 netbeans_parse_messages();
8962#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008963 }
8964}
8965
8966 static void
8967do_exmap(eap, isabbrev)
8968 exarg_T *eap;
8969 int isabbrev;
8970{
8971 int mode;
8972 char_u *cmdp;
8973
8974 cmdp = eap->cmd;
8975 mode = get_map_mode(&cmdp, eap->forceit || isabbrev);
8976
8977 switch (do_map((*cmdp == 'n') ? 2 : (*cmdp == 'u'),
8978 eap->arg, mode, isabbrev))
8979 {
8980 case 1: EMSG(_(e_invarg));
8981 break;
8982 case 2: EMSG(isabbrev ? _(e_noabbr) : _(e_nomap));
8983 break;
8984 }
8985}
8986
8987/*
8988 * ":winsize" command (obsolete).
8989 */
8990 static void
8991ex_winsize(eap)
8992 exarg_T *eap;
8993{
8994 int w, h;
8995 char_u *arg = eap->arg;
8996 char_u *p;
8997
8998 w = getdigits(&arg);
8999 arg = skipwhite(arg);
9000 p = arg;
9001 h = getdigits(&arg);
9002 if (*p != NUL && *arg == NUL)
9003 set_shellsize(w, h, TRUE);
9004 else
9005 EMSG(_("E465: :winsize requires two number arguments"));
9006}
9007
9008#ifdef FEAT_WINDOWS
9009 static void
9010ex_wincmd(eap)
9011 exarg_T *eap;
9012{
9013 int xchar = NUL;
9014 char_u *p;
9015
9016 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
9017 {
9018 /* CTRL-W g and CTRL-W CTRL-G have an extra command character */
9019 if (eap->arg[1] == NUL)
9020 {
9021 EMSG(_(e_invarg));
9022 return;
9023 }
9024 xchar = eap->arg[1];
9025 p = eap->arg + 2;
9026 }
9027 else
9028 p = eap->arg + 1;
9029
9030 eap->nextcmd = check_nextcmd(p);
9031 p = skipwhite(p);
9032 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
9033 EMSG(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02009034 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009035 {
9036 /* Pass flags on for ":vertical wincmd ]". */
9037 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009038 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009039 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
9040 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009041 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009042 }
9043}
9044#endif
9045
Bram Moolenaar843ee412004-06-30 16:16:41 +00009046#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009047/*
9048 * ":winpos".
9049 */
9050 static void
9051ex_winpos(eap)
9052 exarg_T *eap;
9053{
9054 int x, y;
9055 char_u *arg = eap->arg;
9056 char_u *p;
9057
9058 if (*arg == NUL)
9059 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00009060# if defined(FEAT_GUI) || defined(MSWIN)
9061# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00009062 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00009063# else
9064 if (mch_get_winpos(&x, &y) != FAIL)
9065# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009066 {
9067 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
9068 msg(IObuff);
9069 }
9070 else
9071# endif
9072 EMSG(_("E188: Obtaining window position not implemented for this platform"));
9073 }
9074 else
9075 {
9076 x = getdigits(&arg);
9077 arg = skipwhite(arg);
9078 p = arg;
9079 y = getdigits(&arg);
9080 if (*p == NUL || *arg != NUL)
9081 {
9082 EMSG(_("E466: :winpos requires two number arguments"));
9083 return;
9084 }
9085# ifdef FEAT_GUI
9086 if (gui.in_use)
9087 gui_mch_set_winpos(x, y);
9088 else if (gui.starting)
9089 {
9090 /* Remember the coordinates for when the window is opened. */
9091 gui_win_x = x;
9092 gui_win_y = y;
9093 }
9094# ifdef HAVE_TGETENT
9095 else
9096# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00009097# else
9098# ifdef MSWIN
9099 mch_set_winpos(x, y);
9100# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009101# endif
9102# ifdef HAVE_TGETENT
9103 if (*T_CWP)
9104 term_set_winpos(x, y);
9105# endif
9106 }
9107}
9108#endif
9109
9110/*
9111 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
9112 */
9113 static void
9114ex_operators(eap)
9115 exarg_T *eap;
9116{
9117 oparg_T oa;
9118
9119 clear_oparg(&oa);
9120 oa.regname = eap->regname;
9121 oa.start.lnum = eap->line1;
9122 oa.end.lnum = eap->line2;
9123 oa.line_count = eap->line2 - eap->line1 + 1;
9124 oa.motion_type = MLINE;
9125#ifdef FEAT_VIRTUALEDIT
9126 virtual_op = FALSE;
9127#endif
9128 if (eap->cmdidx != CMD_yank) /* position cursor for undo */
9129 {
9130 setpcmark();
9131 curwin->w_cursor.lnum = eap->line1;
9132 beginline(BL_SOL | BL_FIX);
9133 }
9134
Bram Moolenaard07c6e12013-11-21 14:21:40 +01009135 if (VIsual_active)
9136 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01009137
Bram Moolenaar071d4272004-06-13 20:20:40 +00009138 switch (eap->cmdidx)
9139 {
9140 case CMD_delete:
9141 oa.op_type = OP_DELETE;
9142 op_delete(&oa);
9143 break;
9144
9145 case CMD_yank:
9146 oa.op_type = OP_YANK;
9147 (void)op_yank(&oa, FALSE, TRUE);
9148 break;
9149
9150 default: /* CMD_rshift or CMD_lshift */
Bram Moolenaar6e707362013-06-14 19:15:58 +02009151 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009152#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02009153 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
9154#else
9155 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00009156#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02009157 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009158 oa.op_type = OP_RSHIFT;
9159 else
9160 oa.op_type = OP_LSHIFT;
9161 op_shift(&oa, FALSE, eap->amount);
9162 break;
9163 }
9164#ifdef FEAT_VIRTUALEDIT
9165 virtual_op = MAYBE;
9166#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00009167 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009168}
9169
9170/*
9171 * ":put".
9172 */
9173 static void
9174ex_put(eap)
9175 exarg_T *eap;
9176{
9177 /* ":0put" works like ":1put!". */
9178 if (eap->line2 == 0)
9179 {
9180 eap->line2 = 1;
9181 eap->forceit = TRUE;
9182 }
9183 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009184 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
9185 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009186}
9187
9188/*
9189 * Handle ":copy" and ":move".
9190 */
9191 static void
9192ex_copymove(eap)
9193 exarg_T *eap;
9194{
9195 long n;
9196
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01009197 n = get_address(&eap->arg, eap->addr_type, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009198 if (eap->arg == NULL) /* error detected */
9199 {
9200 eap->nextcmd = NULL;
9201 return;
9202 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00009203 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009204
9205 /*
9206 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
9207 */
9208 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
9209 {
9210 EMSG(_(e_invaddr));
9211 return;
9212 }
9213
9214 if (eap->cmdidx == CMD_move)
9215 {
9216 if (do_move(eap->line1, eap->line2, n) == FAIL)
9217 return;
9218 }
9219 else
9220 ex_copy(eap->line1, eap->line2, n);
9221 u_clearline();
9222 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009223 ex_may_print(eap);
9224}
9225
9226/*
9227 * Print the current line if flags were given to the Ex command.
9228 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02009229 void
Bram Moolenaardf177f62005-02-22 08:39:57 +00009230ex_may_print(eap)
9231 exarg_T *eap;
9232{
9233 if (eap->flags != 0)
9234 {
9235 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
9236 (eap->flags & EXFLAG_LIST));
9237 ex_no_reprint = TRUE;
9238 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009239}
9240
9241/*
9242 * ":smagic" and ":snomagic".
9243 */
9244 static void
9245ex_submagic(eap)
9246 exarg_T *eap;
9247{
9248 int magic_save = p_magic;
9249
9250 p_magic = (eap->cmdidx == CMD_smagic);
9251 do_sub(eap);
9252 p_magic = magic_save;
9253}
9254
9255/*
9256 * ":join".
9257 */
9258 static void
9259ex_join(eap)
9260 exarg_T *eap;
9261{
9262 curwin->w_cursor.lnum = eap->line1;
9263 if (eap->line1 == eap->line2)
9264 {
9265 if (eap->addr_count >= 2) /* :2,2join does nothing */
9266 return;
9267 if (eap->line2 == curbuf->b_ml.ml_line_count)
9268 {
9269 beep_flush();
9270 return;
9271 }
9272 ++eap->line2;
9273 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02009274 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009275 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009276 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009277}
9278
9279/*
9280 * ":[addr]@r" or ":[addr]*r": execute register
9281 */
9282 static void
9283ex_at(eap)
9284 exarg_T *eap;
9285{
9286 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00009287 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009288
9289 curwin->w_cursor.lnum = eap->line2;
9290
9291#ifdef USE_ON_FLY_SCROLL
9292 dont_scroll = TRUE; /* disallow scrolling here */
9293#endif
9294
9295 /* get the register name. No name means to use the previous one */
9296 c = *eap->arg;
9297 if (c == NUL || (c == '*' && *eap->cmd == '*'))
9298 c = '@';
Bram Moolenaard333d1e2006-11-07 17:43:47 +00009299 /* Put the register in the typeahead buffer with the "silent" flag. */
9300 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
9301 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009302 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009303 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00009304 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009305 else
9306 {
9307 int save_efr = exec_from_reg;
9308
9309 exec_from_reg = TRUE;
9310
9311 /*
9312 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00009313 * Continue until the stuff buffer is empty and all added characters
9314 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009315 */
Bram Moolenaar60462872009-11-03 11:40:19 +00009316 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009317 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
9318
9319 exec_from_reg = save_efr;
9320 }
9321}
9322
9323/*
9324 * ":!".
9325 */
9326 static void
9327ex_bang(eap)
9328 exarg_T *eap;
9329{
9330 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
9331}
9332
9333/*
9334 * ":undo".
9335 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009336 static void
9337ex_undo(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009338 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009339{
Bram Moolenaard3667a22006-03-16 21:35:52 +00009340 if (eap->addr_count == 1) /* :undo 123 */
Bram Moolenaar730cde92010-06-27 05:18:54 +02009341 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00009342 else
9343 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009344}
9345
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009346#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02009347 static void
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009348ex_wundo(eap)
9349 exarg_T *eap;
9350{
9351 char_u hash[UNDO_HASH_SIZE];
9352
9353 u_compute_hash(hash);
9354 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
9355}
9356
Bram Moolenaar6df6f472010-07-18 18:04:50 +02009357 static void
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009358ex_rundo(eap)
9359 exarg_T *eap;
9360{
9361 char_u hash[UNDO_HASH_SIZE];
9362
9363 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02009364 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009365}
9366#endif
9367
Bram Moolenaar071d4272004-06-13 20:20:40 +00009368/*
9369 * ":redo".
9370 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009371 static void
9372ex_redo(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009373 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009374{
9375 u_redo(1);
9376}
9377
9378/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009379 * ":earlier" and ":later".
9380 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009381 static void
9382ex_later(eap)
9383 exarg_T *eap;
9384{
9385 long count = 0;
9386 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02009387 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009388 char_u *p = eap->arg;
9389
9390 if (*p == NUL)
9391 count = 1;
9392 else if (isdigit(*p))
9393 {
9394 count = getdigits(&p);
9395 switch (*p)
9396 {
9397 case 's': ++p; sec = TRUE; break;
9398 case 'm': ++p; sec = TRUE; count *= 60; break;
9399 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02009400 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
9401 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009402 }
9403 }
9404
9405 if (*p != NUL)
9406 EMSG2(_(e_invarg2), eap->arg);
9407 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02009408 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
9409 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009410}
9411
9412/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009413 * ":redir": start/stop redirection.
9414 */
9415 static void
9416ex_redir(eap)
9417 exarg_T *eap;
9418{
9419 char *mode;
9420 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00009421 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009422
9423 if (STRICMP(eap->arg, "END") == 0)
9424 close_redir();
9425 else
9426 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009427 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009428 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009429 ++arg;
9430 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009431 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009432 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009433 mode = "a";
9434 }
9435 else
9436 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00009437 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009438
9439 close_redir();
9440
9441 /* Expand environment variables and "~/". */
Bram Moolenaarca472992005-01-21 11:46:23 +00009442 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009443 if (fname == NULL)
9444 return;
9445#ifdef FEAT_BROWSE
9446 if (cmdmod.browse)
9447 {
9448 char_u *browseFile;
9449
Bram Moolenaar7171abe2004-10-11 10:06:20 +00009450 browseFile = do_browse(BROWSE_SAVE,
9451 (char_u *)_("Save Redirection"),
9452 fname, NULL, NULL, BROWSE_FILTER_ALL_FILES, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009453 if (browseFile == NULL)
9454 return; /* operation cancelled */
9455 vim_free(fname);
9456 fname = browseFile;
9457 eap->forceit = TRUE; /* since dialog already asked */
9458 }
9459#endif
9460
9461 redir_fd = open_exfile(fname, eap->forceit, mode);
9462 vim_free(fname);
9463 }
9464#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00009465 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009466 {
9467 /* redirect to a register a-z (resp. A-Z for appending) */
9468 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00009469 ++arg;
9470 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009471# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00009472 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00009473 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00009474# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00009475 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009476 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009477 redir_reg = *arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009478 if (*arg == '>' && arg[1] == '>') /* append */
Bram Moolenaard9d30582005-05-18 22:10:28 +00009479 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009480 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009481 {
Bram Moolenaarc188b882007-10-19 14:20:54 +00009482 /* Can use both "@a" and "@a>". */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00009483 if (*arg == '>')
9484 arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009485 /* Make register empty when not using @A-@Z and the
9486 * command is valid. */
9487 if (*arg == NUL && !isupper(redir_reg))
9488 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009489 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00009490 }
9491 if (*arg != NUL)
9492 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00009493 redir_reg = 0;
Bram Moolenaard9d30582005-05-18 22:10:28 +00009494 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009495 }
9496 }
9497 else if (*arg == '=' && arg[1] == '>')
9498 {
9499 int append;
9500
9501 /* redirect to a variable */
9502 close_redir();
9503 arg += 2;
9504
9505 if (*arg == '>')
9506 {
9507 ++arg;
9508 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009509 }
9510 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00009511 append = FALSE;
9512
9513 if (var_redir_start(skipwhite(arg), append) == OK)
9514 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009515 }
9516#endif
9517
9518 /* TODO: redirect to a buffer */
9519
Bram Moolenaar071d4272004-06-13 20:20:40 +00009520 else
Bram Moolenaarca472992005-01-21 11:46:23 +00009521 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009522 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00009523
9524 /* Make sure redirection is not off. Can happen for cmdline completion
9525 * that indirectly invokes a command to catch its output. */
9526 if (redir_fd != NULL
9527#ifdef FEAT_EVAL
9528 || redir_reg || redir_vname
9529#endif
9530 )
9531 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009532}
9533
9534/*
9535 * ":redraw": force redraw
9536 */
9537 static void
9538ex_redraw(eap)
9539 exarg_T *eap;
9540{
9541 int r = RedrawingDisabled;
9542 int p = p_lz;
9543
9544 RedrawingDisabled = 0;
9545 p_lz = FALSE;
9546 update_topline();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009547 update_screen(eap->forceit ? CLEAR : VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009548#ifdef FEAT_TITLE
9549 if (need_maketitle)
9550 maketitle();
9551#endif
9552 RedrawingDisabled = r;
9553 p_lz = p;
9554
9555 /* Reset msg_didout, so that a message that's there is overwritten. */
9556 msg_didout = FALSE;
9557 msg_col = 0;
9558
Bram Moolenaar56667a52013-07-17 11:54:28 +02009559 /* No need to wait after an intentional redraw. */
9560 need_wait_return = FALSE;
9561
Bram Moolenaar071d4272004-06-13 20:20:40 +00009562 out_flush();
9563}
9564
9565/*
9566 * ":redrawstatus": force redraw of status line(s)
9567 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009568 static void
9569ex_redrawstatus(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009570 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009571{
9572#if defined(FEAT_WINDOWS)
9573 int r = RedrawingDisabled;
9574 int p = p_lz;
9575
9576 RedrawingDisabled = 0;
9577 p_lz = FALSE;
9578 if (eap->forceit)
9579 status_redraw_all();
9580 else
9581 status_redraw_curbuf();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009582 update_screen(VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009583 RedrawingDisabled = r;
9584 p_lz = p;
9585 out_flush();
9586#endif
9587}
9588
9589 static void
9590close_redir()
9591{
9592 if (redir_fd != NULL)
9593 {
9594 fclose(redir_fd);
9595 redir_fd = NULL;
9596 }
9597#ifdef FEAT_EVAL
9598 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009599 if (redir_vname)
9600 {
9601 var_redir_stop();
9602 redir_vname = 0;
9603 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009604#endif
9605}
9606
9607#if defined(FEAT_SESSION) && defined(USE_CRNL)
9608# define MKSESSION_NL
9609static int mksession_nl = FALSE; /* use NL only in put_eol() */
9610#endif
9611
9612/*
9613 * ":mkexrc", ":mkvimrc", ":mkview" and ":mksession".
9614 */
9615 static void
9616ex_mkrc(eap)
9617 exarg_T *eap;
9618{
9619 FILE *fd;
9620 int failed = FALSE;
9621 char_u *fname;
9622#ifdef FEAT_BROWSE
9623 char_u *browseFile = NULL;
9624#endif
9625#ifdef FEAT_SESSION
9626 int view_session = FALSE;
9627 int using_vdir = FALSE; /* using 'viewdir'? */
9628 char_u *viewFile = NULL;
9629 unsigned *flagp;
9630#endif
9631
9632 if (eap->cmdidx == CMD_mksession || eap->cmdidx == CMD_mkview)
9633 {
9634#ifdef FEAT_SESSION
9635 view_session = TRUE;
9636#else
9637 ex_ni(eap);
9638 return;
9639#endif
9640 }
9641
9642#ifdef FEAT_SESSION
Bram Moolenaar8d594672009-07-01 18:18:57 +00009643 /* Use the short file name until ":lcd" is used. We also don't use the
9644 * short file name when 'acd' is set, that is checked later. */
Bram Moolenaareeefcc72007-05-01 21:21:21 +00009645 did_lcd = FALSE;
9646
Bram Moolenaar071d4272004-06-13 20:20:40 +00009647 /* ":mkview" or ":mkview 9": generate file name with 'viewdir' */
9648 if (eap->cmdidx == CMD_mkview
9649 && (*eap->arg == NUL
9650 || (vim_isdigit(*eap->arg) && eap->arg[1] == NUL)))
9651 {
9652 eap->forceit = TRUE;
9653 fname = get_view_file(*eap->arg);
9654 if (fname == NULL)
9655 return;
9656 viewFile = fname;
9657 using_vdir = TRUE;
9658 }
9659 else
9660#endif
9661 if (*eap->arg != NUL)
9662 fname = eap->arg;
9663 else if (eap->cmdidx == CMD_mkvimrc)
9664 fname = (char_u *)VIMRC_FILE;
9665#ifdef FEAT_SESSION
9666 else if (eap->cmdidx == CMD_mksession)
9667 fname = (char_u *)SESSION_FILE;
9668#endif
9669 else
9670 fname = (char_u *)EXRC_FILE;
9671
9672#ifdef FEAT_BROWSE
9673 if (cmdmod.browse)
9674 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00009675 browseFile = do_browse(BROWSE_SAVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009676# ifdef FEAT_SESSION
9677 eap->cmdidx == CMD_mkview ? (char_u *)_("Save View") :
9678 eap->cmdidx == CMD_mksession ? (char_u *)_("Save Session") :
9679# endif
9680 (char_u *)_("Save Setup"),
9681 fname, (char_u *)"vim", NULL, BROWSE_FILTER_MACROS, NULL);
9682 if (browseFile == NULL)
9683 goto theend;
9684 fname = browseFile;
9685 eap->forceit = TRUE; /* since dialog already asked */
9686 }
9687#endif
9688
9689#if defined(FEAT_SESSION) && defined(vim_mkdir)
9690 /* When using 'viewdir' may have to create the directory. */
9691 if (using_vdir && !mch_isdir(p_vdir))
Bram Moolenaardf177f62005-02-22 08:39:57 +00009692 vim_mkdir_emsg(p_vdir, 0755);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009693#endif
9694
9695 fd = open_exfile(fname, eap->forceit, WRITEBIN);
9696 if (fd != NULL)
9697 {
9698#ifdef FEAT_SESSION
9699 if (eap->cmdidx == CMD_mkview)
9700 flagp = &vop_flags;
9701 else
9702 flagp = &ssop_flags;
9703#endif
9704
9705#ifdef MKSESSION_NL
9706 /* "unix" in 'sessionoptions': use NL line separator */
9707 if (view_session && (*flagp & SSOP_UNIX))
9708 mksession_nl = TRUE;
9709#endif
9710
9711 /* Write the version command for :mkvimrc */
9712 if (eap->cmdidx == CMD_mkvimrc)
9713 (void)put_line(fd, "version 6.0");
9714
9715#ifdef FEAT_SESSION
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009716 if (eap->cmdidx == CMD_mksession)
9717 {
9718 if (put_line(fd, "let SessionLoad = 1") == FAIL)
9719 failed = TRUE;
9720 }
9721
Bram Moolenaar071d4272004-06-13 20:20:40 +00009722 if (eap->cmdidx != CMD_mkview)
9723#endif
9724 {
9725 /* Write setting 'compatible' first, because it has side effects.
9726 * For that same reason only do it when needed. */
9727 if (p_cp)
9728 (void)put_line(fd, "if !&cp | set cp | endif");
9729 else
9730 (void)put_line(fd, "if &cp | set nocp | endif");
9731 }
9732
9733#ifdef FEAT_SESSION
9734 if (!view_session
9735 || (eap->cmdidx == CMD_mksession
9736 && (*flagp & SSOP_OPTIONS)))
9737#endif
9738 failed |= (makemap(fd, NULL) == FAIL
9739 || makeset(fd, OPT_GLOBAL, FALSE) == FAIL);
9740
9741#ifdef FEAT_SESSION
9742 if (!failed && view_session)
9743 {
9744 if (put_line(fd, "let s:so_save = &so | let s:siso_save = &siso | set so=0 siso=0") == FAIL)
9745 failed = TRUE;
9746 if (eap->cmdidx == CMD_mksession)
9747 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02009748 char_u *dirnow; /* current directory */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009749
Bram Moolenaard9462e32011-04-11 21:35:11 +02009750 dirnow = alloc(MAXPATHL);
9751 if (dirnow == NULL)
9752 failed = TRUE;
9753 else
9754 {
9755 /*
9756 * Change to session file's dir.
9757 */
9758 if (mch_dirname(dirnow, MAXPATHL) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00009759 || mch_chdir((char *)dirnow) != 0)
Bram Moolenaard9462e32011-04-11 21:35:11 +02009760 *dirnow = NUL;
9761 if (*dirnow != NUL && (ssop_flags & SSOP_SESDIR))
9762 {
9763 if (vim_chdirfile(fname) == OK)
9764 shorten_fnames(TRUE);
9765 }
9766 else if (*dirnow != NUL
9767 && (ssop_flags & SSOP_CURDIR) && globaldir != NULL)
9768 {
9769 if (mch_chdir((char *)globaldir) == 0)
9770 shorten_fnames(TRUE);
9771 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009772
Bram Moolenaard9462e32011-04-11 21:35:11 +02009773 failed |= (makeopens(fd, dirnow) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009774
Bram Moolenaard9462e32011-04-11 21:35:11 +02009775 /* restore original dir */
9776 if (*dirnow != NUL && ((ssop_flags & SSOP_SESDIR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009777 || ((ssop_flags & SSOP_CURDIR) && globaldir != NULL)))
Bram Moolenaard9462e32011-04-11 21:35:11 +02009778 {
9779 if (mch_chdir((char *)dirnow) != 0)
9780 EMSG(_(e_prev_dir));
9781 shorten_fnames(TRUE);
9782 }
9783 vim_free(dirnow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009784 }
9785 }
9786 else
9787 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +00009788 failed |= (put_view(fd, curwin, !using_vdir, flagp,
9789 -1) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009790 }
9791 if (put_line(fd, "let &so = s:so_save | let &siso = s:siso_save")
9792 == FAIL)
9793 failed = TRUE;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009794 if (put_line(fd, "doautoall SessionLoadPost") == FAIL)
9795 failed = TRUE;
Bram Moolenaar4770d092006-01-12 23:22:24 +00009796 if (eap->cmdidx == CMD_mksession)
9797 {
9798 if (put_line(fd, "unlet SessionLoad") == FAIL)
9799 failed = TRUE;
9800 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009801 }
9802#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009803 if (put_line(fd, "\" vim: set ft=vim :") == FAIL)
9804 failed = TRUE;
9805
Bram Moolenaar071d4272004-06-13 20:20:40 +00009806 failed |= fclose(fd);
9807
9808 if (failed)
9809 EMSG(_(e_write));
9810#if defined(FEAT_EVAL) && defined(FEAT_SESSION)
9811 else if (eap->cmdidx == CMD_mksession)
9812 {
9813 /* successful session write - set this_session var */
Bram Moolenaard9462e32011-04-11 21:35:11 +02009814 char_u *tbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009815
Bram Moolenaard9462e32011-04-11 21:35:11 +02009816 tbuf = alloc(MAXPATHL);
9817 if (tbuf != NULL)
9818 {
9819 if (vim_FullName(fname, tbuf, MAXPATHL, FALSE) == OK)
9820 set_vim_var_string(VV_THIS_SESSION, tbuf, -1);
9821 vim_free(tbuf);
9822 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009823 }
9824#endif
9825#ifdef MKSESSION_NL
9826 mksession_nl = FALSE;
9827#endif
9828 }
9829
9830#ifdef FEAT_BROWSE
9831theend:
9832 vim_free(browseFile);
9833#endif
9834#ifdef FEAT_SESSION
9835 vim_free(viewFile);
9836#endif
9837}
9838
Bram Moolenaardf177f62005-02-22 08:39:57 +00009839#if ((defined(FEAT_SESSION) || defined(FEAT_EVAL)) && defined(vim_mkdir)) \
9840 || defined(PROTO)
9841 int
9842vim_mkdir_emsg(name, prot)
9843 char_u *name;
Bram Moolenaar78a15312009-05-15 19:33:18 +00009844 int prot UNUSED;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009845{
9846 if (vim_mkdir(name, prot) != 0)
9847 {
9848 EMSG2(_("E739: Cannot create directory: %s"), name);
9849 return FAIL;
9850 }
9851 return OK;
9852}
9853#endif
9854
Bram Moolenaar071d4272004-06-13 20:20:40 +00009855/*
9856 * Open a file for writing for an Ex command, with some checks.
9857 * Return file descriptor, or NULL on failure.
9858 */
9859 FILE *
9860open_exfile(fname, forceit, mode)
9861 char_u *fname;
9862 int forceit;
9863 char *mode; /* "w" for create new file or "a" for append */
9864{
9865 FILE *fd;
9866
9867#ifdef UNIX
9868 /* with Unix it is possible to open a directory */
9869 if (mch_isdir(fname))
9870 {
9871 EMSG2(_(e_isadir2), fname);
9872 return NULL;
9873 }
9874#endif
9875 if (!forceit && *mode != 'a' && vim_fexists(fname))
9876 {
9877 EMSG2(_("E189: \"%s\" exists (add ! to override)"), fname);
9878 return NULL;
9879 }
9880
9881 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
9882 EMSG2(_("E190: Cannot open \"%s\" for writing"), fname);
9883
9884 return fd;
9885}
9886
9887/*
9888 * ":mark" and ":k".
9889 */
9890 static void
9891ex_mark(eap)
9892 exarg_T *eap;
9893{
9894 pos_T pos;
9895
9896 if (*eap->arg == NUL) /* No argument? */
9897 EMSG(_(e_argreq));
9898 else if (eap->arg[1] != NUL) /* more than one character? */
9899 EMSG(_(e_trailing));
9900 else
9901 {
9902 pos = curwin->w_cursor; /* save curwin->w_cursor */
9903 curwin->w_cursor.lnum = eap->line2;
9904 beginline(BL_WHITE | BL_FIX);
9905 if (setmark(*eap->arg) == FAIL) /* set mark */
9906 EMSG(_("E191: Argument must be a letter or forward/backward quote"));
9907 curwin->w_cursor = pos; /* restore curwin->w_cursor */
9908 }
9909}
9910
9911/*
9912 * Update w_topline, w_leftcol and the cursor position.
9913 */
9914 void
9915update_topline_cursor()
9916{
9917 check_cursor(); /* put cursor on valid line */
9918 update_topline();
9919 if (!curwin->w_p_wrap)
9920 validate_cursor();
9921 update_curswant();
9922}
9923
9924#ifdef FEAT_EX_EXTRA
9925/*
9926 * ":normal[!] {commands}": Execute normal mode commands.
9927 */
9928 static void
9929ex_normal(eap)
9930 exarg_T *eap;
9931{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009932 int save_msg_scroll = msg_scroll;
9933 int save_restart_edit = restart_edit;
9934 int save_msg_didout = msg_didout;
9935 int save_State = State;
9936 tasave_T tabuf;
9937 int save_insertmode = p_im;
9938 int save_finish_op = finish_op;
Bram Moolenaar38084112008-07-26 14:05:07 +00009939 int save_opcount = opcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009940#ifdef FEAT_MBYTE
9941 char_u *arg = NULL;
9942 int l;
9943 char_u *p;
9944#endif
9945
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00009946 if (ex_normal_lock > 0)
9947 {
9948 EMSG(_(e_secure));
9949 return;
9950 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009951 if (ex_normal_busy >= p_mmd)
9952 {
9953 EMSG(_("E192: Recursive use of :normal too deep"));
9954 return;
9955 }
9956 ++ex_normal_busy;
9957
9958 msg_scroll = FALSE; /* no msg scrolling in Normal mode */
9959 restart_edit = 0; /* don't go to Insert mode */
9960 p_im = FALSE; /* don't use 'insertmode' */
9961
9962#ifdef FEAT_MBYTE
9963 /*
9964 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
9965 * this for the K_SPECIAL leading byte, otherwise special keys will not
9966 * work.
9967 */
9968 if (has_mbyte)
9969 {
9970 int len = 0;
9971
9972 /* Count the number of characters to be escaped. */
9973 for (p = eap->arg; *p != NUL; ++p)
9974 {
9975# ifdef FEAT_GUI
9976 if (*p == CSI) /* leadbyte CSI */
9977 len += 2;
9978# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009979 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009980 if (*++p == K_SPECIAL /* trailbyte K_SPECIAL or CSI */
9981# ifdef FEAT_GUI
9982 || *p == CSI
9983# endif
9984 )
9985 len += 2;
9986 }
9987 if (len > 0)
9988 {
9989 arg = alloc((unsigned)(STRLEN(eap->arg) + len + 1));
9990 if (arg != NULL)
9991 {
9992 len = 0;
9993 for (p = eap->arg; *p != NUL; ++p)
9994 {
9995 arg[len++] = *p;
9996# ifdef FEAT_GUI
9997 if (*p == CSI)
9998 {
9999 arg[len++] = KS_EXTRA;
10000 arg[len++] = (int)KE_CSI;
10001 }
10002# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010003 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010004 {
10005 arg[len++] = *++p;
10006 if (*p == K_SPECIAL)
10007 {
10008 arg[len++] = KS_SPECIAL;
10009 arg[len++] = KE_FILLER;
10010 }
10011# ifdef FEAT_GUI
10012 else if (*p == CSI)
10013 {
10014 arg[len++] = KS_EXTRA;
10015 arg[len++] = (int)KE_CSI;
10016 }
10017# endif
10018 }
10019 arg[len] = NUL;
10020 }
10021 }
10022 }
10023 }
10024#endif
10025
10026 /*
10027 * Save the current typeahead. This is required to allow using ":normal"
10028 * from an event handler and makes sure we don't hang when the argument
10029 * ends with half a command.
10030 */
10031 save_typeahead(&tabuf);
10032 if (tabuf.typebuf_valid)
10033 {
10034 /*
10035 * Repeat the :normal command for each line in the range. When no
10036 * range given, execute it just once, without positioning the cursor
10037 * first.
10038 */
10039 do
10040 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010041 if (eap->addr_count != 0)
10042 {
10043 curwin->w_cursor.lnum = eap->line1++;
10044 curwin->w_cursor.col = 0;
10045 }
10046
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010047 exec_normal_cmd(
Bram Moolenaar071d4272004-06-13 20:20:40 +000010048#ifdef FEAT_MBYTE
10049 arg != NULL ? arg :
10050#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010051 eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010052 }
10053 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
10054 }
10055
10056 /* Might not return to the main loop when in an event handler. */
10057 update_topline_cursor();
10058
10059 /* Restore the previous typeahead. */
10060 restore_typeahead(&tabuf);
10061
10062 --ex_normal_busy;
10063 msg_scroll = save_msg_scroll;
10064 restart_edit = save_restart_edit;
10065 p_im = save_insertmode;
10066 finish_op = save_finish_op;
Bram Moolenaar38084112008-07-26 14:05:07 +000010067 opcount = save_opcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010068 msg_didout |= save_msg_didout; /* don't reset msg_didout now */
10069
10070 /* Restore the state (needed when called from a function executed for
Bram Moolenaareda73602014-11-05 09:53:23 +010010071 * 'indentexpr'). Update the mouse and cursor, they may have changed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010072 State = save_State;
Bram Moolenaareda73602014-11-05 09:53:23 +010010073#ifdef FEAT_MOUSE
10074 setmouse();
10075#endif
10076#ifdef CURSOR_SHAPE
10077 ui_cursor_shape(); /* may show different cursor shape */
10078#endif
10079
Bram Moolenaar071d4272004-06-13 20:20:40 +000010080#ifdef FEAT_MBYTE
10081 vim_free(arg);
10082#endif
10083}
10084
10085/*
Bram Moolenaar64969662005-12-14 21:59:55 +000010086 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010087 */
10088 static void
10089ex_startinsert(eap)
10090 exarg_T *eap;
10091{
Bram Moolenaarfd371682005-01-14 21:42:54 +000010092 if (eap->forceit)
10093 {
10094 coladvance((colnr_T)MAXCOL);
10095 curwin->w_curswant = MAXCOL;
10096 curwin->w_set_curswant = FALSE;
10097 }
10098
Bram Moolenaara40c5002005-01-09 21:16:21 +000010099 /* Ignore the command when already in Insert mode. Inserting an
10100 * expression register that invokes a function can do this. */
10101 if (State & INSERT)
10102 return;
10103
Bram Moolenaar64969662005-12-14 21:59:55 +000010104 if (eap->cmdidx == CMD_startinsert)
10105 restart_edit = 'a';
10106 else if (eap->cmdidx == CMD_startreplace)
10107 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010108 else
Bram Moolenaar64969662005-12-14 21:59:55 +000010109 restart_edit = 'V';
10110
10111 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010112 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010113 if (eap->cmdidx == CMD_startinsert)
10114 restart_edit = 'i';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010115 curwin->w_curswant = 0; /* avoid MAXCOL */
10116 }
10117}
10118
10119/*
10120 * ":stopinsert"
10121 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010122 static void
10123ex_stopinsert(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010124 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010125{
10126 restart_edit = 0;
10127 stop_insert_mode = TRUE;
10128}
10129#endif
10130
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010131#if defined(FEAT_EX_EXTRA) || defined(FEAT_MENU) || defined(PROTO)
10132/*
10133 * Execute normal mode command "cmd".
10134 * "remap" can be REMAP_NONE or REMAP_YES.
10135 */
10136 void
10137exec_normal_cmd(cmd, remap, silent)
10138 char_u *cmd;
10139 int remap;
10140 int silent;
10141{
10142 oparg_T oa;
10143
10144 /*
10145 * Stuff the argument into the typeahead buffer.
10146 * Execute normal_cmd() until there is no typeahead left.
10147 */
10148 clear_oparg(&oa);
10149 finish_op = FALSE;
10150 ins_typebuf(cmd, remap, 0, TRUE, silent);
10151 while ((!stuff_empty() || (!typebuf_typed() && typebuf.tb_len > 0))
10152 && !got_int)
10153 {
10154 update_topline_cursor();
Bram Moolenaar48ac02c2011-01-17 19:50:06 +010010155 normal_cmd(&oa, TRUE); /* execute a Normal mode cmd */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010156 }
10157}
10158#endif
10159
Bram Moolenaar071d4272004-06-13 20:20:40 +000010160#ifdef FEAT_FIND_ID
10161 static void
10162ex_checkpath(eap)
10163 exarg_T *eap;
10164{
10165 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
10166 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
10167 (linenr_T)1, (linenr_T)MAXLNUM);
10168}
10169
10170#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
10171/*
10172 * ":psearch"
10173 */
10174 static void
10175ex_psearch(eap)
10176 exarg_T *eap;
10177{
10178 g_do_tagpreview = p_pvh;
10179 ex_findpat(eap);
10180 g_do_tagpreview = 0;
10181}
10182#endif
10183
10184 static void
10185ex_findpat(eap)
10186 exarg_T *eap;
10187{
10188 int whole = TRUE;
10189 long n;
10190 char_u *p;
10191 int action;
10192
10193 switch (cmdnames[eap->cmdidx].cmd_name[2])
10194 {
10195 case 'e': /* ":psearch", ":isearch" and ":dsearch" */
10196 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
10197 action = ACTION_GOTO;
10198 else
10199 action = ACTION_SHOW;
10200 break;
10201 case 'i': /* ":ilist" and ":dlist" */
10202 action = ACTION_SHOW_ALL;
10203 break;
10204 case 'u': /* ":ijump" and ":djump" */
10205 action = ACTION_GOTO;
10206 break;
10207 default: /* ":isplit" and ":dsplit" */
10208 action = ACTION_SPLIT;
10209 break;
10210 }
10211
10212 n = 1;
10213 if (vim_isdigit(*eap->arg)) /* get count */
10214 {
10215 n = getdigits(&eap->arg);
10216 eap->arg = skipwhite(eap->arg);
10217 }
10218 if (*eap->arg == '/') /* Match regexp, not just whole words */
10219 {
10220 whole = FALSE;
10221 ++eap->arg;
10222 p = skip_regexp(eap->arg, '/', p_magic, NULL);
10223 if (*p)
10224 {
10225 *p++ = NUL;
10226 p = skipwhite(p);
10227
10228 /* Check for trailing illegal characters */
10229 if (!ends_excmd(*p))
10230 eap->errmsg = e_trailing;
10231 else
10232 eap->nextcmd = check_nextcmd(p);
10233 }
10234 }
10235 if (!eap->skip)
10236 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
10237 whole, !eap->forceit,
10238 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
10239 n, action, eap->line1, eap->line2);
10240}
10241#endif
10242
10243#ifdef FEAT_WINDOWS
10244
10245# ifdef FEAT_QUICKFIX
10246/*
10247 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
10248 */
10249 static void
10250ex_ptag(eap)
10251 exarg_T *eap;
10252{
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +010010253 g_do_tagpreview = p_pvh; /* will be reset to 0 in ex_tag_cmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010254 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
10255}
10256
10257/*
10258 * ":pedit"
10259 */
10260 static void
10261ex_pedit(eap)
10262 exarg_T *eap;
10263{
10264 win_T *curwin_save = curwin;
10265
10266 g_do_tagpreview = p_pvh;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +000010267 prepare_tagpreview(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010268 keep_help_flag = curwin_save->w_buffer->b_help;
10269 do_exedit(eap, NULL);
10270 keep_help_flag = FALSE;
10271 if (curwin != curwin_save && win_valid(curwin_save))
10272 {
10273 /* Return cursor to where we were */
10274 validate_cursor();
10275 redraw_later(VALID);
10276 win_enter(curwin_save, TRUE);
10277 }
10278 g_do_tagpreview = 0;
10279}
10280# endif
10281
10282/*
10283 * ":stag", ":stselect" and ":stjump".
10284 */
10285 static void
10286ex_stag(eap)
10287 exarg_T *eap;
10288{
10289 postponed_split = -1;
10290 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +000010291 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010292 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
10293 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +000010294 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010295}
10296#endif
10297
10298/*
10299 * ":tag", ":tselect", ":tjump", ":tnext", etc.
10300 */
10301 static void
10302ex_tag(eap)
10303 exarg_T *eap;
10304{
10305 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
10306}
10307
10308 static void
10309ex_tag_cmd(eap, name)
10310 exarg_T *eap;
10311 char_u *name;
10312{
10313 int cmd;
10314
10315 switch (name[1])
10316 {
10317 case 'j': cmd = DT_JUMP; /* ":tjump" */
10318 break;
10319 case 's': cmd = DT_SELECT; /* ":tselect" */
10320 break;
10321 case 'p': cmd = DT_PREV; /* ":tprevious" */
10322 break;
10323 case 'N': cmd = DT_PREV; /* ":tNext" */
10324 break;
10325 case 'n': cmd = DT_NEXT; /* ":tnext" */
10326 break;
10327 case 'o': cmd = DT_POP; /* ":pop" */
10328 break;
10329 case 'f': /* ":tfirst" */
10330 case 'r': cmd = DT_FIRST; /* ":trewind" */
10331 break;
10332 case 'l': cmd = DT_LAST; /* ":tlast" */
10333 break;
10334 default: /* ":tag" */
10335#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +000010336 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010337 {
10338 do_cstag(eap);
10339 return;
10340 }
10341#endif
10342 cmd = DT_TAG;
10343 break;
10344 }
10345
Bram Moolenaarb8a7b562006-02-01 21:47:16 +000010346 if (name[0] == 'l')
10347 {
10348#ifndef FEAT_QUICKFIX
10349 ex_ni(eap);
10350 return;
10351#else
10352 cmd = DT_LTAG;
10353#endif
10354 }
10355
Bram Moolenaar071d4272004-06-13 20:20:40 +000010356 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
10357 eap->forceit, TRUE);
10358}
10359
10360/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010361 * Check "str" for starting with a special cmdline variable.
10362 * If found return one of the SPEC_ values and set "*usedlen" to the length of
10363 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
10364 */
10365 int
10366find_cmdline_var(src, usedlen)
10367 char_u *src;
10368 int *usedlen;
10369{
10370 int len;
10371 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +000010372 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010373 "%",
10374#define SPEC_PERC 0
10375 "#",
10376#define SPEC_HASH 1
10377 "<cword>", /* cursor word */
10378#define SPEC_CWORD 2
10379 "<cWORD>", /* cursor WORD */
10380#define SPEC_CCWORD 3
10381 "<cfile>", /* cursor path name */
10382#define SPEC_CFILE 4
10383 "<sfile>", /* ":so" file name */
10384#define SPEC_SFILE 5
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010385 "<slnum>", /* ":so" file line number */
10386#define SPEC_SLNUM 6
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010387#ifdef FEAT_AUTOCMD
10388 "<afile>", /* autocommand file name */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010389# define SPEC_AFILE 7
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010390 "<abuf>", /* autocommand buffer number */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010391# define SPEC_ABUF 8
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010392 "<amatch>", /* autocommand match name */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010393# define SPEC_AMATCH 9
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010394#endif
10395#ifdef FEAT_CLIENTSERVER
10396 "<client>"
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010397# ifdef FEAT_AUTOCMD
10398# define SPEC_CLIENT 10
10399# else
10400# define SPEC_CLIENT 7
10401# endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010402#endif
10403 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010404
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000010405 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010406 {
10407 len = (int)STRLEN(spec_str[i]);
10408 if (STRNCMP(src, spec_str[i], len) == 0)
10409 {
10410 *usedlen = len;
10411 return i;
10412 }
10413 }
10414 return -1;
10415}
10416
10417/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010418 * Evaluate cmdline variables.
10419 *
10420 * change '%' to curbuf->b_ffname
10421 * '#' to curwin->w_altfile
10422 * '<cword>' to word under the cursor
10423 * '<cWORD>' to WORD under the cursor
10424 * '<cfile>' to path name under the cursor
10425 * '<sfile>' to sourced file name
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010426 * '<slnum>' to sourced file line number
Bram Moolenaar071d4272004-06-13 20:20:40 +000010427 * '<afile>' to file name for autocommand
10428 * '<abuf>' to buffer number for autocommand
10429 * '<amatch>' to matching name for autocommand
10430 *
10431 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
10432 * "" for error without a message) and NULL is returned.
10433 * Returns an allocated string if a valid match was found.
10434 * Returns NULL if no match was found. "usedlen" then still contains the
10435 * number of characters to skip.
10436 */
10437 char_u *
Bram Moolenaar63b92542007-03-27 14:57:09 +000010438eval_vars(src, srcstart, usedlen, lnump, errormsg, escaped)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010439 char_u *src; /* pointer into commandline */
Bram Moolenaar63b92542007-03-27 14:57:09 +000010440 char_u *srcstart; /* beginning of valid memory for src */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010441 int *usedlen; /* characters after src that are used */
10442 linenr_T *lnump; /* line number for :e command, or NULL */
10443 char_u **errormsg; /* pointer to error message */
Bram Moolenaar63b92542007-03-27 14:57:09 +000010444 int *escaped; /* return value has escaped white space (can
10445 * be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010446{
10447 int i;
10448 char_u *s;
10449 char_u *result;
10450 char_u *resultbuf = NULL;
10451 int resultlen;
10452 buf_T *buf;
10453 int valid = VALID_HEAD + VALID_PATH; /* assume valid result */
10454 int spec_idx;
10455#ifdef FEAT_MODIFY_FNAME
10456 int skip_mod = FALSE;
10457#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010458 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010459
10460 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +000010461 if (escaped != NULL)
10462 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010463
10464 /*
10465 * Check if there is something to do.
10466 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010467 spec_idx = find_cmdline_var(src, usedlen);
10468 if (spec_idx < 0) /* no match */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010469 {
10470 *usedlen = 1;
10471 return NULL;
10472 }
10473
10474 /*
10475 * Skip when preceded with a backslash "\%" and "\#".
10476 * Note: In "\\%" the % is also not recognized!
10477 */
10478 if (src > srcstart && src[-1] == '\\')
10479 {
10480 *usedlen = 0;
Bram Moolenaara7241f52008-06-24 20:39:31 +000010481 STRMOVE(src - 1, src); /* remove backslash */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010482 return NULL;
10483 }
10484
10485 /*
10486 * word or WORD under cursor
10487 */
10488 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD)
10489 {
10490 resultlen = find_ident_under_cursor(&result, spec_idx == SPEC_CWORD ?
10491 (FIND_IDENT|FIND_STRING) : FIND_STRING);
10492 if (resultlen == 0)
10493 {
10494 *errormsg = (char_u *)"";
10495 return NULL;
10496 }
10497 }
10498
10499 /*
10500 * '#': Alternate file name
10501 * '%': Current file name
10502 * File name under the cursor
10503 * File name for autocommand
10504 * and following modifiers
10505 */
10506 else
10507 {
10508 switch (spec_idx)
10509 {
10510 case SPEC_PERC: /* '%': current file */
10511 if (curbuf->b_fname == NULL)
10512 {
10513 result = (char_u *)"";
10514 valid = 0; /* Must have ":p:h" to be valid */
10515 }
10516 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010517 result = curbuf->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010518 break;
10519
10520 case SPEC_HASH: /* '#' or "#99": alternate file */
10521 if (src[1] == '#') /* "##": the argument list */
10522 {
10523 result = arg_all();
10524 resultbuf = result;
10525 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +000010526 if (escaped != NULL)
10527 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010528#ifdef FEAT_MODIFY_FNAME
10529 skip_mod = TRUE;
10530#endif
10531 break;
10532 }
10533 s = src + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +000010534 if (*s == '<') /* "#<99" uses v:oldfiles */
10535 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010536 i = (int)getdigits(&s);
10537 *usedlen = (int)(s - src); /* length of what we expand */
10538
Bram Moolenaard812df62008-11-09 12:46:09 +000010539 if (src[1] == '<')
Bram Moolenaar071d4272004-06-13 20:20:40 +000010540 {
Bram Moolenaard812df62008-11-09 12:46:09 +000010541 if (*usedlen < 2)
10542 {
10543 /* Should we give an error message for #<text? */
10544 *usedlen = 1;
10545 return NULL;
10546 }
10547#ifdef FEAT_EVAL
10548 result = list_find_str(get_vim_var_list(VV_OLDFILES),
10549 (long)i);
10550 if (result == NULL)
10551 {
10552 *errormsg = (char_u *)"";
10553 return NULL;
10554 }
10555#else
10556 *errormsg = (char_u *)_("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +000010557 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +000010558#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010559 }
10560 else
Bram Moolenaard812df62008-11-09 12:46:09 +000010561 {
10562 buf = buflist_findnr(i);
10563 if (buf == NULL)
10564 {
10565 *errormsg = (char_u *)_("E194: No alternate file name to substitute for '#'");
10566 return NULL;
10567 }
10568 if (lnump != NULL)
10569 *lnump = ECMD_LAST;
10570 if (buf->b_fname == NULL)
10571 {
10572 result = (char_u *)"";
10573 valid = 0; /* Must have ":p:h" to be valid */
10574 }
10575 else
10576 result = buf->b_fname;
10577 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010578 break;
10579
10580#ifdef FEAT_SEARCHPATH
10581 case SPEC_CFILE: /* file name under cursor */
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010582 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010583 if (result == NULL)
10584 {
10585 *errormsg = (char_u *)"";
10586 return NULL;
10587 }
10588 resultbuf = result; /* remember allocated string */
10589 break;
10590#endif
10591
10592#ifdef FEAT_AUTOCMD
10593 case SPEC_AFILE: /* file name for autocommand */
10594 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +000010595 if (result != NULL && !autocmd_fname_full)
10596 {
10597 /* Still need to turn the fname into a full path. It is
10598 * postponed to avoid a delay when <afile> is not used. */
10599 autocmd_fname_full = TRUE;
10600 result = FullName_save(autocmd_fname, FALSE);
10601 vim_free(autocmd_fname);
10602 autocmd_fname = result;
10603 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010604 if (result == NULL)
10605 {
10606 *errormsg = (char_u *)_("E495: no autocommand file name to substitute for \"<afile>\"");
10607 return NULL;
10608 }
Bram Moolenaara0174af2008-01-02 20:08:25 +000010609 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010610 break;
10611
10612 case SPEC_ABUF: /* buffer number for autocommand */
10613 if (autocmd_bufnr <= 0)
10614 {
10615 *errormsg = (char_u *)_("E496: no autocommand buffer number to substitute for \"<abuf>\"");
10616 return NULL;
10617 }
10618 sprintf((char *)strbuf, "%d", autocmd_bufnr);
10619 result = strbuf;
10620 break;
10621
10622 case SPEC_AMATCH: /* match name for autocommand */
10623 result = autocmd_match;
10624 if (result == NULL)
10625 {
10626 *errormsg = (char_u *)_("E497: no autocommand match name to substitute for \"<amatch>\"");
10627 return NULL;
10628 }
10629 break;
10630
10631#endif
10632 case SPEC_SFILE: /* file name for ":so" command */
10633 result = sourcing_name;
10634 if (result == NULL)
10635 {
10636 *errormsg = (char_u *)_("E498: no :source file name to substitute for \"<sfile>\"");
10637 return NULL;
10638 }
10639 break;
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010640 case SPEC_SLNUM: /* line in file for ":so" command */
10641 if (sourcing_name == NULL || sourcing_lnum == 0)
10642 {
10643 *errormsg = (char_u *)_("E842: no line number to use for \"<slnum>\"");
10644 return NULL;
10645 }
10646 sprintf((char *)strbuf, "%ld", (long)sourcing_lnum);
10647 result = strbuf;
10648 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010649#if defined(FEAT_CLIENTSERVER)
10650 case SPEC_CLIENT: /* Source of last submitted input */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010651 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
10652 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010653 result = strbuf;
10654 break;
10655#endif
10656 }
10657
10658 resultlen = (int)STRLEN(result); /* length of new string */
10659 if (src[*usedlen] == '<') /* remove the file name extension */
10660 {
10661 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010662 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010663 resultlen = (int)(s - result);
10664 }
10665#ifdef FEAT_MODIFY_FNAME
10666 else if (!skip_mod)
10667 {
10668 valid |= modify_fname(src, usedlen, &result, &resultbuf,
10669 &resultlen);
10670 if (result == NULL)
10671 {
10672 *errormsg = (char_u *)"";
10673 return NULL;
10674 }
10675 }
10676#endif
10677 }
10678
10679 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
10680 {
10681 if (valid != VALID_HEAD + VALID_PATH)
10682 /* xgettext:no-c-format */
10683 *errormsg = (char_u *)_("E499: Empty file name for '%' or '#', only works with \":p:h\"");
10684 else
10685 *errormsg = (char_u *)_("E500: Evaluates to an empty string");
10686 result = NULL;
10687 }
10688 else
10689 result = vim_strnsave(result, resultlen);
10690 vim_free(resultbuf);
10691 return result;
10692}
10693
10694/*
10695 * Concatenate all files in the argument list, separated by spaces, and return
10696 * it in one allocated string.
10697 * Spaces and backslashes in the file names are escaped with a backslash.
10698 * Returns NULL when out of memory.
10699 */
10700 static char_u *
10701arg_all()
10702{
10703 int len;
10704 int idx;
10705 char_u *retval = NULL;
10706 char_u *p;
10707
10708 /*
10709 * Do this loop two times:
10710 * first time: compute the total length
10711 * second time: concatenate the names
10712 */
10713 for (;;)
10714 {
10715 len = 0;
10716 for (idx = 0; idx < ARGCOUNT; ++idx)
10717 {
10718 p = alist_name(&ARGLIST[idx]);
10719 if (p != NULL)
10720 {
10721 if (len > 0)
10722 {
10723 /* insert a space in between names */
10724 if (retval != NULL)
10725 retval[len] = ' ';
10726 ++len;
10727 }
10728 for ( ; *p != NUL; ++p)
10729 {
10730 if (*p == ' ' || *p == '\\')
10731 {
10732 /* insert a backslash */
10733 if (retval != NULL)
10734 retval[len] = '\\';
10735 ++len;
10736 }
10737 if (retval != NULL)
10738 retval[len] = *p;
10739 ++len;
10740 }
10741 }
10742 }
10743
10744 /* second time: break here */
10745 if (retval != NULL)
10746 {
10747 retval[len] = NUL;
10748 break;
10749 }
10750
10751 /* allocate memory */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000010752 retval = alloc((unsigned)len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010753 if (retval == NULL)
10754 break;
10755 }
10756
10757 return retval;
10758}
10759
10760#if defined(FEAT_AUTOCMD) || defined(PROTO)
10761/*
10762 * Expand the <sfile> string in "arg".
10763 *
10764 * Returns an allocated string, or NULL for any error.
10765 */
10766 char_u *
10767expand_sfile(arg)
10768 char_u *arg;
10769{
10770 char_u *errormsg;
10771 int len;
10772 char_u *result;
10773 char_u *newres;
10774 char_u *repl;
10775 int srclen;
10776 char_u *p;
10777
10778 result = vim_strsave(arg);
10779 if (result == NULL)
10780 return NULL;
10781
10782 for (p = result; *p; )
10783 {
10784 if (STRNCMP(p, "<sfile>", 7) != 0)
10785 ++p;
10786 else
10787 {
10788 /* replace "<sfile>" with the sourced file name, and do ":" stuff */
Bram Moolenaar63b92542007-03-27 14:57:09 +000010789 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010790 if (errormsg != NULL)
10791 {
10792 if (*errormsg)
10793 emsg(errormsg);
10794 vim_free(result);
10795 return NULL;
10796 }
10797 if (repl == NULL) /* no match (cannot happen) */
10798 {
10799 p += srclen;
10800 continue;
10801 }
10802 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
10803 newres = alloc(len);
10804 if (newres == NULL)
10805 {
10806 vim_free(repl);
10807 vim_free(result);
10808 return NULL;
10809 }
10810 mch_memmove(newres, result, (size_t)(p - result));
10811 STRCPY(newres + (p - result), repl);
10812 len = (int)STRLEN(newres);
10813 STRCAT(newres, p + srclen);
10814 vim_free(repl);
10815 vim_free(result);
10816 result = newres;
10817 p = newres + len; /* continue after the match */
10818 }
10819 }
10820
10821 return result;
10822}
10823#endif
10824
10825#ifdef FEAT_SESSION
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010826static int ses_winsizes __ARGS((FILE *fd, int restore_size,
10827 win_T *tab_firstwin));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010828static int ses_win_rec __ARGS((FILE *fd, frame_T *fr));
10829static frame_T *ses_skipframe __ARGS((frame_T *fr));
10830static int ses_do_frame __ARGS((frame_T *fr));
10831static int ses_do_win __ARGS((win_T *wp));
10832static int ses_arglist __ARGS((FILE *fd, char *cmd, garray_T *gap, int fullname, unsigned *flagp));
10833static int ses_put_fname __ARGS((FILE *fd, char_u *name, unsigned *flagp));
10834static int ses_fname __ARGS((FILE *fd, buf_T *buf, unsigned *flagp));
10835
10836/*
10837 * Write openfile commands for the current buffers to an .exrc file.
10838 * Return FAIL on error, OK otherwise.
10839 */
10840 static int
10841makeopens(fd, dirnow)
10842 FILE *fd;
10843 char_u *dirnow; /* Current directory name */
10844{
10845 buf_T *buf;
10846 int only_save_windows = TRUE;
10847 int nr;
10848 int cnr = 1;
10849 int restore_size = TRUE;
10850 win_T *wp;
10851 char_u *sname;
10852 win_T *edited_win = NULL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010853 int tabnr;
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020010854 int restore_stal = FALSE;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010855 win_T *tab_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010856 frame_T *tab_topframe;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010857 int cur_arg_idx = 0;
Bram Moolenaar383c6f52008-01-04 15:01:07 +000010858 int next_arg_idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010859
10860 if (ssop_flags & SSOP_BUFFERS)
10861 only_save_windows = FALSE; /* Save ALL buffers */
10862
10863 /*
10864 * Begin by setting the this_session variable, and then other
10865 * sessionable variables.
10866 */
10867#ifdef FEAT_EVAL
10868 if (put_line(fd, "let v:this_session=expand(\"<sfile>:p\")") == FAIL)
10869 return FAIL;
10870 if (ssop_flags & SSOP_GLOBALS)
10871 if (store_session_globals(fd) == FAIL)
10872 return FAIL;
10873#endif
10874
10875 /*
10876 * Close all windows but one.
10877 */
10878 if (put_line(fd, "silent only") == FAIL)
10879 return FAIL;
10880
10881 /*
10882 * Now a :cd command to the session directory or the current directory
10883 */
10884 if (ssop_flags & SSOP_SESDIR)
10885 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010886 if (put_line(fd, "exe \"cd \" . escape(expand(\"<sfile>:p:h\"), ' ')")
10887 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010888 return FAIL;
10889 }
10890 else if (ssop_flags & SSOP_CURDIR)
10891 {
10892 sname = home_replace_save(NULL, globaldir != NULL ? globaldir : dirnow);
10893 if (sname == NULL
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010894 || fputs("cd ", fd) < 0
10895 || ses_put_fname(fd, sname, &ssop_flags) == FAIL
10896 || put_eol(fd) == FAIL)
10897 {
10898 vim_free(sname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010899 return FAIL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010900 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010901 vim_free(sname);
10902 }
10903
10904 /*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010905 * If there is an empty, unnamed buffer we will wipe it out later.
10906 * Remember the buffer number.
10907 */
10908 if (put_line(fd, "if expand('%') == '' && !&modified && line('$') <= 1 && getline(1) == ''") == FAIL)
10909 return FAIL;
10910 if (put_line(fd, " let s:wipebuf = bufnr('%')") == FAIL)
10911 return FAIL;
10912 if (put_line(fd, "endif") == FAIL)
10913 return FAIL;
10914
10915 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010916 * Now save the current files, current buffer first.
10917 */
10918 if (put_line(fd, "set shortmess=aoO") == FAIL)
10919 return FAIL;
10920
10921 /* Now put the other buffers into the buffer list */
10922 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
10923 {
10924 if (!(only_save_windows && buf->b_nwindows == 0)
10925 && !(buf->b_help && !(ssop_flags & SSOP_HELP))
10926 && buf->b_fname != NULL
10927 && buf->b_p_bl)
10928 {
10929 if (fprintf(fd, "badd +%ld ", buf->b_wininfo == NULL ? 1L
10930 : buf->b_wininfo->wi_fpos.lnum) < 0
10931 || ses_fname(fd, buf, &ssop_flags) == FAIL)
10932 return FAIL;
10933 }
10934 }
10935
10936 /* the global argument list */
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010010937 if (ses_arglist(fd, "argglobal", &global_alist.al_ga,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010938 !(ssop_flags & SSOP_CURDIR), &ssop_flags) == FAIL)
10939 return FAIL;
10940
10941 if (ssop_flags & SSOP_RESIZE)
10942 {
10943 /* Note: after the restore we still check it worked!*/
10944 if (fprintf(fd, "set lines=%ld columns=%ld" , Rows, Columns) < 0
10945 || put_eol(fd) == FAIL)
10946 return FAIL;
10947 }
10948
10949#ifdef FEAT_GUI
10950 if (gui.in_use && (ssop_flags & SSOP_WINPOS))
10951 {
10952 int x, y;
10953
10954 if (gui_mch_get_winpos(&x, &y) == OK)
10955 {
10956 /* Note: after the restore we still check it worked!*/
10957 if (fprintf(fd, "winpos %d %d", x, y) < 0 || put_eol(fd) == FAIL)
10958 return FAIL;
10959 }
10960 }
10961#endif
10962
10963 /*
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020010964 * When there are two or more tabpages and 'showtabline' is 1 the tabline
10965 * will be displayed when creating the next tab. That resizes the windows
10966 * in the first tab, which may cause problems. Set 'showtabline' to 2
10967 * temporarily to avoid that.
10968 */
10969 if (p_stal == 1 && first_tabpage->tp_next != NULL)
10970 {
10971 if (put_line(fd, "set stal=2") == FAIL)
10972 return FAIL;
10973 restore_stal = TRUE;
10974 }
10975
10976 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000010977 * May repeat putting Windows for each tab, when "tabpages" is in
10978 * 'sessionoptions'.
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010979 * Don't use goto_tabpage(), it may change directory and trigger
10980 * autocommands.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010981 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010982 tab_firstwin = firstwin; /* first window in tab page "tabnr" */
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010983 tab_topframe = topframe;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010984 for (tabnr = 1; ; ++tabnr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010985 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010986 int need_tabnew = FALSE;
10987
Bram Moolenaar18144c82006-04-12 21:52:12 +000010988 if ((ssop_flags & SSOP_TABPAGES))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010989 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010990 tabpage_T *tp = find_tabpage(tabnr);
10991
10992 if (tp == NULL)
10993 break; /* done all tab pages */
10994 if (tp == curtab)
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010995 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010996 tab_firstwin = firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010997 tab_topframe = topframe;
10998 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010999 else
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011000 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011001 tab_firstwin = tp->tp_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011002 tab_topframe = tp->tp_topframe;
11003 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011004 if (tabnr > 1)
11005 need_tabnew = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011006 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011007
Bram Moolenaar18144c82006-04-12 21:52:12 +000011008 /*
11009 * Before creating the window layout, try loading one file. If this
11010 * is aborted we don't end up with a number of useless windows.
11011 * This may have side effects! (e.g., compressed or network file).
11012 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011013 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011014 {
11015 if (ses_do_win(wp)
11016 && wp->w_buffer->b_ffname != NULL
11017 && !wp->w_buffer->b_help
11018#ifdef FEAT_QUICKFIX
11019 && !bt_nofile(wp->w_buffer)
11020#endif
11021 )
11022 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011023 if (fputs(need_tabnew ? "tabedit " : "edit ", fd) < 0
Bram Moolenaar18144c82006-04-12 21:52:12 +000011024 || ses_fname(fd, wp->w_buffer, &ssop_flags) == FAIL)
11025 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011026 need_tabnew = FALSE;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011027 if (!wp->w_arg_idx_invalid)
11028 edited_win = wp;
11029 break;
11030 }
11031 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011032
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011033 /* If no file got edited create an empty tab page. */
11034 if (need_tabnew && put_line(fd, "tabnew") == FAIL)
11035 return FAIL;
11036
Bram Moolenaar18144c82006-04-12 21:52:12 +000011037 /*
11038 * Save current window layout.
11039 */
11040 if (put_line(fd, "set splitbelow splitright") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011041 return FAIL;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011042 if (ses_win_rec(fd, tab_topframe) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011043 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011044 if (!p_sb && put_line(fd, "set nosplitbelow") == FAIL)
11045 return FAIL;
11046 if (!p_spr && put_line(fd, "set nosplitright") == FAIL)
11047 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011048
Bram Moolenaar18144c82006-04-12 21:52:12 +000011049 /*
11050 * Check if window sizes can be restored (no windows omitted).
11051 * Remember the window number of the current window after restoring.
11052 */
11053 nr = 0;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011054 for (wp = tab_firstwin; wp != NULL; wp = W_NEXT(wp))
Bram Moolenaar18144c82006-04-12 21:52:12 +000011055 {
11056 if (ses_do_win(wp))
11057 ++nr;
11058 else
11059 restore_size = FALSE;
11060 if (curwin == wp)
11061 cnr = nr;
11062 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011063
Bram Moolenaar18144c82006-04-12 21:52:12 +000011064 /* Go to the first window. */
11065 if (put_line(fd, "wincmd t") == FAIL)
11066 return FAIL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011067
Bram Moolenaar18144c82006-04-12 21:52:12 +000011068 /*
11069 * If more than one window, see if sizes can be restored.
11070 * First set 'winheight' and 'winwidth' to 1 to avoid the windows being
11071 * resized when moving between windows.
11072 * Do this before restoring the view, so that the topline and the
11073 * cursor can be set. This is done again below.
11074 */
11075 if (put_line(fd, "set winheight=1 winwidth=1") == FAIL)
11076 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011077 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011078 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011079
Bram Moolenaar18144c82006-04-12 21:52:12 +000011080 /*
11081 * Restore the view of the window (options, file, cursor, etc.).
11082 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011083 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011084 {
11085 if (!ses_do_win(wp))
11086 continue;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011087 if (put_view(fd, wp, wp != edited_win, &ssop_flags,
11088 cur_arg_idx) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011089 return FAIL;
11090 if (nr > 1 && put_line(fd, "wincmd w") == FAIL)
11091 return FAIL;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011092 next_arg_idx = wp->w_arg_idx;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011093 }
11094
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011095 /* The argument index in the first tab page is zero, need to set it in
11096 * each window. For further tab pages it's the window where we do
11097 * "tabedit". */
11098 cur_arg_idx = next_arg_idx;
11099
Bram Moolenaar18144c82006-04-12 21:52:12 +000011100 /*
11101 * Restore cursor to the current window if it's not the first one.
11102 */
11103 if (cnr > 1 && (fprintf(fd, "%dwincmd w", cnr) < 0
11104 || put_eol(fd) == FAIL))
11105 return FAIL;
11106
11107 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000011108 * Restore window sizes again after jumping around in windows, because
11109 * the current window has a minimum size while others may not.
11110 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011111 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011112 return FAIL;
11113
Bram Moolenaar18144c82006-04-12 21:52:12 +000011114 /* Don't continue in another tab page when doing only the current one
11115 * or when at the last tab page. */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011116 if (!(ssop_flags & SSOP_TABPAGES))
Bram Moolenaar18144c82006-04-12 21:52:12 +000011117 break;
11118 }
11119
11120 if (ssop_flags & SSOP_TABPAGES)
11121 {
Bram Moolenaar18144c82006-04-12 21:52:12 +000011122 if (fprintf(fd, "tabnext %d", tabpage_index(curtab)) < 0
11123 || put_eol(fd) == FAIL)
11124 return FAIL;
11125 }
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020011126 if (restore_stal && put_line(fd, "set stal=1") == FAIL)
11127 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011128
Bram Moolenaar9c102382006-05-03 21:26:49 +000011129 /*
11130 * Wipe out an empty unnamed buffer we started in.
11131 */
11132 if (put_line(fd, "if exists('s:wipebuf')") == FAIL)
11133 return FAIL;
Bram Moolenaarf3a67882006-05-05 21:09:41 +000011134 if (put_line(fd, " silent exe 'bwipe ' . s:wipebuf") == FAIL)
Bram Moolenaar9c102382006-05-03 21:26:49 +000011135 return FAIL;
11136 if (put_line(fd, "endif") == FAIL)
11137 return FAIL;
11138 if (put_line(fd, "unlet! s:wipebuf") == FAIL)
11139 return FAIL;
11140
11141 /* Re-apply 'winheight', 'winwidth' and 'shortmess'. */
11142 if (fprintf(fd, "set winheight=%ld winwidth=%ld shortmess=%s",
11143 p_wh, p_wiw, p_shm) < 0 || put_eol(fd) == FAIL)
11144 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011145
11146 /*
11147 * Lastly, execute the x.vim file if it exists.
11148 */
11149 if (put_line(fd, "let s:sx = expand(\"<sfile>:p:r\").\"x.vim\"") == FAIL
11150 || put_line(fd, "if file_readable(s:sx)") == FAIL
Bram Moolenaar42ba1262008-12-09 10:18:03 +000011151 || put_line(fd, " exe \"source \" . fnameescape(s:sx)") == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000011152 || put_line(fd, "endif") == FAIL)
11153 return FAIL;
11154
11155 return OK;
11156}
11157
11158 static int
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011159ses_winsizes(fd, restore_size, tab_firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011160 FILE *fd;
11161 int restore_size;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011162 win_T *tab_firstwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011163{
11164 int n = 0;
11165 win_T *wp;
11166
11167 if (restore_size && (ssop_flags & SSOP_WINSIZE))
11168 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011169 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011170 {
11171 if (!ses_do_win(wp))
11172 continue;
11173 ++n;
11174
11175 /* restore height when not full height */
11176 if (wp->w_height + wp->w_status_height < topframe->fr_height
11177 && (fprintf(fd,
11178 "exe '%dresize ' . ((&lines * %ld + %ld) / %ld)",
11179 n, (long)wp->w_height, Rows / 2, Rows) < 0
11180 || put_eol(fd) == FAIL))
11181 return FAIL;
11182
11183 /* restore width when not full width */
11184 if (wp->w_width < Columns && (fprintf(fd,
11185 "exe 'vert %dresize ' . ((&columns * %ld + %ld) / %ld)",
11186 n, (long)wp->w_width, Columns / 2, Columns) < 0
11187 || put_eol(fd) == FAIL))
11188 return FAIL;
11189 }
11190 }
11191 else
11192 {
11193 /* Just equalise window sizes */
11194 if (put_line(fd, "wincmd =") == FAIL)
11195 return FAIL;
11196 }
11197 return OK;
11198}
11199
11200/*
11201 * Write commands to "fd" to recursively create windows for frame "fr",
11202 * horizontally and vertically split.
11203 * After the commands the last window in the frame is the current window.
11204 * Returns FAIL when writing the commands to "fd" fails.
11205 */
11206 static int
11207ses_win_rec(fd, fr)
11208 FILE *fd;
11209 frame_T *fr;
11210{
11211 frame_T *frc;
11212 int count = 0;
11213
11214 if (fr->fr_layout != FR_LEAF)
11215 {
11216 /* Find first frame that's not skipped and then create a window for
11217 * each following one (first frame is already there). */
11218 frc = ses_skipframe(fr->fr_child);
11219 if (frc != NULL)
11220 while ((frc = ses_skipframe(frc->fr_next)) != NULL)
11221 {
11222 /* Make window as big as possible so that we have lots of room
11223 * to split. */
11224 if (put_line(fd, "wincmd _ | wincmd |") == FAIL
11225 || put_line(fd, fr->fr_layout == FR_COL
11226 ? "split" : "vsplit") == FAIL)
11227 return FAIL;
11228 ++count;
11229 }
11230
11231 /* Go back to the first window. */
11232 if (count > 0 && (fprintf(fd, fr->fr_layout == FR_COL
11233 ? "%dwincmd k" : "%dwincmd h", count) < 0
11234 || put_eol(fd) == FAIL))
11235 return FAIL;
11236
11237 /* Recursively create frames/windows in each window of this column or
11238 * row. */
11239 frc = ses_skipframe(fr->fr_child);
11240 while (frc != NULL)
11241 {
11242 ses_win_rec(fd, frc);
11243 frc = ses_skipframe(frc->fr_next);
11244 /* Go to next window. */
11245 if (frc != NULL && put_line(fd, "wincmd w") == FAIL)
11246 return FAIL;
11247 }
11248 }
11249 return OK;
11250}
11251
11252/*
11253 * Skip frames that don't contain windows we want to save in the Session.
11254 * Returns NULL when there none.
11255 */
11256 static frame_T *
11257ses_skipframe(fr)
11258 frame_T *fr;
11259{
11260 frame_T *frc;
11261
11262 for (frc = fr; frc != NULL; frc = frc->fr_next)
11263 if (ses_do_frame(frc))
11264 break;
11265 return frc;
11266}
11267
11268/*
11269 * Return TRUE if frame "fr" has a window somewhere that we want to save in
11270 * the Session.
11271 */
11272 static int
11273ses_do_frame(fr)
11274 frame_T *fr;
11275{
11276 frame_T *frc;
11277
11278 if (fr->fr_layout == FR_LEAF)
11279 return ses_do_win(fr->fr_win);
11280 for (frc = fr->fr_child; frc != NULL; frc = frc->fr_next)
11281 if (ses_do_frame(frc))
11282 return TRUE;
11283 return FALSE;
11284}
11285
11286/*
11287 * Return non-zero if window "wp" is to be stored in the Session.
11288 */
11289 static int
11290ses_do_win(wp)
11291 win_T *wp;
11292{
11293 if (wp->w_buffer->b_fname == NULL
11294#ifdef FEAT_QUICKFIX
11295 /* When 'buftype' is "nofile" can't restore the window contents. */
11296 || bt_nofile(wp->w_buffer)
11297#endif
11298 )
11299 return (ssop_flags & SSOP_BLANK);
11300 if (wp->w_buffer->b_help)
11301 return (ssop_flags & SSOP_HELP);
11302 return TRUE;
11303}
11304
11305/*
11306 * Write commands to "fd" to restore the view of a window.
11307 * Caller must make sure 'scrolloff' is zero.
11308 */
11309 static int
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011310put_view(fd, wp, add_edit, flagp, current_arg_idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011311 FILE *fd;
11312 win_T *wp;
11313 int add_edit; /* add ":edit" command to view */
11314 unsigned *flagp; /* vop_flags or ssop_flags */
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011315 int current_arg_idx; /* current argument index of the window, use
11316 * -1 if unknown */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011317{
11318 win_T *save_curwin;
11319 int f;
11320 int do_cursor;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011321 int did_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011322
11323 /* Always restore cursor position for ":mksession". For ":mkview" only
11324 * when 'viewoptions' contains "cursor". */
11325 do_cursor = (flagp == &ssop_flags || *flagp & SSOP_CURSOR);
11326
11327 /*
11328 * Local argument list.
11329 */
11330 if (wp->w_alist == &global_alist)
11331 {
11332 if (put_line(fd, "argglobal") == FAIL)
11333 return FAIL;
11334 }
11335 else
11336 {
11337 if (ses_arglist(fd, "arglocal", &wp->w_alist->al_ga,
11338 flagp == &vop_flags
11339 || !(*flagp & SSOP_CURDIR)
11340 || wp->w_localdir != NULL, flagp) == FAIL)
11341 return FAIL;
11342 }
11343
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011344 /* Only when part of a session: restore the argument index. Some
11345 * arguments may have been deleted, check if the index is valid. */
Bram Moolenaar51f53df2010-06-26 05:25:54 +020011346 if (wp->w_arg_idx != current_arg_idx && wp->w_arg_idx < WARGCOUNT(wp)
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011347 && flagp == &ssop_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011348 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011349 if (fprintf(fd, "%ldargu", (long)wp->w_arg_idx + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011350 || put_eol(fd) == FAIL)
11351 return FAIL;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011352 did_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011353 }
11354
11355 /* Edit the file. Skip this when ":next" already did it. */
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011356 if (add_edit && (!did_next || wp->w_arg_idx_invalid))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011357 {
11358 /*
11359 * Load the file.
11360 */
11361 if (wp->w_buffer->b_ffname != NULL
11362#ifdef FEAT_QUICKFIX
11363 && !bt_nofile(wp->w_buffer)
11364#endif
11365 )
11366 {
11367 /*
11368 * Editing a file in this buffer: use ":edit file".
11369 * This may have side effects! (e.g., compressed or network file).
11370 */
11371 if (fputs("edit ", fd) < 0
11372 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
11373 return FAIL;
11374 }
11375 else
11376 {
11377 /* No file in this buffer, just make it empty. */
11378 if (put_line(fd, "enew") == FAIL)
11379 return FAIL;
11380#ifdef FEAT_QUICKFIX
11381 if (wp->w_buffer->b_ffname != NULL)
11382 {
11383 /* The buffer does have a name, but it's not a file name. */
11384 if (fputs("file ", fd) < 0
11385 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
11386 return FAIL;
11387 }
11388#endif
11389 do_cursor = FALSE;
11390 }
11391 }
11392
11393 /*
11394 * Local mappings and abbreviations.
11395 */
11396 if ((*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
11397 && makemap(fd, wp->w_buffer) == FAIL)
11398 return FAIL;
11399
11400 /*
11401 * Local options. Need to go to the window temporarily.
11402 * Store only local values when using ":mkview" and when ":mksession" is
11403 * used and 'sessionoptions' doesn't include "options".
11404 * Some folding options are always stored when "folds" is included,
11405 * otherwise the folds would not be restored correctly.
11406 */
11407 save_curwin = curwin;
11408 curwin = wp;
11409 curbuf = curwin->w_buffer;
11410 if (*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
11411 f = makeset(fd, OPT_LOCAL,
11412 flagp == &vop_flags || !(*flagp & SSOP_OPTIONS));
11413#ifdef FEAT_FOLDING
11414 else if (*flagp & SSOP_FOLDS)
11415 f = makefoldset(fd);
11416#endif
11417 else
11418 f = OK;
11419 curwin = save_curwin;
11420 curbuf = curwin->w_buffer;
11421 if (f == FAIL)
11422 return FAIL;
11423
11424#ifdef FEAT_FOLDING
11425 /*
11426 * Save Folds when 'buftype' is empty and for help files.
11427 */
11428 if ((*flagp & SSOP_FOLDS)
11429 && wp->w_buffer->b_ffname != NULL
Bram Moolenaarb1b715d2006-01-21 22:09:43 +000011430# ifdef FEAT_QUICKFIX
11431 && (*wp->w_buffer->b_p_bt == NUL || wp->w_buffer->b_help)
11432# endif
11433 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000011434 {
11435 if (put_folds(fd, wp) == FAIL)
11436 return FAIL;
11437 }
11438#endif
11439
11440 /*
11441 * Set the cursor after creating folds, since that moves the cursor.
11442 */
11443 if (do_cursor)
11444 {
11445
11446 /* Restore the cursor line in the file and relatively in the
11447 * window. Don't use "G", it changes the jumplist. */
11448 if (fprintf(fd, "let s:l = %ld - ((%ld * winheight(0) + %ld) / %ld)",
11449 (long)wp->w_cursor.lnum,
11450 (long)(wp->w_cursor.lnum - wp->w_topline),
11451 (long)wp->w_height / 2, (long)wp->w_height) < 0
11452 || put_eol(fd) == FAIL
11453 || put_line(fd, "if s:l < 1 | let s:l = 1 | endif") == FAIL
11454 || put_line(fd, "exe s:l") == FAIL
11455 || put_line(fd, "normal! zt") == FAIL
11456 || fprintf(fd, "%ld", (long)wp->w_cursor.lnum) < 0
11457 || put_eol(fd) == FAIL)
11458 return FAIL;
11459 /* Restore the cursor column and left offset when not wrapping. */
11460 if (wp->w_cursor.col == 0)
11461 {
11462 if (put_line(fd, "normal! 0") == FAIL)
11463 return FAIL;
11464 }
11465 else
11466 {
11467 if (!wp->w_p_wrap && wp->w_leftcol > 0 && wp->w_width > 0)
11468 {
11469 if (fprintf(fd,
11470 "let s:c = %ld - ((%ld * winwidth(0) + %ld) / %ld)",
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011471 (long)wp->w_virtcol + 1,
11472 (long)(wp->w_virtcol - wp->w_leftcol),
Bram Moolenaar071d4272004-06-13 20:20:40 +000011473 (long)wp->w_width / 2, (long)wp->w_width) < 0
11474 || put_eol(fd) == FAIL
11475 || put_line(fd, "if s:c > 0") == FAIL
11476 || fprintf(fd,
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011477 " exe 'normal! ' . s:c . '|zs' . %ld . '|'",
11478 (long)wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011479 || put_eol(fd) == FAIL
11480 || put_line(fd, "else") == FAIL
Bram Moolenaarfdf447b2013-02-26 17:21:29 +010011481 || fprintf(fd, " normal! 0%d|", wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011482 || put_eol(fd) == FAIL
11483 || put_line(fd, "endif") == FAIL)
11484 return FAIL;
11485 }
11486 else
11487 {
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011488 if (fprintf(fd, "normal! 0%d|", wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011489 || put_eol(fd) == FAIL)
11490 return FAIL;
11491 }
11492 }
11493 }
11494
11495 /*
11496 * Local directory.
11497 */
11498 if (wp->w_localdir != NULL)
11499 {
11500 if (fputs("lcd ", fd) < 0
11501 || ses_put_fname(fd, wp->w_localdir, flagp) == FAIL
11502 || put_eol(fd) == FAIL)
11503 return FAIL;
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011504 did_lcd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011505 }
11506
11507 return OK;
11508}
11509
11510/*
11511 * Write an argument list to the session file.
11512 * Returns FAIL if writing fails.
11513 */
11514 static int
11515ses_arglist(fd, cmd, gap, fullname, flagp)
11516 FILE *fd;
11517 char *cmd;
11518 garray_T *gap;
11519 int fullname; /* TRUE: use full path name */
11520 unsigned *flagp;
11521{
11522 int i;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011523 char_u *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011524 char_u *s;
11525
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011526 if (fputs(cmd, fd) < 0 || put_eol(fd) == FAIL)
11527 return FAIL;
11528 if (put_line(fd, "silent! argdel *") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011529 return FAIL;
11530 for (i = 0; i < gap->ga_len; ++i)
11531 {
11532 /* NULL file names are skipped (only happens when out of memory). */
11533 s = alist_name(&((aentry_T *)gap->ga_data)[i]);
11534 if (s != NULL)
11535 {
11536 if (fullname)
11537 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020011538 buf = alloc(MAXPATHL);
11539 if (buf != NULL)
11540 {
11541 (void)vim_FullName(s, buf, MAXPATHL, FALSE);
11542 s = buf;
11543 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011544 }
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011545 if (fputs("argadd ", fd) < 0
11546 || ses_put_fname(fd, s, flagp) == FAIL
11547 || put_eol(fd) == FAIL)
Bram Moolenaard9462e32011-04-11 21:35:11 +020011548 {
11549 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011550 return FAIL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011551 }
11552 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011553 }
11554 }
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011555 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011556}
11557
11558/*
11559 * Write a buffer name to the session file.
11560 * Also ends the line.
11561 * Returns FAIL if writing fails.
11562 */
11563 static int
11564ses_fname(fd, buf, flagp)
11565 FILE *fd;
11566 buf_T *buf;
11567 unsigned *flagp;
11568{
11569 char_u *name;
11570
11571 /* Use the short file name if the current directory is known at the time
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011572 * the session file will be sourced.
11573 * Don't do this for ":mkview", we don't know the current directory.
11574 * Don't do this after ":lcd", we don't keep track of what the current
11575 * directory is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011576 if (buf->b_sfname != NULL
11577 && flagp == &ssop_flags
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011578 && (ssop_flags & (SSOP_CURDIR | SSOP_SESDIR))
Bram Moolenaar8d594672009-07-01 18:18:57 +000011579#ifdef FEAT_AUTOCHDIR
11580 && !p_acd
11581#endif
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011582 && !did_lcd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011583 name = buf->b_sfname;
11584 else
11585 name = buf->b_ffname;
11586 if (ses_put_fname(fd, name, flagp) == FAIL || put_eol(fd) == FAIL)
11587 return FAIL;
11588 return OK;
11589}
11590
11591/*
11592 * Write a file name to the session file.
11593 * Takes care of the "slash" option in 'sessionoptions' and escapes special
11594 * characters.
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011595 * Returns FAIL if writing fails or out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011596 */
11597 static int
11598ses_put_fname(fd, name, flagp)
11599 FILE *fd;
11600 char_u *name;
11601 unsigned *flagp;
11602{
11603 char_u *sname;
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011604 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011605 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011606
11607 sname = home_replace_save(NULL, name);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011608 if (sname == NULL)
11609 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011610
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011611 if (*flagp & SSOP_SLASH)
11612 {
11613 /* change all backslashes to forward slashes */
11614 for (p = sname; *p != NUL; mb_ptr_adv(p))
11615 if (*p == '\\')
11616 *p = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +000011617 }
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011618
Bram Moolenaar84a05ac2013-05-06 04:24:17 +020011619 /* escape special characters */
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011620 p = vim_strsave_fnameescape(sname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011621 vim_free(sname);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011622 if (p == NULL)
11623 return FAIL;
11624
11625 /* write the result */
11626 if (fputs((char *)p, fd) < 0)
11627 retval = FAIL;
11628
11629 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011630 return retval;
11631}
11632
11633/*
11634 * ":loadview [nr]"
11635 */
11636 static void
11637ex_loadview(eap)
11638 exarg_T *eap;
11639{
11640 char_u *fname;
11641
11642 fname = get_view_file(*eap->arg);
11643 if (fname != NULL)
11644 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +000011645 do_source(fname, FALSE, DOSO_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011646 vim_free(fname);
11647 }
11648}
11649
11650/*
11651 * Get the name of the view file for the current buffer.
11652 */
11653 static char_u *
11654get_view_file(c)
11655 int c;
11656{
11657 int len = 0;
11658 char_u *p, *s;
11659 char_u *retval;
11660 char_u *sname;
11661
11662 if (curbuf->b_ffname == NULL)
11663 {
11664 EMSG(_(e_noname));
11665 return NULL;
11666 }
11667 sname = home_replace_save(NULL, curbuf->b_ffname);
11668 if (sname == NULL)
11669 return NULL;
11670
11671 /*
11672 * We want a file name without separators, because we're not going to make
11673 * a directory.
11674 * "normal" path separator -> "=+"
11675 * "=" -> "=="
11676 * ":" path separator -> "=-"
11677 */
11678 for (p = sname; *p; ++p)
11679 if (*p == '=' || vim_ispathsep(*p))
11680 ++len;
11681 retval = alloc((unsigned)(STRLEN(sname) + len + STRLEN(p_vdir) + 9));
11682 if (retval != NULL)
11683 {
11684 STRCPY(retval, p_vdir);
11685 add_pathsep(retval);
11686 s = retval + STRLEN(retval);
11687 for (p = sname; *p; ++p)
11688 {
11689 if (*p == '=')
11690 {
11691 *s++ = '=';
11692 *s++ = '=';
11693 }
11694 else if (vim_ispathsep(*p))
11695 {
11696 *s++ = '=';
Bram Moolenaare60acc12011-05-10 16:41:25 +020011697#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011698 if (*p == ':')
11699 *s++ = '-';
11700 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000011701#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000011702 *s++ = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +000011703 }
11704 else
11705 *s++ = *p;
11706 }
11707 *s++ = '=';
11708 *s++ = c;
11709 STRCPY(s, ".vim");
11710 }
11711
11712 vim_free(sname);
11713 return retval;
11714}
11715
11716#endif /* FEAT_SESSION */
11717
11718/*
11719 * Write end-of-line character(s) for ":mkexrc", ":mkvimrc" and ":mksession".
11720 * Return FAIL for a write error.
11721 */
11722 int
11723put_eol(fd)
11724 FILE *fd;
11725{
11726 if (
11727#ifdef USE_CRNL
11728 (
11729# ifdef MKSESSION_NL
11730 !mksession_nl &&
11731# endif
11732 (putc('\r', fd) < 0)) ||
11733#endif
11734 (putc('\n', fd) < 0))
11735 return FAIL;
11736 return OK;
11737}
11738
11739/*
11740 * Write a line to "fd".
11741 * Return FAIL for a write error.
11742 */
11743 int
11744put_line(fd, s)
11745 FILE *fd;
11746 char *s;
11747{
11748 if (fputs(s, fd) < 0 || put_eol(fd) == FAIL)
11749 return FAIL;
11750 return OK;
11751}
11752
11753#ifdef FEAT_VIMINFO
11754/*
11755 * ":rviminfo" and ":wviminfo".
11756 */
11757 static void
11758ex_viminfo(eap)
11759 exarg_T *eap;
11760{
11761 char_u *save_viminfo;
11762
11763 save_viminfo = p_viminfo;
11764 if (*p_viminfo == NUL)
11765 p_viminfo = (char_u *)"'100";
11766 if (eap->cmdidx == CMD_rviminfo)
11767 {
Bram Moolenaard812df62008-11-09 12:46:09 +000011768 if (read_viminfo(eap->arg, VIF_WANT_INFO | VIF_WANT_MARKS
11769 | (eap->forceit ? VIF_FORCEIT : 0)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011770 EMSG(_("E195: Cannot open viminfo file for reading"));
11771 }
11772 else
11773 write_viminfo(eap->arg, eap->forceit);
11774 p_viminfo = save_viminfo;
11775}
11776#endif
11777
11778#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +000011779/*
Bram Moolenaard9462e32011-04-11 21:35:11 +020011780 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +000011781 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +000011782 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011783 void
11784dialog_msg(buff, format, fname)
11785 char_u *buff;
11786 char *format;
11787 char_u *fname;
11788{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011789 if (fname == NULL)
11790 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +020011791 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011792}
11793#endif
11794
11795/*
11796 * ":behave {mswin,xterm}"
11797 */
11798 static void
11799ex_behave(eap)
11800 exarg_T *eap;
11801{
11802 if (STRCMP(eap->arg, "mswin") == 0)
11803 {
11804 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
11805 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
11806 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
11807 set_option_value((char_u *)"keymodel", 0L,
11808 (char_u *)"startsel,stopsel", 0);
11809 }
11810 else if (STRCMP(eap->arg, "xterm") == 0)
11811 {
11812 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
11813 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
11814 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
11815 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
11816 }
11817 else
11818 EMSG2(_(e_invarg2), eap->arg);
11819}
11820
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010011821#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
11822/*
11823 * Function given to ExpandGeneric() to obtain the possible arguments of the
11824 * ":behave {mswin,xterm}" command.
11825 */
11826 char_u *
11827get_behave_arg(xp, idx)
11828 expand_T *xp UNUSED;
11829 int idx;
11830{
11831 if (idx == 0)
11832 return (char_u *)"mswin";
11833 if (idx == 1)
11834 return (char_u *)"xterm";
11835 return NULL;
11836}
11837#endif
11838
Bram Moolenaar071d4272004-06-13 20:20:40 +000011839#ifdef FEAT_AUTOCMD
11840static int filetype_detect = FALSE;
11841static int filetype_plugin = FALSE;
11842static int filetype_indent = FALSE;
11843
11844/*
11845 * ":filetype [plugin] [indent] {on,off,detect}"
11846 * on: Load the filetype.vim file to install autocommands for file types.
11847 * off: Load the ftoff.vim file to remove all autocommands for file types.
11848 * plugin on: load filetype.vim and ftplugin.vim
11849 * plugin off: load ftplugof.vim
11850 * indent on: load filetype.vim and indent.vim
11851 * indent off: load indoff.vim
11852 */
11853 static void
11854ex_filetype(eap)
11855 exarg_T *eap;
11856{
11857 char_u *arg = eap->arg;
11858 int plugin = FALSE;
11859 int indent = FALSE;
11860
11861 if (*eap->arg == NUL)
11862 {
11863 /* Print current status. */
11864 smsg((char_u *)"filetype detection:%s plugin:%s indent:%s",
11865 filetype_detect ? "ON" : "OFF",
11866 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
11867 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
11868 return;
11869 }
11870
11871 /* Accept "plugin" and "indent" in any order. */
11872 for (;;)
11873 {
11874 if (STRNCMP(arg, "plugin", 6) == 0)
11875 {
11876 plugin = TRUE;
11877 arg = skipwhite(arg + 6);
11878 continue;
11879 }
11880 if (STRNCMP(arg, "indent", 6) == 0)
11881 {
11882 indent = TRUE;
11883 arg = skipwhite(arg + 6);
11884 continue;
11885 }
11886 break;
11887 }
11888 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
11889 {
11890 if (*arg == 'o' || !filetype_detect)
11891 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011892 source_runtime((char_u *)FILETYPE_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011893 filetype_detect = TRUE;
11894 if (plugin)
11895 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011896 source_runtime((char_u *)FTPLUGIN_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011897 filetype_plugin = TRUE;
11898 }
11899 if (indent)
11900 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011901 source_runtime((char_u *)INDENT_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011902 filetype_indent = TRUE;
11903 }
11904 }
11905 if (*arg == 'd')
11906 {
11907 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE);
Bram Moolenaara3227e22006-03-08 21:32:40 +000011908 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011909 }
11910 }
11911 else if (STRCMP(arg, "off") == 0)
11912 {
11913 if (plugin || indent)
11914 {
11915 if (plugin)
11916 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011917 source_runtime((char_u *)FTPLUGOF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011918 filetype_plugin = FALSE;
11919 }
11920 if (indent)
11921 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011922 source_runtime((char_u *)INDOFF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011923 filetype_indent = FALSE;
11924 }
11925 }
11926 else
11927 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011928 source_runtime((char_u *)FTOFF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011929 filetype_detect = FALSE;
11930 }
11931 }
11932 else
11933 EMSG2(_(e_invarg2), arg);
11934}
11935
11936/*
11937 * ":setfiletype {name}"
11938 */
11939 static void
11940ex_setfiletype(eap)
11941 exarg_T *eap;
11942{
11943 if (!did_filetype)
11944 set_option_value((char_u *)"filetype", 0L, eap->arg, OPT_LOCAL);
11945}
11946#endif
11947
Bram Moolenaar071d4272004-06-13 20:20:40 +000011948 static void
11949ex_digraphs(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +000011950 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011951{
11952#ifdef FEAT_DIGRAPHS
11953 if (*eap->arg != NUL)
11954 putdigraph(eap->arg);
11955 else
11956 listdigraphs();
11957#else
11958 EMSG(_("E196: No digraphs in this version"));
11959#endif
11960}
11961
11962 static void
11963ex_set(eap)
11964 exarg_T *eap;
11965{
11966 int flags = 0;
11967
11968 if (eap->cmdidx == CMD_setlocal)
11969 flags = OPT_LOCAL;
11970 else if (eap->cmdidx == CMD_setglobal)
11971 flags = OPT_GLOBAL;
11972#if defined(FEAT_EVAL) && defined(FEAT_AUTOCMD) && defined(FEAT_BROWSE)
11973 if (cmdmod.browse && flags == 0)
11974 ex_options(eap);
11975 else
11976#endif
11977 (void)do_set(eap->arg, flags);
11978}
11979
11980#ifdef FEAT_SEARCH_EXTRA
11981/*
11982 * ":nohlsearch"
11983 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011984 static void
11985ex_nohlsearch(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +000011986 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011987{
Bram Moolenaar8050efa2013-11-08 04:30:20 +010011988 SET_NO_HLSEARCH(TRUE);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +000011989 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011990}
11991
11992/*
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011993 * ":[N]match {group} {pattern}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000011994 * Sets nextcmd to the start of the next command, if any. Also called when
11995 * skipping commands to find the next command.
11996 */
11997 static void
11998ex_match(eap)
11999 exarg_T *eap;
12000{
12001 char_u *p;
Bram Moolenaar52d36c82007-08-11 14:00:30 +000012002 char_u *g = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012003 char_u *end;
12004 int c;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012005 int id;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000012006
12007 if (eap->line2 <= 3)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012008 id = eap->line2;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000012009 else
12010 {
12011 EMSG(e_invcmd);
12012 return;
12013 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012014
12015 /* First clear any old pattern. */
12016 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012017 match_delete(curwin, id, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012018
12019 if (ends_excmd(*eap->arg))
12020 end = eap->arg;
12021 else if ((STRNICMP(eap->arg, "none", 4) == 0
12022 && (vim_iswhite(eap->arg[4]) || ends_excmd(eap->arg[4]))))
12023 end = eap->arg + 4;
12024 else
12025 {
12026 p = skiptowhite(eap->arg);
12027 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012028 g = vim_strnsave(eap->arg, (int)(p - eap->arg));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012029 p = skipwhite(p);
12030 if (*p == NUL)
12031 {
12032 /* There must be two arguments. */
12033 EMSG2(_(e_invarg2), eap->arg);
12034 return;
12035 }
12036 end = skip_regexp(p + 1, *p, TRUE, NULL);
12037 if (!eap->skip)
12038 {
12039 if (*end != NUL && !ends_excmd(*skipwhite(end + 1)))
12040 {
12041 eap->errmsg = e_trailing;
12042 return;
12043 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000012044 if (*end != *p)
12045 {
12046 EMSG2(_(e_invarg2), p);
12047 return;
12048 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012049
12050 c = *end;
12051 *end = NUL;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012052 match_add(curwin, g, p + 1, 10, id, NULL);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012053 vim_free(g);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012054 *end = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012055 }
12056 }
12057 eap->nextcmd = find_nextcmd(end);
12058}
12059#endif
12060
12061#ifdef FEAT_CRYPT
12062/*
12063 * ":X": Get crypt key
12064 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012065 static void
12066ex_X(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +000012067 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012068{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +010012069 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +020012070 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012071}
12072#endif
12073
12074#ifdef FEAT_FOLDING
12075 static void
12076ex_fold(eap)
12077 exarg_T *eap;
12078{
12079 if (foldManualAllowed(TRUE))
12080 foldCreate(eap->line1, eap->line2);
12081}
12082
12083 static void
12084ex_foldopen(eap)
12085 exarg_T *eap;
12086{
12087 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
12088 eap->forceit, FALSE);
12089}
12090
12091 static void
12092ex_folddo(eap)
12093 exarg_T *eap;
12094{
12095 linenr_T lnum;
12096
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020012097#ifdef FEAT_CLIPBOARD
12098 start_global_changes();
12099#endif
12100
Bram Moolenaar071d4272004-06-13 20:20:40 +000012101 /* First set the marks for all lines closed/open. */
12102 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
12103 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
12104 ml_setmarked(lnum);
12105
12106 /* Execute the command on the marked lines. */
12107 global_exe(eap->arg);
12108 ml_clearmarked(); /* clear rest of the marks */
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020012109#ifdef FEAT_CLIPBOARD
12110 end_global_changes();
12111#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012112}
12113#endif