blob: 1c700d47355bae2d0691c2b2d64e63012b7e025e [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * ex_docmd.c: functions for executing an Ex command line.
12 */
13
14#include "vim.h"
15
Bram Moolenaar071d4272004-06-13 20:20:40 +000016static int quitmore = 0;
17static int ex_pressedreturn = FALSE;
18#ifndef FEAT_PRINTER
19# define ex_hardcopy ex_ni
20#endif
21
22#ifdef FEAT_USR_CMDS
23typedef struct ucmd
24{
25 char_u *uc_name; /* The command name */
26 long_u uc_argt; /* The argument type */
27 char_u *uc_rep; /* The command's replacement string */
28 long uc_def; /* The default value for a range/count */
Bram Moolenaar071d4272004-06-13 20:20:40 +000029 int uc_compl; /* completion type */
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010030# ifdef FEAT_EVAL
31 scid_T uc_scriptID; /* SID where the command was defined */
32# ifdef FEAT_CMDL_COMPL
Bram Moolenaar071d4272004-06-13 20:20:40 +000033 char_u *uc_compl_arg; /* completion argument if any */
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010034# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000035# endif
36} ucmd_T;
37
38#define UC_BUFFER 1 /* -buffer: local to current buffer */
39
Bram Moolenaar2c29bee2005-06-01 21:46:07 +000040static garray_T ucmds = {0, 0, sizeof(ucmd_T), 4, NULL};
Bram Moolenaar071d4272004-06-13 20:20:40 +000041
42#define USER_CMD(i) (&((ucmd_T *)(ucmds.ga_data))[i])
43#define USER_CMD_GA(gap, i) (&((ucmd_T *)((gap)->ga_data))[i])
44
45static void do_ucmd __ARGS((exarg_T *eap));
46static void ex_command __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +000047static void ex_delcommand __ARGS((exarg_T *eap));
48# ifdef FEAT_CMDL_COMPL
49static char_u *get_user_command_name __ARGS((int idx));
50# endif
51
52#else
53# define ex_command ex_ni
54# define ex_comclear ex_ni
55# define ex_delcommand ex_ni
56#endif
57
58#ifdef FEAT_EVAL
Bram Moolenaar89d40322006-08-29 15:30:07 +000059static char_u *do_one_cmd __ARGS((char_u **, int, struct condstack *, char_u *(*fgetline)(int, void *, int), void *cookie));
Bram Moolenaar071d4272004-06-13 20:20:40 +000060#else
Bram Moolenaar89d40322006-08-29 15:30:07 +000061static char_u *do_one_cmd __ARGS((char_u **, int, char_u *(*fgetline)(int, void *, int), void *cookie));
Bram Moolenaar071d4272004-06-13 20:20:40 +000062static int if_level = 0; /* depth in :if */
63#endif
Bram Moolenaara6f4d612011-09-21 19:10:46 +020064static void append_command __ARGS((char_u *cmd));
Bram Moolenaar071d4272004-06-13 20:20:40 +000065static char_u *find_command __ARGS((exarg_T *eap, int *full));
66
67static void ex_abbreviate __ARGS((exarg_T *eap));
68static void ex_map __ARGS((exarg_T *eap));
69static void ex_unmap __ARGS((exarg_T *eap));
70static void ex_mapclear __ARGS((exarg_T *eap));
71static void ex_abclear __ARGS((exarg_T *eap));
72#ifndef FEAT_MENU
73# define ex_emenu ex_ni
74# define ex_menu ex_ni
75# define ex_menutranslate ex_ni
76#endif
77#ifdef FEAT_AUTOCMD
78static void ex_autocmd __ARGS((exarg_T *eap));
79static void ex_doautocmd __ARGS((exarg_T *eap));
80#else
81# define ex_autocmd ex_ni
82# define ex_doautocmd ex_ni
83# define ex_doautoall ex_ni
84#endif
85#ifdef FEAT_LISTCMDS
86static void ex_bunload __ARGS((exarg_T *eap));
87static void ex_buffer __ARGS((exarg_T *eap));
88static void ex_bmodified __ARGS((exarg_T *eap));
89static void ex_bnext __ARGS((exarg_T *eap));
90static void ex_bprevious __ARGS((exarg_T *eap));
91static void ex_brewind __ARGS((exarg_T *eap));
92static void ex_blast __ARGS((exarg_T *eap));
93#else
94# define ex_bunload ex_ni
95# define ex_buffer ex_ni
96# define ex_bmodified ex_ni
97# define ex_bnext ex_ni
98# define ex_bprevious ex_ni
99# define ex_brewind ex_ni
100# define ex_blast ex_ni
101# define buflist_list ex_ni
102# define ex_checktime ex_ni
103#endif
104#if !defined(FEAT_LISTCMDS) || !defined(FEAT_WINDOWS)
105# define ex_buffer_all ex_ni
106#endif
107static char_u *getargcmd __ARGS((char_u **));
108static char_u *skip_cmd_arg __ARGS((char_u *p, int rembs));
109static int getargopt __ARGS((exarg_T *eap));
110#ifndef FEAT_QUICKFIX
111# define ex_make ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +0000112# define ex_cbuffer ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000113# define ex_cc ex_ni
114# define ex_cnext ex_ni
115# define ex_cfile ex_ni
116# define qf_list ex_ni
117# define qf_age ex_ni
118# define ex_helpgrep ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +0000119# define ex_vimgrep ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000120#endif
121#if !defined(FEAT_QUICKFIX) || !defined(FEAT_WINDOWS)
122# define ex_cclose ex_ni
123# define ex_copen ex_ni
124# define ex_cwindow ex_ni
125#endif
Bram Moolenaar1e015462005-09-25 22:16:38 +0000126#if !defined(FEAT_QUICKFIX) || !defined(FEAT_EVAL)
127# define ex_cexpr ex_ni
128#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000129
130static int check_more __ARGS((int, int));
131static linenr_T get_address __ARGS((char_u **, int skip, int to_other_file));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000132static void get_flags __ARGS((exarg_T *eap));
Bram Moolenaar85363ab2010-07-18 13:58:26 +0200133#if !defined(FEAT_PERL) \
134 || !defined(FEAT_PYTHON) || !defined(FEAT_PYTHON3) \
135 || !defined(FEAT_TCL) \
136 || !defined(FEAT_RUBY) \
137 || !defined(FEAT_LUA) \
138 || !defined(FEAT_MZSCHEME)
Bram Moolenaar7bb75552007-07-16 18:39:49 +0000139# define HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +0000140static void ex_script_ni __ARGS((exarg_T *eap));
141#endif
142static char_u *invalid_range __ARGS((exarg_T *eap));
143static void correct_range __ARGS((exarg_T *eap));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000144#ifdef FEAT_QUICKFIX
145static char_u *replace_makeprg __ARGS((exarg_T *eap, char_u *p, char_u **cmdlinep));
146#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000147static char_u *repl_cmdline __ARGS((exarg_T *eap, char_u *src, int srclen, char_u *repl, char_u **cmdlinep));
148static void ex_highlight __ARGS((exarg_T *eap));
149static void ex_colorscheme __ARGS((exarg_T *eap));
150static void ex_quit __ARGS((exarg_T *eap));
151static void ex_cquit __ARGS((exarg_T *eap));
152static void ex_quit_all __ARGS((exarg_T *eap));
153#ifdef FEAT_WINDOWS
154static void ex_close __ARGS((exarg_T *eap));
Bram Moolenaarf740b292006-02-16 22:11:02 +0000155static void ex_win_close __ARGS((int forceit, win_T *win, tabpage_T *tp));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000156static void ex_only __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000157static void ex_resize __ARGS((exarg_T *eap));
158static void ex_stag __ARGS((exarg_T *eap));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000159static void ex_tabclose __ARGS((exarg_T *eap));
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000160static void ex_tabonly __ARGS((exarg_T *eap));
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000161static void ex_tabnext __ARGS((exarg_T *eap));
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000162static void ex_tabmove __ARGS((exarg_T *eap));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000163static void ex_tabs __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000164#else
165# define ex_close ex_ni
166# define ex_only ex_ni
167# define ex_all ex_ni
168# define ex_resize ex_ni
169# define ex_splitview ex_ni
170# define ex_stag ex_ni
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000171# define ex_tabnext ex_ni
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000172# define ex_tabmove ex_ni
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000173# define ex_tabs ex_ni
174# define ex_tabclose ex_ni
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000175# define ex_tabonly ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000176#endif
177#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
178static void ex_pclose __ARGS((exarg_T *eap));
179static void ex_ptag __ARGS((exarg_T *eap));
180static void ex_pedit __ARGS((exarg_T *eap));
181#else
182# define ex_pclose ex_ni
183# define ex_ptag ex_ni
184# define ex_pedit ex_ni
185#endif
186static void ex_hide __ARGS((exarg_T *eap));
187static void ex_stop __ARGS((exarg_T *eap));
188static void ex_exit __ARGS((exarg_T *eap));
189static void ex_print __ARGS((exarg_T *eap));
190#ifdef FEAT_BYTEOFF
191static void ex_goto __ARGS((exarg_T *eap));
192#else
193# define ex_goto ex_ni
194#endif
195static void ex_shell __ARGS((exarg_T *eap));
196static void ex_preserve __ARGS((exarg_T *eap));
197static void ex_recover __ARGS((exarg_T *eap));
198#ifndef FEAT_LISTCMDS
199# define ex_argedit ex_ni
200# define ex_argadd ex_ni
201# define ex_argdelete ex_ni
202# define ex_listdo ex_ni
203#endif
204static void ex_mode __ARGS((exarg_T *eap));
205static void ex_wrongmodifier __ARGS((exarg_T *eap));
206static void ex_find __ARGS((exarg_T *eap));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000207static void ex_open __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000208static void ex_edit __ARGS((exarg_T *eap));
209#if !defined(FEAT_GUI) && !defined(FEAT_CLIENTSERVER)
210# define ex_drop ex_ni
211#endif
212#ifndef FEAT_GUI
213# define ex_gui ex_nogui
214static void ex_nogui __ARGS((exarg_T *eap));
215#endif
216#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
217static void ex_tearoff __ARGS((exarg_T *eap));
218#else
219# define ex_tearoff ex_ni
220#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000221#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000222static void ex_popup __ARGS((exarg_T *eap));
223#else
224# define ex_popup ex_ni
225#endif
226#ifndef FEAT_GUI_MSWIN
227# define ex_simalt ex_ni
228#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000229#if !defined(FEAT_GUI_MSWIN) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000230# define gui_mch_find_dialog ex_ni
231# define gui_mch_replace_dialog ex_ni
232#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000233#if !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000234# define ex_helpfind ex_ni
235#endif
236#ifndef FEAT_CSCOPE
237# define do_cscope ex_ni
238# define do_scscope ex_ni
239# define do_cstag ex_ni
240#endif
241#ifndef FEAT_SYN_HL
242# define ex_syntax ex_ni
Bram Moolenaar860cae12010-06-05 23:22:07 +0200243# define ex_ownsyntax ex_ni
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000244#endif
Bram Moolenaarf7512552013-06-06 14:55:19 +0200245#if !defined(FEAT_SYN_HL) || !defined(FEAT_PROFILE)
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +0200246# define ex_syntime ex_ni
247#endif
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000248#ifndef FEAT_SPELL
Bram Moolenaarb765d632005-06-07 21:00:02 +0000249# define ex_spell ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000250# define ex_mkspell ex_ni
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000251# define ex_spelldump ex_ni
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000252# define ex_spellinfo ex_ni
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000253# define ex_spellrepall ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000254#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200255#ifndef FEAT_PERSISTENT_UNDO
256# define ex_rundo ex_ni
257# define ex_wundo ex_ni
258#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200259#ifndef FEAT_LUA
260# define ex_lua ex_script_ni
261# define ex_luado ex_ni
262# define ex_luafile ex_ni
263#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000264#ifndef FEAT_MZSCHEME
265# define ex_mzscheme ex_script_ni
266# define ex_mzfile ex_ni
267#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000268#ifndef FEAT_PERL
269# define ex_perl ex_script_ni
270# define ex_perldo ex_ni
271#endif
272#ifndef FEAT_PYTHON
273# define ex_python ex_script_ni
Bram Moolenaard620aa92013-05-17 16:40:06 +0200274# define ex_pydo ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000275# define ex_pyfile ex_ni
276#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200277#ifndef FEAT_PYTHON3
Bram Moolenaar368373e2010-07-19 20:46:22 +0200278# define ex_py3 ex_script_ni
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200279# define ex_py3do ex_ni
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200280# define ex_py3file ex_ni
281#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000282#ifndef FEAT_TCL
283# define ex_tcl ex_script_ni
284# define ex_tcldo ex_ni
285# define ex_tclfile ex_ni
286#endif
287#ifndef FEAT_RUBY
288# define ex_ruby ex_script_ni
289# define ex_rubydo ex_ni
290# define ex_rubyfile ex_ni
291#endif
292#ifndef FEAT_SNIFF
293# define ex_sniff ex_ni
294#endif
295#ifndef FEAT_KEYMAP
296# define ex_loadkeymap ex_ni
297#endif
298static void ex_swapname __ARGS((exarg_T *eap));
299static void ex_syncbind __ARGS((exarg_T *eap));
300static void ex_read __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000301static void ex_pwd __ARGS((exarg_T *eap));
302static void ex_equal __ARGS((exarg_T *eap));
303static void ex_sleep __ARGS((exarg_T *eap));
304static void do_exmap __ARGS((exarg_T *eap, int isabbrev));
305static void ex_winsize __ARGS((exarg_T *eap));
306#ifdef FEAT_WINDOWS
307static void ex_wincmd __ARGS((exarg_T *eap));
308#else
309# define ex_wincmd ex_ni
310#endif
Bram Moolenaar843ee412004-06-30 16:16:41 +0000311#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000312static void ex_winpos __ARGS((exarg_T *eap));
313#else
314# define ex_winpos ex_ni
315#endif
316static void ex_operators __ARGS((exarg_T *eap));
317static void ex_put __ARGS((exarg_T *eap));
318static void ex_copymove __ARGS((exarg_T *eap));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000319static void ex_may_print __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000320static void ex_submagic __ARGS((exarg_T *eap));
321static void ex_join __ARGS((exarg_T *eap));
322static void ex_at __ARGS((exarg_T *eap));
323static void ex_bang __ARGS((exarg_T *eap));
324static void ex_undo __ARGS((exarg_T *eap));
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200325#ifdef FEAT_PERSISTENT_UNDO
326static void ex_wundo __ARGS((exarg_T *eap));
327static void ex_rundo __ARGS((exarg_T *eap));
328#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000329static void ex_redo __ARGS((exarg_T *eap));
Bram Moolenaarc7d89352006-03-14 22:53:34 +0000330static void ex_later __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000331static void ex_redir __ARGS((exarg_T *eap));
332static void ex_redraw __ARGS((exarg_T *eap));
333static void ex_redrawstatus __ARGS((exarg_T *eap));
334static void close_redir __ARGS((void));
335static void ex_mkrc __ARGS((exarg_T *eap));
336static void ex_mark __ARGS((exarg_T *eap));
337#ifdef FEAT_USR_CMDS
338static char_u *uc_fun_cmd __ARGS((void));
Bram Moolenaar52b4b552005-03-07 23:00:57 +0000339static char_u *find_ucmd __ARGS((exarg_T *eap, char_u *p, int *full, expand_T *xp, int *compl));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000340#endif
341#ifdef FEAT_EX_EXTRA
342static void ex_normal __ARGS((exarg_T *eap));
343static void ex_startinsert __ARGS((exarg_T *eap));
344static void ex_stopinsert __ARGS((exarg_T *eap));
345#else
346# define ex_normal ex_ni
347# define ex_align ex_ni
348# define ex_retab ex_ni
349# define ex_startinsert ex_ni
350# define ex_stopinsert ex_ni
351# define ex_helptags ex_ni
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +0000352# define ex_sort ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000353#endif
354#ifdef FEAT_FIND_ID
355static void ex_checkpath __ARGS((exarg_T *eap));
356static void ex_findpat __ARGS((exarg_T *eap));
357#else
358# define ex_findpat ex_ni
359# define ex_checkpath ex_ni
360#endif
361#if defined(FEAT_FIND_ID) && defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
362static void ex_psearch __ARGS((exarg_T *eap));
363#else
364# define ex_psearch ex_ni
365#endif
366static void ex_tag __ARGS((exarg_T *eap));
367static void ex_tag_cmd __ARGS((exarg_T *eap, char_u *name));
368#ifndef FEAT_EVAL
369# define ex_scriptnames ex_ni
370# define ex_finish ex_ni
371# define ex_echo ex_ni
372# define ex_echohl ex_ni
373# define ex_execute ex_ni
374# define ex_call ex_ni
375# define ex_if ex_ni
376# define ex_endif ex_ni
377# define ex_else ex_ni
378# define ex_while ex_ni
379# define ex_continue ex_ni
380# define ex_break ex_ni
381# define ex_endwhile ex_ni
382# define ex_throw ex_ni
383# define ex_try ex_ni
384# define ex_catch ex_ni
385# define ex_finally ex_ni
386# define ex_endtry ex_ni
387# define ex_endfunction ex_ni
388# define ex_let ex_ni
389# define ex_unlet ex_ni
Bram Moolenaar65c1b012005-01-31 19:02:28 +0000390# define ex_lockvar ex_ni
391# define ex_unlockvar ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000392# define ex_function ex_ni
393# define ex_delfunction ex_ni
394# define ex_return ex_ni
Bram Moolenaard812df62008-11-09 12:46:09 +0000395# define ex_oldfiles ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000396#endif
397static char_u *arg_all __ARGS((void));
398#ifdef FEAT_SESSION
399static int makeopens __ARGS((FILE *fd, char_u *dirnow));
Bram Moolenaarf13be0d2008-01-02 14:13:10 +0000400static int put_view __ARGS((FILE *fd, win_T *wp, int add_edit, unsigned *flagp, int current_arg_idx));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000401static void ex_loadview __ARGS((exarg_T *eap));
402static char_u *get_view_file __ARGS((int c));
Bram Moolenaareeefcc72007-05-01 21:21:21 +0000403static int did_lcd; /* whether ":lcd" was produced for a session */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000404#else
405# define ex_loadview ex_ni
406#endif
407#ifndef FEAT_EVAL
408# define ex_compiler ex_ni
409#endif
410#ifdef FEAT_VIMINFO
411static void ex_viminfo __ARGS((exarg_T *eap));
412#else
413# define ex_viminfo ex_ni
414#endif
415static void ex_behave __ARGS((exarg_T *eap));
416#ifdef FEAT_AUTOCMD
417static void ex_filetype __ARGS((exarg_T *eap));
418static void ex_setfiletype __ARGS((exarg_T *eap));
419#else
420# define ex_filetype ex_ni
421# define ex_setfiletype ex_ni
422#endif
423#ifndef FEAT_DIFF
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000424# define ex_diffoff ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000425# define ex_diffpatch ex_ni
426# define ex_diffgetput ex_ni
427# define ex_diffsplit ex_ni
428# define ex_diffthis ex_ni
429# define ex_diffupdate ex_ni
430#endif
431static void ex_digraphs __ARGS((exarg_T *eap));
432static void ex_set __ARGS((exarg_T *eap));
433#if !defined(FEAT_EVAL) || !defined(FEAT_AUTOCMD)
434# define ex_options ex_ni
435#endif
436#ifdef FEAT_SEARCH_EXTRA
437static void ex_nohlsearch __ARGS((exarg_T *eap));
438static void ex_match __ARGS((exarg_T *eap));
439#else
440# define ex_nohlsearch ex_ni
441# define ex_match ex_ni
442#endif
443#ifdef FEAT_CRYPT
444static void ex_X __ARGS((exarg_T *eap));
445#else
446# define ex_X ex_ni
447#endif
448#ifdef FEAT_FOLDING
449static void ex_fold __ARGS((exarg_T *eap));
450static void ex_foldopen __ARGS((exarg_T *eap));
451static void ex_folddo __ARGS((exarg_T *eap));
452#else
453# define ex_fold ex_ni
454# define ex_foldopen ex_ni
455# define ex_folddo ex_ni
456#endif
457#if !((defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
458 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE)))
459# define ex_language ex_ni
460#endif
461#ifndef FEAT_SIGNS
462# define ex_sign ex_ni
463#endif
464#ifndef FEAT_SUN_WORKSHOP
465# define ex_wsverb ex_ni
466#endif
Bram Moolenaar009b2592004-10-24 19:18:58 +0000467#ifndef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200468# define ex_nbclose ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000469# define ex_nbkey ex_ni
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200470# define ex_nbstart ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000471#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000472
473#ifndef FEAT_EVAL
474# define ex_debug ex_ni
475# define ex_breakadd ex_ni
476# define ex_debuggreedy ex_ni
477# define ex_breakdel ex_ni
478# define ex_breaklist ex_ni
479#endif
480
481#ifndef FEAT_CMDHIST
482# define ex_history ex_ni
483#endif
484#ifndef FEAT_JUMPLIST
485# define ex_jumps ex_ni
486# define ex_changes ex_ni
487#endif
488
Bram Moolenaar05159a02005-02-26 23:04:13 +0000489#ifndef FEAT_PROFILE
490# define ex_profile ex_ni
491#endif
492
Bram Moolenaar071d4272004-06-13 20:20:40 +0000493/*
494 * Declare cmdnames[].
495 */
496#define DO_DECLARE_EXCMD
497#include "ex_cmds.h"
498
499/*
500 * Table used to quickly search for a command, based on its first character.
501 */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +0000502static cmdidx_T cmdidxs[27] =
Bram Moolenaar071d4272004-06-13 20:20:40 +0000503{
504 CMD_append,
505 CMD_buffer,
506 CMD_change,
507 CMD_delete,
508 CMD_edit,
509 CMD_file,
510 CMD_global,
511 CMD_help,
512 CMD_insert,
513 CMD_join,
514 CMD_k,
515 CMD_list,
516 CMD_move,
517 CMD_next,
518 CMD_open,
519 CMD_print,
520 CMD_quit,
521 CMD_read,
522 CMD_substitute,
523 CMD_t,
524 CMD_undo,
525 CMD_vglobal,
526 CMD_write,
527 CMD_xit,
528 CMD_yank,
529 CMD_z,
530 CMD_bang
531};
532
533static char_u dollar_command[2] = {'$', 0};
534
535
536#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000537/* Struct for storing a line inside a while/for loop */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000538typedef struct
539{
540 char_u *line; /* command line */
541 linenr_T lnum; /* sourcing_lnum of the line */
542} wcmd_T;
543
544/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000545 * Structure used to store info for line position in a while or for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000546 * This is required, because do_one_cmd() may invoke ex_function(), which
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000547 * reads more lines that may come from the while/for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000548 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000549struct loop_cookie
Bram Moolenaar071d4272004-06-13 20:20:40 +0000550{
551 garray_T *lines_gap; /* growarray with line info */
552 int current_line; /* last read line from growarray */
553 int repeating; /* TRUE when looping a second time */
554 /* When "repeating" is FALSE use "getline" and "cookie" to get lines */
555 char_u *(*getline) __ARGS((int, void *, int));
556 void *cookie;
557};
558
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000559static char_u *get_loop_line __ARGS((int c, void *cookie, int indent));
560static int store_loop_line __ARGS((garray_T *gap, char_u *line));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000561static void free_cmdlines __ARGS((garray_T *gap));
Bram Moolenaared203462004-06-16 11:19:22 +0000562
563/* Struct to save a few things while debugging. Used in do_cmdline() only. */
564struct dbg_stuff
565{
566 int trylevel;
567 int force_abort;
568 except_T *caught_stack;
569 char_u *vv_exception;
570 char_u *vv_throwpoint;
571 int did_emsg;
572 int got_int;
573 int did_throw;
574 int need_rethrow;
575 int check_cstack;
576 except_T *current_exception;
577};
578
579static void save_dbg_stuff __ARGS((struct dbg_stuff *dsp));
580static void restore_dbg_stuff __ARGS((struct dbg_stuff *dsp));
581
582 static void
583save_dbg_stuff(dsp)
584 struct dbg_stuff *dsp;
585{
586 dsp->trylevel = trylevel; trylevel = 0;
587 dsp->force_abort = force_abort; force_abort = FALSE;
588 dsp->caught_stack = caught_stack; caught_stack = NULL;
589 dsp->vv_exception = v_exception(NULL);
590 dsp->vv_throwpoint = v_throwpoint(NULL);
591
592 /* Necessary for debugging an inactive ":catch", ":finally", ":endtry" */
593 dsp->did_emsg = did_emsg; did_emsg = FALSE;
594 dsp->got_int = got_int; got_int = FALSE;
595 dsp->did_throw = did_throw; did_throw = FALSE;
596 dsp->need_rethrow = need_rethrow; need_rethrow = FALSE;
597 dsp->check_cstack = check_cstack; check_cstack = FALSE;
598 dsp->current_exception = current_exception; current_exception = NULL;
599}
600
601 static void
602restore_dbg_stuff(dsp)
603 struct dbg_stuff *dsp;
604{
605 suppress_errthrow = FALSE;
606 trylevel = dsp->trylevel;
607 force_abort = dsp->force_abort;
608 caught_stack = dsp->caught_stack;
609 (void)v_exception(dsp->vv_exception);
610 (void)v_throwpoint(dsp->vv_throwpoint);
611 did_emsg = dsp->did_emsg;
612 got_int = dsp->got_int;
613 did_throw = dsp->did_throw;
614 need_rethrow = dsp->need_rethrow;
615 check_cstack = dsp->check_cstack;
616 current_exception = dsp->current_exception;
617}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000618#endif
619
620
621/*
622 * do_exmode(): Repeatedly get commands for the "Ex" mode, until the ":vi"
623 * command is given.
624 */
625 void
626do_exmode(improved)
627 int improved; /* TRUE for "improved Ex" mode */
628{
629 int save_msg_scroll;
630 int prev_msg_row;
631 linenr_T prev_line;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000632 int changedtick;
633
634 if (improved)
635 exmode_active = EXMODE_VIM;
636 else
637 exmode_active = EXMODE_NORMAL;
638 State = NORMAL;
639
640 /* When using ":global /pat/ visual" and then "Q" we return to continue
641 * the :global command. */
642 if (global_busy)
643 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000644
645 save_msg_scroll = msg_scroll;
646 ++RedrawingDisabled; /* don't redisplay the window */
647 ++no_wait_return; /* don't wait for return */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000648#ifdef FEAT_GUI
649 /* Ignore scrollbar and mouse events in Ex mode */
650 ++hold_gui_events;
651#endif
652#ifdef FEAT_SNIFF
653 want_sniff_request = 0; /* No K_SNIFF wanted */
654#endif
655
656 MSG(_("Entering Ex mode. Type \"visual\" to go to Normal mode."));
657 while (exmode_active)
658 {
Bram Moolenaar7c626922005-02-07 22:01:03 +0000659#ifdef FEAT_EX_EXTRA
660 /* Check for a ":normal" command and no more characters left. */
661 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
662 {
663 exmode_active = FALSE;
664 break;
665 }
666#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667 msg_scroll = TRUE;
668 need_wait_return = FALSE;
669 ex_pressedreturn = FALSE;
670 ex_no_reprint = FALSE;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000671 changedtick = curbuf->b_changedtick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000672 prev_msg_row = msg_row;
673 prev_line = curwin->w_cursor.lnum;
674#ifdef FEAT_SNIFF
675 ProcessSniffRequests();
676#endif
677 if (improved)
678 {
679 cmdline_row = msg_row;
680 do_cmdline(NULL, getexline, NULL, 0);
681 }
682 else
683 do_cmdline(NULL, getexmodeline, NULL, DOCMD_NOWAIT);
684 lines_left = Rows - 1;
685
Bram Moolenaardf177f62005-02-22 08:39:57 +0000686 if ((prev_line != curwin->w_cursor.lnum
687 || changedtick != curbuf->b_changedtick) && !ex_no_reprint)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000689 if (curbuf->b_ml.ml_flags & ML_EMPTY)
690 EMSG(_(e_emptybuf));
691 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000693 if (ex_pressedreturn)
694 {
695 /* go up one line, to overwrite the ":<CR>" line, so the
Bram Moolenaar81870892007-11-11 18:17:28 +0000696 * output doesn't contain empty lines. */
Bram Moolenaardf177f62005-02-22 08:39:57 +0000697 msg_row = prev_msg_row;
698 if (prev_msg_row == Rows - 1)
699 msg_row--;
700 }
701 msg_col = 0;
702 print_line_no_prefix(curwin->w_cursor.lnum, FALSE, FALSE);
703 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000705 }
Bram Moolenaardf177f62005-02-22 08:39:57 +0000706 else if (ex_pressedreturn && !ex_no_reprint) /* must be at EOF */
707 {
708 if (curbuf->b_ml.ml_flags & ML_EMPTY)
709 EMSG(_(e_emptybuf));
710 else
711 EMSG(_("E501: At end-of-file"));
712 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000713 }
714
715#ifdef FEAT_GUI
716 --hold_gui_events;
717#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000718 --RedrawingDisabled;
719 --no_wait_return;
720 update_screen(CLEAR);
721 need_wait_return = FALSE;
722 msg_scroll = save_msg_scroll;
723}
724
725/*
726 * Execute a simple command line. Used for translated commands like "*".
727 */
728 int
729do_cmdline_cmd(cmd)
730 char_u *cmd;
731{
732 return do_cmdline(cmd, NULL, NULL,
733 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
734}
735
736/*
737 * do_cmdline(): execute one Ex command line
738 *
739 * 1. Execute "cmdline" when it is not NULL.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100740 * If "cmdline" is NULL, or more lines are needed, fgetline() is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000741 * 2. Split up in parts separated with '|'.
742 *
743 * This function can be called recursively!
744 *
745 * flags:
746 * DOCMD_VERBOSE - The command will be included in the error message.
747 * DOCMD_NOWAIT - Don't call wait_return() and friends.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100748 * DOCMD_REPEAT - Repeat execution until fgetline() returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000749 * DOCMD_KEYTYPED - Don't reset KeyTyped.
750 * DOCMD_EXCRESET - Reset the exception environment (used for debugging).
751 * DOCMD_KEEPLINE - Store first typed line (for repeating with ".").
752 *
753 * return FAIL if cmdline could not be executed, OK otherwise
754 */
755 int
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100756do_cmdline(cmdline, fgetline, cookie, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000757 char_u *cmdline;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100758 char_u *(*fgetline) __ARGS((int, void *, int));
759 void *cookie; /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000760 int flags;
761{
762 char_u *next_cmdline; /* next cmd to execute */
763 char_u *cmdline_copy = NULL; /* copy of cmd line */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100764 int used_getline = FALSE; /* used "fgetline" to obtain command */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765 static int recursive = 0; /* recursive depth */
766 int msg_didout_before_start = 0;
767 int count = 0; /* line number count */
768 int did_inc = FALSE; /* incremented RedrawingDisabled */
769 int retval = OK;
770#ifdef FEAT_EVAL
771 struct condstack cstack; /* conditional stack */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000772 garray_T lines_ga; /* keep lines for ":while"/":for" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773 int current_line = 0; /* active line in lines_ga */
774 char_u *fname = NULL; /* function or script name */
775 linenr_T *breakpoint = NULL; /* ptr to breakpoint field in cookie */
776 int *dbg_tick = NULL; /* ptr to dbg_tick field in cookie */
Bram Moolenaared203462004-06-16 11:19:22 +0000777 struct dbg_stuff debug_saved; /* saved things for debug mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778 int initial_trylevel;
779 struct msglist **saved_msg_list = NULL;
780 struct msglist *private_msg_list;
781
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100782 /* "fgetline" and "cookie" passed to do_one_cmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783 char_u *(*cmd_getline) __ARGS((int, void *, int));
784 void *cmd_cookie;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000785 struct loop_cookie cmd_loop_cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786 void *real_cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000787 int getline_is_func;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788#else
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100789# define cmd_getline fgetline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790# define cmd_cookie cookie
791#endif
792 static int call_depth = 0; /* recursiveness */
793
794#ifdef FEAT_EVAL
795 /* For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory
796 * location for storing error messages to be converted to an exception.
Bram Moolenaarcf3630f2005-01-08 16:04:29 +0000797 * This ensures that the do_errthrow() call in do_one_cmd() does not
798 * combine the messages stored by an earlier invocation of do_one_cmd()
799 * with the command name of the later one. This would happen when
800 * BufWritePost autocommands are executed after a write error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801 saved_msg_list = msg_list;
802 msg_list = &private_msg_list;
803 private_msg_list = NULL;
804#endif
805
806 /* It's possible to create an endless loop with ":execute", catch that
807 * here. The value of 200 allows nested function calls, ":source", etc. */
808 if (call_depth == 200)
809 {
810 EMSG(_("E169: Command too recursive"));
811#ifdef FEAT_EVAL
812 /* When converting to an exception, we do not include the command name
813 * since this is not an error of the specific command. */
814 do_errthrow((struct condstack *)NULL, (char_u *)NULL);
815 msg_list = saved_msg_list;
816#endif
817 return FAIL;
818 }
819 ++call_depth;
820
821#ifdef FEAT_EVAL
822 cstack.cs_idx = -1;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000823 cstack.cs_looplevel = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824 cstack.cs_trylevel = 0;
825 cstack.cs_emsg_silent_list = NULL;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000826 cstack.cs_lflags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827 ga_init2(&lines_ga, (int)sizeof(wcmd_T), 10);
828
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100829 real_cookie = getline_cookie(fgetline, cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830
831 /* Inside a function use a higher nesting level. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100832 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000833 if (getline_is_func && ex_nesting_level == func_level(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834 ++ex_nesting_level;
835
836 /* Get the function or script name and the address where the next breakpoint
837 * line and the debug tick for a function or script are stored. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000838 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839 {
840 fname = func_name(real_cookie);
841 breakpoint = func_breakpoint(real_cookie);
842 dbg_tick = func_dbg_tick(real_cookie);
843 }
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100844 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000845 {
846 fname = sourcing_name;
847 breakpoint = source_breakpoint(real_cookie);
848 dbg_tick = source_dbg_tick(real_cookie);
849 }
850
851 /*
852 * Initialize "force_abort" and "suppress_errthrow" at the top level.
853 */
854 if (!recursive)
855 {
856 force_abort = FALSE;
857 suppress_errthrow = FALSE;
858 }
859
860 /*
861 * If requested, store and reset the global values controlling the
Bram Moolenaar89d40322006-08-29 15:30:07 +0000862 * exception handling (used when debugging). Otherwise clear it to avoid
863 * a bogus compiler warning when the optimizer uses inline functions...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000864 */
Bram Moolenaarb4872942006-05-13 10:32:52 +0000865 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +0000866 save_dbg_stuff(&debug_saved);
Bram Moolenaar89d40322006-08-29 15:30:07 +0000867 else
Bram Moolenaar7db5fc82010-05-24 11:59:29 +0200868 vim_memset(&debug_saved, 0, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000869
870 initial_trylevel = trylevel;
871
872 /*
873 * "did_throw" will be set to TRUE when an exception is being thrown.
874 */
875 did_throw = FALSE;
876#endif
877 /*
878 * "did_emsg" will be set to TRUE when emsg() is used, in which case we
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000879 * cancel the whole command line, and any if/endif or loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000880 * If force_abort is set, we cancel everything.
881 */
882 did_emsg = FALSE;
883
884 /*
885 * KeyTyped is only set when calling vgetc(). Reset it here when not
886 * calling vgetc() (sourced command lines).
887 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100888 if (!(flags & DOCMD_KEYTYPED)
889 && !getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000890 KeyTyped = FALSE;
891
892 /*
893 * Continue executing command lines:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000894 * - when inside an ":if", ":while" or ":for"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000895 * - for multiple commands on one line, separated with '|'
896 * - when repeating until there are no more lines (for ":source")
897 */
898 next_cmdline = cmdline;
899 do
900 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000901#ifdef FEAT_EVAL
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100902 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000903#endif
904
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000905 /* stop skipping cmds for an error msg after all endif/while/for */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906 if (next_cmdline == NULL
907#ifdef FEAT_EVAL
908 && !force_abort
909 && cstack.cs_idx < 0
Bram Moolenaar05159a02005-02-26 23:04:13 +0000910 && !(getline_is_func && func_has_abort(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000911#endif
912 )
913 did_emsg = FALSE;
914
915 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000916 * 1. If repeating a line in a loop, get a line from lines_ga.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100917 * 2. If no line given: Get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000918 * 3. If a line is given: Make a copy, so we can mess with it.
919 */
920
921#ifdef FEAT_EVAL
922 /* 1. If repeating, get a previous line from lines_ga. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000923 if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000924 {
925 /* Each '|' separated command is stored separately in lines_ga, to
926 * be able to jump to it. Don't use next_cmdline now. */
927 vim_free(cmdline_copy);
928 cmdline_copy = NULL;
929
930 /* Check if a function has returned or, unless it has an unclosed
931 * try conditional, aborted. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000932 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000933 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000934# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000935 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000936 func_line_end(real_cookie);
937# endif
938 if (func_has_ended(real_cookie))
939 {
940 retval = FAIL;
941 break;
942 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000943 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000944#ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000945 else if (do_profiling == PROF_YES
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100946 && getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000947 script_line_end();
948#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949
950 /* Check if a sourced file hit a ":finish" command. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100951 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000952 {
953 retval = FAIL;
954 break;
955 }
956
957 /* If breakpoints have been added/deleted need to check for it. */
958 if (breakpoint != NULL && dbg_tick != NULL
959 && *dbg_tick != debug_tick)
960 {
961 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100962 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000963 fname, sourcing_lnum);
964 *dbg_tick = debug_tick;
965 }
966
967 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
968 sourcing_lnum = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
969
970 /* Did we encounter a breakpoint? */
971 if (breakpoint != NULL && *breakpoint != 0
972 && *breakpoint <= sourcing_lnum)
973 {
974 dbg_breakpoint(fname, sourcing_lnum);
975 /* Find next breakpoint. */
976 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100977 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978 fname, sourcing_lnum);
979 *dbg_tick = debug_tick;
980 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000981# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000982 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000983 {
984 if (getline_is_func)
985 func_line_start(real_cookie);
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100986 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000987 script_line_start();
988 }
989# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990 }
991
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000992 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000994 /* Inside a while/for loop we need to store the lines and use them
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100995 * again. Pass a different "fgetline" function to do_one_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996 * below, so that it stores lines in or reads them from
997 * "lines_ga". Makes it possible to define a function inside a
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000998 * while/for loop. */
999 cmd_getline = get_loop_line;
1000 cmd_cookie = (void *)&cmd_loop_cookie;
1001 cmd_loop_cookie.lines_gap = &lines_ga;
1002 cmd_loop_cookie.current_line = current_line;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001003 cmd_loop_cookie.getline = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001004 cmd_loop_cookie.cookie = cookie;
1005 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006 }
1007 else
1008 {
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001009 cmd_getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010 cmd_cookie = cookie;
1011 }
1012#endif
1013
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001014 /* 2. If no line given, get an allocated line with fgetline(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015 if (next_cmdline == NULL)
1016 {
1017 /*
1018 * Need to set msg_didout for the first line after an ":if",
1019 * otherwise the ":if" will be overwritten.
1020 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001021 if (count == 1 && getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022 msg_didout = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001023 if (fgetline == NULL || (next_cmdline = fgetline(':', cookie,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001024#ifdef FEAT_EVAL
1025 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
1026#else
1027 0
1028#endif
1029 )) == NULL)
1030 {
1031 /* Don't call wait_return for aborted command line. The NULL
1032 * returned for the end of a sourced file or executed function
1033 * doesn't do this. */
1034 if (KeyTyped && !(flags & DOCMD_REPEAT))
1035 need_wait_return = FALSE;
1036 retval = FAIL;
1037 break;
1038 }
1039 used_getline = TRUE;
1040
1041 /*
1042 * Keep the first typed line. Clear it when more lines are typed.
1043 */
1044 if (flags & DOCMD_KEEPLINE)
1045 {
1046 vim_free(repeat_cmdline);
1047 if (count == 0)
1048 repeat_cmdline = vim_strsave(next_cmdline);
1049 else
1050 repeat_cmdline = NULL;
1051 }
1052 }
1053
1054 /* 3. Make a copy of the command so we can mess with it. */
1055 else if (cmdline_copy == NULL)
1056 {
1057 next_cmdline = vim_strsave(next_cmdline);
1058 if (next_cmdline == NULL)
1059 {
1060 EMSG(_(e_outofmem));
1061 retval = FAIL;
1062 break;
1063 }
1064 }
1065 cmdline_copy = next_cmdline;
1066
1067#ifdef FEAT_EVAL
1068 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001069 * Save the current line when inside a ":while" or ":for", and when
1070 * the command looks like a ":while" or ":for", because we may need it
1071 * later. When there is a '|' and another command, it is stored
1072 * separately, because we need to be able to jump back to it from an
1073 * :endwhile/:endfor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001075 if (current_line == lines_ga.ga_len
1076 && (cstack.cs_looplevel || has_loop_cmd(next_cmdline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001078 if (store_loop_line(&lines_ga, next_cmdline) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079 {
1080 retval = FAIL;
1081 break;
1082 }
1083 }
1084 did_endif = FALSE;
1085#endif
1086
1087 if (count++ == 0)
1088 {
1089 /*
1090 * All output from the commands is put below each other, without
1091 * waiting for a return. Don't do this when executing commands
1092 * from a script or when being called recursive (e.g. for ":e
1093 * +command file").
1094 */
1095 if (!(flags & DOCMD_NOWAIT) && !recursive)
1096 {
1097 msg_didout_before_start = msg_didout;
1098 msg_didany = FALSE; /* no output yet */
1099 msg_start();
1100 msg_scroll = TRUE; /* put messages below each other */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001101 ++no_wait_return; /* don't wait for return until finished */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102 ++RedrawingDisabled;
1103 did_inc = TRUE;
1104 }
1105 }
1106
1107 if (p_verbose >= 15 && sourcing_name != NULL)
1108 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109 ++no_wait_return;
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001110 verbose_enter_scroll();
1111
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112 smsg((char_u *)_("line %ld: %s"),
1113 (long)sourcing_lnum, cmdline_copy);
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001114 if (msg_silent == 0)
1115 msg_puts((char_u *)"\n"); /* don't overwrite this */
1116
1117 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118 --no_wait_return;
1119 }
1120
1121 /*
1122 * 2. Execute one '|' separated command.
1123 * do_one_cmd() will return NULL if there is no trailing '|'.
1124 * "cmdline_copy" can change, e.g. for '%' and '#' expansion.
1125 */
1126 ++recursive;
1127 next_cmdline = do_one_cmd(&cmdline_copy, flags & DOCMD_VERBOSE,
1128#ifdef FEAT_EVAL
1129 &cstack,
1130#endif
1131 cmd_getline, cmd_cookie);
1132 --recursive;
1133
1134#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001135 if (cmd_cookie == (void *)&cmd_loop_cookie)
1136 /* Use "current_line" from "cmd_loop_cookie", it may have been
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137 * incremented when defining a function. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001138 current_line = cmd_loop_cookie.current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139#endif
1140
1141 if (next_cmdline == NULL)
1142 {
1143 vim_free(cmdline_copy);
1144 cmdline_copy = NULL;
1145#ifdef FEAT_CMDHIST
1146 /*
1147 * If the command was typed, remember it for the ':' register.
1148 * Do this AFTER executing the command to make :@: work.
1149 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001150 if (getline_equal(fgetline, cookie, getexline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001151 && new_last_cmdline != NULL)
1152 {
1153 vim_free(last_cmdline);
1154 last_cmdline = new_last_cmdline;
1155 new_last_cmdline = NULL;
1156 }
1157#endif
1158 }
1159 else
1160 {
1161 /* need to copy the command after the '|' to cmdline_copy, for the
1162 * next do_one_cmd() */
Bram Moolenaara7241f52008-06-24 20:39:31 +00001163 STRMOVE(cmdline_copy, next_cmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001164 next_cmdline = cmdline_copy;
1165 }
1166
1167
1168#ifdef FEAT_EVAL
1169 /* reset did_emsg for a function that is not aborted by an error */
1170 if (did_emsg && !force_abort
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001171 && getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172 && !func_has_abort(real_cookie))
1173 did_emsg = FALSE;
1174
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001175 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001176 {
1177 ++current_line;
1178
1179 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001180 * An ":endwhile", ":endfor" and ":continue" is handled here.
1181 * If we were executing commands, jump back to the ":while" or
1182 * ":for".
1183 * If we were not executing commands, decrement cs_looplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001185 if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001187 cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001189 /* Jump back to the matching ":while" or ":for". Be careful
1190 * not to use a cs_line[] from an entry that isn't a ":while"
1191 * or ":for": It would make "current_line" invalid and can
1192 * cause a crash. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193 if (!did_emsg && !got_int && !did_throw
1194 && cstack.cs_idx >= 0
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001195 && (cstack.cs_flags[cstack.cs_idx]
1196 & (CSF_WHILE | CSF_FOR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197 && cstack.cs_line[cstack.cs_idx] >= 0
1198 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
1199 {
1200 current_line = cstack.cs_line[cstack.cs_idx];
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001201 /* remember we jumped there */
1202 cstack.cs_lflags |= CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203 line_breakcheck(); /* check if CTRL-C typed */
1204
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001205 /* Check for the next breakpoint at or after the ":while"
1206 * or ":for". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207 if (breakpoint != NULL)
1208 {
1209 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001210 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211 fname,
1212 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
1213 *dbg_tick = debug_tick;
1214 }
1215 }
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001216 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001217 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001218 /* can only get here with ":endwhile" or ":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219 if (cstack.cs_idx >= 0)
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001220 rewind_conditionals(&cstack, cstack.cs_idx - 1,
1221 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222 }
1223 }
1224
1225 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001226 * For a ":while" or ":for" we need to remember the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001227 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001228 else if (cstack.cs_lflags & CSL_HAD_LOOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001229 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001230 cstack.cs_lflags &= ~CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001231 cstack.cs_line[cstack.cs_idx] = current_line - 1;
1232 }
1233 }
1234
1235 /*
1236 * When not inside any ":while" loop, clear remembered lines.
1237 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001238 if (cstack.cs_looplevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239 {
1240 if (lines_ga.ga_len > 0)
1241 {
1242 sourcing_lnum =
1243 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
1244 free_cmdlines(&lines_ga);
1245 }
1246 current_line = 0;
1247 }
1248
1249 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001250 * A ":finally" makes did_emsg, got_int, and did_throw pending for
1251 * being restored at the ":endtry". Reset them here and set the
1252 * ACTIVE and FINALLY flags, so that the finally clause gets executed.
1253 * This includes the case where a missing ":endif", ":endwhile" or
1254 * ":endfor" was detected by the ":finally" itself.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001256 if (cstack.cs_lflags & CSL_HAD_FINA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001257 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001258 cstack.cs_lflags &= ~CSL_HAD_FINA;
1259 report_make_pending(cstack.cs_pending[cstack.cs_idx]
1260 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261 did_throw ? (void *)current_exception : NULL);
1262 did_emsg = got_int = did_throw = FALSE;
1263 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
1264 }
1265
1266 /* Update global "trylevel" for recursive calls to do_cmdline() from
1267 * within this loop. */
1268 trylevel = initial_trylevel + cstack.cs_trylevel;
1269
1270 /*
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001271 * If the outermost try conditional (across function calls and sourced
Bram Moolenaar071d4272004-06-13 20:20:40 +00001272 * files) is aborted because of an error, an interrupt, or an uncaught
1273 * exception, cancel everything. If it is left normally, reset
1274 * force_abort to get the non-EH compatible abortion behavior for
1275 * the rest of the script.
1276 */
1277 if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
1278 force_abort = FALSE;
1279
1280 /* Convert an interrupt to an exception if appropriate. */
1281 (void)do_intthrow(&cstack);
1282#endif /* FEAT_EVAL */
1283
1284 }
1285 /*
1286 * Continue executing command lines when:
1287 * - no CTRL-C typed, no aborting error, no exception thrown or try
1288 * conditionals need to be checked for executing finally clauses or
1289 * catching an interrupt exception
1290 * - didn't get an error message or lines are not typed
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001291 * - there is a command after '|', inside a :if, :while, :for or :try, or
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292 * looping for ":source" command or function call.
1293 */
1294 while (!((got_int
1295#ifdef FEAT_EVAL
1296 || (did_emsg && force_abort) || did_throw
1297#endif
1298 )
1299#ifdef FEAT_EVAL
1300 && cstack.cs_trylevel == 0
1301#endif
1302 )
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001303 && !(did_emsg
1304#ifdef FEAT_EVAL
1305 /* Keep going when inside try/catch, so that the error can be
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001306 * deal with, except when it is a syntax error, it may cause
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001307 * the :endtry to be missed. */
1308 && (cstack.cs_trylevel == 0 || did_emsg_syntax)
1309#endif
1310 && used_getline
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001311 && (getline_equal(fgetline, cookie, getexmodeline)
1312 || getline_equal(fgetline, cookie, getexline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313 && (next_cmdline != NULL
1314#ifdef FEAT_EVAL
1315 || cstack.cs_idx >= 0
1316#endif
1317 || (flags & DOCMD_REPEAT)));
1318
1319 vim_free(cmdline_copy);
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001320 did_emsg_syntax = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321#ifdef FEAT_EVAL
1322 free_cmdlines(&lines_ga);
1323 ga_clear(&lines_ga);
1324
1325 if (cstack.cs_idx >= 0)
1326 {
1327 /*
1328 * If a sourced file or executed function ran to its end, report the
1329 * unclosed conditional.
1330 */
1331 if (!got_int && !did_throw
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001332 && ((getline_equal(fgetline, cookie, getsourceline)
1333 && !source_finished(fgetline, cookie))
1334 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335 && !func_has_ended(real_cookie))))
1336 {
1337 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
1338 EMSG(_(e_endtry));
1339 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
1340 EMSG(_(e_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001341 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
1342 EMSG(_(e_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343 else
1344 EMSG(_(e_endif));
1345 }
1346
1347 /*
1348 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1349 * ":endtry" in a sourced file or executed function. If the try
1350 * conditional is in its finally clause, ignore anything pending.
1351 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001352 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353 */
1354 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001355 {
1356 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1357
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001358 if (idx >= 0)
1359 --idx; /* remove try block not in its finally clause */
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001360 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1361 &cstack.cs_looplevel);
1362 }
1363 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364 trylevel = initial_trylevel;
1365 }
1366
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001367 /* If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1368 * lack was reported above and the error message is to be converted to an
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369 * exception, do this now after rewinding the cstack. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001370 do_errthrow(&cstack, getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371 ? (char_u *)"endfunction" : (char_u *)NULL);
1372
1373 if (trylevel == 0)
1374 {
1375 /*
1376 * When an exception is being thrown out of the outermost try
1377 * conditional, discard the uncaught exception, disable the conversion
1378 * of interrupts or errors to exceptions, and ensure that no more
1379 * commands are executed.
1380 */
1381 if (did_throw)
1382 {
1383 void *p = NULL;
1384 char_u *saved_sourcing_name;
1385 int saved_sourcing_lnum;
1386 struct msglist *messages = NULL, *next;
1387
1388 /*
1389 * If the uncaught exception is a user exception, report it as an
1390 * error. If it is an error exception, display the saved error
1391 * message now. For an interrupt exception, do nothing; the
1392 * interrupt message is given elsewhere.
1393 */
1394 switch (current_exception->type)
1395 {
1396 case ET_USER:
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001397 vim_snprintf((char *)IObuff, IOSIZE,
1398 _("E605: Exception not caught: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001399 current_exception->value);
1400 p = vim_strsave(IObuff);
1401 break;
1402 case ET_ERROR:
1403 messages = current_exception->messages;
1404 current_exception->messages = NULL;
1405 break;
1406 case ET_INTERRUPT:
1407 break;
1408 default:
1409 p = vim_strsave((char_u *)_(e_internal));
1410 }
1411
1412 saved_sourcing_name = sourcing_name;
1413 saved_sourcing_lnum = sourcing_lnum;
1414 sourcing_name = current_exception->throw_name;
1415 sourcing_lnum = current_exception->throw_lnum;
1416 current_exception->throw_name = NULL;
1417
1418 discard_current_exception(); /* uses IObuff if 'verbose' */
1419 suppress_errthrow = TRUE;
1420 force_abort = TRUE;
1421
1422 if (messages != NULL)
1423 {
1424 do
1425 {
1426 next = messages->next;
1427 emsg(messages->msg);
1428 vim_free(messages->msg);
1429 vim_free(messages);
1430 messages = next;
1431 }
1432 while (messages != NULL);
1433 }
1434 else if (p != NULL)
1435 {
1436 emsg(p);
1437 vim_free(p);
1438 }
1439 vim_free(sourcing_name);
1440 sourcing_name = saved_sourcing_name;
1441 sourcing_lnum = saved_sourcing_lnum;
1442 }
1443
1444 /*
1445 * On an interrupt or an aborting error not converted to an exception,
1446 * disable the conversion of errors to exceptions. (Interrupts are not
1447 * converted any more, here.) This enables also the interrupt message
1448 * when force_abort is set and did_emsg unset in case of an interrupt
1449 * from a finally clause after an error.
1450 */
1451 else if (got_int || (did_emsg && force_abort))
1452 suppress_errthrow = TRUE;
1453 }
1454
1455 /*
1456 * The current cstack will be freed when do_cmdline() returns. An uncaught
1457 * exception will have to be rethrown in the previous cstack. If a function
1458 * has just returned or a script file was just finished and the previous
1459 * cstack belongs to the same function or, respectively, script file, it
1460 * will have to be checked for finally clauses to be executed due to the
1461 * ":return" or ":finish". This is done in do_one_cmd().
1462 */
1463 if (did_throw)
1464 need_rethrow = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001465 if ((getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466 && ex_nesting_level > source_level(real_cookie))
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001467 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001468 && ex_nesting_level > func_level(real_cookie) + 1))
1469 {
1470 if (!did_throw)
1471 check_cstack = TRUE;
1472 }
1473 else
1474 {
1475 /* When leaving a function, reduce nesting level. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001476 if (getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001477 --ex_nesting_level;
1478 /*
1479 * Go to debug mode when returning from a function in which we are
1480 * single-stepping.
1481 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001482 if ((getline_equal(fgetline, cookie, getsourceline)
1483 || getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484 && ex_nesting_level + 1 <= debug_break_level)
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001485 do_debug(getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001486 ? (char_u *)_("End of sourced file")
1487 : (char_u *)_("End of function"));
1488 }
1489
1490 /*
1491 * Restore the exception environment (done after returning from the
1492 * debugger).
1493 */
1494 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001495 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001496
1497 msg_list = saved_msg_list;
1498#endif /* FEAT_EVAL */
1499
1500 /*
1501 * If there was too much output to fit on the command line, ask the user to
1502 * hit return before redrawing the screen. With the ":global" command we do
1503 * this only once after the command is finished.
1504 */
1505 if (did_inc)
1506 {
1507 --RedrawingDisabled;
1508 --no_wait_return;
1509 msg_scroll = FALSE;
1510
1511 /*
1512 * When just finished an ":if"-":else" which was typed, no need to
1513 * wait for hit-return. Also for an error situation.
1514 */
1515 if (retval == FAIL
1516#ifdef FEAT_EVAL
1517 || (did_endif && KeyTyped && !did_emsg)
1518#endif
1519 )
1520 {
1521 need_wait_return = FALSE;
1522 msg_didany = FALSE; /* don't wait when restarting edit */
1523 }
1524 else if (need_wait_return)
1525 {
1526 /*
1527 * The msg_start() above clears msg_didout. The wait_return we do
1528 * here should not overwrite the command that may be shown before
1529 * doing that.
1530 */
1531 msg_didout |= msg_didout_before_start;
1532 wait_return(FALSE);
1533 }
1534 }
1535
Bram Moolenaar2a942252012-11-28 23:03:07 +01001536#ifdef FEAT_EVAL
1537 did_endif = FALSE; /* in case do_cmdline used recursively */
1538#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001539 /*
1540 * Reset if_level, in case a sourced script file contains more ":if" than
1541 * ":endif" (could be ":if x | foo | endif").
1542 */
1543 if_level = 0;
Bram Moolenaar2e18a122012-11-28 19:10:54 +01001544#endif
Bram Moolenaard4ad0d42012-11-28 17:34:48 +01001545
Bram Moolenaar071d4272004-06-13 20:20:40 +00001546 --call_depth;
1547 return retval;
1548}
1549
1550#ifdef FEAT_EVAL
1551/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001552 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001553 */
1554 static char_u *
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001555get_loop_line(c, cookie, indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001556 int c;
1557 void *cookie;
1558 int indent;
1559{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001560 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561 wcmd_T *wp;
1562 char_u *line;
1563
1564 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1565 {
1566 if (cp->repeating)
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001567 return NULL; /* trying to read past ":endwhile"/":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001568
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001569 /* First time inside the ":while"/":for": get line normally. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570 if (cp->getline == NULL)
1571 line = getcmdline(c, 0L, indent);
1572 else
1573 line = cp->getline(c, cp->cookie, indent);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001574 if (line != NULL && store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575 ++cp->current_line;
1576
1577 return line;
1578 }
1579
1580 KeyTyped = FALSE;
1581 ++cp->current_line;
1582 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
1583 sourcing_lnum = wp->lnum;
1584 return vim_strsave(wp->line);
1585}
1586
1587/*
1588 * Store a line in "gap" so that a ":while" loop can execute it again.
1589 */
1590 static int
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001591store_loop_line(gap, line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592 garray_T *gap;
1593 char_u *line;
1594{
1595 if (ga_grow(gap, 1) == FAIL)
1596 return FAIL;
1597 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
1598 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = sourcing_lnum;
1599 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001600 return OK;
1601}
1602
1603/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001604 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001605 */
1606 static void
1607free_cmdlines(gap)
1608 garray_T *gap;
1609{
1610 while (gap->ga_len > 0)
1611 {
1612 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1613 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001614 }
1615}
1616#endif
1617
1618/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001619 * If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals
1620 * "func". * Otherwise return TRUE when "fgetline" equals "func".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001622 int
Bram Moolenaar89d40322006-08-29 15:30:07 +00001623getline_equal(fgetline, cookie, func)
1624 char_u *(*fgetline) __ARGS((int, void *, int));
Bram Moolenaar78a15312009-05-15 19:33:18 +00001625 void *cookie UNUSED; /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626 char_u *(*func) __ARGS((int, void *, int));
1627{
1628#ifdef FEAT_EVAL
1629 char_u *(*gp) __ARGS((int, void *, int));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001630 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631
Bram Moolenaar89d40322006-08-29 15:30:07 +00001632 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001633 * function that's originally used to obtain the lines. This may be
1634 * nested several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001635 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001636 cp = (struct loop_cookie *)cookie;
1637 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638 {
1639 gp = cp->getline;
1640 cp = cp->cookie;
1641 }
1642 return gp == func;
1643#else
Bram Moolenaar89d40322006-08-29 15:30:07 +00001644 return fgetline == func;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001645#endif
1646}
1647
1648#if defined(FEAT_EVAL) || defined(FEAT_MBYTE) || defined(PROTO)
1649/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001650 * If "fgetline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651 * getline function. Otherwise return "cookie".
1652 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653 void *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001654getline_cookie(fgetline, cookie)
Bram Moolenaar78a15312009-05-15 19:33:18 +00001655 char_u *(*fgetline) __ARGS((int, void *, int)) UNUSED;
Bram Moolenaar89d40322006-08-29 15:30:07 +00001656 void *cookie; /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001657{
1658# ifdef FEAT_EVAL
1659 char_u *(*gp) __ARGS((int, void *, int));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001660 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661
Bram Moolenaar89d40322006-08-29 15:30:07 +00001662 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001663 * cookie that's originally used to obtain the lines. This may be nested
Bram Moolenaar071d4272004-06-13 20:20:40 +00001664 * several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001665 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001666 cp = (struct loop_cookie *)cookie;
1667 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001668 {
1669 gp = cp->getline;
1670 cp = cp->cookie;
1671 }
1672 return cp;
1673# else
1674 return cookie;
1675# endif
1676}
1677#endif
1678
1679/*
1680 * Execute one Ex command.
1681 *
1682 * If 'sourcing' is TRUE, the command will be included in the error message.
1683 *
1684 * 1. skip comment lines and leading space
1685 * 2. handle command modifiers
1686 * 3. parse range
1687 * 4. parse command
1688 * 5. parse arguments
1689 * 6. switch on command name
1690 *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001691 * Note: "fgetline" can be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001692 *
1693 * This function may be called recursively!
1694 */
1695#if (_MSC_VER == 1200)
1696/*
Bram Moolenaared203462004-06-16 11:19:22 +00001697 * Avoid optimisation bug in VC++ version 6.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001698 */
Bram Moolenaar281bdce2005-01-25 21:53:18 +00001699 #pragma optimize( "g", off )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001700#endif
1701 static char_u *
1702do_one_cmd(cmdlinep, sourcing,
1703#ifdef FEAT_EVAL
1704 cstack,
1705#endif
Bram Moolenaar89d40322006-08-29 15:30:07 +00001706 fgetline, cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001707 char_u **cmdlinep;
1708 int sourcing;
1709#ifdef FEAT_EVAL
1710 struct condstack *cstack;
1711#endif
Bram Moolenaar89d40322006-08-29 15:30:07 +00001712 char_u *(*fgetline) __ARGS((int, void *, int));
1713 void *cookie; /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714{
1715 char_u *p;
1716 linenr_T lnum;
1717 long n;
1718 char_u *errormsg = NULL; /* error message */
1719 exarg_T ea; /* Ex command arguments */
1720 long verbose_save = -1;
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001721 int save_msg_scroll = msg_scroll;
1722 int save_msg_silent = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723 int did_esilent = 0;
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001724#ifdef HAVE_SANDBOX
1725 int did_sandbox = FALSE;
1726#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727 cmdmod_T save_cmdmod;
1728 int ni; /* set when Not Implemented */
1729
1730 vim_memset(&ea, 0, sizeof(ea));
1731 ea.line1 = 1;
1732 ea.line2 = 1;
1733#ifdef FEAT_EVAL
1734 ++ex_nesting_level;
1735#endif
1736
Bram Moolenaar21691f82012-12-05 19:13:18 +01001737 /* When the last file has not been edited :q has to be typed twice. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738 if (quitmore
1739#ifdef FEAT_EVAL
1740 /* avoid that a function call in 'statusline' does this */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001741 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001742#endif
1743#ifdef FEAT_AUTOCMD
Bram Moolenaar21691f82012-12-05 19:13:18 +01001744 /* avoid that an autocommand, e.g. QuitPre, does this */
1745 && !getline_equal(fgetline, cookie, getnextac)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001746#endif
1747 )
1748 --quitmore;
1749
1750 /*
1751 * Reset browse, confirm, etc.. They are restored when returning, for
1752 * recursive calls.
1753 */
1754 save_cmdmod = cmdmod;
1755 vim_memset(&cmdmod, 0, sizeof(cmdmod));
1756
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001757 /* "#!anything" is handled like a comment. */
1758 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1759 goto doend;
1760
Bram Moolenaar071d4272004-06-13 20:20:40 +00001761 /*
1762 * Repeat until no more command modifiers are found.
1763 */
1764 ea.cmd = *cmdlinep;
1765 for (;;)
1766 {
1767/*
1768 * 1. skip comment lines and leading white space and colons
1769 */
1770 while (*ea.cmd == ' ' || *ea.cmd == '\t' || *ea.cmd == ':')
1771 ++ea.cmd;
1772
1773 /* in ex mode, an empty line works like :+ */
1774 if (*ea.cmd == NUL && exmode_active
Bram Moolenaar89d40322006-08-29 15:30:07 +00001775 && (getline_equal(fgetline, cookie, getexmodeline)
1776 || getline_equal(fgetline, cookie, getexline))
Bram Moolenaardf177f62005-02-22 08:39:57 +00001777 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778 {
1779 ea.cmd = (char_u *)"+";
1780 ex_pressedreturn = TRUE;
1781 }
1782
1783 /* ignore comment and empty lines */
Bram Moolenaarf998c042007-11-20 11:31:26 +00001784 if (*ea.cmd == '"')
1785 goto doend;
1786 if (*ea.cmd == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001787 {
1788 ex_pressedreturn = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001790 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791
1792/*
1793 * 2. handle command modifiers.
1794 */
1795 p = ea.cmd;
1796 if (VIM_ISDIGIT(*ea.cmd))
1797 p = skipwhite(skipdigits(ea.cmd));
1798 switch (*p)
1799 {
1800 /* When adding an entry, also modify cmd_exists(). */
1801 case 'a': if (!checkforcmd(&ea.cmd, "aboveleft", 3))
1802 break;
1803#ifdef FEAT_WINDOWS
1804 cmdmod.split |= WSP_ABOVE;
1805#endif
1806 continue;
1807
1808 case 'b': if (checkforcmd(&ea.cmd, "belowright", 3))
1809 {
1810#ifdef FEAT_WINDOWS
1811 cmdmod.split |= WSP_BELOW;
1812#endif
1813 continue;
1814 }
1815 if (checkforcmd(&ea.cmd, "browse", 3))
1816 {
Bram Moolenaard812df62008-11-09 12:46:09 +00001817#ifdef FEAT_BROWSE_CMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818 cmdmod.browse = TRUE;
1819#endif
1820 continue;
1821 }
1822 if (!checkforcmd(&ea.cmd, "botright", 2))
1823 break;
1824#ifdef FEAT_WINDOWS
1825 cmdmod.split |= WSP_BOT;
1826#endif
1827 continue;
1828
1829 case 'c': if (!checkforcmd(&ea.cmd, "confirm", 4))
1830 break;
1831#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1832 cmdmod.confirm = TRUE;
1833#endif
1834 continue;
1835
1836 case 'k': if (checkforcmd(&ea.cmd, "keepmarks", 3))
1837 {
1838 cmdmod.keepmarks = TRUE;
1839 continue;
1840 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001841 if (checkforcmd(&ea.cmd, "keepalt", 5))
1842 {
1843 cmdmod.keepalt = TRUE;
1844 continue;
1845 }
Bram Moolenaara939e432013-11-09 05:30:26 +01001846 if (checkforcmd(&ea.cmd, "keeppatterns", 5))
1847 {
1848 cmdmod.keeppatterns = TRUE;
1849 continue;
1850 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851 if (!checkforcmd(&ea.cmd, "keepjumps", 5))
1852 break;
1853 cmdmod.keepjumps = TRUE;
1854 continue;
1855
1856 /* ":hide" and ":hide | cmd" are not modifiers */
1857 case 'h': if (p != ea.cmd || !checkforcmd(&p, "hide", 3)
1858 || *p == NUL || ends_excmd(*p))
1859 break;
1860 ea.cmd = p;
1861 cmdmod.hide = TRUE;
1862 continue;
1863
1864 case 'l': if (checkforcmd(&ea.cmd, "lockmarks", 3))
1865 {
1866 cmdmod.lockmarks = TRUE;
1867 continue;
1868 }
1869
1870 if (!checkforcmd(&ea.cmd, "leftabove", 5))
1871 break;
1872#ifdef FEAT_WINDOWS
1873 cmdmod.split |= WSP_ABOVE;
1874#endif
1875 continue;
1876
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001877 case 'n': if (!checkforcmd(&ea.cmd, "noautocmd", 3))
1878 break;
1879#ifdef FEAT_AUTOCMD
1880 if (cmdmod.save_ei == NULL)
1881 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001882 /* Set 'eventignore' to "all". Restore the
1883 * existing option value later. */
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001884 cmdmod.save_ei = vim_strsave(p_ei);
1885 set_string_option_direct((char_u *)"ei", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001886 (char_u *)"all", OPT_FREE, SID_NONE);
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001887 }
1888#endif
1889 continue;
1890
Bram Moolenaar071d4272004-06-13 20:20:40 +00001891 case 'r': if (!checkforcmd(&ea.cmd, "rightbelow", 6))
1892 break;
1893#ifdef FEAT_WINDOWS
1894 cmdmod.split |= WSP_BELOW;
1895#endif
1896 continue;
1897
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001898 case 's': if (checkforcmd(&ea.cmd, "sandbox", 3))
1899 {
1900#ifdef HAVE_SANDBOX
1901 if (!did_sandbox)
1902 ++sandbox;
1903 did_sandbox = TRUE;
1904#endif
1905 continue;
1906 }
1907 if (!checkforcmd(&ea.cmd, "silent", 3))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001908 break;
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001909 if (save_msg_silent == -1)
1910 save_msg_silent = msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001911 ++msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912 if (*ea.cmd == '!' && !vim_iswhite(ea.cmd[-1]))
1913 {
1914 /* ":silent!", but not "silent !cmd" */
1915 ea.cmd = skipwhite(ea.cmd + 1);
1916 ++emsg_silent;
1917 ++did_esilent;
1918 }
1919 continue;
1920
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001921 case 't': if (checkforcmd(&p, "tab", 3))
1922 {
1923#ifdef FEAT_WINDOWS
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001924 if (vim_isdigit(*ea.cmd))
1925 cmdmod.tab = atoi((char *)ea.cmd) + 1;
1926 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001927 cmdmod.tab = tabpage_index(curtab) + 1;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001928 ea.cmd = p;
1929#endif
1930 continue;
1931 }
1932 if (!checkforcmd(&ea.cmd, "topleft", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001933 break;
1934#ifdef FEAT_WINDOWS
1935 cmdmod.split |= WSP_TOP;
1936#endif
1937 continue;
1938
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001939 case 'u': if (!checkforcmd(&ea.cmd, "unsilent", 3))
1940 break;
1941 if (save_msg_silent == -1)
1942 save_msg_silent = msg_silent;
1943 msg_silent = 0;
1944 continue;
1945
Bram Moolenaar071d4272004-06-13 20:20:40 +00001946 case 'v': if (checkforcmd(&ea.cmd, "vertical", 4))
1947 {
1948#ifdef FEAT_VERTSPLIT
1949 cmdmod.split |= WSP_VERT;
1950#endif
1951 continue;
1952 }
1953 if (!checkforcmd(&p, "verbose", 4))
1954 break;
1955 if (verbose_save < 0)
1956 verbose_save = p_verbose;
Bram Moolenaared203462004-06-16 11:19:22 +00001957 if (vim_isdigit(*ea.cmd))
1958 p_verbose = atoi((char *)ea.cmd);
1959 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001960 p_verbose = 1;
1961 ea.cmd = p;
1962 continue;
1963 }
1964 break;
1965 }
1966
1967#ifdef FEAT_EVAL
1968 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
1969 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
1970#else
1971 ea.skip = (if_level > 0);
1972#endif
1973
1974#ifdef FEAT_EVAL
Bram Moolenaar05159a02005-02-26 23:04:13 +00001975# ifdef FEAT_PROFILE
1976 /* Count this line for profiling if ea.skip is FALSE. */
Bram Moolenaar371d5402006-03-20 21:47:49 +00001977 if (do_profiling == PROF_YES && !ea.skip)
Bram Moolenaar05159a02005-02-26 23:04:13 +00001978 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001979 if (getline_equal(fgetline, cookie, get_func_line))
1980 func_line_exec(getline_cookie(fgetline, cookie));
1981 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +00001982 script_line_exec();
1983 }
1984#endif
1985
Bram Moolenaar071d4272004-06-13 20:20:40 +00001986 /* May go to debug mode. If this happens and the ">quit" debug command is
1987 * used, throw an interrupt exception and skip the next command. */
1988 dbg_check_breakpoint(&ea);
1989 if (!ea.skip && got_int)
1990 {
1991 ea.skip = TRUE;
1992 (void)do_intthrow(cstack);
1993 }
1994#endif
1995
1996/*
1997 * 3. parse a range specifier of the form: addr [,addr] [;addr] ..
1998 *
1999 * where 'addr' is:
2000 *
2001 * % (entire file)
2002 * $ [+-NUM]
2003 * 'x [+-NUM] (where x denotes a currently defined mark)
2004 * . [+-NUM]
2005 * [+-NUM]..
2006 * NUM
2007 *
2008 * The ea.cmd pointer is updated to point to the first character following the
2009 * range spec. If an initial address is found, but no second, the upper bound
2010 * is equal to the lower.
2011 */
2012
2013 /* repeat for all ',' or ';' separated addresses */
2014 for (;;)
2015 {
2016 ea.line1 = ea.line2;
2017 ea.line2 = curwin->w_cursor.lnum; /* default is current line number */
2018 ea.cmd = skipwhite(ea.cmd);
2019 lnum = get_address(&ea.cmd, ea.skip, ea.addr_count == 0);
2020 if (ea.cmd == NULL) /* error detected */
2021 goto doend;
2022 if (lnum == MAXLNUM)
2023 {
2024 if (*ea.cmd == '%') /* '%' - all lines */
2025 {
2026 ++ea.cmd;
2027 ea.line1 = 1;
2028 ea.line2 = curbuf->b_ml.ml_line_count;
2029 ++ea.addr_count;
2030 }
2031 /* '*' - visual area */
2032 else if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
2033 {
2034 pos_T *fp;
2035
2036 ++ea.cmd;
2037 if (!ea.skip)
2038 {
2039 fp = getmark('<', FALSE);
2040 if (check_mark(fp) == FAIL)
2041 goto doend;
2042 ea.line1 = fp->lnum;
2043 fp = getmark('>', FALSE);
2044 if (check_mark(fp) == FAIL)
2045 goto doend;
2046 ea.line2 = fp->lnum;
2047 ++ea.addr_count;
2048 }
2049 }
2050 }
2051 else
2052 ea.line2 = lnum;
2053 ea.addr_count++;
2054
2055 if (*ea.cmd == ';')
2056 {
2057 if (!ea.skip)
2058 curwin->w_cursor.lnum = ea.line2;
2059 }
2060 else if (*ea.cmd != ',')
2061 break;
2062 ++ea.cmd;
2063 }
2064
2065 /* One address given: set start and end lines */
2066 if (ea.addr_count == 1)
2067 {
2068 ea.line1 = ea.line2;
2069 /* ... but only implicit: really no address given */
2070 if (lnum == MAXLNUM)
2071 ea.addr_count = 0;
2072 }
2073
2074 /* Don't leave the cursor on an illegal line (caused by ';') */
2075 check_cursor_lnum();
2076
2077/*
2078 * 4. parse command
2079 */
2080
2081 /*
2082 * Skip ':' and any white space
2083 */
2084 ea.cmd = skipwhite(ea.cmd);
2085 while (*ea.cmd == ':')
2086 ea.cmd = skipwhite(ea.cmd + 1);
2087
2088 /*
2089 * If we got a line, but no command, then go to the line.
2090 * If we find a '|' or '\n' we set ea.nextcmd.
2091 */
2092 if (*ea.cmd == NUL || *ea.cmd == '"' ||
2093 (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
2094 {
2095 /*
2096 * strange vi behaviour:
2097 * ":3" jumps to line 3
2098 * ":3|..." prints line 3
2099 * ":|" prints current line
2100 */
2101 if (ea.skip) /* skip this if inside :if */
2102 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00002103 if (*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002104 {
2105 ea.cmdidx = CMD_print;
2106 ea.argt = RANGE+COUNT+TRLBAR;
2107 if ((errormsg = invalid_range(&ea)) == NULL)
2108 {
2109 correct_range(&ea);
2110 ex_print(&ea);
2111 }
2112 }
2113 else if (ea.addr_count != 0)
2114 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002115 if (ea.line2 > curbuf->b_ml.ml_line_count)
2116 {
2117 /* With '-' in 'cpoptions' a line number past the file is an
2118 * error, otherwise put it at the end of the file. */
2119 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
2120 ea.line2 = -1;
2121 else
2122 ea.line2 = curbuf->b_ml.ml_line_count;
2123 }
2124
2125 if (ea.line2 < 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002126 errormsg = (char_u *)_(e_invrange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002127 else
2128 {
2129 if (ea.line2 == 0)
2130 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002131 else
2132 curwin->w_cursor.lnum = ea.line2;
2133 beginline(BL_SOL | BL_FIX);
2134 }
2135 }
2136 goto doend;
2137 }
2138
2139 /* Find the command and let "p" point to after it. */
2140 p = find_command(&ea, NULL);
2141
2142#ifdef FEAT_USR_CMDS
2143 if (p == NULL)
2144 {
2145 if (!ea.skip)
2146 errormsg = (char_u *)_("E464: Ambiguous use of user-defined command");
2147 goto doend;
2148 }
2149 /* Check for wrong commands. */
2150 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78)
2151 {
2152 errormsg = uc_fun_cmd();
2153 goto doend;
2154 }
2155#endif
2156 if (ea.cmdidx == CMD_SIZE)
2157 {
2158 if (!ea.skip)
2159 {
2160 STRCPY(IObuff, _("E492: Not an editor command"));
2161 if (!sourcing)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002162 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163 errormsg = IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02002164 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165 }
2166 goto doend;
2167 }
2168
2169 ni = (
2170#ifdef FEAT_USR_CMDS
2171 !USER_CMDIDX(ea.cmdidx) &&
2172#endif
Bram Moolenaar3ebc1e52007-07-05 07:54:17 +00002173 (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00002174#ifdef HAVE_EX_SCRIPT_NI
2175 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
2176#endif
2177 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178
2179#ifndef FEAT_EVAL
2180 /*
2181 * When the expression evaluation is disabled, recognize the ":if" and
2182 * ":endif" commands and ignore everything in between it.
2183 */
2184 if (ea.cmdidx == CMD_if)
2185 ++if_level;
2186 if (if_level)
2187 {
2188 if (ea.cmdidx == CMD_endif)
2189 --if_level;
2190 goto doend;
2191 }
2192
2193#endif
2194
Bram Moolenaar196b3b02008-06-20 16:51:41 +00002195 /* forced commands */
2196 if (*p == '!' && ea.cmdidx != CMD_substitute
2197 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002198 {
2199 ++p;
2200 ea.forceit = TRUE;
2201 }
2202 else
2203 ea.forceit = FALSE;
2204
2205/*
2206 * 5. parse arguments
2207 */
2208#ifdef FEAT_USR_CMDS
2209 if (!USER_CMDIDX(ea.cmdidx))
2210#endif
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002211 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002212
2213 if (!ea.skip)
2214 {
2215#ifdef HAVE_SANDBOX
2216 if (sandbox != 0 && !(ea.argt & SBOXOK))
2217 {
2218 /* Command not allowed in sandbox. */
2219 errormsg = (char_u *)_(e_sandbox);
2220 goto doend;
2221 }
2222#endif
2223 if (!curbuf->b_p_ma && (ea.argt & MODIFY))
2224 {
2225 /* Command not allowed in non-'modifiable' buffer */
2226 errormsg = (char_u *)_(e_modifiable);
2227 goto doend;
2228 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002229
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002230 if (text_locked() && !(ea.argt & CMDWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231# ifdef FEAT_USR_CMDS
2232 && !USER_CMDIDX(ea.cmdidx)
2233# endif
2234 )
2235 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002236 /* Command not allowed when editing the command line. */
2237#ifdef FEAT_CMDWIN
2238 if (cmdwin_type != 0)
2239 errormsg = (char_u *)_(e_cmdwin);
2240 else
2241#endif
2242 errormsg = (char_u *)_(e_secure);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002243 goto doend;
2244 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002245#ifdef FEAT_AUTOCMD
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002246 /* Disallow editing another buffer when "curbuf_lock" is set.
2247 * Do allow ":edit" (check for argument later).
2248 * Do allow ":checktime" (it's postponed). */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002249 if (!(ea.argt & CMDWIN)
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002250 && ea.cmdidx != CMD_edit
2251 && ea.cmdidx != CMD_checktime
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002252# ifdef FEAT_USR_CMDS
2253 && !USER_CMDIDX(ea.cmdidx)
2254# endif
2255 && curbuf_locked())
2256 goto doend;
2257#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258
2259 if (!ni && !(ea.argt & RANGE) && ea.addr_count > 0)
2260 {
2261 /* no range allowed */
2262 errormsg = (char_u *)_(e_norange);
2263 goto doend;
2264 }
2265 }
2266
2267 if (!ni && !(ea.argt & BANG) && ea.forceit) /* no <!> allowed */
2268 {
2269 errormsg = (char_u *)_(e_nobang);
2270 goto doend;
2271 }
2272
2273 /*
2274 * Don't complain about the range if it is not used
2275 * (could happen if line_count is accidentally set to 0).
2276 */
2277 if (!ea.skip && !ni)
2278 {
2279 /*
2280 * If the range is backwards, ask for confirmation and, if given, swap
2281 * ea.line1 & ea.line2 so it's forwards again.
2282 * When global command is busy, don't ask, will fail below.
2283 */
2284 if (!global_busy && ea.line1 > ea.line2)
2285 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002286 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002287 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002288 if (sourcing || exmode_active)
2289 {
2290 errormsg = (char_u *)_("E493: Backwards range given");
2291 goto doend;
2292 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002293 if (ask_yesno((char_u *)
2294 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002295 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002296 }
2297 lnum = ea.line1;
2298 ea.line1 = ea.line2;
2299 ea.line2 = lnum;
2300 }
2301 if ((errormsg = invalid_range(&ea)) != NULL)
2302 goto doend;
2303 }
2304
2305 if ((ea.argt & NOTADR) && ea.addr_count == 0) /* default is 1, not cursor */
2306 ea.line2 = 1;
2307
2308 correct_range(&ea);
2309
2310#ifdef FEAT_FOLDING
2311 if (((ea.argt & WHOLEFOLD) || ea.addr_count >= 2) && !global_busy)
2312 {
2313 /* Put the first line at the start of a closed fold, put the last line
2314 * at the end of a closed fold. */
2315 (void)hasFolding(ea.line1, &ea.line1, NULL);
2316 (void)hasFolding(ea.line2, NULL, &ea.line2);
2317 }
2318#endif
2319
2320#ifdef FEAT_QUICKFIX
2321 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002322 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323 * option here, so things like % get expanded.
2324 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002325 p = replace_makeprg(&ea, p, cmdlinep);
2326 if (p == NULL)
2327 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002328#endif
2329
2330 /*
2331 * Skip to start of argument.
2332 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2333 */
2334 if (ea.cmdidx == CMD_bang)
2335 ea.arg = p;
2336 else
2337 ea.arg = skipwhite(p);
2338
2339 /*
2340 * Check for "++opt=val" argument.
2341 * Must be first, allow ":w ++enc=utf8 !cmd"
2342 */
2343 if (ea.argt & ARGOPT)
2344 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2345 if (getargopt(&ea) == FAIL && !ni)
2346 {
2347 errormsg = (char_u *)_(e_invarg);
2348 goto doend;
2349 }
2350
2351 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2352 {
2353 if (*ea.arg == '>') /* append */
2354 {
2355 if (*++ea.arg != '>') /* typed wrong */
2356 {
2357 errormsg = (char_u *)_("E494: Use w or w>>");
2358 goto doend;
2359 }
2360 ea.arg = skipwhite(ea.arg + 1);
2361 ea.append = TRUE;
2362 }
2363 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
2364 {
2365 ++ea.arg;
2366 ea.usefilter = TRUE;
2367 }
2368 }
2369
2370 if (ea.cmdidx == CMD_read)
2371 {
2372 if (ea.forceit)
2373 {
2374 ea.usefilter = TRUE; /* :r! filter if ea.forceit */
2375 ea.forceit = FALSE;
2376 }
2377 else if (*ea.arg == '!') /* :r !filter */
2378 {
2379 ++ea.arg;
2380 ea.usefilter = TRUE;
2381 }
2382 }
2383
2384 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2385 {
2386 ea.amount = 1;
2387 while (*ea.arg == *ea.cmd) /* count number of '>' or '<' */
2388 {
2389 ++ea.arg;
2390 ++ea.amount;
2391 }
2392 ea.arg = skipwhite(ea.arg);
2393 }
2394
2395 /*
2396 * Check for "+command" argument, before checking for next command.
2397 * Don't do this for ":read !cmd" and ":write !cmd".
2398 */
2399 if ((ea.argt & EDITCMD) && !ea.usefilter)
2400 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2401
2402 /*
2403 * Check for '|' to separate commands and '"' to start comments.
2404 * Don't do this for ":read !cmd" and ":write !cmd".
2405 */
2406 if ((ea.argt & TRLBAR) && !ea.usefilter)
2407 separate_nextcmd(&ea);
2408
2409 /*
2410 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002411 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2412 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002414 else if (ea.cmdidx == CMD_bang
2415 || ea.cmdidx == CMD_global
2416 || ea.cmdidx == CMD_vglobal
2417 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002418 {
2419 for (p = ea.arg; *p; ++p)
2420 {
2421 /* Remove one backslash before a newline, so that it's possible to
2422 * pass a newline to the shell and also a newline that is preceded
2423 * with a backslash. This makes it impossible to end a shell
2424 * command in a backslash, but that doesn't appear useful.
2425 * Halving the number of backslashes is incompatible with previous
2426 * versions. */
2427 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002428 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429 else if (*p == '\n')
2430 {
2431 ea.nextcmd = p + 1;
2432 *p = NUL;
2433 break;
2434 }
2435 }
2436 }
2437
2438 if ((ea.argt & DFLALL) && ea.addr_count == 0)
2439 {
2440 ea.line1 = 1;
2441 ea.line2 = curbuf->b_ml.ml_line_count;
2442 }
2443
2444 /* accept numbered register only when no count allowed (:put) */
2445 if ( (ea.argt & REGSTR)
2446 && *ea.arg != NUL
2447#ifdef FEAT_USR_CMDS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448 /* Do not allow register = for user commands */
2449 && (!USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002450#endif
2451 && !((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)))
2452 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002453#ifndef FEAT_CLIPBOARD
2454 /* check these explicitly for a more specific error message */
2455 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002457 errormsg = (char_u *)_(e_invalidreg);
2458 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459 }
2460#endif
Bram Moolenaar85de2062011-05-05 14:26:41 +02002461 if (
2462#ifdef FEAT_USR_CMDS
2463 valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2464 && USER_CMDIDX(ea.cmdidx)))
2465#else
2466 valid_yank_reg(*ea.arg, ea.cmdidx != CMD_put)
2467#endif
2468 )
2469 {
2470 ea.regname = *ea.arg++;
2471#ifdef FEAT_EVAL
2472 /* for '=' register: accept the rest of the line as an expression */
2473 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2474 {
2475 set_expr_line(vim_strsave(ea.arg));
2476 ea.arg += STRLEN(ea.arg);
2477 }
2478#endif
2479 ea.arg = skipwhite(ea.arg);
2480 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481 }
2482
2483 /*
2484 * Check for a count. When accepting a BUFNAME, don't use "123foo" as a
2485 * count, it's a buffer name.
2486 */
2487 if ((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)
2488 && (!(ea.argt & BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
2489 || vim_iswhite(*p)))
2490 {
2491 n = getdigits(&ea.arg);
2492 ea.arg = skipwhite(ea.arg);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002493 if (n <= 0 && !ni && (ea.argt & ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494 {
2495 errormsg = (char_u *)_(e_zerocount);
2496 goto doend;
2497 }
2498 if (ea.argt & NOTADR) /* e.g. :buffer 2, :sleep 3 */
2499 {
2500 ea.line2 = n;
2501 if (ea.addr_count == 0)
2502 ea.addr_count = 1;
2503 }
2504 else
2505 {
2506 ea.line1 = ea.line2;
2507 ea.line2 += n - 1;
2508 ++ea.addr_count;
2509 /*
2510 * Be vi compatible: no error message for out of range.
2511 */
2512 if (ea.line2 > curbuf->b_ml.ml_line_count)
2513 ea.line2 = curbuf->b_ml.ml_line_count;
2514 }
2515 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002516
2517 /*
2518 * Check for flags: 'l', 'p' and '#'.
2519 */
2520 if (ea.argt & EXFLAGS)
2521 get_flags(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522 /* no arguments allowed */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002523 if (!ni && !(ea.argt & EXTRA) && *ea.arg != NUL
Bram Moolenaara2031822006-03-07 22:29:51 +00002524 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002525 {
2526 errormsg = (char_u *)_(e_trailing);
2527 goto doend;
2528 }
2529
2530 if (!ni && (ea.argt & NEEDARG) && *ea.arg == NUL)
2531 {
2532 errormsg = (char_u *)_(e_argreq);
2533 goto doend;
2534 }
2535
2536#ifdef FEAT_EVAL
2537 /*
2538 * Skip the command when it's not going to be executed.
2539 * The commands like :if, :endif, etc. always need to be executed.
2540 * Also make an exception for commands that handle a trailing command
2541 * themselves.
2542 */
2543 if (ea.skip)
2544 {
2545 switch (ea.cmdidx)
2546 {
2547 /* commands that need evaluation */
2548 case CMD_while:
2549 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002550 case CMD_for:
2551 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002552 case CMD_if:
2553 case CMD_elseif:
2554 case CMD_else:
2555 case CMD_endif:
2556 case CMD_try:
2557 case CMD_catch:
2558 case CMD_finally:
2559 case CMD_endtry:
2560 case CMD_function:
2561 break;
2562
2563 /* Commands that handle '|' themselves. Check: A command should
2564 * either have the TRLBAR flag, appear in this list or appear in
2565 * the list at ":help :bar". */
2566 case CMD_aboveleft:
2567 case CMD_and:
2568 case CMD_belowright:
2569 case CMD_botright:
2570 case CMD_browse:
2571 case CMD_call:
2572 case CMD_confirm:
2573 case CMD_delfunction:
2574 case CMD_djump:
2575 case CMD_dlist:
2576 case CMD_dsearch:
2577 case CMD_dsplit:
2578 case CMD_echo:
2579 case CMD_echoerr:
2580 case CMD_echomsg:
2581 case CMD_echon:
2582 case CMD_execute:
2583 case CMD_help:
2584 case CMD_hide:
2585 case CMD_ijump:
2586 case CMD_ilist:
2587 case CMD_isearch:
2588 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002589 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002590 case CMD_keepjumps:
2591 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01002592 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593 case CMD_leftabove:
2594 case CMD_let:
2595 case CMD_lockmarks:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002596 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002597 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002598 case CMD_mzscheme:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599 case CMD_perl:
2600 case CMD_psearch:
2601 case CMD_python:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002602 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002603 case CMD_python3:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604 case CMD_return:
2605 case CMD_rightbelow:
2606 case CMD_ruby:
2607 case CMD_silent:
2608 case CMD_smagic:
2609 case CMD_snomagic:
2610 case CMD_substitute:
2611 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002612 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002613 case CMD_tcl:
2614 case CMD_throw:
2615 case CMD_tilde:
2616 case CMD_topleft:
2617 case CMD_unlet:
2618 case CMD_verbose:
2619 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002620 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002621 break;
2622
2623 default: goto doend;
2624 }
2625 }
2626#endif
2627
2628 if (ea.argt & XFILE)
2629 {
2630 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2631 goto doend;
2632 }
2633
2634#ifdef FEAT_LISTCMDS
2635 /*
2636 * Accept buffer name. Cannot be used at the same time with a buffer
2637 * number. Don't do this for a user command.
2638 */
2639 if ((ea.argt & BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
2640# ifdef FEAT_USR_CMDS
2641 && !USER_CMDIDX(ea.cmdidx)
2642# endif
2643 )
2644 {
2645 /*
2646 * :bdelete, :bwipeout and :bunload take several arguments, separated
2647 * by spaces: find next space (skipping over escaped characters).
2648 * The others take one argument: ignore trailing spaces.
2649 */
2650 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2651 || ea.cmdidx == CMD_bunload)
2652 p = skiptowhite_esc(ea.arg);
2653 else
2654 {
2655 p = ea.arg + STRLEN(ea.arg);
2656 while (p > ea.arg && vim_iswhite(p[-1]))
2657 --p;
2658 }
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002659 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & BUFUNL) != 0,
2660 FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661 if (ea.line2 < 0) /* failed */
2662 goto doend;
2663 ea.addr_count = 1;
2664 ea.arg = skipwhite(p);
2665 }
2666#endif
2667
2668/*
2669 * 6. switch on command name
2670 *
2671 * The "ea" structure holds the arguments that can be used.
2672 */
2673 ea.cmdlinep = cmdlinep;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002674 ea.getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675 ea.cookie = cookie;
2676#ifdef FEAT_EVAL
2677 ea.cstack = cstack;
2678#endif
2679
2680#ifdef FEAT_USR_CMDS
2681 if (USER_CMDIDX(ea.cmdidx))
2682 {
2683 /*
2684 * Execute a user-defined command.
2685 */
2686 do_ucmd(&ea);
2687 }
2688 else
2689#endif
2690 {
2691 /*
2692 * Call the function to execute the command.
2693 */
2694 ea.errmsg = NULL;
2695 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2696 if (ea.errmsg != NULL)
2697 errormsg = (char_u *)_(ea.errmsg);
2698 }
2699
2700#ifdef FEAT_EVAL
2701 /*
2702 * If the command just executed called do_cmdline(), any throw or ":return"
2703 * or ":finish" encountered there must also check the cstack of the still
2704 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2705 * exception, or reanimate a returned function or finished script file and
2706 * return or finish it again.
2707 */
2708 if (need_rethrow)
2709 do_throw(cstack);
2710 else if (check_cstack)
2711 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002712 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002714 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715 && current_func_returned())
2716 do_return(&ea, TRUE, FALSE, NULL);
2717 }
2718 need_rethrow = check_cstack = FALSE;
2719#endif
2720
2721doend:
2722 if (curwin->w_cursor.lnum == 0) /* can happen with zero line number */
2723 curwin->w_cursor.lnum = 1;
2724
2725 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2726 {
2727 if (sourcing)
2728 {
2729 if (errormsg != IObuff)
2730 {
2731 STRCPY(IObuff, errormsg);
2732 errormsg = IObuff;
2733 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002734 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002735 }
2736 emsg(errormsg);
2737 }
2738#ifdef FEAT_EVAL
2739 do_errthrow(cstack,
2740 (ea.cmdidx != CMD_SIZE
2741# ifdef FEAT_USR_CMDS
2742 && !USER_CMDIDX(ea.cmdidx)
2743# endif
2744 ) ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
2745#endif
2746
2747 if (verbose_save >= 0)
2748 p_verbose = verbose_save;
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00002749#ifdef FEAT_AUTOCMD
2750 if (cmdmod.save_ei != NULL)
2751 {
2752 /* Restore 'eventignore' to the value before ":noautocmd". */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002753 set_string_option_direct((char_u *)"ei", -1, cmdmod.save_ei,
2754 OPT_FREE, SID_NONE);
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00002755 free_string_option(cmdmod.save_ei);
2756 }
2757#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002758
2759 cmdmod = save_cmdmod;
2760
Bram Moolenaar8e258a42009-07-09 13:55:43 +00002761 if (save_msg_silent != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002762 {
2763 /* messages could be enabled for a serious error, need to check if the
2764 * counters don't become negative */
Bram Moolenaarbecf4282009-09-30 11:24:36 +00002765 if (!did_emsg || msg_silent > save_msg_silent)
Bram Moolenaar8e258a42009-07-09 13:55:43 +00002766 msg_silent = save_msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002767 emsg_silent -= did_esilent;
2768 if (emsg_silent < 0)
2769 emsg_silent = 0;
2770 /* Restore msg_scroll, it's set by file I/O commands, even when no
2771 * message is actually displayed. */
2772 msg_scroll = save_msg_scroll;
Bram Moolenaar77ab2802009-04-22 12:44:48 +00002773
2774 /* "silent reg" or "silent echo x" inside "redir" leaves msg_col
2775 * somewhere in the line. Put it back in the first column. */
2776 if (redirecting())
2777 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002778 }
2779
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002780#ifdef HAVE_SANDBOX
2781 if (did_sandbox)
2782 --sandbox;
2783#endif
2784
Bram Moolenaar071d4272004-06-13 20:20:40 +00002785 if (ea.nextcmd && *ea.nextcmd == NUL) /* not really a next command */
2786 ea.nextcmd = NULL;
2787
2788#ifdef FEAT_EVAL
2789 --ex_nesting_level;
2790#endif
2791
2792 return ea.nextcmd;
2793}
2794#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00002795 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002796#endif
2797
2798/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002799 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002800 * If there is a match advance "pp" to the argument and return TRUE.
2801 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002802 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00002803checkforcmd(pp, cmd, len)
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002804 char_u **pp; /* start of command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002805 char *cmd; /* name of command */
2806 int len; /* required length */
2807{
2808 int i;
2809
2810 for (i = 0; cmd[i] != NUL; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00002811 if (((char_u *)cmd)[i] != (*pp)[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00002812 break;
2813 if (i >= len && !isalpha((*pp)[i]))
2814 {
2815 *pp = skipwhite(*pp + i);
2816 return TRUE;
2817 }
2818 return FALSE;
2819}
2820
2821/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002822 * Append "cmd" to the error message in IObuff.
2823 * Takes care of limiting the length and handling 0xa0, which would be
2824 * invisible otherwise.
2825 */
2826 static void
2827append_command(cmd)
2828 char_u *cmd;
2829{
2830 char_u *s = cmd;
2831 char_u *d;
2832
2833 STRCAT(IObuff, ": ");
2834 d = IObuff + STRLEN(IObuff);
2835 while (*s != NUL && d - IObuff < IOSIZE - 7)
2836 {
2837 if (
2838#ifdef FEAT_MBYTE
2839 enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) :
2840#endif
2841 *s == 0xa0)
2842 {
2843 s +=
2844#ifdef FEAT_MBYTE
2845 enc_utf8 ? 2 :
2846#endif
2847 1;
2848 STRCPY(d, "<a0>");
2849 d += 4;
2850 }
2851 else
2852 MB_COPY_CHAR(s, d);
2853 }
2854 *d = NUL;
2855}
2856
2857/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002859 * Start of the name can be found at eap->cmd.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002860 * Returns pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002861 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002862 * Returns NULL for an ambiguous user command.
2863 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002864 static char_u *
2865find_command(eap, full)
2866 exarg_T *eap;
Bram Moolenaar78a15312009-05-15 19:33:18 +00002867 int *full UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002868{
2869 int len;
2870 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00002871 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002872
2873 /*
2874 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00002875 * Exceptions:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002876 * - the 'k' command can directly be followed by any character.
2877 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
2878 * but :sre[wind] is another command, as are :scrip[tnames],
2879 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00002880 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881 */
2882 p = eap->cmd;
2883 if (*p == 'k')
2884 {
2885 eap->cmdidx = CMD_k;
2886 ++p;
2887 }
2888 else if (p[0] == 's'
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002889 && ((p[1] == 'c' && p[2] != 's' && p[2] != 'r'
2890 && p[3] != 'i' && p[4] != 'p')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002891 || p[1] == 'g'
2892 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
2893 || p[1] == 'I'
2894 || (p[1] == 'r' && p[2] != 'e')))
2895 {
2896 eap->cmdidx = CMD_substitute;
2897 ++p;
2898 }
2899 else
2900 {
2901 while (ASCII_ISALPHA(*p))
2902 ++p;
Bram Moolenaarb6590522010-07-21 16:00:43 +02002903 /* for python 3.x support ":py3", ":python3", ":py3file", etc. */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002904 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaarb6590522010-07-21 16:00:43 +02002905 while (ASCII_ISALNUM(*p))
2906 ++p;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02002907
Bram Moolenaar071d4272004-06-13 20:20:40 +00002908 /* check for non-alpha command */
2909 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
2910 ++p;
2911 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00002912 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
2913 {
2914 /* Check for ":dl", ":dell", etc. to ":deletel": that's
2915 * :delete with the 'l' flag. Same for 'p'. */
2916 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00002917 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00002918 break;
2919 if (i == len - 1)
2920 {
2921 --len;
2922 if (p[-1] == 'l')
2923 eap->flags |= EXFLAG_LIST;
2924 else
2925 eap->flags |= EXFLAG_PRINT;
2926 }
2927 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002928
2929 if (ASCII_ISLOWER(*eap->cmd))
2930 eap->cmdidx = cmdidxs[CharOrdLow(*eap->cmd)];
2931 else
2932 eap->cmdidx = cmdidxs[26];
2933
2934 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
2935 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
2936 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
2937 (size_t)len) == 0)
2938 {
2939#ifdef FEAT_EVAL
2940 if (full != NULL
2941 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
2942 *full = TRUE;
2943#endif
2944 break;
2945 }
2946
2947#ifdef FEAT_USR_CMDS
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01002948 /* Look for a user defined command as a last resort. Let ":Print" be
2949 * overruled by a user defined command. */
2950 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
2951 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002952 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002953 /* User defined commands may contain digits. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002954 while (ASCII_ISALNUM(*p))
2955 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002956 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002957 }
2958#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002959 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002960 eap->cmdidx = CMD_SIZE;
2961 }
2962
2963 return p;
2964}
2965
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002966#ifdef FEAT_USR_CMDS
2967/*
2968 * Search for a user command that matches "eap->cmd".
2969 * Return cmdidx in "eap->cmdidx", flags in "eap->argt", idx in "eap->useridx".
2970 * Return a pointer to just after the command.
2971 * Return NULL if there is no matching command.
2972 */
2973 static char_u *
2974find_ucmd(eap, p, full, xp, compl)
2975 exarg_T *eap;
2976 char_u *p; /* end of the command (possibly including count) */
2977 int *full; /* set to TRUE for a full match */
2978 expand_T *xp; /* used for completion, NULL otherwise */
2979 int *compl; /* completion flags or NULL */
2980{
2981 int len = (int)(p - eap->cmd);
2982 int j, k, matchlen = 0;
2983 ucmd_T *uc;
2984 int found = FALSE;
2985 int possible = FALSE;
2986 char_u *cp, *np; /* Point into typed cmd and test name */
2987 garray_T *gap;
2988 int amb_local = FALSE; /* Found ambiguous buffer-local command,
2989 only full match global is accepted. */
2990
2991 /*
2992 * Look for buffer-local user commands first, then global ones.
2993 */
2994 gap = &curbuf->b_ucmds;
2995 for (;;)
2996 {
2997 for (j = 0; j < gap->ga_len; ++j)
2998 {
2999 uc = USER_CMD_GA(gap, j);
3000 cp = eap->cmd;
3001 np = uc->uc_name;
3002 k = 0;
3003 while (k < len && *np != NUL && *cp++ == *np++)
3004 k++;
3005 if (k == len || (*np == NUL && vim_isdigit(eap->cmd[k])))
3006 {
3007 /* If finding a second match, the command is ambiguous. But
3008 * not if a buffer-local command wasn't a full match and a
3009 * global command is a full match. */
3010 if (k == len && found && *np != NUL)
3011 {
3012 if (gap == &ucmds)
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003013 return NULL;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003014 amb_local = TRUE;
3015 }
3016
3017 if (!found || (k == len && *np == NUL))
3018 {
3019 /* If we matched up to a digit, then there could
3020 * be another command including the digit that we
3021 * should use instead.
3022 */
3023 if (k == len)
3024 found = TRUE;
3025 else
3026 possible = TRUE;
3027
3028 if (gap == &ucmds)
3029 eap->cmdidx = CMD_USER;
3030 else
3031 eap->cmdidx = CMD_USER_BUF;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003032 eap->argt = (long)uc->uc_argt;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003033 eap->useridx = j;
3034
3035# ifdef FEAT_CMDL_COMPL
3036 if (compl != NULL)
3037 *compl = uc->uc_compl;
3038# ifdef FEAT_EVAL
3039 if (xp != NULL)
3040 {
3041 xp->xp_arg = uc->uc_compl_arg;
3042 xp->xp_scriptID = uc->uc_scriptID;
3043 }
3044# endif
3045# endif
3046 /* Do not search for further abbreviations
3047 * if this is an exact match. */
3048 matchlen = k;
3049 if (k == len && *np == NUL)
3050 {
3051 if (full != NULL)
3052 *full = TRUE;
3053 amb_local = FALSE;
3054 break;
3055 }
3056 }
3057 }
3058 }
3059
3060 /* Stop if we found a full match or searched all. */
3061 if (j < gap->ga_len || gap == &ucmds)
3062 break;
3063 gap = &ucmds;
3064 }
3065
3066 /* Only found ambiguous matches. */
3067 if (amb_local)
3068 {
3069 if (xp != NULL)
3070 xp->xp_context = EXPAND_UNSUCCESSFUL;
3071 return NULL;
3072 }
3073
3074 /* The match we found may be followed immediately by a number. Move "p"
3075 * back to point to it. */
3076 if (found || possible)
3077 return p + (matchlen - len);
3078 return p;
3079}
3080#endif
3081
Bram Moolenaar071d4272004-06-13 20:20:40 +00003082#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003083static struct cmdmod
3084{
3085 char *name;
3086 int minlen;
3087 int has_count; /* :123verbose :3tab */
3088} cmdmods[] = {
3089 {"aboveleft", 3, FALSE},
3090 {"belowright", 3, FALSE},
3091 {"botright", 2, FALSE},
3092 {"browse", 3, FALSE},
3093 {"confirm", 4, FALSE},
3094 {"hide", 3, FALSE},
3095 {"keepalt", 5, FALSE},
3096 {"keepjumps", 5, FALSE},
3097 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01003098 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003099 {"leftabove", 5, FALSE},
3100 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003101 {"noautocmd", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003102 {"rightbelow", 6, FALSE},
3103 {"sandbox", 3, FALSE},
3104 {"silent", 3, FALSE},
3105 {"tab", 3, TRUE},
3106 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003107 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003108 {"verbose", 4, TRUE},
3109 {"vertical", 4, FALSE},
3110};
3111
3112/*
3113 * Return length of a command modifier (including optional count).
3114 * Return zero when it's not a modifier.
3115 */
3116 int
3117modifier_len(cmd)
3118 char_u *cmd;
3119{
3120 int i, j;
3121 char_u *p = cmd;
3122
3123 if (VIM_ISDIGIT(*cmd))
3124 p = skipwhite(skipdigits(cmd));
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003125 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003126 {
3127 for (j = 0; p[j] != NUL; ++j)
3128 if (p[j] != cmdmods[i].name[j])
3129 break;
Bram Moolenaar2d473ab2013-06-12 17:12:24 +02003130 if (!ASCII_ISALPHA(p[j]) && j >= cmdmods[i].minlen
Bram Moolenaared53fb92007-11-24 20:50:24 +00003131 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003132 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003133 }
3134 return 0;
3135}
3136
Bram Moolenaar071d4272004-06-13 20:20:40 +00003137/*
3138 * Return > 0 if an Ex command "name" exists.
3139 * Return 2 if there is an exact match.
3140 * Return 3 if there is an ambiguous match.
3141 */
3142 int
3143cmd_exists(name)
3144 char_u *name;
3145{
3146 exarg_T ea;
3147 int full = FALSE;
3148 int i;
3149 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003150 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003151
3152 /* Check command modifiers. */
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003153 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154 {
3155 for (j = 0; name[j] != NUL; ++j)
3156 if (name[j] != cmdmods[i].name[j])
3157 break;
3158 if (name[j] == NUL && j >= cmdmods[i].minlen)
3159 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3160 }
3161
Bram Moolenaara9587612006-05-04 21:47:50 +00003162 /* Check built-in commands and user defined commands.
3163 * For ":2match" and ":3match" we need to skip the number. */
3164 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003165 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003166 p = find_command(&ea, &full);
3167 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003168 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003169 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3170 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003171 if (*skipwhite(p) != NUL)
3172 return 0; /* trailing garbage */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3174}
3175#endif
3176
3177/*
3178 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
3179 * we don't need/want deleted. Maybe this could be done better if we didn't
3180 * repeat all this stuff. The only problem is that they may not stay
3181 * perfectly compatible with each other, but then the command line syntax
3182 * probably won't change that much -- webb.
3183 */
3184 char_u *
3185set_one_cmd_context(xp, buff)
3186 expand_T *xp;
3187 char_u *buff; /* buffer for command string */
3188{
3189 char_u *p;
3190 char_u *cmd, *arg;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003191 int len = 0;
3192 exarg_T ea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3194 int compl = EXPAND_NOTHING;
3195#endif
3196#ifdef FEAT_CMDL_COMPL
3197 int delim;
3198#endif
3199 int forceit = FALSE;
3200 int usefilter = FALSE; /* filter instead of file name */
3201
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003202 ExpandInit(xp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203 xp->xp_pattern = buff;
3204 xp->xp_context = EXPAND_COMMANDS; /* Default until we get past command */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003205 ea.argt = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206
3207/*
3208 * 2. skip comment lines and leading space, colons or bars
3209 */
3210 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
3211 ;
3212 xp->xp_pattern = cmd;
3213
3214 if (*cmd == NUL)
3215 return NULL;
3216 if (*cmd == '"') /* ignore comment lines */
3217 {
3218 xp->xp_context = EXPAND_NOTHING;
3219 return NULL;
3220 }
3221
3222/*
3223 * 3. parse a range specifier of the form: addr [,addr] [;addr] ..
3224 */
3225 cmd = skip_range(cmd, &xp->xp_context);
3226
3227/*
3228 * 4. parse command
3229 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230 xp->xp_pattern = cmd;
3231 if (*cmd == NUL)
3232 return NULL;
3233 if (*cmd == '"')
3234 {
3235 xp->xp_context = EXPAND_NOTHING;
3236 return NULL;
3237 }
3238
3239 if (*cmd == '|' || *cmd == '\n')
3240 return cmd + 1; /* There's another command */
3241
3242 /*
3243 * Isolate the command and search for it in the command table.
3244 * Exceptions:
3245 * - the 'k' command can directly be followed by any character, but
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003246 * do accept "keepmarks", "keepalt" and "keepjumps".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003247 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
3248 */
3249 if (*cmd == 'k' && cmd[1] != 'e')
3250 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003251 ea.cmdidx = CMD_k;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003252 p = cmd + 1;
3253 }
3254 else
3255 {
3256 p = cmd;
3257 while (ASCII_ISALPHA(*p) || *p == '*') /* Allow * wild card */
3258 ++p;
3259 /* check for non-alpha command */
3260 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3261 ++p;
Bram Moolenaar9f5d6002013-06-02 19:22:13 +02003262 /* for python 3.x: ":py3*" commands completion */
3263 if (cmd[0] == 'p' && cmd[1] == 'y' && p == cmd + 2 && *p == '3')
3264 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003265 len = (int)(p - cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003266
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003267 if (len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003268 {
3269 xp->xp_context = EXPAND_UNSUCCESSFUL;
3270 return NULL;
3271 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003272 for (ea.cmdidx = (cmdidx_T)0; (int)ea.cmdidx < (int)CMD_SIZE;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01003273 ea.cmdidx = (cmdidx_T)((int)ea.cmdidx + 1))
3274 if (STRNCMP(cmdnames[(int)ea.cmdidx].cmd_name, cmd,
3275 (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276 break;
3277
3278#ifdef FEAT_USR_CMDS
3279 if (cmd[0] >= 'A' && cmd[0] <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280 while (ASCII_ISALNUM(*p) || *p == '*') /* Allow * wild card */
3281 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282#endif
3283 }
3284
3285 /*
3286 * If the cursor is touching the command, and it ends in an alpha-numeric
3287 * character, complete the command name.
3288 */
3289 if (*p == NUL && ASCII_ISALNUM(p[-1]))
3290 return NULL;
3291
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003292 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293 {
3294 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
3295 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003296 ea.cmdidx = CMD_substitute;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297 p = cmd + 1;
3298 }
3299#ifdef FEAT_USR_CMDS
3300 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
3301 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003302 ea.cmd = cmd;
3303 p = find_ucmd(&ea, p, NULL, xp,
3304# if defined(FEAT_CMDL_COMPL)
3305 &compl
3306# else
3307 NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003308# endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003309 );
Bram Moolenaarebefac62005-12-28 22:39:57 +00003310 if (p == NULL)
3311 ea.cmdidx = CMD_SIZE; /* ambiguous user command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312 }
3313#endif
3314 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003315 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003316 {
3317 /* Not still touching the command and it was an illegal one */
3318 xp->xp_context = EXPAND_UNSUCCESSFUL;
3319 return NULL;
3320 }
3321
3322 xp->xp_context = EXPAND_NOTHING; /* Default now that we're past command */
3323
3324 if (*p == '!') /* forced commands */
3325 {
3326 forceit = TRUE;
3327 ++p;
3328 }
3329
3330/*
3331 * 5. parse arguments
3332 */
3333#ifdef FEAT_USR_CMDS
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003334 if (!USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335#endif
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003336 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337
3338 arg = skipwhite(p);
3339
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003340 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003341 {
3342 if (*arg == '>') /* append */
3343 {
3344 if (*++arg == '>')
3345 ++arg;
3346 arg = skipwhite(arg);
3347 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003348 else if (*arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349 {
3350 ++arg;
3351 usefilter = TRUE;
3352 }
3353 }
3354
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003355 if (ea.cmdidx == CMD_read)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003356 {
3357 usefilter = forceit; /* :r! filter if forced */
3358 if (*arg == '!') /* :r !filter */
3359 {
3360 ++arg;
3361 usefilter = TRUE;
3362 }
3363 }
3364
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003365 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366 {
3367 while (*arg == *cmd) /* allow any number of '>' or '<' */
3368 ++arg;
3369 arg = skipwhite(arg);
3370 }
3371
3372 /* Does command allow "+command"? */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003373 if ((ea.argt & EDITCMD) && !usefilter && *arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374 {
3375 /* Check if we're in the +command */
3376 p = arg + 1;
3377 arg = skip_cmd_arg(arg, FALSE);
3378
3379 /* Still touching the command after '+'? */
3380 if (*arg == NUL)
3381 return p;
3382
3383 /* Skip space(s) after +command to get to the real argument */
3384 arg = skipwhite(arg);
3385 }
3386
3387 /*
3388 * Check for '|' to separate commands and '"' to start comments.
3389 * Don't do this for ":read !cmd" and ":write !cmd".
3390 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003391 if ((ea.argt & TRLBAR) && !usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392 {
3393 p = arg;
3394 /* ":redir @" is not the start of a comment */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003395 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396 p += 2;
3397 while (*p)
3398 {
3399 if (*p == Ctrl_V)
3400 {
3401 if (p[1] != NUL)
3402 ++p;
3403 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003404 else if ( (*p == '"' && !(ea.argt & NOTRLCOM))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405 || *p == '|' || *p == '\n')
3406 {
3407 if (*(p - 1) != '\\')
3408 {
3409 if (*p == '|' || *p == '\n')
3410 return p + 1;
3411 return NULL; /* It's a comment */
3412 }
3413 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003414 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415 }
3416 }
3417
3418 /* no arguments allowed */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003419 if (!(ea.argt & EXTRA) && *arg != NUL &&
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420 vim_strchr((char_u *)"|\"", *arg) == NULL)
3421 return NULL;
3422
3423 /* Find start of last argument (argument just before cursor): */
Bram Moolenaar848f8762012-07-25 17:22:23 +02003424 p = buff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003425 xp->xp_pattern = p;
Bram Moolenaar09168a72012-08-02 21:24:42 +02003426 len = (int)STRLEN(buff);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003427 while (*p && p < buff + len)
3428 {
3429 if (*p == ' ' || *p == TAB)
3430 {
3431 /* argument starts after a space */
3432 xp->xp_pattern = ++p;
3433 }
3434 else
3435 {
3436 if (*p == '\\' && *(p + 1) != NUL)
3437 ++p; /* skip over escaped character */
3438 mb_ptr_adv(p);
3439 }
3440 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003441
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003442 if (ea.argt & XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003444 int c;
3445 int in_quote = FALSE;
3446 char_u *bow = NULL; /* Beginning of word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447
3448 /*
3449 * Allow spaces within back-quotes to count as part of the argument
3450 * being expanded.
3451 */
3452 xp->xp_pattern = skipwhite(arg);
Bram Moolenaar6529c102007-08-18 15:47:34 +00003453 p = xp->xp_pattern;
3454 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003456#ifdef FEAT_MBYTE
3457 if (has_mbyte)
3458 c = mb_ptr2char(p);
3459 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460#endif
Bram Moolenaar6529c102007-08-18 15:47:34 +00003461 c = *p;
3462 if (c == '\\' && p[1] != NUL)
3463 ++p;
3464 else if (c == '`')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465 {
3466 if (!in_quote)
3467 {
3468 xp->xp_pattern = p;
3469 bow = p + 1;
3470 }
3471 in_quote = !in_quote;
3472 }
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003473 /* An argument can contain just about everything, except
3474 * characters that end the command and white space. */
3475 else if (c == '|' || c == '\n' || c == '"' || (vim_iswhite(c)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003476#ifdef SPACE_IN_FILENAME
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003477 && (!(ea.argt & NOSPC) || usefilter)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003478#endif
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003479 ))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003480 {
Bram Moolenaarcf5a5b82008-02-26 20:30:12 +00003481 len = 0; /* avoid getting stuck when space is in 'isfname' */
Bram Moolenaar6529c102007-08-18 15:47:34 +00003482 while (*p != NUL)
3483 {
3484#ifdef FEAT_MBYTE
3485 if (has_mbyte)
3486 c = mb_ptr2char(p);
3487 else
3488#endif
3489 c = *p;
Bram Moolenaardd87969c2007-08-21 13:07:12 +00003490 if (c == '`' || vim_isfilec_or_wc(c))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003491 break;
3492#ifdef FEAT_MBYTE
3493 if (has_mbyte)
3494 len = (*mb_ptr2len)(p);
3495 else
3496#endif
3497 len = 1;
3498 mb_ptr_adv(p);
3499 }
3500 if (in_quote)
3501 bow = p;
3502 else
3503 xp->xp_pattern = p;
3504 p -= len;
3505 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003506 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507 }
3508
3509 /*
3510 * If we are still inside the quotes, and we passed a space, just
3511 * expand from there.
3512 */
3513 if (bow != NULL && in_quote)
3514 xp->xp_pattern = bow;
3515 xp->xp_context = EXPAND_FILES;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003516
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003517 /* For a shell command more chars need to be escaped. */
3518 if (usefilter || ea.cmdidx == CMD_bang)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003519 {
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003520#ifndef BACKSLASH_IN_FILENAME
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003521 xp->xp_shell = TRUE;
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003522#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003523 /* When still after the command name expand executables. */
3524 if (xp->xp_pattern == skipwhite(arg))
Bram Moolenaar5c5b0942007-05-06 12:07:59 +00003525 xp->xp_context = EXPAND_SHELLCMD;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003526 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527
3528 /* Check for environment variable */
3529 if (*xp->xp_pattern == '$'
3530#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
3531 || *xp->xp_pattern == '%'
3532#endif
3533 )
3534 {
3535 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
3536 if (!vim_isIDc(*p))
3537 break;
3538 if (*p == NUL)
3539 {
3540 xp->xp_context = EXPAND_ENV_VARS;
3541 ++xp->xp_pattern;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003542#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3543 /* Avoid that the assignment uses EXPAND_FILES again. */
Bram Moolenaara466c992005-07-09 21:03:22 +00003544 if (compl != EXPAND_USER_DEFINED && compl != EXPAND_USER_LIST)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003545 compl = EXPAND_ENV_VARS;
3546#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547 }
3548 }
Bram Moolenaar24305862012-08-15 14:05:05 +02003549#if defined(FEAT_CMDL_COMPL)
3550 /* Check for user names */
3551 if (*xp->xp_pattern == '~')
3552 {
3553 for (p = xp->xp_pattern + 1; *p != NUL && *p != '/'; ++p)
3554 ;
3555 /* Complete ~user only if it partially matches a user name.
3556 * A full match ~user<Tab> will be replaced by user's home
3557 * directory i.e. something like ~user<Tab> -> /home/user/ */
3558 if (*p == NUL && p > xp->xp_pattern + 1
3559 && match_user(xp->xp_pattern + 1) == 1)
3560 {
3561 xp->xp_context = EXPAND_USER;
3562 ++xp->xp_pattern;
3563 }
3564 }
3565#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566 }
3567
3568/*
3569 * 6. switch on command name
3570 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003571 switch (ea.cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003572 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003573 case CMD_find:
3574 case CMD_sfind:
3575 case CMD_tabfind:
Bram Moolenaarc24b6972010-08-16 22:34:29 +02003576 if (xp->xp_context == EXPAND_FILES)
3577 xp->xp_context = EXPAND_FILES_IN_PATH;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003578 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579 case CMD_cd:
3580 case CMD_chdir:
3581 case CMD_lcd:
3582 case CMD_lchdir:
3583 if (xp->xp_context == EXPAND_FILES)
3584 xp->xp_context = EXPAND_DIRECTORIES;
3585 break;
3586 case CMD_help:
3587 xp->xp_context = EXPAND_HELP;
3588 xp->xp_pattern = arg;
3589 break;
3590
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003591 /* Command modifiers: return the argument.
3592 * Also for commands with an argument that is a command. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003593 case CMD_aboveleft:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003594 case CMD_argdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003595 case CMD_belowright:
3596 case CMD_botright:
3597 case CMD_browse:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003598 case CMD_bufdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599 case CMD_confirm:
Bram Moolenaardf177f62005-02-22 08:39:57 +00003600 case CMD_debug:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003601 case CMD_folddoclosed:
3602 case CMD_folddoopen:
3603 case CMD_hide:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003604 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605 case CMD_keepjumps:
3606 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01003607 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608 case CMD_leftabove:
3609 case CMD_lockmarks:
3610 case CMD_rightbelow:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003611 case CMD_sandbox:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612 case CMD_silent:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003613 case CMD_tab:
Bram Moolenaar70baa402013-06-16 16:14:03 +02003614 case CMD_tabdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003615 case CMD_topleft:
3616 case CMD_verbose:
3617 case CMD_vertical:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003618 case CMD_windo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619 return arg;
3620
Bram Moolenaar4f688582007-07-24 12:34:30 +00003621#ifdef FEAT_CMDL_COMPL
3622# ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623 case CMD_match:
3624 if (*arg == NUL || !ends_excmd(*arg))
3625 {
Bram Moolenaar4f688582007-07-24 12:34:30 +00003626 /* also complete "None" */
3627 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628 arg = skipwhite(skiptowhite(arg));
3629 if (*arg != NUL)
3630 {
3631 xp->xp_context = EXPAND_NOTHING;
3632 arg = skip_regexp(arg + 1, *arg, p_magic, NULL);
3633 }
3634 }
3635 return find_nextcmd(arg);
Bram Moolenaar4f688582007-07-24 12:34:30 +00003636# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003637
Bram Moolenaar071d4272004-06-13 20:20:40 +00003638/*
3639 * All completion for the +cmdline_compl feature goes here.
3640 */
3641
3642# ifdef FEAT_USR_CMDS
3643 case CMD_command:
3644 /* Check for attributes */
3645 while (*arg == '-')
3646 {
3647 arg++; /* Skip "-" */
3648 p = skiptowhite(arg);
3649 if (*p == NUL)
3650 {
3651 /* Cursor is still in the attribute */
3652 p = vim_strchr(arg, '=');
3653 if (p == NULL)
3654 {
3655 /* No "=", so complete attribute names */
3656 xp->xp_context = EXPAND_USER_CMD_FLAGS;
3657 xp->xp_pattern = arg;
3658 return NULL;
3659 }
3660
3661 /* For the -complete and -nargs attributes, we complete
3662 * their arguments as well.
3663 */
3664 if (STRNICMP(arg, "complete", p - arg) == 0)
3665 {
3666 xp->xp_context = EXPAND_USER_COMPLETE;
3667 xp->xp_pattern = p + 1;
3668 return NULL;
3669 }
3670 else if (STRNICMP(arg, "nargs", p - arg) == 0)
3671 {
3672 xp->xp_context = EXPAND_USER_NARGS;
3673 xp->xp_pattern = p + 1;
3674 return NULL;
3675 }
3676 return NULL;
3677 }
3678 arg = skipwhite(p);
3679 }
3680
3681 /* After the attributes comes the new command name */
3682 p = skiptowhite(arg);
3683 if (*p == NUL)
3684 {
3685 xp->xp_context = EXPAND_USER_COMMANDS;
3686 xp->xp_pattern = arg;
3687 break;
3688 }
3689
3690 /* And finally comes a normal command */
3691 return skipwhite(p);
3692
3693 case CMD_delcommand:
3694 xp->xp_context = EXPAND_USER_COMMANDS;
3695 xp->xp_pattern = arg;
3696 break;
3697# endif
3698
3699 case CMD_global:
3700 case CMD_vglobal:
3701 delim = *arg; /* get the delimiter */
3702 if (delim)
3703 ++arg; /* skip delimiter if there is one */
3704
3705 while (arg[0] != NUL && arg[0] != delim)
3706 {
3707 if (arg[0] == '\\' && arg[1] != NUL)
3708 ++arg;
3709 ++arg;
3710 }
3711 if (arg[0] != NUL)
3712 return arg + 1;
3713 break;
3714 case CMD_and:
3715 case CMD_substitute:
3716 delim = *arg;
3717 if (delim)
3718 {
3719 /* skip "from" part */
3720 ++arg;
3721 arg = skip_regexp(arg, delim, p_magic, NULL);
3722 }
3723 /* skip "to" part */
3724 while (arg[0] != NUL && arg[0] != delim)
3725 {
3726 if (arg[0] == '\\' && arg[1] != NUL)
3727 ++arg;
3728 ++arg;
3729 }
3730 if (arg[0] != NUL) /* skip delimiter */
3731 ++arg;
3732 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
3733 ++arg;
3734 if (arg[0] != NUL)
3735 return arg;
3736 break;
3737 case CMD_isearch:
3738 case CMD_dsearch:
3739 case CMD_ilist:
3740 case CMD_dlist:
3741 case CMD_ijump:
3742 case CMD_psearch:
3743 case CMD_djump:
3744 case CMD_isplit:
3745 case CMD_dsplit:
3746 arg = skipwhite(skipdigits(arg)); /* skip count */
3747 if (*arg == '/') /* Match regexp, not just whole words */
3748 {
3749 for (++arg; *arg && *arg != '/'; arg++)
3750 if (*arg == '\\' && arg[1] != NUL)
3751 arg++;
3752 if (*arg)
3753 {
3754 arg = skipwhite(arg + 1);
3755
3756 /* Check for trailing illegal characters */
3757 if (*arg && vim_strchr((char_u *)"|\"\n", *arg) == NULL)
3758 xp->xp_context = EXPAND_NOTHING;
3759 else
3760 return arg;
3761 }
3762 }
3763 break;
3764#ifdef FEAT_AUTOCMD
3765 case CMD_autocmd:
3766 return set_context_in_autocmd(xp, arg, FALSE);
3767
3768 case CMD_doautocmd:
Bram Moolenaar73a9d7b2008-11-06 16:16:44 +00003769 case CMD_doautoall:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770 return set_context_in_autocmd(xp, arg, TRUE);
3771#endif
3772 case CMD_set:
3773 set_context_in_set_cmd(xp, arg, 0);
3774 break;
3775 case CMD_setglobal:
3776 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
3777 break;
3778 case CMD_setlocal:
3779 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
3780 break;
3781 case CMD_tag:
3782 case CMD_stag:
3783 case CMD_ptag:
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00003784 case CMD_ltag:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003785 case CMD_tselect:
3786 case CMD_stselect:
3787 case CMD_ptselect:
3788 case CMD_tjump:
3789 case CMD_stjump:
3790 case CMD_ptjump:
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003791 if (*p_wop != NUL)
3792 xp->xp_context = EXPAND_TAGS_LISTFILES;
3793 else
3794 xp->xp_context = EXPAND_TAGS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003795 xp->xp_pattern = arg;
3796 break;
3797 case CMD_augroup:
3798 xp->xp_context = EXPAND_AUGROUP;
3799 xp->xp_pattern = arg;
3800 break;
3801#ifdef FEAT_SYN_HL
3802 case CMD_syntax:
3803 set_context_in_syntax_cmd(xp, arg);
3804 break;
3805#endif
3806#ifdef FEAT_EVAL
3807 case CMD_let:
3808 case CMD_if:
3809 case CMD_elseif:
3810 case CMD_while:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00003811 case CMD_for:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003812 case CMD_echo:
3813 case CMD_echon:
3814 case CMD_execute:
3815 case CMD_echomsg:
3816 case CMD_echoerr:
3817 case CMD_call:
3818 case CMD_return:
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003819 set_context_for_expression(xp, arg, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003820 break;
3821
3822 case CMD_unlet:
3823 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3824 arg = xp->xp_pattern + 1;
3825 xp->xp_context = EXPAND_USER_VARS;
3826 xp->xp_pattern = arg;
3827 break;
3828
3829 case CMD_function:
3830 case CMD_delfunction:
3831 xp->xp_context = EXPAND_USER_FUNC;
3832 xp->xp_pattern = arg;
3833 break;
3834
3835 case CMD_echohl:
Bram Moolenaar4f688582007-07-24 12:34:30 +00003836 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003837 break;
3838#endif
3839 case CMD_highlight:
3840 set_context_in_highlight_cmd(xp, arg);
3841 break;
Bram Moolenaarf4580d82009-03-18 11:52:53 +00003842#ifdef FEAT_CSCOPE
3843 case CMD_cscope:
Bram Moolenaar7bfef802009-04-22 14:25:01 +00003844 case CMD_lcscope:
3845 case CMD_scscope:
3846 set_context_in_cscope_cmd(xp, arg, ea.cmdidx);
Bram Moolenaarf4580d82009-03-18 11:52:53 +00003847 break;
3848#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00003849#ifdef FEAT_SIGNS
3850 case CMD_sign:
3851 set_context_in_sign_cmd(xp, arg);
3852 break;
3853#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854#ifdef FEAT_LISTCMDS
3855 case CMD_bdelete:
3856 case CMD_bwipeout:
3857 case CMD_bunload:
3858 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3859 arg = xp->xp_pattern + 1;
3860 /*FALLTHROUGH*/
3861 case CMD_buffer:
3862 case CMD_sbuffer:
3863 case CMD_checktime:
3864 xp->xp_context = EXPAND_BUFFERS;
3865 xp->xp_pattern = arg;
3866 break;
3867#endif
3868#ifdef FEAT_USR_CMDS
3869 case CMD_USER:
3870 case CMD_USER_BUF:
3871 if (compl != EXPAND_NOTHING)
3872 {
3873 /* XFILE: file names are handled above */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003874 if (!(ea.argt & XFILE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003875 {
3876# ifdef FEAT_MENU
3877 if (compl == EXPAND_MENUS)
3878 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
3879# endif
3880 if (compl == EXPAND_COMMANDS)
3881 return arg;
3882 if (compl == EXPAND_MAPPINGS)
3883 return set_context_in_map_cmd(xp, (char_u *)"map",
3884 arg, forceit, FALSE, FALSE, CMD_map);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003885 /* Find start of last argument. */
3886 p = arg;
3887 while (*p)
3888 {
3889 if (*p == ' ')
Bram Moolenaar848f8762012-07-25 17:22:23 +02003890 /* argument starts after a space */
3891 arg = p + 1;
Bram Moolenaara07c8312012-07-27 21:12:07 +02003892 else if (*p == '\\' && *(p + 1) != NUL)
3893 ++p; /* skip over escaped character */
3894 mb_ptr_adv(p);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003895 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896 xp->xp_pattern = arg;
3897 }
3898 xp->xp_context = compl;
3899 }
3900 break;
3901#endif
3902 case CMD_map: case CMD_noremap:
3903 case CMD_nmap: case CMD_nnoremap:
3904 case CMD_vmap: case CMD_vnoremap:
3905 case CMD_omap: case CMD_onoremap:
3906 case CMD_imap: case CMD_inoremap:
3907 case CMD_cmap: case CMD_cnoremap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003908 case CMD_lmap: case CMD_lnoremap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02003909 case CMD_smap: case CMD_snoremap:
3910 case CMD_xmap: case CMD_xnoremap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003911 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003912 FALSE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003913 case CMD_unmap:
3914 case CMD_nunmap:
3915 case CMD_vunmap:
3916 case CMD_ounmap:
3917 case CMD_iunmap:
3918 case CMD_cunmap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003919 case CMD_lunmap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02003920 case CMD_sunmap:
3921 case CMD_xunmap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003922 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003923 FALSE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003924 case CMD_abbreviate: case CMD_noreabbrev:
3925 case CMD_cabbrev: case CMD_cnoreabbrev:
3926 case CMD_iabbrev: case CMD_inoreabbrev:
3927 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003928 TRUE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929 case CMD_unabbreviate:
3930 case CMD_cunabbrev:
3931 case CMD_iunabbrev:
3932 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003933 TRUE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003934#ifdef FEAT_MENU
3935 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
3936 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
3937 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
3938 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
3939 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
3940 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
3941 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
3942 case CMD_tmenu: case CMD_tunmenu:
3943 case CMD_popup: case CMD_tearoff: case CMD_emenu:
3944 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
3945#endif
3946
3947 case CMD_colorscheme:
3948 xp->xp_context = EXPAND_COLORS;
3949 xp->xp_pattern = arg;
3950 break;
3951
3952 case CMD_compiler:
3953 xp->xp_context = EXPAND_COMPILER;
3954 xp->xp_pattern = arg;
3955 break;
3956
Bram Moolenaar883f5d02010-06-21 06:24:34 +02003957 case CMD_ownsyntax:
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02003958 xp->xp_context = EXPAND_OWNSYNTAX;
3959 xp->xp_pattern = arg;
3960 break;
3961
3962 case CMD_setfiletype:
Bram Moolenaar883f5d02010-06-21 06:24:34 +02003963 xp->xp_context = EXPAND_FILETYPE;
3964 xp->xp_pattern = arg;
3965 break;
3966
Bram Moolenaar071d4272004-06-13 20:20:40 +00003967#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
3968 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
3969 case CMD_language:
Bram Moolenaara660dc82011-05-25 12:51:22 +02003970 p = skiptowhite(arg);
3971 if (*p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003972 {
3973 xp->xp_context = EXPAND_LANGUAGE;
3974 xp->xp_pattern = arg;
3975 }
3976 else
Bram Moolenaara660dc82011-05-25 12:51:22 +02003977 {
3978 if ( STRNCMP(arg, "messages", p - arg) == 0
3979 || STRNCMP(arg, "ctype", p - arg) == 0
3980 || STRNCMP(arg, "time", p - arg) == 0)
3981 {
3982 xp->xp_context = EXPAND_LOCALES;
3983 xp->xp_pattern = skipwhite(p);
3984 }
3985 else
3986 xp->xp_context = EXPAND_NOTHING;
3987 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003988 break;
3989#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01003990#if defined(FEAT_PROFILE)
3991 case CMD_profile:
3992 set_context_in_profile_cmd(xp, arg);
3993 break;
3994#endif
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01003995 case CMD_behave:
3996 xp->xp_context = EXPAND_BEHAVE;
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02003997 xp->xp_pattern = arg;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01003998 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003999
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004000#if defined(FEAT_CMDHIST)
4001 case CMD_history:
4002 xp->xp_context = EXPAND_HISTORY;
4003 xp->xp_pattern = arg;
4004 break;
4005#endif
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02004006#if defined(FEAT_PROFILE)
4007 case CMD_syntime:
4008 xp->xp_context = EXPAND_SYNTIME;
4009 xp->xp_pattern = arg;
4010 break;
4011#endif
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004012
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013#endif /* FEAT_CMDL_COMPL */
4014
4015 default:
4016 break;
4017 }
4018 return NULL;
4019}
4020
4021/*
4022 * skip a range specifier of the form: addr [,addr] [;addr] ..
4023 *
4024 * Backslashed delimiters after / or ? will be skipped, and commands will
4025 * not be expanded between /'s and ?'s or after "'".
4026 *
Bram Moolenaardf177f62005-02-22 08:39:57 +00004027 * Also skip white space and ":" characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028 * Returns the "cmd" pointer advanced to beyond the range.
4029 */
4030 char_u *
4031skip_range(cmd, ctx)
4032 char_u *cmd;
4033 int *ctx; /* pointer to xp_context or NULL */
4034{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004035 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004036
Bram Moolenaardf177f62005-02-22 08:39:57 +00004037 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004038 {
4039 if (*cmd == '\'')
4040 {
4041 if (*++cmd == NUL && ctx != NULL)
4042 *ctx = EXPAND_NOTHING;
4043 }
4044 else if (*cmd == '/' || *cmd == '?')
4045 {
4046 delim = *cmd++;
4047 while (*cmd != NUL && *cmd != delim)
4048 if (*cmd++ == '\\' && *cmd != NUL)
4049 ++cmd;
4050 if (*cmd == NUL && ctx != NULL)
4051 *ctx = EXPAND_NOTHING;
4052 }
4053 if (*cmd != NUL)
4054 ++cmd;
4055 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004056
4057 /* Skip ":" and white space. */
4058 while (*cmd == ':')
4059 cmd = skipwhite(cmd + 1);
4060
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061 return cmd;
4062}
4063
4064/*
4065 * get a single EX address
4066 *
4067 * Set ptr to the next character after the part that was interpreted.
4068 * Set ptr to NULL when an error is encountered.
4069 *
4070 * Return MAXLNUM when no Ex address was found.
4071 */
4072 static linenr_T
4073get_address(ptr, skip, to_other_file)
4074 char_u **ptr;
4075 int skip; /* only skip the address, don't use it */
4076 int to_other_file; /* flag: may jump to other file */
4077{
4078 int c;
4079 int i;
4080 long n;
4081 char_u *cmd;
4082 pos_T pos;
4083 pos_T *fp;
4084 linenr_T lnum;
4085
4086 cmd = skipwhite(*ptr);
4087 lnum = MAXLNUM;
4088 do
4089 {
4090 switch (*cmd)
4091 {
4092 case '.': /* '.' - Cursor position */
4093 ++cmd;
4094 lnum = curwin->w_cursor.lnum;
4095 break;
4096
4097 case '$': /* '$' - last line */
4098 ++cmd;
4099 lnum = curbuf->b_ml.ml_line_count;
4100 break;
4101
4102 case '\'': /* ''' - mark */
4103 if (*++cmd == NUL)
4104 {
4105 cmd = NULL;
4106 goto error;
4107 }
4108 if (skip)
4109 ++cmd;
4110 else
4111 {
4112 /* Only accept a mark in another file when it is
4113 * used by itself: ":'M". */
4114 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
4115 ++cmd;
4116 if (fp == (pos_T *)-1)
4117 /* Jumped to another file. */
4118 lnum = curwin->w_cursor.lnum;
4119 else
4120 {
4121 if (check_mark(fp) == FAIL)
4122 {
4123 cmd = NULL;
4124 goto error;
4125 }
4126 lnum = fp->lnum;
4127 }
4128 }
4129 break;
4130
4131 case '/':
4132 case '?': /* '/' or '?' - search */
4133 c = *cmd++;
4134 if (skip) /* skip "/pat/" */
4135 {
4136 cmd = skip_regexp(cmd, c, (int)p_magic, NULL);
4137 if (*cmd == c)
4138 ++cmd;
4139 }
4140 else
4141 {
4142 pos = curwin->w_cursor; /* save curwin->w_cursor */
4143 /*
4144 * When '/' or '?' follows another address, start
4145 * from there.
4146 */
4147 if (lnum != MAXLNUM)
4148 curwin->w_cursor.lnum = lnum;
4149 /*
4150 * Start a forward search at the end of the line.
4151 * Start a backward search at the start of the line.
4152 * This makes sure we never match in the current
4153 * line, and can match anywhere in the
4154 * next/previous line.
4155 */
4156 if (c == '/')
4157 curwin->w_cursor.col = MAXCOL;
4158 else
4159 curwin->w_cursor.col = 0;
4160 searchcmdlen = 0;
4161 if (!do_search(NULL, c, cmd, 1L,
Bram Moolenaaraad86642008-03-10 20:34:59 +00004162 SEARCH_HIS | SEARCH_MSG, NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004163 {
4164 curwin->w_cursor = pos;
4165 cmd = NULL;
4166 goto error;
4167 }
4168 lnum = curwin->w_cursor.lnum;
4169 curwin->w_cursor = pos;
4170 /* adjust command string pointer */
4171 cmd += searchcmdlen;
4172 }
4173 break;
4174
4175 case '\\': /* "\?", "\/" or "\&", repeat search */
4176 ++cmd;
4177 if (*cmd == '&')
4178 i = RE_SUBST;
4179 else if (*cmd == '?' || *cmd == '/')
4180 i = RE_SEARCH;
4181 else
4182 {
4183 EMSG(_(e_backslash));
4184 cmd = NULL;
4185 goto error;
4186 }
4187
4188 if (!skip)
4189 {
4190 /*
4191 * When search follows another address, start from
4192 * there.
4193 */
4194 if (lnum != MAXLNUM)
4195 pos.lnum = lnum;
4196 else
4197 pos.lnum = curwin->w_cursor.lnum;
4198
4199 /*
4200 * Start the search just like for the above
4201 * do_search().
4202 */
4203 if (*cmd != '?')
4204 pos.col = MAXCOL;
4205 else
4206 pos.col = 0;
4207 if (searchit(curwin, curbuf, &pos,
4208 *cmd == '?' ? BACKWARD : FORWARD,
Bram Moolenaaraad86642008-03-10 20:34:59 +00004209 (char_u *)"", 1L, SEARCH_MSG,
Bram Moolenaar76929292008-01-06 19:07:36 +00004210 i, (linenr_T)0, NULL) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004211 lnum = pos.lnum;
4212 else
4213 {
4214 cmd = NULL;
4215 goto error;
4216 }
4217 }
4218 ++cmd;
4219 break;
4220
4221 default:
4222 if (VIM_ISDIGIT(*cmd)) /* absolute line number */
4223 lnum = getdigits(&cmd);
4224 }
4225
4226 for (;;)
4227 {
4228 cmd = skipwhite(cmd);
4229 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4230 break;
4231
4232 if (lnum == MAXLNUM)
4233 lnum = curwin->w_cursor.lnum; /* "+1" is same as ".+1" */
4234 if (VIM_ISDIGIT(*cmd))
4235 i = '+'; /* "number" is same as "+number" */
4236 else
4237 i = *cmd++;
4238 if (!VIM_ISDIGIT(*cmd)) /* '+' is '+1', but '+0' is not '+1' */
4239 n = 1;
4240 else
4241 n = getdigits(&cmd);
4242 if (i == '-')
4243 lnum -= n;
4244 else
4245 lnum += n;
4246 }
4247 } while (*cmd == '/' || *cmd == '?');
4248
4249error:
4250 *ptr = cmd;
4251 return lnum;
4252}
4253
4254/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004255 * Get flags from an Ex command argument.
4256 */
4257 static void
4258get_flags(eap)
4259 exarg_T *eap;
4260{
4261 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4262 {
4263 if (*eap->arg == 'l')
4264 eap->flags |= EXFLAG_LIST;
4265 else if (*eap->arg == 'p')
4266 eap->flags |= EXFLAG_PRINT;
4267 else
4268 eap->flags |= EXFLAG_NR;
4269 eap->arg = skipwhite(eap->arg + 1);
4270 }
4271}
4272
4273/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274 * Function called for command which is Not Implemented. NI!
4275 */
4276 void
4277ex_ni(eap)
4278 exarg_T *eap;
4279{
4280 if (!eap->skip)
4281 eap->errmsg = (char_u *)N_("E319: Sorry, the command is not available in this version");
4282}
4283
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004284#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004285/*
4286 * Function called for script command which is Not Implemented. NI!
4287 * Skips over ":perl <<EOF" constructs.
4288 */
4289 static void
4290ex_script_ni(eap)
4291 exarg_T *eap;
4292{
4293 if (!eap->skip)
4294 ex_ni(eap);
4295 else
4296 vim_free(script_get(eap, eap->arg));
4297}
4298#endif
4299
4300/*
4301 * Check range in Ex command for validity.
4302 * Return NULL when valid, error message when invalid.
4303 */
4304 static char_u *
4305invalid_range(eap)
4306 exarg_T *eap;
4307{
4308 if ( eap->line1 < 0
4309 || eap->line2 < 0
4310 || eap->line1 > eap->line2
4311 || ((eap->argt & RANGE)
4312 && !(eap->argt & NOTADR)
4313 && eap->line2 > curbuf->b_ml.ml_line_count
4314#ifdef FEAT_DIFF
4315 + (eap->cmdidx == CMD_diffget)
4316#endif
4317 ))
4318 return (char_u *)_(e_invrange);
4319 return NULL;
4320}
4321
4322/*
4323 * Correct the range for zero line number, if required.
4324 */
4325 static void
4326correct_range(eap)
4327 exarg_T *eap;
4328{
4329 if (!(eap->argt & ZEROR)) /* zero in range not allowed */
4330 {
4331 if (eap->line1 == 0)
4332 eap->line1 = 1;
4333 if (eap->line2 == 0)
4334 eap->line2 = 1;
4335 }
4336}
4337
Bram Moolenaar748bf032005-02-02 23:04:36 +00004338#ifdef FEAT_QUICKFIX
4339static char_u *skip_grep_pat __ARGS((exarg_T *eap));
4340
4341/*
4342 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4343 * pattern. Otherwise return eap->arg.
4344 */
4345 static char_u *
4346skip_grep_pat(eap)
4347 exarg_T *eap;
4348{
4349 char_u *p = eap->arg;
4350
Bram Moolenaara37420f2006-02-04 22:37:47 +00004351 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4352 || eap->cmdidx == CMD_vimgrepadd
4353 || eap->cmdidx == CMD_lvimgrepadd
4354 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004355 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004356 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004357 if (p == NULL)
4358 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004359 }
4360 return p;
4361}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004362
4363/*
4364 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4365 * in the command line, so that things like % get expanded.
4366 */
4367 static char_u *
4368replace_makeprg(eap, p, cmdlinep)
4369 exarg_T *eap;
4370 char_u *p;
4371 char_u **cmdlinep;
4372{
4373 char_u *new_cmdline;
4374 char_u *program;
4375 char_u *pos;
4376 char_u *ptr;
4377 int len;
4378 int i;
4379
4380 /*
4381 * Don't do it when ":vimgrep" is used for ":grep".
4382 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004383 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4384 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4385 || eap->cmdidx == CMD_grepadd
4386 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004387 && !grep_internal(eap->cmdidx))
4388 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004389 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4390 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004391 {
4392 if (*curbuf->b_p_gp == NUL)
4393 program = p_gp;
4394 else
4395 program = curbuf->b_p_gp;
4396 }
4397 else
4398 {
4399 if (*curbuf->b_p_mp == NUL)
4400 program = p_mp;
4401 else
4402 program = curbuf->b_p_mp;
4403 }
4404
4405 p = skipwhite(p);
4406
4407 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4408 {
4409 /* replace $* by given arguments */
4410 i = 1;
4411 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4412 ++i;
4413 len = (int)STRLEN(p);
4414 new_cmdline = alloc((int)(STRLEN(program) + i * (len - 2) + 1));
4415 if (new_cmdline == NULL)
4416 return NULL; /* out of memory */
4417 ptr = new_cmdline;
4418 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4419 {
4420 i = (int)(pos - program);
4421 STRNCPY(ptr, program, i);
4422 STRCPY(ptr += i, p);
4423 ptr += len;
4424 program = pos + 2;
4425 }
4426 STRCPY(ptr, program);
4427 }
4428 else
4429 {
4430 new_cmdline = alloc((int)(STRLEN(program) + STRLEN(p) + 2));
4431 if (new_cmdline == NULL)
4432 return NULL; /* out of memory */
4433 STRCPY(new_cmdline, program);
4434 STRCAT(new_cmdline, " ");
4435 STRCAT(new_cmdline, p);
4436 }
4437 msg_make(p);
4438
4439 /* 'eap->cmd' is not set here, because it is not used at CMD_make */
4440 vim_free(*cmdlinep);
4441 *cmdlinep = new_cmdline;
4442 p = new_cmdline;
4443 }
4444 return p;
4445}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004446#endif
4447
Bram Moolenaar071d4272004-06-13 20:20:40 +00004448/*
4449 * Expand file name in Ex command argument.
4450 * Return FAIL for failure, OK otherwise.
4451 */
4452 int
4453expand_filename(eap, cmdlinep, errormsgp)
4454 exarg_T *eap;
4455 char_u **cmdlinep;
4456 char_u **errormsgp;
4457{
4458 int has_wildcards; /* need to expand wildcards */
4459 char_u *repl;
4460 int srclen;
4461 char_u *p;
4462 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004463 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004464
Bram Moolenaar748bf032005-02-02 23:04:36 +00004465#ifdef FEAT_QUICKFIX
4466 /* Skip a regexp pattern for ":vimgrep[add] pat file..." */
4467 p = skip_grep_pat(eap);
4468#else
4469 p = eap->arg;
4470#endif
4471
Bram Moolenaar071d4272004-06-13 20:20:40 +00004472 /*
4473 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4474 * the file name contains a wildcard it should not cause expanding.
4475 * (it will be expanded anyway if there is a wildcard before replacing).
4476 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004477 has_wildcards = mch_has_wildcard(p);
4478 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004479 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004480#ifdef FEAT_EVAL
4481 /* Skip over `=expr`, wildcards in it are not expanded. */
4482 if (p[0] == '`' && p[1] == '=')
4483 {
4484 p += 2;
4485 (void)skip_expr(&p);
4486 if (*p == '`')
4487 ++p;
4488 continue;
4489 }
4490#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004491 /*
4492 * Quick check if this cannot be the start of a special string.
4493 * Also removes backslash before '%', '#' and '<'.
4494 */
4495 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4496 {
4497 ++p;
4498 continue;
4499 }
4500
4501 /*
4502 * Try to find a match at this position.
4503 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004504 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
4505 errormsgp, &escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004506 if (*errormsgp != NULL) /* error detected */
4507 return FAIL;
4508 if (repl == NULL) /* no match found */
4509 {
4510 p += srclen;
4511 continue;
4512 }
4513
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004514 /* Wildcards won't be expanded below, the replacement is taken
4515 * literally. But do expand "~/file", "~user/file" and "$HOME/file". */
4516 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4517 {
4518 char_u *l = repl;
4519
4520 repl = expand_env_save(repl);
4521 vim_free(l);
4522 }
4523
Bram Moolenaar63b92542007-03-27 14:57:09 +00004524 /* Need to escape white space et al. with a backslash.
4525 * Don't do this for:
4526 * - replacement that already has been escaped: "##"
4527 * - shell commands (may have to use quotes instead).
4528 * - non-unix systems when there is a single argument (spaces don't
4529 * separate arguments then).
4530 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004531 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00004532 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00004533 && eap->cmdidx != CMD_bang
4534 && eap->cmdidx != CMD_make
Bram Moolenaara37420f2006-02-04 22:37:47 +00004535 && eap->cmdidx != CMD_lmake
Bram Moolenaar071d4272004-06-13 20:20:40 +00004536 && eap->cmdidx != CMD_grep
Bram Moolenaara37420f2006-02-04 22:37:47 +00004537 && eap->cmdidx != CMD_lgrep
Bram Moolenaar071d4272004-06-13 20:20:40 +00004538 && eap->cmdidx != CMD_grepadd
Bram Moolenaara37420f2006-02-04 22:37:47 +00004539 && eap->cmdidx != CMD_lgrepadd
Bram Moolenaar071d4272004-06-13 20:20:40 +00004540#ifndef UNIX
4541 && !(eap->argt & NOSPC)
4542#endif
4543 )
4544 {
4545 char_u *l;
4546#ifdef BACKSLASH_IN_FILENAME
4547 /* Don't escape a backslash here, because rem_backslash() doesn't
4548 * remove it later. */
4549 static char_u *nobslash = (char_u *)" \t\"|";
4550# define ESCAPE_CHARS nobslash
4551#else
4552# define ESCAPE_CHARS escape_chars
4553#endif
4554
4555 for (l = repl; *l; ++l)
4556 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
4557 {
4558 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
4559 if (l != NULL)
4560 {
4561 vim_free(repl);
4562 repl = l;
4563 }
4564 break;
4565 }
4566 }
4567
4568 /* For a shell command a '!' must be escaped. */
4569 if ((eap->usefilter || eap->cmdidx == CMD_bang)
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004570 && vim_strpbrk(repl, (char_u *)"!&;()<>") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004571 {
4572 char_u *l;
4573
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004574 l = vim_strsave_escaped(repl, (char_u *)"!&;()<>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004575 if (l != NULL)
4576 {
4577 vim_free(repl);
4578 repl = l;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004579 /* For a sh-like shell escape "!" another time. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004580 if (strstr((char *)p_sh, "sh") != NULL)
4581 {
4582 l = vim_strsave_escaped(repl, (char_u *)"!");
4583 if (l != NULL)
4584 {
4585 vim_free(repl);
4586 repl = l;
4587 }
4588 }
4589 }
4590 }
4591
4592 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
4593 vim_free(repl);
4594 if (p == NULL)
4595 return FAIL;
4596 }
4597
4598 /*
4599 * One file argument: Expand wildcards.
4600 * Don't do this with ":r !command" or ":w !command".
4601 */
4602 if ((eap->argt & NOSPC) && !eap->usefilter)
4603 {
4604 /*
4605 * May do this twice:
4606 * 1. Replace environment variables.
4607 * 2. Replace any other wildcards, remove backslashes.
4608 */
4609 for (n = 1; n <= 2; ++n)
4610 {
4611 if (n == 2)
4612 {
4613#ifdef UNIX
4614 /*
4615 * Only for Unix we check for more than one file name.
4616 * For other systems spaces are considered to be part
4617 * of the file name.
4618 * Only check here if there is no wildcard, otherwise
4619 * ExpandOne() will check for errors. This allows
4620 * ":e `ls ve*.c`" on Unix.
4621 */
4622 if (!has_wildcards)
4623 for (p = eap->arg; *p; ++p)
4624 {
4625 /* skip escaped characters */
4626 if (p[1] && (*p == '\\' || *p == Ctrl_V))
4627 ++p;
4628 else if (vim_iswhite(*p))
4629 {
4630 *errormsgp = (char_u *)_("E172: Only one file name allowed");
4631 return FAIL;
4632 }
4633 }
4634#endif
4635
4636 /*
4637 * Halve the number of backslashes (this is Vi compatible).
4638 * For Unix and OS/2, when wildcards are expanded, this is
4639 * done by ExpandOne() below.
4640 */
4641#if defined(UNIX) || defined(OS2)
4642 if (!has_wildcards)
4643#endif
4644 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004645 }
4646
4647 if (has_wildcards)
4648 {
4649 if (n == 1)
4650 {
4651 /*
4652 * First loop: May expand environment variables. This
4653 * can be done much faster with expand_env() than with
4654 * something else (e.g., calling a shell).
4655 * After expanding environment variables, check again
4656 * if there are still wildcards present.
4657 */
4658 if (vim_strchr(eap->arg, '$') != NULL
4659 || vim_strchr(eap->arg, '~') != NULL)
4660 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00004661 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004662 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004663 has_wildcards = mch_has_wildcard(NameBuff);
4664 p = NameBuff;
4665 }
4666 else
4667 p = NULL;
4668 }
4669 else /* n == 2 */
4670 {
4671 expand_T xpc;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004672 int options = WILD_LIST_NOTFOUND|WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004673
4674 ExpandInit(&xpc);
4675 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004676 if (p_wic)
4677 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004678 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01004679 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004680 if (p == NULL)
4681 return FAIL;
4682 }
4683 if (p != NULL)
4684 {
4685 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
4686 p, cmdlinep);
4687 if (n == 2) /* p came from ExpandOne() */
4688 vim_free(p);
4689 }
4690 }
4691 }
4692 }
4693 return OK;
4694}
4695
4696/*
4697 * Replace part of the command line, keeping eap->cmd, eap->arg and
4698 * eap->nextcmd correct.
4699 * "src" points to the part that is to be replaced, of length "srclen".
4700 * "repl" is the replacement string.
4701 * Returns a pointer to the character after the replaced string.
4702 * Returns NULL for failure.
4703 */
4704 static char_u *
4705repl_cmdline(eap, src, srclen, repl, cmdlinep)
4706 exarg_T *eap;
4707 char_u *src;
4708 int srclen;
4709 char_u *repl;
4710 char_u **cmdlinep;
4711{
4712 int len;
4713 int i;
4714 char_u *new_cmdline;
4715
4716 /*
4717 * The new command line is build in new_cmdline[].
4718 * First allocate it.
4719 * Careful: a "+cmd" argument may have been NUL terminated.
4720 */
4721 len = (int)STRLEN(repl);
4722 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004723 if (eap->nextcmd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004724 i += (int)STRLEN(eap->nextcmd);/* add space for next command */
4725 if ((new_cmdline = alloc((unsigned)i)) == NULL)
4726 return NULL; /* out of memory! */
4727
4728 /*
4729 * Copy the stuff before the expanded part.
4730 * Copy the expanded stuff.
4731 * Copy what came after the expanded part.
4732 * Copy the next commands, if there are any.
4733 */
4734 i = (int)(src - *cmdlinep); /* length of part before match */
4735 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00004736
Bram Moolenaar071d4272004-06-13 20:20:40 +00004737 mch_memmove(new_cmdline + i, repl, (size_t)len);
4738 i += len; /* remember the end of the string */
4739 STRCPY(new_cmdline + i, src + srclen);
4740 src = new_cmdline + i; /* remember where to continue */
4741
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004742 if (eap->nextcmd != NULL) /* append next command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004743 {
4744 i = (int)STRLEN(new_cmdline) + 1;
4745 STRCPY(new_cmdline + i, eap->nextcmd);
4746 eap->nextcmd = new_cmdline + i;
4747 }
4748 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
4749 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
4750 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
4751 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
4752 vim_free(*cmdlinep);
4753 *cmdlinep = new_cmdline;
4754
4755 return src;
4756}
4757
4758/*
4759 * Check for '|' to separate commands and '"' to start comments.
4760 */
4761 void
4762separate_nextcmd(eap)
4763 exarg_T *eap;
4764{
4765 char_u *p;
4766
Bram Moolenaar86b68352004-12-27 21:59:20 +00004767#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004768 p = skip_grep_pat(eap);
4769#else
4770 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004771#endif
4772
4773 for ( ; *p; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004774 {
4775 if (*p == Ctrl_V)
4776 {
4777 if (eap->argt & (USECTRLV | XFILE))
4778 ++p; /* skip CTRL-V and next char */
4779 else
Bram Moolenaarb0db5692007-08-14 20:54:49 +00004780 /* remove CTRL-V and skip next char */
Bram Moolenaara7241f52008-06-24 20:39:31 +00004781 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004782 if (*p == NUL) /* stop at NUL after CTRL-V */
4783 break;
4784 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004785
4786#ifdef FEAT_EVAL
4787 /* Skip over `=expr` when wildcards are expanded. */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004788 else if (p[0] == '`' && p[1] == '=' && (eap->argt & XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004789 {
4790 p += 2;
4791 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004792 }
4793#endif
4794
Bram Moolenaar071d4272004-06-13 20:20:40 +00004795 /* Check for '"': start of comment or '|': next command */
4796 /* :@" and :*" do not start a comment!
4797 * :redir @" doesn't either. */
4798 else if ((*p == '"' && !(eap->argt & NOTRLCOM)
4799 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
4800 || p != eap->arg)
4801 && (eap->cmdidx != CMD_redir
4802 || p != eap->arg + 1 || p[-1] != '@'))
4803 || *p == '|' || *p == '\n')
4804 {
4805 /*
4806 * We remove the '\' before the '|', unless USECTRLV is used
4807 * AND 'b' is present in 'cpoptions'.
4808 */
4809 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
4810 || !(eap->argt & USECTRLV)) && *(p - 1) == '\\')
4811 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00004812 STRMOVE(p - 1, p); /* remove the '\' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004813 --p;
4814 }
4815 else
4816 {
4817 eap->nextcmd = check_nextcmd(p);
4818 *p = NUL;
4819 break;
4820 }
4821 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004822 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004823
Bram Moolenaar071d4272004-06-13 20:20:40 +00004824 if (!(eap->argt & NOTRLCOM)) /* remove trailing spaces */
4825 del_trailing_spaces(eap->arg);
4826}
4827
4828/*
4829 * get + command from ex argument
4830 */
4831 static char_u *
4832getargcmd(argp)
4833 char_u **argp;
4834{
4835 char_u *arg = *argp;
4836 char_u *command = NULL;
4837
4838 if (*arg == '+') /* +[command] */
4839 {
4840 ++arg;
4841 if (vim_isspace(*arg))
4842 command = dollar_command;
4843 else
4844 {
4845 command = arg;
4846 arg = skip_cmd_arg(command, TRUE);
4847 if (*arg != NUL)
4848 *arg++ = NUL; /* terminate command with NUL */
4849 }
4850
4851 arg = skipwhite(arg); /* skip over spaces */
4852 *argp = arg;
4853 }
4854 return command;
4855}
4856
4857/*
4858 * Find end of "+command" argument. Skip over "\ " and "\\".
4859 */
4860 static char_u *
4861skip_cmd_arg(p, rembs)
4862 char_u *p;
4863 int rembs; /* TRUE to halve the number of backslashes */
4864{
4865 while (*p && !vim_isspace(*p))
4866 {
4867 if (*p == '\\' && p[1] != NUL)
4868 {
4869 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00004870 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004871 else
4872 ++p;
4873 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004874 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004875 }
4876 return p;
4877}
4878
4879/*
4880 * Get "++opt=arg" argument.
4881 * Return FAIL or OK.
4882 */
4883 static int
4884getargopt(eap)
4885 exarg_T *eap;
4886{
4887 char_u *arg = eap->arg + 2;
4888 int *pp = NULL;
4889#ifdef FEAT_MBYTE
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004890 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891 char_u *p;
4892#endif
4893
4894 /* ":edit ++[no]bin[ary] file" */
4895 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
4896 {
4897 if (*arg == 'n')
4898 {
4899 arg += 2;
4900 eap->force_bin = FORCE_NOBIN;
4901 }
4902 else
4903 eap->force_bin = FORCE_BIN;
4904 if (!checkforcmd(&arg, "binary", 3))
4905 return FAIL;
4906 eap->arg = skipwhite(arg);
4907 return OK;
4908 }
4909
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004910 /* ":read ++edit file" */
4911 if (STRNCMP(arg, "edit", 4) == 0)
4912 {
4913 eap->read_edit = TRUE;
4914 eap->arg = skipwhite(arg + 4);
4915 return OK;
4916 }
4917
Bram Moolenaar071d4272004-06-13 20:20:40 +00004918 if (STRNCMP(arg, "ff", 2) == 0)
4919 {
4920 arg += 2;
4921 pp = &eap->force_ff;
4922 }
4923 else if (STRNCMP(arg, "fileformat", 10) == 0)
4924 {
4925 arg += 10;
4926 pp = &eap->force_ff;
4927 }
4928#ifdef FEAT_MBYTE
4929 else if (STRNCMP(arg, "enc", 3) == 0)
4930 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01004931 if (STRNCMP(arg, "encoding", 8) == 0)
4932 arg += 8;
4933 else
4934 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004935 pp = &eap->force_enc;
4936 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004937 else if (STRNCMP(arg, "bad", 3) == 0)
4938 {
4939 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004940 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004941 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004942#endif
4943
4944 if (pp == NULL || *arg != '=')
4945 return FAIL;
4946
4947 ++arg;
4948 *pp = (int)(arg - eap->cmd);
4949 arg = skip_cmd_arg(arg, FALSE);
4950 eap->arg = skipwhite(arg);
4951 *arg = NUL;
4952
4953#ifdef FEAT_MBYTE
4954 if (pp == &eap->force_ff)
4955 {
4956#endif
4957 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
4958 return FAIL;
4959#ifdef FEAT_MBYTE
4960 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004961 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962 {
4963 /* Make 'fileencoding' lower case. */
4964 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
4965 *p = TOLOWER_ASC(*p);
4966 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004967 else
4968 {
4969 /* Check ++bad= argument. Must be a single-byte character, "keep" or
4970 * "drop". */
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004971 p = eap->cmd + bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004972 if (STRICMP(p, "keep") == 0)
4973 eap->bad_char = BAD_KEEP;
4974 else if (STRICMP(p, "drop") == 0)
4975 eap->bad_char = BAD_DROP;
4976 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
4977 eap->bad_char = *p;
4978 else
4979 return FAIL;
4980 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981#endif
4982
4983 return OK;
4984}
4985
4986/*
4987 * ":abbreviate" and friends.
4988 */
4989 static void
4990ex_abbreviate(eap)
4991 exarg_T *eap;
4992{
4993 do_exmap(eap, TRUE); /* almost the same as mapping */
4994}
4995
4996/*
4997 * ":map" and friends.
4998 */
4999 static void
5000ex_map(eap)
5001 exarg_T *eap;
5002{
5003 /*
5004 * If we are sourcing .exrc or .vimrc in current directory we
5005 * print the mappings for security reasons.
5006 */
5007 if (secure)
5008 {
5009 secure = 2;
5010 msg_outtrans(eap->cmd);
5011 msg_putchar('\n');
5012 }
5013 do_exmap(eap, FALSE);
5014}
5015
5016/*
5017 * ":unmap" and friends.
5018 */
5019 static void
5020ex_unmap(eap)
5021 exarg_T *eap;
5022{
5023 do_exmap(eap, FALSE);
5024}
5025
5026/*
5027 * ":mapclear" and friends.
5028 */
5029 static void
5030ex_mapclear(eap)
5031 exarg_T *eap;
5032{
5033 map_clear(eap->cmd, eap->arg, eap->forceit, FALSE);
5034}
5035
5036/*
5037 * ":abclear" and friends.
5038 */
5039 static void
5040ex_abclear(eap)
5041 exarg_T *eap;
5042{
5043 map_clear(eap->cmd, eap->arg, TRUE, TRUE);
5044}
5045
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005046#if defined(FEAT_AUTOCMD) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047 static void
5048ex_autocmd(eap)
5049 exarg_T *eap;
5050{
5051 /*
5052 * Disallow auto commands from .exrc and .vimrc in current
5053 * directory for security reasons.
5054 */
5055 if (secure)
5056 {
5057 secure = 2;
5058 eap->errmsg = e_curdir;
5059 }
5060 else if (eap->cmdidx == CMD_autocmd)
5061 do_autocmd(eap->arg, eap->forceit);
5062 else
5063 do_augroup(eap->arg, eap->forceit);
5064}
5065
5066/*
5067 * ":doautocmd": Apply the automatic commands to the current buffer.
5068 */
5069 static void
5070ex_doautocmd(eap)
5071 exarg_T *eap;
5072{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005073 char_u *arg = eap->arg;
5074 int call_do_modelines = check_nomodeline(&arg);
5075
5076 (void)do_doautocmd(arg, TRUE);
5077 if (call_do_modelines) /* Only when there is no <nomodeline>. */
5078 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079}
5080#endif
5081
5082#ifdef FEAT_LISTCMDS
5083/*
5084 * :[N]bunload[!] [N] [bufname] unload buffer
5085 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
5086 * :[N]bwipeout[!] [N] [bufname] delete buffer really
5087 */
5088 static void
5089ex_bunload(eap)
5090 exarg_T *eap;
5091{
5092 eap->errmsg = do_bufdel(
5093 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
5094 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
5095 : DOBUF_UNLOAD, eap->arg,
5096 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
5097}
5098
5099/*
5100 * :[N]buffer [N] to buffer N
5101 * :[N]sbuffer [N] to buffer N
5102 */
5103 static void
5104ex_buffer(eap)
5105 exarg_T *eap;
5106{
5107 if (*eap->arg)
5108 eap->errmsg = e_trailing;
5109 else
5110 {
5111 if (eap->addr_count == 0) /* default is current buffer */
5112 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
5113 else
5114 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
5115 }
5116}
5117
5118/*
5119 * :[N]bmodified [N] to next mod. buffer
5120 * :[N]sbmodified [N] to next mod. buffer
5121 */
5122 static void
5123ex_bmodified(eap)
5124 exarg_T *eap;
5125{
5126 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
5127}
5128
5129/*
5130 * :[N]bnext [N] to next buffer
5131 * :[N]sbnext [N] split and to next buffer
5132 */
5133 static void
5134ex_bnext(eap)
5135 exarg_T *eap;
5136{
5137 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
5138}
5139
5140/*
5141 * :[N]bNext [N] to previous buffer
5142 * :[N]bprevious [N] to previous buffer
5143 * :[N]sbNext [N] split and to previous buffer
5144 * :[N]sbprevious [N] split and to previous buffer
5145 */
5146 static void
5147ex_bprevious(eap)
5148 exarg_T *eap;
5149{
5150 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
5151}
5152
5153/*
5154 * :brewind to first buffer
5155 * :bfirst to first buffer
5156 * :sbrewind split and to first buffer
5157 * :sbfirst split and to first buffer
5158 */
5159 static void
5160ex_brewind(eap)
5161 exarg_T *eap;
5162{
5163 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
5164}
5165
5166/*
5167 * :blast to last buffer
5168 * :sblast split and to last buffer
5169 */
5170 static void
5171ex_blast(eap)
5172 exarg_T *eap;
5173{
5174 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
5175}
5176#endif
5177
5178 int
5179ends_excmd(c)
5180 int c;
5181{
5182 return (c == NUL || c == '|' || c == '"' || c == '\n');
5183}
5184
5185#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5186 || defined(PROTO)
5187/*
5188 * Return the next command, after the first '|' or '\n'.
5189 * Return NULL if not found.
5190 */
5191 char_u *
5192find_nextcmd(p)
5193 char_u *p;
5194{
5195 while (*p != '|' && *p != '\n')
5196 {
5197 if (*p == NUL)
5198 return NULL;
5199 ++p;
5200 }
5201 return (p + 1);
5202}
5203#endif
5204
5205/*
5206 * Check if *p is a separator between Ex commands.
5207 * Return NULL if it isn't, (p + 1) if it is.
5208 */
5209 char_u *
5210check_nextcmd(p)
5211 char_u *p;
5212{
5213 p = skipwhite(p);
5214 if (*p == '|' || *p == '\n')
5215 return (p + 1);
5216 else
5217 return NULL;
5218}
5219
5220/*
5221 * - if there are more files to edit
5222 * - and this is the last window
5223 * - and forceit not used
5224 * - and not repeated twice on a row
5225 * return FAIL and give error message if 'message' TRUE
5226 * return OK otherwise
5227 */
5228 static int
5229check_more(message, forceit)
5230 int message; /* when FALSE check only, no messages */
5231 int forceit;
5232{
5233 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5234
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005235 if (!forceit && only_one_window()
5236 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005237 {
5238 if (message)
5239 {
5240#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
5241 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
5242 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005243 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005244
5245 if (n == 1)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02005246 vim_strncpy(buff,
5247 (char_u *)_("1 more file to edit. Quit anyway?"),
Bram Moolenaard9462e32011-04-11 21:35:11 +02005248 DIALOG_MSG_SIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005249 else
Bram Moolenaard9462e32011-04-11 21:35:11 +02005250 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005251 _("%d more files to edit. Quit anyway?"), n);
5252 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5253 return OK;
5254 return FAIL;
5255 }
5256#endif
5257 if (n == 1)
5258 EMSG(_("E173: 1 more file to edit"));
5259 else
5260 EMSGN(_("E173: %ld more files to edit"), n);
5261 quitmore = 2; /* next try to quit is allowed */
5262 }
5263 return FAIL;
5264 }
5265 return OK;
5266}
5267
5268#ifdef FEAT_CMDL_COMPL
5269/*
5270 * Function given to ExpandGeneric() to obtain the list of command names.
5271 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005272 char_u *
5273get_command_name(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00005274 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005275 int idx;
5276{
5277 if (idx >= (int)CMD_SIZE)
5278# ifdef FEAT_USR_CMDS
5279 return get_user_command_name(idx);
5280# else
5281 return NULL;
5282# endif
5283 return cmdnames[idx].cmd_name;
5284}
5285#endif
5286
5287#if defined(FEAT_USR_CMDS) || defined(PROTO)
5288static int uc_add_command __ARGS((char_u *name, size_t name_len, char_u *rep, long argt, long def, int flags, int compl, char_u *compl_arg, int force));
5289static void uc_list __ARGS((char_u *name, size_t name_len));
5290static int uc_scan_attr __ARGS((char_u *attr, size_t len, long *argt, long *def, int *flags, int *compl, char_u **compl_arg));
5291static char_u *uc_split_args __ARGS((char_u *arg, size_t *lenp));
5292static 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));
5293
5294 static int
5295uc_add_command(name, name_len, rep, argt, def, flags, compl, compl_arg, force)
5296 char_u *name;
5297 size_t name_len;
5298 char_u *rep;
5299 long argt;
5300 long def;
5301 int flags;
5302 int compl;
5303 char_u *compl_arg;
5304 int force;
5305{
5306 ucmd_T *cmd = NULL;
5307 char_u *p;
5308 int i;
5309 int cmp = 1;
5310 char_u *rep_buf = NULL;
5311 garray_T *gap;
5312
Bram Moolenaar9c102382006-05-03 21:26:49 +00005313 replace_termcodes(rep, &rep_buf, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005314 if (rep_buf == NULL)
5315 {
5316 /* Can't replace termcodes - try using the string as is */
5317 rep_buf = vim_strsave(rep);
5318
5319 /* Give up if out of memory */
5320 if (rep_buf == NULL)
5321 return FAIL;
5322 }
5323
5324 /* get address of growarray: global or in curbuf */
5325 if (flags & UC_BUFFER)
5326 {
5327 gap = &curbuf->b_ucmds;
5328 if (gap->ga_itemsize == 0)
5329 ga_init2(gap, (int)sizeof(ucmd_T), 4);
5330 }
5331 else
5332 gap = &ucmds;
5333
5334 /* Search for the command in the already defined commands. */
5335 for (i = 0; i < gap->ga_len; ++i)
5336 {
5337 size_t len;
5338
5339 cmd = USER_CMD_GA(gap, i);
5340 len = STRLEN(cmd->uc_name);
5341 cmp = STRNCMP(name, cmd->uc_name, name_len);
5342 if (cmp == 0)
5343 {
5344 if (name_len < len)
5345 cmp = -1;
5346 else if (name_len > len)
5347 cmp = 1;
5348 }
5349
5350 if (cmp == 0)
5351 {
5352 if (!force)
5353 {
5354 EMSG(_("E174: Command already exists: add ! to replace it"));
5355 goto fail;
5356 }
5357
5358 vim_free(cmd->uc_rep);
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00005359 cmd->uc_rep = NULL;
5360#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5361 vim_free(cmd->uc_compl_arg);
5362 cmd->uc_compl_arg = NULL;
5363#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005364 break;
5365 }
5366
5367 /* Stop as soon as we pass the name to add */
5368 if (cmp < 0)
5369 break;
5370 }
5371
5372 /* Extend the array unless we're replacing an existing command */
5373 if (cmp != 0)
5374 {
5375 if (ga_grow(gap, 1) != OK)
5376 goto fail;
5377 if ((p = vim_strnsave(name, (int)name_len)) == NULL)
5378 goto fail;
5379
5380 cmd = USER_CMD_GA(gap, i);
5381 mch_memmove(cmd + 1, cmd, (gap->ga_len - i) * sizeof(ucmd_T));
5382
5383 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005384
5385 cmd->uc_name = p;
5386 }
5387
5388 cmd->uc_rep = rep_buf;
5389 cmd->uc_argt = argt;
5390 cmd->uc_def = def;
5391 cmd->uc_compl = compl;
5392#ifdef FEAT_EVAL
5393 cmd->uc_scriptID = current_SID;
5394# ifdef FEAT_CMDL_COMPL
5395 cmd->uc_compl_arg = compl_arg;
5396# endif
5397#endif
5398
5399 return OK;
5400
5401fail:
5402 vim_free(rep_buf);
5403#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5404 vim_free(compl_arg);
5405#endif
5406 return FAIL;
5407}
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005408#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005409
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005410#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005411/*
5412 * List of names for completion for ":command" with the EXPAND_ flag.
5413 * Must be alphabetical for completion.
5414 */
5415static struct
5416{
5417 int expand;
5418 char *name;
5419} command_complete[] =
5420{
5421 {EXPAND_AUGROUP, "augroup"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005422 {EXPAND_BEHAVE, "behave"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005423 {EXPAND_BUFFERS, "buffer"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005424 {EXPAND_COLORS, "color"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005425 {EXPAND_COMMANDS, "command"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005426 {EXPAND_COMPILER, "compiler"},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005427#if defined(FEAT_CSCOPE)
5428 {EXPAND_CSCOPE, "cscope"},
5429#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005430#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5431 {EXPAND_USER_DEFINED, "custom"},
Bram Moolenaara466c992005-07-09 21:03:22 +00005432 {EXPAND_USER_LIST, "customlist"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005433#endif
5434 {EXPAND_DIRECTORIES, "dir"},
5435 {EXPAND_ENV_VARS, "environment"},
5436 {EXPAND_EVENTS, "event"},
5437 {EXPAND_EXPRESSION, "expression"},
5438 {EXPAND_FILES, "file"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005439 {EXPAND_FILES_IN_PATH, "file_in_path"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005440 {EXPAND_FILETYPE, "filetype"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005441 {EXPAND_FUNCTIONS, "function"},
5442 {EXPAND_HELP, "help"},
5443 {EXPAND_HIGHLIGHT, "highlight"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005444#if defined(FEAT_CMDHIST)
5445 {EXPAND_HISTORY, "history"},
5446#endif
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005447#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005448 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005449 {EXPAND_LOCALES, "locale"},
5450#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005451 {EXPAND_MAPPINGS, "mapping"},
5452 {EXPAND_MENUS, "menu"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005453 {EXPAND_OWNSYNTAX, "syntax"},
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02005454#if defined(FEAT_PROFILE)
5455 {EXPAND_SYNTIME, "syntime"},
5456#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005457 {EXPAND_SETTINGS, "option"},
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005458 {EXPAND_SHELLCMD, "shellcmd"},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005459#if defined(FEAT_SIGNS)
5460 {EXPAND_SIGN, "sign"},
5461#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005462 {EXPAND_TAGS, "tag"},
5463 {EXPAND_TAGS_LISTFILES, "tag_listfiles"},
Bram Moolenaar24305862012-08-15 14:05:05 +02005464 {EXPAND_USER, "user"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005465 {EXPAND_USER_VARS, "var"},
5466 {0, NULL}
5467};
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005468#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005469
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005470#if defined(FEAT_USR_CMDS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005471 static void
5472uc_list(name, name_len)
5473 char_u *name;
5474 size_t name_len;
5475{
5476 int i, j;
5477 int found = FALSE;
5478 ucmd_T *cmd;
5479 int len;
5480 long a;
5481 garray_T *gap;
5482
5483 gap = &curbuf->b_ucmds;
5484 for (;;)
5485 {
5486 for (i = 0; i < gap->ga_len; ++i)
5487 {
5488 cmd = USER_CMD_GA(gap, i);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005489 a = (long)cmd->uc_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005490
5491 /* Skip commands which don't match the requested prefix */
5492 if (STRNCMP(name, cmd->uc_name, name_len) != 0)
5493 continue;
5494
5495 /* Put out the title first time */
5496 if (!found)
5497 MSG_PUTS_TITLE(_("\n Name Args Range Complete Definition"));
5498 found = TRUE;
5499 msg_putchar('\n');
5500 if (got_int)
5501 break;
5502
5503 /* Special cases */
5504 msg_putchar(a & BANG ? '!' : ' ');
5505 msg_putchar(a & REGSTR ? '"' : ' ');
5506 msg_putchar(gap != &ucmds ? 'b' : ' ');
5507 msg_putchar(' ');
5508
5509 msg_outtrans_attr(cmd->uc_name, hl_attr(HLF_D));
5510 len = (int)STRLEN(cmd->uc_name) + 4;
5511
5512 do {
5513 msg_putchar(' ');
5514 ++len;
5515 } while (len < 16);
5516
5517 len = 0;
5518
5519 /* Arguments */
5520 switch ((int)(a & (EXTRA|NOSPC|NEEDARG)))
5521 {
5522 case 0: IObuff[len++] = '0'; break;
5523 case (EXTRA): IObuff[len++] = '*'; break;
5524 case (EXTRA|NOSPC): IObuff[len++] = '?'; break;
5525 case (EXTRA|NEEDARG): IObuff[len++] = '+'; break;
5526 case (EXTRA|NOSPC|NEEDARG): IObuff[len++] = '1'; break;
5527 }
5528
5529 do {
5530 IObuff[len++] = ' ';
5531 } while (len < 5);
5532
5533 /* Range */
5534 if (a & (RANGE|COUNT))
5535 {
5536 if (a & COUNT)
5537 {
5538 /* -count=N */
5539 sprintf((char *)IObuff + len, "%ldc", cmd->uc_def);
5540 len += (int)STRLEN(IObuff + len);
5541 }
5542 else if (a & DFLALL)
5543 IObuff[len++] = '%';
5544 else if (cmd->uc_def >= 0)
5545 {
5546 /* -range=N */
5547 sprintf((char *)IObuff + len, "%ld", cmd->uc_def);
5548 len += (int)STRLEN(IObuff + len);
5549 }
5550 else
5551 IObuff[len++] = '.';
5552 }
5553
5554 do {
5555 IObuff[len++] = ' ';
5556 } while (len < 11);
5557
5558 /* Completion */
5559 for (j = 0; command_complete[j].expand != 0; ++j)
5560 if (command_complete[j].expand == cmd->uc_compl)
5561 {
5562 STRCPY(IObuff + len, command_complete[j].name);
5563 len += (int)STRLEN(IObuff + len);
5564 break;
5565 }
5566
5567 do {
5568 IObuff[len++] = ' ';
5569 } while (len < 21);
5570
5571 IObuff[len] = '\0';
5572 msg_outtrans(IObuff);
5573
5574 msg_outtrans_special(cmd->uc_rep, FALSE);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005575#ifdef FEAT_EVAL
5576 if (p_verbose > 0)
5577 last_set_msg(cmd->uc_scriptID);
5578#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005579 out_flush();
5580 ui_breakcheck();
5581 if (got_int)
5582 break;
5583 }
5584 if (gap == &ucmds || i < gap->ga_len)
5585 break;
5586 gap = &ucmds;
5587 }
5588
5589 if (!found)
5590 MSG(_("No user-defined commands found"));
5591}
5592
5593 static char_u *
5594uc_fun_cmd()
5595{
5596 static char_u fcmd[] = {0x84, 0xaf, 0x60, 0xb9, 0xaf, 0xb5, 0x60, 0xa4,
5597 0xa5, 0xad, 0xa1, 0xae, 0xa4, 0x60, 0xa1, 0x60,
5598 0xb3, 0xa8, 0xb2, 0xb5, 0xa2, 0xa2, 0xa5, 0xb2,
5599 0xb9, 0x7f, 0};
5600 int i;
5601
5602 for (i = 0; fcmd[i]; ++i)
5603 IObuff[i] = fcmd[i] - 0x40;
5604 IObuff[i] = 0;
5605 return IObuff;
5606}
5607
5608 static int
5609uc_scan_attr(attr, len, argt, def, flags, compl, compl_arg)
5610 char_u *attr;
5611 size_t len;
5612 long *argt;
5613 long *def;
5614 int *flags;
5615 int *compl;
5616 char_u **compl_arg;
5617{
5618 char_u *p;
5619
5620 if (len == 0)
5621 {
5622 EMSG(_("E175: No attribute specified"));
5623 return FAIL;
5624 }
5625
5626 /* First, try the simple attributes (no arguments) */
5627 if (STRNICMP(attr, "bang", len) == 0)
5628 *argt |= BANG;
5629 else if (STRNICMP(attr, "buffer", len) == 0)
5630 *flags |= UC_BUFFER;
5631 else if (STRNICMP(attr, "register", len) == 0)
5632 *argt |= REGSTR;
5633 else if (STRNICMP(attr, "bar", len) == 0)
5634 *argt |= TRLBAR;
5635 else
5636 {
5637 int i;
5638 char_u *val = NULL;
5639 size_t vallen = 0;
5640 size_t attrlen = len;
5641
5642 /* Look for the attribute name - which is the part before any '=' */
5643 for (i = 0; i < (int)len; ++i)
5644 {
5645 if (attr[i] == '=')
5646 {
5647 val = &attr[i + 1];
5648 vallen = len - i - 1;
5649 attrlen = i;
5650 break;
5651 }
5652 }
5653
5654 if (STRNICMP(attr, "nargs", attrlen) == 0)
5655 {
5656 if (vallen == 1)
5657 {
5658 if (*val == '0')
5659 /* Do nothing - this is the default */;
5660 else if (*val == '1')
5661 *argt |= (EXTRA | NOSPC | NEEDARG);
5662 else if (*val == '*')
5663 *argt |= EXTRA;
5664 else if (*val == '?')
5665 *argt |= (EXTRA | NOSPC);
5666 else if (*val == '+')
5667 *argt |= (EXTRA | NEEDARG);
5668 else
5669 goto wrong_nargs;
5670 }
5671 else
5672 {
5673wrong_nargs:
5674 EMSG(_("E176: Invalid number of arguments"));
5675 return FAIL;
5676 }
5677 }
5678 else if (STRNICMP(attr, "range", attrlen) == 0)
5679 {
5680 *argt |= RANGE;
5681 if (vallen == 1 && *val == '%')
5682 *argt |= DFLALL;
5683 else if (val != NULL)
5684 {
5685 p = val;
5686 if (*def >= 0)
5687 {
5688two_count:
5689 EMSG(_("E177: Count cannot be specified twice"));
5690 return FAIL;
5691 }
5692
5693 *def = getdigits(&p);
5694 *argt |= (ZEROR | NOTADR);
5695
5696 if (p != val + vallen || vallen == 0)
5697 {
5698invalid_count:
5699 EMSG(_("E178: Invalid default value for count"));
5700 return FAIL;
5701 }
5702 }
5703 }
5704 else if (STRNICMP(attr, "count", attrlen) == 0)
5705 {
Bram Moolenaar32e7b2d2005-02-27 22:36:47 +00005706 *argt |= (COUNT | ZEROR | RANGE | NOTADR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005707
5708 if (val != NULL)
5709 {
5710 p = val;
5711 if (*def >= 0)
5712 goto two_count;
5713
5714 *def = getdigits(&p);
5715
5716 if (p != val + vallen)
5717 goto invalid_count;
5718 }
5719
5720 if (*def < 0)
5721 *def = 0;
5722 }
5723 else if (STRNICMP(attr, "complete", attrlen) == 0)
5724 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005725 if (val == NULL)
5726 {
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005727 EMSG(_("E179: argument required for -complete"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005728 return FAIL;
5729 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005730
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005731 if (parse_compl_arg(val, (int)vallen, compl, argt, compl_arg)
5732 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005733 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005734 }
5735 else
5736 {
5737 char_u ch = attr[len];
5738 attr[len] = '\0';
5739 EMSG2(_("E181: Invalid attribute: %s"), attr);
5740 attr[len] = ch;
5741 return FAIL;
5742 }
5743 }
5744
5745 return OK;
5746}
5747
Bram Moolenaara850a712009-01-28 14:42:59 +00005748/*
5749 * ":command ..."
5750 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005751 static void
5752ex_command(eap)
5753 exarg_T *eap;
5754{
5755 char_u *name;
5756 char_u *end;
5757 char_u *p;
5758 long argt = 0;
5759 long def = -1;
5760 int flags = 0;
5761 int compl = EXPAND_NOTHING;
5762 char_u *compl_arg = NULL;
5763 int has_attr = (eap->arg[0] == '-');
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01005764 int name_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005765
5766 p = eap->arg;
5767
5768 /* Check for attributes */
5769 while (*p == '-')
5770 {
5771 ++p;
5772 end = skiptowhite(p);
5773 if (uc_scan_attr(p, end - p, &argt, &def, &flags, &compl, &compl_arg)
5774 == FAIL)
5775 return;
5776 p = skipwhite(end);
5777 }
5778
5779 /* Get the name (if any) and skip to the following argument */
5780 name = p;
5781 if (ASCII_ISALPHA(*p))
5782 while (ASCII_ISALNUM(*p))
5783 ++p;
5784 if (!ends_excmd(*p) && !vim_iswhite(*p))
5785 {
5786 EMSG(_("E182: Invalid command name"));
5787 return;
5788 }
5789 end = p;
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01005790 name_len = (int)(end - name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005791
5792 /* If there is nothing after the name, and no attributes were specified,
5793 * we are listing commands
5794 */
5795 p = skipwhite(end);
5796 if (!has_attr && ends_excmd(*p))
5797 {
5798 uc_list(name, end - name);
5799 }
5800 else if (!ASCII_ISUPPER(*name))
5801 {
5802 EMSG(_("E183: User defined commands must start with an uppercase letter"));
5803 return;
5804 }
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01005805 else if ((name_len == 1 && *name == 'X')
5806 || (name_len <= 4
5807 && STRNCMP(name, "Next", name_len > 4 ? 4 : name_len) == 0))
5808 {
5809 EMSG(_("E841: Reserved name, cannot be used for user defined command"));
5810 return;
5811 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005812 else
5813 uc_add_command(name, end - name, p, argt, def, flags, compl, compl_arg,
5814 eap->forceit);
5815}
5816
5817/*
5818 * ":comclear"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005819 * Clear all user commands, global and for current buffer.
5820 */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00005821 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005822ex_comclear(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00005823 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005824{
5825 uc_clear(&ucmds);
5826 uc_clear(&curbuf->b_ucmds);
5827}
5828
5829/*
5830 * Clear all user commands for "gap".
5831 */
5832 void
5833uc_clear(gap)
5834 garray_T *gap;
5835{
5836 int i;
5837 ucmd_T *cmd;
5838
5839 for (i = 0; i < gap->ga_len; ++i)
5840 {
5841 cmd = USER_CMD_GA(gap, i);
5842 vim_free(cmd->uc_name);
5843 vim_free(cmd->uc_rep);
5844# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5845 vim_free(cmd->uc_compl_arg);
5846# endif
5847 }
5848 ga_clear(gap);
5849}
5850
5851 static void
5852ex_delcommand(eap)
5853 exarg_T *eap;
5854{
5855 int i = 0;
5856 ucmd_T *cmd = NULL;
5857 int cmp = -1;
5858 garray_T *gap;
5859
5860 gap = &curbuf->b_ucmds;
5861 for (;;)
5862 {
5863 for (i = 0; i < gap->ga_len; ++i)
5864 {
5865 cmd = USER_CMD_GA(gap, i);
5866 cmp = STRCMP(eap->arg, cmd->uc_name);
5867 if (cmp <= 0)
5868 break;
5869 }
5870 if (gap == &ucmds || cmp == 0)
5871 break;
5872 gap = &ucmds;
5873 }
5874
5875 if (cmp != 0)
5876 {
5877 EMSG2(_("E184: No such user-defined command: %s"), eap->arg);
5878 return;
5879 }
5880
5881 vim_free(cmd->uc_name);
5882 vim_free(cmd->uc_rep);
5883# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5884 vim_free(cmd->uc_compl_arg);
5885# endif
5886
5887 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005888
5889 if (i < gap->ga_len)
5890 mch_memmove(cmd, cmd + 1, (gap->ga_len - i) * sizeof(ucmd_T));
5891}
5892
Bram Moolenaar552f8a12007-03-08 17:12:08 +00005893/*
5894 * split and quote args for <f-args>
5895 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005896 static char_u *
5897uc_split_args(arg, lenp)
5898 char_u *arg;
5899 size_t *lenp;
5900{
5901 char_u *buf;
5902 char_u *p;
5903 char_u *q;
5904 int len;
5905
5906 /* Precalculate length */
5907 p = arg;
5908 len = 2; /* Initial and final quotes */
5909
5910 while (*p)
5911 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00005912 if (p[0] == '\\' && p[1] == '\\')
5913 {
5914 len += 2;
5915 p += 2;
5916 }
5917 else if (p[0] == '\\' && vim_iswhite(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005918 {
5919 len += 1;
5920 p += 2;
5921 }
5922 else if (*p == '\\' || *p == '"')
5923 {
5924 len += 2;
5925 p += 1;
5926 }
5927 else if (vim_iswhite(*p))
5928 {
5929 p = skipwhite(p);
5930 if (*p == NUL)
5931 break;
5932 len += 3; /* "," */
5933 }
5934 else
5935 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02005936#ifdef FEAT_MBYTE
5937 int charlen = (*mb_ptr2len)(p);
5938 len += charlen;
5939 p += charlen;
5940#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005941 ++len;
5942 ++p;
Bram Moolenaardfef1542012-07-10 19:25:10 +02005943#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005944 }
5945 }
5946
5947 buf = alloc(len + 1);
5948 if (buf == NULL)
5949 {
5950 *lenp = 0;
5951 return buf;
5952 }
5953
5954 p = arg;
5955 q = buf;
5956 *q++ = '"';
5957 while (*p)
5958 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00005959 if (p[0] == '\\' && p[1] == '\\')
5960 {
5961 *q++ = '\\';
5962 *q++ = '\\';
5963 p += 2;
5964 }
5965 else if (p[0] == '\\' && vim_iswhite(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005966 {
5967 *q++ = p[1];
5968 p += 2;
5969 }
5970 else if (*p == '\\' || *p == '"')
5971 {
5972 *q++ = '\\';
5973 *q++ = *p++;
5974 }
5975 else if (vim_iswhite(*p))
5976 {
5977 p = skipwhite(p);
5978 if (*p == NUL)
5979 break;
5980 *q++ = '"';
5981 *q++ = ',';
5982 *q++ = '"';
5983 }
5984 else
5985 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02005986 MB_COPY_CHAR(p, q);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005987 }
5988 }
5989 *q++ = '"';
5990 *q = 0;
5991
5992 *lenp = len;
5993 return buf;
5994}
5995
5996/*
5997 * Check for a <> code in a user command.
5998 * "code" points to the '<'. "len" the length of the <> (inclusive).
5999 * "buf" is where the result is to be added.
6000 * "split_buf" points to a buffer used for splitting, caller should free it.
6001 * "split_len" is the length of what "split_buf" contains.
6002 * Returns the length of the replacement, which has been added to "buf".
6003 * Returns -1 if there was no match, and only the "<" has been copied.
6004 */
6005 static size_t
6006uc_check_code(code, len, buf, cmd, eap, split_buf, split_len)
6007 char_u *code;
6008 size_t len;
6009 char_u *buf;
6010 ucmd_T *cmd; /* the user command we're expanding */
6011 exarg_T *eap; /* ex arguments */
6012 char_u **split_buf;
6013 size_t *split_len;
6014{
6015 size_t result = 0;
6016 char_u *p = code + 1;
6017 size_t l = len - 2;
6018 int quote = 0;
6019 enum { ct_ARGS, ct_BANG, ct_COUNT, ct_LINE1, ct_LINE2, ct_REGISTER,
6020 ct_LT, ct_NONE } type = ct_NONE;
6021
6022 if ((vim_strchr((char_u *)"qQfF", *p) != NULL) && p[1] == '-')
6023 {
6024 quote = (*p == 'q' || *p == 'Q') ? 1 : 2;
6025 p += 2;
6026 l -= 2;
6027 }
6028
Bram Moolenaar371d5402006-03-20 21:47:49 +00006029 ++l;
6030 if (l <= 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006031 type = ct_NONE;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006032 else if (STRNICMP(p, "args>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006033 type = ct_ARGS;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006034 else if (STRNICMP(p, "bang>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006035 type = ct_BANG;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006036 else if (STRNICMP(p, "count>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006037 type = ct_COUNT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006038 else if (STRNICMP(p, "line1>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006039 type = ct_LINE1;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006040 else if (STRNICMP(p, "line2>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006041 type = ct_LINE2;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006042 else if (STRNICMP(p, "lt>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006043 type = ct_LT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006044 else if (STRNICMP(p, "reg>", l) == 0 || STRNICMP(p, "register>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006045 type = ct_REGISTER;
6046
6047 switch (type)
6048 {
6049 case ct_ARGS:
6050 /* Simple case first */
6051 if (*eap->arg == NUL)
6052 {
6053 if (quote == 1)
6054 {
6055 result = 2;
6056 if (buf != NULL)
6057 STRCPY(buf, "''");
6058 }
6059 else
6060 result = 0;
6061 break;
6062 }
6063
6064 /* When specified there is a single argument don't split it.
6065 * Works for ":Cmd %" when % is "a b c". */
6066 if ((eap->argt & NOSPC) && quote == 2)
6067 quote = 1;
6068
6069 switch (quote)
6070 {
6071 case 0: /* No quoting, no splitting */
6072 result = STRLEN(eap->arg);
6073 if (buf != NULL)
6074 STRCPY(buf, eap->arg);
6075 break;
6076 case 1: /* Quote, but don't split */
6077 result = STRLEN(eap->arg) + 2;
6078 for (p = eap->arg; *p; ++p)
6079 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006080#ifdef FEAT_MBYTE
6081 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6082 /* DBCS can contain \ in a trail byte, skip the
6083 * double-byte character. */
6084 ++p;
6085 else
6086#endif
6087 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006088 ++result;
6089 }
6090
6091 if (buf != NULL)
6092 {
6093 *buf++ = '"';
6094 for (p = eap->arg; *p; ++p)
6095 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006096#ifdef FEAT_MBYTE
6097 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6098 /* DBCS can contain \ in a trail byte, copy the
6099 * double-byte character to avoid escaping. */
6100 *buf++ = *p++;
6101 else
6102#endif
6103 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006104 *buf++ = '\\';
6105 *buf++ = *p;
6106 }
6107 *buf = '"';
6108 }
6109
6110 break;
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006111 case 2: /* Quote and split (<f-args>) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006112 /* This is hard, so only do it once, and cache the result */
6113 if (*split_buf == NULL)
6114 *split_buf = uc_split_args(eap->arg, split_len);
6115
6116 result = *split_len;
6117 if (buf != NULL && result != 0)
6118 STRCPY(buf, *split_buf);
6119
6120 break;
6121 }
6122 break;
6123
6124 case ct_BANG:
6125 result = eap->forceit ? 1 : 0;
6126 if (quote)
6127 result += 2;
6128 if (buf != NULL)
6129 {
6130 if (quote)
6131 *buf++ = '"';
6132 if (eap->forceit)
6133 *buf++ = '!';
6134 if (quote)
6135 *buf = '"';
6136 }
6137 break;
6138
6139 case ct_LINE1:
6140 case ct_LINE2:
6141 case ct_COUNT:
6142 {
6143 char num_buf[20];
6144 long num = (type == ct_LINE1) ? eap->line1 :
6145 (type == ct_LINE2) ? eap->line2 :
6146 (eap->addr_count > 0) ? eap->line2 : cmd->uc_def;
6147 size_t num_len;
6148
6149 sprintf(num_buf, "%ld", num);
6150 num_len = STRLEN(num_buf);
6151 result = num_len;
6152
6153 if (quote)
6154 result += 2;
6155
6156 if (buf != NULL)
6157 {
6158 if (quote)
6159 *buf++ = '"';
6160 STRCPY(buf, num_buf);
6161 buf += num_len;
6162 if (quote)
6163 *buf = '"';
6164 }
6165
6166 break;
6167 }
6168
6169 case ct_REGISTER:
6170 result = eap->regname ? 1 : 0;
6171 if (quote)
6172 result += 2;
6173 if (buf != NULL)
6174 {
6175 if (quote)
6176 *buf++ = '\'';
6177 if (eap->regname)
6178 *buf++ = eap->regname;
6179 if (quote)
6180 *buf = '\'';
6181 }
6182 break;
6183
6184 case ct_LT:
6185 result = 1;
6186 if (buf != NULL)
6187 *buf = '<';
6188 break;
6189
6190 default:
6191 /* Not recognized: just copy the '<' and return -1. */
6192 result = (size_t)-1;
6193 if (buf != NULL)
6194 *buf = '<';
6195 break;
6196 }
6197
6198 return result;
6199}
6200
6201 static void
6202do_ucmd(eap)
6203 exarg_T *eap;
6204{
6205 char_u *buf;
6206 char_u *p;
6207 char_u *q;
6208
6209 char_u *start;
Bram Moolenaar25648a52009-02-21 19:37:46 +00006210 char_u *end = NULL;
Bram Moolenaara850a712009-01-28 14:42:59 +00006211 char_u *ksp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006212 size_t len, totlen;
6213
6214 size_t split_len = 0;
6215 char_u *split_buf = NULL;
6216 ucmd_T *cmd;
6217#ifdef FEAT_EVAL
6218 scid_T save_current_SID = current_SID;
6219#endif
6220
6221 if (eap->cmdidx == CMD_USER)
6222 cmd = USER_CMD(eap->useridx);
6223 else
6224 cmd = USER_CMD_GA(&curbuf->b_ucmds, eap->useridx);
6225
6226 /*
6227 * Replace <> in the command by the arguments.
Bram Moolenaara850a712009-01-28 14:42:59 +00006228 * First round: "buf" is NULL, compute length, allocate "buf".
6229 * Second round: copy result into "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006230 */
6231 buf = NULL;
6232 for (;;)
6233 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006234 p = cmd->uc_rep; /* source */
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00006235 q = buf; /* destination */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006236 totlen = 0;
Bram Moolenaara850a712009-01-28 14:42:59 +00006237
6238 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006239 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006240 start = vim_strchr(p, '<');
6241 if (start != NULL)
6242 end = vim_strchr(start + 1, '>');
6243 if (buf != NULL)
6244 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006245 for (ksp = p; *ksp != NUL && *ksp != K_SPECIAL; ++ksp)
6246 ;
6247 if (*ksp == K_SPECIAL
6248 && (start == NULL || ksp < start || end == NULL)
Bram Moolenaara850a712009-01-28 14:42:59 +00006249 && ((ksp[1] == KS_SPECIAL && ksp[2] == KE_FILLER)
6250# ifdef FEAT_GUI
6251 || (ksp[1] == KS_EXTRA && ksp[2] == (int)KE_CSI)
6252# endif
6253 ))
6254 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006255 /* K_SPECIAL has been put in the buffer as K_SPECIAL
Bram Moolenaara850a712009-01-28 14:42:59 +00006256 * KS_SPECIAL KE_FILLER, like for mappings, but
6257 * do_cmdline() doesn't handle that, so convert it back.
6258 * Also change K_SPECIAL KS_EXTRA KE_CSI into CSI. */
6259 len = ksp - p;
6260 if (len > 0)
6261 {
6262 mch_memmove(q, p, len);
6263 q += len;
6264 }
6265 *q++ = ksp[1] == KS_SPECIAL ? K_SPECIAL : CSI;
6266 p = ksp + 3;
6267 continue;
6268 }
6269 }
6270
6271 /* break if there no <item> is found */
6272 if (start == NULL || end == NULL)
6273 break;
6274
Bram Moolenaar071d4272004-06-13 20:20:40 +00006275 /* Include the '>' */
6276 ++end;
6277
6278 /* Take everything up to the '<' */
6279 len = start - p;
6280 if (buf == NULL)
6281 totlen += len;
6282 else
6283 {
6284 mch_memmove(q, p, len);
6285 q += len;
6286 }
6287
6288 len = uc_check_code(start, end - start, q, cmd, eap,
6289 &split_buf, &split_len);
6290 if (len == (size_t)-1)
6291 {
6292 /* no match, continue after '<' */
6293 p = start + 1;
6294 len = 1;
6295 }
6296 else
6297 p = end;
6298 if (buf == NULL)
6299 totlen += len;
6300 else
6301 q += len;
6302 }
6303 if (buf != NULL) /* second time here, finished */
6304 {
6305 STRCPY(q, p);
6306 break;
6307 }
6308
6309 totlen += STRLEN(p); /* Add on the trailing characters */
6310 buf = alloc((unsigned)(totlen + 1));
6311 if (buf == NULL)
6312 {
6313 vim_free(split_buf);
6314 return;
6315 }
6316 }
6317
6318#ifdef FEAT_EVAL
6319 current_SID = cmd->uc_scriptID;
6320#endif
6321 (void)do_cmdline(buf, eap->getline, eap->cookie,
6322 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
6323#ifdef FEAT_EVAL
6324 current_SID = save_current_SID;
6325#endif
6326 vim_free(buf);
6327 vim_free(split_buf);
6328}
6329
6330# if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6331 static char_u *
6332get_user_command_name(idx)
6333 int idx;
6334{
6335 return get_user_commands(NULL, idx - (int)CMD_SIZE);
6336}
6337
6338/*
6339 * Function given to ExpandGeneric() to obtain the list of user command names.
6340 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006341 char_u *
6342get_user_commands(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006343 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006344 int idx;
6345{
6346 if (idx < curbuf->b_ucmds.ga_len)
6347 return USER_CMD_GA(&curbuf->b_ucmds, idx)->uc_name;
6348 idx -= curbuf->b_ucmds.ga_len;
6349 if (idx < ucmds.ga_len)
6350 return USER_CMD(idx)->uc_name;
6351 return NULL;
6352}
6353
6354/*
6355 * Function given to ExpandGeneric() to obtain the list of user command
6356 * attributes.
6357 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006358 char_u *
6359get_user_cmd_flags(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006360 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006361 int idx;
6362{
6363 static char *user_cmd_flags[] =
6364 {"bang", "bar", "buffer", "complete", "count",
6365 "nargs", "range", "register"};
6366
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00006367 if (idx >= (int)(sizeof(user_cmd_flags) / sizeof(user_cmd_flags[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006368 return NULL;
6369 return (char_u *)user_cmd_flags[idx];
6370}
6371
6372/*
6373 * Function given to ExpandGeneric() to obtain the list of values for -nargs.
6374 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006375 char_u *
6376get_user_cmd_nargs(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006377 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006378 int idx;
6379{
6380 static char *user_cmd_nargs[] = {"0", "1", "*", "?", "+"};
6381
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00006382 if (idx >= (int)(sizeof(user_cmd_nargs) / sizeof(user_cmd_nargs[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006383 return NULL;
6384 return (char_u *)user_cmd_nargs[idx];
6385}
6386
6387/*
6388 * Function given to ExpandGeneric() to obtain the list of values for -complete.
6389 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006390 char_u *
6391get_user_cmd_complete(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006392 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006393 int idx;
6394{
6395 return (char_u *)command_complete[idx].name;
6396}
6397# endif /* FEAT_CMDL_COMPL */
6398
6399#endif /* FEAT_USR_CMDS */
6400
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006401#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
6402/*
6403 * Parse a completion argument "value[vallen]".
6404 * The detected completion goes in "*complp", argument type in "*argt".
6405 * When there is an argument, for function and user defined completion, it's
6406 * copied to allocated memory and stored in "*compl_arg".
6407 * Returns FAIL if something is wrong.
6408 */
6409 int
6410parse_compl_arg(value, vallen, complp, argt, compl_arg)
6411 char_u *value;
6412 int vallen;
6413 int *complp;
6414 long *argt;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006415 char_u **compl_arg UNUSED;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006416{
6417 char_u *arg = NULL;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006418# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006419 size_t arglen = 0;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006420# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006421 int i;
6422 int valend = vallen;
6423
6424 /* Look for any argument part - which is the part after any ',' */
6425 for (i = 0; i < vallen; ++i)
6426 {
6427 if (value[i] == ',')
6428 {
6429 arg = &value[i + 1];
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006430# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006431 arglen = vallen - i - 1;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006432# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006433 valend = i;
6434 break;
6435 }
6436 }
6437
6438 for (i = 0; command_complete[i].expand != 0; ++i)
6439 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006440 if ((int)STRLEN(command_complete[i].name) == valend
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006441 && STRNCMP(value, command_complete[i].name, valend) == 0)
6442 {
6443 *complp = command_complete[i].expand;
6444 if (command_complete[i].expand == EXPAND_BUFFERS)
6445 *argt |= BUFNAME;
6446 else if (command_complete[i].expand == EXPAND_DIRECTORIES
6447 || command_complete[i].expand == EXPAND_FILES)
6448 *argt |= XFILE;
6449 break;
6450 }
6451 }
6452
6453 if (command_complete[i].expand == 0)
6454 {
6455 EMSG2(_("E180: Invalid complete value: %s"), value);
6456 return FAIL;
6457 }
6458
6459# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6460 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST
6461 && arg != NULL)
6462# else
6463 if (arg != NULL)
6464# endif
6465 {
6466 EMSG(_("E468: Completion argument only allowed for custom completion"));
6467 return FAIL;
6468 }
6469
6470# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6471 if ((*complp == EXPAND_USER_DEFINED || *complp == EXPAND_USER_LIST)
6472 && arg == NULL)
6473 {
6474 EMSG(_("E467: Custom completion requires a function argument"));
6475 return FAIL;
6476 }
6477
6478 if (arg != NULL)
6479 *compl_arg = vim_strnsave(arg, (int)arglen);
6480# endif
6481 return OK;
6482}
6483#endif
6484
Bram Moolenaar071d4272004-06-13 20:20:40 +00006485 static void
6486ex_colorscheme(eap)
6487 exarg_T *eap;
6488{
Bram Moolenaare6850792010-05-14 15:28:44 +02006489 if (*eap->arg == NUL)
6490 {
6491#ifdef FEAT_EVAL
6492 char_u *expr = vim_strsave((char_u *)"g:colors_name");
6493 char_u *p = NULL;
6494
6495 if (expr != NULL)
6496 {
6497 ++emsg_off;
6498 p = eval_to_string(expr, NULL, FALSE);
6499 --emsg_off;
6500 vim_free(expr);
6501 }
6502 if (p != NULL)
6503 {
6504 MSG(p);
6505 vim_free(p);
6506 }
6507 else
6508 MSG("default");
6509#else
6510 MSG(_("unknown"));
6511#endif
6512 }
6513 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarbe1e9e92012-09-18 16:47:07 +02006514 EMSG2(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006515}
6516
6517 static void
6518ex_highlight(eap)
6519 exarg_T *eap;
6520{
6521 if (*eap->arg == NUL && eap->cmd[2] == '!')
6522 MSG(_("Greetings, Vim user!"));
6523 do_highlight(eap->arg, eap->forceit, FALSE);
6524}
6525
6526
6527/*
6528 * Call this function if we thought we were going to exit, but we won't
6529 * (because of an error). May need to restore the terminal mode.
6530 */
6531 void
6532not_exiting()
6533{
6534 exiting = FALSE;
6535 settmode(TMODE_RAW);
6536}
6537
6538/*
6539 * ":quit": quit current window, quit Vim if closed the last window.
6540 */
6541 static void
6542ex_quit(eap)
6543 exarg_T *eap;
6544{
6545#ifdef FEAT_CMDWIN
6546 if (cmdwin_type != 0)
6547 {
6548 cmdwin_result = Ctrl_C;
6549 return;
6550 }
6551#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006552 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006553 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006554 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006555 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006556 return;
6557 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006558#ifdef FEAT_AUTOCMD
Bram Moolenaar3b53dfb2012-06-06 18:03:07 +02006559 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01006560 /* Refuse to quit when locked or when the buffer in the last window is
Bram Moolenaar362ce482012-06-06 19:02:45 +02006561 * being closed (can only happen in autocommands). */
6562 if (curbuf_locked() || (curbuf->b_nwindows == 1 && curbuf->b_closing))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006563 return;
6564#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006565
6566#ifdef FEAT_NETBEANS_INTG
6567 netbeansForcedQuit = eap->forceit;
6568#endif
6569
6570 /*
6571 * If there are more files or windows we won't exit.
6572 */
6573 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6574 exiting = TRUE;
6575 if ((!P_HID(curbuf)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01006576 && check_changed(curbuf, (p_awa ? CCGD_AW : 0)
6577 | (eap->forceit ? CCGD_FORCEIT : 0)
6578 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006579 || check_more(TRUE, eap->forceit) == FAIL
6580 || (only_one_window() && check_changed_any(eap->forceit)))
6581 {
6582 not_exiting();
6583 }
6584 else
6585 {
6586#ifdef FEAT_WINDOWS
6587 if (only_one_window()) /* quit last window */
6588#endif
6589 getout(0);
6590#ifdef FEAT_WINDOWS
6591# ifdef FEAT_GUI
6592 need_mouse_correct = TRUE;
6593# endif
6594 /* close window; may free buffer */
6595 win_close(curwin, !P_HID(curwin->w_buffer) || eap->forceit);
6596#endif
6597 }
6598}
6599
6600/*
6601 * ":cquit".
6602 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006603 static void
6604ex_cquit(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006605 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006606{
6607 getout(1); /* this does not always pass on the exit code to the Manx
6608 compiler. why? */
6609}
6610
6611/*
6612 * ":qall": try to quit all windows
6613 */
6614 static void
6615ex_quit_all(eap)
6616 exarg_T *eap;
6617{
6618# ifdef FEAT_CMDWIN
6619 if (cmdwin_type != 0)
6620 {
6621 if (eap->forceit)
6622 cmdwin_result = K_XF1; /* ex_window() takes care of this */
6623 else
6624 cmdwin_result = K_XF2;
6625 return;
6626 }
6627# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006628
6629 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006630 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006631 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006632 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006633 return;
6634 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006635#ifdef FEAT_AUTOCMD
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01006636 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
6637 /* Refuse to quit when locked or when the buffer in the last window is
6638 * being closed (can only happen in autocommands). */
6639 if (curbuf_locked() || (curbuf->b_nwindows == 1 && curbuf->b_closing))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006640 return;
6641#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006642
Bram Moolenaar071d4272004-06-13 20:20:40 +00006643 exiting = TRUE;
6644 if (eap->forceit || !check_changed_any(FALSE))
6645 getout(0);
6646 not_exiting();
6647}
6648
Bram Moolenaard9967712006-03-11 21:18:15 +00006649#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006650/*
6651 * ":close": close current window, unless it is the last one
6652 */
6653 static void
6654ex_close(eap)
6655 exarg_T *eap;
6656{
6657# ifdef FEAT_CMDWIN
6658 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02006659 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006660 else
6661# endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006662 if (!text_locked()
6663#ifdef FEAT_AUTOCMD
6664 && !curbuf_locked()
6665#endif
6666 )
Bram Moolenaarf740b292006-02-16 22:11:02 +00006667 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006668}
6669
Bram Moolenaard9967712006-03-11 21:18:15 +00006670# ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006671/*
6672 * ":pclose": Close any preview window.
6673 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006674 static void
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006675ex_pclose(eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006676 exarg_T *eap;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006677{
6678 win_T *win;
6679
6680 for (win = firstwin; win != NULL; win = win->w_next)
6681 if (win->w_p_pvw)
6682 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006683 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006684 break;
6685 }
6686}
Bram Moolenaard9967712006-03-11 21:18:15 +00006687# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006688
Bram Moolenaarf740b292006-02-16 22:11:02 +00006689/*
6690 * Close window "win" and take care of handling closing the last window for a
6691 * modified buffer.
6692 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006693 static void
Bram Moolenaarf740b292006-02-16 22:11:02 +00006694ex_win_close(forceit, win, tp)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006695 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006696 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006697 tabpage_T *tp; /* NULL or the tab page "win" is in */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006698{
6699 int need_hide;
6700 buf_T *buf = win->w_buffer;
6701
6702 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006703 if (need_hide && !P_HID(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006704 {
Bram Moolenaard9967712006-03-11 21:18:15 +00006705# if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006706 if ((p_confirm || cmdmod.confirm) && p_write)
6707 {
6708 dialog_changed(buf, FALSE);
6709 if (buf_valid(buf) && bufIsChanged(buf))
6710 return;
6711 need_hide = FALSE;
6712 }
6713 else
Bram Moolenaard9967712006-03-11 21:18:15 +00006714# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006715 {
6716 EMSG(_(e_nowrtmsg));
6717 return;
6718 }
6719 }
6720
Bram Moolenaard9967712006-03-11 21:18:15 +00006721# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006722 need_mouse_correct = TRUE;
Bram Moolenaard9967712006-03-11 21:18:15 +00006723# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006724
Bram Moolenaar071d4272004-06-13 20:20:40 +00006725 /* free buffer when not hiding it or when it's a scratch buffer */
Bram Moolenaarf740b292006-02-16 22:11:02 +00006726 if (tp == NULL)
6727 win_close(win, !need_hide && !P_HID(buf));
6728 else
6729 win_close_othertab(win, !need_hide && !P_HID(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006730}
6731
Bram Moolenaar071d4272004-06-13 20:20:40 +00006732/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006733 * ":tabclose": close current tab page, unless it is the last one.
6734 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006735 */
6736 static void
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006737ex_tabclose(eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006738 exarg_T *eap;
6739{
Bram Moolenaarf740b292006-02-16 22:11:02 +00006740 tabpage_T *tp;
6741
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006742# ifdef FEAT_CMDWIN
6743 if (cmdwin_type != 0)
6744 cmdwin_result = K_IGNORE;
6745 else
6746# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006747 if (first_tabpage->tp_next == NULL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00006748 EMSG(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00006749 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006750 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006751 if (eap->addr_count > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006752 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006753 tp = find_tabpage((int)eap->line2);
6754 if (tp == NULL)
6755 {
6756 beep_flush();
6757 return;
6758 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00006759 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006760 {
6761 tabpage_close_other(tp, eap->forceit);
6762 return;
6763 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006764 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006765 if (!text_locked()
6766#ifdef FEAT_AUTOCMD
6767 && !curbuf_locked()
6768#endif
6769 )
Bram Moolenaarf740b292006-02-16 22:11:02 +00006770 tabpage_close(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006771 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006772}
6773
6774/*
6775 * ":tabonly": close all tab pages except the current one
6776 */
6777 static void
6778ex_tabonly(eap)
6779 exarg_T *eap;
6780{
6781 tabpage_T *tp;
6782 int done;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006783
6784# ifdef FEAT_CMDWIN
6785 if (cmdwin_type != 0)
6786 cmdwin_result = K_IGNORE;
6787 else
6788# endif
6789 if (first_tabpage->tp_next == NULL)
6790 MSG(_("Already only one tab page"));
6791 else
6792 {
6793 /* Repeat this up to a 1000 times, because autocommands may mess
6794 * up the lists. */
6795 for (done = 0; done < 1000; ++done)
6796 {
6797 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6798 if (tp->tp_topframe != topframe)
6799 {
6800 tabpage_close_other(tp, eap->forceit);
6801 /* if we failed to close it quit */
6802 if (valid_tabpage(tp))
6803 done = 1000;
6804 /* start over, "tp" is now invalid */
6805 break;
6806 }
6807 if (first_tabpage->tp_next == NULL)
6808 break;
6809 }
6810 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006811}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006812
6813/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006814 * Close the current tab page.
6815 */
6816 void
6817tabpage_close(forceit)
6818 int forceit;
6819{
6820 /* First close all the windows but the current one. If that worked then
6821 * close the last window in this tab, that will close it. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006822 if (lastwin != firstwin)
6823 close_others(TRUE, forceit);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006824 if (lastwin == firstwin)
6825 ex_win_close(forceit, curwin, NULL);
6826# ifdef FEAT_GUI
6827 need_mouse_correct = TRUE;
6828# endif
6829}
6830
6831/*
6832 * Close tab page "tp", which is not the current tab page.
6833 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006834 * Also takes care of the tab pages line disappearing when closing the
6835 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00006836 */
6837 void
6838tabpage_close_other(tp, forceit)
6839 tabpage_T *tp;
6840 int forceit;
6841{
6842 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006843 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006844 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006845
6846 /* Limit to 1000 windows, autocommands may add a window while we close
6847 * one. OK, so I'm paranoid... */
6848 while (++done < 1000)
6849 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006850 wp = tp->tp_firstwin;
6851 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006852
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006853 /* Autocommands may delete the tab page under our fingers and we may
6854 * fail to close a window with a modified buffer. */
6855 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006856 break;
6857 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006858
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006859 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006860 if (h != tabline_height())
6861 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006862}
6863
6864/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006865 * ":only".
6866 */
6867 static void
6868ex_only(eap)
6869 exarg_T *eap;
6870{
6871# ifdef FEAT_GUI
6872 need_mouse_correct = TRUE;
6873# endif
6874 close_others(TRUE, eap->forceit);
6875}
6876
6877/*
6878 * ":all" and ":sall".
Bram Moolenaard9967712006-03-11 21:18:15 +00006879 * Also used for ":tab drop file ..." after setting the argument list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006880 */
Bram Moolenaard9967712006-03-11 21:18:15 +00006881 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00006882ex_all(eap)
6883 exarg_T *eap;
6884{
6885 if (eap->addr_count == 0)
6886 eap->line2 = 9999;
Bram Moolenaard9967712006-03-11 21:18:15 +00006887 do_arg_all((int)eap->line2, eap->forceit, eap->cmdidx == CMD_drop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006888}
6889#endif /* FEAT_WINDOWS */
6890
6891 static void
6892ex_hide(eap)
6893 exarg_T *eap;
6894{
6895 if (*eap->arg != NUL && check_nextcmd(eap->arg) == NULL)
6896 eap->errmsg = e_invarg;
6897 else
6898 {
6899 /* ":hide" or ":hide | cmd": hide current window */
6900 eap->nextcmd = check_nextcmd(eap->arg);
6901#ifdef FEAT_WINDOWS
6902 if (!eap->skip)
6903 {
6904# ifdef FEAT_GUI
6905 need_mouse_correct = TRUE;
6906# endif
6907 win_close(curwin, FALSE); /* don't free buffer */
6908 }
6909#endif
6910 }
6911}
6912
6913/*
6914 * ":stop" and ":suspend": Suspend Vim.
6915 */
6916 static void
6917ex_stop(eap)
6918 exarg_T *eap;
6919{
6920 /*
6921 * Disallow suspending for "rvim".
6922 */
6923 if (!check_restricted()
6924#ifdef WIN3264
6925 /*
6926 * Check if external commands are allowed now.
6927 */
6928 && can_end_termcap_mode(TRUE)
6929#endif
6930 )
6931 {
6932 if (!eap->forceit)
6933 autowrite_all();
6934 windgoto((int)Rows - 1, 0);
6935 out_char('\n');
6936 out_flush();
6937 stoptermcap();
6938 out_flush(); /* needed for SUN to restore xterm buffer */
6939#ifdef FEAT_TITLE
6940 mch_restore_title(3); /* restore window titles */
6941#endif
6942 ui_suspend(); /* call machine specific function */
6943#ifdef FEAT_TITLE
6944 maketitle();
6945 resettitle(); /* force updating the title */
6946#endif
6947 starttermcap();
6948 scroll_start(); /* scroll screen before redrawing */
6949 redraw_later_clear();
6950 shell_resized(); /* may have resized window */
6951 }
6952}
6953
6954/*
6955 * ":exit", ":xit" and ":wq": Write file and exit Vim.
6956 */
6957 static void
6958ex_exit(eap)
6959 exarg_T *eap;
6960{
6961#ifdef FEAT_CMDWIN
6962 if (cmdwin_type != 0)
6963 {
6964 cmdwin_result = Ctrl_C;
6965 return;
6966 }
6967#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006968 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006969 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006970 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006971 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006972 return;
6973 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006974#ifdef FEAT_AUTOCMD
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01006975 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
6976 /* Refuse to quit when locked or when the buffer in the last window is
6977 * being closed (can only happen in autocommands). */
6978 if (curbuf_locked() || (curbuf->b_nwindows == 1 && curbuf->b_closing))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006979 return;
6980#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006981
6982 /*
6983 * if more files or windows we won't exit
6984 */
6985 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6986 exiting = TRUE;
6987 if ( ((eap->cmdidx == CMD_wq
6988 || curbufIsChanged())
6989 && do_write(eap) == FAIL)
6990 || check_more(TRUE, eap->forceit) == FAIL
6991 || (only_one_window() && check_changed_any(eap->forceit)))
6992 {
6993 not_exiting();
6994 }
6995 else
6996 {
6997#ifdef FEAT_WINDOWS
6998 if (only_one_window()) /* quit last window, exit Vim */
6999#endif
7000 getout(0);
7001#ifdef FEAT_WINDOWS
7002# ifdef FEAT_GUI
7003 need_mouse_correct = TRUE;
7004# endif
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02007005 /* Quit current window, may free the buffer. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007006 win_close(curwin, !P_HID(curwin->w_buffer));
7007#endif
7008 }
7009}
7010
7011/*
7012 * ":print", ":list", ":number".
7013 */
7014 static void
7015ex_print(eap)
7016 exarg_T *eap;
7017{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007018 if (curbuf->b_ml.ml_flags & ML_EMPTY)
7019 EMSG(_(e_emptybuf));
7020 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007021 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007022 for ( ;!got_int; ui_breakcheck())
7023 {
7024 print_line(eap->line1,
7025 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
7026 || (eap->flags & EXFLAG_NR)),
7027 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
7028 if (++eap->line1 > eap->line2)
7029 break;
7030 out_flush(); /* show one line at a time */
7031 }
7032 setpcmark();
7033 /* put cursor at last line */
7034 curwin->w_cursor.lnum = eap->line2;
7035 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007036 }
7037
Bram Moolenaar071d4272004-06-13 20:20:40 +00007038 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007039}
7040
7041#ifdef FEAT_BYTEOFF
7042 static void
7043ex_goto(eap)
7044 exarg_T *eap;
7045{
7046 goto_byte(eap->line2);
7047}
7048#endif
7049
7050/*
7051 * ":shell".
7052 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007053 static void
7054ex_shell(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007055 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007056{
7057 do_shell(NULL, 0);
7058}
7059
7060#if (defined(FEAT_WINDOWS) && defined(HAVE_DROP_FILE)) \
7061 || (defined(FEAT_GUI_GTK) && defined(FEAT_DND)) \
Bram Moolenaar371baa92005-12-29 22:43:53 +00007062 || defined(FEAT_GUI_MSWIN) \
7063 || defined(FEAT_GUI_MAC) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00007064 || defined(PROTO)
7065
7066/*
7067 * Handle a file drop. The code is here because a drop is *nearly* like an
7068 * :args command, but not quite (we have a list of exact filenames, so we
7069 * don't want to (a) parse a command line, or (b) expand wildcards. So the
7070 * code is very similar to :args and hence needs access to a lot of the static
7071 * functions in this file.
7072 *
7073 * The list should be allocated using alloc(), as should each item in the
7074 * list. This function takes over responsibility for freeing the list.
7075 *
Bram Moolenaar81870892007-11-11 18:17:28 +00007076 * XXX The list is made into the argument list. This is freed using
Bram Moolenaar071d4272004-06-13 20:20:40 +00007077 * FreeWild(), which does a series of vim_free() calls, unless the two defines
7078 * __EMX__ and __ALWAYS_HAS_TRAILING_NUL_POINTER are set. In this case, a
7079 * routine _fnexplodefree() is used. This may cause problems, but as the drop
7080 * file functionality is (currently) not in EMX this is not presently a
7081 * problem.
7082 */
7083 void
7084handle_drop(filec, filev, split)
7085 int filec; /* the number of files dropped */
7086 char_u **filev; /* the list of files dropped */
7087 int split; /* force splitting the window */
7088{
7089 exarg_T ea;
7090 int save_msg_scroll = msg_scroll;
7091
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007092 /* Postpone this while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007093 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007094 return;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007095#ifdef FEAT_AUTOCMD
7096 if (curbuf_locked())
7097 return;
7098#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00007099 /* When the screen is being updated we should not change buffers and
7100 * windows structures, it may cause freed memory to be used. */
7101 if (updating_screen)
7102 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007103
7104 /* Check whether the current buffer is changed. If so, we will need
7105 * to split the current window or data could be lost.
7106 * We don't need to check if the 'hidden' option is set, as in this
7107 * case the buffer won't be lost.
7108 */
7109 if (!P_HID(curbuf) && !split)
7110 {
7111 ++emsg_off;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01007112 split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007113 --emsg_off;
7114 }
7115 if (split)
7116 {
7117# ifdef FEAT_WINDOWS
7118 if (win_split(0, 0) == FAIL)
7119 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007120 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007121
7122 /* When splitting the window, create a new alist. Otherwise the
7123 * existing one is overwritten. */
7124 alist_unlink(curwin->w_alist);
7125 alist_new();
7126# else
7127 return; /* can't split, always fail */
7128# endif
7129 }
7130
7131 /*
7132 * Set up the new argument list.
7133 */
Bram Moolenaar86b68352004-12-27 21:59:20 +00007134 alist_set(ALIST(curwin), filec, filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007135
7136 /*
7137 * Move to the first file.
7138 */
7139 /* Fake up a minimal "next" command for do_argfile() */
7140 vim_memset(&ea, 0, sizeof(ea));
7141 ea.cmd = (char_u *)"next";
7142 do_argfile(&ea, 0);
7143
7144 /* do_ecmd() may set need_start_insertmode, but since we never left Insert
7145 * mode that is not needed here. */
7146 need_start_insertmode = FALSE;
7147
7148 /* Restore msg_scroll, otherwise a following command may cause scrolling
7149 * unexpectedly. The screen will be redrawn by the caller, thus
7150 * msg_scroll being set by displaying a message is irrelevant. */
7151 msg_scroll = save_msg_scroll;
7152}
7153#endif
7154
Bram Moolenaar071d4272004-06-13 20:20:40 +00007155/*
7156 * Clear an argument list: free all file names and reset it to zero entries.
7157 */
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007158 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00007159alist_clear(al)
7160 alist_T *al;
7161{
7162 while (--al->al_ga.ga_len >= 0)
7163 vim_free(AARGLIST(al)[al->al_ga.ga_len].ae_fname);
7164 ga_clear(&al->al_ga);
7165}
7166
7167/*
7168 * Init an argument list.
7169 */
7170 void
7171alist_init(al)
7172 alist_T *al;
7173{
7174 ga_init2(&al->al_ga, (int)sizeof(aentry_T), 5);
7175}
7176
7177#if defined(FEAT_WINDOWS) || defined(PROTO)
7178
7179/*
7180 * Remove a reference from an argument list.
7181 * Ignored when the argument list is the global one.
7182 * If the argument list is no longer used by any window, free it.
7183 */
7184 void
7185alist_unlink(al)
7186 alist_T *al;
7187{
7188 if (al != &global_alist && --al->al_refcount <= 0)
7189 {
7190 alist_clear(al);
7191 vim_free(al);
7192 }
7193}
7194
7195# if defined(FEAT_LISTCMDS) || defined(HAVE_DROP_FILE) || defined(PROTO)
7196/*
7197 * Create a new argument list and use it for the current window.
7198 */
7199 void
7200alist_new()
7201{
7202 curwin->w_alist = (alist_T *)alloc((unsigned)sizeof(alist_T));
7203 if (curwin->w_alist == NULL)
7204 {
7205 curwin->w_alist = &global_alist;
7206 ++global_alist.al_refcount;
7207 }
7208 else
7209 {
7210 curwin->w_alist->al_refcount = 1;
7211 alist_init(curwin->w_alist);
7212 }
7213}
7214# endif
7215#endif
7216
7217#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE) || defined(PROTO)
7218/*
7219 * Expand the file names in the global argument list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00007220 * If "fnum_list" is not NULL, use "fnum_list[fnum_len]" as a list of buffer
7221 * numbers to be re-used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007222 */
7223 void
Bram Moolenaar86b68352004-12-27 21:59:20 +00007224alist_expand(fnum_list, fnum_len)
7225 int *fnum_list;
7226 int fnum_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007227{
7228 char_u **old_arg_files;
Bram Moolenaar86b68352004-12-27 21:59:20 +00007229 int old_arg_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007230 char_u **new_arg_files;
7231 int new_arg_file_count;
7232 char_u *save_p_su = p_su;
7233 int i;
7234
7235 /* Don't use 'suffixes' here. This should work like the shell did the
7236 * expansion. Also, the vimrc file isn't read yet, thus the user
7237 * can't set the options. */
7238 p_su = empty_option;
7239 old_arg_files = (char_u **)alloc((unsigned)(sizeof(char_u *) * GARGCOUNT));
7240 if (old_arg_files != NULL)
7241 {
7242 for (i = 0; i < GARGCOUNT; ++i)
Bram Moolenaar86b68352004-12-27 21:59:20 +00007243 old_arg_files[i] = vim_strsave(GARGLIST[i].ae_fname);
7244 old_arg_count = GARGCOUNT;
7245 if (expand_wildcards(old_arg_count, old_arg_files,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007246 &new_arg_file_count, &new_arg_files,
Bram Moolenaarb5609832011-07-20 15:04:58 +02007247 EW_FILE|EW_NOTFOUND|EW_ADDSLASH|EW_NOERROR) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007248 && new_arg_file_count > 0)
7249 {
Bram Moolenaar86b68352004-12-27 21:59:20 +00007250 alist_set(&global_alist, new_arg_file_count, new_arg_files,
7251 TRUE, fnum_list, fnum_len);
7252 FreeWild(old_arg_count, old_arg_files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007253 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007254 }
7255 p_su = save_p_su;
7256}
7257#endif
7258
7259/*
7260 * Set the argument list for the current window.
7261 * Takes over the allocated files[] and the allocated fnames in it.
7262 */
7263 void
Bram Moolenaar86b68352004-12-27 21:59:20 +00007264alist_set(al, count, files, use_curbuf, fnum_list, fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007265 alist_T *al;
7266 int count;
7267 char_u **files;
7268 int use_curbuf;
Bram Moolenaar86b68352004-12-27 21:59:20 +00007269 int *fnum_list;
7270 int fnum_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007271{
7272 int i;
7273
7274 alist_clear(al);
7275 if (ga_grow(&al->al_ga, count) == OK)
7276 {
7277 for (i = 0; i < count; ++i)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007278 {
7279 if (got_int)
7280 {
7281 /* When adding many buffers this can take a long time. Allow
7282 * interrupting here. */
7283 while (i < count)
7284 vim_free(files[i++]);
7285 break;
7286 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00007287
7288 /* May set buffer name of a buffer previously used for the
7289 * argument list, so that it's re-used by alist_add. */
7290 if (fnum_list != NULL && i < fnum_len)
7291 buf_set_name(fnum_list[i], files[i]);
7292
Bram Moolenaar071d4272004-06-13 20:20:40 +00007293 alist_add(al, files[i], use_curbuf ? 2 : 1);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007294 ui_breakcheck();
7295 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007296 vim_free(files);
7297 }
7298 else
7299 FreeWild(count, files);
7300#ifdef FEAT_WINDOWS
7301 if (al == &global_alist)
7302#endif
7303 arg_had_last = FALSE;
7304}
7305
7306/*
7307 * Add file "fname" to argument list "al".
7308 * "fname" must have been allocated and "al" must have been checked for room.
7309 */
7310 void
7311alist_add(al, fname, set_fnum)
7312 alist_T *al;
7313 char_u *fname;
7314 int set_fnum; /* 1: set buffer number; 2: re-use curbuf */
7315{
7316 if (fname == NULL) /* don't add NULL file names */
7317 return;
7318#ifdef BACKSLASH_IN_FILENAME
7319 slash_adjust(fname);
7320#endif
7321 AARGLIST(al)[al->al_ga.ga_len].ae_fname = fname;
7322 if (set_fnum > 0)
7323 AARGLIST(al)[al->al_ga.ga_len].ae_fnum =
7324 buflist_add(fname, BLN_LISTED | (set_fnum == 2 ? BLN_CURBUF : 0));
7325 ++al->al_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007326}
7327
7328#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
7329/*
7330 * Adjust slashes in file names. Called after 'shellslash' was set.
7331 */
7332 void
7333alist_slash_adjust()
7334{
7335 int i;
7336# ifdef FEAT_WINDOWS
7337 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007338 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007339# endif
7340
7341 for (i = 0; i < GARGCOUNT; ++i)
7342 if (GARGLIST[i].ae_fname != NULL)
7343 slash_adjust(GARGLIST[i].ae_fname);
7344# ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00007345 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007346 if (wp->w_alist != &global_alist)
7347 for (i = 0; i < WARGCOUNT(wp); ++i)
7348 if (WARGLIST(wp)[i].ae_fname != NULL)
7349 slash_adjust(WARGLIST(wp)[i].ae_fname);
7350# endif
7351}
7352#endif
7353
7354/*
7355 * ":preserve".
7356 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007357 static void
7358ex_preserve(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007359 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007360{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007361 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007362 ml_preserve(curbuf, TRUE);
7363}
7364
7365/*
7366 * ":recover".
7367 */
7368 static void
7369ex_recover(eap)
7370 exarg_T *eap;
7371{
7372 /* Set recoverymode right away to avoid the ATTENTION prompt. */
7373 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01007374 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
7375 | CCGD_MULTWIN
7376 | (eap->forceit ? CCGD_FORCEIT : 0)
7377 | CCGD_EXCMD)
7378
Bram Moolenaar071d4272004-06-13 20:20:40 +00007379 && (*eap->arg == NUL
7380 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
7381 ml_recover();
7382 recoverymode = FALSE;
7383}
7384
7385/*
7386 * Command modifier used in a wrong way.
7387 */
7388 static void
7389ex_wrongmodifier(eap)
7390 exarg_T *eap;
7391{
7392 eap->errmsg = e_invcmd;
7393}
7394
7395#ifdef FEAT_WINDOWS
7396/*
7397 * :sview [+command] file split window with new file, read-only
7398 * :split [[+command] file] split window with current or new file
7399 * :vsplit [[+command] file] split window vertically with current or new file
7400 * :new [[+command] file] split window with no or new file
7401 * :vnew [[+command] file] split vertically window with no or new file
7402 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007403 *
7404 * :tabedit open new Tab page with empty window
7405 * :tabedit [+command] file open new Tab page and edit "file"
7406 * :tabnew [[+command] file] just like :tabedit
7407 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007408 */
7409 void
7410ex_splitview(eap)
7411 exarg_T *eap;
7412{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007413 win_T *old_curwin = curwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007414# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007415 char_u *fname = NULL;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007416# endif
7417# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007418 int browse_flag = cmdmod.browse;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007419# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007420
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007421# ifndef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007422 if (eap->cmdidx == CMD_vsplit || eap->cmdidx == CMD_vnew)
7423 {
7424 ex_ni(eap);
7425 return;
7426 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007427# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007428
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007429# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007430 need_mouse_correct = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007431# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007432
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007433# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00007434 /* A ":split" in the quickfix window works like ":new". Don't want two
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007435 * quickfix windows. But it's OK when doing ":tab split". */
7436 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007437 {
7438 if (eap->cmdidx == CMD_split)
7439 eap->cmdidx = CMD_new;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007440# ifdef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007441 if (eap->cmdidx == CMD_vsplit)
7442 eap->cmdidx = CMD_vnew;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007443# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007444 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007445# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007446
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007447# ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007448 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007449 {
7450 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
7451 FNAME_MESS, TRUE, curbuf->b_ffname);
7452 if (fname == NULL)
7453 goto theend;
7454 eap->arg = fname;
7455 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007456# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007457 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007458# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007459# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007460# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007461 if (cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007462# ifdef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007463 && eap->cmdidx != CMD_vnew
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007464# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007465 && eap->cmdidx != CMD_new)
7466 {
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007467# ifdef FEAT_AUTOCMD
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007468 if (
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007469# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007470 !gui.in_use &&
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007471# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007472 au_has_group((char_u *)"FileExplorer"))
7473 {
7474 /* No browsing supported but we do have the file explorer:
7475 * Edit the directory. */
7476 if (*eap->arg == NUL || !mch_isdir(eap->arg))
7477 eap->arg = (char_u *)".";
7478 }
7479 else
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007480# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007481 {
7482 fname = do_browse(0, (char_u *)_("Edit File in new window"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007483 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007484 if (fname == NULL)
7485 goto theend;
7486 eap->arg = fname;
7487 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007488 }
7489 cmdmod.browse = FALSE; /* Don't browse again in do_ecmd(). */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007490# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007491
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007492 /*
7493 * Either open new tab page or split the window.
7494 */
7495 if (eap->cmdidx == CMD_tabedit
7496 || eap->cmdidx == CMD_tabfind
7497 || eap->cmdidx == CMD_tabnew)
7498 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00007499 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
7500 : eap->addr_count == 0 ? 0
7501 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007502 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00007503 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007504
7505 /* set the alternate buffer for the window we came from */
7506 if (curwin != old_curwin
7507 && win_valid(old_curwin)
7508 && old_curwin->w_buffer != curbuf
7509 && !cmdmod.keepalt)
7510 old_curwin->w_alt_fnum = curbuf->b_fnum;
7511 }
7512 }
7513 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007514 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
7515 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007516# ifdef FEAT_SCROLLBIND
Bram Moolenaar071d4272004-06-13 20:20:40 +00007517 /* Reset 'scrollbind' when editing another file, but keep it when
7518 * doing ":split" without arguments. */
7519 if (*eap->arg != NUL
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007520# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007521 || cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007522# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007523 )
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007524 {
7525 RESET_BINDING(curwin);
7526 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007527 else
7528 do_check_scrollbind(FALSE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007529# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007530 do_exedit(eap, old_curwin);
7531 }
7532
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007533# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007534 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007535# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007536
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007537# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007538theend:
7539 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007540# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007541}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007542
7543/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007544 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007545 */
7546 void
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007547tabpage_new()
7548{
7549 exarg_T ea;
7550
7551 vim_memset(&ea, 0, sizeof(ea));
7552 ea.cmdidx = CMD_tabnew;
7553 ea.cmd = (char_u *)"tabn";
7554 ea.arg = (char_u *)"";
7555 ex_splitview(&ea);
7556}
7557
7558/*
7559 * :tabnext command
7560 */
7561 static void
7562ex_tabnext(eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007563 exarg_T *eap;
7564{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007565 switch (eap->cmdidx)
7566 {
7567 case CMD_tabfirst:
7568 case CMD_tabrewind:
7569 goto_tabpage(1);
7570 break;
7571 case CMD_tablast:
7572 goto_tabpage(9999);
7573 break;
7574 case CMD_tabprevious:
7575 case CMD_tabNext:
7576 goto_tabpage(eap->addr_count == 0 ? -1 : -(int)eap->line2);
7577 break;
7578 default: /* CMD_tabnext */
7579 goto_tabpage(eap->addr_count == 0 ? 0 : (int)eap->line2);
7580 break;
7581 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007582}
7583
7584/*
7585 * :tabmove command
7586 */
7587 static void
7588ex_tabmove(eap)
7589 exarg_T *eap;
7590{
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02007591 int tab_number = 9999;
7592
7593 if (eap->arg && *eap->arg != NUL)
7594 {
7595 char_u *p = eap->arg;
7596 int relative = 0; /* argument +N/-N means: move N places to the
7597 * right/left relative to the current position. */
7598
7599 if (*eap->arg == '-')
7600 {
7601 relative = -1;
7602 p = eap->arg + 1;
7603 }
7604 else if (*eap->arg == '+')
7605 {
7606 relative = 1;
7607 p = eap->arg + 1;
7608 }
7609 else
7610 p = eap->arg;
7611
7612 if (p == skipdigits(p))
7613 {
7614 /* No numbers as argument. */
7615 eap->errmsg = e_invarg;
7616 return;
7617 }
7618
7619 tab_number = getdigits(&p);
7620 if (relative != 0)
7621 tab_number = tab_number * relative + tabpage_index(curtab) - 1;;
7622 }
7623 else if (eap->addr_count != 0)
7624 tab_number = eap->line2;
7625
7626 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007627}
7628
7629/*
7630 * :tabs command: List tabs and their contents.
7631 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007632 static void
7633ex_tabs(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007634 exarg_T *eap UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007635{
7636 tabpage_T *tp;
7637 win_T *wp;
7638 int tabcount = 1;
7639
7640 msg_start();
7641 msg_scroll = TRUE;
7642 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
7643 {
7644 msg_putchar('\n');
7645 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
7646 msg_outtrans_attr(IObuff, hl_attr(HLF_T));
7647 out_flush(); /* output one line at a time */
7648 ui_breakcheck();
7649
Bram Moolenaar030f0df2006-02-21 22:02:53 +00007650 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007651 wp = firstwin;
7652 else
7653 wp = tp->tp_firstwin;
7654 for ( ; wp != NULL && !got_int; wp = wp->w_next)
7655 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007656 msg_putchar('\n');
7657 msg_putchar(wp == curwin ? '>' : ' ');
7658 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007659 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
7660 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007661 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02007662 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007663 else
7664 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
7665 IObuff, IOSIZE, TRUE);
7666 msg_outtrans(IObuff);
7667 out_flush(); /* output one line at a time */
7668 ui_breakcheck();
7669 }
7670 }
7671}
7672
7673#endif /* FEAT_WINDOWS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007674
7675/*
7676 * ":mode": Set screen mode.
7677 * If no argument given, just get the screen size and redraw.
7678 */
7679 static void
7680ex_mode(eap)
7681 exarg_T *eap;
7682{
7683 if (*eap->arg == NUL)
7684 shell_resized();
7685 else
7686 mch_screenmode(eap->arg);
7687}
7688
7689#ifdef FEAT_WINDOWS
7690/*
7691 * ":resize".
7692 * set, increment or decrement current window height
7693 */
7694 static void
7695ex_resize(eap)
7696 exarg_T *eap;
7697{
7698 int n;
7699 win_T *wp = curwin;
7700
7701 if (eap->addr_count > 0)
7702 {
7703 n = eap->line2;
7704 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
7705 ;
7706 }
7707
7708#ifdef FEAT_GUI
7709 need_mouse_correct = TRUE;
7710#endif
7711 n = atol((char *)eap->arg);
7712#ifdef FEAT_VERTSPLIT
7713 if (cmdmod.split & WSP_VERT)
7714 {
7715 if (*eap->arg == '-' || *eap->arg == '+')
7716 n += W_WIDTH(curwin);
7717 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
7718 n = 9999;
7719 win_setwidth_win((int)n, wp);
7720 }
7721 else
7722#endif
7723 {
7724 if (*eap->arg == '-' || *eap->arg == '+')
7725 n += curwin->w_height;
7726 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
7727 n = 9999;
7728 win_setheight_win((int)n, wp);
7729 }
7730}
7731#endif
7732
7733/*
7734 * ":find [+command] <file>" command.
7735 */
7736 static void
7737ex_find(eap)
7738 exarg_T *eap;
7739{
7740#ifdef FEAT_SEARCHPATH
7741 char_u *fname;
7742 int count;
7743
7744 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
7745 TRUE, curbuf->b_ffname);
7746 if (eap->addr_count > 0)
7747 {
7748 /* Repeat finding the file "count" times. This matters when it
7749 * appears several times in the path. */
7750 count = eap->line2;
7751 while (fname != NULL && --count > 0)
7752 {
7753 vim_free(fname);
7754 fname = find_file_in_path(NULL, 0, FNAME_MESS,
7755 FALSE, curbuf->b_ffname);
7756 }
7757 }
7758
7759 if (fname != NULL)
7760 {
7761 eap->arg = fname;
7762#endif
7763 do_exedit(eap, NULL);
7764#ifdef FEAT_SEARCHPATH
7765 vim_free(fname);
7766 }
7767#endif
7768}
7769
7770/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00007771 * ":open" simulation: for now just work like ":visual".
7772 */
7773 static void
7774ex_open(eap)
7775 exarg_T *eap;
7776{
7777 regmatch_T regmatch;
7778 char_u *p;
7779
7780 curwin->w_cursor.lnum = eap->line2;
7781 beginline(BL_SOL | BL_FIX);
7782 if (*eap->arg == '/')
7783 {
7784 /* ":open /pattern/": put cursor in column found with pattern */
7785 ++eap->arg;
7786 p = skip_regexp(eap->arg, '/', p_magic, NULL);
7787 *p = NUL;
7788 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
7789 if (regmatch.regprog != NULL)
7790 {
7791 regmatch.rm_ic = p_ic;
7792 p = ml_get_curline();
7793 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007794 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007795 else
7796 EMSG(_(e_nomatch));
Bram Moolenaar473de612013-06-08 18:19:48 +02007797 vim_regfree(regmatch.regprog);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007798 }
7799 /* Move to the NUL, ignore any other arguments. */
7800 eap->arg += STRLEN(eap->arg);
7801 }
7802 check_cursor();
7803
7804 eap->cmdidx = CMD_visual;
7805 do_exedit(eap, NULL);
7806}
7807
7808/*
7809 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007810 */
7811 static void
7812ex_edit(eap)
7813 exarg_T *eap;
7814{
7815 do_exedit(eap, NULL);
7816}
7817
7818/*
7819 * ":edit <file>" command and alikes.
7820 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007821 void
7822do_exedit(eap, old_curwin)
7823 exarg_T *eap;
7824 win_T *old_curwin; /* curwin before doing a split or NULL */
7825{
7826 int n;
7827#ifdef FEAT_WINDOWS
7828 int need_hide;
7829#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00007830 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007831
7832 /*
7833 * ":vi" command ends Ex mode.
7834 */
7835 if (exmode_active && (eap->cmdidx == CMD_visual
7836 || eap->cmdidx == CMD_view))
7837 {
7838 exmode_active = FALSE;
7839 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007840 {
7841 /* Special case: ":global/pat/visual\NLvi-commands" */
7842 if (global_busy)
7843 {
7844 int rd = RedrawingDisabled;
7845 int nwr = no_wait_return;
7846 int ms = msg_scroll;
7847#ifdef FEAT_GUI
7848 int he = hold_gui_events;
7849#endif
7850
7851 if (eap->nextcmd != NULL)
7852 {
7853 stuffReadbuff(eap->nextcmd);
7854 eap->nextcmd = NULL;
7855 }
7856
7857 if (exmode_was != EXMODE_VIM)
7858 settmode(TMODE_RAW);
7859 RedrawingDisabled = 0;
7860 no_wait_return = 0;
7861 need_wait_return = FALSE;
7862 msg_scroll = 0;
7863#ifdef FEAT_GUI
7864 hold_gui_events = 0;
7865#endif
7866 must_redraw = CLEAR;
7867
7868 main_loop(FALSE, TRUE);
7869
7870 RedrawingDisabled = rd;
7871 no_wait_return = nwr;
7872 msg_scroll = ms;
7873#ifdef FEAT_GUI
7874 hold_gui_events = he;
7875#endif
7876 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007877 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007878 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007879 }
7880
7881 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007882 || eap->cmdidx == CMD_tabnew
7883 || eap->cmdidx == CMD_tabedit
Bram Moolenaar071d4272004-06-13 20:20:40 +00007884#ifdef FEAT_VERTSPLIT
7885 || eap->cmdidx == CMD_vnew
7886#endif
7887 ) && *eap->arg == NUL)
7888 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007889 /* ":new" or ":tabnew" without argument: edit an new empty buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007890 setpcmark();
7891 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007892 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
7893 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007894 }
7895 else if ((eap->cmdidx != CMD_split
7896#ifdef FEAT_VERTSPLIT
7897 && eap->cmdidx != CMD_vsplit
7898#endif
7899 )
7900 || *eap->arg != NUL
7901#ifdef FEAT_BROWSE
7902 || cmdmod.browse
7903#endif
7904 )
7905 {
Bram Moolenaar5555acc2006-04-07 21:33:12 +00007906#ifdef FEAT_AUTOCMD
7907 /* Can't edit another file when "curbuf_lock" is set. Only ":edit"
7908 * can bring us here, others are stopped earlier. */
7909 if (*eap->arg != NUL && curbuf_locked())
7910 return;
7911#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007912 n = readonlymode;
7913 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
7914 readonlymode = TRUE;
7915 else if (eap->cmdidx == CMD_enew)
7916 readonlymode = FALSE; /* 'readonly' doesn't make sense in an
7917 empty buffer */
7918 setpcmark();
7919 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
7920 NULL, eap,
7921 /* ":edit" goes to first line if Vi compatible */
7922 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
7923 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
7924 ? ECMD_ONE : eap->do_ecmd_lnum,
7925 (P_HID(curbuf) ? ECMD_HIDE : 0)
7926 + (eap->forceit ? ECMD_FORCEIT : 0)
7927#ifdef FEAT_LISTCMDS
7928 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
7929#endif
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007930 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007931 {
7932 /* Editing the file failed. If the window was split, close it. */
7933#ifdef FEAT_WINDOWS
7934 if (old_curwin != NULL)
7935 {
7936 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
7937 if (!need_hide || P_HID(curbuf))
7938 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007939# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7940 cleanup_T cs;
7941
7942 /* Reset the error/interrupt/exception state here so that
7943 * aborting() returns FALSE when closing a window. */
7944 enter_cleanup(&cs);
7945# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007946# ifdef FEAT_GUI
7947 need_mouse_correct = TRUE;
7948# endif
7949 win_close(curwin, !need_hide && !P_HID(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007950
7951# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7952 /* Restore the error/interrupt/exception state if not
7953 * discarded by a new aborting error, interrupt, or
7954 * uncaught exception. */
7955 leave_cleanup(&cs);
7956# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007957 }
7958 }
7959#endif
7960 }
7961 else if (readonlymode && curbuf->b_nwindows == 1)
7962 {
7963 /* When editing an already visited buffer, 'readonly' won't be set
7964 * but the previous value is kept. With ":view" and ":sview" we
7965 * want the file to be readonly, except when another window is
7966 * editing the same buffer. */
7967 curbuf->b_p_ro = TRUE;
7968 }
7969 readonlymode = n;
7970 }
7971 else
7972 {
7973 if (eap->do_ecmd_cmd != NULL)
7974 do_cmdline_cmd(eap->do_ecmd_cmd);
7975#ifdef FEAT_TITLE
7976 n = curwin->w_arg_idx_invalid;
7977#endif
7978 check_arg_idx(curwin);
7979#ifdef FEAT_TITLE
7980 if (n != curwin->w_arg_idx_invalid)
7981 maketitle();
7982#endif
7983 }
7984
7985#ifdef FEAT_WINDOWS
7986 /*
7987 * if ":split file" worked, set alternate file name in old window to new
7988 * file
7989 */
7990 if (old_curwin != NULL
7991 && *eap->arg != NUL
7992 && curwin != old_curwin
7993 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00007994 && old_curwin->w_buffer != curbuf
7995 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007996 old_curwin->w_alt_fnum = curbuf->b_fnum;
7997#endif
7998
7999 ex_no_reprint = TRUE;
8000}
8001
8002#ifndef FEAT_GUI
8003/*
8004 * ":gui" and ":gvim" when there is no GUI.
8005 */
8006 static void
8007ex_nogui(eap)
8008 exarg_T *eap;
8009{
8010 eap->errmsg = e_nogvim;
8011}
8012#endif
8013
8014#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
8015 static void
8016ex_tearoff(eap)
8017 exarg_T *eap;
8018{
8019 gui_make_tearoff(eap->arg);
8020}
8021#endif
8022
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008023#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008024 static void
8025ex_popup(eap)
8026 exarg_T *eap;
8027{
Bram Moolenaar97409f12005-07-08 22:17:29 +00008028 gui_make_popup(eap->arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008029}
8030#endif
8031
Bram Moolenaar071d4272004-06-13 20:20:40 +00008032 static void
8033ex_swapname(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008034 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008035{
8036 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
8037 MSG(_("No swap file"));
8038 else
8039 msg(curbuf->b_ml.ml_mfp->mf_fname);
8040}
8041
8042/*
8043 * ":syncbind" forces all 'scrollbind' windows to have the same relative
8044 * offset.
8045 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
8046 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008047 static void
8048ex_syncbind(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008049 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008050{
8051#ifdef FEAT_SCROLLBIND
8052 win_T *wp;
8053 long topline;
8054 long y;
8055 linenr_T old_linenr = curwin->w_cursor.lnum;
8056
8057 setpcmark();
8058
8059 /*
8060 * determine max topline
8061 */
8062 if (curwin->w_p_scb)
8063 {
8064 topline = curwin->w_topline;
8065 for (wp = firstwin; wp; wp = wp->w_next)
8066 {
8067 if (wp->w_p_scb && wp->w_buffer)
8068 {
8069 y = wp->w_buffer->b_ml.ml_line_count - p_so;
8070 if (topline > y)
8071 topline = y;
8072 }
8073 }
8074 if (topline < 1)
8075 topline = 1;
8076 }
8077 else
8078 {
8079 topline = 1;
8080 }
8081
8082
8083 /*
8084 * set all scrollbind windows to the same topline
8085 */
8086 wp = curwin;
8087 for (curwin = firstwin; curwin; curwin = curwin->w_next)
8088 {
8089 if (curwin->w_p_scb)
8090 {
8091 y = topline - curwin->w_topline;
8092 if (y > 0)
8093 scrollup(y, TRUE);
8094 else
8095 scrolldown(-y, TRUE);
8096 curwin->w_scbind_pos = topline;
8097 redraw_later(VALID);
8098 cursor_correct();
8099#ifdef FEAT_WINDOWS
8100 curwin->w_redr_status = TRUE;
8101#endif
8102 }
8103 }
8104 curwin = wp;
8105 if (curwin->w_p_scb)
8106 {
8107 did_syncbind = TRUE;
8108 checkpcmark();
8109 if (old_linenr != curwin->w_cursor.lnum)
8110 {
8111 char_u ctrl_o[2];
8112
8113 ctrl_o[0] = Ctrl_O;
8114 ctrl_o[1] = 0;
8115 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
8116 }
8117 }
8118#endif
8119}
8120
8121
8122 static void
8123ex_read(eap)
8124 exarg_T *eap;
8125{
Bram Moolenaardf177f62005-02-22 08:39:57 +00008126 int i;
8127 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
8128 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008129
8130 if (eap->usefilter) /* :r!cmd */
8131 do_bang(1, eap, FALSE, FALSE, TRUE);
8132 else
8133 {
8134 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
8135 return;
8136
8137#ifdef FEAT_BROWSE
8138 if (cmdmod.browse)
8139 {
8140 char_u *browseFile;
8141
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008142 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008143 NULL, NULL, NULL, curbuf);
8144 if (browseFile != NULL)
8145 {
8146 i = readfile(browseFile, NULL,
8147 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8148 vim_free(browseFile);
8149 }
8150 else
8151 i = OK;
8152 }
8153 else
8154#endif
8155 if (*eap->arg == NUL)
8156 {
8157 if (check_fname() == FAIL) /* check for no file name */
8158 return;
8159 i = readfile(curbuf->b_ffname, curbuf->b_fname,
8160 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8161 }
8162 else
8163 {
8164 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
8165 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
8166 i = readfile(eap->arg, NULL,
8167 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8168
8169 }
8170 if (i == FAIL)
8171 {
8172#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
8173 if (!aborting())
8174#endif
8175 EMSG2(_(e_notopen), eap->arg);
8176 }
8177 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008178 {
8179 if (empty && exmode_active)
8180 {
8181 /* Delete the empty line that remains. Historically ex does
8182 * this but vi doesn't. */
8183 if (eap->line2 == 0)
8184 lnum = curbuf->b_ml.ml_line_count;
8185 else
8186 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008187 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008188 {
8189 ml_delete(lnum, FALSE);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008190 if (curwin->w_cursor.lnum > 1
8191 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008192 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00008193 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008194 }
8195 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008196 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008197 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008198 }
8199}
8200
Bram Moolenaarea408852005-06-25 22:49:46 +00008201static char_u *prev_dir = NULL;
8202
8203#if defined(EXITFREE) || defined(PROTO)
8204 void
8205free_cd_dir()
8206{
8207 vim_free(prev_dir);
Bram Moolenaara0174af2008-01-02 20:08:25 +00008208 prev_dir = NULL;
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00008209
8210 vim_free(globaldir);
8211 globaldir = NULL;
Bram Moolenaarea408852005-06-25 22:49:46 +00008212}
8213#endif
8214
Bram Moolenaarf4258302013-06-02 18:20:17 +02008215/*
8216 * Deal with the side effects of changing the current directory.
8217 * When "local" is TRUE then this was after an ":lcd" command.
8218 */
8219 void
8220post_chdir(local)
8221 int local;
8222{
8223 vim_free(curwin->w_localdir);
8224 if (local)
8225 {
8226 /* If still in global directory, need to remember current
8227 * directory as global directory. */
8228 if (globaldir == NULL && prev_dir != NULL)
8229 globaldir = vim_strsave(prev_dir);
8230 /* Remember this local directory for the window. */
8231 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8232 curwin->w_localdir = vim_strsave(NameBuff);
8233 }
8234 else
8235 {
8236 /* We are now in the global directory, no need to remember its
8237 * name. */
8238 vim_free(globaldir);
8239 globaldir = NULL;
8240 curwin->w_localdir = NULL;
8241 }
8242
8243 shorten_fnames(TRUE);
8244}
8245
Bram Moolenaarea408852005-06-25 22:49:46 +00008246
Bram Moolenaar071d4272004-06-13 20:20:40 +00008247/*
8248 * ":cd", ":lcd", ":chdir" and ":lchdir".
8249 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00008250 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00008251ex_cd(eap)
8252 exarg_T *eap;
8253{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008254 char_u *new_dir;
8255 char_u *tofree;
8256
8257 new_dir = eap->arg;
8258#if !defined(UNIX) && !defined(VMS)
8259 /* for non-UNIX ":cd" means: print current directory */
8260 if (*new_dir == NUL)
8261 ex_pwd(NULL);
8262 else
8263#endif
8264 {
Bram Moolenaar8e8fe9b2009-03-11 14:37:32 +00008265#ifdef FEAT_AUTOCMD
8266 if (allbuf_locked())
8267 return;
8268#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00008269 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged()
8270 && !eap->forceit)
8271 {
Bram Moolenaar81870892007-11-11 18:17:28 +00008272 EMSG(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00008273 return;
8274 }
8275
Bram Moolenaar071d4272004-06-13 20:20:40 +00008276 /* ":cd -": Change to previous directory */
8277 if (STRCMP(new_dir, "-") == 0)
8278 {
8279 if (prev_dir == NULL)
8280 {
8281 EMSG(_("E186: No previous directory"));
8282 return;
8283 }
8284 new_dir = prev_dir;
8285 }
8286
8287 /* Save current directory for next ":cd -" */
8288 tofree = prev_dir;
8289 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8290 prev_dir = vim_strsave(NameBuff);
8291 else
8292 prev_dir = NULL;
8293
8294#if defined(UNIX) || defined(VMS)
8295 /* for UNIX ":cd" means: go to home directory */
8296 if (*new_dir == NUL)
8297 {
8298 /* use NameBuff for home directory name */
8299# ifdef VMS
8300 char_u *p;
8301
8302 p = mch_getenv((char_u *)"SYS$LOGIN");
8303 if (p == NULL || *p == NUL) /* empty is the same as not set */
8304 NameBuff[0] = NUL;
8305 else
Bram Moolenaarb6356332005-07-18 21:40:44 +00008306 vim_strncpy(NameBuff, p, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008307# else
8308 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
8309# endif
8310 new_dir = NameBuff;
8311 }
8312#endif
8313 if (new_dir == NULL || vim_chdir(new_dir))
8314 EMSG(_(e_failed));
8315 else
8316 {
Bram Moolenaarf4258302013-06-02 18:20:17 +02008317 post_chdir(eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008318
8319 /* Echo the new current directory if the command was typed. */
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00008320 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008321 ex_pwd(eap);
8322 }
8323 vim_free(tofree);
8324 }
8325}
8326
8327/*
8328 * ":pwd".
8329 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008330 static void
8331ex_pwd(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008332 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008333{
8334 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8335 {
8336#ifdef BACKSLASH_IN_FILENAME
8337 slash_adjust(NameBuff);
8338#endif
8339 msg(NameBuff);
8340 }
8341 else
8342 EMSG(_("E187: Unknown"));
8343}
8344
8345/*
8346 * ":=".
8347 */
8348 static void
8349ex_equal(eap)
8350 exarg_T *eap;
8351{
8352 smsg((char_u *)"%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008353 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008354}
8355
8356 static void
8357ex_sleep(eap)
8358 exarg_T *eap;
8359{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008360 int n;
8361 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008362
8363 if (cursor_valid())
8364 {
8365 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
8366 if (n >= 0)
8367 windgoto((int)n, curwin->w_wcol);
8368 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008369
8370 len = eap->line2;
8371 switch (*eap->arg)
8372 {
8373 case 'm': break;
8374 case NUL: len *= 1000L; break;
8375 default: EMSG2(_(e_invarg2), eap->arg); return;
8376 }
8377 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008378}
8379
8380/*
8381 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
8382 */
8383 void
8384do_sleep(msec)
8385 long msec;
8386{
8387 long done;
8388
8389 cursor_on();
8390 out_flush();
8391 for (done = 0; !got_int && done < msec; done += 1000L)
8392 {
8393 ui_delay(msec - done > 1000L ? 1000L : msec - done, TRUE);
8394 ui_breakcheck();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02008395#ifdef FEAT_NETBEANS_INTG
8396 /* Process the netbeans messages that may have been received in the
8397 * call to ui_breakcheck() when the GUI is in use. This may occur when
8398 * running a test case. */
8399 netbeans_parse_messages();
8400#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008401 }
8402}
8403
8404 static void
8405do_exmap(eap, isabbrev)
8406 exarg_T *eap;
8407 int isabbrev;
8408{
8409 int mode;
8410 char_u *cmdp;
8411
8412 cmdp = eap->cmd;
8413 mode = get_map_mode(&cmdp, eap->forceit || isabbrev);
8414
8415 switch (do_map((*cmdp == 'n') ? 2 : (*cmdp == 'u'),
8416 eap->arg, mode, isabbrev))
8417 {
8418 case 1: EMSG(_(e_invarg));
8419 break;
8420 case 2: EMSG(isabbrev ? _(e_noabbr) : _(e_nomap));
8421 break;
8422 }
8423}
8424
8425/*
8426 * ":winsize" command (obsolete).
8427 */
8428 static void
8429ex_winsize(eap)
8430 exarg_T *eap;
8431{
8432 int w, h;
8433 char_u *arg = eap->arg;
8434 char_u *p;
8435
8436 w = getdigits(&arg);
8437 arg = skipwhite(arg);
8438 p = arg;
8439 h = getdigits(&arg);
8440 if (*p != NUL && *arg == NUL)
8441 set_shellsize(w, h, TRUE);
8442 else
8443 EMSG(_("E465: :winsize requires two number arguments"));
8444}
8445
8446#ifdef FEAT_WINDOWS
8447 static void
8448ex_wincmd(eap)
8449 exarg_T *eap;
8450{
8451 int xchar = NUL;
8452 char_u *p;
8453
8454 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
8455 {
8456 /* CTRL-W g and CTRL-W CTRL-G have an extra command character */
8457 if (eap->arg[1] == NUL)
8458 {
8459 EMSG(_(e_invarg));
8460 return;
8461 }
8462 xchar = eap->arg[1];
8463 p = eap->arg + 2;
8464 }
8465 else
8466 p = eap->arg + 1;
8467
8468 eap->nextcmd = check_nextcmd(p);
8469 p = skipwhite(p);
8470 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
8471 EMSG(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02008472 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008473 {
8474 /* Pass flags on for ":vertical wincmd ]". */
8475 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008476 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008477 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
8478 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008479 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008480 }
8481}
8482#endif
8483
Bram Moolenaar843ee412004-06-30 16:16:41 +00008484#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008485/*
8486 * ":winpos".
8487 */
8488 static void
8489ex_winpos(eap)
8490 exarg_T *eap;
8491{
8492 int x, y;
8493 char_u *arg = eap->arg;
8494 char_u *p;
8495
8496 if (*arg == NUL)
8497 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00008498# if defined(FEAT_GUI) || defined(MSWIN)
8499# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008500 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00008501# else
8502 if (mch_get_winpos(&x, &y) != FAIL)
8503# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008504 {
8505 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
8506 msg(IObuff);
8507 }
8508 else
8509# endif
8510 EMSG(_("E188: Obtaining window position not implemented for this platform"));
8511 }
8512 else
8513 {
8514 x = getdigits(&arg);
8515 arg = skipwhite(arg);
8516 p = arg;
8517 y = getdigits(&arg);
8518 if (*p == NUL || *arg != NUL)
8519 {
8520 EMSG(_("E466: :winpos requires two number arguments"));
8521 return;
8522 }
8523# ifdef FEAT_GUI
8524 if (gui.in_use)
8525 gui_mch_set_winpos(x, y);
8526 else if (gui.starting)
8527 {
8528 /* Remember the coordinates for when the window is opened. */
8529 gui_win_x = x;
8530 gui_win_y = y;
8531 }
8532# ifdef HAVE_TGETENT
8533 else
8534# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00008535# else
8536# ifdef MSWIN
8537 mch_set_winpos(x, y);
8538# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008539# endif
8540# ifdef HAVE_TGETENT
8541 if (*T_CWP)
8542 term_set_winpos(x, y);
8543# endif
8544 }
8545}
8546#endif
8547
8548/*
8549 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
8550 */
8551 static void
8552ex_operators(eap)
8553 exarg_T *eap;
8554{
8555 oparg_T oa;
8556
8557 clear_oparg(&oa);
8558 oa.regname = eap->regname;
8559 oa.start.lnum = eap->line1;
8560 oa.end.lnum = eap->line2;
8561 oa.line_count = eap->line2 - eap->line1 + 1;
8562 oa.motion_type = MLINE;
8563#ifdef FEAT_VIRTUALEDIT
8564 virtual_op = FALSE;
8565#endif
8566 if (eap->cmdidx != CMD_yank) /* position cursor for undo */
8567 {
8568 setpcmark();
8569 curwin->w_cursor.lnum = eap->line1;
8570 beginline(BL_SOL | BL_FIX);
8571 }
8572
8573 switch (eap->cmdidx)
8574 {
8575 case CMD_delete:
8576 oa.op_type = OP_DELETE;
8577 op_delete(&oa);
8578 break;
8579
8580 case CMD_yank:
8581 oa.op_type = OP_YANK;
8582 (void)op_yank(&oa, FALSE, TRUE);
8583 break;
8584
8585 default: /* CMD_rshift or CMD_lshift */
Bram Moolenaar6e707362013-06-14 19:15:58 +02008586 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00008587#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02008588 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
8589#else
8590 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00008591#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02008592 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00008593 oa.op_type = OP_RSHIFT;
8594 else
8595 oa.op_type = OP_LSHIFT;
8596 op_shift(&oa, FALSE, eap->amount);
8597 break;
8598 }
8599#ifdef FEAT_VIRTUALEDIT
8600 virtual_op = MAYBE;
8601#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00008602 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008603}
8604
8605/*
8606 * ":put".
8607 */
8608 static void
8609ex_put(eap)
8610 exarg_T *eap;
8611{
8612 /* ":0put" works like ":1put!". */
8613 if (eap->line2 == 0)
8614 {
8615 eap->line2 = 1;
8616 eap->forceit = TRUE;
8617 }
8618 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008619 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
8620 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008621}
8622
8623/*
8624 * Handle ":copy" and ":move".
8625 */
8626 static void
8627ex_copymove(eap)
8628 exarg_T *eap;
8629{
8630 long n;
8631
8632 n = get_address(&eap->arg, FALSE, FALSE);
8633 if (eap->arg == NULL) /* error detected */
8634 {
8635 eap->nextcmd = NULL;
8636 return;
8637 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008638 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008639
8640 /*
8641 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
8642 */
8643 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
8644 {
8645 EMSG(_(e_invaddr));
8646 return;
8647 }
8648
8649 if (eap->cmdidx == CMD_move)
8650 {
8651 if (do_move(eap->line1, eap->line2, n) == FAIL)
8652 return;
8653 }
8654 else
8655 ex_copy(eap->line1, eap->line2, n);
8656 u_clearline();
8657 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008658 ex_may_print(eap);
8659}
8660
8661/*
8662 * Print the current line if flags were given to the Ex command.
8663 */
8664 static void
8665ex_may_print(eap)
8666 exarg_T *eap;
8667{
8668 if (eap->flags != 0)
8669 {
8670 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
8671 (eap->flags & EXFLAG_LIST));
8672 ex_no_reprint = TRUE;
8673 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008674}
8675
8676/*
8677 * ":smagic" and ":snomagic".
8678 */
8679 static void
8680ex_submagic(eap)
8681 exarg_T *eap;
8682{
8683 int magic_save = p_magic;
8684
8685 p_magic = (eap->cmdidx == CMD_smagic);
8686 do_sub(eap);
8687 p_magic = magic_save;
8688}
8689
8690/*
8691 * ":join".
8692 */
8693 static void
8694ex_join(eap)
8695 exarg_T *eap;
8696{
8697 curwin->w_cursor.lnum = eap->line1;
8698 if (eap->line1 == eap->line2)
8699 {
8700 if (eap->addr_count >= 2) /* :2,2join does nothing */
8701 return;
8702 if (eap->line2 == curbuf->b_ml.ml_line_count)
8703 {
8704 beep_flush();
8705 return;
8706 }
8707 ++eap->line2;
8708 }
Bram Moolenaar81340392012-06-06 16:12:59 +02008709 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008710 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008711 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008712}
8713
8714/*
8715 * ":[addr]@r" or ":[addr]*r": execute register
8716 */
8717 static void
8718ex_at(eap)
8719 exarg_T *eap;
8720{
8721 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00008722 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008723
8724 curwin->w_cursor.lnum = eap->line2;
8725
8726#ifdef USE_ON_FLY_SCROLL
8727 dont_scroll = TRUE; /* disallow scrolling here */
8728#endif
8729
8730 /* get the register name. No name means to use the previous one */
8731 c = *eap->arg;
8732 if (c == NUL || (c == '*' && *eap->cmd == '*'))
8733 c = '@';
Bram Moolenaard333d1e2006-11-07 17:43:47 +00008734 /* Put the register in the typeahead buffer with the "silent" flag. */
8735 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
8736 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008737 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008738 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00008739 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008740 else
8741 {
8742 int save_efr = exec_from_reg;
8743
8744 exec_from_reg = TRUE;
8745
8746 /*
8747 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00008748 * Continue until the stuff buffer is empty and all added characters
8749 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008750 */
Bram Moolenaar60462872009-11-03 11:40:19 +00008751 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008752 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
8753
8754 exec_from_reg = save_efr;
8755 }
8756}
8757
8758/*
8759 * ":!".
8760 */
8761 static void
8762ex_bang(eap)
8763 exarg_T *eap;
8764{
8765 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
8766}
8767
8768/*
8769 * ":undo".
8770 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008771 static void
8772ex_undo(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008773 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008774{
Bram Moolenaard3667a22006-03-16 21:35:52 +00008775 if (eap->addr_count == 1) /* :undo 123 */
Bram Moolenaar730cde92010-06-27 05:18:54 +02008776 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00008777 else
8778 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008779}
8780
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008781#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008782 static void
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008783ex_wundo(eap)
8784 exarg_T *eap;
8785{
8786 char_u hash[UNDO_HASH_SIZE];
8787
8788 u_compute_hash(hash);
8789 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
8790}
8791
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008792 static void
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008793ex_rundo(eap)
8794 exarg_T *eap;
8795{
8796 char_u hash[UNDO_HASH_SIZE];
8797
8798 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02008799 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008800}
8801#endif
8802
Bram Moolenaar071d4272004-06-13 20:20:40 +00008803/*
8804 * ":redo".
8805 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008806 static void
8807ex_redo(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008808 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008809{
8810 u_redo(1);
8811}
8812
8813/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008814 * ":earlier" and ":later".
8815 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008816 static void
8817ex_later(eap)
8818 exarg_T *eap;
8819{
8820 long count = 0;
8821 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008822 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008823 char_u *p = eap->arg;
8824
8825 if (*p == NUL)
8826 count = 1;
8827 else if (isdigit(*p))
8828 {
8829 count = getdigits(&p);
8830 switch (*p)
8831 {
8832 case 's': ++p; sec = TRUE; break;
8833 case 'm': ++p; sec = TRUE; count *= 60; break;
8834 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008835 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
8836 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008837 }
8838 }
8839
8840 if (*p != NUL)
8841 EMSG2(_(e_invarg2), eap->arg);
8842 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02008843 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
8844 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008845}
8846
8847/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008848 * ":redir": start/stop redirection.
8849 */
8850 static void
8851ex_redir(eap)
8852 exarg_T *eap;
8853{
8854 char *mode;
8855 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00008856 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008857
8858 if (STRICMP(eap->arg, "END") == 0)
8859 close_redir();
8860 else
8861 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008862 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008863 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008864 ++arg;
8865 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008866 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008867 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008868 mode = "a";
8869 }
8870 else
8871 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00008872 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008873
8874 close_redir();
8875
8876 /* Expand environment variables and "~/". */
Bram Moolenaarca472992005-01-21 11:46:23 +00008877 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008878 if (fname == NULL)
8879 return;
8880#ifdef FEAT_BROWSE
8881 if (cmdmod.browse)
8882 {
8883 char_u *browseFile;
8884
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008885 browseFile = do_browse(BROWSE_SAVE,
8886 (char_u *)_("Save Redirection"),
8887 fname, NULL, NULL, BROWSE_FILTER_ALL_FILES, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008888 if (browseFile == NULL)
8889 return; /* operation cancelled */
8890 vim_free(fname);
8891 fname = browseFile;
8892 eap->forceit = TRUE; /* since dialog already asked */
8893 }
8894#endif
8895
8896 redir_fd = open_exfile(fname, eap->forceit, mode);
8897 vim_free(fname);
8898 }
8899#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00008900 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008901 {
8902 /* redirect to a register a-z (resp. A-Z for appending) */
8903 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00008904 ++arg;
8905 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008906# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00008907 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00008908 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008909# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00008910 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008911 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008912 redir_reg = *arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008913 if (*arg == '>' && arg[1] == '>') /* append */
Bram Moolenaard9d30582005-05-18 22:10:28 +00008914 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008915 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008916 {
Bram Moolenaarc188b882007-10-19 14:20:54 +00008917 /* Can use both "@a" and "@a>". */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00008918 if (*arg == '>')
8919 arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008920 /* Make register empty when not using @A-@Z and the
8921 * command is valid. */
8922 if (*arg == NUL && !isupper(redir_reg))
8923 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008924 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008925 }
8926 if (*arg != NUL)
8927 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00008928 redir_reg = 0;
Bram Moolenaard9d30582005-05-18 22:10:28 +00008929 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008930 }
8931 }
8932 else if (*arg == '=' && arg[1] == '>')
8933 {
8934 int append;
8935
8936 /* redirect to a variable */
8937 close_redir();
8938 arg += 2;
8939
8940 if (*arg == '>')
8941 {
8942 ++arg;
8943 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008944 }
8945 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008946 append = FALSE;
8947
8948 if (var_redir_start(skipwhite(arg), append) == OK)
8949 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008950 }
8951#endif
8952
8953 /* TODO: redirect to a buffer */
8954
Bram Moolenaar071d4272004-06-13 20:20:40 +00008955 else
Bram Moolenaarca472992005-01-21 11:46:23 +00008956 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008957 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00008958
8959 /* Make sure redirection is not off. Can happen for cmdline completion
8960 * that indirectly invokes a command to catch its output. */
8961 if (redir_fd != NULL
8962#ifdef FEAT_EVAL
8963 || redir_reg || redir_vname
8964#endif
8965 )
8966 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008967}
8968
8969/*
8970 * ":redraw": force redraw
8971 */
8972 static void
8973ex_redraw(eap)
8974 exarg_T *eap;
8975{
8976 int r = RedrawingDisabled;
8977 int p = p_lz;
8978
8979 RedrawingDisabled = 0;
8980 p_lz = FALSE;
8981 update_topline();
8982 update_screen(eap->forceit ? CLEAR :
8983#ifdef FEAT_VISUAL
8984 VIsual_active ? INVERTED :
8985#endif
8986 0);
8987#ifdef FEAT_TITLE
8988 if (need_maketitle)
8989 maketitle();
8990#endif
8991 RedrawingDisabled = r;
8992 p_lz = p;
8993
8994 /* Reset msg_didout, so that a message that's there is overwritten. */
8995 msg_didout = FALSE;
8996 msg_col = 0;
8997
Bram Moolenaar56667a52013-07-17 11:54:28 +02008998 /* No need to wait after an intentional redraw. */
8999 need_wait_return = FALSE;
9000
Bram Moolenaar071d4272004-06-13 20:20:40 +00009001 out_flush();
9002}
9003
9004/*
9005 * ":redrawstatus": force redraw of status line(s)
9006 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009007 static void
9008ex_redrawstatus(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009009 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009010{
9011#if defined(FEAT_WINDOWS)
9012 int r = RedrawingDisabled;
9013 int p = p_lz;
9014
9015 RedrawingDisabled = 0;
9016 p_lz = FALSE;
9017 if (eap->forceit)
9018 status_redraw_all();
9019 else
9020 status_redraw_curbuf();
9021 update_screen(
9022# ifdef FEAT_VISUAL
9023 VIsual_active ? INVERTED :
9024# endif
9025 0);
9026 RedrawingDisabled = r;
9027 p_lz = p;
9028 out_flush();
9029#endif
9030}
9031
9032 static void
9033close_redir()
9034{
9035 if (redir_fd != NULL)
9036 {
9037 fclose(redir_fd);
9038 redir_fd = NULL;
9039 }
9040#ifdef FEAT_EVAL
9041 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009042 if (redir_vname)
9043 {
9044 var_redir_stop();
9045 redir_vname = 0;
9046 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009047#endif
9048}
9049
9050#if defined(FEAT_SESSION) && defined(USE_CRNL)
9051# define MKSESSION_NL
9052static int mksession_nl = FALSE; /* use NL only in put_eol() */
9053#endif
9054
9055/*
9056 * ":mkexrc", ":mkvimrc", ":mkview" and ":mksession".
9057 */
9058 static void
9059ex_mkrc(eap)
9060 exarg_T *eap;
9061{
9062 FILE *fd;
9063 int failed = FALSE;
9064 char_u *fname;
9065#ifdef FEAT_BROWSE
9066 char_u *browseFile = NULL;
9067#endif
9068#ifdef FEAT_SESSION
9069 int view_session = FALSE;
9070 int using_vdir = FALSE; /* using 'viewdir'? */
9071 char_u *viewFile = NULL;
9072 unsigned *flagp;
9073#endif
9074
9075 if (eap->cmdidx == CMD_mksession || eap->cmdidx == CMD_mkview)
9076 {
9077#ifdef FEAT_SESSION
9078 view_session = TRUE;
9079#else
9080 ex_ni(eap);
9081 return;
9082#endif
9083 }
9084
9085#ifdef FEAT_SESSION
Bram Moolenaar8d594672009-07-01 18:18:57 +00009086 /* Use the short file name until ":lcd" is used. We also don't use the
9087 * short file name when 'acd' is set, that is checked later. */
Bram Moolenaareeefcc72007-05-01 21:21:21 +00009088 did_lcd = FALSE;
9089
Bram Moolenaar071d4272004-06-13 20:20:40 +00009090 /* ":mkview" or ":mkview 9": generate file name with 'viewdir' */
9091 if (eap->cmdidx == CMD_mkview
9092 && (*eap->arg == NUL
9093 || (vim_isdigit(*eap->arg) && eap->arg[1] == NUL)))
9094 {
9095 eap->forceit = TRUE;
9096 fname = get_view_file(*eap->arg);
9097 if (fname == NULL)
9098 return;
9099 viewFile = fname;
9100 using_vdir = TRUE;
9101 }
9102 else
9103#endif
9104 if (*eap->arg != NUL)
9105 fname = eap->arg;
9106 else if (eap->cmdidx == CMD_mkvimrc)
9107 fname = (char_u *)VIMRC_FILE;
9108#ifdef FEAT_SESSION
9109 else if (eap->cmdidx == CMD_mksession)
9110 fname = (char_u *)SESSION_FILE;
9111#endif
9112 else
9113 fname = (char_u *)EXRC_FILE;
9114
9115#ifdef FEAT_BROWSE
9116 if (cmdmod.browse)
9117 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00009118 browseFile = do_browse(BROWSE_SAVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009119# ifdef FEAT_SESSION
9120 eap->cmdidx == CMD_mkview ? (char_u *)_("Save View") :
9121 eap->cmdidx == CMD_mksession ? (char_u *)_("Save Session") :
9122# endif
9123 (char_u *)_("Save Setup"),
9124 fname, (char_u *)"vim", NULL, BROWSE_FILTER_MACROS, NULL);
9125 if (browseFile == NULL)
9126 goto theend;
9127 fname = browseFile;
9128 eap->forceit = TRUE; /* since dialog already asked */
9129 }
9130#endif
9131
9132#if defined(FEAT_SESSION) && defined(vim_mkdir)
9133 /* When using 'viewdir' may have to create the directory. */
9134 if (using_vdir && !mch_isdir(p_vdir))
Bram Moolenaardf177f62005-02-22 08:39:57 +00009135 vim_mkdir_emsg(p_vdir, 0755);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009136#endif
9137
9138 fd = open_exfile(fname, eap->forceit, WRITEBIN);
9139 if (fd != NULL)
9140 {
9141#ifdef FEAT_SESSION
9142 if (eap->cmdidx == CMD_mkview)
9143 flagp = &vop_flags;
9144 else
9145 flagp = &ssop_flags;
9146#endif
9147
9148#ifdef MKSESSION_NL
9149 /* "unix" in 'sessionoptions': use NL line separator */
9150 if (view_session && (*flagp & SSOP_UNIX))
9151 mksession_nl = TRUE;
9152#endif
9153
9154 /* Write the version command for :mkvimrc */
9155 if (eap->cmdidx == CMD_mkvimrc)
9156 (void)put_line(fd, "version 6.0");
9157
9158#ifdef FEAT_SESSION
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009159 if (eap->cmdidx == CMD_mksession)
9160 {
9161 if (put_line(fd, "let SessionLoad = 1") == FAIL)
9162 failed = TRUE;
9163 }
9164
Bram Moolenaar071d4272004-06-13 20:20:40 +00009165 if (eap->cmdidx != CMD_mkview)
9166#endif
9167 {
9168 /* Write setting 'compatible' first, because it has side effects.
9169 * For that same reason only do it when needed. */
9170 if (p_cp)
9171 (void)put_line(fd, "if !&cp | set cp | endif");
9172 else
9173 (void)put_line(fd, "if &cp | set nocp | endif");
9174 }
9175
9176#ifdef FEAT_SESSION
9177 if (!view_session
9178 || (eap->cmdidx == CMD_mksession
9179 && (*flagp & SSOP_OPTIONS)))
9180#endif
9181 failed |= (makemap(fd, NULL) == FAIL
9182 || makeset(fd, OPT_GLOBAL, FALSE) == FAIL);
9183
9184#ifdef FEAT_SESSION
9185 if (!failed && view_session)
9186 {
9187 if (put_line(fd, "let s:so_save = &so | let s:siso_save = &siso | set so=0 siso=0") == FAIL)
9188 failed = TRUE;
9189 if (eap->cmdidx == CMD_mksession)
9190 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02009191 char_u *dirnow; /* current directory */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009192
Bram Moolenaard9462e32011-04-11 21:35:11 +02009193 dirnow = alloc(MAXPATHL);
9194 if (dirnow == NULL)
9195 failed = TRUE;
9196 else
9197 {
9198 /*
9199 * Change to session file's dir.
9200 */
9201 if (mch_dirname(dirnow, MAXPATHL) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00009202 || mch_chdir((char *)dirnow) != 0)
Bram Moolenaard9462e32011-04-11 21:35:11 +02009203 *dirnow = NUL;
9204 if (*dirnow != NUL && (ssop_flags & SSOP_SESDIR))
9205 {
9206 if (vim_chdirfile(fname) == OK)
9207 shorten_fnames(TRUE);
9208 }
9209 else if (*dirnow != NUL
9210 && (ssop_flags & SSOP_CURDIR) && globaldir != NULL)
9211 {
9212 if (mch_chdir((char *)globaldir) == 0)
9213 shorten_fnames(TRUE);
9214 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009215
Bram Moolenaard9462e32011-04-11 21:35:11 +02009216 failed |= (makeopens(fd, dirnow) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009217
Bram Moolenaard9462e32011-04-11 21:35:11 +02009218 /* restore original dir */
9219 if (*dirnow != NUL && ((ssop_flags & SSOP_SESDIR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009220 || ((ssop_flags & SSOP_CURDIR) && globaldir != NULL)))
Bram Moolenaard9462e32011-04-11 21:35:11 +02009221 {
9222 if (mch_chdir((char *)dirnow) != 0)
9223 EMSG(_(e_prev_dir));
9224 shorten_fnames(TRUE);
9225 }
9226 vim_free(dirnow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009227 }
9228 }
9229 else
9230 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +00009231 failed |= (put_view(fd, curwin, !using_vdir, flagp,
9232 -1) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009233 }
9234 if (put_line(fd, "let &so = s:so_save | let &siso = s:siso_save")
9235 == FAIL)
9236 failed = TRUE;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009237 if (put_line(fd, "doautoall SessionLoadPost") == FAIL)
9238 failed = TRUE;
Bram Moolenaar4770d092006-01-12 23:22:24 +00009239 if (eap->cmdidx == CMD_mksession)
9240 {
9241 if (put_line(fd, "unlet SessionLoad") == FAIL)
9242 failed = TRUE;
9243 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009244 }
9245#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009246 if (put_line(fd, "\" vim: set ft=vim :") == FAIL)
9247 failed = TRUE;
9248
Bram Moolenaar071d4272004-06-13 20:20:40 +00009249 failed |= fclose(fd);
9250
9251 if (failed)
9252 EMSG(_(e_write));
9253#if defined(FEAT_EVAL) && defined(FEAT_SESSION)
9254 else if (eap->cmdidx == CMD_mksession)
9255 {
9256 /* successful session write - set this_session var */
Bram Moolenaard9462e32011-04-11 21:35:11 +02009257 char_u *tbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009258
Bram Moolenaard9462e32011-04-11 21:35:11 +02009259 tbuf = alloc(MAXPATHL);
9260 if (tbuf != NULL)
9261 {
9262 if (vim_FullName(fname, tbuf, MAXPATHL, FALSE) == OK)
9263 set_vim_var_string(VV_THIS_SESSION, tbuf, -1);
9264 vim_free(tbuf);
9265 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009266 }
9267#endif
9268#ifdef MKSESSION_NL
9269 mksession_nl = FALSE;
9270#endif
9271 }
9272
9273#ifdef FEAT_BROWSE
9274theend:
9275 vim_free(browseFile);
9276#endif
9277#ifdef FEAT_SESSION
9278 vim_free(viewFile);
9279#endif
9280}
9281
Bram Moolenaardf177f62005-02-22 08:39:57 +00009282#if ((defined(FEAT_SESSION) || defined(FEAT_EVAL)) && defined(vim_mkdir)) \
9283 || defined(PROTO)
9284 int
9285vim_mkdir_emsg(name, prot)
9286 char_u *name;
Bram Moolenaar78a15312009-05-15 19:33:18 +00009287 int prot UNUSED;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009288{
9289 if (vim_mkdir(name, prot) != 0)
9290 {
9291 EMSG2(_("E739: Cannot create directory: %s"), name);
9292 return FAIL;
9293 }
9294 return OK;
9295}
9296#endif
9297
Bram Moolenaar071d4272004-06-13 20:20:40 +00009298/*
9299 * Open a file for writing for an Ex command, with some checks.
9300 * Return file descriptor, or NULL on failure.
9301 */
9302 FILE *
9303open_exfile(fname, forceit, mode)
9304 char_u *fname;
9305 int forceit;
9306 char *mode; /* "w" for create new file or "a" for append */
9307{
9308 FILE *fd;
9309
9310#ifdef UNIX
9311 /* with Unix it is possible to open a directory */
9312 if (mch_isdir(fname))
9313 {
9314 EMSG2(_(e_isadir2), fname);
9315 return NULL;
9316 }
9317#endif
9318 if (!forceit && *mode != 'a' && vim_fexists(fname))
9319 {
9320 EMSG2(_("E189: \"%s\" exists (add ! to override)"), fname);
9321 return NULL;
9322 }
9323
9324 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
9325 EMSG2(_("E190: Cannot open \"%s\" for writing"), fname);
9326
9327 return fd;
9328}
9329
9330/*
9331 * ":mark" and ":k".
9332 */
9333 static void
9334ex_mark(eap)
9335 exarg_T *eap;
9336{
9337 pos_T pos;
9338
9339 if (*eap->arg == NUL) /* No argument? */
9340 EMSG(_(e_argreq));
9341 else if (eap->arg[1] != NUL) /* more than one character? */
9342 EMSG(_(e_trailing));
9343 else
9344 {
9345 pos = curwin->w_cursor; /* save curwin->w_cursor */
9346 curwin->w_cursor.lnum = eap->line2;
9347 beginline(BL_WHITE | BL_FIX);
9348 if (setmark(*eap->arg) == FAIL) /* set mark */
9349 EMSG(_("E191: Argument must be a letter or forward/backward quote"));
9350 curwin->w_cursor = pos; /* restore curwin->w_cursor */
9351 }
9352}
9353
9354/*
9355 * Update w_topline, w_leftcol and the cursor position.
9356 */
9357 void
9358update_topline_cursor()
9359{
9360 check_cursor(); /* put cursor on valid line */
9361 update_topline();
9362 if (!curwin->w_p_wrap)
9363 validate_cursor();
9364 update_curswant();
9365}
9366
9367#ifdef FEAT_EX_EXTRA
9368/*
9369 * ":normal[!] {commands}": Execute normal mode commands.
9370 */
9371 static void
9372ex_normal(eap)
9373 exarg_T *eap;
9374{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009375 int save_msg_scroll = msg_scroll;
9376 int save_restart_edit = restart_edit;
9377 int save_msg_didout = msg_didout;
9378 int save_State = State;
9379 tasave_T tabuf;
9380 int save_insertmode = p_im;
9381 int save_finish_op = finish_op;
Bram Moolenaar38084112008-07-26 14:05:07 +00009382 int save_opcount = opcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009383#ifdef FEAT_MBYTE
9384 char_u *arg = NULL;
9385 int l;
9386 char_u *p;
9387#endif
9388
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00009389 if (ex_normal_lock > 0)
9390 {
9391 EMSG(_(e_secure));
9392 return;
9393 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009394 if (ex_normal_busy >= p_mmd)
9395 {
9396 EMSG(_("E192: Recursive use of :normal too deep"));
9397 return;
9398 }
9399 ++ex_normal_busy;
9400
9401 msg_scroll = FALSE; /* no msg scrolling in Normal mode */
9402 restart_edit = 0; /* don't go to Insert mode */
9403 p_im = FALSE; /* don't use 'insertmode' */
9404
9405#ifdef FEAT_MBYTE
9406 /*
9407 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
9408 * this for the K_SPECIAL leading byte, otherwise special keys will not
9409 * work.
9410 */
9411 if (has_mbyte)
9412 {
9413 int len = 0;
9414
9415 /* Count the number of characters to be escaped. */
9416 for (p = eap->arg; *p != NUL; ++p)
9417 {
9418# ifdef FEAT_GUI
9419 if (*p == CSI) /* leadbyte CSI */
9420 len += 2;
9421# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009422 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009423 if (*++p == K_SPECIAL /* trailbyte K_SPECIAL or CSI */
9424# ifdef FEAT_GUI
9425 || *p == CSI
9426# endif
9427 )
9428 len += 2;
9429 }
9430 if (len > 0)
9431 {
9432 arg = alloc((unsigned)(STRLEN(eap->arg) + len + 1));
9433 if (arg != NULL)
9434 {
9435 len = 0;
9436 for (p = eap->arg; *p != NUL; ++p)
9437 {
9438 arg[len++] = *p;
9439# ifdef FEAT_GUI
9440 if (*p == CSI)
9441 {
9442 arg[len++] = KS_EXTRA;
9443 arg[len++] = (int)KE_CSI;
9444 }
9445# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009446 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009447 {
9448 arg[len++] = *++p;
9449 if (*p == K_SPECIAL)
9450 {
9451 arg[len++] = KS_SPECIAL;
9452 arg[len++] = KE_FILLER;
9453 }
9454# ifdef FEAT_GUI
9455 else if (*p == CSI)
9456 {
9457 arg[len++] = KS_EXTRA;
9458 arg[len++] = (int)KE_CSI;
9459 }
9460# endif
9461 }
9462 arg[len] = NUL;
9463 }
9464 }
9465 }
9466 }
9467#endif
9468
9469 /*
9470 * Save the current typeahead. This is required to allow using ":normal"
9471 * from an event handler and makes sure we don't hang when the argument
9472 * ends with half a command.
9473 */
9474 save_typeahead(&tabuf);
9475 if (tabuf.typebuf_valid)
9476 {
9477 /*
9478 * Repeat the :normal command for each line in the range. When no
9479 * range given, execute it just once, without positioning the cursor
9480 * first.
9481 */
9482 do
9483 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009484 if (eap->addr_count != 0)
9485 {
9486 curwin->w_cursor.lnum = eap->line1++;
9487 curwin->w_cursor.col = 0;
9488 }
9489
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009490 exec_normal_cmd(
Bram Moolenaar071d4272004-06-13 20:20:40 +00009491#ifdef FEAT_MBYTE
9492 arg != NULL ? arg :
9493#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009494 eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009495 }
9496 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
9497 }
9498
9499 /* Might not return to the main loop when in an event handler. */
9500 update_topline_cursor();
9501
9502 /* Restore the previous typeahead. */
9503 restore_typeahead(&tabuf);
9504
9505 --ex_normal_busy;
9506 msg_scroll = save_msg_scroll;
9507 restart_edit = save_restart_edit;
9508 p_im = save_insertmode;
9509 finish_op = save_finish_op;
Bram Moolenaar38084112008-07-26 14:05:07 +00009510 opcount = save_opcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009511 msg_didout |= save_msg_didout; /* don't reset msg_didout now */
9512
9513 /* Restore the state (needed when called from a function executed for
9514 * 'indentexpr'). */
9515 State = save_State;
9516#ifdef FEAT_MBYTE
9517 vim_free(arg);
9518#endif
9519}
9520
9521/*
Bram Moolenaar64969662005-12-14 21:59:55 +00009522 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009523 */
9524 static void
9525ex_startinsert(eap)
9526 exarg_T *eap;
9527{
Bram Moolenaarfd371682005-01-14 21:42:54 +00009528 if (eap->forceit)
9529 {
9530 coladvance((colnr_T)MAXCOL);
9531 curwin->w_curswant = MAXCOL;
9532 curwin->w_set_curswant = FALSE;
9533 }
9534
Bram Moolenaara40c5002005-01-09 21:16:21 +00009535 /* Ignore the command when already in Insert mode. Inserting an
9536 * expression register that invokes a function can do this. */
9537 if (State & INSERT)
9538 return;
9539
Bram Moolenaar64969662005-12-14 21:59:55 +00009540 if (eap->cmdidx == CMD_startinsert)
9541 restart_edit = 'a';
9542 else if (eap->cmdidx == CMD_startreplace)
9543 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00009544 else
Bram Moolenaar64969662005-12-14 21:59:55 +00009545 restart_edit = 'V';
9546
9547 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009548 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009549 if (eap->cmdidx == CMD_startinsert)
9550 restart_edit = 'i';
Bram Moolenaar071d4272004-06-13 20:20:40 +00009551 curwin->w_curswant = 0; /* avoid MAXCOL */
9552 }
9553}
9554
9555/*
9556 * ":stopinsert"
9557 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009558 static void
9559ex_stopinsert(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009560 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009561{
9562 restart_edit = 0;
9563 stop_insert_mode = TRUE;
9564}
9565#endif
9566
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009567#if defined(FEAT_EX_EXTRA) || defined(FEAT_MENU) || defined(PROTO)
9568/*
9569 * Execute normal mode command "cmd".
9570 * "remap" can be REMAP_NONE or REMAP_YES.
9571 */
9572 void
9573exec_normal_cmd(cmd, remap, silent)
9574 char_u *cmd;
9575 int remap;
9576 int silent;
9577{
9578 oparg_T oa;
9579
9580 /*
9581 * Stuff the argument into the typeahead buffer.
9582 * Execute normal_cmd() until there is no typeahead left.
9583 */
9584 clear_oparg(&oa);
9585 finish_op = FALSE;
9586 ins_typebuf(cmd, remap, 0, TRUE, silent);
9587 while ((!stuff_empty() || (!typebuf_typed() && typebuf.tb_len > 0))
9588 && !got_int)
9589 {
9590 update_topline_cursor();
Bram Moolenaar48ac02c2011-01-17 19:50:06 +01009591 normal_cmd(&oa, TRUE); /* execute a Normal mode cmd */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009592 }
9593}
9594#endif
9595
Bram Moolenaar071d4272004-06-13 20:20:40 +00009596#ifdef FEAT_FIND_ID
9597 static void
9598ex_checkpath(eap)
9599 exarg_T *eap;
9600{
9601 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
9602 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
9603 (linenr_T)1, (linenr_T)MAXLNUM);
9604}
9605
9606#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
9607/*
9608 * ":psearch"
9609 */
9610 static void
9611ex_psearch(eap)
9612 exarg_T *eap;
9613{
9614 g_do_tagpreview = p_pvh;
9615 ex_findpat(eap);
9616 g_do_tagpreview = 0;
9617}
9618#endif
9619
9620 static void
9621ex_findpat(eap)
9622 exarg_T *eap;
9623{
9624 int whole = TRUE;
9625 long n;
9626 char_u *p;
9627 int action;
9628
9629 switch (cmdnames[eap->cmdidx].cmd_name[2])
9630 {
9631 case 'e': /* ":psearch", ":isearch" and ":dsearch" */
9632 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
9633 action = ACTION_GOTO;
9634 else
9635 action = ACTION_SHOW;
9636 break;
9637 case 'i': /* ":ilist" and ":dlist" */
9638 action = ACTION_SHOW_ALL;
9639 break;
9640 case 'u': /* ":ijump" and ":djump" */
9641 action = ACTION_GOTO;
9642 break;
9643 default: /* ":isplit" and ":dsplit" */
9644 action = ACTION_SPLIT;
9645 break;
9646 }
9647
9648 n = 1;
9649 if (vim_isdigit(*eap->arg)) /* get count */
9650 {
9651 n = getdigits(&eap->arg);
9652 eap->arg = skipwhite(eap->arg);
9653 }
9654 if (*eap->arg == '/') /* Match regexp, not just whole words */
9655 {
9656 whole = FALSE;
9657 ++eap->arg;
9658 p = skip_regexp(eap->arg, '/', p_magic, NULL);
9659 if (*p)
9660 {
9661 *p++ = NUL;
9662 p = skipwhite(p);
9663
9664 /* Check for trailing illegal characters */
9665 if (!ends_excmd(*p))
9666 eap->errmsg = e_trailing;
9667 else
9668 eap->nextcmd = check_nextcmd(p);
9669 }
9670 }
9671 if (!eap->skip)
9672 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
9673 whole, !eap->forceit,
9674 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
9675 n, action, eap->line1, eap->line2);
9676}
9677#endif
9678
9679#ifdef FEAT_WINDOWS
9680
9681# ifdef FEAT_QUICKFIX
9682/*
9683 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
9684 */
9685 static void
9686ex_ptag(eap)
9687 exarg_T *eap;
9688{
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01009689 g_do_tagpreview = p_pvh; /* will be reset to 0 in ex_tag_cmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009690 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9691}
9692
9693/*
9694 * ":pedit"
9695 */
9696 static void
9697ex_pedit(eap)
9698 exarg_T *eap;
9699{
9700 win_T *curwin_save = curwin;
9701
9702 g_do_tagpreview = p_pvh;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00009703 prepare_tagpreview(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009704 keep_help_flag = curwin_save->w_buffer->b_help;
9705 do_exedit(eap, NULL);
9706 keep_help_flag = FALSE;
9707 if (curwin != curwin_save && win_valid(curwin_save))
9708 {
9709 /* Return cursor to where we were */
9710 validate_cursor();
9711 redraw_later(VALID);
9712 win_enter(curwin_save, TRUE);
9713 }
9714 g_do_tagpreview = 0;
9715}
9716# endif
9717
9718/*
9719 * ":stag", ":stselect" and ":stjump".
9720 */
9721 static void
9722ex_stag(eap)
9723 exarg_T *eap;
9724{
9725 postponed_split = -1;
9726 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009727 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009728 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9729 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009730 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009731}
9732#endif
9733
9734/*
9735 * ":tag", ":tselect", ":tjump", ":tnext", etc.
9736 */
9737 static void
9738ex_tag(eap)
9739 exarg_T *eap;
9740{
9741 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
9742}
9743
9744 static void
9745ex_tag_cmd(eap, name)
9746 exarg_T *eap;
9747 char_u *name;
9748{
9749 int cmd;
9750
9751 switch (name[1])
9752 {
9753 case 'j': cmd = DT_JUMP; /* ":tjump" */
9754 break;
9755 case 's': cmd = DT_SELECT; /* ":tselect" */
9756 break;
9757 case 'p': cmd = DT_PREV; /* ":tprevious" */
9758 break;
9759 case 'N': cmd = DT_PREV; /* ":tNext" */
9760 break;
9761 case 'n': cmd = DT_NEXT; /* ":tnext" */
9762 break;
9763 case 'o': cmd = DT_POP; /* ":pop" */
9764 break;
9765 case 'f': /* ":tfirst" */
9766 case 'r': cmd = DT_FIRST; /* ":trewind" */
9767 break;
9768 case 'l': cmd = DT_LAST; /* ":tlast" */
9769 break;
9770 default: /* ":tag" */
9771#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +00009772 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009773 {
9774 do_cstag(eap);
9775 return;
9776 }
9777#endif
9778 cmd = DT_TAG;
9779 break;
9780 }
9781
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00009782 if (name[0] == 'l')
9783 {
9784#ifndef FEAT_QUICKFIX
9785 ex_ni(eap);
9786 return;
9787#else
9788 cmd = DT_LTAG;
9789#endif
9790 }
9791
Bram Moolenaar071d4272004-06-13 20:20:40 +00009792 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
9793 eap->forceit, TRUE);
9794}
9795
9796/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009797 * Check "str" for starting with a special cmdline variable.
9798 * If found return one of the SPEC_ values and set "*usedlen" to the length of
9799 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
9800 */
9801 int
9802find_cmdline_var(src, usedlen)
9803 char_u *src;
9804 int *usedlen;
9805{
9806 int len;
9807 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +00009808 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009809 "%",
9810#define SPEC_PERC 0
9811 "#",
9812#define SPEC_HASH 1
9813 "<cword>", /* cursor word */
9814#define SPEC_CWORD 2
9815 "<cWORD>", /* cursor WORD */
9816#define SPEC_CCWORD 3
9817 "<cfile>", /* cursor path name */
9818#define SPEC_CFILE 4
9819 "<sfile>", /* ":so" file name */
9820#define SPEC_SFILE 5
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009821 "<slnum>", /* ":so" file line number */
9822#define SPEC_SLNUM 6
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009823#ifdef FEAT_AUTOCMD
9824 "<afile>", /* autocommand file name */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009825# define SPEC_AFILE 7
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009826 "<abuf>", /* autocommand buffer number */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009827# define SPEC_ABUF 8
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009828 "<amatch>", /* autocommand match name */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009829# define SPEC_AMATCH 9
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009830#endif
9831#ifdef FEAT_CLIENTSERVER
9832 "<client>"
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009833# ifdef FEAT_AUTOCMD
9834# define SPEC_CLIENT 10
9835# else
9836# define SPEC_CLIENT 7
9837# endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009838#endif
9839 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009840
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009841 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009842 {
9843 len = (int)STRLEN(spec_str[i]);
9844 if (STRNCMP(src, spec_str[i], len) == 0)
9845 {
9846 *usedlen = len;
9847 return i;
9848 }
9849 }
9850 return -1;
9851}
9852
9853/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009854 * Evaluate cmdline variables.
9855 *
9856 * change '%' to curbuf->b_ffname
9857 * '#' to curwin->w_altfile
9858 * '<cword>' to word under the cursor
9859 * '<cWORD>' to WORD under the cursor
9860 * '<cfile>' to path name under the cursor
9861 * '<sfile>' to sourced file name
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009862 * '<slnum>' to sourced file line number
Bram Moolenaar071d4272004-06-13 20:20:40 +00009863 * '<afile>' to file name for autocommand
9864 * '<abuf>' to buffer number for autocommand
9865 * '<amatch>' to matching name for autocommand
9866 *
9867 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
9868 * "" for error without a message) and NULL is returned.
9869 * Returns an allocated string if a valid match was found.
9870 * Returns NULL if no match was found. "usedlen" then still contains the
9871 * number of characters to skip.
9872 */
9873 char_u *
Bram Moolenaar63b92542007-03-27 14:57:09 +00009874eval_vars(src, srcstart, usedlen, lnump, errormsg, escaped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009875 char_u *src; /* pointer into commandline */
Bram Moolenaar63b92542007-03-27 14:57:09 +00009876 char_u *srcstart; /* beginning of valid memory for src */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009877 int *usedlen; /* characters after src that are used */
9878 linenr_T *lnump; /* line number for :e command, or NULL */
9879 char_u **errormsg; /* pointer to error message */
Bram Moolenaar63b92542007-03-27 14:57:09 +00009880 int *escaped; /* return value has escaped white space (can
9881 * be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009882{
9883 int i;
9884 char_u *s;
9885 char_u *result;
9886 char_u *resultbuf = NULL;
9887 int resultlen;
9888 buf_T *buf;
9889 int valid = VALID_HEAD + VALID_PATH; /* assume valid result */
9890 int spec_idx;
9891#ifdef FEAT_MODIFY_FNAME
9892 int skip_mod = FALSE;
9893#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009894 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009895
9896 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009897 if (escaped != NULL)
9898 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009899
9900 /*
9901 * Check if there is something to do.
9902 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009903 spec_idx = find_cmdline_var(src, usedlen);
9904 if (spec_idx < 0) /* no match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009905 {
9906 *usedlen = 1;
9907 return NULL;
9908 }
9909
9910 /*
9911 * Skip when preceded with a backslash "\%" and "\#".
9912 * Note: In "\\%" the % is also not recognized!
9913 */
9914 if (src > srcstart && src[-1] == '\\')
9915 {
9916 *usedlen = 0;
Bram Moolenaara7241f52008-06-24 20:39:31 +00009917 STRMOVE(src - 1, src); /* remove backslash */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009918 return NULL;
9919 }
9920
9921 /*
9922 * word or WORD under cursor
9923 */
9924 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD)
9925 {
9926 resultlen = find_ident_under_cursor(&result, spec_idx == SPEC_CWORD ?
9927 (FIND_IDENT|FIND_STRING) : FIND_STRING);
9928 if (resultlen == 0)
9929 {
9930 *errormsg = (char_u *)"";
9931 return NULL;
9932 }
9933 }
9934
9935 /*
9936 * '#': Alternate file name
9937 * '%': Current file name
9938 * File name under the cursor
9939 * File name for autocommand
9940 * and following modifiers
9941 */
9942 else
9943 {
9944 switch (spec_idx)
9945 {
9946 case SPEC_PERC: /* '%': current file */
9947 if (curbuf->b_fname == NULL)
9948 {
9949 result = (char_u *)"";
9950 valid = 0; /* Must have ":p:h" to be valid */
9951 }
9952 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009953 result = curbuf->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009954 break;
9955
9956 case SPEC_HASH: /* '#' or "#99": alternate file */
9957 if (src[1] == '#') /* "##": the argument list */
9958 {
9959 result = arg_all();
9960 resultbuf = result;
9961 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009962 if (escaped != NULL)
9963 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009964#ifdef FEAT_MODIFY_FNAME
9965 skip_mod = TRUE;
9966#endif
9967 break;
9968 }
9969 s = src + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00009970 if (*s == '<') /* "#<99" uses v:oldfiles */
9971 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009972 i = (int)getdigits(&s);
9973 *usedlen = (int)(s - src); /* length of what we expand */
9974
Bram Moolenaard812df62008-11-09 12:46:09 +00009975 if (src[1] == '<')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009976 {
Bram Moolenaard812df62008-11-09 12:46:09 +00009977 if (*usedlen < 2)
9978 {
9979 /* Should we give an error message for #<text? */
9980 *usedlen = 1;
9981 return NULL;
9982 }
9983#ifdef FEAT_EVAL
9984 result = list_find_str(get_vim_var_list(VV_OLDFILES),
9985 (long)i);
9986 if (result == NULL)
9987 {
9988 *errormsg = (char_u *)"";
9989 return NULL;
9990 }
9991#else
9992 *errormsg = (char_u *)_("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009993 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00009994#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009995 }
9996 else
Bram Moolenaard812df62008-11-09 12:46:09 +00009997 {
9998 buf = buflist_findnr(i);
9999 if (buf == NULL)
10000 {
10001 *errormsg = (char_u *)_("E194: No alternate file name to substitute for '#'");
10002 return NULL;
10003 }
10004 if (lnump != NULL)
10005 *lnump = ECMD_LAST;
10006 if (buf->b_fname == NULL)
10007 {
10008 result = (char_u *)"";
10009 valid = 0; /* Must have ":p:h" to be valid */
10010 }
10011 else
10012 result = buf->b_fname;
10013 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010014 break;
10015
10016#ifdef FEAT_SEARCHPATH
10017 case SPEC_CFILE: /* file name under cursor */
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010018 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010019 if (result == NULL)
10020 {
10021 *errormsg = (char_u *)"";
10022 return NULL;
10023 }
10024 resultbuf = result; /* remember allocated string */
10025 break;
10026#endif
10027
10028#ifdef FEAT_AUTOCMD
10029 case SPEC_AFILE: /* file name for autocommand */
10030 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +000010031 if (result != NULL && !autocmd_fname_full)
10032 {
10033 /* Still need to turn the fname into a full path. It is
10034 * postponed to avoid a delay when <afile> is not used. */
10035 autocmd_fname_full = TRUE;
10036 result = FullName_save(autocmd_fname, FALSE);
10037 vim_free(autocmd_fname);
10038 autocmd_fname = result;
10039 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010040 if (result == NULL)
10041 {
10042 *errormsg = (char_u *)_("E495: no autocommand file name to substitute for \"<afile>\"");
10043 return NULL;
10044 }
Bram Moolenaara0174af2008-01-02 20:08:25 +000010045 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010046 break;
10047
10048 case SPEC_ABUF: /* buffer number for autocommand */
10049 if (autocmd_bufnr <= 0)
10050 {
10051 *errormsg = (char_u *)_("E496: no autocommand buffer number to substitute for \"<abuf>\"");
10052 return NULL;
10053 }
10054 sprintf((char *)strbuf, "%d", autocmd_bufnr);
10055 result = strbuf;
10056 break;
10057
10058 case SPEC_AMATCH: /* match name for autocommand */
10059 result = autocmd_match;
10060 if (result == NULL)
10061 {
10062 *errormsg = (char_u *)_("E497: no autocommand match name to substitute for \"<amatch>\"");
10063 return NULL;
10064 }
10065 break;
10066
10067#endif
10068 case SPEC_SFILE: /* file name for ":so" command */
10069 result = sourcing_name;
10070 if (result == NULL)
10071 {
10072 *errormsg = (char_u *)_("E498: no :source file name to substitute for \"<sfile>\"");
10073 return NULL;
10074 }
10075 break;
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010076 case SPEC_SLNUM: /* line in file for ":so" command */
10077 if (sourcing_name == NULL || sourcing_lnum == 0)
10078 {
10079 *errormsg = (char_u *)_("E842: no line number to use for \"<slnum>\"");
10080 return NULL;
10081 }
10082 sprintf((char *)strbuf, "%ld", (long)sourcing_lnum);
10083 result = strbuf;
10084 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010085#if defined(FEAT_CLIENTSERVER)
10086 case SPEC_CLIENT: /* Source of last submitted input */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010087 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
10088 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010089 result = strbuf;
10090 break;
10091#endif
10092 }
10093
10094 resultlen = (int)STRLEN(result); /* length of new string */
10095 if (src[*usedlen] == '<') /* remove the file name extension */
10096 {
10097 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010098 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010099 resultlen = (int)(s - result);
10100 }
10101#ifdef FEAT_MODIFY_FNAME
10102 else if (!skip_mod)
10103 {
10104 valid |= modify_fname(src, usedlen, &result, &resultbuf,
10105 &resultlen);
10106 if (result == NULL)
10107 {
10108 *errormsg = (char_u *)"";
10109 return NULL;
10110 }
10111 }
10112#endif
10113 }
10114
10115 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
10116 {
10117 if (valid != VALID_HEAD + VALID_PATH)
10118 /* xgettext:no-c-format */
10119 *errormsg = (char_u *)_("E499: Empty file name for '%' or '#', only works with \":p:h\"");
10120 else
10121 *errormsg = (char_u *)_("E500: Evaluates to an empty string");
10122 result = NULL;
10123 }
10124 else
10125 result = vim_strnsave(result, resultlen);
10126 vim_free(resultbuf);
10127 return result;
10128}
10129
10130/*
10131 * Concatenate all files in the argument list, separated by spaces, and return
10132 * it in one allocated string.
10133 * Spaces and backslashes in the file names are escaped with a backslash.
10134 * Returns NULL when out of memory.
10135 */
10136 static char_u *
10137arg_all()
10138{
10139 int len;
10140 int idx;
10141 char_u *retval = NULL;
10142 char_u *p;
10143
10144 /*
10145 * Do this loop two times:
10146 * first time: compute the total length
10147 * second time: concatenate the names
10148 */
10149 for (;;)
10150 {
10151 len = 0;
10152 for (idx = 0; idx < ARGCOUNT; ++idx)
10153 {
10154 p = alist_name(&ARGLIST[idx]);
10155 if (p != NULL)
10156 {
10157 if (len > 0)
10158 {
10159 /* insert a space in between names */
10160 if (retval != NULL)
10161 retval[len] = ' ';
10162 ++len;
10163 }
10164 for ( ; *p != NUL; ++p)
10165 {
10166 if (*p == ' ' || *p == '\\')
10167 {
10168 /* insert a backslash */
10169 if (retval != NULL)
10170 retval[len] = '\\';
10171 ++len;
10172 }
10173 if (retval != NULL)
10174 retval[len] = *p;
10175 ++len;
10176 }
10177 }
10178 }
10179
10180 /* second time: break here */
10181 if (retval != NULL)
10182 {
10183 retval[len] = NUL;
10184 break;
10185 }
10186
10187 /* allocate memory */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000010188 retval = alloc((unsigned)len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010189 if (retval == NULL)
10190 break;
10191 }
10192
10193 return retval;
10194}
10195
10196#if defined(FEAT_AUTOCMD) || defined(PROTO)
10197/*
10198 * Expand the <sfile> string in "arg".
10199 *
10200 * Returns an allocated string, or NULL for any error.
10201 */
10202 char_u *
10203expand_sfile(arg)
10204 char_u *arg;
10205{
10206 char_u *errormsg;
10207 int len;
10208 char_u *result;
10209 char_u *newres;
10210 char_u *repl;
10211 int srclen;
10212 char_u *p;
10213
10214 result = vim_strsave(arg);
10215 if (result == NULL)
10216 return NULL;
10217
10218 for (p = result; *p; )
10219 {
10220 if (STRNCMP(p, "<sfile>", 7) != 0)
10221 ++p;
10222 else
10223 {
10224 /* replace "<sfile>" with the sourced file name, and do ":" stuff */
Bram Moolenaar63b92542007-03-27 14:57:09 +000010225 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010226 if (errormsg != NULL)
10227 {
10228 if (*errormsg)
10229 emsg(errormsg);
10230 vim_free(result);
10231 return NULL;
10232 }
10233 if (repl == NULL) /* no match (cannot happen) */
10234 {
10235 p += srclen;
10236 continue;
10237 }
10238 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
10239 newres = alloc(len);
10240 if (newres == NULL)
10241 {
10242 vim_free(repl);
10243 vim_free(result);
10244 return NULL;
10245 }
10246 mch_memmove(newres, result, (size_t)(p - result));
10247 STRCPY(newres + (p - result), repl);
10248 len = (int)STRLEN(newres);
10249 STRCAT(newres, p + srclen);
10250 vim_free(repl);
10251 vim_free(result);
10252 result = newres;
10253 p = newres + len; /* continue after the match */
10254 }
10255 }
10256
10257 return result;
10258}
10259#endif
10260
10261#ifdef FEAT_SESSION
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010262static int ses_winsizes __ARGS((FILE *fd, int restore_size,
10263 win_T *tab_firstwin));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010264static int ses_win_rec __ARGS((FILE *fd, frame_T *fr));
10265static frame_T *ses_skipframe __ARGS((frame_T *fr));
10266static int ses_do_frame __ARGS((frame_T *fr));
10267static int ses_do_win __ARGS((win_T *wp));
10268static int ses_arglist __ARGS((FILE *fd, char *cmd, garray_T *gap, int fullname, unsigned *flagp));
10269static int ses_put_fname __ARGS((FILE *fd, char_u *name, unsigned *flagp));
10270static int ses_fname __ARGS((FILE *fd, buf_T *buf, unsigned *flagp));
10271
10272/*
10273 * Write openfile commands for the current buffers to an .exrc file.
10274 * Return FAIL on error, OK otherwise.
10275 */
10276 static int
10277makeopens(fd, dirnow)
10278 FILE *fd;
10279 char_u *dirnow; /* Current directory name */
10280{
10281 buf_T *buf;
10282 int only_save_windows = TRUE;
10283 int nr;
10284 int cnr = 1;
10285 int restore_size = TRUE;
10286 win_T *wp;
10287 char_u *sname;
10288 win_T *edited_win = NULL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010289 int tabnr;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010290 win_T *tab_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010291 frame_T *tab_topframe;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010292 int cur_arg_idx = 0;
Bram Moolenaar383c6f52008-01-04 15:01:07 +000010293 int next_arg_idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010294
10295 if (ssop_flags & SSOP_BUFFERS)
10296 only_save_windows = FALSE; /* Save ALL buffers */
10297
10298 /*
10299 * Begin by setting the this_session variable, and then other
10300 * sessionable variables.
10301 */
10302#ifdef FEAT_EVAL
10303 if (put_line(fd, "let v:this_session=expand(\"<sfile>:p\")") == FAIL)
10304 return FAIL;
10305 if (ssop_flags & SSOP_GLOBALS)
10306 if (store_session_globals(fd) == FAIL)
10307 return FAIL;
10308#endif
10309
10310 /*
10311 * Close all windows but one.
10312 */
10313 if (put_line(fd, "silent only") == FAIL)
10314 return FAIL;
10315
10316 /*
10317 * Now a :cd command to the session directory or the current directory
10318 */
10319 if (ssop_flags & SSOP_SESDIR)
10320 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010321 if (put_line(fd, "exe \"cd \" . escape(expand(\"<sfile>:p:h\"), ' ')")
10322 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010323 return FAIL;
10324 }
10325 else if (ssop_flags & SSOP_CURDIR)
10326 {
10327 sname = home_replace_save(NULL, globaldir != NULL ? globaldir : dirnow);
10328 if (sname == NULL
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010329 || fputs("cd ", fd) < 0
10330 || ses_put_fname(fd, sname, &ssop_flags) == FAIL
10331 || put_eol(fd) == FAIL)
10332 {
10333 vim_free(sname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010334 return FAIL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010335 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010336 vim_free(sname);
10337 }
10338
10339 /*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010340 * If there is an empty, unnamed buffer we will wipe it out later.
10341 * Remember the buffer number.
10342 */
10343 if (put_line(fd, "if expand('%') == '' && !&modified && line('$') <= 1 && getline(1) == ''") == FAIL)
10344 return FAIL;
10345 if (put_line(fd, " let s:wipebuf = bufnr('%')") == FAIL)
10346 return FAIL;
10347 if (put_line(fd, "endif") == FAIL)
10348 return FAIL;
10349
10350 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010351 * Now save the current files, current buffer first.
10352 */
10353 if (put_line(fd, "set shortmess=aoO") == FAIL)
10354 return FAIL;
10355
10356 /* Now put the other buffers into the buffer list */
10357 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
10358 {
10359 if (!(only_save_windows && buf->b_nwindows == 0)
10360 && !(buf->b_help && !(ssop_flags & SSOP_HELP))
10361 && buf->b_fname != NULL
10362 && buf->b_p_bl)
10363 {
10364 if (fprintf(fd, "badd +%ld ", buf->b_wininfo == NULL ? 1L
10365 : buf->b_wininfo->wi_fpos.lnum) < 0
10366 || ses_fname(fd, buf, &ssop_flags) == FAIL)
10367 return FAIL;
10368 }
10369 }
10370
10371 /* the global argument list */
10372 if (ses_arglist(fd, "args", &global_alist.al_ga,
10373 !(ssop_flags & SSOP_CURDIR), &ssop_flags) == FAIL)
10374 return FAIL;
10375
10376 if (ssop_flags & SSOP_RESIZE)
10377 {
10378 /* Note: after the restore we still check it worked!*/
10379 if (fprintf(fd, "set lines=%ld columns=%ld" , Rows, Columns) < 0
10380 || put_eol(fd) == FAIL)
10381 return FAIL;
10382 }
10383
10384#ifdef FEAT_GUI
10385 if (gui.in_use && (ssop_flags & SSOP_WINPOS))
10386 {
10387 int x, y;
10388
10389 if (gui_mch_get_winpos(&x, &y) == OK)
10390 {
10391 /* Note: after the restore we still check it worked!*/
10392 if (fprintf(fd, "winpos %d %d", x, y) < 0 || put_eol(fd) == FAIL)
10393 return FAIL;
10394 }
10395 }
10396#endif
10397
10398 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000010399 * May repeat putting Windows for each tab, when "tabpages" is in
10400 * 'sessionoptions'.
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010401 * Don't use goto_tabpage(), it may change directory and trigger
10402 * autocommands.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010403 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010404 tab_firstwin = firstwin; /* first window in tab page "tabnr" */
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010405 tab_topframe = topframe;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010406 for (tabnr = 1; ; ++tabnr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010407 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010408 int need_tabnew = FALSE;
10409
Bram Moolenaar18144c82006-04-12 21:52:12 +000010410 if ((ssop_flags & SSOP_TABPAGES))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010411 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010412 tabpage_T *tp = find_tabpage(tabnr);
10413
10414 if (tp == NULL)
10415 break; /* done all tab pages */
10416 if (tp == curtab)
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010417 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010418 tab_firstwin = firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010419 tab_topframe = topframe;
10420 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010421 else
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010422 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010423 tab_firstwin = tp->tp_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010424 tab_topframe = tp->tp_topframe;
10425 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010426 if (tabnr > 1)
10427 need_tabnew = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010428 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010429
Bram Moolenaar18144c82006-04-12 21:52:12 +000010430 /*
10431 * Before creating the window layout, try loading one file. If this
10432 * is aborted we don't end up with a number of useless windows.
10433 * This may have side effects! (e.g., compressed or network file).
10434 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010435 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010436 {
10437 if (ses_do_win(wp)
10438 && wp->w_buffer->b_ffname != NULL
10439 && !wp->w_buffer->b_help
10440#ifdef FEAT_QUICKFIX
10441 && !bt_nofile(wp->w_buffer)
10442#endif
10443 )
10444 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010445 if (fputs(need_tabnew ? "tabedit " : "edit ", fd) < 0
Bram Moolenaar18144c82006-04-12 21:52:12 +000010446 || ses_fname(fd, wp->w_buffer, &ssop_flags) == FAIL)
10447 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010448 need_tabnew = FALSE;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010449 if (!wp->w_arg_idx_invalid)
10450 edited_win = wp;
10451 break;
10452 }
10453 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010454
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010455 /* If no file got edited create an empty tab page. */
10456 if (need_tabnew && put_line(fd, "tabnew") == FAIL)
10457 return FAIL;
10458
Bram Moolenaar18144c82006-04-12 21:52:12 +000010459 /*
10460 * Save current window layout.
10461 */
10462 if (put_line(fd, "set splitbelow splitright") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010463 return FAIL;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010464 if (ses_win_rec(fd, tab_topframe) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010465 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010466 if (!p_sb && put_line(fd, "set nosplitbelow") == FAIL)
10467 return FAIL;
10468 if (!p_spr && put_line(fd, "set nosplitright") == FAIL)
10469 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010470
Bram Moolenaar18144c82006-04-12 21:52:12 +000010471 /*
10472 * Check if window sizes can be restored (no windows omitted).
10473 * Remember the window number of the current window after restoring.
10474 */
10475 nr = 0;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010476 for (wp = tab_firstwin; wp != NULL; wp = W_NEXT(wp))
Bram Moolenaar18144c82006-04-12 21:52:12 +000010477 {
10478 if (ses_do_win(wp))
10479 ++nr;
10480 else
10481 restore_size = FALSE;
10482 if (curwin == wp)
10483 cnr = nr;
10484 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010485
Bram Moolenaar18144c82006-04-12 21:52:12 +000010486 /* Go to the first window. */
10487 if (put_line(fd, "wincmd t") == FAIL)
10488 return FAIL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010489
Bram Moolenaar18144c82006-04-12 21:52:12 +000010490 /*
10491 * If more than one window, see if sizes can be restored.
10492 * First set 'winheight' and 'winwidth' to 1 to avoid the windows being
10493 * resized when moving between windows.
10494 * Do this before restoring the view, so that the topline and the
10495 * cursor can be set. This is done again below.
10496 */
10497 if (put_line(fd, "set winheight=1 winwidth=1") == FAIL)
10498 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010499 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010500 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010501
Bram Moolenaar18144c82006-04-12 21:52:12 +000010502 /*
10503 * Restore the view of the window (options, file, cursor, etc.).
10504 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010505 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010506 {
10507 if (!ses_do_win(wp))
10508 continue;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010509 if (put_view(fd, wp, wp != edited_win, &ssop_flags,
10510 cur_arg_idx) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010511 return FAIL;
10512 if (nr > 1 && put_line(fd, "wincmd w") == FAIL)
10513 return FAIL;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010514 next_arg_idx = wp->w_arg_idx;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010515 }
10516
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010517 /* The argument index in the first tab page is zero, need to set it in
10518 * each window. For further tab pages it's the window where we do
10519 * "tabedit". */
10520 cur_arg_idx = next_arg_idx;
10521
Bram Moolenaar18144c82006-04-12 21:52:12 +000010522 /*
10523 * Restore cursor to the current window if it's not the first one.
10524 */
10525 if (cnr > 1 && (fprintf(fd, "%dwincmd w", cnr) < 0
10526 || put_eol(fd) == FAIL))
10527 return FAIL;
10528
10529 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000010530 * Restore window sizes again after jumping around in windows, because
10531 * the current window has a minimum size while others may not.
10532 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010533 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010534 return FAIL;
10535
Bram Moolenaar18144c82006-04-12 21:52:12 +000010536 /* Don't continue in another tab page when doing only the current one
10537 * or when at the last tab page. */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010538 if (!(ssop_flags & SSOP_TABPAGES))
Bram Moolenaar18144c82006-04-12 21:52:12 +000010539 break;
10540 }
10541
10542 if (ssop_flags & SSOP_TABPAGES)
10543 {
Bram Moolenaar18144c82006-04-12 21:52:12 +000010544 if (fprintf(fd, "tabnext %d", tabpage_index(curtab)) < 0
10545 || put_eol(fd) == FAIL)
10546 return FAIL;
10547 }
10548
Bram Moolenaar9c102382006-05-03 21:26:49 +000010549 /*
10550 * Wipe out an empty unnamed buffer we started in.
10551 */
10552 if (put_line(fd, "if exists('s:wipebuf')") == FAIL)
10553 return FAIL;
Bram Moolenaarf3a67882006-05-05 21:09:41 +000010554 if (put_line(fd, " silent exe 'bwipe ' . s:wipebuf") == FAIL)
Bram Moolenaar9c102382006-05-03 21:26:49 +000010555 return FAIL;
10556 if (put_line(fd, "endif") == FAIL)
10557 return FAIL;
10558 if (put_line(fd, "unlet! s:wipebuf") == FAIL)
10559 return FAIL;
10560
10561 /* Re-apply 'winheight', 'winwidth' and 'shortmess'. */
10562 if (fprintf(fd, "set winheight=%ld winwidth=%ld shortmess=%s",
10563 p_wh, p_wiw, p_shm) < 0 || put_eol(fd) == FAIL)
10564 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010565
10566 /*
10567 * Lastly, execute the x.vim file if it exists.
10568 */
10569 if (put_line(fd, "let s:sx = expand(\"<sfile>:p:r\").\"x.vim\"") == FAIL
10570 || put_line(fd, "if file_readable(s:sx)") == FAIL
Bram Moolenaar42ba1262008-12-09 10:18:03 +000010571 || put_line(fd, " exe \"source \" . fnameescape(s:sx)") == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000010572 || put_line(fd, "endif") == FAIL)
10573 return FAIL;
10574
10575 return OK;
10576}
10577
10578 static int
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010579ses_winsizes(fd, restore_size, tab_firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010580 FILE *fd;
10581 int restore_size;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010582 win_T *tab_firstwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010583{
10584 int n = 0;
10585 win_T *wp;
10586
10587 if (restore_size && (ssop_flags & SSOP_WINSIZE))
10588 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010589 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010590 {
10591 if (!ses_do_win(wp))
10592 continue;
10593 ++n;
10594
10595 /* restore height when not full height */
10596 if (wp->w_height + wp->w_status_height < topframe->fr_height
10597 && (fprintf(fd,
10598 "exe '%dresize ' . ((&lines * %ld + %ld) / %ld)",
10599 n, (long)wp->w_height, Rows / 2, Rows) < 0
10600 || put_eol(fd) == FAIL))
10601 return FAIL;
10602
10603 /* restore width when not full width */
10604 if (wp->w_width < Columns && (fprintf(fd,
10605 "exe 'vert %dresize ' . ((&columns * %ld + %ld) / %ld)",
10606 n, (long)wp->w_width, Columns / 2, Columns) < 0
10607 || put_eol(fd) == FAIL))
10608 return FAIL;
10609 }
10610 }
10611 else
10612 {
10613 /* Just equalise window sizes */
10614 if (put_line(fd, "wincmd =") == FAIL)
10615 return FAIL;
10616 }
10617 return OK;
10618}
10619
10620/*
10621 * Write commands to "fd" to recursively create windows for frame "fr",
10622 * horizontally and vertically split.
10623 * After the commands the last window in the frame is the current window.
10624 * Returns FAIL when writing the commands to "fd" fails.
10625 */
10626 static int
10627ses_win_rec(fd, fr)
10628 FILE *fd;
10629 frame_T *fr;
10630{
10631 frame_T *frc;
10632 int count = 0;
10633
10634 if (fr->fr_layout != FR_LEAF)
10635 {
10636 /* Find first frame that's not skipped and then create a window for
10637 * each following one (first frame is already there). */
10638 frc = ses_skipframe(fr->fr_child);
10639 if (frc != NULL)
10640 while ((frc = ses_skipframe(frc->fr_next)) != NULL)
10641 {
10642 /* Make window as big as possible so that we have lots of room
10643 * to split. */
10644 if (put_line(fd, "wincmd _ | wincmd |") == FAIL
10645 || put_line(fd, fr->fr_layout == FR_COL
10646 ? "split" : "vsplit") == FAIL)
10647 return FAIL;
10648 ++count;
10649 }
10650
10651 /* Go back to the first window. */
10652 if (count > 0 && (fprintf(fd, fr->fr_layout == FR_COL
10653 ? "%dwincmd k" : "%dwincmd h", count) < 0
10654 || put_eol(fd) == FAIL))
10655 return FAIL;
10656
10657 /* Recursively create frames/windows in each window of this column or
10658 * row. */
10659 frc = ses_skipframe(fr->fr_child);
10660 while (frc != NULL)
10661 {
10662 ses_win_rec(fd, frc);
10663 frc = ses_skipframe(frc->fr_next);
10664 /* Go to next window. */
10665 if (frc != NULL && put_line(fd, "wincmd w") == FAIL)
10666 return FAIL;
10667 }
10668 }
10669 return OK;
10670}
10671
10672/*
10673 * Skip frames that don't contain windows we want to save in the Session.
10674 * Returns NULL when there none.
10675 */
10676 static frame_T *
10677ses_skipframe(fr)
10678 frame_T *fr;
10679{
10680 frame_T *frc;
10681
10682 for (frc = fr; frc != NULL; frc = frc->fr_next)
10683 if (ses_do_frame(frc))
10684 break;
10685 return frc;
10686}
10687
10688/*
10689 * Return TRUE if frame "fr" has a window somewhere that we want to save in
10690 * the Session.
10691 */
10692 static int
10693ses_do_frame(fr)
10694 frame_T *fr;
10695{
10696 frame_T *frc;
10697
10698 if (fr->fr_layout == FR_LEAF)
10699 return ses_do_win(fr->fr_win);
10700 for (frc = fr->fr_child; frc != NULL; frc = frc->fr_next)
10701 if (ses_do_frame(frc))
10702 return TRUE;
10703 return FALSE;
10704}
10705
10706/*
10707 * Return non-zero if window "wp" is to be stored in the Session.
10708 */
10709 static int
10710ses_do_win(wp)
10711 win_T *wp;
10712{
10713 if (wp->w_buffer->b_fname == NULL
10714#ifdef FEAT_QUICKFIX
10715 /* When 'buftype' is "nofile" can't restore the window contents. */
10716 || bt_nofile(wp->w_buffer)
10717#endif
10718 )
10719 return (ssop_flags & SSOP_BLANK);
10720 if (wp->w_buffer->b_help)
10721 return (ssop_flags & SSOP_HELP);
10722 return TRUE;
10723}
10724
10725/*
10726 * Write commands to "fd" to restore the view of a window.
10727 * Caller must make sure 'scrolloff' is zero.
10728 */
10729 static int
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010730put_view(fd, wp, add_edit, flagp, current_arg_idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010731 FILE *fd;
10732 win_T *wp;
10733 int add_edit; /* add ":edit" command to view */
10734 unsigned *flagp; /* vop_flags or ssop_flags */
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010735 int current_arg_idx; /* current argument index of the window, use
10736 * -1 if unknown */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010737{
10738 win_T *save_curwin;
10739 int f;
10740 int do_cursor;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010741 int did_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010742
10743 /* Always restore cursor position for ":mksession". For ":mkview" only
10744 * when 'viewoptions' contains "cursor". */
10745 do_cursor = (flagp == &ssop_flags || *flagp & SSOP_CURSOR);
10746
10747 /*
10748 * Local argument list.
10749 */
10750 if (wp->w_alist == &global_alist)
10751 {
10752 if (put_line(fd, "argglobal") == FAIL)
10753 return FAIL;
10754 }
10755 else
10756 {
10757 if (ses_arglist(fd, "arglocal", &wp->w_alist->al_ga,
10758 flagp == &vop_flags
10759 || !(*flagp & SSOP_CURDIR)
10760 || wp->w_localdir != NULL, flagp) == FAIL)
10761 return FAIL;
10762 }
10763
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010764 /* Only when part of a session: restore the argument index. Some
10765 * arguments may have been deleted, check if the index is valid. */
Bram Moolenaar51f53df2010-06-26 05:25:54 +020010766 if (wp->w_arg_idx != current_arg_idx && wp->w_arg_idx < WARGCOUNT(wp)
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010767 && flagp == &ssop_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010768 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010769 if (fprintf(fd, "%ldargu", (long)wp->w_arg_idx + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010770 || put_eol(fd) == FAIL)
10771 return FAIL;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010772 did_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010773 }
10774
10775 /* Edit the file. Skip this when ":next" already did it. */
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010776 if (add_edit && (!did_next || wp->w_arg_idx_invalid))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010777 {
10778 /*
10779 * Load the file.
10780 */
10781 if (wp->w_buffer->b_ffname != NULL
10782#ifdef FEAT_QUICKFIX
10783 && !bt_nofile(wp->w_buffer)
10784#endif
10785 )
10786 {
10787 /*
10788 * Editing a file in this buffer: use ":edit file".
10789 * This may have side effects! (e.g., compressed or network file).
10790 */
10791 if (fputs("edit ", fd) < 0
10792 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
10793 return FAIL;
10794 }
10795 else
10796 {
10797 /* No file in this buffer, just make it empty. */
10798 if (put_line(fd, "enew") == FAIL)
10799 return FAIL;
10800#ifdef FEAT_QUICKFIX
10801 if (wp->w_buffer->b_ffname != NULL)
10802 {
10803 /* The buffer does have a name, but it's not a file name. */
10804 if (fputs("file ", fd) < 0
10805 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
10806 return FAIL;
10807 }
10808#endif
10809 do_cursor = FALSE;
10810 }
10811 }
10812
10813 /*
10814 * Local mappings and abbreviations.
10815 */
10816 if ((*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
10817 && makemap(fd, wp->w_buffer) == FAIL)
10818 return FAIL;
10819
10820 /*
10821 * Local options. Need to go to the window temporarily.
10822 * Store only local values when using ":mkview" and when ":mksession" is
10823 * used and 'sessionoptions' doesn't include "options".
10824 * Some folding options are always stored when "folds" is included,
10825 * otherwise the folds would not be restored correctly.
10826 */
10827 save_curwin = curwin;
10828 curwin = wp;
10829 curbuf = curwin->w_buffer;
10830 if (*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
10831 f = makeset(fd, OPT_LOCAL,
10832 flagp == &vop_flags || !(*flagp & SSOP_OPTIONS));
10833#ifdef FEAT_FOLDING
10834 else if (*flagp & SSOP_FOLDS)
10835 f = makefoldset(fd);
10836#endif
10837 else
10838 f = OK;
10839 curwin = save_curwin;
10840 curbuf = curwin->w_buffer;
10841 if (f == FAIL)
10842 return FAIL;
10843
10844#ifdef FEAT_FOLDING
10845 /*
10846 * Save Folds when 'buftype' is empty and for help files.
10847 */
10848 if ((*flagp & SSOP_FOLDS)
10849 && wp->w_buffer->b_ffname != NULL
Bram Moolenaarb1b715d2006-01-21 22:09:43 +000010850# ifdef FEAT_QUICKFIX
10851 && (*wp->w_buffer->b_p_bt == NUL || wp->w_buffer->b_help)
10852# endif
10853 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000010854 {
10855 if (put_folds(fd, wp) == FAIL)
10856 return FAIL;
10857 }
10858#endif
10859
10860 /*
10861 * Set the cursor after creating folds, since that moves the cursor.
10862 */
10863 if (do_cursor)
10864 {
10865
10866 /* Restore the cursor line in the file and relatively in the
10867 * window. Don't use "G", it changes the jumplist. */
10868 if (fprintf(fd, "let s:l = %ld - ((%ld * winheight(0) + %ld) / %ld)",
10869 (long)wp->w_cursor.lnum,
10870 (long)(wp->w_cursor.lnum - wp->w_topline),
10871 (long)wp->w_height / 2, (long)wp->w_height) < 0
10872 || put_eol(fd) == FAIL
10873 || put_line(fd, "if s:l < 1 | let s:l = 1 | endif") == FAIL
10874 || put_line(fd, "exe s:l") == FAIL
10875 || put_line(fd, "normal! zt") == FAIL
10876 || fprintf(fd, "%ld", (long)wp->w_cursor.lnum) < 0
10877 || put_eol(fd) == FAIL)
10878 return FAIL;
10879 /* Restore the cursor column and left offset when not wrapping. */
10880 if (wp->w_cursor.col == 0)
10881 {
10882 if (put_line(fd, "normal! 0") == FAIL)
10883 return FAIL;
10884 }
10885 else
10886 {
10887 if (!wp->w_p_wrap && wp->w_leftcol > 0 && wp->w_width > 0)
10888 {
10889 if (fprintf(fd,
10890 "let s:c = %ld - ((%ld * winwidth(0) + %ld) / %ld)",
Bram Moolenaar558ddad2013-02-20 19:26:29 +010010891 (long)wp->w_virtcol + 1,
10892 (long)(wp->w_virtcol - wp->w_leftcol),
Bram Moolenaar071d4272004-06-13 20:20:40 +000010893 (long)wp->w_width / 2, (long)wp->w_width) < 0
10894 || put_eol(fd) == FAIL
10895 || put_line(fd, "if s:c > 0") == FAIL
10896 || fprintf(fd,
Bram Moolenaar558ddad2013-02-20 19:26:29 +010010897 " exe 'normal! ' . s:c . '|zs' . %ld . '|'",
10898 (long)wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010899 || put_eol(fd) == FAIL
10900 || put_line(fd, "else") == FAIL
Bram Moolenaarfdf447b2013-02-26 17:21:29 +010010901 || fprintf(fd, " normal! 0%d|", wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010902 || put_eol(fd) == FAIL
10903 || put_line(fd, "endif") == FAIL)
10904 return FAIL;
10905 }
10906 else
10907 {
Bram Moolenaar558ddad2013-02-20 19:26:29 +010010908 if (fprintf(fd, "normal! 0%d|", wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010909 || put_eol(fd) == FAIL)
10910 return FAIL;
10911 }
10912 }
10913 }
10914
10915 /*
10916 * Local directory.
10917 */
10918 if (wp->w_localdir != NULL)
10919 {
10920 if (fputs("lcd ", fd) < 0
10921 || ses_put_fname(fd, wp->w_localdir, flagp) == FAIL
10922 || put_eol(fd) == FAIL)
10923 return FAIL;
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010924 did_lcd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010925 }
10926
10927 return OK;
10928}
10929
10930/*
10931 * Write an argument list to the session file.
10932 * Returns FAIL if writing fails.
10933 */
10934 static int
10935ses_arglist(fd, cmd, gap, fullname, flagp)
10936 FILE *fd;
10937 char *cmd;
10938 garray_T *gap;
10939 int fullname; /* TRUE: use full path name */
10940 unsigned *flagp;
10941{
10942 int i;
Bram Moolenaard9462e32011-04-11 21:35:11 +020010943 char_u *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010944 char_u *s;
10945
10946 if (gap->ga_len == 0)
10947 return put_line(fd, "silent! argdel *");
10948 if (fputs(cmd, fd) < 0)
10949 return FAIL;
10950 for (i = 0; i < gap->ga_len; ++i)
10951 {
10952 /* NULL file names are skipped (only happens when out of memory). */
10953 s = alist_name(&((aentry_T *)gap->ga_data)[i]);
10954 if (s != NULL)
10955 {
10956 if (fullname)
10957 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020010958 buf = alloc(MAXPATHL);
10959 if (buf != NULL)
10960 {
10961 (void)vim_FullName(s, buf, MAXPATHL, FALSE);
10962 s = buf;
10963 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010964 }
10965 if (fputs(" ", fd) < 0 || ses_put_fname(fd, s, flagp) == FAIL)
Bram Moolenaard9462e32011-04-11 21:35:11 +020010966 {
10967 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010968 return FAIL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020010969 }
10970 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010971 }
10972 }
10973 return put_eol(fd);
10974}
10975
10976/*
10977 * Write a buffer name to the session file.
10978 * Also ends the line.
10979 * Returns FAIL if writing fails.
10980 */
10981 static int
10982ses_fname(fd, buf, flagp)
10983 FILE *fd;
10984 buf_T *buf;
10985 unsigned *flagp;
10986{
10987 char_u *name;
10988
10989 /* Use the short file name if the current directory is known at the time
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010990 * the session file will be sourced.
10991 * Don't do this for ":mkview", we don't know the current directory.
10992 * Don't do this after ":lcd", we don't keep track of what the current
10993 * directory is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010994 if (buf->b_sfname != NULL
10995 && flagp == &ssop_flags
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010996 && (ssop_flags & (SSOP_CURDIR | SSOP_SESDIR))
Bram Moolenaar8d594672009-07-01 18:18:57 +000010997#ifdef FEAT_AUTOCHDIR
10998 && !p_acd
10999#endif
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011000 && !did_lcd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011001 name = buf->b_sfname;
11002 else
11003 name = buf->b_ffname;
11004 if (ses_put_fname(fd, name, flagp) == FAIL || put_eol(fd) == FAIL)
11005 return FAIL;
11006 return OK;
11007}
11008
11009/*
11010 * Write a file name to the session file.
11011 * Takes care of the "slash" option in 'sessionoptions' and escapes special
11012 * characters.
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011013 * Returns FAIL if writing fails or out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011014 */
11015 static int
11016ses_put_fname(fd, name, flagp)
11017 FILE *fd;
11018 char_u *name;
11019 unsigned *flagp;
11020{
11021 char_u *sname;
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011022 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011023 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011024
11025 sname = home_replace_save(NULL, name);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011026 if (sname == NULL)
11027 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011028
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011029 if (*flagp & SSOP_SLASH)
11030 {
11031 /* change all backslashes to forward slashes */
11032 for (p = sname; *p != NUL; mb_ptr_adv(p))
11033 if (*p == '\\')
11034 *p = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +000011035 }
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011036
Bram Moolenaar84a05ac2013-05-06 04:24:17 +020011037 /* escape special characters */
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011038 p = vim_strsave_fnameescape(sname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011039 vim_free(sname);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011040 if (p == NULL)
11041 return FAIL;
11042
11043 /* write the result */
11044 if (fputs((char *)p, fd) < 0)
11045 retval = FAIL;
11046
11047 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011048 return retval;
11049}
11050
11051/*
11052 * ":loadview [nr]"
11053 */
11054 static void
11055ex_loadview(eap)
11056 exarg_T *eap;
11057{
11058 char_u *fname;
11059
11060 fname = get_view_file(*eap->arg);
11061 if (fname != NULL)
11062 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +000011063 do_source(fname, FALSE, DOSO_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011064 vim_free(fname);
11065 }
11066}
11067
11068/*
11069 * Get the name of the view file for the current buffer.
11070 */
11071 static char_u *
11072get_view_file(c)
11073 int c;
11074{
11075 int len = 0;
11076 char_u *p, *s;
11077 char_u *retval;
11078 char_u *sname;
11079
11080 if (curbuf->b_ffname == NULL)
11081 {
11082 EMSG(_(e_noname));
11083 return NULL;
11084 }
11085 sname = home_replace_save(NULL, curbuf->b_ffname);
11086 if (sname == NULL)
11087 return NULL;
11088
11089 /*
11090 * We want a file name without separators, because we're not going to make
11091 * a directory.
11092 * "normal" path separator -> "=+"
11093 * "=" -> "=="
11094 * ":" path separator -> "=-"
11095 */
11096 for (p = sname; *p; ++p)
11097 if (*p == '=' || vim_ispathsep(*p))
11098 ++len;
11099 retval = alloc((unsigned)(STRLEN(sname) + len + STRLEN(p_vdir) + 9));
11100 if (retval != NULL)
11101 {
11102 STRCPY(retval, p_vdir);
11103 add_pathsep(retval);
11104 s = retval + STRLEN(retval);
11105 for (p = sname; *p; ++p)
11106 {
11107 if (*p == '=')
11108 {
11109 *s++ = '=';
11110 *s++ = '=';
11111 }
11112 else if (vim_ispathsep(*p))
11113 {
11114 *s++ = '=';
Bram Moolenaare60acc12011-05-10 16:41:25 +020011115#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011116 if (*p == ':')
11117 *s++ = '-';
11118 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000011119#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000011120 *s++ = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +000011121 }
11122 else
11123 *s++ = *p;
11124 }
11125 *s++ = '=';
11126 *s++ = c;
11127 STRCPY(s, ".vim");
11128 }
11129
11130 vim_free(sname);
11131 return retval;
11132}
11133
11134#endif /* FEAT_SESSION */
11135
11136/*
11137 * Write end-of-line character(s) for ":mkexrc", ":mkvimrc" and ":mksession".
11138 * Return FAIL for a write error.
11139 */
11140 int
11141put_eol(fd)
11142 FILE *fd;
11143{
11144 if (
11145#ifdef USE_CRNL
11146 (
11147# ifdef MKSESSION_NL
11148 !mksession_nl &&
11149# endif
11150 (putc('\r', fd) < 0)) ||
11151#endif
11152 (putc('\n', fd) < 0))
11153 return FAIL;
11154 return OK;
11155}
11156
11157/*
11158 * Write a line to "fd".
11159 * Return FAIL for a write error.
11160 */
11161 int
11162put_line(fd, s)
11163 FILE *fd;
11164 char *s;
11165{
11166 if (fputs(s, fd) < 0 || put_eol(fd) == FAIL)
11167 return FAIL;
11168 return OK;
11169}
11170
11171#ifdef FEAT_VIMINFO
11172/*
11173 * ":rviminfo" and ":wviminfo".
11174 */
11175 static void
11176ex_viminfo(eap)
11177 exarg_T *eap;
11178{
11179 char_u *save_viminfo;
11180
11181 save_viminfo = p_viminfo;
11182 if (*p_viminfo == NUL)
11183 p_viminfo = (char_u *)"'100";
11184 if (eap->cmdidx == CMD_rviminfo)
11185 {
Bram Moolenaard812df62008-11-09 12:46:09 +000011186 if (read_viminfo(eap->arg, VIF_WANT_INFO | VIF_WANT_MARKS
11187 | (eap->forceit ? VIF_FORCEIT : 0)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011188 EMSG(_("E195: Cannot open viminfo file for reading"));
11189 }
11190 else
11191 write_viminfo(eap->arg, eap->forceit);
11192 p_viminfo = save_viminfo;
11193}
11194#endif
11195
11196#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +000011197/*
Bram Moolenaard9462e32011-04-11 21:35:11 +020011198 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +000011199 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +000011200 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011201 void
11202dialog_msg(buff, format, fname)
11203 char_u *buff;
11204 char *format;
11205 char_u *fname;
11206{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011207 if (fname == NULL)
11208 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +020011209 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011210}
11211#endif
11212
11213/*
11214 * ":behave {mswin,xterm}"
11215 */
11216 static void
11217ex_behave(eap)
11218 exarg_T *eap;
11219{
11220 if (STRCMP(eap->arg, "mswin") == 0)
11221 {
11222 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
11223 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
11224 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
11225 set_option_value((char_u *)"keymodel", 0L,
11226 (char_u *)"startsel,stopsel", 0);
11227 }
11228 else if (STRCMP(eap->arg, "xterm") == 0)
11229 {
11230 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
11231 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
11232 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
11233 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
11234 }
11235 else
11236 EMSG2(_(e_invarg2), eap->arg);
11237}
11238
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010011239#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
11240/*
11241 * Function given to ExpandGeneric() to obtain the possible arguments of the
11242 * ":behave {mswin,xterm}" command.
11243 */
11244 char_u *
11245get_behave_arg(xp, idx)
11246 expand_T *xp UNUSED;
11247 int idx;
11248{
11249 if (idx == 0)
11250 return (char_u *)"mswin";
11251 if (idx == 1)
11252 return (char_u *)"xterm";
11253 return NULL;
11254}
11255#endif
11256
Bram Moolenaar071d4272004-06-13 20:20:40 +000011257#ifdef FEAT_AUTOCMD
11258static int filetype_detect = FALSE;
11259static int filetype_plugin = FALSE;
11260static int filetype_indent = FALSE;
11261
11262/*
11263 * ":filetype [plugin] [indent] {on,off,detect}"
11264 * on: Load the filetype.vim file to install autocommands for file types.
11265 * off: Load the ftoff.vim file to remove all autocommands for file types.
11266 * plugin on: load filetype.vim and ftplugin.vim
11267 * plugin off: load ftplugof.vim
11268 * indent on: load filetype.vim and indent.vim
11269 * indent off: load indoff.vim
11270 */
11271 static void
11272ex_filetype(eap)
11273 exarg_T *eap;
11274{
11275 char_u *arg = eap->arg;
11276 int plugin = FALSE;
11277 int indent = FALSE;
11278
11279 if (*eap->arg == NUL)
11280 {
11281 /* Print current status. */
11282 smsg((char_u *)"filetype detection:%s plugin:%s indent:%s",
11283 filetype_detect ? "ON" : "OFF",
11284 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
11285 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
11286 return;
11287 }
11288
11289 /* Accept "plugin" and "indent" in any order. */
11290 for (;;)
11291 {
11292 if (STRNCMP(arg, "plugin", 6) == 0)
11293 {
11294 plugin = TRUE;
11295 arg = skipwhite(arg + 6);
11296 continue;
11297 }
11298 if (STRNCMP(arg, "indent", 6) == 0)
11299 {
11300 indent = TRUE;
11301 arg = skipwhite(arg + 6);
11302 continue;
11303 }
11304 break;
11305 }
11306 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
11307 {
11308 if (*arg == 'o' || !filetype_detect)
11309 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011310 source_runtime((char_u *)FILETYPE_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011311 filetype_detect = TRUE;
11312 if (plugin)
11313 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011314 source_runtime((char_u *)FTPLUGIN_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011315 filetype_plugin = TRUE;
11316 }
11317 if (indent)
11318 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011319 source_runtime((char_u *)INDENT_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011320 filetype_indent = TRUE;
11321 }
11322 }
11323 if (*arg == 'd')
11324 {
11325 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE);
Bram Moolenaara3227e22006-03-08 21:32:40 +000011326 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011327 }
11328 }
11329 else if (STRCMP(arg, "off") == 0)
11330 {
11331 if (plugin || indent)
11332 {
11333 if (plugin)
11334 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011335 source_runtime((char_u *)FTPLUGOF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011336 filetype_plugin = FALSE;
11337 }
11338 if (indent)
11339 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011340 source_runtime((char_u *)INDOFF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011341 filetype_indent = FALSE;
11342 }
11343 }
11344 else
11345 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011346 source_runtime((char_u *)FTOFF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011347 filetype_detect = FALSE;
11348 }
11349 }
11350 else
11351 EMSG2(_(e_invarg2), arg);
11352}
11353
11354/*
11355 * ":setfiletype {name}"
11356 */
11357 static void
11358ex_setfiletype(eap)
11359 exarg_T *eap;
11360{
11361 if (!did_filetype)
11362 set_option_value((char_u *)"filetype", 0L, eap->arg, OPT_LOCAL);
11363}
11364#endif
11365
Bram Moolenaar071d4272004-06-13 20:20:40 +000011366 static void
11367ex_digraphs(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +000011368 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011369{
11370#ifdef FEAT_DIGRAPHS
11371 if (*eap->arg != NUL)
11372 putdigraph(eap->arg);
11373 else
11374 listdigraphs();
11375#else
11376 EMSG(_("E196: No digraphs in this version"));
11377#endif
11378}
11379
11380 static void
11381ex_set(eap)
11382 exarg_T *eap;
11383{
11384 int flags = 0;
11385
11386 if (eap->cmdidx == CMD_setlocal)
11387 flags = OPT_LOCAL;
11388 else if (eap->cmdidx == CMD_setglobal)
11389 flags = OPT_GLOBAL;
11390#if defined(FEAT_EVAL) && defined(FEAT_AUTOCMD) && defined(FEAT_BROWSE)
11391 if (cmdmod.browse && flags == 0)
11392 ex_options(eap);
11393 else
11394#endif
11395 (void)do_set(eap->arg, flags);
11396}
11397
11398#ifdef FEAT_SEARCH_EXTRA
11399/*
11400 * ":nohlsearch"
11401 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011402 static void
11403ex_nohlsearch(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +000011404 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011405{
Bram Moolenaar8050efa2013-11-08 04:30:20 +010011406 SET_NO_HLSEARCH(TRUE);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +000011407 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011408}
11409
11410/*
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011411 * ":[N]match {group} {pattern}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000011412 * Sets nextcmd to the start of the next command, if any. Also called when
11413 * skipping commands to find the next command.
11414 */
11415 static void
11416ex_match(eap)
11417 exarg_T *eap;
11418{
11419 char_u *p;
Bram Moolenaar52d36c82007-08-11 14:00:30 +000011420 char_u *g = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011421 char_u *end;
11422 int c;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011423 int id;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011424
11425 if (eap->line2 <= 3)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011426 id = eap->line2;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011427 else
11428 {
11429 EMSG(e_invcmd);
11430 return;
11431 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011432
11433 /* First clear any old pattern. */
11434 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011435 match_delete(curwin, id, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011436
11437 if (ends_excmd(*eap->arg))
11438 end = eap->arg;
11439 else if ((STRNICMP(eap->arg, "none", 4) == 0
11440 && (vim_iswhite(eap->arg[4]) || ends_excmd(eap->arg[4]))))
11441 end = eap->arg + 4;
11442 else
11443 {
11444 p = skiptowhite(eap->arg);
11445 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011446 g = vim_strnsave(eap->arg, (int)(p - eap->arg));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011447 p = skipwhite(p);
11448 if (*p == NUL)
11449 {
11450 /* There must be two arguments. */
11451 EMSG2(_(e_invarg2), eap->arg);
11452 return;
11453 }
11454 end = skip_regexp(p + 1, *p, TRUE, NULL);
11455 if (!eap->skip)
11456 {
11457 if (*end != NUL && !ends_excmd(*skipwhite(end + 1)))
11458 {
11459 eap->errmsg = e_trailing;
11460 return;
11461 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000011462 if (*end != *p)
11463 {
11464 EMSG2(_(e_invarg2), p);
11465 return;
11466 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011467
11468 c = *end;
11469 *end = NUL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011470 match_add(curwin, g, p + 1, 10, id);
11471 vim_free(g);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000011472 *end = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011473 }
11474 }
11475 eap->nextcmd = find_nextcmd(end);
11476}
11477#endif
11478
11479#ifdef FEAT_CRYPT
11480/*
11481 * ":X": Get crypt key
11482 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011483 static void
11484ex_X(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +000011485 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011486{
Bram Moolenaar49771f42010-07-20 17:32:38 +020011487 if (get_crypt_method(curbuf) == 0 || blowfish_self_test() == OK)
Bram Moolenaar40e6a712010-05-16 22:32:54 +020011488 (void)get_crypt_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011489}
11490#endif
11491
11492#ifdef FEAT_FOLDING
11493 static void
11494ex_fold(eap)
11495 exarg_T *eap;
11496{
11497 if (foldManualAllowed(TRUE))
11498 foldCreate(eap->line1, eap->line2);
11499}
11500
11501 static void
11502ex_foldopen(eap)
11503 exarg_T *eap;
11504{
11505 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
11506 eap->forceit, FALSE);
11507}
11508
11509 static void
11510ex_folddo(eap)
11511 exarg_T *eap;
11512{
11513 linenr_T lnum;
11514
11515 /* First set the marks for all lines closed/open. */
11516 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
11517 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
11518 ml_setmarked(lnum);
11519
11520 /* Execute the command on the marked lines. */
11521 global_exe(eap->arg);
11522 ml_clearmarked(); /* clear rest of the marks */
11523}
11524#endif