blob: 971a2022bde1e68c05091af4a5e611a4670ad358 [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
Bram Moolenaarc7a0d322015-06-19 12:43:07 +02007095 /* quit last window
7096 * Note: only_one_window() returns true, even so a help window is
7097 * still open. In that case only quit, if no address has been
7098 * specified. Example:
7099 * :h|wincmd w|1q - don't quit
7100 * :h|wincmd w|q - quit
7101 */
7102 if (only_one_window() && (firstwin == lastwin || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007103#endif
7104 getout(0);
7105#ifdef FEAT_WINDOWS
7106# ifdef FEAT_GUI
7107 need_mouse_correct = TRUE;
7108# endif
7109 /* close window; may free buffer */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007110 win_close(wp, !P_HID(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007111#endif
7112 }
7113}
7114
7115/*
7116 * ":cquit".
7117 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007118 static void
7119ex_cquit(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007120 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007121{
7122 getout(1); /* this does not always pass on the exit code to the Manx
7123 compiler. why? */
7124}
7125
7126/*
7127 * ":qall": try to quit all windows
7128 */
7129 static void
7130ex_quit_all(eap)
7131 exarg_T *eap;
7132{
7133# ifdef FEAT_CMDWIN
7134 if (cmdwin_type != 0)
7135 {
7136 if (eap->forceit)
7137 cmdwin_result = K_XF1; /* ex_window() takes care of this */
7138 else
7139 cmdwin_result = K_XF2;
7140 return;
7141 }
7142# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007143
7144 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007145 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007146 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007147 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007148 return;
7149 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007150#ifdef FEAT_AUTOCMD
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01007151 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
7152 /* Refuse to quit when locked or when the buffer in the last window is
7153 * being closed (can only happen in autocommands). */
7154 if (curbuf_locked() || (curbuf->b_nwindows == 1 && curbuf->b_closing))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007155 return;
7156#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007157
Bram Moolenaar071d4272004-06-13 20:20:40 +00007158 exiting = TRUE;
7159 if (eap->forceit || !check_changed_any(FALSE))
7160 getout(0);
7161 not_exiting();
7162}
7163
Bram Moolenaard9967712006-03-11 21:18:15 +00007164#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007165/*
7166 * ":close": close current window, unless it is the last one
7167 */
7168 static void
7169ex_close(eap)
7170 exarg_T *eap;
7171{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007172 win_T *win;
7173 int winnr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007174# ifdef FEAT_CMDWIN
7175 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02007176 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007177 else
7178# endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007179 if (!text_locked()
7180#ifdef FEAT_AUTOCMD
7181 && !curbuf_locked()
7182#endif
7183 )
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007184 {
7185 if (eap->addr_count == 0)
7186 ex_win_close(eap->forceit, curwin, NULL);
7187 else {
7188 for (win = firstwin; win != NULL; win = win->w_next)
7189 {
7190 winnr++;
7191 if (winnr == eap->line2)
7192 break;
7193 }
7194 if (win == NULL)
7195 win = lastwin;
7196 ex_win_close(eap->forceit, win, NULL);
7197 }
7198 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007199}
7200
Bram Moolenaard9967712006-03-11 21:18:15 +00007201# ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007202/*
7203 * ":pclose": Close any preview window.
7204 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007205 static void
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007206ex_pclose(eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007207 exarg_T *eap;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007208{
7209 win_T *win;
7210
7211 for (win = firstwin; win != NULL; win = win->w_next)
7212 if (win->w_p_pvw)
7213 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00007214 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007215 break;
7216 }
7217}
Bram Moolenaard9967712006-03-11 21:18:15 +00007218# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007219
Bram Moolenaarf740b292006-02-16 22:11:02 +00007220/*
7221 * Close window "win" and take care of handling closing the last window for a
7222 * modified buffer.
7223 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007224 static void
Bram Moolenaarf740b292006-02-16 22:11:02 +00007225ex_win_close(forceit, win, tp)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007226 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007227 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007228 tabpage_T *tp; /* NULL or the tab page "win" is in */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007229{
7230 int need_hide;
7231 buf_T *buf = win->w_buffer;
7232
7233 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007234 if (need_hide && !P_HID(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007235 {
Bram Moolenaard9967712006-03-11 21:18:15 +00007236# if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007237 if ((p_confirm || cmdmod.confirm) && p_write)
7238 {
7239 dialog_changed(buf, FALSE);
7240 if (buf_valid(buf) && bufIsChanged(buf))
7241 return;
7242 need_hide = FALSE;
7243 }
7244 else
Bram Moolenaard9967712006-03-11 21:18:15 +00007245# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007246 {
7247 EMSG(_(e_nowrtmsg));
7248 return;
7249 }
7250 }
7251
Bram Moolenaard9967712006-03-11 21:18:15 +00007252# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007253 need_mouse_correct = TRUE;
Bram Moolenaard9967712006-03-11 21:18:15 +00007254# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00007255
Bram Moolenaar071d4272004-06-13 20:20:40 +00007256 /* free buffer when not hiding it or when it's a scratch buffer */
Bram Moolenaarf740b292006-02-16 22:11:02 +00007257 if (tp == NULL)
7258 win_close(win, !need_hide && !P_HID(buf));
7259 else
7260 win_close_othertab(win, !need_hide && !P_HID(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007261}
7262
Bram Moolenaar071d4272004-06-13 20:20:40 +00007263/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00007264 * ":tabclose": close current tab page, unless it is the last one.
7265 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007266 */
7267 static void
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007268ex_tabclose(eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007269 exarg_T *eap;
7270{
Bram Moolenaarf740b292006-02-16 22:11:02 +00007271 tabpage_T *tp;
7272
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007273# ifdef FEAT_CMDWIN
7274 if (cmdwin_type != 0)
7275 cmdwin_result = K_IGNORE;
7276 else
7277# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00007278 if (first_tabpage->tp_next == NULL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007279 EMSG(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00007280 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007281 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00007282 if (eap->addr_count > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007283 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00007284 tp = find_tabpage((int)eap->line2);
7285 if (tp == NULL)
7286 {
7287 beep_flush();
7288 return;
7289 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007290 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007291 {
7292 tabpage_close_other(tp, eap->forceit);
7293 return;
7294 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007295 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007296 if (!text_locked()
7297#ifdef FEAT_AUTOCMD
7298 && !curbuf_locked()
7299#endif
7300 )
Bram Moolenaarf740b292006-02-16 22:11:02 +00007301 tabpage_close(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007302 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007303}
7304
7305/*
7306 * ":tabonly": close all tab pages except the current one
7307 */
7308 static void
7309ex_tabonly(eap)
7310 exarg_T *eap;
7311{
7312 tabpage_T *tp;
7313 int done;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007314
7315# ifdef FEAT_CMDWIN
7316 if (cmdwin_type != 0)
7317 cmdwin_result = K_IGNORE;
7318 else
7319# endif
7320 if (first_tabpage->tp_next == NULL)
7321 MSG(_("Already only one tab page"));
7322 else
7323 {
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007324 if (eap->addr_count > 0)
7325 goto_tabpage(eap->line2);
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007326 /* Repeat this up to a 1000 times, because autocommands may mess
7327 * up the lists. */
7328 for (done = 0; done < 1000; ++done)
7329 {
7330 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
7331 if (tp->tp_topframe != topframe)
7332 {
7333 tabpage_close_other(tp, eap->forceit);
7334 /* if we failed to close it quit */
7335 if (valid_tabpage(tp))
7336 done = 1000;
7337 /* start over, "tp" is now invalid */
7338 break;
7339 }
7340 if (first_tabpage->tp_next == NULL)
7341 break;
7342 }
7343 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007344}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007345
7346/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00007347 * Close the current tab page.
7348 */
7349 void
7350tabpage_close(forceit)
7351 int forceit;
7352{
7353 /* First close all the windows but the current one. If that worked then
7354 * close the last window in this tab, that will close it. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007355 if (lastwin != firstwin)
7356 close_others(TRUE, forceit);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007357 if (lastwin == firstwin)
7358 ex_win_close(forceit, curwin, NULL);
7359# ifdef FEAT_GUI
7360 need_mouse_correct = TRUE;
7361# endif
7362}
7363
7364/*
7365 * Close tab page "tp", which is not the current tab page.
7366 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007367 * Also takes care of the tab pages line disappearing when closing the
7368 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00007369 */
7370 void
7371tabpage_close_other(tp, forceit)
7372 tabpage_T *tp;
7373 int forceit;
7374{
7375 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007376 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007377 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00007378
7379 /* Limit to 1000 windows, autocommands may add a window while we close
7380 * one. OK, so I'm paranoid... */
7381 while (++done < 1000)
7382 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007383 wp = tp->tp_firstwin;
7384 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007385
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007386 /* Autocommands may delete the tab page under our fingers and we may
7387 * fail to close a window with a modified buffer. */
7388 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007389 break;
7390 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007391
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007392 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007393 if (h != tabline_height())
7394 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00007395}
7396
7397/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007398 * ":only".
7399 */
7400 static void
7401ex_only(eap)
7402 exarg_T *eap;
7403{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007404 win_T *wp;
7405 int wnr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007406# ifdef FEAT_GUI
7407 need_mouse_correct = TRUE;
7408# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007409 if (eap->addr_count > 0)
7410 {
7411 wnr = eap->line2;
7412 for (wp = firstwin; --wnr > 0; )
7413 {
7414 if (wp->w_next == NULL)
7415 break;
7416 else
7417 wp = wp->w_next;
7418 }
7419 win_goto(wp);
7420 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007421 close_others(TRUE, eap->forceit);
7422}
7423
7424/*
7425 * ":all" and ":sall".
Bram Moolenaard9967712006-03-11 21:18:15 +00007426 * Also used for ":tab drop file ..." after setting the argument list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007427 */
Bram Moolenaard9967712006-03-11 21:18:15 +00007428 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00007429ex_all(eap)
7430 exarg_T *eap;
7431{
7432 if (eap->addr_count == 0)
7433 eap->line2 = 9999;
Bram Moolenaard9967712006-03-11 21:18:15 +00007434 do_arg_all((int)eap->line2, eap->forceit, eap->cmdidx == CMD_drop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007435}
7436#endif /* FEAT_WINDOWS */
7437
7438 static void
7439ex_hide(eap)
7440 exarg_T *eap;
7441{
7442 if (*eap->arg != NUL && check_nextcmd(eap->arg) == NULL)
7443 eap->errmsg = e_invarg;
7444 else
7445 {
7446 /* ":hide" or ":hide | cmd": hide current window */
7447 eap->nextcmd = check_nextcmd(eap->arg);
7448#ifdef FEAT_WINDOWS
7449 if (!eap->skip)
7450 {
7451# ifdef FEAT_GUI
7452 need_mouse_correct = TRUE;
7453# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007454 if (eap->addr_count == 0)
7455 win_close(curwin, FALSE); /* don't free buffer */
Bram Moolenaarf240e182014-11-27 18:33:02 +01007456 else
7457 {
7458 int winnr = 0;
7459 win_T *win;
7460
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007461 for (win = firstwin; win != NULL; win = win->w_next)
7462 {
7463 winnr++;
7464 if (winnr == eap->line2)
7465 break;
7466 }
7467 if (win == NULL)
7468 win = lastwin;
7469 win_close(win, FALSE);
7470 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007471 }
7472#endif
7473 }
7474}
7475
7476/*
7477 * ":stop" and ":suspend": Suspend Vim.
7478 */
7479 static void
7480ex_stop(eap)
7481 exarg_T *eap;
7482{
7483 /*
7484 * Disallow suspending for "rvim".
7485 */
7486 if (!check_restricted()
7487#ifdef WIN3264
7488 /*
7489 * Check if external commands are allowed now.
7490 */
7491 && can_end_termcap_mode(TRUE)
7492#endif
7493 )
7494 {
7495 if (!eap->forceit)
7496 autowrite_all();
7497 windgoto((int)Rows - 1, 0);
7498 out_char('\n');
7499 out_flush();
7500 stoptermcap();
7501 out_flush(); /* needed for SUN to restore xterm buffer */
7502#ifdef FEAT_TITLE
7503 mch_restore_title(3); /* restore window titles */
7504#endif
7505 ui_suspend(); /* call machine specific function */
7506#ifdef FEAT_TITLE
7507 maketitle();
7508 resettitle(); /* force updating the title */
7509#endif
7510 starttermcap();
7511 scroll_start(); /* scroll screen before redrawing */
7512 redraw_later_clear();
7513 shell_resized(); /* may have resized window */
7514 }
7515}
7516
7517/*
7518 * ":exit", ":xit" and ":wq": Write file and exit Vim.
7519 */
7520 static void
7521ex_exit(eap)
7522 exarg_T *eap;
7523{
7524#ifdef FEAT_CMDWIN
7525 if (cmdwin_type != 0)
7526 {
7527 cmdwin_result = Ctrl_C;
7528 return;
7529 }
7530#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007531 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007532 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007533 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007534 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007535 return;
7536 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007537#ifdef FEAT_AUTOCMD
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01007538 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
7539 /* Refuse to quit when locked or when the buffer in the last window is
7540 * being closed (can only happen in autocommands). */
7541 if (curbuf_locked() || (curbuf->b_nwindows == 1 && curbuf->b_closing))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007542 return;
7543#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007544
7545 /*
7546 * if more files or windows we won't exit
7547 */
7548 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
7549 exiting = TRUE;
7550 if ( ((eap->cmdidx == CMD_wq
7551 || curbufIsChanged())
7552 && do_write(eap) == FAIL)
7553 || check_more(TRUE, eap->forceit) == FAIL
7554 || (only_one_window() && check_changed_any(eap->forceit)))
7555 {
7556 not_exiting();
7557 }
7558 else
7559 {
7560#ifdef FEAT_WINDOWS
7561 if (only_one_window()) /* quit last window, exit Vim */
7562#endif
7563 getout(0);
7564#ifdef FEAT_WINDOWS
7565# ifdef FEAT_GUI
7566 need_mouse_correct = TRUE;
7567# endif
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02007568 /* Quit current window, may free the buffer. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007569 win_close(curwin, !P_HID(curwin->w_buffer));
7570#endif
7571 }
7572}
7573
7574/*
7575 * ":print", ":list", ":number".
7576 */
7577 static void
7578ex_print(eap)
7579 exarg_T *eap;
7580{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007581 if (curbuf->b_ml.ml_flags & ML_EMPTY)
7582 EMSG(_(e_emptybuf));
7583 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007584 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007585 for ( ;!got_int; ui_breakcheck())
7586 {
7587 print_line(eap->line1,
7588 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
7589 || (eap->flags & EXFLAG_NR)),
7590 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
7591 if (++eap->line1 > eap->line2)
7592 break;
7593 out_flush(); /* show one line at a time */
7594 }
7595 setpcmark();
7596 /* put cursor at last line */
7597 curwin->w_cursor.lnum = eap->line2;
7598 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007599 }
7600
Bram Moolenaar071d4272004-06-13 20:20:40 +00007601 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007602}
7603
7604#ifdef FEAT_BYTEOFF
7605 static void
7606ex_goto(eap)
7607 exarg_T *eap;
7608{
7609 goto_byte(eap->line2);
7610}
7611#endif
7612
7613/*
7614 * ":shell".
7615 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007616 static void
7617ex_shell(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007618 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007619{
7620 do_shell(NULL, 0);
7621}
7622
7623#if (defined(FEAT_WINDOWS) && defined(HAVE_DROP_FILE)) \
7624 || (defined(FEAT_GUI_GTK) && defined(FEAT_DND)) \
Bram Moolenaar371baa92005-12-29 22:43:53 +00007625 || defined(FEAT_GUI_MSWIN) \
7626 || defined(FEAT_GUI_MAC) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00007627 || defined(PROTO)
7628
7629/*
7630 * Handle a file drop. The code is here because a drop is *nearly* like an
7631 * :args command, but not quite (we have a list of exact filenames, so we
7632 * don't want to (a) parse a command line, or (b) expand wildcards. So the
7633 * code is very similar to :args and hence needs access to a lot of the static
7634 * functions in this file.
7635 *
7636 * The list should be allocated using alloc(), as should each item in the
7637 * list. This function takes over responsibility for freeing the list.
7638 *
Bram Moolenaar81870892007-11-11 18:17:28 +00007639 * XXX The list is made into the argument list. This is freed using
Bram Moolenaar071d4272004-06-13 20:20:40 +00007640 * FreeWild(), which does a series of vim_free() calls, unless the two defines
7641 * __EMX__ and __ALWAYS_HAS_TRAILING_NUL_POINTER are set. In this case, a
7642 * routine _fnexplodefree() is used. This may cause problems, but as the drop
7643 * file functionality is (currently) not in EMX this is not presently a
7644 * problem.
7645 */
7646 void
7647handle_drop(filec, filev, split)
7648 int filec; /* the number of files dropped */
7649 char_u **filev; /* the list of files dropped */
7650 int split; /* force splitting the window */
7651{
7652 exarg_T ea;
7653 int save_msg_scroll = msg_scroll;
7654
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007655 /* Postpone this while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007656 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007657 return;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007658#ifdef FEAT_AUTOCMD
7659 if (curbuf_locked())
7660 return;
7661#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00007662 /* When the screen is being updated we should not change buffers and
7663 * windows structures, it may cause freed memory to be used. */
7664 if (updating_screen)
7665 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007666
7667 /* Check whether the current buffer is changed. If so, we will need
7668 * to split the current window or data could be lost.
7669 * We don't need to check if the 'hidden' option is set, as in this
7670 * case the buffer won't be lost.
7671 */
7672 if (!P_HID(curbuf) && !split)
7673 {
7674 ++emsg_off;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01007675 split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007676 --emsg_off;
7677 }
7678 if (split)
7679 {
7680# ifdef FEAT_WINDOWS
7681 if (win_split(0, 0) == FAIL)
7682 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007683 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007684
7685 /* When splitting the window, create a new alist. Otherwise the
7686 * existing one is overwritten. */
7687 alist_unlink(curwin->w_alist);
7688 alist_new();
7689# else
7690 return; /* can't split, always fail */
7691# endif
7692 }
7693
7694 /*
7695 * Set up the new argument list.
7696 */
Bram Moolenaar86b68352004-12-27 21:59:20 +00007697 alist_set(ALIST(curwin), filec, filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007698
7699 /*
7700 * Move to the first file.
7701 */
7702 /* Fake up a minimal "next" command for do_argfile() */
7703 vim_memset(&ea, 0, sizeof(ea));
7704 ea.cmd = (char_u *)"next";
7705 do_argfile(&ea, 0);
7706
7707 /* do_ecmd() may set need_start_insertmode, but since we never left Insert
7708 * mode that is not needed here. */
7709 need_start_insertmode = FALSE;
7710
7711 /* Restore msg_scroll, otherwise a following command may cause scrolling
7712 * unexpectedly. The screen will be redrawn by the caller, thus
7713 * msg_scroll being set by displaying a message is irrelevant. */
7714 msg_scroll = save_msg_scroll;
7715}
7716#endif
7717
Bram Moolenaar071d4272004-06-13 20:20:40 +00007718/*
7719 * Clear an argument list: free all file names and reset it to zero entries.
7720 */
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007721 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00007722alist_clear(al)
7723 alist_T *al;
7724{
7725 while (--al->al_ga.ga_len >= 0)
7726 vim_free(AARGLIST(al)[al->al_ga.ga_len].ae_fname);
7727 ga_clear(&al->al_ga);
7728}
7729
7730/*
7731 * Init an argument list.
7732 */
7733 void
7734alist_init(al)
7735 alist_T *al;
7736{
7737 ga_init2(&al->al_ga, (int)sizeof(aentry_T), 5);
7738}
7739
7740#if defined(FEAT_WINDOWS) || defined(PROTO)
7741
7742/*
7743 * Remove a reference from an argument list.
7744 * Ignored when the argument list is the global one.
7745 * If the argument list is no longer used by any window, free it.
7746 */
7747 void
7748alist_unlink(al)
7749 alist_T *al;
7750{
7751 if (al != &global_alist && --al->al_refcount <= 0)
7752 {
7753 alist_clear(al);
7754 vim_free(al);
7755 }
7756}
7757
7758# if defined(FEAT_LISTCMDS) || defined(HAVE_DROP_FILE) || defined(PROTO)
7759/*
7760 * Create a new argument list and use it for the current window.
7761 */
7762 void
7763alist_new()
7764{
7765 curwin->w_alist = (alist_T *)alloc((unsigned)sizeof(alist_T));
7766 if (curwin->w_alist == NULL)
7767 {
7768 curwin->w_alist = &global_alist;
7769 ++global_alist.al_refcount;
7770 }
7771 else
7772 {
7773 curwin->w_alist->al_refcount = 1;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02007774 curwin->w_alist->id = ++max_alist_id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007775 alist_init(curwin->w_alist);
7776 }
7777}
7778# endif
7779#endif
7780
7781#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE) || defined(PROTO)
7782/*
7783 * Expand the file names in the global argument list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00007784 * If "fnum_list" is not NULL, use "fnum_list[fnum_len]" as a list of buffer
7785 * numbers to be re-used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007786 */
7787 void
Bram Moolenaar86b68352004-12-27 21:59:20 +00007788alist_expand(fnum_list, fnum_len)
7789 int *fnum_list;
7790 int fnum_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007791{
7792 char_u **old_arg_files;
Bram Moolenaar86b68352004-12-27 21:59:20 +00007793 int old_arg_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007794 char_u **new_arg_files;
7795 int new_arg_file_count;
7796 char_u *save_p_su = p_su;
7797 int i;
7798
7799 /* Don't use 'suffixes' here. This should work like the shell did the
7800 * expansion. Also, the vimrc file isn't read yet, thus the user
7801 * can't set the options. */
7802 p_su = empty_option;
7803 old_arg_files = (char_u **)alloc((unsigned)(sizeof(char_u *) * GARGCOUNT));
7804 if (old_arg_files != NULL)
7805 {
7806 for (i = 0; i < GARGCOUNT; ++i)
Bram Moolenaar86b68352004-12-27 21:59:20 +00007807 old_arg_files[i] = vim_strsave(GARGLIST[i].ae_fname);
7808 old_arg_count = GARGCOUNT;
7809 if (expand_wildcards(old_arg_count, old_arg_files,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007810 &new_arg_file_count, &new_arg_files,
Bram Moolenaarb5609832011-07-20 15:04:58 +02007811 EW_FILE|EW_NOTFOUND|EW_ADDSLASH|EW_NOERROR) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007812 && new_arg_file_count > 0)
7813 {
Bram Moolenaar86b68352004-12-27 21:59:20 +00007814 alist_set(&global_alist, new_arg_file_count, new_arg_files,
7815 TRUE, fnum_list, fnum_len);
7816 FreeWild(old_arg_count, old_arg_files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007817 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007818 }
7819 p_su = save_p_su;
7820}
7821#endif
7822
7823/*
7824 * Set the argument list for the current window.
7825 * Takes over the allocated files[] and the allocated fnames in it.
7826 */
7827 void
Bram Moolenaar86b68352004-12-27 21:59:20 +00007828alist_set(al, count, files, use_curbuf, fnum_list, fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007829 alist_T *al;
7830 int count;
7831 char_u **files;
7832 int use_curbuf;
Bram Moolenaar86b68352004-12-27 21:59:20 +00007833 int *fnum_list;
7834 int fnum_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007835{
7836 int i;
7837
7838 alist_clear(al);
7839 if (ga_grow(&al->al_ga, count) == OK)
7840 {
7841 for (i = 0; i < count; ++i)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007842 {
7843 if (got_int)
7844 {
7845 /* When adding many buffers this can take a long time. Allow
7846 * interrupting here. */
7847 while (i < count)
7848 vim_free(files[i++]);
7849 break;
7850 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00007851
7852 /* May set buffer name of a buffer previously used for the
7853 * argument list, so that it's re-used by alist_add. */
7854 if (fnum_list != NULL && i < fnum_len)
7855 buf_set_name(fnum_list[i], files[i]);
7856
Bram Moolenaar071d4272004-06-13 20:20:40 +00007857 alist_add(al, files[i], use_curbuf ? 2 : 1);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007858 ui_breakcheck();
7859 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007860 vim_free(files);
7861 }
7862 else
7863 FreeWild(count, files);
7864#ifdef FEAT_WINDOWS
7865 if (al == &global_alist)
7866#endif
7867 arg_had_last = FALSE;
7868}
7869
7870/*
7871 * Add file "fname" to argument list "al".
7872 * "fname" must have been allocated and "al" must have been checked for room.
7873 */
7874 void
7875alist_add(al, fname, set_fnum)
7876 alist_T *al;
7877 char_u *fname;
7878 int set_fnum; /* 1: set buffer number; 2: re-use curbuf */
7879{
7880 if (fname == NULL) /* don't add NULL file names */
7881 return;
7882#ifdef BACKSLASH_IN_FILENAME
7883 slash_adjust(fname);
7884#endif
7885 AARGLIST(al)[al->al_ga.ga_len].ae_fname = fname;
7886 if (set_fnum > 0)
7887 AARGLIST(al)[al->al_ga.ga_len].ae_fnum =
7888 buflist_add(fname, BLN_LISTED | (set_fnum == 2 ? BLN_CURBUF : 0));
7889 ++al->al_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007890}
7891
7892#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
7893/*
7894 * Adjust slashes in file names. Called after 'shellslash' was set.
7895 */
7896 void
7897alist_slash_adjust()
7898{
7899 int i;
7900# ifdef FEAT_WINDOWS
7901 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007902 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007903# endif
7904
7905 for (i = 0; i < GARGCOUNT; ++i)
7906 if (GARGLIST[i].ae_fname != NULL)
7907 slash_adjust(GARGLIST[i].ae_fname);
7908# ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00007909 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007910 if (wp->w_alist != &global_alist)
7911 for (i = 0; i < WARGCOUNT(wp); ++i)
7912 if (WARGLIST(wp)[i].ae_fname != NULL)
7913 slash_adjust(WARGLIST(wp)[i].ae_fname);
7914# endif
7915}
7916#endif
7917
7918/*
7919 * ":preserve".
7920 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007921 static void
7922ex_preserve(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007923 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007924{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007925 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007926 ml_preserve(curbuf, TRUE);
7927}
7928
7929/*
7930 * ":recover".
7931 */
7932 static void
7933ex_recover(eap)
7934 exarg_T *eap;
7935{
7936 /* Set recoverymode right away to avoid the ATTENTION prompt. */
7937 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01007938 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
7939 | CCGD_MULTWIN
7940 | (eap->forceit ? CCGD_FORCEIT : 0)
7941 | CCGD_EXCMD)
7942
Bram Moolenaar071d4272004-06-13 20:20:40 +00007943 && (*eap->arg == NUL
7944 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
7945 ml_recover();
7946 recoverymode = FALSE;
7947}
7948
7949/*
7950 * Command modifier used in a wrong way.
7951 */
7952 static void
7953ex_wrongmodifier(eap)
7954 exarg_T *eap;
7955{
7956 eap->errmsg = e_invcmd;
7957}
7958
7959#ifdef FEAT_WINDOWS
7960/*
7961 * :sview [+command] file split window with new file, read-only
7962 * :split [[+command] file] split window with current or new file
7963 * :vsplit [[+command] file] split window vertically with current or new file
7964 * :new [[+command] file] split window with no or new file
7965 * :vnew [[+command] file] split vertically window with no or new file
7966 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007967 *
7968 * :tabedit open new Tab page with empty window
7969 * :tabedit [+command] file open new Tab page and edit "file"
7970 * :tabnew [[+command] file] just like :tabedit
7971 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007972 */
7973 void
7974ex_splitview(eap)
7975 exarg_T *eap;
7976{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007977 win_T *old_curwin = curwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007978# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007979 char_u *fname = NULL;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007980# endif
7981# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007982 int browse_flag = cmdmod.browse;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007983# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007984
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007985# ifndef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007986 if (eap->cmdidx == CMD_vsplit || eap->cmdidx == CMD_vnew)
7987 {
7988 ex_ni(eap);
7989 return;
7990 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007991# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007992
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007993# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007994 need_mouse_correct = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007995# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007996
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007997# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00007998 /* A ":split" in the quickfix window works like ":new". Don't want two
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007999 * quickfix windows. But it's OK when doing ":tab split". */
8000 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008001 {
8002 if (eap->cmdidx == CMD_split)
8003 eap->cmdidx = CMD_new;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008004# ifdef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00008005 if (eap->cmdidx == CMD_vsplit)
8006 eap->cmdidx = CMD_vnew;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008007# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008008 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008009# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008010
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008011# ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008012 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008013 {
8014 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
8015 FNAME_MESS, TRUE, curbuf->b_ffname);
8016 if (fname == NULL)
8017 goto theend;
8018 eap->arg = fname;
8019 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008020# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008021 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008022# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008023# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008024# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008025 if (cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008026# ifdef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00008027 && eap->cmdidx != CMD_vnew
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008028# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008029 && eap->cmdidx != CMD_new)
8030 {
Bram Moolenaar15bfa092008-07-24 16:45:38 +00008031# ifdef FEAT_AUTOCMD
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008032 if (
Bram Moolenaar15bfa092008-07-24 16:45:38 +00008033# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008034 !gui.in_use &&
Bram Moolenaar15bfa092008-07-24 16:45:38 +00008035# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008036 au_has_group((char_u *)"FileExplorer"))
8037 {
8038 /* No browsing supported but we do have the file explorer:
8039 * Edit the directory. */
8040 if (*eap->arg == NUL || !mch_isdir(eap->arg))
8041 eap->arg = (char_u *)".";
8042 }
8043 else
Bram Moolenaar15bfa092008-07-24 16:45:38 +00008044# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008045 {
8046 fname = do_browse(0, (char_u *)_("Edit File in new window"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00008047 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008048 if (fname == NULL)
8049 goto theend;
8050 eap->arg = fname;
8051 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008052 }
8053 cmdmod.browse = FALSE; /* Don't browse again in do_ecmd(). */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008054# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008055
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008056 /*
8057 * Either open new tab page or split the window.
8058 */
8059 if (eap->cmdidx == CMD_tabedit
8060 || eap->cmdidx == CMD_tabfind
8061 || eap->cmdidx == CMD_tabnew)
8062 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00008063 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
8064 : eap->addr_count == 0 ? 0
8065 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008066 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00008067 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008068
8069 /* set the alternate buffer for the window we came from */
8070 if (curwin != old_curwin
8071 && win_valid(old_curwin)
8072 && old_curwin->w_buffer != curbuf
8073 && !cmdmod.keepalt)
8074 old_curwin->w_alt_fnum = curbuf->b_fnum;
8075 }
8076 }
8077 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008078 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
8079 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008080# ifdef FEAT_SCROLLBIND
Bram Moolenaar071d4272004-06-13 20:20:40 +00008081 /* Reset 'scrollbind' when editing another file, but keep it when
8082 * doing ":split" without arguments. */
8083 if (*eap->arg != NUL
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008084# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008085 || cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008086# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008087 )
Bram Moolenaar3368ea22010-09-21 16:56:35 +02008088 {
8089 RESET_BINDING(curwin);
8090 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008091 else
8092 do_check_scrollbind(FALSE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008093# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008094 do_exedit(eap, old_curwin);
8095 }
8096
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008097# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008098 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008099# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008100
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008101# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008102theend:
8103 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008104# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008105}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008106
8107/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008108 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008109 */
8110 void
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008111tabpage_new()
8112{
8113 exarg_T ea;
8114
8115 vim_memset(&ea, 0, sizeof(ea));
8116 ea.cmdidx = CMD_tabnew;
8117 ea.cmd = (char_u *)"tabn";
8118 ea.arg = (char_u *)"";
8119 ex_splitview(&ea);
8120}
8121
8122/*
8123 * :tabnext command
8124 */
8125 static void
8126ex_tabnext(eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008127 exarg_T *eap;
8128{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008129 switch (eap->cmdidx)
8130 {
8131 case CMD_tabfirst:
8132 case CMD_tabrewind:
8133 goto_tabpage(1);
8134 break;
8135 case CMD_tablast:
8136 goto_tabpage(9999);
8137 break;
8138 case CMD_tabprevious:
8139 case CMD_tabNext:
8140 goto_tabpage(eap->addr_count == 0 ? -1 : -(int)eap->line2);
8141 break;
8142 default: /* CMD_tabnext */
8143 goto_tabpage(eap->addr_count == 0 ? 0 : (int)eap->line2);
8144 break;
8145 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008146}
8147
8148/*
8149 * :tabmove command
8150 */
8151 static void
8152ex_tabmove(eap)
8153 exarg_T *eap;
8154{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02008155 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02008156
8157 if (eap->arg && *eap->arg != NUL)
8158 {
8159 char_u *p = eap->arg;
8160 int relative = 0; /* argument +N/-N means: move N places to the
8161 * right/left relative to the current position. */
8162
8163 if (*eap->arg == '-')
8164 {
8165 relative = -1;
8166 p = eap->arg + 1;
8167 }
8168 else if (*eap->arg == '+')
8169 {
8170 relative = 1;
8171 p = eap->arg + 1;
8172 }
8173 else
8174 p = eap->arg;
8175
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02008176 if (relative == 0)
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02008177 {
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02008178 if (STRCMP(p, "$") == 0)
8179 tab_number = LAST_TAB_NR;
8180 else if (p == skipdigits(p))
8181 {
8182 /* No numbers as argument. */
8183 eap->errmsg = e_invarg;
8184 return;
8185 }
8186 else
8187 tab_number = getdigits(&p);
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02008188 }
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02008189 else
8190 {
8191 if (*p != NUL)
8192 tab_number = getdigits(&p);
8193 else
8194 tab_number = 1;
8195 tab_number = tab_number * relative + tabpage_index(curtab);
8196 if (relative == -1)
8197 --tab_number;
8198 }
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02008199 }
8200 else if (eap->addr_count != 0)
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02008201 {
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02008202 tab_number = eap->line2;
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02008203 if (**eap->cmdlinep == '-')
8204 --tab_number;
8205 }
8206 else
8207 tab_number = LAST_TAB_NR;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02008208
8209 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008210}
8211
8212/*
8213 * :tabs command: List tabs and their contents.
8214 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008215 static void
8216ex_tabs(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008217 exarg_T *eap UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008218{
8219 tabpage_T *tp;
8220 win_T *wp;
8221 int tabcount = 1;
8222
8223 msg_start();
8224 msg_scroll = TRUE;
8225 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
8226 {
8227 msg_putchar('\n');
8228 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
8229 msg_outtrans_attr(IObuff, hl_attr(HLF_T));
8230 out_flush(); /* output one line at a time */
8231 ui_breakcheck();
8232
Bram Moolenaar030f0df2006-02-21 22:02:53 +00008233 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008234 wp = firstwin;
8235 else
8236 wp = tp->tp_firstwin;
8237 for ( ; wp != NULL && !got_int; wp = wp->w_next)
8238 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008239 msg_putchar('\n');
8240 msg_putchar(wp == curwin ? '>' : ' ');
8241 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00008242 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
8243 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008244 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02008245 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008246 else
8247 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
8248 IObuff, IOSIZE, TRUE);
8249 msg_outtrans(IObuff);
8250 out_flush(); /* output one line at a time */
8251 ui_breakcheck();
8252 }
8253 }
8254}
8255
8256#endif /* FEAT_WINDOWS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008257
8258/*
8259 * ":mode": Set screen mode.
8260 * If no argument given, just get the screen size and redraw.
8261 */
8262 static void
8263ex_mode(eap)
8264 exarg_T *eap;
8265{
8266 if (*eap->arg == NUL)
8267 shell_resized();
8268 else
8269 mch_screenmode(eap->arg);
8270}
8271
8272#ifdef FEAT_WINDOWS
8273/*
8274 * ":resize".
8275 * set, increment or decrement current window height
8276 */
8277 static void
8278ex_resize(eap)
8279 exarg_T *eap;
8280{
8281 int n;
8282 win_T *wp = curwin;
8283
8284 if (eap->addr_count > 0)
8285 {
8286 n = eap->line2;
8287 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
8288 ;
8289 }
8290
8291#ifdef FEAT_GUI
8292 need_mouse_correct = TRUE;
8293#endif
8294 n = atol((char *)eap->arg);
8295#ifdef FEAT_VERTSPLIT
8296 if (cmdmod.split & WSP_VERT)
8297 {
8298 if (*eap->arg == '-' || *eap->arg == '+')
8299 n += W_WIDTH(curwin);
8300 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
8301 n = 9999;
8302 win_setwidth_win((int)n, wp);
8303 }
8304 else
8305#endif
8306 {
8307 if (*eap->arg == '-' || *eap->arg == '+')
8308 n += curwin->w_height;
8309 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
8310 n = 9999;
8311 win_setheight_win((int)n, wp);
8312 }
8313}
8314#endif
8315
8316/*
8317 * ":find [+command] <file>" command.
8318 */
8319 static void
8320ex_find(eap)
8321 exarg_T *eap;
8322{
8323#ifdef FEAT_SEARCHPATH
8324 char_u *fname;
8325 int count;
8326
8327 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
8328 TRUE, curbuf->b_ffname);
8329 if (eap->addr_count > 0)
8330 {
8331 /* Repeat finding the file "count" times. This matters when it
8332 * appears several times in the path. */
8333 count = eap->line2;
8334 while (fname != NULL && --count > 0)
8335 {
8336 vim_free(fname);
8337 fname = find_file_in_path(NULL, 0, FNAME_MESS,
8338 FALSE, curbuf->b_ffname);
8339 }
8340 }
8341
8342 if (fname != NULL)
8343 {
8344 eap->arg = fname;
8345#endif
8346 do_exedit(eap, NULL);
8347#ifdef FEAT_SEARCHPATH
8348 vim_free(fname);
8349 }
8350#endif
8351}
8352
8353/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00008354 * ":open" simulation: for now just work like ":visual".
8355 */
8356 static void
8357ex_open(eap)
8358 exarg_T *eap;
8359{
8360 regmatch_T regmatch;
8361 char_u *p;
8362
8363 curwin->w_cursor.lnum = eap->line2;
8364 beginline(BL_SOL | BL_FIX);
8365 if (*eap->arg == '/')
8366 {
8367 /* ":open /pattern/": put cursor in column found with pattern */
8368 ++eap->arg;
8369 p = skip_regexp(eap->arg, '/', p_magic, NULL);
8370 *p = NUL;
8371 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
8372 if (regmatch.regprog != NULL)
8373 {
8374 regmatch.rm_ic = p_ic;
8375 p = ml_get_curline();
8376 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008377 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008378 else
8379 EMSG(_(e_nomatch));
Bram Moolenaar473de612013-06-08 18:19:48 +02008380 vim_regfree(regmatch.regprog);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008381 }
8382 /* Move to the NUL, ignore any other arguments. */
8383 eap->arg += STRLEN(eap->arg);
8384 }
8385 check_cursor();
8386
8387 eap->cmdidx = CMD_visual;
8388 do_exedit(eap, NULL);
8389}
8390
8391/*
8392 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008393 */
8394 static void
8395ex_edit(eap)
8396 exarg_T *eap;
8397{
8398 do_exedit(eap, NULL);
8399}
8400
8401/*
8402 * ":edit <file>" command and alikes.
8403 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008404 void
8405do_exedit(eap, old_curwin)
8406 exarg_T *eap;
8407 win_T *old_curwin; /* curwin before doing a split or NULL */
8408{
8409 int n;
8410#ifdef FEAT_WINDOWS
8411 int need_hide;
8412#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00008413 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008414
8415 /*
8416 * ":vi" command ends Ex mode.
8417 */
8418 if (exmode_active && (eap->cmdidx == CMD_visual
8419 || eap->cmdidx == CMD_view))
8420 {
8421 exmode_active = FALSE;
8422 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008423 {
8424 /* Special case: ":global/pat/visual\NLvi-commands" */
8425 if (global_busy)
8426 {
8427 int rd = RedrawingDisabled;
8428 int nwr = no_wait_return;
8429 int ms = msg_scroll;
8430#ifdef FEAT_GUI
8431 int he = hold_gui_events;
8432#endif
8433
8434 if (eap->nextcmd != NULL)
8435 {
8436 stuffReadbuff(eap->nextcmd);
8437 eap->nextcmd = NULL;
8438 }
8439
8440 if (exmode_was != EXMODE_VIM)
8441 settmode(TMODE_RAW);
8442 RedrawingDisabled = 0;
8443 no_wait_return = 0;
8444 need_wait_return = FALSE;
8445 msg_scroll = 0;
8446#ifdef FEAT_GUI
8447 hold_gui_events = 0;
8448#endif
8449 must_redraw = CLEAR;
8450
8451 main_loop(FALSE, TRUE);
8452
8453 RedrawingDisabled = rd;
8454 no_wait_return = nwr;
8455 msg_scroll = ms;
8456#ifdef FEAT_GUI
8457 hold_gui_events = he;
8458#endif
8459 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008460 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008461 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008462 }
8463
8464 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008465 || eap->cmdidx == CMD_tabnew
8466 || eap->cmdidx == CMD_tabedit
Bram Moolenaar071d4272004-06-13 20:20:40 +00008467#ifdef FEAT_VERTSPLIT
8468 || eap->cmdidx == CMD_vnew
8469#endif
8470 ) && *eap->arg == NUL)
8471 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008472 /* ":new" or ":tabnew" without argument: edit an new empty buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008473 setpcmark();
8474 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00008475 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
8476 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008477 }
8478 else if ((eap->cmdidx != CMD_split
8479#ifdef FEAT_VERTSPLIT
8480 && eap->cmdidx != CMD_vsplit
8481#endif
8482 )
8483 || *eap->arg != NUL
8484#ifdef FEAT_BROWSE
8485 || cmdmod.browse
8486#endif
8487 )
8488 {
Bram Moolenaar5555acc2006-04-07 21:33:12 +00008489#ifdef FEAT_AUTOCMD
8490 /* Can't edit another file when "curbuf_lock" is set. Only ":edit"
8491 * can bring us here, others are stopped earlier. */
8492 if (*eap->arg != NUL && curbuf_locked())
8493 return;
8494#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008495 n = readonlymode;
8496 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
8497 readonlymode = TRUE;
8498 else if (eap->cmdidx == CMD_enew)
8499 readonlymode = FALSE; /* 'readonly' doesn't make sense in an
8500 empty buffer */
8501 setpcmark();
8502 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
8503 NULL, eap,
8504 /* ":edit" goes to first line if Vi compatible */
8505 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
8506 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
8507 ? ECMD_ONE : eap->do_ecmd_lnum,
8508 (P_HID(curbuf) ? ECMD_HIDE : 0)
8509 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar7b449342014-03-25 13:03:48 +01008510 /* after a split we can use an existing buffer */
8511 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008512#ifdef FEAT_LISTCMDS
8513 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
8514#endif
Bram Moolenaar701f7af2008-11-15 13:12:07 +00008515 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008516 {
8517 /* Editing the file failed. If the window was split, close it. */
8518#ifdef FEAT_WINDOWS
8519 if (old_curwin != NULL)
8520 {
8521 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
8522 if (!need_hide || P_HID(curbuf))
8523 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008524# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
8525 cleanup_T cs;
8526
8527 /* Reset the error/interrupt/exception state here so that
8528 * aborting() returns FALSE when closing a window. */
8529 enter_cleanup(&cs);
8530# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008531# ifdef FEAT_GUI
8532 need_mouse_correct = TRUE;
8533# endif
8534 win_close(curwin, !need_hide && !P_HID(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008535
8536# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
8537 /* Restore the error/interrupt/exception state if not
8538 * discarded by a new aborting error, interrupt, or
8539 * uncaught exception. */
8540 leave_cleanup(&cs);
8541# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008542 }
8543 }
8544#endif
8545 }
8546 else if (readonlymode && curbuf->b_nwindows == 1)
8547 {
8548 /* When editing an already visited buffer, 'readonly' won't be set
8549 * but the previous value is kept. With ":view" and ":sview" we
8550 * want the file to be readonly, except when another window is
8551 * editing the same buffer. */
8552 curbuf->b_p_ro = TRUE;
8553 }
8554 readonlymode = n;
8555 }
8556 else
8557 {
8558 if (eap->do_ecmd_cmd != NULL)
8559 do_cmdline_cmd(eap->do_ecmd_cmd);
8560#ifdef FEAT_TITLE
8561 n = curwin->w_arg_idx_invalid;
8562#endif
8563 check_arg_idx(curwin);
8564#ifdef FEAT_TITLE
8565 if (n != curwin->w_arg_idx_invalid)
8566 maketitle();
8567#endif
8568 }
8569
8570#ifdef FEAT_WINDOWS
8571 /*
8572 * if ":split file" worked, set alternate file name in old window to new
8573 * file
8574 */
8575 if (old_curwin != NULL
8576 && *eap->arg != NUL
8577 && curwin != old_curwin
8578 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00008579 && old_curwin->w_buffer != curbuf
8580 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008581 old_curwin->w_alt_fnum = curbuf->b_fnum;
8582#endif
8583
8584 ex_no_reprint = TRUE;
8585}
8586
8587#ifndef FEAT_GUI
8588/*
8589 * ":gui" and ":gvim" when there is no GUI.
8590 */
8591 static void
8592ex_nogui(eap)
8593 exarg_T *eap;
8594{
8595 eap->errmsg = e_nogvim;
8596}
8597#endif
8598
8599#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
8600 static void
8601ex_tearoff(eap)
8602 exarg_T *eap;
8603{
8604 gui_make_tearoff(eap->arg);
8605}
8606#endif
8607
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008608#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008609 static void
8610ex_popup(eap)
8611 exarg_T *eap;
8612{
Bram Moolenaar97409f12005-07-08 22:17:29 +00008613 gui_make_popup(eap->arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008614}
8615#endif
8616
Bram Moolenaar071d4272004-06-13 20:20:40 +00008617 static void
8618ex_swapname(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008619 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008620{
8621 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
8622 MSG(_("No swap file"));
8623 else
8624 msg(curbuf->b_ml.ml_mfp->mf_fname);
8625}
8626
8627/*
8628 * ":syncbind" forces all 'scrollbind' windows to have the same relative
8629 * offset.
8630 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
8631 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008632 static void
8633ex_syncbind(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008634 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008635{
8636#ifdef FEAT_SCROLLBIND
8637 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008638 win_T *save_curwin = curwin;
8639 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008640 long topline;
8641 long y;
8642 linenr_T old_linenr = curwin->w_cursor.lnum;
8643
8644 setpcmark();
8645
8646 /*
8647 * determine max topline
8648 */
8649 if (curwin->w_p_scb)
8650 {
8651 topline = curwin->w_topline;
8652 for (wp = firstwin; wp; wp = wp->w_next)
8653 {
8654 if (wp->w_p_scb && wp->w_buffer)
8655 {
8656 y = wp->w_buffer->b_ml.ml_line_count - p_so;
8657 if (topline > y)
8658 topline = y;
8659 }
8660 }
8661 if (topline < 1)
8662 topline = 1;
8663 }
8664 else
8665 {
8666 topline = 1;
8667 }
8668
8669
8670 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008671 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008672 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008673 for (curwin = firstwin; curwin; curwin = curwin->w_next)
8674 {
8675 if (curwin->w_p_scb)
8676 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008677 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008678 y = topline - curwin->w_topline;
8679 if (y > 0)
8680 scrollup(y, TRUE);
8681 else
8682 scrolldown(-y, TRUE);
8683 curwin->w_scbind_pos = topline;
8684 redraw_later(VALID);
8685 cursor_correct();
8686#ifdef FEAT_WINDOWS
8687 curwin->w_redr_status = TRUE;
8688#endif
8689 }
8690 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008691 curwin = save_curwin;
8692 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008693 if (curwin->w_p_scb)
8694 {
8695 did_syncbind = TRUE;
8696 checkpcmark();
8697 if (old_linenr != curwin->w_cursor.lnum)
8698 {
8699 char_u ctrl_o[2];
8700
8701 ctrl_o[0] = Ctrl_O;
8702 ctrl_o[1] = 0;
8703 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
8704 }
8705 }
8706#endif
8707}
8708
8709
8710 static void
8711ex_read(eap)
8712 exarg_T *eap;
8713{
Bram Moolenaardf177f62005-02-22 08:39:57 +00008714 int i;
8715 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
8716 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008717
8718 if (eap->usefilter) /* :r!cmd */
8719 do_bang(1, eap, FALSE, FALSE, TRUE);
8720 else
8721 {
8722 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
8723 return;
8724
8725#ifdef FEAT_BROWSE
8726 if (cmdmod.browse)
8727 {
8728 char_u *browseFile;
8729
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008730 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008731 NULL, NULL, NULL, curbuf);
8732 if (browseFile != NULL)
8733 {
8734 i = readfile(browseFile, NULL,
8735 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8736 vim_free(browseFile);
8737 }
8738 else
8739 i = OK;
8740 }
8741 else
8742#endif
8743 if (*eap->arg == NUL)
8744 {
8745 if (check_fname() == FAIL) /* check for no file name */
8746 return;
8747 i = readfile(curbuf->b_ffname, curbuf->b_fname,
8748 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8749 }
8750 else
8751 {
8752 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
8753 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
8754 i = readfile(eap->arg, NULL,
8755 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8756
8757 }
8758 if (i == FAIL)
8759 {
8760#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
8761 if (!aborting())
8762#endif
8763 EMSG2(_(e_notopen), eap->arg);
8764 }
8765 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008766 {
8767 if (empty && exmode_active)
8768 {
8769 /* Delete the empty line that remains. Historically ex does
8770 * this but vi doesn't. */
8771 if (eap->line2 == 0)
8772 lnum = curbuf->b_ml.ml_line_count;
8773 else
8774 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008775 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008776 {
8777 ml_delete(lnum, FALSE);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008778 if (curwin->w_cursor.lnum > 1
8779 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008780 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00008781 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008782 }
8783 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008784 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008785 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008786 }
8787}
8788
Bram Moolenaarea408852005-06-25 22:49:46 +00008789static char_u *prev_dir = NULL;
8790
8791#if defined(EXITFREE) || defined(PROTO)
8792 void
8793free_cd_dir()
8794{
8795 vim_free(prev_dir);
Bram Moolenaara0174af2008-01-02 20:08:25 +00008796 prev_dir = NULL;
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00008797
8798 vim_free(globaldir);
8799 globaldir = NULL;
Bram Moolenaarea408852005-06-25 22:49:46 +00008800}
8801#endif
8802
Bram Moolenaarf4258302013-06-02 18:20:17 +02008803/*
8804 * Deal with the side effects of changing the current directory.
8805 * When "local" is TRUE then this was after an ":lcd" command.
8806 */
8807 void
8808post_chdir(local)
8809 int local;
8810{
8811 vim_free(curwin->w_localdir);
Bram Moolenaarbd2dc342014-01-10 15:53:13 +01008812 curwin->w_localdir = NULL;
Bram Moolenaarf4258302013-06-02 18:20:17 +02008813 if (local)
8814 {
8815 /* If still in global directory, need to remember current
8816 * directory as global directory. */
8817 if (globaldir == NULL && prev_dir != NULL)
8818 globaldir = vim_strsave(prev_dir);
8819 /* Remember this local directory for the window. */
8820 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8821 curwin->w_localdir = vim_strsave(NameBuff);
8822 }
8823 else
8824 {
8825 /* We are now in the global directory, no need to remember its
8826 * name. */
8827 vim_free(globaldir);
8828 globaldir = NULL;
Bram Moolenaarf4258302013-06-02 18:20:17 +02008829 }
8830
8831 shorten_fnames(TRUE);
8832}
8833
Bram Moolenaarea408852005-06-25 22:49:46 +00008834
Bram Moolenaar071d4272004-06-13 20:20:40 +00008835/*
8836 * ":cd", ":lcd", ":chdir" and ":lchdir".
8837 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00008838 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00008839ex_cd(eap)
8840 exarg_T *eap;
8841{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008842 char_u *new_dir;
8843 char_u *tofree;
8844
8845 new_dir = eap->arg;
8846#if !defined(UNIX) && !defined(VMS)
8847 /* for non-UNIX ":cd" means: print current directory */
8848 if (*new_dir == NUL)
8849 ex_pwd(NULL);
8850 else
8851#endif
8852 {
Bram Moolenaar8e8fe9b2009-03-11 14:37:32 +00008853#ifdef FEAT_AUTOCMD
8854 if (allbuf_locked())
8855 return;
8856#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00008857 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged()
8858 && !eap->forceit)
8859 {
Bram Moolenaar81870892007-11-11 18:17:28 +00008860 EMSG(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00008861 return;
8862 }
8863
Bram Moolenaar071d4272004-06-13 20:20:40 +00008864 /* ":cd -": Change to previous directory */
8865 if (STRCMP(new_dir, "-") == 0)
8866 {
8867 if (prev_dir == NULL)
8868 {
8869 EMSG(_("E186: No previous directory"));
8870 return;
8871 }
8872 new_dir = prev_dir;
8873 }
8874
8875 /* Save current directory for next ":cd -" */
8876 tofree = prev_dir;
8877 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8878 prev_dir = vim_strsave(NameBuff);
8879 else
8880 prev_dir = NULL;
8881
8882#if defined(UNIX) || defined(VMS)
8883 /* for UNIX ":cd" means: go to home directory */
8884 if (*new_dir == NUL)
8885 {
8886 /* use NameBuff for home directory name */
8887# ifdef VMS
8888 char_u *p;
8889
8890 p = mch_getenv((char_u *)"SYS$LOGIN");
8891 if (p == NULL || *p == NUL) /* empty is the same as not set */
8892 NameBuff[0] = NUL;
8893 else
Bram Moolenaarb6356332005-07-18 21:40:44 +00008894 vim_strncpy(NameBuff, p, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008895# else
8896 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
8897# endif
8898 new_dir = NameBuff;
8899 }
8900#endif
8901 if (new_dir == NULL || vim_chdir(new_dir))
8902 EMSG(_(e_failed));
8903 else
8904 {
Bram Moolenaarf4258302013-06-02 18:20:17 +02008905 post_chdir(eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008906
8907 /* Echo the new current directory if the command was typed. */
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00008908 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008909 ex_pwd(eap);
8910 }
8911 vim_free(tofree);
8912 }
8913}
8914
8915/*
8916 * ":pwd".
8917 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008918 static void
8919ex_pwd(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008920 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008921{
8922 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8923 {
8924#ifdef BACKSLASH_IN_FILENAME
8925 slash_adjust(NameBuff);
8926#endif
8927 msg(NameBuff);
8928 }
8929 else
8930 EMSG(_("E187: Unknown"));
8931}
8932
8933/*
8934 * ":=".
8935 */
8936 static void
8937ex_equal(eap)
8938 exarg_T *eap;
8939{
8940 smsg((char_u *)"%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008941 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008942}
8943
8944 static void
8945ex_sleep(eap)
8946 exarg_T *eap;
8947{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008948 int n;
8949 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008950
8951 if (cursor_valid())
8952 {
8953 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
8954 if (n >= 0)
Bram Moolenaar10395d82014-02-05 22:46:52 +01008955 windgoto((int)n, W_WINCOL(curwin) + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008956 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008957
8958 len = eap->line2;
8959 switch (*eap->arg)
8960 {
8961 case 'm': break;
8962 case NUL: len *= 1000L; break;
8963 default: EMSG2(_(e_invarg2), eap->arg); return;
8964 }
8965 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008966}
8967
8968/*
8969 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
8970 */
8971 void
8972do_sleep(msec)
8973 long msec;
8974{
8975 long done;
8976
8977 cursor_on();
8978 out_flush();
8979 for (done = 0; !got_int && done < msec; done += 1000L)
8980 {
8981 ui_delay(msec - done > 1000L ? 1000L : msec - done, TRUE);
8982 ui_breakcheck();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02008983#ifdef FEAT_NETBEANS_INTG
8984 /* Process the netbeans messages that may have been received in the
8985 * call to ui_breakcheck() when the GUI is in use. This may occur when
8986 * running a test case. */
8987 netbeans_parse_messages();
8988#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008989 }
8990}
8991
8992 static void
8993do_exmap(eap, isabbrev)
8994 exarg_T *eap;
8995 int isabbrev;
8996{
8997 int mode;
8998 char_u *cmdp;
8999
9000 cmdp = eap->cmd;
9001 mode = get_map_mode(&cmdp, eap->forceit || isabbrev);
9002
9003 switch (do_map((*cmdp == 'n') ? 2 : (*cmdp == 'u'),
9004 eap->arg, mode, isabbrev))
9005 {
9006 case 1: EMSG(_(e_invarg));
9007 break;
9008 case 2: EMSG(isabbrev ? _(e_noabbr) : _(e_nomap));
9009 break;
9010 }
9011}
9012
9013/*
9014 * ":winsize" command (obsolete).
9015 */
9016 static void
9017ex_winsize(eap)
9018 exarg_T *eap;
9019{
9020 int w, h;
9021 char_u *arg = eap->arg;
9022 char_u *p;
9023
9024 w = getdigits(&arg);
9025 arg = skipwhite(arg);
9026 p = arg;
9027 h = getdigits(&arg);
9028 if (*p != NUL && *arg == NUL)
9029 set_shellsize(w, h, TRUE);
9030 else
9031 EMSG(_("E465: :winsize requires two number arguments"));
9032}
9033
9034#ifdef FEAT_WINDOWS
9035 static void
9036ex_wincmd(eap)
9037 exarg_T *eap;
9038{
9039 int xchar = NUL;
9040 char_u *p;
9041
9042 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
9043 {
9044 /* CTRL-W g and CTRL-W CTRL-G have an extra command character */
9045 if (eap->arg[1] == NUL)
9046 {
9047 EMSG(_(e_invarg));
9048 return;
9049 }
9050 xchar = eap->arg[1];
9051 p = eap->arg + 2;
9052 }
9053 else
9054 p = eap->arg + 1;
9055
9056 eap->nextcmd = check_nextcmd(p);
9057 p = skipwhite(p);
9058 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
9059 EMSG(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02009060 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009061 {
9062 /* Pass flags on for ":vertical wincmd ]". */
9063 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009064 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009065 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
9066 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009067 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009068 }
9069}
9070#endif
9071
Bram Moolenaar843ee412004-06-30 16:16:41 +00009072#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009073/*
9074 * ":winpos".
9075 */
9076 static void
9077ex_winpos(eap)
9078 exarg_T *eap;
9079{
9080 int x, y;
9081 char_u *arg = eap->arg;
9082 char_u *p;
9083
9084 if (*arg == NUL)
9085 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00009086# if defined(FEAT_GUI) || defined(MSWIN)
9087# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00009088 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00009089# else
9090 if (mch_get_winpos(&x, &y) != FAIL)
9091# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009092 {
9093 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
9094 msg(IObuff);
9095 }
9096 else
9097# endif
9098 EMSG(_("E188: Obtaining window position not implemented for this platform"));
9099 }
9100 else
9101 {
9102 x = getdigits(&arg);
9103 arg = skipwhite(arg);
9104 p = arg;
9105 y = getdigits(&arg);
9106 if (*p == NUL || *arg != NUL)
9107 {
9108 EMSG(_("E466: :winpos requires two number arguments"));
9109 return;
9110 }
9111# ifdef FEAT_GUI
9112 if (gui.in_use)
9113 gui_mch_set_winpos(x, y);
9114 else if (gui.starting)
9115 {
9116 /* Remember the coordinates for when the window is opened. */
9117 gui_win_x = x;
9118 gui_win_y = y;
9119 }
9120# ifdef HAVE_TGETENT
9121 else
9122# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00009123# else
9124# ifdef MSWIN
9125 mch_set_winpos(x, y);
9126# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009127# endif
9128# ifdef HAVE_TGETENT
9129 if (*T_CWP)
9130 term_set_winpos(x, y);
9131# endif
9132 }
9133}
9134#endif
9135
9136/*
9137 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
9138 */
9139 static void
9140ex_operators(eap)
9141 exarg_T *eap;
9142{
9143 oparg_T oa;
9144
9145 clear_oparg(&oa);
9146 oa.regname = eap->regname;
9147 oa.start.lnum = eap->line1;
9148 oa.end.lnum = eap->line2;
9149 oa.line_count = eap->line2 - eap->line1 + 1;
9150 oa.motion_type = MLINE;
9151#ifdef FEAT_VIRTUALEDIT
9152 virtual_op = FALSE;
9153#endif
9154 if (eap->cmdidx != CMD_yank) /* position cursor for undo */
9155 {
9156 setpcmark();
9157 curwin->w_cursor.lnum = eap->line1;
9158 beginline(BL_SOL | BL_FIX);
9159 }
9160
Bram Moolenaard07c6e12013-11-21 14:21:40 +01009161 if (VIsual_active)
9162 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01009163
Bram Moolenaar071d4272004-06-13 20:20:40 +00009164 switch (eap->cmdidx)
9165 {
9166 case CMD_delete:
9167 oa.op_type = OP_DELETE;
9168 op_delete(&oa);
9169 break;
9170
9171 case CMD_yank:
9172 oa.op_type = OP_YANK;
9173 (void)op_yank(&oa, FALSE, TRUE);
9174 break;
9175
9176 default: /* CMD_rshift or CMD_lshift */
Bram Moolenaar6e707362013-06-14 19:15:58 +02009177 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009178#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02009179 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
9180#else
9181 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00009182#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02009183 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009184 oa.op_type = OP_RSHIFT;
9185 else
9186 oa.op_type = OP_LSHIFT;
9187 op_shift(&oa, FALSE, eap->amount);
9188 break;
9189 }
9190#ifdef FEAT_VIRTUALEDIT
9191 virtual_op = MAYBE;
9192#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00009193 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009194}
9195
9196/*
9197 * ":put".
9198 */
9199 static void
9200ex_put(eap)
9201 exarg_T *eap;
9202{
9203 /* ":0put" works like ":1put!". */
9204 if (eap->line2 == 0)
9205 {
9206 eap->line2 = 1;
9207 eap->forceit = TRUE;
9208 }
9209 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009210 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
9211 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009212}
9213
9214/*
9215 * Handle ":copy" and ":move".
9216 */
9217 static void
9218ex_copymove(eap)
9219 exarg_T *eap;
9220{
9221 long n;
9222
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01009223 n = get_address(&eap->arg, eap->addr_type, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009224 if (eap->arg == NULL) /* error detected */
9225 {
9226 eap->nextcmd = NULL;
9227 return;
9228 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00009229 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009230
9231 /*
9232 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
9233 */
9234 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
9235 {
9236 EMSG(_(e_invaddr));
9237 return;
9238 }
9239
9240 if (eap->cmdidx == CMD_move)
9241 {
9242 if (do_move(eap->line1, eap->line2, n) == FAIL)
9243 return;
9244 }
9245 else
9246 ex_copy(eap->line1, eap->line2, n);
9247 u_clearline();
9248 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009249 ex_may_print(eap);
9250}
9251
9252/*
9253 * Print the current line if flags were given to the Ex command.
9254 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02009255 void
Bram Moolenaardf177f62005-02-22 08:39:57 +00009256ex_may_print(eap)
9257 exarg_T *eap;
9258{
9259 if (eap->flags != 0)
9260 {
9261 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
9262 (eap->flags & EXFLAG_LIST));
9263 ex_no_reprint = TRUE;
9264 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009265}
9266
9267/*
9268 * ":smagic" and ":snomagic".
9269 */
9270 static void
9271ex_submagic(eap)
9272 exarg_T *eap;
9273{
9274 int magic_save = p_magic;
9275
9276 p_magic = (eap->cmdidx == CMD_smagic);
9277 do_sub(eap);
9278 p_magic = magic_save;
9279}
9280
9281/*
9282 * ":join".
9283 */
9284 static void
9285ex_join(eap)
9286 exarg_T *eap;
9287{
9288 curwin->w_cursor.lnum = eap->line1;
9289 if (eap->line1 == eap->line2)
9290 {
9291 if (eap->addr_count >= 2) /* :2,2join does nothing */
9292 return;
9293 if (eap->line2 == curbuf->b_ml.ml_line_count)
9294 {
9295 beep_flush();
9296 return;
9297 }
9298 ++eap->line2;
9299 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02009300 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009301 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009302 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009303}
9304
9305/*
9306 * ":[addr]@r" or ":[addr]*r": execute register
9307 */
9308 static void
9309ex_at(eap)
9310 exarg_T *eap;
9311{
9312 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00009313 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009314
9315 curwin->w_cursor.lnum = eap->line2;
9316
9317#ifdef USE_ON_FLY_SCROLL
9318 dont_scroll = TRUE; /* disallow scrolling here */
9319#endif
9320
9321 /* get the register name. No name means to use the previous one */
9322 c = *eap->arg;
9323 if (c == NUL || (c == '*' && *eap->cmd == '*'))
9324 c = '@';
Bram Moolenaard333d1e2006-11-07 17:43:47 +00009325 /* Put the register in the typeahead buffer with the "silent" flag. */
9326 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
9327 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009328 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009329 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00009330 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009331 else
9332 {
9333 int save_efr = exec_from_reg;
9334
9335 exec_from_reg = TRUE;
9336
9337 /*
9338 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00009339 * Continue until the stuff buffer is empty and all added characters
9340 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009341 */
Bram Moolenaar60462872009-11-03 11:40:19 +00009342 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009343 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
9344
9345 exec_from_reg = save_efr;
9346 }
9347}
9348
9349/*
9350 * ":!".
9351 */
9352 static void
9353ex_bang(eap)
9354 exarg_T *eap;
9355{
9356 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
9357}
9358
9359/*
9360 * ":undo".
9361 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009362 static void
9363ex_undo(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009364 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009365{
Bram Moolenaard3667a22006-03-16 21:35:52 +00009366 if (eap->addr_count == 1) /* :undo 123 */
Bram Moolenaar730cde92010-06-27 05:18:54 +02009367 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00009368 else
9369 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009370}
9371
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009372#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02009373 static void
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009374ex_wundo(eap)
9375 exarg_T *eap;
9376{
9377 char_u hash[UNDO_HASH_SIZE];
9378
9379 u_compute_hash(hash);
9380 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
9381}
9382
Bram Moolenaar6df6f472010-07-18 18:04:50 +02009383 static void
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009384ex_rundo(eap)
9385 exarg_T *eap;
9386{
9387 char_u hash[UNDO_HASH_SIZE];
9388
9389 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02009390 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009391}
9392#endif
9393
Bram Moolenaar071d4272004-06-13 20:20:40 +00009394/*
9395 * ":redo".
9396 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009397 static void
9398ex_redo(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009399 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009400{
9401 u_redo(1);
9402}
9403
9404/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009405 * ":earlier" and ":later".
9406 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009407 static void
9408ex_later(eap)
9409 exarg_T *eap;
9410{
9411 long count = 0;
9412 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02009413 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009414 char_u *p = eap->arg;
9415
9416 if (*p == NUL)
9417 count = 1;
9418 else if (isdigit(*p))
9419 {
9420 count = getdigits(&p);
9421 switch (*p)
9422 {
9423 case 's': ++p; sec = TRUE; break;
9424 case 'm': ++p; sec = TRUE; count *= 60; break;
9425 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02009426 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
9427 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009428 }
9429 }
9430
9431 if (*p != NUL)
9432 EMSG2(_(e_invarg2), eap->arg);
9433 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02009434 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
9435 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009436}
9437
9438/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009439 * ":redir": start/stop redirection.
9440 */
9441 static void
9442ex_redir(eap)
9443 exarg_T *eap;
9444{
9445 char *mode;
9446 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00009447 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009448
9449 if (STRICMP(eap->arg, "END") == 0)
9450 close_redir();
9451 else
9452 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009453 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009454 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009455 ++arg;
9456 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009457 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009458 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009459 mode = "a";
9460 }
9461 else
9462 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00009463 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009464
9465 close_redir();
9466
9467 /* Expand environment variables and "~/". */
Bram Moolenaarca472992005-01-21 11:46:23 +00009468 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009469 if (fname == NULL)
9470 return;
9471#ifdef FEAT_BROWSE
9472 if (cmdmod.browse)
9473 {
9474 char_u *browseFile;
9475
Bram Moolenaar7171abe2004-10-11 10:06:20 +00009476 browseFile = do_browse(BROWSE_SAVE,
9477 (char_u *)_("Save Redirection"),
9478 fname, NULL, NULL, BROWSE_FILTER_ALL_FILES, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009479 if (browseFile == NULL)
9480 return; /* operation cancelled */
9481 vim_free(fname);
9482 fname = browseFile;
9483 eap->forceit = TRUE; /* since dialog already asked */
9484 }
9485#endif
9486
9487 redir_fd = open_exfile(fname, eap->forceit, mode);
9488 vim_free(fname);
9489 }
9490#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00009491 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009492 {
9493 /* redirect to a register a-z (resp. A-Z for appending) */
9494 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00009495 ++arg;
9496 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009497# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00009498 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00009499 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00009500# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00009501 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009502 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009503 redir_reg = *arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009504 if (*arg == '>' && arg[1] == '>') /* append */
Bram Moolenaard9d30582005-05-18 22:10:28 +00009505 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009506 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009507 {
Bram Moolenaarc188b882007-10-19 14:20:54 +00009508 /* Can use both "@a" and "@a>". */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00009509 if (*arg == '>')
9510 arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009511 /* Make register empty when not using @A-@Z and the
9512 * command is valid. */
9513 if (*arg == NUL && !isupper(redir_reg))
9514 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009515 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00009516 }
9517 if (*arg != NUL)
9518 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00009519 redir_reg = 0;
Bram Moolenaard9d30582005-05-18 22:10:28 +00009520 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009521 }
9522 }
9523 else if (*arg == '=' && arg[1] == '>')
9524 {
9525 int append;
9526
9527 /* redirect to a variable */
9528 close_redir();
9529 arg += 2;
9530
9531 if (*arg == '>')
9532 {
9533 ++arg;
9534 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009535 }
9536 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00009537 append = FALSE;
9538
9539 if (var_redir_start(skipwhite(arg), append) == OK)
9540 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009541 }
9542#endif
9543
9544 /* TODO: redirect to a buffer */
9545
Bram Moolenaar071d4272004-06-13 20:20:40 +00009546 else
Bram Moolenaarca472992005-01-21 11:46:23 +00009547 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009548 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00009549
9550 /* Make sure redirection is not off. Can happen for cmdline completion
9551 * that indirectly invokes a command to catch its output. */
9552 if (redir_fd != NULL
9553#ifdef FEAT_EVAL
9554 || redir_reg || redir_vname
9555#endif
9556 )
9557 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009558}
9559
9560/*
9561 * ":redraw": force redraw
9562 */
9563 static void
9564ex_redraw(eap)
9565 exarg_T *eap;
9566{
9567 int r = RedrawingDisabled;
9568 int p = p_lz;
9569
9570 RedrawingDisabled = 0;
9571 p_lz = FALSE;
9572 update_topline();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009573 update_screen(eap->forceit ? CLEAR : VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009574#ifdef FEAT_TITLE
9575 if (need_maketitle)
9576 maketitle();
9577#endif
9578 RedrawingDisabled = r;
9579 p_lz = p;
9580
9581 /* Reset msg_didout, so that a message that's there is overwritten. */
9582 msg_didout = FALSE;
9583 msg_col = 0;
9584
Bram Moolenaar56667a52013-07-17 11:54:28 +02009585 /* No need to wait after an intentional redraw. */
9586 need_wait_return = FALSE;
9587
Bram Moolenaar071d4272004-06-13 20:20:40 +00009588 out_flush();
9589}
9590
9591/*
9592 * ":redrawstatus": force redraw of status line(s)
9593 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009594 static void
9595ex_redrawstatus(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009596 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009597{
9598#if defined(FEAT_WINDOWS)
9599 int r = RedrawingDisabled;
9600 int p = p_lz;
9601
9602 RedrawingDisabled = 0;
9603 p_lz = FALSE;
9604 if (eap->forceit)
9605 status_redraw_all();
9606 else
9607 status_redraw_curbuf();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009608 update_screen(VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009609 RedrawingDisabled = r;
9610 p_lz = p;
9611 out_flush();
9612#endif
9613}
9614
9615 static void
9616close_redir()
9617{
9618 if (redir_fd != NULL)
9619 {
9620 fclose(redir_fd);
9621 redir_fd = NULL;
9622 }
9623#ifdef FEAT_EVAL
9624 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009625 if (redir_vname)
9626 {
9627 var_redir_stop();
9628 redir_vname = 0;
9629 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009630#endif
9631}
9632
9633#if defined(FEAT_SESSION) && defined(USE_CRNL)
9634# define MKSESSION_NL
9635static int mksession_nl = FALSE; /* use NL only in put_eol() */
9636#endif
9637
9638/*
9639 * ":mkexrc", ":mkvimrc", ":mkview" and ":mksession".
9640 */
9641 static void
9642ex_mkrc(eap)
9643 exarg_T *eap;
9644{
9645 FILE *fd;
9646 int failed = FALSE;
9647 char_u *fname;
9648#ifdef FEAT_BROWSE
9649 char_u *browseFile = NULL;
9650#endif
9651#ifdef FEAT_SESSION
9652 int view_session = FALSE;
9653 int using_vdir = FALSE; /* using 'viewdir'? */
9654 char_u *viewFile = NULL;
9655 unsigned *flagp;
9656#endif
9657
9658 if (eap->cmdidx == CMD_mksession || eap->cmdidx == CMD_mkview)
9659 {
9660#ifdef FEAT_SESSION
9661 view_session = TRUE;
9662#else
9663 ex_ni(eap);
9664 return;
9665#endif
9666 }
9667
9668#ifdef FEAT_SESSION
Bram Moolenaar8d594672009-07-01 18:18:57 +00009669 /* Use the short file name until ":lcd" is used. We also don't use the
9670 * short file name when 'acd' is set, that is checked later. */
Bram Moolenaareeefcc72007-05-01 21:21:21 +00009671 did_lcd = FALSE;
9672
Bram Moolenaar071d4272004-06-13 20:20:40 +00009673 /* ":mkview" or ":mkview 9": generate file name with 'viewdir' */
9674 if (eap->cmdidx == CMD_mkview
9675 && (*eap->arg == NUL
9676 || (vim_isdigit(*eap->arg) && eap->arg[1] == NUL)))
9677 {
9678 eap->forceit = TRUE;
9679 fname = get_view_file(*eap->arg);
9680 if (fname == NULL)
9681 return;
9682 viewFile = fname;
9683 using_vdir = TRUE;
9684 }
9685 else
9686#endif
9687 if (*eap->arg != NUL)
9688 fname = eap->arg;
9689 else if (eap->cmdidx == CMD_mkvimrc)
9690 fname = (char_u *)VIMRC_FILE;
9691#ifdef FEAT_SESSION
9692 else if (eap->cmdidx == CMD_mksession)
9693 fname = (char_u *)SESSION_FILE;
9694#endif
9695 else
9696 fname = (char_u *)EXRC_FILE;
9697
9698#ifdef FEAT_BROWSE
9699 if (cmdmod.browse)
9700 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00009701 browseFile = do_browse(BROWSE_SAVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009702# ifdef FEAT_SESSION
9703 eap->cmdidx == CMD_mkview ? (char_u *)_("Save View") :
9704 eap->cmdidx == CMD_mksession ? (char_u *)_("Save Session") :
9705# endif
9706 (char_u *)_("Save Setup"),
9707 fname, (char_u *)"vim", NULL, BROWSE_FILTER_MACROS, NULL);
9708 if (browseFile == NULL)
9709 goto theend;
9710 fname = browseFile;
9711 eap->forceit = TRUE; /* since dialog already asked */
9712 }
9713#endif
9714
9715#if defined(FEAT_SESSION) && defined(vim_mkdir)
9716 /* When using 'viewdir' may have to create the directory. */
9717 if (using_vdir && !mch_isdir(p_vdir))
Bram Moolenaardf177f62005-02-22 08:39:57 +00009718 vim_mkdir_emsg(p_vdir, 0755);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009719#endif
9720
9721 fd = open_exfile(fname, eap->forceit, WRITEBIN);
9722 if (fd != NULL)
9723 {
9724#ifdef FEAT_SESSION
9725 if (eap->cmdidx == CMD_mkview)
9726 flagp = &vop_flags;
9727 else
9728 flagp = &ssop_flags;
9729#endif
9730
9731#ifdef MKSESSION_NL
9732 /* "unix" in 'sessionoptions': use NL line separator */
9733 if (view_session && (*flagp & SSOP_UNIX))
9734 mksession_nl = TRUE;
9735#endif
9736
9737 /* Write the version command for :mkvimrc */
9738 if (eap->cmdidx == CMD_mkvimrc)
9739 (void)put_line(fd, "version 6.0");
9740
9741#ifdef FEAT_SESSION
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009742 if (eap->cmdidx == CMD_mksession)
9743 {
9744 if (put_line(fd, "let SessionLoad = 1") == FAIL)
9745 failed = TRUE;
9746 }
9747
Bram Moolenaar071d4272004-06-13 20:20:40 +00009748 if (eap->cmdidx != CMD_mkview)
9749#endif
9750 {
9751 /* Write setting 'compatible' first, because it has side effects.
9752 * For that same reason only do it when needed. */
9753 if (p_cp)
9754 (void)put_line(fd, "if !&cp | set cp | endif");
9755 else
9756 (void)put_line(fd, "if &cp | set nocp | endif");
9757 }
9758
9759#ifdef FEAT_SESSION
9760 if (!view_session
9761 || (eap->cmdidx == CMD_mksession
9762 && (*flagp & SSOP_OPTIONS)))
9763#endif
9764 failed |= (makemap(fd, NULL) == FAIL
9765 || makeset(fd, OPT_GLOBAL, FALSE) == FAIL);
9766
9767#ifdef FEAT_SESSION
9768 if (!failed && view_session)
9769 {
9770 if (put_line(fd, "let s:so_save = &so | let s:siso_save = &siso | set so=0 siso=0") == FAIL)
9771 failed = TRUE;
9772 if (eap->cmdidx == CMD_mksession)
9773 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02009774 char_u *dirnow; /* current directory */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009775
Bram Moolenaard9462e32011-04-11 21:35:11 +02009776 dirnow = alloc(MAXPATHL);
9777 if (dirnow == NULL)
9778 failed = TRUE;
9779 else
9780 {
9781 /*
9782 * Change to session file's dir.
9783 */
9784 if (mch_dirname(dirnow, MAXPATHL) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00009785 || mch_chdir((char *)dirnow) != 0)
Bram Moolenaard9462e32011-04-11 21:35:11 +02009786 *dirnow = NUL;
9787 if (*dirnow != NUL && (ssop_flags & SSOP_SESDIR))
9788 {
9789 if (vim_chdirfile(fname) == OK)
9790 shorten_fnames(TRUE);
9791 }
9792 else if (*dirnow != NUL
9793 && (ssop_flags & SSOP_CURDIR) && globaldir != NULL)
9794 {
9795 if (mch_chdir((char *)globaldir) == 0)
9796 shorten_fnames(TRUE);
9797 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009798
Bram Moolenaard9462e32011-04-11 21:35:11 +02009799 failed |= (makeopens(fd, dirnow) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009800
Bram Moolenaard9462e32011-04-11 21:35:11 +02009801 /* restore original dir */
9802 if (*dirnow != NUL && ((ssop_flags & SSOP_SESDIR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009803 || ((ssop_flags & SSOP_CURDIR) && globaldir != NULL)))
Bram Moolenaard9462e32011-04-11 21:35:11 +02009804 {
9805 if (mch_chdir((char *)dirnow) != 0)
9806 EMSG(_(e_prev_dir));
9807 shorten_fnames(TRUE);
9808 }
9809 vim_free(dirnow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009810 }
9811 }
9812 else
9813 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +00009814 failed |= (put_view(fd, curwin, !using_vdir, flagp,
9815 -1) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009816 }
9817 if (put_line(fd, "let &so = s:so_save | let &siso = s:siso_save")
9818 == FAIL)
9819 failed = TRUE;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009820 if (put_line(fd, "doautoall SessionLoadPost") == FAIL)
9821 failed = TRUE;
Bram Moolenaar4770d092006-01-12 23:22:24 +00009822 if (eap->cmdidx == CMD_mksession)
9823 {
9824 if (put_line(fd, "unlet SessionLoad") == FAIL)
9825 failed = TRUE;
9826 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009827 }
9828#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009829 if (put_line(fd, "\" vim: set ft=vim :") == FAIL)
9830 failed = TRUE;
9831
Bram Moolenaar071d4272004-06-13 20:20:40 +00009832 failed |= fclose(fd);
9833
9834 if (failed)
9835 EMSG(_(e_write));
9836#if defined(FEAT_EVAL) && defined(FEAT_SESSION)
9837 else if (eap->cmdidx == CMD_mksession)
9838 {
9839 /* successful session write - set this_session var */
Bram Moolenaard9462e32011-04-11 21:35:11 +02009840 char_u *tbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009841
Bram Moolenaard9462e32011-04-11 21:35:11 +02009842 tbuf = alloc(MAXPATHL);
9843 if (tbuf != NULL)
9844 {
9845 if (vim_FullName(fname, tbuf, MAXPATHL, FALSE) == OK)
9846 set_vim_var_string(VV_THIS_SESSION, tbuf, -1);
9847 vim_free(tbuf);
9848 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009849 }
9850#endif
9851#ifdef MKSESSION_NL
9852 mksession_nl = FALSE;
9853#endif
9854 }
9855
9856#ifdef FEAT_BROWSE
9857theend:
9858 vim_free(browseFile);
9859#endif
9860#ifdef FEAT_SESSION
9861 vim_free(viewFile);
9862#endif
9863}
9864
Bram Moolenaardf177f62005-02-22 08:39:57 +00009865#if ((defined(FEAT_SESSION) || defined(FEAT_EVAL)) && defined(vim_mkdir)) \
9866 || defined(PROTO)
9867 int
9868vim_mkdir_emsg(name, prot)
9869 char_u *name;
Bram Moolenaar78a15312009-05-15 19:33:18 +00009870 int prot UNUSED;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009871{
9872 if (vim_mkdir(name, prot) != 0)
9873 {
9874 EMSG2(_("E739: Cannot create directory: %s"), name);
9875 return FAIL;
9876 }
9877 return OK;
9878}
9879#endif
9880
Bram Moolenaar071d4272004-06-13 20:20:40 +00009881/*
9882 * Open a file for writing for an Ex command, with some checks.
9883 * Return file descriptor, or NULL on failure.
9884 */
9885 FILE *
9886open_exfile(fname, forceit, mode)
9887 char_u *fname;
9888 int forceit;
9889 char *mode; /* "w" for create new file or "a" for append */
9890{
9891 FILE *fd;
9892
9893#ifdef UNIX
9894 /* with Unix it is possible to open a directory */
9895 if (mch_isdir(fname))
9896 {
9897 EMSG2(_(e_isadir2), fname);
9898 return NULL;
9899 }
9900#endif
9901 if (!forceit && *mode != 'a' && vim_fexists(fname))
9902 {
9903 EMSG2(_("E189: \"%s\" exists (add ! to override)"), fname);
9904 return NULL;
9905 }
9906
9907 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
9908 EMSG2(_("E190: Cannot open \"%s\" for writing"), fname);
9909
9910 return fd;
9911}
9912
9913/*
9914 * ":mark" and ":k".
9915 */
9916 static void
9917ex_mark(eap)
9918 exarg_T *eap;
9919{
9920 pos_T pos;
9921
9922 if (*eap->arg == NUL) /* No argument? */
9923 EMSG(_(e_argreq));
9924 else if (eap->arg[1] != NUL) /* more than one character? */
9925 EMSG(_(e_trailing));
9926 else
9927 {
9928 pos = curwin->w_cursor; /* save curwin->w_cursor */
9929 curwin->w_cursor.lnum = eap->line2;
9930 beginline(BL_WHITE | BL_FIX);
9931 if (setmark(*eap->arg) == FAIL) /* set mark */
9932 EMSG(_("E191: Argument must be a letter or forward/backward quote"));
9933 curwin->w_cursor = pos; /* restore curwin->w_cursor */
9934 }
9935}
9936
9937/*
9938 * Update w_topline, w_leftcol and the cursor position.
9939 */
9940 void
9941update_topline_cursor()
9942{
9943 check_cursor(); /* put cursor on valid line */
9944 update_topline();
9945 if (!curwin->w_p_wrap)
9946 validate_cursor();
9947 update_curswant();
9948}
9949
9950#ifdef FEAT_EX_EXTRA
9951/*
9952 * ":normal[!] {commands}": Execute normal mode commands.
9953 */
9954 static void
9955ex_normal(eap)
9956 exarg_T *eap;
9957{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009958 int save_msg_scroll = msg_scroll;
9959 int save_restart_edit = restart_edit;
9960 int save_msg_didout = msg_didout;
9961 int save_State = State;
9962 tasave_T tabuf;
9963 int save_insertmode = p_im;
9964 int save_finish_op = finish_op;
Bram Moolenaar38084112008-07-26 14:05:07 +00009965 int save_opcount = opcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009966#ifdef FEAT_MBYTE
9967 char_u *arg = NULL;
9968 int l;
9969 char_u *p;
9970#endif
9971
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00009972 if (ex_normal_lock > 0)
9973 {
9974 EMSG(_(e_secure));
9975 return;
9976 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009977 if (ex_normal_busy >= p_mmd)
9978 {
9979 EMSG(_("E192: Recursive use of :normal too deep"));
9980 return;
9981 }
9982 ++ex_normal_busy;
9983
9984 msg_scroll = FALSE; /* no msg scrolling in Normal mode */
9985 restart_edit = 0; /* don't go to Insert mode */
9986 p_im = FALSE; /* don't use 'insertmode' */
9987
9988#ifdef FEAT_MBYTE
9989 /*
9990 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
9991 * this for the K_SPECIAL leading byte, otherwise special keys will not
9992 * work.
9993 */
9994 if (has_mbyte)
9995 {
9996 int len = 0;
9997
9998 /* Count the number of characters to be escaped. */
9999 for (p = eap->arg; *p != NUL; ++p)
10000 {
10001# ifdef FEAT_GUI
10002 if (*p == CSI) /* leadbyte CSI */
10003 len += 2;
10004# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010005 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010006 if (*++p == K_SPECIAL /* trailbyte K_SPECIAL or CSI */
10007# ifdef FEAT_GUI
10008 || *p == CSI
10009# endif
10010 )
10011 len += 2;
10012 }
10013 if (len > 0)
10014 {
10015 arg = alloc((unsigned)(STRLEN(eap->arg) + len + 1));
10016 if (arg != NULL)
10017 {
10018 len = 0;
10019 for (p = eap->arg; *p != NUL; ++p)
10020 {
10021 arg[len++] = *p;
10022# ifdef FEAT_GUI
10023 if (*p == CSI)
10024 {
10025 arg[len++] = KS_EXTRA;
10026 arg[len++] = (int)KE_CSI;
10027 }
10028# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010029 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010030 {
10031 arg[len++] = *++p;
10032 if (*p == K_SPECIAL)
10033 {
10034 arg[len++] = KS_SPECIAL;
10035 arg[len++] = KE_FILLER;
10036 }
10037# ifdef FEAT_GUI
10038 else if (*p == CSI)
10039 {
10040 arg[len++] = KS_EXTRA;
10041 arg[len++] = (int)KE_CSI;
10042 }
10043# endif
10044 }
10045 arg[len] = NUL;
10046 }
10047 }
10048 }
10049 }
10050#endif
10051
10052 /*
10053 * Save the current typeahead. This is required to allow using ":normal"
10054 * from an event handler and makes sure we don't hang when the argument
10055 * ends with half a command.
10056 */
10057 save_typeahead(&tabuf);
10058 if (tabuf.typebuf_valid)
10059 {
10060 /*
10061 * Repeat the :normal command for each line in the range. When no
10062 * range given, execute it just once, without positioning the cursor
10063 * first.
10064 */
10065 do
10066 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010067 if (eap->addr_count != 0)
10068 {
10069 curwin->w_cursor.lnum = eap->line1++;
10070 curwin->w_cursor.col = 0;
10071 }
10072
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010073 exec_normal_cmd(
Bram Moolenaar071d4272004-06-13 20:20:40 +000010074#ifdef FEAT_MBYTE
10075 arg != NULL ? arg :
10076#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010077 eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010078 }
10079 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
10080 }
10081
10082 /* Might not return to the main loop when in an event handler. */
10083 update_topline_cursor();
10084
10085 /* Restore the previous typeahead. */
10086 restore_typeahead(&tabuf);
10087
10088 --ex_normal_busy;
10089 msg_scroll = save_msg_scroll;
10090 restart_edit = save_restart_edit;
10091 p_im = save_insertmode;
10092 finish_op = save_finish_op;
Bram Moolenaar38084112008-07-26 14:05:07 +000010093 opcount = save_opcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010094 msg_didout |= save_msg_didout; /* don't reset msg_didout now */
10095
10096 /* Restore the state (needed when called from a function executed for
Bram Moolenaareda73602014-11-05 09:53:23 +010010097 * 'indentexpr'). Update the mouse and cursor, they may have changed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010098 State = save_State;
Bram Moolenaareda73602014-11-05 09:53:23 +010010099#ifdef FEAT_MOUSE
10100 setmouse();
10101#endif
10102#ifdef CURSOR_SHAPE
10103 ui_cursor_shape(); /* may show different cursor shape */
10104#endif
10105
Bram Moolenaar071d4272004-06-13 20:20:40 +000010106#ifdef FEAT_MBYTE
10107 vim_free(arg);
10108#endif
10109}
10110
10111/*
Bram Moolenaar64969662005-12-14 21:59:55 +000010112 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010113 */
10114 static void
10115ex_startinsert(eap)
10116 exarg_T *eap;
10117{
Bram Moolenaarfd371682005-01-14 21:42:54 +000010118 if (eap->forceit)
10119 {
10120 coladvance((colnr_T)MAXCOL);
10121 curwin->w_curswant = MAXCOL;
10122 curwin->w_set_curswant = FALSE;
10123 }
10124
Bram Moolenaara40c5002005-01-09 21:16:21 +000010125 /* Ignore the command when already in Insert mode. Inserting an
10126 * expression register that invokes a function can do this. */
10127 if (State & INSERT)
10128 return;
10129
Bram Moolenaar64969662005-12-14 21:59:55 +000010130 if (eap->cmdidx == CMD_startinsert)
10131 restart_edit = 'a';
10132 else if (eap->cmdidx == CMD_startreplace)
10133 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010134 else
Bram Moolenaar64969662005-12-14 21:59:55 +000010135 restart_edit = 'V';
10136
10137 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010138 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010139 if (eap->cmdidx == CMD_startinsert)
10140 restart_edit = 'i';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010141 curwin->w_curswant = 0; /* avoid MAXCOL */
10142 }
10143}
10144
10145/*
10146 * ":stopinsert"
10147 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010148 static void
10149ex_stopinsert(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010150 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010151{
10152 restart_edit = 0;
10153 stop_insert_mode = TRUE;
10154}
10155#endif
10156
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010157#if defined(FEAT_EX_EXTRA) || defined(FEAT_MENU) || defined(PROTO)
10158/*
10159 * Execute normal mode command "cmd".
10160 * "remap" can be REMAP_NONE or REMAP_YES.
10161 */
10162 void
10163exec_normal_cmd(cmd, remap, silent)
10164 char_u *cmd;
10165 int remap;
10166 int silent;
10167{
10168 oparg_T oa;
10169
10170 /*
10171 * Stuff the argument into the typeahead buffer.
10172 * Execute normal_cmd() until there is no typeahead left.
10173 */
10174 clear_oparg(&oa);
10175 finish_op = FALSE;
10176 ins_typebuf(cmd, remap, 0, TRUE, silent);
10177 while ((!stuff_empty() || (!typebuf_typed() && typebuf.tb_len > 0))
10178 && !got_int)
10179 {
10180 update_topline_cursor();
Bram Moolenaar48ac02c2011-01-17 19:50:06 +010010181 normal_cmd(&oa, TRUE); /* execute a Normal mode cmd */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010182 }
10183}
10184#endif
10185
Bram Moolenaar071d4272004-06-13 20:20:40 +000010186#ifdef FEAT_FIND_ID
10187 static void
10188ex_checkpath(eap)
10189 exarg_T *eap;
10190{
10191 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
10192 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
10193 (linenr_T)1, (linenr_T)MAXLNUM);
10194}
10195
10196#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
10197/*
10198 * ":psearch"
10199 */
10200 static void
10201ex_psearch(eap)
10202 exarg_T *eap;
10203{
10204 g_do_tagpreview = p_pvh;
10205 ex_findpat(eap);
10206 g_do_tagpreview = 0;
10207}
10208#endif
10209
10210 static void
10211ex_findpat(eap)
10212 exarg_T *eap;
10213{
10214 int whole = TRUE;
10215 long n;
10216 char_u *p;
10217 int action;
10218
10219 switch (cmdnames[eap->cmdidx].cmd_name[2])
10220 {
10221 case 'e': /* ":psearch", ":isearch" and ":dsearch" */
10222 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
10223 action = ACTION_GOTO;
10224 else
10225 action = ACTION_SHOW;
10226 break;
10227 case 'i': /* ":ilist" and ":dlist" */
10228 action = ACTION_SHOW_ALL;
10229 break;
10230 case 'u': /* ":ijump" and ":djump" */
10231 action = ACTION_GOTO;
10232 break;
10233 default: /* ":isplit" and ":dsplit" */
10234 action = ACTION_SPLIT;
10235 break;
10236 }
10237
10238 n = 1;
10239 if (vim_isdigit(*eap->arg)) /* get count */
10240 {
10241 n = getdigits(&eap->arg);
10242 eap->arg = skipwhite(eap->arg);
10243 }
10244 if (*eap->arg == '/') /* Match regexp, not just whole words */
10245 {
10246 whole = FALSE;
10247 ++eap->arg;
10248 p = skip_regexp(eap->arg, '/', p_magic, NULL);
10249 if (*p)
10250 {
10251 *p++ = NUL;
10252 p = skipwhite(p);
10253
10254 /* Check for trailing illegal characters */
10255 if (!ends_excmd(*p))
10256 eap->errmsg = e_trailing;
10257 else
10258 eap->nextcmd = check_nextcmd(p);
10259 }
10260 }
10261 if (!eap->skip)
10262 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
10263 whole, !eap->forceit,
10264 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
10265 n, action, eap->line1, eap->line2);
10266}
10267#endif
10268
10269#ifdef FEAT_WINDOWS
10270
10271# ifdef FEAT_QUICKFIX
10272/*
10273 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
10274 */
10275 static void
10276ex_ptag(eap)
10277 exarg_T *eap;
10278{
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +010010279 g_do_tagpreview = p_pvh; /* will be reset to 0 in ex_tag_cmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010280 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
10281}
10282
10283/*
10284 * ":pedit"
10285 */
10286 static void
10287ex_pedit(eap)
10288 exarg_T *eap;
10289{
10290 win_T *curwin_save = curwin;
10291
10292 g_do_tagpreview = p_pvh;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +000010293 prepare_tagpreview(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010294 keep_help_flag = curwin_save->w_buffer->b_help;
10295 do_exedit(eap, NULL);
10296 keep_help_flag = FALSE;
10297 if (curwin != curwin_save && win_valid(curwin_save))
10298 {
10299 /* Return cursor to where we were */
10300 validate_cursor();
10301 redraw_later(VALID);
10302 win_enter(curwin_save, TRUE);
10303 }
10304 g_do_tagpreview = 0;
10305}
10306# endif
10307
10308/*
10309 * ":stag", ":stselect" and ":stjump".
10310 */
10311 static void
10312ex_stag(eap)
10313 exarg_T *eap;
10314{
10315 postponed_split = -1;
10316 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +000010317 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010318 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
10319 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +000010320 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010321}
10322#endif
10323
10324/*
10325 * ":tag", ":tselect", ":tjump", ":tnext", etc.
10326 */
10327 static void
10328ex_tag(eap)
10329 exarg_T *eap;
10330{
10331 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
10332}
10333
10334 static void
10335ex_tag_cmd(eap, name)
10336 exarg_T *eap;
10337 char_u *name;
10338{
10339 int cmd;
10340
10341 switch (name[1])
10342 {
10343 case 'j': cmd = DT_JUMP; /* ":tjump" */
10344 break;
10345 case 's': cmd = DT_SELECT; /* ":tselect" */
10346 break;
10347 case 'p': cmd = DT_PREV; /* ":tprevious" */
10348 break;
10349 case 'N': cmd = DT_PREV; /* ":tNext" */
10350 break;
10351 case 'n': cmd = DT_NEXT; /* ":tnext" */
10352 break;
10353 case 'o': cmd = DT_POP; /* ":pop" */
10354 break;
10355 case 'f': /* ":tfirst" */
10356 case 'r': cmd = DT_FIRST; /* ":trewind" */
10357 break;
10358 case 'l': cmd = DT_LAST; /* ":tlast" */
10359 break;
10360 default: /* ":tag" */
10361#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +000010362 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010363 {
10364 do_cstag(eap);
10365 return;
10366 }
10367#endif
10368 cmd = DT_TAG;
10369 break;
10370 }
10371
Bram Moolenaarb8a7b562006-02-01 21:47:16 +000010372 if (name[0] == 'l')
10373 {
10374#ifndef FEAT_QUICKFIX
10375 ex_ni(eap);
10376 return;
10377#else
10378 cmd = DT_LTAG;
10379#endif
10380 }
10381
Bram Moolenaar071d4272004-06-13 20:20:40 +000010382 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
10383 eap->forceit, TRUE);
10384}
10385
10386/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010387 * Check "str" for starting with a special cmdline variable.
10388 * If found return one of the SPEC_ values and set "*usedlen" to the length of
10389 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
10390 */
10391 int
10392find_cmdline_var(src, usedlen)
10393 char_u *src;
10394 int *usedlen;
10395{
10396 int len;
10397 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +000010398 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010399 "%",
10400#define SPEC_PERC 0
10401 "#",
10402#define SPEC_HASH 1
10403 "<cword>", /* cursor word */
10404#define SPEC_CWORD 2
10405 "<cWORD>", /* cursor WORD */
10406#define SPEC_CCWORD 3
10407 "<cfile>", /* cursor path name */
10408#define SPEC_CFILE 4
10409 "<sfile>", /* ":so" file name */
10410#define SPEC_SFILE 5
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010411 "<slnum>", /* ":so" file line number */
10412#define SPEC_SLNUM 6
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010413#ifdef FEAT_AUTOCMD
10414 "<afile>", /* autocommand file name */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010415# define SPEC_AFILE 7
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010416 "<abuf>", /* autocommand buffer number */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010417# define SPEC_ABUF 8
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010418 "<amatch>", /* autocommand match name */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010419# define SPEC_AMATCH 9
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010420#endif
10421#ifdef FEAT_CLIENTSERVER
10422 "<client>"
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010423# ifdef FEAT_AUTOCMD
10424# define SPEC_CLIENT 10
10425# else
10426# define SPEC_CLIENT 7
10427# endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010428#endif
10429 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010430
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000010431 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010432 {
10433 len = (int)STRLEN(spec_str[i]);
10434 if (STRNCMP(src, spec_str[i], len) == 0)
10435 {
10436 *usedlen = len;
10437 return i;
10438 }
10439 }
10440 return -1;
10441}
10442
10443/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010444 * Evaluate cmdline variables.
10445 *
10446 * change '%' to curbuf->b_ffname
10447 * '#' to curwin->w_altfile
10448 * '<cword>' to word under the cursor
10449 * '<cWORD>' to WORD under the cursor
10450 * '<cfile>' to path name under the cursor
10451 * '<sfile>' to sourced file name
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010452 * '<slnum>' to sourced file line number
Bram Moolenaar071d4272004-06-13 20:20:40 +000010453 * '<afile>' to file name for autocommand
10454 * '<abuf>' to buffer number for autocommand
10455 * '<amatch>' to matching name for autocommand
10456 *
10457 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
10458 * "" for error without a message) and NULL is returned.
10459 * Returns an allocated string if a valid match was found.
10460 * Returns NULL if no match was found. "usedlen" then still contains the
10461 * number of characters to skip.
10462 */
10463 char_u *
Bram Moolenaar63b92542007-03-27 14:57:09 +000010464eval_vars(src, srcstart, usedlen, lnump, errormsg, escaped)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010465 char_u *src; /* pointer into commandline */
Bram Moolenaar63b92542007-03-27 14:57:09 +000010466 char_u *srcstart; /* beginning of valid memory for src */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010467 int *usedlen; /* characters after src that are used */
10468 linenr_T *lnump; /* line number for :e command, or NULL */
10469 char_u **errormsg; /* pointer to error message */
Bram Moolenaar63b92542007-03-27 14:57:09 +000010470 int *escaped; /* return value has escaped white space (can
10471 * be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010472{
10473 int i;
10474 char_u *s;
10475 char_u *result;
10476 char_u *resultbuf = NULL;
10477 int resultlen;
10478 buf_T *buf;
10479 int valid = VALID_HEAD + VALID_PATH; /* assume valid result */
10480 int spec_idx;
10481#ifdef FEAT_MODIFY_FNAME
10482 int skip_mod = FALSE;
10483#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010484 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010485
10486 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +000010487 if (escaped != NULL)
10488 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010489
10490 /*
10491 * Check if there is something to do.
10492 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010493 spec_idx = find_cmdline_var(src, usedlen);
10494 if (spec_idx < 0) /* no match */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010495 {
10496 *usedlen = 1;
10497 return NULL;
10498 }
10499
10500 /*
10501 * Skip when preceded with a backslash "\%" and "\#".
10502 * Note: In "\\%" the % is also not recognized!
10503 */
10504 if (src > srcstart && src[-1] == '\\')
10505 {
10506 *usedlen = 0;
Bram Moolenaara7241f52008-06-24 20:39:31 +000010507 STRMOVE(src - 1, src); /* remove backslash */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010508 return NULL;
10509 }
10510
10511 /*
10512 * word or WORD under cursor
10513 */
10514 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD)
10515 {
10516 resultlen = find_ident_under_cursor(&result, spec_idx == SPEC_CWORD ?
10517 (FIND_IDENT|FIND_STRING) : FIND_STRING);
10518 if (resultlen == 0)
10519 {
10520 *errormsg = (char_u *)"";
10521 return NULL;
10522 }
10523 }
10524
10525 /*
10526 * '#': Alternate file name
10527 * '%': Current file name
10528 * File name under the cursor
10529 * File name for autocommand
10530 * and following modifiers
10531 */
10532 else
10533 {
10534 switch (spec_idx)
10535 {
10536 case SPEC_PERC: /* '%': current file */
10537 if (curbuf->b_fname == NULL)
10538 {
10539 result = (char_u *)"";
10540 valid = 0; /* Must have ":p:h" to be valid */
10541 }
10542 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010543 result = curbuf->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010544 break;
10545
10546 case SPEC_HASH: /* '#' or "#99": alternate file */
10547 if (src[1] == '#') /* "##": the argument list */
10548 {
10549 result = arg_all();
10550 resultbuf = result;
10551 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +000010552 if (escaped != NULL)
10553 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010554#ifdef FEAT_MODIFY_FNAME
10555 skip_mod = TRUE;
10556#endif
10557 break;
10558 }
10559 s = src + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +000010560 if (*s == '<') /* "#<99" uses v:oldfiles */
10561 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010562 i = (int)getdigits(&s);
10563 *usedlen = (int)(s - src); /* length of what we expand */
10564
Bram Moolenaard812df62008-11-09 12:46:09 +000010565 if (src[1] == '<')
Bram Moolenaar071d4272004-06-13 20:20:40 +000010566 {
Bram Moolenaard812df62008-11-09 12:46:09 +000010567 if (*usedlen < 2)
10568 {
10569 /* Should we give an error message for #<text? */
10570 *usedlen = 1;
10571 return NULL;
10572 }
10573#ifdef FEAT_EVAL
10574 result = list_find_str(get_vim_var_list(VV_OLDFILES),
10575 (long)i);
10576 if (result == NULL)
10577 {
10578 *errormsg = (char_u *)"";
10579 return NULL;
10580 }
10581#else
10582 *errormsg = (char_u *)_("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +000010583 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +000010584#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010585 }
10586 else
Bram Moolenaard812df62008-11-09 12:46:09 +000010587 {
10588 buf = buflist_findnr(i);
10589 if (buf == NULL)
10590 {
10591 *errormsg = (char_u *)_("E194: No alternate file name to substitute for '#'");
10592 return NULL;
10593 }
10594 if (lnump != NULL)
10595 *lnump = ECMD_LAST;
10596 if (buf->b_fname == NULL)
10597 {
10598 result = (char_u *)"";
10599 valid = 0; /* Must have ":p:h" to be valid */
10600 }
10601 else
10602 result = buf->b_fname;
10603 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010604 break;
10605
10606#ifdef FEAT_SEARCHPATH
10607 case SPEC_CFILE: /* file name under cursor */
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010608 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010609 if (result == NULL)
10610 {
10611 *errormsg = (char_u *)"";
10612 return NULL;
10613 }
10614 resultbuf = result; /* remember allocated string */
10615 break;
10616#endif
10617
10618#ifdef FEAT_AUTOCMD
10619 case SPEC_AFILE: /* file name for autocommand */
10620 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +000010621 if (result != NULL && !autocmd_fname_full)
10622 {
10623 /* Still need to turn the fname into a full path. It is
10624 * postponed to avoid a delay when <afile> is not used. */
10625 autocmd_fname_full = TRUE;
10626 result = FullName_save(autocmd_fname, FALSE);
10627 vim_free(autocmd_fname);
10628 autocmd_fname = result;
10629 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010630 if (result == NULL)
10631 {
10632 *errormsg = (char_u *)_("E495: no autocommand file name to substitute for \"<afile>\"");
10633 return NULL;
10634 }
Bram Moolenaara0174af2008-01-02 20:08:25 +000010635 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010636 break;
10637
10638 case SPEC_ABUF: /* buffer number for autocommand */
10639 if (autocmd_bufnr <= 0)
10640 {
10641 *errormsg = (char_u *)_("E496: no autocommand buffer number to substitute for \"<abuf>\"");
10642 return NULL;
10643 }
10644 sprintf((char *)strbuf, "%d", autocmd_bufnr);
10645 result = strbuf;
10646 break;
10647
10648 case SPEC_AMATCH: /* match name for autocommand */
10649 result = autocmd_match;
10650 if (result == NULL)
10651 {
10652 *errormsg = (char_u *)_("E497: no autocommand match name to substitute for \"<amatch>\"");
10653 return NULL;
10654 }
10655 break;
10656
10657#endif
10658 case SPEC_SFILE: /* file name for ":so" command */
10659 result = sourcing_name;
10660 if (result == NULL)
10661 {
10662 *errormsg = (char_u *)_("E498: no :source file name to substitute for \"<sfile>\"");
10663 return NULL;
10664 }
10665 break;
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010666 case SPEC_SLNUM: /* line in file for ":so" command */
10667 if (sourcing_name == NULL || sourcing_lnum == 0)
10668 {
10669 *errormsg = (char_u *)_("E842: no line number to use for \"<slnum>\"");
10670 return NULL;
10671 }
10672 sprintf((char *)strbuf, "%ld", (long)sourcing_lnum);
10673 result = strbuf;
10674 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010675#if defined(FEAT_CLIENTSERVER)
10676 case SPEC_CLIENT: /* Source of last submitted input */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010677 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
10678 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010679 result = strbuf;
10680 break;
10681#endif
10682 }
10683
10684 resultlen = (int)STRLEN(result); /* length of new string */
10685 if (src[*usedlen] == '<') /* remove the file name extension */
10686 {
10687 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010688 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010689 resultlen = (int)(s - result);
10690 }
10691#ifdef FEAT_MODIFY_FNAME
10692 else if (!skip_mod)
10693 {
10694 valid |= modify_fname(src, usedlen, &result, &resultbuf,
10695 &resultlen);
10696 if (result == NULL)
10697 {
10698 *errormsg = (char_u *)"";
10699 return NULL;
10700 }
10701 }
10702#endif
10703 }
10704
10705 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
10706 {
10707 if (valid != VALID_HEAD + VALID_PATH)
10708 /* xgettext:no-c-format */
10709 *errormsg = (char_u *)_("E499: Empty file name for '%' or '#', only works with \":p:h\"");
10710 else
10711 *errormsg = (char_u *)_("E500: Evaluates to an empty string");
10712 result = NULL;
10713 }
10714 else
10715 result = vim_strnsave(result, resultlen);
10716 vim_free(resultbuf);
10717 return result;
10718}
10719
10720/*
10721 * Concatenate all files in the argument list, separated by spaces, and return
10722 * it in one allocated string.
10723 * Spaces and backslashes in the file names are escaped with a backslash.
10724 * Returns NULL when out of memory.
10725 */
10726 static char_u *
10727arg_all()
10728{
10729 int len;
10730 int idx;
10731 char_u *retval = NULL;
10732 char_u *p;
10733
10734 /*
10735 * Do this loop two times:
10736 * first time: compute the total length
10737 * second time: concatenate the names
10738 */
10739 for (;;)
10740 {
10741 len = 0;
10742 for (idx = 0; idx < ARGCOUNT; ++idx)
10743 {
10744 p = alist_name(&ARGLIST[idx]);
10745 if (p != NULL)
10746 {
10747 if (len > 0)
10748 {
10749 /* insert a space in between names */
10750 if (retval != NULL)
10751 retval[len] = ' ';
10752 ++len;
10753 }
10754 for ( ; *p != NUL; ++p)
10755 {
Bram Moolenaar6e8d3b02015-06-09 21:33:31 +020010756 if (*p == ' '
10757#ifndef BACKSLASH_IN_FILENAME
10758 || *p == '\\'
10759#endif
10760 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000010761 {
10762 /* insert a backslash */
10763 if (retval != NULL)
10764 retval[len] = '\\';
10765 ++len;
10766 }
10767 if (retval != NULL)
10768 retval[len] = *p;
10769 ++len;
10770 }
10771 }
10772 }
10773
10774 /* second time: break here */
10775 if (retval != NULL)
10776 {
10777 retval[len] = NUL;
10778 break;
10779 }
10780
10781 /* allocate memory */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000010782 retval = alloc((unsigned)len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010783 if (retval == NULL)
10784 break;
10785 }
10786
10787 return retval;
10788}
10789
10790#if defined(FEAT_AUTOCMD) || defined(PROTO)
10791/*
10792 * Expand the <sfile> string in "arg".
10793 *
10794 * Returns an allocated string, or NULL for any error.
10795 */
10796 char_u *
10797expand_sfile(arg)
10798 char_u *arg;
10799{
10800 char_u *errormsg;
10801 int len;
10802 char_u *result;
10803 char_u *newres;
10804 char_u *repl;
10805 int srclen;
10806 char_u *p;
10807
10808 result = vim_strsave(arg);
10809 if (result == NULL)
10810 return NULL;
10811
10812 for (p = result; *p; )
10813 {
10814 if (STRNCMP(p, "<sfile>", 7) != 0)
10815 ++p;
10816 else
10817 {
10818 /* replace "<sfile>" with the sourced file name, and do ":" stuff */
Bram Moolenaar63b92542007-03-27 14:57:09 +000010819 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010820 if (errormsg != NULL)
10821 {
10822 if (*errormsg)
10823 emsg(errormsg);
10824 vim_free(result);
10825 return NULL;
10826 }
10827 if (repl == NULL) /* no match (cannot happen) */
10828 {
10829 p += srclen;
10830 continue;
10831 }
10832 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
10833 newres = alloc(len);
10834 if (newres == NULL)
10835 {
10836 vim_free(repl);
10837 vim_free(result);
10838 return NULL;
10839 }
10840 mch_memmove(newres, result, (size_t)(p - result));
10841 STRCPY(newres + (p - result), repl);
10842 len = (int)STRLEN(newres);
10843 STRCAT(newres, p + srclen);
10844 vim_free(repl);
10845 vim_free(result);
10846 result = newres;
10847 p = newres + len; /* continue after the match */
10848 }
10849 }
10850
10851 return result;
10852}
10853#endif
10854
10855#ifdef FEAT_SESSION
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010856static int ses_winsizes __ARGS((FILE *fd, int restore_size,
10857 win_T *tab_firstwin));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010858static int ses_win_rec __ARGS((FILE *fd, frame_T *fr));
10859static frame_T *ses_skipframe __ARGS((frame_T *fr));
10860static int ses_do_frame __ARGS((frame_T *fr));
10861static int ses_do_win __ARGS((win_T *wp));
10862static int ses_arglist __ARGS((FILE *fd, char *cmd, garray_T *gap, int fullname, unsigned *flagp));
10863static int ses_put_fname __ARGS((FILE *fd, char_u *name, unsigned *flagp));
10864static int ses_fname __ARGS((FILE *fd, buf_T *buf, unsigned *flagp));
10865
10866/*
10867 * Write openfile commands for the current buffers to an .exrc file.
10868 * Return FAIL on error, OK otherwise.
10869 */
10870 static int
10871makeopens(fd, dirnow)
10872 FILE *fd;
10873 char_u *dirnow; /* Current directory name */
10874{
10875 buf_T *buf;
10876 int only_save_windows = TRUE;
10877 int nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010878 int restore_size = TRUE;
10879 win_T *wp;
10880 char_u *sname;
10881 win_T *edited_win = NULL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010882 int tabnr;
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020010883 int restore_stal = FALSE;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010884 win_T *tab_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010885 frame_T *tab_topframe;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010886 int cur_arg_idx = 0;
Bram Moolenaar383c6f52008-01-04 15:01:07 +000010887 int next_arg_idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010888
10889 if (ssop_flags & SSOP_BUFFERS)
10890 only_save_windows = FALSE; /* Save ALL buffers */
10891
10892 /*
10893 * Begin by setting the this_session variable, and then other
10894 * sessionable variables.
10895 */
10896#ifdef FEAT_EVAL
10897 if (put_line(fd, "let v:this_session=expand(\"<sfile>:p\")") == FAIL)
10898 return FAIL;
10899 if (ssop_flags & SSOP_GLOBALS)
10900 if (store_session_globals(fd) == FAIL)
10901 return FAIL;
10902#endif
10903
10904 /*
10905 * Close all windows but one.
10906 */
10907 if (put_line(fd, "silent only") == FAIL)
10908 return FAIL;
10909
10910 /*
10911 * Now a :cd command to the session directory or the current directory
10912 */
10913 if (ssop_flags & SSOP_SESDIR)
10914 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010915 if (put_line(fd, "exe \"cd \" . escape(expand(\"<sfile>:p:h\"), ' ')")
10916 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010917 return FAIL;
10918 }
10919 else if (ssop_flags & SSOP_CURDIR)
10920 {
10921 sname = home_replace_save(NULL, globaldir != NULL ? globaldir : dirnow);
10922 if (sname == NULL
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010923 || fputs("cd ", fd) < 0
10924 || ses_put_fname(fd, sname, &ssop_flags) == FAIL
10925 || put_eol(fd) == FAIL)
10926 {
10927 vim_free(sname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010928 return FAIL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010929 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010930 vim_free(sname);
10931 }
10932
10933 /*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010934 * If there is an empty, unnamed buffer we will wipe it out later.
10935 * Remember the buffer number.
10936 */
10937 if (put_line(fd, "if expand('%') == '' && !&modified && line('$') <= 1 && getline(1) == ''") == FAIL)
10938 return FAIL;
10939 if (put_line(fd, " let s:wipebuf = bufnr('%')") == FAIL)
10940 return FAIL;
10941 if (put_line(fd, "endif") == FAIL)
10942 return FAIL;
10943
10944 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010945 * Now save the current files, current buffer first.
10946 */
10947 if (put_line(fd, "set shortmess=aoO") == FAIL)
10948 return FAIL;
10949
10950 /* Now put the other buffers into the buffer list */
10951 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
10952 {
10953 if (!(only_save_windows && buf->b_nwindows == 0)
10954 && !(buf->b_help && !(ssop_flags & SSOP_HELP))
10955 && buf->b_fname != NULL
10956 && buf->b_p_bl)
10957 {
10958 if (fprintf(fd, "badd +%ld ", buf->b_wininfo == NULL ? 1L
10959 : buf->b_wininfo->wi_fpos.lnum) < 0
10960 || ses_fname(fd, buf, &ssop_flags) == FAIL)
10961 return FAIL;
10962 }
10963 }
10964
10965 /* the global argument list */
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010010966 if (ses_arglist(fd, "argglobal", &global_alist.al_ga,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010967 !(ssop_flags & SSOP_CURDIR), &ssop_flags) == FAIL)
10968 return FAIL;
10969
10970 if (ssop_flags & SSOP_RESIZE)
10971 {
10972 /* Note: after the restore we still check it worked!*/
10973 if (fprintf(fd, "set lines=%ld columns=%ld" , Rows, Columns) < 0
10974 || put_eol(fd) == FAIL)
10975 return FAIL;
10976 }
10977
10978#ifdef FEAT_GUI
10979 if (gui.in_use && (ssop_flags & SSOP_WINPOS))
10980 {
10981 int x, y;
10982
10983 if (gui_mch_get_winpos(&x, &y) == OK)
10984 {
10985 /* Note: after the restore we still check it worked!*/
10986 if (fprintf(fd, "winpos %d %d", x, y) < 0 || put_eol(fd) == FAIL)
10987 return FAIL;
10988 }
10989 }
10990#endif
10991
10992 /*
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020010993 * When there are two or more tabpages and 'showtabline' is 1 the tabline
10994 * will be displayed when creating the next tab. That resizes the windows
10995 * in the first tab, which may cause problems. Set 'showtabline' to 2
10996 * temporarily to avoid that.
10997 */
10998 if (p_stal == 1 && first_tabpage->tp_next != NULL)
10999 {
11000 if (put_line(fd, "set stal=2") == FAIL)
11001 return FAIL;
11002 restore_stal = TRUE;
11003 }
11004
11005 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000011006 * May repeat putting Windows for each tab, when "tabpages" is in
11007 * 'sessionoptions'.
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011008 * Don't use goto_tabpage(), it may change directory and trigger
11009 * autocommands.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011010 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011011 tab_firstwin = firstwin; /* first window in tab page "tabnr" */
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011012 tab_topframe = topframe;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011013 for (tabnr = 1; ; ++tabnr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011014 {
Bram Moolenaar695baee2015-04-13 12:39:22 +020011015 int need_tabnew = FALSE;
11016 int cnr = 1;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011017
Bram Moolenaar18144c82006-04-12 21:52:12 +000011018 if ((ssop_flags & SSOP_TABPAGES))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011019 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011020 tabpage_T *tp = find_tabpage(tabnr);
11021
11022 if (tp == NULL)
11023 break; /* done all tab pages */
11024 if (tp == curtab)
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011025 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011026 tab_firstwin = firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011027 tab_topframe = topframe;
11028 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011029 else
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011030 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011031 tab_firstwin = tp->tp_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011032 tab_topframe = tp->tp_topframe;
11033 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011034 if (tabnr > 1)
11035 need_tabnew = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011036 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011037
Bram Moolenaar18144c82006-04-12 21:52:12 +000011038 /*
11039 * Before creating the window layout, try loading one file. If this
11040 * is aborted we don't end up with a number of useless windows.
11041 * This may have side effects! (e.g., compressed or network file).
11042 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011043 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011044 {
11045 if (ses_do_win(wp)
11046 && wp->w_buffer->b_ffname != NULL
11047 && !wp->w_buffer->b_help
11048#ifdef FEAT_QUICKFIX
11049 && !bt_nofile(wp->w_buffer)
11050#endif
11051 )
11052 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011053 if (fputs(need_tabnew ? "tabedit " : "edit ", fd) < 0
Bram Moolenaar18144c82006-04-12 21:52:12 +000011054 || ses_fname(fd, wp->w_buffer, &ssop_flags) == FAIL)
11055 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011056 need_tabnew = FALSE;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011057 if (!wp->w_arg_idx_invalid)
11058 edited_win = wp;
11059 break;
11060 }
11061 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011062
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011063 /* If no file got edited create an empty tab page. */
11064 if (need_tabnew && put_line(fd, "tabnew") == FAIL)
11065 return FAIL;
11066
Bram Moolenaar18144c82006-04-12 21:52:12 +000011067 /*
11068 * Save current window layout.
11069 */
11070 if (put_line(fd, "set splitbelow splitright") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011071 return FAIL;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011072 if (ses_win_rec(fd, tab_topframe) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011073 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011074 if (!p_sb && put_line(fd, "set nosplitbelow") == FAIL)
11075 return FAIL;
11076 if (!p_spr && put_line(fd, "set nosplitright") == FAIL)
11077 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011078
Bram Moolenaar18144c82006-04-12 21:52:12 +000011079 /*
11080 * Check if window sizes can be restored (no windows omitted).
11081 * Remember the window number of the current window after restoring.
11082 */
11083 nr = 0;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011084 for (wp = tab_firstwin; wp != NULL; wp = W_NEXT(wp))
Bram Moolenaar18144c82006-04-12 21:52:12 +000011085 {
11086 if (ses_do_win(wp))
11087 ++nr;
11088 else
11089 restore_size = FALSE;
11090 if (curwin == wp)
11091 cnr = nr;
11092 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011093
Bram Moolenaar18144c82006-04-12 21:52:12 +000011094 /* Go to the first window. */
11095 if (put_line(fd, "wincmd t") == FAIL)
11096 return FAIL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011097
Bram Moolenaar18144c82006-04-12 21:52:12 +000011098 /*
11099 * If more than one window, see if sizes can be restored.
11100 * First set 'winheight' and 'winwidth' to 1 to avoid the windows being
11101 * resized when moving between windows.
11102 * Do this before restoring the view, so that the topline and the
11103 * cursor can be set. This is done again below.
11104 */
11105 if (put_line(fd, "set winheight=1 winwidth=1") == FAIL)
11106 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011107 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011108 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011109
Bram Moolenaar18144c82006-04-12 21:52:12 +000011110 /*
11111 * Restore the view of the window (options, file, cursor, etc.).
11112 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011113 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011114 {
11115 if (!ses_do_win(wp))
11116 continue;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011117 if (put_view(fd, wp, wp != edited_win, &ssop_flags,
11118 cur_arg_idx) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011119 return FAIL;
11120 if (nr > 1 && put_line(fd, "wincmd w") == FAIL)
11121 return FAIL;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011122 next_arg_idx = wp->w_arg_idx;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011123 }
11124
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011125 /* The argument index in the first tab page is zero, need to set it in
11126 * each window. For further tab pages it's the window where we do
11127 * "tabedit". */
11128 cur_arg_idx = next_arg_idx;
11129
Bram Moolenaar18144c82006-04-12 21:52:12 +000011130 /*
11131 * Restore cursor to the current window if it's not the first one.
11132 */
11133 if (cnr > 1 && (fprintf(fd, "%dwincmd w", cnr) < 0
11134 || put_eol(fd) == FAIL))
11135 return FAIL;
11136
11137 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000011138 * Restore window sizes again after jumping around in windows, because
11139 * the current window has a minimum size while others may not.
11140 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011141 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011142 return FAIL;
11143
Bram Moolenaar18144c82006-04-12 21:52:12 +000011144 /* Don't continue in another tab page when doing only the current one
11145 * or when at the last tab page. */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011146 if (!(ssop_flags & SSOP_TABPAGES))
Bram Moolenaar18144c82006-04-12 21:52:12 +000011147 break;
11148 }
11149
11150 if (ssop_flags & SSOP_TABPAGES)
11151 {
Bram Moolenaar18144c82006-04-12 21:52:12 +000011152 if (fprintf(fd, "tabnext %d", tabpage_index(curtab)) < 0
11153 || put_eol(fd) == FAIL)
11154 return FAIL;
11155 }
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020011156 if (restore_stal && put_line(fd, "set stal=1") == FAIL)
11157 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011158
Bram Moolenaar9c102382006-05-03 21:26:49 +000011159 /*
11160 * Wipe out an empty unnamed buffer we started in.
11161 */
11162 if (put_line(fd, "if exists('s:wipebuf')") == FAIL)
11163 return FAIL;
Bram Moolenaarf3a67882006-05-05 21:09:41 +000011164 if (put_line(fd, " silent exe 'bwipe ' . s:wipebuf") == FAIL)
Bram Moolenaar9c102382006-05-03 21:26:49 +000011165 return FAIL;
11166 if (put_line(fd, "endif") == FAIL)
11167 return FAIL;
11168 if (put_line(fd, "unlet! s:wipebuf") == FAIL)
11169 return FAIL;
11170
11171 /* Re-apply 'winheight', 'winwidth' and 'shortmess'. */
11172 if (fprintf(fd, "set winheight=%ld winwidth=%ld shortmess=%s",
11173 p_wh, p_wiw, p_shm) < 0 || put_eol(fd) == FAIL)
11174 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011175
11176 /*
11177 * Lastly, execute the x.vim file if it exists.
11178 */
11179 if (put_line(fd, "let s:sx = expand(\"<sfile>:p:r\").\"x.vim\"") == FAIL
11180 || put_line(fd, "if file_readable(s:sx)") == FAIL
Bram Moolenaar42ba1262008-12-09 10:18:03 +000011181 || put_line(fd, " exe \"source \" . fnameescape(s:sx)") == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000011182 || put_line(fd, "endif") == FAIL)
11183 return FAIL;
11184
11185 return OK;
11186}
11187
11188 static int
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011189ses_winsizes(fd, restore_size, tab_firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011190 FILE *fd;
11191 int restore_size;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011192 win_T *tab_firstwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011193{
11194 int n = 0;
11195 win_T *wp;
11196
11197 if (restore_size && (ssop_flags & SSOP_WINSIZE))
11198 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011199 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011200 {
11201 if (!ses_do_win(wp))
11202 continue;
11203 ++n;
11204
11205 /* restore height when not full height */
11206 if (wp->w_height + wp->w_status_height < topframe->fr_height
11207 && (fprintf(fd,
11208 "exe '%dresize ' . ((&lines * %ld + %ld) / %ld)",
11209 n, (long)wp->w_height, Rows / 2, Rows) < 0
11210 || put_eol(fd) == FAIL))
11211 return FAIL;
11212
11213 /* restore width when not full width */
11214 if (wp->w_width < Columns && (fprintf(fd,
11215 "exe 'vert %dresize ' . ((&columns * %ld + %ld) / %ld)",
11216 n, (long)wp->w_width, Columns / 2, Columns) < 0
11217 || put_eol(fd) == FAIL))
11218 return FAIL;
11219 }
11220 }
11221 else
11222 {
11223 /* Just equalise window sizes */
11224 if (put_line(fd, "wincmd =") == FAIL)
11225 return FAIL;
11226 }
11227 return OK;
11228}
11229
11230/*
11231 * Write commands to "fd" to recursively create windows for frame "fr",
11232 * horizontally and vertically split.
11233 * After the commands the last window in the frame is the current window.
11234 * Returns FAIL when writing the commands to "fd" fails.
11235 */
11236 static int
11237ses_win_rec(fd, fr)
11238 FILE *fd;
11239 frame_T *fr;
11240{
11241 frame_T *frc;
11242 int count = 0;
11243
11244 if (fr->fr_layout != FR_LEAF)
11245 {
11246 /* Find first frame that's not skipped and then create a window for
11247 * each following one (first frame is already there). */
11248 frc = ses_skipframe(fr->fr_child);
11249 if (frc != NULL)
11250 while ((frc = ses_skipframe(frc->fr_next)) != NULL)
11251 {
11252 /* Make window as big as possible so that we have lots of room
11253 * to split. */
11254 if (put_line(fd, "wincmd _ | wincmd |") == FAIL
11255 || put_line(fd, fr->fr_layout == FR_COL
11256 ? "split" : "vsplit") == FAIL)
11257 return FAIL;
11258 ++count;
11259 }
11260
11261 /* Go back to the first window. */
11262 if (count > 0 && (fprintf(fd, fr->fr_layout == FR_COL
11263 ? "%dwincmd k" : "%dwincmd h", count) < 0
11264 || put_eol(fd) == FAIL))
11265 return FAIL;
11266
11267 /* Recursively create frames/windows in each window of this column or
11268 * row. */
11269 frc = ses_skipframe(fr->fr_child);
11270 while (frc != NULL)
11271 {
11272 ses_win_rec(fd, frc);
11273 frc = ses_skipframe(frc->fr_next);
11274 /* Go to next window. */
11275 if (frc != NULL && put_line(fd, "wincmd w") == FAIL)
11276 return FAIL;
11277 }
11278 }
11279 return OK;
11280}
11281
11282/*
11283 * Skip frames that don't contain windows we want to save in the Session.
11284 * Returns NULL when there none.
11285 */
11286 static frame_T *
11287ses_skipframe(fr)
11288 frame_T *fr;
11289{
11290 frame_T *frc;
11291
11292 for (frc = fr; frc != NULL; frc = frc->fr_next)
11293 if (ses_do_frame(frc))
11294 break;
11295 return frc;
11296}
11297
11298/*
11299 * Return TRUE if frame "fr" has a window somewhere that we want to save in
11300 * the Session.
11301 */
11302 static int
11303ses_do_frame(fr)
11304 frame_T *fr;
11305{
11306 frame_T *frc;
11307
11308 if (fr->fr_layout == FR_LEAF)
11309 return ses_do_win(fr->fr_win);
11310 for (frc = fr->fr_child; frc != NULL; frc = frc->fr_next)
11311 if (ses_do_frame(frc))
11312 return TRUE;
11313 return FALSE;
11314}
11315
11316/*
11317 * Return non-zero if window "wp" is to be stored in the Session.
11318 */
11319 static int
11320ses_do_win(wp)
11321 win_T *wp;
11322{
11323 if (wp->w_buffer->b_fname == NULL
11324#ifdef FEAT_QUICKFIX
11325 /* When 'buftype' is "nofile" can't restore the window contents. */
11326 || bt_nofile(wp->w_buffer)
11327#endif
11328 )
11329 return (ssop_flags & SSOP_BLANK);
11330 if (wp->w_buffer->b_help)
11331 return (ssop_flags & SSOP_HELP);
11332 return TRUE;
11333}
11334
11335/*
11336 * Write commands to "fd" to restore the view of a window.
11337 * Caller must make sure 'scrolloff' is zero.
11338 */
11339 static int
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011340put_view(fd, wp, add_edit, flagp, current_arg_idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011341 FILE *fd;
11342 win_T *wp;
11343 int add_edit; /* add ":edit" command to view */
11344 unsigned *flagp; /* vop_flags or ssop_flags */
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011345 int current_arg_idx; /* current argument index of the window, use
11346 * -1 if unknown */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011347{
11348 win_T *save_curwin;
11349 int f;
11350 int do_cursor;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011351 int did_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011352
11353 /* Always restore cursor position for ":mksession". For ":mkview" only
11354 * when 'viewoptions' contains "cursor". */
11355 do_cursor = (flagp == &ssop_flags || *flagp & SSOP_CURSOR);
11356
11357 /*
11358 * Local argument list.
11359 */
11360 if (wp->w_alist == &global_alist)
11361 {
11362 if (put_line(fd, "argglobal") == FAIL)
11363 return FAIL;
11364 }
11365 else
11366 {
11367 if (ses_arglist(fd, "arglocal", &wp->w_alist->al_ga,
11368 flagp == &vop_flags
11369 || !(*flagp & SSOP_CURDIR)
11370 || wp->w_localdir != NULL, flagp) == FAIL)
11371 return FAIL;
11372 }
11373
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011374 /* Only when part of a session: restore the argument index. Some
11375 * arguments may have been deleted, check if the index is valid. */
Bram Moolenaar51f53df2010-06-26 05:25:54 +020011376 if (wp->w_arg_idx != current_arg_idx && wp->w_arg_idx < WARGCOUNT(wp)
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011377 && flagp == &ssop_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011378 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011379 if (fprintf(fd, "%ldargu", (long)wp->w_arg_idx + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011380 || put_eol(fd) == FAIL)
11381 return FAIL;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011382 did_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011383 }
11384
11385 /* Edit the file. Skip this when ":next" already did it. */
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011386 if (add_edit && (!did_next || wp->w_arg_idx_invalid))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011387 {
11388 /*
11389 * Load the file.
11390 */
11391 if (wp->w_buffer->b_ffname != NULL
11392#ifdef FEAT_QUICKFIX
11393 && !bt_nofile(wp->w_buffer)
11394#endif
11395 )
11396 {
11397 /*
11398 * Editing a file in this buffer: use ":edit file".
11399 * This may have side effects! (e.g., compressed or network file).
11400 */
11401 if (fputs("edit ", fd) < 0
11402 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
11403 return FAIL;
11404 }
11405 else
11406 {
11407 /* No file in this buffer, just make it empty. */
11408 if (put_line(fd, "enew") == FAIL)
11409 return FAIL;
11410#ifdef FEAT_QUICKFIX
11411 if (wp->w_buffer->b_ffname != NULL)
11412 {
11413 /* The buffer does have a name, but it's not a file name. */
11414 if (fputs("file ", fd) < 0
11415 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
11416 return FAIL;
11417 }
11418#endif
11419 do_cursor = FALSE;
11420 }
11421 }
11422
11423 /*
11424 * Local mappings and abbreviations.
11425 */
11426 if ((*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
11427 && makemap(fd, wp->w_buffer) == FAIL)
11428 return FAIL;
11429
11430 /*
11431 * Local options. Need to go to the window temporarily.
11432 * Store only local values when using ":mkview" and when ":mksession" is
11433 * used and 'sessionoptions' doesn't include "options".
11434 * Some folding options are always stored when "folds" is included,
11435 * otherwise the folds would not be restored correctly.
11436 */
11437 save_curwin = curwin;
11438 curwin = wp;
11439 curbuf = curwin->w_buffer;
11440 if (*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
11441 f = makeset(fd, OPT_LOCAL,
11442 flagp == &vop_flags || !(*flagp & SSOP_OPTIONS));
11443#ifdef FEAT_FOLDING
11444 else if (*flagp & SSOP_FOLDS)
11445 f = makefoldset(fd);
11446#endif
11447 else
11448 f = OK;
11449 curwin = save_curwin;
11450 curbuf = curwin->w_buffer;
11451 if (f == FAIL)
11452 return FAIL;
11453
11454#ifdef FEAT_FOLDING
11455 /*
11456 * Save Folds when 'buftype' is empty and for help files.
11457 */
11458 if ((*flagp & SSOP_FOLDS)
11459 && wp->w_buffer->b_ffname != NULL
Bram Moolenaarb1b715d2006-01-21 22:09:43 +000011460# ifdef FEAT_QUICKFIX
11461 && (*wp->w_buffer->b_p_bt == NUL || wp->w_buffer->b_help)
11462# endif
11463 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000011464 {
11465 if (put_folds(fd, wp) == FAIL)
11466 return FAIL;
11467 }
11468#endif
11469
11470 /*
11471 * Set the cursor after creating folds, since that moves the cursor.
11472 */
11473 if (do_cursor)
11474 {
11475
11476 /* Restore the cursor line in the file and relatively in the
11477 * window. Don't use "G", it changes the jumplist. */
11478 if (fprintf(fd, "let s:l = %ld - ((%ld * winheight(0) + %ld) / %ld)",
11479 (long)wp->w_cursor.lnum,
11480 (long)(wp->w_cursor.lnum - wp->w_topline),
11481 (long)wp->w_height / 2, (long)wp->w_height) < 0
11482 || put_eol(fd) == FAIL
11483 || put_line(fd, "if s:l < 1 | let s:l = 1 | endif") == FAIL
11484 || put_line(fd, "exe s:l") == FAIL
11485 || put_line(fd, "normal! zt") == FAIL
11486 || fprintf(fd, "%ld", (long)wp->w_cursor.lnum) < 0
11487 || put_eol(fd) == FAIL)
11488 return FAIL;
11489 /* Restore the cursor column and left offset when not wrapping. */
11490 if (wp->w_cursor.col == 0)
11491 {
11492 if (put_line(fd, "normal! 0") == FAIL)
11493 return FAIL;
11494 }
11495 else
11496 {
11497 if (!wp->w_p_wrap && wp->w_leftcol > 0 && wp->w_width > 0)
11498 {
11499 if (fprintf(fd,
11500 "let s:c = %ld - ((%ld * winwidth(0) + %ld) / %ld)",
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011501 (long)wp->w_virtcol + 1,
11502 (long)(wp->w_virtcol - wp->w_leftcol),
Bram Moolenaar071d4272004-06-13 20:20:40 +000011503 (long)wp->w_width / 2, (long)wp->w_width) < 0
11504 || put_eol(fd) == FAIL
11505 || put_line(fd, "if s:c > 0") == FAIL
11506 || fprintf(fd,
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011507 " exe 'normal! ' . s:c . '|zs' . %ld . '|'",
11508 (long)wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011509 || put_eol(fd) == FAIL
11510 || put_line(fd, "else") == FAIL
Bram Moolenaarfdf447b2013-02-26 17:21:29 +010011511 || fprintf(fd, " normal! 0%d|", wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011512 || put_eol(fd) == FAIL
11513 || put_line(fd, "endif") == FAIL)
11514 return FAIL;
11515 }
11516 else
11517 {
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011518 if (fprintf(fd, "normal! 0%d|", wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011519 || put_eol(fd) == FAIL)
11520 return FAIL;
11521 }
11522 }
11523 }
11524
11525 /*
11526 * Local directory.
11527 */
11528 if (wp->w_localdir != NULL)
11529 {
11530 if (fputs("lcd ", fd) < 0
11531 || ses_put_fname(fd, wp->w_localdir, flagp) == FAIL
11532 || put_eol(fd) == FAIL)
11533 return FAIL;
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011534 did_lcd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011535 }
11536
11537 return OK;
11538}
11539
11540/*
11541 * Write an argument list to the session file.
11542 * Returns FAIL if writing fails.
11543 */
11544 static int
11545ses_arglist(fd, cmd, gap, fullname, flagp)
11546 FILE *fd;
11547 char *cmd;
11548 garray_T *gap;
11549 int fullname; /* TRUE: use full path name */
11550 unsigned *flagp;
11551{
11552 int i;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011553 char_u *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011554 char_u *s;
11555
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011556 if (fputs(cmd, fd) < 0 || put_eol(fd) == FAIL)
11557 return FAIL;
11558 if (put_line(fd, "silent! argdel *") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011559 return FAIL;
11560 for (i = 0; i < gap->ga_len; ++i)
11561 {
11562 /* NULL file names are skipped (only happens when out of memory). */
11563 s = alist_name(&((aentry_T *)gap->ga_data)[i]);
11564 if (s != NULL)
11565 {
11566 if (fullname)
11567 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020011568 buf = alloc(MAXPATHL);
11569 if (buf != NULL)
11570 {
11571 (void)vim_FullName(s, buf, MAXPATHL, FALSE);
11572 s = buf;
11573 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011574 }
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011575 if (fputs("argadd ", fd) < 0
11576 || ses_put_fname(fd, s, flagp) == FAIL
11577 || put_eol(fd) == FAIL)
Bram Moolenaard9462e32011-04-11 21:35:11 +020011578 {
11579 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011580 return FAIL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011581 }
11582 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011583 }
11584 }
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011585 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011586}
11587
11588/*
11589 * Write a buffer name to the session file.
11590 * Also ends the line.
11591 * Returns FAIL if writing fails.
11592 */
11593 static int
11594ses_fname(fd, buf, flagp)
11595 FILE *fd;
11596 buf_T *buf;
11597 unsigned *flagp;
11598{
11599 char_u *name;
11600
11601 /* Use the short file name if the current directory is known at the time
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011602 * the session file will be sourced.
11603 * Don't do this for ":mkview", we don't know the current directory.
11604 * Don't do this after ":lcd", we don't keep track of what the current
11605 * directory is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011606 if (buf->b_sfname != NULL
11607 && flagp == &ssop_flags
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011608 && (ssop_flags & (SSOP_CURDIR | SSOP_SESDIR))
Bram Moolenaar8d594672009-07-01 18:18:57 +000011609#ifdef FEAT_AUTOCHDIR
11610 && !p_acd
11611#endif
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011612 && !did_lcd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011613 name = buf->b_sfname;
11614 else
11615 name = buf->b_ffname;
11616 if (ses_put_fname(fd, name, flagp) == FAIL || put_eol(fd) == FAIL)
11617 return FAIL;
11618 return OK;
11619}
11620
11621/*
11622 * Write a file name to the session file.
11623 * Takes care of the "slash" option in 'sessionoptions' and escapes special
11624 * characters.
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011625 * Returns FAIL if writing fails or out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011626 */
11627 static int
11628ses_put_fname(fd, name, flagp)
11629 FILE *fd;
11630 char_u *name;
11631 unsigned *flagp;
11632{
11633 char_u *sname;
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011634 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011635 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011636
11637 sname = home_replace_save(NULL, name);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011638 if (sname == NULL)
11639 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011640
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011641 if (*flagp & SSOP_SLASH)
11642 {
11643 /* change all backslashes to forward slashes */
11644 for (p = sname; *p != NUL; mb_ptr_adv(p))
11645 if (*p == '\\')
11646 *p = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +000011647 }
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011648
Bram Moolenaar84a05ac2013-05-06 04:24:17 +020011649 /* escape special characters */
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011650 p = vim_strsave_fnameescape(sname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011651 vim_free(sname);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011652 if (p == NULL)
11653 return FAIL;
11654
11655 /* write the result */
11656 if (fputs((char *)p, fd) < 0)
11657 retval = FAIL;
11658
11659 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011660 return retval;
11661}
11662
11663/*
11664 * ":loadview [nr]"
11665 */
11666 static void
11667ex_loadview(eap)
11668 exarg_T *eap;
11669{
11670 char_u *fname;
11671
11672 fname = get_view_file(*eap->arg);
11673 if (fname != NULL)
11674 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +000011675 do_source(fname, FALSE, DOSO_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011676 vim_free(fname);
11677 }
11678}
11679
11680/*
11681 * Get the name of the view file for the current buffer.
11682 */
11683 static char_u *
11684get_view_file(c)
11685 int c;
11686{
11687 int len = 0;
11688 char_u *p, *s;
11689 char_u *retval;
11690 char_u *sname;
11691
11692 if (curbuf->b_ffname == NULL)
11693 {
11694 EMSG(_(e_noname));
11695 return NULL;
11696 }
11697 sname = home_replace_save(NULL, curbuf->b_ffname);
11698 if (sname == NULL)
11699 return NULL;
11700
11701 /*
11702 * We want a file name without separators, because we're not going to make
11703 * a directory.
11704 * "normal" path separator -> "=+"
11705 * "=" -> "=="
11706 * ":" path separator -> "=-"
11707 */
11708 for (p = sname; *p; ++p)
11709 if (*p == '=' || vim_ispathsep(*p))
11710 ++len;
11711 retval = alloc((unsigned)(STRLEN(sname) + len + STRLEN(p_vdir) + 9));
11712 if (retval != NULL)
11713 {
11714 STRCPY(retval, p_vdir);
11715 add_pathsep(retval);
11716 s = retval + STRLEN(retval);
11717 for (p = sname; *p; ++p)
11718 {
11719 if (*p == '=')
11720 {
11721 *s++ = '=';
11722 *s++ = '=';
11723 }
11724 else if (vim_ispathsep(*p))
11725 {
11726 *s++ = '=';
Bram Moolenaare60acc12011-05-10 16:41:25 +020011727#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011728 if (*p == ':')
11729 *s++ = '-';
11730 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000011731#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000011732 *s++ = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +000011733 }
11734 else
11735 *s++ = *p;
11736 }
11737 *s++ = '=';
11738 *s++ = c;
11739 STRCPY(s, ".vim");
11740 }
11741
11742 vim_free(sname);
11743 return retval;
11744}
11745
11746#endif /* FEAT_SESSION */
11747
11748/*
11749 * Write end-of-line character(s) for ":mkexrc", ":mkvimrc" and ":mksession".
11750 * Return FAIL for a write error.
11751 */
11752 int
11753put_eol(fd)
11754 FILE *fd;
11755{
11756 if (
11757#ifdef USE_CRNL
11758 (
11759# ifdef MKSESSION_NL
11760 !mksession_nl &&
11761# endif
11762 (putc('\r', fd) < 0)) ||
11763#endif
11764 (putc('\n', fd) < 0))
11765 return FAIL;
11766 return OK;
11767}
11768
11769/*
11770 * Write a line to "fd".
11771 * Return FAIL for a write error.
11772 */
11773 int
11774put_line(fd, s)
11775 FILE *fd;
11776 char *s;
11777{
11778 if (fputs(s, fd) < 0 || put_eol(fd) == FAIL)
11779 return FAIL;
11780 return OK;
11781}
11782
11783#ifdef FEAT_VIMINFO
11784/*
11785 * ":rviminfo" and ":wviminfo".
11786 */
11787 static void
11788ex_viminfo(eap)
11789 exarg_T *eap;
11790{
11791 char_u *save_viminfo;
11792
11793 save_viminfo = p_viminfo;
11794 if (*p_viminfo == NUL)
11795 p_viminfo = (char_u *)"'100";
11796 if (eap->cmdidx == CMD_rviminfo)
11797 {
Bram Moolenaard812df62008-11-09 12:46:09 +000011798 if (read_viminfo(eap->arg, VIF_WANT_INFO | VIF_WANT_MARKS
11799 | (eap->forceit ? VIF_FORCEIT : 0)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011800 EMSG(_("E195: Cannot open viminfo file for reading"));
11801 }
11802 else
11803 write_viminfo(eap->arg, eap->forceit);
11804 p_viminfo = save_viminfo;
11805}
11806#endif
11807
11808#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +000011809/*
Bram Moolenaard9462e32011-04-11 21:35:11 +020011810 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +000011811 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +000011812 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011813 void
11814dialog_msg(buff, format, fname)
11815 char_u *buff;
11816 char *format;
11817 char_u *fname;
11818{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011819 if (fname == NULL)
11820 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +020011821 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011822}
11823#endif
11824
11825/*
11826 * ":behave {mswin,xterm}"
11827 */
11828 static void
11829ex_behave(eap)
11830 exarg_T *eap;
11831{
11832 if (STRCMP(eap->arg, "mswin") == 0)
11833 {
11834 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
11835 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
11836 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
11837 set_option_value((char_u *)"keymodel", 0L,
11838 (char_u *)"startsel,stopsel", 0);
11839 }
11840 else if (STRCMP(eap->arg, "xterm") == 0)
11841 {
11842 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
11843 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
11844 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
11845 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
11846 }
11847 else
11848 EMSG2(_(e_invarg2), eap->arg);
11849}
11850
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010011851#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
11852/*
11853 * Function given to ExpandGeneric() to obtain the possible arguments of the
11854 * ":behave {mswin,xterm}" command.
11855 */
11856 char_u *
11857get_behave_arg(xp, idx)
11858 expand_T *xp UNUSED;
11859 int idx;
11860{
11861 if (idx == 0)
11862 return (char_u *)"mswin";
11863 if (idx == 1)
11864 return (char_u *)"xterm";
11865 return NULL;
11866}
11867#endif
11868
Bram Moolenaar071d4272004-06-13 20:20:40 +000011869#ifdef FEAT_AUTOCMD
11870static int filetype_detect = FALSE;
11871static int filetype_plugin = FALSE;
11872static int filetype_indent = FALSE;
11873
11874/*
11875 * ":filetype [plugin] [indent] {on,off,detect}"
11876 * on: Load the filetype.vim file to install autocommands for file types.
11877 * off: Load the ftoff.vim file to remove all autocommands for file types.
11878 * plugin on: load filetype.vim and ftplugin.vim
11879 * plugin off: load ftplugof.vim
11880 * indent on: load filetype.vim and indent.vim
11881 * indent off: load indoff.vim
11882 */
11883 static void
11884ex_filetype(eap)
11885 exarg_T *eap;
11886{
11887 char_u *arg = eap->arg;
11888 int plugin = FALSE;
11889 int indent = FALSE;
11890
11891 if (*eap->arg == NUL)
11892 {
11893 /* Print current status. */
11894 smsg((char_u *)"filetype detection:%s plugin:%s indent:%s",
11895 filetype_detect ? "ON" : "OFF",
11896 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
11897 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
11898 return;
11899 }
11900
11901 /* Accept "plugin" and "indent" in any order. */
11902 for (;;)
11903 {
11904 if (STRNCMP(arg, "plugin", 6) == 0)
11905 {
11906 plugin = TRUE;
11907 arg = skipwhite(arg + 6);
11908 continue;
11909 }
11910 if (STRNCMP(arg, "indent", 6) == 0)
11911 {
11912 indent = TRUE;
11913 arg = skipwhite(arg + 6);
11914 continue;
11915 }
11916 break;
11917 }
11918 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
11919 {
11920 if (*arg == 'o' || !filetype_detect)
11921 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011922 source_runtime((char_u *)FILETYPE_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011923 filetype_detect = TRUE;
11924 if (plugin)
11925 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011926 source_runtime((char_u *)FTPLUGIN_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011927 filetype_plugin = TRUE;
11928 }
11929 if (indent)
11930 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011931 source_runtime((char_u *)INDENT_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011932 filetype_indent = TRUE;
11933 }
11934 }
11935 if (*arg == 'd')
11936 {
11937 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE);
Bram Moolenaara3227e22006-03-08 21:32:40 +000011938 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011939 }
11940 }
11941 else if (STRCMP(arg, "off") == 0)
11942 {
11943 if (plugin || indent)
11944 {
11945 if (plugin)
11946 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011947 source_runtime((char_u *)FTPLUGOF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011948 filetype_plugin = FALSE;
11949 }
11950 if (indent)
11951 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011952 source_runtime((char_u *)INDOFF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011953 filetype_indent = FALSE;
11954 }
11955 }
11956 else
11957 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011958 source_runtime((char_u *)FTOFF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011959 filetype_detect = FALSE;
11960 }
11961 }
11962 else
11963 EMSG2(_(e_invarg2), arg);
11964}
11965
11966/*
11967 * ":setfiletype {name}"
11968 */
11969 static void
11970ex_setfiletype(eap)
11971 exarg_T *eap;
11972{
11973 if (!did_filetype)
11974 set_option_value((char_u *)"filetype", 0L, eap->arg, OPT_LOCAL);
11975}
11976#endif
11977
Bram Moolenaar071d4272004-06-13 20:20:40 +000011978 static void
11979ex_digraphs(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +000011980 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011981{
11982#ifdef FEAT_DIGRAPHS
11983 if (*eap->arg != NUL)
11984 putdigraph(eap->arg);
11985 else
11986 listdigraphs();
11987#else
11988 EMSG(_("E196: No digraphs in this version"));
11989#endif
11990}
11991
11992 static void
11993ex_set(eap)
11994 exarg_T *eap;
11995{
11996 int flags = 0;
11997
11998 if (eap->cmdidx == CMD_setlocal)
11999 flags = OPT_LOCAL;
12000 else if (eap->cmdidx == CMD_setglobal)
12001 flags = OPT_GLOBAL;
12002#if defined(FEAT_EVAL) && defined(FEAT_AUTOCMD) && defined(FEAT_BROWSE)
12003 if (cmdmod.browse && flags == 0)
12004 ex_options(eap);
12005 else
12006#endif
12007 (void)do_set(eap->arg, flags);
12008}
12009
12010#ifdef FEAT_SEARCH_EXTRA
12011/*
12012 * ":nohlsearch"
12013 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012014 static void
12015ex_nohlsearch(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +000012016 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012017{
Bram Moolenaar8050efa2013-11-08 04:30:20 +010012018 SET_NO_HLSEARCH(TRUE);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +000012019 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012020}
12021
12022/*
Bram Moolenaare1438bb2006-03-01 22:01:55 +000012023 * ":[N]match {group} {pattern}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000012024 * Sets nextcmd to the start of the next command, if any. Also called when
12025 * skipping commands to find the next command.
12026 */
12027 static void
12028ex_match(eap)
12029 exarg_T *eap;
12030{
12031 char_u *p;
Bram Moolenaar52d36c82007-08-11 14:00:30 +000012032 char_u *g = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012033 char_u *end;
12034 int c;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012035 int id;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000012036
12037 if (eap->line2 <= 3)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012038 id = eap->line2;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000012039 else
12040 {
12041 EMSG(e_invcmd);
12042 return;
12043 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012044
12045 /* First clear any old pattern. */
12046 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012047 match_delete(curwin, id, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012048
12049 if (ends_excmd(*eap->arg))
12050 end = eap->arg;
12051 else if ((STRNICMP(eap->arg, "none", 4) == 0
12052 && (vim_iswhite(eap->arg[4]) || ends_excmd(eap->arg[4]))))
12053 end = eap->arg + 4;
12054 else
12055 {
12056 p = skiptowhite(eap->arg);
12057 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012058 g = vim_strnsave(eap->arg, (int)(p - eap->arg));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012059 p = skipwhite(p);
12060 if (*p == NUL)
12061 {
12062 /* There must be two arguments. */
12063 EMSG2(_(e_invarg2), eap->arg);
12064 return;
12065 }
12066 end = skip_regexp(p + 1, *p, TRUE, NULL);
12067 if (!eap->skip)
12068 {
12069 if (*end != NUL && !ends_excmd(*skipwhite(end + 1)))
12070 {
12071 eap->errmsg = e_trailing;
12072 return;
12073 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000012074 if (*end != *p)
12075 {
12076 EMSG2(_(e_invarg2), p);
12077 return;
12078 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012079
12080 c = *end;
12081 *end = NUL;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012082 match_add(curwin, g, p + 1, 10, id, NULL);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012083 vim_free(g);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012084 *end = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012085 }
12086 }
12087 eap->nextcmd = find_nextcmd(end);
12088}
12089#endif
12090
12091#ifdef FEAT_CRYPT
12092/*
12093 * ":X": Get crypt key
12094 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012095 static void
12096ex_X(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +000012097 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012098{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +010012099 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +020012100 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012101}
12102#endif
12103
12104#ifdef FEAT_FOLDING
12105 static void
12106ex_fold(eap)
12107 exarg_T *eap;
12108{
12109 if (foldManualAllowed(TRUE))
12110 foldCreate(eap->line1, eap->line2);
12111}
12112
12113 static void
12114ex_foldopen(eap)
12115 exarg_T *eap;
12116{
12117 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
12118 eap->forceit, FALSE);
12119}
12120
12121 static void
12122ex_folddo(eap)
12123 exarg_T *eap;
12124{
12125 linenr_T lnum;
12126
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020012127#ifdef FEAT_CLIPBOARD
12128 start_global_changes();
12129#endif
12130
Bram Moolenaar071d4272004-06-13 20:20:40 +000012131 /* First set the marks for all lines closed/open. */
12132 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
12133 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
12134 ml_setmarked(lnum);
12135
12136 /* Execute the command on the marked lines. */
12137 global_exe(eap->arg);
12138 ml_clearmarked(); /* clear rest of the marks */
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020012139#ifdef FEAT_CLIPBOARD
12140 end_global_changes();
12141#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012142}
12143#endif