blob: 8d06a95c93538117c1c01a11980e8d0176034f3d [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 Moolenaar071d4272004-06-13 20:20:40 +00001846 if (!checkforcmd(&ea.cmd, "keepjumps", 5))
1847 break;
1848 cmdmod.keepjumps = TRUE;
1849 continue;
1850
1851 /* ":hide" and ":hide | cmd" are not modifiers */
1852 case 'h': if (p != ea.cmd || !checkforcmd(&p, "hide", 3)
1853 || *p == NUL || ends_excmd(*p))
1854 break;
1855 ea.cmd = p;
1856 cmdmod.hide = TRUE;
1857 continue;
1858
1859 case 'l': if (checkforcmd(&ea.cmd, "lockmarks", 3))
1860 {
1861 cmdmod.lockmarks = TRUE;
1862 continue;
1863 }
1864
1865 if (!checkforcmd(&ea.cmd, "leftabove", 5))
1866 break;
1867#ifdef FEAT_WINDOWS
1868 cmdmod.split |= WSP_ABOVE;
1869#endif
1870 continue;
1871
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001872 case 'n': if (!checkforcmd(&ea.cmd, "noautocmd", 3))
1873 break;
1874#ifdef FEAT_AUTOCMD
1875 if (cmdmod.save_ei == NULL)
1876 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001877 /* Set 'eventignore' to "all". Restore the
1878 * existing option value later. */
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001879 cmdmod.save_ei = vim_strsave(p_ei);
1880 set_string_option_direct((char_u *)"ei", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001881 (char_u *)"all", OPT_FREE, SID_NONE);
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001882 }
1883#endif
1884 continue;
1885
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886 case 'r': if (!checkforcmd(&ea.cmd, "rightbelow", 6))
1887 break;
1888#ifdef FEAT_WINDOWS
1889 cmdmod.split |= WSP_BELOW;
1890#endif
1891 continue;
1892
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001893 case 's': if (checkforcmd(&ea.cmd, "sandbox", 3))
1894 {
1895#ifdef HAVE_SANDBOX
1896 if (!did_sandbox)
1897 ++sandbox;
1898 did_sandbox = TRUE;
1899#endif
1900 continue;
1901 }
1902 if (!checkforcmd(&ea.cmd, "silent", 3))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903 break;
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001904 if (save_msg_silent == -1)
1905 save_msg_silent = msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001906 ++msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907 if (*ea.cmd == '!' && !vim_iswhite(ea.cmd[-1]))
1908 {
1909 /* ":silent!", but not "silent !cmd" */
1910 ea.cmd = skipwhite(ea.cmd + 1);
1911 ++emsg_silent;
1912 ++did_esilent;
1913 }
1914 continue;
1915
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001916 case 't': if (checkforcmd(&p, "tab", 3))
1917 {
1918#ifdef FEAT_WINDOWS
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001919 if (vim_isdigit(*ea.cmd))
1920 cmdmod.tab = atoi((char *)ea.cmd) + 1;
1921 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001922 cmdmod.tab = tabpage_index(curtab) + 1;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001923 ea.cmd = p;
1924#endif
1925 continue;
1926 }
1927 if (!checkforcmd(&ea.cmd, "topleft", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001928 break;
1929#ifdef FEAT_WINDOWS
1930 cmdmod.split |= WSP_TOP;
1931#endif
1932 continue;
1933
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001934 case 'u': if (!checkforcmd(&ea.cmd, "unsilent", 3))
1935 break;
1936 if (save_msg_silent == -1)
1937 save_msg_silent = msg_silent;
1938 msg_silent = 0;
1939 continue;
1940
Bram Moolenaar071d4272004-06-13 20:20:40 +00001941 case 'v': if (checkforcmd(&ea.cmd, "vertical", 4))
1942 {
1943#ifdef FEAT_VERTSPLIT
1944 cmdmod.split |= WSP_VERT;
1945#endif
1946 continue;
1947 }
1948 if (!checkforcmd(&p, "verbose", 4))
1949 break;
1950 if (verbose_save < 0)
1951 verbose_save = p_verbose;
Bram Moolenaared203462004-06-16 11:19:22 +00001952 if (vim_isdigit(*ea.cmd))
1953 p_verbose = atoi((char *)ea.cmd);
1954 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955 p_verbose = 1;
1956 ea.cmd = p;
1957 continue;
1958 }
1959 break;
1960 }
1961
1962#ifdef FEAT_EVAL
1963 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
1964 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
1965#else
1966 ea.skip = (if_level > 0);
1967#endif
1968
1969#ifdef FEAT_EVAL
Bram Moolenaar05159a02005-02-26 23:04:13 +00001970# ifdef FEAT_PROFILE
1971 /* Count this line for profiling if ea.skip is FALSE. */
Bram Moolenaar371d5402006-03-20 21:47:49 +00001972 if (do_profiling == PROF_YES && !ea.skip)
Bram Moolenaar05159a02005-02-26 23:04:13 +00001973 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001974 if (getline_equal(fgetline, cookie, get_func_line))
1975 func_line_exec(getline_cookie(fgetline, cookie));
1976 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +00001977 script_line_exec();
1978 }
1979#endif
1980
Bram Moolenaar071d4272004-06-13 20:20:40 +00001981 /* May go to debug mode. If this happens and the ">quit" debug command is
1982 * used, throw an interrupt exception and skip the next command. */
1983 dbg_check_breakpoint(&ea);
1984 if (!ea.skip && got_int)
1985 {
1986 ea.skip = TRUE;
1987 (void)do_intthrow(cstack);
1988 }
1989#endif
1990
1991/*
1992 * 3. parse a range specifier of the form: addr [,addr] [;addr] ..
1993 *
1994 * where 'addr' is:
1995 *
1996 * % (entire file)
1997 * $ [+-NUM]
1998 * 'x [+-NUM] (where x denotes a currently defined mark)
1999 * . [+-NUM]
2000 * [+-NUM]..
2001 * NUM
2002 *
2003 * The ea.cmd pointer is updated to point to the first character following the
2004 * range spec. If an initial address is found, but no second, the upper bound
2005 * is equal to the lower.
2006 */
2007
2008 /* repeat for all ',' or ';' separated addresses */
2009 for (;;)
2010 {
2011 ea.line1 = ea.line2;
2012 ea.line2 = curwin->w_cursor.lnum; /* default is current line number */
2013 ea.cmd = skipwhite(ea.cmd);
2014 lnum = get_address(&ea.cmd, ea.skip, ea.addr_count == 0);
2015 if (ea.cmd == NULL) /* error detected */
2016 goto doend;
2017 if (lnum == MAXLNUM)
2018 {
2019 if (*ea.cmd == '%') /* '%' - all lines */
2020 {
2021 ++ea.cmd;
2022 ea.line1 = 1;
2023 ea.line2 = curbuf->b_ml.ml_line_count;
2024 ++ea.addr_count;
2025 }
2026 /* '*' - visual area */
2027 else if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
2028 {
2029 pos_T *fp;
2030
2031 ++ea.cmd;
2032 if (!ea.skip)
2033 {
2034 fp = getmark('<', FALSE);
2035 if (check_mark(fp) == FAIL)
2036 goto doend;
2037 ea.line1 = fp->lnum;
2038 fp = getmark('>', FALSE);
2039 if (check_mark(fp) == FAIL)
2040 goto doend;
2041 ea.line2 = fp->lnum;
2042 ++ea.addr_count;
2043 }
2044 }
2045 }
2046 else
2047 ea.line2 = lnum;
2048 ea.addr_count++;
2049
2050 if (*ea.cmd == ';')
2051 {
2052 if (!ea.skip)
2053 curwin->w_cursor.lnum = ea.line2;
2054 }
2055 else if (*ea.cmd != ',')
2056 break;
2057 ++ea.cmd;
2058 }
2059
2060 /* One address given: set start and end lines */
2061 if (ea.addr_count == 1)
2062 {
2063 ea.line1 = ea.line2;
2064 /* ... but only implicit: really no address given */
2065 if (lnum == MAXLNUM)
2066 ea.addr_count = 0;
2067 }
2068
2069 /* Don't leave the cursor on an illegal line (caused by ';') */
2070 check_cursor_lnum();
2071
2072/*
2073 * 4. parse command
2074 */
2075
2076 /*
2077 * Skip ':' and any white space
2078 */
2079 ea.cmd = skipwhite(ea.cmd);
2080 while (*ea.cmd == ':')
2081 ea.cmd = skipwhite(ea.cmd + 1);
2082
2083 /*
2084 * If we got a line, but no command, then go to the line.
2085 * If we find a '|' or '\n' we set ea.nextcmd.
2086 */
2087 if (*ea.cmd == NUL || *ea.cmd == '"' ||
2088 (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
2089 {
2090 /*
2091 * strange vi behaviour:
2092 * ":3" jumps to line 3
2093 * ":3|..." prints line 3
2094 * ":|" prints current line
2095 */
2096 if (ea.skip) /* skip this if inside :if */
2097 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00002098 if (*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002099 {
2100 ea.cmdidx = CMD_print;
2101 ea.argt = RANGE+COUNT+TRLBAR;
2102 if ((errormsg = invalid_range(&ea)) == NULL)
2103 {
2104 correct_range(&ea);
2105 ex_print(&ea);
2106 }
2107 }
2108 else if (ea.addr_count != 0)
2109 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002110 if (ea.line2 > curbuf->b_ml.ml_line_count)
2111 {
2112 /* With '-' in 'cpoptions' a line number past the file is an
2113 * error, otherwise put it at the end of the file. */
2114 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
2115 ea.line2 = -1;
2116 else
2117 ea.line2 = curbuf->b_ml.ml_line_count;
2118 }
2119
2120 if (ea.line2 < 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002121 errormsg = (char_u *)_(e_invrange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002122 else
2123 {
2124 if (ea.line2 == 0)
2125 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002126 else
2127 curwin->w_cursor.lnum = ea.line2;
2128 beginline(BL_SOL | BL_FIX);
2129 }
2130 }
2131 goto doend;
2132 }
2133
2134 /* Find the command and let "p" point to after it. */
2135 p = find_command(&ea, NULL);
2136
2137#ifdef FEAT_USR_CMDS
2138 if (p == NULL)
2139 {
2140 if (!ea.skip)
2141 errormsg = (char_u *)_("E464: Ambiguous use of user-defined command");
2142 goto doend;
2143 }
2144 /* Check for wrong commands. */
2145 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78)
2146 {
2147 errormsg = uc_fun_cmd();
2148 goto doend;
2149 }
2150#endif
2151 if (ea.cmdidx == CMD_SIZE)
2152 {
2153 if (!ea.skip)
2154 {
2155 STRCPY(IObuff, _("E492: Not an editor command"));
2156 if (!sourcing)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002157 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158 errormsg = IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02002159 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160 }
2161 goto doend;
2162 }
2163
2164 ni = (
2165#ifdef FEAT_USR_CMDS
2166 !USER_CMDIDX(ea.cmdidx) &&
2167#endif
Bram Moolenaar3ebc1e52007-07-05 07:54:17 +00002168 (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00002169#ifdef HAVE_EX_SCRIPT_NI
2170 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
2171#endif
2172 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002173
2174#ifndef FEAT_EVAL
2175 /*
2176 * When the expression evaluation is disabled, recognize the ":if" and
2177 * ":endif" commands and ignore everything in between it.
2178 */
2179 if (ea.cmdidx == CMD_if)
2180 ++if_level;
2181 if (if_level)
2182 {
2183 if (ea.cmdidx == CMD_endif)
2184 --if_level;
2185 goto doend;
2186 }
2187
2188#endif
2189
Bram Moolenaar196b3b02008-06-20 16:51:41 +00002190 /* forced commands */
2191 if (*p == '!' && ea.cmdidx != CMD_substitute
2192 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002193 {
2194 ++p;
2195 ea.forceit = TRUE;
2196 }
2197 else
2198 ea.forceit = FALSE;
2199
2200/*
2201 * 5. parse arguments
2202 */
2203#ifdef FEAT_USR_CMDS
2204 if (!USER_CMDIDX(ea.cmdidx))
2205#endif
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002206 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002207
2208 if (!ea.skip)
2209 {
2210#ifdef HAVE_SANDBOX
2211 if (sandbox != 0 && !(ea.argt & SBOXOK))
2212 {
2213 /* Command not allowed in sandbox. */
2214 errormsg = (char_u *)_(e_sandbox);
2215 goto doend;
2216 }
2217#endif
2218 if (!curbuf->b_p_ma && (ea.argt & MODIFY))
2219 {
2220 /* Command not allowed in non-'modifiable' buffer */
2221 errormsg = (char_u *)_(e_modifiable);
2222 goto doend;
2223 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002224
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002225 if (text_locked() && !(ea.argt & CMDWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002226# ifdef FEAT_USR_CMDS
2227 && !USER_CMDIDX(ea.cmdidx)
2228# endif
2229 )
2230 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002231 /* Command not allowed when editing the command line. */
2232#ifdef FEAT_CMDWIN
2233 if (cmdwin_type != 0)
2234 errormsg = (char_u *)_(e_cmdwin);
2235 else
2236#endif
2237 errormsg = (char_u *)_(e_secure);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002238 goto doend;
2239 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002240#ifdef FEAT_AUTOCMD
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002241 /* Disallow editing another buffer when "curbuf_lock" is set.
2242 * Do allow ":edit" (check for argument later).
2243 * Do allow ":checktime" (it's postponed). */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002244 if (!(ea.argt & CMDWIN)
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002245 && ea.cmdidx != CMD_edit
2246 && ea.cmdidx != CMD_checktime
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002247# ifdef FEAT_USR_CMDS
2248 && !USER_CMDIDX(ea.cmdidx)
2249# endif
2250 && curbuf_locked())
2251 goto doend;
2252#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002253
2254 if (!ni && !(ea.argt & RANGE) && ea.addr_count > 0)
2255 {
2256 /* no range allowed */
2257 errormsg = (char_u *)_(e_norange);
2258 goto doend;
2259 }
2260 }
2261
2262 if (!ni && !(ea.argt & BANG) && ea.forceit) /* no <!> allowed */
2263 {
2264 errormsg = (char_u *)_(e_nobang);
2265 goto doend;
2266 }
2267
2268 /*
2269 * Don't complain about the range if it is not used
2270 * (could happen if line_count is accidentally set to 0).
2271 */
2272 if (!ea.skip && !ni)
2273 {
2274 /*
2275 * If the range is backwards, ask for confirmation and, if given, swap
2276 * ea.line1 & ea.line2 so it's forwards again.
2277 * When global command is busy, don't ask, will fail below.
2278 */
2279 if (!global_busy && ea.line1 > ea.line2)
2280 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002281 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002283 if (sourcing || exmode_active)
2284 {
2285 errormsg = (char_u *)_("E493: Backwards range given");
2286 goto doend;
2287 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288 if (ask_yesno((char_u *)
2289 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002290 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291 }
2292 lnum = ea.line1;
2293 ea.line1 = ea.line2;
2294 ea.line2 = lnum;
2295 }
2296 if ((errormsg = invalid_range(&ea)) != NULL)
2297 goto doend;
2298 }
2299
2300 if ((ea.argt & NOTADR) && ea.addr_count == 0) /* default is 1, not cursor */
2301 ea.line2 = 1;
2302
2303 correct_range(&ea);
2304
2305#ifdef FEAT_FOLDING
2306 if (((ea.argt & WHOLEFOLD) || ea.addr_count >= 2) && !global_busy)
2307 {
2308 /* Put the first line at the start of a closed fold, put the last line
2309 * at the end of a closed fold. */
2310 (void)hasFolding(ea.line1, &ea.line1, NULL);
2311 (void)hasFolding(ea.line2, NULL, &ea.line2);
2312 }
2313#endif
2314
2315#ifdef FEAT_QUICKFIX
2316 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002317 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318 * option here, so things like % get expanded.
2319 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002320 p = replace_makeprg(&ea, p, cmdlinep);
2321 if (p == NULL)
2322 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323#endif
2324
2325 /*
2326 * Skip to start of argument.
2327 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2328 */
2329 if (ea.cmdidx == CMD_bang)
2330 ea.arg = p;
2331 else
2332 ea.arg = skipwhite(p);
2333
2334 /*
2335 * Check for "++opt=val" argument.
2336 * Must be first, allow ":w ++enc=utf8 !cmd"
2337 */
2338 if (ea.argt & ARGOPT)
2339 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2340 if (getargopt(&ea) == FAIL && !ni)
2341 {
2342 errormsg = (char_u *)_(e_invarg);
2343 goto doend;
2344 }
2345
2346 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2347 {
2348 if (*ea.arg == '>') /* append */
2349 {
2350 if (*++ea.arg != '>') /* typed wrong */
2351 {
2352 errormsg = (char_u *)_("E494: Use w or w>>");
2353 goto doend;
2354 }
2355 ea.arg = skipwhite(ea.arg + 1);
2356 ea.append = TRUE;
2357 }
2358 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
2359 {
2360 ++ea.arg;
2361 ea.usefilter = TRUE;
2362 }
2363 }
2364
2365 if (ea.cmdidx == CMD_read)
2366 {
2367 if (ea.forceit)
2368 {
2369 ea.usefilter = TRUE; /* :r! filter if ea.forceit */
2370 ea.forceit = FALSE;
2371 }
2372 else if (*ea.arg == '!') /* :r !filter */
2373 {
2374 ++ea.arg;
2375 ea.usefilter = TRUE;
2376 }
2377 }
2378
2379 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2380 {
2381 ea.amount = 1;
2382 while (*ea.arg == *ea.cmd) /* count number of '>' or '<' */
2383 {
2384 ++ea.arg;
2385 ++ea.amount;
2386 }
2387 ea.arg = skipwhite(ea.arg);
2388 }
2389
2390 /*
2391 * Check for "+command" argument, before checking for next command.
2392 * Don't do this for ":read !cmd" and ":write !cmd".
2393 */
2394 if ((ea.argt & EDITCMD) && !ea.usefilter)
2395 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2396
2397 /*
2398 * Check for '|' to separate commands and '"' to start comments.
2399 * Don't do this for ":read !cmd" and ":write !cmd".
2400 */
2401 if ((ea.argt & TRLBAR) && !ea.usefilter)
2402 separate_nextcmd(&ea);
2403
2404 /*
2405 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002406 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2407 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002409 else if (ea.cmdidx == CMD_bang
2410 || ea.cmdidx == CMD_global
2411 || ea.cmdidx == CMD_vglobal
2412 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413 {
2414 for (p = ea.arg; *p; ++p)
2415 {
2416 /* Remove one backslash before a newline, so that it's possible to
2417 * pass a newline to the shell and also a newline that is preceded
2418 * with a backslash. This makes it impossible to end a shell
2419 * command in a backslash, but that doesn't appear useful.
2420 * Halving the number of backslashes is incompatible with previous
2421 * versions. */
2422 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002423 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424 else if (*p == '\n')
2425 {
2426 ea.nextcmd = p + 1;
2427 *p = NUL;
2428 break;
2429 }
2430 }
2431 }
2432
2433 if ((ea.argt & DFLALL) && ea.addr_count == 0)
2434 {
2435 ea.line1 = 1;
2436 ea.line2 = curbuf->b_ml.ml_line_count;
2437 }
2438
2439 /* accept numbered register only when no count allowed (:put) */
2440 if ( (ea.argt & REGSTR)
2441 && *ea.arg != NUL
2442#ifdef FEAT_USR_CMDS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443 /* Do not allow register = for user commands */
2444 && (!USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002445#endif
2446 && !((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)))
2447 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002448#ifndef FEAT_CLIPBOARD
2449 /* check these explicitly for a more specific error message */
2450 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002452 errormsg = (char_u *)_(e_invalidreg);
2453 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454 }
2455#endif
Bram Moolenaar85de2062011-05-05 14:26:41 +02002456 if (
2457#ifdef FEAT_USR_CMDS
2458 valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2459 && USER_CMDIDX(ea.cmdidx)))
2460#else
2461 valid_yank_reg(*ea.arg, ea.cmdidx != CMD_put)
2462#endif
2463 )
2464 {
2465 ea.regname = *ea.arg++;
2466#ifdef FEAT_EVAL
2467 /* for '=' register: accept the rest of the line as an expression */
2468 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2469 {
2470 set_expr_line(vim_strsave(ea.arg));
2471 ea.arg += STRLEN(ea.arg);
2472 }
2473#endif
2474 ea.arg = skipwhite(ea.arg);
2475 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002476 }
2477
2478 /*
2479 * Check for a count. When accepting a BUFNAME, don't use "123foo" as a
2480 * count, it's a buffer name.
2481 */
2482 if ((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)
2483 && (!(ea.argt & BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
2484 || vim_iswhite(*p)))
2485 {
2486 n = getdigits(&ea.arg);
2487 ea.arg = skipwhite(ea.arg);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002488 if (n <= 0 && !ni && (ea.argt & ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002489 {
2490 errormsg = (char_u *)_(e_zerocount);
2491 goto doend;
2492 }
2493 if (ea.argt & NOTADR) /* e.g. :buffer 2, :sleep 3 */
2494 {
2495 ea.line2 = n;
2496 if (ea.addr_count == 0)
2497 ea.addr_count = 1;
2498 }
2499 else
2500 {
2501 ea.line1 = ea.line2;
2502 ea.line2 += n - 1;
2503 ++ea.addr_count;
2504 /*
2505 * Be vi compatible: no error message for out of range.
2506 */
2507 if (ea.line2 > curbuf->b_ml.ml_line_count)
2508 ea.line2 = curbuf->b_ml.ml_line_count;
2509 }
2510 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002511
2512 /*
2513 * Check for flags: 'l', 'p' and '#'.
2514 */
2515 if (ea.argt & EXFLAGS)
2516 get_flags(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002517 /* no arguments allowed */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002518 if (!ni && !(ea.argt & EXTRA) && *ea.arg != NUL
Bram Moolenaara2031822006-03-07 22:29:51 +00002519 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520 {
2521 errormsg = (char_u *)_(e_trailing);
2522 goto doend;
2523 }
2524
2525 if (!ni && (ea.argt & NEEDARG) && *ea.arg == NUL)
2526 {
2527 errormsg = (char_u *)_(e_argreq);
2528 goto doend;
2529 }
2530
2531#ifdef FEAT_EVAL
2532 /*
2533 * Skip the command when it's not going to be executed.
2534 * The commands like :if, :endif, etc. always need to be executed.
2535 * Also make an exception for commands that handle a trailing command
2536 * themselves.
2537 */
2538 if (ea.skip)
2539 {
2540 switch (ea.cmdidx)
2541 {
2542 /* commands that need evaluation */
2543 case CMD_while:
2544 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002545 case CMD_for:
2546 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547 case CMD_if:
2548 case CMD_elseif:
2549 case CMD_else:
2550 case CMD_endif:
2551 case CMD_try:
2552 case CMD_catch:
2553 case CMD_finally:
2554 case CMD_endtry:
2555 case CMD_function:
2556 break;
2557
2558 /* Commands that handle '|' themselves. Check: A command should
2559 * either have the TRLBAR flag, appear in this list or appear in
2560 * the list at ":help :bar". */
2561 case CMD_aboveleft:
2562 case CMD_and:
2563 case CMD_belowright:
2564 case CMD_botright:
2565 case CMD_browse:
2566 case CMD_call:
2567 case CMD_confirm:
2568 case CMD_delfunction:
2569 case CMD_djump:
2570 case CMD_dlist:
2571 case CMD_dsearch:
2572 case CMD_dsplit:
2573 case CMD_echo:
2574 case CMD_echoerr:
2575 case CMD_echomsg:
2576 case CMD_echon:
2577 case CMD_execute:
2578 case CMD_help:
2579 case CMD_hide:
2580 case CMD_ijump:
2581 case CMD_ilist:
2582 case CMD_isearch:
2583 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002584 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002585 case CMD_keepjumps:
2586 case CMD_keepmarks:
2587 case CMD_leftabove:
2588 case CMD_let:
2589 case CMD_lockmarks:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002590 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002591 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002592 case CMD_mzscheme:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593 case CMD_perl:
2594 case CMD_psearch:
2595 case CMD_python:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002596 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002597 case CMD_python3:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002598 case CMD_return:
2599 case CMD_rightbelow:
2600 case CMD_ruby:
2601 case CMD_silent:
2602 case CMD_smagic:
2603 case CMD_snomagic:
2604 case CMD_substitute:
2605 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002606 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002607 case CMD_tcl:
2608 case CMD_throw:
2609 case CMD_tilde:
2610 case CMD_topleft:
2611 case CMD_unlet:
2612 case CMD_verbose:
2613 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002614 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002615 break;
2616
2617 default: goto doend;
2618 }
2619 }
2620#endif
2621
2622 if (ea.argt & XFILE)
2623 {
2624 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2625 goto doend;
2626 }
2627
2628#ifdef FEAT_LISTCMDS
2629 /*
2630 * Accept buffer name. Cannot be used at the same time with a buffer
2631 * number. Don't do this for a user command.
2632 */
2633 if ((ea.argt & BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
2634# ifdef FEAT_USR_CMDS
2635 && !USER_CMDIDX(ea.cmdidx)
2636# endif
2637 )
2638 {
2639 /*
2640 * :bdelete, :bwipeout and :bunload take several arguments, separated
2641 * by spaces: find next space (skipping over escaped characters).
2642 * The others take one argument: ignore trailing spaces.
2643 */
2644 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2645 || ea.cmdidx == CMD_bunload)
2646 p = skiptowhite_esc(ea.arg);
2647 else
2648 {
2649 p = ea.arg + STRLEN(ea.arg);
2650 while (p > ea.arg && vim_iswhite(p[-1]))
2651 --p;
2652 }
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002653 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & BUFUNL) != 0,
2654 FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655 if (ea.line2 < 0) /* failed */
2656 goto doend;
2657 ea.addr_count = 1;
2658 ea.arg = skipwhite(p);
2659 }
2660#endif
2661
2662/*
2663 * 6. switch on command name
2664 *
2665 * The "ea" structure holds the arguments that can be used.
2666 */
2667 ea.cmdlinep = cmdlinep;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002668 ea.getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002669 ea.cookie = cookie;
2670#ifdef FEAT_EVAL
2671 ea.cstack = cstack;
2672#endif
2673
2674#ifdef FEAT_USR_CMDS
2675 if (USER_CMDIDX(ea.cmdidx))
2676 {
2677 /*
2678 * Execute a user-defined command.
2679 */
2680 do_ucmd(&ea);
2681 }
2682 else
2683#endif
2684 {
2685 /*
2686 * Call the function to execute the command.
2687 */
2688 ea.errmsg = NULL;
2689 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2690 if (ea.errmsg != NULL)
2691 errormsg = (char_u *)_(ea.errmsg);
2692 }
2693
2694#ifdef FEAT_EVAL
2695 /*
2696 * If the command just executed called do_cmdline(), any throw or ":return"
2697 * or ":finish" encountered there must also check the cstack of the still
2698 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2699 * exception, or reanimate a returned function or finished script file and
2700 * return or finish it again.
2701 */
2702 if (need_rethrow)
2703 do_throw(cstack);
2704 else if (check_cstack)
2705 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002706 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002708 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709 && current_func_returned())
2710 do_return(&ea, TRUE, FALSE, NULL);
2711 }
2712 need_rethrow = check_cstack = FALSE;
2713#endif
2714
2715doend:
2716 if (curwin->w_cursor.lnum == 0) /* can happen with zero line number */
2717 curwin->w_cursor.lnum = 1;
2718
2719 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2720 {
2721 if (sourcing)
2722 {
2723 if (errormsg != IObuff)
2724 {
2725 STRCPY(IObuff, errormsg);
2726 errormsg = IObuff;
2727 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002728 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002729 }
2730 emsg(errormsg);
2731 }
2732#ifdef FEAT_EVAL
2733 do_errthrow(cstack,
2734 (ea.cmdidx != CMD_SIZE
2735# ifdef FEAT_USR_CMDS
2736 && !USER_CMDIDX(ea.cmdidx)
2737# endif
2738 ) ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
2739#endif
2740
2741 if (verbose_save >= 0)
2742 p_verbose = verbose_save;
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00002743#ifdef FEAT_AUTOCMD
2744 if (cmdmod.save_ei != NULL)
2745 {
2746 /* Restore 'eventignore' to the value before ":noautocmd". */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002747 set_string_option_direct((char_u *)"ei", -1, cmdmod.save_ei,
2748 OPT_FREE, SID_NONE);
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00002749 free_string_option(cmdmod.save_ei);
2750 }
2751#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002752
2753 cmdmod = save_cmdmod;
2754
Bram Moolenaar8e258a42009-07-09 13:55:43 +00002755 if (save_msg_silent != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002756 {
2757 /* messages could be enabled for a serious error, need to check if the
2758 * counters don't become negative */
Bram Moolenaarbecf4282009-09-30 11:24:36 +00002759 if (!did_emsg || msg_silent > save_msg_silent)
Bram Moolenaar8e258a42009-07-09 13:55:43 +00002760 msg_silent = save_msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002761 emsg_silent -= did_esilent;
2762 if (emsg_silent < 0)
2763 emsg_silent = 0;
2764 /* Restore msg_scroll, it's set by file I/O commands, even when no
2765 * message is actually displayed. */
2766 msg_scroll = save_msg_scroll;
Bram Moolenaar77ab2802009-04-22 12:44:48 +00002767
2768 /* "silent reg" or "silent echo x" inside "redir" leaves msg_col
2769 * somewhere in the line. Put it back in the first column. */
2770 if (redirecting())
2771 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002772 }
2773
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002774#ifdef HAVE_SANDBOX
2775 if (did_sandbox)
2776 --sandbox;
2777#endif
2778
Bram Moolenaar071d4272004-06-13 20:20:40 +00002779 if (ea.nextcmd && *ea.nextcmd == NUL) /* not really a next command */
2780 ea.nextcmd = NULL;
2781
2782#ifdef FEAT_EVAL
2783 --ex_nesting_level;
2784#endif
2785
2786 return ea.nextcmd;
2787}
2788#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00002789 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002790#endif
2791
2792/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002793 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002794 * If there is a match advance "pp" to the argument and return TRUE.
2795 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002796 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00002797checkforcmd(pp, cmd, len)
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002798 char_u **pp; /* start of command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799 char *cmd; /* name of command */
2800 int len; /* required length */
2801{
2802 int i;
2803
2804 for (i = 0; cmd[i] != NUL; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00002805 if (((char_u *)cmd)[i] != (*pp)[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00002806 break;
2807 if (i >= len && !isalpha((*pp)[i]))
2808 {
2809 *pp = skipwhite(*pp + i);
2810 return TRUE;
2811 }
2812 return FALSE;
2813}
2814
2815/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002816 * Append "cmd" to the error message in IObuff.
2817 * Takes care of limiting the length and handling 0xa0, which would be
2818 * invisible otherwise.
2819 */
2820 static void
2821append_command(cmd)
2822 char_u *cmd;
2823{
2824 char_u *s = cmd;
2825 char_u *d;
2826
2827 STRCAT(IObuff, ": ");
2828 d = IObuff + STRLEN(IObuff);
2829 while (*s != NUL && d - IObuff < IOSIZE - 7)
2830 {
2831 if (
2832#ifdef FEAT_MBYTE
2833 enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) :
2834#endif
2835 *s == 0xa0)
2836 {
2837 s +=
2838#ifdef FEAT_MBYTE
2839 enc_utf8 ? 2 :
2840#endif
2841 1;
2842 STRCPY(d, "<a0>");
2843 d += 4;
2844 }
2845 else
2846 MB_COPY_CHAR(s, d);
2847 }
2848 *d = NUL;
2849}
2850
2851/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002853 * Start of the name can be found at eap->cmd.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002854 * Returns pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002855 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002856 * Returns NULL for an ambiguous user command.
2857 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858 static char_u *
2859find_command(eap, full)
2860 exarg_T *eap;
Bram Moolenaar78a15312009-05-15 19:33:18 +00002861 int *full UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002862{
2863 int len;
2864 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00002865 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002866
2867 /*
2868 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00002869 * Exceptions:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002870 * - the 'k' command can directly be followed by any character.
2871 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
2872 * but :sre[wind] is another command, as are :scrip[tnames],
2873 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00002874 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002875 */
2876 p = eap->cmd;
2877 if (*p == 'k')
2878 {
2879 eap->cmdidx = CMD_k;
2880 ++p;
2881 }
2882 else if (p[0] == 's'
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002883 && ((p[1] == 'c' && p[2] != 's' && p[2] != 'r'
2884 && p[3] != 'i' && p[4] != 'p')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002885 || p[1] == 'g'
2886 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
2887 || p[1] == 'I'
2888 || (p[1] == 'r' && p[2] != 'e')))
2889 {
2890 eap->cmdidx = CMD_substitute;
2891 ++p;
2892 }
2893 else
2894 {
2895 while (ASCII_ISALPHA(*p))
2896 ++p;
Bram Moolenaarb6590522010-07-21 16:00:43 +02002897 /* for python 3.x support ":py3", ":python3", ":py3file", etc. */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002898 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaarb6590522010-07-21 16:00:43 +02002899 while (ASCII_ISALNUM(*p))
2900 ++p;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02002901
Bram Moolenaar071d4272004-06-13 20:20:40 +00002902 /* check for non-alpha command */
2903 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
2904 ++p;
2905 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00002906 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
2907 {
2908 /* Check for ":dl", ":dell", etc. to ":deletel": that's
2909 * :delete with the 'l' flag. Same for 'p'. */
2910 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00002911 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00002912 break;
2913 if (i == len - 1)
2914 {
2915 --len;
2916 if (p[-1] == 'l')
2917 eap->flags |= EXFLAG_LIST;
2918 else
2919 eap->flags |= EXFLAG_PRINT;
2920 }
2921 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002922
2923 if (ASCII_ISLOWER(*eap->cmd))
2924 eap->cmdidx = cmdidxs[CharOrdLow(*eap->cmd)];
2925 else
2926 eap->cmdidx = cmdidxs[26];
2927
2928 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
2929 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
2930 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
2931 (size_t)len) == 0)
2932 {
2933#ifdef FEAT_EVAL
2934 if (full != NULL
2935 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
2936 *full = TRUE;
2937#endif
2938 break;
2939 }
2940
2941#ifdef FEAT_USR_CMDS
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01002942 /* Look for a user defined command as a last resort. Let ":Print" be
2943 * overruled by a user defined command. */
2944 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
2945 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002946 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002947 /* User defined commands may contain digits. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002948 while (ASCII_ISALNUM(*p))
2949 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002950 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002951 }
2952#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002953 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002954 eap->cmdidx = CMD_SIZE;
2955 }
2956
2957 return p;
2958}
2959
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002960#ifdef FEAT_USR_CMDS
2961/*
2962 * Search for a user command that matches "eap->cmd".
2963 * Return cmdidx in "eap->cmdidx", flags in "eap->argt", idx in "eap->useridx".
2964 * Return a pointer to just after the command.
2965 * Return NULL if there is no matching command.
2966 */
2967 static char_u *
2968find_ucmd(eap, p, full, xp, compl)
2969 exarg_T *eap;
2970 char_u *p; /* end of the command (possibly including count) */
2971 int *full; /* set to TRUE for a full match */
2972 expand_T *xp; /* used for completion, NULL otherwise */
2973 int *compl; /* completion flags or NULL */
2974{
2975 int len = (int)(p - eap->cmd);
2976 int j, k, matchlen = 0;
2977 ucmd_T *uc;
2978 int found = FALSE;
2979 int possible = FALSE;
2980 char_u *cp, *np; /* Point into typed cmd and test name */
2981 garray_T *gap;
2982 int amb_local = FALSE; /* Found ambiguous buffer-local command,
2983 only full match global is accepted. */
2984
2985 /*
2986 * Look for buffer-local user commands first, then global ones.
2987 */
2988 gap = &curbuf->b_ucmds;
2989 for (;;)
2990 {
2991 for (j = 0; j < gap->ga_len; ++j)
2992 {
2993 uc = USER_CMD_GA(gap, j);
2994 cp = eap->cmd;
2995 np = uc->uc_name;
2996 k = 0;
2997 while (k < len && *np != NUL && *cp++ == *np++)
2998 k++;
2999 if (k == len || (*np == NUL && vim_isdigit(eap->cmd[k])))
3000 {
3001 /* If finding a second match, the command is ambiguous. But
3002 * not if a buffer-local command wasn't a full match and a
3003 * global command is a full match. */
3004 if (k == len && found && *np != NUL)
3005 {
3006 if (gap == &ucmds)
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003007 return NULL;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003008 amb_local = TRUE;
3009 }
3010
3011 if (!found || (k == len && *np == NUL))
3012 {
3013 /* If we matched up to a digit, then there could
3014 * be another command including the digit that we
3015 * should use instead.
3016 */
3017 if (k == len)
3018 found = TRUE;
3019 else
3020 possible = TRUE;
3021
3022 if (gap == &ucmds)
3023 eap->cmdidx = CMD_USER;
3024 else
3025 eap->cmdidx = CMD_USER_BUF;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003026 eap->argt = (long)uc->uc_argt;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003027 eap->useridx = j;
3028
3029# ifdef FEAT_CMDL_COMPL
3030 if (compl != NULL)
3031 *compl = uc->uc_compl;
3032# ifdef FEAT_EVAL
3033 if (xp != NULL)
3034 {
3035 xp->xp_arg = uc->uc_compl_arg;
3036 xp->xp_scriptID = uc->uc_scriptID;
3037 }
3038# endif
3039# endif
3040 /* Do not search for further abbreviations
3041 * if this is an exact match. */
3042 matchlen = k;
3043 if (k == len && *np == NUL)
3044 {
3045 if (full != NULL)
3046 *full = TRUE;
3047 amb_local = FALSE;
3048 break;
3049 }
3050 }
3051 }
3052 }
3053
3054 /* Stop if we found a full match or searched all. */
3055 if (j < gap->ga_len || gap == &ucmds)
3056 break;
3057 gap = &ucmds;
3058 }
3059
3060 /* Only found ambiguous matches. */
3061 if (amb_local)
3062 {
3063 if (xp != NULL)
3064 xp->xp_context = EXPAND_UNSUCCESSFUL;
3065 return NULL;
3066 }
3067
3068 /* The match we found may be followed immediately by a number. Move "p"
3069 * back to point to it. */
3070 if (found || possible)
3071 return p + (matchlen - len);
3072 return p;
3073}
3074#endif
3075
Bram Moolenaar071d4272004-06-13 20:20:40 +00003076#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003077static struct cmdmod
3078{
3079 char *name;
3080 int minlen;
3081 int has_count; /* :123verbose :3tab */
3082} cmdmods[] = {
3083 {"aboveleft", 3, FALSE},
3084 {"belowright", 3, FALSE},
3085 {"botright", 2, FALSE},
3086 {"browse", 3, FALSE},
3087 {"confirm", 4, FALSE},
3088 {"hide", 3, FALSE},
3089 {"keepalt", 5, FALSE},
3090 {"keepjumps", 5, FALSE},
3091 {"keepmarks", 3, FALSE},
3092 {"leftabove", 5, FALSE},
3093 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003094 {"noautocmd", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003095 {"rightbelow", 6, FALSE},
3096 {"sandbox", 3, FALSE},
3097 {"silent", 3, FALSE},
3098 {"tab", 3, TRUE},
3099 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003100 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003101 {"verbose", 4, TRUE},
3102 {"vertical", 4, FALSE},
3103};
3104
3105/*
3106 * Return length of a command modifier (including optional count).
3107 * Return zero when it's not a modifier.
3108 */
3109 int
3110modifier_len(cmd)
3111 char_u *cmd;
3112{
3113 int i, j;
3114 char_u *p = cmd;
3115
3116 if (VIM_ISDIGIT(*cmd))
3117 p = skipwhite(skipdigits(cmd));
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003118 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003119 {
3120 for (j = 0; p[j] != NUL; ++j)
3121 if (p[j] != cmdmods[i].name[j])
3122 break;
Bram Moolenaar2d473ab2013-06-12 17:12:24 +02003123 if (!ASCII_ISALPHA(p[j]) && j >= cmdmods[i].minlen
Bram Moolenaared53fb92007-11-24 20:50:24 +00003124 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003125 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003126 }
3127 return 0;
3128}
3129
Bram Moolenaar071d4272004-06-13 20:20:40 +00003130/*
3131 * Return > 0 if an Ex command "name" exists.
3132 * Return 2 if there is an exact match.
3133 * Return 3 if there is an ambiguous match.
3134 */
3135 int
3136cmd_exists(name)
3137 char_u *name;
3138{
3139 exarg_T ea;
3140 int full = FALSE;
3141 int i;
3142 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003143 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003144
3145 /* Check command modifiers. */
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003146 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003147 {
3148 for (j = 0; name[j] != NUL; ++j)
3149 if (name[j] != cmdmods[i].name[j])
3150 break;
3151 if (name[j] == NUL && j >= cmdmods[i].minlen)
3152 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3153 }
3154
Bram Moolenaara9587612006-05-04 21:47:50 +00003155 /* Check built-in commands and user defined commands.
3156 * For ":2match" and ":3match" we need to skip the number. */
3157 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003158 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003159 p = find_command(&ea, &full);
3160 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003162 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3163 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003164 if (*skipwhite(p) != NUL)
3165 return 0; /* trailing garbage */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003166 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3167}
3168#endif
3169
3170/*
3171 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
3172 * we don't need/want deleted. Maybe this could be done better if we didn't
3173 * repeat all this stuff. The only problem is that they may not stay
3174 * perfectly compatible with each other, but then the command line syntax
3175 * probably won't change that much -- webb.
3176 */
3177 char_u *
3178set_one_cmd_context(xp, buff)
3179 expand_T *xp;
3180 char_u *buff; /* buffer for command string */
3181{
3182 char_u *p;
3183 char_u *cmd, *arg;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003184 int len = 0;
3185 exarg_T ea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003186#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3187 int compl = EXPAND_NOTHING;
3188#endif
3189#ifdef FEAT_CMDL_COMPL
3190 int delim;
3191#endif
3192 int forceit = FALSE;
3193 int usefilter = FALSE; /* filter instead of file name */
3194
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003195 ExpandInit(xp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003196 xp->xp_pattern = buff;
3197 xp->xp_context = EXPAND_COMMANDS; /* Default until we get past command */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003198 ea.argt = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003199
3200/*
3201 * 2. skip comment lines and leading space, colons or bars
3202 */
3203 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
3204 ;
3205 xp->xp_pattern = cmd;
3206
3207 if (*cmd == NUL)
3208 return NULL;
3209 if (*cmd == '"') /* ignore comment lines */
3210 {
3211 xp->xp_context = EXPAND_NOTHING;
3212 return NULL;
3213 }
3214
3215/*
3216 * 3. parse a range specifier of the form: addr [,addr] [;addr] ..
3217 */
3218 cmd = skip_range(cmd, &xp->xp_context);
3219
3220/*
3221 * 4. parse command
3222 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223 xp->xp_pattern = cmd;
3224 if (*cmd == NUL)
3225 return NULL;
3226 if (*cmd == '"')
3227 {
3228 xp->xp_context = EXPAND_NOTHING;
3229 return NULL;
3230 }
3231
3232 if (*cmd == '|' || *cmd == '\n')
3233 return cmd + 1; /* There's another command */
3234
3235 /*
3236 * Isolate the command and search for it in the command table.
3237 * Exceptions:
3238 * - the 'k' command can directly be followed by any character, but
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003239 * do accept "keepmarks", "keepalt" and "keepjumps".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003240 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
3241 */
3242 if (*cmd == 'k' && cmd[1] != 'e')
3243 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003244 ea.cmdidx = CMD_k;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003245 p = cmd + 1;
3246 }
3247 else
3248 {
3249 p = cmd;
3250 while (ASCII_ISALPHA(*p) || *p == '*') /* Allow * wild card */
3251 ++p;
3252 /* check for non-alpha command */
3253 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3254 ++p;
Bram Moolenaar9f5d6002013-06-02 19:22:13 +02003255 /* for python 3.x: ":py3*" commands completion */
3256 if (cmd[0] == 'p' && cmd[1] == 'y' && p == cmd + 2 && *p == '3')
3257 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003258 len = (int)(p - cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003259
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003260 if (len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003261 {
3262 xp->xp_context = EXPAND_UNSUCCESSFUL;
3263 return NULL;
3264 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003265 for (ea.cmdidx = (cmdidx_T)0; (int)ea.cmdidx < (int)CMD_SIZE;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01003266 ea.cmdidx = (cmdidx_T)((int)ea.cmdidx + 1))
3267 if (STRNCMP(cmdnames[(int)ea.cmdidx].cmd_name, cmd,
3268 (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003269 break;
3270
3271#ifdef FEAT_USR_CMDS
3272 if (cmd[0] >= 'A' && cmd[0] <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273 while (ASCII_ISALNUM(*p) || *p == '*') /* Allow * wild card */
3274 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275#endif
3276 }
3277
3278 /*
3279 * If the cursor is touching the command, and it ends in an alpha-numeric
3280 * character, complete the command name.
3281 */
3282 if (*p == NUL && ASCII_ISALNUM(p[-1]))
3283 return NULL;
3284
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003285 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003286 {
3287 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
3288 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003289 ea.cmdidx = CMD_substitute;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290 p = cmd + 1;
3291 }
3292#ifdef FEAT_USR_CMDS
3293 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
3294 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003295 ea.cmd = cmd;
3296 p = find_ucmd(&ea, p, NULL, xp,
3297# if defined(FEAT_CMDL_COMPL)
3298 &compl
3299# else
3300 NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003301# endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003302 );
Bram Moolenaarebefac62005-12-28 22:39:57 +00003303 if (p == NULL)
3304 ea.cmdidx = CMD_SIZE; /* ambiguous user command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305 }
3306#endif
3307 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003308 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309 {
3310 /* Not still touching the command and it was an illegal one */
3311 xp->xp_context = EXPAND_UNSUCCESSFUL;
3312 return NULL;
3313 }
3314
3315 xp->xp_context = EXPAND_NOTHING; /* Default now that we're past command */
3316
3317 if (*p == '!') /* forced commands */
3318 {
3319 forceit = TRUE;
3320 ++p;
3321 }
3322
3323/*
3324 * 5. parse arguments
3325 */
3326#ifdef FEAT_USR_CMDS
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003327 if (!USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328#endif
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003329 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003330
3331 arg = skipwhite(p);
3332
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003333 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334 {
3335 if (*arg == '>') /* append */
3336 {
3337 if (*++arg == '>')
3338 ++arg;
3339 arg = skipwhite(arg);
3340 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003341 else if (*arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342 {
3343 ++arg;
3344 usefilter = TRUE;
3345 }
3346 }
3347
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003348 if (ea.cmdidx == CMD_read)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349 {
3350 usefilter = forceit; /* :r! filter if forced */
3351 if (*arg == '!') /* :r !filter */
3352 {
3353 ++arg;
3354 usefilter = TRUE;
3355 }
3356 }
3357
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003358 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359 {
3360 while (*arg == *cmd) /* allow any number of '>' or '<' */
3361 ++arg;
3362 arg = skipwhite(arg);
3363 }
3364
3365 /* Does command allow "+command"? */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003366 if ((ea.argt & EDITCMD) && !usefilter && *arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367 {
3368 /* Check if we're in the +command */
3369 p = arg + 1;
3370 arg = skip_cmd_arg(arg, FALSE);
3371
3372 /* Still touching the command after '+'? */
3373 if (*arg == NUL)
3374 return p;
3375
3376 /* Skip space(s) after +command to get to the real argument */
3377 arg = skipwhite(arg);
3378 }
3379
3380 /*
3381 * Check for '|' to separate commands and '"' to start comments.
3382 * Don't do this for ":read !cmd" and ":write !cmd".
3383 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003384 if ((ea.argt & TRLBAR) && !usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385 {
3386 p = arg;
3387 /* ":redir @" is not the start of a comment */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003388 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389 p += 2;
3390 while (*p)
3391 {
3392 if (*p == Ctrl_V)
3393 {
3394 if (p[1] != NUL)
3395 ++p;
3396 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003397 else if ( (*p == '"' && !(ea.argt & NOTRLCOM))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398 || *p == '|' || *p == '\n')
3399 {
3400 if (*(p - 1) != '\\')
3401 {
3402 if (*p == '|' || *p == '\n')
3403 return p + 1;
3404 return NULL; /* It's a comment */
3405 }
3406 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003407 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408 }
3409 }
3410
3411 /* no arguments allowed */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003412 if (!(ea.argt & EXTRA) && *arg != NUL &&
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413 vim_strchr((char_u *)"|\"", *arg) == NULL)
3414 return NULL;
3415
3416 /* Find start of last argument (argument just before cursor): */
Bram Moolenaar848f8762012-07-25 17:22:23 +02003417 p = buff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418 xp->xp_pattern = p;
Bram Moolenaar09168a72012-08-02 21:24:42 +02003419 len = (int)STRLEN(buff);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003420 while (*p && p < buff + len)
3421 {
3422 if (*p == ' ' || *p == TAB)
3423 {
3424 /* argument starts after a space */
3425 xp->xp_pattern = ++p;
3426 }
3427 else
3428 {
3429 if (*p == '\\' && *(p + 1) != NUL)
3430 ++p; /* skip over escaped character */
3431 mb_ptr_adv(p);
3432 }
3433 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003435 if (ea.argt & XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003437 int c;
3438 int in_quote = FALSE;
3439 char_u *bow = NULL; /* Beginning of word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440
3441 /*
3442 * Allow spaces within back-quotes to count as part of the argument
3443 * being expanded.
3444 */
3445 xp->xp_pattern = skipwhite(arg);
Bram Moolenaar6529c102007-08-18 15:47:34 +00003446 p = xp->xp_pattern;
3447 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003449#ifdef FEAT_MBYTE
3450 if (has_mbyte)
3451 c = mb_ptr2char(p);
3452 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453#endif
Bram Moolenaar6529c102007-08-18 15:47:34 +00003454 c = *p;
3455 if (c == '\\' && p[1] != NUL)
3456 ++p;
3457 else if (c == '`')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458 {
3459 if (!in_quote)
3460 {
3461 xp->xp_pattern = p;
3462 bow = p + 1;
3463 }
3464 in_quote = !in_quote;
3465 }
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003466 /* An argument can contain just about everything, except
3467 * characters that end the command and white space. */
3468 else if (c == '|' || c == '\n' || c == '"' || (vim_iswhite(c)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003469#ifdef SPACE_IN_FILENAME
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003470 && (!(ea.argt & NOSPC) || usefilter)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003471#endif
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003472 ))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003473 {
Bram Moolenaarcf5a5b82008-02-26 20:30:12 +00003474 len = 0; /* avoid getting stuck when space is in 'isfname' */
Bram Moolenaar6529c102007-08-18 15:47:34 +00003475 while (*p != NUL)
3476 {
3477#ifdef FEAT_MBYTE
3478 if (has_mbyte)
3479 c = mb_ptr2char(p);
3480 else
3481#endif
3482 c = *p;
Bram Moolenaardd87969c2007-08-21 13:07:12 +00003483 if (c == '`' || vim_isfilec_or_wc(c))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003484 break;
3485#ifdef FEAT_MBYTE
3486 if (has_mbyte)
3487 len = (*mb_ptr2len)(p);
3488 else
3489#endif
3490 len = 1;
3491 mb_ptr_adv(p);
3492 }
3493 if (in_quote)
3494 bow = p;
3495 else
3496 xp->xp_pattern = p;
3497 p -= len;
3498 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003499 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500 }
3501
3502 /*
3503 * If we are still inside the quotes, and we passed a space, just
3504 * expand from there.
3505 */
3506 if (bow != NULL && in_quote)
3507 xp->xp_pattern = bow;
3508 xp->xp_context = EXPAND_FILES;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003509
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003510 /* For a shell command more chars need to be escaped. */
3511 if (usefilter || ea.cmdidx == CMD_bang)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003512 {
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003513#ifndef BACKSLASH_IN_FILENAME
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003514 xp->xp_shell = TRUE;
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003515#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003516 /* When still after the command name expand executables. */
3517 if (xp->xp_pattern == skipwhite(arg))
Bram Moolenaar5c5b0942007-05-06 12:07:59 +00003518 xp->xp_context = EXPAND_SHELLCMD;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003519 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520
3521 /* Check for environment variable */
3522 if (*xp->xp_pattern == '$'
3523#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
3524 || *xp->xp_pattern == '%'
3525#endif
3526 )
3527 {
3528 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
3529 if (!vim_isIDc(*p))
3530 break;
3531 if (*p == NUL)
3532 {
3533 xp->xp_context = EXPAND_ENV_VARS;
3534 ++xp->xp_pattern;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003535#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3536 /* Avoid that the assignment uses EXPAND_FILES again. */
Bram Moolenaara466c992005-07-09 21:03:22 +00003537 if (compl != EXPAND_USER_DEFINED && compl != EXPAND_USER_LIST)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003538 compl = EXPAND_ENV_VARS;
3539#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540 }
3541 }
Bram Moolenaar24305862012-08-15 14:05:05 +02003542#if defined(FEAT_CMDL_COMPL)
3543 /* Check for user names */
3544 if (*xp->xp_pattern == '~')
3545 {
3546 for (p = xp->xp_pattern + 1; *p != NUL && *p != '/'; ++p)
3547 ;
3548 /* Complete ~user only if it partially matches a user name.
3549 * A full match ~user<Tab> will be replaced by user's home
3550 * directory i.e. something like ~user<Tab> -> /home/user/ */
3551 if (*p == NUL && p > xp->xp_pattern + 1
3552 && match_user(xp->xp_pattern + 1) == 1)
3553 {
3554 xp->xp_context = EXPAND_USER;
3555 ++xp->xp_pattern;
3556 }
3557 }
3558#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559 }
3560
3561/*
3562 * 6. switch on command name
3563 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003564 switch (ea.cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003566 case CMD_find:
3567 case CMD_sfind:
3568 case CMD_tabfind:
Bram Moolenaarc24b6972010-08-16 22:34:29 +02003569 if (xp->xp_context == EXPAND_FILES)
3570 xp->xp_context = EXPAND_FILES_IN_PATH;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003571 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003572 case CMD_cd:
3573 case CMD_chdir:
3574 case CMD_lcd:
3575 case CMD_lchdir:
3576 if (xp->xp_context == EXPAND_FILES)
3577 xp->xp_context = EXPAND_DIRECTORIES;
3578 break;
3579 case CMD_help:
3580 xp->xp_context = EXPAND_HELP;
3581 xp->xp_pattern = arg;
3582 break;
3583
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003584 /* Command modifiers: return the argument.
3585 * Also for commands with an argument that is a command. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586 case CMD_aboveleft:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003587 case CMD_argdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588 case CMD_belowright:
3589 case CMD_botright:
3590 case CMD_browse:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003591 case CMD_bufdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003592 case CMD_confirm:
Bram Moolenaardf177f62005-02-22 08:39:57 +00003593 case CMD_debug:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594 case CMD_folddoclosed:
3595 case CMD_folddoopen:
3596 case CMD_hide:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003597 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598 case CMD_keepjumps:
3599 case CMD_keepmarks:
3600 case CMD_leftabove:
3601 case CMD_lockmarks:
3602 case CMD_rightbelow:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003603 case CMD_sandbox:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604 case CMD_silent:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003605 case CMD_tab:
Bram Moolenaar70baa402013-06-16 16:14:03 +02003606 case CMD_tabdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607 case CMD_topleft:
3608 case CMD_verbose:
3609 case CMD_vertical:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003610 case CMD_windo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611 return arg;
3612
Bram Moolenaar4f688582007-07-24 12:34:30 +00003613#ifdef FEAT_CMDL_COMPL
3614# ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar071d4272004-06-13 20:20:40 +00003615 case CMD_match:
3616 if (*arg == NUL || !ends_excmd(*arg))
3617 {
Bram Moolenaar4f688582007-07-24 12:34:30 +00003618 /* also complete "None" */
3619 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003620 arg = skipwhite(skiptowhite(arg));
3621 if (*arg != NUL)
3622 {
3623 xp->xp_context = EXPAND_NOTHING;
3624 arg = skip_regexp(arg + 1, *arg, p_magic, NULL);
3625 }
3626 }
3627 return find_nextcmd(arg);
Bram Moolenaar4f688582007-07-24 12:34:30 +00003628# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630/*
3631 * All completion for the +cmdline_compl feature goes here.
3632 */
3633
3634# ifdef FEAT_USR_CMDS
3635 case CMD_command:
3636 /* Check for attributes */
3637 while (*arg == '-')
3638 {
3639 arg++; /* Skip "-" */
3640 p = skiptowhite(arg);
3641 if (*p == NUL)
3642 {
3643 /* Cursor is still in the attribute */
3644 p = vim_strchr(arg, '=');
3645 if (p == NULL)
3646 {
3647 /* No "=", so complete attribute names */
3648 xp->xp_context = EXPAND_USER_CMD_FLAGS;
3649 xp->xp_pattern = arg;
3650 return NULL;
3651 }
3652
3653 /* For the -complete and -nargs attributes, we complete
3654 * their arguments as well.
3655 */
3656 if (STRNICMP(arg, "complete", p - arg) == 0)
3657 {
3658 xp->xp_context = EXPAND_USER_COMPLETE;
3659 xp->xp_pattern = p + 1;
3660 return NULL;
3661 }
3662 else if (STRNICMP(arg, "nargs", p - arg) == 0)
3663 {
3664 xp->xp_context = EXPAND_USER_NARGS;
3665 xp->xp_pattern = p + 1;
3666 return NULL;
3667 }
3668 return NULL;
3669 }
3670 arg = skipwhite(p);
3671 }
3672
3673 /* After the attributes comes the new command name */
3674 p = skiptowhite(arg);
3675 if (*p == NUL)
3676 {
3677 xp->xp_context = EXPAND_USER_COMMANDS;
3678 xp->xp_pattern = arg;
3679 break;
3680 }
3681
3682 /* And finally comes a normal command */
3683 return skipwhite(p);
3684
3685 case CMD_delcommand:
3686 xp->xp_context = EXPAND_USER_COMMANDS;
3687 xp->xp_pattern = arg;
3688 break;
3689# endif
3690
3691 case CMD_global:
3692 case CMD_vglobal:
3693 delim = *arg; /* get the delimiter */
3694 if (delim)
3695 ++arg; /* skip delimiter if there is one */
3696
3697 while (arg[0] != NUL && arg[0] != delim)
3698 {
3699 if (arg[0] == '\\' && arg[1] != NUL)
3700 ++arg;
3701 ++arg;
3702 }
3703 if (arg[0] != NUL)
3704 return arg + 1;
3705 break;
3706 case CMD_and:
3707 case CMD_substitute:
3708 delim = *arg;
3709 if (delim)
3710 {
3711 /* skip "from" part */
3712 ++arg;
3713 arg = skip_regexp(arg, delim, p_magic, NULL);
3714 }
3715 /* skip "to" part */
3716 while (arg[0] != NUL && arg[0] != delim)
3717 {
3718 if (arg[0] == '\\' && arg[1] != NUL)
3719 ++arg;
3720 ++arg;
3721 }
3722 if (arg[0] != NUL) /* skip delimiter */
3723 ++arg;
3724 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
3725 ++arg;
3726 if (arg[0] != NUL)
3727 return arg;
3728 break;
3729 case CMD_isearch:
3730 case CMD_dsearch:
3731 case CMD_ilist:
3732 case CMD_dlist:
3733 case CMD_ijump:
3734 case CMD_psearch:
3735 case CMD_djump:
3736 case CMD_isplit:
3737 case CMD_dsplit:
3738 arg = skipwhite(skipdigits(arg)); /* skip count */
3739 if (*arg == '/') /* Match regexp, not just whole words */
3740 {
3741 for (++arg; *arg && *arg != '/'; arg++)
3742 if (*arg == '\\' && arg[1] != NUL)
3743 arg++;
3744 if (*arg)
3745 {
3746 arg = skipwhite(arg + 1);
3747
3748 /* Check for trailing illegal characters */
3749 if (*arg && vim_strchr((char_u *)"|\"\n", *arg) == NULL)
3750 xp->xp_context = EXPAND_NOTHING;
3751 else
3752 return arg;
3753 }
3754 }
3755 break;
3756#ifdef FEAT_AUTOCMD
3757 case CMD_autocmd:
3758 return set_context_in_autocmd(xp, arg, FALSE);
3759
3760 case CMD_doautocmd:
Bram Moolenaar73a9d7b2008-11-06 16:16:44 +00003761 case CMD_doautoall:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003762 return set_context_in_autocmd(xp, arg, TRUE);
3763#endif
3764 case CMD_set:
3765 set_context_in_set_cmd(xp, arg, 0);
3766 break;
3767 case CMD_setglobal:
3768 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
3769 break;
3770 case CMD_setlocal:
3771 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
3772 break;
3773 case CMD_tag:
3774 case CMD_stag:
3775 case CMD_ptag:
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00003776 case CMD_ltag:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003777 case CMD_tselect:
3778 case CMD_stselect:
3779 case CMD_ptselect:
3780 case CMD_tjump:
3781 case CMD_stjump:
3782 case CMD_ptjump:
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003783 if (*p_wop != NUL)
3784 xp->xp_context = EXPAND_TAGS_LISTFILES;
3785 else
3786 xp->xp_context = EXPAND_TAGS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003787 xp->xp_pattern = arg;
3788 break;
3789 case CMD_augroup:
3790 xp->xp_context = EXPAND_AUGROUP;
3791 xp->xp_pattern = arg;
3792 break;
3793#ifdef FEAT_SYN_HL
3794 case CMD_syntax:
3795 set_context_in_syntax_cmd(xp, arg);
3796 break;
3797#endif
3798#ifdef FEAT_EVAL
3799 case CMD_let:
3800 case CMD_if:
3801 case CMD_elseif:
3802 case CMD_while:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00003803 case CMD_for:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804 case CMD_echo:
3805 case CMD_echon:
3806 case CMD_execute:
3807 case CMD_echomsg:
3808 case CMD_echoerr:
3809 case CMD_call:
3810 case CMD_return:
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003811 set_context_for_expression(xp, arg, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003812 break;
3813
3814 case CMD_unlet:
3815 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3816 arg = xp->xp_pattern + 1;
3817 xp->xp_context = EXPAND_USER_VARS;
3818 xp->xp_pattern = arg;
3819 break;
3820
3821 case CMD_function:
3822 case CMD_delfunction:
3823 xp->xp_context = EXPAND_USER_FUNC;
3824 xp->xp_pattern = arg;
3825 break;
3826
3827 case CMD_echohl:
Bram Moolenaar4f688582007-07-24 12:34:30 +00003828 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003829 break;
3830#endif
3831 case CMD_highlight:
3832 set_context_in_highlight_cmd(xp, arg);
3833 break;
Bram Moolenaarf4580d82009-03-18 11:52:53 +00003834#ifdef FEAT_CSCOPE
3835 case CMD_cscope:
Bram Moolenaar7bfef802009-04-22 14:25:01 +00003836 case CMD_lcscope:
3837 case CMD_scscope:
3838 set_context_in_cscope_cmd(xp, arg, ea.cmdidx);
Bram Moolenaarf4580d82009-03-18 11:52:53 +00003839 break;
3840#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00003841#ifdef FEAT_SIGNS
3842 case CMD_sign:
3843 set_context_in_sign_cmd(xp, arg);
3844 break;
3845#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003846#ifdef FEAT_LISTCMDS
3847 case CMD_bdelete:
3848 case CMD_bwipeout:
3849 case CMD_bunload:
3850 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3851 arg = xp->xp_pattern + 1;
3852 /*FALLTHROUGH*/
3853 case CMD_buffer:
3854 case CMD_sbuffer:
3855 case CMD_checktime:
3856 xp->xp_context = EXPAND_BUFFERS;
3857 xp->xp_pattern = arg;
3858 break;
3859#endif
3860#ifdef FEAT_USR_CMDS
3861 case CMD_USER:
3862 case CMD_USER_BUF:
3863 if (compl != EXPAND_NOTHING)
3864 {
3865 /* XFILE: file names are handled above */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003866 if (!(ea.argt & XFILE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867 {
3868# ifdef FEAT_MENU
3869 if (compl == EXPAND_MENUS)
3870 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
3871# endif
3872 if (compl == EXPAND_COMMANDS)
3873 return arg;
3874 if (compl == EXPAND_MAPPINGS)
3875 return set_context_in_map_cmd(xp, (char_u *)"map",
3876 arg, forceit, FALSE, FALSE, CMD_map);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003877 /* Find start of last argument. */
3878 p = arg;
3879 while (*p)
3880 {
3881 if (*p == ' ')
Bram Moolenaar848f8762012-07-25 17:22:23 +02003882 /* argument starts after a space */
3883 arg = p + 1;
Bram Moolenaara07c8312012-07-27 21:12:07 +02003884 else if (*p == '\\' && *(p + 1) != NUL)
3885 ++p; /* skip over escaped character */
3886 mb_ptr_adv(p);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003887 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888 xp->xp_pattern = arg;
3889 }
3890 xp->xp_context = compl;
3891 }
3892 break;
3893#endif
3894 case CMD_map: case CMD_noremap:
3895 case CMD_nmap: case CMD_nnoremap:
3896 case CMD_vmap: case CMD_vnoremap:
3897 case CMD_omap: case CMD_onoremap:
3898 case CMD_imap: case CMD_inoremap:
3899 case CMD_cmap: case CMD_cnoremap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003900 case CMD_lmap: case CMD_lnoremap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02003901 case CMD_smap: case CMD_snoremap:
3902 case CMD_xmap: case CMD_xnoremap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003903 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003904 FALSE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003905 case CMD_unmap:
3906 case CMD_nunmap:
3907 case CMD_vunmap:
3908 case CMD_ounmap:
3909 case CMD_iunmap:
3910 case CMD_cunmap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003911 case CMD_lunmap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02003912 case CMD_sunmap:
3913 case CMD_xunmap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003914 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003915 FALSE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003916 case CMD_abbreviate: case CMD_noreabbrev:
3917 case CMD_cabbrev: case CMD_cnoreabbrev:
3918 case CMD_iabbrev: case CMD_inoreabbrev:
3919 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003920 TRUE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003921 case CMD_unabbreviate:
3922 case CMD_cunabbrev:
3923 case CMD_iunabbrev:
3924 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003925 TRUE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926#ifdef FEAT_MENU
3927 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
3928 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
3929 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
3930 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
3931 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
3932 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
3933 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
3934 case CMD_tmenu: case CMD_tunmenu:
3935 case CMD_popup: case CMD_tearoff: case CMD_emenu:
3936 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
3937#endif
3938
3939 case CMD_colorscheme:
3940 xp->xp_context = EXPAND_COLORS;
3941 xp->xp_pattern = arg;
3942 break;
3943
3944 case CMD_compiler:
3945 xp->xp_context = EXPAND_COMPILER;
3946 xp->xp_pattern = arg;
3947 break;
3948
Bram Moolenaar883f5d02010-06-21 06:24:34 +02003949 case CMD_ownsyntax:
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02003950 xp->xp_context = EXPAND_OWNSYNTAX;
3951 xp->xp_pattern = arg;
3952 break;
3953
3954 case CMD_setfiletype:
Bram Moolenaar883f5d02010-06-21 06:24:34 +02003955 xp->xp_context = EXPAND_FILETYPE;
3956 xp->xp_pattern = arg;
3957 break;
3958
Bram Moolenaar071d4272004-06-13 20:20:40 +00003959#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
3960 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
3961 case CMD_language:
Bram Moolenaara660dc82011-05-25 12:51:22 +02003962 p = skiptowhite(arg);
3963 if (*p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003964 {
3965 xp->xp_context = EXPAND_LANGUAGE;
3966 xp->xp_pattern = arg;
3967 }
3968 else
Bram Moolenaara660dc82011-05-25 12:51:22 +02003969 {
3970 if ( STRNCMP(arg, "messages", p - arg) == 0
3971 || STRNCMP(arg, "ctype", p - arg) == 0
3972 || STRNCMP(arg, "time", p - arg) == 0)
3973 {
3974 xp->xp_context = EXPAND_LOCALES;
3975 xp->xp_pattern = skipwhite(p);
3976 }
3977 else
3978 xp->xp_context = EXPAND_NOTHING;
3979 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003980 break;
3981#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01003982#if defined(FEAT_PROFILE)
3983 case CMD_profile:
3984 set_context_in_profile_cmd(xp, arg);
3985 break;
3986#endif
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01003987 case CMD_behave:
3988 xp->xp_context = EXPAND_BEHAVE;
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02003989 xp->xp_pattern = arg;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01003990 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02003992#if defined(FEAT_CMDHIST)
3993 case CMD_history:
3994 xp->xp_context = EXPAND_HISTORY;
3995 xp->xp_pattern = arg;
3996 break;
3997#endif
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02003998#if defined(FEAT_PROFILE)
3999 case CMD_syntime:
4000 xp->xp_context = EXPAND_SYNTIME;
4001 xp->xp_pattern = arg;
4002 break;
4003#endif
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004004
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005#endif /* FEAT_CMDL_COMPL */
4006
4007 default:
4008 break;
4009 }
4010 return NULL;
4011}
4012
4013/*
4014 * skip a range specifier of the form: addr [,addr] [;addr] ..
4015 *
4016 * Backslashed delimiters after / or ? will be skipped, and commands will
4017 * not be expanded between /'s and ?'s or after "'".
4018 *
Bram Moolenaardf177f62005-02-22 08:39:57 +00004019 * Also skip white space and ":" characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004020 * Returns the "cmd" pointer advanced to beyond the range.
4021 */
4022 char_u *
4023skip_range(cmd, ctx)
4024 char_u *cmd;
4025 int *ctx; /* pointer to xp_context or NULL */
4026{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004027 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028
Bram Moolenaardf177f62005-02-22 08:39:57 +00004029 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004030 {
4031 if (*cmd == '\'')
4032 {
4033 if (*++cmd == NUL && ctx != NULL)
4034 *ctx = EXPAND_NOTHING;
4035 }
4036 else if (*cmd == '/' || *cmd == '?')
4037 {
4038 delim = *cmd++;
4039 while (*cmd != NUL && *cmd != delim)
4040 if (*cmd++ == '\\' && *cmd != NUL)
4041 ++cmd;
4042 if (*cmd == NUL && ctx != NULL)
4043 *ctx = EXPAND_NOTHING;
4044 }
4045 if (*cmd != NUL)
4046 ++cmd;
4047 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004048
4049 /* Skip ":" and white space. */
4050 while (*cmd == ':')
4051 cmd = skipwhite(cmd + 1);
4052
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053 return cmd;
4054}
4055
4056/*
4057 * get a single EX address
4058 *
4059 * Set ptr to the next character after the part that was interpreted.
4060 * Set ptr to NULL when an error is encountered.
4061 *
4062 * Return MAXLNUM when no Ex address was found.
4063 */
4064 static linenr_T
4065get_address(ptr, skip, to_other_file)
4066 char_u **ptr;
4067 int skip; /* only skip the address, don't use it */
4068 int to_other_file; /* flag: may jump to other file */
4069{
4070 int c;
4071 int i;
4072 long n;
4073 char_u *cmd;
4074 pos_T pos;
4075 pos_T *fp;
4076 linenr_T lnum;
4077
4078 cmd = skipwhite(*ptr);
4079 lnum = MAXLNUM;
4080 do
4081 {
4082 switch (*cmd)
4083 {
4084 case '.': /* '.' - Cursor position */
4085 ++cmd;
4086 lnum = curwin->w_cursor.lnum;
4087 break;
4088
4089 case '$': /* '$' - last line */
4090 ++cmd;
4091 lnum = curbuf->b_ml.ml_line_count;
4092 break;
4093
4094 case '\'': /* ''' - mark */
4095 if (*++cmd == NUL)
4096 {
4097 cmd = NULL;
4098 goto error;
4099 }
4100 if (skip)
4101 ++cmd;
4102 else
4103 {
4104 /* Only accept a mark in another file when it is
4105 * used by itself: ":'M". */
4106 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
4107 ++cmd;
4108 if (fp == (pos_T *)-1)
4109 /* Jumped to another file. */
4110 lnum = curwin->w_cursor.lnum;
4111 else
4112 {
4113 if (check_mark(fp) == FAIL)
4114 {
4115 cmd = NULL;
4116 goto error;
4117 }
4118 lnum = fp->lnum;
4119 }
4120 }
4121 break;
4122
4123 case '/':
4124 case '?': /* '/' or '?' - search */
4125 c = *cmd++;
4126 if (skip) /* skip "/pat/" */
4127 {
4128 cmd = skip_regexp(cmd, c, (int)p_magic, NULL);
4129 if (*cmd == c)
4130 ++cmd;
4131 }
4132 else
4133 {
4134 pos = curwin->w_cursor; /* save curwin->w_cursor */
4135 /*
4136 * When '/' or '?' follows another address, start
4137 * from there.
4138 */
4139 if (lnum != MAXLNUM)
4140 curwin->w_cursor.lnum = lnum;
4141 /*
4142 * Start a forward search at the end of the line.
4143 * Start a backward search at the start of the line.
4144 * This makes sure we never match in the current
4145 * line, and can match anywhere in the
4146 * next/previous line.
4147 */
4148 if (c == '/')
4149 curwin->w_cursor.col = MAXCOL;
4150 else
4151 curwin->w_cursor.col = 0;
4152 searchcmdlen = 0;
4153 if (!do_search(NULL, c, cmd, 1L,
Bram Moolenaaraad86642008-03-10 20:34:59 +00004154 SEARCH_HIS | SEARCH_MSG, NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004155 {
4156 curwin->w_cursor = pos;
4157 cmd = NULL;
4158 goto error;
4159 }
4160 lnum = curwin->w_cursor.lnum;
4161 curwin->w_cursor = pos;
4162 /* adjust command string pointer */
4163 cmd += searchcmdlen;
4164 }
4165 break;
4166
4167 case '\\': /* "\?", "\/" or "\&", repeat search */
4168 ++cmd;
4169 if (*cmd == '&')
4170 i = RE_SUBST;
4171 else if (*cmd == '?' || *cmd == '/')
4172 i = RE_SEARCH;
4173 else
4174 {
4175 EMSG(_(e_backslash));
4176 cmd = NULL;
4177 goto error;
4178 }
4179
4180 if (!skip)
4181 {
4182 /*
4183 * When search follows another address, start from
4184 * there.
4185 */
4186 if (lnum != MAXLNUM)
4187 pos.lnum = lnum;
4188 else
4189 pos.lnum = curwin->w_cursor.lnum;
4190
4191 /*
4192 * Start the search just like for the above
4193 * do_search().
4194 */
4195 if (*cmd != '?')
4196 pos.col = MAXCOL;
4197 else
4198 pos.col = 0;
4199 if (searchit(curwin, curbuf, &pos,
4200 *cmd == '?' ? BACKWARD : FORWARD,
Bram Moolenaaraad86642008-03-10 20:34:59 +00004201 (char_u *)"", 1L, SEARCH_MSG,
Bram Moolenaar76929292008-01-06 19:07:36 +00004202 i, (linenr_T)0, NULL) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203 lnum = pos.lnum;
4204 else
4205 {
4206 cmd = NULL;
4207 goto error;
4208 }
4209 }
4210 ++cmd;
4211 break;
4212
4213 default:
4214 if (VIM_ISDIGIT(*cmd)) /* absolute line number */
4215 lnum = getdigits(&cmd);
4216 }
4217
4218 for (;;)
4219 {
4220 cmd = skipwhite(cmd);
4221 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4222 break;
4223
4224 if (lnum == MAXLNUM)
4225 lnum = curwin->w_cursor.lnum; /* "+1" is same as ".+1" */
4226 if (VIM_ISDIGIT(*cmd))
4227 i = '+'; /* "number" is same as "+number" */
4228 else
4229 i = *cmd++;
4230 if (!VIM_ISDIGIT(*cmd)) /* '+' is '+1', but '+0' is not '+1' */
4231 n = 1;
4232 else
4233 n = getdigits(&cmd);
4234 if (i == '-')
4235 lnum -= n;
4236 else
4237 lnum += n;
4238 }
4239 } while (*cmd == '/' || *cmd == '?');
4240
4241error:
4242 *ptr = cmd;
4243 return lnum;
4244}
4245
4246/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004247 * Get flags from an Ex command argument.
4248 */
4249 static void
4250get_flags(eap)
4251 exarg_T *eap;
4252{
4253 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4254 {
4255 if (*eap->arg == 'l')
4256 eap->flags |= EXFLAG_LIST;
4257 else if (*eap->arg == 'p')
4258 eap->flags |= EXFLAG_PRINT;
4259 else
4260 eap->flags |= EXFLAG_NR;
4261 eap->arg = skipwhite(eap->arg + 1);
4262 }
4263}
4264
4265/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004266 * Function called for command which is Not Implemented. NI!
4267 */
4268 void
4269ex_ni(eap)
4270 exarg_T *eap;
4271{
4272 if (!eap->skip)
4273 eap->errmsg = (char_u *)N_("E319: Sorry, the command is not available in this version");
4274}
4275
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004276#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277/*
4278 * Function called for script command which is Not Implemented. NI!
4279 * Skips over ":perl <<EOF" constructs.
4280 */
4281 static void
4282ex_script_ni(eap)
4283 exarg_T *eap;
4284{
4285 if (!eap->skip)
4286 ex_ni(eap);
4287 else
4288 vim_free(script_get(eap, eap->arg));
4289}
4290#endif
4291
4292/*
4293 * Check range in Ex command for validity.
4294 * Return NULL when valid, error message when invalid.
4295 */
4296 static char_u *
4297invalid_range(eap)
4298 exarg_T *eap;
4299{
4300 if ( eap->line1 < 0
4301 || eap->line2 < 0
4302 || eap->line1 > eap->line2
4303 || ((eap->argt & RANGE)
4304 && !(eap->argt & NOTADR)
4305 && eap->line2 > curbuf->b_ml.ml_line_count
4306#ifdef FEAT_DIFF
4307 + (eap->cmdidx == CMD_diffget)
4308#endif
4309 ))
4310 return (char_u *)_(e_invrange);
4311 return NULL;
4312}
4313
4314/*
4315 * Correct the range for zero line number, if required.
4316 */
4317 static void
4318correct_range(eap)
4319 exarg_T *eap;
4320{
4321 if (!(eap->argt & ZEROR)) /* zero in range not allowed */
4322 {
4323 if (eap->line1 == 0)
4324 eap->line1 = 1;
4325 if (eap->line2 == 0)
4326 eap->line2 = 1;
4327 }
4328}
4329
Bram Moolenaar748bf032005-02-02 23:04:36 +00004330#ifdef FEAT_QUICKFIX
4331static char_u *skip_grep_pat __ARGS((exarg_T *eap));
4332
4333/*
4334 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4335 * pattern. Otherwise return eap->arg.
4336 */
4337 static char_u *
4338skip_grep_pat(eap)
4339 exarg_T *eap;
4340{
4341 char_u *p = eap->arg;
4342
Bram Moolenaara37420f2006-02-04 22:37:47 +00004343 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4344 || eap->cmdidx == CMD_vimgrepadd
4345 || eap->cmdidx == CMD_lvimgrepadd
4346 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004347 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004348 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004349 if (p == NULL)
4350 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004351 }
4352 return p;
4353}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004354
4355/*
4356 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4357 * in the command line, so that things like % get expanded.
4358 */
4359 static char_u *
4360replace_makeprg(eap, p, cmdlinep)
4361 exarg_T *eap;
4362 char_u *p;
4363 char_u **cmdlinep;
4364{
4365 char_u *new_cmdline;
4366 char_u *program;
4367 char_u *pos;
4368 char_u *ptr;
4369 int len;
4370 int i;
4371
4372 /*
4373 * Don't do it when ":vimgrep" is used for ":grep".
4374 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004375 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4376 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4377 || eap->cmdidx == CMD_grepadd
4378 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004379 && !grep_internal(eap->cmdidx))
4380 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004381 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4382 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004383 {
4384 if (*curbuf->b_p_gp == NUL)
4385 program = p_gp;
4386 else
4387 program = curbuf->b_p_gp;
4388 }
4389 else
4390 {
4391 if (*curbuf->b_p_mp == NUL)
4392 program = p_mp;
4393 else
4394 program = curbuf->b_p_mp;
4395 }
4396
4397 p = skipwhite(p);
4398
4399 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4400 {
4401 /* replace $* by given arguments */
4402 i = 1;
4403 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4404 ++i;
4405 len = (int)STRLEN(p);
4406 new_cmdline = alloc((int)(STRLEN(program) + i * (len - 2) + 1));
4407 if (new_cmdline == NULL)
4408 return NULL; /* out of memory */
4409 ptr = new_cmdline;
4410 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4411 {
4412 i = (int)(pos - program);
4413 STRNCPY(ptr, program, i);
4414 STRCPY(ptr += i, p);
4415 ptr += len;
4416 program = pos + 2;
4417 }
4418 STRCPY(ptr, program);
4419 }
4420 else
4421 {
4422 new_cmdline = alloc((int)(STRLEN(program) + STRLEN(p) + 2));
4423 if (new_cmdline == NULL)
4424 return NULL; /* out of memory */
4425 STRCPY(new_cmdline, program);
4426 STRCAT(new_cmdline, " ");
4427 STRCAT(new_cmdline, p);
4428 }
4429 msg_make(p);
4430
4431 /* 'eap->cmd' is not set here, because it is not used at CMD_make */
4432 vim_free(*cmdlinep);
4433 *cmdlinep = new_cmdline;
4434 p = new_cmdline;
4435 }
4436 return p;
4437}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004438#endif
4439
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440/*
4441 * Expand file name in Ex command argument.
4442 * Return FAIL for failure, OK otherwise.
4443 */
4444 int
4445expand_filename(eap, cmdlinep, errormsgp)
4446 exarg_T *eap;
4447 char_u **cmdlinep;
4448 char_u **errormsgp;
4449{
4450 int has_wildcards; /* need to expand wildcards */
4451 char_u *repl;
4452 int srclen;
4453 char_u *p;
4454 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004455 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004456
Bram Moolenaar748bf032005-02-02 23:04:36 +00004457#ifdef FEAT_QUICKFIX
4458 /* Skip a regexp pattern for ":vimgrep[add] pat file..." */
4459 p = skip_grep_pat(eap);
4460#else
4461 p = eap->arg;
4462#endif
4463
Bram Moolenaar071d4272004-06-13 20:20:40 +00004464 /*
4465 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4466 * the file name contains a wildcard it should not cause expanding.
4467 * (it will be expanded anyway if there is a wildcard before replacing).
4468 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004469 has_wildcards = mch_has_wildcard(p);
4470 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004471 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004472#ifdef FEAT_EVAL
4473 /* Skip over `=expr`, wildcards in it are not expanded. */
4474 if (p[0] == '`' && p[1] == '=')
4475 {
4476 p += 2;
4477 (void)skip_expr(&p);
4478 if (*p == '`')
4479 ++p;
4480 continue;
4481 }
4482#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004483 /*
4484 * Quick check if this cannot be the start of a special string.
4485 * Also removes backslash before '%', '#' and '<'.
4486 */
4487 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4488 {
4489 ++p;
4490 continue;
4491 }
4492
4493 /*
4494 * Try to find a match at this position.
4495 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004496 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
4497 errormsgp, &escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004498 if (*errormsgp != NULL) /* error detected */
4499 return FAIL;
4500 if (repl == NULL) /* no match found */
4501 {
4502 p += srclen;
4503 continue;
4504 }
4505
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004506 /* Wildcards won't be expanded below, the replacement is taken
4507 * literally. But do expand "~/file", "~user/file" and "$HOME/file". */
4508 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4509 {
4510 char_u *l = repl;
4511
4512 repl = expand_env_save(repl);
4513 vim_free(l);
4514 }
4515
Bram Moolenaar63b92542007-03-27 14:57:09 +00004516 /* Need to escape white space et al. with a backslash.
4517 * Don't do this for:
4518 * - replacement that already has been escaped: "##"
4519 * - shell commands (may have to use quotes instead).
4520 * - non-unix systems when there is a single argument (spaces don't
4521 * separate arguments then).
4522 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004523 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00004524 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525 && eap->cmdidx != CMD_bang
4526 && eap->cmdidx != CMD_make
Bram Moolenaara37420f2006-02-04 22:37:47 +00004527 && eap->cmdidx != CMD_lmake
Bram Moolenaar071d4272004-06-13 20:20:40 +00004528 && eap->cmdidx != CMD_grep
Bram Moolenaara37420f2006-02-04 22:37:47 +00004529 && eap->cmdidx != CMD_lgrep
Bram Moolenaar071d4272004-06-13 20:20:40 +00004530 && eap->cmdidx != CMD_grepadd
Bram Moolenaara37420f2006-02-04 22:37:47 +00004531 && eap->cmdidx != CMD_lgrepadd
Bram Moolenaar071d4272004-06-13 20:20:40 +00004532#ifndef UNIX
4533 && !(eap->argt & NOSPC)
4534#endif
4535 )
4536 {
4537 char_u *l;
4538#ifdef BACKSLASH_IN_FILENAME
4539 /* Don't escape a backslash here, because rem_backslash() doesn't
4540 * remove it later. */
4541 static char_u *nobslash = (char_u *)" \t\"|";
4542# define ESCAPE_CHARS nobslash
4543#else
4544# define ESCAPE_CHARS escape_chars
4545#endif
4546
4547 for (l = repl; *l; ++l)
4548 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
4549 {
4550 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
4551 if (l != NULL)
4552 {
4553 vim_free(repl);
4554 repl = l;
4555 }
4556 break;
4557 }
4558 }
4559
4560 /* For a shell command a '!' must be escaped. */
4561 if ((eap->usefilter || eap->cmdidx == CMD_bang)
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004562 && vim_strpbrk(repl, (char_u *)"!&;()<>") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004563 {
4564 char_u *l;
4565
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004566 l = vim_strsave_escaped(repl, (char_u *)"!&;()<>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004567 if (l != NULL)
4568 {
4569 vim_free(repl);
4570 repl = l;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004571 /* For a sh-like shell escape "!" another time. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004572 if (strstr((char *)p_sh, "sh") != NULL)
4573 {
4574 l = vim_strsave_escaped(repl, (char_u *)"!");
4575 if (l != NULL)
4576 {
4577 vim_free(repl);
4578 repl = l;
4579 }
4580 }
4581 }
4582 }
4583
4584 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
4585 vim_free(repl);
4586 if (p == NULL)
4587 return FAIL;
4588 }
4589
4590 /*
4591 * One file argument: Expand wildcards.
4592 * Don't do this with ":r !command" or ":w !command".
4593 */
4594 if ((eap->argt & NOSPC) && !eap->usefilter)
4595 {
4596 /*
4597 * May do this twice:
4598 * 1. Replace environment variables.
4599 * 2. Replace any other wildcards, remove backslashes.
4600 */
4601 for (n = 1; n <= 2; ++n)
4602 {
4603 if (n == 2)
4604 {
4605#ifdef UNIX
4606 /*
4607 * Only for Unix we check for more than one file name.
4608 * For other systems spaces are considered to be part
4609 * of the file name.
4610 * Only check here if there is no wildcard, otherwise
4611 * ExpandOne() will check for errors. This allows
4612 * ":e `ls ve*.c`" on Unix.
4613 */
4614 if (!has_wildcards)
4615 for (p = eap->arg; *p; ++p)
4616 {
4617 /* skip escaped characters */
4618 if (p[1] && (*p == '\\' || *p == Ctrl_V))
4619 ++p;
4620 else if (vim_iswhite(*p))
4621 {
4622 *errormsgp = (char_u *)_("E172: Only one file name allowed");
4623 return FAIL;
4624 }
4625 }
4626#endif
4627
4628 /*
4629 * Halve the number of backslashes (this is Vi compatible).
4630 * For Unix and OS/2, when wildcards are expanded, this is
4631 * done by ExpandOne() below.
4632 */
4633#if defined(UNIX) || defined(OS2)
4634 if (!has_wildcards)
4635#endif
4636 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004637 }
4638
4639 if (has_wildcards)
4640 {
4641 if (n == 1)
4642 {
4643 /*
4644 * First loop: May expand environment variables. This
4645 * can be done much faster with expand_env() than with
4646 * something else (e.g., calling a shell).
4647 * After expanding environment variables, check again
4648 * if there are still wildcards present.
4649 */
4650 if (vim_strchr(eap->arg, '$') != NULL
4651 || vim_strchr(eap->arg, '~') != NULL)
4652 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00004653 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004654 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004655 has_wildcards = mch_has_wildcard(NameBuff);
4656 p = NameBuff;
4657 }
4658 else
4659 p = NULL;
4660 }
4661 else /* n == 2 */
4662 {
4663 expand_T xpc;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004664 int options = WILD_LIST_NOTFOUND|WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004665
4666 ExpandInit(&xpc);
4667 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004668 if (p_wic)
4669 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004670 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01004671 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004672 if (p == NULL)
4673 return FAIL;
4674 }
4675 if (p != NULL)
4676 {
4677 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
4678 p, cmdlinep);
4679 if (n == 2) /* p came from ExpandOne() */
4680 vim_free(p);
4681 }
4682 }
4683 }
4684 }
4685 return OK;
4686}
4687
4688/*
4689 * Replace part of the command line, keeping eap->cmd, eap->arg and
4690 * eap->nextcmd correct.
4691 * "src" points to the part that is to be replaced, of length "srclen".
4692 * "repl" is the replacement string.
4693 * Returns a pointer to the character after the replaced string.
4694 * Returns NULL for failure.
4695 */
4696 static char_u *
4697repl_cmdline(eap, src, srclen, repl, cmdlinep)
4698 exarg_T *eap;
4699 char_u *src;
4700 int srclen;
4701 char_u *repl;
4702 char_u **cmdlinep;
4703{
4704 int len;
4705 int i;
4706 char_u *new_cmdline;
4707
4708 /*
4709 * The new command line is build in new_cmdline[].
4710 * First allocate it.
4711 * Careful: a "+cmd" argument may have been NUL terminated.
4712 */
4713 len = (int)STRLEN(repl);
4714 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004715 if (eap->nextcmd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716 i += (int)STRLEN(eap->nextcmd);/* add space for next command */
4717 if ((new_cmdline = alloc((unsigned)i)) == NULL)
4718 return NULL; /* out of memory! */
4719
4720 /*
4721 * Copy the stuff before the expanded part.
4722 * Copy the expanded stuff.
4723 * Copy what came after the expanded part.
4724 * Copy the next commands, if there are any.
4725 */
4726 i = (int)(src - *cmdlinep); /* length of part before match */
4727 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00004728
Bram Moolenaar071d4272004-06-13 20:20:40 +00004729 mch_memmove(new_cmdline + i, repl, (size_t)len);
4730 i += len; /* remember the end of the string */
4731 STRCPY(new_cmdline + i, src + srclen);
4732 src = new_cmdline + i; /* remember where to continue */
4733
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004734 if (eap->nextcmd != NULL) /* append next command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004735 {
4736 i = (int)STRLEN(new_cmdline) + 1;
4737 STRCPY(new_cmdline + i, eap->nextcmd);
4738 eap->nextcmd = new_cmdline + i;
4739 }
4740 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
4741 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
4742 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
4743 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
4744 vim_free(*cmdlinep);
4745 *cmdlinep = new_cmdline;
4746
4747 return src;
4748}
4749
4750/*
4751 * Check for '|' to separate commands and '"' to start comments.
4752 */
4753 void
4754separate_nextcmd(eap)
4755 exarg_T *eap;
4756{
4757 char_u *p;
4758
Bram Moolenaar86b68352004-12-27 21:59:20 +00004759#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004760 p = skip_grep_pat(eap);
4761#else
4762 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004763#endif
4764
4765 for ( ; *p; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004766 {
4767 if (*p == Ctrl_V)
4768 {
4769 if (eap->argt & (USECTRLV | XFILE))
4770 ++p; /* skip CTRL-V and next char */
4771 else
Bram Moolenaarb0db5692007-08-14 20:54:49 +00004772 /* remove CTRL-V and skip next char */
Bram Moolenaara7241f52008-06-24 20:39:31 +00004773 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004774 if (*p == NUL) /* stop at NUL after CTRL-V */
4775 break;
4776 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004777
4778#ifdef FEAT_EVAL
4779 /* Skip over `=expr` when wildcards are expanded. */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004780 else if (p[0] == '`' && p[1] == '=' && (eap->argt & XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004781 {
4782 p += 2;
4783 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004784 }
4785#endif
4786
Bram Moolenaar071d4272004-06-13 20:20:40 +00004787 /* Check for '"': start of comment or '|': next command */
4788 /* :@" and :*" do not start a comment!
4789 * :redir @" doesn't either. */
4790 else if ((*p == '"' && !(eap->argt & NOTRLCOM)
4791 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
4792 || p != eap->arg)
4793 && (eap->cmdidx != CMD_redir
4794 || p != eap->arg + 1 || p[-1] != '@'))
4795 || *p == '|' || *p == '\n')
4796 {
4797 /*
4798 * We remove the '\' before the '|', unless USECTRLV is used
4799 * AND 'b' is present in 'cpoptions'.
4800 */
4801 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
4802 || !(eap->argt & USECTRLV)) && *(p - 1) == '\\')
4803 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00004804 STRMOVE(p - 1, p); /* remove the '\' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004805 --p;
4806 }
4807 else
4808 {
4809 eap->nextcmd = check_nextcmd(p);
4810 *p = NUL;
4811 break;
4812 }
4813 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004814 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004815
Bram Moolenaar071d4272004-06-13 20:20:40 +00004816 if (!(eap->argt & NOTRLCOM)) /* remove trailing spaces */
4817 del_trailing_spaces(eap->arg);
4818}
4819
4820/*
4821 * get + command from ex argument
4822 */
4823 static char_u *
4824getargcmd(argp)
4825 char_u **argp;
4826{
4827 char_u *arg = *argp;
4828 char_u *command = NULL;
4829
4830 if (*arg == '+') /* +[command] */
4831 {
4832 ++arg;
4833 if (vim_isspace(*arg))
4834 command = dollar_command;
4835 else
4836 {
4837 command = arg;
4838 arg = skip_cmd_arg(command, TRUE);
4839 if (*arg != NUL)
4840 *arg++ = NUL; /* terminate command with NUL */
4841 }
4842
4843 arg = skipwhite(arg); /* skip over spaces */
4844 *argp = arg;
4845 }
4846 return command;
4847}
4848
4849/*
4850 * Find end of "+command" argument. Skip over "\ " and "\\".
4851 */
4852 static char_u *
4853skip_cmd_arg(p, rembs)
4854 char_u *p;
4855 int rembs; /* TRUE to halve the number of backslashes */
4856{
4857 while (*p && !vim_isspace(*p))
4858 {
4859 if (*p == '\\' && p[1] != NUL)
4860 {
4861 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00004862 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004863 else
4864 ++p;
4865 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004866 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004867 }
4868 return p;
4869}
4870
4871/*
4872 * Get "++opt=arg" argument.
4873 * Return FAIL or OK.
4874 */
4875 static int
4876getargopt(eap)
4877 exarg_T *eap;
4878{
4879 char_u *arg = eap->arg + 2;
4880 int *pp = NULL;
4881#ifdef FEAT_MBYTE
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004882 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004883 char_u *p;
4884#endif
4885
4886 /* ":edit ++[no]bin[ary] file" */
4887 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
4888 {
4889 if (*arg == 'n')
4890 {
4891 arg += 2;
4892 eap->force_bin = FORCE_NOBIN;
4893 }
4894 else
4895 eap->force_bin = FORCE_BIN;
4896 if (!checkforcmd(&arg, "binary", 3))
4897 return FAIL;
4898 eap->arg = skipwhite(arg);
4899 return OK;
4900 }
4901
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004902 /* ":read ++edit file" */
4903 if (STRNCMP(arg, "edit", 4) == 0)
4904 {
4905 eap->read_edit = TRUE;
4906 eap->arg = skipwhite(arg + 4);
4907 return OK;
4908 }
4909
Bram Moolenaar071d4272004-06-13 20:20:40 +00004910 if (STRNCMP(arg, "ff", 2) == 0)
4911 {
4912 arg += 2;
4913 pp = &eap->force_ff;
4914 }
4915 else if (STRNCMP(arg, "fileformat", 10) == 0)
4916 {
4917 arg += 10;
4918 pp = &eap->force_ff;
4919 }
4920#ifdef FEAT_MBYTE
4921 else if (STRNCMP(arg, "enc", 3) == 0)
4922 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01004923 if (STRNCMP(arg, "encoding", 8) == 0)
4924 arg += 8;
4925 else
4926 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927 pp = &eap->force_enc;
4928 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004929 else if (STRNCMP(arg, "bad", 3) == 0)
4930 {
4931 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004932 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004933 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004934#endif
4935
4936 if (pp == NULL || *arg != '=')
4937 return FAIL;
4938
4939 ++arg;
4940 *pp = (int)(arg - eap->cmd);
4941 arg = skip_cmd_arg(arg, FALSE);
4942 eap->arg = skipwhite(arg);
4943 *arg = NUL;
4944
4945#ifdef FEAT_MBYTE
4946 if (pp == &eap->force_ff)
4947 {
4948#endif
4949 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
4950 return FAIL;
4951#ifdef FEAT_MBYTE
4952 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004953 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954 {
4955 /* Make 'fileencoding' lower case. */
4956 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
4957 *p = TOLOWER_ASC(*p);
4958 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004959 else
4960 {
4961 /* Check ++bad= argument. Must be a single-byte character, "keep" or
4962 * "drop". */
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004963 p = eap->cmd + bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004964 if (STRICMP(p, "keep") == 0)
4965 eap->bad_char = BAD_KEEP;
4966 else if (STRICMP(p, "drop") == 0)
4967 eap->bad_char = BAD_DROP;
4968 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
4969 eap->bad_char = *p;
4970 else
4971 return FAIL;
4972 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004973#endif
4974
4975 return OK;
4976}
4977
4978/*
4979 * ":abbreviate" and friends.
4980 */
4981 static void
4982ex_abbreviate(eap)
4983 exarg_T *eap;
4984{
4985 do_exmap(eap, TRUE); /* almost the same as mapping */
4986}
4987
4988/*
4989 * ":map" and friends.
4990 */
4991 static void
4992ex_map(eap)
4993 exarg_T *eap;
4994{
4995 /*
4996 * If we are sourcing .exrc or .vimrc in current directory we
4997 * print the mappings for security reasons.
4998 */
4999 if (secure)
5000 {
5001 secure = 2;
5002 msg_outtrans(eap->cmd);
5003 msg_putchar('\n');
5004 }
5005 do_exmap(eap, FALSE);
5006}
5007
5008/*
5009 * ":unmap" and friends.
5010 */
5011 static void
5012ex_unmap(eap)
5013 exarg_T *eap;
5014{
5015 do_exmap(eap, FALSE);
5016}
5017
5018/*
5019 * ":mapclear" and friends.
5020 */
5021 static void
5022ex_mapclear(eap)
5023 exarg_T *eap;
5024{
5025 map_clear(eap->cmd, eap->arg, eap->forceit, FALSE);
5026}
5027
5028/*
5029 * ":abclear" and friends.
5030 */
5031 static void
5032ex_abclear(eap)
5033 exarg_T *eap;
5034{
5035 map_clear(eap->cmd, eap->arg, TRUE, TRUE);
5036}
5037
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005038#if defined(FEAT_AUTOCMD) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005039 static void
5040ex_autocmd(eap)
5041 exarg_T *eap;
5042{
5043 /*
5044 * Disallow auto commands from .exrc and .vimrc in current
5045 * directory for security reasons.
5046 */
5047 if (secure)
5048 {
5049 secure = 2;
5050 eap->errmsg = e_curdir;
5051 }
5052 else if (eap->cmdidx == CMD_autocmd)
5053 do_autocmd(eap->arg, eap->forceit);
5054 else
5055 do_augroup(eap->arg, eap->forceit);
5056}
5057
5058/*
5059 * ":doautocmd": Apply the automatic commands to the current buffer.
5060 */
5061 static void
5062ex_doautocmd(eap)
5063 exarg_T *eap;
5064{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005065 char_u *arg = eap->arg;
5066 int call_do_modelines = check_nomodeline(&arg);
5067
5068 (void)do_doautocmd(arg, TRUE);
5069 if (call_do_modelines) /* Only when there is no <nomodeline>. */
5070 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005071}
5072#endif
5073
5074#ifdef FEAT_LISTCMDS
5075/*
5076 * :[N]bunload[!] [N] [bufname] unload buffer
5077 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
5078 * :[N]bwipeout[!] [N] [bufname] delete buffer really
5079 */
5080 static void
5081ex_bunload(eap)
5082 exarg_T *eap;
5083{
5084 eap->errmsg = do_bufdel(
5085 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
5086 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
5087 : DOBUF_UNLOAD, eap->arg,
5088 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
5089}
5090
5091/*
5092 * :[N]buffer [N] to buffer N
5093 * :[N]sbuffer [N] to buffer N
5094 */
5095 static void
5096ex_buffer(eap)
5097 exarg_T *eap;
5098{
5099 if (*eap->arg)
5100 eap->errmsg = e_trailing;
5101 else
5102 {
5103 if (eap->addr_count == 0) /* default is current buffer */
5104 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
5105 else
5106 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
5107 }
5108}
5109
5110/*
5111 * :[N]bmodified [N] to next mod. buffer
5112 * :[N]sbmodified [N] to next mod. buffer
5113 */
5114 static void
5115ex_bmodified(eap)
5116 exarg_T *eap;
5117{
5118 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
5119}
5120
5121/*
5122 * :[N]bnext [N] to next buffer
5123 * :[N]sbnext [N] split and to next buffer
5124 */
5125 static void
5126ex_bnext(eap)
5127 exarg_T *eap;
5128{
5129 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
5130}
5131
5132/*
5133 * :[N]bNext [N] to previous buffer
5134 * :[N]bprevious [N] to previous buffer
5135 * :[N]sbNext [N] split and to previous buffer
5136 * :[N]sbprevious [N] split and to previous buffer
5137 */
5138 static void
5139ex_bprevious(eap)
5140 exarg_T *eap;
5141{
5142 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
5143}
5144
5145/*
5146 * :brewind to first buffer
5147 * :bfirst to first buffer
5148 * :sbrewind split and to first buffer
5149 * :sbfirst split and to first buffer
5150 */
5151 static void
5152ex_brewind(eap)
5153 exarg_T *eap;
5154{
5155 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
5156}
5157
5158/*
5159 * :blast to last buffer
5160 * :sblast split and to last buffer
5161 */
5162 static void
5163ex_blast(eap)
5164 exarg_T *eap;
5165{
5166 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
5167}
5168#endif
5169
5170 int
5171ends_excmd(c)
5172 int c;
5173{
5174 return (c == NUL || c == '|' || c == '"' || c == '\n');
5175}
5176
5177#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5178 || defined(PROTO)
5179/*
5180 * Return the next command, after the first '|' or '\n'.
5181 * Return NULL if not found.
5182 */
5183 char_u *
5184find_nextcmd(p)
5185 char_u *p;
5186{
5187 while (*p != '|' && *p != '\n')
5188 {
5189 if (*p == NUL)
5190 return NULL;
5191 ++p;
5192 }
5193 return (p + 1);
5194}
5195#endif
5196
5197/*
5198 * Check if *p is a separator between Ex commands.
5199 * Return NULL if it isn't, (p + 1) if it is.
5200 */
5201 char_u *
5202check_nextcmd(p)
5203 char_u *p;
5204{
5205 p = skipwhite(p);
5206 if (*p == '|' || *p == '\n')
5207 return (p + 1);
5208 else
5209 return NULL;
5210}
5211
5212/*
5213 * - if there are more files to edit
5214 * - and this is the last window
5215 * - and forceit not used
5216 * - and not repeated twice on a row
5217 * return FAIL and give error message if 'message' TRUE
5218 * return OK otherwise
5219 */
5220 static int
5221check_more(message, forceit)
5222 int message; /* when FALSE check only, no messages */
5223 int forceit;
5224{
5225 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5226
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005227 if (!forceit && only_one_window()
5228 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005229 {
5230 if (message)
5231 {
5232#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
5233 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
5234 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005235 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005236
5237 if (n == 1)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02005238 vim_strncpy(buff,
5239 (char_u *)_("1 more file to edit. Quit anyway?"),
Bram Moolenaard9462e32011-04-11 21:35:11 +02005240 DIALOG_MSG_SIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005241 else
Bram Moolenaard9462e32011-04-11 21:35:11 +02005242 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243 _("%d more files to edit. Quit anyway?"), n);
5244 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5245 return OK;
5246 return FAIL;
5247 }
5248#endif
5249 if (n == 1)
5250 EMSG(_("E173: 1 more file to edit"));
5251 else
5252 EMSGN(_("E173: %ld more files to edit"), n);
5253 quitmore = 2; /* next try to quit is allowed */
5254 }
5255 return FAIL;
5256 }
5257 return OK;
5258}
5259
5260#ifdef FEAT_CMDL_COMPL
5261/*
5262 * Function given to ExpandGeneric() to obtain the list of command names.
5263 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005264 char_u *
5265get_command_name(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00005266 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005267 int idx;
5268{
5269 if (idx >= (int)CMD_SIZE)
5270# ifdef FEAT_USR_CMDS
5271 return get_user_command_name(idx);
5272# else
5273 return NULL;
5274# endif
5275 return cmdnames[idx].cmd_name;
5276}
5277#endif
5278
5279#if defined(FEAT_USR_CMDS) || defined(PROTO)
5280static 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));
5281static void uc_list __ARGS((char_u *name, size_t name_len));
5282static int uc_scan_attr __ARGS((char_u *attr, size_t len, long *argt, long *def, int *flags, int *compl, char_u **compl_arg));
5283static char_u *uc_split_args __ARGS((char_u *arg, size_t *lenp));
5284static 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));
5285
5286 static int
5287uc_add_command(name, name_len, rep, argt, def, flags, compl, compl_arg, force)
5288 char_u *name;
5289 size_t name_len;
5290 char_u *rep;
5291 long argt;
5292 long def;
5293 int flags;
5294 int compl;
5295 char_u *compl_arg;
5296 int force;
5297{
5298 ucmd_T *cmd = NULL;
5299 char_u *p;
5300 int i;
5301 int cmp = 1;
5302 char_u *rep_buf = NULL;
5303 garray_T *gap;
5304
Bram Moolenaar9c102382006-05-03 21:26:49 +00005305 replace_termcodes(rep, &rep_buf, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005306 if (rep_buf == NULL)
5307 {
5308 /* Can't replace termcodes - try using the string as is */
5309 rep_buf = vim_strsave(rep);
5310
5311 /* Give up if out of memory */
5312 if (rep_buf == NULL)
5313 return FAIL;
5314 }
5315
5316 /* get address of growarray: global or in curbuf */
5317 if (flags & UC_BUFFER)
5318 {
5319 gap = &curbuf->b_ucmds;
5320 if (gap->ga_itemsize == 0)
5321 ga_init2(gap, (int)sizeof(ucmd_T), 4);
5322 }
5323 else
5324 gap = &ucmds;
5325
5326 /* Search for the command in the already defined commands. */
5327 for (i = 0; i < gap->ga_len; ++i)
5328 {
5329 size_t len;
5330
5331 cmd = USER_CMD_GA(gap, i);
5332 len = STRLEN(cmd->uc_name);
5333 cmp = STRNCMP(name, cmd->uc_name, name_len);
5334 if (cmp == 0)
5335 {
5336 if (name_len < len)
5337 cmp = -1;
5338 else if (name_len > len)
5339 cmp = 1;
5340 }
5341
5342 if (cmp == 0)
5343 {
5344 if (!force)
5345 {
5346 EMSG(_("E174: Command already exists: add ! to replace it"));
5347 goto fail;
5348 }
5349
5350 vim_free(cmd->uc_rep);
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00005351 cmd->uc_rep = NULL;
5352#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5353 vim_free(cmd->uc_compl_arg);
5354 cmd->uc_compl_arg = NULL;
5355#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005356 break;
5357 }
5358
5359 /* Stop as soon as we pass the name to add */
5360 if (cmp < 0)
5361 break;
5362 }
5363
5364 /* Extend the array unless we're replacing an existing command */
5365 if (cmp != 0)
5366 {
5367 if (ga_grow(gap, 1) != OK)
5368 goto fail;
5369 if ((p = vim_strnsave(name, (int)name_len)) == NULL)
5370 goto fail;
5371
5372 cmd = USER_CMD_GA(gap, i);
5373 mch_memmove(cmd + 1, cmd, (gap->ga_len - i) * sizeof(ucmd_T));
5374
5375 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005376
5377 cmd->uc_name = p;
5378 }
5379
5380 cmd->uc_rep = rep_buf;
5381 cmd->uc_argt = argt;
5382 cmd->uc_def = def;
5383 cmd->uc_compl = compl;
5384#ifdef FEAT_EVAL
5385 cmd->uc_scriptID = current_SID;
5386# ifdef FEAT_CMDL_COMPL
5387 cmd->uc_compl_arg = compl_arg;
5388# endif
5389#endif
5390
5391 return OK;
5392
5393fail:
5394 vim_free(rep_buf);
5395#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5396 vim_free(compl_arg);
5397#endif
5398 return FAIL;
5399}
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005400#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005401
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005402#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005403/*
5404 * List of names for completion for ":command" with the EXPAND_ flag.
5405 * Must be alphabetical for completion.
5406 */
5407static struct
5408{
5409 int expand;
5410 char *name;
5411} command_complete[] =
5412{
5413 {EXPAND_AUGROUP, "augroup"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005414 {EXPAND_BEHAVE, "behave"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005415 {EXPAND_BUFFERS, "buffer"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005416 {EXPAND_COLORS, "color"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005417 {EXPAND_COMMANDS, "command"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005418 {EXPAND_COMPILER, "compiler"},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005419#if defined(FEAT_CSCOPE)
5420 {EXPAND_CSCOPE, "cscope"},
5421#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005422#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5423 {EXPAND_USER_DEFINED, "custom"},
Bram Moolenaara466c992005-07-09 21:03:22 +00005424 {EXPAND_USER_LIST, "customlist"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005425#endif
5426 {EXPAND_DIRECTORIES, "dir"},
5427 {EXPAND_ENV_VARS, "environment"},
5428 {EXPAND_EVENTS, "event"},
5429 {EXPAND_EXPRESSION, "expression"},
5430 {EXPAND_FILES, "file"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005431 {EXPAND_FILES_IN_PATH, "file_in_path"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005432 {EXPAND_FILETYPE, "filetype"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005433 {EXPAND_FUNCTIONS, "function"},
5434 {EXPAND_HELP, "help"},
5435 {EXPAND_HIGHLIGHT, "highlight"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005436#if defined(FEAT_CMDHIST)
5437 {EXPAND_HISTORY, "history"},
5438#endif
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005439#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005440 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005441 {EXPAND_LOCALES, "locale"},
5442#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005443 {EXPAND_MAPPINGS, "mapping"},
5444 {EXPAND_MENUS, "menu"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005445 {EXPAND_OWNSYNTAX, "syntax"},
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02005446#if defined(FEAT_PROFILE)
5447 {EXPAND_SYNTIME, "syntime"},
5448#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005449 {EXPAND_SETTINGS, "option"},
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005450 {EXPAND_SHELLCMD, "shellcmd"},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005451#if defined(FEAT_SIGNS)
5452 {EXPAND_SIGN, "sign"},
5453#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005454 {EXPAND_TAGS, "tag"},
5455 {EXPAND_TAGS_LISTFILES, "tag_listfiles"},
Bram Moolenaar24305862012-08-15 14:05:05 +02005456 {EXPAND_USER, "user"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005457 {EXPAND_USER_VARS, "var"},
5458 {0, NULL}
5459};
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005460#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005461
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005462#if defined(FEAT_USR_CMDS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005463 static void
5464uc_list(name, name_len)
5465 char_u *name;
5466 size_t name_len;
5467{
5468 int i, j;
5469 int found = FALSE;
5470 ucmd_T *cmd;
5471 int len;
5472 long a;
5473 garray_T *gap;
5474
5475 gap = &curbuf->b_ucmds;
5476 for (;;)
5477 {
5478 for (i = 0; i < gap->ga_len; ++i)
5479 {
5480 cmd = USER_CMD_GA(gap, i);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005481 a = (long)cmd->uc_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005482
5483 /* Skip commands which don't match the requested prefix */
5484 if (STRNCMP(name, cmd->uc_name, name_len) != 0)
5485 continue;
5486
5487 /* Put out the title first time */
5488 if (!found)
5489 MSG_PUTS_TITLE(_("\n Name Args Range Complete Definition"));
5490 found = TRUE;
5491 msg_putchar('\n');
5492 if (got_int)
5493 break;
5494
5495 /* Special cases */
5496 msg_putchar(a & BANG ? '!' : ' ');
5497 msg_putchar(a & REGSTR ? '"' : ' ');
5498 msg_putchar(gap != &ucmds ? 'b' : ' ');
5499 msg_putchar(' ');
5500
5501 msg_outtrans_attr(cmd->uc_name, hl_attr(HLF_D));
5502 len = (int)STRLEN(cmd->uc_name) + 4;
5503
5504 do {
5505 msg_putchar(' ');
5506 ++len;
5507 } while (len < 16);
5508
5509 len = 0;
5510
5511 /* Arguments */
5512 switch ((int)(a & (EXTRA|NOSPC|NEEDARG)))
5513 {
5514 case 0: IObuff[len++] = '0'; break;
5515 case (EXTRA): IObuff[len++] = '*'; break;
5516 case (EXTRA|NOSPC): IObuff[len++] = '?'; break;
5517 case (EXTRA|NEEDARG): IObuff[len++] = '+'; break;
5518 case (EXTRA|NOSPC|NEEDARG): IObuff[len++] = '1'; break;
5519 }
5520
5521 do {
5522 IObuff[len++] = ' ';
5523 } while (len < 5);
5524
5525 /* Range */
5526 if (a & (RANGE|COUNT))
5527 {
5528 if (a & COUNT)
5529 {
5530 /* -count=N */
5531 sprintf((char *)IObuff + len, "%ldc", cmd->uc_def);
5532 len += (int)STRLEN(IObuff + len);
5533 }
5534 else if (a & DFLALL)
5535 IObuff[len++] = '%';
5536 else if (cmd->uc_def >= 0)
5537 {
5538 /* -range=N */
5539 sprintf((char *)IObuff + len, "%ld", cmd->uc_def);
5540 len += (int)STRLEN(IObuff + len);
5541 }
5542 else
5543 IObuff[len++] = '.';
5544 }
5545
5546 do {
5547 IObuff[len++] = ' ';
5548 } while (len < 11);
5549
5550 /* Completion */
5551 for (j = 0; command_complete[j].expand != 0; ++j)
5552 if (command_complete[j].expand == cmd->uc_compl)
5553 {
5554 STRCPY(IObuff + len, command_complete[j].name);
5555 len += (int)STRLEN(IObuff + len);
5556 break;
5557 }
5558
5559 do {
5560 IObuff[len++] = ' ';
5561 } while (len < 21);
5562
5563 IObuff[len] = '\0';
5564 msg_outtrans(IObuff);
5565
5566 msg_outtrans_special(cmd->uc_rep, FALSE);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005567#ifdef FEAT_EVAL
5568 if (p_verbose > 0)
5569 last_set_msg(cmd->uc_scriptID);
5570#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005571 out_flush();
5572 ui_breakcheck();
5573 if (got_int)
5574 break;
5575 }
5576 if (gap == &ucmds || i < gap->ga_len)
5577 break;
5578 gap = &ucmds;
5579 }
5580
5581 if (!found)
5582 MSG(_("No user-defined commands found"));
5583}
5584
5585 static char_u *
5586uc_fun_cmd()
5587{
5588 static char_u fcmd[] = {0x84, 0xaf, 0x60, 0xb9, 0xaf, 0xb5, 0x60, 0xa4,
5589 0xa5, 0xad, 0xa1, 0xae, 0xa4, 0x60, 0xa1, 0x60,
5590 0xb3, 0xa8, 0xb2, 0xb5, 0xa2, 0xa2, 0xa5, 0xb2,
5591 0xb9, 0x7f, 0};
5592 int i;
5593
5594 for (i = 0; fcmd[i]; ++i)
5595 IObuff[i] = fcmd[i] - 0x40;
5596 IObuff[i] = 0;
5597 return IObuff;
5598}
5599
5600 static int
5601uc_scan_attr(attr, len, argt, def, flags, compl, compl_arg)
5602 char_u *attr;
5603 size_t len;
5604 long *argt;
5605 long *def;
5606 int *flags;
5607 int *compl;
5608 char_u **compl_arg;
5609{
5610 char_u *p;
5611
5612 if (len == 0)
5613 {
5614 EMSG(_("E175: No attribute specified"));
5615 return FAIL;
5616 }
5617
5618 /* First, try the simple attributes (no arguments) */
5619 if (STRNICMP(attr, "bang", len) == 0)
5620 *argt |= BANG;
5621 else if (STRNICMP(attr, "buffer", len) == 0)
5622 *flags |= UC_BUFFER;
5623 else if (STRNICMP(attr, "register", len) == 0)
5624 *argt |= REGSTR;
5625 else if (STRNICMP(attr, "bar", len) == 0)
5626 *argt |= TRLBAR;
5627 else
5628 {
5629 int i;
5630 char_u *val = NULL;
5631 size_t vallen = 0;
5632 size_t attrlen = len;
5633
5634 /* Look for the attribute name - which is the part before any '=' */
5635 for (i = 0; i < (int)len; ++i)
5636 {
5637 if (attr[i] == '=')
5638 {
5639 val = &attr[i + 1];
5640 vallen = len - i - 1;
5641 attrlen = i;
5642 break;
5643 }
5644 }
5645
5646 if (STRNICMP(attr, "nargs", attrlen) == 0)
5647 {
5648 if (vallen == 1)
5649 {
5650 if (*val == '0')
5651 /* Do nothing - this is the default */;
5652 else if (*val == '1')
5653 *argt |= (EXTRA | NOSPC | NEEDARG);
5654 else if (*val == '*')
5655 *argt |= EXTRA;
5656 else if (*val == '?')
5657 *argt |= (EXTRA | NOSPC);
5658 else if (*val == '+')
5659 *argt |= (EXTRA | NEEDARG);
5660 else
5661 goto wrong_nargs;
5662 }
5663 else
5664 {
5665wrong_nargs:
5666 EMSG(_("E176: Invalid number of arguments"));
5667 return FAIL;
5668 }
5669 }
5670 else if (STRNICMP(attr, "range", attrlen) == 0)
5671 {
5672 *argt |= RANGE;
5673 if (vallen == 1 && *val == '%')
5674 *argt |= DFLALL;
5675 else if (val != NULL)
5676 {
5677 p = val;
5678 if (*def >= 0)
5679 {
5680two_count:
5681 EMSG(_("E177: Count cannot be specified twice"));
5682 return FAIL;
5683 }
5684
5685 *def = getdigits(&p);
5686 *argt |= (ZEROR | NOTADR);
5687
5688 if (p != val + vallen || vallen == 0)
5689 {
5690invalid_count:
5691 EMSG(_("E178: Invalid default value for count"));
5692 return FAIL;
5693 }
5694 }
5695 }
5696 else if (STRNICMP(attr, "count", attrlen) == 0)
5697 {
Bram Moolenaar32e7b2d2005-02-27 22:36:47 +00005698 *argt |= (COUNT | ZEROR | RANGE | NOTADR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005699
5700 if (val != NULL)
5701 {
5702 p = val;
5703 if (*def >= 0)
5704 goto two_count;
5705
5706 *def = getdigits(&p);
5707
5708 if (p != val + vallen)
5709 goto invalid_count;
5710 }
5711
5712 if (*def < 0)
5713 *def = 0;
5714 }
5715 else if (STRNICMP(attr, "complete", attrlen) == 0)
5716 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005717 if (val == NULL)
5718 {
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005719 EMSG(_("E179: argument required for -complete"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005720 return FAIL;
5721 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005722
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005723 if (parse_compl_arg(val, (int)vallen, compl, argt, compl_arg)
5724 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005725 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005726 }
5727 else
5728 {
5729 char_u ch = attr[len];
5730 attr[len] = '\0';
5731 EMSG2(_("E181: Invalid attribute: %s"), attr);
5732 attr[len] = ch;
5733 return FAIL;
5734 }
5735 }
5736
5737 return OK;
5738}
5739
Bram Moolenaara850a712009-01-28 14:42:59 +00005740/*
5741 * ":command ..."
5742 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005743 static void
5744ex_command(eap)
5745 exarg_T *eap;
5746{
5747 char_u *name;
5748 char_u *end;
5749 char_u *p;
5750 long argt = 0;
5751 long def = -1;
5752 int flags = 0;
5753 int compl = EXPAND_NOTHING;
5754 char_u *compl_arg = NULL;
5755 int has_attr = (eap->arg[0] == '-');
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01005756 int name_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005757
5758 p = eap->arg;
5759
5760 /* Check for attributes */
5761 while (*p == '-')
5762 {
5763 ++p;
5764 end = skiptowhite(p);
5765 if (uc_scan_attr(p, end - p, &argt, &def, &flags, &compl, &compl_arg)
5766 == FAIL)
5767 return;
5768 p = skipwhite(end);
5769 }
5770
5771 /* Get the name (if any) and skip to the following argument */
5772 name = p;
5773 if (ASCII_ISALPHA(*p))
5774 while (ASCII_ISALNUM(*p))
5775 ++p;
5776 if (!ends_excmd(*p) && !vim_iswhite(*p))
5777 {
5778 EMSG(_("E182: Invalid command name"));
5779 return;
5780 }
5781 end = p;
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01005782 name_len = (int)(end - name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005783
5784 /* If there is nothing after the name, and no attributes were specified,
5785 * we are listing commands
5786 */
5787 p = skipwhite(end);
5788 if (!has_attr && ends_excmd(*p))
5789 {
5790 uc_list(name, end - name);
5791 }
5792 else if (!ASCII_ISUPPER(*name))
5793 {
5794 EMSG(_("E183: User defined commands must start with an uppercase letter"));
5795 return;
5796 }
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01005797 else if ((name_len == 1 && *name == 'X')
5798 || (name_len <= 4
5799 && STRNCMP(name, "Next", name_len > 4 ? 4 : name_len) == 0))
5800 {
5801 EMSG(_("E841: Reserved name, cannot be used for user defined command"));
5802 return;
5803 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005804 else
5805 uc_add_command(name, end - name, p, argt, def, flags, compl, compl_arg,
5806 eap->forceit);
5807}
5808
5809/*
5810 * ":comclear"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005811 * Clear all user commands, global and for current buffer.
5812 */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00005813 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005814ex_comclear(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00005815 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005816{
5817 uc_clear(&ucmds);
5818 uc_clear(&curbuf->b_ucmds);
5819}
5820
5821/*
5822 * Clear all user commands for "gap".
5823 */
5824 void
5825uc_clear(gap)
5826 garray_T *gap;
5827{
5828 int i;
5829 ucmd_T *cmd;
5830
5831 for (i = 0; i < gap->ga_len; ++i)
5832 {
5833 cmd = USER_CMD_GA(gap, i);
5834 vim_free(cmd->uc_name);
5835 vim_free(cmd->uc_rep);
5836# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5837 vim_free(cmd->uc_compl_arg);
5838# endif
5839 }
5840 ga_clear(gap);
5841}
5842
5843 static void
5844ex_delcommand(eap)
5845 exarg_T *eap;
5846{
5847 int i = 0;
5848 ucmd_T *cmd = NULL;
5849 int cmp = -1;
5850 garray_T *gap;
5851
5852 gap = &curbuf->b_ucmds;
5853 for (;;)
5854 {
5855 for (i = 0; i < gap->ga_len; ++i)
5856 {
5857 cmd = USER_CMD_GA(gap, i);
5858 cmp = STRCMP(eap->arg, cmd->uc_name);
5859 if (cmp <= 0)
5860 break;
5861 }
5862 if (gap == &ucmds || cmp == 0)
5863 break;
5864 gap = &ucmds;
5865 }
5866
5867 if (cmp != 0)
5868 {
5869 EMSG2(_("E184: No such user-defined command: %s"), eap->arg);
5870 return;
5871 }
5872
5873 vim_free(cmd->uc_name);
5874 vim_free(cmd->uc_rep);
5875# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5876 vim_free(cmd->uc_compl_arg);
5877# endif
5878
5879 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005880
5881 if (i < gap->ga_len)
5882 mch_memmove(cmd, cmd + 1, (gap->ga_len - i) * sizeof(ucmd_T));
5883}
5884
Bram Moolenaar552f8a12007-03-08 17:12:08 +00005885/*
5886 * split and quote args for <f-args>
5887 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005888 static char_u *
5889uc_split_args(arg, lenp)
5890 char_u *arg;
5891 size_t *lenp;
5892{
5893 char_u *buf;
5894 char_u *p;
5895 char_u *q;
5896 int len;
5897
5898 /* Precalculate length */
5899 p = arg;
5900 len = 2; /* Initial and final quotes */
5901
5902 while (*p)
5903 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00005904 if (p[0] == '\\' && p[1] == '\\')
5905 {
5906 len += 2;
5907 p += 2;
5908 }
5909 else if (p[0] == '\\' && vim_iswhite(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005910 {
5911 len += 1;
5912 p += 2;
5913 }
5914 else if (*p == '\\' || *p == '"')
5915 {
5916 len += 2;
5917 p += 1;
5918 }
5919 else if (vim_iswhite(*p))
5920 {
5921 p = skipwhite(p);
5922 if (*p == NUL)
5923 break;
5924 len += 3; /* "," */
5925 }
5926 else
5927 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02005928#ifdef FEAT_MBYTE
5929 int charlen = (*mb_ptr2len)(p);
5930 len += charlen;
5931 p += charlen;
5932#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005933 ++len;
5934 ++p;
Bram Moolenaardfef1542012-07-10 19:25:10 +02005935#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005936 }
5937 }
5938
5939 buf = alloc(len + 1);
5940 if (buf == NULL)
5941 {
5942 *lenp = 0;
5943 return buf;
5944 }
5945
5946 p = arg;
5947 q = buf;
5948 *q++ = '"';
5949 while (*p)
5950 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00005951 if (p[0] == '\\' && p[1] == '\\')
5952 {
5953 *q++ = '\\';
5954 *q++ = '\\';
5955 p += 2;
5956 }
5957 else if (p[0] == '\\' && vim_iswhite(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005958 {
5959 *q++ = p[1];
5960 p += 2;
5961 }
5962 else if (*p == '\\' || *p == '"')
5963 {
5964 *q++ = '\\';
5965 *q++ = *p++;
5966 }
5967 else if (vim_iswhite(*p))
5968 {
5969 p = skipwhite(p);
5970 if (*p == NUL)
5971 break;
5972 *q++ = '"';
5973 *q++ = ',';
5974 *q++ = '"';
5975 }
5976 else
5977 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02005978 MB_COPY_CHAR(p, q);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005979 }
5980 }
5981 *q++ = '"';
5982 *q = 0;
5983
5984 *lenp = len;
5985 return buf;
5986}
5987
5988/*
5989 * Check for a <> code in a user command.
5990 * "code" points to the '<'. "len" the length of the <> (inclusive).
5991 * "buf" is where the result is to be added.
5992 * "split_buf" points to a buffer used for splitting, caller should free it.
5993 * "split_len" is the length of what "split_buf" contains.
5994 * Returns the length of the replacement, which has been added to "buf".
5995 * Returns -1 if there was no match, and only the "<" has been copied.
5996 */
5997 static size_t
5998uc_check_code(code, len, buf, cmd, eap, split_buf, split_len)
5999 char_u *code;
6000 size_t len;
6001 char_u *buf;
6002 ucmd_T *cmd; /* the user command we're expanding */
6003 exarg_T *eap; /* ex arguments */
6004 char_u **split_buf;
6005 size_t *split_len;
6006{
6007 size_t result = 0;
6008 char_u *p = code + 1;
6009 size_t l = len - 2;
6010 int quote = 0;
6011 enum { ct_ARGS, ct_BANG, ct_COUNT, ct_LINE1, ct_LINE2, ct_REGISTER,
6012 ct_LT, ct_NONE } type = ct_NONE;
6013
6014 if ((vim_strchr((char_u *)"qQfF", *p) != NULL) && p[1] == '-')
6015 {
6016 quote = (*p == 'q' || *p == 'Q') ? 1 : 2;
6017 p += 2;
6018 l -= 2;
6019 }
6020
Bram Moolenaar371d5402006-03-20 21:47:49 +00006021 ++l;
6022 if (l <= 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006023 type = ct_NONE;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006024 else if (STRNICMP(p, "args>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006025 type = ct_ARGS;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006026 else if (STRNICMP(p, "bang>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006027 type = ct_BANG;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006028 else if (STRNICMP(p, "count>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006029 type = ct_COUNT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006030 else if (STRNICMP(p, "line1>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006031 type = ct_LINE1;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006032 else if (STRNICMP(p, "line2>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006033 type = ct_LINE2;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006034 else if (STRNICMP(p, "lt>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006035 type = ct_LT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006036 else if (STRNICMP(p, "reg>", l) == 0 || STRNICMP(p, "register>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006037 type = ct_REGISTER;
6038
6039 switch (type)
6040 {
6041 case ct_ARGS:
6042 /* Simple case first */
6043 if (*eap->arg == NUL)
6044 {
6045 if (quote == 1)
6046 {
6047 result = 2;
6048 if (buf != NULL)
6049 STRCPY(buf, "''");
6050 }
6051 else
6052 result = 0;
6053 break;
6054 }
6055
6056 /* When specified there is a single argument don't split it.
6057 * Works for ":Cmd %" when % is "a b c". */
6058 if ((eap->argt & NOSPC) && quote == 2)
6059 quote = 1;
6060
6061 switch (quote)
6062 {
6063 case 0: /* No quoting, no splitting */
6064 result = STRLEN(eap->arg);
6065 if (buf != NULL)
6066 STRCPY(buf, eap->arg);
6067 break;
6068 case 1: /* Quote, but don't split */
6069 result = STRLEN(eap->arg) + 2;
6070 for (p = eap->arg; *p; ++p)
6071 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006072#ifdef FEAT_MBYTE
6073 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6074 /* DBCS can contain \ in a trail byte, skip the
6075 * double-byte character. */
6076 ++p;
6077 else
6078#endif
6079 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006080 ++result;
6081 }
6082
6083 if (buf != NULL)
6084 {
6085 *buf++ = '"';
6086 for (p = eap->arg; *p; ++p)
6087 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006088#ifdef FEAT_MBYTE
6089 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6090 /* DBCS can contain \ in a trail byte, copy the
6091 * double-byte character to avoid escaping. */
6092 *buf++ = *p++;
6093 else
6094#endif
6095 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006096 *buf++ = '\\';
6097 *buf++ = *p;
6098 }
6099 *buf = '"';
6100 }
6101
6102 break;
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006103 case 2: /* Quote and split (<f-args>) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006104 /* This is hard, so only do it once, and cache the result */
6105 if (*split_buf == NULL)
6106 *split_buf = uc_split_args(eap->arg, split_len);
6107
6108 result = *split_len;
6109 if (buf != NULL && result != 0)
6110 STRCPY(buf, *split_buf);
6111
6112 break;
6113 }
6114 break;
6115
6116 case ct_BANG:
6117 result = eap->forceit ? 1 : 0;
6118 if (quote)
6119 result += 2;
6120 if (buf != NULL)
6121 {
6122 if (quote)
6123 *buf++ = '"';
6124 if (eap->forceit)
6125 *buf++ = '!';
6126 if (quote)
6127 *buf = '"';
6128 }
6129 break;
6130
6131 case ct_LINE1:
6132 case ct_LINE2:
6133 case ct_COUNT:
6134 {
6135 char num_buf[20];
6136 long num = (type == ct_LINE1) ? eap->line1 :
6137 (type == ct_LINE2) ? eap->line2 :
6138 (eap->addr_count > 0) ? eap->line2 : cmd->uc_def;
6139 size_t num_len;
6140
6141 sprintf(num_buf, "%ld", num);
6142 num_len = STRLEN(num_buf);
6143 result = num_len;
6144
6145 if (quote)
6146 result += 2;
6147
6148 if (buf != NULL)
6149 {
6150 if (quote)
6151 *buf++ = '"';
6152 STRCPY(buf, num_buf);
6153 buf += num_len;
6154 if (quote)
6155 *buf = '"';
6156 }
6157
6158 break;
6159 }
6160
6161 case ct_REGISTER:
6162 result = eap->regname ? 1 : 0;
6163 if (quote)
6164 result += 2;
6165 if (buf != NULL)
6166 {
6167 if (quote)
6168 *buf++ = '\'';
6169 if (eap->regname)
6170 *buf++ = eap->regname;
6171 if (quote)
6172 *buf = '\'';
6173 }
6174 break;
6175
6176 case ct_LT:
6177 result = 1;
6178 if (buf != NULL)
6179 *buf = '<';
6180 break;
6181
6182 default:
6183 /* Not recognized: just copy the '<' and return -1. */
6184 result = (size_t)-1;
6185 if (buf != NULL)
6186 *buf = '<';
6187 break;
6188 }
6189
6190 return result;
6191}
6192
6193 static void
6194do_ucmd(eap)
6195 exarg_T *eap;
6196{
6197 char_u *buf;
6198 char_u *p;
6199 char_u *q;
6200
6201 char_u *start;
Bram Moolenaar25648a52009-02-21 19:37:46 +00006202 char_u *end = NULL;
Bram Moolenaara850a712009-01-28 14:42:59 +00006203 char_u *ksp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006204 size_t len, totlen;
6205
6206 size_t split_len = 0;
6207 char_u *split_buf = NULL;
6208 ucmd_T *cmd;
6209#ifdef FEAT_EVAL
6210 scid_T save_current_SID = current_SID;
6211#endif
6212
6213 if (eap->cmdidx == CMD_USER)
6214 cmd = USER_CMD(eap->useridx);
6215 else
6216 cmd = USER_CMD_GA(&curbuf->b_ucmds, eap->useridx);
6217
6218 /*
6219 * Replace <> in the command by the arguments.
Bram Moolenaara850a712009-01-28 14:42:59 +00006220 * First round: "buf" is NULL, compute length, allocate "buf".
6221 * Second round: copy result into "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006222 */
6223 buf = NULL;
6224 for (;;)
6225 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006226 p = cmd->uc_rep; /* source */
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00006227 q = buf; /* destination */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006228 totlen = 0;
Bram Moolenaara850a712009-01-28 14:42:59 +00006229
6230 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006231 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006232 start = vim_strchr(p, '<');
6233 if (start != NULL)
6234 end = vim_strchr(start + 1, '>');
6235 if (buf != NULL)
6236 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006237 for (ksp = p; *ksp != NUL && *ksp != K_SPECIAL; ++ksp)
6238 ;
6239 if (*ksp == K_SPECIAL
6240 && (start == NULL || ksp < start || end == NULL)
Bram Moolenaara850a712009-01-28 14:42:59 +00006241 && ((ksp[1] == KS_SPECIAL && ksp[2] == KE_FILLER)
6242# ifdef FEAT_GUI
6243 || (ksp[1] == KS_EXTRA && ksp[2] == (int)KE_CSI)
6244# endif
6245 ))
6246 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006247 /* K_SPECIAL has been put in the buffer as K_SPECIAL
Bram Moolenaara850a712009-01-28 14:42:59 +00006248 * KS_SPECIAL KE_FILLER, like for mappings, but
6249 * do_cmdline() doesn't handle that, so convert it back.
6250 * Also change K_SPECIAL KS_EXTRA KE_CSI into CSI. */
6251 len = ksp - p;
6252 if (len > 0)
6253 {
6254 mch_memmove(q, p, len);
6255 q += len;
6256 }
6257 *q++ = ksp[1] == KS_SPECIAL ? K_SPECIAL : CSI;
6258 p = ksp + 3;
6259 continue;
6260 }
6261 }
6262
6263 /* break if there no <item> is found */
6264 if (start == NULL || end == NULL)
6265 break;
6266
Bram Moolenaar071d4272004-06-13 20:20:40 +00006267 /* Include the '>' */
6268 ++end;
6269
6270 /* Take everything up to the '<' */
6271 len = start - p;
6272 if (buf == NULL)
6273 totlen += len;
6274 else
6275 {
6276 mch_memmove(q, p, len);
6277 q += len;
6278 }
6279
6280 len = uc_check_code(start, end - start, q, cmd, eap,
6281 &split_buf, &split_len);
6282 if (len == (size_t)-1)
6283 {
6284 /* no match, continue after '<' */
6285 p = start + 1;
6286 len = 1;
6287 }
6288 else
6289 p = end;
6290 if (buf == NULL)
6291 totlen += len;
6292 else
6293 q += len;
6294 }
6295 if (buf != NULL) /* second time here, finished */
6296 {
6297 STRCPY(q, p);
6298 break;
6299 }
6300
6301 totlen += STRLEN(p); /* Add on the trailing characters */
6302 buf = alloc((unsigned)(totlen + 1));
6303 if (buf == NULL)
6304 {
6305 vim_free(split_buf);
6306 return;
6307 }
6308 }
6309
6310#ifdef FEAT_EVAL
6311 current_SID = cmd->uc_scriptID;
6312#endif
6313 (void)do_cmdline(buf, eap->getline, eap->cookie,
6314 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
6315#ifdef FEAT_EVAL
6316 current_SID = save_current_SID;
6317#endif
6318 vim_free(buf);
6319 vim_free(split_buf);
6320}
6321
6322# if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6323 static char_u *
6324get_user_command_name(idx)
6325 int idx;
6326{
6327 return get_user_commands(NULL, idx - (int)CMD_SIZE);
6328}
6329
6330/*
6331 * Function given to ExpandGeneric() to obtain the list of user command names.
6332 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006333 char_u *
6334get_user_commands(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006335 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006336 int idx;
6337{
6338 if (idx < curbuf->b_ucmds.ga_len)
6339 return USER_CMD_GA(&curbuf->b_ucmds, idx)->uc_name;
6340 idx -= curbuf->b_ucmds.ga_len;
6341 if (idx < ucmds.ga_len)
6342 return USER_CMD(idx)->uc_name;
6343 return NULL;
6344}
6345
6346/*
6347 * Function given to ExpandGeneric() to obtain the list of user command
6348 * attributes.
6349 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006350 char_u *
6351get_user_cmd_flags(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006352 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006353 int idx;
6354{
6355 static char *user_cmd_flags[] =
6356 {"bang", "bar", "buffer", "complete", "count",
6357 "nargs", "range", "register"};
6358
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00006359 if (idx >= (int)(sizeof(user_cmd_flags) / sizeof(user_cmd_flags[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006360 return NULL;
6361 return (char_u *)user_cmd_flags[idx];
6362}
6363
6364/*
6365 * Function given to ExpandGeneric() to obtain the list of values for -nargs.
6366 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006367 char_u *
6368get_user_cmd_nargs(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006369 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006370 int idx;
6371{
6372 static char *user_cmd_nargs[] = {"0", "1", "*", "?", "+"};
6373
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00006374 if (idx >= (int)(sizeof(user_cmd_nargs) / sizeof(user_cmd_nargs[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006375 return NULL;
6376 return (char_u *)user_cmd_nargs[idx];
6377}
6378
6379/*
6380 * Function given to ExpandGeneric() to obtain the list of values for -complete.
6381 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006382 char_u *
6383get_user_cmd_complete(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006384 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006385 int idx;
6386{
6387 return (char_u *)command_complete[idx].name;
6388}
6389# endif /* FEAT_CMDL_COMPL */
6390
6391#endif /* FEAT_USR_CMDS */
6392
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006393#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
6394/*
6395 * Parse a completion argument "value[vallen]".
6396 * The detected completion goes in "*complp", argument type in "*argt".
6397 * When there is an argument, for function and user defined completion, it's
6398 * copied to allocated memory and stored in "*compl_arg".
6399 * Returns FAIL if something is wrong.
6400 */
6401 int
6402parse_compl_arg(value, vallen, complp, argt, compl_arg)
6403 char_u *value;
6404 int vallen;
6405 int *complp;
6406 long *argt;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006407 char_u **compl_arg UNUSED;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006408{
6409 char_u *arg = NULL;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006410# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006411 size_t arglen = 0;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006412# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006413 int i;
6414 int valend = vallen;
6415
6416 /* Look for any argument part - which is the part after any ',' */
6417 for (i = 0; i < vallen; ++i)
6418 {
6419 if (value[i] == ',')
6420 {
6421 arg = &value[i + 1];
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006422# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006423 arglen = vallen - i - 1;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006424# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006425 valend = i;
6426 break;
6427 }
6428 }
6429
6430 for (i = 0; command_complete[i].expand != 0; ++i)
6431 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006432 if ((int)STRLEN(command_complete[i].name) == valend
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006433 && STRNCMP(value, command_complete[i].name, valend) == 0)
6434 {
6435 *complp = command_complete[i].expand;
6436 if (command_complete[i].expand == EXPAND_BUFFERS)
6437 *argt |= BUFNAME;
6438 else if (command_complete[i].expand == EXPAND_DIRECTORIES
6439 || command_complete[i].expand == EXPAND_FILES)
6440 *argt |= XFILE;
6441 break;
6442 }
6443 }
6444
6445 if (command_complete[i].expand == 0)
6446 {
6447 EMSG2(_("E180: Invalid complete value: %s"), value);
6448 return FAIL;
6449 }
6450
6451# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6452 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST
6453 && arg != NULL)
6454# else
6455 if (arg != NULL)
6456# endif
6457 {
6458 EMSG(_("E468: Completion argument only allowed for custom completion"));
6459 return FAIL;
6460 }
6461
6462# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6463 if ((*complp == EXPAND_USER_DEFINED || *complp == EXPAND_USER_LIST)
6464 && arg == NULL)
6465 {
6466 EMSG(_("E467: Custom completion requires a function argument"));
6467 return FAIL;
6468 }
6469
6470 if (arg != NULL)
6471 *compl_arg = vim_strnsave(arg, (int)arglen);
6472# endif
6473 return OK;
6474}
6475#endif
6476
Bram Moolenaar071d4272004-06-13 20:20:40 +00006477 static void
6478ex_colorscheme(eap)
6479 exarg_T *eap;
6480{
Bram Moolenaare6850792010-05-14 15:28:44 +02006481 if (*eap->arg == NUL)
6482 {
6483#ifdef FEAT_EVAL
6484 char_u *expr = vim_strsave((char_u *)"g:colors_name");
6485 char_u *p = NULL;
6486
6487 if (expr != NULL)
6488 {
6489 ++emsg_off;
6490 p = eval_to_string(expr, NULL, FALSE);
6491 --emsg_off;
6492 vim_free(expr);
6493 }
6494 if (p != NULL)
6495 {
6496 MSG(p);
6497 vim_free(p);
6498 }
6499 else
6500 MSG("default");
6501#else
6502 MSG(_("unknown"));
6503#endif
6504 }
6505 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarbe1e9e92012-09-18 16:47:07 +02006506 EMSG2(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006507}
6508
6509 static void
6510ex_highlight(eap)
6511 exarg_T *eap;
6512{
6513 if (*eap->arg == NUL && eap->cmd[2] == '!')
6514 MSG(_("Greetings, Vim user!"));
6515 do_highlight(eap->arg, eap->forceit, FALSE);
6516}
6517
6518
6519/*
6520 * Call this function if we thought we were going to exit, but we won't
6521 * (because of an error). May need to restore the terminal mode.
6522 */
6523 void
6524not_exiting()
6525{
6526 exiting = FALSE;
6527 settmode(TMODE_RAW);
6528}
6529
6530/*
6531 * ":quit": quit current window, quit Vim if closed the last window.
6532 */
6533 static void
6534ex_quit(eap)
6535 exarg_T *eap;
6536{
6537#ifdef FEAT_CMDWIN
6538 if (cmdwin_type != 0)
6539 {
6540 cmdwin_result = Ctrl_C;
6541 return;
6542 }
6543#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006544 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006545 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006546 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006547 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006548 return;
6549 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006550#ifdef FEAT_AUTOCMD
Bram Moolenaar3b53dfb2012-06-06 18:03:07 +02006551 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01006552 /* Refuse to quit when locked or when the buffer in the last window is
Bram Moolenaar362ce482012-06-06 19:02:45 +02006553 * being closed (can only happen in autocommands). */
6554 if (curbuf_locked() || (curbuf->b_nwindows == 1 && curbuf->b_closing))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006555 return;
6556#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006557
6558#ifdef FEAT_NETBEANS_INTG
6559 netbeansForcedQuit = eap->forceit;
6560#endif
6561
6562 /*
6563 * If there are more files or windows we won't exit.
6564 */
6565 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6566 exiting = TRUE;
6567 if ((!P_HID(curbuf)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01006568 && check_changed(curbuf, (p_awa ? CCGD_AW : 0)
6569 | (eap->forceit ? CCGD_FORCEIT : 0)
6570 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006571 || check_more(TRUE, eap->forceit) == FAIL
6572 || (only_one_window() && check_changed_any(eap->forceit)))
6573 {
6574 not_exiting();
6575 }
6576 else
6577 {
6578#ifdef FEAT_WINDOWS
6579 if (only_one_window()) /* quit last window */
6580#endif
6581 getout(0);
6582#ifdef FEAT_WINDOWS
6583# ifdef FEAT_GUI
6584 need_mouse_correct = TRUE;
6585# endif
6586 /* close window; may free buffer */
6587 win_close(curwin, !P_HID(curwin->w_buffer) || eap->forceit);
6588#endif
6589 }
6590}
6591
6592/*
6593 * ":cquit".
6594 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006595 static void
6596ex_cquit(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006597 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006598{
6599 getout(1); /* this does not always pass on the exit code to the Manx
6600 compiler. why? */
6601}
6602
6603/*
6604 * ":qall": try to quit all windows
6605 */
6606 static void
6607ex_quit_all(eap)
6608 exarg_T *eap;
6609{
6610# ifdef FEAT_CMDWIN
6611 if (cmdwin_type != 0)
6612 {
6613 if (eap->forceit)
6614 cmdwin_result = K_XF1; /* ex_window() takes care of this */
6615 else
6616 cmdwin_result = K_XF2;
6617 return;
6618 }
6619# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006620
6621 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006622 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006623 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006624 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006625 return;
6626 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006627#ifdef FEAT_AUTOCMD
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01006628 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
6629 /* Refuse to quit when locked or when the buffer in the last window is
6630 * being closed (can only happen in autocommands). */
6631 if (curbuf_locked() || (curbuf->b_nwindows == 1 && curbuf->b_closing))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006632 return;
6633#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006634
Bram Moolenaar071d4272004-06-13 20:20:40 +00006635 exiting = TRUE;
6636 if (eap->forceit || !check_changed_any(FALSE))
6637 getout(0);
6638 not_exiting();
6639}
6640
Bram Moolenaard9967712006-03-11 21:18:15 +00006641#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006642/*
6643 * ":close": close current window, unless it is the last one
6644 */
6645 static void
6646ex_close(eap)
6647 exarg_T *eap;
6648{
6649# ifdef FEAT_CMDWIN
6650 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02006651 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006652 else
6653# endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006654 if (!text_locked()
6655#ifdef FEAT_AUTOCMD
6656 && !curbuf_locked()
6657#endif
6658 )
Bram Moolenaarf740b292006-02-16 22:11:02 +00006659 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006660}
6661
Bram Moolenaard9967712006-03-11 21:18:15 +00006662# ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006663/*
6664 * ":pclose": Close any preview window.
6665 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006666 static void
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006667ex_pclose(eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006668 exarg_T *eap;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006669{
6670 win_T *win;
6671
6672 for (win = firstwin; win != NULL; win = win->w_next)
6673 if (win->w_p_pvw)
6674 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006675 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006676 break;
6677 }
6678}
Bram Moolenaard9967712006-03-11 21:18:15 +00006679# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006680
Bram Moolenaarf740b292006-02-16 22:11:02 +00006681/*
6682 * Close window "win" and take care of handling closing the last window for a
6683 * modified buffer.
6684 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006685 static void
Bram Moolenaarf740b292006-02-16 22:11:02 +00006686ex_win_close(forceit, win, tp)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006687 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006688 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006689 tabpage_T *tp; /* NULL or the tab page "win" is in */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006690{
6691 int need_hide;
6692 buf_T *buf = win->w_buffer;
6693
6694 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006695 if (need_hide && !P_HID(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006696 {
Bram Moolenaard9967712006-03-11 21:18:15 +00006697# if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006698 if ((p_confirm || cmdmod.confirm) && p_write)
6699 {
6700 dialog_changed(buf, FALSE);
6701 if (buf_valid(buf) && bufIsChanged(buf))
6702 return;
6703 need_hide = FALSE;
6704 }
6705 else
Bram Moolenaard9967712006-03-11 21:18:15 +00006706# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006707 {
6708 EMSG(_(e_nowrtmsg));
6709 return;
6710 }
6711 }
6712
Bram Moolenaard9967712006-03-11 21:18:15 +00006713# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006714 need_mouse_correct = TRUE;
Bram Moolenaard9967712006-03-11 21:18:15 +00006715# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006716
Bram Moolenaar071d4272004-06-13 20:20:40 +00006717 /* free buffer when not hiding it or when it's a scratch buffer */
Bram Moolenaarf740b292006-02-16 22:11:02 +00006718 if (tp == NULL)
6719 win_close(win, !need_hide && !P_HID(buf));
6720 else
6721 win_close_othertab(win, !need_hide && !P_HID(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006722}
6723
Bram Moolenaar071d4272004-06-13 20:20:40 +00006724/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006725 * ":tabclose": close current tab page, unless it is the last one.
6726 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006727 */
6728 static void
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006729ex_tabclose(eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006730 exarg_T *eap;
6731{
Bram Moolenaarf740b292006-02-16 22:11:02 +00006732 tabpage_T *tp;
6733
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006734# ifdef FEAT_CMDWIN
6735 if (cmdwin_type != 0)
6736 cmdwin_result = K_IGNORE;
6737 else
6738# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006739 if (first_tabpage->tp_next == NULL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00006740 EMSG(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00006741 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006742 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006743 if (eap->addr_count > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006744 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006745 tp = find_tabpage((int)eap->line2);
6746 if (tp == NULL)
6747 {
6748 beep_flush();
6749 return;
6750 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00006751 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006752 {
6753 tabpage_close_other(tp, eap->forceit);
6754 return;
6755 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006756 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006757 if (!text_locked()
6758#ifdef FEAT_AUTOCMD
6759 && !curbuf_locked()
6760#endif
6761 )
Bram Moolenaarf740b292006-02-16 22:11:02 +00006762 tabpage_close(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006763 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006764}
6765
6766/*
6767 * ":tabonly": close all tab pages except the current one
6768 */
6769 static void
6770ex_tabonly(eap)
6771 exarg_T *eap;
6772{
6773 tabpage_T *tp;
6774 int done;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006775
6776# ifdef FEAT_CMDWIN
6777 if (cmdwin_type != 0)
6778 cmdwin_result = K_IGNORE;
6779 else
6780# endif
6781 if (first_tabpage->tp_next == NULL)
6782 MSG(_("Already only one tab page"));
6783 else
6784 {
6785 /* Repeat this up to a 1000 times, because autocommands may mess
6786 * up the lists. */
6787 for (done = 0; done < 1000; ++done)
6788 {
6789 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6790 if (tp->tp_topframe != topframe)
6791 {
6792 tabpage_close_other(tp, eap->forceit);
6793 /* if we failed to close it quit */
6794 if (valid_tabpage(tp))
6795 done = 1000;
6796 /* start over, "tp" is now invalid */
6797 break;
6798 }
6799 if (first_tabpage->tp_next == NULL)
6800 break;
6801 }
6802 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006803}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006804
6805/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006806 * Close the current tab page.
6807 */
6808 void
6809tabpage_close(forceit)
6810 int forceit;
6811{
6812 /* First close all the windows but the current one. If that worked then
6813 * close the last window in this tab, that will close it. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006814 if (lastwin != firstwin)
6815 close_others(TRUE, forceit);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006816 if (lastwin == firstwin)
6817 ex_win_close(forceit, curwin, NULL);
6818# ifdef FEAT_GUI
6819 need_mouse_correct = TRUE;
6820# endif
6821}
6822
6823/*
6824 * Close tab page "tp", which is not the current tab page.
6825 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006826 * Also takes care of the tab pages line disappearing when closing the
6827 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00006828 */
6829 void
6830tabpage_close_other(tp, forceit)
6831 tabpage_T *tp;
6832 int forceit;
6833{
6834 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006835 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006836 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006837
6838 /* Limit to 1000 windows, autocommands may add a window while we close
6839 * one. OK, so I'm paranoid... */
6840 while (++done < 1000)
6841 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006842 wp = tp->tp_firstwin;
6843 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006844
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006845 /* Autocommands may delete the tab page under our fingers and we may
6846 * fail to close a window with a modified buffer. */
6847 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006848 break;
6849 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006850
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006851 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006852 if (h != tabline_height())
6853 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006854}
6855
6856/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006857 * ":only".
6858 */
6859 static void
6860ex_only(eap)
6861 exarg_T *eap;
6862{
6863# ifdef FEAT_GUI
6864 need_mouse_correct = TRUE;
6865# endif
6866 close_others(TRUE, eap->forceit);
6867}
6868
6869/*
6870 * ":all" and ":sall".
Bram Moolenaard9967712006-03-11 21:18:15 +00006871 * Also used for ":tab drop file ..." after setting the argument list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006872 */
Bram Moolenaard9967712006-03-11 21:18:15 +00006873 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00006874ex_all(eap)
6875 exarg_T *eap;
6876{
6877 if (eap->addr_count == 0)
6878 eap->line2 = 9999;
Bram Moolenaard9967712006-03-11 21:18:15 +00006879 do_arg_all((int)eap->line2, eap->forceit, eap->cmdidx == CMD_drop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006880}
6881#endif /* FEAT_WINDOWS */
6882
6883 static void
6884ex_hide(eap)
6885 exarg_T *eap;
6886{
6887 if (*eap->arg != NUL && check_nextcmd(eap->arg) == NULL)
6888 eap->errmsg = e_invarg;
6889 else
6890 {
6891 /* ":hide" or ":hide | cmd": hide current window */
6892 eap->nextcmd = check_nextcmd(eap->arg);
6893#ifdef FEAT_WINDOWS
6894 if (!eap->skip)
6895 {
6896# ifdef FEAT_GUI
6897 need_mouse_correct = TRUE;
6898# endif
6899 win_close(curwin, FALSE); /* don't free buffer */
6900 }
6901#endif
6902 }
6903}
6904
6905/*
6906 * ":stop" and ":suspend": Suspend Vim.
6907 */
6908 static void
6909ex_stop(eap)
6910 exarg_T *eap;
6911{
6912 /*
6913 * Disallow suspending for "rvim".
6914 */
6915 if (!check_restricted()
6916#ifdef WIN3264
6917 /*
6918 * Check if external commands are allowed now.
6919 */
6920 && can_end_termcap_mode(TRUE)
6921#endif
6922 )
6923 {
6924 if (!eap->forceit)
6925 autowrite_all();
6926 windgoto((int)Rows - 1, 0);
6927 out_char('\n');
6928 out_flush();
6929 stoptermcap();
6930 out_flush(); /* needed for SUN to restore xterm buffer */
6931#ifdef FEAT_TITLE
6932 mch_restore_title(3); /* restore window titles */
6933#endif
6934 ui_suspend(); /* call machine specific function */
6935#ifdef FEAT_TITLE
6936 maketitle();
6937 resettitle(); /* force updating the title */
6938#endif
6939 starttermcap();
6940 scroll_start(); /* scroll screen before redrawing */
6941 redraw_later_clear();
6942 shell_resized(); /* may have resized window */
6943 }
6944}
6945
6946/*
6947 * ":exit", ":xit" and ":wq": Write file and exit Vim.
6948 */
6949 static void
6950ex_exit(eap)
6951 exarg_T *eap;
6952{
6953#ifdef FEAT_CMDWIN
6954 if (cmdwin_type != 0)
6955 {
6956 cmdwin_result = Ctrl_C;
6957 return;
6958 }
6959#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006960 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006961 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006962 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006963 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006964 return;
6965 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006966#ifdef FEAT_AUTOCMD
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01006967 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
6968 /* Refuse to quit when locked or when the buffer in the last window is
6969 * being closed (can only happen in autocommands). */
6970 if (curbuf_locked() || (curbuf->b_nwindows == 1 && curbuf->b_closing))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006971 return;
6972#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006973
6974 /*
6975 * if more files or windows we won't exit
6976 */
6977 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6978 exiting = TRUE;
6979 if ( ((eap->cmdidx == CMD_wq
6980 || curbufIsChanged())
6981 && do_write(eap) == FAIL)
6982 || check_more(TRUE, eap->forceit) == FAIL
6983 || (only_one_window() && check_changed_any(eap->forceit)))
6984 {
6985 not_exiting();
6986 }
6987 else
6988 {
6989#ifdef FEAT_WINDOWS
6990 if (only_one_window()) /* quit last window, exit Vim */
6991#endif
6992 getout(0);
6993#ifdef FEAT_WINDOWS
6994# ifdef FEAT_GUI
6995 need_mouse_correct = TRUE;
6996# endif
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02006997 /* Quit current window, may free the buffer. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006998 win_close(curwin, !P_HID(curwin->w_buffer));
6999#endif
7000 }
7001}
7002
7003/*
7004 * ":print", ":list", ":number".
7005 */
7006 static void
7007ex_print(eap)
7008 exarg_T *eap;
7009{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007010 if (curbuf->b_ml.ml_flags & ML_EMPTY)
7011 EMSG(_(e_emptybuf));
7012 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007013 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007014 for ( ;!got_int; ui_breakcheck())
7015 {
7016 print_line(eap->line1,
7017 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
7018 || (eap->flags & EXFLAG_NR)),
7019 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
7020 if (++eap->line1 > eap->line2)
7021 break;
7022 out_flush(); /* show one line at a time */
7023 }
7024 setpcmark();
7025 /* put cursor at last line */
7026 curwin->w_cursor.lnum = eap->line2;
7027 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007028 }
7029
Bram Moolenaar071d4272004-06-13 20:20:40 +00007030 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007031}
7032
7033#ifdef FEAT_BYTEOFF
7034 static void
7035ex_goto(eap)
7036 exarg_T *eap;
7037{
7038 goto_byte(eap->line2);
7039}
7040#endif
7041
7042/*
7043 * ":shell".
7044 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007045 static void
7046ex_shell(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007047 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007048{
7049 do_shell(NULL, 0);
7050}
7051
7052#if (defined(FEAT_WINDOWS) && defined(HAVE_DROP_FILE)) \
7053 || (defined(FEAT_GUI_GTK) && defined(FEAT_DND)) \
Bram Moolenaar371baa92005-12-29 22:43:53 +00007054 || defined(FEAT_GUI_MSWIN) \
7055 || defined(FEAT_GUI_MAC) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00007056 || defined(PROTO)
7057
7058/*
7059 * Handle a file drop. The code is here because a drop is *nearly* like an
7060 * :args command, but not quite (we have a list of exact filenames, so we
7061 * don't want to (a) parse a command line, or (b) expand wildcards. So the
7062 * code is very similar to :args and hence needs access to a lot of the static
7063 * functions in this file.
7064 *
7065 * The list should be allocated using alloc(), as should each item in the
7066 * list. This function takes over responsibility for freeing the list.
7067 *
Bram Moolenaar81870892007-11-11 18:17:28 +00007068 * XXX The list is made into the argument list. This is freed using
Bram Moolenaar071d4272004-06-13 20:20:40 +00007069 * FreeWild(), which does a series of vim_free() calls, unless the two defines
7070 * __EMX__ and __ALWAYS_HAS_TRAILING_NUL_POINTER are set. In this case, a
7071 * routine _fnexplodefree() is used. This may cause problems, but as the drop
7072 * file functionality is (currently) not in EMX this is not presently a
7073 * problem.
7074 */
7075 void
7076handle_drop(filec, filev, split)
7077 int filec; /* the number of files dropped */
7078 char_u **filev; /* the list of files dropped */
7079 int split; /* force splitting the window */
7080{
7081 exarg_T ea;
7082 int save_msg_scroll = msg_scroll;
7083
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007084 /* Postpone this while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007085 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007086 return;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007087#ifdef FEAT_AUTOCMD
7088 if (curbuf_locked())
7089 return;
7090#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00007091 /* When the screen is being updated we should not change buffers and
7092 * windows structures, it may cause freed memory to be used. */
7093 if (updating_screen)
7094 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007095
7096 /* Check whether the current buffer is changed. If so, we will need
7097 * to split the current window or data could be lost.
7098 * We don't need to check if the 'hidden' option is set, as in this
7099 * case the buffer won't be lost.
7100 */
7101 if (!P_HID(curbuf) && !split)
7102 {
7103 ++emsg_off;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01007104 split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007105 --emsg_off;
7106 }
7107 if (split)
7108 {
7109# ifdef FEAT_WINDOWS
7110 if (win_split(0, 0) == FAIL)
7111 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007112 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007113
7114 /* When splitting the window, create a new alist. Otherwise the
7115 * existing one is overwritten. */
7116 alist_unlink(curwin->w_alist);
7117 alist_new();
7118# else
7119 return; /* can't split, always fail */
7120# endif
7121 }
7122
7123 /*
7124 * Set up the new argument list.
7125 */
Bram Moolenaar86b68352004-12-27 21:59:20 +00007126 alist_set(ALIST(curwin), filec, filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007127
7128 /*
7129 * Move to the first file.
7130 */
7131 /* Fake up a minimal "next" command for do_argfile() */
7132 vim_memset(&ea, 0, sizeof(ea));
7133 ea.cmd = (char_u *)"next";
7134 do_argfile(&ea, 0);
7135
7136 /* do_ecmd() may set need_start_insertmode, but since we never left Insert
7137 * mode that is not needed here. */
7138 need_start_insertmode = FALSE;
7139
7140 /* Restore msg_scroll, otherwise a following command may cause scrolling
7141 * unexpectedly. The screen will be redrawn by the caller, thus
7142 * msg_scroll being set by displaying a message is irrelevant. */
7143 msg_scroll = save_msg_scroll;
7144}
7145#endif
7146
Bram Moolenaar071d4272004-06-13 20:20:40 +00007147/*
7148 * Clear an argument list: free all file names and reset it to zero entries.
7149 */
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007150 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00007151alist_clear(al)
7152 alist_T *al;
7153{
7154 while (--al->al_ga.ga_len >= 0)
7155 vim_free(AARGLIST(al)[al->al_ga.ga_len].ae_fname);
7156 ga_clear(&al->al_ga);
7157}
7158
7159/*
7160 * Init an argument list.
7161 */
7162 void
7163alist_init(al)
7164 alist_T *al;
7165{
7166 ga_init2(&al->al_ga, (int)sizeof(aentry_T), 5);
7167}
7168
7169#if defined(FEAT_WINDOWS) || defined(PROTO)
7170
7171/*
7172 * Remove a reference from an argument list.
7173 * Ignored when the argument list is the global one.
7174 * If the argument list is no longer used by any window, free it.
7175 */
7176 void
7177alist_unlink(al)
7178 alist_T *al;
7179{
7180 if (al != &global_alist && --al->al_refcount <= 0)
7181 {
7182 alist_clear(al);
7183 vim_free(al);
7184 }
7185}
7186
7187# if defined(FEAT_LISTCMDS) || defined(HAVE_DROP_FILE) || defined(PROTO)
7188/*
7189 * Create a new argument list and use it for the current window.
7190 */
7191 void
7192alist_new()
7193{
7194 curwin->w_alist = (alist_T *)alloc((unsigned)sizeof(alist_T));
7195 if (curwin->w_alist == NULL)
7196 {
7197 curwin->w_alist = &global_alist;
7198 ++global_alist.al_refcount;
7199 }
7200 else
7201 {
7202 curwin->w_alist->al_refcount = 1;
7203 alist_init(curwin->w_alist);
7204 }
7205}
7206# endif
7207#endif
7208
7209#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE) || defined(PROTO)
7210/*
7211 * Expand the file names in the global argument list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00007212 * If "fnum_list" is not NULL, use "fnum_list[fnum_len]" as a list of buffer
7213 * numbers to be re-used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007214 */
7215 void
Bram Moolenaar86b68352004-12-27 21:59:20 +00007216alist_expand(fnum_list, fnum_len)
7217 int *fnum_list;
7218 int fnum_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007219{
7220 char_u **old_arg_files;
Bram Moolenaar86b68352004-12-27 21:59:20 +00007221 int old_arg_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007222 char_u **new_arg_files;
7223 int new_arg_file_count;
7224 char_u *save_p_su = p_su;
7225 int i;
7226
7227 /* Don't use 'suffixes' here. This should work like the shell did the
7228 * expansion. Also, the vimrc file isn't read yet, thus the user
7229 * can't set the options. */
7230 p_su = empty_option;
7231 old_arg_files = (char_u **)alloc((unsigned)(sizeof(char_u *) * GARGCOUNT));
7232 if (old_arg_files != NULL)
7233 {
7234 for (i = 0; i < GARGCOUNT; ++i)
Bram Moolenaar86b68352004-12-27 21:59:20 +00007235 old_arg_files[i] = vim_strsave(GARGLIST[i].ae_fname);
7236 old_arg_count = GARGCOUNT;
7237 if (expand_wildcards(old_arg_count, old_arg_files,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007238 &new_arg_file_count, &new_arg_files,
Bram Moolenaarb5609832011-07-20 15:04:58 +02007239 EW_FILE|EW_NOTFOUND|EW_ADDSLASH|EW_NOERROR) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007240 && new_arg_file_count > 0)
7241 {
Bram Moolenaar86b68352004-12-27 21:59:20 +00007242 alist_set(&global_alist, new_arg_file_count, new_arg_files,
7243 TRUE, fnum_list, fnum_len);
7244 FreeWild(old_arg_count, old_arg_files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007245 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007246 }
7247 p_su = save_p_su;
7248}
7249#endif
7250
7251/*
7252 * Set the argument list for the current window.
7253 * Takes over the allocated files[] and the allocated fnames in it.
7254 */
7255 void
Bram Moolenaar86b68352004-12-27 21:59:20 +00007256alist_set(al, count, files, use_curbuf, fnum_list, fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007257 alist_T *al;
7258 int count;
7259 char_u **files;
7260 int use_curbuf;
Bram Moolenaar86b68352004-12-27 21:59:20 +00007261 int *fnum_list;
7262 int fnum_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007263{
7264 int i;
7265
7266 alist_clear(al);
7267 if (ga_grow(&al->al_ga, count) == OK)
7268 {
7269 for (i = 0; i < count; ++i)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007270 {
7271 if (got_int)
7272 {
7273 /* When adding many buffers this can take a long time. Allow
7274 * interrupting here. */
7275 while (i < count)
7276 vim_free(files[i++]);
7277 break;
7278 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00007279
7280 /* May set buffer name of a buffer previously used for the
7281 * argument list, so that it's re-used by alist_add. */
7282 if (fnum_list != NULL && i < fnum_len)
7283 buf_set_name(fnum_list[i], files[i]);
7284
Bram Moolenaar071d4272004-06-13 20:20:40 +00007285 alist_add(al, files[i], use_curbuf ? 2 : 1);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007286 ui_breakcheck();
7287 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007288 vim_free(files);
7289 }
7290 else
7291 FreeWild(count, files);
7292#ifdef FEAT_WINDOWS
7293 if (al == &global_alist)
7294#endif
7295 arg_had_last = FALSE;
7296}
7297
7298/*
7299 * Add file "fname" to argument list "al".
7300 * "fname" must have been allocated and "al" must have been checked for room.
7301 */
7302 void
7303alist_add(al, fname, set_fnum)
7304 alist_T *al;
7305 char_u *fname;
7306 int set_fnum; /* 1: set buffer number; 2: re-use curbuf */
7307{
7308 if (fname == NULL) /* don't add NULL file names */
7309 return;
7310#ifdef BACKSLASH_IN_FILENAME
7311 slash_adjust(fname);
7312#endif
7313 AARGLIST(al)[al->al_ga.ga_len].ae_fname = fname;
7314 if (set_fnum > 0)
7315 AARGLIST(al)[al->al_ga.ga_len].ae_fnum =
7316 buflist_add(fname, BLN_LISTED | (set_fnum == 2 ? BLN_CURBUF : 0));
7317 ++al->al_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007318}
7319
7320#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
7321/*
7322 * Adjust slashes in file names. Called after 'shellslash' was set.
7323 */
7324 void
7325alist_slash_adjust()
7326{
7327 int i;
7328# ifdef FEAT_WINDOWS
7329 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007330 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007331# endif
7332
7333 for (i = 0; i < GARGCOUNT; ++i)
7334 if (GARGLIST[i].ae_fname != NULL)
7335 slash_adjust(GARGLIST[i].ae_fname);
7336# ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00007337 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007338 if (wp->w_alist != &global_alist)
7339 for (i = 0; i < WARGCOUNT(wp); ++i)
7340 if (WARGLIST(wp)[i].ae_fname != NULL)
7341 slash_adjust(WARGLIST(wp)[i].ae_fname);
7342# endif
7343}
7344#endif
7345
7346/*
7347 * ":preserve".
7348 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007349 static void
7350ex_preserve(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007351 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007352{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007353 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007354 ml_preserve(curbuf, TRUE);
7355}
7356
7357/*
7358 * ":recover".
7359 */
7360 static void
7361ex_recover(eap)
7362 exarg_T *eap;
7363{
7364 /* Set recoverymode right away to avoid the ATTENTION prompt. */
7365 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01007366 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
7367 | CCGD_MULTWIN
7368 | (eap->forceit ? CCGD_FORCEIT : 0)
7369 | CCGD_EXCMD)
7370
Bram Moolenaar071d4272004-06-13 20:20:40 +00007371 && (*eap->arg == NUL
7372 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
7373 ml_recover();
7374 recoverymode = FALSE;
7375}
7376
7377/*
7378 * Command modifier used in a wrong way.
7379 */
7380 static void
7381ex_wrongmodifier(eap)
7382 exarg_T *eap;
7383{
7384 eap->errmsg = e_invcmd;
7385}
7386
7387#ifdef FEAT_WINDOWS
7388/*
7389 * :sview [+command] file split window with new file, read-only
7390 * :split [[+command] file] split window with current or new file
7391 * :vsplit [[+command] file] split window vertically with current or new file
7392 * :new [[+command] file] split window with no or new file
7393 * :vnew [[+command] file] split vertically window with no or new file
7394 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007395 *
7396 * :tabedit open new Tab page with empty window
7397 * :tabedit [+command] file open new Tab page and edit "file"
7398 * :tabnew [[+command] file] just like :tabedit
7399 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007400 */
7401 void
7402ex_splitview(eap)
7403 exarg_T *eap;
7404{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007405 win_T *old_curwin = curwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007406# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007407 char_u *fname = NULL;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007408# endif
7409# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007410 int browse_flag = cmdmod.browse;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007411# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007412
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007413# ifndef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007414 if (eap->cmdidx == CMD_vsplit || eap->cmdidx == CMD_vnew)
7415 {
7416 ex_ni(eap);
7417 return;
7418 }
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# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007422 need_mouse_correct = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007423# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007424
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007425# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00007426 /* A ":split" in the quickfix window works like ":new". Don't want two
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007427 * quickfix windows. But it's OK when doing ":tab split". */
7428 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007429 {
7430 if (eap->cmdidx == CMD_split)
7431 eap->cmdidx = CMD_new;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007432# ifdef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007433 if (eap->cmdidx == CMD_vsplit)
7434 eap->cmdidx = CMD_vnew;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007435# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007436 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007437# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007438
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007439# ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007440 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007441 {
7442 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
7443 FNAME_MESS, TRUE, curbuf->b_ffname);
7444 if (fname == NULL)
7445 goto theend;
7446 eap->arg = fname;
7447 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007448# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007449 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007450# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007451# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007452# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007453 if (cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007454# ifdef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007455 && eap->cmdidx != CMD_vnew
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007456# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007457 && eap->cmdidx != CMD_new)
7458 {
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007459# ifdef FEAT_AUTOCMD
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007460 if (
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007461# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007462 !gui.in_use &&
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007463# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007464 au_has_group((char_u *)"FileExplorer"))
7465 {
7466 /* No browsing supported but we do have the file explorer:
7467 * Edit the directory. */
7468 if (*eap->arg == NUL || !mch_isdir(eap->arg))
7469 eap->arg = (char_u *)".";
7470 }
7471 else
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007472# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007473 {
7474 fname = do_browse(0, (char_u *)_("Edit File in new window"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007475 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007476 if (fname == NULL)
7477 goto theend;
7478 eap->arg = fname;
7479 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007480 }
7481 cmdmod.browse = FALSE; /* Don't browse again in do_ecmd(). */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007482# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007483
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007484 /*
7485 * Either open new tab page or split the window.
7486 */
7487 if (eap->cmdidx == CMD_tabedit
7488 || eap->cmdidx == CMD_tabfind
7489 || eap->cmdidx == CMD_tabnew)
7490 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00007491 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
7492 : eap->addr_count == 0 ? 0
7493 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007494 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00007495 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007496
7497 /* set the alternate buffer for the window we came from */
7498 if (curwin != old_curwin
7499 && win_valid(old_curwin)
7500 && old_curwin->w_buffer != curbuf
7501 && !cmdmod.keepalt)
7502 old_curwin->w_alt_fnum = curbuf->b_fnum;
7503 }
7504 }
7505 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007506 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
7507 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007508# ifdef FEAT_SCROLLBIND
Bram Moolenaar071d4272004-06-13 20:20:40 +00007509 /* Reset 'scrollbind' when editing another file, but keep it when
7510 * doing ":split" without arguments. */
7511 if (*eap->arg != NUL
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007512# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007513 || cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007514# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007515 )
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007516 {
7517 RESET_BINDING(curwin);
7518 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007519 else
7520 do_check_scrollbind(FALSE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007521# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007522 do_exedit(eap, old_curwin);
7523 }
7524
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007525# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007526 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007527# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007528
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007529# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007530theend:
7531 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007532# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007533}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007534
7535/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007536 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007537 */
7538 void
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007539tabpage_new()
7540{
7541 exarg_T ea;
7542
7543 vim_memset(&ea, 0, sizeof(ea));
7544 ea.cmdidx = CMD_tabnew;
7545 ea.cmd = (char_u *)"tabn";
7546 ea.arg = (char_u *)"";
7547 ex_splitview(&ea);
7548}
7549
7550/*
7551 * :tabnext command
7552 */
7553 static void
7554ex_tabnext(eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007555 exarg_T *eap;
7556{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007557 switch (eap->cmdidx)
7558 {
7559 case CMD_tabfirst:
7560 case CMD_tabrewind:
7561 goto_tabpage(1);
7562 break;
7563 case CMD_tablast:
7564 goto_tabpage(9999);
7565 break;
7566 case CMD_tabprevious:
7567 case CMD_tabNext:
7568 goto_tabpage(eap->addr_count == 0 ? -1 : -(int)eap->line2);
7569 break;
7570 default: /* CMD_tabnext */
7571 goto_tabpage(eap->addr_count == 0 ? 0 : (int)eap->line2);
7572 break;
7573 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007574}
7575
7576/*
7577 * :tabmove command
7578 */
7579 static void
7580ex_tabmove(eap)
7581 exarg_T *eap;
7582{
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02007583 int tab_number = 9999;
7584
7585 if (eap->arg && *eap->arg != NUL)
7586 {
7587 char_u *p = eap->arg;
7588 int relative = 0; /* argument +N/-N means: move N places to the
7589 * right/left relative to the current position. */
7590
7591 if (*eap->arg == '-')
7592 {
7593 relative = -1;
7594 p = eap->arg + 1;
7595 }
7596 else if (*eap->arg == '+')
7597 {
7598 relative = 1;
7599 p = eap->arg + 1;
7600 }
7601 else
7602 p = eap->arg;
7603
7604 if (p == skipdigits(p))
7605 {
7606 /* No numbers as argument. */
7607 eap->errmsg = e_invarg;
7608 return;
7609 }
7610
7611 tab_number = getdigits(&p);
7612 if (relative != 0)
7613 tab_number = tab_number * relative + tabpage_index(curtab) - 1;;
7614 }
7615 else if (eap->addr_count != 0)
7616 tab_number = eap->line2;
7617
7618 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007619}
7620
7621/*
7622 * :tabs command: List tabs and their contents.
7623 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007624 static void
7625ex_tabs(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007626 exarg_T *eap UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007627{
7628 tabpage_T *tp;
7629 win_T *wp;
7630 int tabcount = 1;
7631
7632 msg_start();
7633 msg_scroll = TRUE;
7634 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
7635 {
7636 msg_putchar('\n');
7637 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
7638 msg_outtrans_attr(IObuff, hl_attr(HLF_T));
7639 out_flush(); /* output one line at a time */
7640 ui_breakcheck();
7641
Bram Moolenaar030f0df2006-02-21 22:02:53 +00007642 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007643 wp = firstwin;
7644 else
7645 wp = tp->tp_firstwin;
7646 for ( ; wp != NULL && !got_int; wp = wp->w_next)
7647 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007648 msg_putchar('\n');
7649 msg_putchar(wp == curwin ? '>' : ' ');
7650 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007651 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
7652 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007653 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02007654 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007655 else
7656 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
7657 IObuff, IOSIZE, TRUE);
7658 msg_outtrans(IObuff);
7659 out_flush(); /* output one line at a time */
7660 ui_breakcheck();
7661 }
7662 }
7663}
7664
7665#endif /* FEAT_WINDOWS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007666
7667/*
7668 * ":mode": Set screen mode.
7669 * If no argument given, just get the screen size and redraw.
7670 */
7671 static void
7672ex_mode(eap)
7673 exarg_T *eap;
7674{
7675 if (*eap->arg == NUL)
7676 shell_resized();
7677 else
7678 mch_screenmode(eap->arg);
7679}
7680
7681#ifdef FEAT_WINDOWS
7682/*
7683 * ":resize".
7684 * set, increment or decrement current window height
7685 */
7686 static void
7687ex_resize(eap)
7688 exarg_T *eap;
7689{
7690 int n;
7691 win_T *wp = curwin;
7692
7693 if (eap->addr_count > 0)
7694 {
7695 n = eap->line2;
7696 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
7697 ;
7698 }
7699
7700#ifdef FEAT_GUI
7701 need_mouse_correct = TRUE;
7702#endif
7703 n = atol((char *)eap->arg);
7704#ifdef FEAT_VERTSPLIT
7705 if (cmdmod.split & WSP_VERT)
7706 {
7707 if (*eap->arg == '-' || *eap->arg == '+')
7708 n += W_WIDTH(curwin);
7709 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
7710 n = 9999;
7711 win_setwidth_win((int)n, wp);
7712 }
7713 else
7714#endif
7715 {
7716 if (*eap->arg == '-' || *eap->arg == '+')
7717 n += curwin->w_height;
7718 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
7719 n = 9999;
7720 win_setheight_win((int)n, wp);
7721 }
7722}
7723#endif
7724
7725/*
7726 * ":find [+command] <file>" command.
7727 */
7728 static void
7729ex_find(eap)
7730 exarg_T *eap;
7731{
7732#ifdef FEAT_SEARCHPATH
7733 char_u *fname;
7734 int count;
7735
7736 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
7737 TRUE, curbuf->b_ffname);
7738 if (eap->addr_count > 0)
7739 {
7740 /* Repeat finding the file "count" times. This matters when it
7741 * appears several times in the path. */
7742 count = eap->line2;
7743 while (fname != NULL && --count > 0)
7744 {
7745 vim_free(fname);
7746 fname = find_file_in_path(NULL, 0, FNAME_MESS,
7747 FALSE, curbuf->b_ffname);
7748 }
7749 }
7750
7751 if (fname != NULL)
7752 {
7753 eap->arg = fname;
7754#endif
7755 do_exedit(eap, NULL);
7756#ifdef FEAT_SEARCHPATH
7757 vim_free(fname);
7758 }
7759#endif
7760}
7761
7762/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00007763 * ":open" simulation: for now just work like ":visual".
7764 */
7765 static void
7766ex_open(eap)
7767 exarg_T *eap;
7768{
7769 regmatch_T regmatch;
7770 char_u *p;
7771
7772 curwin->w_cursor.lnum = eap->line2;
7773 beginline(BL_SOL | BL_FIX);
7774 if (*eap->arg == '/')
7775 {
7776 /* ":open /pattern/": put cursor in column found with pattern */
7777 ++eap->arg;
7778 p = skip_regexp(eap->arg, '/', p_magic, NULL);
7779 *p = NUL;
7780 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
7781 if (regmatch.regprog != NULL)
7782 {
7783 regmatch.rm_ic = p_ic;
7784 p = ml_get_curline();
7785 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007786 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007787 else
7788 EMSG(_(e_nomatch));
Bram Moolenaar473de612013-06-08 18:19:48 +02007789 vim_regfree(regmatch.regprog);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007790 }
7791 /* Move to the NUL, ignore any other arguments. */
7792 eap->arg += STRLEN(eap->arg);
7793 }
7794 check_cursor();
7795
7796 eap->cmdidx = CMD_visual;
7797 do_exedit(eap, NULL);
7798}
7799
7800/*
7801 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007802 */
7803 static void
7804ex_edit(eap)
7805 exarg_T *eap;
7806{
7807 do_exedit(eap, NULL);
7808}
7809
7810/*
7811 * ":edit <file>" command and alikes.
7812 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007813 void
7814do_exedit(eap, old_curwin)
7815 exarg_T *eap;
7816 win_T *old_curwin; /* curwin before doing a split or NULL */
7817{
7818 int n;
7819#ifdef FEAT_WINDOWS
7820 int need_hide;
7821#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00007822 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007823
7824 /*
7825 * ":vi" command ends Ex mode.
7826 */
7827 if (exmode_active && (eap->cmdidx == CMD_visual
7828 || eap->cmdidx == CMD_view))
7829 {
7830 exmode_active = FALSE;
7831 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007832 {
7833 /* Special case: ":global/pat/visual\NLvi-commands" */
7834 if (global_busy)
7835 {
7836 int rd = RedrawingDisabled;
7837 int nwr = no_wait_return;
7838 int ms = msg_scroll;
7839#ifdef FEAT_GUI
7840 int he = hold_gui_events;
7841#endif
7842
7843 if (eap->nextcmd != NULL)
7844 {
7845 stuffReadbuff(eap->nextcmd);
7846 eap->nextcmd = NULL;
7847 }
7848
7849 if (exmode_was != EXMODE_VIM)
7850 settmode(TMODE_RAW);
7851 RedrawingDisabled = 0;
7852 no_wait_return = 0;
7853 need_wait_return = FALSE;
7854 msg_scroll = 0;
7855#ifdef FEAT_GUI
7856 hold_gui_events = 0;
7857#endif
7858 must_redraw = CLEAR;
7859
7860 main_loop(FALSE, TRUE);
7861
7862 RedrawingDisabled = rd;
7863 no_wait_return = nwr;
7864 msg_scroll = ms;
7865#ifdef FEAT_GUI
7866 hold_gui_events = he;
7867#endif
7868 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007869 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007870 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007871 }
7872
7873 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007874 || eap->cmdidx == CMD_tabnew
7875 || eap->cmdidx == CMD_tabedit
Bram Moolenaar071d4272004-06-13 20:20:40 +00007876#ifdef FEAT_VERTSPLIT
7877 || eap->cmdidx == CMD_vnew
7878#endif
7879 ) && *eap->arg == NUL)
7880 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007881 /* ":new" or ":tabnew" without argument: edit an new empty buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007882 setpcmark();
7883 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007884 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
7885 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007886 }
7887 else if ((eap->cmdidx != CMD_split
7888#ifdef FEAT_VERTSPLIT
7889 && eap->cmdidx != CMD_vsplit
7890#endif
7891 )
7892 || *eap->arg != NUL
7893#ifdef FEAT_BROWSE
7894 || cmdmod.browse
7895#endif
7896 )
7897 {
Bram Moolenaar5555acc2006-04-07 21:33:12 +00007898#ifdef FEAT_AUTOCMD
7899 /* Can't edit another file when "curbuf_lock" is set. Only ":edit"
7900 * can bring us here, others are stopped earlier. */
7901 if (*eap->arg != NUL && curbuf_locked())
7902 return;
7903#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007904 n = readonlymode;
7905 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
7906 readonlymode = TRUE;
7907 else if (eap->cmdidx == CMD_enew)
7908 readonlymode = FALSE; /* 'readonly' doesn't make sense in an
7909 empty buffer */
7910 setpcmark();
7911 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
7912 NULL, eap,
7913 /* ":edit" goes to first line if Vi compatible */
7914 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
7915 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
7916 ? ECMD_ONE : eap->do_ecmd_lnum,
7917 (P_HID(curbuf) ? ECMD_HIDE : 0)
7918 + (eap->forceit ? ECMD_FORCEIT : 0)
7919#ifdef FEAT_LISTCMDS
7920 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
7921#endif
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007922 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007923 {
7924 /* Editing the file failed. If the window was split, close it. */
7925#ifdef FEAT_WINDOWS
7926 if (old_curwin != NULL)
7927 {
7928 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
7929 if (!need_hide || P_HID(curbuf))
7930 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007931# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7932 cleanup_T cs;
7933
7934 /* Reset the error/interrupt/exception state here so that
7935 * aborting() returns FALSE when closing a window. */
7936 enter_cleanup(&cs);
7937# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007938# ifdef FEAT_GUI
7939 need_mouse_correct = TRUE;
7940# endif
7941 win_close(curwin, !need_hide && !P_HID(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007942
7943# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7944 /* Restore the error/interrupt/exception state if not
7945 * discarded by a new aborting error, interrupt, or
7946 * uncaught exception. */
7947 leave_cleanup(&cs);
7948# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007949 }
7950 }
7951#endif
7952 }
7953 else if (readonlymode && curbuf->b_nwindows == 1)
7954 {
7955 /* When editing an already visited buffer, 'readonly' won't be set
7956 * but the previous value is kept. With ":view" and ":sview" we
7957 * want the file to be readonly, except when another window is
7958 * editing the same buffer. */
7959 curbuf->b_p_ro = TRUE;
7960 }
7961 readonlymode = n;
7962 }
7963 else
7964 {
7965 if (eap->do_ecmd_cmd != NULL)
7966 do_cmdline_cmd(eap->do_ecmd_cmd);
7967#ifdef FEAT_TITLE
7968 n = curwin->w_arg_idx_invalid;
7969#endif
7970 check_arg_idx(curwin);
7971#ifdef FEAT_TITLE
7972 if (n != curwin->w_arg_idx_invalid)
7973 maketitle();
7974#endif
7975 }
7976
7977#ifdef FEAT_WINDOWS
7978 /*
7979 * if ":split file" worked, set alternate file name in old window to new
7980 * file
7981 */
7982 if (old_curwin != NULL
7983 && *eap->arg != NUL
7984 && curwin != old_curwin
7985 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00007986 && old_curwin->w_buffer != curbuf
7987 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007988 old_curwin->w_alt_fnum = curbuf->b_fnum;
7989#endif
7990
7991 ex_no_reprint = TRUE;
7992}
7993
7994#ifndef FEAT_GUI
7995/*
7996 * ":gui" and ":gvim" when there is no GUI.
7997 */
7998 static void
7999ex_nogui(eap)
8000 exarg_T *eap;
8001{
8002 eap->errmsg = e_nogvim;
8003}
8004#endif
8005
8006#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
8007 static void
8008ex_tearoff(eap)
8009 exarg_T *eap;
8010{
8011 gui_make_tearoff(eap->arg);
8012}
8013#endif
8014
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008015#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008016 static void
8017ex_popup(eap)
8018 exarg_T *eap;
8019{
Bram Moolenaar97409f12005-07-08 22:17:29 +00008020 gui_make_popup(eap->arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008021}
8022#endif
8023
Bram Moolenaar071d4272004-06-13 20:20:40 +00008024 static void
8025ex_swapname(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008026 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008027{
8028 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
8029 MSG(_("No swap file"));
8030 else
8031 msg(curbuf->b_ml.ml_mfp->mf_fname);
8032}
8033
8034/*
8035 * ":syncbind" forces all 'scrollbind' windows to have the same relative
8036 * offset.
8037 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
8038 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008039 static void
8040ex_syncbind(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008041 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008042{
8043#ifdef FEAT_SCROLLBIND
8044 win_T *wp;
8045 long topline;
8046 long y;
8047 linenr_T old_linenr = curwin->w_cursor.lnum;
8048
8049 setpcmark();
8050
8051 /*
8052 * determine max topline
8053 */
8054 if (curwin->w_p_scb)
8055 {
8056 topline = curwin->w_topline;
8057 for (wp = firstwin; wp; wp = wp->w_next)
8058 {
8059 if (wp->w_p_scb && wp->w_buffer)
8060 {
8061 y = wp->w_buffer->b_ml.ml_line_count - p_so;
8062 if (topline > y)
8063 topline = y;
8064 }
8065 }
8066 if (topline < 1)
8067 topline = 1;
8068 }
8069 else
8070 {
8071 topline = 1;
8072 }
8073
8074
8075 /*
8076 * set all scrollbind windows to the same topline
8077 */
8078 wp = curwin;
8079 for (curwin = firstwin; curwin; curwin = curwin->w_next)
8080 {
8081 if (curwin->w_p_scb)
8082 {
8083 y = topline - curwin->w_topline;
8084 if (y > 0)
8085 scrollup(y, TRUE);
8086 else
8087 scrolldown(-y, TRUE);
8088 curwin->w_scbind_pos = topline;
8089 redraw_later(VALID);
8090 cursor_correct();
8091#ifdef FEAT_WINDOWS
8092 curwin->w_redr_status = TRUE;
8093#endif
8094 }
8095 }
8096 curwin = wp;
8097 if (curwin->w_p_scb)
8098 {
8099 did_syncbind = TRUE;
8100 checkpcmark();
8101 if (old_linenr != curwin->w_cursor.lnum)
8102 {
8103 char_u ctrl_o[2];
8104
8105 ctrl_o[0] = Ctrl_O;
8106 ctrl_o[1] = 0;
8107 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
8108 }
8109 }
8110#endif
8111}
8112
8113
8114 static void
8115ex_read(eap)
8116 exarg_T *eap;
8117{
Bram Moolenaardf177f62005-02-22 08:39:57 +00008118 int i;
8119 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
8120 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008121
8122 if (eap->usefilter) /* :r!cmd */
8123 do_bang(1, eap, FALSE, FALSE, TRUE);
8124 else
8125 {
8126 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
8127 return;
8128
8129#ifdef FEAT_BROWSE
8130 if (cmdmod.browse)
8131 {
8132 char_u *browseFile;
8133
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008134 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008135 NULL, NULL, NULL, curbuf);
8136 if (browseFile != NULL)
8137 {
8138 i = readfile(browseFile, NULL,
8139 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8140 vim_free(browseFile);
8141 }
8142 else
8143 i = OK;
8144 }
8145 else
8146#endif
8147 if (*eap->arg == NUL)
8148 {
8149 if (check_fname() == FAIL) /* check for no file name */
8150 return;
8151 i = readfile(curbuf->b_ffname, curbuf->b_fname,
8152 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8153 }
8154 else
8155 {
8156 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
8157 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
8158 i = readfile(eap->arg, NULL,
8159 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8160
8161 }
8162 if (i == FAIL)
8163 {
8164#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
8165 if (!aborting())
8166#endif
8167 EMSG2(_(e_notopen), eap->arg);
8168 }
8169 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008170 {
8171 if (empty && exmode_active)
8172 {
8173 /* Delete the empty line that remains. Historically ex does
8174 * this but vi doesn't. */
8175 if (eap->line2 == 0)
8176 lnum = curbuf->b_ml.ml_line_count;
8177 else
8178 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008179 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008180 {
8181 ml_delete(lnum, FALSE);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008182 if (curwin->w_cursor.lnum > 1
8183 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008184 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00008185 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008186 }
8187 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008188 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008189 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008190 }
8191}
8192
Bram Moolenaarea408852005-06-25 22:49:46 +00008193static char_u *prev_dir = NULL;
8194
8195#if defined(EXITFREE) || defined(PROTO)
8196 void
8197free_cd_dir()
8198{
8199 vim_free(prev_dir);
Bram Moolenaara0174af2008-01-02 20:08:25 +00008200 prev_dir = NULL;
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00008201
8202 vim_free(globaldir);
8203 globaldir = NULL;
Bram Moolenaarea408852005-06-25 22:49:46 +00008204}
8205#endif
8206
Bram Moolenaarf4258302013-06-02 18:20:17 +02008207/*
8208 * Deal with the side effects of changing the current directory.
8209 * When "local" is TRUE then this was after an ":lcd" command.
8210 */
8211 void
8212post_chdir(local)
8213 int local;
8214{
8215 vim_free(curwin->w_localdir);
8216 if (local)
8217 {
8218 /* If still in global directory, need to remember current
8219 * directory as global directory. */
8220 if (globaldir == NULL && prev_dir != NULL)
8221 globaldir = vim_strsave(prev_dir);
8222 /* Remember this local directory for the window. */
8223 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8224 curwin->w_localdir = vim_strsave(NameBuff);
8225 }
8226 else
8227 {
8228 /* We are now in the global directory, no need to remember its
8229 * name. */
8230 vim_free(globaldir);
8231 globaldir = NULL;
8232 curwin->w_localdir = NULL;
8233 }
8234
8235 shorten_fnames(TRUE);
8236}
8237
Bram Moolenaarea408852005-06-25 22:49:46 +00008238
Bram Moolenaar071d4272004-06-13 20:20:40 +00008239/*
8240 * ":cd", ":lcd", ":chdir" and ":lchdir".
8241 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00008242 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00008243ex_cd(eap)
8244 exarg_T *eap;
8245{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008246 char_u *new_dir;
8247 char_u *tofree;
8248
8249 new_dir = eap->arg;
8250#if !defined(UNIX) && !defined(VMS)
8251 /* for non-UNIX ":cd" means: print current directory */
8252 if (*new_dir == NUL)
8253 ex_pwd(NULL);
8254 else
8255#endif
8256 {
Bram Moolenaar8e8fe9b2009-03-11 14:37:32 +00008257#ifdef FEAT_AUTOCMD
8258 if (allbuf_locked())
8259 return;
8260#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00008261 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged()
8262 && !eap->forceit)
8263 {
Bram Moolenaar81870892007-11-11 18:17:28 +00008264 EMSG(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00008265 return;
8266 }
8267
Bram Moolenaar071d4272004-06-13 20:20:40 +00008268 /* ":cd -": Change to previous directory */
8269 if (STRCMP(new_dir, "-") == 0)
8270 {
8271 if (prev_dir == NULL)
8272 {
8273 EMSG(_("E186: No previous directory"));
8274 return;
8275 }
8276 new_dir = prev_dir;
8277 }
8278
8279 /* Save current directory for next ":cd -" */
8280 tofree = prev_dir;
8281 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8282 prev_dir = vim_strsave(NameBuff);
8283 else
8284 prev_dir = NULL;
8285
8286#if defined(UNIX) || defined(VMS)
8287 /* for UNIX ":cd" means: go to home directory */
8288 if (*new_dir == NUL)
8289 {
8290 /* use NameBuff for home directory name */
8291# ifdef VMS
8292 char_u *p;
8293
8294 p = mch_getenv((char_u *)"SYS$LOGIN");
8295 if (p == NULL || *p == NUL) /* empty is the same as not set */
8296 NameBuff[0] = NUL;
8297 else
Bram Moolenaarb6356332005-07-18 21:40:44 +00008298 vim_strncpy(NameBuff, p, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008299# else
8300 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
8301# endif
8302 new_dir = NameBuff;
8303 }
8304#endif
8305 if (new_dir == NULL || vim_chdir(new_dir))
8306 EMSG(_(e_failed));
8307 else
8308 {
Bram Moolenaarf4258302013-06-02 18:20:17 +02008309 post_chdir(eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008310
8311 /* Echo the new current directory if the command was typed. */
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00008312 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008313 ex_pwd(eap);
8314 }
8315 vim_free(tofree);
8316 }
8317}
8318
8319/*
8320 * ":pwd".
8321 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008322 static void
8323ex_pwd(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008324 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008325{
8326 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8327 {
8328#ifdef BACKSLASH_IN_FILENAME
8329 slash_adjust(NameBuff);
8330#endif
8331 msg(NameBuff);
8332 }
8333 else
8334 EMSG(_("E187: Unknown"));
8335}
8336
8337/*
8338 * ":=".
8339 */
8340 static void
8341ex_equal(eap)
8342 exarg_T *eap;
8343{
8344 smsg((char_u *)"%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008345 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008346}
8347
8348 static void
8349ex_sleep(eap)
8350 exarg_T *eap;
8351{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008352 int n;
8353 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008354
8355 if (cursor_valid())
8356 {
8357 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
8358 if (n >= 0)
8359 windgoto((int)n, curwin->w_wcol);
8360 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008361
8362 len = eap->line2;
8363 switch (*eap->arg)
8364 {
8365 case 'm': break;
8366 case NUL: len *= 1000L; break;
8367 default: EMSG2(_(e_invarg2), eap->arg); return;
8368 }
8369 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008370}
8371
8372/*
8373 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
8374 */
8375 void
8376do_sleep(msec)
8377 long msec;
8378{
8379 long done;
8380
8381 cursor_on();
8382 out_flush();
8383 for (done = 0; !got_int && done < msec; done += 1000L)
8384 {
8385 ui_delay(msec - done > 1000L ? 1000L : msec - done, TRUE);
8386 ui_breakcheck();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02008387#ifdef FEAT_NETBEANS_INTG
8388 /* Process the netbeans messages that may have been received in the
8389 * call to ui_breakcheck() when the GUI is in use. This may occur when
8390 * running a test case. */
8391 netbeans_parse_messages();
8392#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008393 }
8394}
8395
8396 static void
8397do_exmap(eap, isabbrev)
8398 exarg_T *eap;
8399 int isabbrev;
8400{
8401 int mode;
8402 char_u *cmdp;
8403
8404 cmdp = eap->cmd;
8405 mode = get_map_mode(&cmdp, eap->forceit || isabbrev);
8406
8407 switch (do_map((*cmdp == 'n') ? 2 : (*cmdp == 'u'),
8408 eap->arg, mode, isabbrev))
8409 {
8410 case 1: EMSG(_(e_invarg));
8411 break;
8412 case 2: EMSG(isabbrev ? _(e_noabbr) : _(e_nomap));
8413 break;
8414 }
8415}
8416
8417/*
8418 * ":winsize" command (obsolete).
8419 */
8420 static void
8421ex_winsize(eap)
8422 exarg_T *eap;
8423{
8424 int w, h;
8425 char_u *arg = eap->arg;
8426 char_u *p;
8427
8428 w = getdigits(&arg);
8429 arg = skipwhite(arg);
8430 p = arg;
8431 h = getdigits(&arg);
8432 if (*p != NUL && *arg == NUL)
8433 set_shellsize(w, h, TRUE);
8434 else
8435 EMSG(_("E465: :winsize requires two number arguments"));
8436}
8437
8438#ifdef FEAT_WINDOWS
8439 static void
8440ex_wincmd(eap)
8441 exarg_T *eap;
8442{
8443 int xchar = NUL;
8444 char_u *p;
8445
8446 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
8447 {
8448 /* CTRL-W g and CTRL-W CTRL-G have an extra command character */
8449 if (eap->arg[1] == NUL)
8450 {
8451 EMSG(_(e_invarg));
8452 return;
8453 }
8454 xchar = eap->arg[1];
8455 p = eap->arg + 2;
8456 }
8457 else
8458 p = eap->arg + 1;
8459
8460 eap->nextcmd = check_nextcmd(p);
8461 p = skipwhite(p);
8462 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
8463 EMSG(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02008464 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008465 {
8466 /* Pass flags on for ":vertical wincmd ]". */
8467 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008468 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008469 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
8470 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008471 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008472 }
8473}
8474#endif
8475
Bram Moolenaar843ee412004-06-30 16:16:41 +00008476#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008477/*
8478 * ":winpos".
8479 */
8480 static void
8481ex_winpos(eap)
8482 exarg_T *eap;
8483{
8484 int x, y;
8485 char_u *arg = eap->arg;
8486 char_u *p;
8487
8488 if (*arg == NUL)
8489 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00008490# if defined(FEAT_GUI) || defined(MSWIN)
8491# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008492 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00008493# else
8494 if (mch_get_winpos(&x, &y) != FAIL)
8495# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008496 {
8497 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
8498 msg(IObuff);
8499 }
8500 else
8501# endif
8502 EMSG(_("E188: Obtaining window position not implemented for this platform"));
8503 }
8504 else
8505 {
8506 x = getdigits(&arg);
8507 arg = skipwhite(arg);
8508 p = arg;
8509 y = getdigits(&arg);
8510 if (*p == NUL || *arg != NUL)
8511 {
8512 EMSG(_("E466: :winpos requires two number arguments"));
8513 return;
8514 }
8515# ifdef FEAT_GUI
8516 if (gui.in_use)
8517 gui_mch_set_winpos(x, y);
8518 else if (gui.starting)
8519 {
8520 /* Remember the coordinates for when the window is opened. */
8521 gui_win_x = x;
8522 gui_win_y = y;
8523 }
8524# ifdef HAVE_TGETENT
8525 else
8526# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00008527# else
8528# ifdef MSWIN
8529 mch_set_winpos(x, y);
8530# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008531# endif
8532# ifdef HAVE_TGETENT
8533 if (*T_CWP)
8534 term_set_winpos(x, y);
8535# endif
8536 }
8537}
8538#endif
8539
8540/*
8541 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
8542 */
8543 static void
8544ex_operators(eap)
8545 exarg_T *eap;
8546{
8547 oparg_T oa;
8548
8549 clear_oparg(&oa);
8550 oa.regname = eap->regname;
8551 oa.start.lnum = eap->line1;
8552 oa.end.lnum = eap->line2;
8553 oa.line_count = eap->line2 - eap->line1 + 1;
8554 oa.motion_type = MLINE;
8555#ifdef FEAT_VIRTUALEDIT
8556 virtual_op = FALSE;
8557#endif
8558 if (eap->cmdidx != CMD_yank) /* position cursor for undo */
8559 {
8560 setpcmark();
8561 curwin->w_cursor.lnum = eap->line1;
8562 beginline(BL_SOL | BL_FIX);
8563 }
8564
8565 switch (eap->cmdidx)
8566 {
8567 case CMD_delete:
8568 oa.op_type = OP_DELETE;
8569 op_delete(&oa);
8570 break;
8571
8572 case CMD_yank:
8573 oa.op_type = OP_YANK;
8574 (void)op_yank(&oa, FALSE, TRUE);
8575 break;
8576
8577 default: /* CMD_rshift or CMD_lshift */
Bram Moolenaar6e707362013-06-14 19:15:58 +02008578 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00008579#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02008580 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
8581#else
8582 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00008583#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02008584 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00008585 oa.op_type = OP_RSHIFT;
8586 else
8587 oa.op_type = OP_LSHIFT;
8588 op_shift(&oa, FALSE, eap->amount);
8589 break;
8590 }
8591#ifdef FEAT_VIRTUALEDIT
8592 virtual_op = MAYBE;
8593#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00008594 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008595}
8596
8597/*
8598 * ":put".
8599 */
8600 static void
8601ex_put(eap)
8602 exarg_T *eap;
8603{
8604 /* ":0put" works like ":1put!". */
8605 if (eap->line2 == 0)
8606 {
8607 eap->line2 = 1;
8608 eap->forceit = TRUE;
8609 }
8610 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008611 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
8612 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008613}
8614
8615/*
8616 * Handle ":copy" and ":move".
8617 */
8618 static void
8619ex_copymove(eap)
8620 exarg_T *eap;
8621{
8622 long n;
8623
8624 n = get_address(&eap->arg, FALSE, FALSE);
8625 if (eap->arg == NULL) /* error detected */
8626 {
8627 eap->nextcmd = NULL;
8628 return;
8629 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008630 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008631
8632 /*
8633 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
8634 */
8635 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
8636 {
8637 EMSG(_(e_invaddr));
8638 return;
8639 }
8640
8641 if (eap->cmdidx == CMD_move)
8642 {
8643 if (do_move(eap->line1, eap->line2, n) == FAIL)
8644 return;
8645 }
8646 else
8647 ex_copy(eap->line1, eap->line2, n);
8648 u_clearline();
8649 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008650 ex_may_print(eap);
8651}
8652
8653/*
8654 * Print the current line if flags were given to the Ex command.
8655 */
8656 static void
8657ex_may_print(eap)
8658 exarg_T *eap;
8659{
8660 if (eap->flags != 0)
8661 {
8662 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
8663 (eap->flags & EXFLAG_LIST));
8664 ex_no_reprint = TRUE;
8665 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008666}
8667
8668/*
8669 * ":smagic" and ":snomagic".
8670 */
8671 static void
8672ex_submagic(eap)
8673 exarg_T *eap;
8674{
8675 int magic_save = p_magic;
8676
8677 p_magic = (eap->cmdidx == CMD_smagic);
8678 do_sub(eap);
8679 p_magic = magic_save;
8680}
8681
8682/*
8683 * ":join".
8684 */
8685 static void
8686ex_join(eap)
8687 exarg_T *eap;
8688{
8689 curwin->w_cursor.lnum = eap->line1;
8690 if (eap->line1 == eap->line2)
8691 {
8692 if (eap->addr_count >= 2) /* :2,2join does nothing */
8693 return;
8694 if (eap->line2 == curbuf->b_ml.ml_line_count)
8695 {
8696 beep_flush();
8697 return;
8698 }
8699 ++eap->line2;
8700 }
Bram Moolenaar81340392012-06-06 16:12:59 +02008701 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008702 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008703 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008704}
8705
8706/*
8707 * ":[addr]@r" or ":[addr]*r": execute register
8708 */
8709 static void
8710ex_at(eap)
8711 exarg_T *eap;
8712{
8713 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00008714 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008715
8716 curwin->w_cursor.lnum = eap->line2;
8717
8718#ifdef USE_ON_FLY_SCROLL
8719 dont_scroll = TRUE; /* disallow scrolling here */
8720#endif
8721
8722 /* get the register name. No name means to use the previous one */
8723 c = *eap->arg;
8724 if (c == NUL || (c == '*' && *eap->cmd == '*'))
8725 c = '@';
Bram Moolenaard333d1e2006-11-07 17:43:47 +00008726 /* Put the register in the typeahead buffer with the "silent" flag. */
8727 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
8728 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008729 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008730 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00008731 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008732 else
8733 {
8734 int save_efr = exec_from_reg;
8735
8736 exec_from_reg = TRUE;
8737
8738 /*
8739 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00008740 * Continue until the stuff buffer is empty and all added characters
8741 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008742 */
Bram Moolenaar60462872009-11-03 11:40:19 +00008743 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008744 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
8745
8746 exec_from_reg = save_efr;
8747 }
8748}
8749
8750/*
8751 * ":!".
8752 */
8753 static void
8754ex_bang(eap)
8755 exarg_T *eap;
8756{
8757 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
8758}
8759
8760/*
8761 * ":undo".
8762 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008763 static void
8764ex_undo(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008765 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008766{
Bram Moolenaard3667a22006-03-16 21:35:52 +00008767 if (eap->addr_count == 1) /* :undo 123 */
Bram Moolenaar730cde92010-06-27 05:18:54 +02008768 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00008769 else
8770 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008771}
8772
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008773#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008774 static void
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008775ex_wundo(eap)
8776 exarg_T *eap;
8777{
8778 char_u hash[UNDO_HASH_SIZE];
8779
8780 u_compute_hash(hash);
8781 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
8782}
8783
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008784 static void
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008785ex_rundo(eap)
8786 exarg_T *eap;
8787{
8788 char_u hash[UNDO_HASH_SIZE];
8789
8790 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02008791 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008792}
8793#endif
8794
Bram Moolenaar071d4272004-06-13 20:20:40 +00008795/*
8796 * ":redo".
8797 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008798 static void
8799ex_redo(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008800 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008801{
8802 u_redo(1);
8803}
8804
8805/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008806 * ":earlier" and ":later".
8807 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008808 static void
8809ex_later(eap)
8810 exarg_T *eap;
8811{
8812 long count = 0;
8813 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008814 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008815 char_u *p = eap->arg;
8816
8817 if (*p == NUL)
8818 count = 1;
8819 else if (isdigit(*p))
8820 {
8821 count = getdigits(&p);
8822 switch (*p)
8823 {
8824 case 's': ++p; sec = TRUE; break;
8825 case 'm': ++p; sec = TRUE; count *= 60; break;
8826 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008827 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
8828 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008829 }
8830 }
8831
8832 if (*p != NUL)
8833 EMSG2(_(e_invarg2), eap->arg);
8834 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02008835 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
8836 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008837}
8838
8839/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008840 * ":redir": start/stop redirection.
8841 */
8842 static void
8843ex_redir(eap)
8844 exarg_T *eap;
8845{
8846 char *mode;
8847 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00008848 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008849
8850 if (STRICMP(eap->arg, "END") == 0)
8851 close_redir();
8852 else
8853 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008854 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008855 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008856 ++arg;
8857 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008858 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008859 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008860 mode = "a";
8861 }
8862 else
8863 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00008864 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008865
8866 close_redir();
8867
8868 /* Expand environment variables and "~/". */
Bram Moolenaarca472992005-01-21 11:46:23 +00008869 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008870 if (fname == NULL)
8871 return;
8872#ifdef FEAT_BROWSE
8873 if (cmdmod.browse)
8874 {
8875 char_u *browseFile;
8876
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008877 browseFile = do_browse(BROWSE_SAVE,
8878 (char_u *)_("Save Redirection"),
8879 fname, NULL, NULL, BROWSE_FILTER_ALL_FILES, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008880 if (browseFile == NULL)
8881 return; /* operation cancelled */
8882 vim_free(fname);
8883 fname = browseFile;
8884 eap->forceit = TRUE; /* since dialog already asked */
8885 }
8886#endif
8887
8888 redir_fd = open_exfile(fname, eap->forceit, mode);
8889 vim_free(fname);
8890 }
8891#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00008892 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008893 {
8894 /* redirect to a register a-z (resp. A-Z for appending) */
8895 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00008896 ++arg;
8897 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008898# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00008899 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00008900 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008901# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00008902 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008903 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008904 redir_reg = *arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008905 if (*arg == '>' && arg[1] == '>') /* append */
Bram Moolenaard9d30582005-05-18 22:10:28 +00008906 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008907 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008908 {
Bram Moolenaarc188b882007-10-19 14:20:54 +00008909 /* Can use both "@a" and "@a>". */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00008910 if (*arg == '>')
8911 arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008912 /* Make register empty when not using @A-@Z and the
8913 * command is valid. */
8914 if (*arg == NUL && !isupper(redir_reg))
8915 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008916 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008917 }
8918 if (*arg != NUL)
8919 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00008920 redir_reg = 0;
Bram Moolenaard9d30582005-05-18 22:10:28 +00008921 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008922 }
8923 }
8924 else if (*arg == '=' && arg[1] == '>')
8925 {
8926 int append;
8927
8928 /* redirect to a variable */
8929 close_redir();
8930 arg += 2;
8931
8932 if (*arg == '>')
8933 {
8934 ++arg;
8935 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008936 }
8937 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008938 append = FALSE;
8939
8940 if (var_redir_start(skipwhite(arg), append) == OK)
8941 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008942 }
8943#endif
8944
8945 /* TODO: redirect to a buffer */
8946
Bram Moolenaar071d4272004-06-13 20:20:40 +00008947 else
Bram Moolenaarca472992005-01-21 11:46:23 +00008948 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008949 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00008950
8951 /* Make sure redirection is not off. Can happen for cmdline completion
8952 * that indirectly invokes a command to catch its output. */
8953 if (redir_fd != NULL
8954#ifdef FEAT_EVAL
8955 || redir_reg || redir_vname
8956#endif
8957 )
8958 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008959}
8960
8961/*
8962 * ":redraw": force redraw
8963 */
8964 static void
8965ex_redraw(eap)
8966 exarg_T *eap;
8967{
8968 int r = RedrawingDisabled;
8969 int p = p_lz;
8970
8971 RedrawingDisabled = 0;
8972 p_lz = FALSE;
8973 update_topline();
8974 update_screen(eap->forceit ? CLEAR :
8975#ifdef FEAT_VISUAL
8976 VIsual_active ? INVERTED :
8977#endif
8978 0);
8979#ifdef FEAT_TITLE
8980 if (need_maketitle)
8981 maketitle();
8982#endif
8983 RedrawingDisabled = r;
8984 p_lz = p;
8985
8986 /* Reset msg_didout, so that a message that's there is overwritten. */
8987 msg_didout = FALSE;
8988 msg_col = 0;
8989
Bram Moolenaar56667a52013-07-17 11:54:28 +02008990 /* No need to wait after an intentional redraw. */
8991 need_wait_return = FALSE;
8992
Bram Moolenaar071d4272004-06-13 20:20:40 +00008993 out_flush();
8994}
8995
8996/*
8997 * ":redrawstatus": force redraw of status line(s)
8998 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008999 static void
9000ex_redrawstatus(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009001 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009002{
9003#if defined(FEAT_WINDOWS)
9004 int r = RedrawingDisabled;
9005 int p = p_lz;
9006
9007 RedrawingDisabled = 0;
9008 p_lz = FALSE;
9009 if (eap->forceit)
9010 status_redraw_all();
9011 else
9012 status_redraw_curbuf();
9013 update_screen(
9014# ifdef FEAT_VISUAL
9015 VIsual_active ? INVERTED :
9016# endif
9017 0);
9018 RedrawingDisabled = r;
9019 p_lz = p;
9020 out_flush();
9021#endif
9022}
9023
9024 static void
9025close_redir()
9026{
9027 if (redir_fd != NULL)
9028 {
9029 fclose(redir_fd);
9030 redir_fd = NULL;
9031 }
9032#ifdef FEAT_EVAL
9033 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009034 if (redir_vname)
9035 {
9036 var_redir_stop();
9037 redir_vname = 0;
9038 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009039#endif
9040}
9041
9042#if defined(FEAT_SESSION) && defined(USE_CRNL)
9043# define MKSESSION_NL
9044static int mksession_nl = FALSE; /* use NL only in put_eol() */
9045#endif
9046
9047/*
9048 * ":mkexrc", ":mkvimrc", ":mkview" and ":mksession".
9049 */
9050 static void
9051ex_mkrc(eap)
9052 exarg_T *eap;
9053{
9054 FILE *fd;
9055 int failed = FALSE;
9056 char_u *fname;
9057#ifdef FEAT_BROWSE
9058 char_u *browseFile = NULL;
9059#endif
9060#ifdef FEAT_SESSION
9061 int view_session = FALSE;
9062 int using_vdir = FALSE; /* using 'viewdir'? */
9063 char_u *viewFile = NULL;
9064 unsigned *flagp;
9065#endif
9066
9067 if (eap->cmdidx == CMD_mksession || eap->cmdidx == CMD_mkview)
9068 {
9069#ifdef FEAT_SESSION
9070 view_session = TRUE;
9071#else
9072 ex_ni(eap);
9073 return;
9074#endif
9075 }
9076
9077#ifdef FEAT_SESSION
Bram Moolenaar8d594672009-07-01 18:18:57 +00009078 /* Use the short file name until ":lcd" is used. We also don't use the
9079 * short file name when 'acd' is set, that is checked later. */
Bram Moolenaareeefcc72007-05-01 21:21:21 +00009080 did_lcd = FALSE;
9081
Bram Moolenaar071d4272004-06-13 20:20:40 +00009082 /* ":mkview" or ":mkview 9": generate file name with 'viewdir' */
9083 if (eap->cmdidx == CMD_mkview
9084 && (*eap->arg == NUL
9085 || (vim_isdigit(*eap->arg) && eap->arg[1] == NUL)))
9086 {
9087 eap->forceit = TRUE;
9088 fname = get_view_file(*eap->arg);
9089 if (fname == NULL)
9090 return;
9091 viewFile = fname;
9092 using_vdir = TRUE;
9093 }
9094 else
9095#endif
9096 if (*eap->arg != NUL)
9097 fname = eap->arg;
9098 else if (eap->cmdidx == CMD_mkvimrc)
9099 fname = (char_u *)VIMRC_FILE;
9100#ifdef FEAT_SESSION
9101 else if (eap->cmdidx == CMD_mksession)
9102 fname = (char_u *)SESSION_FILE;
9103#endif
9104 else
9105 fname = (char_u *)EXRC_FILE;
9106
9107#ifdef FEAT_BROWSE
9108 if (cmdmod.browse)
9109 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00009110 browseFile = do_browse(BROWSE_SAVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009111# ifdef FEAT_SESSION
9112 eap->cmdidx == CMD_mkview ? (char_u *)_("Save View") :
9113 eap->cmdidx == CMD_mksession ? (char_u *)_("Save Session") :
9114# endif
9115 (char_u *)_("Save Setup"),
9116 fname, (char_u *)"vim", NULL, BROWSE_FILTER_MACROS, NULL);
9117 if (browseFile == NULL)
9118 goto theend;
9119 fname = browseFile;
9120 eap->forceit = TRUE; /* since dialog already asked */
9121 }
9122#endif
9123
9124#if defined(FEAT_SESSION) && defined(vim_mkdir)
9125 /* When using 'viewdir' may have to create the directory. */
9126 if (using_vdir && !mch_isdir(p_vdir))
Bram Moolenaardf177f62005-02-22 08:39:57 +00009127 vim_mkdir_emsg(p_vdir, 0755);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009128#endif
9129
9130 fd = open_exfile(fname, eap->forceit, WRITEBIN);
9131 if (fd != NULL)
9132 {
9133#ifdef FEAT_SESSION
9134 if (eap->cmdidx == CMD_mkview)
9135 flagp = &vop_flags;
9136 else
9137 flagp = &ssop_flags;
9138#endif
9139
9140#ifdef MKSESSION_NL
9141 /* "unix" in 'sessionoptions': use NL line separator */
9142 if (view_session && (*flagp & SSOP_UNIX))
9143 mksession_nl = TRUE;
9144#endif
9145
9146 /* Write the version command for :mkvimrc */
9147 if (eap->cmdidx == CMD_mkvimrc)
9148 (void)put_line(fd, "version 6.0");
9149
9150#ifdef FEAT_SESSION
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009151 if (eap->cmdidx == CMD_mksession)
9152 {
9153 if (put_line(fd, "let SessionLoad = 1") == FAIL)
9154 failed = TRUE;
9155 }
9156
Bram Moolenaar071d4272004-06-13 20:20:40 +00009157 if (eap->cmdidx != CMD_mkview)
9158#endif
9159 {
9160 /* Write setting 'compatible' first, because it has side effects.
9161 * For that same reason only do it when needed. */
9162 if (p_cp)
9163 (void)put_line(fd, "if !&cp | set cp | endif");
9164 else
9165 (void)put_line(fd, "if &cp | set nocp | endif");
9166 }
9167
9168#ifdef FEAT_SESSION
9169 if (!view_session
9170 || (eap->cmdidx == CMD_mksession
9171 && (*flagp & SSOP_OPTIONS)))
9172#endif
9173 failed |= (makemap(fd, NULL) == FAIL
9174 || makeset(fd, OPT_GLOBAL, FALSE) == FAIL);
9175
9176#ifdef FEAT_SESSION
9177 if (!failed && view_session)
9178 {
9179 if (put_line(fd, "let s:so_save = &so | let s:siso_save = &siso | set so=0 siso=0") == FAIL)
9180 failed = TRUE;
9181 if (eap->cmdidx == CMD_mksession)
9182 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02009183 char_u *dirnow; /* current directory */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009184
Bram Moolenaard9462e32011-04-11 21:35:11 +02009185 dirnow = alloc(MAXPATHL);
9186 if (dirnow == NULL)
9187 failed = TRUE;
9188 else
9189 {
9190 /*
9191 * Change to session file's dir.
9192 */
9193 if (mch_dirname(dirnow, MAXPATHL) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00009194 || mch_chdir((char *)dirnow) != 0)
Bram Moolenaard9462e32011-04-11 21:35:11 +02009195 *dirnow = NUL;
9196 if (*dirnow != NUL && (ssop_flags & SSOP_SESDIR))
9197 {
9198 if (vim_chdirfile(fname) == OK)
9199 shorten_fnames(TRUE);
9200 }
9201 else if (*dirnow != NUL
9202 && (ssop_flags & SSOP_CURDIR) && globaldir != NULL)
9203 {
9204 if (mch_chdir((char *)globaldir) == 0)
9205 shorten_fnames(TRUE);
9206 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009207
Bram Moolenaard9462e32011-04-11 21:35:11 +02009208 failed |= (makeopens(fd, dirnow) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009209
Bram Moolenaard9462e32011-04-11 21:35:11 +02009210 /* restore original dir */
9211 if (*dirnow != NUL && ((ssop_flags & SSOP_SESDIR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009212 || ((ssop_flags & SSOP_CURDIR) && globaldir != NULL)))
Bram Moolenaard9462e32011-04-11 21:35:11 +02009213 {
9214 if (mch_chdir((char *)dirnow) != 0)
9215 EMSG(_(e_prev_dir));
9216 shorten_fnames(TRUE);
9217 }
9218 vim_free(dirnow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009219 }
9220 }
9221 else
9222 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +00009223 failed |= (put_view(fd, curwin, !using_vdir, flagp,
9224 -1) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009225 }
9226 if (put_line(fd, "let &so = s:so_save | let &siso = s:siso_save")
9227 == FAIL)
9228 failed = TRUE;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009229 if (put_line(fd, "doautoall SessionLoadPost") == FAIL)
9230 failed = TRUE;
Bram Moolenaar4770d092006-01-12 23:22:24 +00009231 if (eap->cmdidx == CMD_mksession)
9232 {
9233 if (put_line(fd, "unlet SessionLoad") == FAIL)
9234 failed = TRUE;
9235 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009236 }
9237#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009238 if (put_line(fd, "\" vim: set ft=vim :") == FAIL)
9239 failed = TRUE;
9240
Bram Moolenaar071d4272004-06-13 20:20:40 +00009241 failed |= fclose(fd);
9242
9243 if (failed)
9244 EMSG(_(e_write));
9245#if defined(FEAT_EVAL) && defined(FEAT_SESSION)
9246 else if (eap->cmdidx == CMD_mksession)
9247 {
9248 /* successful session write - set this_session var */
Bram Moolenaard9462e32011-04-11 21:35:11 +02009249 char_u *tbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009250
Bram Moolenaard9462e32011-04-11 21:35:11 +02009251 tbuf = alloc(MAXPATHL);
9252 if (tbuf != NULL)
9253 {
9254 if (vim_FullName(fname, tbuf, MAXPATHL, FALSE) == OK)
9255 set_vim_var_string(VV_THIS_SESSION, tbuf, -1);
9256 vim_free(tbuf);
9257 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009258 }
9259#endif
9260#ifdef MKSESSION_NL
9261 mksession_nl = FALSE;
9262#endif
9263 }
9264
9265#ifdef FEAT_BROWSE
9266theend:
9267 vim_free(browseFile);
9268#endif
9269#ifdef FEAT_SESSION
9270 vim_free(viewFile);
9271#endif
9272}
9273
Bram Moolenaardf177f62005-02-22 08:39:57 +00009274#if ((defined(FEAT_SESSION) || defined(FEAT_EVAL)) && defined(vim_mkdir)) \
9275 || defined(PROTO)
9276 int
9277vim_mkdir_emsg(name, prot)
9278 char_u *name;
Bram Moolenaar78a15312009-05-15 19:33:18 +00009279 int prot UNUSED;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009280{
9281 if (vim_mkdir(name, prot) != 0)
9282 {
9283 EMSG2(_("E739: Cannot create directory: %s"), name);
9284 return FAIL;
9285 }
9286 return OK;
9287}
9288#endif
9289
Bram Moolenaar071d4272004-06-13 20:20:40 +00009290/*
9291 * Open a file for writing for an Ex command, with some checks.
9292 * Return file descriptor, or NULL on failure.
9293 */
9294 FILE *
9295open_exfile(fname, forceit, mode)
9296 char_u *fname;
9297 int forceit;
9298 char *mode; /* "w" for create new file or "a" for append */
9299{
9300 FILE *fd;
9301
9302#ifdef UNIX
9303 /* with Unix it is possible to open a directory */
9304 if (mch_isdir(fname))
9305 {
9306 EMSG2(_(e_isadir2), fname);
9307 return NULL;
9308 }
9309#endif
9310 if (!forceit && *mode != 'a' && vim_fexists(fname))
9311 {
9312 EMSG2(_("E189: \"%s\" exists (add ! to override)"), fname);
9313 return NULL;
9314 }
9315
9316 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
9317 EMSG2(_("E190: Cannot open \"%s\" for writing"), fname);
9318
9319 return fd;
9320}
9321
9322/*
9323 * ":mark" and ":k".
9324 */
9325 static void
9326ex_mark(eap)
9327 exarg_T *eap;
9328{
9329 pos_T pos;
9330
9331 if (*eap->arg == NUL) /* No argument? */
9332 EMSG(_(e_argreq));
9333 else if (eap->arg[1] != NUL) /* more than one character? */
9334 EMSG(_(e_trailing));
9335 else
9336 {
9337 pos = curwin->w_cursor; /* save curwin->w_cursor */
9338 curwin->w_cursor.lnum = eap->line2;
9339 beginline(BL_WHITE | BL_FIX);
9340 if (setmark(*eap->arg) == FAIL) /* set mark */
9341 EMSG(_("E191: Argument must be a letter or forward/backward quote"));
9342 curwin->w_cursor = pos; /* restore curwin->w_cursor */
9343 }
9344}
9345
9346/*
9347 * Update w_topline, w_leftcol and the cursor position.
9348 */
9349 void
9350update_topline_cursor()
9351{
9352 check_cursor(); /* put cursor on valid line */
9353 update_topline();
9354 if (!curwin->w_p_wrap)
9355 validate_cursor();
9356 update_curswant();
9357}
9358
9359#ifdef FEAT_EX_EXTRA
9360/*
9361 * ":normal[!] {commands}": Execute normal mode commands.
9362 */
9363 static void
9364ex_normal(eap)
9365 exarg_T *eap;
9366{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009367 int save_msg_scroll = msg_scroll;
9368 int save_restart_edit = restart_edit;
9369 int save_msg_didout = msg_didout;
9370 int save_State = State;
9371 tasave_T tabuf;
9372 int save_insertmode = p_im;
9373 int save_finish_op = finish_op;
Bram Moolenaar38084112008-07-26 14:05:07 +00009374 int save_opcount = opcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009375#ifdef FEAT_MBYTE
9376 char_u *arg = NULL;
9377 int l;
9378 char_u *p;
9379#endif
9380
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00009381 if (ex_normal_lock > 0)
9382 {
9383 EMSG(_(e_secure));
9384 return;
9385 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009386 if (ex_normal_busy >= p_mmd)
9387 {
9388 EMSG(_("E192: Recursive use of :normal too deep"));
9389 return;
9390 }
9391 ++ex_normal_busy;
9392
9393 msg_scroll = FALSE; /* no msg scrolling in Normal mode */
9394 restart_edit = 0; /* don't go to Insert mode */
9395 p_im = FALSE; /* don't use 'insertmode' */
9396
9397#ifdef FEAT_MBYTE
9398 /*
9399 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
9400 * this for the K_SPECIAL leading byte, otherwise special keys will not
9401 * work.
9402 */
9403 if (has_mbyte)
9404 {
9405 int len = 0;
9406
9407 /* Count the number of characters to be escaped. */
9408 for (p = eap->arg; *p != NUL; ++p)
9409 {
9410# ifdef FEAT_GUI
9411 if (*p == CSI) /* leadbyte CSI */
9412 len += 2;
9413# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009414 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009415 if (*++p == K_SPECIAL /* trailbyte K_SPECIAL or CSI */
9416# ifdef FEAT_GUI
9417 || *p == CSI
9418# endif
9419 )
9420 len += 2;
9421 }
9422 if (len > 0)
9423 {
9424 arg = alloc((unsigned)(STRLEN(eap->arg) + len + 1));
9425 if (arg != NULL)
9426 {
9427 len = 0;
9428 for (p = eap->arg; *p != NUL; ++p)
9429 {
9430 arg[len++] = *p;
9431# ifdef FEAT_GUI
9432 if (*p == CSI)
9433 {
9434 arg[len++] = KS_EXTRA;
9435 arg[len++] = (int)KE_CSI;
9436 }
9437# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009438 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009439 {
9440 arg[len++] = *++p;
9441 if (*p == K_SPECIAL)
9442 {
9443 arg[len++] = KS_SPECIAL;
9444 arg[len++] = KE_FILLER;
9445 }
9446# ifdef FEAT_GUI
9447 else if (*p == CSI)
9448 {
9449 arg[len++] = KS_EXTRA;
9450 arg[len++] = (int)KE_CSI;
9451 }
9452# endif
9453 }
9454 arg[len] = NUL;
9455 }
9456 }
9457 }
9458 }
9459#endif
9460
9461 /*
9462 * Save the current typeahead. This is required to allow using ":normal"
9463 * from an event handler and makes sure we don't hang when the argument
9464 * ends with half a command.
9465 */
9466 save_typeahead(&tabuf);
9467 if (tabuf.typebuf_valid)
9468 {
9469 /*
9470 * Repeat the :normal command for each line in the range. When no
9471 * range given, execute it just once, without positioning the cursor
9472 * first.
9473 */
9474 do
9475 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009476 if (eap->addr_count != 0)
9477 {
9478 curwin->w_cursor.lnum = eap->line1++;
9479 curwin->w_cursor.col = 0;
9480 }
9481
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009482 exec_normal_cmd(
Bram Moolenaar071d4272004-06-13 20:20:40 +00009483#ifdef FEAT_MBYTE
9484 arg != NULL ? arg :
9485#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009486 eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009487 }
9488 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
9489 }
9490
9491 /* Might not return to the main loop when in an event handler. */
9492 update_topline_cursor();
9493
9494 /* Restore the previous typeahead. */
9495 restore_typeahead(&tabuf);
9496
9497 --ex_normal_busy;
9498 msg_scroll = save_msg_scroll;
9499 restart_edit = save_restart_edit;
9500 p_im = save_insertmode;
9501 finish_op = save_finish_op;
Bram Moolenaar38084112008-07-26 14:05:07 +00009502 opcount = save_opcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009503 msg_didout |= save_msg_didout; /* don't reset msg_didout now */
9504
9505 /* Restore the state (needed when called from a function executed for
9506 * 'indentexpr'). */
9507 State = save_State;
9508#ifdef FEAT_MBYTE
9509 vim_free(arg);
9510#endif
9511}
9512
9513/*
Bram Moolenaar64969662005-12-14 21:59:55 +00009514 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009515 */
9516 static void
9517ex_startinsert(eap)
9518 exarg_T *eap;
9519{
Bram Moolenaarfd371682005-01-14 21:42:54 +00009520 if (eap->forceit)
9521 {
9522 coladvance((colnr_T)MAXCOL);
9523 curwin->w_curswant = MAXCOL;
9524 curwin->w_set_curswant = FALSE;
9525 }
9526
Bram Moolenaara40c5002005-01-09 21:16:21 +00009527 /* Ignore the command when already in Insert mode. Inserting an
9528 * expression register that invokes a function can do this. */
9529 if (State & INSERT)
9530 return;
9531
Bram Moolenaar64969662005-12-14 21:59:55 +00009532 if (eap->cmdidx == CMD_startinsert)
9533 restart_edit = 'a';
9534 else if (eap->cmdidx == CMD_startreplace)
9535 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00009536 else
Bram Moolenaar64969662005-12-14 21:59:55 +00009537 restart_edit = 'V';
9538
9539 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009540 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009541 if (eap->cmdidx == CMD_startinsert)
9542 restart_edit = 'i';
Bram Moolenaar071d4272004-06-13 20:20:40 +00009543 curwin->w_curswant = 0; /* avoid MAXCOL */
9544 }
9545}
9546
9547/*
9548 * ":stopinsert"
9549 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009550 static void
9551ex_stopinsert(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009552 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009553{
9554 restart_edit = 0;
9555 stop_insert_mode = TRUE;
9556}
9557#endif
9558
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009559#if defined(FEAT_EX_EXTRA) || defined(FEAT_MENU) || defined(PROTO)
9560/*
9561 * Execute normal mode command "cmd".
9562 * "remap" can be REMAP_NONE or REMAP_YES.
9563 */
9564 void
9565exec_normal_cmd(cmd, remap, silent)
9566 char_u *cmd;
9567 int remap;
9568 int silent;
9569{
9570 oparg_T oa;
9571
9572 /*
9573 * Stuff the argument into the typeahead buffer.
9574 * Execute normal_cmd() until there is no typeahead left.
9575 */
9576 clear_oparg(&oa);
9577 finish_op = FALSE;
9578 ins_typebuf(cmd, remap, 0, TRUE, silent);
9579 while ((!stuff_empty() || (!typebuf_typed() && typebuf.tb_len > 0))
9580 && !got_int)
9581 {
9582 update_topline_cursor();
Bram Moolenaar48ac02c2011-01-17 19:50:06 +01009583 normal_cmd(&oa, TRUE); /* execute a Normal mode cmd */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009584 }
9585}
9586#endif
9587
Bram Moolenaar071d4272004-06-13 20:20:40 +00009588#ifdef FEAT_FIND_ID
9589 static void
9590ex_checkpath(eap)
9591 exarg_T *eap;
9592{
9593 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
9594 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
9595 (linenr_T)1, (linenr_T)MAXLNUM);
9596}
9597
9598#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
9599/*
9600 * ":psearch"
9601 */
9602 static void
9603ex_psearch(eap)
9604 exarg_T *eap;
9605{
9606 g_do_tagpreview = p_pvh;
9607 ex_findpat(eap);
9608 g_do_tagpreview = 0;
9609}
9610#endif
9611
9612 static void
9613ex_findpat(eap)
9614 exarg_T *eap;
9615{
9616 int whole = TRUE;
9617 long n;
9618 char_u *p;
9619 int action;
9620
9621 switch (cmdnames[eap->cmdidx].cmd_name[2])
9622 {
9623 case 'e': /* ":psearch", ":isearch" and ":dsearch" */
9624 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
9625 action = ACTION_GOTO;
9626 else
9627 action = ACTION_SHOW;
9628 break;
9629 case 'i': /* ":ilist" and ":dlist" */
9630 action = ACTION_SHOW_ALL;
9631 break;
9632 case 'u': /* ":ijump" and ":djump" */
9633 action = ACTION_GOTO;
9634 break;
9635 default: /* ":isplit" and ":dsplit" */
9636 action = ACTION_SPLIT;
9637 break;
9638 }
9639
9640 n = 1;
9641 if (vim_isdigit(*eap->arg)) /* get count */
9642 {
9643 n = getdigits(&eap->arg);
9644 eap->arg = skipwhite(eap->arg);
9645 }
9646 if (*eap->arg == '/') /* Match regexp, not just whole words */
9647 {
9648 whole = FALSE;
9649 ++eap->arg;
9650 p = skip_regexp(eap->arg, '/', p_magic, NULL);
9651 if (*p)
9652 {
9653 *p++ = NUL;
9654 p = skipwhite(p);
9655
9656 /* Check for trailing illegal characters */
9657 if (!ends_excmd(*p))
9658 eap->errmsg = e_trailing;
9659 else
9660 eap->nextcmd = check_nextcmd(p);
9661 }
9662 }
9663 if (!eap->skip)
9664 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
9665 whole, !eap->forceit,
9666 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
9667 n, action, eap->line1, eap->line2);
9668}
9669#endif
9670
9671#ifdef FEAT_WINDOWS
9672
9673# ifdef FEAT_QUICKFIX
9674/*
9675 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
9676 */
9677 static void
9678ex_ptag(eap)
9679 exarg_T *eap;
9680{
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01009681 g_do_tagpreview = p_pvh; /* will be reset to 0 in ex_tag_cmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009682 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9683}
9684
9685/*
9686 * ":pedit"
9687 */
9688 static void
9689ex_pedit(eap)
9690 exarg_T *eap;
9691{
9692 win_T *curwin_save = curwin;
9693
9694 g_do_tagpreview = p_pvh;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00009695 prepare_tagpreview(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009696 keep_help_flag = curwin_save->w_buffer->b_help;
9697 do_exedit(eap, NULL);
9698 keep_help_flag = FALSE;
9699 if (curwin != curwin_save && win_valid(curwin_save))
9700 {
9701 /* Return cursor to where we were */
9702 validate_cursor();
9703 redraw_later(VALID);
9704 win_enter(curwin_save, TRUE);
9705 }
9706 g_do_tagpreview = 0;
9707}
9708# endif
9709
9710/*
9711 * ":stag", ":stselect" and ":stjump".
9712 */
9713 static void
9714ex_stag(eap)
9715 exarg_T *eap;
9716{
9717 postponed_split = -1;
9718 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009719 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009720 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9721 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009722 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009723}
9724#endif
9725
9726/*
9727 * ":tag", ":tselect", ":tjump", ":tnext", etc.
9728 */
9729 static void
9730ex_tag(eap)
9731 exarg_T *eap;
9732{
9733 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
9734}
9735
9736 static void
9737ex_tag_cmd(eap, name)
9738 exarg_T *eap;
9739 char_u *name;
9740{
9741 int cmd;
9742
9743 switch (name[1])
9744 {
9745 case 'j': cmd = DT_JUMP; /* ":tjump" */
9746 break;
9747 case 's': cmd = DT_SELECT; /* ":tselect" */
9748 break;
9749 case 'p': cmd = DT_PREV; /* ":tprevious" */
9750 break;
9751 case 'N': cmd = DT_PREV; /* ":tNext" */
9752 break;
9753 case 'n': cmd = DT_NEXT; /* ":tnext" */
9754 break;
9755 case 'o': cmd = DT_POP; /* ":pop" */
9756 break;
9757 case 'f': /* ":tfirst" */
9758 case 'r': cmd = DT_FIRST; /* ":trewind" */
9759 break;
9760 case 'l': cmd = DT_LAST; /* ":tlast" */
9761 break;
9762 default: /* ":tag" */
9763#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +00009764 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009765 {
9766 do_cstag(eap);
9767 return;
9768 }
9769#endif
9770 cmd = DT_TAG;
9771 break;
9772 }
9773
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00009774 if (name[0] == 'l')
9775 {
9776#ifndef FEAT_QUICKFIX
9777 ex_ni(eap);
9778 return;
9779#else
9780 cmd = DT_LTAG;
9781#endif
9782 }
9783
Bram Moolenaar071d4272004-06-13 20:20:40 +00009784 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
9785 eap->forceit, TRUE);
9786}
9787
9788/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009789 * Check "str" for starting with a special cmdline variable.
9790 * If found return one of the SPEC_ values and set "*usedlen" to the length of
9791 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
9792 */
9793 int
9794find_cmdline_var(src, usedlen)
9795 char_u *src;
9796 int *usedlen;
9797{
9798 int len;
9799 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +00009800 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009801 "%",
9802#define SPEC_PERC 0
9803 "#",
9804#define SPEC_HASH 1
9805 "<cword>", /* cursor word */
9806#define SPEC_CWORD 2
9807 "<cWORD>", /* cursor WORD */
9808#define SPEC_CCWORD 3
9809 "<cfile>", /* cursor path name */
9810#define SPEC_CFILE 4
9811 "<sfile>", /* ":so" file name */
9812#define SPEC_SFILE 5
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009813 "<slnum>", /* ":so" file line number */
9814#define SPEC_SLNUM 6
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009815#ifdef FEAT_AUTOCMD
9816 "<afile>", /* autocommand file name */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009817# define SPEC_AFILE 7
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009818 "<abuf>", /* autocommand buffer number */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009819# define SPEC_ABUF 8
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009820 "<amatch>", /* autocommand match name */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009821# define SPEC_AMATCH 9
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009822#endif
9823#ifdef FEAT_CLIENTSERVER
9824 "<client>"
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009825# ifdef FEAT_AUTOCMD
9826# define SPEC_CLIENT 10
9827# else
9828# define SPEC_CLIENT 7
9829# endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009830#endif
9831 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009832
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009833 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009834 {
9835 len = (int)STRLEN(spec_str[i]);
9836 if (STRNCMP(src, spec_str[i], len) == 0)
9837 {
9838 *usedlen = len;
9839 return i;
9840 }
9841 }
9842 return -1;
9843}
9844
9845/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009846 * Evaluate cmdline variables.
9847 *
9848 * change '%' to curbuf->b_ffname
9849 * '#' to curwin->w_altfile
9850 * '<cword>' to word under the cursor
9851 * '<cWORD>' to WORD under the cursor
9852 * '<cfile>' to path name under the cursor
9853 * '<sfile>' to sourced file name
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009854 * '<slnum>' to sourced file line number
Bram Moolenaar071d4272004-06-13 20:20:40 +00009855 * '<afile>' to file name for autocommand
9856 * '<abuf>' to buffer number for autocommand
9857 * '<amatch>' to matching name for autocommand
9858 *
9859 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
9860 * "" for error without a message) and NULL is returned.
9861 * Returns an allocated string if a valid match was found.
9862 * Returns NULL if no match was found. "usedlen" then still contains the
9863 * number of characters to skip.
9864 */
9865 char_u *
Bram Moolenaar63b92542007-03-27 14:57:09 +00009866eval_vars(src, srcstart, usedlen, lnump, errormsg, escaped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009867 char_u *src; /* pointer into commandline */
Bram Moolenaar63b92542007-03-27 14:57:09 +00009868 char_u *srcstart; /* beginning of valid memory for src */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009869 int *usedlen; /* characters after src that are used */
9870 linenr_T *lnump; /* line number for :e command, or NULL */
9871 char_u **errormsg; /* pointer to error message */
Bram Moolenaar63b92542007-03-27 14:57:09 +00009872 int *escaped; /* return value has escaped white space (can
9873 * be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009874{
9875 int i;
9876 char_u *s;
9877 char_u *result;
9878 char_u *resultbuf = NULL;
9879 int resultlen;
9880 buf_T *buf;
9881 int valid = VALID_HEAD + VALID_PATH; /* assume valid result */
9882 int spec_idx;
9883#ifdef FEAT_MODIFY_FNAME
9884 int skip_mod = FALSE;
9885#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009886 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009887
9888 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009889 if (escaped != NULL)
9890 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009891
9892 /*
9893 * Check if there is something to do.
9894 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009895 spec_idx = find_cmdline_var(src, usedlen);
9896 if (spec_idx < 0) /* no match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009897 {
9898 *usedlen = 1;
9899 return NULL;
9900 }
9901
9902 /*
9903 * Skip when preceded with a backslash "\%" and "\#".
9904 * Note: In "\\%" the % is also not recognized!
9905 */
9906 if (src > srcstart && src[-1] == '\\')
9907 {
9908 *usedlen = 0;
Bram Moolenaara7241f52008-06-24 20:39:31 +00009909 STRMOVE(src - 1, src); /* remove backslash */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009910 return NULL;
9911 }
9912
9913 /*
9914 * word or WORD under cursor
9915 */
9916 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD)
9917 {
9918 resultlen = find_ident_under_cursor(&result, spec_idx == SPEC_CWORD ?
9919 (FIND_IDENT|FIND_STRING) : FIND_STRING);
9920 if (resultlen == 0)
9921 {
9922 *errormsg = (char_u *)"";
9923 return NULL;
9924 }
9925 }
9926
9927 /*
9928 * '#': Alternate file name
9929 * '%': Current file name
9930 * File name under the cursor
9931 * File name for autocommand
9932 * and following modifiers
9933 */
9934 else
9935 {
9936 switch (spec_idx)
9937 {
9938 case SPEC_PERC: /* '%': current file */
9939 if (curbuf->b_fname == NULL)
9940 {
9941 result = (char_u *)"";
9942 valid = 0; /* Must have ":p:h" to be valid */
9943 }
9944 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009945 result = curbuf->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009946 break;
9947
9948 case SPEC_HASH: /* '#' or "#99": alternate file */
9949 if (src[1] == '#') /* "##": the argument list */
9950 {
9951 result = arg_all();
9952 resultbuf = result;
9953 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009954 if (escaped != NULL)
9955 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009956#ifdef FEAT_MODIFY_FNAME
9957 skip_mod = TRUE;
9958#endif
9959 break;
9960 }
9961 s = src + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00009962 if (*s == '<') /* "#<99" uses v:oldfiles */
9963 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009964 i = (int)getdigits(&s);
9965 *usedlen = (int)(s - src); /* length of what we expand */
9966
Bram Moolenaard812df62008-11-09 12:46:09 +00009967 if (src[1] == '<')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009968 {
Bram Moolenaard812df62008-11-09 12:46:09 +00009969 if (*usedlen < 2)
9970 {
9971 /* Should we give an error message for #<text? */
9972 *usedlen = 1;
9973 return NULL;
9974 }
9975#ifdef FEAT_EVAL
9976 result = list_find_str(get_vim_var_list(VV_OLDFILES),
9977 (long)i);
9978 if (result == NULL)
9979 {
9980 *errormsg = (char_u *)"";
9981 return NULL;
9982 }
9983#else
9984 *errormsg = (char_u *)_("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009985 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00009986#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009987 }
9988 else
Bram Moolenaard812df62008-11-09 12:46:09 +00009989 {
9990 buf = buflist_findnr(i);
9991 if (buf == NULL)
9992 {
9993 *errormsg = (char_u *)_("E194: No alternate file name to substitute for '#'");
9994 return NULL;
9995 }
9996 if (lnump != NULL)
9997 *lnump = ECMD_LAST;
9998 if (buf->b_fname == NULL)
9999 {
10000 result = (char_u *)"";
10001 valid = 0; /* Must have ":p:h" to be valid */
10002 }
10003 else
10004 result = buf->b_fname;
10005 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010006 break;
10007
10008#ifdef FEAT_SEARCHPATH
10009 case SPEC_CFILE: /* file name under cursor */
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010010 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010011 if (result == NULL)
10012 {
10013 *errormsg = (char_u *)"";
10014 return NULL;
10015 }
10016 resultbuf = result; /* remember allocated string */
10017 break;
10018#endif
10019
10020#ifdef FEAT_AUTOCMD
10021 case SPEC_AFILE: /* file name for autocommand */
10022 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +000010023 if (result != NULL && !autocmd_fname_full)
10024 {
10025 /* Still need to turn the fname into a full path. It is
10026 * postponed to avoid a delay when <afile> is not used. */
10027 autocmd_fname_full = TRUE;
10028 result = FullName_save(autocmd_fname, FALSE);
10029 vim_free(autocmd_fname);
10030 autocmd_fname = result;
10031 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010032 if (result == NULL)
10033 {
10034 *errormsg = (char_u *)_("E495: no autocommand file name to substitute for \"<afile>\"");
10035 return NULL;
10036 }
Bram Moolenaara0174af2008-01-02 20:08:25 +000010037 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010038 break;
10039
10040 case SPEC_ABUF: /* buffer number for autocommand */
10041 if (autocmd_bufnr <= 0)
10042 {
10043 *errormsg = (char_u *)_("E496: no autocommand buffer number to substitute for \"<abuf>\"");
10044 return NULL;
10045 }
10046 sprintf((char *)strbuf, "%d", autocmd_bufnr);
10047 result = strbuf;
10048 break;
10049
10050 case SPEC_AMATCH: /* match name for autocommand */
10051 result = autocmd_match;
10052 if (result == NULL)
10053 {
10054 *errormsg = (char_u *)_("E497: no autocommand match name to substitute for \"<amatch>\"");
10055 return NULL;
10056 }
10057 break;
10058
10059#endif
10060 case SPEC_SFILE: /* file name for ":so" command */
10061 result = sourcing_name;
10062 if (result == NULL)
10063 {
10064 *errormsg = (char_u *)_("E498: no :source file name to substitute for \"<sfile>\"");
10065 return NULL;
10066 }
10067 break;
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010068 case SPEC_SLNUM: /* line in file for ":so" command */
10069 if (sourcing_name == NULL || sourcing_lnum == 0)
10070 {
10071 *errormsg = (char_u *)_("E842: no line number to use for \"<slnum>\"");
10072 return NULL;
10073 }
10074 sprintf((char *)strbuf, "%ld", (long)sourcing_lnum);
10075 result = strbuf;
10076 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010077#if defined(FEAT_CLIENTSERVER)
10078 case SPEC_CLIENT: /* Source of last submitted input */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010079 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
10080 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010081 result = strbuf;
10082 break;
10083#endif
10084 }
10085
10086 resultlen = (int)STRLEN(result); /* length of new string */
10087 if (src[*usedlen] == '<') /* remove the file name extension */
10088 {
10089 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010090 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010091 resultlen = (int)(s - result);
10092 }
10093#ifdef FEAT_MODIFY_FNAME
10094 else if (!skip_mod)
10095 {
10096 valid |= modify_fname(src, usedlen, &result, &resultbuf,
10097 &resultlen);
10098 if (result == NULL)
10099 {
10100 *errormsg = (char_u *)"";
10101 return NULL;
10102 }
10103 }
10104#endif
10105 }
10106
10107 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
10108 {
10109 if (valid != VALID_HEAD + VALID_PATH)
10110 /* xgettext:no-c-format */
10111 *errormsg = (char_u *)_("E499: Empty file name for '%' or '#', only works with \":p:h\"");
10112 else
10113 *errormsg = (char_u *)_("E500: Evaluates to an empty string");
10114 result = NULL;
10115 }
10116 else
10117 result = vim_strnsave(result, resultlen);
10118 vim_free(resultbuf);
10119 return result;
10120}
10121
10122/*
10123 * Concatenate all files in the argument list, separated by spaces, and return
10124 * it in one allocated string.
10125 * Spaces and backslashes in the file names are escaped with a backslash.
10126 * Returns NULL when out of memory.
10127 */
10128 static char_u *
10129arg_all()
10130{
10131 int len;
10132 int idx;
10133 char_u *retval = NULL;
10134 char_u *p;
10135
10136 /*
10137 * Do this loop two times:
10138 * first time: compute the total length
10139 * second time: concatenate the names
10140 */
10141 for (;;)
10142 {
10143 len = 0;
10144 for (idx = 0; idx < ARGCOUNT; ++idx)
10145 {
10146 p = alist_name(&ARGLIST[idx]);
10147 if (p != NULL)
10148 {
10149 if (len > 0)
10150 {
10151 /* insert a space in between names */
10152 if (retval != NULL)
10153 retval[len] = ' ';
10154 ++len;
10155 }
10156 for ( ; *p != NUL; ++p)
10157 {
10158 if (*p == ' ' || *p == '\\')
10159 {
10160 /* insert a backslash */
10161 if (retval != NULL)
10162 retval[len] = '\\';
10163 ++len;
10164 }
10165 if (retval != NULL)
10166 retval[len] = *p;
10167 ++len;
10168 }
10169 }
10170 }
10171
10172 /* second time: break here */
10173 if (retval != NULL)
10174 {
10175 retval[len] = NUL;
10176 break;
10177 }
10178
10179 /* allocate memory */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000010180 retval = alloc((unsigned)len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010181 if (retval == NULL)
10182 break;
10183 }
10184
10185 return retval;
10186}
10187
10188#if defined(FEAT_AUTOCMD) || defined(PROTO)
10189/*
10190 * Expand the <sfile> string in "arg".
10191 *
10192 * Returns an allocated string, or NULL for any error.
10193 */
10194 char_u *
10195expand_sfile(arg)
10196 char_u *arg;
10197{
10198 char_u *errormsg;
10199 int len;
10200 char_u *result;
10201 char_u *newres;
10202 char_u *repl;
10203 int srclen;
10204 char_u *p;
10205
10206 result = vim_strsave(arg);
10207 if (result == NULL)
10208 return NULL;
10209
10210 for (p = result; *p; )
10211 {
10212 if (STRNCMP(p, "<sfile>", 7) != 0)
10213 ++p;
10214 else
10215 {
10216 /* replace "<sfile>" with the sourced file name, and do ":" stuff */
Bram Moolenaar63b92542007-03-27 14:57:09 +000010217 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010218 if (errormsg != NULL)
10219 {
10220 if (*errormsg)
10221 emsg(errormsg);
10222 vim_free(result);
10223 return NULL;
10224 }
10225 if (repl == NULL) /* no match (cannot happen) */
10226 {
10227 p += srclen;
10228 continue;
10229 }
10230 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
10231 newres = alloc(len);
10232 if (newres == NULL)
10233 {
10234 vim_free(repl);
10235 vim_free(result);
10236 return NULL;
10237 }
10238 mch_memmove(newres, result, (size_t)(p - result));
10239 STRCPY(newres + (p - result), repl);
10240 len = (int)STRLEN(newres);
10241 STRCAT(newres, p + srclen);
10242 vim_free(repl);
10243 vim_free(result);
10244 result = newres;
10245 p = newres + len; /* continue after the match */
10246 }
10247 }
10248
10249 return result;
10250}
10251#endif
10252
10253#ifdef FEAT_SESSION
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010254static int ses_winsizes __ARGS((FILE *fd, int restore_size,
10255 win_T *tab_firstwin));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010256static int ses_win_rec __ARGS((FILE *fd, frame_T *fr));
10257static frame_T *ses_skipframe __ARGS((frame_T *fr));
10258static int ses_do_frame __ARGS((frame_T *fr));
10259static int ses_do_win __ARGS((win_T *wp));
10260static int ses_arglist __ARGS((FILE *fd, char *cmd, garray_T *gap, int fullname, unsigned *flagp));
10261static int ses_put_fname __ARGS((FILE *fd, char_u *name, unsigned *flagp));
10262static int ses_fname __ARGS((FILE *fd, buf_T *buf, unsigned *flagp));
10263
10264/*
10265 * Write openfile commands for the current buffers to an .exrc file.
10266 * Return FAIL on error, OK otherwise.
10267 */
10268 static int
10269makeopens(fd, dirnow)
10270 FILE *fd;
10271 char_u *dirnow; /* Current directory name */
10272{
10273 buf_T *buf;
10274 int only_save_windows = TRUE;
10275 int nr;
10276 int cnr = 1;
10277 int restore_size = TRUE;
10278 win_T *wp;
10279 char_u *sname;
10280 win_T *edited_win = NULL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010281 int tabnr;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010282 win_T *tab_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010283 frame_T *tab_topframe;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010284 int cur_arg_idx = 0;
Bram Moolenaar383c6f52008-01-04 15:01:07 +000010285 int next_arg_idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010286
10287 if (ssop_flags & SSOP_BUFFERS)
10288 only_save_windows = FALSE; /* Save ALL buffers */
10289
10290 /*
10291 * Begin by setting the this_session variable, and then other
10292 * sessionable variables.
10293 */
10294#ifdef FEAT_EVAL
10295 if (put_line(fd, "let v:this_session=expand(\"<sfile>:p\")") == FAIL)
10296 return FAIL;
10297 if (ssop_flags & SSOP_GLOBALS)
10298 if (store_session_globals(fd) == FAIL)
10299 return FAIL;
10300#endif
10301
10302 /*
10303 * Close all windows but one.
10304 */
10305 if (put_line(fd, "silent only") == FAIL)
10306 return FAIL;
10307
10308 /*
10309 * Now a :cd command to the session directory or the current directory
10310 */
10311 if (ssop_flags & SSOP_SESDIR)
10312 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010313 if (put_line(fd, "exe \"cd \" . escape(expand(\"<sfile>:p:h\"), ' ')")
10314 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010315 return FAIL;
10316 }
10317 else if (ssop_flags & SSOP_CURDIR)
10318 {
10319 sname = home_replace_save(NULL, globaldir != NULL ? globaldir : dirnow);
10320 if (sname == NULL
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010321 || fputs("cd ", fd) < 0
10322 || ses_put_fname(fd, sname, &ssop_flags) == FAIL
10323 || put_eol(fd) == FAIL)
10324 {
10325 vim_free(sname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010326 return FAIL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010327 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010328 vim_free(sname);
10329 }
10330
10331 /*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010332 * If there is an empty, unnamed buffer we will wipe it out later.
10333 * Remember the buffer number.
10334 */
10335 if (put_line(fd, "if expand('%') == '' && !&modified && line('$') <= 1 && getline(1) == ''") == FAIL)
10336 return FAIL;
10337 if (put_line(fd, " let s:wipebuf = bufnr('%')") == FAIL)
10338 return FAIL;
10339 if (put_line(fd, "endif") == FAIL)
10340 return FAIL;
10341
10342 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010343 * Now save the current files, current buffer first.
10344 */
10345 if (put_line(fd, "set shortmess=aoO") == FAIL)
10346 return FAIL;
10347
10348 /* Now put the other buffers into the buffer list */
10349 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
10350 {
10351 if (!(only_save_windows && buf->b_nwindows == 0)
10352 && !(buf->b_help && !(ssop_flags & SSOP_HELP))
10353 && buf->b_fname != NULL
10354 && buf->b_p_bl)
10355 {
10356 if (fprintf(fd, "badd +%ld ", buf->b_wininfo == NULL ? 1L
10357 : buf->b_wininfo->wi_fpos.lnum) < 0
10358 || ses_fname(fd, buf, &ssop_flags) == FAIL)
10359 return FAIL;
10360 }
10361 }
10362
10363 /* the global argument list */
10364 if (ses_arglist(fd, "args", &global_alist.al_ga,
10365 !(ssop_flags & SSOP_CURDIR), &ssop_flags) == FAIL)
10366 return FAIL;
10367
10368 if (ssop_flags & SSOP_RESIZE)
10369 {
10370 /* Note: after the restore we still check it worked!*/
10371 if (fprintf(fd, "set lines=%ld columns=%ld" , Rows, Columns) < 0
10372 || put_eol(fd) == FAIL)
10373 return FAIL;
10374 }
10375
10376#ifdef FEAT_GUI
10377 if (gui.in_use && (ssop_flags & SSOP_WINPOS))
10378 {
10379 int x, y;
10380
10381 if (gui_mch_get_winpos(&x, &y) == OK)
10382 {
10383 /* Note: after the restore we still check it worked!*/
10384 if (fprintf(fd, "winpos %d %d", x, y) < 0 || put_eol(fd) == FAIL)
10385 return FAIL;
10386 }
10387 }
10388#endif
10389
10390 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000010391 * May repeat putting Windows for each tab, when "tabpages" is in
10392 * 'sessionoptions'.
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010393 * Don't use goto_tabpage(), it may change directory and trigger
10394 * autocommands.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010395 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010396 tab_firstwin = firstwin; /* first window in tab page "tabnr" */
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010397 tab_topframe = topframe;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010398 for (tabnr = 1; ; ++tabnr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010399 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010400 int need_tabnew = FALSE;
10401
Bram Moolenaar18144c82006-04-12 21:52:12 +000010402 if ((ssop_flags & SSOP_TABPAGES))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010403 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010404 tabpage_T *tp = find_tabpage(tabnr);
10405
10406 if (tp == NULL)
10407 break; /* done all tab pages */
10408 if (tp == curtab)
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010409 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010410 tab_firstwin = firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010411 tab_topframe = topframe;
10412 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010413 else
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010414 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010415 tab_firstwin = tp->tp_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010416 tab_topframe = tp->tp_topframe;
10417 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010418 if (tabnr > 1)
10419 need_tabnew = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010420 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010421
Bram Moolenaar18144c82006-04-12 21:52:12 +000010422 /*
10423 * Before creating the window layout, try loading one file. If this
10424 * is aborted we don't end up with a number of useless windows.
10425 * This may have side effects! (e.g., compressed or network file).
10426 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010427 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010428 {
10429 if (ses_do_win(wp)
10430 && wp->w_buffer->b_ffname != NULL
10431 && !wp->w_buffer->b_help
10432#ifdef FEAT_QUICKFIX
10433 && !bt_nofile(wp->w_buffer)
10434#endif
10435 )
10436 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010437 if (fputs(need_tabnew ? "tabedit " : "edit ", fd) < 0
Bram Moolenaar18144c82006-04-12 21:52:12 +000010438 || ses_fname(fd, wp->w_buffer, &ssop_flags) == FAIL)
10439 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010440 need_tabnew = FALSE;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010441 if (!wp->w_arg_idx_invalid)
10442 edited_win = wp;
10443 break;
10444 }
10445 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010446
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010447 /* If no file got edited create an empty tab page. */
10448 if (need_tabnew && put_line(fd, "tabnew") == FAIL)
10449 return FAIL;
10450
Bram Moolenaar18144c82006-04-12 21:52:12 +000010451 /*
10452 * Save current window layout.
10453 */
10454 if (put_line(fd, "set splitbelow splitright") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010455 return FAIL;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010456 if (ses_win_rec(fd, tab_topframe) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010457 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010458 if (!p_sb && put_line(fd, "set nosplitbelow") == FAIL)
10459 return FAIL;
10460 if (!p_spr && put_line(fd, "set nosplitright") == FAIL)
10461 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010462
Bram Moolenaar18144c82006-04-12 21:52:12 +000010463 /*
10464 * Check if window sizes can be restored (no windows omitted).
10465 * Remember the window number of the current window after restoring.
10466 */
10467 nr = 0;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010468 for (wp = tab_firstwin; wp != NULL; wp = W_NEXT(wp))
Bram Moolenaar18144c82006-04-12 21:52:12 +000010469 {
10470 if (ses_do_win(wp))
10471 ++nr;
10472 else
10473 restore_size = FALSE;
10474 if (curwin == wp)
10475 cnr = nr;
10476 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010477
Bram Moolenaar18144c82006-04-12 21:52:12 +000010478 /* Go to the first window. */
10479 if (put_line(fd, "wincmd t") == FAIL)
10480 return FAIL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010481
Bram Moolenaar18144c82006-04-12 21:52:12 +000010482 /*
10483 * If more than one window, see if sizes can be restored.
10484 * First set 'winheight' and 'winwidth' to 1 to avoid the windows being
10485 * resized when moving between windows.
10486 * Do this before restoring the view, so that the topline and the
10487 * cursor can be set. This is done again below.
10488 */
10489 if (put_line(fd, "set winheight=1 winwidth=1") == FAIL)
10490 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010491 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010492 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010493
Bram Moolenaar18144c82006-04-12 21:52:12 +000010494 /*
10495 * Restore the view of the window (options, file, cursor, etc.).
10496 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010497 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010498 {
10499 if (!ses_do_win(wp))
10500 continue;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010501 if (put_view(fd, wp, wp != edited_win, &ssop_flags,
10502 cur_arg_idx) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010503 return FAIL;
10504 if (nr > 1 && put_line(fd, "wincmd w") == FAIL)
10505 return FAIL;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010506 next_arg_idx = wp->w_arg_idx;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010507 }
10508
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010509 /* The argument index in the first tab page is zero, need to set it in
10510 * each window. For further tab pages it's the window where we do
10511 * "tabedit". */
10512 cur_arg_idx = next_arg_idx;
10513
Bram Moolenaar18144c82006-04-12 21:52:12 +000010514 /*
10515 * Restore cursor to the current window if it's not the first one.
10516 */
10517 if (cnr > 1 && (fprintf(fd, "%dwincmd w", cnr) < 0
10518 || put_eol(fd) == FAIL))
10519 return FAIL;
10520
10521 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000010522 * Restore window sizes again after jumping around in windows, because
10523 * the current window has a minimum size while others may not.
10524 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010525 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010526 return FAIL;
10527
Bram Moolenaar18144c82006-04-12 21:52:12 +000010528 /* Don't continue in another tab page when doing only the current one
10529 * or when at the last tab page. */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010530 if (!(ssop_flags & SSOP_TABPAGES))
Bram Moolenaar18144c82006-04-12 21:52:12 +000010531 break;
10532 }
10533
10534 if (ssop_flags & SSOP_TABPAGES)
10535 {
Bram Moolenaar18144c82006-04-12 21:52:12 +000010536 if (fprintf(fd, "tabnext %d", tabpage_index(curtab)) < 0
10537 || put_eol(fd) == FAIL)
10538 return FAIL;
10539 }
10540
Bram Moolenaar9c102382006-05-03 21:26:49 +000010541 /*
10542 * Wipe out an empty unnamed buffer we started in.
10543 */
10544 if (put_line(fd, "if exists('s:wipebuf')") == FAIL)
10545 return FAIL;
Bram Moolenaarf3a67882006-05-05 21:09:41 +000010546 if (put_line(fd, " silent exe 'bwipe ' . s:wipebuf") == FAIL)
Bram Moolenaar9c102382006-05-03 21:26:49 +000010547 return FAIL;
10548 if (put_line(fd, "endif") == FAIL)
10549 return FAIL;
10550 if (put_line(fd, "unlet! s:wipebuf") == FAIL)
10551 return FAIL;
10552
10553 /* Re-apply 'winheight', 'winwidth' and 'shortmess'. */
10554 if (fprintf(fd, "set winheight=%ld winwidth=%ld shortmess=%s",
10555 p_wh, p_wiw, p_shm) < 0 || put_eol(fd) == FAIL)
10556 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010557
10558 /*
10559 * Lastly, execute the x.vim file if it exists.
10560 */
10561 if (put_line(fd, "let s:sx = expand(\"<sfile>:p:r\").\"x.vim\"") == FAIL
10562 || put_line(fd, "if file_readable(s:sx)") == FAIL
Bram Moolenaar42ba1262008-12-09 10:18:03 +000010563 || put_line(fd, " exe \"source \" . fnameescape(s:sx)") == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000010564 || put_line(fd, "endif") == FAIL)
10565 return FAIL;
10566
10567 return OK;
10568}
10569
10570 static int
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010571ses_winsizes(fd, restore_size, tab_firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010572 FILE *fd;
10573 int restore_size;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010574 win_T *tab_firstwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010575{
10576 int n = 0;
10577 win_T *wp;
10578
10579 if (restore_size && (ssop_flags & SSOP_WINSIZE))
10580 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010581 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010582 {
10583 if (!ses_do_win(wp))
10584 continue;
10585 ++n;
10586
10587 /* restore height when not full height */
10588 if (wp->w_height + wp->w_status_height < topframe->fr_height
10589 && (fprintf(fd,
10590 "exe '%dresize ' . ((&lines * %ld + %ld) / %ld)",
10591 n, (long)wp->w_height, Rows / 2, Rows) < 0
10592 || put_eol(fd) == FAIL))
10593 return FAIL;
10594
10595 /* restore width when not full width */
10596 if (wp->w_width < Columns && (fprintf(fd,
10597 "exe 'vert %dresize ' . ((&columns * %ld + %ld) / %ld)",
10598 n, (long)wp->w_width, Columns / 2, Columns) < 0
10599 || put_eol(fd) == FAIL))
10600 return FAIL;
10601 }
10602 }
10603 else
10604 {
10605 /* Just equalise window sizes */
10606 if (put_line(fd, "wincmd =") == FAIL)
10607 return FAIL;
10608 }
10609 return OK;
10610}
10611
10612/*
10613 * Write commands to "fd" to recursively create windows for frame "fr",
10614 * horizontally and vertically split.
10615 * After the commands the last window in the frame is the current window.
10616 * Returns FAIL when writing the commands to "fd" fails.
10617 */
10618 static int
10619ses_win_rec(fd, fr)
10620 FILE *fd;
10621 frame_T *fr;
10622{
10623 frame_T *frc;
10624 int count = 0;
10625
10626 if (fr->fr_layout != FR_LEAF)
10627 {
10628 /* Find first frame that's not skipped and then create a window for
10629 * each following one (first frame is already there). */
10630 frc = ses_skipframe(fr->fr_child);
10631 if (frc != NULL)
10632 while ((frc = ses_skipframe(frc->fr_next)) != NULL)
10633 {
10634 /* Make window as big as possible so that we have lots of room
10635 * to split. */
10636 if (put_line(fd, "wincmd _ | wincmd |") == FAIL
10637 || put_line(fd, fr->fr_layout == FR_COL
10638 ? "split" : "vsplit") == FAIL)
10639 return FAIL;
10640 ++count;
10641 }
10642
10643 /* Go back to the first window. */
10644 if (count > 0 && (fprintf(fd, fr->fr_layout == FR_COL
10645 ? "%dwincmd k" : "%dwincmd h", count) < 0
10646 || put_eol(fd) == FAIL))
10647 return FAIL;
10648
10649 /* Recursively create frames/windows in each window of this column or
10650 * row. */
10651 frc = ses_skipframe(fr->fr_child);
10652 while (frc != NULL)
10653 {
10654 ses_win_rec(fd, frc);
10655 frc = ses_skipframe(frc->fr_next);
10656 /* Go to next window. */
10657 if (frc != NULL && put_line(fd, "wincmd w") == FAIL)
10658 return FAIL;
10659 }
10660 }
10661 return OK;
10662}
10663
10664/*
10665 * Skip frames that don't contain windows we want to save in the Session.
10666 * Returns NULL when there none.
10667 */
10668 static frame_T *
10669ses_skipframe(fr)
10670 frame_T *fr;
10671{
10672 frame_T *frc;
10673
10674 for (frc = fr; frc != NULL; frc = frc->fr_next)
10675 if (ses_do_frame(frc))
10676 break;
10677 return frc;
10678}
10679
10680/*
10681 * Return TRUE if frame "fr" has a window somewhere that we want to save in
10682 * the Session.
10683 */
10684 static int
10685ses_do_frame(fr)
10686 frame_T *fr;
10687{
10688 frame_T *frc;
10689
10690 if (fr->fr_layout == FR_LEAF)
10691 return ses_do_win(fr->fr_win);
10692 for (frc = fr->fr_child; frc != NULL; frc = frc->fr_next)
10693 if (ses_do_frame(frc))
10694 return TRUE;
10695 return FALSE;
10696}
10697
10698/*
10699 * Return non-zero if window "wp" is to be stored in the Session.
10700 */
10701 static int
10702ses_do_win(wp)
10703 win_T *wp;
10704{
10705 if (wp->w_buffer->b_fname == NULL
10706#ifdef FEAT_QUICKFIX
10707 /* When 'buftype' is "nofile" can't restore the window contents. */
10708 || bt_nofile(wp->w_buffer)
10709#endif
10710 )
10711 return (ssop_flags & SSOP_BLANK);
10712 if (wp->w_buffer->b_help)
10713 return (ssop_flags & SSOP_HELP);
10714 return TRUE;
10715}
10716
10717/*
10718 * Write commands to "fd" to restore the view of a window.
10719 * Caller must make sure 'scrolloff' is zero.
10720 */
10721 static int
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010722put_view(fd, wp, add_edit, flagp, current_arg_idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010723 FILE *fd;
10724 win_T *wp;
10725 int add_edit; /* add ":edit" command to view */
10726 unsigned *flagp; /* vop_flags or ssop_flags */
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010727 int current_arg_idx; /* current argument index of the window, use
10728 * -1 if unknown */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010729{
10730 win_T *save_curwin;
10731 int f;
10732 int do_cursor;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010733 int did_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010734
10735 /* Always restore cursor position for ":mksession". For ":mkview" only
10736 * when 'viewoptions' contains "cursor". */
10737 do_cursor = (flagp == &ssop_flags || *flagp & SSOP_CURSOR);
10738
10739 /*
10740 * Local argument list.
10741 */
10742 if (wp->w_alist == &global_alist)
10743 {
10744 if (put_line(fd, "argglobal") == FAIL)
10745 return FAIL;
10746 }
10747 else
10748 {
10749 if (ses_arglist(fd, "arglocal", &wp->w_alist->al_ga,
10750 flagp == &vop_flags
10751 || !(*flagp & SSOP_CURDIR)
10752 || wp->w_localdir != NULL, flagp) == FAIL)
10753 return FAIL;
10754 }
10755
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010756 /* Only when part of a session: restore the argument index. Some
10757 * arguments may have been deleted, check if the index is valid. */
Bram Moolenaar51f53df2010-06-26 05:25:54 +020010758 if (wp->w_arg_idx != current_arg_idx && wp->w_arg_idx < WARGCOUNT(wp)
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010759 && flagp == &ssop_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010760 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010761 if (fprintf(fd, "%ldargu", (long)wp->w_arg_idx + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010762 || put_eol(fd) == FAIL)
10763 return FAIL;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010764 did_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010765 }
10766
10767 /* Edit the file. Skip this when ":next" already did it. */
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010768 if (add_edit && (!did_next || wp->w_arg_idx_invalid))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010769 {
10770 /*
10771 * Load the file.
10772 */
10773 if (wp->w_buffer->b_ffname != NULL
10774#ifdef FEAT_QUICKFIX
10775 && !bt_nofile(wp->w_buffer)
10776#endif
10777 )
10778 {
10779 /*
10780 * Editing a file in this buffer: use ":edit file".
10781 * This may have side effects! (e.g., compressed or network file).
10782 */
10783 if (fputs("edit ", fd) < 0
10784 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
10785 return FAIL;
10786 }
10787 else
10788 {
10789 /* No file in this buffer, just make it empty. */
10790 if (put_line(fd, "enew") == FAIL)
10791 return FAIL;
10792#ifdef FEAT_QUICKFIX
10793 if (wp->w_buffer->b_ffname != NULL)
10794 {
10795 /* The buffer does have a name, but it's not a file name. */
10796 if (fputs("file ", fd) < 0
10797 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
10798 return FAIL;
10799 }
10800#endif
10801 do_cursor = FALSE;
10802 }
10803 }
10804
10805 /*
10806 * Local mappings and abbreviations.
10807 */
10808 if ((*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
10809 && makemap(fd, wp->w_buffer) == FAIL)
10810 return FAIL;
10811
10812 /*
10813 * Local options. Need to go to the window temporarily.
10814 * Store only local values when using ":mkview" and when ":mksession" is
10815 * used and 'sessionoptions' doesn't include "options".
10816 * Some folding options are always stored when "folds" is included,
10817 * otherwise the folds would not be restored correctly.
10818 */
10819 save_curwin = curwin;
10820 curwin = wp;
10821 curbuf = curwin->w_buffer;
10822 if (*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
10823 f = makeset(fd, OPT_LOCAL,
10824 flagp == &vop_flags || !(*flagp & SSOP_OPTIONS));
10825#ifdef FEAT_FOLDING
10826 else if (*flagp & SSOP_FOLDS)
10827 f = makefoldset(fd);
10828#endif
10829 else
10830 f = OK;
10831 curwin = save_curwin;
10832 curbuf = curwin->w_buffer;
10833 if (f == FAIL)
10834 return FAIL;
10835
10836#ifdef FEAT_FOLDING
10837 /*
10838 * Save Folds when 'buftype' is empty and for help files.
10839 */
10840 if ((*flagp & SSOP_FOLDS)
10841 && wp->w_buffer->b_ffname != NULL
Bram Moolenaarb1b715d2006-01-21 22:09:43 +000010842# ifdef FEAT_QUICKFIX
10843 && (*wp->w_buffer->b_p_bt == NUL || wp->w_buffer->b_help)
10844# endif
10845 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000010846 {
10847 if (put_folds(fd, wp) == FAIL)
10848 return FAIL;
10849 }
10850#endif
10851
10852 /*
10853 * Set the cursor after creating folds, since that moves the cursor.
10854 */
10855 if (do_cursor)
10856 {
10857
10858 /* Restore the cursor line in the file and relatively in the
10859 * window. Don't use "G", it changes the jumplist. */
10860 if (fprintf(fd, "let s:l = %ld - ((%ld * winheight(0) + %ld) / %ld)",
10861 (long)wp->w_cursor.lnum,
10862 (long)(wp->w_cursor.lnum - wp->w_topline),
10863 (long)wp->w_height / 2, (long)wp->w_height) < 0
10864 || put_eol(fd) == FAIL
10865 || put_line(fd, "if s:l < 1 | let s:l = 1 | endif") == FAIL
10866 || put_line(fd, "exe s:l") == FAIL
10867 || put_line(fd, "normal! zt") == FAIL
10868 || fprintf(fd, "%ld", (long)wp->w_cursor.lnum) < 0
10869 || put_eol(fd) == FAIL)
10870 return FAIL;
10871 /* Restore the cursor column and left offset when not wrapping. */
10872 if (wp->w_cursor.col == 0)
10873 {
10874 if (put_line(fd, "normal! 0") == FAIL)
10875 return FAIL;
10876 }
10877 else
10878 {
10879 if (!wp->w_p_wrap && wp->w_leftcol > 0 && wp->w_width > 0)
10880 {
10881 if (fprintf(fd,
10882 "let s:c = %ld - ((%ld * winwidth(0) + %ld) / %ld)",
Bram Moolenaar558ddad2013-02-20 19:26:29 +010010883 (long)wp->w_virtcol + 1,
10884 (long)(wp->w_virtcol - wp->w_leftcol),
Bram Moolenaar071d4272004-06-13 20:20:40 +000010885 (long)wp->w_width / 2, (long)wp->w_width) < 0
10886 || put_eol(fd) == FAIL
10887 || put_line(fd, "if s:c > 0") == FAIL
10888 || fprintf(fd,
Bram Moolenaar558ddad2013-02-20 19:26:29 +010010889 " exe 'normal! ' . s:c . '|zs' . %ld . '|'",
10890 (long)wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010891 || put_eol(fd) == FAIL
10892 || put_line(fd, "else") == FAIL
Bram Moolenaarfdf447b2013-02-26 17:21:29 +010010893 || fprintf(fd, " normal! 0%d|", wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010894 || put_eol(fd) == FAIL
10895 || put_line(fd, "endif") == FAIL)
10896 return FAIL;
10897 }
10898 else
10899 {
Bram Moolenaar558ddad2013-02-20 19:26:29 +010010900 if (fprintf(fd, "normal! 0%d|", wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010901 || put_eol(fd) == FAIL)
10902 return FAIL;
10903 }
10904 }
10905 }
10906
10907 /*
10908 * Local directory.
10909 */
10910 if (wp->w_localdir != NULL)
10911 {
10912 if (fputs("lcd ", fd) < 0
10913 || ses_put_fname(fd, wp->w_localdir, flagp) == FAIL
10914 || put_eol(fd) == FAIL)
10915 return FAIL;
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010916 did_lcd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010917 }
10918
10919 return OK;
10920}
10921
10922/*
10923 * Write an argument list to the session file.
10924 * Returns FAIL if writing fails.
10925 */
10926 static int
10927ses_arglist(fd, cmd, gap, fullname, flagp)
10928 FILE *fd;
10929 char *cmd;
10930 garray_T *gap;
10931 int fullname; /* TRUE: use full path name */
10932 unsigned *flagp;
10933{
10934 int i;
Bram Moolenaard9462e32011-04-11 21:35:11 +020010935 char_u *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010936 char_u *s;
10937
10938 if (gap->ga_len == 0)
10939 return put_line(fd, "silent! argdel *");
10940 if (fputs(cmd, fd) < 0)
10941 return FAIL;
10942 for (i = 0; i < gap->ga_len; ++i)
10943 {
10944 /* NULL file names are skipped (only happens when out of memory). */
10945 s = alist_name(&((aentry_T *)gap->ga_data)[i]);
10946 if (s != NULL)
10947 {
10948 if (fullname)
10949 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020010950 buf = alloc(MAXPATHL);
10951 if (buf != NULL)
10952 {
10953 (void)vim_FullName(s, buf, MAXPATHL, FALSE);
10954 s = buf;
10955 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010956 }
10957 if (fputs(" ", fd) < 0 || ses_put_fname(fd, s, flagp) == FAIL)
Bram Moolenaard9462e32011-04-11 21:35:11 +020010958 {
10959 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010960 return FAIL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020010961 }
10962 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010963 }
10964 }
10965 return put_eol(fd);
10966}
10967
10968/*
10969 * Write a buffer name to the session file.
10970 * Also ends the line.
10971 * Returns FAIL if writing fails.
10972 */
10973 static int
10974ses_fname(fd, buf, flagp)
10975 FILE *fd;
10976 buf_T *buf;
10977 unsigned *flagp;
10978{
10979 char_u *name;
10980
10981 /* Use the short file name if the current directory is known at the time
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010982 * the session file will be sourced.
10983 * Don't do this for ":mkview", we don't know the current directory.
10984 * Don't do this after ":lcd", we don't keep track of what the current
10985 * directory is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010986 if (buf->b_sfname != NULL
10987 && flagp == &ssop_flags
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010988 && (ssop_flags & (SSOP_CURDIR | SSOP_SESDIR))
Bram Moolenaar8d594672009-07-01 18:18:57 +000010989#ifdef FEAT_AUTOCHDIR
10990 && !p_acd
10991#endif
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010992 && !did_lcd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010993 name = buf->b_sfname;
10994 else
10995 name = buf->b_ffname;
10996 if (ses_put_fname(fd, name, flagp) == FAIL || put_eol(fd) == FAIL)
10997 return FAIL;
10998 return OK;
10999}
11000
11001/*
11002 * Write a file name to the session file.
11003 * Takes care of the "slash" option in 'sessionoptions' and escapes special
11004 * characters.
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011005 * Returns FAIL if writing fails or out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011006 */
11007 static int
11008ses_put_fname(fd, name, flagp)
11009 FILE *fd;
11010 char_u *name;
11011 unsigned *flagp;
11012{
11013 char_u *sname;
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011014 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011015 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011016
11017 sname = home_replace_save(NULL, name);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011018 if (sname == NULL)
11019 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011020
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011021 if (*flagp & SSOP_SLASH)
11022 {
11023 /* change all backslashes to forward slashes */
11024 for (p = sname; *p != NUL; mb_ptr_adv(p))
11025 if (*p == '\\')
11026 *p = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +000011027 }
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011028
Bram Moolenaar84a05ac2013-05-06 04:24:17 +020011029 /* escape special characters */
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011030 p = vim_strsave_fnameescape(sname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011031 vim_free(sname);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011032 if (p == NULL)
11033 return FAIL;
11034
11035 /* write the result */
11036 if (fputs((char *)p, fd) < 0)
11037 retval = FAIL;
11038
11039 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011040 return retval;
11041}
11042
11043/*
11044 * ":loadview [nr]"
11045 */
11046 static void
11047ex_loadview(eap)
11048 exarg_T *eap;
11049{
11050 char_u *fname;
11051
11052 fname = get_view_file(*eap->arg);
11053 if (fname != NULL)
11054 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +000011055 do_source(fname, FALSE, DOSO_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011056 vim_free(fname);
11057 }
11058}
11059
11060/*
11061 * Get the name of the view file for the current buffer.
11062 */
11063 static char_u *
11064get_view_file(c)
11065 int c;
11066{
11067 int len = 0;
11068 char_u *p, *s;
11069 char_u *retval;
11070 char_u *sname;
11071
11072 if (curbuf->b_ffname == NULL)
11073 {
11074 EMSG(_(e_noname));
11075 return NULL;
11076 }
11077 sname = home_replace_save(NULL, curbuf->b_ffname);
11078 if (sname == NULL)
11079 return NULL;
11080
11081 /*
11082 * We want a file name without separators, because we're not going to make
11083 * a directory.
11084 * "normal" path separator -> "=+"
11085 * "=" -> "=="
11086 * ":" path separator -> "=-"
11087 */
11088 for (p = sname; *p; ++p)
11089 if (*p == '=' || vim_ispathsep(*p))
11090 ++len;
11091 retval = alloc((unsigned)(STRLEN(sname) + len + STRLEN(p_vdir) + 9));
11092 if (retval != NULL)
11093 {
11094 STRCPY(retval, p_vdir);
11095 add_pathsep(retval);
11096 s = retval + STRLEN(retval);
11097 for (p = sname; *p; ++p)
11098 {
11099 if (*p == '=')
11100 {
11101 *s++ = '=';
11102 *s++ = '=';
11103 }
11104 else if (vim_ispathsep(*p))
11105 {
11106 *s++ = '=';
Bram Moolenaare60acc12011-05-10 16:41:25 +020011107#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011108 if (*p == ':')
11109 *s++ = '-';
11110 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000011111#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000011112 *s++ = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +000011113 }
11114 else
11115 *s++ = *p;
11116 }
11117 *s++ = '=';
11118 *s++ = c;
11119 STRCPY(s, ".vim");
11120 }
11121
11122 vim_free(sname);
11123 return retval;
11124}
11125
11126#endif /* FEAT_SESSION */
11127
11128/*
11129 * Write end-of-line character(s) for ":mkexrc", ":mkvimrc" and ":mksession".
11130 * Return FAIL for a write error.
11131 */
11132 int
11133put_eol(fd)
11134 FILE *fd;
11135{
11136 if (
11137#ifdef USE_CRNL
11138 (
11139# ifdef MKSESSION_NL
11140 !mksession_nl &&
11141# endif
11142 (putc('\r', fd) < 0)) ||
11143#endif
11144 (putc('\n', fd) < 0))
11145 return FAIL;
11146 return OK;
11147}
11148
11149/*
11150 * Write a line to "fd".
11151 * Return FAIL for a write error.
11152 */
11153 int
11154put_line(fd, s)
11155 FILE *fd;
11156 char *s;
11157{
11158 if (fputs(s, fd) < 0 || put_eol(fd) == FAIL)
11159 return FAIL;
11160 return OK;
11161}
11162
11163#ifdef FEAT_VIMINFO
11164/*
11165 * ":rviminfo" and ":wviminfo".
11166 */
11167 static void
11168ex_viminfo(eap)
11169 exarg_T *eap;
11170{
11171 char_u *save_viminfo;
11172
11173 save_viminfo = p_viminfo;
11174 if (*p_viminfo == NUL)
11175 p_viminfo = (char_u *)"'100";
11176 if (eap->cmdidx == CMD_rviminfo)
11177 {
Bram Moolenaard812df62008-11-09 12:46:09 +000011178 if (read_viminfo(eap->arg, VIF_WANT_INFO | VIF_WANT_MARKS
11179 | (eap->forceit ? VIF_FORCEIT : 0)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011180 EMSG(_("E195: Cannot open viminfo file for reading"));
11181 }
11182 else
11183 write_viminfo(eap->arg, eap->forceit);
11184 p_viminfo = save_viminfo;
11185}
11186#endif
11187
11188#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +000011189/*
Bram Moolenaard9462e32011-04-11 21:35:11 +020011190 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +000011191 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +000011192 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011193 void
11194dialog_msg(buff, format, fname)
11195 char_u *buff;
11196 char *format;
11197 char_u *fname;
11198{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011199 if (fname == NULL)
11200 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +020011201 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011202}
11203#endif
11204
11205/*
11206 * ":behave {mswin,xterm}"
11207 */
11208 static void
11209ex_behave(eap)
11210 exarg_T *eap;
11211{
11212 if (STRCMP(eap->arg, "mswin") == 0)
11213 {
11214 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
11215 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
11216 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
11217 set_option_value((char_u *)"keymodel", 0L,
11218 (char_u *)"startsel,stopsel", 0);
11219 }
11220 else if (STRCMP(eap->arg, "xterm") == 0)
11221 {
11222 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
11223 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
11224 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
11225 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
11226 }
11227 else
11228 EMSG2(_(e_invarg2), eap->arg);
11229}
11230
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010011231#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
11232/*
11233 * Function given to ExpandGeneric() to obtain the possible arguments of the
11234 * ":behave {mswin,xterm}" command.
11235 */
11236 char_u *
11237get_behave_arg(xp, idx)
11238 expand_T *xp UNUSED;
11239 int idx;
11240{
11241 if (idx == 0)
11242 return (char_u *)"mswin";
11243 if (idx == 1)
11244 return (char_u *)"xterm";
11245 return NULL;
11246}
11247#endif
11248
Bram Moolenaar071d4272004-06-13 20:20:40 +000011249#ifdef FEAT_AUTOCMD
11250static int filetype_detect = FALSE;
11251static int filetype_plugin = FALSE;
11252static int filetype_indent = FALSE;
11253
11254/*
11255 * ":filetype [plugin] [indent] {on,off,detect}"
11256 * on: Load the filetype.vim file to install autocommands for file types.
11257 * off: Load the ftoff.vim file to remove all autocommands for file types.
11258 * plugin on: load filetype.vim and ftplugin.vim
11259 * plugin off: load ftplugof.vim
11260 * indent on: load filetype.vim and indent.vim
11261 * indent off: load indoff.vim
11262 */
11263 static void
11264ex_filetype(eap)
11265 exarg_T *eap;
11266{
11267 char_u *arg = eap->arg;
11268 int plugin = FALSE;
11269 int indent = FALSE;
11270
11271 if (*eap->arg == NUL)
11272 {
11273 /* Print current status. */
11274 smsg((char_u *)"filetype detection:%s plugin:%s indent:%s",
11275 filetype_detect ? "ON" : "OFF",
11276 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
11277 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
11278 return;
11279 }
11280
11281 /* Accept "plugin" and "indent" in any order. */
11282 for (;;)
11283 {
11284 if (STRNCMP(arg, "plugin", 6) == 0)
11285 {
11286 plugin = TRUE;
11287 arg = skipwhite(arg + 6);
11288 continue;
11289 }
11290 if (STRNCMP(arg, "indent", 6) == 0)
11291 {
11292 indent = TRUE;
11293 arg = skipwhite(arg + 6);
11294 continue;
11295 }
11296 break;
11297 }
11298 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
11299 {
11300 if (*arg == 'o' || !filetype_detect)
11301 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011302 source_runtime((char_u *)FILETYPE_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011303 filetype_detect = TRUE;
11304 if (plugin)
11305 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011306 source_runtime((char_u *)FTPLUGIN_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011307 filetype_plugin = TRUE;
11308 }
11309 if (indent)
11310 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011311 source_runtime((char_u *)INDENT_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011312 filetype_indent = TRUE;
11313 }
11314 }
11315 if (*arg == 'd')
11316 {
11317 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE);
Bram Moolenaara3227e22006-03-08 21:32:40 +000011318 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011319 }
11320 }
11321 else if (STRCMP(arg, "off") == 0)
11322 {
11323 if (plugin || indent)
11324 {
11325 if (plugin)
11326 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011327 source_runtime((char_u *)FTPLUGOF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011328 filetype_plugin = FALSE;
11329 }
11330 if (indent)
11331 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011332 source_runtime((char_u *)INDOFF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011333 filetype_indent = FALSE;
11334 }
11335 }
11336 else
11337 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011338 source_runtime((char_u *)FTOFF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011339 filetype_detect = FALSE;
11340 }
11341 }
11342 else
11343 EMSG2(_(e_invarg2), arg);
11344}
11345
11346/*
11347 * ":setfiletype {name}"
11348 */
11349 static void
11350ex_setfiletype(eap)
11351 exarg_T *eap;
11352{
11353 if (!did_filetype)
11354 set_option_value((char_u *)"filetype", 0L, eap->arg, OPT_LOCAL);
11355}
11356#endif
11357
Bram Moolenaar071d4272004-06-13 20:20:40 +000011358 static void
11359ex_digraphs(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +000011360 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011361{
11362#ifdef FEAT_DIGRAPHS
11363 if (*eap->arg != NUL)
11364 putdigraph(eap->arg);
11365 else
11366 listdigraphs();
11367#else
11368 EMSG(_("E196: No digraphs in this version"));
11369#endif
11370}
11371
11372 static void
11373ex_set(eap)
11374 exarg_T *eap;
11375{
11376 int flags = 0;
11377
11378 if (eap->cmdidx == CMD_setlocal)
11379 flags = OPT_LOCAL;
11380 else if (eap->cmdidx == CMD_setglobal)
11381 flags = OPT_GLOBAL;
11382#if defined(FEAT_EVAL) && defined(FEAT_AUTOCMD) && defined(FEAT_BROWSE)
11383 if (cmdmod.browse && flags == 0)
11384 ex_options(eap);
11385 else
11386#endif
11387 (void)do_set(eap->arg, flags);
11388}
11389
11390#ifdef FEAT_SEARCH_EXTRA
11391/*
11392 * ":nohlsearch"
11393 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011394 static void
11395ex_nohlsearch(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +000011396 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011397{
Bram Moolenaar8050efa2013-11-08 04:30:20 +010011398 SET_NO_HLSEARCH(TRUE);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +000011399 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011400}
11401
11402/*
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011403 * ":[N]match {group} {pattern}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000011404 * Sets nextcmd to the start of the next command, if any. Also called when
11405 * skipping commands to find the next command.
11406 */
11407 static void
11408ex_match(eap)
11409 exarg_T *eap;
11410{
11411 char_u *p;
Bram Moolenaar52d36c82007-08-11 14:00:30 +000011412 char_u *g = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011413 char_u *end;
11414 int c;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011415 int id;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011416
11417 if (eap->line2 <= 3)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011418 id = eap->line2;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011419 else
11420 {
11421 EMSG(e_invcmd);
11422 return;
11423 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011424
11425 /* First clear any old pattern. */
11426 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011427 match_delete(curwin, id, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011428
11429 if (ends_excmd(*eap->arg))
11430 end = eap->arg;
11431 else if ((STRNICMP(eap->arg, "none", 4) == 0
11432 && (vim_iswhite(eap->arg[4]) || ends_excmd(eap->arg[4]))))
11433 end = eap->arg + 4;
11434 else
11435 {
11436 p = skiptowhite(eap->arg);
11437 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011438 g = vim_strnsave(eap->arg, (int)(p - eap->arg));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011439 p = skipwhite(p);
11440 if (*p == NUL)
11441 {
11442 /* There must be two arguments. */
11443 EMSG2(_(e_invarg2), eap->arg);
11444 return;
11445 }
11446 end = skip_regexp(p + 1, *p, TRUE, NULL);
11447 if (!eap->skip)
11448 {
11449 if (*end != NUL && !ends_excmd(*skipwhite(end + 1)))
11450 {
11451 eap->errmsg = e_trailing;
11452 return;
11453 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000011454 if (*end != *p)
11455 {
11456 EMSG2(_(e_invarg2), p);
11457 return;
11458 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011459
11460 c = *end;
11461 *end = NUL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011462 match_add(curwin, g, p + 1, 10, id);
11463 vim_free(g);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000011464 *end = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011465 }
11466 }
11467 eap->nextcmd = find_nextcmd(end);
11468}
11469#endif
11470
11471#ifdef FEAT_CRYPT
11472/*
11473 * ":X": Get crypt key
11474 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011475 static void
11476ex_X(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +000011477 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011478{
Bram Moolenaar49771f42010-07-20 17:32:38 +020011479 if (get_crypt_method(curbuf) == 0 || blowfish_self_test() == OK)
Bram Moolenaar40e6a712010-05-16 22:32:54 +020011480 (void)get_crypt_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011481}
11482#endif
11483
11484#ifdef FEAT_FOLDING
11485 static void
11486ex_fold(eap)
11487 exarg_T *eap;
11488{
11489 if (foldManualAllowed(TRUE))
11490 foldCreate(eap->line1, eap->line2);
11491}
11492
11493 static void
11494ex_foldopen(eap)
11495 exarg_T *eap;
11496{
11497 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
11498 eap->forceit, FALSE);
11499}
11500
11501 static void
11502ex_folddo(eap)
11503 exarg_T *eap;
11504{
11505 linenr_T lnum;
11506
11507 /* First set the marks for all lines closed/open. */
11508 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
11509 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
11510 ml_setmarked(lnum);
11511
11512 /* Execute the command on the marked lines. */
11513 global_exe(eap->arg);
11514 ml_clearmarked(); /* clear rest of the marks */
11515}
11516#endif