blob: fe711d79d30441eb5ac996cb8081bc269c2069ab [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 Moolenaar071d4272004-06-13 20:20:40 +00003606 case CMD_topleft:
3607 case CMD_verbose:
3608 case CMD_vertical:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003609 case CMD_windo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610 return arg;
3611
Bram Moolenaar4f688582007-07-24 12:34:30 +00003612#ifdef FEAT_CMDL_COMPL
3613# ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614 case CMD_match:
3615 if (*arg == NUL || !ends_excmd(*arg))
3616 {
Bram Moolenaar4f688582007-07-24 12:34:30 +00003617 /* also complete "None" */
3618 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619 arg = skipwhite(skiptowhite(arg));
3620 if (*arg != NUL)
3621 {
3622 xp->xp_context = EXPAND_NOTHING;
3623 arg = skip_regexp(arg + 1, *arg, p_magic, NULL);
3624 }
3625 }
3626 return find_nextcmd(arg);
Bram Moolenaar4f688582007-07-24 12:34:30 +00003627# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629/*
3630 * All completion for the +cmdline_compl feature goes here.
3631 */
3632
3633# ifdef FEAT_USR_CMDS
3634 case CMD_command:
3635 /* Check for attributes */
3636 while (*arg == '-')
3637 {
3638 arg++; /* Skip "-" */
3639 p = skiptowhite(arg);
3640 if (*p == NUL)
3641 {
3642 /* Cursor is still in the attribute */
3643 p = vim_strchr(arg, '=');
3644 if (p == NULL)
3645 {
3646 /* No "=", so complete attribute names */
3647 xp->xp_context = EXPAND_USER_CMD_FLAGS;
3648 xp->xp_pattern = arg;
3649 return NULL;
3650 }
3651
3652 /* For the -complete and -nargs attributes, we complete
3653 * their arguments as well.
3654 */
3655 if (STRNICMP(arg, "complete", p - arg) == 0)
3656 {
3657 xp->xp_context = EXPAND_USER_COMPLETE;
3658 xp->xp_pattern = p + 1;
3659 return NULL;
3660 }
3661 else if (STRNICMP(arg, "nargs", p - arg) == 0)
3662 {
3663 xp->xp_context = EXPAND_USER_NARGS;
3664 xp->xp_pattern = p + 1;
3665 return NULL;
3666 }
3667 return NULL;
3668 }
3669 arg = skipwhite(p);
3670 }
3671
3672 /* After the attributes comes the new command name */
3673 p = skiptowhite(arg);
3674 if (*p == NUL)
3675 {
3676 xp->xp_context = EXPAND_USER_COMMANDS;
3677 xp->xp_pattern = arg;
3678 break;
3679 }
3680
3681 /* And finally comes a normal command */
3682 return skipwhite(p);
3683
3684 case CMD_delcommand:
3685 xp->xp_context = EXPAND_USER_COMMANDS;
3686 xp->xp_pattern = arg;
3687 break;
3688# endif
3689
3690 case CMD_global:
3691 case CMD_vglobal:
3692 delim = *arg; /* get the delimiter */
3693 if (delim)
3694 ++arg; /* skip delimiter if there is one */
3695
3696 while (arg[0] != NUL && arg[0] != delim)
3697 {
3698 if (arg[0] == '\\' && arg[1] != NUL)
3699 ++arg;
3700 ++arg;
3701 }
3702 if (arg[0] != NUL)
3703 return arg + 1;
3704 break;
3705 case CMD_and:
3706 case CMD_substitute:
3707 delim = *arg;
3708 if (delim)
3709 {
3710 /* skip "from" part */
3711 ++arg;
3712 arg = skip_regexp(arg, delim, p_magic, NULL);
3713 }
3714 /* skip "to" part */
3715 while (arg[0] != NUL && arg[0] != delim)
3716 {
3717 if (arg[0] == '\\' && arg[1] != NUL)
3718 ++arg;
3719 ++arg;
3720 }
3721 if (arg[0] != NUL) /* skip delimiter */
3722 ++arg;
3723 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
3724 ++arg;
3725 if (arg[0] != NUL)
3726 return arg;
3727 break;
3728 case CMD_isearch:
3729 case CMD_dsearch:
3730 case CMD_ilist:
3731 case CMD_dlist:
3732 case CMD_ijump:
3733 case CMD_psearch:
3734 case CMD_djump:
3735 case CMD_isplit:
3736 case CMD_dsplit:
3737 arg = skipwhite(skipdigits(arg)); /* skip count */
3738 if (*arg == '/') /* Match regexp, not just whole words */
3739 {
3740 for (++arg; *arg && *arg != '/'; arg++)
3741 if (*arg == '\\' && arg[1] != NUL)
3742 arg++;
3743 if (*arg)
3744 {
3745 arg = skipwhite(arg + 1);
3746
3747 /* Check for trailing illegal characters */
3748 if (*arg && vim_strchr((char_u *)"|\"\n", *arg) == NULL)
3749 xp->xp_context = EXPAND_NOTHING;
3750 else
3751 return arg;
3752 }
3753 }
3754 break;
3755#ifdef FEAT_AUTOCMD
3756 case CMD_autocmd:
3757 return set_context_in_autocmd(xp, arg, FALSE);
3758
3759 case CMD_doautocmd:
Bram Moolenaar73a9d7b2008-11-06 16:16:44 +00003760 case CMD_doautoall:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761 return set_context_in_autocmd(xp, arg, TRUE);
3762#endif
3763 case CMD_set:
3764 set_context_in_set_cmd(xp, arg, 0);
3765 break;
3766 case CMD_setglobal:
3767 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
3768 break;
3769 case CMD_setlocal:
3770 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
3771 break;
3772 case CMD_tag:
3773 case CMD_stag:
3774 case CMD_ptag:
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00003775 case CMD_ltag:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003776 case CMD_tselect:
3777 case CMD_stselect:
3778 case CMD_ptselect:
3779 case CMD_tjump:
3780 case CMD_stjump:
3781 case CMD_ptjump:
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003782 if (*p_wop != NUL)
3783 xp->xp_context = EXPAND_TAGS_LISTFILES;
3784 else
3785 xp->xp_context = EXPAND_TAGS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003786 xp->xp_pattern = arg;
3787 break;
3788 case CMD_augroup:
3789 xp->xp_context = EXPAND_AUGROUP;
3790 xp->xp_pattern = arg;
3791 break;
3792#ifdef FEAT_SYN_HL
3793 case CMD_syntax:
3794 set_context_in_syntax_cmd(xp, arg);
3795 break;
3796#endif
3797#ifdef FEAT_EVAL
3798 case CMD_let:
3799 case CMD_if:
3800 case CMD_elseif:
3801 case CMD_while:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00003802 case CMD_for:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803 case CMD_echo:
3804 case CMD_echon:
3805 case CMD_execute:
3806 case CMD_echomsg:
3807 case CMD_echoerr:
3808 case CMD_call:
3809 case CMD_return:
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003810 set_context_for_expression(xp, arg, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811 break;
3812
3813 case CMD_unlet:
3814 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3815 arg = xp->xp_pattern + 1;
3816 xp->xp_context = EXPAND_USER_VARS;
3817 xp->xp_pattern = arg;
3818 break;
3819
3820 case CMD_function:
3821 case CMD_delfunction:
3822 xp->xp_context = EXPAND_USER_FUNC;
3823 xp->xp_pattern = arg;
3824 break;
3825
3826 case CMD_echohl:
Bram Moolenaar4f688582007-07-24 12:34:30 +00003827 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003828 break;
3829#endif
3830 case CMD_highlight:
3831 set_context_in_highlight_cmd(xp, arg);
3832 break;
Bram Moolenaarf4580d82009-03-18 11:52:53 +00003833#ifdef FEAT_CSCOPE
3834 case CMD_cscope:
Bram Moolenaar7bfef802009-04-22 14:25:01 +00003835 case CMD_lcscope:
3836 case CMD_scscope:
3837 set_context_in_cscope_cmd(xp, arg, ea.cmdidx);
Bram Moolenaarf4580d82009-03-18 11:52:53 +00003838 break;
3839#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00003840#ifdef FEAT_SIGNS
3841 case CMD_sign:
3842 set_context_in_sign_cmd(xp, arg);
3843 break;
3844#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003845#ifdef FEAT_LISTCMDS
3846 case CMD_bdelete:
3847 case CMD_bwipeout:
3848 case CMD_bunload:
3849 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3850 arg = xp->xp_pattern + 1;
3851 /*FALLTHROUGH*/
3852 case CMD_buffer:
3853 case CMD_sbuffer:
3854 case CMD_checktime:
3855 xp->xp_context = EXPAND_BUFFERS;
3856 xp->xp_pattern = arg;
3857 break;
3858#endif
3859#ifdef FEAT_USR_CMDS
3860 case CMD_USER:
3861 case CMD_USER_BUF:
3862 if (compl != EXPAND_NOTHING)
3863 {
3864 /* XFILE: file names are handled above */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003865 if (!(ea.argt & XFILE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003866 {
3867# ifdef FEAT_MENU
3868 if (compl == EXPAND_MENUS)
3869 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
3870# endif
3871 if (compl == EXPAND_COMMANDS)
3872 return arg;
3873 if (compl == EXPAND_MAPPINGS)
3874 return set_context_in_map_cmd(xp, (char_u *)"map",
3875 arg, forceit, FALSE, FALSE, CMD_map);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003876 /* Find start of last argument. */
3877 p = arg;
3878 while (*p)
3879 {
3880 if (*p == ' ')
Bram Moolenaar848f8762012-07-25 17:22:23 +02003881 /* argument starts after a space */
3882 arg = p + 1;
Bram Moolenaara07c8312012-07-27 21:12:07 +02003883 else if (*p == '\\' && *(p + 1) != NUL)
3884 ++p; /* skip over escaped character */
3885 mb_ptr_adv(p);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003886 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887 xp->xp_pattern = arg;
3888 }
3889 xp->xp_context = compl;
3890 }
3891 break;
3892#endif
3893 case CMD_map: case CMD_noremap:
3894 case CMD_nmap: case CMD_nnoremap:
3895 case CMD_vmap: case CMD_vnoremap:
3896 case CMD_omap: case CMD_onoremap:
3897 case CMD_imap: case CMD_inoremap:
3898 case CMD_cmap: case CMD_cnoremap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003899 case CMD_lmap: case CMD_lnoremap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02003900 case CMD_smap: case CMD_snoremap:
3901 case CMD_xmap: case CMD_xnoremap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003902 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003903 FALSE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003904 case CMD_unmap:
3905 case CMD_nunmap:
3906 case CMD_vunmap:
3907 case CMD_ounmap:
3908 case CMD_iunmap:
3909 case CMD_cunmap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003910 case CMD_lunmap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02003911 case CMD_sunmap:
3912 case CMD_xunmap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003913 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003914 FALSE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003915 case CMD_abbreviate: case CMD_noreabbrev:
3916 case CMD_cabbrev: case CMD_cnoreabbrev:
3917 case CMD_iabbrev: case CMD_inoreabbrev:
3918 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003919 TRUE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920 case CMD_unabbreviate:
3921 case CMD_cunabbrev:
3922 case CMD_iunabbrev:
3923 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003924 TRUE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003925#ifdef FEAT_MENU
3926 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
3927 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
3928 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
3929 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
3930 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
3931 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
3932 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
3933 case CMD_tmenu: case CMD_tunmenu:
3934 case CMD_popup: case CMD_tearoff: case CMD_emenu:
3935 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
3936#endif
3937
3938 case CMD_colorscheme:
3939 xp->xp_context = EXPAND_COLORS;
3940 xp->xp_pattern = arg;
3941 break;
3942
3943 case CMD_compiler:
3944 xp->xp_context = EXPAND_COMPILER;
3945 xp->xp_pattern = arg;
3946 break;
3947
Bram Moolenaar883f5d02010-06-21 06:24:34 +02003948 case CMD_ownsyntax:
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02003949 xp->xp_context = EXPAND_OWNSYNTAX;
3950 xp->xp_pattern = arg;
3951 break;
3952
3953 case CMD_setfiletype:
Bram Moolenaar883f5d02010-06-21 06:24:34 +02003954 xp->xp_context = EXPAND_FILETYPE;
3955 xp->xp_pattern = arg;
3956 break;
3957
Bram Moolenaar071d4272004-06-13 20:20:40 +00003958#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
3959 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
3960 case CMD_language:
Bram Moolenaara660dc82011-05-25 12:51:22 +02003961 p = skiptowhite(arg);
3962 if (*p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003963 {
3964 xp->xp_context = EXPAND_LANGUAGE;
3965 xp->xp_pattern = arg;
3966 }
3967 else
Bram Moolenaara660dc82011-05-25 12:51:22 +02003968 {
3969 if ( STRNCMP(arg, "messages", p - arg) == 0
3970 || STRNCMP(arg, "ctype", p - arg) == 0
3971 || STRNCMP(arg, "time", p - arg) == 0)
3972 {
3973 xp->xp_context = EXPAND_LOCALES;
3974 xp->xp_pattern = skipwhite(p);
3975 }
3976 else
3977 xp->xp_context = EXPAND_NOTHING;
3978 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003979 break;
3980#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01003981#if defined(FEAT_PROFILE)
3982 case CMD_profile:
3983 set_context_in_profile_cmd(xp, arg);
3984 break;
3985#endif
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01003986 case CMD_behave:
3987 xp->xp_context = EXPAND_BEHAVE;
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02003988 xp->xp_pattern = arg;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01003989 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003990
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02003991#if defined(FEAT_CMDHIST)
3992 case CMD_history:
3993 xp->xp_context = EXPAND_HISTORY;
3994 xp->xp_pattern = arg;
3995 break;
3996#endif
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02003997#if defined(FEAT_PROFILE)
3998 case CMD_syntime:
3999 xp->xp_context = EXPAND_SYNTIME;
4000 xp->xp_pattern = arg;
4001 break;
4002#endif
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004003
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004#endif /* FEAT_CMDL_COMPL */
4005
4006 default:
4007 break;
4008 }
4009 return NULL;
4010}
4011
4012/*
4013 * skip a range specifier of the form: addr [,addr] [;addr] ..
4014 *
4015 * Backslashed delimiters after / or ? will be skipped, and commands will
4016 * not be expanded between /'s and ?'s or after "'".
4017 *
Bram Moolenaardf177f62005-02-22 08:39:57 +00004018 * Also skip white space and ":" characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004019 * Returns the "cmd" pointer advanced to beyond the range.
4020 */
4021 char_u *
4022skip_range(cmd, ctx)
4023 char_u *cmd;
4024 int *ctx; /* pointer to xp_context or NULL */
4025{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004026 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027
Bram Moolenaardf177f62005-02-22 08:39:57 +00004028 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004029 {
4030 if (*cmd == '\'')
4031 {
4032 if (*++cmd == NUL && ctx != NULL)
4033 *ctx = EXPAND_NOTHING;
4034 }
4035 else if (*cmd == '/' || *cmd == '?')
4036 {
4037 delim = *cmd++;
4038 while (*cmd != NUL && *cmd != delim)
4039 if (*cmd++ == '\\' && *cmd != NUL)
4040 ++cmd;
4041 if (*cmd == NUL && ctx != NULL)
4042 *ctx = EXPAND_NOTHING;
4043 }
4044 if (*cmd != NUL)
4045 ++cmd;
4046 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004047
4048 /* Skip ":" and white space. */
4049 while (*cmd == ':')
4050 cmd = skipwhite(cmd + 1);
4051
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052 return cmd;
4053}
4054
4055/*
4056 * get a single EX address
4057 *
4058 * Set ptr to the next character after the part that was interpreted.
4059 * Set ptr to NULL when an error is encountered.
4060 *
4061 * Return MAXLNUM when no Ex address was found.
4062 */
4063 static linenr_T
4064get_address(ptr, skip, to_other_file)
4065 char_u **ptr;
4066 int skip; /* only skip the address, don't use it */
4067 int to_other_file; /* flag: may jump to other file */
4068{
4069 int c;
4070 int i;
4071 long n;
4072 char_u *cmd;
4073 pos_T pos;
4074 pos_T *fp;
4075 linenr_T lnum;
4076
4077 cmd = skipwhite(*ptr);
4078 lnum = MAXLNUM;
4079 do
4080 {
4081 switch (*cmd)
4082 {
4083 case '.': /* '.' - Cursor position */
4084 ++cmd;
4085 lnum = curwin->w_cursor.lnum;
4086 break;
4087
4088 case '$': /* '$' - last line */
4089 ++cmd;
4090 lnum = curbuf->b_ml.ml_line_count;
4091 break;
4092
4093 case '\'': /* ''' - mark */
4094 if (*++cmd == NUL)
4095 {
4096 cmd = NULL;
4097 goto error;
4098 }
4099 if (skip)
4100 ++cmd;
4101 else
4102 {
4103 /* Only accept a mark in another file when it is
4104 * used by itself: ":'M". */
4105 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
4106 ++cmd;
4107 if (fp == (pos_T *)-1)
4108 /* Jumped to another file. */
4109 lnum = curwin->w_cursor.lnum;
4110 else
4111 {
4112 if (check_mark(fp) == FAIL)
4113 {
4114 cmd = NULL;
4115 goto error;
4116 }
4117 lnum = fp->lnum;
4118 }
4119 }
4120 break;
4121
4122 case '/':
4123 case '?': /* '/' or '?' - search */
4124 c = *cmd++;
4125 if (skip) /* skip "/pat/" */
4126 {
4127 cmd = skip_regexp(cmd, c, (int)p_magic, NULL);
4128 if (*cmd == c)
4129 ++cmd;
4130 }
4131 else
4132 {
4133 pos = curwin->w_cursor; /* save curwin->w_cursor */
4134 /*
4135 * When '/' or '?' follows another address, start
4136 * from there.
4137 */
4138 if (lnum != MAXLNUM)
4139 curwin->w_cursor.lnum = lnum;
4140 /*
4141 * Start a forward search at the end of the line.
4142 * Start a backward search at the start of the line.
4143 * This makes sure we never match in the current
4144 * line, and can match anywhere in the
4145 * next/previous line.
4146 */
4147 if (c == '/')
4148 curwin->w_cursor.col = MAXCOL;
4149 else
4150 curwin->w_cursor.col = 0;
4151 searchcmdlen = 0;
4152 if (!do_search(NULL, c, cmd, 1L,
Bram Moolenaaraad86642008-03-10 20:34:59 +00004153 SEARCH_HIS | SEARCH_MSG, NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154 {
4155 curwin->w_cursor = pos;
4156 cmd = NULL;
4157 goto error;
4158 }
4159 lnum = curwin->w_cursor.lnum;
4160 curwin->w_cursor = pos;
4161 /* adjust command string pointer */
4162 cmd += searchcmdlen;
4163 }
4164 break;
4165
4166 case '\\': /* "\?", "\/" or "\&", repeat search */
4167 ++cmd;
4168 if (*cmd == '&')
4169 i = RE_SUBST;
4170 else if (*cmd == '?' || *cmd == '/')
4171 i = RE_SEARCH;
4172 else
4173 {
4174 EMSG(_(e_backslash));
4175 cmd = NULL;
4176 goto error;
4177 }
4178
4179 if (!skip)
4180 {
4181 /*
4182 * When search follows another address, start from
4183 * there.
4184 */
4185 if (lnum != MAXLNUM)
4186 pos.lnum = lnum;
4187 else
4188 pos.lnum = curwin->w_cursor.lnum;
4189
4190 /*
4191 * Start the search just like for the above
4192 * do_search().
4193 */
4194 if (*cmd != '?')
4195 pos.col = MAXCOL;
4196 else
4197 pos.col = 0;
4198 if (searchit(curwin, curbuf, &pos,
4199 *cmd == '?' ? BACKWARD : FORWARD,
Bram Moolenaaraad86642008-03-10 20:34:59 +00004200 (char_u *)"", 1L, SEARCH_MSG,
Bram Moolenaar76929292008-01-06 19:07:36 +00004201 i, (linenr_T)0, NULL) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004202 lnum = pos.lnum;
4203 else
4204 {
4205 cmd = NULL;
4206 goto error;
4207 }
4208 }
4209 ++cmd;
4210 break;
4211
4212 default:
4213 if (VIM_ISDIGIT(*cmd)) /* absolute line number */
4214 lnum = getdigits(&cmd);
4215 }
4216
4217 for (;;)
4218 {
4219 cmd = skipwhite(cmd);
4220 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4221 break;
4222
4223 if (lnum == MAXLNUM)
4224 lnum = curwin->w_cursor.lnum; /* "+1" is same as ".+1" */
4225 if (VIM_ISDIGIT(*cmd))
4226 i = '+'; /* "number" is same as "+number" */
4227 else
4228 i = *cmd++;
4229 if (!VIM_ISDIGIT(*cmd)) /* '+' is '+1', but '+0' is not '+1' */
4230 n = 1;
4231 else
4232 n = getdigits(&cmd);
4233 if (i == '-')
4234 lnum -= n;
4235 else
4236 lnum += n;
4237 }
4238 } while (*cmd == '/' || *cmd == '?');
4239
4240error:
4241 *ptr = cmd;
4242 return lnum;
4243}
4244
4245/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004246 * Get flags from an Ex command argument.
4247 */
4248 static void
4249get_flags(eap)
4250 exarg_T *eap;
4251{
4252 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4253 {
4254 if (*eap->arg == 'l')
4255 eap->flags |= EXFLAG_LIST;
4256 else if (*eap->arg == 'p')
4257 eap->flags |= EXFLAG_PRINT;
4258 else
4259 eap->flags |= EXFLAG_NR;
4260 eap->arg = skipwhite(eap->arg + 1);
4261 }
4262}
4263
4264/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004265 * Function called for command which is Not Implemented. NI!
4266 */
4267 void
4268ex_ni(eap)
4269 exarg_T *eap;
4270{
4271 if (!eap->skip)
4272 eap->errmsg = (char_u *)N_("E319: Sorry, the command is not available in this version");
4273}
4274
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004275#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004276/*
4277 * Function called for script command which is Not Implemented. NI!
4278 * Skips over ":perl <<EOF" constructs.
4279 */
4280 static void
4281ex_script_ni(eap)
4282 exarg_T *eap;
4283{
4284 if (!eap->skip)
4285 ex_ni(eap);
4286 else
4287 vim_free(script_get(eap, eap->arg));
4288}
4289#endif
4290
4291/*
4292 * Check range in Ex command for validity.
4293 * Return NULL when valid, error message when invalid.
4294 */
4295 static char_u *
4296invalid_range(eap)
4297 exarg_T *eap;
4298{
4299 if ( eap->line1 < 0
4300 || eap->line2 < 0
4301 || eap->line1 > eap->line2
4302 || ((eap->argt & RANGE)
4303 && !(eap->argt & NOTADR)
4304 && eap->line2 > curbuf->b_ml.ml_line_count
4305#ifdef FEAT_DIFF
4306 + (eap->cmdidx == CMD_diffget)
4307#endif
4308 ))
4309 return (char_u *)_(e_invrange);
4310 return NULL;
4311}
4312
4313/*
4314 * Correct the range for zero line number, if required.
4315 */
4316 static void
4317correct_range(eap)
4318 exarg_T *eap;
4319{
4320 if (!(eap->argt & ZEROR)) /* zero in range not allowed */
4321 {
4322 if (eap->line1 == 0)
4323 eap->line1 = 1;
4324 if (eap->line2 == 0)
4325 eap->line2 = 1;
4326 }
4327}
4328
Bram Moolenaar748bf032005-02-02 23:04:36 +00004329#ifdef FEAT_QUICKFIX
4330static char_u *skip_grep_pat __ARGS((exarg_T *eap));
4331
4332/*
4333 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4334 * pattern. Otherwise return eap->arg.
4335 */
4336 static char_u *
4337skip_grep_pat(eap)
4338 exarg_T *eap;
4339{
4340 char_u *p = eap->arg;
4341
Bram Moolenaara37420f2006-02-04 22:37:47 +00004342 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4343 || eap->cmdidx == CMD_vimgrepadd
4344 || eap->cmdidx == CMD_lvimgrepadd
4345 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004346 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004347 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004348 if (p == NULL)
4349 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004350 }
4351 return p;
4352}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004353
4354/*
4355 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4356 * in the command line, so that things like % get expanded.
4357 */
4358 static char_u *
4359replace_makeprg(eap, p, cmdlinep)
4360 exarg_T *eap;
4361 char_u *p;
4362 char_u **cmdlinep;
4363{
4364 char_u *new_cmdline;
4365 char_u *program;
4366 char_u *pos;
4367 char_u *ptr;
4368 int len;
4369 int i;
4370
4371 /*
4372 * Don't do it when ":vimgrep" is used for ":grep".
4373 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004374 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4375 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4376 || eap->cmdidx == CMD_grepadd
4377 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004378 && !grep_internal(eap->cmdidx))
4379 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004380 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4381 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004382 {
4383 if (*curbuf->b_p_gp == NUL)
4384 program = p_gp;
4385 else
4386 program = curbuf->b_p_gp;
4387 }
4388 else
4389 {
4390 if (*curbuf->b_p_mp == NUL)
4391 program = p_mp;
4392 else
4393 program = curbuf->b_p_mp;
4394 }
4395
4396 p = skipwhite(p);
4397
4398 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4399 {
4400 /* replace $* by given arguments */
4401 i = 1;
4402 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4403 ++i;
4404 len = (int)STRLEN(p);
4405 new_cmdline = alloc((int)(STRLEN(program) + i * (len - 2) + 1));
4406 if (new_cmdline == NULL)
4407 return NULL; /* out of memory */
4408 ptr = new_cmdline;
4409 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4410 {
4411 i = (int)(pos - program);
4412 STRNCPY(ptr, program, i);
4413 STRCPY(ptr += i, p);
4414 ptr += len;
4415 program = pos + 2;
4416 }
4417 STRCPY(ptr, program);
4418 }
4419 else
4420 {
4421 new_cmdline = alloc((int)(STRLEN(program) + STRLEN(p) + 2));
4422 if (new_cmdline == NULL)
4423 return NULL; /* out of memory */
4424 STRCPY(new_cmdline, program);
4425 STRCAT(new_cmdline, " ");
4426 STRCAT(new_cmdline, p);
4427 }
4428 msg_make(p);
4429
4430 /* 'eap->cmd' is not set here, because it is not used at CMD_make */
4431 vim_free(*cmdlinep);
4432 *cmdlinep = new_cmdline;
4433 p = new_cmdline;
4434 }
4435 return p;
4436}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004437#endif
4438
Bram Moolenaar071d4272004-06-13 20:20:40 +00004439/*
4440 * Expand file name in Ex command argument.
4441 * Return FAIL for failure, OK otherwise.
4442 */
4443 int
4444expand_filename(eap, cmdlinep, errormsgp)
4445 exarg_T *eap;
4446 char_u **cmdlinep;
4447 char_u **errormsgp;
4448{
4449 int has_wildcards; /* need to expand wildcards */
4450 char_u *repl;
4451 int srclen;
4452 char_u *p;
4453 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004454 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004455
Bram Moolenaar748bf032005-02-02 23:04:36 +00004456#ifdef FEAT_QUICKFIX
4457 /* Skip a regexp pattern for ":vimgrep[add] pat file..." */
4458 p = skip_grep_pat(eap);
4459#else
4460 p = eap->arg;
4461#endif
4462
Bram Moolenaar071d4272004-06-13 20:20:40 +00004463 /*
4464 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4465 * the file name contains a wildcard it should not cause expanding.
4466 * (it will be expanded anyway if there is a wildcard before replacing).
4467 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004468 has_wildcards = mch_has_wildcard(p);
4469 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004471#ifdef FEAT_EVAL
4472 /* Skip over `=expr`, wildcards in it are not expanded. */
4473 if (p[0] == '`' && p[1] == '=')
4474 {
4475 p += 2;
4476 (void)skip_expr(&p);
4477 if (*p == '`')
4478 ++p;
4479 continue;
4480 }
4481#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004482 /*
4483 * Quick check if this cannot be the start of a special string.
4484 * Also removes backslash before '%', '#' and '<'.
4485 */
4486 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4487 {
4488 ++p;
4489 continue;
4490 }
4491
4492 /*
4493 * Try to find a match at this position.
4494 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004495 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
4496 errormsgp, &escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004497 if (*errormsgp != NULL) /* error detected */
4498 return FAIL;
4499 if (repl == NULL) /* no match found */
4500 {
4501 p += srclen;
4502 continue;
4503 }
4504
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004505 /* Wildcards won't be expanded below, the replacement is taken
4506 * literally. But do expand "~/file", "~user/file" and "$HOME/file". */
4507 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4508 {
4509 char_u *l = repl;
4510
4511 repl = expand_env_save(repl);
4512 vim_free(l);
4513 }
4514
Bram Moolenaar63b92542007-03-27 14:57:09 +00004515 /* Need to escape white space et al. with a backslash.
4516 * Don't do this for:
4517 * - replacement that already has been escaped: "##"
4518 * - shell commands (may have to use quotes instead).
4519 * - non-unix systems when there is a single argument (spaces don't
4520 * separate arguments then).
4521 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004522 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00004523 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00004524 && eap->cmdidx != CMD_bang
4525 && eap->cmdidx != CMD_make
Bram Moolenaara37420f2006-02-04 22:37:47 +00004526 && eap->cmdidx != CMD_lmake
Bram Moolenaar071d4272004-06-13 20:20:40 +00004527 && eap->cmdidx != CMD_grep
Bram Moolenaara37420f2006-02-04 22:37:47 +00004528 && eap->cmdidx != CMD_lgrep
Bram Moolenaar071d4272004-06-13 20:20:40 +00004529 && eap->cmdidx != CMD_grepadd
Bram Moolenaara37420f2006-02-04 22:37:47 +00004530 && eap->cmdidx != CMD_lgrepadd
Bram Moolenaar071d4272004-06-13 20:20:40 +00004531#ifndef UNIX
4532 && !(eap->argt & NOSPC)
4533#endif
4534 )
4535 {
4536 char_u *l;
4537#ifdef BACKSLASH_IN_FILENAME
4538 /* Don't escape a backslash here, because rem_backslash() doesn't
4539 * remove it later. */
4540 static char_u *nobslash = (char_u *)" \t\"|";
4541# define ESCAPE_CHARS nobslash
4542#else
4543# define ESCAPE_CHARS escape_chars
4544#endif
4545
4546 for (l = repl; *l; ++l)
4547 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
4548 {
4549 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
4550 if (l != NULL)
4551 {
4552 vim_free(repl);
4553 repl = l;
4554 }
4555 break;
4556 }
4557 }
4558
4559 /* For a shell command a '!' must be escaped. */
4560 if ((eap->usefilter || eap->cmdidx == CMD_bang)
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004561 && vim_strpbrk(repl, (char_u *)"!&;()<>") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004562 {
4563 char_u *l;
4564
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004565 l = vim_strsave_escaped(repl, (char_u *)"!&;()<>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004566 if (l != NULL)
4567 {
4568 vim_free(repl);
4569 repl = l;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004570 /* For a sh-like shell escape "!" another time. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004571 if (strstr((char *)p_sh, "sh") != NULL)
4572 {
4573 l = vim_strsave_escaped(repl, (char_u *)"!");
4574 if (l != NULL)
4575 {
4576 vim_free(repl);
4577 repl = l;
4578 }
4579 }
4580 }
4581 }
4582
4583 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
4584 vim_free(repl);
4585 if (p == NULL)
4586 return FAIL;
4587 }
4588
4589 /*
4590 * One file argument: Expand wildcards.
4591 * Don't do this with ":r !command" or ":w !command".
4592 */
4593 if ((eap->argt & NOSPC) && !eap->usefilter)
4594 {
4595 /*
4596 * May do this twice:
4597 * 1. Replace environment variables.
4598 * 2. Replace any other wildcards, remove backslashes.
4599 */
4600 for (n = 1; n <= 2; ++n)
4601 {
4602 if (n == 2)
4603 {
4604#ifdef UNIX
4605 /*
4606 * Only for Unix we check for more than one file name.
4607 * For other systems spaces are considered to be part
4608 * of the file name.
4609 * Only check here if there is no wildcard, otherwise
4610 * ExpandOne() will check for errors. This allows
4611 * ":e `ls ve*.c`" on Unix.
4612 */
4613 if (!has_wildcards)
4614 for (p = eap->arg; *p; ++p)
4615 {
4616 /* skip escaped characters */
4617 if (p[1] && (*p == '\\' || *p == Ctrl_V))
4618 ++p;
4619 else if (vim_iswhite(*p))
4620 {
4621 *errormsgp = (char_u *)_("E172: Only one file name allowed");
4622 return FAIL;
4623 }
4624 }
4625#endif
4626
4627 /*
4628 * Halve the number of backslashes (this is Vi compatible).
4629 * For Unix and OS/2, when wildcards are expanded, this is
4630 * done by ExpandOne() below.
4631 */
4632#if defined(UNIX) || defined(OS2)
4633 if (!has_wildcards)
4634#endif
4635 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004636 }
4637
4638 if (has_wildcards)
4639 {
4640 if (n == 1)
4641 {
4642 /*
4643 * First loop: May expand environment variables. This
4644 * can be done much faster with expand_env() than with
4645 * something else (e.g., calling a shell).
4646 * After expanding environment variables, check again
4647 * if there are still wildcards present.
4648 */
4649 if (vim_strchr(eap->arg, '$') != NULL
4650 || vim_strchr(eap->arg, '~') != NULL)
4651 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00004652 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004653 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004654 has_wildcards = mch_has_wildcard(NameBuff);
4655 p = NameBuff;
4656 }
4657 else
4658 p = NULL;
4659 }
4660 else /* n == 2 */
4661 {
4662 expand_T xpc;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004663 int options = WILD_LIST_NOTFOUND|WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004664
4665 ExpandInit(&xpc);
4666 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004667 if (p_wic)
4668 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004669 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01004670 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004671 if (p == NULL)
4672 return FAIL;
4673 }
4674 if (p != NULL)
4675 {
4676 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
4677 p, cmdlinep);
4678 if (n == 2) /* p came from ExpandOne() */
4679 vim_free(p);
4680 }
4681 }
4682 }
4683 }
4684 return OK;
4685}
4686
4687/*
4688 * Replace part of the command line, keeping eap->cmd, eap->arg and
4689 * eap->nextcmd correct.
4690 * "src" points to the part that is to be replaced, of length "srclen".
4691 * "repl" is the replacement string.
4692 * Returns a pointer to the character after the replaced string.
4693 * Returns NULL for failure.
4694 */
4695 static char_u *
4696repl_cmdline(eap, src, srclen, repl, cmdlinep)
4697 exarg_T *eap;
4698 char_u *src;
4699 int srclen;
4700 char_u *repl;
4701 char_u **cmdlinep;
4702{
4703 int len;
4704 int i;
4705 char_u *new_cmdline;
4706
4707 /*
4708 * The new command line is build in new_cmdline[].
4709 * First allocate it.
4710 * Careful: a "+cmd" argument may have been NUL terminated.
4711 */
4712 len = (int)STRLEN(repl);
4713 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004714 if (eap->nextcmd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004715 i += (int)STRLEN(eap->nextcmd);/* add space for next command */
4716 if ((new_cmdline = alloc((unsigned)i)) == NULL)
4717 return NULL; /* out of memory! */
4718
4719 /*
4720 * Copy the stuff before the expanded part.
4721 * Copy the expanded stuff.
4722 * Copy what came after the expanded part.
4723 * Copy the next commands, if there are any.
4724 */
4725 i = (int)(src - *cmdlinep); /* length of part before match */
4726 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00004727
Bram Moolenaar071d4272004-06-13 20:20:40 +00004728 mch_memmove(new_cmdline + i, repl, (size_t)len);
4729 i += len; /* remember the end of the string */
4730 STRCPY(new_cmdline + i, src + srclen);
4731 src = new_cmdline + i; /* remember where to continue */
4732
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004733 if (eap->nextcmd != NULL) /* append next command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004734 {
4735 i = (int)STRLEN(new_cmdline) + 1;
4736 STRCPY(new_cmdline + i, eap->nextcmd);
4737 eap->nextcmd = new_cmdline + i;
4738 }
4739 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
4740 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
4741 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
4742 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
4743 vim_free(*cmdlinep);
4744 *cmdlinep = new_cmdline;
4745
4746 return src;
4747}
4748
4749/*
4750 * Check for '|' to separate commands and '"' to start comments.
4751 */
4752 void
4753separate_nextcmd(eap)
4754 exarg_T *eap;
4755{
4756 char_u *p;
4757
Bram Moolenaar86b68352004-12-27 21:59:20 +00004758#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004759 p = skip_grep_pat(eap);
4760#else
4761 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004762#endif
4763
4764 for ( ; *p; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004765 {
4766 if (*p == Ctrl_V)
4767 {
4768 if (eap->argt & (USECTRLV | XFILE))
4769 ++p; /* skip CTRL-V and next char */
4770 else
Bram Moolenaarb0db5692007-08-14 20:54:49 +00004771 /* remove CTRL-V and skip next char */
Bram Moolenaara7241f52008-06-24 20:39:31 +00004772 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004773 if (*p == NUL) /* stop at NUL after CTRL-V */
4774 break;
4775 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004776
4777#ifdef FEAT_EVAL
4778 /* Skip over `=expr` when wildcards are expanded. */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004779 else if (p[0] == '`' && p[1] == '=' && (eap->argt & XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004780 {
4781 p += 2;
4782 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004783 }
4784#endif
4785
Bram Moolenaar071d4272004-06-13 20:20:40 +00004786 /* Check for '"': start of comment or '|': next command */
4787 /* :@" and :*" do not start a comment!
4788 * :redir @" doesn't either. */
4789 else if ((*p == '"' && !(eap->argt & NOTRLCOM)
4790 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
4791 || p != eap->arg)
4792 && (eap->cmdidx != CMD_redir
4793 || p != eap->arg + 1 || p[-1] != '@'))
4794 || *p == '|' || *p == '\n')
4795 {
4796 /*
4797 * We remove the '\' before the '|', unless USECTRLV is used
4798 * AND 'b' is present in 'cpoptions'.
4799 */
4800 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
4801 || !(eap->argt & USECTRLV)) && *(p - 1) == '\\')
4802 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00004803 STRMOVE(p - 1, p); /* remove the '\' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004804 --p;
4805 }
4806 else
4807 {
4808 eap->nextcmd = check_nextcmd(p);
4809 *p = NUL;
4810 break;
4811 }
4812 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004813 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004814
Bram Moolenaar071d4272004-06-13 20:20:40 +00004815 if (!(eap->argt & NOTRLCOM)) /* remove trailing spaces */
4816 del_trailing_spaces(eap->arg);
4817}
4818
4819/*
4820 * get + command from ex argument
4821 */
4822 static char_u *
4823getargcmd(argp)
4824 char_u **argp;
4825{
4826 char_u *arg = *argp;
4827 char_u *command = NULL;
4828
4829 if (*arg == '+') /* +[command] */
4830 {
4831 ++arg;
4832 if (vim_isspace(*arg))
4833 command = dollar_command;
4834 else
4835 {
4836 command = arg;
4837 arg = skip_cmd_arg(command, TRUE);
4838 if (*arg != NUL)
4839 *arg++ = NUL; /* terminate command with NUL */
4840 }
4841
4842 arg = skipwhite(arg); /* skip over spaces */
4843 *argp = arg;
4844 }
4845 return command;
4846}
4847
4848/*
4849 * Find end of "+command" argument. Skip over "\ " and "\\".
4850 */
4851 static char_u *
4852skip_cmd_arg(p, rembs)
4853 char_u *p;
4854 int rembs; /* TRUE to halve the number of backslashes */
4855{
4856 while (*p && !vim_isspace(*p))
4857 {
4858 if (*p == '\\' && p[1] != NUL)
4859 {
4860 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00004861 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004862 else
4863 ++p;
4864 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004865 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004866 }
4867 return p;
4868}
4869
4870/*
4871 * Get "++opt=arg" argument.
4872 * Return FAIL or OK.
4873 */
4874 static int
4875getargopt(eap)
4876 exarg_T *eap;
4877{
4878 char_u *arg = eap->arg + 2;
4879 int *pp = NULL;
4880#ifdef FEAT_MBYTE
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004881 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004882 char_u *p;
4883#endif
4884
4885 /* ":edit ++[no]bin[ary] file" */
4886 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
4887 {
4888 if (*arg == 'n')
4889 {
4890 arg += 2;
4891 eap->force_bin = FORCE_NOBIN;
4892 }
4893 else
4894 eap->force_bin = FORCE_BIN;
4895 if (!checkforcmd(&arg, "binary", 3))
4896 return FAIL;
4897 eap->arg = skipwhite(arg);
4898 return OK;
4899 }
4900
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004901 /* ":read ++edit file" */
4902 if (STRNCMP(arg, "edit", 4) == 0)
4903 {
4904 eap->read_edit = TRUE;
4905 eap->arg = skipwhite(arg + 4);
4906 return OK;
4907 }
4908
Bram Moolenaar071d4272004-06-13 20:20:40 +00004909 if (STRNCMP(arg, "ff", 2) == 0)
4910 {
4911 arg += 2;
4912 pp = &eap->force_ff;
4913 }
4914 else if (STRNCMP(arg, "fileformat", 10) == 0)
4915 {
4916 arg += 10;
4917 pp = &eap->force_ff;
4918 }
4919#ifdef FEAT_MBYTE
4920 else if (STRNCMP(arg, "enc", 3) == 0)
4921 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01004922 if (STRNCMP(arg, "encoding", 8) == 0)
4923 arg += 8;
4924 else
4925 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004926 pp = &eap->force_enc;
4927 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004928 else if (STRNCMP(arg, "bad", 3) == 0)
4929 {
4930 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004931 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004932 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004933#endif
4934
4935 if (pp == NULL || *arg != '=')
4936 return FAIL;
4937
4938 ++arg;
4939 *pp = (int)(arg - eap->cmd);
4940 arg = skip_cmd_arg(arg, FALSE);
4941 eap->arg = skipwhite(arg);
4942 *arg = NUL;
4943
4944#ifdef FEAT_MBYTE
4945 if (pp == &eap->force_ff)
4946 {
4947#endif
4948 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
4949 return FAIL;
4950#ifdef FEAT_MBYTE
4951 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004952 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004953 {
4954 /* Make 'fileencoding' lower case. */
4955 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
4956 *p = TOLOWER_ASC(*p);
4957 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004958 else
4959 {
4960 /* Check ++bad= argument. Must be a single-byte character, "keep" or
4961 * "drop". */
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004962 p = eap->cmd + bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004963 if (STRICMP(p, "keep") == 0)
4964 eap->bad_char = BAD_KEEP;
4965 else if (STRICMP(p, "drop") == 0)
4966 eap->bad_char = BAD_DROP;
4967 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
4968 eap->bad_char = *p;
4969 else
4970 return FAIL;
4971 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004972#endif
4973
4974 return OK;
4975}
4976
4977/*
4978 * ":abbreviate" and friends.
4979 */
4980 static void
4981ex_abbreviate(eap)
4982 exarg_T *eap;
4983{
4984 do_exmap(eap, TRUE); /* almost the same as mapping */
4985}
4986
4987/*
4988 * ":map" and friends.
4989 */
4990 static void
4991ex_map(eap)
4992 exarg_T *eap;
4993{
4994 /*
4995 * If we are sourcing .exrc or .vimrc in current directory we
4996 * print the mappings for security reasons.
4997 */
4998 if (secure)
4999 {
5000 secure = 2;
5001 msg_outtrans(eap->cmd);
5002 msg_putchar('\n');
5003 }
5004 do_exmap(eap, FALSE);
5005}
5006
5007/*
5008 * ":unmap" and friends.
5009 */
5010 static void
5011ex_unmap(eap)
5012 exarg_T *eap;
5013{
5014 do_exmap(eap, FALSE);
5015}
5016
5017/*
5018 * ":mapclear" and friends.
5019 */
5020 static void
5021ex_mapclear(eap)
5022 exarg_T *eap;
5023{
5024 map_clear(eap->cmd, eap->arg, eap->forceit, FALSE);
5025}
5026
5027/*
5028 * ":abclear" and friends.
5029 */
5030 static void
5031ex_abclear(eap)
5032 exarg_T *eap;
5033{
5034 map_clear(eap->cmd, eap->arg, TRUE, TRUE);
5035}
5036
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005037#if defined(FEAT_AUTOCMD) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005038 static void
5039ex_autocmd(eap)
5040 exarg_T *eap;
5041{
5042 /*
5043 * Disallow auto commands from .exrc and .vimrc in current
5044 * directory for security reasons.
5045 */
5046 if (secure)
5047 {
5048 secure = 2;
5049 eap->errmsg = e_curdir;
5050 }
5051 else if (eap->cmdidx == CMD_autocmd)
5052 do_autocmd(eap->arg, eap->forceit);
5053 else
5054 do_augroup(eap->arg, eap->forceit);
5055}
5056
5057/*
5058 * ":doautocmd": Apply the automatic commands to the current buffer.
5059 */
5060 static void
5061ex_doautocmd(eap)
5062 exarg_T *eap;
5063{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005064 char_u *arg = eap->arg;
5065 int call_do_modelines = check_nomodeline(&arg);
5066
5067 (void)do_doautocmd(arg, TRUE);
5068 if (call_do_modelines) /* Only when there is no <nomodeline>. */
5069 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070}
5071#endif
5072
5073#ifdef FEAT_LISTCMDS
5074/*
5075 * :[N]bunload[!] [N] [bufname] unload buffer
5076 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
5077 * :[N]bwipeout[!] [N] [bufname] delete buffer really
5078 */
5079 static void
5080ex_bunload(eap)
5081 exarg_T *eap;
5082{
5083 eap->errmsg = do_bufdel(
5084 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
5085 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
5086 : DOBUF_UNLOAD, eap->arg,
5087 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
5088}
5089
5090/*
5091 * :[N]buffer [N] to buffer N
5092 * :[N]sbuffer [N] to buffer N
5093 */
5094 static void
5095ex_buffer(eap)
5096 exarg_T *eap;
5097{
5098 if (*eap->arg)
5099 eap->errmsg = e_trailing;
5100 else
5101 {
5102 if (eap->addr_count == 0) /* default is current buffer */
5103 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
5104 else
5105 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
5106 }
5107}
5108
5109/*
5110 * :[N]bmodified [N] to next mod. buffer
5111 * :[N]sbmodified [N] to next mod. buffer
5112 */
5113 static void
5114ex_bmodified(eap)
5115 exarg_T *eap;
5116{
5117 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
5118}
5119
5120/*
5121 * :[N]bnext [N] to next buffer
5122 * :[N]sbnext [N] split and to next buffer
5123 */
5124 static void
5125ex_bnext(eap)
5126 exarg_T *eap;
5127{
5128 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
5129}
5130
5131/*
5132 * :[N]bNext [N] to previous buffer
5133 * :[N]bprevious [N] to previous buffer
5134 * :[N]sbNext [N] split and to previous buffer
5135 * :[N]sbprevious [N] split and to previous buffer
5136 */
5137 static void
5138ex_bprevious(eap)
5139 exarg_T *eap;
5140{
5141 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
5142}
5143
5144/*
5145 * :brewind to first buffer
5146 * :bfirst to first buffer
5147 * :sbrewind split and to first buffer
5148 * :sbfirst split and to first buffer
5149 */
5150 static void
5151ex_brewind(eap)
5152 exarg_T *eap;
5153{
5154 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
5155}
5156
5157/*
5158 * :blast to last buffer
5159 * :sblast split and to last buffer
5160 */
5161 static void
5162ex_blast(eap)
5163 exarg_T *eap;
5164{
5165 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
5166}
5167#endif
5168
5169 int
5170ends_excmd(c)
5171 int c;
5172{
5173 return (c == NUL || c == '|' || c == '"' || c == '\n');
5174}
5175
5176#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5177 || defined(PROTO)
5178/*
5179 * Return the next command, after the first '|' or '\n'.
5180 * Return NULL if not found.
5181 */
5182 char_u *
5183find_nextcmd(p)
5184 char_u *p;
5185{
5186 while (*p != '|' && *p != '\n')
5187 {
5188 if (*p == NUL)
5189 return NULL;
5190 ++p;
5191 }
5192 return (p + 1);
5193}
5194#endif
5195
5196/*
5197 * Check if *p is a separator between Ex commands.
5198 * Return NULL if it isn't, (p + 1) if it is.
5199 */
5200 char_u *
5201check_nextcmd(p)
5202 char_u *p;
5203{
5204 p = skipwhite(p);
5205 if (*p == '|' || *p == '\n')
5206 return (p + 1);
5207 else
5208 return NULL;
5209}
5210
5211/*
5212 * - if there are more files to edit
5213 * - and this is the last window
5214 * - and forceit not used
5215 * - and not repeated twice on a row
5216 * return FAIL and give error message if 'message' TRUE
5217 * return OK otherwise
5218 */
5219 static int
5220check_more(message, forceit)
5221 int message; /* when FALSE check only, no messages */
5222 int forceit;
5223{
5224 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5225
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005226 if (!forceit && only_one_window()
5227 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005228 {
5229 if (message)
5230 {
5231#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
5232 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
5233 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005234 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005235
5236 if (n == 1)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02005237 vim_strncpy(buff,
5238 (char_u *)_("1 more file to edit. Quit anyway?"),
Bram Moolenaard9462e32011-04-11 21:35:11 +02005239 DIALOG_MSG_SIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005240 else
Bram Moolenaard9462e32011-04-11 21:35:11 +02005241 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005242 _("%d more files to edit. Quit anyway?"), n);
5243 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5244 return OK;
5245 return FAIL;
5246 }
5247#endif
5248 if (n == 1)
5249 EMSG(_("E173: 1 more file to edit"));
5250 else
5251 EMSGN(_("E173: %ld more files to edit"), n);
5252 quitmore = 2; /* next try to quit is allowed */
5253 }
5254 return FAIL;
5255 }
5256 return OK;
5257}
5258
5259#ifdef FEAT_CMDL_COMPL
5260/*
5261 * Function given to ExpandGeneric() to obtain the list of command names.
5262 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005263 char_u *
5264get_command_name(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00005265 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005266 int idx;
5267{
5268 if (idx >= (int)CMD_SIZE)
5269# ifdef FEAT_USR_CMDS
5270 return get_user_command_name(idx);
5271# else
5272 return NULL;
5273# endif
5274 return cmdnames[idx].cmd_name;
5275}
5276#endif
5277
5278#if defined(FEAT_USR_CMDS) || defined(PROTO)
5279static 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));
5280static void uc_list __ARGS((char_u *name, size_t name_len));
5281static int uc_scan_attr __ARGS((char_u *attr, size_t len, long *argt, long *def, int *flags, int *compl, char_u **compl_arg));
5282static char_u *uc_split_args __ARGS((char_u *arg, size_t *lenp));
5283static 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));
5284
5285 static int
5286uc_add_command(name, name_len, rep, argt, def, flags, compl, compl_arg, force)
5287 char_u *name;
5288 size_t name_len;
5289 char_u *rep;
5290 long argt;
5291 long def;
5292 int flags;
5293 int compl;
5294 char_u *compl_arg;
5295 int force;
5296{
5297 ucmd_T *cmd = NULL;
5298 char_u *p;
5299 int i;
5300 int cmp = 1;
5301 char_u *rep_buf = NULL;
5302 garray_T *gap;
5303
Bram Moolenaar9c102382006-05-03 21:26:49 +00005304 replace_termcodes(rep, &rep_buf, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005305 if (rep_buf == NULL)
5306 {
5307 /* Can't replace termcodes - try using the string as is */
5308 rep_buf = vim_strsave(rep);
5309
5310 /* Give up if out of memory */
5311 if (rep_buf == NULL)
5312 return FAIL;
5313 }
5314
5315 /* get address of growarray: global or in curbuf */
5316 if (flags & UC_BUFFER)
5317 {
5318 gap = &curbuf->b_ucmds;
5319 if (gap->ga_itemsize == 0)
5320 ga_init2(gap, (int)sizeof(ucmd_T), 4);
5321 }
5322 else
5323 gap = &ucmds;
5324
5325 /* Search for the command in the already defined commands. */
5326 for (i = 0; i < gap->ga_len; ++i)
5327 {
5328 size_t len;
5329
5330 cmd = USER_CMD_GA(gap, i);
5331 len = STRLEN(cmd->uc_name);
5332 cmp = STRNCMP(name, cmd->uc_name, name_len);
5333 if (cmp == 0)
5334 {
5335 if (name_len < len)
5336 cmp = -1;
5337 else if (name_len > len)
5338 cmp = 1;
5339 }
5340
5341 if (cmp == 0)
5342 {
5343 if (!force)
5344 {
5345 EMSG(_("E174: Command already exists: add ! to replace it"));
5346 goto fail;
5347 }
5348
5349 vim_free(cmd->uc_rep);
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00005350 cmd->uc_rep = NULL;
5351#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5352 vim_free(cmd->uc_compl_arg);
5353 cmd->uc_compl_arg = NULL;
5354#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005355 break;
5356 }
5357
5358 /* Stop as soon as we pass the name to add */
5359 if (cmp < 0)
5360 break;
5361 }
5362
5363 /* Extend the array unless we're replacing an existing command */
5364 if (cmp != 0)
5365 {
5366 if (ga_grow(gap, 1) != OK)
5367 goto fail;
5368 if ((p = vim_strnsave(name, (int)name_len)) == NULL)
5369 goto fail;
5370
5371 cmd = USER_CMD_GA(gap, i);
5372 mch_memmove(cmd + 1, cmd, (gap->ga_len - i) * sizeof(ucmd_T));
5373
5374 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005375
5376 cmd->uc_name = p;
5377 }
5378
5379 cmd->uc_rep = rep_buf;
5380 cmd->uc_argt = argt;
5381 cmd->uc_def = def;
5382 cmd->uc_compl = compl;
5383#ifdef FEAT_EVAL
5384 cmd->uc_scriptID = current_SID;
5385# ifdef FEAT_CMDL_COMPL
5386 cmd->uc_compl_arg = compl_arg;
5387# endif
5388#endif
5389
5390 return OK;
5391
5392fail:
5393 vim_free(rep_buf);
5394#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5395 vim_free(compl_arg);
5396#endif
5397 return FAIL;
5398}
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005399#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005400
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005401#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005402/*
5403 * List of names for completion for ":command" with the EXPAND_ flag.
5404 * Must be alphabetical for completion.
5405 */
5406static struct
5407{
5408 int expand;
5409 char *name;
5410} command_complete[] =
5411{
5412 {EXPAND_AUGROUP, "augroup"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005413 {EXPAND_BEHAVE, "behave"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005414 {EXPAND_BUFFERS, "buffer"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005415 {EXPAND_COLORS, "color"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005416 {EXPAND_COMMANDS, "command"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005417 {EXPAND_COMPILER, "compiler"},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005418#if defined(FEAT_CSCOPE)
5419 {EXPAND_CSCOPE, "cscope"},
5420#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005421#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5422 {EXPAND_USER_DEFINED, "custom"},
Bram Moolenaara466c992005-07-09 21:03:22 +00005423 {EXPAND_USER_LIST, "customlist"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005424#endif
5425 {EXPAND_DIRECTORIES, "dir"},
5426 {EXPAND_ENV_VARS, "environment"},
5427 {EXPAND_EVENTS, "event"},
5428 {EXPAND_EXPRESSION, "expression"},
5429 {EXPAND_FILES, "file"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005430 {EXPAND_FILES_IN_PATH, "file_in_path"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005431 {EXPAND_FILETYPE, "filetype"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005432 {EXPAND_FUNCTIONS, "function"},
5433 {EXPAND_HELP, "help"},
5434 {EXPAND_HIGHLIGHT, "highlight"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005435#if defined(FEAT_CMDHIST)
5436 {EXPAND_HISTORY, "history"},
5437#endif
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005438#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005439 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005440 {EXPAND_LOCALES, "locale"},
5441#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005442 {EXPAND_MAPPINGS, "mapping"},
5443 {EXPAND_MENUS, "menu"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005444 {EXPAND_OWNSYNTAX, "syntax"},
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02005445#if defined(FEAT_PROFILE)
5446 {EXPAND_SYNTIME, "syntime"},
5447#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005448 {EXPAND_SETTINGS, "option"},
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005449 {EXPAND_SHELLCMD, "shellcmd"},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005450#if defined(FEAT_SIGNS)
5451 {EXPAND_SIGN, "sign"},
5452#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005453 {EXPAND_TAGS, "tag"},
5454 {EXPAND_TAGS_LISTFILES, "tag_listfiles"},
Bram Moolenaar24305862012-08-15 14:05:05 +02005455 {EXPAND_USER, "user"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005456 {EXPAND_USER_VARS, "var"},
5457 {0, NULL}
5458};
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005459#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005460
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005461#if defined(FEAT_USR_CMDS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005462 static void
5463uc_list(name, name_len)
5464 char_u *name;
5465 size_t name_len;
5466{
5467 int i, j;
5468 int found = FALSE;
5469 ucmd_T *cmd;
5470 int len;
5471 long a;
5472 garray_T *gap;
5473
5474 gap = &curbuf->b_ucmds;
5475 for (;;)
5476 {
5477 for (i = 0; i < gap->ga_len; ++i)
5478 {
5479 cmd = USER_CMD_GA(gap, i);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005480 a = (long)cmd->uc_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005481
5482 /* Skip commands which don't match the requested prefix */
5483 if (STRNCMP(name, cmd->uc_name, name_len) != 0)
5484 continue;
5485
5486 /* Put out the title first time */
5487 if (!found)
5488 MSG_PUTS_TITLE(_("\n Name Args Range Complete Definition"));
5489 found = TRUE;
5490 msg_putchar('\n');
5491 if (got_int)
5492 break;
5493
5494 /* Special cases */
5495 msg_putchar(a & BANG ? '!' : ' ');
5496 msg_putchar(a & REGSTR ? '"' : ' ');
5497 msg_putchar(gap != &ucmds ? 'b' : ' ');
5498 msg_putchar(' ');
5499
5500 msg_outtrans_attr(cmd->uc_name, hl_attr(HLF_D));
5501 len = (int)STRLEN(cmd->uc_name) + 4;
5502
5503 do {
5504 msg_putchar(' ');
5505 ++len;
5506 } while (len < 16);
5507
5508 len = 0;
5509
5510 /* Arguments */
5511 switch ((int)(a & (EXTRA|NOSPC|NEEDARG)))
5512 {
5513 case 0: IObuff[len++] = '0'; break;
5514 case (EXTRA): IObuff[len++] = '*'; break;
5515 case (EXTRA|NOSPC): IObuff[len++] = '?'; break;
5516 case (EXTRA|NEEDARG): IObuff[len++] = '+'; break;
5517 case (EXTRA|NOSPC|NEEDARG): IObuff[len++] = '1'; break;
5518 }
5519
5520 do {
5521 IObuff[len++] = ' ';
5522 } while (len < 5);
5523
5524 /* Range */
5525 if (a & (RANGE|COUNT))
5526 {
5527 if (a & COUNT)
5528 {
5529 /* -count=N */
5530 sprintf((char *)IObuff + len, "%ldc", cmd->uc_def);
5531 len += (int)STRLEN(IObuff + len);
5532 }
5533 else if (a & DFLALL)
5534 IObuff[len++] = '%';
5535 else if (cmd->uc_def >= 0)
5536 {
5537 /* -range=N */
5538 sprintf((char *)IObuff + len, "%ld", cmd->uc_def);
5539 len += (int)STRLEN(IObuff + len);
5540 }
5541 else
5542 IObuff[len++] = '.';
5543 }
5544
5545 do {
5546 IObuff[len++] = ' ';
5547 } while (len < 11);
5548
5549 /* Completion */
5550 for (j = 0; command_complete[j].expand != 0; ++j)
5551 if (command_complete[j].expand == cmd->uc_compl)
5552 {
5553 STRCPY(IObuff + len, command_complete[j].name);
5554 len += (int)STRLEN(IObuff + len);
5555 break;
5556 }
5557
5558 do {
5559 IObuff[len++] = ' ';
5560 } while (len < 21);
5561
5562 IObuff[len] = '\0';
5563 msg_outtrans(IObuff);
5564
5565 msg_outtrans_special(cmd->uc_rep, FALSE);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005566#ifdef FEAT_EVAL
5567 if (p_verbose > 0)
5568 last_set_msg(cmd->uc_scriptID);
5569#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005570 out_flush();
5571 ui_breakcheck();
5572 if (got_int)
5573 break;
5574 }
5575 if (gap == &ucmds || i < gap->ga_len)
5576 break;
5577 gap = &ucmds;
5578 }
5579
5580 if (!found)
5581 MSG(_("No user-defined commands found"));
5582}
5583
5584 static char_u *
5585uc_fun_cmd()
5586{
5587 static char_u fcmd[] = {0x84, 0xaf, 0x60, 0xb9, 0xaf, 0xb5, 0x60, 0xa4,
5588 0xa5, 0xad, 0xa1, 0xae, 0xa4, 0x60, 0xa1, 0x60,
5589 0xb3, 0xa8, 0xb2, 0xb5, 0xa2, 0xa2, 0xa5, 0xb2,
5590 0xb9, 0x7f, 0};
5591 int i;
5592
5593 for (i = 0; fcmd[i]; ++i)
5594 IObuff[i] = fcmd[i] - 0x40;
5595 IObuff[i] = 0;
5596 return IObuff;
5597}
5598
5599 static int
5600uc_scan_attr(attr, len, argt, def, flags, compl, compl_arg)
5601 char_u *attr;
5602 size_t len;
5603 long *argt;
5604 long *def;
5605 int *flags;
5606 int *compl;
5607 char_u **compl_arg;
5608{
5609 char_u *p;
5610
5611 if (len == 0)
5612 {
5613 EMSG(_("E175: No attribute specified"));
5614 return FAIL;
5615 }
5616
5617 /* First, try the simple attributes (no arguments) */
5618 if (STRNICMP(attr, "bang", len) == 0)
5619 *argt |= BANG;
5620 else if (STRNICMP(attr, "buffer", len) == 0)
5621 *flags |= UC_BUFFER;
5622 else if (STRNICMP(attr, "register", len) == 0)
5623 *argt |= REGSTR;
5624 else if (STRNICMP(attr, "bar", len) == 0)
5625 *argt |= TRLBAR;
5626 else
5627 {
5628 int i;
5629 char_u *val = NULL;
5630 size_t vallen = 0;
5631 size_t attrlen = len;
5632
5633 /* Look for the attribute name - which is the part before any '=' */
5634 for (i = 0; i < (int)len; ++i)
5635 {
5636 if (attr[i] == '=')
5637 {
5638 val = &attr[i + 1];
5639 vallen = len - i - 1;
5640 attrlen = i;
5641 break;
5642 }
5643 }
5644
5645 if (STRNICMP(attr, "nargs", attrlen) == 0)
5646 {
5647 if (vallen == 1)
5648 {
5649 if (*val == '0')
5650 /* Do nothing - this is the default */;
5651 else if (*val == '1')
5652 *argt |= (EXTRA | NOSPC | NEEDARG);
5653 else if (*val == '*')
5654 *argt |= EXTRA;
5655 else if (*val == '?')
5656 *argt |= (EXTRA | NOSPC);
5657 else if (*val == '+')
5658 *argt |= (EXTRA | NEEDARG);
5659 else
5660 goto wrong_nargs;
5661 }
5662 else
5663 {
5664wrong_nargs:
5665 EMSG(_("E176: Invalid number of arguments"));
5666 return FAIL;
5667 }
5668 }
5669 else if (STRNICMP(attr, "range", attrlen) == 0)
5670 {
5671 *argt |= RANGE;
5672 if (vallen == 1 && *val == '%')
5673 *argt |= DFLALL;
5674 else if (val != NULL)
5675 {
5676 p = val;
5677 if (*def >= 0)
5678 {
5679two_count:
5680 EMSG(_("E177: Count cannot be specified twice"));
5681 return FAIL;
5682 }
5683
5684 *def = getdigits(&p);
5685 *argt |= (ZEROR | NOTADR);
5686
5687 if (p != val + vallen || vallen == 0)
5688 {
5689invalid_count:
5690 EMSG(_("E178: Invalid default value for count"));
5691 return FAIL;
5692 }
5693 }
5694 }
5695 else if (STRNICMP(attr, "count", attrlen) == 0)
5696 {
Bram Moolenaar32e7b2d2005-02-27 22:36:47 +00005697 *argt |= (COUNT | ZEROR | RANGE | NOTADR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005698
5699 if (val != NULL)
5700 {
5701 p = val;
5702 if (*def >= 0)
5703 goto two_count;
5704
5705 *def = getdigits(&p);
5706
5707 if (p != val + vallen)
5708 goto invalid_count;
5709 }
5710
5711 if (*def < 0)
5712 *def = 0;
5713 }
5714 else if (STRNICMP(attr, "complete", attrlen) == 0)
5715 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005716 if (val == NULL)
5717 {
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005718 EMSG(_("E179: argument required for -complete"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719 return FAIL;
5720 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005721
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005722 if (parse_compl_arg(val, (int)vallen, compl, argt, compl_arg)
5723 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005724 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005725 }
5726 else
5727 {
5728 char_u ch = attr[len];
5729 attr[len] = '\0';
5730 EMSG2(_("E181: Invalid attribute: %s"), attr);
5731 attr[len] = ch;
5732 return FAIL;
5733 }
5734 }
5735
5736 return OK;
5737}
5738
Bram Moolenaara850a712009-01-28 14:42:59 +00005739/*
5740 * ":command ..."
5741 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005742 static void
5743ex_command(eap)
5744 exarg_T *eap;
5745{
5746 char_u *name;
5747 char_u *end;
5748 char_u *p;
5749 long argt = 0;
5750 long def = -1;
5751 int flags = 0;
5752 int compl = EXPAND_NOTHING;
5753 char_u *compl_arg = NULL;
5754 int has_attr = (eap->arg[0] == '-');
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01005755 int name_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005756
5757 p = eap->arg;
5758
5759 /* Check for attributes */
5760 while (*p == '-')
5761 {
5762 ++p;
5763 end = skiptowhite(p);
5764 if (uc_scan_attr(p, end - p, &argt, &def, &flags, &compl, &compl_arg)
5765 == FAIL)
5766 return;
5767 p = skipwhite(end);
5768 }
5769
5770 /* Get the name (if any) and skip to the following argument */
5771 name = p;
5772 if (ASCII_ISALPHA(*p))
5773 while (ASCII_ISALNUM(*p))
5774 ++p;
5775 if (!ends_excmd(*p) && !vim_iswhite(*p))
5776 {
5777 EMSG(_("E182: Invalid command name"));
5778 return;
5779 }
5780 end = p;
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01005781 name_len = (int)(end - name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005782
5783 /* If there is nothing after the name, and no attributes were specified,
5784 * we are listing commands
5785 */
5786 p = skipwhite(end);
5787 if (!has_attr && ends_excmd(*p))
5788 {
5789 uc_list(name, end - name);
5790 }
5791 else if (!ASCII_ISUPPER(*name))
5792 {
5793 EMSG(_("E183: User defined commands must start with an uppercase letter"));
5794 return;
5795 }
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01005796 else if ((name_len == 1 && *name == 'X')
5797 || (name_len <= 4
5798 && STRNCMP(name, "Next", name_len > 4 ? 4 : name_len) == 0))
5799 {
5800 EMSG(_("E841: Reserved name, cannot be used for user defined command"));
5801 return;
5802 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005803 else
5804 uc_add_command(name, end - name, p, argt, def, flags, compl, compl_arg,
5805 eap->forceit);
5806}
5807
5808/*
5809 * ":comclear"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005810 * Clear all user commands, global and for current buffer.
5811 */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00005812 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005813ex_comclear(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00005814 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005815{
5816 uc_clear(&ucmds);
5817 uc_clear(&curbuf->b_ucmds);
5818}
5819
5820/*
5821 * Clear all user commands for "gap".
5822 */
5823 void
5824uc_clear(gap)
5825 garray_T *gap;
5826{
5827 int i;
5828 ucmd_T *cmd;
5829
5830 for (i = 0; i < gap->ga_len; ++i)
5831 {
5832 cmd = USER_CMD_GA(gap, i);
5833 vim_free(cmd->uc_name);
5834 vim_free(cmd->uc_rep);
5835# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5836 vim_free(cmd->uc_compl_arg);
5837# endif
5838 }
5839 ga_clear(gap);
5840}
5841
5842 static void
5843ex_delcommand(eap)
5844 exarg_T *eap;
5845{
5846 int i = 0;
5847 ucmd_T *cmd = NULL;
5848 int cmp = -1;
5849 garray_T *gap;
5850
5851 gap = &curbuf->b_ucmds;
5852 for (;;)
5853 {
5854 for (i = 0; i < gap->ga_len; ++i)
5855 {
5856 cmd = USER_CMD_GA(gap, i);
5857 cmp = STRCMP(eap->arg, cmd->uc_name);
5858 if (cmp <= 0)
5859 break;
5860 }
5861 if (gap == &ucmds || cmp == 0)
5862 break;
5863 gap = &ucmds;
5864 }
5865
5866 if (cmp != 0)
5867 {
5868 EMSG2(_("E184: No such user-defined command: %s"), eap->arg);
5869 return;
5870 }
5871
5872 vim_free(cmd->uc_name);
5873 vim_free(cmd->uc_rep);
5874# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5875 vim_free(cmd->uc_compl_arg);
5876# endif
5877
5878 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005879
5880 if (i < gap->ga_len)
5881 mch_memmove(cmd, cmd + 1, (gap->ga_len - i) * sizeof(ucmd_T));
5882}
5883
Bram Moolenaar552f8a12007-03-08 17:12:08 +00005884/*
5885 * split and quote args for <f-args>
5886 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005887 static char_u *
5888uc_split_args(arg, lenp)
5889 char_u *arg;
5890 size_t *lenp;
5891{
5892 char_u *buf;
5893 char_u *p;
5894 char_u *q;
5895 int len;
5896
5897 /* Precalculate length */
5898 p = arg;
5899 len = 2; /* Initial and final quotes */
5900
5901 while (*p)
5902 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00005903 if (p[0] == '\\' && p[1] == '\\')
5904 {
5905 len += 2;
5906 p += 2;
5907 }
5908 else if (p[0] == '\\' && vim_iswhite(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005909 {
5910 len += 1;
5911 p += 2;
5912 }
5913 else if (*p == '\\' || *p == '"')
5914 {
5915 len += 2;
5916 p += 1;
5917 }
5918 else if (vim_iswhite(*p))
5919 {
5920 p = skipwhite(p);
5921 if (*p == NUL)
5922 break;
5923 len += 3; /* "," */
5924 }
5925 else
5926 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02005927#ifdef FEAT_MBYTE
5928 int charlen = (*mb_ptr2len)(p);
5929 len += charlen;
5930 p += charlen;
5931#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005932 ++len;
5933 ++p;
Bram Moolenaardfef1542012-07-10 19:25:10 +02005934#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005935 }
5936 }
5937
5938 buf = alloc(len + 1);
5939 if (buf == NULL)
5940 {
5941 *lenp = 0;
5942 return buf;
5943 }
5944
5945 p = arg;
5946 q = buf;
5947 *q++ = '"';
5948 while (*p)
5949 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00005950 if (p[0] == '\\' && p[1] == '\\')
5951 {
5952 *q++ = '\\';
5953 *q++ = '\\';
5954 p += 2;
5955 }
5956 else if (p[0] == '\\' && vim_iswhite(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005957 {
5958 *q++ = p[1];
5959 p += 2;
5960 }
5961 else if (*p == '\\' || *p == '"')
5962 {
5963 *q++ = '\\';
5964 *q++ = *p++;
5965 }
5966 else if (vim_iswhite(*p))
5967 {
5968 p = skipwhite(p);
5969 if (*p == NUL)
5970 break;
5971 *q++ = '"';
5972 *q++ = ',';
5973 *q++ = '"';
5974 }
5975 else
5976 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02005977 MB_COPY_CHAR(p, q);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005978 }
5979 }
5980 *q++ = '"';
5981 *q = 0;
5982
5983 *lenp = len;
5984 return buf;
5985}
5986
5987/*
5988 * Check for a <> code in a user command.
5989 * "code" points to the '<'. "len" the length of the <> (inclusive).
5990 * "buf" is where the result is to be added.
5991 * "split_buf" points to a buffer used for splitting, caller should free it.
5992 * "split_len" is the length of what "split_buf" contains.
5993 * Returns the length of the replacement, which has been added to "buf".
5994 * Returns -1 if there was no match, and only the "<" has been copied.
5995 */
5996 static size_t
5997uc_check_code(code, len, buf, cmd, eap, split_buf, split_len)
5998 char_u *code;
5999 size_t len;
6000 char_u *buf;
6001 ucmd_T *cmd; /* the user command we're expanding */
6002 exarg_T *eap; /* ex arguments */
6003 char_u **split_buf;
6004 size_t *split_len;
6005{
6006 size_t result = 0;
6007 char_u *p = code + 1;
6008 size_t l = len - 2;
6009 int quote = 0;
6010 enum { ct_ARGS, ct_BANG, ct_COUNT, ct_LINE1, ct_LINE2, ct_REGISTER,
6011 ct_LT, ct_NONE } type = ct_NONE;
6012
6013 if ((vim_strchr((char_u *)"qQfF", *p) != NULL) && p[1] == '-')
6014 {
6015 quote = (*p == 'q' || *p == 'Q') ? 1 : 2;
6016 p += 2;
6017 l -= 2;
6018 }
6019
Bram Moolenaar371d5402006-03-20 21:47:49 +00006020 ++l;
6021 if (l <= 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006022 type = ct_NONE;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006023 else if (STRNICMP(p, "args>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006024 type = ct_ARGS;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006025 else if (STRNICMP(p, "bang>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006026 type = ct_BANG;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006027 else if (STRNICMP(p, "count>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006028 type = ct_COUNT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006029 else if (STRNICMP(p, "line1>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006030 type = ct_LINE1;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006031 else if (STRNICMP(p, "line2>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006032 type = ct_LINE2;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006033 else if (STRNICMP(p, "lt>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006034 type = ct_LT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006035 else if (STRNICMP(p, "reg>", l) == 0 || STRNICMP(p, "register>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006036 type = ct_REGISTER;
6037
6038 switch (type)
6039 {
6040 case ct_ARGS:
6041 /* Simple case first */
6042 if (*eap->arg == NUL)
6043 {
6044 if (quote == 1)
6045 {
6046 result = 2;
6047 if (buf != NULL)
6048 STRCPY(buf, "''");
6049 }
6050 else
6051 result = 0;
6052 break;
6053 }
6054
6055 /* When specified there is a single argument don't split it.
6056 * Works for ":Cmd %" when % is "a b c". */
6057 if ((eap->argt & NOSPC) && quote == 2)
6058 quote = 1;
6059
6060 switch (quote)
6061 {
6062 case 0: /* No quoting, no splitting */
6063 result = STRLEN(eap->arg);
6064 if (buf != NULL)
6065 STRCPY(buf, eap->arg);
6066 break;
6067 case 1: /* Quote, but don't split */
6068 result = STRLEN(eap->arg) + 2;
6069 for (p = eap->arg; *p; ++p)
6070 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006071#ifdef FEAT_MBYTE
6072 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6073 /* DBCS can contain \ in a trail byte, skip the
6074 * double-byte character. */
6075 ++p;
6076 else
6077#endif
6078 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006079 ++result;
6080 }
6081
6082 if (buf != NULL)
6083 {
6084 *buf++ = '"';
6085 for (p = eap->arg; *p; ++p)
6086 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006087#ifdef FEAT_MBYTE
6088 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6089 /* DBCS can contain \ in a trail byte, copy the
6090 * double-byte character to avoid escaping. */
6091 *buf++ = *p++;
6092 else
6093#endif
6094 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006095 *buf++ = '\\';
6096 *buf++ = *p;
6097 }
6098 *buf = '"';
6099 }
6100
6101 break;
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006102 case 2: /* Quote and split (<f-args>) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006103 /* This is hard, so only do it once, and cache the result */
6104 if (*split_buf == NULL)
6105 *split_buf = uc_split_args(eap->arg, split_len);
6106
6107 result = *split_len;
6108 if (buf != NULL && result != 0)
6109 STRCPY(buf, *split_buf);
6110
6111 break;
6112 }
6113 break;
6114
6115 case ct_BANG:
6116 result = eap->forceit ? 1 : 0;
6117 if (quote)
6118 result += 2;
6119 if (buf != NULL)
6120 {
6121 if (quote)
6122 *buf++ = '"';
6123 if (eap->forceit)
6124 *buf++ = '!';
6125 if (quote)
6126 *buf = '"';
6127 }
6128 break;
6129
6130 case ct_LINE1:
6131 case ct_LINE2:
6132 case ct_COUNT:
6133 {
6134 char num_buf[20];
6135 long num = (type == ct_LINE1) ? eap->line1 :
6136 (type == ct_LINE2) ? eap->line2 :
6137 (eap->addr_count > 0) ? eap->line2 : cmd->uc_def;
6138 size_t num_len;
6139
6140 sprintf(num_buf, "%ld", num);
6141 num_len = STRLEN(num_buf);
6142 result = num_len;
6143
6144 if (quote)
6145 result += 2;
6146
6147 if (buf != NULL)
6148 {
6149 if (quote)
6150 *buf++ = '"';
6151 STRCPY(buf, num_buf);
6152 buf += num_len;
6153 if (quote)
6154 *buf = '"';
6155 }
6156
6157 break;
6158 }
6159
6160 case ct_REGISTER:
6161 result = eap->regname ? 1 : 0;
6162 if (quote)
6163 result += 2;
6164 if (buf != NULL)
6165 {
6166 if (quote)
6167 *buf++ = '\'';
6168 if (eap->regname)
6169 *buf++ = eap->regname;
6170 if (quote)
6171 *buf = '\'';
6172 }
6173 break;
6174
6175 case ct_LT:
6176 result = 1;
6177 if (buf != NULL)
6178 *buf = '<';
6179 break;
6180
6181 default:
6182 /* Not recognized: just copy the '<' and return -1. */
6183 result = (size_t)-1;
6184 if (buf != NULL)
6185 *buf = '<';
6186 break;
6187 }
6188
6189 return result;
6190}
6191
6192 static void
6193do_ucmd(eap)
6194 exarg_T *eap;
6195{
6196 char_u *buf;
6197 char_u *p;
6198 char_u *q;
6199
6200 char_u *start;
Bram Moolenaar25648a52009-02-21 19:37:46 +00006201 char_u *end = NULL;
Bram Moolenaara850a712009-01-28 14:42:59 +00006202 char_u *ksp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006203 size_t len, totlen;
6204
6205 size_t split_len = 0;
6206 char_u *split_buf = NULL;
6207 ucmd_T *cmd;
6208#ifdef FEAT_EVAL
6209 scid_T save_current_SID = current_SID;
6210#endif
6211
6212 if (eap->cmdidx == CMD_USER)
6213 cmd = USER_CMD(eap->useridx);
6214 else
6215 cmd = USER_CMD_GA(&curbuf->b_ucmds, eap->useridx);
6216
6217 /*
6218 * Replace <> in the command by the arguments.
Bram Moolenaara850a712009-01-28 14:42:59 +00006219 * First round: "buf" is NULL, compute length, allocate "buf".
6220 * Second round: copy result into "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006221 */
6222 buf = NULL;
6223 for (;;)
6224 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006225 p = cmd->uc_rep; /* source */
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00006226 q = buf; /* destination */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006227 totlen = 0;
Bram Moolenaara850a712009-01-28 14:42:59 +00006228
6229 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006230 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006231 start = vim_strchr(p, '<');
6232 if (start != NULL)
6233 end = vim_strchr(start + 1, '>');
6234 if (buf != NULL)
6235 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006236 for (ksp = p; *ksp != NUL && *ksp != K_SPECIAL; ++ksp)
6237 ;
6238 if (*ksp == K_SPECIAL
6239 && (start == NULL || ksp < start || end == NULL)
Bram Moolenaara850a712009-01-28 14:42:59 +00006240 && ((ksp[1] == KS_SPECIAL && ksp[2] == KE_FILLER)
6241# ifdef FEAT_GUI
6242 || (ksp[1] == KS_EXTRA && ksp[2] == (int)KE_CSI)
6243# endif
6244 ))
6245 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006246 /* K_SPECIAL has been put in the buffer as K_SPECIAL
Bram Moolenaara850a712009-01-28 14:42:59 +00006247 * KS_SPECIAL KE_FILLER, like for mappings, but
6248 * do_cmdline() doesn't handle that, so convert it back.
6249 * Also change K_SPECIAL KS_EXTRA KE_CSI into CSI. */
6250 len = ksp - p;
6251 if (len > 0)
6252 {
6253 mch_memmove(q, p, len);
6254 q += len;
6255 }
6256 *q++ = ksp[1] == KS_SPECIAL ? K_SPECIAL : CSI;
6257 p = ksp + 3;
6258 continue;
6259 }
6260 }
6261
6262 /* break if there no <item> is found */
6263 if (start == NULL || end == NULL)
6264 break;
6265
Bram Moolenaar071d4272004-06-13 20:20:40 +00006266 /* Include the '>' */
6267 ++end;
6268
6269 /* Take everything up to the '<' */
6270 len = start - p;
6271 if (buf == NULL)
6272 totlen += len;
6273 else
6274 {
6275 mch_memmove(q, p, len);
6276 q += len;
6277 }
6278
6279 len = uc_check_code(start, end - start, q, cmd, eap,
6280 &split_buf, &split_len);
6281 if (len == (size_t)-1)
6282 {
6283 /* no match, continue after '<' */
6284 p = start + 1;
6285 len = 1;
6286 }
6287 else
6288 p = end;
6289 if (buf == NULL)
6290 totlen += len;
6291 else
6292 q += len;
6293 }
6294 if (buf != NULL) /* second time here, finished */
6295 {
6296 STRCPY(q, p);
6297 break;
6298 }
6299
6300 totlen += STRLEN(p); /* Add on the trailing characters */
6301 buf = alloc((unsigned)(totlen + 1));
6302 if (buf == NULL)
6303 {
6304 vim_free(split_buf);
6305 return;
6306 }
6307 }
6308
6309#ifdef FEAT_EVAL
6310 current_SID = cmd->uc_scriptID;
6311#endif
6312 (void)do_cmdline(buf, eap->getline, eap->cookie,
6313 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
6314#ifdef FEAT_EVAL
6315 current_SID = save_current_SID;
6316#endif
6317 vim_free(buf);
6318 vim_free(split_buf);
6319}
6320
6321# if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6322 static char_u *
6323get_user_command_name(idx)
6324 int idx;
6325{
6326 return get_user_commands(NULL, idx - (int)CMD_SIZE);
6327}
6328
6329/*
6330 * Function given to ExpandGeneric() to obtain the list of user command names.
6331 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006332 char_u *
6333get_user_commands(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006334 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006335 int idx;
6336{
6337 if (idx < curbuf->b_ucmds.ga_len)
6338 return USER_CMD_GA(&curbuf->b_ucmds, idx)->uc_name;
6339 idx -= curbuf->b_ucmds.ga_len;
6340 if (idx < ucmds.ga_len)
6341 return USER_CMD(idx)->uc_name;
6342 return NULL;
6343}
6344
6345/*
6346 * Function given to ExpandGeneric() to obtain the list of user command
6347 * attributes.
6348 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006349 char_u *
6350get_user_cmd_flags(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006351 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006352 int idx;
6353{
6354 static char *user_cmd_flags[] =
6355 {"bang", "bar", "buffer", "complete", "count",
6356 "nargs", "range", "register"};
6357
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00006358 if (idx >= (int)(sizeof(user_cmd_flags) / sizeof(user_cmd_flags[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006359 return NULL;
6360 return (char_u *)user_cmd_flags[idx];
6361}
6362
6363/*
6364 * Function given to ExpandGeneric() to obtain the list of values for -nargs.
6365 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006366 char_u *
6367get_user_cmd_nargs(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006368 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006369 int idx;
6370{
6371 static char *user_cmd_nargs[] = {"0", "1", "*", "?", "+"};
6372
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00006373 if (idx >= (int)(sizeof(user_cmd_nargs) / sizeof(user_cmd_nargs[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006374 return NULL;
6375 return (char_u *)user_cmd_nargs[idx];
6376}
6377
6378/*
6379 * Function given to ExpandGeneric() to obtain the list of values for -complete.
6380 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006381 char_u *
6382get_user_cmd_complete(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006383 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006384 int idx;
6385{
6386 return (char_u *)command_complete[idx].name;
6387}
6388# endif /* FEAT_CMDL_COMPL */
6389
6390#endif /* FEAT_USR_CMDS */
6391
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006392#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
6393/*
6394 * Parse a completion argument "value[vallen]".
6395 * The detected completion goes in "*complp", argument type in "*argt".
6396 * When there is an argument, for function and user defined completion, it's
6397 * copied to allocated memory and stored in "*compl_arg".
6398 * Returns FAIL if something is wrong.
6399 */
6400 int
6401parse_compl_arg(value, vallen, complp, argt, compl_arg)
6402 char_u *value;
6403 int vallen;
6404 int *complp;
6405 long *argt;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006406 char_u **compl_arg UNUSED;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006407{
6408 char_u *arg = NULL;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006409# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006410 size_t arglen = 0;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006411# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006412 int i;
6413 int valend = vallen;
6414
6415 /* Look for any argument part - which is the part after any ',' */
6416 for (i = 0; i < vallen; ++i)
6417 {
6418 if (value[i] == ',')
6419 {
6420 arg = &value[i + 1];
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006421# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006422 arglen = vallen - i - 1;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006423# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006424 valend = i;
6425 break;
6426 }
6427 }
6428
6429 for (i = 0; command_complete[i].expand != 0; ++i)
6430 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006431 if ((int)STRLEN(command_complete[i].name) == valend
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006432 && STRNCMP(value, command_complete[i].name, valend) == 0)
6433 {
6434 *complp = command_complete[i].expand;
6435 if (command_complete[i].expand == EXPAND_BUFFERS)
6436 *argt |= BUFNAME;
6437 else if (command_complete[i].expand == EXPAND_DIRECTORIES
6438 || command_complete[i].expand == EXPAND_FILES)
6439 *argt |= XFILE;
6440 break;
6441 }
6442 }
6443
6444 if (command_complete[i].expand == 0)
6445 {
6446 EMSG2(_("E180: Invalid complete value: %s"), value);
6447 return FAIL;
6448 }
6449
6450# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6451 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST
6452 && arg != NULL)
6453# else
6454 if (arg != NULL)
6455# endif
6456 {
6457 EMSG(_("E468: Completion argument only allowed for custom completion"));
6458 return FAIL;
6459 }
6460
6461# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6462 if ((*complp == EXPAND_USER_DEFINED || *complp == EXPAND_USER_LIST)
6463 && arg == NULL)
6464 {
6465 EMSG(_("E467: Custom completion requires a function argument"));
6466 return FAIL;
6467 }
6468
6469 if (arg != NULL)
6470 *compl_arg = vim_strnsave(arg, (int)arglen);
6471# endif
6472 return OK;
6473}
6474#endif
6475
Bram Moolenaar071d4272004-06-13 20:20:40 +00006476 static void
6477ex_colorscheme(eap)
6478 exarg_T *eap;
6479{
Bram Moolenaare6850792010-05-14 15:28:44 +02006480 if (*eap->arg == NUL)
6481 {
6482#ifdef FEAT_EVAL
6483 char_u *expr = vim_strsave((char_u *)"g:colors_name");
6484 char_u *p = NULL;
6485
6486 if (expr != NULL)
6487 {
6488 ++emsg_off;
6489 p = eval_to_string(expr, NULL, FALSE);
6490 --emsg_off;
6491 vim_free(expr);
6492 }
6493 if (p != NULL)
6494 {
6495 MSG(p);
6496 vim_free(p);
6497 }
6498 else
6499 MSG("default");
6500#else
6501 MSG(_("unknown"));
6502#endif
6503 }
6504 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarbe1e9e92012-09-18 16:47:07 +02006505 EMSG2(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006506}
6507
6508 static void
6509ex_highlight(eap)
6510 exarg_T *eap;
6511{
6512 if (*eap->arg == NUL && eap->cmd[2] == '!')
6513 MSG(_("Greetings, Vim user!"));
6514 do_highlight(eap->arg, eap->forceit, FALSE);
6515}
6516
6517
6518/*
6519 * Call this function if we thought we were going to exit, but we won't
6520 * (because of an error). May need to restore the terminal mode.
6521 */
6522 void
6523not_exiting()
6524{
6525 exiting = FALSE;
6526 settmode(TMODE_RAW);
6527}
6528
6529/*
6530 * ":quit": quit current window, quit Vim if closed the last window.
6531 */
6532 static void
6533ex_quit(eap)
6534 exarg_T *eap;
6535{
6536#ifdef FEAT_CMDWIN
6537 if (cmdwin_type != 0)
6538 {
6539 cmdwin_result = Ctrl_C;
6540 return;
6541 }
6542#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006543 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006544 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006545 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006546 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006547 return;
6548 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006549#ifdef FEAT_AUTOCMD
Bram Moolenaar3b53dfb2012-06-06 18:03:07 +02006550 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01006551 /* Refuse to quit when locked or when the buffer in the last window is
Bram Moolenaar362ce482012-06-06 19:02:45 +02006552 * being closed (can only happen in autocommands). */
6553 if (curbuf_locked() || (curbuf->b_nwindows == 1 && curbuf->b_closing))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006554 return;
6555#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006556
6557#ifdef FEAT_NETBEANS_INTG
6558 netbeansForcedQuit = eap->forceit;
6559#endif
6560
6561 /*
6562 * If there are more files or windows we won't exit.
6563 */
6564 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6565 exiting = TRUE;
6566 if ((!P_HID(curbuf)
6567 && check_changed(curbuf, p_awa, FALSE, eap->forceit, FALSE))
6568 || check_more(TRUE, eap->forceit) == FAIL
6569 || (only_one_window() && check_changed_any(eap->forceit)))
6570 {
6571 not_exiting();
6572 }
6573 else
6574 {
6575#ifdef FEAT_WINDOWS
6576 if (only_one_window()) /* quit last window */
6577#endif
6578 getout(0);
6579#ifdef FEAT_WINDOWS
6580# ifdef FEAT_GUI
6581 need_mouse_correct = TRUE;
6582# endif
6583 /* close window; may free buffer */
6584 win_close(curwin, !P_HID(curwin->w_buffer) || eap->forceit);
6585#endif
6586 }
6587}
6588
6589/*
6590 * ":cquit".
6591 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006592 static void
6593ex_cquit(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006594 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006595{
6596 getout(1); /* this does not always pass on the exit code to the Manx
6597 compiler. why? */
6598}
6599
6600/*
6601 * ":qall": try to quit all windows
6602 */
6603 static void
6604ex_quit_all(eap)
6605 exarg_T *eap;
6606{
6607# ifdef FEAT_CMDWIN
6608 if (cmdwin_type != 0)
6609 {
6610 if (eap->forceit)
6611 cmdwin_result = K_XF1; /* ex_window() takes care of this */
6612 else
6613 cmdwin_result = K_XF2;
6614 return;
6615 }
6616# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006617
6618 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006619 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006620 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006621 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006622 return;
6623 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006624#ifdef FEAT_AUTOCMD
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01006625 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
6626 /* Refuse to quit when locked or when the buffer in the last window is
6627 * being closed (can only happen in autocommands). */
6628 if (curbuf_locked() || (curbuf->b_nwindows == 1 && curbuf->b_closing))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006629 return;
6630#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006631
Bram Moolenaar071d4272004-06-13 20:20:40 +00006632 exiting = TRUE;
6633 if (eap->forceit || !check_changed_any(FALSE))
6634 getout(0);
6635 not_exiting();
6636}
6637
Bram Moolenaard9967712006-03-11 21:18:15 +00006638#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006639/*
6640 * ":close": close current window, unless it is the last one
6641 */
6642 static void
6643ex_close(eap)
6644 exarg_T *eap;
6645{
6646# ifdef FEAT_CMDWIN
6647 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02006648 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006649 else
6650# endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006651 if (!text_locked()
6652#ifdef FEAT_AUTOCMD
6653 && !curbuf_locked()
6654#endif
6655 )
Bram Moolenaarf740b292006-02-16 22:11:02 +00006656 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006657}
6658
Bram Moolenaard9967712006-03-11 21:18:15 +00006659# ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006660/*
6661 * ":pclose": Close any preview window.
6662 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006663 static void
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006664ex_pclose(eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006665 exarg_T *eap;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006666{
6667 win_T *win;
6668
6669 for (win = firstwin; win != NULL; win = win->w_next)
6670 if (win->w_p_pvw)
6671 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006672 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006673 break;
6674 }
6675}
Bram Moolenaard9967712006-03-11 21:18:15 +00006676# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006677
Bram Moolenaarf740b292006-02-16 22:11:02 +00006678/*
6679 * Close window "win" and take care of handling closing the last window for a
6680 * modified buffer.
6681 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006682 static void
Bram Moolenaarf740b292006-02-16 22:11:02 +00006683ex_win_close(forceit, win, tp)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006684 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006685 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006686 tabpage_T *tp; /* NULL or the tab page "win" is in */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006687{
6688 int need_hide;
6689 buf_T *buf = win->w_buffer;
6690
6691 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006692 if (need_hide && !P_HID(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006693 {
Bram Moolenaard9967712006-03-11 21:18:15 +00006694# if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006695 if ((p_confirm || cmdmod.confirm) && p_write)
6696 {
6697 dialog_changed(buf, FALSE);
6698 if (buf_valid(buf) && bufIsChanged(buf))
6699 return;
6700 need_hide = FALSE;
6701 }
6702 else
Bram Moolenaard9967712006-03-11 21:18:15 +00006703# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006704 {
6705 EMSG(_(e_nowrtmsg));
6706 return;
6707 }
6708 }
6709
Bram Moolenaard9967712006-03-11 21:18:15 +00006710# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006711 need_mouse_correct = TRUE;
Bram Moolenaard9967712006-03-11 21:18:15 +00006712# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006713
Bram Moolenaar071d4272004-06-13 20:20:40 +00006714 /* free buffer when not hiding it or when it's a scratch buffer */
Bram Moolenaarf740b292006-02-16 22:11:02 +00006715 if (tp == NULL)
6716 win_close(win, !need_hide && !P_HID(buf));
6717 else
6718 win_close_othertab(win, !need_hide && !P_HID(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006719}
6720
Bram Moolenaar071d4272004-06-13 20:20:40 +00006721/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006722 * ":tabclose": close current tab page, unless it is the last one.
6723 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006724 */
6725 static void
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006726ex_tabclose(eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006727 exarg_T *eap;
6728{
Bram Moolenaarf740b292006-02-16 22:11:02 +00006729 tabpage_T *tp;
6730
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006731# ifdef FEAT_CMDWIN
6732 if (cmdwin_type != 0)
6733 cmdwin_result = K_IGNORE;
6734 else
6735# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006736 if (first_tabpage->tp_next == NULL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00006737 EMSG(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00006738 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006739 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006740 if (eap->addr_count > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006741 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006742 tp = find_tabpage((int)eap->line2);
6743 if (tp == NULL)
6744 {
6745 beep_flush();
6746 return;
6747 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00006748 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006749 {
6750 tabpage_close_other(tp, eap->forceit);
6751 return;
6752 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006753 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006754 if (!text_locked()
6755#ifdef FEAT_AUTOCMD
6756 && !curbuf_locked()
6757#endif
6758 )
Bram Moolenaarf740b292006-02-16 22:11:02 +00006759 tabpage_close(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006760 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006761}
6762
6763/*
6764 * ":tabonly": close all tab pages except the current one
6765 */
6766 static void
6767ex_tabonly(eap)
6768 exarg_T *eap;
6769{
6770 tabpage_T *tp;
6771 int done;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006772
6773# ifdef FEAT_CMDWIN
6774 if (cmdwin_type != 0)
6775 cmdwin_result = K_IGNORE;
6776 else
6777# endif
6778 if (first_tabpage->tp_next == NULL)
6779 MSG(_("Already only one tab page"));
6780 else
6781 {
6782 /* Repeat this up to a 1000 times, because autocommands may mess
6783 * up the lists. */
6784 for (done = 0; done < 1000; ++done)
6785 {
6786 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6787 if (tp->tp_topframe != topframe)
6788 {
6789 tabpage_close_other(tp, eap->forceit);
6790 /* if we failed to close it quit */
6791 if (valid_tabpage(tp))
6792 done = 1000;
6793 /* start over, "tp" is now invalid */
6794 break;
6795 }
6796 if (first_tabpage->tp_next == NULL)
6797 break;
6798 }
6799 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006800}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006801
6802/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006803 * Close the current tab page.
6804 */
6805 void
6806tabpage_close(forceit)
6807 int forceit;
6808{
6809 /* First close all the windows but the current one. If that worked then
6810 * close the last window in this tab, that will close it. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006811 if (lastwin != firstwin)
6812 close_others(TRUE, forceit);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006813 if (lastwin == firstwin)
6814 ex_win_close(forceit, curwin, NULL);
6815# ifdef FEAT_GUI
6816 need_mouse_correct = TRUE;
6817# endif
6818}
6819
6820/*
6821 * Close tab page "tp", which is not the current tab page.
6822 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006823 * Also takes care of the tab pages line disappearing when closing the
6824 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00006825 */
6826 void
6827tabpage_close_other(tp, forceit)
6828 tabpage_T *tp;
6829 int forceit;
6830{
6831 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006832 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006833 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006834
6835 /* Limit to 1000 windows, autocommands may add a window while we close
6836 * one. OK, so I'm paranoid... */
6837 while (++done < 1000)
6838 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006839 wp = tp->tp_firstwin;
6840 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006841
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006842 /* Autocommands may delete the tab page under our fingers and we may
6843 * fail to close a window with a modified buffer. */
6844 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006845 break;
6846 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006847
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006848 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006849 if (h != tabline_height())
6850 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006851}
6852
6853/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006854 * ":only".
6855 */
6856 static void
6857ex_only(eap)
6858 exarg_T *eap;
6859{
6860# ifdef FEAT_GUI
6861 need_mouse_correct = TRUE;
6862# endif
6863 close_others(TRUE, eap->forceit);
6864}
6865
6866/*
6867 * ":all" and ":sall".
Bram Moolenaard9967712006-03-11 21:18:15 +00006868 * Also used for ":tab drop file ..." after setting the argument list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006869 */
Bram Moolenaard9967712006-03-11 21:18:15 +00006870 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00006871ex_all(eap)
6872 exarg_T *eap;
6873{
6874 if (eap->addr_count == 0)
6875 eap->line2 = 9999;
Bram Moolenaard9967712006-03-11 21:18:15 +00006876 do_arg_all((int)eap->line2, eap->forceit, eap->cmdidx == CMD_drop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006877}
6878#endif /* FEAT_WINDOWS */
6879
6880 static void
6881ex_hide(eap)
6882 exarg_T *eap;
6883{
6884 if (*eap->arg != NUL && check_nextcmd(eap->arg) == NULL)
6885 eap->errmsg = e_invarg;
6886 else
6887 {
6888 /* ":hide" or ":hide | cmd": hide current window */
6889 eap->nextcmd = check_nextcmd(eap->arg);
6890#ifdef FEAT_WINDOWS
6891 if (!eap->skip)
6892 {
6893# ifdef FEAT_GUI
6894 need_mouse_correct = TRUE;
6895# endif
6896 win_close(curwin, FALSE); /* don't free buffer */
6897 }
6898#endif
6899 }
6900}
6901
6902/*
6903 * ":stop" and ":suspend": Suspend Vim.
6904 */
6905 static void
6906ex_stop(eap)
6907 exarg_T *eap;
6908{
6909 /*
6910 * Disallow suspending for "rvim".
6911 */
6912 if (!check_restricted()
6913#ifdef WIN3264
6914 /*
6915 * Check if external commands are allowed now.
6916 */
6917 && can_end_termcap_mode(TRUE)
6918#endif
6919 )
6920 {
6921 if (!eap->forceit)
6922 autowrite_all();
6923 windgoto((int)Rows - 1, 0);
6924 out_char('\n');
6925 out_flush();
6926 stoptermcap();
6927 out_flush(); /* needed for SUN to restore xterm buffer */
6928#ifdef FEAT_TITLE
6929 mch_restore_title(3); /* restore window titles */
6930#endif
6931 ui_suspend(); /* call machine specific function */
6932#ifdef FEAT_TITLE
6933 maketitle();
6934 resettitle(); /* force updating the title */
6935#endif
6936 starttermcap();
6937 scroll_start(); /* scroll screen before redrawing */
6938 redraw_later_clear();
6939 shell_resized(); /* may have resized window */
6940 }
6941}
6942
6943/*
6944 * ":exit", ":xit" and ":wq": Write file and exit Vim.
6945 */
6946 static void
6947ex_exit(eap)
6948 exarg_T *eap;
6949{
6950#ifdef FEAT_CMDWIN
6951 if (cmdwin_type != 0)
6952 {
6953 cmdwin_result = Ctrl_C;
6954 return;
6955 }
6956#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006957 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006958 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006959 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006960 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006961 return;
6962 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006963#ifdef FEAT_AUTOCMD
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01006964 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
6965 /* Refuse to quit when locked or when the buffer in the last window is
6966 * being closed (can only happen in autocommands). */
6967 if (curbuf_locked() || (curbuf->b_nwindows == 1 && curbuf->b_closing))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006968 return;
6969#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006970
6971 /*
6972 * if more files or windows we won't exit
6973 */
6974 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6975 exiting = TRUE;
6976 if ( ((eap->cmdidx == CMD_wq
6977 || curbufIsChanged())
6978 && do_write(eap) == FAIL)
6979 || check_more(TRUE, eap->forceit) == FAIL
6980 || (only_one_window() && check_changed_any(eap->forceit)))
6981 {
6982 not_exiting();
6983 }
6984 else
6985 {
6986#ifdef FEAT_WINDOWS
6987 if (only_one_window()) /* quit last window, exit Vim */
6988#endif
6989 getout(0);
6990#ifdef FEAT_WINDOWS
6991# ifdef FEAT_GUI
6992 need_mouse_correct = TRUE;
6993# endif
6994 /* quit current window, may free buffer */
6995 win_close(curwin, !P_HID(curwin->w_buffer));
6996#endif
6997 }
6998}
6999
7000/*
7001 * ":print", ":list", ":number".
7002 */
7003 static void
7004ex_print(eap)
7005 exarg_T *eap;
7006{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007007 if (curbuf->b_ml.ml_flags & ML_EMPTY)
7008 EMSG(_(e_emptybuf));
7009 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007010 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007011 for ( ;!got_int; ui_breakcheck())
7012 {
7013 print_line(eap->line1,
7014 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
7015 || (eap->flags & EXFLAG_NR)),
7016 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
7017 if (++eap->line1 > eap->line2)
7018 break;
7019 out_flush(); /* show one line at a time */
7020 }
7021 setpcmark();
7022 /* put cursor at last line */
7023 curwin->w_cursor.lnum = eap->line2;
7024 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007025 }
7026
Bram Moolenaar071d4272004-06-13 20:20:40 +00007027 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007028}
7029
7030#ifdef FEAT_BYTEOFF
7031 static void
7032ex_goto(eap)
7033 exarg_T *eap;
7034{
7035 goto_byte(eap->line2);
7036}
7037#endif
7038
7039/*
7040 * ":shell".
7041 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007042 static void
7043ex_shell(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007044 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007045{
7046 do_shell(NULL, 0);
7047}
7048
7049#if (defined(FEAT_WINDOWS) && defined(HAVE_DROP_FILE)) \
7050 || (defined(FEAT_GUI_GTK) && defined(FEAT_DND)) \
Bram Moolenaar371baa92005-12-29 22:43:53 +00007051 || defined(FEAT_GUI_MSWIN) \
7052 || defined(FEAT_GUI_MAC) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00007053 || defined(PROTO)
7054
7055/*
7056 * Handle a file drop. The code is here because a drop is *nearly* like an
7057 * :args command, but not quite (we have a list of exact filenames, so we
7058 * don't want to (a) parse a command line, or (b) expand wildcards. So the
7059 * code is very similar to :args and hence needs access to a lot of the static
7060 * functions in this file.
7061 *
7062 * The list should be allocated using alloc(), as should each item in the
7063 * list. This function takes over responsibility for freeing the list.
7064 *
Bram Moolenaar81870892007-11-11 18:17:28 +00007065 * XXX The list is made into the argument list. This is freed using
Bram Moolenaar071d4272004-06-13 20:20:40 +00007066 * FreeWild(), which does a series of vim_free() calls, unless the two defines
7067 * __EMX__ and __ALWAYS_HAS_TRAILING_NUL_POINTER are set. In this case, a
7068 * routine _fnexplodefree() is used. This may cause problems, but as the drop
7069 * file functionality is (currently) not in EMX this is not presently a
7070 * problem.
7071 */
7072 void
7073handle_drop(filec, filev, split)
7074 int filec; /* the number of files dropped */
7075 char_u **filev; /* the list of files dropped */
7076 int split; /* force splitting the window */
7077{
7078 exarg_T ea;
7079 int save_msg_scroll = msg_scroll;
7080
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007081 /* Postpone this while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007082 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007083 return;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007084#ifdef FEAT_AUTOCMD
7085 if (curbuf_locked())
7086 return;
7087#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00007088 /* When the screen is being updated we should not change buffers and
7089 * windows structures, it may cause freed memory to be used. */
7090 if (updating_screen)
7091 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007092
7093 /* Check whether the current buffer is changed. If so, we will need
7094 * to split the current window or data could be lost.
7095 * We don't need to check if the 'hidden' option is set, as in this
7096 * case the buffer won't be lost.
7097 */
7098 if (!P_HID(curbuf) && !split)
7099 {
7100 ++emsg_off;
7101 split = check_changed(curbuf, TRUE, FALSE, FALSE, FALSE);
7102 --emsg_off;
7103 }
7104 if (split)
7105 {
7106# ifdef FEAT_WINDOWS
7107 if (win_split(0, 0) == FAIL)
7108 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007109 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007110
7111 /* When splitting the window, create a new alist. Otherwise the
7112 * existing one is overwritten. */
7113 alist_unlink(curwin->w_alist);
7114 alist_new();
7115# else
7116 return; /* can't split, always fail */
7117# endif
7118 }
7119
7120 /*
7121 * Set up the new argument list.
7122 */
Bram Moolenaar86b68352004-12-27 21:59:20 +00007123 alist_set(ALIST(curwin), filec, filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007124
7125 /*
7126 * Move to the first file.
7127 */
7128 /* Fake up a minimal "next" command for do_argfile() */
7129 vim_memset(&ea, 0, sizeof(ea));
7130 ea.cmd = (char_u *)"next";
7131 do_argfile(&ea, 0);
7132
7133 /* do_ecmd() may set need_start_insertmode, but since we never left Insert
7134 * mode that is not needed here. */
7135 need_start_insertmode = FALSE;
7136
7137 /* Restore msg_scroll, otherwise a following command may cause scrolling
7138 * unexpectedly. The screen will be redrawn by the caller, thus
7139 * msg_scroll being set by displaying a message is irrelevant. */
7140 msg_scroll = save_msg_scroll;
7141}
7142#endif
7143
Bram Moolenaar071d4272004-06-13 20:20:40 +00007144/*
7145 * Clear an argument list: free all file names and reset it to zero entries.
7146 */
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007147 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00007148alist_clear(al)
7149 alist_T *al;
7150{
7151 while (--al->al_ga.ga_len >= 0)
7152 vim_free(AARGLIST(al)[al->al_ga.ga_len].ae_fname);
7153 ga_clear(&al->al_ga);
7154}
7155
7156/*
7157 * Init an argument list.
7158 */
7159 void
7160alist_init(al)
7161 alist_T *al;
7162{
7163 ga_init2(&al->al_ga, (int)sizeof(aentry_T), 5);
7164}
7165
7166#if defined(FEAT_WINDOWS) || defined(PROTO)
7167
7168/*
7169 * Remove a reference from an argument list.
7170 * Ignored when the argument list is the global one.
7171 * If the argument list is no longer used by any window, free it.
7172 */
7173 void
7174alist_unlink(al)
7175 alist_T *al;
7176{
7177 if (al != &global_alist && --al->al_refcount <= 0)
7178 {
7179 alist_clear(al);
7180 vim_free(al);
7181 }
7182}
7183
7184# if defined(FEAT_LISTCMDS) || defined(HAVE_DROP_FILE) || defined(PROTO)
7185/*
7186 * Create a new argument list and use it for the current window.
7187 */
7188 void
7189alist_new()
7190{
7191 curwin->w_alist = (alist_T *)alloc((unsigned)sizeof(alist_T));
7192 if (curwin->w_alist == NULL)
7193 {
7194 curwin->w_alist = &global_alist;
7195 ++global_alist.al_refcount;
7196 }
7197 else
7198 {
7199 curwin->w_alist->al_refcount = 1;
7200 alist_init(curwin->w_alist);
7201 }
7202}
7203# endif
7204#endif
7205
7206#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE) || defined(PROTO)
7207/*
7208 * Expand the file names in the global argument list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00007209 * If "fnum_list" is not NULL, use "fnum_list[fnum_len]" as a list of buffer
7210 * numbers to be re-used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007211 */
7212 void
Bram Moolenaar86b68352004-12-27 21:59:20 +00007213alist_expand(fnum_list, fnum_len)
7214 int *fnum_list;
7215 int fnum_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007216{
7217 char_u **old_arg_files;
Bram Moolenaar86b68352004-12-27 21:59:20 +00007218 int old_arg_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007219 char_u **new_arg_files;
7220 int new_arg_file_count;
7221 char_u *save_p_su = p_su;
7222 int i;
7223
7224 /* Don't use 'suffixes' here. This should work like the shell did the
7225 * expansion. Also, the vimrc file isn't read yet, thus the user
7226 * can't set the options. */
7227 p_su = empty_option;
7228 old_arg_files = (char_u **)alloc((unsigned)(sizeof(char_u *) * GARGCOUNT));
7229 if (old_arg_files != NULL)
7230 {
7231 for (i = 0; i < GARGCOUNT; ++i)
Bram Moolenaar86b68352004-12-27 21:59:20 +00007232 old_arg_files[i] = vim_strsave(GARGLIST[i].ae_fname);
7233 old_arg_count = GARGCOUNT;
7234 if (expand_wildcards(old_arg_count, old_arg_files,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007235 &new_arg_file_count, &new_arg_files,
Bram Moolenaarb5609832011-07-20 15:04:58 +02007236 EW_FILE|EW_NOTFOUND|EW_ADDSLASH|EW_NOERROR) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007237 && new_arg_file_count > 0)
7238 {
Bram Moolenaar86b68352004-12-27 21:59:20 +00007239 alist_set(&global_alist, new_arg_file_count, new_arg_files,
7240 TRUE, fnum_list, fnum_len);
7241 FreeWild(old_arg_count, old_arg_files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007242 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007243 }
7244 p_su = save_p_su;
7245}
7246#endif
7247
7248/*
7249 * Set the argument list for the current window.
7250 * Takes over the allocated files[] and the allocated fnames in it.
7251 */
7252 void
Bram Moolenaar86b68352004-12-27 21:59:20 +00007253alist_set(al, count, files, use_curbuf, fnum_list, fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007254 alist_T *al;
7255 int count;
7256 char_u **files;
7257 int use_curbuf;
Bram Moolenaar86b68352004-12-27 21:59:20 +00007258 int *fnum_list;
7259 int fnum_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007260{
7261 int i;
7262
7263 alist_clear(al);
7264 if (ga_grow(&al->al_ga, count) == OK)
7265 {
7266 for (i = 0; i < count; ++i)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007267 {
7268 if (got_int)
7269 {
7270 /* When adding many buffers this can take a long time. Allow
7271 * interrupting here. */
7272 while (i < count)
7273 vim_free(files[i++]);
7274 break;
7275 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00007276
7277 /* May set buffer name of a buffer previously used for the
7278 * argument list, so that it's re-used by alist_add. */
7279 if (fnum_list != NULL && i < fnum_len)
7280 buf_set_name(fnum_list[i], files[i]);
7281
Bram Moolenaar071d4272004-06-13 20:20:40 +00007282 alist_add(al, files[i], use_curbuf ? 2 : 1);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007283 ui_breakcheck();
7284 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007285 vim_free(files);
7286 }
7287 else
7288 FreeWild(count, files);
7289#ifdef FEAT_WINDOWS
7290 if (al == &global_alist)
7291#endif
7292 arg_had_last = FALSE;
7293}
7294
7295/*
7296 * Add file "fname" to argument list "al".
7297 * "fname" must have been allocated and "al" must have been checked for room.
7298 */
7299 void
7300alist_add(al, fname, set_fnum)
7301 alist_T *al;
7302 char_u *fname;
7303 int set_fnum; /* 1: set buffer number; 2: re-use curbuf */
7304{
7305 if (fname == NULL) /* don't add NULL file names */
7306 return;
7307#ifdef BACKSLASH_IN_FILENAME
7308 slash_adjust(fname);
7309#endif
7310 AARGLIST(al)[al->al_ga.ga_len].ae_fname = fname;
7311 if (set_fnum > 0)
7312 AARGLIST(al)[al->al_ga.ga_len].ae_fnum =
7313 buflist_add(fname, BLN_LISTED | (set_fnum == 2 ? BLN_CURBUF : 0));
7314 ++al->al_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007315}
7316
7317#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
7318/*
7319 * Adjust slashes in file names. Called after 'shellslash' was set.
7320 */
7321 void
7322alist_slash_adjust()
7323{
7324 int i;
7325# ifdef FEAT_WINDOWS
7326 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007327 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007328# endif
7329
7330 for (i = 0; i < GARGCOUNT; ++i)
7331 if (GARGLIST[i].ae_fname != NULL)
7332 slash_adjust(GARGLIST[i].ae_fname);
7333# ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00007334 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007335 if (wp->w_alist != &global_alist)
7336 for (i = 0; i < WARGCOUNT(wp); ++i)
7337 if (WARGLIST(wp)[i].ae_fname != NULL)
7338 slash_adjust(WARGLIST(wp)[i].ae_fname);
7339# endif
7340}
7341#endif
7342
7343/*
7344 * ":preserve".
7345 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007346 static void
7347ex_preserve(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007348 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007349{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007350 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007351 ml_preserve(curbuf, TRUE);
7352}
7353
7354/*
7355 * ":recover".
7356 */
7357 static void
7358ex_recover(eap)
7359 exarg_T *eap;
7360{
7361 /* Set recoverymode right away to avoid the ATTENTION prompt. */
7362 recoverymode = TRUE;
7363 if (!check_changed(curbuf, p_awa, TRUE, eap->forceit, FALSE)
7364 && (*eap->arg == NUL
7365 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
7366 ml_recover();
7367 recoverymode = FALSE;
7368}
7369
7370/*
7371 * Command modifier used in a wrong way.
7372 */
7373 static void
7374ex_wrongmodifier(eap)
7375 exarg_T *eap;
7376{
7377 eap->errmsg = e_invcmd;
7378}
7379
7380#ifdef FEAT_WINDOWS
7381/*
7382 * :sview [+command] file split window with new file, read-only
7383 * :split [[+command] file] split window with current or new file
7384 * :vsplit [[+command] file] split window vertically with current or new file
7385 * :new [[+command] file] split window with no or new file
7386 * :vnew [[+command] file] split vertically window with no or new file
7387 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007388 *
7389 * :tabedit open new Tab page with empty window
7390 * :tabedit [+command] file open new Tab page and edit "file"
7391 * :tabnew [[+command] file] just like :tabedit
7392 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007393 */
7394 void
7395ex_splitview(eap)
7396 exarg_T *eap;
7397{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007398 win_T *old_curwin = curwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007399# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007400 char_u *fname = NULL;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007401# endif
7402# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007403 int browse_flag = cmdmod.browse;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007404# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007405
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007406# ifndef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007407 if (eap->cmdidx == CMD_vsplit || eap->cmdidx == CMD_vnew)
7408 {
7409 ex_ni(eap);
7410 return;
7411 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007412# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007413
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007414# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007415 need_mouse_correct = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007416# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007417
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007418# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00007419 /* A ":split" in the quickfix window works like ":new". Don't want two
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007420 * quickfix windows. But it's OK when doing ":tab split". */
7421 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007422 {
7423 if (eap->cmdidx == CMD_split)
7424 eap->cmdidx = CMD_new;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007425# ifdef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007426 if (eap->cmdidx == CMD_vsplit)
7427 eap->cmdidx = CMD_vnew;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007428# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007429 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007430# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007431
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007432# ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007433 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007434 {
7435 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
7436 FNAME_MESS, TRUE, curbuf->b_ffname);
7437 if (fname == NULL)
7438 goto theend;
7439 eap->arg = fname;
7440 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007441# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007442 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007443# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007444# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007445# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007446 if (cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007447# ifdef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007448 && eap->cmdidx != CMD_vnew
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007449# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007450 && eap->cmdidx != CMD_new)
7451 {
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007452# ifdef FEAT_AUTOCMD
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007453 if (
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007454# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007455 !gui.in_use &&
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007456# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007457 au_has_group((char_u *)"FileExplorer"))
7458 {
7459 /* No browsing supported but we do have the file explorer:
7460 * Edit the directory. */
7461 if (*eap->arg == NUL || !mch_isdir(eap->arg))
7462 eap->arg = (char_u *)".";
7463 }
7464 else
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007465# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007466 {
7467 fname = do_browse(0, (char_u *)_("Edit File in new window"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007468 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007469 if (fname == NULL)
7470 goto theend;
7471 eap->arg = fname;
7472 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007473 }
7474 cmdmod.browse = FALSE; /* Don't browse again in do_ecmd(). */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007475# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007476
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007477 /*
7478 * Either open new tab page or split the window.
7479 */
7480 if (eap->cmdidx == CMD_tabedit
7481 || eap->cmdidx == CMD_tabfind
7482 || eap->cmdidx == CMD_tabnew)
7483 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00007484 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
7485 : eap->addr_count == 0 ? 0
7486 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007487 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00007488 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007489
7490 /* set the alternate buffer for the window we came from */
7491 if (curwin != old_curwin
7492 && win_valid(old_curwin)
7493 && old_curwin->w_buffer != curbuf
7494 && !cmdmod.keepalt)
7495 old_curwin->w_alt_fnum = curbuf->b_fnum;
7496 }
7497 }
7498 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007499 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
7500 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007501# ifdef FEAT_SCROLLBIND
Bram Moolenaar071d4272004-06-13 20:20:40 +00007502 /* Reset 'scrollbind' when editing another file, but keep it when
7503 * doing ":split" without arguments. */
7504 if (*eap->arg != NUL
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007505# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007506 || cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007507# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007508 )
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007509 {
7510 RESET_BINDING(curwin);
7511 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007512 else
7513 do_check_scrollbind(FALSE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007514# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007515 do_exedit(eap, old_curwin);
7516 }
7517
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007518# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007519 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007520# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007521
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007522# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007523theend:
7524 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007525# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007526}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007527
7528/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007529 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007530 */
7531 void
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007532tabpage_new()
7533{
7534 exarg_T ea;
7535
7536 vim_memset(&ea, 0, sizeof(ea));
7537 ea.cmdidx = CMD_tabnew;
7538 ea.cmd = (char_u *)"tabn";
7539 ea.arg = (char_u *)"";
7540 ex_splitview(&ea);
7541}
7542
7543/*
7544 * :tabnext command
7545 */
7546 static void
7547ex_tabnext(eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007548 exarg_T *eap;
7549{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007550 switch (eap->cmdidx)
7551 {
7552 case CMD_tabfirst:
7553 case CMD_tabrewind:
7554 goto_tabpage(1);
7555 break;
7556 case CMD_tablast:
7557 goto_tabpage(9999);
7558 break;
7559 case CMD_tabprevious:
7560 case CMD_tabNext:
7561 goto_tabpage(eap->addr_count == 0 ? -1 : -(int)eap->line2);
7562 break;
7563 default: /* CMD_tabnext */
7564 goto_tabpage(eap->addr_count == 0 ? 0 : (int)eap->line2);
7565 break;
7566 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007567}
7568
7569/*
7570 * :tabmove command
7571 */
7572 static void
7573ex_tabmove(eap)
7574 exarg_T *eap;
7575{
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02007576 int tab_number = 9999;
7577
7578 if (eap->arg && *eap->arg != NUL)
7579 {
7580 char_u *p = eap->arg;
7581 int relative = 0; /* argument +N/-N means: move N places to the
7582 * right/left relative to the current position. */
7583
7584 if (*eap->arg == '-')
7585 {
7586 relative = -1;
7587 p = eap->arg + 1;
7588 }
7589 else if (*eap->arg == '+')
7590 {
7591 relative = 1;
7592 p = eap->arg + 1;
7593 }
7594 else
7595 p = eap->arg;
7596
7597 if (p == skipdigits(p))
7598 {
7599 /* No numbers as argument. */
7600 eap->errmsg = e_invarg;
7601 return;
7602 }
7603
7604 tab_number = getdigits(&p);
7605 if (relative != 0)
7606 tab_number = tab_number * relative + tabpage_index(curtab) - 1;;
7607 }
7608 else if (eap->addr_count != 0)
7609 tab_number = eap->line2;
7610
7611 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007612}
7613
7614/*
7615 * :tabs command: List tabs and their contents.
7616 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007617 static void
7618ex_tabs(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007619 exarg_T *eap UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007620{
7621 tabpage_T *tp;
7622 win_T *wp;
7623 int tabcount = 1;
7624
7625 msg_start();
7626 msg_scroll = TRUE;
7627 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
7628 {
7629 msg_putchar('\n');
7630 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
7631 msg_outtrans_attr(IObuff, hl_attr(HLF_T));
7632 out_flush(); /* output one line at a time */
7633 ui_breakcheck();
7634
Bram Moolenaar030f0df2006-02-21 22:02:53 +00007635 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007636 wp = firstwin;
7637 else
7638 wp = tp->tp_firstwin;
7639 for ( ; wp != NULL && !got_int; wp = wp->w_next)
7640 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007641 msg_putchar('\n');
7642 msg_putchar(wp == curwin ? '>' : ' ');
7643 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007644 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
7645 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007646 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02007647 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007648 else
7649 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
7650 IObuff, IOSIZE, TRUE);
7651 msg_outtrans(IObuff);
7652 out_flush(); /* output one line at a time */
7653 ui_breakcheck();
7654 }
7655 }
7656}
7657
7658#endif /* FEAT_WINDOWS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007659
7660/*
7661 * ":mode": Set screen mode.
7662 * If no argument given, just get the screen size and redraw.
7663 */
7664 static void
7665ex_mode(eap)
7666 exarg_T *eap;
7667{
7668 if (*eap->arg == NUL)
7669 shell_resized();
7670 else
7671 mch_screenmode(eap->arg);
7672}
7673
7674#ifdef FEAT_WINDOWS
7675/*
7676 * ":resize".
7677 * set, increment or decrement current window height
7678 */
7679 static void
7680ex_resize(eap)
7681 exarg_T *eap;
7682{
7683 int n;
7684 win_T *wp = curwin;
7685
7686 if (eap->addr_count > 0)
7687 {
7688 n = eap->line2;
7689 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
7690 ;
7691 }
7692
7693#ifdef FEAT_GUI
7694 need_mouse_correct = TRUE;
7695#endif
7696 n = atol((char *)eap->arg);
7697#ifdef FEAT_VERTSPLIT
7698 if (cmdmod.split & WSP_VERT)
7699 {
7700 if (*eap->arg == '-' || *eap->arg == '+')
7701 n += W_WIDTH(curwin);
7702 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
7703 n = 9999;
7704 win_setwidth_win((int)n, wp);
7705 }
7706 else
7707#endif
7708 {
7709 if (*eap->arg == '-' || *eap->arg == '+')
7710 n += curwin->w_height;
7711 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
7712 n = 9999;
7713 win_setheight_win((int)n, wp);
7714 }
7715}
7716#endif
7717
7718/*
7719 * ":find [+command] <file>" command.
7720 */
7721 static void
7722ex_find(eap)
7723 exarg_T *eap;
7724{
7725#ifdef FEAT_SEARCHPATH
7726 char_u *fname;
7727 int count;
7728
7729 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
7730 TRUE, curbuf->b_ffname);
7731 if (eap->addr_count > 0)
7732 {
7733 /* Repeat finding the file "count" times. This matters when it
7734 * appears several times in the path. */
7735 count = eap->line2;
7736 while (fname != NULL && --count > 0)
7737 {
7738 vim_free(fname);
7739 fname = find_file_in_path(NULL, 0, FNAME_MESS,
7740 FALSE, curbuf->b_ffname);
7741 }
7742 }
7743
7744 if (fname != NULL)
7745 {
7746 eap->arg = fname;
7747#endif
7748 do_exedit(eap, NULL);
7749#ifdef FEAT_SEARCHPATH
7750 vim_free(fname);
7751 }
7752#endif
7753}
7754
7755/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00007756 * ":open" simulation: for now just work like ":visual".
7757 */
7758 static void
7759ex_open(eap)
7760 exarg_T *eap;
7761{
7762 regmatch_T regmatch;
7763 char_u *p;
7764
7765 curwin->w_cursor.lnum = eap->line2;
7766 beginline(BL_SOL | BL_FIX);
7767 if (*eap->arg == '/')
7768 {
7769 /* ":open /pattern/": put cursor in column found with pattern */
7770 ++eap->arg;
7771 p = skip_regexp(eap->arg, '/', p_magic, NULL);
7772 *p = NUL;
7773 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
7774 if (regmatch.regprog != NULL)
7775 {
7776 regmatch.rm_ic = p_ic;
7777 p = ml_get_curline();
7778 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007779 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007780 else
7781 EMSG(_(e_nomatch));
Bram Moolenaar473de612013-06-08 18:19:48 +02007782 vim_regfree(regmatch.regprog);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007783 }
7784 /* Move to the NUL, ignore any other arguments. */
7785 eap->arg += STRLEN(eap->arg);
7786 }
7787 check_cursor();
7788
7789 eap->cmdidx = CMD_visual;
7790 do_exedit(eap, NULL);
7791}
7792
7793/*
7794 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007795 */
7796 static void
7797ex_edit(eap)
7798 exarg_T *eap;
7799{
7800 do_exedit(eap, NULL);
7801}
7802
7803/*
7804 * ":edit <file>" command and alikes.
7805 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007806 void
7807do_exedit(eap, old_curwin)
7808 exarg_T *eap;
7809 win_T *old_curwin; /* curwin before doing a split or NULL */
7810{
7811 int n;
7812#ifdef FEAT_WINDOWS
7813 int need_hide;
7814#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00007815 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007816
7817 /*
7818 * ":vi" command ends Ex mode.
7819 */
7820 if (exmode_active && (eap->cmdidx == CMD_visual
7821 || eap->cmdidx == CMD_view))
7822 {
7823 exmode_active = FALSE;
7824 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007825 {
7826 /* Special case: ":global/pat/visual\NLvi-commands" */
7827 if (global_busy)
7828 {
7829 int rd = RedrawingDisabled;
7830 int nwr = no_wait_return;
7831 int ms = msg_scroll;
7832#ifdef FEAT_GUI
7833 int he = hold_gui_events;
7834#endif
7835
7836 if (eap->nextcmd != NULL)
7837 {
7838 stuffReadbuff(eap->nextcmd);
7839 eap->nextcmd = NULL;
7840 }
7841
7842 if (exmode_was != EXMODE_VIM)
7843 settmode(TMODE_RAW);
7844 RedrawingDisabled = 0;
7845 no_wait_return = 0;
7846 need_wait_return = FALSE;
7847 msg_scroll = 0;
7848#ifdef FEAT_GUI
7849 hold_gui_events = 0;
7850#endif
7851 must_redraw = CLEAR;
7852
7853 main_loop(FALSE, TRUE);
7854
7855 RedrawingDisabled = rd;
7856 no_wait_return = nwr;
7857 msg_scroll = ms;
7858#ifdef FEAT_GUI
7859 hold_gui_events = he;
7860#endif
7861 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007862 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007863 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007864 }
7865
7866 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007867 || eap->cmdidx == CMD_tabnew
7868 || eap->cmdidx == CMD_tabedit
Bram Moolenaar071d4272004-06-13 20:20:40 +00007869#ifdef FEAT_VERTSPLIT
7870 || eap->cmdidx == CMD_vnew
7871#endif
7872 ) && *eap->arg == NUL)
7873 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007874 /* ":new" or ":tabnew" without argument: edit an new empty buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007875 setpcmark();
7876 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007877 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
7878 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007879 }
7880 else if ((eap->cmdidx != CMD_split
7881#ifdef FEAT_VERTSPLIT
7882 && eap->cmdidx != CMD_vsplit
7883#endif
7884 )
7885 || *eap->arg != NUL
7886#ifdef FEAT_BROWSE
7887 || cmdmod.browse
7888#endif
7889 )
7890 {
Bram Moolenaar5555acc2006-04-07 21:33:12 +00007891#ifdef FEAT_AUTOCMD
7892 /* Can't edit another file when "curbuf_lock" is set. Only ":edit"
7893 * can bring us here, others are stopped earlier. */
7894 if (*eap->arg != NUL && curbuf_locked())
7895 return;
7896#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007897 n = readonlymode;
7898 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
7899 readonlymode = TRUE;
7900 else if (eap->cmdidx == CMD_enew)
7901 readonlymode = FALSE; /* 'readonly' doesn't make sense in an
7902 empty buffer */
7903 setpcmark();
7904 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
7905 NULL, eap,
7906 /* ":edit" goes to first line if Vi compatible */
7907 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
7908 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
7909 ? ECMD_ONE : eap->do_ecmd_lnum,
7910 (P_HID(curbuf) ? ECMD_HIDE : 0)
7911 + (eap->forceit ? ECMD_FORCEIT : 0)
7912#ifdef FEAT_LISTCMDS
7913 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
7914#endif
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007915 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007916 {
7917 /* Editing the file failed. If the window was split, close it. */
7918#ifdef FEAT_WINDOWS
7919 if (old_curwin != NULL)
7920 {
7921 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
7922 if (!need_hide || P_HID(curbuf))
7923 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007924# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7925 cleanup_T cs;
7926
7927 /* Reset the error/interrupt/exception state here so that
7928 * aborting() returns FALSE when closing a window. */
7929 enter_cleanup(&cs);
7930# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007931# ifdef FEAT_GUI
7932 need_mouse_correct = TRUE;
7933# endif
7934 win_close(curwin, !need_hide && !P_HID(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007935
7936# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7937 /* Restore the error/interrupt/exception state if not
7938 * discarded by a new aborting error, interrupt, or
7939 * uncaught exception. */
7940 leave_cleanup(&cs);
7941# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007942 }
7943 }
7944#endif
7945 }
7946 else if (readonlymode && curbuf->b_nwindows == 1)
7947 {
7948 /* When editing an already visited buffer, 'readonly' won't be set
7949 * but the previous value is kept. With ":view" and ":sview" we
7950 * want the file to be readonly, except when another window is
7951 * editing the same buffer. */
7952 curbuf->b_p_ro = TRUE;
7953 }
7954 readonlymode = n;
7955 }
7956 else
7957 {
7958 if (eap->do_ecmd_cmd != NULL)
7959 do_cmdline_cmd(eap->do_ecmd_cmd);
7960#ifdef FEAT_TITLE
7961 n = curwin->w_arg_idx_invalid;
7962#endif
7963 check_arg_idx(curwin);
7964#ifdef FEAT_TITLE
7965 if (n != curwin->w_arg_idx_invalid)
7966 maketitle();
7967#endif
7968 }
7969
7970#ifdef FEAT_WINDOWS
7971 /*
7972 * if ":split file" worked, set alternate file name in old window to new
7973 * file
7974 */
7975 if (old_curwin != NULL
7976 && *eap->arg != NUL
7977 && curwin != old_curwin
7978 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00007979 && old_curwin->w_buffer != curbuf
7980 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007981 old_curwin->w_alt_fnum = curbuf->b_fnum;
7982#endif
7983
7984 ex_no_reprint = TRUE;
7985}
7986
7987#ifndef FEAT_GUI
7988/*
7989 * ":gui" and ":gvim" when there is no GUI.
7990 */
7991 static void
7992ex_nogui(eap)
7993 exarg_T *eap;
7994{
7995 eap->errmsg = e_nogvim;
7996}
7997#endif
7998
7999#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
8000 static void
8001ex_tearoff(eap)
8002 exarg_T *eap;
8003{
8004 gui_make_tearoff(eap->arg);
8005}
8006#endif
8007
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008008#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008009 static void
8010ex_popup(eap)
8011 exarg_T *eap;
8012{
Bram Moolenaar97409f12005-07-08 22:17:29 +00008013 gui_make_popup(eap->arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008014}
8015#endif
8016
Bram Moolenaar071d4272004-06-13 20:20:40 +00008017 static void
8018ex_swapname(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008019 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008020{
8021 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
8022 MSG(_("No swap file"));
8023 else
8024 msg(curbuf->b_ml.ml_mfp->mf_fname);
8025}
8026
8027/*
8028 * ":syncbind" forces all 'scrollbind' windows to have the same relative
8029 * offset.
8030 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
8031 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008032 static void
8033ex_syncbind(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008034 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008035{
8036#ifdef FEAT_SCROLLBIND
8037 win_T *wp;
8038 long topline;
8039 long y;
8040 linenr_T old_linenr = curwin->w_cursor.lnum;
8041
8042 setpcmark();
8043
8044 /*
8045 * determine max topline
8046 */
8047 if (curwin->w_p_scb)
8048 {
8049 topline = curwin->w_topline;
8050 for (wp = firstwin; wp; wp = wp->w_next)
8051 {
8052 if (wp->w_p_scb && wp->w_buffer)
8053 {
8054 y = wp->w_buffer->b_ml.ml_line_count - p_so;
8055 if (topline > y)
8056 topline = y;
8057 }
8058 }
8059 if (topline < 1)
8060 topline = 1;
8061 }
8062 else
8063 {
8064 topline = 1;
8065 }
8066
8067
8068 /*
8069 * set all scrollbind windows to the same topline
8070 */
8071 wp = curwin;
8072 for (curwin = firstwin; curwin; curwin = curwin->w_next)
8073 {
8074 if (curwin->w_p_scb)
8075 {
8076 y = topline - curwin->w_topline;
8077 if (y > 0)
8078 scrollup(y, TRUE);
8079 else
8080 scrolldown(-y, TRUE);
8081 curwin->w_scbind_pos = topline;
8082 redraw_later(VALID);
8083 cursor_correct();
8084#ifdef FEAT_WINDOWS
8085 curwin->w_redr_status = TRUE;
8086#endif
8087 }
8088 }
8089 curwin = wp;
8090 if (curwin->w_p_scb)
8091 {
8092 did_syncbind = TRUE;
8093 checkpcmark();
8094 if (old_linenr != curwin->w_cursor.lnum)
8095 {
8096 char_u ctrl_o[2];
8097
8098 ctrl_o[0] = Ctrl_O;
8099 ctrl_o[1] = 0;
8100 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
8101 }
8102 }
8103#endif
8104}
8105
8106
8107 static void
8108ex_read(eap)
8109 exarg_T *eap;
8110{
Bram Moolenaardf177f62005-02-22 08:39:57 +00008111 int i;
8112 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
8113 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008114
8115 if (eap->usefilter) /* :r!cmd */
8116 do_bang(1, eap, FALSE, FALSE, TRUE);
8117 else
8118 {
8119 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
8120 return;
8121
8122#ifdef FEAT_BROWSE
8123 if (cmdmod.browse)
8124 {
8125 char_u *browseFile;
8126
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008127 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008128 NULL, NULL, NULL, curbuf);
8129 if (browseFile != NULL)
8130 {
8131 i = readfile(browseFile, NULL,
8132 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8133 vim_free(browseFile);
8134 }
8135 else
8136 i = OK;
8137 }
8138 else
8139#endif
8140 if (*eap->arg == NUL)
8141 {
8142 if (check_fname() == FAIL) /* check for no file name */
8143 return;
8144 i = readfile(curbuf->b_ffname, curbuf->b_fname,
8145 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8146 }
8147 else
8148 {
8149 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
8150 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
8151 i = readfile(eap->arg, NULL,
8152 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8153
8154 }
8155 if (i == FAIL)
8156 {
8157#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
8158 if (!aborting())
8159#endif
8160 EMSG2(_(e_notopen), eap->arg);
8161 }
8162 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008163 {
8164 if (empty && exmode_active)
8165 {
8166 /* Delete the empty line that remains. Historically ex does
8167 * this but vi doesn't. */
8168 if (eap->line2 == 0)
8169 lnum = curbuf->b_ml.ml_line_count;
8170 else
8171 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008172 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008173 {
8174 ml_delete(lnum, FALSE);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008175 if (curwin->w_cursor.lnum > 1
8176 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008177 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00008178 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008179 }
8180 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008181 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008182 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008183 }
8184}
8185
Bram Moolenaarea408852005-06-25 22:49:46 +00008186static char_u *prev_dir = NULL;
8187
8188#if defined(EXITFREE) || defined(PROTO)
8189 void
8190free_cd_dir()
8191{
8192 vim_free(prev_dir);
Bram Moolenaara0174af2008-01-02 20:08:25 +00008193 prev_dir = NULL;
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00008194
8195 vim_free(globaldir);
8196 globaldir = NULL;
Bram Moolenaarea408852005-06-25 22:49:46 +00008197}
8198#endif
8199
Bram Moolenaarf4258302013-06-02 18:20:17 +02008200/*
8201 * Deal with the side effects of changing the current directory.
8202 * When "local" is TRUE then this was after an ":lcd" command.
8203 */
8204 void
8205post_chdir(local)
8206 int local;
8207{
8208 vim_free(curwin->w_localdir);
8209 if (local)
8210 {
8211 /* If still in global directory, need to remember current
8212 * directory as global directory. */
8213 if (globaldir == NULL && prev_dir != NULL)
8214 globaldir = vim_strsave(prev_dir);
8215 /* Remember this local directory for the window. */
8216 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8217 curwin->w_localdir = vim_strsave(NameBuff);
8218 }
8219 else
8220 {
8221 /* We are now in the global directory, no need to remember its
8222 * name. */
8223 vim_free(globaldir);
8224 globaldir = NULL;
8225 curwin->w_localdir = NULL;
8226 }
8227
8228 shorten_fnames(TRUE);
8229}
8230
Bram Moolenaarea408852005-06-25 22:49:46 +00008231
Bram Moolenaar071d4272004-06-13 20:20:40 +00008232/*
8233 * ":cd", ":lcd", ":chdir" and ":lchdir".
8234 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00008235 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00008236ex_cd(eap)
8237 exarg_T *eap;
8238{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008239 char_u *new_dir;
8240 char_u *tofree;
8241
8242 new_dir = eap->arg;
8243#if !defined(UNIX) && !defined(VMS)
8244 /* for non-UNIX ":cd" means: print current directory */
8245 if (*new_dir == NUL)
8246 ex_pwd(NULL);
8247 else
8248#endif
8249 {
Bram Moolenaar8e8fe9b2009-03-11 14:37:32 +00008250#ifdef FEAT_AUTOCMD
8251 if (allbuf_locked())
8252 return;
8253#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00008254 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged()
8255 && !eap->forceit)
8256 {
Bram Moolenaar81870892007-11-11 18:17:28 +00008257 EMSG(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00008258 return;
8259 }
8260
Bram Moolenaar071d4272004-06-13 20:20:40 +00008261 /* ":cd -": Change to previous directory */
8262 if (STRCMP(new_dir, "-") == 0)
8263 {
8264 if (prev_dir == NULL)
8265 {
8266 EMSG(_("E186: No previous directory"));
8267 return;
8268 }
8269 new_dir = prev_dir;
8270 }
8271
8272 /* Save current directory for next ":cd -" */
8273 tofree = prev_dir;
8274 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8275 prev_dir = vim_strsave(NameBuff);
8276 else
8277 prev_dir = NULL;
8278
8279#if defined(UNIX) || defined(VMS)
8280 /* for UNIX ":cd" means: go to home directory */
8281 if (*new_dir == NUL)
8282 {
8283 /* use NameBuff for home directory name */
8284# ifdef VMS
8285 char_u *p;
8286
8287 p = mch_getenv((char_u *)"SYS$LOGIN");
8288 if (p == NULL || *p == NUL) /* empty is the same as not set */
8289 NameBuff[0] = NUL;
8290 else
Bram Moolenaarb6356332005-07-18 21:40:44 +00008291 vim_strncpy(NameBuff, p, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008292# else
8293 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
8294# endif
8295 new_dir = NameBuff;
8296 }
8297#endif
8298 if (new_dir == NULL || vim_chdir(new_dir))
8299 EMSG(_(e_failed));
8300 else
8301 {
Bram Moolenaarf4258302013-06-02 18:20:17 +02008302 post_chdir(eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008303
8304 /* Echo the new current directory if the command was typed. */
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00008305 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008306 ex_pwd(eap);
8307 }
8308 vim_free(tofree);
8309 }
8310}
8311
8312/*
8313 * ":pwd".
8314 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008315 static void
8316ex_pwd(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008317 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008318{
8319 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8320 {
8321#ifdef BACKSLASH_IN_FILENAME
8322 slash_adjust(NameBuff);
8323#endif
8324 msg(NameBuff);
8325 }
8326 else
8327 EMSG(_("E187: Unknown"));
8328}
8329
8330/*
8331 * ":=".
8332 */
8333 static void
8334ex_equal(eap)
8335 exarg_T *eap;
8336{
8337 smsg((char_u *)"%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008338 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008339}
8340
8341 static void
8342ex_sleep(eap)
8343 exarg_T *eap;
8344{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008345 int n;
8346 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008347
8348 if (cursor_valid())
8349 {
8350 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
8351 if (n >= 0)
8352 windgoto((int)n, curwin->w_wcol);
8353 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008354
8355 len = eap->line2;
8356 switch (*eap->arg)
8357 {
8358 case 'm': break;
8359 case NUL: len *= 1000L; break;
8360 default: EMSG2(_(e_invarg2), eap->arg); return;
8361 }
8362 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008363}
8364
8365/*
8366 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
8367 */
8368 void
8369do_sleep(msec)
8370 long msec;
8371{
8372 long done;
8373
8374 cursor_on();
8375 out_flush();
8376 for (done = 0; !got_int && done < msec; done += 1000L)
8377 {
8378 ui_delay(msec - done > 1000L ? 1000L : msec - done, TRUE);
8379 ui_breakcheck();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02008380#ifdef FEAT_NETBEANS_INTG
8381 /* Process the netbeans messages that may have been received in the
8382 * call to ui_breakcheck() when the GUI is in use. This may occur when
8383 * running a test case. */
8384 netbeans_parse_messages();
8385#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008386 }
8387}
8388
8389 static void
8390do_exmap(eap, isabbrev)
8391 exarg_T *eap;
8392 int isabbrev;
8393{
8394 int mode;
8395 char_u *cmdp;
8396
8397 cmdp = eap->cmd;
8398 mode = get_map_mode(&cmdp, eap->forceit || isabbrev);
8399
8400 switch (do_map((*cmdp == 'n') ? 2 : (*cmdp == 'u'),
8401 eap->arg, mode, isabbrev))
8402 {
8403 case 1: EMSG(_(e_invarg));
8404 break;
8405 case 2: EMSG(isabbrev ? _(e_noabbr) : _(e_nomap));
8406 break;
8407 }
8408}
8409
8410/*
8411 * ":winsize" command (obsolete).
8412 */
8413 static void
8414ex_winsize(eap)
8415 exarg_T *eap;
8416{
8417 int w, h;
8418 char_u *arg = eap->arg;
8419 char_u *p;
8420
8421 w = getdigits(&arg);
8422 arg = skipwhite(arg);
8423 p = arg;
8424 h = getdigits(&arg);
8425 if (*p != NUL && *arg == NUL)
8426 set_shellsize(w, h, TRUE);
8427 else
8428 EMSG(_("E465: :winsize requires two number arguments"));
8429}
8430
8431#ifdef FEAT_WINDOWS
8432 static void
8433ex_wincmd(eap)
8434 exarg_T *eap;
8435{
8436 int xchar = NUL;
8437 char_u *p;
8438
8439 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
8440 {
8441 /* CTRL-W g and CTRL-W CTRL-G have an extra command character */
8442 if (eap->arg[1] == NUL)
8443 {
8444 EMSG(_(e_invarg));
8445 return;
8446 }
8447 xchar = eap->arg[1];
8448 p = eap->arg + 2;
8449 }
8450 else
8451 p = eap->arg + 1;
8452
8453 eap->nextcmd = check_nextcmd(p);
8454 p = skipwhite(p);
8455 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
8456 EMSG(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02008457 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008458 {
8459 /* Pass flags on for ":vertical wincmd ]". */
8460 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008461 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008462 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
8463 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008464 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008465 }
8466}
8467#endif
8468
Bram Moolenaar843ee412004-06-30 16:16:41 +00008469#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008470/*
8471 * ":winpos".
8472 */
8473 static void
8474ex_winpos(eap)
8475 exarg_T *eap;
8476{
8477 int x, y;
8478 char_u *arg = eap->arg;
8479 char_u *p;
8480
8481 if (*arg == NUL)
8482 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00008483# if defined(FEAT_GUI) || defined(MSWIN)
8484# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008485 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00008486# else
8487 if (mch_get_winpos(&x, &y) != FAIL)
8488# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008489 {
8490 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
8491 msg(IObuff);
8492 }
8493 else
8494# endif
8495 EMSG(_("E188: Obtaining window position not implemented for this platform"));
8496 }
8497 else
8498 {
8499 x = getdigits(&arg);
8500 arg = skipwhite(arg);
8501 p = arg;
8502 y = getdigits(&arg);
8503 if (*p == NUL || *arg != NUL)
8504 {
8505 EMSG(_("E466: :winpos requires two number arguments"));
8506 return;
8507 }
8508# ifdef FEAT_GUI
8509 if (gui.in_use)
8510 gui_mch_set_winpos(x, y);
8511 else if (gui.starting)
8512 {
8513 /* Remember the coordinates for when the window is opened. */
8514 gui_win_x = x;
8515 gui_win_y = y;
8516 }
8517# ifdef HAVE_TGETENT
8518 else
8519# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00008520# else
8521# ifdef MSWIN
8522 mch_set_winpos(x, y);
8523# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008524# endif
8525# ifdef HAVE_TGETENT
8526 if (*T_CWP)
8527 term_set_winpos(x, y);
8528# endif
8529 }
8530}
8531#endif
8532
8533/*
8534 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
8535 */
8536 static void
8537ex_operators(eap)
8538 exarg_T *eap;
8539{
8540 oparg_T oa;
8541
8542 clear_oparg(&oa);
8543 oa.regname = eap->regname;
8544 oa.start.lnum = eap->line1;
8545 oa.end.lnum = eap->line2;
8546 oa.line_count = eap->line2 - eap->line1 + 1;
8547 oa.motion_type = MLINE;
8548#ifdef FEAT_VIRTUALEDIT
8549 virtual_op = FALSE;
8550#endif
8551 if (eap->cmdidx != CMD_yank) /* position cursor for undo */
8552 {
8553 setpcmark();
8554 curwin->w_cursor.lnum = eap->line1;
8555 beginline(BL_SOL | BL_FIX);
8556 }
8557
8558 switch (eap->cmdidx)
8559 {
8560 case CMD_delete:
8561 oa.op_type = OP_DELETE;
8562 op_delete(&oa);
8563 break;
8564
8565 case CMD_yank:
8566 oa.op_type = OP_YANK;
8567 (void)op_yank(&oa, FALSE, TRUE);
8568 break;
8569
8570 default: /* CMD_rshift or CMD_lshift */
8571 if ((eap->cmdidx == CMD_rshift)
8572#ifdef FEAT_RIGHTLEFT
8573 ^ curwin->w_p_rl
8574#endif
8575 )
8576 oa.op_type = OP_RSHIFT;
8577 else
8578 oa.op_type = OP_LSHIFT;
8579 op_shift(&oa, FALSE, eap->amount);
8580 break;
8581 }
8582#ifdef FEAT_VIRTUALEDIT
8583 virtual_op = MAYBE;
8584#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00008585 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008586}
8587
8588/*
8589 * ":put".
8590 */
8591 static void
8592ex_put(eap)
8593 exarg_T *eap;
8594{
8595 /* ":0put" works like ":1put!". */
8596 if (eap->line2 == 0)
8597 {
8598 eap->line2 = 1;
8599 eap->forceit = TRUE;
8600 }
8601 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008602 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
8603 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008604}
8605
8606/*
8607 * Handle ":copy" and ":move".
8608 */
8609 static void
8610ex_copymove(eap)
8611 exarg_T *eap;
8612{
8613 long n;
8614
8615 n = get_address(&eap->arg, FALSE, FALSE);
8616 if (eap->arg == NULL) /* error detected */
8617 {
8618 eap->nextcmd = NULL;
8619 return;
8620 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008621 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008622
8623 /*
8624 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
8625 */
8626 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
8627 {
8628 EMSG(_(e_invaddr));
8629 return;
8630 }
8631
8632 if (eap->cmdidx == CMD_move)
8633 {
8634 if (do_move(eap->line1, eap->line2, n) == FAIL)
8635 return;
8636 }
8637 else
8638 ex_copy(eap->line1, eap->line2, n);
8639 u_clearline();
8640 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008641 ex_may_print(eap);
8642}
8643
8644/*
8645 * Print the current line if flags were given to the Ex command.
8646 */
8647 static void
8648ex_may_print(eap)
8649 exarg_T *eap;
8650{
8651 if (eap->flags != 0)
8652 {
8653 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
8654 (eap->flags & EXFLAG_LIST));
8655 ex_no_reprint = TRUE;
8656 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008657}
8658
8659/*
8660 * ":smagic" and ":snomagic".
8661 */
8662 static void
8663ex_submagic(eap)
8664 exarg_T *eap;
8665{
8666 int magic_save = p_magic;
8667
8668 p_magic = (eap->cmdidx == CMD_smagic);
8669 do_sub(eap);
8670 p_magic = magic_save;
8671}
8672
8673/*
8674 * ":join".
8675 */
8676 static void
8677ex_join(eap)
8678 exarg_T *eap;
8679{
8680 curwin->w_cursor.lnum = eap->line1;
8681 if (eap->line1 == eap->line2)
8682 {
8683 if (eap->addr_count >= 2) /* :2,2join does nothing */
8684 return;
8685 if (eap->line2 == curbuf->b_ml.ml_line_count)
8686 {
8687 beep_flush();
8688 return;
8689 }
8690 ++eap->line2;
8691 }
Bram Moolenaar81340392012-06-06 16:12:59 +02008692 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008693 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008694 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008695}
8696
8697/*
8698 * ":[addr]@r" or ":[addr]*r": execute register
8699 */
8700 static void
8701ex_at(eap)
8702 exarg_T *eap;
8703{
8704 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00008705 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008706
8707 curwin->w_cursor.lnum = eap->line2;
8708
8709#ifdef USE_ON_FLY_SCROLL
8710 dont_scroll = TRUE; /* disallow scrolling here */
8711#endif
8712
8713 /* get the register name. No name means to use the previous one */
8714 c = *eap->arg;
8715 if (c == NUL || (c == '*' && *eap->cmd == '*'))
8716 c = '@';
Bram Moolenaard333d1e2006-11-07 17:43:47 +00008717 /* Put the register in the typeahead buffer with the "silent" flag. */
8718 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
8719 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008720 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008721 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00008722 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008723 else
8724 {
8725 int save_efr = exec_from_reg;
8726
8727 exec_from_reg = TRUE;
8728
8729 /*
8730 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00008731 * Continue until the stuff buffer is empty and all added characters
8732 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008733 */
Bram Moolenaar60462872009-11-03 11:40:19 +00008734 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008735 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
8736
8737 exec_from_reg = save_efr;
8738 }
8739}
8740
8741/*
8742 * ":!".
8743 */
8744 static void
8745ex_bang(eap)
8746 exarg_T *eap;
8747{
8748 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
8749}
8750
8751/*
8752 * ":undo".
8753 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008754 static void
8755ex_undo(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008756 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008757{
Bram Moolenaard3667a22006-03-16 21:35:52 +00008758 if (eap->addr_count == 1) /* :undo 123 */
Bram Moolenaar730cde92010-06-27 05:18:54 +02008759 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00008760 else
8761 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008762}
8763
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008764#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008765 static void
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008766ex_wundo(eap)
8767 exarg_T *eap;
8768{
8769 char_u hash[UNDO_HASH_SIZE];
8770
8771 u_compute_hash(hash);
8772 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
8773}
8774
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008775 static void
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008776ex_rundo(eap)
8777 exarg_T *eap;
8778{
8779 char_u hash[UNDO_HASH_SIZE];
8780
8781 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02008782 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008783}
8784#endif
8785
Bram Moolenaar071d4272004-06-13 20:20:40 +00008786/*
8787 * ":redo".
8788 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008789 static void
8790ex_redo(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008791 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008792{
8793 u_redo(1);
8794}
8795
8796/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008797 * ":earlier" and ":later".
8798 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008799 static void
8800ex_later(eap)
8801 exarg_T *eap;
8802{
8803 long count = 0;
8804 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008805 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008806 char_u *p = eap->arg;
8807
8808 if (*p == NUL)
8809 count = 1;
8810 else if (isdigit(*p))
8811 {
8812 count = getdigits(&p);
8813 switch (*p)
8814 {
8815 case 's': ++p; sec = TRUE; break;
8816 case 'm': ++p; sec = TRUE; count *= 60; break;
8817 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008818 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
8819 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008820 }
8821 }
8822
8823 if (*p != NUL)
8824 EMSG2(_(e_invarg2), eap->arg);
8825 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02008826 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
8827 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008828}
8829
8830/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008831 * ":redir": start/stop redirection.
8832 */
8833 static void
8834ex_redir(eap)
8835 exarg_T *eap;
8836{
8837 char *mode;
8838 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00008839 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008840
8841 if (STRICMP(eap->arg, "END") == 0)
8842 close_redir();
8843 else
8844 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008845 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008846 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008847 ++arg;
8848 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008849 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008850 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008851 mode = "a";
8852 }
8853 else
8854 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00008855 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008856
8857 close_redir();
8858
8859 /* Expand environment variables and "~/". */
Bram Moolenaarca472992005-01-21 11:46:23 +00008860 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008861 if (fname == NULL)
8862 return;
8863#ifdef FEAT_BROWSE
8864 if (cmdmod.browse)
8865 {
8866 char_u *browseFile;
8867
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008868 browseFile = do_browse(BROWSE_SAVE,
8869 (char_u *)_("Save Redirection"),
8870 fname, NULL, NULL, BROWSE_FILTER_ALL_FILES, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008871 if (browseFile == NULL)
8872 return; /* operation cancelled */
8873 vim_free(fname);
8874 fname = browseFile;
8875 eap->forceit = TRUE; /* since dialog already asked */
8876 }
8877#endif
8878
8879 redir_fd = open_exfile(fname, eap->forceit, mode);
8880 vim_free(fname);
8881 }
8882#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00008883 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008884 {
8885 /* redirect to a register a-z (resp. A-Z for appending) */
8886 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00008887 ++arg;
8888 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008889# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00008890 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00008891 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008892# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00008893 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008894 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008895 redir_reg = *arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008896 if (*arg == '>' && arg[1] == '>') /* append */
Bram Moolenaard9d30582005-05-18 22:10:28 +00008897 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008898 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008899 {
Bram Moolenaarc188b882007-10-19 14:20:54 +00008900 /* Can use both "@a" and "@a>". */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00008901 if (*arg == '>')
8902 arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008903 /* Make register empty when not using @A-@Z and the
8904 * command is valid. */
8905 if (*arg == NUL && !isupper(redir_reg))
8906 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008907 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008908 }
8909 if (*arg != NUL)
8910 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00008911 redir_reg = 0;
Bram Moolenaard9d30582005-05-18 22:10:28 +00008912 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008913 }
8914 }
8915 else if (*arg == '=' && arg[1] == '>')
8916 {
8917 int append;
8918
8919 /* redirect to a variable */
8920 close_redir();
8921 arg += 2;
8922
8923 if (*arg == '>')
8924 {
8925 ++arg;
8926 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008927 }
8928 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008929 append = FALSE;
8930
8931 if (var_redir_start(skipwhite(arg), append) == OK)
8932 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008933 }
8934#endif
8935
8936 /* TODO: redirect to a buffer */
8937
Bram Moolenaar071d4272004-06-13 20:20:40 +00008938 else
Bram Moolenaarca472992005-01-21 11:46:23 +00008939 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008940 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00008941
8942 /* Make sure redirection is not off. Can happen for cmdline completion
8943 * that indirectly invokes a command to catch its output. */
8944 if (redir_fd != NULL
8945#ifdef FEAT_EVAL
8946 || redir_reg || redir_vname
8947#endif
8948 )
8949 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008950}
8951
8952/*
8953 * ":redraw": force redraw
8954 */
8955 static void
8956ex_redraw(eap)
8957 exarg_T *eap;
8958{
8959 int r = RedrawingDisabled;
8960 int p = p_lz;
8961
8962 RedrawingDisabled = 0;
8963 p_lz = FALSE;
8964 update_topline();
8965 update_screen(eap->forceit ? CLEAR :
8966#ifdef FEAT_VISUAL
8967 VIsual_active ? INVERTED :
8968#endif
8969 0);
8970#ifdef FEAT_TITLE
8971 if (need_maketitle)
8972 maketitle();
8973#endif
8974 RedrawingDisabled = r;
8975 p_lz = p;
8976
8977 /* Reset msg_didout, so that a message that's there is overwritten. */
8978 msg_didout = FALSE;
8979 msg_col = 0;
8980
8981 out_flush();
8982}
8983
8984/*
8985 * ":redrawstatus": force redraw of status line(s)
8986 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008987 static void
8988ex_redrawstatus(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008989 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008990{
8991#if defined(FEAT_WINDOWS)
8992 int r = RedrawingDisabled;
8993 int p = p_lz;
8994
8995 RedrawingDisabled = 0;
8996 p_lz = FALSE;
8997 if (eap->forceit)
8998 status_redraw_all();
8999 else
9000 status_redraw_curbuf();
9001 update_screen(
9002# ifdef FEAT_VISUAL
9003 VIsual_active ? INVERTED :
9004# endif
9005 0);
9006 RedrawingDisabled = r;
9007 p_lz = p;
9008 out_flush();
9009#endif
9010}
9011
9012 static void
9013close_redir()
9014{
9015 if (redir_fd != NULL)
9016 {
9017 fclose(redir_fd);
9018 redir_fd = NULL;
9019 }
9020#ifdef FEAT_EVAL
9021 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009022 if (redir_vname)
9023 {
9024 var_redir_stop();
9025 redir_vname = 0;
9026 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009027#endif
9028}
9029
9030#if defined(FEAT_SESSION) && defined(USE_CRNL)
9031# define MKSESSION_NL
9032static int mksession_nl = FALSE; /* use NL only in put_eol() */
9033#endif
9034
9035/*
9036 * ":mkexrc", ":mkvimrc", ":mkview" and ":mksession".
9037 */
9038 static void
9039ex_mkrc(eap)
9040 exarg_T *eap;
9041{
9042 FILE *fd;
9043 int failed = FALSE;
9044 char_u *fname;
9045#ifdef FEAT_BROWSE
9046 char_u *browseFile = NULL;
9047#endif
9048#ifdef FEAT_SESSION
9049 int view_session = FALSE;
9050 int using_vdir = FALSE; /* using 'viewdir'? */
9051 char_u *viewFile = NULL;
9052 unsigned *flagp;
9053#endif
9054
9055 if (eap->cmdidx == CMD_mksession || eap->cmdidx == CMD_mkview)
9056 {
9057#ifdef FEAT_SESSION
9058 view_session = TRUE;
9059#else
9060 ex_ni(eap);
9061 return;
9062#endif
9063 }
9064
9065#ifdef FEAT_SESSION
Bram Moolenaar8d594672009-07-01 18:18:57 +00009066 /* Use the short file name until ":lcd" is used. We also don't use the
9067 * short file name when 'acd' is set, that is checked later. */
Bram Moolenaareeefcc72007-05-01 21:21:21 +00009068 did_lcd = FALSE;
9069
Bram Moolenaar071d4272004-06-13 20:20:40 +00009070 /* ":mkview" or ":mkview 9": generate file name with 'viewdir' */
9071 if (eap->cmdidx == CMD_mkview
9072 && (*eap->arg == NUL
9073 || (vim_isdigit(*eap->arg) && eap->arg[1] == NUL)))
9074 {
9075 eap->forceit = TRUE;
9076 fname = get_view_file(*eap->arg);
9077 if (fname == NULL)
9078 return;
9079 viewFile = fname;
9080 using_vdir = TRUE;
9081 }
9082 else
9083#endif
9084 if (*eap->arg != NUL)
9085 fname = eap->arg;
9086 else if (eap->cmdidx == CMD_mkvimrc)
9087 fname = (char_u *)VIMRC_FILE;
9088#ifdef FEAT_SESSION
9089 else if (eap->cmdidx == CMD_mksession)
9090 fname = (char_u *)SESSION_FILE;
9091#endif
9092 else
9093 fname = (char_u *)EXRC_FILE;
9094
9095#ifdef FEAT_BROWSE
9096 if (cmdmod.browse)
9097 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00009098 browseFile = do_browse(BROWSE_SAVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009099# ifdef FEAT_SESSION
9100 eap->cmdidx == CMD_mkview ? (char_u *)_("Save View") :
9101 eap->cmdidx == CMD_mksession ? (char_u *)_("Save Session") :
9102# endif
9103 (char_u *)_("Save Setup"),
9104 fname, (char_u *)"vim", NULL, BROWSE_FILTER_MACROS, NULL);
9105 if (browseFile == NULL)
9106 goto theend;
9107 fname = browseFile;
9108 eap->forceit = TRUE; /* since dialog already asked */
9109 }
9110#endif
9111
9112#if defined(FEAT_SESSION) && defined(vim_mkdir)
9113 /* When using 'viewdir' may have to create the directory. */
9114 if (using_vdir && !mch_isdir(p_vdir))
Bram Moolenaardf177f62005-02-22 08:39:57 +00009115 vim_mkdir_emsg(p_vdir, 0755);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009116#endif
9117
9118 fd = open_exfile(fname, eap->forceit, WRITEBIN);
9119 if (fd != NULL)
9120 {
9121#ifdef FEAT_SESSION
9122 if (eap->cmdidx == CMD_mkview)
9123 flagp = &vop_flags;
9124 else
9125 flagp = &ssop_flags;
9126#endif
9127
9128#ifdef MKSESSION_NL
9129 /* "unix" in 'sessionoptions': use NL line separator */
9130 if (view_session && (*flagp & SSOP_UNIX))
9131 mksession_nl = TRUE;
9132#endif
9133
9134 /* Write the version command for :mkvimrc */
9135 if (eap->cmdidx == CMD_mkvimrc)
9136 (void)put_line(fd, "version 6.0");
9137
9138#ifdef FEAT_SESSION
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009139 if (eap->cmdidx == CMD_mksession)
9140 {
9141 if (put_line(fd, "let SessionLoad = 1") == FAIL)
9142 failed = TRUE;
9143 }
9144
Bram Moolenaar071d4272004-06-13 20:20:40 +00009145 if (eap->cmdidx != CMD_mkview)
9146#endif
9147 {
9148 /* Write setting 'compatible' first, because it has side effects.
9149 * For that same reason only do it when needed. */
9150 if (p_cp)
9151 (void)put_line(fd, "if !&cp | set cp | endif");
9152 else
9153 (void)put_line(fd, "if &cp | set nocp | endif");
9154 }
9155
9156#ifdef FEAT_SESSION
9157 if (!view_session
9158 || (eap->cmdidx == CMD_mksession
9159 && (*flagp & SSOP_OPTIONS)))
9160#endif
9161 failed |= (makemap(fd, NULL) == FAIL
9162 || makeset(fd, OPT_GLOBAL, FALSE) == FAIL);
9163
9164#ifdef FEAT_SESSION
9165 if (!failed && view_session)
9166 {
9167 if (put_line(fd, "let s:so_save = &so | let s:siso_save = &siso | set so=0 siso=0") == FAIL)
9168 failed = TRUE;
9169 if (eap->cmdidx == CMD_mksession)
9170 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02009171 char_u *dirnow; /* current directory */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009172
Bram Moolenaard9462e32011-04-11 21:35:11 +02009173 dirnow = alloc(MAXPATHL);
9174 if (dirnow == NULL)
9175 failed = TRUE;
9176 else
9177 {
9178 /*
9179 * Change to session file's dir.
9180 */
9181 if (mch_dirname(dirnow, MAXPATHL) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00009182 || mch_chdir((char *)dirnow) != 0)
Bram Moolenaard9462e32011-04-11 21:35:11 +02009183 *dirnow = NUL;
9184 if (*dirnow != NUL && (ssop_flags & SSOP_SESDIR))
9185 {
9186 if (vim_chdirfile(fname) == OK)
9187 shorten_fnames(TRUE);
9188 }
9189 else if (*dirnow != NUL
9190 && (ssop_flags & SSOP_CURDIR) && globaldir != NULL)
9191 {
9192 if (mch_chdir((char *)globaldir) == 0)
9193 shorten_fnames(TRUE);
9194 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009195
Bram Moolenaard9462e32011-04-11 21:35:11 +02009196 failed |= (makeopens(fd, dirnow) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009197
Bram Moolenaard9462e32011-04-11 21:35:11 +02009198 /* restore original dir */
9199 if (*dirnow != NUL && ((ssop_flags & SSOP_SESDIR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009200 || ((ssop_flags & SSOP_CURDIR) && globaldir != NULL)))
Bram Moolenaard9462e32011-04-11 21:35:11 +02009201 {
9202 if (mch_chdir((char *)dirnow) != 0)
9203 EMSG(_(e_prev_dir));
9204 shorten_fnames(TRUE);
9205 }
9206 vim_free(dirnow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009207 }
9208 }
9209 else
9210 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +00009211 failed |= (put_view(fd, curwin, !using_vdir, flagp,
9212 -1) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009213 }
9214 if (put_line(fd, "let &so = s:so_save | let &siso = s:siso_save")
9215 == FAIL)
9216 failed = TRUE;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009217 if (put_line(fd, "doautoall SessionLoadPost") == FAIL)
9218 failed = TRUE;
Bram Moolenaar4770d092006-01-12 23:22:24 +00009219 if (eap->cmdidx == CMD_mksession)
9220 {
9221 if (put_line(fd, "unlet SessionLoad") == FAIL)
9222 failed = TRUE;
9223 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009224 }
9225#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009226 if (put_line(fd, "\" vim: set ft=vim :") == FAIL)
9227 failed = TRUE;
9228
Bram Moolenaar071d4272004-06-13 20:20:40 +00009229 failed |= fclose(fd);
9230
9231 if (failed)
9232 EMSG(_(e_write));
9233#if defined(FEAT_EVAL) && defined(FEAT_SESSION)
9234 else if (eap->cmdidx == CMD_mksession)
9235 {
9236 /* successful session write - set this_session var */
Bram Moolenaard9462e32011-04-11 21:35:11 +02009237 char_u *tbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009238
Bram Moolenaard9462e32011-04-11 21:35:11 +02009239 tbuf = alloc(MAXPATHL);
9240 if (tbuf != NULL)
9241 {
9242 if (vim_FullName(fname, tbuf, MAXPATHL, FALSE) == OK)
9243 set_vim_var_string(VV_THIS_SESSION, tbuf, -1);
9244 vim_free(tbuf);
9245 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009246 }
9247#endif
9248#ifdef MKSESSION_NL
9249 mksession_nl = FALSE;
9250#endif
9251 }
9252
9253#ifdef FEAT_BROWSE
9254theend:
9255 vim_free(browseFile);
9256#endif
9257#ifdef FEAT_SESSION
9258 vim_free(viewFile);
9259#endif
9260}
9261
Bram Moolenaardf177f62005-02-22 08:39:57 +00009262#if ((defined(FEAT_SESSION) || defined(FEAT_EVAL)) && defined(vim_mkdir)) \
9263 || defined(PROTO)
9264 int
9265vim_mkdir_emsg(name, prot)
9266 char_u *name;
Bram Moolenaar78a15312009-05-15 19:33:18 +00009267 int prot UNUSED;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009268{
9269 if (vim_mkdir(name, prot) != 0)
9270 {
9271 EMSG2(_("E739: Cannot create directory: %s"), name);
9272 return FAIL;
9273 }
9274 return OK;
9275}
9276#endif
9277
Bram Moolenaar071d4272004-06-13 20:20:40 +00009278/*
9279 * Open a file for writing for an Ex command, with some checks.
9280 * Return file descriptor, or NULL on failure.
9281 */
9282 FILE *
9283open_exfile(fname, forceit, mode)
9284 char_u *fname;
9285 int forceit;
9286 char *mode; /* "w" for create new file or "a" for append */
9287{
9288 FILE *fd;
9289
9290#ifdef UNIX
9291 /* with Unix it is possible to open a directory */
9292 if (mch_isdir(fname))
9293 {
9294 EMSG2(_(e_isadir2), fname);
9295 return NULL;
9296 }
9297#endif
9298 if (!forceit && *mode != 'a' && vim_fexists(fname))
9299 {
9300 EMSG2(_("E189: \"%s\" exists (add ! to override)"), fname);
9301 return NULL;
9302 }
9303
9304 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
9305 EMSG2(_("E190: Cannot open \"%s\" for writing"), fname);
9306
9307 return fd;
9308}
9309
9310/*
9311 * ":mark" and ":k".
9312 */
9313 static void
9314ex_mark(eap)
9315 exarg_T *eap;
9316{
9317 pos_T pos;
9318
9319 if (*eap->arg == NUL) /* No argument? */
9320 EMSG(_(e_argreq));
9321 else if (eap->arg[1] != NUL) /* more than one character? */
9322 EMSG(_(e_trailing));
9323 else
9324 {
9325 pos = curwin->w_cursor; /* save curwin->w_cursor */
9326 curwin->w_cursor.lnum = eap->line2;
9327 beginline(BL_WHITE | BL_FIX);
9328 if (setmark(*eap->arg) == FAIL) /* set mark */
9329 EMSG(_("E191: Argument must be a letter or forward/backward quote"));
9330 curwin->w_cursor = pos; /* restore curwin->w_cursor */
9331 }
9332}
9333
9334/*
9335 * Update w_topline, w_leftcol and the cursor position.
9336 */
9337 void
9338update_topline_cursor()
9339{
9340 check_cursor(); /* put cursor on valid line */
9341 update_topline();
9342 if (!curwin->w_p_wrap)
9343 validate_cursor();
9344 update_curswant();
9345}
9346
9347#ifdef FEAT_EX_EXTRA
9348/*
9349 * ":normal[!] {commands}": Execute normal mode commands.
9350 */
9351 static void
9352ex_normal(eap)
9353 exarg_T *eap;
9354{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009355 int save_msg_scroll = msg_scroll;
9356 int save_restart_edit = restart_edit;
9357 int save_msg_didout = msg_didout;
9358 int save_State = State;
9359 tasave_T tabuf;
9360 int save_insertmode = p_im;
9361 int save_finish_op = finish_op;
Bram Moolenaar38084112008-07-26 14:05:07 +00009362 int save_opcount = opcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009363#ifdef FEAT_MBYTE
9364 char_u *arg = NULL;
9365 int l;
9366 char_u *p;
9367#endif
9368
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00009369 if (ex_normal_lock > 0)
9370 {
9371 EMSG(_(e_secure));
9372 return;
9373 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009374 if (ex_normal_busy >= p_mmd)
9375 {
9376 EMSG(_("E192: Recursive use of :normal too deep"));
9377 return;
9378 }
9379 ++ex_normal_busy;
9380
9381 msg_scroll = FALSE; /* no msg scrolling in Normal mode */
9382 restart_edit = 0; /* don't go to Insert mode */
9383 p_im = FALSE; /* don't use 'insertmode' */
9384
9385#ifdef FEAT_MBYTE
9386 /*
9387 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
9388 * this for the K_SPECIAL leading byte, otherwise special keys will not
9389 * work.
9390 */
9391 if (has_mbyte)
9392 {
9393 int len = 0;
9394
9395 /* Count the number of characters to be escaped. */
9396 for (p = eap->arg; *p != NUL; ++p)
9397 {
9398# ifdef FEAT_GUI
9399 if (*p == CSI) /* leadbyte CSI */
9400 len += 2;
9401# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009402 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009403 if (*++p == K_SPECIAL /* trailbyte K_SPECIAL or CSI */
9404# ifdef FEAT_GUI
9405 || *p == CSI
9406# endif
9407 )
9408 len += 2;
9409 }
9410 if (len > 0)
9411 {
9412 arg = alloc((unsigned)(STRLEN(eap->arg) + len + 1));
9413 if (arg != NULL)
9414 {
9415 len = 0;
9416 for (p = eap->arg; *p != NUL; ++p)
9417 {
9418 arg[len++] = *p;
9419# ifdef FEAT_GUI
9420 if (*p == CSI)
9421 {
9422 arg[len++] = KS_EXTRA;
9423 arg[len++] = (int)KE_CSI;
9424 }
9425# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009426 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009427 {
9428 arg[len++] = *++p;
9429 if (*p == K_SPECIAL)
9430 {
9431 arg[len++] = KS_SPECIAL;
9432 arg[len++] = KE_FILLER;
9433 }
9434# ifdef FEAT_GUI
9435 else if (*p == CSI)
9436 {
9437 arg[len++] = KS_EXTRA;
9438 arg[len++] = (int)KE_CSI;
9439 }
9440# endif
9441 }
9442 arg[len] = NUL;
9443 }
9444 }
9445 }
9446 }
9447#endif
9448
9449 /*
9450 * Save the current typeahead. This is required to allow using ":normal"
9451 * from an event handler and makes sure we don't hang when the argument
9452 * ends with half a command.
9453 */
9454 save_typeahead(&tabuf);
9455 if (tabuf.typebuf_valid)
9456 {
9457 /*
9458 * Repeat the :normal command for each line in the range. When no
9459 * range given, execute it just once, without positioning the cursor
9460 * first.
9461 */
9462 do
9463 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009464 if (eap->addr_count != 0)
9465 {
9466 curwin->w_cursor.lnum = eap->line1++;
9467 curwin->w_cursor.col = 0;
9468 }
9469
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009470 exec_normal_cmd(
Bram Moolenaar071d4272004-06-13 20:20:40 +00009471#ifdef FEAT_MBYTE
9472 arg != NULL ? arg :
9473#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009474 eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009475 }
9476 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
9477 }
9478
9479 /* Might not return to the main loop when in an event handler. */
9480 update_topline_cursor();
9481
9482 /* Restore the previous typeahead. */
9483 restore_typeahead(&tabuf);
9484
9485 --ex_normal_busy;
9486 msg_scroll = save_msg_scroll;
9487 restart_edit = save_restart_edit;
9488 p_im = save_insertmode;
9489 finish_op = save_finish_op;
Bram Moolenaar38084112008-07-26 14:05:07 +00009490 opcount = save_opcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009491 msg_didout |= save_msg_didout; /* don't reset msg_didout now */
9492
9493 /* Restore the state (needed when called from a function executed for
9494 * 'indentexpr'). */
9495 State = save_State;
9496#ifdef FEAT_MBYTE
9497 vim_free(arg);
9498#endif
9499}
9500
9501/*
Bram Moolenaar64969662005-12-14 21:59:55 +00009502 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009503 */
9504 static void
9505ex_startinsert(eap)
9506 exarg_T *eap;
9507{
Bram Moolenaarfd371682005-01-14 21:42:54 +00009508 if (eap->forceit)
9509 {
9510 coladvance((colnr_T)MAXCOL);
9511 curwin->w_curswant = MAXCOL;
9512 curwin->w_set_curswant = FALSE;
9513 }
9514
Bram Moolenaara40c5002005-01-09 21:16:21 +00009515 /* Ignore the command when already in Insert mode. Inserting an
9516 * expression register that invokes a function can do this. */
9517 if (State & INSERT)
9518 return;
9519
Bram Moolenaar64969662005-12-14 21:59:55 +00009520 if (eap->cmdidx == CMD_startinsert)
9521 restart_edit = 'a';
9522 else if (eap->cmdidx == CMD_startreplace)
9523 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00009524 else
Bram Moolenaar64969662005-12-14 21:59:55 +00009525 restart_edit = 'V';
9526
9527 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009528 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009529 if (eap->cmdidx == CMD_startinsert)
9530 restart_edit = 'i';
Bram Moolenaar071d4272004-06-13 20:20:40 +00009531 curwin->w_curswant = 0; /* avoid MAXCOL */
9532 }
9533}
9534
9535/*
9536 * ":stopinsert"
9537 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009538 static void
9539ex_stopinsert(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009540 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009541{
9542 restart_edit = 0;
9543 stop_insert_mode = TRUE;
9544}
9545#endif
9546
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009547#if defined(FEAT_EX_EXTRA) || defined(FEAT_MENU) || defined(PROTO)
9548/*
9549 * Execute normal mode command "cmd".
9550 * "remap" can be REMAP_NONE or REMAP_YES.
9551 */
9552 void
9553exec_normal_cmd(cmd, remap, silent)
9554 char_u *cmd;
9555 int remap;
9556 int silent;
9557{
9558 oparg_T oa;
9559
9560 /*
9561 * Stuff the argument into the typeahead buffer.
9562 * Execute normal_cmd() until there is no typeahead left.
9563 */
9564 clear_oparg(&oa);
9565 finish_op = FALSE;
9566 ins_typebuf(cmd, remap, 0, TRUE, silent);
9567 while ((!stuff_empty() || (!typebuf_typed() && typebuf.tb_len > 0))
9568 && !got_int)
9569 {
9570 update_topline_cursor();
Bram Moolenaar48ac02c2011-01-17 19:50:06 +01009571 normal_cmd(&oa, TRUE); /* execute a Normal mode cmd */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009572 }
9573}
9574#endif
9575
Bram Moolenaar071d4272004-06-13 20:20:40 +00009576#ifdef FEAT_FIND_ID
9577 static void
9578ex_checkpath(eap)
9579 exarg_T *eap;
9580{
9581 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
9582 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
9583 (linenr_T)1, (linenr_T)MAXLNUM);
9584}
9585
9586#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
9587/*
9588 * ":psearch"
9589 */
9590 static void
9591ex_psearch(eap)
9592 exarg_T *eap;
9593{
9594 g_do_tagpreview = p_pvh;
9595 ex_findpat(eap);
9596 g_do_tagpreview = 0;
9597}
9598#endif
9599
9600 static void
9601ex_findpat(eap)
9602 exarg_T *eap;
9603{
9604 int whole = TRUE;
9605 long n;
9606 char_u *p;
9607 int action;
9608
9609 switch (cmdnames[eap->cmdidx].cmd_name[2])
9610 {
9611 case 'e': /* ":psearch", ":isearch" and ":dsearch" */
9612 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
9613 action = ACTION_GOTO;
9614 else
9615 action = ACTION_SHOW;
9616 break;
9617 case 'i': /* ":ilist" and ":dlist" */
9618 action = ACTION_SHOW_ALL;
9619 break;
9620 case 'u': /* ":ijump" and ":djump" */
9621 action = ACTION_GOTO;
9622 break;
9623 default: /* ":isplit" and ":dsplit" */
9624 action = ACTION_SPLIT;
9625 break;
9626 }
9627
9628 n = 1;
9629 if (vim_isdigit(*eap->arg)) /* get count */
9630 {
9631 n = getdigits(&eap->arg);
9632 eap->arg = skipwhite(eap->arg);
9633 }
9634 if (*eap->arg == '/') /* Match regexp, not just whole words */
9635 {
9636 whole = FALSE;
9637 ++eap->arg;
9638 p = skip_regexp(eap->arg, '/', p_magic, NULL);
9639 if (*p)
9640 {
9641 *p++ = NUL;
9642 p = skipwhite(p);
9643
9644 /* Check for trailing illegal characters */
9645 if (!ends_excmd(*p))
9646 eap->errmsg = e_trailing;
9647 else
9648 eap->nextcmd = check_nextcmd(p);
9649 }
9650 }
9651 if (!eap->skip)
9652 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
9653 whole, !eap->forceit,
9654 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
9655 n, action, eap->line1, eap->line2);
9656}
9657#endif
9658
9659#ifdef FEAT_WINDOWS
9660
9661# ifdef FEAT_QUICKFIX
9662/*
9663 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
9664 */
9665 static void
9666ex_ptag(eap)
9667 exarg_T *eap;
9668{
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01009669 g_do_tagpreview = p_pvh; /* will be reset to 0 in ex_tag_cmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009670 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9671}
9672
9673/*
9674 * ":pedit"
9675 */
9676 static void
9677ex_pedit(eap)
9678 exarg_T *eap;
9679{
9680 win_T *curwin_save = curwin;
9681
9682 g_do_tagpreview = p_pvh;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00009683 prepare_tagpreview(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009684 keep_help_flag = curwin_save->w_buffer->b_help;
9685 do_exedit(eap, NULL);
9686 keep_help_flag = FALSE;
9687 if (curwin != curwin_save && win_valid(curwin_save))
9688 {
9689 /* Return cursor to where we were */
9690 validate_cursor();
9691 redraw_later(VALID);
9692 win_enter(curwin_save, TRUE);
9693 }
9694 g_do_tagpreview = 0;
9695}
9696# endif
9697
9698/*
9699 * ":stag", ":stselect" and ":stjump".
9700 */
9701 static void
9702ex_stag(eap)
9703 exarg_T *eap;
9704{
9705 postponed_split = -1;
9706 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009707 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009708 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9709 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009710 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009711}
9712#endif
9713
9714/*
9715 * ":tag", ":tselect", ":tjump", ":tnext", etc.
9716 */
9717 static void
9718ex_tag(eap)
9719 exarg_T *eap;
9720{
9721 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
9722}
9723
9724 static void
9725ex_tag_cmd(eap, name)
9726 exarg_T *eap;
9727 char_u *name;
9728{
9729 int cmd;
9730
9731 switch (name[1])
9732 {
9733 case 'j': cmd = DT_JUMP; /* ":tjump" */
9734 break;
9735 case 's': cmd = DT_SELECT; /* ":tselect" */
9736 break;
9737 case 'p': cmd = DT_PREV; /* ":tprevious" */
9738 break;
9739 case 'N': cmd = DT_PREV; /* ":tNext" */
9740 break;
9741 case 'n': cmd = DT_NEXT; /* ":tnext" */
9742 break;
9743 case 'o': cmd = DT_POP; /* ":pop" */
9744 break;
9745 case 'f': /* ":tfirst" */
9746 case 'r': cmd = DT_FIRST; /* ":trewind" */
9747 break;
9748 case 'l': cmd = DT_LAST; /* ":tlast" */
9749 break;
9750 default: /* ":tag" */
9751#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +00009752 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009753 {
9754 do_cstag(eap);
9755 return;
9756 }
9757#endif
9758 cmd = DT_TAG;
9759 break;
9760 }
9761
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00009762 if (name[0] == 'l')
9763 {
9764#ifndef FEAT_QUICKFIX
9765 ex_ni(eap);
9766 return;
9767#else
9768 cmd = DT_LTAG;
9769#endif
9770 }
9771
Bram Moolenaar071d4272004-06-13 20:20:40 +00009772 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
9773 eap->forceit, TRUE);
9774}
9775
9776/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009777 * Check "str" for starting with a special cmdline variable.
9778 * If found return one of the SPEC_ values and set "*usedlen" to the length of
9779 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
9780 */
9781 int
9782find_cmdline_var(src, usedlen)
9783 char_u *src;
9784 int *usedlen;
9785{
9786 int len;
9787 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +00009788 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009789 "%",
9790#define SPEC_PERC 0
9791 "#",
9792#define SPEC_HASH 1
9793 "<cword>", /* cursor word */
9794#define SPEC_CWORD 2
9795 "<cWORD>", /* cursor WORD */
9796#define SPEC_CCWORD 3
9797 "<cfile>", /* cursor path name */
9798#define SPEC_CFILE 4
9799 "<sfile>", /* ":so" file name */
9800#define SPEC_SFILE 5
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009801 "<slnum>", /* ":so" file line number */
9802#define SPEC_SLNUM 6
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009803#ifdef FEAT_AUTOCMD
9804 "<afile>", /* autocommand file name */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009805# define SPEC_AFILE 7
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009806 "<abuf>", /* autocommand buffer number */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009807# define SPEC_ABUF 8
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009808 "<amatch>", /* autocommand match name */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009809# define SPEC_AMATCH 9
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009810#endif
9811#ifdef FEAT_CLIENTSERVER
9812 "<client>"
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009813# ifdef FEAT_AUTOCMD
9814# define SPEC_CLIENT 10
9815# else
9816# define SPEC_CLIENT 7
9817# endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009818#endif
9819 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009820
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009821 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009822 {
9823 len = (int)STRLEN(spec_str[i]);
9824 if (STRNCMP(src, spec_str[i], len) == 0)
9825 {
9826 *usedlen = len;
9827 return i;
9828 }
9829 }
9830 return -1;
9831}
9832
9833/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009834 * Evaluate cmdline variables.
9835 *
9836 * change '%' to curbuf->b_ffname
9837 * '#' to curwin->w_altfile
9838 * '<cword>' to word under the cursor
9839 * '<cWORD>' to WORD under the cursor
9840 * '<cfile>' to path name under the cursor
9841 * '<sfile>' to sourced file name
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009842 * '<slnum>' to sourced file line number
Bram Moolenaar071d4272004-06-13 20:20:40 +00009843 * '<afile>' to file name for autocommand
9844 * '<abuf>' to buffer number for autocommand
9845 * '<amatch>' to matching name for autocommand
9846 *
9847 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
9848 * "" for error without a message) and NULL is returned.
9849 * Returns an allocated string if a valid match was found.
9850 * Returns NULL if no match was found. "usedlen" then still contains the
9851 * number of characters to skip.
9852 */
9853 char_u *
Bram Moolenaar63b92542007-03-27 14:57:09 +00009854eval_vars(src, srcstart, usedlen, lnump, errormsg, escaped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009855 char_u *src; /* pointer into commandline */
Bram Moolenaar63b92542007-03-27 14:57:09 +00009856 char_u *srcstart; /* beginning of valid memory for src */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009857 int *usedlen; /* characters after src that are used */
9858 linenr_T *lnump; /* line number for :e command, or NULL */
9859 char_u **errormsg; /* pointer to error message */
Bram Moolenaar63b92542007-03-27 14:57:09 +00009860 int *escaped; /* return value has escaped white space (can
9861 * be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009862{
9863 int i;
9864 char_u *s;
9865 char_u *result;
9866 char_u *resultbuf = NULL;
9867 int resultlen;
9868 buf_T *buf;
9869 int valid = VALID_HEAD + VALID_PATH; /* assume valid result */
9870 int spec_idx;
9871#ifdef FEAT_MODIFY_FNAME
9872 int skip_mod = FALSE;
9873#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009874 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009875
9876 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009877 if (escaped != NULL)
9878 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009879
9880 /*
9881 * Check if there is something to do.
9882 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009883 spec_idx = find_cmdline_var(src, usedlen);
9884 if (spec_idx < 0) /* no match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009885 {
9886 *usedlen = 1;
9887 return NULL;
9888 }
9889
9890 /*
9891 * Skip when preceded with a backslash "\%" and "\#".
9892 * Note: In "\\%" the % is also not recognized!
9893 */
9894 if (src > srcstart && src[-1] == '\\')
9895 {
9896 *usedlen = 0;
Bram Moolenaara7241f52008-06-24 20:39:31 +00009897 STRMOVE(src - 1, src); /* remove backslash */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009898 return NULL;
9899 }
9900
9901 /*
9902 * word or WORD under cursor
9903 */
9904 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD)
9905 {
9906 resultlen = find_ident_under_cursor(&result, spec_idx == SPEC_CWORD ?
9907 (FIND_IDENT|FIND_STRING) : FIND_STRING);
9908 if (resultlen == 0)
9909 {
9910 *errormsg = (char_u *)"";
9911 return NULL;
9912 }
9913 }
9914
9915 /*
9916 * '#': Alternate file name
9917 * '%': Current file name
9918 * File name under the cursor
9919 * File name for autocommand
9920 * and following modifiers
9921 */
9922 else
9923 {
9924 switch (spec_idx)
9925 {
9926 case SPEC_PERC: /* '%': current file */
9927 if (curbuf->b_fname == NULL)
9928 {
9929 result = (char_u *)"";
9930 valid = 0; /* Must have ":p:h" to be valid */
9931 }
9932 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009933 result = curbuf->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009934 break;
9935
9936 case SPEC_HASH: /* '#' or "#99": alternate file */
9937 if (src[1] == '#') /* "##": the argument list */
9938 {
9939 result = arg_all();
9940 resultbuf = result;
9941 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009942 if (escaped != NULL)
9943 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009944#ifdef FEAT_MODIFY_FNAME
9945 skip_mod = TRUE;
9946#endif
9947 break;
9948 }
9949 s = src + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00009950 if (*s == '<') /* "#<99" uses v:oldfiles */
9951 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009952 i = (int)getdigits(&s);
9953 *usedlen = (int)(s - src); /* length of what we expand */
9954
Bram Moolenaard812df62008-11-09 12:46:09 +00009955 if (src[1] == '<')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009956 {
Bram Moolenaard812df62008-11-09 12:46:09 +00009957 if (*usedlen < 2)
9958 {
9959 /* Should we give an error message for #<text? */
9960 *usedlen = 1;
9961 return NULL;
9962 }
9963#ifdef FEAT_EVAL
9964 result = list_find_str(get_vim_var_list(VV_OLDFILES),
9965 (long)i);
9966 if (result == NULL)
9967 {
9968 *errormsg = (char_u *)"";
9969 return NULL;
9970 }
9971#else
9972 *errormsg = (char_u *)_("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009973 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00009974#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009975 }
9976 else
Bram Moolenaard812df62008-11-09 12:46:09 +00009977 {
9978 buf = buflist_findnr(i);
9979 if (buf == NULL)
9980 {
9981 *errormsg = (char_u *)_("E194: No alternate file name to substitute for '#'");
9982 return NULL;
9983 }
9984 if (lnump != NULL)
9985 *lnump = ECMD_LAST;
9986 if (buf->b_fname == NULL)
9987 {
9988 result = (char_u *)"";
9989 valid = 0; /* Must have ":p:h" to be valid */
9990 }
9991 else
9992 result = buf->b_fname;
9993 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009994 break;
9995
9996#ifdef FEAT_SEARCHPATH
9997 case SPEC_CFILE: /* file name under cursor */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009998 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009999 if (result == NULL)
10000 {
10001 *errormsg = (char_u *)"";
10002 return NULL;
10003 }
10004 resultbuf = result; /* remember allocated string */
10005 break;
10006#endif
10007
10008#ifdef FEAT_AUTOCMD
10009 case SPEC_AFILE: /* file name for autocommand */
10010 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +000010011 if (result != NULL && !autocmd_fname_full)
10012 {
10013 /* Still need to turn the fname into a full path. It is
10014 * postponed to avoid a delay when <afile> is not used. */
10015 autocmd_fname_full = TRUE;
10016 result = FullName_save(autocmd_fname, FALSE);
10017 vim_free(autocmd_fname);
10018 autocmd_fname = result;
10019 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010020 if (result == NULL)
10021 {
10022 *errormsg = (char_u *)_("E495: no autocommand file name to substitute for \"<afile>\"");
10023 return NULL;
10024 }
Bram Moolenaara0174af2008-01-02 20:08:25 +000010025 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010026 break;
10027
10028 case SPEC_ABUF: /* buffer number for autocommand */
10029 if (autocmd_bufnr <= 0)
10030 {
10031 *errormsg = (char_u *)_("E496: no autocommand buffer number to substitute for \"<abuf>\"");
10032 return NULL;
10033 }
10034 sprintf((char *)strbuf, "%d", autocmd_bufnr);
10035 result = strbuf;
10036 break;
10037
10038 case SPEC_AMATCH: /* match name for autocommand */
10039 result = autocmd_match;
10040 if (result == NULL)
10041 {
10042 *errormsg = (char_u *)_("E497: no autocommand match name to substitute for \"<amatch>\"");
10043 return NULL;
10044 }
10045 break;
10046
10047#endif
10048 case SPEC_SFILE: /* file name for ":so" command */
10049 result = sourcing_name;
10050 if (result == NULL)
10051 {
10052 *errormsg = (char_u *)_("E498: no :source file name to substitute for \"<sfile>\"");
10053 return NULL;
10054 }
10055 break;
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010056 case SPEC_SLNUM: /* line in file for ":so" command */
10057 if (sourcing_name == NULL || sourcing_lnum == 0)
10058 {
10059 *errormsg = (char_u *)_("E842: no line number to use for \"<slnum>\"");
10060 return NULL;
10061 }
10062 sprintf((char *)strbuf, "%ld", (long)sourcing_lnum);
10063 result = strbuf;
10064 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010065#if defined(FEAT_CLIENTSERVER)
10066 case SPEC_CLIENT: /* Source of last submitted input */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010067 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
10068 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010069 result = strbuf;
10070 break;
10071#endif
10072 }
10073
10074 resultlen = (int)STRLEN(result); /* length of new string */
10075 if (src[*usedlen] == '<') /* remove the file name extension */
10076 {
10077 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010078 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010079 resultlen = (int)(s - result);
10080 }
10081#ifdef FEAT_MODIFY_FNAME
10082 else if (!skip_mod)
10083 {
10084 valid |= modify_fname(src, usedlen, &result, &resultbuf,
10085 &resultlen);
10086 if (result == NULL)
10087 {
10088 *errormsg = (char_u *)"";
10089 return NULL;
10090 }
10091 }
10092#endif
10093 }
10094
10095 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
10096 {
10097 if (valid != VALID_HEAD + VALID_PATH)
10098 /* xgettext:no-c-format */
10099 *errormsg = (char_u *)_("E499: Empty file name for '%' or '#', only works with \":p:h\"");
10100 else
10101 *errormsg = (char_u *)_("E500: Evaluates to an empty string");
10102 result = NULL;
10103 }
10104 else
10105 result = vim_strnsave(result, resultlen);
10106 vim_free(resultbuf);
10107 return result;
10108}
10109
10110/*
10111 * Concatenate all files in the argument list, separated by spaces, and return
10112 * it in one allocated string.
10113 * Spaces and backslashes in the file names are escaped with a backslash.
10114 * Returns NULL when out of memory.
10115 */
10116 static char_u *
10117arg_all()
10118{
10119 int len;
10120 int idx;
10121 char_u *retval = NULL;
10122 char_u *p;
10123
10124 /*
10125 * Do this loop two times:
10126 * first time: compute the total length
10127 * second time: concatenate the names
10128 */
10129 for (;;)
10130 {
10131 len = 0;
10132 for (idx = 0; idx < ARGCOUNT; ++idx)
10133 {
10134 p = alist_name(&ARGLIST[idx]);
10135 if (p != NULL)
10136 {
10137 if (len > 0)
10138 {
10139 /* insert a space in between names */
10140 if (retval != NULL)
10141 retval[len] = ' ';
10142 ++len;
10143 }
10144 for ( ; *p != NUL; ++p)
10145 {
10146 if (*p == ' ' || *p == '\\')
10147 {
10148 /* insert a backslash */
10149 if (retval != NULL)
10150 retval[len] = '\\';
10151 ++len;
10152 }
10153 if (retval != NULL)
10154 retval[len] = *p;
10155 ++len;
10156 }
10157 }
10158 }
10159
10160 /* second time: break here */
10161 if (retval != NULL)
10162 {
10163 retval[len] = NUL;
10164 break;
10165 }
10166
10167 /* allocate memory */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000010168 retval = alloc((unsigned)len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010169 if (retval == NULL)
10170 break;
10171 }
10172
10173 return retval;
10174}
10175
10176#if defined(FEAT_AUTOCMD) || defined(PROTO)
10177/*
10178 * Expand the <sfile> string in "arg".
10179 *
10180 * Returns an allocated string, or NULL for any error.
10181 */
10182 char_u *
10183expand_sfile(arg)
10184 char_u *arg;
10185{
10186 char_u *errormsg;
10187 int len;
10188 char_u *result;
10189 char_u *newres;
10190 char_u *repl;
10191 int srclen;
10192 char_u *p;
10193
10194 result = vim_strsave(arg);
10195 if (result == NULL)
10196 return NULL;
10197
10198 for (p = result; *p; )
10199 {
10200 if (STRNCMP(p, "<sfile>", 7) != 0)
10201 ++p;
10202 else
10203 {
10204 /* replace "<sfile>" with the sourced file name, and do ":" stuff */
Bram Moolenaar63b92542007-03-27 14:57:09 +000010205 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010206 if (errormsg != NULL)
10207 {
10208 if (*errormsg)
10209 emsg(errormsg);
10210 vim_free(result);
10211 return NULL;
10212 }
10213 if (repl == NULL) /* no match (cannot happen) */
10214 {
10215 p += srclen;
10216 continue;
10217 }
10218 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
10219 newres = alloc(len);
10220 if (newres == NULL)
10221 {
10222 vim_free(repl);
10223 vim_free(result);
10224 return NULL;
10225 }
10226 mch_memmove(newres, result, (size_t)(p - result));
10227 STRCPY(newres + (p - result), repl);
10228 len = (int)STRLEN(newres);
10229 STRCAT(newres, p + srclen);
10230 vim_free(repl);
10231 vim_free(result);
10232 result = newres;
10233 p = newres + len; /* continue after the match */
10234 }
10235 }
10236
10237 return result;
10238}
10239#endif
10240
10241#ifdef FEAT_SESSION
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010242static int ses_winsizes __ARGS((FILE *fd, int restore_size,
10243 win_T *tab_firstwin));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010244static int ses_win_rec __ARGS((FILE *fd, frame_T *fr));
10245static frame_T *ses_skipframe __ARGS((frame_T *fr));
10246static int ses_do_frame __ARGS((frame_T *fr));
10247static int ses_do_win __ARGS((win_T *wp));
10248static int ses_arglist __ARGS((FILE *fd, char *cmd, garray_T *gap, int fullname, unsigned *flagp));
10249static int ses_put_fname __ARGS((FILE *fd, char_u *name, unsigned *flagp));
10250static int ses_fname __ARGS((FILE *fd, buf_T *buf, unsigned *flagp));
10251
10252/*
10253 * Write openfile commands for the current buffers to an .exrc file.
10254 * Return FAIL on error, OK otherwise.
10255 */
10256 static int
10257makeopens(fd, dirnow)
10258 FILE *fd;
10259 char_u *dirnow; /* Current directory name */
10260{
10261 buf_T *buf;
10262 int only_save_windows = TRUE;
10263 int nr;
10264 int cnr = 1;
10265 int restore_size = TRUE;
10266 win_T *wp;
10267 char_u *sname;
10268 win_T *edited_win = NULL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010269 int tabnr;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010270 win_T *tab_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010271 frame_T *tab_topframe;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010272 int cur_arg_idx = 0;
Bram Moolenaar383c6f52008-01-04 15:01:07 +000010273 int next_arg_idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010274
10275 if (ssop_flags & SSOP_BUFFERS)
10276 only_save_windows = FALSE; /* Save ALL buffers */
10277
10278 /*
10279 * Begin by setting the this_session variable, and then other
10280 * sessionable variables.
10281 */
10282#ifdef FEAT_EVAL
10283 if (put_line(fd, "let v:this_session=expand(\"<sfile>:p\")") == FAIL)
10284 return FAIL;
10285 if (ssop_flags & SSOP_GLOBALS)
10286 if (store_session_globals(fd) == FAIL)
10287 return FAIL;
10288#endif
10289
10290 /*
10291 * Close all windows but one.
10292 */
10293 if (put_line(fd, "silent only") == FAIL)
10294 return FAIL;
10295
10296 /*
10297 * Now a :cd command to the session directory or the current directory
10298 */
10299 if (ssop_flags & SSOP_SESDIR)
10300 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010301 if (put_line(fd, "exe \"cd \" . escape(expand(\"<sfile>:p:h\"), ' ')")
10302 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010303 return FAIL;
10304 }
10305 else if (ssop_flags & SSOP_CURDIR)
10306 {
10307 sname = home_replace_save(NULL, globaldir != NULL ? globaldir : dirnow);
10308 if (sname == NULL
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010309 || fputs("cd ", fd) < 0
10310 || ses_put_fname(fd, sname, &ssop_flags) == FAIL
10311 || put_eol(fd) == FAIL)
10312 {
10313 vim_free(sname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010314 return FAIL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010315 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010316 vim_free(sname);
10317 }
10318
10319 /*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010320 * If there is an empty, unnamed buffer we will wipe it out later.
10321 * Remember the buffer number.
10322 */
10323 if (put_line(fd, "if expand('%') == '' && !&modified && line('$') <= 1 && getline(1) == ''") == FAIL)
10324 return FAIL;
10325 if (put_line(fd, " let s:wipebuf = bufnr('%')") == FAIL)
10326 return FAIL;
10327 if (put_line(fd, "endif") == FAIL)
10328 return FAIL;
10329
10330 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010331 * Now save the current files, current buffer first.
10332 */
10333 if (put_line(fd, "set shortmess=aoO") == FAIL)
10334 return FAIL;
10335
10336 /* Now put the other buffers into the buffer list */
10337 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
10338 {
10339 if (!(only_save_windows && buf->b_nwindows == 0)
10340 && !(buf->b_help && !(ssop_flags & SSOP_HELP))
10341 && buf->b_fname != NULL
10342 && buf->b_p_bl)
10343 {
10344 if (fprintf(fd, "badd +%ld ", buf->b_wininfo == NULL ? 1L
10345 : buf->b_wininfo->wi_fpos.lnum) < 0
10346 || ses_fname(fd, buf, &ssop_flags) == FAIL)
10347 return FAIL;
10348 }
10349 }
10350
10351 /* the global argument list */
10352 if (ses_arglist(fd, "args", &global_alist.al_ga,
10353 !(ssop_flags & SSOP_CURDIR), &ssop_flags) == FAIL)
10354 return FAIL;
10355
10356 if (ssop_flags & SSOP_RESIZE)
10357 {
10358 /* Note: after the restore we still check it worked!*/
10359 if (fprintf(fd, "set lines=%ld columns=%ld" , Rows, Columns) < 0
10360 || put_eol(fd) == FAIL)
10361 return FAIL;
10362 }
10363
10364#ifdef FEAT_GUI
10365 if (gui.in_use && (ssop_flags & SSOP_WINPOS))
10366 {
10367 int x, y;
10368
10369 if (gui_mch_get_winpos(&x, &y) == OK)
10370 {
10371 /* Note: after the restore we still check it worked!*/
10372 if (fprintf(fd, "winpos %d %d", x, y) < 0 || put_eol(fd) == FAIL)
10373 return FAIL;
10374 }
10375 }
10376#endif
10377
10378 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000010379 * May repeat putting Windows for each tab, when "tabpages" is in
10380 * 'sessionoptions'.
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010381 * Don't use goto_tabpage(), it may change directory and trigger
10382 * autocommands.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010383 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010384 tab_firstwin = firstwin; /* first window in tab page "tabnr" */
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010385 tab_topframe = topframe;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010386 for (tabnr = 1; ; ++tabnr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010387 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010388 int need_tabnew = FALSE;
10389
Bram Moolenaar18144c82006-04-12 21:52:12 +000010390 if ((ssop_flags & SSOP_TABPAGES))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010391 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010392 tabpage_T *tp = find_tabpage(tabnr);
10393
10394 if (tp == NULL)
10395 break; /* done all tab pages */
10396 if (tp == curtab)
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010397 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010398 tab_firstwin = firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010399 tab_topframe = topframe;
10400 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010401 else
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010402 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010403 tab_firstwin = tp->tp_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010404 tab_topframe = tp->tp_topframe;
10405 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010406 if (tabnr > 1)
10407 need_tabnew = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010408 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010409
Bram Moolenaar18144c82006-04-12 21:52:12 +000010410 /*
10411 * Before creating the window layout, try loading one file. If this
10412 * is aborted we don't end up with a number of useless windows.
10413 * This may have side effects! (e.g., compressed or network file).
10414 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010415 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010416 {
10417 if (ses_do_win(wp)
10418 && wp->w_buffer->b_ffname != NULL
10419 && !wp->w_buffer->b_help
10420#ifdef FEAT_QUICKFIX
10421 && !bt_nofile(wp->w_buffer)
10422#endif
10423 )
10424 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010425 if (fputs(need_tabnew ? "tabedit " : "edit ", fd) < 0
Bram Moolenaar18144c82006-04-12 21:52:12 +000010426 || ses_fname(fd, wp->w_buffer, &ssop_flags) == FAIL)
10427 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010428 need_tabnew = FALSE;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010429 if (!wp->w_arg_idx_invalid)
10430 edited_win = wp;
10431 break;
10432 }
10433 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010434
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010435 /* If no file got edited create an empty tab page. */
10436 if (need_tabnew && put_line(fd, "tabnew") == FAIL)
10437 return FAIL;
10438
Bram Moolenaar18144c82006-04-12 21:52:12 +000010439 /*
10440 * Save current window layout.
10441 */
10442 if (put_line(fd, "set splitbelow splitright") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010443 return FAIL;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010444 if (ses_win_rec(fd, tab_topframe) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010445 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010446 if (!p_sb && put_line(fd, "set nosplitbelow") == FAIL)
10447 return FAIL;
10448 if (!p_spr && put_line(fd, "set nosplitright") == FAIL)
10449 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010450
Bram Moolenaar18144c82006-04-12 21:52:12 +000010451 /*
10452 * Check if window sizes can be restored (no windows omitted).
10453 * Remember the window number of the current window after restoring.
10454 */
10455 nr = 0;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010456 for (wp = tab_firstwin; wp != NULL; wp = W_NEXT(wp))
Bram Moolenaar18144c82006-04-12 21:52:12 +000010457 {
10458 if (ses_do_win(wp))
10459 ++nr;
10460 else
10461 restore_size = FALSE;
10462 if (curwin == wp)
10463 cnr = nr;
10464 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010465
Bram Moolenaar18144c82006-04-12 21:52:12 +000010466 /* Go to the first window. */
10467 if (put_line(fd, "wincmd t") == FAIL)
10468 return FAIL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010469
Bram Moolenaar18144c82006-04-12 21:52:12 +000010470 /*
10471 * If more than one window, see if sizes can be restored.
10472 * First set 'winheight' and 'winwidth' to 1 to avoid the windows being
10473 * resized when moving between windows.
10474 * Do this before restoring the view, so that the topline and the
10475 * cursor can be set. This is done again below.
10476 */
10477 if (put_line(fd, "set winheight=1 winwidth=1") == FAIL)
10478 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010479 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010480 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010481
Bram Moolenaar18144c82006-04-12 21:52:12 +000010482 /*
10483 * Restore the view of the window (options, file, cursor, etc.).
10484 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010485 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010486 {
10487 if (!ses_do_win(wp))
10488 continue;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010489 if (put_view(fd, wp, wp != edited_win, &ssop_flags,
10490 cur_arg_idx) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010491 return FAIL;
10492 if (nr > 1 && put_line(fd, "wincmd w") == FAIL)
10493 return FAIL;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010494 next_arg_idx = wp->w_arg_idx;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010495 }
10496
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010497 /* The argument index in the first tab page is zero, need to set it in
10498 * each window. For further tab pages it's the window where we do
10499 * "tabedit". */
10500 cur_arg_idx = next_arg_idx;
10501
Bram Moolenaar18144c82006-04-12 21:52:12 +000010502 /*
10503 * Restore cursor to the current window if it's not the first one.
10504 */
10505 if (cnr > 1 && (fprintf(fd, "%dwincmd w", cnr) < 0
10506 || put_eol(fd) == FAIL))
10507 return FAIL;
10508
10509 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000010510 * Restore window sizes again after jumping around in windows, because
10511 * the current window has a minimum size while others may not.
10512 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010513 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010514 return FAIL;
10515
Bram Moolenaar18144c82006-04-12 21:52:12 +000010516 /* Don't continue in another tab page when doing only the current one
10517 * or when at the last tab page. */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010518 if (!(ssop_flags & SSOP_TABPAGES))
Bram Moolenaar18144c82006-04-12 21:52:12 +000010519 break;
10520 }
10521
10522 if (ssop_flags & SSOP_TABPAGES)
10523 {
Bram Moolenaar18144c82006-04-12 21:52:12 +000010524 if (fprintf(fd, "tabnext %d", tabpage_index(curtab)) < 0
10525 || put_eol(fd) == FAIL)
10526 return FAIL;
10527 }
10528
Bram Moolenaar9c102382006-05-03 21:26:49 +000010529 /*
10530 * Wipe out an empty unnamed buffer we started in.
10531 */
10532 if (put_line(fd, "if exists('s:wipebuf')") == FAIL)
10533 return FAIL;
Bram Moolenaarf3a67882006-05-05 21:09:41 +000010534 if (put_line(fd, " silent exe 'bwipe ' . s:wipebuf") == FAIL)
Bram Moolenaar9c102382006-05-03 21:26:49 +000010535 return FAIL;
10536 if (put_line(fd, "endif") == FAIL)
10537 return FAIL;
10538 if (put_line(fd, "unlet! s:wipebuf") == FAIL)
10539 return FAIL;
10540
10541 /* Re-apply 'winheight', 'winwidth' and 'shortmess'. */
10542 if (fprintf(fd, "set winheight=%ld winwidth=%ld shortmess=%s",
10543 p_wh, p_wiw, p_shm) < 0 || put_eol(fd) == FAIL)
10544 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010545
10546 /*
10547 * Lastly, execute the x.vim file if it exists.
10548 */
10549 if (put_line(fd, "let s:sx = expand(\"<sfile>:p:r\").\"x.vim\"") == FAIL
10550 || put_line(fd, "if file_readable(s:sx)") == FAIL
Bram Moolenaar42ba1262008-12-09 10:18:03 +000010551 || put_line(fd, " exe \"source \" . fnameescape(s:sx)") == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000010552 || put_line(fd, "endif") == FAIL)
10553 return FAIL;
10554
10555 return OK;
10556}
10557
10558 static int
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010559ses_winsizes(fd, restore_size, tab_firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010560 FILE *fd;
10561 int restore_size;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010562 win_T *tab_firstwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010563{
10564 int n = 0;
10565 win_T *wp;
10566
10567 if (restore_size && (ssop_flags & SSOP_WINSIZE))
10568 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010569 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010570 {
10571 if (!ses_do_win(wp))
10572 continue;
10573 ++n;
10574
10575 /* restore height when not full height */
10576 if (wp->w_height + wp->w_status_height < topframe->fr_height
10577 && (fprintf(fd,
10578 "exe '%dresize ' . ((&lines * %ld + %ld) / %ld)",
10579 n, (long)wp->w_height, Rows / 2, Rows) < 0
10580 || put_eol(fd) == FAIL))
10581 return FAIL;
10582
10583 /* restore width when not full width */
10584 if (wp->w_width < Columns && (fprintf(fd,
10585 "exe 'vert %dresize ' . ((&columns * %ld + %ld) / %ld)",
10586 n, (long)wp->w_width, Columns / 2, Columns) < 0
10587 || put_eol(fd) == FAIL))
10588 return FAIL;
10589 }
10590 }
10591 else
10592 {
10593 /* Just equalise window sizes */
10594 if (put_line(fd, "wincmd =") == FAIL)
10595 return FAIL;
10596 }
10597 return OK;
10598}
10599
10600/*
10601 * Write commands to "fd" to recursively create windows for frame "fr",
10602 * horizontally and vertically split.
10603 * After the commands the last window in the frame is the current window.
10604 * Returns FAIL when writing the commands to "fd" fails.
10605 */
10606 static int
10607ses_win_rec(fd, fr)
10608 FILE *fd;
10609 frame_T *fr;
10610{
10611 frame_T *frc;
10612 int count = 0;
10613
10614 if (fr->fr_layout != FR_LEAF)
10615 {
10616 /* Find first frame that's not skipped and then create a window for
10617 * each following one (first frame is already there). */
10618 frc = ses_skipframe(fr->fr_child);
10619 if (frc != NULL)
10620 while ((frc = ses_skipframe(frc->fr_next)) != NULL)
10621 {
10622 /* Make window as big as possible so that we have lots of room
10623 * to split. */
10624 if (put_line(fd, "wincmd _ | wincmd |") == FAIL
10625 || put_line(fd, fr->fr_layout == FR_COL
10626 ? "split" : "vsplit") == FAIL)
10627 return FAIL;
10628 ++count;
10629 }
10630
10631 /* Go back to the first window. */
10632 if (count > 0 && (fprintf(fd, fr->fr_layout == FR_COL
10633 ? "%dwincmd k" : "%dwincmd h", count) < 0
10634 || put_eol(fd) == FAIL))
10635 return FAIL;
10636
10637 /* Recursively create frames/windows in each window of this column or
10638 * row. */
10639 frc = ses_skipframe(fr->fr_child);
10640 while (frc != NULL)
10641 {
10642 ses_win_rec(fd, frc);
10643 frc = ses_skipframe(frc->fr_next);
10644 /* Go to next window. */
10645 if (frc != NULL && put_line(fd, "wincmd w") == FAIL)
10646 return FAIL;
10647 }
10648 }
10649 return OK;
10650}
10651
10652/*
10653 * Skip frames that don't contain windows we want to save in the Session.
10654 * Returns NULL when there none.
10655 */
10656 static frame_T *
10657ses_skipframe(fr)
10658 frame_T *fr;
10659{
10660 frame_T *frc;
10661
10662 for (frc = fr; frc != NULL; frc = frc->fr_next)
10663 if (ses_do_frame(frc))
10664 break;
10665 return frc;
10666}
10667
10668/*
10669 * Return TRUE if frame "fr" has a window somewhere that we want to save in
10670 * the Session.
10671 */
10672 static int
10673ses_do_frame(fr)
10674 frame_T *fr;
10675{
10676 frame_T *frc;
10677
10678 if (fr->fr_layout == FR_LEAF)
10679 return ses_do_win(fr->fr_win);
10680 for (frc = fr->fr_child; frc != NULL; frc = frc->fr_next)
10681 if (ses_do_frame(frc))
10682 return TRUE;
10683 return FALSE;
10684}
10685
10686/*
10687 * Return non-zero if window "wp" is to be stored in the Session.
10688 */
10689 static int
10690ses_do_win(wp)
10691 win_T *wp;
10692{
10693 if (wp->w_buffer->b_fname == NULL
10694#ifdef FEAT_QUICKFIX
10695 /* When 'buftype' is "nofile" can't restore the window contents. */
10696 || bt_nofile(wp->w_buffer)
10697#endif
10698 )
10699 return (ssop_flags & SSOP_BLANK);
10700 if (wp->w_buffer->b_help)
10701 return (ssop_flags & SSOP_HELP);
10702 return TRUE;
10703}
10704
10705/*
10706 * Write commands to "fd" to restore the view of a window.
10707 * Caller must make sure 'scrolloff' is zero.
10708 */
10709 static int
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010710put_view(fd, wp, add_edit, flagp, current_arg_idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010711 FILE *fd;
10712 win_T *wp;
10713 int add_edit; /* add ":edit" command to view */
10714 unsigned *flagp; /* vop_flags or ssop_flags */
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010715 int current_arg_idx; /* current argument index of the window, use
10716 * -1 if unknown */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010717{
10718 win_T *save_curwin;
10719 int f;
10720 int do_cursor;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010721 int did_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010722
10723 /* Always restore cursor position for ":mksession". For ":mkview" only
10724 * when 'viewoptions' contains "cursor". */
10725 do_cursor = (flagp == &ssop_flags || *flagp & SSOP_CURSOR);
10726
10727 /*
10728 * Local argument list.
10729 */
10730 if (wp->w_alist == &global_alist)
10731 {
10732 if (put_line(fd, "argglobal") == FAIL)
10733 return FAIL;
10734 }
10735 else
10736 {
10737 if (ses_arglist(fd, "arglocal", &wp->w_alist->al_ga,
10738 flagp == &vop_flags
10739 || !(*flagp & SSOP_CURDIR)
10740 || wp->w_localdir != NULL, flagp) == FAIL)
10741 return FAIL;
10742 }
10743
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010744 /* Only when part of a session: restore the argument index. Some
10745 * arguments may have been deleted, check if the index is valid. */
Bram Moolenaar51f53df2010-06-26 05:25:54 +020010746 if (wp->w_arg_idx != current_arg_idx && wp->w_arg_idx < WARGCOUNT(wp)
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010747 && flagp == &ssop_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010748 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010749 if (fprintf(fd, "%ldargu", (long)wp->w_arg_idx + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010750 || put_eol(fd) == FAIL)
10751 return FAIL;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010752 did_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010753 }
10754
10755 /* Edit the file. Skip this when ":next" already did it. */
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010756 if (add_edit && (!did_next || wp->w_arg_idx_invalid))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010757 {
10758 /*
10759 * Load the file.
10760 */
10761 if (wp->w_buffer->b_ffname != NULL
10762#ifdef FEAT_QUICKFIX
10763 && !bt_nofile(wp->w_buffer)
10764#endif
10765 )
10766 {
10767 /*
10768 * Editing a file in this buffer: use ":edit file".
10769 * This may have side effects! (e.g., compressed or network file).
10770 */
10771 if (fputs("edit ", fd) < 0
10772 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
10773 return FAIL;
10774 }
10775 else
10776 {
10777 /* No file in this buffer, just make it empty. */
10778 if (put_line(fd, "enew") == FAIL)
10779 return FAIL;
10780#ifdef FEAT_QUICKFIX
10781 if (wp->w_buffer->b_ffname != NULL)
10782 {
10783 /* The buffer does have a name, but it's not a file name. */
10784 if (fputs("file ", fd) < 0
10785 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
10786 return FAIL;
10787 }
10788#endif
10789 do_cursor = FALSE;
10790 }
10791 }
10792
10793 /*
10794 * Local mappings and abbreviations.
10795 */
10796 if ((*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
10797 && makemap(fd, wp->w_buffer) == FAIL)
10798 return FAIL;
10799
10800 /*
10801 * Local options. Need to go to the window temporarily.
10802 * Store only local values when using ":mkview" and when ":mksession" is
10803 * used and 'sessionoptions' doesn't include "options".
10804 * Some folding options are always stored when "folds" is included,
10805 * otherwise the folds would not be restored correctly.
10806 */
10807 save_curwin = curwin;
10808 curwin = wp;
10809 curbuf = curwin->w_buffer;
10810 if (*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
10811 f = makeset(fd, OPT_LOCAL,
10812 flagp == &vop_flags || !(*flagp & SSOP_OPTIONS));
10813#ifdef FEAT_FOLDING
10814 else if (*flagp & SSOP_FOLDS)
10815 f = makefoldset(fd);
10816#endif
10817 else
10818 f = OK;
10819 curwin = save_curwin;
10820 curbuf = curwin->w_buffer;
10821 if (f == FAIL)
10822 return FAIL;
10823
10824#ifdef FEAT_FOLDING
10825 /*
10826 * Save Folds when 'buftype' is empty and for help files.
10827 */
10828 if ((*flagp & SSOP_FOLDS)
10829 && wp->w_buffer->b_ffname != NULL
Bram Moolenaarb1b715d2006-01-21 22:09:43 +000010830# ifdef FEAT_QUICKFIX
10831 && (*wp->w_buffer->b_p_bt == NUL || wp->w_buffer->b_help)
10832# endif
10833 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000010834 {
10835 if (put_folds(fd, wp) == FAIL)
10836 return FAIL;
10837 }
10838#endif
10839
10840 /*
10841 * Set the cursor after creating folds, since that moves the cursor.
10842 */
10843 if (do_cursor)
10844 {
10845
10846 /* Restore the cursor line in the file and relatively in the
10847 * window. Don't use "G", it changes the jumplist. */
10848 if (fprintf(fd, "let s:l = %ld - ((%ld * winheight(0) + %ld) / %ld)",
10849 (long)wp->w_cursor.lnum,
10850 (long)(wp->w_cursor.lnum - wp->w_topline),
10851 (long)wp->w_height / 2, (long)wp->w_height) < 0
10852 || put_eol(fd) == FAIL
10853 || put_line(fd, "if s:l < 1 | let s:l = 1 | endif") == FAIL
10854 || put_line(fd, "exe s:l") == FAIL
10855 || put_line(fd, "normal! zt") == FAIL
10856 || fprintf(fd, "%ld", (long)wp->w_cursor.lnum) < 0
10857 || put_eol(fd) == FAIL)
10858 return FAIL;
10859 /* Restore the cursor column and left offset when not wrapping. */
10860 if (wp->w_cursor.col == 0)
10861 {
10862 if (put_line(fd, "normal! 0") == FAIL)
10863 return FAIL;
10864 }
10865 else
10866 {
10867 if (!wp->w_p_wrap && wp->w_leftcol > 0 && wp->w_width > 0)
10868 {
10869 if (fprintf(fd,
10870 "let s:c = %ld - ((%ld * winwidth(0) + %ld) / %ld)",
Bram Moolenaar558ddad2013-02-20 19:26:29 +010010871 (long)wp->w_virtcol + 1,
10872 (long)(wp->w_virtcol - wp->w_leftcol),
Bram Moolenaar071d4272004-06-13 20:20:40 +000010873 (long)wp->w_width / 2, (long)wp->w_width) < 0
10874 || put_eol(fd) == FAIL
10875 || put_line(fd, "if s:c > 0") == FAIL
10876 || fprintf(fd,
Bram Moolenaar558ddad2013-02-20 19:26:29 +010010877 " exe 'normal! ' . s:c . '|zs' . %ld . '|'",
10878 (long)wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010879 || put_eol(fd) == FAIL
10880 || put_line(fd, "else") == FAIL
Bram Moolenaarfdf447b2013-02-26 17:21:29 +010010881 || fprintf(fd, " normal! 0%d|", wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010882 || put_eol(fd) == FAIL
10883 || put_line(fd, "endif") == FAIL)
10884 return FAIL;
10885 }
10886 else
10887 {
Bram Moolenaar558ddad2013-02-20 19:26:29 +010010888 if (fprintf(fd, "normal! 0%d|", wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010889 || put_eol(fd) == FAIL)
10890 return FAIL;
10891 }
10892 }
10893 }
10894
10895 /*
10896 * Local directory.
10897 */
10898 if (wp->w_localdir != NULL)
10899 {
10900 if (fputs("lcd ", fd) < 0
10901 || ses_put_fname(fd, wp->w_localdir, flagp) == FAIL
10902 || put_eol(fd) == FAIL)
10903 return FAIL;
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010904 did_lcd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010905 }
10906
10907 return OK;
10908}
10909
10910/*
10911 * Write an argument list to the session file.
10912 * Returns FAIL if writing fails.
10913 */
10914 static int
10915ses_arglist(fd, cmd, gap, fullname, flagp)
10916 FILE *fd;
10917 char *cmd;
10918 garray_T *gap;
10919 int fullname; /* TRUE: use full path name */
10920 unsigned *flagp;
10921{
10922 int i;
Bram Moolenaard9462e32011-04-11 21:35:11 +020010923 char_u *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010924 char_u *s;
10925
10926 if (gap->ga_len == 0)
10927 return put_line(fd, "silent! argdel *");
10928 if (fputs(cmd, fd) < 0)
10929 return FAIL;
10930 for (i = 0; i < gap->ga_len; ++i)
10931 {
10932 /* NULL file names are skipped (only happens when out of memory). */
10933 s = alist_name(&((aentry_T *)gap->ga_data)[i]);
10934 if (s != NULL)
10935 {
10936 if (fullname)
10937 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020010938 buf = alloc(MAXPATHL);
10939 if (buf != NULL)
10940 {
10941 (void)vim_FullName(s, buf, MAXPATHL, FALSE);
10942 s = buf;
10943 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010944 }
10945 if (fputs(" ", fd) < 0 || ses_put_fname(fd, s, flagp) == FAIL)
Bram Moolenaard9462e32011-04-11 21:35:11 +020010946 {
10947 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010948 return FAIL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020010949 }
10950 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010951 }
10952 }
10953 return put_eol(fd);
10954}
10955
10956/*
10957 * Write a buffer name to the session file.
10958 * Also ends the line.
10959 * Returns FAIL if writing fails.
10960 */
10961 static int
10962ses_fname(fd, buf, flagp)
10963 FILE *fd;
10964 buf_T *buf;
10965 unsigned *flagp;
10966{
10967 char_u *name;
10968
10969 /* Use the short file name if the current directory is known at the time
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010970 * the session file will be sourced.
10971 * Don't do this for ":mkview", we don't know the current directory.
10972 * Don't do this after ":lcd", we don't keep track of what the current
10973 * directory is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010974 if (buf->b_sfname != NULL
10975 && flagp == &ssop_flags
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010976 && (ssop_flags & (SSOP_CURDIR | SSOP_SESDIR))
Bram Moolenaar8d594672009-07-01 18:18:57 +000010977#ifdef FEAT_AUTOCHDIR
10978 && !p_acd
10979#endif
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010980 && !did_lcd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010981 name = buf->b_sfname;
10982 else
10983 name = buf->b_ffname;
10984 if (ses_put_fname(fd, name, flagp) == FAIL || put_eol(fd) == FAIL)
10985 return FAIL;
10986 return OK;
10987}
10988
10989/*
10990 * Write a file name to the session file.
10991 * Takes care of the "slash" option in 'sessionoptions' and escapes special
10992 * characters.
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010993 * Returns FAIL if writing fails or out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010994 */
10995 static int
10996ses_put_fname(fd, name, flagp)
10997 FILE *fd;
10998 char_u *name;
10999 unsigned *flagp;
11000{
11001 char_u *sname;
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011002 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011003 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011004
11005 sname = home_replace_save(NULL, name);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011006 if (sname == NULL)
11007 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011008
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011009 if (*flagp & SSOP_SLASH)
11010 {
11011 /* change all backslashes to forward slashes */
11012 for (p = sname; *p != NUL; mb_ptr_adv(p))
11013 if (*p == '\\')
11014 *p = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +000011015 }
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011016
Bram Moolenaar84a05ac2013-05-06 04:24:17 +020011017 /* escape special characters */
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011018 p = vim_strsave_fnameescape(sname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011019 vim_free(sname);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011020 if (p == NULL)
11021 return FAIL;
11022
11023 /* write the result */
11024 if (fputs((char *)p, fd) < 0)
11025 retval = FAIL;
11026
11027 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011028 return retval;
11029}
11030
11031/*
11032 * ":loadview [nr]"
11033 */
11034 static void
11035ex_loadview(eap)
11036 exarg_T *eap;
11037{
11038 char_u *fname;
11039
11040 fname = get_view_file(*eap->arg);
11041 if (fname != NULL)
11042 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +000011043 do_source(fname, FALSE, DOSO_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011044 vim_free(fname);
11045 }
11046}
11047
11048/*
11049 * Get the name of the view file for the current buffer.
11050 */
11051 static char_u *
11052get_view_file(c)
11053 int c;
11054{
11055 int len = 0;
11056 char_u *p, *s;
11057 char_u *retval;
11058 char_u *sname;
11059
11060 if (curbuf->b_ffname == NULL)
11061 {
11062 EMSG(_(e_noname));
11063 return NULL;
11064 }
11065 sname = home_replace_save(NULL, curbuf->b_ffname);
11066 if (sname == NULL)
11067 return NULL;
11068
11069 /*
11070 * We want a file name without separators, because we're not going to make
11071 * a directory.
11072 * "normal" path separator -> "=+"
11073 * "=" -> "=="
11074 * ":" path separator -> "=-"
11075 */
11076 for (p = sname; *p; ++p)
11077 if (*p == '=' || vim_ispathsep(*p))
11078 ++len;
11079 retval = alloc((unsigned)(STRLEN(sname) + len + STRLEN(p_vdir) + 9));
11080 if (retval != NULL)
11081 {
11082 STRCPY(retval, p_vdir);
11083 add_pathsep(retval);
11084 s = retval + STRLEN(retval);
11085 for (p = sname; *p; ++p)
11086 {
11087 if (*p == '=')
11088 {
11089 *s++ = '=';
11090 *s++ = '=';
11091 }
11092 else if (vim_ispathsep(*p))
11093 {
11094 *s++ = '=';
Bram Moolenaare60acc12011-05-10 16:41:25 +020011095#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011096 if (*p == ':')
11097 *s++ = '-';
11098 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000011099#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000011100 *s++ = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +000011101 }
11102 else
11103 *s++ = *p;
11104 }
11105 *s++ = '=';
11106 *s++ = c;
11107 STRCPY(s, ".vim");
11108 }
11109
11110 vim_free(sname);
11111 return retval;
11112}
11113
11114#endif /* FEAT_SESSION */
11115
11116/*
11117 * Write end-of-line character(s) for ":mkexrc", ":mkvimrc" and ":mksession".
11118 * Return FAIL for a write error.
11119 */
11120 int
11121put_eol(fd)
11122 FILE *fd;
11123{
11124 if (
11125#ifdef USE_CRNL
11126 (
11127# ifdef MKSESSION_NL
11128 !mksession_nl &&
11129# endif
11130 (putc('\r', fd) < 0)) ||
11131#endif
11132 (putc('\n', fd) < 0))
11133 return FAIL;
11134 return OK;
11135}
11136
11137/*
11138 * Write a line to "fd".
11139 * Return FAIL for a write error.
11140 */
11141 int
11142put_line(fd, s)
11143 FILE *fd;
11144 char *s;
11145{
11146 if (fputs(s, fd) < 0 || put_eol(fd) == FAIL)
11147 return FAIL;
11148 return OK;
11149}
11150
11151#ifdef FEAT_VIMINFO
11152/*
11153 * ":rviminfo" and ":wviminfo".
11154 */
11155 static void
11156ex_viminfo(eap)
11157 exarg_T *eap;
11158{
11159 char_u *save_viminfo;
11160
11161 save_viminfo = p_viminfo;
11162 if (*p_viminfo == NUL)
11163 p_viminfo = (char_u *)"'100";
11164 if (eap->cmdidx == CMD_rviminfo)
11165 {
Bram Moolenaard812df62008-11-09 12:46:09 +000011166 if (read_viminfo(eap->arg, VIF_WANT_INFO | VIF_WANT_MARKS
11167 | (eap->forceit ? VIF_FORCEIT : 0)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011168 EMSG(_("E195: Cannot open viminfo file for reading"));
11169 }
11170 else
11171 write_viminfo(eap->arg, eap->forceit);
11172 p_viminfo = save_viminfo;
11173}
11174#endif
11175
11176#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +000011177/*
Bram Moolenaard9462e32011-04-11 21:35:11 +020011178 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +000011179 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +000011180 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011181 void
11182dialog_msg(buff, format, fname)
11183 char_u *buff;
11184 char *format;
11185 char_u *fname;
11186{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011187 if (fname == NULL)
11188 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +020011189 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011190}
11191#endif
11192
11193/*
11194 * ":behave {mswin,xterm}"
11195 */
11196 static void
11197ex_behave(eap)
11198 exarg_T *eap;
11199{
11200 if (STRCMP(eap->arg, "mswin") == 0)
11201 {
11202 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
11203 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
11204 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
11205 set_option_value((char_u *)"keymodel", 0L,
11206 (char_u *)"startsel,stopsel", 0);
11207 }
11208 else if (STRCMP(eap->arg, "xterm") == 0)
11209 {
11210 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
11211 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
11212 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
11213 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
11214 }
11215 else
11216 EMSG2(_(e_invarg2), eap->arg);
11217}
11218
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010011219#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
11220/*
11221 * Function given to ExpandGeneric() to obtain the possible arguments of the
11222 * ":behave {mswin,xterm}" command.
11223 */
11224 char_u *
11225get_behave_arg(xp, idx)
11226 expand_T *xp UNUSED;
11227 int idx;
11228{
11229 if (idx == 0)
11230 return (char_u *)"mswin";
11231 if (idx == 1)
11232 return (char_u *)"xterm";
11233 return NULL;
11234}
11235#endif
11236
Bram Moolenaar071d4272004-06-13 20:20:40 +000011237#ifdef FEAT_AUTOCMD
11238static int filetype_detect = FALSE;
11239static int filetype_plugin = FALSE;
11240static int filetype_indent = FALSE;
11241
11242/*
11243 * ":filetype [plugin] [indent] {on,off,detect}"
11244 * on: Load the filetype.vim file to install autocommands for file types.
11245 * off: Load the ftoff.vim file to remove all autocommands for file types.
11246 * plugin on: load filetype.vim and ftplugin.vim
11247 * plugin off: load ftplugof.vim
11248 * indent on: load filetype.vim and indent.vim
11249 * indent off: load indoff.vim
11250 */
11251 static void
11252ex_filetype(eap)
11253 exarg_T *eap;
11254{
11255 char_u *arg = eap->arg;
11256 int plugin = FALSE;
11257 int indent = FALSE;
11258
11259 if (*eap->arg == NUL)
11260 {
11261 /* Print current status. */
11262 smsg((char_u *)"filetype detection:%s plugin:%s indent:%s",
11263 filetype_detect ? "ON" : "OFF",
11264 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
11265 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
11266 return;
11267 }
11268
11269 /* Accept "plugin" and "indent" in any order. */
11270 for (;;)
11271 {
11272 if (STRNCMP(arg, "plugin", 6) == 0)
11273 {
11274 plugin = TRUE;
11275 arg = skipwhite(arg + 6);
11276 continue;
11277 }
11278 if (STRNCMP(arg, "indent", 6) == 0)
11279 {
11280 indent = TRUE;
11281 arg = skipwhite(arg + 6);
11282 continue;
11283 }
11284 break;
11285 }
11286 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
11287 {
11288 if (*arg == 'o' || !filetype_detect)
11289 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011290 source_runtime((char_u *)FILETYPE_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011291 filetype_detect = TRUE;
11292 if (plugin)
11293 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011294 source_runtime((char_u *)FTPLUGIN_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011295 filetype_plugin = TRUE;
11296 }
11297 if (indent)
11298 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011299 source_runtime((char_u *)INDENT_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011300 filetype_indent = TRUE;
11301 }
11302 }
11303 if (*arg == 'd')
11304 {
11305 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE);
Bram Moolenaara3227e22006-03-08 21:32:40 +000011306 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011307 }
11308 }
11309 else if (STRCMP(arg, "off") == 0)
11310 {
11311 if (plugin || indent)
11312 {
11313 if (plugin)
11314 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011315 source_runtime((char_u *)FTPLUGOF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011316 filetype_plugin = FALSE;
11317 }
11318 if (indent)
11319 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011320 source_runtime((char_u *)INDOFF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011321 filetype_indent = FALSE;
11322 }
11323 }
11324 else
11325 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011326 source_runtime((char_u *)FTOFF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011327 filetype_detect = FALSE;
11328 }
11329 }
11330 else
11331 EMSG2(_(e_invarg2), arg);
11332}
11333
11334/*
11335 * ":setfiletype {name}"
11336 */
11337 static void
11338ex_setfiletype(eap)
11339 exarg_T *eap;
11340{
11341 if (!did_filetype)
11342 set_option_value((char_u *)"filetype", 0L, eap->arg, OPT_LOCAL);
11343}
11344#endif
11345
Bram Moolenaar071d4272004-06-13 20:20:40 +000011346 static void
11347ex_digraphs(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +000011348 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011349{
11350#ifdef FEAT_DIGRAPHS
11351 if (*eap->arg != NUL)
11352 putdigraph(eap->arg);
11353 else
11354 listdigraphs();
11355#else
11356 EMSG(_("E196: No digraphs in this version"));
11357#endif
11358}
11359
11360 static void
11361ex_set(eap)
11362 exarg_T *eap;
11363{
11364 int flags = 0;
11365
11366 if (eap->cmdidx == CMD_setlocal)
11367 flags = OPT_LOCAL;
11368 else if (eap->cmdidx == CMD_setglobal)
11369 flags = OPT_GLOBAL;
11370#if defined(FEAT_EVAL) && defined(FEAT_AUTOCMD) && defined(FEAT_BROWSE)
11371 if (cmdmod.browse && flags == 0)
11372 ex_options(eap);
11373 else
11374#endif
11375 (void)do_set(eap->arg, flags);
11376}
11377
11378#ifdef FEAT_SEARCH_EXTRA
11379/*
11380 * ":nohlsearch"
11381 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011382 static void
11383ex_nohlsearch(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +000011384 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011385{
11386 no_hlsearch = TRUE;
Bram Moolenaarf71a3db2006-03-12 21:50:18 +000011387 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011388}
11389
11390/*
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011391 * ":[N]match {group} {pattern}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000011392 * Sets nextcmd to the start of the next command, if any. Also called when
11393 * skipping commands to find the next command.
11394 */
11395 static void
11396ex_match(eap)
11397 exarg_T *eap;
11398{
11399 char_u *p;
Bram Moolenaar52d36c82007-08-11 14:00:30 +000011400 char_u *g = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011401 char_u *end;
11402 int c;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011403 int id;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011404
11405 if (eap->line2 <= 3)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011406 id = eap->line2;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011407 else
11408 {
11409 EMSG(e_invcmd);
11410 return;
11411 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011412
11413 /* First clear any old pattern. */
11414 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011415 match_delete(curwin, id, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011416
11417 if (ends_excmd(*eap->arg))
11418 end = eap->arg;
11419 else if ((STRNICMP(eap->arg, "none", 4) == 0
11420 && (vim_iswhite(eap->arg[4]) || ends_excmd(eap->arg[4]))))
11421 end = eap->arg + 4;
11422 else
11423 {
11424 p = skiptowhite(eap->arg);
11425 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011426 g = vim_strnsave(eap->arg, (int)(p - eap->arg));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011427 p = skipwhite(p);
11428 if (*p == NUL)
11429 {
11430 /* There must be two arguments. */
11431 EMSG2(_(e_invarg2), eap->arg);
11432 return;
11433 }
11434 end = skip_regexp(p + 1, *p, TRUE, NULL);
11435 if (!eap->skip)
11436 {
11437 if (*end != NUL && !ends_excmd(*skipwhite(end + 1)))
11438 {
11439 eap->errmsg = e_trailing;
11440 return;
11441 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000011442 if (*end != *p)
11443 {
11444 EMSG2(_(e_invarg2), p);
11445 return;
11446 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011447
11448 c = *end;
11449 *end = NUL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011450 match_add(curwin, g, p + 1, 10, id);
11451 vim_free(g);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000011452 *end = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011453 }
11454 }
11455 eap->nextcmd = find_nextcmd(end);
11456}
11457#endif
11458
11459#ifdef FEAT_CRYPT
11460/*
11461 * ":X": Get crypt key
11462 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011463 static void
11464ex_X(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +000011465 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011466{
Bram Moolenaar49771f42010-07-20 17:32:38 +020011467 if (get_crypt_method(curbuf) == 0 || blowfish_self_test() == OK)
Bram Moolenaar40e6a712010-05-16 22:32:54 +020011468 (void)get_crypt_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011469}
11470#endif
11471
11472#ifdef FEAT_FOLDING
11473 static void
11474ex_fold(eap)
11475 exarg_T *eap;
11476{
11477 if (foldManualAllowed(TRUE))
11478 foldCreate(eap->line1, eap->line2);
11479}
11480
11481 static void
11482ex_foldopen(eap)
11483 exarg_T *eap;
11484{
11485 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
11486 eap->forceit, FALSE);
11487}
11488
11489 static void
11490ex_folddo(eap)
11491 exarg_T *eap;
11492{
11493 linenr_T lnum;
11494
11495 /* First set the marks for all lines closed/open. */
11496 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
11497 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
11498 ml_setmarked(lnum);
11499
11500 /* Execute the command on the marked lines. */
11501 global_exe(eap->arg);
11502 ml_clearmarked(); /* clear rest of the marks */
11503}
11504#endif