blob: 68269517374188a9be4aad25c86742638bf4ab82 [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));
319static void ex_submagic __ARGS((exarg_T *eap));
320static void ex_join __ARGS((exarg_T *eap));
321static void ex_at __ARGS((exarg_T *eap));
322static void ex_bang __ARGS((exarg_T *eap));
323static void ex_undo __ARGS((exarg_T *eap));
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200324#ifdef FEAT_PERSISTENT_UNDO
325static void ex_wundo __ARGS((exarg_T *eap));
326static void ex_rundo __ARGS((exarg_T *eap));
327#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000328static void ex_redo __ARGS((exarg_T *eap));
Bram Moolenaarc7d89352006-03-14 22:53:34 +0000329static void ex_later __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000330static void ex_redir __ARGS((exarg_T *eap));
331static void ex_redraw __ARGS((exarg_T *eap));
332static void ex_redrawstatus __ARGS((exarg_T *eap));
333static void close_redir __ARGS((void));
334static void ex_mkrc __ARGS((exarg_T *eap));
335static void ex_mark __ARGS((exarg_T *eap));
336#ifdef FEAT_USR_CMDS
337static char_u *uc_fun_cmd __ARGS((void));
Bram Moolenaar52b4b552005-03-07 23:00:57 +0000338static 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 +0000339#endif
340#ifdef FEAT_EX_EXTRA
341static void ex_normal __ARGS((exarg_T *eap));
342static void ex_startinsert __ARGS((exarg_T *eap));
343static void ex_stopinsert __ARGS((exarg_T *eap));
344#else
345# define ex_normal ex_ni
346# define ex_align ex_ni
347# define ex_retab ex_ni
348# define ex_startinsert ex_ni
349# define ex_stopinsert ex_ni
350# define ex_helptags ex_ni
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +0000351# define ex_sort ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000352#endif
353#ifdef FEAT_FIND_ID
354static void ex_checkpath __ARGS((exarg_T *eap));
355static void ex_findpat __ARGS((exarg_T *eap));
356#else
357# define ex_findpat ex_ni
358# define ex_checkpath ex_ni
359#endif
360#if defined(FEAT_FIND_ID) && defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
361static void ex_psearch __ARGS((exarg_T *eap));
362#else
363# define ex_psearch ex_ni
364#endif
365static void ex_tag __ARGS((exarg_T *eap));
366static void ex_tag_cmd __ARGS((exarg_T *eap, char_u *name));
367#ifndef FEAT_EVAL
368# define ex_scriptnames ex_ni
369# define ex_finish ex_ni
370# define ex_echo ex_ni
371# define ex_echohl ex_ni
372# define ex_execute ex_ni
373# define ex_call ex_ni
374# define ex_if ex_ni
375# define ex_endif ex_ni
376# define ex_else ex_ni
377# define ex_while ex_ni
378# define ex_continue ex_ni
379# define ex_break ex_ni
380# define ex_endwhile ex_ni
381# define ex_throw ex_ni
382# define ex_try ex_ni
383# define ex_catch ex_ni
384# define ex_finally ex_ni
385# define ex_endtry ex_ni
386# define ex_endfunction ex_ni
387# define ex_let ex_ni
388# define ex_unlet ex_ni
Bram Moolenaar65c1b012005-01-31 19:02:28 +0000389# define ex_lockvar ex_ni
390# define ex_unlockvar ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000391# define ex_function ex_ni
392# define ex_delfunction ex_ni
393# define ex_return ex_ni
Bram Moolenaard812df62008-11-09 12:46:09 +0000394# define ex_oldfiles ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000395#endif
396static char_u *arg_all __ARGS((void));
397#ifdef FEAT_SESSION
398static int makeopens __ARGS((FILE *fd, char_u *dirnow));
Bram Moolenaarf13be0d2008-01-02 14:13:10 +0000399static 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 +0000400static void ex_loadview __ARGS((exarg_T *eap));
401static char_u *get_view_file __ARGS((int c));
Bram Moolenaareeefcc72007-05-01 21:21:21 +0000402static int did_lcd; /* whether ":lcd" was produced for a session */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000403#else
404# define ex_loadview ex_ni
405#endif
406#ifndef FEAT_EVAL
407# define ex_compiler ex_ni
408#endif
409#ifdef FEAT_VIMINFO
410static void ex_viminfo __ARGS((exarg_T *eap));
411#else
412# define ex_viminfo ex_ni
413#endif
414static void ex_behave __ARGS((exarg_T *eap));
415#ifdef FEAT_AUTOCMD
416static void ex_filetype __ARGS((exarg_T *eap));
417static void ex_setfiletype __ARGS((exarg_T *eap));
418#else
419# define ex_filetype ex_ni
420# define ex_setfiletype ex_ni
421#endif
422#ifndef FEAT_DIFF
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000423# define ex_diffoff ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000424# define ex_diffpatch ex_ni
425# define ex_diffgetput ex_ni
426# define ex_diffsplit ex_ni
427# define ex_diffthis ex_ni
428# define ex_diffupdate ex_ni
429#endif
430static void ex_digraphs __ARGS((exarg_T *eap));
431static void ex_set __ARGS((exarg_T *eap));
432#if !defined(FEAT_EVAL) || !defined(FEAT_AUTOCMD)
433# define ex_options ex_ni
434#endif
435#ifdef FEAT_SEARCH_EXTRA
436static void ex_nohlsearch __ARGS((exarg_T *eap));
437static void ex_match __ARGS((exarg_T *eap));
438#else
439# define ex_nohlsearch ex_ni
440# define ex_match ex_ni
441#endif
442#ifdef FEAT_CRYPT
443static void ex_X __ARGS((exarg_T *eap));
444#else
445# define ex_X ex_ni
446#endif
447#ifdef FEAT_FOLDING
448static void ex_fold __ARGS((exarg_T *eap));
449static void ex_foldopen __ARGS((exarg_T *eap));
450static void ex_folddo __ARGS((exarg_T *eap));
451#else
452# define ex_fold ex_ni
453# define ex_foldopen ex_ni
454# define ex_folddo ex_ni
455#endif
456#if !((defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
457 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE)))
458# define ex_language ex_ni
459#endif
460#ifndef FEAT_SIGNS
461# define ex_sign ex_ni
462#endif
463#ifndef FEAT_SUN_WORKSHOP
464# define ex_wsverb ex_ni
465#endif
Bram Moolenaar009b2592004-10-24 19:18:58 +0000466#ifndef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200467# define ex_nbclose ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000468# define ex_nbkey ex_ni
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200469# define ex_nbstart ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000470#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000471
472#ifndef FEAT_EVAL
473# define ex_debug ex_ni
474# define ex_breakadd ex_ni
475# define ex_debuggreedy ex_ni
476# define ex_breakdel ex_ni
477# define ex_breaklist ex_ni
478#endif
479
480#ifndef FEAT_CMDHIST
481# define ex_history ex_ni
482#endif
483#ifndef FEAT_JUMPLIST
484# define ex_jumps ex_ni
485# define ex_changes ex_ni
486#endif
487
Bram Moolenaar05159a02005-02-26 23:04:13 +0000488#ifndef FEAT_PROFILE
489# define ex_profile ex_ni
490#endif
491
Bram Moolenaar071d4272004-06-13 20:20:40 +0000492/*
493 * Declare cmdnames[].
494 */
495#define DO_DECLARE_EXCMD
496#include "ex_cmds.h"
497
498/*
499 * Table used to quickly search for a command, based on its first character.
500 */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +0000501static cmdidx_T cmdidxs[27] =
Bram Moolenaar071d4272004-06-13 20:20:40 +0000502{
503 CMD_append,
504 CMD_buffer,
505 CMD_change,
506 CMD_delete,
507 CMD_edit,
508 CMD_file,
509 CMD_global,
510 CMD_help,
511 CMD_insert,
512 CMD_join,
513 CMD_k,
514 CMD_list,
515 CMD_move,
516 CMD_next,
517 CMD_open,
518 CMD_print,
519 CMD_quit,
520 CMD_read,
521 CMD_substitute,
522 CMD_t,
523 CMD_undo,
524 CMD_vglobal,
525 CMD_write,
526 CMD_xit,
527 CMD_yank,
528 CMD_z,
529 CMD_bang
530};
531
532static char_u dollar_command[2] = {'$', 0};
533
534
535#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000536/* Struct for storing a line inside a while/for loop */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000537typedef struct
538{
539 char_u *line; /* command line */
540 linenr_T lnum; /* sourcing_lnum of the line */
541} wcmd_T;
542
543/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000544 * Structure used to store info for line position in a while or for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545 * This is required, because do_one_cmd() may invoke ex_function(), which
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000546 * reads more lines that may come from the while/for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000547 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000548struct loop_cookie
Bram Moolenaar071d4272004-06-13 20:20:40 +0000549{
550 garray_T *lines_gap; /* growarray with line info */
551 int current_line; /* last read line from growarray */
552 int repeating; /* TRUE when looping a second time */
553 /* When "repeating" is FALSE use "getline" and "cookie" to get lines */
554 char_u *(*getline) __ARGS((int, void *, int));
555 void *cookie;
556};
557
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000558static char_u *get_loop_line __ARGS((int c, void *cookie, int indent));
559static int store_loop_line __ARGS((garray_T *gap, char_u *line));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000560static void free_cmdlines __ARGS((garray_T *gap));
Bram Moolenaared203462004-06-16 11:19:22 +0000561
562/* Struct to save a few things while debugging. Used in do_cmdline() only. */
563struct dbg_stuff
564{
565 int trylevel;
566 int force_abort;
567 except_T *caught_stack;
568 char_u *vv_exception;
569 char_u *vv_throwpoint;
570 int did_emsg;
571 int got_int;
572 int did_throw;
573 int need_rethrow;
574 int check_cstack;
575 except_T *current_exception;
576};
577
578static void save_dbg_stuff __ARGS((struct dbg_stuff *dsp));
579static void restore_dbg_stuff __ARGS((struct dbg_stuff *dsp));
580
581 static void
582save_dbg_stuff(dsp)
583 struct dbg_stuff *dsp;
584{
585 dsp->trylevel = trylevel; trylevel = 0;
586 dsp->force_abort = force_abort; force_abort = FALSE;
587 dsp->caught_stack = caught_stack; caught_stack = NULL;
588 dsp->vv_exception = v_exception(NULL);
589 dsp->vv_throwpoint = v_throwpoint(NULL);
590
591 /* Necessary for debugging an inactive ":catch", ":finally", ":endtry" */
592 dsp->did_emsg = did_emsg; did_emsg = FALSE;
593 dsp->got_int = got_int; got_int = FALSE;
594 dsp->did_throw = did_throw; did_throw = FALSE;
595 dsp->need_rethrow = need_rethrow; need_rethrow = FALSE;
596 dsp->check_cstack = check_cstack; check_cstack = FALSE;
597 dsp->current_exception = current_exception; current_exception = NULL;
598}
599
600 static void
601restore_dbg_stuff(dsp)
602 struct dbg_stuff *dsp;
603{
604 suppress_errthrow = FALSE;
605 trylevel = dsp->trylevel;
606 force_abort = dsp->force_abort;
607 caught_stack = dsp->caught_stack;
608 (void)v_exception(dsp->vv_exception);
609 (void)v_throwpoint(dsp->vv_throwpoint);
610 did_emsg = dsp->did_emsg;
611 got_int = dsp->got_int;
612 did_throw = dsp->did_throw;
613 need_rethrow = dsp->need_rethrow;
614 check_cstack = dsp->check_cstack;
615 current_exception = dsp->current_exception;
616}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000617#endif
618
619
620/*
621 * do_exmode(): Repeatedly get commands for the "Ex" mode, until the ":vi"
622 * command is given.
623 */
624 void
625do_exmode(improved)
626 int improved; /* TRUE for "improved Ex" mode */
627{
628 int save_msg_scroll;
629 int prev_msg_row;
630 linenr_T prev_line;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000631 int changedtick;
632
633 if (improved)
634 exmode_active = EXMODE_VIM;
635 else
636 exmode_active = EXMODE_NORMAL;
637 State = NORMAL;
638
639 /* When using ":global /pat/ visual" and then "Q" we return to continue
640 * the :global command. */
641 if (global_busy)
642 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000643
644 save_msg_scroll = msg_scroll;
645 ++RedrawingDisabled; /* don't redisplay the window */
646 ++no_wait_return; /* don't wait for return */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000647#ifdef FEAT_GUI
648 /* Ignore scrollbar and mouse events in Ex mode */
649 ++hold_gui_events;
650#endif
651#ifdef FEAT_SNIFF
652 want_sniff_request = 0; /* No K_SNIFF wanted */
653#endif
654
655 MSG(_("Entering Ex mode. Type \"visual\" to go to Normal mode."));
656 while (exmode_active)
657 {
Bram Moolenaar7c626922005-02-07 22:01:03 +0000658#ifdef FEAT_EX_EXTRA
659 /* Check for a ":normal" command and no more characters left. */
660 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
661 {
662 exmode_active = FALSE;
663 break;
664 }
665#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000666 msg_scroll = TRUE;
667 need_wait_return = FALSE;
668 ex_pressedreturn = FALSE;
669 ex_no_reprint = FALSE;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000670 changedtick = curbuf->b_changedtick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000671 prev_msg_row = msg_row;
672 prev_line = curwin->w_cursor.lnum;
673#ifdef FEAT_SNIFF
674 ProcessSniffRequests();
675#endif
676 if (improved)
677 {
678 cmdline_row = msg_row;
679 do_cmdline(NULL, getexline, NULL, 0);
680 }
681 else
682 do_cmdline(NULL, getexmodeline, NULL, DOCMD_NOWAIT);
683 lines_left = Rows - 1;
684
Bram Moolenaardf177f62005-02-22 08:39:57 +0000685 if ((prev_line != curwin->w_cursor.lnum
686 || changedtick != curbuf->b_changedtick) && !ex_no_reprint)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000687 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000688 if (curbuf->b_ml.ml_flags & ML_EMPTY)
689 EMSG(_(e_emptybuf));
690 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000692 if (ex_pressedreturn)
693 {
694 /* go up one line, to overwrite the ":<CR>" line, so the
Bram Moolenaar81870892007-11-11 18:17:28 +0000695 * output doesn't contain empty lines. */
Bram Moolenaardf177f62005-02-22 08:39:57 +0000696 msg_row = prev_msg_row;
697 if (prev_msg_row == Rows - 1)
698 msg_row--;
699 }
700 msg_col = 0;
701 print_line_no_prefix(curwin->w_cursor.lnum, FALSE, FALSE);
702 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000703 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704 }
Bram Moolenaardf177f62005-02-22 08:39:57 +0000705 else if (ex_pressedreturn && !ex_no_reprint) /* must be at EOF */
706 {
707 if (curbuf->b_ml.ml_flags & ML_EMPTY)
708 EMSG(_(e_emptybuf));
709 else
710 EMSG(_("E501: At end-of-file"));
711 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000712 }
713
714#ifdef FEAT_GUI
715 --hold_gui_events;
716#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000717 --RedrawingDisabled;
718 --no_wait_return;
719 update_screen(CLEAR);
720 need_wait_return = FALSE;
721 msg_scroll = save_msg_scroll;
722}
723
724/*
725 * Execute a simple command line. Used for translated commands like "*".
726 */
727 int
728do_cmdline_cmd(cmd)
729 char_u *cmd;
730{
731 return do_cmdline(cmd, NULL, NULL,
732 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
733}
734
735/*
736 * do_cmdline(): execute one Ex command line
737 *
738 * 1. Execute "cmdline" when it is not NULL.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100739 * If "cmdline" is NULL, or more lines are needed, fgetline() is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000740 * 2. Split up in parts separated with '|'.
741 *
742 * This function can be called recursively!
743 *
744 * flags:
745 * DOCMD_VERBOSE - The command will be included in the error message.
746 * DOCMD_NOWAIT - Don't call wait_return() and friends.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100747 * DOCMD_REPEAT - Repeat execution until fgetline() returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000748 * DOCMD_KEYTYPED - Don't reset KeyTyped.
749 * DOCMD_EXCRESET - Reset the exception environment (used for debugging).
750 * DOCMD_KEEPLINE - Store first typed line (for repeating with ".").
751 *
752 * return FAIL if cmdline could not be executed, OK otherwise
753 */
754 int
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100755do_cmdline(cmdline, fgetline, cookie, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000756 char_u *cmdline;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100757 char_u *(*fgetline) __ARGS((int, void *, int));
758 void *cookie; /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000759 int flags;
760{
761 char_u *next_cmdline; /* next cmd to execute */
762 char_u *cmdline_copy = NULL; /* copy of cmd line */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100763 int used_getline = FALSE; /* used "fgetline" to obtain command */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000764 static int recursive = 0; /* recursive depth */
765 int msg_didout_before_start = 0;
766 int count = 0; /* line number count */
767 int did_inc = FALSE; /* incremented RedrawingDisabled */
768 int retval = OK;
769#ifdef FEAT_EVAL
770 struct condstack cstack; /* conditional stack */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000771 garray_T lines_ga; /* keep lines for ":while"/":for" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772 int current_line = 0; /* active line in lines_ga */
773 char_u *fname = NULL; /* function or script name */
774 linenr_T *breakpoint = NULL; /* ptr to breakpoint field in cookie */
775 int *dbg_tick = NULL; /* ptr to dbg_tick field in cookie */
Bram Moolenaared203462004-06-16 11:19:22 +0000776 struct dbg_stuff debug_saved; /* saved things for debug mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000777 int initial_trylevel;
778 struct msglist **saved_msg_list = NULL;
779 struct msglist *private_msg_list;
780
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100781 /* "fgetline" and "cookie" passed to do_one_cmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782 char_u *(*cmd_getline) __ARGS((int, void *, int));
783 void *cmd_cookie;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000784 struct loop_cookie cmd_loop_cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785 void *real_cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000786 int getline_is_func;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787#else
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100788# define cmd_getline fgetline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000789# define cmd_cookie cookie
790#endif
791 static int call_depth = 0; /* recursiveness */
792
793#ifdef FEAT_EVAL
794 /* For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory
795 * location for storing error messages to be converted to an exception.
Bram Moolenaarcf3630f2005-01-08 16:04:29 +0000796 * This ensures that the do_errthrow() call in do_one_cmd() does not
797 * combine the messages stored by an earlier invocation of do_one_cmd()
798 * with the command name of the later one. This would happen when
799 * BufWritePost autocommands are executed after a write error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800 saved_msg_list = msg_list;
801 msg_list = &private_msg_list;
802 private_msg_list = NULL;
803#endif
804
805 /* It's possible to create an endless loop with ":execute", catch that
806 * here. The value of 200 allows nested function calls, ":source", etc. */
807 if (call_depth == 200)
808 {
809 EMSG(_("E169: Command too recursive"));
810#ifdef FEAT_EVAL
811 /* When converting to an exception, we do not include the command name
812 * since this is not an error of the specific command. */
813 do_errthrow((struct condstack *)NULL, (char_u *)NULL);
814 msg_list = saved_msg_list;
815#endif
816 return FAIL;
817 }
818 ++call_depth;
819
820#ifdef FEAT_EVAL
821 cstack.cs_idx = -1;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000822 cstack.cs_looplevel = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823 cstack.cs_trylevel = 0;
824 cstack.cs_emsg_silent_list = NULL;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000825 cstack.cs_lflags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826 ga_init2(&lines_ga, (int)sizeof(wcmd_T), 10);
827
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100828 real_cookie = getline_cookie(fgetline, cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000829
830 /* Inside a function use a higher nesting level. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100831 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000832 if (getline_is_func && ex_nesting_level == func_level(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000833 ++ex_nesting_level;
834
835 /* Get the function or script name and the address where the next breakpoint
836 * line and the debug tick for a function or script are stored. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000837 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000838 {
839 fname = func_name(real_cookie);
840 breakpoint = func_breakpoint(real_cookie);
841 dbg_tick = func_dbg_tick(real_cookie);
842 }
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100843 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000844 {
845 fname = sourcing_name;
846 breakpoint = source_breakpoint(real_cookie);
847 dbg_tick = source_dbg_tick(real_cookie);
848 }
849
850 /*
851 * Initialize "force_abort" and "suppress_errthrow" at the top level.
852 */
853 if (!recursive)
854 {
855 force_abort = FALSE;
856 suppress_errthrow = FALSE;
857 }
858
859 /*
860 * If requested, store and reset the global values controlling the
Bram Moolenaar89d40322006-08-29 15:30:07 +0000861 * exception handling (used when debugging). Otherwise clear it to avoid
862 * a bogus compiler warning when the optimizer uses inline functions...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000863 */
Bram Moolenaarb4872942006-05-13 10:32:52 +0000864 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +0000865 save_dbg_stuff(&debug_saved);
Bram Moolenaar89d40322006-08-29 15:30:07 +0000866 else
Bram Moolenaar7db5fc82010-05-24 11:59:29 +0200867 vim_memset(&debug_saved, 0, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000868
869 initial_trylevel = trylevel;
870
871 /*
872 * "did_throw" will be set to TRUE when an exception is being thrown.
873 */
874 did_throw = FALSE;
875#endif
876 /*
877 * "did_emsg" will be set to TRUE when emsg() is used, in which case we
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000878 * cancel the whole command line, and any if/endif or loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879 * If force_abort is set, we cancel everything.
880 */
881 did_emsg = FALSE;
882
883 /*
884 * KeyTyped is only set when calling vgetc(). Reset it here when not
885 * calling vgetc() (sourced command lines).
886 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100887 if (!(flags & DOCMD_KEYTYPED)
888 && !getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889 KeyTyped = FALSE;
890
891 /*
892 * Continue executing command lines:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000893 * - when inside an ":if", ":while" or ":for"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894 * - for multiple commands on one line, separated with '|'
895 * - when repeating until there are no more lines (for ":source")
896 */
897 next_cmdline = cmdline;
898 do
899 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000900#ifdef FEAT_EVAL
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100901 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000902#endif
903
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000904 /* stop skipping cmds for an error msg after all endif/while/for */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000905 if (next_cmdline == NULL
906#ifdef FEAT_EVAL
907 && !force_abort
908 && cstack.cs_idx < 0
Bram Moolenaar05159a02005-02-26 23:04:13 +0000909 && !(getline_is_func && func_has_abort(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000910#endif
911 )
912 did_emsg = FALSE;
913
914 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000915 * 1. If repeating a line in a loop, get a line from lines_ga.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100916 * 2. If no line given: Get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000917 * 3. If a line is given: Make a copy, so we can mess with it.
918 */
919
920#ifdef FEAT_EVAL
921 /* 1. If repeating, get a previous line from lines_ga. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000922 if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000923 {
924 /* Each '|' separated command is stored separately in lines_ga, to
925 * be able to jump to it. Don't use next_cmdline now. */
926 vim_free(cmdline_copy);
927 cmdline_copy = NULL;
928
929 /* Check if a function has returned or, unless it has an unclosed
930 * try conditional, aborted. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000931 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000932 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000933# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000934 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000935 func_line_end(real_cookie);
936# endif
937 if (func_has_ended(real_cookie))
938 {
939 retval = FAIL;
940 break;
941 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000942 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000943#ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000944 else if (do_profiling == PROF_YES
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100945 && getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000946 script_line_end();
947#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000948
949 /* Check if a sourced file hit a ":finish" command. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100950 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000951 {
952 retval = FAIL;
953 break;
954 }
955
956 /* If breakpoints have been added/deleted need to check for it. */
957 if (breakpoint != NULL && dbg_tick != NULL
958 && *dbg_tick != debug_tick)
959 {
960 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100961 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962 fname, sourcing_lnum);
963 *dbg_tick = debug_tick;
964 }
965
966 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
967 sourcing_lnum = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
968
969 /* Did we encounter a breakpoint? */
970 if (breakpoint != NULL && *breakpoint != 0
971 && *breakpoint <= sourcing_lnum)
972 {
973 dbg_breakpoint(fname, sourcing_lnum);
974 /* Find next breakpoint. */
975 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100976 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977 fname, sourcing_lnum);
978 *dbg_tick = debug_tick;
979 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000980# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000981 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000982 {
983 if (getline_is_func)
984 func_line_start(real_cookie);
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100985 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000986 script_line_start();
987 }
988# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989 }
990
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000991 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000993 /* Inside a while/for loop we need to store the lines and use them
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100994 * again. Pass a different "fgetline" function to do_one_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995 * below, so that it stores lines in or reads them from
996 * "lines_ga". Makes it possible to define a function inside a
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000997 * while/for loop. */
998 cmd_getline = get_loop_line;
999 cmd_cookie = (void *)&cmd_loop_cookie;
1000 cmd_loop_cookie.lines_gap = &lines_ga;
1001 cmd_loop_cookie.current_line = current_line;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001002 cmd_loop_cookie.getline = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001003 cmd_loop_cookie.cookie = cookie;
1004 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005 }
1006 else
1007 {
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001008 cmd_getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009 cmd_cookie = cookie;
1010 }
1011#endif
1012
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001013 /* 2. If no line given, get an allocated line with fgetline(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014 if (next_cmdline == NULL)
1015 {
1016 /*
1017 * Need to set msg_didout for the first line after an ":if",
1018 * otherwise the ":if" will be overwritten.
1019 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001020 if (count == 1 && getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021 msg_didout = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001022 if (fgetline == NULL || (next_cmdline = fgetline(':', cookie,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023#ifdef FEAT_EVAL
1024 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
1025#else
1026 0
1027#endif
1028 )) == NULL)
1029 {
1030 /* Don't call wait_return for aborted command line. The NULL
1031 * returned for the end of a sourced file or executed function
1032 * doesn't do this. */
1033 if (KeyTyped && !(flags & DOCMD_REPEAT))
1034 need_wait_return = FALSE;
1035 retval = FAIL;
1036 break;
1037 }
1038 used_getline = TRUE;
1039
1040 /*
1041 * Keep the first typed line. Clear it when more lines are typed.
1042 */
1043 if (flags & DOCMD_KEEPLINE)
1044 {
1045 vim_free(repeat_cmdline);
1046 if (count == 0)
1047 repeat_cmdline = vim_strsave(next_cmdline);
1048 else
1049 repeat_cmdline = NULL;
1050 }
1051 }
1052
1053 /* 3. Make a copy of the command so we can mess with it. */
1054 else if (cmdline_copy == NULL)
1055 {
1056 next_cmdline = vim_strsave(next_cmdline);
1057 if (next_cmdline == NULL)
1058 {
1059 EMSG(_(e_outofmem));
1060 retval = FAIL;
1061 break;
1062 }
1063 }
1064 cmdline_copy = next_cmdline;
1065
1066#ifdef FEAT_EVAL
1067 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001068 * Save the current line when inside a ":while" or ":for", and when
1069 * the command looks like a ":while" or ":for", because we may need it
1070 * later. When there is a '|' and another command, it is stored
1071 * separately, because we need to be able to jump back to it from an
1072 * :endwhile/:endfor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001073 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001074 if (current_line == lines_ga.ga_len
1075 && (cstack.cs_looplevel || has_loop_cmd(next_cmdline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001076 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001077 if (store_loop_line(&lines_ga, next_cmdline) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078 {
1079 retval = FAIL;
1080 break;
1081 }
1082 }
1083 did_endif = FALSE;
1084#endif
1085
1086 if (count++ == 0)
1087 {
1088 /*
1089 * All output from the commands is put below each other, without
1090 * waiting for a return. Don't do this when executing commands
1091 * from a script or when being called recursive (e.g. for ":e
1092 * +command file").
1093 */
1094 if (!(flags & DOCMD_NOWAIT) && !recursive)
1095 {
1096 msg_didout_before_start = msg_didout;
1097 msg_didany = FALSE; /* no output yet */
1098 msg_start();
1099 msg_scroll = TRUE; /* put messages below each other */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001100 ++no_wait_return; /* don't wait for return until finished */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101 ++RedrawingDisabled;
1102 did_inc = TRUE;
1103 }
1104 }
1105
1106 if (p_verbose >= 15 && sourcing_name != NULL)
1107 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001108 ++no_wait_return;
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001109 verbose_enter_scroll();
1110
Bram Moolenaar071d4272004-06-13 20:20:40 +00001111 smsg((char_u *)_("line %ld: %s"),
1112 (long)sourcing_lnum, cmdline_copy);
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001113 if (msg_silent == 0)
1114 msg_puts((char_u *)"\n"); /* don't overwrite this */
1115
1116 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001117 --no_wait_return;
1118 }
1119
1120 /*
1121 * 2. Execute one '|' separated command.
1122 * do_one_cmd() will return NULL if there is no trailing '|'.
1123 * "cmdline_copy" can change, e.g. for '%' and '#' expansion.
1124 */
1125 ++recursive;
1126 next_cmdline = do_one_cmd(&cmdline_copy, flags & DOCMD_VERBOSE,
1127#ifdef FEAT_EVAL
1128 &cstack,
1129#endif
1130 cmd_getline, cmd_cookie);
1131 --recursive;
1132
1133#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001134 if (cmd_cookie == (void *)&cmd_loop_cookie)
1135 /* Use "current_line" from "cmd_loop_cookie", it may have been
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136 * incremented when defining a function. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001137 current_line = cmd_loop_cookie.current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001138#endif
1139
1140 if (next_cmdline == NULL)
1141 {
1142 vim_free(cmdline_copy);
1143 cmdline_copy = NULL;
1144#ifdef FEAT_CMDHIST
1145 /*
1146 * If the command was typed, remember it for the ':' register.
1147 * Do this AFTER executing the command to make :@: work.
1148 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001149 if (getline_equal(fgetline, cookie, getexline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150 && new_last_cmdline != NULL)
1151 {
1152 vim_free(last_cmdline);
1153 last_cmdline = new_last_cmdline;
1154 new_last_cmdline = NULL;
1155 }
1156#endif
1157 }
1158 else
1159 {
1160 /* need to copy the command after the '|' to cmdline_copy, for the
1161 * next do_one_cmd() */
Bram Moolenaara7241f52008-06-24 20:39:31 +00001162 STRMOVE(cmdline_copy, next_cmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001163 next_cmdline = cmdline_copy;
1164 }
1165
1166
1167#ifdef FEAT_EVAL
1168 /* reset did_emsg for a function that is not aborted by an error */
1169 if (did_emsg && !force_abort
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001170 && getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171 && !func_has_abort(real_cookie))
1172 did_emsg = FALSE;
1173
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001174 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175 {
1176 ++current_line;
1177
1178 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001179 * An ":endwhile", ":endfor" and ":continue" is handled here.
1180 * If we were executing commands, jump back to the ":while" or
1181 * ":for".
1182 * If we were not executing commands, decrement cs_looplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001184 if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001186 cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001187
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001188 /* Jump back to the matching ":while" or ":for". Be careful
1189 * not to use a cs_line[] from an entry that isn't a ":while"
1190 * or ":for": It would make "current_line" invalid and can
1191 * cause a crash. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192 if (!did_emsg && !got_int && !did_throw
1193 && cstack.cs_idx >= 0
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001194 && (cstack.cs_flags[cstack.cs_idx]
1195 & (CSF_WHILE | CSF_FOR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001196 && cstack.cs_line[cstack.cs_idx] >= 0
1197 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
1198 {
1199 current_line = cstack.cs_line[cstack.cs_idx];
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001200 /* remember we jumped there */
1201 cstack.cs_lflags |= CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202 line_breakcheck(); /* check if CTRL-C typed */
1203
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001204 /* Check for the next breakpoint at or after the ":while"
1205 * or ":for". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206 if (breakpoint != NULL)
1207 {
1208 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001209 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210 fname,
1211 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
1212 *dbg_tick = debug_tick;
1213 }
1214 }
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001215 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001217 /* can only get here with ":endwhile" or ":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218 if (cstack.cs_idx >= 0)
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001219 rewind_conditionals(&cstack, cstack.cs_idx - 1,
1220 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221 }
1222 }
1223
1224 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001225 * For a ":while" or ":for" we need to remember the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001227 else if (cstack.cs_lflags & CSL_HAD_LOOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001228 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001229 cstack.cs_lflags &= ~CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001230 cstack.cs_line[cstack.cs_idx] = current_line - 1;
1231 }
1232 }
1233
1234 /*
1235 * When not inside any ":while" loop, clear remembered lines.
1236 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001237 if (cstack.cs_looplevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001238 {
1239 if (lines_ga.ga_len > 0)
1240 {
1241 sourcing_lnum =
1242 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
1243 free_cmdlines(&lines_ga);
1244 }
1245 current_line = 0;
1246 }
1247
1248 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001249 * A ":finally" makes did_emsg, got_int, and did_throw pending for
1250 * being restored at the ":endtry". Reset them here and set the
1251 * ACTIVE and FINALLY flags, so that the finally clause gets executed.
1252 * This includes the case where a missing ":endif", ":endwhile" or
1253 * ":endfor" was detected by the ":finally" itself.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001255 if (cstack.cs_lflags & CSL_HAD_FINA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001257 cstack.cs_lflags &= ~CSL_HAD_FINA;
1258 report_make_pending(cstack.cs_pending[cstack.cs_idx]
1259 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001260 did_throw ? (void *)current_exception : NULL);
1261 did_emsg = got_int = did_throw = FALSE;
1262 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
1263 }
1264
1265 /* Update global "trylevel" for recursive calls to do_cmdline() from
1266 * within this loop. */
1267 trylevel = initial_trylevel + cstack.cs_trylevel;
1268
1269 /*
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001270 * If the outermost try conditional (across function calls and sourced
Bram Moolenaar071d4272004-06-13 20:20:40 +00001271 * files) is aborted because of an error, an interrupt, or an uncaught
1272 * exception, cancel everything. If it is left normally, reset
1273 * force_abort to get the non-EH compatible abortion behavior for
1274 * the rest of the script.
1275 */
1276 if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
1277 force_abort = FALSE;
1278
1279 /* Convert an interrupt to an exception if appropriate. */
1280 (void)do_intthrow(&cstack);
1281#endif /* FEAT_EVAL */
1282
1283 }
1284 /*
1285 * Continue executing command lines when:
1286 * - no CTRL-C typed, no aborting error, no exception thrown or try
1287 * conditionals need to be checked for executing finally clauses or
1288 * catching an interrupt exception
1289 * - didn't get an error message or lines are not typed
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001290 * - there is a command after '|', inside a :if, :while, :for or :try, or
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291 * looping for ":source" command or function call.
1292 */
1293 while (!((got_int
1294#ifdef FEAT_EVAL
1295 || (did_emsg && force_abort) || did_throw
1296#endif
1297 )
1298#ifdef FEAT_EVAL
1299 && cstack.cs_trylevel == 0
1300#endif
1301 )
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001302 && !(did_emsg
1303#ifdef FEAT_EVAL
1304 /* Keep going when inside try/catch, so that the error can be
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001305 * deal with, except when it is a syntax error, it may cause
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001306 * the :endtry to be missed. */
1307 && (cstack.cs_trylevel == 0 || did_emsg_syntax)
1308#endif
1309 && used_getline
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001310 && (getline_equal(fgetline, cookie, getexmodeline)
1311 || getline_equal(fgetline, cookie, getexline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312 && (next_cmdline != NULL
1313#ifdef FEAT_EVAL
1314 || cstack.cs_idx >= 0
1315#endif
1316 || (flags & DOCMD_REPEAT)));
1317
1318 vim_free(cmdline_copy);
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001319 did_emsg_syntax = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001320#ifdef FEAT_EVAL
1321 free_cmdlines(&lines_ga);
1322 ga_clear(&lines_ga);
1323
1324 if (cstack.cs_idx >= 0)
1325 {
1326 /*
1327 * If a sourced file or executed function ran to its end, report the
1328 * unclosed conditional.
1329 */
1330 if (!got_int && !did_throw
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001331 && ((getline_equal(fgetline, cookie, getsourceline)
1332 && !source_finished(fgetline, cookie))
1333 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001334 && !func_has_ended(real_cookie))))
1335 {
1336 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
1337 EMSG(_(e_endtry));
1338 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
1339 EMSG(_(e_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001340 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
1341 EMSG(_(e_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342 else
1343 EMSG(_(e_endif));
1344 }
1345
1346 /*
1347 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1348 * ":endtry" in a sourced file or executed function. If the try
1349 * conditional is in its finally clause, ignore anything pending.
1350 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001351 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352 */
1353 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001354 {
1355 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1356
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001357 if (idx >= 0)
1358 --idx; /* remove try block not in its finally clause */
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001359 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1360 &cstack.cs_looplevel);
1361 }
1362 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001363 trylevel = initial_trylevel;
1364 }
1365
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001366 /* If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1367 * lack was reported above and the error message is to be converted to an
Bram Moolenaar071d4272004-06-13 20:20:40 +00001368 * exception, do this now after rewinding the cstack. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001369 do_errthrow(&cstack, getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370 ? (char_u *)"endfunction" : (char_u *)NULL);
1371
1372 if (trylevel == 0)
1373 {
1374 /*
1375 * When an exception is being thrown out of the outermost try
1376 * conditional, discard the uncaught exception, disable the conversion
1377 * of interrupts or errors to exceptions, and ensure that no more
1378 * commands are executed.
1379 */
1380 if (did_throw)
1381 {
1382 void *p = NULL;
1383 char_u *saved_sourcing_name;
1384 int saved_sourcing_lnum;
1385 struct msglist *messages = NULL, *next;
1386
1387 /*
1388 * If the uncaught exception is a user exception, report it as an
1389 * error. If it is an error exception, display the saved error
1390 * message now. For an interrupt exception, do nothing; the
1391 * interrupt message is given elsewhere.
1392 */
1393 switch (current_exception->type)
1394 {
1395 case ET_USER:
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001396 vim_snprintf((char *)IObuff, IOSIZE,
1397 _("E605: Exception not caught: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398 current_exception->value);
1399 p = vim_strsave(IObuff);
1400 break;
1401 case ET_ERROR:
1402 messages = current_exception->messages;
1403 current_exception->messages = NULL;
1404 break;
1405 case ET_INTERRUPT:
1406 break;
1407 default:
1408 p = vim_strsave((char_u *)_(e_internal));
1409 }
1410
1411 saved_sourcing_name = sourcing_name;
1412 saved_sourcing_lnum = sourcing_lnum;
1413 sourcing_name = current_exception->throw_name;
1414 sourcing_lnum = current_exception->throw_lnum;
1415 current_exception->throw_name = NULL;
1416
1417 discard_current_exception(); /* uses IObuff if 'verbose' */
1418 suppress_errthrow = TRUE;
1419 force_abort = TRUE;
1420
1421 if (messages != NULL)
1422 {
1423 do
1424 {
1425 next = messages->next;
1426 emsg(messages->msg);
1427 vim_free(messages->msg);
1428 vim_free(messages);
1429 messages = next;
1430 }
1431 while (messages != NULL);
1432 }
1433 else if (p != NULL)
1434 {
1435 emsg(p);
1436 vim_free(p);
1437 }
1438 vim_free(sourcing_name);
1439 sourcing_name = saved_sourcing_name;
1440 sourcing_lnum = saved_sourcing_lnum;
1441 }
1442
1443 /*
1444 * On an interrupt or an aborting error not converted to an exception,
1445 * disable the conversion of errors to exceptions. (Interrupts are not
1446 * converted any more, here.) This enables also the interrupt message
1447 * when force_abort is set and did_emsg unset in case of an interrupt
1448 * from a finally clause after an error.
1449 */
1450 else if (got_int || (did_emsg && force_abort))
1451 suppress_errthrow = TRUE;
1452 }
1453
1454 /*
1455 * The current cstack will be freed when do_cmdline() returns. An uncaught
1456 * exception will have to be rethrown in the previous cstack. If a function
1457 * has just returned or a script file was just finished and the previous
1458 * cstack belongs to the same function or, respectively, script file, it
1459 * will have to be checked for finally clauses to be executed due to the
1460 * ":return" or ":finish". This is done in do_one_cmd().
1461 */
1462 if (did_throw)
1463 need_rethrow = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001464 if ((getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001465 && ex_nesting_level > source_level(real_cookie))
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001466 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001467 && ex_nesting_level > func_level(real_cookie) + 1))
1468 {
1469 if (!did_throw)
1470 check_cstack = TRUE;
1471 }
1472 else
1473 {
1474 /* When leaving a function, reduce nesting level. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001475 if (getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476 --ex_nesting_level;
1477 /*
1478 * Go to debug mode when returning from a function in which we are
1479 * single-stepping.
1480 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001481 if ((getline_equal(fgetline, cookie, getsourceline)
1482 || getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483 && ex_nesting_level + 1 <= debug_break_level)
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001484 do_debug(getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001485 ? (char_u *)_("End of sourced file")
1486 : (char_u *)_("End of function"));
1487 }
1488
1489 /*
1490 * Restore the exception environment (done after returning from the
1491 * debugger).
1492 */
1493 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001494 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001495
1496 msg_list = saved_msg_list;
1497#endif /* FEAT_EVAL */
1498
1499 /*
1500 * If there was too much output to fit on the command line, ask the user to
1501 * hit return before redrawing the screen. With the ":global" command we do
1502 * this only once after the command is finished.
1503 */
1504 if (did_inc)
1505 {
1506 --RedrawingDisabled;
1507 --no_wait_return;
1508 msg_scroll = FALSE;
1509
1510 /*
1511 * When just finished an ":if"-":else" which was typed, no need to
1512 * wait for hit-return. Also for an error situation.
1513 */
1514 if (retval == FAIL
1515#ifdef FEAT_EVAL
1516 || (did_endif && KeyTyped && !did_emsg)
1517#endif
1518 )
1519 {
1520 need_wait_return = FALSE;
1521 msg_didany = FALSE; /* don't wait when restarting edit */
1522 }
1523 else if (need_wait_return)
1524 {
1525 /*
1526 * The msg_start() above clears msg_didout. The wait_return we do
1527 * here should not overwrite the command that may be shown before
1528 * doing that.
1529 */
1530 msg_didout |= msg_didout_before_start;
1531 wait_return(FALSE);
1532 }
1533 }
1534
Bram Moolenaar2a942252012-11-28 23:03:07 +01001535#ifdef FEAT_EVAL
1536 did_endif = FALSE; /* in case do_cmdline used recursively */
1537#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001538 /*
1539 * Reset if_level, in case a sourced script file contains more ":if" than
1540 * ":endif" (could be ":if x | foo | endif").
1541 */
1542 if_level = 0;
Bram Moolenaar2e18a122012-11-28 19:10:54 +01001543#endif
Bram Moolenaard4ad0d42012-11-28 17:34:48 +01001544
Bram Moolenaar071d4272004-06-13 20:20:40 +00001545 --call_depth;
1546 return retval;
1547}
1548
1549#ifdef FEAT_EVAL
1550/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001551 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552 */
1553 static char_u *
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001554get_loop_line(c, cookie, indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001555 int c;
1556 void *cookie;
1557 int indent;
1558{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001559 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560 wcmd_T *wp;
1561 char_u *line;
1562
1563 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1564 {
1565 if (cp->repeating)
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001566 return NULL; /* trying to read past ":endwhile"/":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001568 /* First time inside the ":while"/":for": get line normally. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569 if (cp->getline == NULL)
1570 line = getcmdline(c, 0L, indent);
1571 else
1572 line = cp->getline(c, cp->cookie, indent);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001573 if (line != NULL && store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001574 ++cp->current_line;
1575
1576 return line;
1577 }
1578
1579 KeyTyped = FALSE;
1580 ++cp->current_line;
1581 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
1582 sourcing_lnum = wp->lnum;
1583 return vim_strsave(wp->line);
1584}
1585
1586/*
1587 * Store a line in "gap" so that a ":while" loop can execute it again.
1588 */
1589 static int
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001590store_loop_line(gap, line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591 garray_T *gap;
1592 char_u *line;
1593{
1594 if (ga_grow(gap, 1) == FAIL)
1595 return FAIL;
1596 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
1597 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = sourcing_lnum;
1598 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599 return OK;
1600}
1601
1602/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001603 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001604 */
1605 static void
1606free_cmdlines(gap)
1607 garray_T *gap;
1608{
1609 while (gap->ga_len > 0)
1610 {
1611 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1612 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613 }
1614}
1615#endif
1616
1617/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001618 * If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals
1619 * "func". * Otherwise return TRUE when "fgetline" equals "func".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621 int
Bram Moolenaar89d40322006-08-29 15:30:07 +00001622getline_equal(fgetline, cookie, func)
1623 char_u *(*fgetline) __ARGS((int, void *, int));
Bram Moolenaar78a15312009-05-15 19:33:18 +00001624 void *cookie UNUSED; /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625 char_u *(*func) __ARGS((int, void *, int));
1626{
1627#ifdef FEAT_EVAL
1628 char_u *(*gp) __ARGS((int, void *, int));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001629 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001630
Bram Moolenaar89d40322006-08-29 15:30:07 +00001631 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001632 * function that's originally used to obtain the lines. This may be
1633 * nested several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001634 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001635 cp = (struct loop_cookie *)cookie;
1636 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637 {
1638 gp = cp->getline;
1639 cp = cp->cookie;
1640 }
1641 return gp == func;
1642#else
Bram Moolenaar89d40322006-08-29 15:30:07 +00001643 return fgetline == func;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644#endif
1645}
1646
1647#if defined(FEAT_EVAL) || defined(FEAT_MBYTE) || defined(PROTO)
1648/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001649 * If "fgetline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001650 * getline function. Otherwise return "cookie".
1651 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001652 void *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001653getline_cookie(fgetline, cookie)
Bram Moolenaar78a15312009-05-15 19:33:18 +00001654 char_u *(*fgetline) __ARGS((int, void *, int)) UNUSED;
Bram Moolenaar89d40322006-08-29 15:30:07 +00001655 void *cookie; /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656{
1657# ifdef FEAT_EVAL
1658 char_u *(*gp) __ARGS((int, void *, int));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001659 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001660
Bram Moolenaar89d40322006-08-29 15:30:07 +00001661 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001662 * cookie that's originally used to obtain the lines. This may be nested
Bram Moolenaar071d4272004-06-13 20:20:40 +00001663 * several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001664 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001665 cp = (struct loop_cookie *)cookie;
1666 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667 {
1668 gp = cp->getline;
1669 cp = cp->cookie;
1670 }
1671 return cp;
1672# else
1673 return cookie;
1674# endif
1675}
1676#endif
1677
1678/*
1679 * Execute one Ex command.
1680 *
1681 * If 'sourcing' is TRUE, the command will be included in the error message.
1682 *
1683 * 1. skip comment lines and leading space
1684 * 2. handle command modifiers
1685 * 3. parse range
1686 * 4. parse command
1687 * 5. parse arguments
1688 * 6. switch on command name
1689 *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001690 * Note: "fgetline" can be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001691 *
1692 * This function may be called recursively!
1693 */
1694#if (_MSC_VER == 1200)
1695/*
Bram Moolenaared203462004-06-16 11:19:22 +00001696 * Avoid optimisation bug in VC++ version 6.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001697 */
Bram Moolenaar281bdce2005-01-25 21:53:18 +00001698 #pragma optimize( "g", off )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001699#endif
1700 static char_u *
1701do_one_cmd(cmdlinep, sourcing,
1702#ifdef FEAT_EVAL
1703 cstack,
1704#endif
Bram Moolenaar89d40322006-08-29 15:30:07 +00001705 fgetline, cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001706 char_u **cmdlinep;
1707 int sourcing;
1708#ifdef FEAT_EVAL
1709 struct condstack *cstack;
1710#endif
Bram Moolenaar89d40322006-08-29 15:30:07 +00001711 char_u *(*fgetline) __ARGS((int, void *, int));
1712 void *cookie; /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713{
1714 char_u *p;
1715 linenr_T lnum;
1716 long n;
1717 char_u *errormsg = NULL; /* error message */
1718 exarg_T ea; /* Ex command arguments */
1719 long verbose_save = -1;
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001720 int save_msg_scroll = msg_scroll;
1721 int save_msg_silent = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001722 int did_esilent = 0;
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001723#ifdef HAVE_SANDBOX
1724 int did_sandbox = FALSE;
1725#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001726 cmdmod_T save_cmdmod;
1727 int ni; /* set when Not Implemented */
1728
1729 vim_memset(&ea, 0, sizeof(ea));
1730 ea.line1 = 1;
1731 ea.line2 = 1;
1732#ifdef FEAT_EVAL
1733 ++ex_nesting_level;
1734#endif
1735
Bram Moolenaar21691f82012-12-05 19:13:18 +01001736 /* When the last file has not been edited :q has to be typed twice. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001737 if (quitmore
1738#ifdef FEAT_EVAL
1739 /* avoid that a function call in 'statusline' does this */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001740 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001741#endif
1742#ifdef FEAT_AUTOCMD
Bram Moolenaar21691f82012-12-05 19:13:18 +01001743 /* avoid that an autocommand, e.g. QuitPre, does this */
1744 && !getline_equal(fgetline, cookie, getnextac)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745#endif
1746 )
1747 --quitmore;
1748
1749 /*
1750 * Reset browse, confirm, etc.. They are restored when returning, for
1751 * recursive calls.
1752 */
1753 save_cmdmod = cmdmod;
1754 vim_memset(&cmdmod, 0, sizeof(cmdmod));
1755
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001756 /* "#!anything" is handled like a comment. */
1757 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1758 goto doend;
1759
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760 /*
1761 * Repeat until no more command modifiers are found.
1762 */
1763 ea.cmd = *cmdlinep;
1764 for (;;)
1765 {
1766/*
1767 * 1. skip comment lines and leading white space and colons
1768 */
1769 while (*ea.cmd == ' ' || *ea.cmd == '\t' || *ea.cmd == ':')
1770 ++ea.cmd;
1771
1772 /* in ex mode, an empty line works like :+ */
1773 if (*ea.cmd == NUL && exmode_active
Bram Moolenaar89d40322006-08-29 15:30:07 +00001774 && (getline_equal(fgetline, cookie, getexmodeline)
1775 || getline_equal(fgetline, cookie, getexline))
Bram Moolenaardf177f62005-02-22 08:39:57 +00001776 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777 {
1778 ea.cmd = (char_u *)"+";
1779 ex_pressedreturn = TRUE;
1780 }
1781
1782 /* ignore comment and empty lines */
Bram Moolenaarf998c042007-11-20 11:31:26 +00001783 if (*ea.cmd == '"')
1784 goto doend;
1785 if (*ea.cmd == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001786 {
1787 ex_pressedreturn = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001789 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001790
1791/*
1792 * 2. handle command modifiers.
1793 */
1794 p = ea.cmd;
1795 if (VIM_ISDIGIT(*ea.cmd))
1796 p = skipwhite(skipdigits(ea.cmd));
1797 switch (*p)
1798 {
1799 /* When adding an entry, also modify cmd_exists(). */
1800 case 'a': if (!checkforcmd(&ea.cmd, "aboveleft", 3))
1801 break;
1802#ifdef FEAT_WINDOWS
1803 cmdmod.split |= WSP_ABOVE;
1804#endif
1805 continue;
1806
1807 case 'b': if (checkforcmd(&ea.cmd, "belowright", 3))
1808 {
1809#ifdef FEAT_WINDOWS
1810 cmdmod.split |= WSP_BELOW;
1811#endif
1812 continue;
1813 }
1814 if (checkforcmd(&ea.cmd, "browse", 3))
1815 {
Bram Moolenaard812df62008-11-09 12:46:09 +00001816#ifdef FEAT_BROWSE_CMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817 cmdmod.browse = TRUE;
1818#endif
1819 continue;
1820 }
1821 if (!checkforcmd(&ea.cmd, "botright", 2))
1822 break;
1823#ifdef FEAT_WINDOWS
1824 cmdmod.split |= WSP_BOT;
1825#endif
1826 continue;
1827
1828 case 'c': if (!checkforcmd(&ea.cmd, "confirm", 4))
1829 break;
1830#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1831 cmdmod.confirm = TRUE;
1832#endif
1833 continue;
1834
1835 case 'k': if (checkforcmd(&ea.cmd, "keepmarks", 3))
1836 {
1837 cmdmod.keepmarks = TRUE;
1838 continue;
1839 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001840 if (checkforcmd(&ea.cmd, "keepalt", 5))
1841 {
1842 cmdmod.keepalt = TRUE;
1843 continue;
1844 }
Bram Moolenaara939e432013-11-09 05:30:26 +01001845 if (checkforcmd(&ea.cmd, "keeppatterns", 5))
1846 {
1847 cmdmod.keeppatterns = TRUE;
1848 continue;
1849 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001850 if (!checkforcmd(&ea.cmd, "keepjumps", 5))
1851 break;
1852 cmdmod.keepjumps = TRUE;
1853 continue;
1854
1855 /* ":hide" and ":hide | cmd" are not modifiers */
1856 case 'h': if (p != ea.cmd || !checkforcmd(&p, "hide", 3)
1857 || *p == NUL || ends_excmd(*p))
1858 break;
1859 ea.cmd = p;
1860 cmdmod.hide = TRUE;
1861 continue;
1862
1863 case 'l': if (checkforcmd(&ea.cmd, "lockmarks", 3))
1864 {
1865 cmdmod.lockmarks = TRUE;
1866 continue;
1867 }
1868
1869 if (!checkforcmd(&ea.cmd, "leftabove", 5))
1870 break;
1871#ifdef FEAT_WINDOWS
1872 cmdmod.split |= WSP_ABOVE;
1873#endif
1874 continue;
1875
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001876 case 'n': if (checkforcmd(&ea.cmd, "noautocmd", 3))
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001877 {
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001878#ifdef FEAT_AUTOCMD
1879 if (cmdmod.save_ei == NULL)
1880 {
1881 /* Set 'eventignore' to "all". Restore the
1882 * existing option value later. */
1883 cmdmod.save_ei = vim_strsave(p_ei);
1884 set_string_option_direct((char_u *)"ei", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001885 (char_u *)"all", OPT_FREE, SID_NONE);
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001886 }
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001887#endif
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001888 continue;
1889 }
1890 if (!checkforcmd(&ea.cmd, "noswapfile", 6))
1891 break;
1892 cmdmod.noswapfile = TRUE;
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001893 continue;
1894
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895 case 'r': if (!checkforcmd(&ea.cmd, "rightbelow", 6))
1896 break;
1897#ifdef FEAT_WINDOWS
1898 cmdmod.split |= WSP_BELOW;
1899#endif
1900 continue;
1901
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001902 case 's': if (checkforcmd(&ea.cmd, "sandbox", 3))
1903 {
1904#ifdef HAVE_SANDBOX
1905 if (!did_sandbox)
1906 ++sandbox;
1907 did_sandbox = TRUE;
1908#endif
1909 continue;
1910 }
1911 if (!checkforcmd(&ea.cmd, "silent", 3))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912 break;
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001913 if (save_msg_silent == -1)
1914 save_msg_silent = msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001915 ++msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001916 if (*ea.cmd == '!' && !vim_iswhite(ea.cmd[-1]))
1917 {
1918 /* ":silent!", but not "silent !cmd" */
1919 ea.cmd = skipwhite(ea.cmd + 1);
1920 ++emsg_silent;
1921 ++did_esilent;
1922 }
1923 continue;
1924
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001925 case 't': if (checkforcmd(&p, "tab", 3))
1926 {
1927#ifdef FEAT_WINDOWS
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001928 if (vim_isdigit(*ea.cmd))
1929 cmdmod.tab = atoi((char *)ea.cmd) + 1;
1930 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001931 cmdmod.tab = tabpage_index(curtab) + 1;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001932 ea.cmd = p;
1933#endif
1934 continue;
1935 }
1936 if (!checkforcmd(&ea.cmd, "topleft", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001937 break;
1938#ifdef FEAT_WINDOWS
1939 cmdmod.split |= WSP_TOP;
1940#endif
1941 continue;
1942
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001943 case 'u': if (!checkforcmd(&ea.cmd, "unsilent", 3))
1944 break;
1945 if (save_msg_silent == -1)
1946 save_msg_silent = msg_silent;
1947 msg_silent = 0;
1948 continue;
1949
Bram Moolenaar071d4272004-06-13 20:20:40 +00001950 case 'v': if (checkforcmd(&ea.cmd, "vertical", 4))
1951 {
1952#ifdef FEAT_VERTSPLIT
1953 cmdmod.split |= WSP_VERT;
1954#endif
1955 continue;
1956 }
1957 if (!checkforcmd(&p, "verbose", 4))
1958 break;
1959 if (verbose_save < 0)
1960 verbose_save = p_verbose;
Bram Moolenaared203462004-06-16 11:19:22 +00001961 if (vim_isdigit(*ea.cmd))
1962 p_verbose = atoi((char *)ea.cmd);
1963 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001964 p_verbose = 1;
1965 ea.cmd = p;
1966 continue;
1967 }
1968 break;
1969 }
1970
1971#ifdef FEAT_EVAL
1972 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
1973 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
1974#else
1975 ea.skip = (if_level > 0);
1976#endif
1977
1978#ifdef FEAT_EVAL
Bram Moolenaar05159a02005-02-26 23:04:13 +00001979# ifdef FEAT_PROFILE
1980 /* Count this line for profiling if ea.skip is FALSE. */
Bram Moolenaar371d5402006-03-20 21:47:49 +00001981 if (do_profiling == PROF_YES && !ea.skip)
Bram Moolenaar05159a02005-02-26 23:04:13 +00001982 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001983 if (getline_equal(fgetline, cookie, get_func_line))
1984 func_line_exec(getline_cookie(fgetline, cookie));
1985 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +00001986 script_line_exec();
1987 }
1988#endif
1989
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990 /* May go to debug mode. If this happens and the ">quit" debug command is
1991 * used, throw an interrupt exception and skip the next command. */
1992 dbg_check_breakpoint(&ea);
1993 if (!ea.skip && got_int)
1994 {
1995 ea.skip = TRUE;
1996 (void)do_intthrow(cstack);
1997 }
1998#endif
1999
2000/*
2001 * 3. parse a range specifier of the form: addr [,addr] [;addr] ..
2002 *
2003 * where 'addr' is:
2004 *
2005 * % (entire file)
2006 * $ [+-NUM]
2007 * 'x [+-NUM] (where x denotes a currently defined mark)
2008 * . [+-NUM]
2009 * [+-NUM]..
2010 * NUM
2011 *
2012 * The ea.cmd pointer is updated to point to the first character following the
2013 * range spec. If an initial address is found, but no second, the upper bound
2014 * is equal to the lower.
2015 */
2016
2017 /* repeat for all ',' or ';' separated addresses */
2018 for (;;)
2019 {
2020 ea.line1 = ea.line2;
2021 ea.line2 = curwin->w_cursor.lnum; /* default is current line number */
2022 ea.cmd = skipwhite(ea.cmd);
2023 lnum = get_address(&ea.cmd, ea.skip, ea.addr_count == 0);
2024 if (ea.cmd == NULL) /* error detected */
2025 goto doend;
2026 if (lnum == MAXLNUM)
2027 {
2028 if (*ea.cmd == '%') /* '%' - all lines */
2029 {
2030 ++ea.cmd;
2031 ea.line1 = 1;
2032 ea.line2 = curbuf->b_ml.ml_line_count;
2033 ++ea.addr_count;
2034 }
2035 /* '*' - visual area */
2036 else if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
2037 {
2038 pos_T *fp;
2039
2040 ++ea.cmd;
2041 if (!ea.skip)
2042 {
2043 fp = getmark('<', FALSE);
2044 if (check_mark(fp) == FAIL)
2045 goto doend;
2046 ea.line1 = fp->lnum;
2047 fp = getmark('>', FALSE);
2048 if (check_mark(fp) == FAIL)
2049 goto doend;
2050 ea.line2 = fp->lnum;
2051 ++ea.addr_count;
2052 }
2053 }
2054 }
2055 else
2056 ea.line2 = lnum;
2057 ea.addr_count++;
2058
2059 if (*ea.cmd == ';')
2060 {
2061 if (!ea.skip)
2062 curwin->w_cursor.lnum = ea.line2;
2063 }
2064 else if (*ea.cmd != ',')
2065 break;
2066 ++ea.cmd;
2067 }
2068
2069 /* One address given: set start and end lines */
2070 if (ea.addr_count == 1)
2071 {
2072 ea.line1 = ea.line2;
2073 /* ... but only implicit: really no address given */
2074 if (lnum == MAXLNUM)
2075 ea.addr_count = 0;
2076 }
2077
2078 /* Don't leave the cursor on an illegal line (caused by ';') */
2079 check_cursor_lnum();
2080
2081/*
2082 * 4. parse command
2083 */
2084
2085 /*
2086 * Skip ':' and any white space
2087 */
2088 ea.cmd = skipwhite(ea.cmd);
2089 while (*ea.cmd == ':')
2090 ea.cmd = skipwhite(ea.cmd + 1);
2091
2092 /*
2093 * If we got a line, but no command, then go to the line.
2094 * If we find a '|' or '\n' we set ea.nextcmd.
2095 */
2096 if (*ea.cmd == NUL || *ea.cmd == '"' ||
2097 (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
2098 {
2099 /*
2100 * strange vi behaviour:
2101 * ":3" jumps to line 3
2102 * ":3|..." prints line 3
2103 * ":|" prints current line
2104 */
2105 if (ea.skip) /* skip this if inside :if */
2106 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00002107 if (*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002108 {
2109 ea.cmdidx = CMD_print;
2110 ea.argt = RANGE+COUNT+TRLBAR;
2111 if ((errormsg = invalid_range(&ea)) == NULL)
2112 {
2113 correct_range(&ea);
2114 ex_print(&ea);
2115 }
2116 }
2117 else if (ea.addr_count != 0)
2118 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002119 if (ea.line2 > curbuf->b_ml.ml_line_count)
2120 {
2121 /* With '-' in 'cpoptions' a line number past the file is an
2122 * error, otherwise put it at the end of the file. */
2123 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
2124 ea.line2 = -1;
2125 else
2126 ea.line2 = curbuf->b_ml.ml_line_count;
2127 }
2128
2129 if (ea.line2 < 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002130 errormsg = (char_u *)_(e_invrange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002131 else
2132 {
2133 if (ea.line2 == 0)
2134 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135 else
2136 curwin->w_cursor.lnum = ea.line2;
2137 beginline(BL_SOL | BL_FIX);
2138 }
2139 }
2140 goto doend;
2141 }
2142
2143 /* Find the command and let "p" point to after it. */
2144 p = find_command(&ea, NULL);
2145
Bram Moolenaard5005162014-08-22 23:05:54 +02002146#ifdef FEAT_AUTOCMD
2147 /* If this looks like an undefined user command and there are CmdUndefined
2148 * autocommands defined, trigger the matching autocommands. */
2149 if (p != NULL && ea.cmdidx == CMD_SIZE && !ea.skip
2150 && ASCII_ISUPPER(*ea.cmd)
2151 && has_cmdundefined())
2152 {
2153 char_u *p = ea.cmd;
2154 int ret;
2155
2156 while (ASCII_ISALNUM(*p))
2157 ++p;
2158 p = vim_strnsave(ea.cmd, p - ea.cmd);
2159 ret = apply_autocmds(EVENT_CMDUNDEFINED, p, p, TRUE, NULL);
2160 vim_free(p);
2161 if (ret && !aborting())
2162 p = find_command(&ea, NULL);
2163 }
2164#endif
2165
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166#ifdef FEAT_USR_CMDS
2167 if (p == NULL)
2168 {
2169 if (!ea.skip)
2170 errormsg = (char_u *)_("E464: Ambiguous use of user-defined command");
2171 goto doend;
2172 }
2173 /* Check for wrong commands. */
2174 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78)
2175 {
2176 errormsg = uc_fun_cmd();
2177 goto doend;
2178 }
2179#endif
2180 if (ea.cmdidx == CMD_SIZE)
2181 {
2182 if (!ea.skip)
2183 {
2184 STRCPY(IObuff, _("E492: Not an editor command"));
2185 if (!sourcing)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002186 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187 errormsg = IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02002188 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002189 }
2190 goto doend;
2191 }
2192
2193 ni = (
2194#ifdef FEAT_USR_CMDS
2195 !USER_CMDIDX(ea.cmdidx) &&
2196#endif
Bram Moolenaar3ebc1e52007-07-05 07:54:17 +00002197 (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00002198#ifdef HAVE_EX_SCRIPT_NI
2199 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
2200#endif
2201 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202
2203#ifndef FEAT_EVAL
2204 /*
2205 * When the expression evaluation is disabled, recognize the ":if" and
2206 * ":endif" commands and ignore everything in between it.
2207 */
2208 if (ea.cmdidx == CMD_if)
2209 ++if_level;
2210 if (if_level)
2211 {
2212 if (ea.cmdidx == CMD_endif)
2213 --if_level;
2214 goto doend;
2215 }
2216
2217#endif
2218
Bram Moolenaar196b3b02008-06-20 16:51:41 +00002219 /* forced commands */
2220 if (*p == '!' && ea.cmdidx != CMD_substitute
2221 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002222 {
2223 ++p;
2224 ea.forceit = TRUE;
2225 }
2226 else
2227 ea.forceit = FALSE;
2228
2229/*
2230 * 5. parse arguments
2231 */
2232#ifdef FEAT_USR_CMDS
2233 if (!USER_CMDIDX(ea.cmdidx))
2234#endif
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002235 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002236
2237 if (!ea.skip)
2238 {
2239#ifdef HAVE_SANDBOX
2240 if (sandbox != 0 && !(ea.argt & SBOXOK))
2241 {
2242 /* Command not allowed in sandbox. */
2243 errormsg = (char_u *)_(e_sandbox);
2244 goto doend;
2245 }
2246#endif
2247 if (!curbuf->b_p_ma && (ea.argt & MODIFY))
2248 {
2249 /* Command not allowed in non-'modifiable' buffer */
2250 errormsg = (char_u *)_(e_modifiable);
2251 goto doend;
2252 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002253
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002254 if (text_locked() && !(ea.argt & CMDWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002255# ifdef FEAT_USR_CMDS
2256 && !USER_CMDIDX(ea.cmdidx)
2257# endif
2258 )
2259 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002260 /* Command not allowed when editing the command line. */
2261#ifdef FEAT_CMDWIN
2262 if (cmdwin_type != 0)
2263 errormsg = (char_u *)_(e_cmdwin);
2264 else
2265#endif
2266 errormsg = (char_u *)_(e_secure);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002267 goto doend;
2268 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002269#ifdef FEAT_AUTOCMD
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002270 /* Disallow editing another buffer when "curbuf_lock" is set.
2271 * Do allow ":edit" (check for argument later).
2272 * Do allow ":checktime" (it's postponed). */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002273 if (!(ea.argt & CMDWIN)
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002274 && ea.cmdidx != CMD_edit
2275 && ea.cmdidx != CMD_checktime
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002276# ifdef FEAT_USR_CMDS
2277 && !USER_CMDIDX(ea.cmdidx)
2278# endif
2279 && curbuf_locked())
2280 goto doend;
2281#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282
2283 if (!ni && !(ea.argt & RANGE) && ea.addr_count > 0)
2284 {
2285 /* no range allowed */
2286 errormsg = (char_u *)_(e_norange);
2287 goto doend;
2288 }
2289 }
2290
2291 if (!ni && !(ea.argt & BANG) && ea.forceit) /* no <!> allowed */
2292 {
2293 errormsg = (char_u *)_(e_nobang);
2294 goto doend;
2295 }
2296
2297 /*
2298 * Don't complain about the range if it is not used
2299 * (could happen if line_count is accidentally set to 0).
2300 */
2301 if (!ea.skip && !ni)
2302 {
2303 /*
2304 * If the range is backwards, ask for confirmation and, if given, swap
2305 * ea.line1 & ea.line2 so it's forwards again.
2306 * When global command is busy, don't ask, will fail below.
2307 */
2308 if (!global_busy && ea.line1 > ea.line2)
2309 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002310 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002311 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002312 if (sourcing || exmode_active)
2313 {
2314 errormsg = (char_u *)_("E493: Backwards range given");
2315 goto doend;
2316 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317 if (ask_yesno((char_u *)
2318 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002319 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002320 }
2321 lnum = ea.line1;
2322 ea.line1 = ea.line2;
2323 ea.line2 = lnum;
2324 }
2325 if ((errormsg = invalid_range(&ea)) != NULL)
2326 goto doend;
2327 }
2328
2329 if ((ea.argt & NOTADR) && ea.addr_count == 0) /* default is 1, not cursor */
2330 ea.line2 = 1;
2331
2332 correct_range(&ea);
2333
2334#ifdef FEAT_FOLDING
2335 if (((ea.argt & WHOLEFOLD) || ea.addr_count >= 2) && !global_busy)
2336 {
2337 /* Put the first line at the start of a closed fold, put the last line
2338 * at the end of a closed fold. */
2339 (void)hasFolding(ea.line1, &ea.line1, NULL);
2340 (void)hasFolding(ea.line2, NULL, &ea.line2);
2341 }
2342#endif
2343
2344#ifdef FEAT_QUICKFIX
2345 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002346 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347 * option here, so things like % get expanded.
2348 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002349 p = replace_makeprg(&ea, p, cmdlinep);
2350 if (p == NULL)
2351 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352#endif
2353
2354 /*
2355 * Skip to start of argument.
2356 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2357 */
2358 if (ea.cmdidx == CMD_bang)
2359 ea.arg = p;
2360 else
2361 ea.arg = skipwhite(p);
2362
2363 /*
2364 * Check for "++opt=val" argument.
2365 * Must be first, allow ":w ++enc=utf8 !cmd"
2366 */
2367 if (ea.argt & ARGOPT)
2368 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2369 if (getargopt(&ea) == FAIL && !ni)
2370 {
2371 errormsg = (char_u *)_(e_invarg);
2372 goto doend;
2373 }
2374
2375 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2376 {
2377 if (*ea.arg == '>') /* append */
2378 {
2379 if (*++ea.arg != '>') /* typed wrong */
2380 {
2381 errormsg = (char_u *)_("E494: Use w or w>>");
2382 goto doend;
2383 }
2384 ea.arg = skipwhite(ea.arg + 1);
2385 ea.append = TRUE;
2386 }
2387 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
2388 {
2389 ++ea.arg;
2390 ea.usefilter = TRUE;
2391 }
2392 }
2393
2394 if (ea.cmdidx == CMD_read)
2395 {
2396 if (ea.forceit)
2397 {
2398 ea.usefilter = TRUE; /* :r! filter if ea.forceit */
2399 ea.forceit = FALSE;
2400 }
2401 else if (*ea.arg == '!') /* :r !filter */
2402 {
2403 ++ea.arg;
2404 ea.usefilter = TRUE;
2405 }
2406 }
2407
2408 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2409 {
2410 ea.amount = 1;
2411 while (*ea.arg == *ea.cmd) /* count number of '>' or '<' */
2412 {
2413 ++ea.arg;
2414 ++ea.amount;
2415 }
2416 ea.arg = skipwhite(ea.arg);
2417 }
2418
2419 /*
2420 * Check for "+command" argument, before checking for next command.
2421 * Don't do this for ":read !cmd" and ":write !cmd".
2422 */
2423 if ((ea.argt & EDITCMD) && !ea.usefilter)
2424 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2425
2426 /*
2427 * Check for '|' to separate commands and '"' to start comments.
2428 * Don't do this for ":read !cmd" and ":write !cmd".
2429 */
2430 if ((ea.argt & TRLBAR) && !ea.usefilter)
2431 separate_nextcmd(&ea);
2432
2433 /*
2434 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002435 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2436 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002438 else if (ea.cmdidx == CMD_bang
2439 || ea.cmdidx == CMD_global
2440 || ea.cmdidx == CMD_vglobal
2441 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002442 {
2443 for (p = ea.arg; *p; ++p)
2444 {
2445 /* Remove one backslash before a newline, so that it's possible to
2446 * pass a newline to the shell and also a newline that is preceded
2447 * with a backslash. This makes it impossible to end a shell
2448 * command in a backslash, but that doesn't appear useful.
2449 * Halving the number of backslashes is incompatible with previous
2450 * versions. */
2451 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002452 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453 else if (*p == '\n')
2454 {
2455 ea.nextcmd = p + 1;
2456 *p = NUL;
2457 break;
2458 }
2459 }
2460 }
2461
2462 if ((ea.argt & DFLALL) && ea.addr_count == 0)
2463 {
2464 ea.line1 = 1;
2465 ea.line2 = curbuf->b_ml.ml_line_count;
2466 }
2467
2468 /* accept numbered register only when no count allowed (:put) */
2469 if ( (ea.argt & REGSTR)
2470 && *ea.arg != NUL
2471#ifdef FEAT_USR_CMDS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472 /* Do not allow register = for user commands */
2473 && (!USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474#endif
2475 && !((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)))
2476 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002477#ifndef FEAT_CLIPBOARD
2478 /* check these explicitly for a more specific error message */
2479 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002481 errormsg = (char_u *)_(e_invalidreg);
2482 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483 }
2484#endif
Bram Moolenaar85de2062011-05-05 14:26:41 +02002485 if (
2486#ifdef FEAT_USR_CMDS
2487 valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2488 && USER_CMDIDX(ea.cmdidx)))
2489#else
2490 valid_yank_reg(*ea.arg, ea.cmdidx != CMD_put)
2491#endif
2492 )
2493 {
2494 ea.regname = *ea.arg++;
2495#ifdef FEAT_EVAL
2496 /* for '=' register: accept the rest of the line as an expression */
2497 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2498 {
2499 set_expr_line(vim_strsave(ea.arg));
2500 ea.arg += STRLEN(ea.arg);
2501 }
2502#endif
2503 ea.arg = skipwhite(ea.arg);
2504 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002505 }
2506
2507 /*
2508 * Check for a count. When accepting a BUFNAME, don't use "123foo" as a
2509 * count, it's a buffer name.
2510 */
2511 if ((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)
2512 && (!(ea.argt & BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
2513 || vim_iswhite(*p)))
2514 {
2515 n = getdigits(&ea.arg);
2516 ea.arg = skipwhite(ea.arg);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002517 if (n <= 0 && !ni && (ea.argt & ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002518 {
2519 errormsg = (char_u *)_(e_zerocount);
2520 goto doend;
2521 }
2522 if (ea.argt & NOTADR) /* e.g. :buffer 2, :sleep 3 */
2523 {
2524 ea.line2 = n;
2525 if (ea.addr_count == 0)
2526 ea.addr_count = 1;
2527 }
2528 else
2529 {
2530 ea.line1 = ea.line2;
2531 ea.line2 += n - 1;
2532 ++ea.addr_count;
2533 /*
2534 * Be vi compatible: no error message for out of range.
2535 */
2536 if (ea.line2 > curbuf->b_ml.ml_line_count)
2537 ea.line2 = curbuf->b_ml.ml_line_count;
2538 }
2539 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002540
2541 /*
2542 * Check for flags: 'l', 'p' and '#'.
2543 */
2544 if (ea.argt & EXFLAGS)
2545 get_flags(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546 /* no arguments allowed */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002547 if (!ni && !(ea.argt & EXTRA) && *ea.arg != NUL
Bram Moolenaara2031822006-03-07 22:29:51 +00002548 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002549 {
2550 errormsg = (char_u *)_(e_trailing);
2551 goto doend;
2552 }
2553
2554 if (!ni && (ea.argt & NEEDARG) && *ea.arg == NUL)
2555 {
2556 errormsg = (char_u *)_(e_argreq);
2557 goto doend;
2558 }
2559
2560#ifdef FEAT_EVAL
2561 /*
2562 * Skip the command when it's not going to be executed.
2563 * The commands like :if, :endif, etc. always need to be executed.
2564 * Also make an exception for commands that handle a trailing command
2565 * themselves.
2566 */
2567 if (ea.skip)
2568 {
2569 switch (ea.cmdidx)
2570 {
2571 /* commands that need evaluation */
2572 case CMD_while:
2573 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002574 case CMD_for:
2575 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576 case CMD_if:
2577 case CMD_elseif:
2578 case CMD_else:
2579 case CMD_endif:
2580 case CMD_try:
2581 case CMD_catch:
2582 case CMD_finally:
2583 case CMD_endtry:
2584 case CMD_function:
2585 break;
2586
2587 /* Commands that handle '|' themselves. Check: A command should
2588 * either have the TRLBAR flag, appear in this list or appear in
2589 * the list at ":help :bar". */
2590 case CMD_aboveleft:
2591 case CMD_and:
2592 case CMD_belowright:
2593 case CMD_botright:
2594 case CMD_browse:
2595 case CMD_call:
2596 case CMD_confirm:
2597 case CMD_delfunction:
2598 case CMD_djump:
2599 case CMD_dlist:
2600 case CMD_dsearch:
2601 case CMD_dsplit:
2602 case CMD_echo:
2603 case CMD_echoerr:
2604 case CMD_echomsg:
2605 case CMD_echon:
2606 case CMD_execute:
2607 case CMD_help:
2608 case CMD_hide:
2609 case CMD_ijump:
2610 case CMD_ilist:
2611 case CMD_isearch:
2612 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002613 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002614 case CMD_keepjumps:
2615 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01002616 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617 case CMD_leftabove:
2618 case CMD_let:
2619 case CMD_lockmarks:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002620 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002621 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002622 case CMD_mzscheme:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01002623 case CMD_noautocmd:
2624 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002625 case CMD_perl:
2626 case CMD_psearch:
2627 case CMD_python:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002628 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002629 case CMD_python3:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002630 case CMD_return:
2631 case CMD_rightbelow:
2632 case CMD_ruby:
2633 case CMD_silent:
2634 case CMD_smagic:
2635 case CMD_snomagic:
2636 case CMD_substitute:
2637 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002638 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639 case CMD_tcl:
2640 case CMD_throw:
2641 case CMD_tilde:
2642 case CMD_topleft:
2643 case CMD_unlet:
2644 case CMD_verbose:
2645 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002646 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002647 break;
2648
2649 default: goto doend;
2650 }
2651 }
2652#endif
2653
2654 if (ea.argt & XFILE)
2655 {
2656 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2657 goto doend;
2658 }
2659
2660#ifdef FEAT_LISTCMDS
2661 /*
2662 * Accept buffer name. Cannot be used at the same time with a buffer
2663 * number. Don't do this for a user command.
2664 */
2665 if ((ea.argt & BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
2666# ifdef FEAT_USR_CMDS
2667 && !USER_CMDIDX(ea.cmdidx)
2668# endif
2669 )
2670 {
2671 /*
2672 * :bdelete, :bwipeout and :bunload take several arguments, separated
2673 * by spaces: find next space (skipping over escaped characters).
2674 * The others take one argument: ignore trailing spaces.
2675 */
2676 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2677 || ea.cmdidx == CMD_bunload)
2678 p = skiptowhite_esc(ea.arg);
2679 else
2680 {
2681 p = ea.arg + STRLEN(ea.arg);
2682 while (p > ea.arg && vim_iswhite(p[-1]))
2683 --p;
2684 }
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002685 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & BUFUNL) != 0,
2686 FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002687 if (ea.line2 < 0) /* failed */
2688 goto doend;
2689 ea.addr_count = 1;
2690 ea.arg = skipwhite(p);
2691 }
2692#endif
2693
2694/*
2695 * 6. switch on command name
2696 *
2697 * The "ea" structure holds the arguments that can be used.
2698 */
2699 ea.cmdlinep = cmdlinep;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002700 ea.getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701 ea.cookie = cookie;
2702#ifdef FEAT_EVAL
2703 ea.cstack = cstack;
2704#endif
2705
2706#ifdef FEAT_USR_CMDS
2707 if (USER_CMDIDX(ea.cmdidx))
2708 {
2709 /*
2710 * Execute a user-defined command.
2711 */
2712 do_ucmd(&ea);
2713 }
2714 else
2715#endif
2716 {
2717 /*
2718 * Call the function to execute the command.
2719 */
2720 ea.errmsg = NULL;
2721 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2722 if (ea.errmsg != NULL)
2723 errormsg = (char_u *)_(ea.errmsg);
2724 }
2725
2726#ifdef FEAT_EVAL
2727 /*
2728 * If the command just executed called do_cmdline(), any throw or ":return"
2729 * or ":finish" encountered there must also check the cstack of the still
2730 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2731 * exception, or reanimate a returned function or finished script file and
2732 * return or finish it again.
2733 */
2734 if (need_rethrow)
2735 do_throw(cstack);
2736 else if (check_cstack)
2737 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002738 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002739 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002740 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002741 && current_func_returned())
2742 do_return(&ea, TRUE, FALSE, NULL);
2743 }
2744 need_rethrow = check_cstack = FALSE;
2745#endif
2746
2747doend:
2748 if (curwin->w_cursor.lnum == 0) /* can happen with zero line number */
2749 curwin->w_cursor.lnum = 1;
2750
2751 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2752 {
2753 if (sourcing)
2754 {
2755 if (errormsg != IObuff)
2756 {
2757 STRCPY(IObuff, errormsg);
2758 errormsg = IObuff;
2759 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002760 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002761 }
2762 emsg(errormsg);
2763 }
2764#ifdef FEAT_EVAL
2765 do_errthrow(cstack,
2766 (ea.cmdidx != CMD_SIZE
2767# ifdef FEAT_USR_CMDS
2768 && !USER_CMDIDX(ea.cmdidx)
2769# endif
2770 ) ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
2771#endif
2772
2773 if (verbose_save >= 0)
2774 p_verbose = verbose_save;
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00002775#ifdef FEAT_AUTOCMD
2776 if (cmdmod.save_ei != NULL)
2777 {
2778 /* Restore 'eventignore' to the value before ":noautocmd". */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002779 set_string_option_direct((char_u *)"ei", -1, cmdmod.save_ei,
2780 OPT_FREE, SID_NONE);
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00002781 free_string_option(cmdmod.save_ei);
2782 }
2783#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002784
2785 cmdmod = save_cmdmod;
2786
Bram Moolenaar8e258a42009-07-09 13:55:43 +00002787 if (save_msg_silent != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002788 {
2789 /* messages could be enabled for a serious error, need to check if the
2790 * counters don't become negative */
Bram Moolenaarbecf4282009-09-30 11:24:36 +00002791 if (!did_emsg || msg_silent > save_msg_silent)
Bram Moolenaar8e258a42009-07-09 13:55:43 +00002792 msg_silent = save_msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002793 emsg_silent -= did_esilent;
2794 if (emsg_silent < 0)
2795 emsg_silent = 0;
2796 /* Restore msg_scroll, it's set by file I/O commands, even when no
2797 * message is actually displayed. */
2798 msg_scroll = save_msg_scroll;
Bram Moolenaar77ab2802009-04-22 12:44:48 +00002799
2800 /* "silent reg" or "silent echo x" inside "redir" leaves msg_col
2801 * somewhere in the line. Put it back in the first column. */
2802 if (redirecting())
2803 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002804 }
2805
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002806#ifdef HAVE_SANDBOX
2807 if (did_sandbox)
2808 --sandbox;
2809#endif
2810
Bram Moolenaar071d4272004-06-13 20:20:40 +00002811 if (ea.nextcmd && *ea.nextcmd == NUL) /* not really a next command */
2812 ea.nextcmd = NULL;
2813
2814#ifdef FEAT_EVAL
2815 --ex_nesting_level;
2816#endif
2817
2818 return ea.nextcmd;
2819}
2820#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00002821 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002822#endif
2823
2824/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002825 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002826 * If there is a match advance "pp" to the argument and return TRUE.
2827 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002828 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00002829checkforcmd(pp, cmd, len)
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002830 char_u **pp; /* start of command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002831 char *cmd; /* name of command */
2832 int len; /* required length */
2833{
2834 int i;
2835
2836 for (i = 0; cmd[i] != NUL; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00002837 if (((char_u *)cmd)[i] != (*pp)[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00002838 break;
2839 if (i >= len && !isalpha((*pp)[i]))
2840 {
2841 *pp = skipwhite(*pp + i);
2842 return TRUE;
2843 }
2844 return FALSE;
2845}
2846
2847/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002848 * Append "cmd" to the error message in IObuff.
2849 * Takes care of limiting the length and handling 0xa0, which would be
2850 * invisible otherwise.
2851 */
2852 static void
2853append_command(cmd)
2854 char_u *cmd;
2855{
2856 char_u *s = cmd;
2857 char_u *d;
2858
2859 STRCAT(IObuff, ": ");
2860 d = IObuff + STRLEN(IObuff);
2861 while (*s != NUL && d - IObuff < IOSIZE - 7)
2862 {
2863 if (
2864#ifdef FEAT_MBYTE
2865 enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) :
2866#endif
2867 *s == 0xa0)
2868 {
2869 s +=
2870#ifdef FEAT_MBYTE
2871 enc_utf8 ? 2 :
2872#endif
2873 1;
2874 STRCPY(d, "<a0>");
2875 d += 4;
2876 }
2877 else
2878 MB_COPY_CHAR(s, d);
2879 }
2880 *d = NUL;
2881}
2882
2883/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002884 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002885 * Start of the name can be found at eap->cmd.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002886 * Returns pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002887 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002888 * Returns NULL for an ambiguous user command.
2889 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002890 static char_u *
2891find_command(eap, full)
2892 exarg_T *eap;
Bram Moolenaar78a15312009-05-15 19:33:18 +00002893 int *full UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002894{
2895 int len;
2896 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00002897 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002898
2899 /*
2900 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00002901 * Exceptions:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002902 * - the 'k' command can directly be followed by any character.
2903 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
2904 * but :sre[wind] is another command, as are :scrip[tnames],
2905 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00002906 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002907 */
2908 p = eap->cmd;
2909 if (*p == 'k')
2910 {
2911 eap->cmdidx = CMD_k;
2912 ++p;
2913 }
2914 else if (p[0] == 's'
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002915 && ((p[1] == 'c' && p[2] != 's' && p[2] != 'r'
2916 && p[3] != 'i' && p[4] != 'p')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002917 || p[1] == 'g'
2918 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
2919 || p[1] == 'I'
2920 || (p[1] == 'r' && p[2] != 'e')))
2921 {
2922 eap->cmdidx = CMD_substitute;
2923 ++p;
2924 }
2925 else
2926 {
2927 while (ASCII_ISALPHA(*p))
2928 ++p;
Bram Moolenaarb6590522010-07-21 16:00:43 +02002929 /* for python 3.x support ":py3", ":python3", ":py3file", etc. */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002930 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaarb6590522010-07-21 16:00:43 +02002931 while (ASCII_ISALNUM(*p))
2932 ++p;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02002933
Bram Moolenaar071d4272004-06-13 20:20:40 +00002934 /* check for non-alpha command */
2935 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
2936 ++p;
2937 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00002938 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
2939 {
2940 /* Check for ":dl", ":dell", etc. to ":deletel": that's
2941 * :delete with the 'l' flag. Same for 'p'. */
2942 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00002943 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00002944 break;
2945 if (i == len - 1)
2946 {
2947 --len;
2948 if (p[-1] == 'l')
2949 eap->flags |= EXFLAG_LIST;
2950 else
2951 eap->flags |= EXFLAG_PRINT;
2952 }
2953 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002954
2955 if (ASCII_ISLOWER(*eap->cmd))
2956 eap->cmdidx = cmdidxs[CharOrdLow(*eap->cmd)];
2957 else
2958 eap->cmdidx = cmdidxs[26];
2959
2960 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
2961 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
2962 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
2963 (size_t)len) == 0)
2964 {
2965#ifdef FEAT_EVAL
2966 if (full != NULL
2967 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
2968 *full = TRUE;
2969#endif
2970 break;
2971 }
2972
2973#ifdef FEAT_USR_CMDS
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01002974 /* Look for a user defined command as a last resort. Let ":Print" be
2975 * overruled by a user defined command. */
2976 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
2977 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002978 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002979 /* User defined commands may contain digits. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002980 while (ASCII_ISALNUM(*p))
2981 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002982 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002983 }
2984#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002985 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002986 eap->cmdidx = CMD_SIZE;
2987 }
2988
2989 return p;
2990}
2991
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002992#ifdef FEAT_USR_CMDS
2993/*
2994 * Search for a user command that matches "eap->cmd".
2995 * Return cmdidx in "eap->cmdidx", flags in "eap->argt", idx in "eap->useridx".
2996 * Return a pointer to just after the command.
2997 * Return NULL if there is no matching command.
2998 */
2999 static char_u *
3000find_ucmd(eap, p, full, xp, compl)
3001 exarg_T *eap;
3002 char_u *p; /* end of the command (possibly including count) */
3003 int *full; /* set to TRUE for a full match */
3004 expand_T *xp; /* used for completion, NULL otherwise */
3005 int *compl; /* completion flags or NULL */
3006{
3007 int len = (int)(p - eap->cmd);
3008 int j, k, matchlen = 0;
3009 ucmd_T *uc;
3010 int found = FALSE;
3011 int possible = FALSE;
3012 char_u *cp, *np; /* Point into typed cmd and test name */
3013 garray_T *gap;
3014 int amb_local = FALSE; /* Found ambiguous buffer-local command,
3015 only full match global is accepted. */
3016
3017 /*
3018 * Look for buffer-local user commands first, then global ones.
3019 */
3020 gap = &curbuf->b_ucmds;
3021 for (;;)
3022 {
3023 for (j = 0; j < gap->ga_len; ++j)
3024 {
3025 uc = USER_CMD_GA(gap, j);
3026 cp = eap->cmd;
3027 np = uc->uc_name;
3028 k = 0;
3029 while (k < len && *np != NUL && *cp++ == *np++)
3030 k++;
3031 if (k == len || (*np == NUL && vim_isdigit(eap->cmd[k])))
3032 {
3033 /* If finding a second match, the command is ambiguous. But
3034 * not if a buffer-local command wasn't a full match and a
3035 * global command is a full match. */
3036 if (k == len && found && *np != NUL)
3037 {
3038 if (gap == &ucmds)
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003039 return NULL;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003040 amb_local = TRUE;
3041 }
3042
3043 if (!found || (k == len && *np == NUL))
3044 {
3045 /* If we matched up to a digit, then there could
3046 * be another command including the digit that we
3047 * should use instead.
3048 */
3049 if (k == len)
3050 found = TRUE;
3051 else
3052 possible = TRUE;
3053
3054 if (gap == &ucmds)
3055 eap->cmdidx = CMD_USER;
3056 else
3057 eap->cmdidx = CMD_USER_BUF;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003058 eap->argt = (long)uc->uc_argt;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003059 eap->useridx = j;
3060
3061# ifdef FEAT_CMDL_COMPL
3062 if (compl != NULL)
3063 *compl = uc->uc_compl;
3064# ifdef FEAT_EVAL
3065 if (xp != NULL)
3066 {
3067 xp->xp_arg = uc->uc_compl_arg;
3068 xp->xp_scriptID = uc->uc_scriptID;
3069 }
3070# endif
3071# endif
3072 /* Do not search for further abbreviations
3073 * if this is an exact match. */
3074 matchlen = k;
3075 if (k == len && *np == NUL)
3076 {
3077 if (full != NULL)
3078 *full = TRUE;
3079 amb_local = FALSE;
3080 break;
3081 }
3082 }
3083 }
3084 }
3085
3086 /* Stop if we found a full match or searched all. */
3087 if (j < gap->ga_len || gap == &ucmds)
3088 break;
3089 gap = &ucmds;
3090 }
3091
3092 /* Only found ambiguous matches. */
3093 if (amb_local)
3094 {
3095 if (xp != NULL)
3096 xp->xp_context = EXPAND_UNSUCCESSFUL;
3097 return NULL;
3098 }
3099
3100 /* The match we found may be followed immediately by a number. Move "p"
3101 * back to point to it. */
3102 if (found || possible)
3103 return p + (matchlen - len);
3104 return p;
3105}
3106#endif
3107
Bram Moolenaar071d4272004-06-13 20:20:40 +00003108#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003109static struct cmdmod
3110{
3111 char *name;
3112 int minlen;
3113 int has_count; /* :123verbose :3tab */
3114} cmdmods[] = {
3115 {"aboveleft", 3, FALSE},
3116 {"belowright", 3, FALSE},
3117 {"botright", 2, FALSE},
3118 {"browse", 3, FALSE},
3119 {"confirm", 4, FALSE},
3120 {"hide", 3, FALSE},
3121 {"keepalt", 5, FALSE},
3122 {"keepjumps", 5, FALSE},
3123 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01003124 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003125 {"leftabove", 5, FALSE},
3126 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003127 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003128 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003129 {"rightbelow", 6, FALSE},
3130 {"sandbox", 3, FALSE},
3131 {"silent", 3, FALSE},
3132 {"tab", 3, TRUE},
3133 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003134 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003135 {"verbose", 4, TRUE},
3136 {"vertical", 4, FALSE},
3137};
3138
3139/*
3140 * Return length of a command modifier (including optional count).
3141 * Return zero when it's not a modifier.
3142 */
3143 int
3144modifier_len(cmd)
3145 char_u *cmd;
3146{
3147 int i, j;
3148 char_u *p = cmd;
3149
3150 if (VIM_ISDIGIT(*cmd))
3151 p = skipwhite(skipdigits(cmd));
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003152 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003153 {
3154 for (j = 0; p[j] != NUL; ++j)
3155 if (p[j] != cmdmods[i].name[j])
3156 break;
Bram Moolenaar2d473ab2013-06-12 17:12:24 +02003157 if (!ASCII_ISALPHA(p[j]) && j >= cmdmods[i].minlen
Bram Moolenaared53fb92007-11-24 20:50:24 +00003158 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003159 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003160 }
3161 return 0;
3162}
3163
Bram Moolenaar071d4272004-06-13 20:20:40 +00003164/*
3165 * Return > 0 if an Ex command "name" exists.
3166 * Return 2 if there is an exact match.
3167 * Return 3 if there is an ambiguous match.
3168 */
3169 int
3170cmd_exists(name)
3171 char_u *name;
3172{
3173 exarg_T ea;
3174 int full = FALSE;
3175 int i;
3176 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003177 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003178
3179 /* Check command modifiers. */
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003180 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003181 {
3182 for (j = 0; name[j] != NUL; ++j)
3183 if (name[j] != cmdmods[i].name[j])
3184 break;
3185 if (name[j] == NUL && j >= cmdmods[i].minlen)
3186 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3187 }
3188
Bram Moolenaara9587612006-05-04 21:47:50 +00003189 /* Check built-in commands and user defined commands.
3190 * For ":2match" and ":3match" we need to skip the number. */
3191 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003192 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003193 p = find_command(&ea, &full);
3194 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003196 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3197 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003198 if (*skipwhite(p) != NUL)
3199 return 0; /* trailing garbage */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3201}
3202#endif
3203
3204/*
3205 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
3206 * we don't need/want deleted. Maybe this could be done better if we didn't
3207 * repeat all this stuff. The only problem is that they may not stay
3208 * perfectly compatible with each other, but then the command line syntax
3209 * probably won't change that much -- webb.
3210 */
3211 char_u *
3212set_one_cmd_context(xp, buff)
3213 expand_T *xp;
3214 char_u *buff; /* buffer for command string */
3215{
3216 char_u *p;
3217 char_u *cmd, *arg;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003218 int len = 0;
3219 exarg_T ea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003220#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3221 int compl = EXPAND_NOTHING;
3222#endif
3223#ifdef FEAT_CMDL_COMPL
3224 int delim;
3225#endif
3226 int forceit = FALSE;
3227 int usefilter = FALSE; /* filter instead of file name */
3228
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003229 ExpandInit(xp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230 xp->xp_pattern = buff;
3231 xp->xp_context = EXPAND_COMMANDS; /* Default until we get past command */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003232 ea.argt = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003233
3234/*
3235 * 2. skip comment lines and leading space, colons or bars
3236 */
3237 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
3238 ;
3239 xp->xp_pattern = cmd;
3240
3241 if (*cmd == NUL)
3242 return NULL;
3243 if (*cmd == '"') /* ignore comment lines */
3244 {
3245 xp->xp_context = EXPAND_NOTHING;
3246 return NULL;
3247 }
3248
3249/*
3250 * 3. parse a range specifier of the form: addr [,addr] [;addr] ..
3251 */
3252 cmd = skip_range(cmd, &xp->xp_context);
3253
3254/*
3255 * 4. parse command
3256 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003257 xp->xp_pattern = cmd;
3258 if (*cmd == NUL)
3259 return NULL;
3260 if (*cmd == '"')
3261 {
3262 xp->xp_context = EXPAND_NOTHING;
3263 return NULL;
3264 }
3265
3266 if (*cmd == '|' || *cmd == '\n')
3267 return cmd + 1; /* There's another command */
3268
3269 /*
3270 * Isolate the command and search for it in the command table.
3271 * Exceptions:
3272 * - the 'k' command can directly be followed by any character, but
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003273 * do accept "keepmarks", "keepalt" and "keepjumps".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003274 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
3275 */
3276 if (*cmd == 'k' && cmd[1] != 'e')
3277 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003278 ea.cmdidx = CMD_k;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279 p = cmd + 1;
3280 }
3281 else
3282 {
3283 p = cmd;
3284 while (ASCII_ISALPHA(*p) || *p == '*') /* Allow * wild card */
3285 ++p;
3286 /* check for non-alpha command */
3287 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3288 ++p;
Bram Moolenaar9f5d6002013-06-02 19:22:13 +02003289 /* for python 3.x: ":py3*" commands completion */
3290 if (cmd[0] == 'p' && cmd[1] == 'y' && p == cmd + 2 && *p == '3')
Bram Moolenaar893b2d72013-12-11 17:44:38 +01003291 {
Bram Moolenaar9f5d6002013-06-02 19:22:13 +02003292 ++p;
Bram Moolenaar893b2d72013-12-11 17:44:38 +01003293 while (ASCII_ISALPHA(*p) || *p == '*')
3294 ++p;
3295 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003296 len = (int)(p - cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003298 if (len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003299 {
3300 xp->xp_context = EXPAND_UNSUCCESSFUL;
3301 return NULL;
3302 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003303 for (ea.cmdidx = (cmdidx_T)0; (int)ea.cmdidx < (int)CMD_SIZE;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01003304 ea.cmdidx = (cmdidx_T)((int)ea.cmdidx + 1))
3305 if (STRNCMP(cmdnames[(int)ea.cmdidx].cmd_name, cmd,
3306 (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003307 break;
3308
3309#ifdef FEAT_USR_CMDS
3310 if (cmd[0] >= 'A' && cmd[0] <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311 while (ASCII_ISALNUM(*p) || *p == '*') /* Allow * wild card */
3312 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313#endif
3314 }
3315
3316 /*
3317 * If the cursor is touching the command, and it ends in an alpha-numeric
3318 * character, complete the command name.
3319 */
3320 if (*p == NUL && ASCII_ISALNUM(p[-1]))
3321 return NULL;
3322
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003323 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324 {
3325 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
3326 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003327 ea.cmdidx = CMD_substitute;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328 p = cmd + 1;
3329 }
3330#ifdef FEAT_USR_CMDS
3331 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
3332 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003333 ea.cmd = cmd;
3334 p = find_ucmd(&ea, p, NULL, xp,
3335# if defined(FEAT_CMDL_COMPL)
3336 &compl
3337# else
3338 NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003339# endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003340 );
Bram Moolenaarebefac62005-12-28 22:39:57 +00003341 if (p == NULL)
3342 ea.cmdidx = CMD_SIZE; /* ambiguous user command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343 }
3344#endif
3345 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003346 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003347 {
3348 /* Not still touching the command and it was an illegal one */
3349 xp->xp_context = EXPAND_UNSUCCESSFUL;
3350 return NULL;
3351 }
3352
3353 xp->xp_context = EXPAND_NOTHING; /* Default now that we're past command */
3354
3355 if (*p == '!') /* forced commands */
3356 {
3357 forceit = TRUE;
3358 ++p;
3359 }
3360
3361/*
3362 * 5. parse arguments
3363 */
3364#ifdef FEAT_USR_CMDS
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003365 if (!USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366#endif
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003367 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368
3369 arg = skipwhite(p);
3370
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003371 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372 {
3373 if (*arg == '>') /* append */
3374 {
3375 if (*++arg == '>')
3376 ++arg;
3377 arg = skipwhite(arg);
3378 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003379 else if (*arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380 {
3381 ++arg;
3382 usefilter = TRUE;
3383 }
3384 }
3385
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003386 if (ea.cmdidx == CMD_read)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387 {
3388 usefilter = forceit; /* :r! filter if forced */
3389 if (*arg == '!') /* :r !filter */
3390 {
3391 ++arg;
3392 usefilter = TRUE;
3393 }
3394 }
3395
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003396 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397 {
3398 while (*arg == *cmd) /* allow any number of '>' or '<' */
3399 ++arg;
3400 arg = skipwhite(arg);
3401 }
3402
3403 /* Does command allow "+command"? */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003404 if ((ea.argt & EDITCMD) && !usefilter && *arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405 {
3406 /* Check if we're in the +command */
3407 p = arg + 1;
3408 arg = skip_cmd_arg(arg, FALSE);
3409
3410 /* Still touching the command after '+'? */
3411 if (*arg == NUL)
3412 return p;
3413
3414 /* Skip space(s) after +command to get to the real argument */
3415 arg = skipwhite(arg);
3416 }
3417
3418 /*
3419 * Check for '|' to separate commands and '"' to start comments.
3420 * Don't do this for ":read !cmd" and ":write !cmd".
3421 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003422 if ((ea.argt & TRLBAR) && !usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423 {
3424 p = arg;
3425 /* ":redir @" is not the start of a comment */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003426 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427 p += 2;
3428 while (*p)
3429 {
3430 if (*p == Ctrl_V)
3431 {
3432 if (p[1] != NUL)
3433 ++p;
3434 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003435 else if ( (*p == '"' && !(ea.argt & NOTRLCOM))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436 || *p == '|' || *p == '\n')
3437 {
3438 if (*(p - 1) != '\\')
3439 {
3440 if (*p == '|' || *p == '\n')
3441 return p + 1;
3442 return NULL; /* It's a comment */
3443 }
3444 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003445 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446 }
3447 }
3448
3449 /* no arguments allowed */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003450 if (!(ea.argt & EXTRA) && *arg != NUL &&
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451 vim_strchr((char_u *)"|\"", *arg) == NULL)
3452 return NULL;
3453
3454 /* Find start of last argument (argument just before cursor): */
Bram Moolenaar848f8762012-07-25 17:22:23 +02003455 p = buff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456 xp->xp_pattern = p;
Bram Moolenaar09168a72012-08-02 21:24:42 +02003457 len = (int)STRLEN(buff);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003458 while (*p && p < buff + len)
3459 {
3460 if (*p == ' ' || *p == TAB)
3461 {
3462 /* argument starts after a space */
3463 xp->xp_pattern = ++p;
3464 }
3465 else
3466 {
3467 if (*p == '\\' && *(p + 1) != NUL)
3468 ++p; /* skip over escaped character */
3469 mb_ptr_adv(p);
3470 }
3471 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003473 if (ea.argt & XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003475 int c;
3476 int in_quote = FALSE;
3477 char_u *bow = NULL; /* Beginning of word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478
3479 /*
3480 * Allow spaces within back-quotes to count as part of the argument
3481 * being expanded.
3482 */
3483 xp->xp_pattern = skipwhite(arg);
Bram Moolenaar6529c102007-08-18 15:47:34 +00003484 p = xp->xp_pattern;
3485 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003487#ifdef FEAT_MBYTE
3488 if (has_mbyte)
3489 c = mb_ptr2char(p);
3490 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491#endif
Bram Moolenaar6529c102007-08-18 15:47:34 +00003492 c = *p;
3493 if (c == '\\' && p[1] != NUL)
3494 ++p;
3495 else if (c == '`')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496 {
3497 if (!in_quote)
3498 {
3499 xp->xp_pattern = p;
3500 bow = p + 1;
3501 }
3502 in_quote = !in_quote;
3503 }
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003504 /* An argument can contain just about everything, except
3505 * characters that end the command and white space. */
3506 else if (c == '|' || c == '\n' || c == '"' || (vim_iswhite(c)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003507#ifdef SPACE_IN_FILENAME
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003508 && (!(ea.argt & NOSPC) || usefilter)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003509#endif
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003510 ))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003511 {
Bram Moolenaarcf5a5b82008-02-26 20:30:12 +00003512 len = 0; /* avoid getting stuck when space is in 'isfname' */
Bram Moolenaar6529c102007-08-18 15:47:34 +00003513 while (*p != NUL)
3514 {
3515#ifdef FEAT_MBYTE
3516 if (has_mbyte)
3517 c = mb_ptr2char(p);
3518 else
3519#endif
3520 c = *p;
Bram Moolenaardd87969c2007-08-21 13:07:12 +00003521 if (c == '`' || vim_isfilec_or_wc(c))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003522 break;
3523#ifdef FEAT_MBYTE
3524 if (has_mbyte)
3525 len = (*mb_ptr2len)(p);
3526 else
3527#endif
3528 len = 1;
3529 mb_ptr_adv(p);
3530 }
3531 if (in_quote)
3532 bow = p;
3533 else
3534 xp->xp_pattern = p;
3535 p -= len;
3536 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003537 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538 }
3539
3540 /*
3541 * If we are still inside the quotes, and we passed a space, just
3542 * expand from there.
3543 */
3544 if (bow != NULL && in_quote)
3545 xp->xp_pattern = bow;
3546 xp->xp_context = EXPAND_FILES;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003547
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003548 /* For a shell command more chars need to be escaped. */
3549 if (usefilter || ea.cmdidx == CMD_bang)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003550 {
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003551#ifndef BACKSLASH_IN_FILENAME
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003552 xp->xp_shell = TRUE;
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003553#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003554 /* When still after the command name expand executables. */
3555 if (xp->xp_pattern == skipwhite(arg))
Bram Moolenaar5c5b0942007-05-06 12:07:59 +00003556 xp->xp_context = EXPAND_SHELLCMD;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003557 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558
3559 /* Check for environment variable */
3560 if (*xp->xp_pattern == '$'
3561#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
3562 || *xp->xp_pattern == '%'
3563#endif
3564 )
3565 {
3566 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
3567 if (!vim_isIDc(*p))
3568 break;
3569 if (*p == NUL)
3570 {
3571 xp->xp_context = EXPAND_ENV_VARS;
3572 ++xp->xp_pattern;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003573#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3574 /* Avoid that the assignment uses EXPAND_FILES again. */
Bram Moolenaara466c992005-07-09 21:03:22 +00003575 if (compl != EXPAND_USER_DEFINED && compl != EXPAND_USER_LIST)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003576 compl = EXPAND_ENV_VARS;
3577#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578 }
3579 }
Bram Moolenaar24305862012-08-15 14:05:05 +02003580#if defined(FEAT_CMDL_COMPL)
3581 /* Check for user names */
3582 if (*xp->xp_pattern == '~')
3583 {
3584 for (p = xp->xp_pattern + 1; *p != NUL && *p != '/'; ++p)
3585 ;
3586 /* Complete ~user only if it partially matches a user name.
3587 * A full match ~user<Tab> will be replaced by user's home
3588 * directory i.e. something like ~user<Tab> -> /home/user/ */
3589 if (*p == NUL && p > xp->xp_pattern + 1
3590 && match_user(xp->xp_pattern + 1) == 1)
3591 {
3592 xp->xp_context = EXPAND_USER;
3593 ++xp->xp_pattern;
3594 }
3595 }
3596#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597 }
3598
3599/*
3600 * 6. switch on command name
3601 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003602 switch (ea.cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003604 case CMD_find:
3605 case CMD_sfind:
3606 case CMD_tabfind:
Bram Moolenaarc24b6972010-08-16 22:34:29 +02003607 if (xp->xp_context == EXPAND_FILES)
3608 xp->xp_context = EXPAND_FILES_IN_PATH;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003609 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610 case CMD_cd:
3611 case CMD_chdir:
3612 case CMD_lcd:
3613 case CMD_lchdir:
3614 if (xp->xp_context == EXPAND_FILES)
3615 xp->xp_context = EXPAND_DIRECTORIES;
3616 break;
3617 case CMD_help:
3618 xp->xp_context = EXPAND_HELP;
3619 xp->xp_pattern = arg;
3620 break;
3621
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003622 /* Command modifiers: return the argument.
3623 * Also for commands with an argument that is a command. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624 case CMD_aboveleft:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003625 case CMD_argdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626 case CMD_belowright:
3627 case CMD_botright:
3628 case CMD_browse:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003629 case CMD_bufdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630 case CMD_confirm:
Bram Moolenaardf177f62005-02-22 08:39:57 +00003631 case CMD_debug:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003632 case CMD_folddoclosed:
3633 case CMD_folddoopen:
3634 case CMD_hide:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003635 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636 case CMD_keepjumps:
3637 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01003638 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639 case CMD_leftabove:
3640 case CMD_lockmarks:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003641 case CMD_noautocmd:
3642 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643 case CMD_rightbelow:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003644 case CMD_sandbox:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645 case CMD_silent:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003646 case CMD_tab:
Bram Moolenaar70baa402013-06-16 16:14:03 +02003647 case CMD_tabdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003648 case CMD_topleft:
3649 case CMD_verbose:
3650 case CMD_vertical:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003651 case CMD_windo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003652 return arg;
3653
Bram Moolenaar4f688582007-07-24 12:34:30 +00003654#ifdef FEAT_CMDL_COMPL
3655# ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar071d4272004-06-13 20:20:40 +00003656 case CMD_match:
3657 if (*arg == NUL || !ends_excmd(*arg))
3658 {
Bram Moolenaar4f688582007-07-24 12:34:30 +00003659 /* also complete "None" */
3660 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003661 arg = skipwhite(skiptowhite(arg));
3662 if (*arg != NUL)
3663 {
3664 xp->xp_context = EXPAND_NOTHING;
3665 arg = skip_regexp(arg + 1, *arg, p_magic, NULL);
3666 }
3667 }
3668 return find_nextcmd(arg);
Bram Moolenaar4f688582007-07-24 12:34:30 +00003669# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003670
Bram Moolenaar071d4272004-06-13 20:20:40 +00003671/*
3672 * All completion for the +cmdline_compl feature goes here.
3673 */
3674
3675# ifdef FEAT_USR_CMDS
3676 case CMD_command:
3677 /* Check for attributes */
3678 while (*arg == '-')
3679 {
3680 arg++; /* Skip "-" */
3681 p = skiptowhite(arg);
3682 if (*p == NUL)
3683 {
3684 /* Cursor is still in the attribute */
3685 p = vim_strchr(arg, '=');
3686 if (p == NULL)
3687 {
3688 /* No "=", so complete attribute names */
3689 xp->xp_context = EXPAND_USER_CMD_FLAGS;
3690 xp->xp_pattern = arg;
3691 return NULL;
3692 }
3693
3694 /* For the -complete and -nargs attributes, we complete
3695 * their arguments as well.
3696 */
3697 if (STRNICMP(arg, "complete", p - arg) == 0)
3698 {
3699 xp->xp_context = EXPAND_USER_COMPLETE;
3700 xp->xp_pattern = p + 1;
3701 return NULL;
3702 }
3703 else if (STRNICMP(arg, "nargs", p - arg) == 0)
3704 {
3705 xp->xp_context = EXPAND_USER_NARGS;
3706 xp->xp_pattern = p + 1;
3707 return NULL;
3708 }
3709 return NULL;
3710 }
3711 arg = skipwhite(p);
3712 }
3713
3714 /* After the attributes comes the new command name */
3715 p = skiptowhite(arg);
3716 if (*p == NUL)
3717 {
3718 xp->xp_context = EXPAND_USER_COMMANDS;
3719 xp->xp_pattern = arg;
3720 break;
3721 }
3722
3723 /* And finally comes a normal command */
3724 return skipwhite(p);
3725
3726 case CMD_delcommand:
3727 xp->xp_context = EXPAND_USER_COMMANDS;
3728 xp->xp_pattern = arg;
3729 break;
3730# endif
3731
3732 case CMD_global:
3733 case CMD_vglobal:
3734 delim = *arg; /* get the delimiter */
3735 if (delim)
3736 ++arg; /* skip delimiter if there is one */
3737
3738 while (arg[0] != NUL && arg[0] != delim)
3739 {
3740 if (arg[0] == '\\' && arg[1] != NUL)
3741 ++arg;
3742 ++arg;
3743 }
3744 if (arg[0] != NUL)
3745 return arg + 1;
3746 break;
3747 case CMD_and:
3748 case CMD_substitute:
3749 delim = *arg;
3750 if (delim)
3751 {
3752 /* skip "from" part */
3753 ++arg;
3754 arg = skip_regexp(arg, delim, p_magic, NULL);
3755 }
3756 /* skip "to" part */
3757 while (arg[0] != NUL && arg[0] != delim)
3758 {
3759 if (arg[0] == '\\' && arg[1] != NUL)
3760 ++arg;
3761 ++arg;
3762 }
3763 if (arg[0] != NUL) /* skip delimiter */
3764 ++arg;
3765 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
3766 ++arg;
3767 if (arg[0] != NUL)
3768 return arg;
3769 break;
3770 case CMD_isearch:
3771 case CMD_dsearch:
3772 case CMD_ilist:
3773 case CMD_dlist:
3774 case CMD_ijump:
3775 case CMD_psearch:
3776 case CMD_djump:
3777 case CMD_isplit:
3778 case CMD_dsplit:
3779 arg = skipwhite(skipdigits(arg)); /* skip count */
3780 if (*arg == '/') /* Match regexp, not just whole words */
3781 {
3782 for (++arg; *arg && *arg != '/'; arg++)
3783 if (*arg == '\\' && arg[1] != NUL)
3784 arg++;
3785 if (*arg)
3786 {
3787 arg = skipwhite(arg + 1);
3788
3789 /* Check for trailing illegal characters */
3790 if (*arg && vim_strchr((char_u *)"|\"\n", *arg) == NULL)
3791 xp->xp_context = EXPAND_NOTHING;
3792 else
3793 return arg;
3794 }
3795 }
3796 break;
3797#ifdef FEAT_AUTOCMD
3798 case CMD_autocmd:
3799 return set_context_in_autocmd(xp, arg, FALSE);
3800
3801 case CMD_doautocmd:
Bram Moolenaar73a9d7b2008-11-06 16:16:44 +00003802 case CMD_doautoall:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803 return set_context_in_autocmd(xp, arg, TRUE);
3804#endif
3805 case CMD_set:
3806 set_context_in_set_cmd(xp, arg, 0);
3807 break;
3808 case CMD_setglobal:
3809 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
3810 break;
3811 case CMD_setlocal:
3812 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
3813 break;
3814 case CMD_tag:
3815 case CMD_stag:
3816 case CMD_ptag:
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00003817 case CMD_ltag:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818 case CMD_tselect:
3819 case CMD_stselect:
3820 case CMD_ptselect:
3821 case CMD_tjump:
3822 case CMD_stjump:
3823 case CMD_ptjump:
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003824 if (*p_wop != NUL)
3825 xp->xp_context = EXPAND_TAGS_LISTFILES;
3826 else
3827 xp->xp_context = EXPAND_TAGS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003828 xp->xp_pattern = arg;
3829 break;
3830 case CMD_augroup:
3831 xp->xp_context = EXPAND_AUGROUP;
3832 xp->xp_pattern = arg;
3833 break;
3834#ifdef FEAT_SYN_HL
3835 case CMD_syntax:
3836 set_context_in_syntax_cmd(xp, arg);
3837 break;
3838#endif
3839#ifdef FEAT_EVAL
3840 case CMD_let:
3841 case CMD_if:
3842 case CMD_elseif:
3843 case CMD_while:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00003844 case CMD_for:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003845 case CMD_echo:
3846 case CMD_echon:
3847 case CMD_execute:
3848 case CMD_echomsg:
3849 case CMD_echoerr:
3850 case CMD_call:
3851 case CMD_return:
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003852 set_context_for_expression(xp, arg, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003853 break;
3854
3855 case CMD_unlet:
3856 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3857 arg = xp->xp_pattern + 1;
3858 xp->xp_context = EXPAND_USER_VARS;
3859 xp->xp_pattern = arg;
3860 break;
3861
3862 case CMD_function:
3863 case CMD_delfunction:
3864 xp->xp_context = EXPAND_USER_FUNC;
3865 xp->xp_pattern = arg;
3866 break;
3867
3868 case CMD_echohl:
Bram Moolenaar4f688582007-07-24 12:34:30 +00003869 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870 break;
3871#endif
3872 case CMD_highlight:
3873 set_context_in_highlight_cmd(xp, arg);
3874 break;
Bram Moolenaarf4580d82009-03-18 11:52:53 +00003875#ifdef FEAT_CSCOPE
3876 case CMD_cscope:
Bram Moolenaar7bfef802009-04-22 14:25:01 +00003877 case CMD_lcscope:
3878 case CMD_scscope:
3879 set_context_in_cscope_cmd(xp, arg, ea.cmdidx);
Bram Moolenaarf4580d82009-03-18 11:52:53 +00003880 break;
3881#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00003882#ifdef FEAT_SIGNS
3883 case CMD_sign:
3884 set_context_in_sign_cmd(xp, arg);
3885 break;
3886#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887#ifdef FEAT_LISTCMDS
3888 case CMD_bdelete:
3889 case CMD_bwipeout:
3890 case CMD_bunload:
3891 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3892 arg = xp->xp_pattern + 1;
3893 /*FALLTHROUGH*/
3894 case CMD_buffer:
3895 case CMD_sbuffer:
3896 case CMD_checktime:
3897 xp->xp_context = EXPAND_BUFFERS;
3898 xp->xp_pattern = arg;
3899 break;
3900#endif
3901#ifdef FEAT_USR_CMDS
3902 case CMD_USER:
3903 case CMD_USER_BUF:
3904 if (compl != EXPAND_NOTHING)
3905 {
3906 /* XFILE: file names are handled above */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003907 if (!(ea.argt & XFILE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003908 {
3909# ifdef FEAT_MENU
3910 if (compl == EXPAND_MENUS)
3911 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
3912# endif
3913 if (compl == EXPAND_COMMANDS)
3914 return arg;
3915 if (compl == EXPAND_MAPPINGS)
3916 return set_context_in_map_cmd(xp, (char_u *)"map",
3917 arg, forceit, FALSE, FALSE, CMD_map);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003918 /* Find start of last argument. */
3919 p = arg;
3920 while (*p)
3921 {
3922 if (*p == ' ')
Bram Moolenaar848f8762012-07-25 17:22:23 +02003923 /* argument starts after a space */
3924 arg = p + 1;
Bram Moolenaara07c8312012-07-27 21:12:07 +02003925 else if (*p == '\\' && *(p + 1) != NUL)
3926 ++p; /* skip over escaped character */
3927 mb_ptr_adv(p);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003928 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929 xp->xp_pattern = arg;
3930 }
3931 xp->xp_context = compl;
3932 }
3933 break;
3934#endif
3935 case CMD_map: case CMD_noremap:
3936 case CMD_nmap: case CMD_nnoremap:
3937 case CMD_vmap: case CMD_vnoremap:
3938 case CMD_omap: case CMD_onoremap:
3939 case CMD_imap: case CMD_inoremap:
3940 case CMD_cmap: case CMD_cnoremap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003941 case CMD_lmap: case CMD_lnoremap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02003942 case CMD_smap: case CMD_snoremap:
3943 case CMD_xmap: case CMD_xnoremap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003944 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003945 FALSE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946 case CMD_unmap:
3947 case CMD_nunmap:
3948 case CMD_vunmap:
3949 case CMD_ounmap:
3950 case CMD_iunmap:
3951 case CMD_cunmap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003952 case CMD_lunmap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02003953 case CMD_sunmap:
3954 case CMD_xunmap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003955 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003956 FALSE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003957 case CMD_abbreviate: case CMD_noreabbrev:
3958 case CMD_cabbrev: case CMD_cnoreabbrev:
3959 case CMD_iabbrev: case CMD_inoreabbrev:
3960 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003961 TRUE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003962 case CMD_unabbreviate:
3963 case CMD_cunabbrev:
3964 case CMD_iunabbrev:
3965 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003966 TRUE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003967#ifdef FEAT_MENU
3968 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
3969 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
3970 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
3971 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
3972 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
3973 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
3974 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
3975 case CMD_tmenu: case CMD_tunmenu:
3976 case CMD_popup: case CMD_tearoff: case CMD_emenu:
3977 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
3978#endif
3979
3980 case CMD_colorscheme:
3981 xp->xp_context = EXPAND_COLORS;
3982 xp->xp_pattern = arg;
3983 break;
3984
3985 case CMD_compiler:
3986 xp->xp_context = EXPAND_COMPILER;
3987 xp->xp_pattern = arg;
3988 break;
3989
Bram Moolenaar883f5d02010-06-21 06:24:34 +02003990 case CMD_ownsyntax:
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02003991 xp->xp_context = EXPAND_OWNSYNTAX;
3992 xp->xp_pattern = arg;
3993 break;
3994
3995 case CMD_setfiletype:
Bram Moolenaar883f5d02010-06-21 06:24:34 +02003996 xp->xp_context = EXPAND_FILETYPE;
3997 xp->xp_pattern = arg;
3998 break;
3999
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
4001 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
4002 case CMD_language:
Bram Moolenaara660dc82011-05-25 12:51:22 +02004003 p = skiptowhite(arg);
4004 if (*p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005 {
4006 xp->xp_context = EXPAND_LANGUAGE;
4007 xp->xp_pattern = arg;
4008 }
4009 else
Bram Moolenaara660dc82011-05-25 12:51:22 +02004010 {
4011 if ( STRNCMP(arg, "messages", p - arg) == 0
4012 || STRNCMP(arg, "ctype", p - arg) == 0
4013 || STRNCMP(arg, "time", p - arg) == 0)
4014 {
4015 xp->xp_context = EXPAND_LOCALES;
4016 xp->xp_pattern = skipwhite(p);
4017 }
4018 else
4019 xp->xp_context = EXPAND_NOTHING;
4020 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004021 break;
4022#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01004023#if defined(FEAT_PROFILE)
4024 case CMD_profile:
4025 set_context_in_profile_cmd(xp, arg);
4026 break;
4027#endif
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01004028 case CMD_behave:
4029 xp->xp_context = EXPAND_BEHAVE;
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004030 xp->xp_pattern = arg;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01004031 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004032
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004033#if defined(FEAT_CMDHIST)
4034 case CMD_history:
4035 xp->xp_context = EXPAND_HISTORY;
4036 xp->xp_pattern = arg;
4037 break;
4038#endif
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02004039#if defined(FEAT_PROFILE)
4040 case CMD_syntime:
4041 xp->xp_context = EXPAND_SYNTIME;
4042 xp->xp_pattern = arg;
4043 break;
4044#endif
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004045
Bram Moolenaar071d4272004-06-13 20:20:40 +00004046#endif /* FEAT_CMDL_COMPL */
4047
4048 default:
4049 break;
4050 }
4051 return NULL;
4052}
4053
4054/*
4055 * skip a range specifier of the form: addr [,addr] [;addr] ..
4056 *
4057 * Backslashed delimiters after / or ? will be skipped, and commands will
4058 * not be expanded between /'s and ?'s or after "'".
4059 *
Bram Moolenaardf177f62005-02-22 08:39:57 +00004060 * Also skip white space and ":" characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061 * Returns the "cmd" pointer advanced to beyond the range.
4062 */
4063 char_u *
4064skip_range(cmd, ctx)
4065 char_u *cmd;
4066 int *ctx; /* pointer to xp_context or NULL */
4067{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004068 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069
Bram Moolenaardf177f62005-02-22 08:39:57 +00004070 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071 {
4072 if (*cmd == '\'')
4073 {
4074 if (*++cmd == NUL && ctx != NULL)
4075 *ctx = EXPAND_NOTHING;
4076 }
4077 else if (*cmd == '/' || *cmd == '?')
4078 {
4079 delim = *cmd++;
4080 while (*cmd != NUL && *cmd != delim)
4081 if (*cmd++ == '\\' && *cmd != NUL)
4082 ++cmd;
4083 if (*cmd == NUL && ctx != NULL)
4084 *ctx = EXPAND_NOTHING;
4085 }
4086 if (*cmd != NUL)
4087 ++cmd;
4088 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004089
4090 /* Skip ":" and white space. */
4091 while (*cmd == ':')
4092 cmd = skipwhite(cmd + 1);
4093
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094 return cmd;
4095}
4096
4097/*
4098 * get a single EX address
4099 *
4100 * Set ptr to the next character after the part that was interpreted.
4101 * Set ptr to NULL when an error is encountered.
4102 *
4103 * Return MAXLNUM when no Ex address was found.
4104 */
4105 static linenr_T
4106get_address(ptr, skip, to_other_file)
4107 char_u **ptr;
4108 int skip; /* only skip the address, don't use it */
4109 int to_other_file; /* flag: may jump to other file */
4110{
4111 int c;
4112 int i;
4113 long n;
4114 char_u *cmd;
4115 pos_T pos;
4116 pos_T *fp;
4117 linenr_T lnum;
4118
4119 cmd = skipwhite(*ptr);
4120 lnum = MAXLNUM;
4121 do
4122 {
4123 switch (*cmd)
4124 {
4125 case '.': /* '.' - Cursor position */
4126 ++cmd;
4127 lnum = curwin->w_cursor.lnum;
4128 break;
4129
4130 case '$': /* '$' - last line */
4131 ++cmd;
4132 lnum = curbuf->b_ml.ml_line_count;
4133 break;
4134
4135 case '\'': /* ''' - mark */
4136 if (*++cmd == NUL)
4137 {
4138 cmd = NULL;
4139 goto error;
4140 }
4141 if (skip)
4142 ++cmd;
4143 else
4144 {
4145 /* Only accept a mark in another file when it is
4146 * used by itself: ":'M". */
4147 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
4148 ++cmd;
4149 if (fp == (pos_T *)-1)
4150 /* Jumped to another file. */
4151 lnum = curwin->w_cursor.lnum;
4152 else
4153 {
4154 if (check_mark(fp) == FAIL)
4155 {
4156 cmd = NULL;
4157 goto error;
4158 }
4159 lnum = fp->lnum;
4160 }
4161 }
4162 break;
4163
4164 case '/':
4165 case '?': /* '/' or '?' - search */
4166 c = *cmd++;
4167 if (skip) /* skip "/pat/" */
4168 {
4169 cmd = skip_regexp(cmd, c, (int)p_magic, NULL);
4170 if (*cmd == c)
4171 ++cmd;
4172 }
4173 else
4174 {
4175 pos = curwin->w_cursor; /* save curwin->w_cursor */
4176 /*
4177 * When '/' or '?' follows another address, start
4178 * from there.
4179 */
4180 if (lnum != MAXLNUM)
4181 curwin->w_cursor.lnum = lnum;
4182 /*
4183 * Start a forward search at the end of the line.
4184 * Start a backward search at the start of the line.
4185 * This makes sure we never match in the current
4186 * line, and can match anywhere in the
4187 * next/previous line.
4188 */
4189 if (c == '/')
4190 curwin->w_cursor.col = MAXCOL;
4191 else
4192 curwin->w_cursor.col = 0;
4193 searchcmdlen = 0;
4194 if (!do_search(NULL, c, cmd, 1L,
Bram Moolenaaraad86642008-03-10 20:34:59 +00004195 SEARCH_HIS | SEARCH_MSG, NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004196 {
4197 curwin->w_cursor = pos;
4198 cmd = NULL;
4199 goto error;
4200 }
4201 lnum = curwin->w_cursor.lnum;
4202 curwin->w_cursor = pos;
4203 /* adjust command string pointer */
4204 cmd += searchcmdlen;
4205 }
4206 break;
4207
4208 case '\\': /* "\?", "\/" or "\&", repeat search */
4209 ++cmd;
4210 if (*cmd == '&')
4211 i = RE_SUBST;
4212 else if (*cmd == '?' || *cmd == '/')
4213 i = RE_SEARCH;
4214 else
4215 {
4216 EMSG(_(e_backslash));
4217 cmd = NULL;
4218 goto error;
4219 }
4220
4221 if (!skip)
4222 {
4223 /*
4224 * When search follows another address, start from
4225 * there.
4226 */
4227 if (lnum != MAXLNUM)
4228 pos.lnum = lnum;
4229 else
4230 pos.lnum = curwin->w_cursor.lnum;
4231
4232 /*
4233 * Start the search just like for the above
4234 * do_search().
4235 */
4236 if (*cmd != '?')
4237 pos.col = MAXCOL;
4238 else
4239 pos.col = 0;
4240 if (searchit(curwin, curbuf, &pos,
4241 *cmd == '?' ? BACKWARD : FORWARD,
Bram Moolenaaraad86642008-03-10 20:34:59 +00004242 (char_u *)"", 1L, SEARCH_MSG,
Bram Moolenaar76929292008-01-06 19:07:36 +00004243 i, (linenr_T)0, NULL) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244 lnum = pos.lnum;
4245 else
4246 {
4247 cmd = NULL;
4248 goto error;
4249 }
4250 }
4251 ++cmd;
4252 break;
4253
4254 default:
4255 if (VIM_ISDIGIT(*cmd)) /* absolute line number */
4256 lnum = getdigits(&cmd);
4257 }
4258
4259 for (;;)
4260 {
4261 cmd = skipwhite(cmd);
4262 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4263 break;
4264
4265 if (lnum == MAXLNUM)
4266 lnum = curwin->w_cursor.lnum; /* "+1" is same as ".+1" */
4267 if (VIM_ISDIGIT(*cmd))
4268 i = '+'; /* "number" is same as "+number" */
4269 else
4270 i = *cmd++;
4271 if (!VIM_ISDIGIT(*cmd)) /* '+' is '+1', but '+0' is not '+1' */
4272 n = 1;
4273 else
4274 n = getdigits(&cmd);
4275 if (i == '-')
4276 lnum -= n;
4277 else
4278 lnum += n;
4279 }
4280 } while (*cmd == '/' || *cmd == '?');
4281
4282error:
4283 *ptr = cmd;
4284 return lnum;
4285}
4286
4287/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004288 * Get flags from an Ex command argument.
4289 */
4290 static void
4291get_flags(eap)
4292 exarg_T *eap;
4293{
4294 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4295 {
4296 if (*eap->arg == 'l')
4297 eap->flags |= EXFLAG_LIST;
4298 else if (*eap->arg == 'p')
4299 eap->flags |= EXFLAG_PRINT;
4300 else
4301 eap->flags |= EXFLAG_NR;
4302 eap->arg = skipwhite(eap->arg + 1);
4303 }
4304}
4305
4306/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307 * Function called for command which is Not Implemented. NI!
4308 */
4309 void
4310ex_ni(eap)
4311 exarg_T *eap;
4312{
4313 if (!eap->skip)
4314 eap->errmsg = (char_u *)N_("E319: Sorry, the command is not available in this version");
4315}
4316
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004317#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004318/*
4319 * Function called for script command which is Not Implemented. NI!
4320 * Skips over ":perl <<EOF" constructs.
4321 */
4322 static void
4323ex_script_ni(eap)
4324 exarg_T *eap;
4325{
4326 if (!eap->skip)
4327 ex_ni(eap);
4328 else
4329 vim_free(script_get(eap, eap->arg));
4330}
4331#endif
4332
4333/*
4334 * Check range in Ex command for validity.
4335 * Return NULL when valid, error message when invalid.
4336 */
4337 static char_u *
4338invalid_range(eap)
4339 exarg_T *eap;
4340{
4341 if ( eap->line1 < 0
4342 || eap->line2 < 0
4343 || eap->line1 > eap->line2
4344 || ((eap->argt & RANGE)
4345 && !(eap->argt & NOTADR)
4346 && eap->line2 > curbuf->b_ml.ml_line_count
4347#ifdef FEAT_DIFF
4348 + (eap->cmdidx == CMD_diffget)
4349#endif
4350 ))
4351 return (char_u *)_(e_invrange);
4352 return NULL;
4353}
4354
4355/*
4356 * Correct the range for zero line number, if required.
4357 */
4358 static void
4359correct_range(eap)
4360 exarg_T *eap;
4361{
4362 if (!(eap->argt & ZEROR)) /* zero in range not allowed */
4363 {
4364 if (eap->line1 == 0)
4365 eap->line1 = 1;
4366 if (eap->line2 == 0)
4367 eap->line2 = 1;
4368 }
4369}
4370
Bram Moolenaar748bf032005-02-02 23:04:36 +00004371#ifdef FEAT_QUICKFIX
4372static char_u *skip_grep_pat __ARGS((exarg_T *eap));
4373
4374/*
4375 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4376 * pattern. Otherwise return eap->arg.
4377 */
4378 static char_u *
4379skip_grep_pat(eap)
4380 exarg_T *eap;
4381{
4382 char_u *p = eap->arg;
4383
Bram Moolenaara37420f2006-02-04 22:37:47 +00004384 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4385 || eap->cmdidx == CMD_vimgrepadd
4386 || eap->cmdidx == CMD_lvimgrepadd
4387 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004388 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004389 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004390 if (p == NULL)
4391 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004392 }
4393 return p;
4394}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004395
4396/*
4397 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4398 * in the command line, so that things like % get expanded.
4399 */
4400 static char_u *
4401replace_makeprg(eap, p, cmdlinep)
4402 exarg_T *eap;
4403 char_u *p;
4404 char_u **cmdlinep;
4405{
4406 char_u *new_cmdline;
4407 char_u *program;
4408 char_u *pos;
4409 char_u *ptr;
4410 int len;
4411 int i;
4412
4413 /*
4414 * Don't do it when ":vimgrep" is used for ":grep".
4415 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004416 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4417 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4418 || eap->cmdidx == CMD_grepadd
4419 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004420 && !grep_internal(eap->cmdidx))
4421 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004422 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4423 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004424 {
4425 if (*curbuf->b_p_gp == NUL)
4426 program = p_gp;
4427 else
4428 program = curbuf->b_p_gp;
4429 }
4430 else
4431 {
4432 if (*curbuf->b_p_mp == NUL)
4433 program = p_mp;
4434 else
4435 program = curbuf->b_p_mp;
4436 }
4437
4438 p = skipwhite(p);
4439
4440 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4441 {
4442 /* replace $* by given arguments */
4443 i = 1;
4444 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4445 ++i;
4446 len = (int)STRLEN(p);
4447 new_cmdline = alloc((int)(STRLEN(program) + i * (len - 2) + 1));
4448 if (new_cmdline == NULL)
4449 return NULL; /* out of memory */
4450 ptr = new_cmdline;
4451 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4452 {
4453 i = (int)(pos - program);
4454 STRNCPY(ptr, program, i);
4455 STRCPY(ptr += i, p);
4456 ptr += len;
4457 program = pos + 2;
4458 }
4459 STRCPY(ptr, program);
4460 }
4461 else
4462 {
4463 new_cmdline = alloc((int)(STRLEN(program) + STRLEN(p) + 2));
4464 if (new_cmdline == NULL)
4465 return NULL; /* out of memory */
4466 STRCPY(new_cmdline, program);
4467 STRCAT(new_cmdline, " ");
4468 STRCAT(new_cmdline, p);
4469 }
4470 msg_make(p);
4471
4472 /* 'eap->cmd' is not set here, because it is not used at CMD_make */
4473 vim_free(*cmdlinep);
4474 *cmdlinep = new_cmdline;
4475 p = new_cmdline;
4476 }
4477 return p;
4478}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004479#endif
4480
Bram Moolenaar071d4272004-06-13 20:20:40 +00004481/*
4482 * Expand file name in Ex command argument.
4483 * Return FAIL for failure, OK otherwise.
4484 */
4485 int
4486expand_filename(eap, cmdlinep, errormsgp)
4487 exarg_T *eap;
4488 char_u **cmdlinep;
4489 char_u **errormsgp;
4490{
4491 int has_wildcards; /* need to expand wildcards */
4492 char_u *repl;
4493 int srclen;
4494 char_u *p;
4495 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004496 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004497
Bram Moolenaar748bf032005-02-02 23:04:36 +00004498#ifdef FEAT_QUICKFIX
4499 /* Skip a regexp pattern for ":vimgrep[add] pat file..." */
4500 p = skip_grep_pat(eap);
4501#else
4502 p = eap->arg;
4503#endif
4504
Bram Moolenaar071d4272004-06-13 20:20:40 +00004505 /*
4506 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4507 * the file name contains a wildcard it should not cause expanding.
4508 * (it will be expanded anyway if there is a wildcard before replacing).
4509 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004510 has_wildcards = mch_has_wildcard(p);
4511 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004512 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004513#ifdef FEAT_EVAL
4514 /* Skip over `=expr`, wildcards in it are not expanded. */
4515 if (p[0] == '`' && p[1] == '=')
4516 {
4517 p += 2;
4518 (void)skip_expr(&p);
4519 if (*p == '`')
4520 ++p;
4521 continue;
4522 }
4523#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004524 /*
4525 * Quick check if this cannot be the start of a special string.
4526 * Also removes backslash before '%', '#' and '<'.
4527 */
4528 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4529 {
4530 ++p;
4531 continue;
4532 }
4533
4534 /*
4535 * Try to find a match at this position.
4536 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004537 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
4538 errormsgp, &escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004539 if (*errormsgp != NULL) /* error detected */
4540 return FAIL;
4541 if (repl == NULL) /* no match found */
4542 {
4543 p += srclen;
4544 continue;
4545 }
4546
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004547 /* Wildcards won't be expanded below, the replacement is taken
4548 * literally. But do expand "~/file", "~user/file" and "$HOME/file". */
4549 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4550 {
4551 char_u *l = repl;
4552
4553 repl = expand_env_save(repl);
4554 vim_free(l);
4555 }
4556
Bram Moolenaar63b92542007-03-27 14:57:09 +00004557 /* Need to escape white space et al. with a backslash.
4558 * Don't do this for:
4559 * - replacement that already has been escaped: "##"
4560 * - shell commands (may have to use quotes instead).
4561 * - non-unix systems when there is a single argument (spaces don't
4562 * separate arguments then).
4563 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004564 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00004565 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00004566 && eap->cmdidx != CMD_bang
4567 && eap->cmdidx != CMD_make
Bram Moolenaara37420f2006-02-04 22:37:47 +00004568 && eap->cmdidx != CMD_lmake
Bram Moolenaar071d4272004-06-13 20:20:40 +00004569 && eap->cmdidx != CMD_grep
Bram Moolenaara37420f2006-02-04 22:37:47 +00004570 && eap->cmdidx != CMD_lgrep
Bram Moolenaar071d4272004-06-13 20:20:40 +00004571 && eap->cmdidx != CMD_grepadd
Bram Moolenaara37420f2006-02-04 22:37:47 +00004572 && eap->cmdidx != CMD_lgrepadd
Bram Moolenaar071d4272004-06-13 20:20:40 +00004573#ifndef UNIX
4574 && !(eap->argt & NOSPC)
4575#endif
4576 )
4577 {
4578 char_u *l;
4579#ifdef BACKSLASH_IN_FILENAME
4580 /* Don't escape a backslash here, because rem_backslash() doesn't
4581 * remove it later. */
4582 static char_u *nobslash = (char_u *)" \t\"|";
4583# define ESCAPE_CHARS nobslash
4584#else
4585# define ESCAPE_CHARS escape_chars
4586#endif
4587
4588 for (l = repl; *l; ++l)
4589 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
4590 {
4591 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
4592 if (l != NULL)
4593 {
4594 vim_free(repl);
4595 repl = l;
4596 }
4597 break;
4598 }
4599 }
4600
4601 /* For a shell command a '!' must be escaped. */
4602 if ((eap->usefilter || eap->cmdidx == CMD_bang)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004603 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604 {
4605 char_u *l;
4606
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004607 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004608 if (l != NULL)
4609 {
4610 vim_free(repl);
4611 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004612 }
4613 }
4614
4615 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
4616 vim_free(repl);
4617 if (p == NULL)
4618 return FAIL;
4619 }
4620
4621 /*
4622 * One file argument: Expand wildcards.
4623 * Don't do this with ":r !command" or ":w !command".
4624 */
4625 if ((eap->argt & NOSPC) && !eap->usefilter)
4626 {
4627 /*
4628 * May do this twice:
4629 * 1. Replace environment variables.
4630 * 2. Replace any other wildcards, remove backslashes.
4631 */
4632 for (n = 1; n <= 2; ++n)
4633 {
4634 if (n == 2)
4635 {
4636#ifdef UNIX
4637 /*
4638 * Only for Unix we check for more than one file name.
4639 * For other systems spaces are considered to be part
4640 * of the file name.
4641 * Only check here if there is no wildcard, otherwise
4642 * ExpandOne() will check for errors. This allows
4643 * ":e `ls ve*.c`" on Unix.
4644 */
4645 if (!has_wildcards)
4646 for (p = eap->arg; *p; ++p)
4647 {
4648 /* skip escaped characters */
4649 if (p[1] && (*p == '\\' || *p == Ctrl_V))
4650 ++p;
4651 else if (vim_iswhite(*p))
4652 {
4653 *errormsgp = (char_u *)_("E172: Only one file name allowed");
4654 return FAIL;
4655 }
4656 }
4657#endif
4658
4659 /*
4660 * Halve the number of backslashes (this is Vi compatible).
4661 * For Unix and OS/2, when wildcards are expanded, this is
4662 * done by ExpandOne() below.
4663 */
4664#if defined(UNIX) || defined(OS2)
4665 if (!has_wildcards)
4666#endif
4667 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004668 }
4669
4670 if (has_wildcards)
4671 {
4672 if (n == 1)
4673 {
4674 /*
4675 * First loop: May expand environment variables. This
4676 * can be done much faster with expand_env() than with
4677 * something else (e.g., calling a shell).
4678 * After expanding environment variables, check again
4679 * if there are still wildcards present.
4680 */
4681 if (vim_strchr(eap->arg, '$') != NULL
4682 || vim_strchr(eap->arg, '~') != NULL)
4683 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00004684 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004685 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004686 has_wildcards = mch_has_wildcard(NameBuff);
4687 p = NameBuff;
4688 }
4689 else
4690 p = NULL;
4691 }
4692 else /* n == 2 */
4693 {
4694 expand_T xpc;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004695 int options = WILD_LIST_NOTFOUND|WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004696
4697 ExpandInit(&xpc);
4698 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004699 if (p_wic)
4700 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004701 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01004702 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004703 if (p == NULL)
4704 return FAIL;
4705 }
4706 if (p != NULL)
4707 {
4708 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
4709 p, cmdlinep);
4710 if (n == 2) /* p came from ExpandOne() */
4711 vim_free(p);
4712 }
4713 }
4714 }
4715 }
4716 return OK;
4717}
4718
4719/*
4720 * Replace part of the command line, keeping eap->cmd, eap->arg and
4721 * eap->nextcmd correct.
4722 * "src" points to the part that is to be replaced, of length "srclen".
4723 * "repl" is the replacement string.
4724 * Returns a pointer to the character after the replaced string.
4725 * Returns NULL for failure.
4726 */
4727 static char_u *
4728repl_cmdline(eap, src, srclen, repl, cmdlinep)
4729 exarg_T *eap;
4730 char_u *src;
4731 int srclen;
4732 char_u *repl;
4733 char_u **cmdlinep;
4734{
4735 int len;
4736 int i;
4737 char_u *new_cmdline;
4738
4739 /*
4740 * The new command line is build in new_cmdline[].
4741 * First allocate it.
4742 * Careful: a "+cmd" argument may have been NUL terminated.
4743 */
4744 len = (int)STRLEN(repl);
4745 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004746 if (eap->nextcmd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004747 i += (int)STRLEN(eap->nextcmd);/* add space for next command */
4748 if ((new_cmdline = alloc((unsigned)i)) == NULL)
4749 return NULL; /* out of memory! */
4750
4751 /*
4752 * Copy the stuff before the expanded part.
4753 * Copy the expanded stuff.
4754 * Copy what came after the expanded part.
4755 * Copy the next commands, if there are any.
4756 */
4757 i = (int)(src - *cmdlinep); /* length of part before match */
4758 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00004759
Bram Moolenaar071d4272004-06-13 20:20:40 +00004760 mch_memmove(new_cmdline + i, repl, (size_t)len);
4761 i += len; /* remember the end of the string */
4762 STRCPY(new_cmdline + i, src + srclen);
4763 src = new_cmdline + i; /* remember where to continue */
4764
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004765 if (eap->nextcmd != NULL) /* append next command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004766 {
4767 i = (int)STRLEN(new_cmdline) + 1;
4768 STRCPY(new_cmdline + i, eap->nextcmd);
4769 eap->nextcmd = new_cmdline + i;
4770 }
4771 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
4772 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
4773 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
4774 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
4775 vim_free(*cmdlinep);
4776 *cmdlinep = new_cmdline;
4777
4778 return src;
4779}
4780
4781/*
4782 * Check for '|' to separate commands and '"' to start comments.
4783 */
4784 void
4785separate_nextcmd(eap)
4786 exarg_T *eap;
4787{
4788 char_u *p;
4789
Bram Moolenaar86b68352004-12-27 21:59:20 +00004790#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004791 p = skip_grep_pat(eap);
4792#else
4793 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004794#endif
4795
4796 for ( ; *p; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004797 {
4798 if (*p == Ctrl_V)
4799 {
4800 if (eap->argt & (USECTRLV | XFILE))
4801 ++p; /* skip CTRL-V and next char */
4802 else
Bram Moolenaarb0db5692007-08-14 20:54:49 +00004803 /* remove CTRL-V and skip next char */
Bram Moolenaara7241f52008-06-24 20:39:31 +00004804 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004805 if (*p == NUL) /* stop at NUL after CTRL-V */
4806 break;
4807 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004808
4809#ifdef FEAT_EVAL
4810 /* Skip over `=expr` when wildcards are expanded. */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004811 else if (p[0] == '`' && p[1] == '=' && (eap->argt & XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004812 {
4813 p += 2;
4814 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004815 }
4816#endif
4817
Bram Moolenaar071d4272004-06-13 20:20:40 +00004818 /* Check for '"': start of comment or '|': next command */
4819 /* :@" and :*" do not start a comment!
4820 * :redir @" doesn't either. */
4821 else if ((*p == '"' && !(eap->argt & NOTRLCOM)
4822 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
4823 || p != eap->arg)
4824 && (eap->cmdidx != CMD_redir
4825 || p != eap->arg + 1 || p[-1] != '@'))
4826 || *p == '|' || *p == '\n')
4827 {
4828 /*
4829 * We remove the '\' before the '|', unless USECTRLV is used
4830 * AND 'b' is present in 'cpoptions'.
4831 */
4832 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
4833 || !(eap->argt & USECTRLV)) && *(p - 1) == '\\')
4834 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00004835 STRMOVE(p - 1, p); /* remove the '\' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004836 --p;
4837 }
4838 else
4839 {
4840 eap->nextcmd = check_nextcmd(p);
4841 *p = NUL;
4842 break;
4843 }
4844 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004845 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004846
Bram Moolenaar071d4272004-06-13 20:20:40 +00004847 if (!(eap->argt & NOTRLCOM)) /* remove trailing spaces */
4848 del_trailing_spaces(eap->arg);
4849}
4850
4851/*
4852 * get + command from ex argument
4853 */
4854 static char_u *
4855getargcmd(argp)
4856 char_u **argp;
4857{
4858 char_u *arg = *argp;
4859 char_u *command = NULL;
4860
4861 if (*arg == '+') /* +[command] */
4862 {
4863 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02004864 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004865 command = dollar_command;
4866 else
4867 {
4868 command = arg;
4869 arg = skip_cmd_arg(command, TRUE);
4870 if (*arg != NUL)
4871 *arg++ = NUL; /* terminate command with NUL */
4872 }
4873
4874 arg = skipwhite(arg); /* skip over spaces */
4875 *argp = arg;
4876 }
4877 return command;
4878}
4879
4880/*
4881 * Find end of "+command" argument. Skip over "\ " and "\\".
4882 */
4883 static char_u *
4884skip_cmd_arg(p, rembs)
4885 char_u *p;
4886 int rembs; /* TRUE to halve the number of backslashes */
4887{
4888 while (*p && !vim_isspace(*p))
4889 {
4890 if (*p == '\\' && p[1] != NUL)
4891 {
4892 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00004893 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004894 else
4895 ++p;
4896 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004897 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004898 }
4899 return p;
4900}
4901
4902/*
4903 * Get "++opt=arg" argument.
4904 * Return FAIL or OK.
4905 */
4906 static int
4907getargopt(eap)
4908 exarg_T *eap;
4909{
4910 char_u *arg = eap->arg + 2;
4911 int *pp = NULL;
4912#ifdef FEAT_MBYTE
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004913 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004914 char_u *p;
4915#endif
4916
4917 /* ":edit ++[no]bin[ary] file" */
4918 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
4919 {
4920 if (*arg == 'n')
4921 {
4922 arg += 2;
4923 eap->force_bin = FORCE_NOBIN;
4924 }
4925 else
4926 eap->force_bin = FORCE_BIN;
4927 if (!checkforcmd(&arg, "binary", 3))
4928 return FAIL;
4929 eap->arg = skipwhite(arg);
4930 return OK;
4931 }
4932
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004933 /* ":read ++edit file" */
4934 if (STRNCMP(arg, "edit", 4) == 0)
4935 {
4936 eap->read_edit = TRUE;
4937 eap->arg = skipwhite(arg + 4);
4938 return OK;
4939 }
4940
Bram Moolenaar071d4272004-06-13 20:20:40 +00004941 if (STRNCMP(arg, "ff", 2) == 0)
4942 {
4943 arg += 2;
4944 pp = &eap->force_ff;
4945 }
4946 else if (STRNCMP(arg, "fileformat", 10) == 0)
4947 {
4948 arg += 10;
4949 pp = &eap->force_ff;
4950 }
4951#ifdef FEAT_MBYTE
4952 else if (STRNCMP(arg, "enc", 3) == 0)
4953 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01004954 if (STRNCMP(arg, "encoding", 8) == 0)
4955 arg += 8;
4956 else
4957 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004958 pp = &eap->force_enc;
4959 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004960 else if (STRNCMP(arg, "bad", 3) == 0)
4961 {
4962 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004963 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004964 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004965#endif
4966
4967 if (pp == NULL || *arg != '=')
4968 return FAIL;
4969
4970 ++arg;
4971 *pp = (int)(arg - eap->cmd);
4972 arg = skip_cmd_arg(arg, FALSE);
4973 eap->arg = skipwhite(arg);
4974 *arg = NUL;
4975
4976#ifdef FEAT_MBYTE
4977 if (pp == &eap->force_ff)
4978 {
4979#endif
4980 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
4981 return FAIL;
4982#ifdef FEAT_MBYTE
4983 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004984 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985 {
4986 /* Make 'fileencoding' lower case. */
4987 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
4988 *p = TOLOWER_ASC(*p);
4989 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004990 else
4991 {
4992 /* Check ++bad= argument. Must be a single-byte character, "keep" or
4993 * "drop". */
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004994 p = eap->cmd + bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004995 if (STRICMP(p, "keep") == 0)
4996 eap->bad_char = BAD_KEEP;
4997 else if (STRICMP(p, "drop") == 0)
4998 eap->bad_char = BAD_DROP;
4999 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
5000 eap->bad_char = *p;
5001 else
5002 return FAIL;
5003 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005004#endif
5005
5006 return OK;
5007}
5008
5009/*
5010 * ":abbreviate" and friends.
5011 */
5012 static void
5013ex_abbreviate(eap)
5014 exarg_T *eap;
5015{
5016 do_exmap(eap, TRUE); /* almost the same as mapping */
5017}
5018
5019/*
5020 * ":map" and friends.
5021 */
5022 static void
5023ex_map(eap)
5024 exarg_T *eap;
5025{
5026 /*
5027 * If we are sourcing .exrc or .vimrc in current directory we
5028 * print the mappings for security reasons.
5029 */
5030 if (secure)
5031 {
5032 secure = 2;
5033 msg_outtrans(eap->cmd);
5034 msg_putchar('\n');
5035 }
5036 do_exmap(eap, FALSE);
5037}
5038
5039/*
5040 * ":unmap" and friends.
5041 */
5042 static void
5043ex_unmap(eap)
5044 exarg_T *eap;
5045{
5046 do_exmap(eap, FALSE);
5047}
5048
5049/*
5050 * ":mapclear" and friends.
5051 */
5052 static void
5053ex_mapclear(eap)
5054 exarg_T *eap;
5055{
5056 map_clear(eap->cmd, eap->arg, eap->forceit, FALSE);
5057}
5058
5059/*
5060 * ":abclear" and friends.
5061 */
5062 static void
5063ex_abclear(eap)
5064 exarg_T *eap;
5065{
5066 map_clear(eap->cmd, eap->arg, TRUE, TRUE);
5067}
5068
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005069#if defined(FEAT_AUTOCMD) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070 static void
5071ex_autocmd(eap)
5072 exarg_T *eap;
5073{
5074 /*
5075 * Disallow auto commands from .exrc and .vimrc in current
5076 * directory for security reasons.
5077 */
5078 if (secure)
5079 {
5080 secure = 2;
5081 eap->errmsg = e_curdir;
5082 }
5083 else if (eap->cmdidx == CMD_autocmd)
5084 do_autocmd(eap->arg, eap->forceit);
5085 else
5086 do_augroup(eap->arg, eap->forceit);
5087}
5088
5089/*
5090 * ":doautocmd": Apply the automatic commands to the current buffer.
5091 */
5092 static void
5093ex_doautocmd(eap)
5094 exarg_T *eap;
5095{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005096 char_u *arg = eap->arg;
5097 int call_do_modelines = check_nomodeline(&arg);
5098
5099 (void)do_doautocmd(arg, TRUE);
5100 if (call_do_modelines) /* Only when there is no <nomodeline>. */
5101 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005102}
5103#endif
5104
5105#ifdef FEAT_LISTCMDS
5106/*
5107 * :[N]bunload[!] [N] [bufname] unload buffer
5108 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
5109 * :[N]bwipeout[!] [N] [bufname] delete buffer really
5110 */
5111 static void
5112ex_bunload(eap)
5113 exarg_T *eap;
5114{
5115 eap->errmsg = do_bufdel(
5116 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
5117 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
5118 : DOBUF_UNLOAD, eap->arg,
5119 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
5120}
5121
5122/*
5123 * :[N]buffer [N] to buffer N
5124 * :[N]sbuffer [N] to buffer N
5125 */
5126 static void
5127ex_buffer(eap)
5128 exarg_T *eap;
5129{
5130 if (*eap->arg)
5131 eap->errmsg = e_trailing;
5132 else
5133 {
5134 if (eap->addr_count == 0) /* default is current buffer */
5135 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
5136 else
5137 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
5138 }
5139}
5140
5141/*
5142 * :[N]bmodified [N] to next mod. buffer
5143 * :[N]sbmodified [N] to next mod. buffer
5144 */
5145 static void
5146ex_bmodified(eap)
5147 exarg_T *eap;
5148{
5149 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
5150}
5151
5152/*
5153 * :[N]bnext [N] to next buffer
5154 * :[N]sbnext [N] split and to next buffer
5155 */
5156 static void
5157ex_bnext(eap)
5158 exarg_T *eap;
5159{
5160 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
5161}
5162
5163/*
5164 * :[N]bNext [N] to previous buffer
5165 * :[N]bprevious [N] to previous buffer
5166 * :[N]sbNext [N] split and to previous buffer
5167 * :[N]sbprevious [N] split and to previous buffer
5168 */
5169 static void
5170ex_bprevious(eap)
5171 exarg_T *eap;
5172{
5173 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
5174}
5175
5176/*
5177 * :brewind to first buffer
5178 * :bfirst to first buffer
5179 * :sbrewind split and to first buffer
5180 * :sbfirst split and to first buffer
5181 */
5182 static void
5183ex_brewind(eap)
5184 exarg_T *eap;
5185{
5186 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
5187}
5188
5189/*
5190 * :blast to last buffer
5191 * :sblast split and to last buffer
5192 */
5193 static void
5194ex_blast(eap)
5195 exarg_T *eap;
5196{
5197 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
5198}
5199#endif
5200
5201 int
5202ends_excmd(c)
5203 int c;
5204{
5205 return (c == NUL || c == '|' || c == '"' || c == '\n');
5206}
5207
5208#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5209 || defined(PROTO)
5210/*
5211 * Return the next command, after the first '|' or '\n'.
5212 * Return NULL if not found.
5213 */
5214 char_u *
5215find_nextcmd(p)
5216 char_u *p;
5217{
5218 while (*p != '|' && *p != '\n')
5219 {
5220 if (*p == NUL)
5221 return NULL;
5222 ++p;
5223 }
5224 return (p + 1);
5225}
5226#endif
5227
5228/*
5229 * Check if *p is a separator between Ex commands.
5230 * Return NULL if it isn't, (p + 1) if it is.
5231 */
5232 char_u *
5233check_nextcmd(p)
5234 char_u *p;
5235{
5236 p = skipwhite(p);
5237 if (*p == '|' || *p == '\n')
5238 return (p + 1);
5239 else
5240 return NULL;
5241}
5242
5243/*
5244 * - if there are more files to edit
5245 * - and this is the last window
5246 * - and forceit not used
5247 * - and not repeated twice on a row
5248 * return FAIL and give error message if 'message' TRUE
5249 * return OK otherwise
5250 */
5251 static int
5252check_more(message, forceit)
5253 int message; /* when FALSE check only, no messages */
5254 int forceit;
5255{
5256 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5257
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005258 if (!forceit && only_one_window()
5259 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005260 {
5261 if (message)
5262 {
5263#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
5264 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
5265 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005266 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005267
5268 if (n == 1)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02005269 vim_strncpy(buff,
5270 (char_u *)_("1 more file to edit. Quit anyway?"),
Bram Moolenaard9462e32011-04-11 21:35:11 +02005271 DIALOG_MSG_SIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005272 else
Bram Moolenaard9462e32011-04-11 21:35:11 +02005273 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005274 _("%d more files to edit. Quit anyway?"), n);
5275 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5276 return OK;
5277 return FAIL;
5278 }
5279#endif
5280 if (n == 1)
5281 EMSG(_("E173: 1 more file to edit"));
5282 else
5283 EMSGN(_("E173: %ld more files to edit"), n);
5284 quitmore = 2; /* next try to quit is allowed */
5285 }
5286 return FAIL;
5287 }
5288 return OK;
5289}
5290
5291#ifdef FEAT_CMDL_COMPL
5292/*
5293 * Function given to ExpandGeneric() to obtain the list of command names.
5294 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005295 char_u *
5296get_command_name(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00005297 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005298 int idx;
5299{
5300 if (idx >= (int)CMD_SIZE)
5301# ifdef FEAT_USR_CMDS
5302 return get_user_command_name(idx);
5303# else
5304 return NULL;
5305# endif
5306 return cmdnames[idx].cmd_name;
5307}
5308#endif
5309
5310#if defined(FEAT_USR_CMDS) || defined(PROTO)
5311static 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));
5312static void uc_list __ARGS((char_u *name, size_t name_len));
5313static int uc_scan_attr __ARGS((char_u *attr, size_t len, long *argt, long *def, int *flags, int *compl, char_u **compl_arg));
5314static char_u *uc_split_args __ARGS((char_u *arg, size_t *lenp));
5315static 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));
5316
5317 static int
5318uc_add_command(name, name_len, rep, argt, def, flags, compl, compl_arg, force)
5319 char_u *name;
5320 size_t name_len;
5321 char_u *rep;
5322 long argt;
5323 long def;
5324 int flags;
5325 int compl;
5326 char_u *compl_arg;
5327 int force;
5328{
5329 ucmd_T *cmd = NULL;
5330 char_u *p;
5331 int i;
5332 int cmp = 1;
5333 char_u *rep_buf = NULL;
5334 garray_T *gap;
5335
Bram Moolenaar9c102382006-05-03 21:26:49 +00005336 replace_termcodes(rep, &rep_buf, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005337 if (rep_buf == NULL)
5338 {
5339 /* Can't replace termcodes - try using the string as is */
5340 rep_buf = vim_strsave(rep);
5341
5342 /* Give up if out of memory */
5343 if (rep_buf == NULL)
5344 return FAIL;
5345 }
5346
5347 /* get address of growarray: global or in curbuf */
5348 if (flags & UC_BUFFER)
5349 {
5350 gap = &curbuf->b_ucmds;
5351 if (gap->ga_itemsize == 0)
5352 ga_init2(gap, (int)sizeof(ucmd_T), 4);
5353 }
5354 else
5355 gap = &ucmds;
5356
5357 /* Search for the command in the already defined commands. */
5358 for (i = 0; i < gap->ga_len; ++i)
5359 {
5360 size_t len;
5361
5362 cmd = USER_CMD_GA(gap, i);
5363 len = STRLEN(cmd->uc_name);
5364 cmp = STRNCMP(name, cmd->uc_name, name_len);
5365 if (cmp == 0)
5366 {
5367 if (name_len < len)
5368 cmp = -1;
5369 else if (name_len > len)
5370 cmp = 1;
5371 }
5372
5373 if (cmp == 0)
5374 {
5375 if (!force)
5376 {
5377 EMSG(_("E174: Command already exists: add ! to replace it"));
5378 goto fail;
5379 }
5380
5381 vim_free(cmd->uc_rep);
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00005382 cmd->uc_rep = NULL;
5383#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5384 vim_free(cmd->uc_compl_arg);
5385 cmd->uc_compl_arg = NULL;
5386#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005387 break;
5388 }
5389
5390 /* Stop as soon as we pass the name to add */
5391 if (cmp < 0)
5392 break;
5393 }
5394
5395 /* Extend the array unless we're replacing an existing command */
5396 if (cmp != 0)
5397 {
5398 if (ga_grow(gap, 1) != OK)
5399 goto fail;
5400 if ((p = vim_strnsave(name, (int)name_len)) == NULL)
5401 goto fail;
5402
5403 cmd = USER_CMD_GA(gap, i);
5404 mch_memmove(cmd + 1, cmd, (gap->ga_len - i) * sizeof(ucmd_T));
5405
5406 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005407
5408 cmd->uc_name = p;
5409 }
5410
5411 cmd->uc_rep = rep_buf;
5412 cmd->uc_argt = argt;
5413 cmd->uc_def = def;
5414 cmd->uc_compl = compl;
5415#ifdef FEAT_EVAL
5416 cmd->uc_scriptID = current_SID;
5417# ifdef FEAT_CMDL_COMPL
5418 cmd->uc_compl_arg = compl_arg;
5419# endif
5420#endif
5421
5422 return OK;
5423
5424fail:
5425 vim_free(rep_buf);
5426#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5427 vim_free(compl_arg);
5428#endif
5429 return FAIL;
5430}
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005431#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005432
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005433#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005434/*
5435 * List of names for completion for ":command" with the EXPAND_ flag.
5436 * Must be alphabetical for completion.
5437 */
5438static struct
5439{
5440 int expand;
5441 char *name;
5442} command_complete[] =
5443{
5444 {EXPAND_AUGROUP, "augroup"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005445 {EXPAND_BEHAVE, "behave"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005446 {EXPAND_BUFFERS, "buffer"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005447 {EXPAND_COLORS, "color"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005448 {EXPAND_COMMANDS, "command"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005449 {EXPAND_COMPILER, "compiler"},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005450#if defined(FEAT_CSCOPE)
5451 {EXPAND_CSCOPE, "cscope"},
5452#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005453#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5454 {EXPAND_USER_DEFINED, "custom"},
Bram Moolenaara466c992005-07-09 21:03:22 +00005455 {EXPAND_USER_LIST, "customlist"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005456#endif
5457 {EXPAND_DIRECTORIES, "dir"},
5458 {EXPAND_ENV_VARS, "environment"},
5459 {EXPAND_EVENTS, "event"},
5460 {EXPAND_EXPRESSION, "expression"},
5461 {EXPAND_FILES, "file"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005462 {EXPAND_FILES_IN_PATH, "file_in_path"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005463 {EXPAND_FILETYPE, "filetype"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005464 {EXPAND_FUNCTIONS, "function"},
5465 {EXPAND_HELP, "help"},
5466 {EXPAND_HIGHLIGHT, "highlight"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005467#if defined(FEAT_CMDHIST)
5468 {EXPAND_HISTORY, "history"},
5469#endif
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005470#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005471 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005472 {EXPAND_LOCALES, "locale"},
5473#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005474 {EXPAND_MAPPINGS, "mapping"},
5475 {EXPAND_MENUS, "menu"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005476 {EXPAND_OWNSYNTAX, "syntax"},
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02005477#if defined(FEAT_PROFILE)
5478 {EXPAND_SYNTIME, "syntime"},
5479#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005480 {EXPAND_SETTINGS, "option"},
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005481 {EXPAND_SHELLCMD, "shellcmd"},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005482#if defined(FEAT_SIGNS)
5483 {EXPAND_SIGN, "sign"},
5484#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005485 {EXPAND_TAGS, "tag"},
5486 {EXPAND_TAGS_LISTFILES, "tag_listfiles"},
Bram Moolenaar24305862012-08-15 14:05:05 +02005487 {EXPAND_USER, "user"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005488 {EXPAND_USER_VARS, "var"},
5489 {0, NULL}
5490};
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005491#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005492
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005493#if defined(FEAT_USR_CMDS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005494 static void
5495uc_list(name, name_len)
5496 char_u *name;
5497 size_t name_len;
5498{
5499 int i, j;
5500 int found = FALSE;
5501 ucmd_T *cmd;
5502 int len;
5503 long a;
5504 garray_T *gap;
5505
5506 gap = &curbuf->b_ucmds;
5507 for (;;)
5508 {
5509 for (i = 0; i < gap->ga_len; ++i)
5510 {
5511 cmd = USER_CMD_GA(gap, i);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005512 a = (long)cmd->uc_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005513
5514 /* Skip commands which don't match the requested prefix */
5515 if (STRNCMP(name, cmd->uc_name, name_len) != 0)
5516 continue;
5517
5518 /* Put out the title first time */
5519 if (!found)
5520 MSG_PUTS_TITLE(_("\n Name Args Range Complete Definition"));
5521 found = TRUE;
5522 msg_putchar('\n');
5523 if (got_int)
5524 break;
5525
5526 /* Special cases */
5527 msg_putchar(a & BANG ? '!' : ' ');
5528 msg_putchar(a & REGSTR ? '"' : ' ');
5529 msg_putchar(gap != &ucmds ? 'b' : ' ');
5530 msg_putchar(' ');
5531
5532 msg_outtrans_attr(cmd->uc_name, hl_attr(HLF_D));
5533 len = (int)STRLEN(cmd->uc_name) + 4;
5534
5535 do {
5536 msg_putchar(' ');
5537 ++len;
5538 } while (len < 16);
5539
5540 len = 0;
5541
5542 /* Arguments */
5543 switch ((int)(a & (EXTRA|NOSPC|NEEDARG)))
5544 {
5545 case 0: IObuff[len++] = '0'; break;
5546 case (EXTRA): IObuff[len++] = '*'; break;
5547 case (EXTRA|NOSPC): IObuff[len++] = '?'; break;
5548 case (EXTRA|NEEDARG): IObuff[len++] = '+'; break;
5549 case (EXTRA|NOSPC|NEEDARG): IObuff[len++] = '1'; break;
5550 }
5551
5552 do {
5553 IObuff[len++] = ' ';
5554 } while (len < 5);
5555
5556 /* Range */
5557 if (a & (RANGE|COUNT))
5558 {
5559 if (a & COUNT)
5560 {
5561 /* -count=N */
5562 sprintf((char *)IObuff + len, "%ldc", cmd->uc_def);
5563 len += (int)STRLEN(IObuff + len);
5564 }
5565 else if (a & DFLALL)
5566 IObuff[len++] = '%';
5567 else if (cmd->uc_def >= 0)
5568 {
5569 /* -range=N */
5570 sprintf((char *)IObuff + len, "%ld", cmd->uc_def);
5571 len += (int)STRLEN(IObuff + len);
5572 }
5573 else
5574 IObuff[len++] = '.';
5575 }
5576
5577 do {
5578 IObuff[len++] = ' ';
5579 } while (len < 11);
5580
5581 /* Completion */
5582 for (j = 0; command_complete[j].expand != 0; ++j)
5583 if (command_complete[j].expand == cmd->uc_compl)
5584 {
5585 STRCPY(IObuff + len, command_complete[j].name);
5586 len += (int)STRLEN(IObuff + len);
5587 break;
5588 }
5589
5590 do {
5591 IObuff[len++] = ' ';
5592 } while (len < 21);
5593
5594 IObuff[len] = '\0';
5595 msg_outtrans(IObuff);
5596
5597 msg_outtrans_special(cmd->uc_rep, FALSE);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005598#ifdef FEAT_EVAL
5599 if (p_verbose > 0)
5600 last_set_msg(cmd->uc_scriptID);
5601#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005602 out_flush();
5603 ui_breakcheck();
5604 if (got_int)
5605 break;
5606 }
5607 if (gap == &ucmds || i < gap->ga_len)
5608 break;
5609 gap = &ucmds;
5610 }
5611
5612 if (!found)
5613 MSG(_("No user-defined commands found"));
5614}
5615
5616 static char_u *
5617uc_fun_cmd()
5618{
5619 static char_u fcmd[] = {0x84, 0xaf, 0x60, 0xb9, 0xaf, 0xb5, 0x60, 0xa4,
5620 0xa5, 0xad, 0xa1, 0xae, 0xa4, 0x60, 0xa1, 0x60,
5621 0xb3, 0xa8, 0xb2, 0xb5, 0xa2, 0xa2, 0xa5, 0xb2,
5622 0xb9, 0x7f, 0};
5623 int i;
5624
5625 for (i = 0; fcmd[i]; ++i)
5626 IObuff[i] = fcmd[i] - 0x40;
5627 IObuff[i] = 0;
5628 return IObuff;
5629}
5630
5631 static int
5632uc_scan_attr(attr, len, argt, def, flags, compl, compl_arg)
5633 char_u *attr;
5634 size_t len;
5635 long *argt;
5636 long *def;
5637 int *flags;
5638 int *compl;
5639 char_u **compl_arg;
5640{
5641 char_u *p;
5642
5643 if (len == 0)
5644 {
5645 EMSG(_("E175: No attribute specified"));
5646 return FAIL;
5647 }
5648
5649 /* First, try the simple attributes (no arguments) */
5650 if (STRNICMP(attr, "bang", len) == 0)
5651 *argt |= BANG;
5652 else if (STRNICMP(attr, "buffer", len) == 0)
5653 *flags |= UC_BUFFER;
5654 else if (STRNICMP(attr, "register", len) == 0)
5655 *argt |= REGSTR;
5656 else if (STRNICMP(attr, "bar", len) == 0)
5657 *argt |= TRLBAR;
5658 else
5659 {
5660 int i;
5661 char_u *val = NULL;
5662 size_t vallen = 0;
5663 size_t attrlen = len;
5664
5665 /* Look for the attribute name - which is the part before any '=' */
5666 for (i = 0; i < (int)len; ++i)
5667 {
5668 if (attr[i] == '=')
5669 {
5670 val = &attr[i + 1];
5671 vallen = len - i - 1;
5672 attrlen = i;
5673 break;
5674 }
5675 }
5676
5677 if (STRNICMP(attr, "nargs", attrlen) == 0)
5678 {
5679 if (vallen == 1)
5680 {
5681 if (*val == '0')
5682 /* Do nothing - this is the default */;
5683 else if (*val == '1')
5684 *argt |= (EXTRA | NOSPC | NEEDARG);
5685 else if (*val == '*')
5686 *argt |= EXTRA;
5687 else if (*val == '?')
5688 *argt |= (EXTRA | NOSPC);
5689 else if (*val == '+')
5690 *argt |= (EXTRA | NEEDARG);
5691 else
5692 goto wrong_nargs;
5693 }
5694 else
5695 {
5696wrong_nargs:
5697 EMSG(_("E176: Invalid number of arguments"));
5698 return FAIL;
5699 }
5700 }
5701 else if (STRNICMP(attr, "range", attrlen) == 0)
5702 {
5703 *argt |= RANGE;
5704 if (vallen == 1 && *val == '%')
5705 *argt |= DFLALL;
5706 else if (val != NULL)
5707 {
5708 p = val;
5709 if (*def >= 0)
5710 {
5711two_count:
5712 EMSG(_("E177: Count cannot be specified twice"));
5713 return FAIL;
5714 }
5715
5716 *def = getdigits(&p);
5717 *argt |= (ZEROR | NOTADR);
5718
5719 if (p != val + vallen || vallen == 0)
5720 {
5721invalid_count:
5722 EMSG(_("E178: Invalid default value for count"));
5723 return FAIL;
5724 }
5725 }
5726 }
5727 else if (STRNICMP(attr, "count", attrlen) == 0)
5728 {
Bram Moolenaar32e7b2d2005-02-27 22:36:47 +00005729 *argt |= (COUNT | ZEROR | RANGE | NOTADR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005730
5731 if (val != NULL)
5732 {
5733 p = val;
5734 if (*def >= 0)
5735 goto two_count;
5736
5737 *def = getdigits(&p);
5738
5739 if (p != val + vallen)
5740 goto invalid_count;
5741 }
5742
5743 if (*def < 0)
5744 *def = 0;
5745 }
5746 else if (STRNICMP(attr, "complete", attrlen) == 0)
5747 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005748 if (val == NULL)
5749 {
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005750 EMSG(_("E179: argument required for -complete"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005751 return FAIL;
5752 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005753
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005754 if (parse_compl_arg(val, (int)vallen, compl, argt, compl_arg)
5755 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005756 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005757 }
5758 else
5759 {
5760 char_u ch = attr[len];
5761 attr[len] = '\0';
5762 EMSG2(_("E181: Invalid attribute: %s"), attr);
5763 attr[len] = ch;
5764 return FAIL;
5765 }
5766 }
5767
5768 return OK;
5769}
5770
Bram Moolenaara850a712009-01-28 14:42:59 +00005771/*
5772 * ":command ..."
5773 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005774 static void
5775ex_command(eap)
5776 exarg_T *eap;
5777{
5778 char_u *name;
5779 char_u *end;
5780 char_u *p;
5781 long argt = 0;
5782 long def = -1;
5783 int flags = 0;
5784 int compl = EXPAND_NOTHING;
5785 char_u *compl_arg = NULL;
5786 int has_attr = (eap->arg[0] == '-');
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01005787 int name_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005788
5789 p = eap->arg;
5790
5791 /* Check for attributes */
5792 while (*p == '-')
5793 {
5794 ++p;
5795 end = skiptowhite(p);
5796 if (uc_scan_attr(p, end - p, &argt, &def, &flags, &compl, &compl_arg)
5797 == FAIL)
5798 return;
5799 p = skipwhite(end);
5800 }
5801
5802 /* Get the name (if any) and skip to the following argument */
5803 name = p;
5804 if (ASCII_ISALPHA(*p))
5805 while (ASCII_ISALNUM(*p))
5806 ++p;
5807 if (!ends_excmd(*p) && !vim_iswhite(*p))
5808 {
5809 EMSG(_("E182: Invalid command name"));
5810 return;
5811 }
5812 end = p;
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01005813 name_len = (int)(end - name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005814
5815 /* If there is nothing after the name, and no attributes were specified,
5816 * we are listing commands
5817 */
5818 p = skipwhite(end);
5819 if (!has_attr && ends_excmd(*p))
5820 {
5821 uc_list(name, end - name);
5822 }
5823 else if (!ASCII_ISUPPER(*name))
5824 {
5825 EMSG(_("E183: User defined commands must start with an uppercase letter"));
5826 return;
5827 }
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01005828 else if ((name_len == 1 && *name == 'X')
5829 || (name_len <= 4
5830 && STRNCMP(name, "Next", name_len > 4 ? 4 : name_len) == 0))
5831 {
5832 EMSG(_("E841: Reserved name, cannot be used for user defined command"));
5833 return;
5834 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005835 else
5836 uc_add_command(name, end - name, p, argt, def, flags, compl, compl_arg,
5837 eap->forceit);
5838}
5839
5840/*
5841 * ":comclear"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005842 * Clear all user commands, global and for current buffer.
5843 */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00005844 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005845ex_comclear(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00005846 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005847{
5848 uc_clear(&ucmds);
5849 uc_clear(&curbuf->b_ucmds);
5850}
5851
5852/*
5853 * Clear all user commands for "gap".
5854 */
5855 void
5856uc_clear(gap)
5857 garray_T *gap;
5858{
5859 int i;
5860 ucmd_T *cmd;
5861
5862 for (i = 0; i < gap->ga_len; ++i)
5863 {
5864 cmd = USER_CMD_GA(gap, i);
5865 vim_free(cmd->uc_name);
5866 vim_free(cmd->uc_rep);
5867# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5868 vim_free(cmd->uc_compl_arg);
5869# endif
5870 }
5871 ga_clear(gap);
5872}
5873
5874 static void
5875ex_delcommand(eap)
5876 exarg_T *eap;
5877{
5878 int i = 0;
5879 ucmd_T *cmd = NULL;
5880 int cmp = -1;
5881 garray_T *gap;
5882
5883 gap = &curbuf->b_ucmds;
5884 for (;;)
5885 {
5886 for (i = 0; i < gap->ga_len; ++i)
5887 {
5888 cmd = USER_CMD_GA(gap, i);
5889 cmp = STRCMP(eap->arg, cmd->uc_name);
5890 if (cmp <= 0)
5891 break;
5892 }
5893 if (gap == &ucmds || cmp == 0)
5894 break;
5895 gap = &ucmds;
5896 }
5897
5898 if (cmp != 0)
5899 {
5900 EMSG2(_("E184: No such user-defined command: %s"), eap->arg);
5901 return;
5902 }
5903
5904 vim_free(cmd->uc_name);
5905 vim_free(cmd->uc_rep);
5906# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5907 vim_free(cmd->uc_compl_arg);
5908# endif
5909
5910 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005911
5912 if (i < gap->ga_len)
5913 mch_memmove(cmd, cmd + 1, (gap->ga_len - i) * sizeof(ucmd_T));
5914}
5915
Bram Moolenaar552f8a12007-03-08 17:12:08 +00005916/*
5917 * split and quote args for <f-args>
5918 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005919 static char_u *
5920uc_split_args(arg, lenp)
5921 char_u *arg;
5922 size_t *lenp;
5923{
5924 char_u *buf;
5925 char_u *p;
5926 char_u *q;
5927 int len;
5928
5929 /* Precalculate length */
5930 p = arg;
5931 len = 2; /* Initial and final quotes */
5932
5933 while (*p)
5934 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00005935 if (p[0] == '\\' && p[1] == '\\')
5936 {
5937 len += 2;
5938 p += 2;
5939 }
5940 else if (p[0] == '\\' && vim_iswhite(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005941 {
5942 len += 1;
5943 p += 2;
5944 }
5945 else if (*p == '\\' || *p == '"')
5946 {
5947 len += 2;
5948 p += 1;
5949 }
5950 else if (vim_iswhite(*p))
5951 {
5952 p = skipwhite(p);
5953 if (*p == NUL)
5954 break;
5955 len += 3; /* "," */
5956 }
5957 else
5958 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02005959#ifdef FEAT_MBYTE
5960 int charlen = (*mb_ptr2len)(p);
5961 len += charlen;
5962 p += charlen;
5963#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005964 ++len;
5965 ++p;
Bram Moolenaardfef1542012-07-10 19:25:10 +02005966#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005967 }
5968 }
5969
5970 buf = alloc(len + 1);
5971 if (buf == NULL)
5972 {
5973 *lenp = 0;
5974 return buf;
5975 }
5976
5977 p = arg;
5978 q = buf;
5979 *q++ = '"';
5980 while (*p)
5981 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00005982 if (p[0] == '\\' && p[1] == '\\')
5983 {
5984 *q++ = '\\';
5985 *q++ = '\\';
5986 p += 2;
5987 }
5988 else if (p[0] == '\\' && vim_iswhite(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005989 {
5990 *q++ = p[1];
5991 p += 2;
5992 }
5993 else if (*p == '\\' || *p == '"')
5994 {
5995 *q++ = '\\';
5996 *q++ = *p++;
5997 }
5998 else if (vim_iswhite(*p))
5999 {
6000 p = skipwhite(p);
6001 if (*p == NUL)
6002 break;
6003 *q++ = '"';
6004 *q++ = ',';
6005 *q++ = '"';
6006 }
6007 else
6008 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02006009 MB_COPY_CHAR(p, q);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006010 }
6011 }
6012 *q++ = '"';
6013 *q = 0;
6014
6015 *lenp = len;
6016 return buf;
6017}
6018
6019/*
6020 * Check for a <> code in a user command.
6021 * "code" points to the '<'. "len" the length of the <> (inclusive).
6022 * "buf" is where the result is to be added.
6023 * "split_buf" points to a buffer used for splitting, caller should free it.
6024 * "split_len" is the length of what "split_buf" contains.
6025 * Returns the length of the replacement, which has been added to "buf".
6026 * Returns -1 if there was no match, and only the "<" has been copied.
6027 */
6028 static size_t
6029uc_check_code(code, len, buf, cmd, eap, split_buf, split_len)
6030 char_u *code;
6031 size_t len;
6032 char_u *buf;
6033 ucmd_T *cmd; /* the user command we're expanding */
6034 exarg_T *eap; /* ex arguments */
6035 char_u **split_buf;
6036 size_t *split_len;
6037{
6038 size_t result = 0;
6039 char_u *p = code + 1;
6040 size_t l = len - 2;
6041 int quote = 0;
6042 enum { ct_ARGS, ct_BANG, ct_COUNT, ct_LINE1, ct_LINE2, ct_REGISTER,
6043 ct_LT, ct_NONE } type = ct_NONE;
6044
6045 if ((vim_strchr((char_u *)"qQfF", *p) != NULL) && p[1] == '-')
6046 {
6047 quote = (*p == 'q' || *p == 'Q') ? 1 : 2;
6048 p += 2;
6049 l -= 2;
6050 }
6051
Bram Moolenaar371d5402006-03-20 21:47:49 +00006052 ++l;
6053 if (l <= 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006054 type = ct_NONE;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006055 else if (STRNICMP(p, "args>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006056 type = ct_ARGS;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006057 else if (STRNICMP(p, "bang>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006058 type = ct_BANG;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006059 else if (STRNICMP(p, "count>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006060 type = ct_COUNT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006061 else if (STRNICMP(p, "line1>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006062 type = ct_LINE1;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006063 else if (STRNICMP(p, "line2>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006064 type = ct_LINE2;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006065 else if (STRNICMP(p, "lt>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006066 type = ct_LT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006067 else if (STRNICMP(p, "reg>", l) == 0 || STRNICMP(p, "register>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006068 type = ct_REGISTER;
6069
6070 switch (type)
6071 {
6072 case ct_ARGS:
6073 /* Simple case first */
6074 if (*eap->arg == NUL)
6075 {
6076 if (quote == 1)
6077 {
6078 result = 2;
6079 if (buf != NULL)
6080 STRCPY(buf, "''");
6081 }
6082 else
6083 result = 0;
6084 break;
6085 }
6086
6087 /* When specified there is a single argument don't split it.
6088 * Works for ":Cmd %" when % is "a b c". */
6089 if ((eap->argt & NOSPC) && quote == 2)
6090 quote = 1;
6091
6092 switch (quote)
6093 {
6094 case 0: /* No quoting, no splitting */
6095 result = STRLEN(eap->arg);
6096 if (buf != NULL)
6097 STRCPY(buf, eap->arg);
6098 break;
6099 case 1: /* Quote, but don't split */
6100 result = STRLEN(eap->arg) + 2;
6101 for (p = eap->arg; *p; ++p)
6102 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006103#ifdef FEAT_MBYTE
6104 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6105 /* DBCS can contain \ in a trail byte, skip the
6106 * double-byte character. */
6107 ++p;
6108 else
6109#endif
6110 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006111 ++result;
6112 }
6113
6114 if (buf != NULL)
6115 {
6116 *buf++ = '"';
6117 for (p = eap->arg; *p; ++p)
6118 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006119#ifdef FEAT_MBYTE
6120 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6121 /* DBCS can contain \ in a trail byte, copy the
6122 * double-byte character to avoid escaping. */
6123 *buf++ = *p++;
6124 else
6125#endif
6126 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006127 *buf++ = '\\';
6128 *buf++ = *p;
6129 }
6130 *buf = '"';
6131 }
6132
6133 break;
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006134 case 2: /* Quote and split (<f-args>) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006135 /* This is hard, so only do it once, and cache the result */
6136 if (*split_buf == NULL)
6137 *split_buf = uc_split_args(eap->arg, split_len);
6138
6139 result = *split_len;
6140 if (buf != NULL && result != 0)
6141 STRCPY(buf, *split_buf);
6142
6143 break;
6144 }
6145 break;
6146
6147 case ct_BANG:
6148 result = eap->forceit ? 1 : 0;
6149 if (quote)
6150 result += 2;
6151 if (buf != NULL)
6152 {
6153 if (quote)
6154 *buf++ = '"';
6155 if (eap->forceit)
6156 *buf++ = '!';
6157 if (quote)
6158 *buf = '"';
6159 }
6160 break;
6161
6162 case ct_LINE1:
6163 case ct_LINE2:
6164 case ct_COUNT:
6165 {
6166 char num_buf[20];
6167 long num = (type == ct_LINE1) ? eap->line1 :
6168 (type == ct_LINE2) ? eap->line2 :
6169 (eap->addr_count > 0) ? eap->line2 : cmd->uc_def;
6170 size_t num_len;
6171
6172 sprintf(num_buf, "%ld", num);
6173 num_len = STRLEN(num_buf);
6174 result = num_len;
6175
6176 if (quote)
6177 result += 2;
6178
6179 if (buf != NULL)
6180 {
6181 if (quote)
6182 *buf++ = '"';
6183 STRCPY(buf, num_buf);
6184 buf += num_len;
6185 if (quote)
6186 *buf = '"';
6187 }
6188
6189 break;
6190 }
6191
6192 case ct_REGISTER:
6193 result = eap->regname ? 1 : 0;
6194 if (quote)
6195 result += 2;
6196 if (buf != NULL)
6197 {
6198 if (quote)
6199 *buf++ = '\'';
6200 if (eap->regname)
6201 *buf++ = eap->regname;
6202 if (quote)
6203 *buf = '\'';
6204 }
6205 break;
6206
6207 case ct_LT:
6208 result = 1;
6209 if (buf != NULL)
6210 *buf = '<';
6211 break;
6212
6213 default:
6214 /* Not recognized: just copy the '<' and return -1. */
6215 result = (size_t)-1;
6216 if (buf != NULL)
6217 *buf = '<';
6218 break;
6219 }
6220
6221 return result;
6222}
6223
6224 static void
6225do_ucmd(eap)
6226 exarg_T *eap;
6227{
6228 char_u *buf;
6229 char_u *p;
6230 char_u *q;
6231
6232 char_u *start;
Bram Moolenaar25648a52009-02-21 19:37:46 +00006233 char_u *end = NULL;
Bram Moolenaara850a712009-01-28 14:42:59 +00006234 char_u *ksp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006235 size_t len, totlen;
6236
6237 size_t split_len = 0;
6238 char_u *split_buf = NULL;
6239 ucmd_T *cmd;
6240#ifdef FEAT_EVAL
6241 scid_T save_current_SID = current_SID;
6242#endif
6243
6244 if (eap->cmdidx == CMD_USER)
6245 cmd = USER_CMD(eap->useridx);
6246 else
6247 cmd = USER_CMD_GA(&curbuf->b_ucmds, eap->useridx);
6248
6249 /*
6250 * Replace <> in the command by the arguments.
Bram Moolenaara850a712009-01-28 14:42:59 +00006251 * First round: "buf" is NULL, compute length, allocate "buf".
6252 * Second round: copy result into "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006253 */
6254 buf = NULL;
6255 for (;;)
6256 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006257 p = cmd->uc_rep; /* source */
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00006258 q = buf; /* destination */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006259 totlen = 0;
Bram Moolenaara850a712009-01-28 14:42:59 +00006260
6261 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006262 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006263 start = vim_strchr(p, '<');
6264 if (start != NULL)
6265 end = vim_strchr(start + 1, '>');
6266 if (buf != NULL)
6267 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006268 for (ksp = p; *ksp != NUL && *ksp != K_SPECIAL; ++ksp)
6269 ;
6270 if (*ksp == K_SPECIAL
6271 && (start == NULL || ksp < start || end == NULL)
Bram Moolenaara850a712009-01-28 14:42:59 +00006272 && ((ksp[1] == KS_SPECIAL && ksp[2] == KE_FILLER)
6273# ifdef FEAT_GUI
6274 || (ksp[1] == KS_EXTRA && ksp[2] == (int)KE_CSI)
6275# endif
6276 ))
6277 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006278 /* K_SPECIAL has been put in the buffer as K_SPECIAL
Bram Moolenaara850a712009-01-28 14:42:59 +00006279 * KS_SPECIAL KE_FILLER, like for mappings, but
6280 * do_cmdline() doesn't handle that, so convert it back.
6281 * Also change K_SPECIAL KS_EXTRA KE_CSI into CSI. */
6282 len = ksp - p;
6283 if (len > 0)
6284 {
6285 mch_memmove(q, p, len);
6286 q += len;
6287 }
6288 *q++ = ksp[1] == KS_SPECIAL ? K_SPECIAL : CSI;
6289 p = ksp + 3;
6290 continue;
6291 }
6292 }
6293
6294 /* break if there no <item> is found */
6295 if (start == NULL || end == NULL)
6296 break;
6297
Bram Moolenaar071d4272004-06-13 20:20:40 +00006298 /* Include the '>' */
6299 ++end;
6300
6301 /* Take everything up to the '<' */
6302 len = start - p;
6303 if (buf == NULL)
6304 totlen += len;
6305 else
6306 {
6307 mch_memmove(q, p, len);
6308 q += len;
6309 }
6310
6311 len = uc_check_code(start, end - start, q, cmd, eap,
6312 &split_buf, &split_len);
6313 if (len == (size_t)-1)
6314 {
6315 /* no match, continue after '<' */
6316 p = start + 1;
6317 len = 1;
6318 }
6319 else
6320 p = end;
6321 if (buf == NULL)
6322 totlen += len;
6323 else
6324 q += len;
6325 }
6326 if (buf != NULL) /* second time here, finished */
6327 {
6328 STRCPY(q, p);
6329 break;
6330 }
6331
6332 totlen += STRLEN(p); /* Add on the trailing characters */
6333 buf = alloc((unsigned)(totlen + 1));
6334 if (buf == NULL)
6335 {
6336 vim_free(split_buf);
6337 return;
6338 }
6339 }
6340
6341#ifdef FEAT_EVAL
6342 current_SID = cmd->uc_scriptID;
6343#endif
6344 (void)do_cmdline(buf, eap->getline, eap->cookie,
6345 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
6346#ifdef FEAT_EVAL
6347 current_SID = save_current_SID;
6348#endif
6349 vim_free(buf);
6350 vim_free(split_buf);
6351}
6352
6353# if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6354 static char_u *
6355get_user_command_name(idx)
6356 int idx;
6357{
6358 return get_user_commands(NULL, idx - (int)CMD_SIZE);
6359}
6360
6361/*
6362 * Function given to ExpandGeneric() to obtain the list of user command names.
6363 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006364 char_u *
6365get_user_commands(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006366 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006367 int idx;
6368{
6369 if (idx < curbuf->b_ucmds.ga_len)
6370 return USER_CMD_GA(&curbuf->b_ucmds, idx)->uc_name;
6371 idx -= curbuf->b_ucmds.ga_len;
6372 if (idx < ucmds.ga_len)
6373 return USER_CMD(idx)->uc_name;
6374 return NULL;
6375}
6376
6377/*
6378 * Function given to ExpandGeneric() to obtain the list of user command
6379 * attributes.
6380 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006381 char_u *
6382get_user_cmd_flags(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 static char *user_cmd_flags[] =
6387 {"bang", "bar", "buffer", "complete", "count",
6388 "nargs", "range", "register"};
6389
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00006390 if (idx >= (int)(sizeof(user_cmd_flags) / sizeof(user_cmd_flags[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006391 return NULL;
6392 return (char_u *)user_cmd_flags[idx];
6393}
6394
6395/*
6396 * Function given to ExpandGeneric() to obtain the list of values for -nargs.
6397 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006398 char_u *
6399get_user_cmd_nargs(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006400 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006401 int idx;
6402{
6403 static char *user_cmd_nargs[] = {"0", "1", "*", "?", "+"};
6404
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00006405 if (idx >= (int)(sizeof(user_cmd_nargs) / sizeof(user_cmd_nargs[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006406 return NULL;
6407 return (char_u *)user_cmd_nargs[idx];
6408}
6409
6410/*
6411 * Function given to ExpandGeneric() to obtain the list of values for -complete.
6412 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006413 char_u *
6414get_user_cmd_complete(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006415 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006416 int idx;
6417{
6418 return (char_u *)command_complete[idx].name;
6419}
6420# endif /* FEAT_CMDL_COMPL */
6421
6422#endif /* FEAT_USR_CMDS */
6423
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006424#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
6425/*
6426 * Parse a completion argument "value[vallen]".
6427 * The detected completion goes in "*complp", argument type in "*argt".
6428 * When there is an argument, for function and user defined completion, it's
6429 * copied to allocated memory and stored in "*compl_arg".
6430 * Returns FAIL if something is wrong.
6431 */
6432 int
6433parse_compl_arg(value, vallen, complp, argt, compl_arg)
6434 char_u *value;
6435 int vallen;
6436 int *complp;
6437 long *argt;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006438 char_u **compl_arg UNUSED;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006439{
6440 char_u *arg = NULL;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006441# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006442 size_t arglen = 0;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006443# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006444 int i;
6445 int valend = vallen;
6446
6447 /* Look for any argument part - which is the part after any ',' */
6448 for (i = 0; i < vallen; ++i)
6449 {
6450 if (value[i] == ',')
6451 {
6452 arg = &value[i + 1];
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006453# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006454 arglen = vallen - i - 1;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006455# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006456 valend = i;
6457 break;
6458 }
6459 }
6460
6461 for (i = 0; command_complete[i].expand != 0; ++i)
6462 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006463 if ((int)STRLEN(command_complete[i].name) == valend
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006464 && STRNCMP(value, command_complete[i].name, valend) == 0)
6465 {
6466 *complp = command_complete[i].expand;
6467 if (command_complete[i].expand == EXPAND_BUFFERS)
6468 *argt |= BUFNAME;
6469 else if (command_complete[i].expand == EXPAND_DIRECTORIES
6470 || command_complete[i].expand == EXPAND_FILES)
6471 *argt |= XFILE;
6472 break;
6473 }
6474 }
6475
6476 if (command_complete[i].expand == 0)
6477 {
6478 EMSG2(_("E180: Invalid complete value: %s"), value);
6479 return FAIL;
6480 }
6481
6482# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6483 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST
6484 && arg != NULL)
6485# else
6486 if (arg != NULL)
6487# endif
6488 {
6489 EMSG(_("E468: Completion argument only allowed for custom completion"));
6490 return FAIL;
6491 }
6492
6493# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6494 if ((*complp == EXPAND_USER_DEFINED || *complp == EXPAND_USER_LIST)
6495 && arg == NULL)
6496 {
6497 EMSG(_("E467: Custom completion requires a function argument"));
6498 return FAIL;
6499 }
6500
6501 if (arg != NULL)
6502 *compl_arg = vim_strnsave(arg, (int)arglen);
6503# endif
6504 return OK;
6505}
6506#endif
6507
Bram Moolenaar071d4272004-06-13 20:20:40 +00006508 static void
6509ex_colorscheme(eap)
6510 exarg_T *eap;
6511{
Bram Moolenaare6850792010-05-14 15:28:44 +02006512 if (*eap->arg == NUL)
6513 {
6514#ifdef FEAT_EVAL
6515 char_u *expr = vim_strsave((char_u *)"g:colors_name");
6516 char_u *p = NULL;
6517
6518 if (expr != NULL)
6519 {
6520 ++emsg_off;
6521 p = eval_to_string(expr, NULL, FALSE);
6522 --emsg_off;
6523 vim_free(expr);
6524 }
6525 if (p != NULL)
6526 {
6527 MSG(p);
6528 vim_free(p);
6529 }
6530 else
6531 MSG("default");
6532#else
6533 MSG(_("unknown"));
6534#endif
6535 }
6536 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarbe1e9e92012-09-18 16:47:07 +02006537 EMSG2(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006538}
6539
6540 static void
6541ex_highlight(eap)
6542 exarg_T *eap;
6543{
6544 if (*eap->arg == NUL && eap->cmd[2] == '!')
6545 MSG(_("Greetings, Vim user!"));
6546 do_highlight(eap->arg, eap->forceit, FALSE);
6547}
6548
6549
6550/*
6551 * Call this function if we thought we were going to exit, but we won't
6552 * (because of an error). May need to restore the terminal mode.
6553 */
6554 void
6555not_exiting()
6556{
6557 exiting = FALSE;
6558 settmode(TMODE_RAW);
6559}
6560
6561/*
6562 * ":quit": quit current window, quit Vim if closed the last window.
6563 */
6564 static void
6565ex_quit(eap)
6566 exarg_T *eap;
6567{
6568#ifdef FEAT_CMDWIN
6569 if (cmdwin_type != 0)
6570 {
6571 cmdwin_result = Ctrl_C;
6572 return;
6573 }
6574#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006575 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006576 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006577 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006578 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006579 return;
6580 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006581#ifdef FEAT_AUTOCMD
Bram Moolenaar3b53dfb2012-06-06 18:03:07 +02006582 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01006583 /* Refuse to quit when locked or when the buffer in the last window is
Bram Moolenaar362ce482012-06-06 19:02:45 +02006584 * being closed (can only happen in autocommands). */
6585 if (curbuf_locked() || (curbuf->b_nwindows == 1 && curbuf->b_closing))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006586 return;
6587#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006588
6589#ifdef FEAT_NETBEANS_INTG
6590 netbeansForcedQuit = eap->forceit;
6591#endif
6592
6593 /*
6594 * If there are more files or windows we won't exit.
6595 */
6596 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6597 exiting = TRUE;
6598 if ((!P_HID(curbuf)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01006599 && check_changed(curbuf, (p_awa ? CCGD_AW : 0)
6600 | (eap->forceit ? CCGD_FORCEIT : 0)
6601 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006602 || check_more(TRUE, eap->forceit) == FAIL
6603 || (only_one_window() && check_changed_any(eap->forceit)))
6604 {
6605 not_exiting();
6606 }
6607 else
6608 {
6609#ifdef FEAT_WINDOWS
6610 if (only_one_window()) /* quit last window */
6611#endif
6612 getout(0);
6613#ifdef FEAT_WINDOWS
6614# ifdef FEAT_GUI
6615 need_mouse_correct = TRUE;
6616# endif
6617 /* close window; may free buffer */
6618 win_close(curwin, !P_HID(curwin->w_buffer) || eap->forceit);
6619#endif
6620 }
6621}
6622
6623/*
6624 * ":cquit".
6625 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006626 static void
6627ex_cquit(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006628 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006629{
6630 getout(1); /* this does not always pass on the exit code to the Manx
6631 compiler. why? */
6632}
6633
6634/*
6635 * ":qall": try to quit all windows
6636 */
6637 static void
6638ex_quit_all(eap)
6639 exarg_T *eap;
6640{
6641# ifdef FEAT_CMDWIN
6642 if (cmdwin_type != 0)
6643 {
6644 if (eap->forceit)
6645 cmdwin_result = K_XF1; /* ex_window() takes care of this */
6646 else
6647 cmdwin_result = K_XF2;
6648 return;
6649 }
6650# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006651
6652 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006653 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006654 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006655 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006656 return;
6657 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006658#ifdef FEAT_AUTOCMD
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01006659 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
6660 /* Refuse to quit when locked or when the buffer in the last window is
6661 * being closed (can only happen in autocommands). */
6662 if (curbuf_locked() || (curbuf->b_nwindows == 1 && curbuf->b_closing))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006663 return;
6664#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006665
Bram Moolenaar071d4272004-06-13 20:20:40 +00006666 exiting = TRUE;
6667 if (eap->forceit || !check_changed_any(FALSE))
6668 getout(0);
6669 not_exiting();
6670}
6671
Bram Moolenaard9967712006-03-11 21:18:15 +00006672#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006673/*
6674 * ":close": close current window, unless it is the last one
6675 */
6676 static void
6677ex_close(eap)
6678 exarg_T *eap;
6679{
6680# ifdef FEAT_CMDWIN
6681 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02006682 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006683 else
6684# endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006685 if (!text_locked()
6686#ifdef FEAT_AUTOCMD
6687 && !curbuf_locked()
6688#endif
6689 )
Bram Moolenaarf740b292006-02-16 22:11:02 +00006690 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006691}
6692
Bram Moolenaard9967712006-03-11 21:18:15 +00006693# ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006694/*
6695 * ":pclose": Close any preview window.
6696 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006697 static void
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006698ex_pclose(eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006699 exarg_T *eap;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006700{
6701 win_T *win;
6702
6703 for (win = firstwin; win != NULL; win = win->w_next)
6704 if (win->w_p_pvw)
6705 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006706 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006707 break;
6708 }
6709}
Bram Moolenaard9967712006-03-11 21:18:15 +00006710# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006711
Bram Moolenaarf740b292006-02-16 22:11:02 +00006712/*
6713 * Close window "win" and take care of handling closing the last window for a
6714 * modified buffer.
6715 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006716 static void
Bram Moolenaarf740b292006-02-16 22:11:02 +00006717ex_win_close(forceit, win, tp)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006718 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006719 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006720 tabpage_T *tp; /* NULL or the tab page "win" is in */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006721{
6722 int need_hide;
6723 buf_T *buf = win->w_buffer;
6724
6725 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006726 if (need_hide && !P_HID(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006727 {
Bram Moolenaard9967712006-03-11 21:18:15 +00006728# if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006729 if ((p_confirm || cmdmod.confirm) && p_write)
6730 {
6731 dialog_changed(buf, FALSE);
6732 if (buf_valid(buf) && bufIsChanged(buf))
6733 return;
6734 need_hide = FALSE;
6735 }
6736 else
Bram Moolenaard9967712006-03-11 21:18:15 +00006737# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006738 {
6739 EMSG(_(e_nowrtmsg));
6740 return;
6741 }
6742 }
6743
Bram Moolenaard9967712006-03-11 21:18:15 +00006744# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006745 need_mouse_correct = TRUE;
Bram Moolenaard9967712006-03-11 21:18:15 +00006746# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006747
Bram Moolenaar071d4272004-06-13 20:20:40 +00006748 /* free buffer when not hiding it or when it's a scratch buffer */
Bram Moolenaarf740b292006-02-16 22:11:02 +00006749 if (tp == NULL)
6750 win_close(win, !need_hide && !P_HID(buf));
6751 else
6752 win_close_othertab(win, !need_hide && !P_HID(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006753}
6754
Bram Moolenaar071d4272004-06-13 20:20:40 +00006755/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006756 * ":tabclose": close current tab page, unless it is the last one.
6757 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006758 */
6759 static void
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006760ex_tabclose(eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006761 exarg_T *eap;
6762{
Bram Moolenaarf740b292006-02-16 22:11:02 +00006763 tabpage_T *tp;
6764
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006765# ifdef FEAT_CMDWIN
6766 if (cmdwin_type != 0)
6767 cmdwin_result = K_IGNORE;
6768 else
6769# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006770 if (first_tabpage->tp_next == NULL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00006771 EMSG(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00006772 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006773 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006774 if (eap->addr_count > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006775 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006776 tp = find_tabpage((int)eap->line2);
6777 if (tp == NULL)
6778 {
6779 beep_flush();
6780 return;
6781 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00006782 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006783 {
6784 tabpage_close_other(tp, eap->forceit);
6785 return;
6786 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006787 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006788 if (!text_locked()
6789#ifdef FEAT_AUTOCMD
6790 && !curbuf_locked()
6791#endif
6792 )
Bram Moolenaarf740b292006-02-16 22:11:02 +00006793 tabpage_close(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006794 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006795}
6796
6797/*
6798 * ":tabonly": close all tab pages except the current one
6799 */
6800 static void
6801ex_tabonly(eap)
6802 exarg_T *eap;
6803{
6804 tabpage_T *tp;
6805 int done;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006806
6807# ifdef FEAT_CMDWIN
6808 if (cmdwin_type != 0)
6809 cmdwin_result = K_IGNORE;
6810 else
6811# endif
6812 if (first_tabpage->tp_next == NULL)
6813 MSG(_("Already only one tab page"));
6814 else
6815 {
6816 /* Repeat this up to a 1000 times, because autocommands may mess
6817 * up the lists. */
6818 for (done = 0; done < 1000; ++done)
6819 {
6820 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6821 if (tp->tp_topframe != topframe)
6822 {
6823 tabpage_close_other(tp, eap->forceit);
6824 /* if we failed to close it quit */
6825 if (valid_tabpage(tp))
6826 done = 1000;
6827 /* start over, "tp" is now invalid */
6828 break;
6829 }
6830 if (first_tabpage->tp_next == NULL)
6831 break;
6832 }
6833 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006834}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006835
6836/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006837 * Close the current tab page.
6838 */
6839 void
6840tabpage_close(forceit)
6841 int forceit;
6842{
6843 /* First close all the windows but the current one. If that worked then
6844 * close the last window in this tab, that will close it. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006845 if (lastwin != firstwin)
6846 close_others(TRUE, forceit);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006847 if (lastwin == firstwin)
6848 ex_win_close(forceit, curwin, NULL);
6849# ifdef FEAT_GUI
6850 need_mouse_correct = TRUE;
6851# endif
6852}
6853
6854/*
6855 * Close tab page "tp", which is not the current tab page.
6856 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006857 * Also takes care of the tab pages line disappearing when closing the
6858 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00006859 */
6860 void
6861tabpage_close_other(tp, forceit)
6862 tabpage_T *tp;
6863 int forceit;
6864{
6865 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006866 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006867 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006868
6869 /* Limit to 1000 windows, autocommands may add a window while we close
6870 * one. OK, so I'm paranoid... */
6871 while (++done < 1000)
6872 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006873 wp = tp->tp_firstwin;
6874 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006875
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006876 /* Autocommands may delete the tab page under our fingers and we may
6877 * fail to close a window with a modified buffer. */
6878 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006879 break;
6880 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006881
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006882 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006883 if (h != tabline_height())
6884 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006885}
6886
6887/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006888 * ":only".
6889 */
6890 static void
6891ex_only(eap)
6892 exarg_T *eap;
6893{
6894# ifdef FEAT_GUI
6895 need_mouse_correct = TRUE;
6896# endif
6897 close_others(TRUE, eap->forceit);
6898}
6899
6900/*
6901 * ":all" and ":sall".
Bram Moolenaard9967712006-03-11 21:18:15 +00006902 * Also used for ":tab drop file ..." after setting the argument list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006903 */
Bram Moolenaard9967712006-03-11 21:18:15 +00006904 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00006905ex_all(eap)
6906 exarg_T *eap;
6907{
6908 if (eap->addr_count == 0)
6909 eap->line2 = 9999;
Bram Moolenaard9967712006-03-11 21:18:15 +00006910 do_arg_all((int)eap->line2, eap->forceit, eap->cmdidx == CMD_drop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006911}
6912#endif /* FEAT_WINDOWS */
6913
6914 static void
6915ex_hide(eap)
6916 exarg_T *eap;
6917{
6918 if (*eap->arg != NUL && check_nextcmd(eap->arg) == NULL)
6919 eap->errmsg = e_invarg;
6920 else
6921 {
6922 /* ":hide" or ":hide | cmd": hide current window */
6923 eap->nextcmd = check_nextcmd(eap->arg);
6924#ifdef FEAT_WINDOWS
6925 if (!eap->skip)
6926 {
6927# ifdef FEAT_GUI
6928 need_mouse_correct = TRUE;
6929# endif
6930 win_close(curwin, FALSE); /* don't free buffer */
6931 }
6932#endif
6933 }
6934}
6935
6936/*
6937 * ":stop" and ":suspend": Suspend Vim.
6938 */
6939 static void
6940ex_stop(eap)
6941 exarg_T *eap;
6942{
6943 /*
6944 * Disallow suspending for "rvim".
6945 */
6946 if (!check_restricted()
6947#ifdef WIN3264
6948 /*
6949 * Check if external commands are allowed now.
6950 */
6951 && can_end_termcap_mode(TRUE)
6952#endif
6953 )
6954 {
6955 if (!eap->forceit)
6956 autowrite_all();
6957 windgoto((int)Rows - 1, 0);
6958 out_char('\n');
6959 out_flush();
6960 stoptermcap();
6961 out_flush(); /* needed for SUN to restore xterm buffer */
6962#ifdef FEAT_TITLE
6963 mch_restore_title(3); /* restore window titles */
6964#endif
6965 ui_suspend(); /* call machine specific function */
6966#ifdef FEAT_TITLE
6967 maketitle();
6968 resettitle(); /* force updating the title */
6969#endif
6970 starttermcap();
6971 scroll_start(); /* scroll screen before redrawing */
6972 redraw_later_clear();
6973 shell_resized(); /* may have resized window */
6974 }
6975}
6976
6977/*
6978 * ":exit", ":xit" and ":wq": Write file and exit Vim.
6979 */
6980 static void
6981ex_exit(eap)
6982 exarg_T *eap;
6983{
6984#ifdef FEAT_CMDWIN
6985 if (cmdwin_type != 0)
6986 {
6987 cmdwin_result = Ctrl_C;
6988 return;
6989 }
6990#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006991 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006992 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006993 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006994 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006995 return;
6996 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006997#ifdef FEAT_AUTOCMD
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01006998 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
6999 /* Refuse to quit when locked or when the buffer in the last window is
7000 * being closed (can only happen in autocommands). */
7001 if (curbuf_locked() || (curbuf->b_nwindows == 1 && curbuf->b_closing))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007002 return;
7003#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007004
7005 /*
7006 * if more files or windows we won't exit
7007 */
7008 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
7009 exiting = TRUE;
7010 if ( ((eap->cmdidx == CMD_wq
7011 || curbufIsChanged())
7012 && do_write(eap) == FAIL)
7013 || check_more(TRUE, eap->forceit) == FAIL
7014 || (only_one_window() && check_changed_any(eap->forceit)))
7015 {
7016 not_exiting();
7017 }
7018 else
7019 {
7020#ifdef FEAT_WINDOWS
7021 if (only_one_window()) /* quit last window, exit Vim */
7022#endif
7023 getout(0);
7024#ifdef FEAT_WINDOWS
7025# ifdef FEAT_GUI
7026 need_mouse_correct = TRUE;
7027# endif
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02007028 /* Quit current window, may free the buffer. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007029 win_close(curwin, !P_HID(curwin->w_buffer));
7030#endif
7031 }
7032}
7033
7034/*
7035 * ":print", ":list", ":number".
7036 */
7037 static void
7038ex_print(eap)
7039 exarg_T *eap;
7040{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007041 if (curbuf->b_ml.ml_flags & ML_EMPTY)
7042 EMSG(_(e_emptybuf));
7043 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007044 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007045 for ( ;!got_int; ui_breakcheck())
7046 {
7047 print_line(eap->line1,
7048 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
7049 || (eap->flags & EXFLAG_NR)),
7050 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
7051 if (++eap->line1 > eap->line2)
7052 break;
7053 out_flush(); /* show one line at a time */
7054 }
7055 setpcmark();
7056 /* put cursor at last line */
7057 curwin->w_cursor.lnum = eap->line2;
7058 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007059 }
7060
Bram Moolenaar071d4272004-06-13 20:20:40 +00007061 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007062}
7063
7064#ifdef FEAT_BYTEOFF
7065 static void
7066ex_goto(eap)
7067 exarg_T *eap;
7068{
7069 goto_byte(eap->line2);
7070}
7071#endif
7072
7073/*
7074 * ":shell".
7075 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007076 static void
7077ex_shell(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007078 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007079{
7080 do_shell(NULL, 0);
7081}
7082
7083#if (defined(FEAT_WINDOWS) && defined(HAVE_DROP_FILE)) \
7084 || (defined(FEAT_GUI_GTK) && defined(FEAT_DND)) \
Bram Moolenaar371baa92005-12-29 22:43:53 +00007085 || defined(FEAT_GUI_MSWIN) \
7086 || defined(FEAT_GUI_MAC) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00007087 || defined(PROTO)
7088
7089/*
7090 * Handle a file drop. The code is here because a drop is *nearly* like an
7091 * :args command, but not quite (we have a list of exact filenames, so we
7092 * don't want to (a) parse a command line, or (b) expand wildcards. So the
7093 * code is very similar to :args and hence needs access to a lot of the static
7094 * functions in this file.
7095 *
7096 * The list should be allocated using alloc(), as should each item in the
7097 * list. This function takes over responsibility for freeing the list.
7098 *
Bram Moolenaar81870892007-11-11 18:17:28 +00007099 * XXX The list is made into the argument list. This is freed using
Bram Moolenaar071d4272004-06-13 20:20:40 +00007100 * FreeWild(), which does a series of vim_free() calls, unless the two defines
7101 * __EMX__ and __ALWAYS_HAS_TRAILING_NUL_POINTER are set. In this case, a
7102 * routine _fnexplodefree() is used. This may cause problems, but as the drop
7103 * file functionality is (currently) not in EMX this is not presently a
7104 * problem.
7105 */
7106 void
7107handle_drop(filec, filev, split)
7108 int filec; /* the number of files dropped */
7109 char_u **filev; /* the list of files dropped */
7110 int split; /* force splitting the window */
7111{
7112 exarg_T ea;
7113 int save_msg_scroll = msg_scroll;
7114
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007115 /* Postpone this while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007116 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007117 return;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007118#ifdef FEAT_AUTOCMD
7119 if (curbuf_locked())
7120 return;
7121#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00007122 /* When the screen is being updated we should not change buffers and
7123 * windows structures, it may cause freed memory to be used. */
7124 if (updating_screen)
7125 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007126
7127 /* Check whether the current buffer is changed. If so, we will need
7128 * to split the current window or data could be lost.
7129 * We don't need to check if the 'hidden' option is set, as in this
7130 * case the buffer won't be lost.
7131 */
7132 if (!P_HID(curbuf) && !split)
7133 {
7134 ++emsg_off;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01007135 split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007136 --emsg_off;
7137 }
7138 if (split)
7139 {
7140# ifdef FEAT_WINDOWS
7141 if (win_split(0, 0) == FAIL)
7142 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007143 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007144
7145 /* When splitting the window, create a new alist. Otherwise the
7146 * existing one is overwritten. */
7147 alist_unlink(curwin->w_alist);
7148 alist_new();
7149# else
7150 return; /* can't split, always fail */
7151# endif
7152 }
7153
7154 /*
7155 * Set up the new argument list.
7156 */
Bram Moolenaar86b68352004-12-27 21:59:20 +00007157 alist_set(ALIST(curwin), filec, filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007158
7159 /*
7160 * Move to the first file.
7161 */
7162 /* Fake up a minimal "next" command for do_argfile() */
7163 vim_memset(&ea, 0, sizeof(ea));
7164 ea.cmd = (char_u *)"next";
7165 do_argfile(&ea, 0);
7166
7167 /* do_ecmd() may set need_start_insertmode, but since we never left Insert
7168 * mode that is not needed here. */
7169 need_start_insertmode = FALSE;
7170
7171 /* Restore msg_scroll, otherwise a following command may cause scrolling
7172 * unexpectedly. The screen will be redrawn by the caller, thus
7173 * msg_scroll being set by displaying a message is irrelevant. */
7174 msg_scroll = save_msg_scroll;
7175}
7176#endif
7177
Bram Moolenaar071d4272004-06-13 20:20:40 +00007178/*
7179 * Clear an argument list: free all file names and reset it to zero entries.
7180 */
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007181 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00007182alist_clear(al)
7183 alist_T *al;
7184{
7185 while (--al->al_ga.ga_len >= 0)
7186 vim_free(AARGLIST(al)[al->al_ga.ga_len].ae_fname);
7187 ga_clear(&al->al_ga);
7188}
7189
7190/*
7191 * Init an argument list.
7192 */
7193 void
7194alist_init(al)
7195 alist_T *al;
7196{
7197 ga_init2(&al->al_ga, (int)sizeof(aentry_T), 5);
7198}
7199
7200#if defined(FEAT_WINDOWS) || defined(PROTO)
7201
7202/*
7203 * Remove a reference from an argument list.
7204 * Ignored when the argument list is the global one.
7205 * If the argument list is no longer used by any window, free it.
7206 */
7207 void
7208alist_unlink(al)
7209 alist_T *al;
7210{
7211 if (al != &global_alist && --al->al_refcount <= 0)
7212 {
7213 alist_clear(al);
7214 vim_free(al);
7215 }
7216}
7217
7218# if defined(FEAT_LISTCMDS) || defined(HAVE_DROP_FILE) || defined(PROTO)
7219/*
7220 * Create a new argument list and use it for the current window.
7221 */
7222 void
7223alist_new()
7224{
7225 curwin->w_alist = (alist_T *)alloc((unsigned)sizeof(alist_T));
7226 if (curwin->w_alist == NULL)
7227 {
7228 curwin->w_alist = &global_alist;
7229 ++global_alist.al_refcount;
7230 }
7231 else
7232 {
7233 curwin->w_alist->al_refcount = 1;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02007234 curwin->w_alist->id = ++max_alist_id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007235 alist_init(curwin->w_alist);
7236 }
7237}
7238# endif
7239#endif
7240
7241#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE) || defined(PROTO)
7242/*
7243 * Expand the file names in the global argument list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00007244 * If "fnum_list" is not NULL, use "fnum_list[fnum_len]" as a list of buffer
7245 * numbers to be re-used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007246 */
7247 void
Bram Moolenaar86b68352004-12-27 21:59:20 +00007248alist_expand(fnum_list, fnum_len)
7249 int *fnum_list;
7250 int fnum_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007251{
7252 char_u **old_arg_files;
Bram Moolenaar86b68352004-12-27 21:59:20 +00007253 int old_arg_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007254 char_u **new_arg_files;
7255 int new_arg_file_count;
7256 char_u *save_p_su = p_su;
7257 int i;
7258
7259 /* Don't use 'suffixes' here. This should work like the shell did the
7260 * expansion. Also, the vimrc file isn't read yet, thus the user
7261 * can't set the options. */
7262 p_su = empty_option;
7263 old_arg_files = (char_u **)alloc((unsigned)(sizeof(char_u *) * GARGCOUNT));
7264 if (old_arg_files != NULL)
7265 {
7266 for (i = 0; i < GARGCOUNT; ++i)
Bram Moolenaar86b68352004-12-27 21:59:20 +00007267 old_arg_files[i] = vim_strsave(GARGLIST[i].ae_fname);
7268 old_arg_count = GARGCOUNT;
7269 if (expand_wildcards(old_arg_count, old_arg_files,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007270 &new_arg_file_count, &new_arg_files,
Bram Moolenaarb5609832011-07-20 15:04:58 +02007271 EW_FILE|EW_NOTFOUND|EW_ADDSLASH|EW_NOERROR) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007272 && new_arg_file_count > 0)
7273 {
Bram Moolenaar86b68352004-12-27 21:59:20 +00007274 alist_set(&global_alist, new_arg_file_count, new_arg_files,
7275 TRUE, fnum_list, fnum_len);
7276 FreeWild(old_arg_count, old_arg_files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007277 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007278 }
7279 p_su = save_p_su;
7280}
7281#endif
7282
7283/*
7284 * Set the argument list for the current window.
7285 * Takes over the allocated files[] and the allocated fnames in it.
7286 */
7287 void
Bram Moolenaar86b68352004-12-27 21:59:20 +00007288alist_set(al, count, files, use_curbuf, fnum_list, fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007289 alist_T *al;
7290 int count;
7291 char_u **files;
7292 int use_curbuf;
Bram Moolenaar86b68352004-12-27 21:59:20 +00007293 int *fnum_list;
7294 int fnum_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007295{
7296 int i;
7297
7298 alist_clear(al);
7299 if (ga_grow(&al->al_ga, count) == OK)
7300 {
7301 for (i = 0; i < count; ++i)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007302 {
7303 if (got_int)
7304 {
7305 /* When adding many buffers this can take a long time. Allow
7306 * interrupting here. */
7307 while (i < count)
7308 vim_free(files[i++]);
7309 break;
7310 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00007311
7312 /* May set buffer name of a buffer previously used for the
7313 * argument list, so that it's re-used by alist_add. */
7314 if (fnum_list != NULL && i < fnum_len)
7315 buf_set_name(fnum_list[i], files[i]);
7316
Bram Moolenaar071d4272004-06-13 20:20:40 +00007317 alist_add(al, files[i], use_curbuf ? 2 : 1);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007318 ui_breakcheck();
7319 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007320 vim_free(files);
7321 }
7322 else
7323 FreeWild(count, files);
7324#ifdef FEAT_WINDOWS
7325 if (al == &global_alist)
7326#endif
7327 arg_had_last = FALSE;
7328}
7329
7330/*
7331 * Add file "fname" to argument list "al".
7332 * "fname" must have been allocated and "al" must have been checked for room.
7333 */
7334 void
7335alist_add(al, fname, set_fnum)
7336 alist_T *al;
7337 char_u *fname;
7338 int set_fnum; /* 1: set buffer number; 2: re-use curbuf */
7339{
7340 if (fname == NULL) /* don't add NULL file names */
7341 return;
7342#ifdef BACKSLASH_IN_FILENAME
7343 slash_adjust(fname);
7344#endif
7345 AARGLIST(al)[al->al_ga.ga_len].ae_fname = fname;
7346 if (set_fnum > 0)
7347 AARGLIST(al)[al->al_ga.ga_len].ae_fnum =
7348 buflist_add(fname, BLN_LISTED | (set_fnum == 2 ? BLN_CURBUF : 0));
7349 ++al->al_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007350}
7351
7352#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
7353/*
7354 * Adjust slashes in file names. Called after 'shellslash' was set.
7355 */
7356 void
7357alist_slash_adjust()
7358{
7359 int i;
7360# ifdef FEAT_WINDOWS
7361 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007362 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007363# endif
7364
7365 for (i = 0; i < GARGCOUNT; ++i)
7366 if (GARGLIST[i].ae_fname != NULL)
7367 slash_adjust(GARGLIST[i].ae_fname);
7368# ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00007369 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007370 if (wp->w_alist != &global_alist)
7371 for (i = 0; i < WARGCOUNT(wp); ++i)
7372 if (WARGLIST(wp)[i].ae_fname != NULL)
7373 slash_adjust(WARGLIST(wp)[i].ae_fname);
7374# endif
7375}
7376#endif
7377
7378/*
7379 * ":preserve".
7380 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007381 static void
7382ex_preserve(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007383 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007384{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007385 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007386 ml_preserve(curbuf, TRUE);
7387}
7388
7389/*
7390 * ":recover".
7391 */
7392 static void
7393ex_recover(eap)
7394 exarg_T *eap;
7395{
7396 /* Set recoverymode right away to avoid the ATTENTION prompt. */
7397 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01007398 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
7399 | CCGD_MULTWIN
7400 | (eap->forceit ? CCGD_FORCEIT : 0)
7401 | CCGD_EXCMD)
7402
Bram Moolenaar071d4272004-06-13 20:20:40 +00007403 && (*eap->arg == NUL
7404 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
7405 ml_recover();
7406 recoverymode = FALSE;
7407}
7408
7409/*
7410 * Command modifier used in a wrong way.
7411 */
7412 static void
7413ex_wrongmodifier(eap)
7414 exarg_T *eap;
7415{
7416 eap->errmsg = e_invcmd;
7417}
7418
7419#ifdef FEAT_WINDOWS
7420/*
7421 * :sview [+command] file split window with new file, read-only
7422 * :split [[+command] file] split window with current or new file
7423 * :vsplit [[+command] file] split window vertically with current or new file
7424 * :new [[+command] file] split window with no or new file
7425 * :vnew [[+command] file] split vertically window with no or new file
7426 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007427 *
7428 * :tabedit open new Tab page with empty window
7429 * :tabedit [+command] file open new Tab page and edit "file"
7430 * :tabnew [[+command] file] just like :tabedit
7431 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007432 */
7433 void
7434ex_splitview(eap)
7435 exarg_T *eap;
7436{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007437 win_T *old_curwin = curwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007438# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007439 char_u *fname = NULL;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007440# endif
7441# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007442 int browse_flag = cmdmod.browse;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007443# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007444
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007445# ifndef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007446 if (eap->cmdidx == CMD_vsplit || eap->cmdidx == CMD_vnew)
7447 {
7448 ex_ni(eap);
7449 return;
7450 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007451# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007452
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007453# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007454 need_mouse_correct = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007455# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007456
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007457# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00007458 /* A ":split" in the quickfix window works like ":new". Don't want two
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007459 * quickfix windows. But it's OK when doing ":tab split". */
7460 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007461 {
7462 if (eap->cmdidx == CMD_split)
7463 eap->cmdidx = CMD_new;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007464# ifdef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007465 if (eap->cmdidx == CMD_vsplit)
7466 eap->cmdidx = CMD_vnew;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007467# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007468 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007469# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007470
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007471# ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007472 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007473 {
7474 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
7475 FNAME_MESS, TRUE, curbuf->b_ffname);
7476 if (fname == NULL)
7477 goto theend;
7478 eap->arg = fname;
7479 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007480# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007481 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007482# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007483# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007484# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007485 if (cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007486# ifdef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007487 && eap->cmdidx != CMD_vnew
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007488# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007489 && eap->cmdidx != CMD_new)
7490 {
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007491# ifdef FEAT_AUTOCMD
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007492 if (
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007493# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007494 !gui.in_use &&
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007495# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007496 au_has_group((char_u *)"FileExplorer"))
7497 {
7498 /* No browsing supported but we do have the file explorer:
7499 * Edit the directory. */
7500 if (*eap->arg == NUL || !mch_isdir(eap->arg))
7501 eap->arg = (char_u *)".";
7502 }
7503 else
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007504# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007505 {
7506 fname = do_browse(0, (char_u *)_("Edit File in new window"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007507 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007508 if (fname == NULL)
7509 goto theend;
7510 eap->arg = fname;
7511 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007512 }
7513 cmdmod.browse = FALSE; /* Don't browse again in do_ecmd(). */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007514# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007515
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007516 /*
7517 * Either open new tab page or split the window.
7518 */
7519 if (eap->cmdidx == CMD_tabedit
7520 || eap->cmdidx == CMD_tabfind
7521 || eap->cmdidx == CMD_tabnew)
7522 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00007523 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
7524 : eap->addr_count == 0 ? 0
7525 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007526 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00007527 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007528
7529 /* set the alternate buffer for the window we came from */
7530 if (curwin != old_curwin
7531 && win_valid(old_curwin)
7532 && old_curwin->w_buffer != curbuf
7533 && !cmdmod.keepalt)
7534 old_curwin->w_alt_fnum = curbuf->b_fnum;
7535 }
7536 }
7537 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007538 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
7539 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007540# ifdef FEAT_SCROLLBIND
Bram Moolenaar071d4272004-06-13 20:20:40 +00007541 /* Reset 'scrollbind' when editing another file, but keep it when
7542 * doing ":split" without arguments. */
7543 if (*eap->arg != NUL
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007544# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007545 || cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007546# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007547 )
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007548 {
7549 RESET_BINDING(curwin);
7550 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007551 else
7552 do_check_scrollbind(FALSE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007553# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007554 do_exedit(eap, old_curwin);
7555 }
7556
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007557# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007558 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007559# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007560
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007561# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007562theend:
7563 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007564# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007565}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007566
7567/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007568 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007569 */
7570 void
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007571tabpage_new()
7572{
7573 exarg_T ea;
7574
7575 vim_memset(&ea, 0, sizeof(ea));
7576 ea.cmdidx = CMD_tabnew;
7577 ea.cmd = (char_u *)"tabn";
7578 ea.arg = (char_u *)"";
7579 ex_splitview(&ea);
7580}
7581
7582/*
7583 * :tabnext command
7584 */
7585 static void
7586ex_tabnext(eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007587 exarg_T *eap;
7588{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007589 switch (eap->cmdidx)
7590 {
7591 case CMD_tabfirst:
7592 case CMD_tabrewind:
7593 goto_tabpage(1);
7594 break;
7595 case CMD_tablast:
7596 goto_tabpage(9999);
7597 break;
7598 case CMD_tabprevious:
7599 case CMD_tabNext:
7600 goto_tabpage(eap->addr_count == 0 ? -1 : -(int)eap->line2);
7601 break;
7602 default: /* CMD_tabnext */
7603 goto_tabpage(eap->addr_count == 0 ? 0 : (int)eap->line2);
7604 break;
7605 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007606}
7607
7608/*
7609 * :tabmove command
7610 */
7611 static void
7612ex_tabmove(eap)
7613 exarg_T *eap;
7614{
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02007615 int tab_number = 9999;
7616
7617 if (eap->arg && *eap->arg != NUL)
7618 {
7619 char_u *p = eap->arg;
7620 int relative = 0; /* argument +N/-N means: move N places to the
7621 * right/left relative to the current position. */
7622
7623 if (*eap->arg == '-')
7624 {
7625 relative = -1;
7626 p = eap->arg + 1;
7627 }
7628 else if (*eap->arg == '+')
7629 {
7630 relative = 1;
7631 p = eap->arg + 1;
7632 }
7633 else
7634 p = eap->arg;
7635
7636 if (p == skipdigits(p))
7637 {
7638 /* No numbers as argument. */
7639 eap->errmsg = e_invarg;
7640 return;
7641 }
7642
7643 tab_number = getdigits(&p);
7644 if (relative != 0)
7645 tab_number = tab_number * relative + tabpage_index(curtab) - 1;;
7646 }
7647 else if (eap->addr_count != 0)
7648 tab_number = eap->line2;
7649
7650 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007651}
7652
7653/*
7654 * :tabs command: List tabs and their contents.
7655 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007656 static void
7657ex_tabs(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007658 exarg_T *eap UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007659{
7660 tabpage_T *tp;
7661 win_T *wp;
7662 int tabcount = 1;
7663
7664 msg_start();
7665 msg_scroll = TRUE;
7666 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
7667 {
7668 msg_putchar('\n');
7669 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
7670 msg_outtrans_attr(IObuff, hl_attr(HLF_T));
7671 out_flush(); /* output one line at a time */
7672 ui_breakcheck();
7673
Bram Moolenaar030f0df2006-02-21 22:02:53 +00007674 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007675 wp = firstwin;
7676 else
7677 wp = tp->tp_firstwin;
7678 for ( ; wp != NULL && !got_int; wp = wp->w_next)
7679 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007680 msg_putchar('\n');
7681 msg_putchar(wp == curwin ? '>' : ' ');
7682 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007683 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
7684 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007685 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02007686 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007687 else
7688 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
7689 IObuff, IOSIZE, TRUE);
7690 msg_outtrans(IObuff);
7691 out_flush(); /* output one line at a time */
7692 ui_breakcheck();
7693 }
7694 }
7695}
7696
7697#endif /* FEAT_WINDOWS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007698
7699/*
7700 * ":mode": Set screen mode.
7701 * If no argument given, just get the screen size and redraw.
7702 */
7703 static void
7704ex_mode(eap)
7705 exarg_T *eap;
7706{
7707 if (*eap->arg == NUL)
7708 shell_resized();
7709 else
7710 mch_screenmode(eap->arg);
7711}
7712
7713#ifdef FEAT_WINDOWS
7714/*
7715 * ":resize".
7716 * set, increment or decrement current window height
7717 */
7718 static void
7719ex_resize(eap)
7720 exarg_T *eap;
7721{
7722 int n;
7723 win_T *wp = curwin;
7724
7725 if (eap->addr_count > 0)
7726 {
7727 n = eap->line2;
7728 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
7729 ;
7730 }
7731
7732#ifdef FEAT_GUI
7733 need_mouse_correct = TRUE;
7734#endif
7735 n = atol((char *)eap->arg);
7736#ifdef FEAT_VERTSPLIT
7737 if (cmdmod.split & WSP_VERT)
7738 {
7739 if (*eap->arg == '-' || *eap->arg == '+')
7740 n += W_WIDTH(curwin);
7741 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
7742 n = 9999;
7743 win_setwidth_win((int)n, wp);
7744 }
7745 else
7746#endif
7747 {
7748 if (*eap->arg == '-' || *eap->arg == '+')
7749 n += curwin->w_height;
7750 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
7751 n = 9999;
7752 win_setheight_win((int)n, wp);
7753 }
7754}
7755#endif
7756
7757/*
7758 * ":find [+command] <file>" command.
7759 */
7760 static void
7761ex_find(eap)
7762 exarg_T *eap;
7763{
7764#ifdef FEAT_SEARCHPATH
7765 char_u *fname;
7766 int count;
7767
7768 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
7769 TRUE, curbuf->b_ffname);
7770 if (eap->addr_count > 0)
7771 {
7772 /* Repeat finding the file "count" times. This matters when it
7773 * appears several times in the path. */
7774 count = eap->line2;
7775 while (fname != NULL && --count > 0)
7776 {
7777 vim_free(fname);
7778 fname = find_file_in_path(NULL, 0, FNAME_MESS,
7779 FALSE, curbuf->b_ffname);
7780 }
7781 }
7782
7783 if (fname != NULL)
7784 {
7785 eap->arg = fname;
7786#endif
7787 do_exedit(eap, NULL);
7788#ifdef FEAT_SEARCHPATH
7789 vim_free(fname);
7790 }
7791#endif
7792}
7793
7794/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00007795 * ":open" simulation: for now just work like ":visual".
7796 */
7797 static void
7798ex_open(eap)
7799 exarg_T *eap;
7800{
7801 regmatch_T regmatch;
7802 char_u *p;
7803
7804 curwin->w_cursor.lnum = eap->line2;
7805 beginline(BL_SOL | BL_FIX);
7806 if (*eap->arg == '/')
7807 {
7808 /* ":open /pattern/": put cursor in column found with pattern */
7809 ++eap->arg;
7810 p = skip_regexp(eap->arg, '/', p_magic, NULL);
7811 *p = NUL;
7812 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
7813 if (regmatch.regprog != NULL)
7814 {
7815 regmatch.rm_ic = p_ic;
7816 p = ml_get_curline();
7817 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007818 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007819 else
7820 EMSG(_(e_nomatch));
Bram Moolenaar473de612013-06-08 18:19:48 +02007821 vim_regfree(regmatch.regprog);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007822 }
7823 /* Move to the NUL, ignore any other arguments. */
7824 eap->arg += STRLEN(eap->arg);
7825 }
7826 check_cursor();
7827
7828 eap->cmdidx = CMD_visual;
7829 do_exedit(eap, NULL);
7830}
7831
7832/*
7833 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007834 */
7835 static void
7836ex_edit(eap)
7837 exarg_T *eap;
7838{
7839 do_exedit(eap, NULL);
7840}
7841
7842/*
7843 * ":edit <file>" command and alikes.
7844 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007845 void
7846do_exedit(eap, old_curwin)
7847 exarg_T *eap;
7848 win_T *old_curwin; /* curwin before doing a split or NULL */
7849{
7850 int n;
7851#ifdef FEAT_WINDOWS
7852 int need_hide;
7853#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00007854 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007855
7856 /*
7857 * ":vi" command ends Ex mode.
7858 */
7859 if (exmode_active && (eap->cmdidx == CMD_visual
7860 || eap->cmdidx == CMD_view))
7861 {
7862 exmode_active = FALSE;
7863 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007864 {
7865 /* Special case: ":global/pat/visual\NLvi-commands" */
7866 if (global_busy)
7867 {
7868 int rd = RedrawingDisabled;
7869 int nwr = no_wait_return;
7870 int ms = msg_scroll;
7871#ifdef FEAT_GUI
7872 int he = hold_gui_events;
7873#endif
7874
7875 if (eap->nextcmd != NULL)
7876 {
7877 stuffReadbuff(eap->nextcmd);
7878 eap->nextcmd = NULL;
7879 }
7880
7881 if (exmode_was != EXMODE_VIM)
7882 settmode(TMODE_RAW);
7883 RedrawingDisabled = 0;
7884 no_wait_return = 0;
7885 need_wait_return = FALSE;
7886 msg_scroll = 0;
7887#ifdef FEAT_GUI
7888 hold_gui_events = 0;
7889#endif
7890 must_redraw = CLEAR;
7891
7892 main_loop(FALSE, TRUE);
7893
7894 RedrawingDisabled = rd;
7895 no_wait_return = nwr;
7896 msg_scroll = ms;
7897#ifdef FEAT_GUI
7898 hold_gui_events = he;
7899#endif
7900 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007901 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007902 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007903 }
7904
7905 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007906 || eap->cmdidx == CMD_tabnew
7907 || eap->cmdidx == CMD_tabedit
Bram Moolenaar071d4272004-06-13 20:20:40 +00007908#ifdef FEAT_VERTSPLIT
7909 || eap->cmdidx == CMD_vnew
7910#endif
7911 ) && *eap->arg == NUL)
7912 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007913 /* ":new" or ":tabnew" without argument: edit an new empty buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007914 setpcmark();
7915 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007916 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
7917 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007918 }
7919 else if ((eap->cmdidx != CMD_split
7920#ifdef FEAT_VERTSPLIT
7921 && eap->cmdidx != CMD_vsplit
7922#endif
7923 )
7924 || *eap->arg != NUL
7925#ifdef FEAT_BROWSE
7926 || cmdmod.browse
7927#endif
7928 )
7929 {
Bram Moolenaar5555acc2006-04-07 21:33:12 +00007930#ifdef FEAT_AUTOCMD
7931 /* Can't edit another file when "curbuf_lock" is set. Only ":edit"
7932 * can bring us here, others are stopped earlier. */
7933 if (*eap->arg != NUL && curbuf_locked())
7934 return;
7935#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007936 n = readonlymode;
7937 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
7938 readonlymode = TRUE;
7939 else if (eap->cmdidx == CMD_enew)
7940 readonlymode = FALSE; /* 'readonly' doesn't make sense in an
7941 empty buffer */
7942 setpcmark();
7943 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
7944 NULL, eap,
7945 /* ":edit" goes to first line if Vi compatible */
7946 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
7947 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
7948 ? ECMD_ONE : eap->do_ecmd_lnum,
7949 (P_HID(curbuf) ? ECMD_HIDE : 0)
7950 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar7b449342014-03-25 13:03:48 +01007951 /* after a split we can use an existing buffer */
7952 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007953#ifdef FEAT_LISTCMDS
7954 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
7955#endif
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007956 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007957 {
7958 /* Editing the file failed. If the window was split, close it. */
7959#ifdef FEAT_WINDOWS
7960 if (old_curwin != NULL)
7961 {
7962 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
7963 if (!need_hide || P_HID(curbuf))
7964 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007965# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7966 cleanup_T cs;
7967
7968 /* Reset the error/interrupt/exception state here so that
7969 * aborting() returns FALSE when closing a window. */
7970 enter_cleanup(&cs);
7971# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007972# ifdef FEAT_GUI
7973 need_mouse_correct = TRUE;
7974# endif
7975 win_close(curwin, !need_hide && !P_HID(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007976
7977# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7978 /* Restore the error/interrupt/exception state if not
7979 * discarded by a new aborting error, interrupt, or
7980 * uncaught exception. */
7981 leave_cleanup(&cs);
7982# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007983 }
7984 }
7985#endif
7986 }
7987 else if (readonlymode && curbuf->b_nwindows == 1)
7988 {
7989 /* When editing an already visited buffer, 'readonly' won't be set
7990 * but the previous value is kept. With ":view" and ":sview" we
7991 * want the file to be readonly, except when another window is
7992 * editing the same buffer. */
7993 curbuf->b_p_ro = TRUE;
7994 }
7995 readonlymode = n;
7996 }
7997 else
7998 {
7999 if (eap->do_ecmd_cmd != NULL)
8000 do_cmdline_cmd(eap->do_ecmd_cmd);
8001#ifdef FEAT_TITLE
8002 n = curwin->w_arg_idx_invalid;
8003#endif
8004 check_arg_idx(curwin);
8005#ifdef FEAT_TITLE
8006 if (n != curwin->w_arg_idx_invalid)
8007 maketitle();
8008#endif
8009 }
8010
8011#ifdef FEAT_WINDOWS
8012 /*
8013 * if ":split file" worked, set alternate file name in old window to new
8014 * file
8015 */
8016 if (old_curwin != NULL
8017 && *eap->arg != NUL
8018 && curwin != old_curwin
8019 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00008020 && old_curwin->w_buffer != curbuf
8021 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008022 old_curwin->w_alt_fnum = curbuf->b_fnum;
8023#endif
8024
8025 ex_no_reprint = TRUE;
8026}
8027
8028#ifndef FEAT_GUI
8029/*
8030 * ":gui" and ":gvim" when there is no GUI.
8031 */
8032 static void
8033ex_nogui(eap)
8034 exarg_T *eap;
8035{
8036 eap->errmsg = e_nogvim;
8037}
8038#endif
8039
8040#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
8041 static void
8042ex_tearoff(eap)
8043 exarg_T *eap;
8044{
8045 gui_make_tearoff(eap->arg);
8046}
8047#endif
8048
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008049#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008050 static void
8051ex_popup(eap)
8052 exarg_T *eap;
8053{
Bram Moolenaar97409f12005-07-08 22:17:29 +00008054 gui_make_popup(eap->arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008055}
8056#endif
8057
Bram Moolenaar071d4272004-06-13 20:20:40 +00008058 static void
8059ex_swapname(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008060 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008061{
8062 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
8063 MSG(_("No swap file"));
8064 else
8065 msg(curbuf->b_ml.ml_mfp->mf_fname);
8066}
8067
8068/*
8069 * ":syncbind" forces all 'scrollbind' windows to have the same relative
8070 * offset.
8071 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
8072 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008073 static void
8074ex_syncbind(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008075 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008076{
8077#ifdef FEAT_SCROLLBIND
8078 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008079 win_T *save_curwin = curwin;
8080 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008081 long topline;
8082 long y;
8083 linenr_T old_linenr = curwin->w_cursor.lnum;
8084
8085 setpcmark();
8086
8087 /*
8088 * determine max topline
8089 */
8090 if (curwin->w_p_scb)
8091 {
8092 topline = curwin->w_topline;
8093 for (wp = firstwin; wp; wp = wp->w_next)
8094 {
8095 if (wp->w_p_scb && wp->w_buffer)
8096 {
8097 y = wp->w_buffer->b_ml.ml_line_count - p_so;
8098 if (topline > y)
8099 topline = y;
8100 }
8101 }
8102 if (topline < 1)
8103 topline = 1;
8104 }
8105 else
8106 {
8107 topline = 1;
8108 }
8109
8110
8111 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008112 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008113 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008114 for (curwin = firstwin; curwin; curwin = curwin->w_next)
8115 {
8116 if (curwin->w_p_scb)
8117 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008118 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008119 y = topline - curwin->w_topline;
8120 if (y > 0)
8121 scrollup(y, TRUE);
8122 else
8123 scrolldown(-y, TRUE);
8124 curwin->w_scbind_pos = topline;
8125 redraw_later(VALID);
8126 cursor_correct();
8127#ifdef FEAT_WINDOWS
8128 curwin->w_redr_status = TRUE;
8129#endif
8130 }
8131 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008132 curwin = save_curwin;
8133 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008134 if (curwin->w_p_scb)
8135 {
8136 did_syncbind = TRUE;
8137 checkpcmark();
8138 if (old_linenr != curwin->w_cursor.lnum)
8139 {
8140 char_u ctrl_o[2];
8141
8142 ctrl_o[0] = Ctrl_O;
8143 ctrl_o[1] = 0;
8144 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
8145 }
8146 }
8147#endif
8148}
8149
8150
8151 static void
8152ex_read(eap)
8153 exarg_T *eap;
8154{
Bram Moolenaardf177f62005-02-22 08:39:57 +00008155 int i;
8156 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
8157 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008158
8159 if (eap->usefilter) /* :r!cmd */
8160 do_bang(1, eap, FALSE, FALSE, TRUE);
8161 else
8162 {
8163 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
8164 return;
8165
8166#ifdef FEAT_BROWSE
8167 if (cmdmod.browse)
8168 {
8169 char_u *browseFile;
8170
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008171 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008172 NULL, NULL, NULL, curbuf);
8173 if (browseFile != NULL)
8174 {
8175 i = readfile(browseFile, NULL,
8176 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8177 vim_free(browseFile);
8178 }
8179 else
8180 i = OK;
8181 }
8182 else
8183#endif
8184 if (*eap->arg == NUL)
8185 {
8186 if (check_fname() == FAIL) /* check for no file name */
8187 return;
8188 i = readfile(curbuf->b_ffname, curbuf->b_fname,
8189 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8190 }
8191 else
8192 {
8193 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
8194 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
8195 i = readfile(eap->arg, NULL,
8196 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8197
8198 }
8199 if (i == FAIL)
8200 {
8201#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
8202 if (!aborting())
8203#endif
8204 EMSG2(_(e_notopen), eap->arg);
8205 }
8206 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008207 {
8208 if (empty && exmode_active)
8209 {
8210 /* Delete the empty line that remains. Historically ex does
8211 * this but vi doesn't. */
8212 if (eap->line2 == 0)
8213 lnum = curbuf->b_ml.ml_line_count;
8214 else
8215 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008216 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008217 {
8218 ml_delete(lnum, FALSE);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008219 if (curwin->w_cursor.lnum > 1
8220 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008221 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00008222 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008223 }
8224 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008225 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008226 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008227 }
8228}
8229
Bram Moolenaarea408852005-06-25 22:49:46 +00008230static char_u *prev_dir = NULL;
8231
8232#if defined(EXITFREE) || defined(PROTO)
8233 void
8234free_cd_dir()
8235{
8236 vim_free(prev_dir);
Bram Moolenaara0174af2008-01-02 20:08:25 +00008237 prev_dir = NULL;
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00008238
8239 vim_free(globaldir);
8240 globaldir = NULL;
Bram Moolenaarea408852005-06-25 22:49:46 +00008241}
8242#endif
8243
Bram Moolenaarf4258302013-06-02 18:20:17 +02008244/*
8245 * Deal with the side effects of changing the current directory.
8246 * When "local" is TRUE then this was after an ":lcd" command.
8247 */
8248 void
8249post_chdir(local)
8250 int local;
8251{
8252 vim_free(curwin->w_localdir);
Bram Moolenaarbd2dc342014-01-10 15:53:13 +01008253 curwin->w_localdir = NULL;
Bram Moolenaarf4258302013-06-02 18:20:17 +02008254 if (local)
8255 {
8256 /* If still in global directory, need to remember current
8257 * directory as global directory. */
8258 if (globaldir == NULL && prev_dir != NULL)
8259 globaldir = vim_strsave(prev_dir);
8260 /* Remember this local directory for the window. */
8261 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8262 curwin->w_localdir = vim_strsave(NameBuff);
8263 }
8264 else
8265 {
8266 /* We are now in the global directory, no need to remember its
8267 * name. */
8268 vim_free(globaldir);
8269 globaldir = NULL;
Bram Moolenaarf4258302013-06-02 18:20:17 +02008270 }
8271
8272 shorten_fnames(TRUE);
8273}
8274
Bram Moolenaarea408852005-06-25 22:49:46 +00008275
Bram Moolenaar071d4272004-06-13 20:20:40 +00008276/*
8277 * ":cd", ":lcd", ":chdir" and ":lchdir".
8278 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00008279 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00008280ex_cd(eap)
8281 exarg_T *eap;
8282{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008283 char_u *new_dir;
8284 char_u *tofree;
8285
8286 new_dir = eap->arg;
8287#if !defined(UNIX) && !defined(VMS)
8288 /* for non-UNIX ":cd" means: print current directory */
8289 if (*new_dir == NUL)
8290 ex_pwd(NULL);
8291 else
8292#endif
8293 {
Bram Moolenaar8e8fe9b2009-03-11 14:37:32 +00008294#ifdef FEAT_AUTOCMD
8295 if (allbuf_locked())
8296 return;
8297#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00008298 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged()
8299 && !eap->forceit)
8300 {
Bram Moolenaar81870892007-11-11 18:17:28 +00008301 EMSG(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00008302 return;
8303 }
8304
Bram Moolenaar071d4272004-06-13 20:20:40 +00008305 /* ":cd -": Change to previous directory */
8306 if (STRCMP(new_dir, "-") == 0)
8307 {
8308 if (prev_dir == NULL)
8309 {
8310 EMSG(_("E186: No previous directory"));
8311 return;
8312 }
8313 new_dir = prev_dir;
8314 }
8315
8316 /* Save current directory for next ":cd -" */
8317 tofree = prev_dir;
8318 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8319 prev_dir = vim_strsave(NameBuff);
8320 else
8321 prev_dir = NULL;
8322
8323#if defined(UNIX) || defined(VMS)
8324 /* for UNIX ":cd" means: go to home directory */
8325 if (*new_dir == NUL)
8326 {
8327 /* use NameBuff for home directory name */
8328# ifdef VMS
8329 char_u *p;
8330
8331 p = mch_getenv((char_u *)"SYS$LOGIN");
8332 if (p == NULL || *p == NUL) /* empty is the same as not set */
8333 NameBuff[0] = NUL;
8334 else
Bram Moolenaarb6356332005-07-18 21:40:44 +00008335 vim_strncpy(NameBuff, p, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008336# else
8337 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
8338# endif
8339 new_dir = NameBuff;
8340 }
8341#endif
8342 if (new_dir == NULL || vim_chdir(new_dir))
8343 EMSG(_(e_failed));
8344 else
8345 {
Bram Moolenaarf4258302013-06-02 18:20:17 +02008346 post_chdir(eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008347
8348 /* Echo the new current directory if the command was typed. */
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00008349 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008350 ex_pwd(eap);
8351 }
8352 vim_free(tofree);
8353 }
8354}
8355
8356/*
8357 * ":pwd".
8358 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008359 static void
8360ex_pwd(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008361 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008362{
8363 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8364 {
8365#ifdef BACKSLASH_IN_FILENAME
8366 slash_adjust(NameBuff);
8367#endif
8368 msg(NameBuff);
8369 }
8370 else
8371 EMSG(_("E187: Unknown"));
8372}
8373
8374/*
8375 * ":=".
8376 */
8377 static void
8378ex_equal(eap)
8379 exarg_T *eap;
8380{
8381 smsg((char_u *)"%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008382 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008383}
8384
8385 static void
8386ex_sleep(eap)
8387 exarg_T *eap;
8388{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008389 int n;
8390 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008391
8392 if (cursor_valid())
8393 {
8394 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
8395 if (n >= 0)
Bram Moolenaar10395d82014-02-05 22:46:52 +01008396 windgoto((int)n, W_WINCOL(curwin) + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008397 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008398
8399 len = eap->line2;
8400 switch (*eap->arg)
8401 {
8402 case 'm': break;
8403 case NUL: len *= 1000L; break;
8404 default: EMSG2(_(e_invarg2), eap->arg); return;
8405 }
8406 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008407}
8408
8409/*
8410 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
8411 */
8412 void
8413do_sleep(msec)
8414 long msec;
8415{
8416 long done;
8417
8418 cursor_on();
8419 out_flush();
8420 for (done = 0; !got_int && done < msec; done += 1000L)
8421 {
8422 ui_delay(msec - done > 1000L ? 1000L : msec - done, TRUE);
8423 ui_breakcheck();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02008424#ifdef FEAT_NETBEANS_INTG
8425 /* Process the netbeans messages that may have been received in the
8426 * call to ui_breakcheck() when the GUI is in use. This may occur when
8427 * running a test case. */
8428 netbeans_parse_messages();
8429#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008430 }
8431}
8432
8433 static void
8434do_exmap(eap, isabbrev)
8435 exarg_T *eap;
8436 int isabbrev;
8437{
8438 int mode;
8439 char_u *cmdp;
8440
8441 cmdp = eap->cmd;
8442 mode = get_map_mode(&cmdp, eap->forceit || isabbrev);
8443
8444 switch (do_map((*cmdp == 'n') ? 2 : (*cmdp == 'u'),
8445 eap->arg, mode, isabbrev))
8446 {
8447 case 1: EMSG(_(e_invarg));
8448 break;
8449 case 2: EMSG(isabbrev ? _(e_noabbr) : _(e_nomap));
8450 break;
8451 }
8452}
8453
8454/*
8455 * ":winsize" command (obsolete).
8456 */
8457 static void
8458ex_winsize(eap)
8459 exarg_T *eap;
8460{
8461 int w, h;
8462 char_u *arg = eap->arg;
8463 char_u *p;
8464
8465 w = getdigits(&arg);
8466 arg = skipwhite(arg);
8467 p = arg;
8468 h = getdigits(&arg);
8469 if (*p != NUL && *arg == NUL)
8470 set_shellsize(w, h, TRUE);
8471 else
8472 EMSG(_("E465: :winsize requires two number arguments"));
8473}
8474
8475#ifdef FEAT_WINDOWS
8476 static void
8477ex_wincmd(eap)
8478 exarg_T *eap;
8479{
8480 int xchar = NUL;
8481 char_u *p;
8482
8483 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
8484 {
8485 /* CTRL-W g and CTRL-W CTRL-G have an extra command character */
8486 if (eap->arg[1] == NUL)
8487 {
8488 EMSG(_(e_invarg));
8489 return;
8490 }
8491 xchar = eap->arg[1];
8492 p = eap->arg + 2;
8493 }
8494 else
8495 p = eap->arg + 1;
8496
8497 eap->nextcmd = check_nextcmd(p);
8498 p = skipwhite(p);
8499 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
8500 EMSG(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02008501 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008502 {
8503 /* Pass flags on for ":vertical wincmd ]". */
8504 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008505 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008506 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
8507 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008508 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008509 }
8510}
8511#endif
8512
Bram Moolenaar843ee412004-06-30 16:16:41 +00008513#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008514/*
8515 * ":winpos".
8516 */
8517 static void
8518ex_winpos(eap)
8519 exarg_T *eap;
8520{
8521 int x, y;
8522 char_u *arg = eap->arg;
8523 char_u *p;
8524
8525 if (*arg == NUL)
8526 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00008527# if defined(FEAT_GUI) || defined(MSWIN)
8528# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008529 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00008530# else
8531 if (mch_get_winpos(&x, &y) != FAIL)
8532# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008533 {
8534 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
8535 msg(IObuff);
8536 }
8537 else
8538# endif
8539 EMSG(_("E188: Obtaining window position not implemented for this platform"));
8540 }
8541 else
8542 {
8543 x = getdigits(&arg);
8544 arg = skipwhite(arg);
8545 p = arg;
8546 y = getdigits(&arg);
8547 if (*p == NUL || *arg != NUL)
8548 {
8549 EMSG(_("E466: :winpos requires two number arguments"));
8550 return;
8551 }
8552# ifdef FEAT_GUI
8553 if (gui.in_use)
8554 gui_mch_set_winpos(x, y);
8555 else if (gui.starting)
8556 {
8557 /* Remember the coordinates for when the window is opened. */
8558 gui_win_x = x;
8559 gui_win_y = y;
8560 }
8561# ifdef HAVE_TGETENT
8562 else
8563# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00008564# else
8565# ifdef MSWIN
8566 mch_set_winpos(x, y);
8567# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008568# endif
8569# ifdef HAVE_TGETENT
8570 if (*T_CWP)
8571 term_set_winpos(x, y);
8572# endif
8573 }
8574}
8575#endif
8576
8577/*
8578 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
8579 */
8580 static void
8581ex_operators(eap)
8582 exarg_T *eap;
8583{
8584 oparg_T oa;
8585
8586 clear_oparg(&oa);
8587 oa.regname = eap->regname;
8588 oa.start.lnum = eap->line1;
8589 oa.end.lnum = eap->line2;
8590 oa.line_count = eap->line2 - eap->line1 + 1;
8591 oa.motion_type = MLINE;
8592#ifdef FEAT_VIRTUALEDIT
8593 virtual_op = FALSE;
8594#endif
8595 if (eap->cmdidx != CMD_yank) /* position cursor for undo */
8596 {
8597 setpcmark();
8598 curwin->w_cursor.lnum = eap->line1;
8599 beginline(BL_SOL | BL_FIX);
8600 }
8601
Bram Moolenaard07c6e12013-11-21 14:21:40 +01008602 if (VIsual_active)
8603 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01008604
Bram Moolenaar071d4272004-06-13 20:20:40 +00008605 switch (eap->cmdidx)
8606 {
8607 case CMD_delete:
8608 oa.op_type = OP_DELETE;
8609 op_delete(&oa);
8610 break;
8611
8612 case CMD_yank:
8613 oa.op_type = OP_YANK;
8614 (void)op_yank(&oa, FALSE, TRUE);
8615 break;
8616
8617 default: /* CMD_rshift or CMD_lshift */
Bram Moolenaar6e707362013-06-14 19:15:58 +02008618 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00008619#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02008620 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
8621#else
8622 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00008623#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02008624 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00008625 oa.op_type = OP_RSHIFT;
8626 else
8627 oa.op_type = OP_LSHIFT;
8628 op_shift(&oa, FALSE, eap->amount);
8629 break;
8630 }
8631#ifdef FEAT_VIRTUALEDIT
8632 virtual_op = MAYBE;
8633#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00008634 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008635}
8636
8637/*
8638 * ":put".
8639 */
8640 static void
8641ex_put(eap)
8642 exarg_T *eap;
8643{
8644 /* ":0put" works like ":1put!". */
8645 if (eap->line2 == 0)
8646 {
8647 eap->line2 = 1;
8648 eap->forceit = TRUE;
8649 }
8650 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008651 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
8652 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008653}
8654
8655/*
8656 * Handle ":copy" and ":move".
8657 */
8658 static void
8659ex_copymove(eap)
8660 exarg_T *eap;
8661{
8662 long n;
8663
8664 n = get_address(&eap->arg, FALSE, FALSE);
8665 if (eap->arg == NULL) /* error detected */
8666 {
8667 eap->nextcmd = NULL;
8668 return;
8669 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008670 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008671
8672 /*
8673 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
8674 */
8675 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
8676 {
8677 EMSG(_(e_invaddr));
8678 return;
8679 }
8680
8681 if (eap->cmdidx == CMD_move)
8682 {
8683 if (do_move(eap->line1, eap->line2, n) == FAIL)
8684 return;
8685 }
8686 else
8687 ex_copy(eap->line1, eap->line2, n);
8688 u_clearline();
8689 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008690 ex_may_print(eap);
8691}
8692
8693/*
8694 * Print the current line if flags were given to the Ex command.
8695 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02008696 void
Bram Moolenaardf177f62005-02-22 08:39:57 +00008697ex_may_print(eap)
8698 exarg_T *eap;
8699{
8700 if (eap->flags != 0)
8701 {
8702 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
8703 (eap->flags & EXFLAG_LIST));
8704 ex_no_reprint = TRUE;
8705 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008706}
8707
8708/*
8709 * ":smagic" and ":snomagic".
8710 */
8711 static void
8712ex_submagic(eap)
8713 exarg_T *eap;
8714{
8715 int magic_save = p_magic;
8716
8717 p_magic = (eap->cmdidx == CMD_smagic);
8718 do_sub(eap);
8719 p_magic = magic_save;
8720}
8721
8722/*
8723 * ":join".
8724 */
8725 static void
8726ex_join(eap)
8727 exarg_T *eap;
8728{
8729 curwin->w_cursor.lnum = eap->line1;
8730 if (eap->line1 == eap->line2)
8731 {
8732 if (eap->addr_count >= 2) /* :2,2join does nothing */
8733 return;
8734 if (eap->line2 == curbuf->b_ml.ml_line_count)
8735 {
8736 beep_flush();
8737 return;
8738 }
8739 ++eap->line2;
8740 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02008741 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008742 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008743 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008744}
8745
8746/*
8747 * ":[addr]@r" or ":[addr]*r": execute register
8748 */
8749 static void
8750ex_at(eap)
8751 exarg_T *eap;
8752{
8753 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00008754 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008755
8756 curwin->w_cursor.lnum = eap->line2;
8757
8758#ifdef USE_ON_FLY_SCROLL
8759 dont_scroll = TRUE; /* disallow scrolling here */
8760#endif
8761
8762 /* get the register name. No name means to use the previous one */
8763 c = *eap->arg;
8764 if (c == NUL || (c == '*' && *eap->cmd == '*'))
8765 c = '@';
Bram Moolenaard333d1e2006-11-07 17:43:47 +00008766 /* Put the register in the typeahead buffer with the "silent" flag. */
8767 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
8768 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008769 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008770 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00008771 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008772 else
8773 {
8774 int save_efr = exec_from_reg;
8775
8776 exec_from_reg = TRUE;
8777
8778 /*
8779 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00008780 * Continue until the stuff buffer is empty and all added characters
8781 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008782 */
Bram Moolenaar60462872009-11-03 11:40:19 +00008783 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008784 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
8785
8786 exec_from_reg = save_efr;
8787 }
8788}
8789
8790/*
8791 * ":!".
8792 */
8793 static void
8794ex_bang(eap)
8795 exarg_T *eap;
8796{
8797 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
8798}
8799
8800/*
8801 * ":undo".
8802 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008803 static void
8804ex_undo(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008805 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008806{
Bram Moolenaard3667a22006-03-16 21:35:52 +00008807 if (eap->addr_count == 1) /* :undo 123 */
Bram Moolenaar730cde92010-06-27 05:18:54 +02008808 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00008809 else
8810 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008811}
8812
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008813#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008814 static void
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008815ex_wundo(eap)
8816 exarg_T *eap;
8817{
8818 char_u hash[UNDO_HASH_SIZE];
8819
8820 u_compute_hash(hash);
8821 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
8822}
8823
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008824 static void
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008825ex_rundo(eap)
8826 exarg_T *eap;
8827{
8828 char_u hash[UNDO_HASH_SIZE];
8829
8830 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02008831 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008832}
8833#endif
8834
Bram Moolenaar071d4272004-06-13 20:20:40 +00008835/*
8836 * ":redo".
8837 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008838 static void
8839ex_redo(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008840 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008841{
8842 u_redo(1);
8843}
8844
8845/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008846 * ":earlier" and ":later".
8847 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008848 static void
8849ex_later(eap)
8850 exarg_T *eap;
8851{
8852 long count = 0;
8853 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008854 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008855 char_u *p = eap->arg;
8856
8857 if (*p == NUL)
8858 count = 1;
8859 else if (isdigit(*p))
8860 {
8861 count = getdigits(&p);
8862 switch (*p)
8863 {
8864 case 's': ++p; sec = TRUE; break;
8865 case 'm': ++p; sec = TRUE; count *= 60; break;
8866 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008867 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
8868 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008869 }
8870 }
8871
8872 if (*p != NUL)
8873 EMSG2(_(e_invarg2), eap->arg);
8874 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02008875 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
8876 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008877}
8878
8879/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008880 * ":redir": start/stop redirection.
8881 */
8882 static void
8883ex_redir(eap)
8884 exarg_T *eap;
8885{
8886 char *mode;
8887 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00008888 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008889
8890 if (STRICMP(eap->arg, "END") == 0)
8891 close_redir();
8892 else
8893 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008894 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008895 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008896 ++arg;
8897 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008898 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008899 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008900 mode = "a";
8901 }
8902 else
8903 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00008904 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008905
8906 close_redir();
8907
8908 /* Expand environment variables and "~/". */
Bram Moolenaarca472992005-01-21 11:46:23 +00008909 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008910 if (fname == NULL)
8911 return;
8912#ifdef FEAT_BROWSE
8913 if (cmdmod.browse)
8914 {
8915 char_u *browseFile;
8916
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008917 browseFile = do_browse(BROWSE_SAVE,
8918 (char_u *)_("Save Redirection"),
8919 fname, NULL, NULL, BROWSE_FILTER_ALL_FILES, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008920 if (browseFile == NULL)
8921 return; /* operation cancelled */
8922 vim_free(fname);
8923 fname = browseFile;
8924 eap->forceit = TRUE; /* since dialog already asked */
8925 }
8926#endif
8927
8928 redir_fd = open_exfile(fname, eap->forceit, mode);
8929 vim_free(fname);
8930 }
8931#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00008932 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008933 {
8934 /* redirect to a register a-z (resp. A-Z for appending) */
8935 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00008936 ++arg;
8937 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008938# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00008939 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00008940 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008941# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00008942 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008943 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008944 redir_reg = *arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008945 if (*arg == '>' && arg[1] == '>') /* append */
Bram Moolenaard9d30582005-05-18 22:10:28 +00008946 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008947 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008948 {
Bram Moolenaarc188b882007-10-19 14:20:54 +00008949 /* Can use both "@a" and "@a>". */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00008950 if (*arg == '>')
8951 arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008952 /* Make register empty when not using @A-@Z and the
8953 * command is valid. */
8954 if (*arg == NUL && !isupper(redir_reg))
8955 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008956 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008957 }
8958 if (*arg != NUL)
8959 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00008960 redir_reg = 0;
Bram Moolenaard9d30582005-05-18 22:10:28 +00008961 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008962 }
8963 }
8964 else if (*arg == '=' && arg[1] == '>')
8965 {
8966 int append;
8967
8968 /* redirect to a variable */
8969 close_redir();
8970 arg += 2;
8971
8972 if (*arg == '>')
8973 {
8974 ++arg;
8975 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008976 }
8977 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008978 append = FALSE;
8979
8980 if (var_redir_start(skipwhite(arg), append) == OK)
8981 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008982 }
8983#endif
8984
8985 /* TODO: redirect to a buffer */
8986
Bram Moolenaar071d4272004-06-13 20:20:40 +00008987 else
Bram Moolenaarca472992005-01-21 11:46:23 +00008988 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008989 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00008990
8991 /* Make sure redirection is not off. Can happen for cmdline completion
8992 * that indirectly invokes a command to catch its output. */
8993 if (redir_fd != NULL
8994#ifdef FEAT_EVAL
8995 || redir_reg || redir_vname
8996#endif
8997 )
8998 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008999}
9000
9001/*
9002 * ":redraw": force redraw
9003 */
9004 static void
9005ex_redraw(eap)
9006 exarg_T *eap;
9007{
9008 int r = RedrawingDisabled;
9009 int p = p_lz;
9010
9011 RedrawingDisabled = 0;
9012 p_lz = FALSE;
9013 update_topline();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009014 update_screen(eap->forceit ? CLEAR : VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009015#ifdef FEAT_TITLE
9016 if (need_maketitle)
9017 maketitle();
9018#endif
9019 RedrawingDisabled = r;
9020 p_lz = p;
9021
9022 /* Reset msg_didout, so that a message that's there is overwritten. */
9023 msg_didout = FALSE;
9024 msg_col = 0;
9025
Bram Moolenaar56667a52013-07-17 11:54:28 +02009026 /* No need to wait after an intentional redraw. */
9027 need_wait_return = FALSE;
9028
Bram Moolenaar071d4272004-06-13 20:20:40 +00009029 out_flush();
9030}
9031
9032/*
9033 * ":redrawstatus": force redraw of status line(s)
9034 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009035 static void
9036ex_redrawstatus(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009037 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009038{
9039#if defined(FEAT_WINDOWS)
9040 int r = RedrawingDisabled;
9041 int p = p_lz;
9042
9043 RedrawingDisabled = 0;
9044 p_lz = FALSE;
9045 if (eap->forceit)
9046 status_redraw_all();
9047 else
9048 status_redraw_curbuf();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009049 update_screen(VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009050 RedrawingDisabled = r;
9051 p_lz = p;
9052 out_flush();
9053#endif
9054}
9055
9056 static void
9057close_redir()
9058{
9059 if (redir_fd != NULL)
9060 {
9061 fclose(redir_fd);
9062 redir_fd = NULL;
9063 }
9064#ifdef FEAT_EVAL
9065 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009066 if (redir_vname)
9067 {
9068 var_redir_stop();
9069 redir_vname = 0;
9070 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009071#endif
9072}
9073
9074#if defined(FEAT_SESSION) && defined(USE_CRNL)
9075# define MKSESSION_NL
9076static int mksession_nl = FALSE; /* use NL only in put_eol() */
9077#endif
9078
9079/*
9080 * ":mkexrc", ":mkvimrc", ":mkview" and ":mksession".
9081 */
9082 static void
9083ex_mkrc(eap)
9084 exarg_T *eap;
9085{
9086 FILE *fd;
9087 int failed = FALSE;
9088 char_u *fname;
9089#ifdef FEAT_BROWSE
9090 char_u *browseFile = NULL;
9091#endif
9092#ifdef FEAT_SESSION
9093 int view_session = FALSE;
9094 int using_vdir = FALSE; /* using 'viewdir'? */
9095 char_u *viewFile = NULL;
9096 unsigned *flagp;
9097#endif
9098
9099 if (eap->cmdidx == CMD_mksession || eap->cmdidx == CMD_mkview)
9100 {
9101#ifdef FEAT_SESSION
9102 view_session = TRUE;
9103#else
9104 ex_ni(eap);
9105 return;
9106#endif
9107 }
9108
9109#ifdef FEAT_SESSION
Bram Moolenaar8d594672009-07-01 18:18:57 +00009110 /* Use the short file name until ":lcd" is used. We also don't use the
9111 * short file name when 'acd' is set, that is checked later. */
Bram Moolenaareeefcc72007-05-01 21:21:21 +00009112 did_lcd = FALSE;
9113
Bram Moolenaar071d4272004-06-13 20:20:40 +00009114 /* ":mkview" or ":mkview 9": generate file name with 'viewdir' */
9115 if (eap->cmdidx == CMD_mkview
9116 && (*eap->arg == NUL
9117 || (vim_isdigit(*eap->arg) && eap->arg[1] == NUL)))
9118 {
9119 eap->forceit = TRUE;
9120 fname = get_view_file(*eap->arg);
9121 if (fname == NULL)
9122 return;
9123 viewFile = fname;
9124 using_vdir = TRUE;
9125 }
9126 else
9127#endif
9128 if (*eap->arg != NUL)
9129 fname = eap->arg;
9130 else if (eap->cmdidx == CMD_mkvimrc)
9131 fname = (char_u *)VIMRC_FILE;
9132#ifdef FEAT_SESSION
9133 else if (eap->cmdidx == CMD_mksession)
9134 fname = (char_u *)SESSION_FILE;
9135#endif
9136 else
9137 fname = (char_u *)EXRC_FILE;
9138
9139#ifdef FEAT_BROWSE
9140 if (cmdmod.browse)
9141 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00009142 browseFile = do_browse(BROWSE_SAVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009143# ifdef FEAT_SESSION
9144 eap->cmdidx == CMD_mkview ? (char_u *)_("Save View") :
9145 eap->cmdidx == CMD_mksession ? (char_u *)_("Save Session") :
9146# endif
9147 (char_u *)_("Save Setup"),
9148 fname, (char_u *)"vim", NULL, BROWSE_FILTER_MACROS, NULL);
9149 if (browseFile == NULL)
9150 goto theend;
9151 fname = browseFile;
9152 eap->forceit = TRUE; /* since dialog already asked */
9153 }
9154#endif
9155
9156#if defined(FEAT_SESSION) && defined(vim_mkdir)
9157 /* When using 'viewdir' may have to create the directory. */
9158 if (using_vdir && !mch_isdir(p_vdir))
Bram Moolenaardf177f62005-02-22 08:39:57 +00009159 vim_mkdir_emsg(p_vdir, 0755);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009160#endif
9161
9162 fd = open_exfile(fname, eap->forceit, WRITEBIN);
9163 if (fd != NULL)
9164 {
9165#ifdef FEAT_SESSION
9166 if (eap->cmdidx == CMD_mkview)
9167 flagp = &vop_flags;
9168 else
9169 flagp = &ssop_flags;
9170#endif
9171
9172#ifdef MKSESSION_NL
9173 /* "unix" in 'sessionoptions': use NL line separator */
9174 if (view_session && (*flagp & SSOP_UNIX))
9175 mksession_nl = TRUE;
9176#endif
9177
9178 /* Write the version command for :mkvimrc */
9179 if (eap->cmdidx == CMD_mkvimrc)
9180 (void)put_line(fd, "version 6.0");
9181
9182#ifdef FEAT_SESSION
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009183 if (eap->cmdidx == CMD_mksession)
9184 {
9185 if (put_line(fd, "let SessionLoad = 1") == FAIL)
9186 failed = TRUE;
9187 }
9188
Bram Moolenaar071d4272004-06-13 20:20:40 +00009189 if (eap->cmdidx != CMD_mkview)
9190#endif
9191 {
9192 /* Write setting 'compatible' first, because it has side effects.
9193 * For that same reason only do it when needed. */
9194 if (p_cp)
9195 (void)put_line(fd, "if !&cp | set cp | endif");
9196 else
9197 (void)put_line(fd, "if &cp | set nocp | endif");
9198 }
9199
9200#ifdef FEAT_SESSION
9201 if (!view_session
9202 || (eap->cmdidx == CMD_mksession
9203 && (*flagp & SSOP_OPTIONS)))
9204#endif
9205 failed |= (makemap(fd, NULL) == FAIL
9206 || makeset(fd, OPT_GLOBAL, FALSE) == FAIL);
9207
9208#ifdef FEAT_SESSION
9209 if (!failed && view_session)
9210 {
9211 if (put_line(fd, "let s:so_save = &so | let s:siso_save = &siso | set so=0 siso=0") == FAIL)
9212 failed = TRUE;
9213 if (eap->cmdidx == CMD_mksession)
9214 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02009215 char_u *dirnow; /* current directory */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009216
Bram Moolenaard9462e32011-04-11 21:35:11 +02009217 dirnow = alloc(MAXPATHL);
9218 if (dirnow == NULL)
9219 failed = TRUE;
9220 else
9221 {
9222 /*
9223 * Change to session file's dir.
9224 */
9225 if (mch_dirname(dirnow, MAXPATHL) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00009226 || mch_chdir((char *)dirnow) != 0)
Bram Moolenaard9462e32011-04-11 21:35:11 +02009227 *dirnow = NUL;
9228 if (*dirnow != NUL && (ssop_flags & SSOP_SESDIR))
9229 {
9230 if (vim_chdirfile(fname) == OK)
9231 shorten_fnames(TRUE);
9232 }
9233 else if (*dirnow != NUL
9234 && (ssop_flags & SSOP_CURDIR) && globaldir != NULL)
9235 {
9236 if (mch_chdir((char *)globaldir) == 0)
9237 shorten_fnames(TRUE);
9238 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009239
Bram Moolenaard9462e32011-04-11 21:35:11 +02009240 failed |= (makeopens(fd, dirnow) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009241
Bram Moolenaard9462e32011-04-11 21:35:11 +02009242 /* restore original dir */
9243 if (*dirnow != NUL && ((ssop_flags & SSOP_SESDIR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009244 || ((ssop_flags & SSOP_CURDIR) && globaldir != NULL)))
Bram Moolenaard9462e32011-04-11 21:35:11 +02009245 {
9246 if (mch_chdir((char *)dirnow) != 0)
9247 EMSG(_(e_prev_dir));
9248 shorten_fnames(TRUE);
9249 }
9250 vim_free(dirnow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009251 }
9252 }
9253 else
9254 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +00009255 failed |= (put_view(fd, curwin, !using_vdir, flagp,
9256 -1) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009257 }
9258 if (put_line(fd, "let &so = s:so_save | let &siso = s:siso_save")
9259 == FAIL)
9260 failed = TRUE;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009261 if (put_line(fd, "doautoall SessionLoadPost") == FAIL)
9262 failed = TRUE;
Bram Moolenaar4770d092006-01-12 23:22:24 +00009263 if (eap->cmdidx == CMD_mksession)
9264 {
9265 if (put_line(fd, "unlet SessionLoad") == FAIL)
9266 failed = TRUE;
9267 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009268 }
9269#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009270 if (put_line(fd, "\" vim: set ft=vim :") == FAIL)
9271 failed = TRUE;
9272
Bram Moolenaar071d4272004-06-13 20:20:40 +00009273 failed |= fclose(fd);
9274
9275 if (failed)
9276 EMSG(_(e_write));
9277#if defined(FEAT_EVAL) && defined(FEAT_SESSION)
9278 else if (eap->cmdidx == CMD_mksession)
9279 {
9280 /* successful session write - set this_session var */
Bram Moolenaard9462e32011-04-11 21:35:11 +02009281 char_u *tbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009282
Bram Moolenaard9462e32011-04-11 21:35:11 +02009283 tbuf = alloc(MAXPATHL);
9284 if (tbuf != NULL)
9285 {
9286 if (vim_FullName(fname, tbuf, MAXPATHL, FALSE) == OK)
9287 set_vim_var_string(VV_THIS_SESSION, tbuf, -1);
9288 vim_free(tbuf);
9289 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009290 }
9291#endif
9292#ifdef MKSESSION_NL
9293 mksession_nl = FALSE;
9294#endif
9295 }
9296
9297#ifdef FEAT_BROWSE
9298theend:
9299 vim_free(browseFile);
9300#endif
9301#ifdef FEAT_SESSION
9302 vim_free(viewFile);
9303#endif
9304}
9305
Bram Moolenaardf177f62005-02-22 08:39:57 +00009306#if ((defined(FEAT_SESSION) || defined(FEAT_EVAL)) && defined(vim_mkdir)) \
9307 || defined(PROTO)
9308 int
9309vim_mkdir_emsg(name, prot)
9310 char_u *name;
Bram Moolenaar78a15312009-05-15 19:33:18 +00009311 int prot UNUSED;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009312{
9313 if (vim_mkdir(name, prot) != 0)
9314 {
9315 EMSG2(_("E739: Cannot create directory: %s"), name);
9316 return FAIL;
9317 }
9318 return OK;
9319}
9320#endif
9321
Bram Moolenaar071d4272004-06-13 20:20:40 +00009322/*
9323 * Open a file for writing for an Ex command, with some checks.
9324 * Return file descriptor, or NULL on failure.
9325 */
9326 FILE *
9327open_exfile(fname, forceit, mode)
9328 char_u *fname;
9329 int forceit;
9330 char *mode; /* "w" for create new file or "a" for append */
9331{
9332 FILE *fd;
9333
9334#ifdef UNIX
9335 /* with Unix it is possible to open a directory */
9336 if (mch_isdir(fname))
9337 {
9338 EMSG2(_(e_isadir2), fname);
9339 return NULL;
9340 }
9341#endif
9342 if (!forceit && *mode != 'a' && vim_fexists(fname))
9343 {
9344 EMSG2(_("E189: \"%s\" exists (add ! to override)"), fname);
9345 return NULL;
9346 }
9347
9348 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
9349 EMSG2(_("E190: Cannot open \"%s\" for writing"), fname);
9350
9351 return fd;
9352}
9353
9354/*
9355 * ":mark" and ":k".
9356 */
9357 static void
9358ex_mark(eap)
9359 exarg_T *eap;
9360{
9361 pos_T pos;
9362
9363 if (*eap->arg == NUL) /* No argument? */
9364 EMSG(_(e_argreq));
9365 else if (eap->arg[1] != NUL) /* more than one character? */
9366 EMSG(_(e_trailing));
9367 else
9368 {
9369 pos = curwin->w_cursor; /* save curwin->w_cursor */
9370 curwin->w_cursor.lnum = eap->line2;
9371 beginline(BL_WHITE | BL_FIX);
9372 if (setmark(*eap->arg) == FAIL) /* set mark */
9373 EMSG(_("E191: Argument must be a letter or forward/backward quote"));
9374 curwin->w_cursor = pos; /* restore curwin->w_cursor */
9375 }
9376}
9377
9378/*
9379 * Update w_topline, w_leftcol and the cursor position.
9380 */
9381 void
9382update_topline_cursor()
9383{
9384 check_cursor(); /* put cursor on valid line */
9385 update_topline();
9386 if (!curwin->w_p_wrap)
9387 validate_cursor();
9388 update_curswant();
9389}
9390
9391#ifdef FEAT_EX_EXTRA
9392/*
9393 * ":normal[!] {commands}": Execute normal mode commands.
9394 */
9395 static void
9396ex_normal(eap)
9397 exarg_T *eap;
9398{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009399 int save_msg_scroll = msg_scroll;
9400 int save_restart_edit = restart_edit;
9401 int save_msg_didout = msg_didout;
9402 int save_State = State;
9403 tasave_T tabuf;
9404 int save_insertmode = p_im;
9405 int save_finish_op = finish_op;
Bram Moolenaar38084112008-07-26 14:05:07 +00009406 int save_opcount = opcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009407#ifdef FEAT_MBYTE
9408 char_u *arg = NULL;
9409 int l;
9410 char_u *p;
9411#endif
9412
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00009413 if (ex_normal_lock > 0)
9414 {
9415 EMSG(_(e_secure));
9416 return;
9417 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009418 if (ex_normal_busy >= p_mmd)
9419 {
9420 EMSG(_("E192: Recursive use of :normal too deep"));
9421 return;
9422 }
9423 ++ex_normal_busy;
9424
9425 msg_scroll = FALSE; /* no msg scrolling in Normal mode */
9426 restart_edit = 0; /* don't go to Insert mode */
9427 p_im = FALSE; /* don't use 'insertmode' */
9428
9429#ifdef FEAT_MBYTE
9430 /*
9431 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
9432 * this for the K_SPECIAL leading byte, otherwise special keys will not
9433 * work.
9434 */
9435 if (has_mbyte)
9436 {
9437 int len = 0;
9438
9439 /* Count the number of characters to be escaped. */
9440 for (p = eap->arg; *p != NUL; ++p)
9441 {
9442# ifdef FEAT_GUI
9443 if (*p == CSI) /* leadbyte CSI */
9444 len += 2;
9445# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009446 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009447 if (*++p == K_SPECIAL /* trailbyte K_SPECIAL or CSI */
9448# ifdef FEAT_GUI
9449 || *p == CSI
9450# endif
9451 )
9452 len += 2;
9453 }
9454 if (len > 0)
9455 {
9456 arg = alloc((unsigned)(STRLEN(eap->arg) + len + 1));
9457 if (arg != NULL)
9458 {
9459 len = 0;
9460 for (p = eap->arg; *p != NUL; ++p)
9461 {
9462 arg[len++] = *p;
9463# ifdef FEAT_GUI
9464 if (*p == CSI)
9465 {
9466 arg[len++] = KS_EXTRA;
9467 arg[len++] = (int)KE_CSI;
9468 }
9469# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009470 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009471 {
9472 arg[len++] = *++p;
9473 if (*p == K_SPECIAL)
9474 {
9475 arg[len++] = KS_SPECIAL;
9476 arg[len++] = KE_FILLER;
9477 }
9478# ifdef FEAT_GUI
9479 else if (*p == CSI)
9480 {
9481 arg[len++] = KS_EXTRA;
9482 arg[len++] = (int)KE_CSI;
9483 }
9484# endif
9485 }
9486 arg[len] = NUL;
9487 }
9488 }
9489 }
9490 }
9491#endif
9492
9493 /*
9494 * Save the current typeahead. This is required to allow using ":normal"
9495 * from an event handler and makes sure we don't hang when the argument
9496 * ends with half a command.
9497 */
9498 save_typeahead(&tabuf);
9499 if (tabuf.typebuf_valid)
9500 {
9501 /*
9502 * Repeat the :normal command for each line in the range. When no
9503 * range given, execute it just once, without positioning the cursor
9504 * first.
9505 */
9506 do
9507 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009508 if (eap->addr_count != 0)
9509 {
9510 curwin->w_cursor.lnum = eap->line1++;
9511 curwin->w_cursor.col = 0;
9512 }
9513
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009514 exec_normal_cmd(
Bram Moolenaar071d4272004-06-13 20:20:40 +00009515#ifdef FEAT_MBYTE
9516 arg != NULL ? arg :
9517#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009518 eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009519 }
9520 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
9521 }
9522
9523 /* Might not return to the main loop when in an event handler. */
9524 update_topline_cursor();
9525
9526 /* Restore the previous typeahead. */
9527 restore_typeahead(&tabuf);
9528
9529 --ex_normal_busy;
9530 msg_scroll = save_msg_scroll;
9531 restart_edit = save_restart_edit;
9532 p_im = save_insertmode;
9533 finish_op = save_finish_op;
Bram Moolenaar38084112008-07-26 14:05:07 +00009534 opcount = save_opcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009535 msg_didout |= save_msg_didout; /* don't reset msg_didout now */
9536
9537 /* Restore the state (needed when called from a function executed for
9538 * 'indentexpr'). */
9539 State = save_State;
9540#ifdef FEAT_MBYTE
9541 vim_free(arg);
9542#endif
9543}
9544
9545/*
Bram Moolenaar64969662005-12-14 21:59:55 +00009546 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009547 */
9548 static void
9549ex_startinsert(eap)
9550 exarg_T *eap;
9551{
Bram Moolenaarfd371682005-01-14 21:42:54 +00009552 if (eap->forceit)
9553 {
9554 coladvance((colnr_T)MAXCOL);
9555 curwin->w_curswant = MAXCOL;
9556 curwin->w_set_curswant = FALSE;
9557 }
9558
Bram Moolenaara40c5002005-01-09 21:16:21 +00009559 /* Ignore the command when already in Insert mode. Inserting an
9560 * expression register that invokes a function can do this. */
9561 if (State & INSERT)
9562 return;
9563
Bram Moolenaar64969662005-12-14 21:59:55 +00009564 if (eap->cmdidx == CMD_startinsert)
9565 restart_edit = 'a';
9566 else if (eap->cmdidx == CMD_startreplace)
9567 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00009568 else
Bram Moolenaar64969662005-12-14 21:59:55 +00009569 restart_edit = 'V';
9570
9571 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009572 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009573 if (eap->cmdidx == CMD_startinsert)
9574 restart_edit = 'i';
Bram Moolenaar071d4272004-06-13 20:20:40 +00009575 curwin->w_curswant = 0; /* avoid MAXCOL */
9576 }
9577}
9578
9579/*
9580 * ":stopinsert"
9581 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009582 static void
9583ex_stopinsert(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009584 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009585{
9586 restart_edit = 0;
9587 stop_insert_mode = TRUE;
9588}
9589#endif
9590
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009591#if defined(FEAT_EX_EXTRA) || defined(FEAT_MENU) || defined(PROTO)
9592/*
9593 * Execute normal mode command "cmd".
9594 * "remap" can be REMAP_NONE or REMAP_YES.
9595 */
9596 void
9597exec_normal_cmd(cmd, remap, silent)
9598 char_u *cmd;
9599 int remap;
9600 int silent;
9601{
9602 oparg_T oa;
9603
9604 /*
9605 * Stuff the argument into the typeahead buffer.
9606 * Execute normal_cmd() until there is no typeahead left.
9607 */
9608 clear_oparg(&oa);
9609 finish_op = FALSE;
9610 ins_typebuf(cmd, remap, 0, TRUE, silent);
9611 while ((!stuff_empty() || (!typebuf_typed() && typebuf.tb_len > 0))
9612 && !got_int)
9613 {
9614 update_topline_cursor();
Bram Moolenaar48ac02c2011-01-17 19:50:06 +01009615 normal_cmd(&oa, TRUE); /* execute a Normal mode cmd */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009616 }
9617}
9618#endif
9619
Bram Moolenaar071d4272004-06-13 20:20:40 +00009620#ifdef FEAT_FIND_ID
9621 static void
9622ex_checkpath(eap)
9623 exarg_T *eap;
9624{
9625 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
9626 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
9627 (linenr_T)1, (linenr_T)MAXLNUM);
9628}
9629
9630#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
9631/*
9632 * ":psearch"
9633 */
9634 static void
9635ex_psearch(eap)
9636 exarg_T *eap;
9637{
9638 g_do_tagpreview = p_pvh;
9639 ex_findpat(eap);
9640 g_do_tagpreview = 0;
9641}
9642#endif
9643
9644 static void
9645ex_findpat(eap)
9646 exarg_T *eap;
9647{
9648 int whole = TRUE;
9649 long n;
9650 char_u *p;
9651 int action;
9652
9653 switch (cmdnames[eap->cmdidx].cmd_name[2])
9654 {
9655 case 'e': /* ":psearch", ":isearch" and ":dsearch" */
9656 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
9657 action = ACTION_GOTO;
9658 else
9659 action = ACTION_SHOW;
9660 break;
9661 case 'i': /* ":ilist" and ":dlist" */
9662 action = ACTION_SHOW_ALL;
9663 break;
9664 case 'u': /* ":ijump" and ":djump" */
9665 action = ACTION_GOTO;
9666 break;
9667 default: /* ":isplit" and ":dsplit" */
9668 action = ACTION_SPLIT;
9669 break;
9670 }
9671
9672 n = 1;
9673 if (vim_isdigit(*eap->arg)) /* get count */
9674 {
9675 n = getdigits(&eap->arg);
9676 eap->arg = skipwhite(eap->arg);
9677 }
9678 if (*eap->arg == '/') /* Match regexp, not just whole words */
9679 {
9680 whole = FALSE;
9681 ++eap->arg;
9682 p = skip_regexp(eap->arg, '/', p_magic, NULL);
9683 if (*p)
9684 {
9685 *p++ = NUL;
9686 p = skipwhite(p);
9687
9688 /* Check for trailing illegal characters */
9689 if (!ends_excmd(*p))
9690 eap->errmsg = e_trailing;
9691 else
9692 eap->nextcmd = check_nextcmd(p);
9693 }
9694 }
9695 if (!eap->skip)
9696 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
9697 whole, !eap->forceit,
9698 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
9699 n, action, eap->line1, eap->line2);
9700}
9701#endif
9702
9703#ifdef FEAT_WINDOWS
9704
9705# ifdef FEAT_QUICKFIX
9706/*
9707 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
9708 */
9709 static void
9710ex_ptag(eap)
9711 exarg_T *eap;
9712{
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01009713 g_do_tagpreview = p_pvh; /* will be reset to 0 in ex_tag_cmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009714 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9715}
9716
9717/*
9718 * ":pedit"
9719 */
9720 static void
9721ex_pedit(eap)
9722 exarg_T *eap;
9723{
9724 win_T *curwin_save = curwin;
9725
9726 g_do_tagpreview = p_pvh;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00009727 prepare_tagpreview(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009728 keep_help_flag = curwin_save->w_buffer->b_help;
9729 do_exedit(eap, NULL);
9730 keep_help_flag = FALSE;
9731 if (curwin != curwin_save && win_valid(curwin_save))
9732 {
9733 /* Return cursor to where we were */
9734 validate_cursor();
9735 redraw_later(VALID);
9736 win_enter(curwin_save, TRUE);
9737 }
9738 g_do_tagpreview = 0;
9739}
9740# endif
9741
9742/*
9743 * ":stag", ":stselect" and ":stjump".
9744 */
9745 static void
9746ex_stag(eap)
9747 exarg_T *eap;
9748{
9749 postponed_split = -1;
9750 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009751 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009752 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9753 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009754 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009755}
9756#endif
9757
9758/*
9759 * ":tag", ":tselect", ":tjump", ":tnext", etc.
9760 */
9761 static void
9762ex_tag(eap)
9763 exarg_T *eap;
9764{
9765 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
9766}
9767
9768 static void
9769ex_tag_cmd(eap, name)
9770 exarg_T *eap;
9771 char_u *name;
9772{
9773 int cmd;
9774
9775 switch (name[1])
9776 {
9777 case 'j': cmd = DT_JUMP; /* ":tjump" */
9778 break;
9779 case 's': cmd = DT_SELECT; /* ":tselect" */
9780 break;
9781 case 'p': cmd = DT_PREV; /* ":tprevious" */
9782 break;
9783 case 'N': cmd = DT_PREV; /* ":tNext" */
9784 break;
9785 case 'n': cmd = DT_NEXT; /* ":tnext" */
9786 break;
9787 case 'o': cmd = DT_POP; /* ":pop" */
9788 break;
9789 case 'f': /* ":tfirst" */
9790 case 'r': cmd = DT_FIRST; /* ":trewind" */
9791 break;
9792 case 'l': cmd = DT_LAST; /* ":tlast" */
9793 break;
9794 default: /* ":tag" */
9795#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +00009796 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009797 {
9798 do_cstag(eap);
9799 return;
9800 }
9801#endif
9802 cmd = DT_TAG;
9803 break;
9804 }
9805
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00009806 if (name[0] == 'l')
9807 {
9808#ifndef FEAT_QUICKFIX
9809 ex_ni(eap);
9810 return;
9811#else
9812 cmd = DT_LTAG;
9813#endif
9814 }
9815
Bram Moolenaar071d4272004-06-13 20:20:40 +00009816 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
9817 eap->forceit, TRUE);
9818}
9819
9820/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009821 * Check "str" for starting with a special cmdline variable.
9822 * If found return one of the SPEC_ values and set "*usedlen" to the length of
9823 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
9824 */
9825 int
9826find_cmdline_var(src, usedlen)
9827 char_u *src;
9828 int *usedlen;
9829{
9830 int len;
9831 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +00009832 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009833 "%",
9834#define SPEC_PERC 0
9835 "#",
9836#define SPEC_HASH 1
9837 "<cword>", /* cursor word */
9838#define SPEC_CWORD 2
9839 "<cWORD>", /* cursor WORD */
9840#define SPEC_CCWORD 3
9841 "<cfile>", /* cursor path name */
9842#define SPEC_CFILE 4
9843 "<sfile>", /* ":so" file name */
9844#define SPEC_SFILE 5
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009845 "<slnum>", /* ":so" file line number */
9846#define SPEC_SLNUM 6
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009847#ifdef FEAT_AUTOCMD
9848 "<afile>", /* autocommand file name */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009849# define SPEC_AFILE 7
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009850 "<abuf>", /* autocommand buffer number */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009851# define SPEC_ABUF 8
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009852 "<amatch>", /* autocommand match name */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009853# define SPEC_AMATCH 9
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009854#endif
9855#ifdef FEAT_CLIENTSERVER
9856 "<client>"
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009857# ifdef FEAT_AUTOCMD
9858# define SPEC_CLIENT 10
9859# else
9860# define SPEC_CLIENT 7
9861# endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009862#endif
9863 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009864
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009865 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009866 {
9867 len = (int)STRLEN(spec_str[i]);
9868 if (STRNCMP(src, spec_str[i], len) == 0)
9869 {
9870 *usedlen = len;
9871 return i;
9872 }
9873 }
9874 return -1;
9875}
9876
9877/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009878 * Evaluate cmdline variables.
9879 *
9880 * change '%' to curbuf->b_ffname
9881 * '#' to curwin->w_altfile
9882 * '<cword>' to word under the cursor
9883 * '<cWORD>' to WORD under the cursor
9884 * '<cfile>' to path name under the cursor
9885 * '<sfile>' to sourced file name
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009886 * '<slnum>' to sourced file line number
Bram Moolenaar071d4272004-06-13 20:20:40 +00009887 * '<afile>' to file name for autocommand
9888 * '<abuf>' to buffer number for autocommand
9889 * '<amatch>' to matching name for autocommand
9890 *
9891 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
9892 * "" for error without a message) and NULL is returned.
9893 * Returns an allocated string if a valid match was found.
9894 * Returns NULL if no match was found. "usedlen" then still contains the
9895 * number of characters to skip.
9896 */
9897 char_u *
Bram Moolenaar63b92542007-03-27 14:57:09 +00009898eval_vars(src, srcstart, usedlen, lnump, errormsg, escaped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009899 char_u *src; /* pointer into commandline */
Bram Moolenaar63b92542007-03-27 14:57:09 +00009900 char_u *srcstart; /* beginning of valid memory for src */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009901 int *usedlen; /* characters after src that are used */
9902 linenr_T *lnump; /* line number for :e command, or NULL */
9903 char_u **errormsg; /* pointer to error message */
Bram Moolenaar63b92542007-03-27 14:57:09 +00009904 int *escaped; /* return value has escaped white space (can
9905 * be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009906{
9907 int i;
9908 char_u *s;
9909 char_u *result;
9910 char_u *resultbuf = NULL;
9911 int resultlen;
9912 buf_T *buf;
9913 int valid = VALID_HEAD + VALID_PATH; /* assume valid result */
9914 int spec_idx;
9915#ifdef FEAT_MODIFY_FNAME
9916 int skip_mod = FALSE;
9917#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009918 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009919
9920 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009921 if (escaped != NULL)
9922 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009923
9924 /*
9925 * Check if there is something to do.
9926 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009927 spec_idx = find_cmdline_var(src, usedlen);
9928 if (spec_idx < 0) /* no match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009929 {
9930 *usedlen = 1;
9931 return NULL;
9932 }
9933
9934 /*
9935 * Skip when preceded with a backslash "\%" and "\#".
9936 * Note: In "\\%" the % is also not recognized!
9937 */
9938 if (src > srcstart && src[-1] == '\\')
9939 {
9940 *usedlen = 0;
Bram Moolenaara7241f52008-06-24 20:39:31 +00009941 STRMOVE(src - 1, src); /* remove backslash */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009942 return NULL;
9943 }
9944
9945 /*
9946 * word or WORD under cursor
9947 */
9948 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD)
9949 {
9950 resultlen = find_ident_under_cursor(&result, spec_idx == SPEC_CWORD ?
9951 (FIND_IDENT|FIND_STRING) : FIND_STRING);
9952 if (resultlen == 0)
9953 {
9954 *errormsg = (char_u *)"";
9955 return NULL;
9956 }
9957 }
9958
9959 /*
9960 * '#': Alternate file name
9961 * '%': Current file name
9962 * File name under the cursor
9963 * File name for autocommand
9964 * and following modifiers
9965 */
9966 else
9967 {
9968 switch (spec_idx)
9969 {
9970 case SPEC_PERC: /* '%': current file */
9971 if (curbuf->b_fname == NULL)
9972 {
9973 result = (char_u *)"";
9974 valid = 0; /* Must have ":p:h" to be valid */
9975 }
9976 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009977 result = curbuf->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009978 break;
9979
9980 case SPEC_HASH: /* '#' or "#99": alternate file */
9981 if (src[1] == '#') /* "##": the argument list */
9982 {
9983 result = arg_all();
9984 resultbuf = result;
9985 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009986 if (escaped != NULL)
9987 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009988#ifdef FEAT_MODIFY_FNAME
9989 skip_mod = TRUE;
9990#endif
9991 break;
9992 }
9993 s = src + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00009994 if (*s == '<') /* "#<99" uses v:oldfiles */
9995 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009996 i = (int)getdigits(&s);
9997 *usedlen = (int)(s - src); /* length of what we expand */
9998
Bram Moolenaard812df62008-11-09 12:46:09 +00009999 if (src[1] == '<')
Bram Moolenaar071d4272004-06-13 20:20:40 +000010000 {
Bram Moolenaard812df62008-11-09 12:46:09 +000010001 if (*usedlen < 2)
10002 {
10003 /* Should we give an error message for #<text? */
10004 *usedlen = 1;
10005 return NULL;
10006 }
10007#ifdef FEAT_EVAL
10008 result = list_find_str(get_vim_var_list(VV_OLDFILES),
10009 (long)i);
10010 if (result == NULL)
10011 {
10012 *errormsg = (char_u *)"";
10013 return NULL;
10014 }
10015#else
10016 *errormsg = (char_u *)_("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +000010017 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +000010018#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010019 }
10020 else
Bram Moolenaard812df62008-11-09 12:46:09 +000010021 {
10022 buf = buflist_findnr(i);
10023 if (buf == NULL)
10024 {
10025 *errormsg = (char_u *)_("E194: No alternate file name to substitute for '#'");
10026 return NULL;
10027 }
10028 if (lnump != NULL)
10029 *lnump = ECMD_LAST;
10030 if (buf->b_fname == NULL)
10031 {
10032 result = (char_u *)"";
10033 valid = 0; /* Must have ":p:h" to be valid */
10034 }
10035 else
10036 result = buf->b_fname;
10037 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010038 break;
10039
10040#ifdef FEAT_SEARCHPATH
10041 case SPEC_CFILE: /* file name under cursor */
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010042 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010043 if (result == NULL)
10044 {
10045 *errormsg = (char_u *)"";
10046 return NULL;
10047 }
10048 resultbuf = result; /* remember allocated string */
10049 break;
10050#endif
10051
10052#ifdef FEAT_AUTOCMD
10053 case SPEC_AFILE: /* file name for autocommand */
10054 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +000010055 if (result != NULL && !autocmd_fname_full)
10056 {
10057 /* Still need to turn the fname into a full path. It is
10058 * postponed to avoid a delay when <afile> is not used. */
10059 autocmd_fname_full = TRUE;
10060 result = FullName_save(autocmd_fname, FALSE);
10061 vim_free(autocmd_fname);
10062 autocmd_fname = result;
10063 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010064 if (result == NULL)
10065 {
10066 *errormsg = (char_u *)_("E495: no autocommand file name to substitute for \"<afile>\"");
10067 return NULL;
10068 }
Bram Moolenaara0174af2008-01-02 20:08:25 +000010069 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010070 break;
10071
10072 case SPEC_ABUF: /* buffer number for autocommand */
10073 if (autocmd_bufnr <= 0)
10074 {
10075 *errormsg = (char_u *)_("E496: no autocommand buffer number to substitute for \"<abuf>\"");
10076 return NULL;
10077 }
10078 sprintf((char *)strbuf, "%d", autocmd_bufnr);
10079 result = strbuf;
10080 break;
10081
10082 case SPEC_AMATCH: /* match name for autocommand */
10083 result = autocmd_match;
10084 if (result == NULL)
10085 {
10086 *errormsg = (char_u *)_("E497: no autocommand match name to substitute for \"<amatch>\"");
10087 return NULL;
10088 }
10089 break;
10090
10091#endif
10092 case SPEC_SFILE: /* file name for ":so" command */
10093 result = sourcing_name;
10094 if (result == NULL)
10095 {
10096 *errormsg = (char_u *)_("E498: no :source file name to substitute for \"<sfile>\"");
10097 return NULL;
10098 }
10099 break;
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010100 case SPEC_SLNUM: /* line in file for ":so" command */
10101 if (sourcing_name == NULL || sourcing_lnum == 0)
10102 {
10103 *errormsg = (char_u *)_("E842: no line number to use for \"<slnum>\"");
10104 return NULL;
10105 }
10106 sprintf((char *)strbuf, "%ld", (long)sourcing_lnum);
10107 result = strbuf;
10108 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010109#if defined(FEAT_CLIENTSERVER)
10110 case SPEC_CLIENT: /* Source of last submitted input */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010111 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
10112 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010113 result = strbuf;
10114 break;
10115#endif
10116 }
10117
10118 resultlen = (int)STRLEN(result); /* length of new string */
10119 if (src[*usedlen] == '<') /* remove the file name extension */
10120 {
10121 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010122 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010123 resultlen = (int)(s - result);
10124 }
10125#ifdef FEAT_MODIFY_FNAME
10126 else if (!skip_mod)
10127 {
10128 valid |= modify_fname(src, usedlen, &result, &resultbuf,
10129 &resultlen);
10130 if (result == NULL)
10131 {
10132 *errormsg = (char_u *)"";
10133 return NULL;
10134 }
10135 }
10136#endif
10137 }
10138
10139 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
10140 {
10141 if (valid != VALID_HEAD + VALID_PATH)
10142 /* xgettext:no-c-format */
10143 *errormsg = (char_u *)_("E499: Empty file name for '%' or '#', only works with \":p:h\"");
10144 else
10145 *errormsg = (char_u *)_("E500: Evaluates to an empty string");
10146 result = NULL;
10147 }
10148 else
10149 result = vim_strnsave(result, resultlen);
10150 vim_free(resultbuf);
10151 return result;
10152}
10153
10154/*
10155 * Concatenate all files in the argument list, separated by spaces, and return
10156 * it in one allocated string.
10157 * Spaces and backslashes in the file names are escaped with a backslash.
10158 * Returns NULL when out of memory.
10159 */
10160 static char_u *
10161arg_all()
10162{
10163 int len;
10164 int idx;
10165 char_u *retval = NULL;
10166 char_u *p;
10167
10168 /*
10169 * Do this loop two times:
10170 * first time: compute the total length
10171 * second time: concatenate the names
10172 */
10173 for (;;)
10174 {
10175 len = 0;
10176 for (idx = 0; idx < ARGCOUNT; ++idx)
10177 {
10178 p = alist_name(&ARGLIST[idx]);
10179 if (p != NULL)
10180 {
10181 if (len > 0)
10182 {
10183 /* insert a space in between names */
10184 if (retval != NULL)
10185 retval[len] = ' ';
10186 ++len;
10187 }
10188 for ( ; *p != NUL; ++p)
10189 {
10190 if (*p == ' ' || *p == '\\')
10191 {
10192 /* insert a backslash */
10193 if (retval != NULL)
10194 retval[len] = '\\';
10195 ++len;
10196 }
10197 if (retval != NULL)
10198 retval[len] = *p;
10199 ++len;
10200 }
10201 }
10202 }
10203
10204 /* second time: break here */
10205 if (retval != NULL)
10206 {
10207 retval[len] = NUL;
10208 break;
10209 }
10210
10211 /* allocate memory */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000010212 retval = alloc((unsigned)len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010213 if (retval == NULL)
10214 break;
10215 }
10216
10217 return retval;
10218}
10219
10220#if defined(FEAT_AUTOCMD) || defined(PROTO)
10221/*
10222 * Expand the <sfile> string in "arg".
10223 *
10224 * Returns an allocated string, or NULL for any error.
10225 */
10226 char_u *
10227expand_sfile(arg)
10228 char_u *arg;
10229{
10230 char_u *errormsg;
10231 int len;
10232 char_u *result;
10233 char_u *newres;
10234 char_u *repl;
10235 int srclen;
10236 char_u *p;
10237
10238 result = vim_strsave(arg);
10239 if (result == NULL)
10240 return NULL;
10241
10242 for (p = result; *p; )
10243 {
10244 if (STRNCMP(p, "<sfile>", 7) != 0)
10245 ++p;
10246 else
10247 {
10248 /* replace "<sfile>" with the sourced file name, and do ":" stuff */
Bram Moolenaar63b92542007-03-27 14:57:09 +000010249 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010250 if (errormsg != NULL)
10251 {
10252 if (*errormsg)
10253 emsg(errormsg);
10254 vim_free(result);
10255 return NULL;
10256 }
10257 if (repl == NULL) /* no match (cannot happen) */
10258 {
10259 p += srclen;
10260 continue;
10261 }
10262 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
10263 newres = alloc(len);
10264 if (newres == NULL)
10265 {
10266 vim_free(repl);
10267 vim_free(result);
10268 return NULL;
10269 }
10270 mch_memmove(newres, result, (size_t)(p - result));
10271 STRCPY(newres + (p - result), repl);
10272 len = (int)STRLEN(newres);
10273 STRCAT(newres, p + srclen);
10274 vim_free(repl);
10275 vim_free(result);
10276 result = newres;
10277 p = newres + len; /* continue after the match */
10278 }
10279 }
10280
10281 return result;
10282}
10283#endif
10284
10285#ifdef FEAT_SESSION
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010286static int ses_winsizes __ARGS((FILE *fd, int restore_size,
10287 win_T *tab_firstwin));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010288static int ses_win_rec __ARGS((FILE *fd, frame_T *fr));
10289static frame_T *ses_skipframe __ARGS((frame_T *fr));
10290static int ses_do_frame __ARGS((frame_T *fr));
10291static int ses_do_win __ARGS((win_T *wp));
10292static int ses_arglist __ARGS((FILE *fd, char *cmd, garray_T *gap, int fullname, unsigned *flagp));
10293static int ses_put_fname __ARGS((FILE *fd, char_u *name, unsigned *flagp));
10294static int ses_fname __ARGS((FILE *fd, buf_T *buf, unsigned *flagp));
10295
10296/*
10297 * Write openfile commands for the current buffers to an .exrc file.
10298 * Return FAIL on error, OK otherwise.
10299 */
10300 static int
10301makeopens(fd, dirnow)
10302 FILE *fd;
10303 char_u *dirnow; /* Current directory name */
10304{
10305 buf_T *buf;
10306 int only_save_windows = TRUE;
10307 int nr;
10308 int cnr = 1;
10309 int restore_size = TRUE;
10310 win_T *wp;
10311 char_u *sname;
10312 win_T *edited_win = NULL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010313 int tabnr;
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020010314 int restore_stal = FALSE;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010315 win_T *tab_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010316 frame_T *tab_topframe;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010317 int cur_arg_idx = 0;
Bram Moolenaar383c6f52008-01-04 15:01:07 +000010318 int next_arg_idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010319
10320 if (ssop_flags & SSOP_BUFFERS)
10321 only_save_windows = FALSE; /* Save ALL buffers */
10322
10323 /*
10324 * Begin by setting the this_session variable, and then other
10325 * sessionable variables.
10326 */
10327#ifdef FEAT_EVAL
10328 if (put_line(fd, "let v:this_session=expand(\"<sfile>:p\")") == FAIL)
10329 return FAIL;
10330 if (ssop_flags & SSOP_GLOBALS)
10331 if (store_session_globals(fd) == FAIL)
10332 return FAIL;
10333#endif
10334
10335 /*
10336 * Close all windows but one.
10337 */
10338 if (put_line(fd, "silent only") == FAIL)
10339 return FAIL;
10340
10341 /*
10342 * Now a :cd command to the session directory or the current directory
10343 */
10344 if (ssop_flags & SSOP_SESDIR)
10345 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010346 if (put_line(fd, "exe \"cd \" . escape(expand(\"<sfile>:p:h\"), ' ')")
10347 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010348 return FAIL;
10349 }
10350 else if (ssop_flags & SSOP_CURDIR)
10351 {
10352 sname = home_replace_save(NULL, globaldir != NULL ? globaldir : dirnow);
10353 if (sname == NULL
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010354 || fputs("cd ", fd) < 0
10355 || ses_put_fname(fd, sname, &ssop_flags) == FAIL
10356 || put_eol(fd) == FAIL)
10357 {
10358 vim_free(sname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010359 return FAIL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010360 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010361 vim_free(sname);
10362 }
10363
10364 /*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010365 * If there is an empty, unnamed buffer we will wipe it out later.
10366 * Remember the buffer number.
10367 */
10368 if (put_line(fd, "if expand('%') == '' && !&modified && line('$') <= 1 && getline(1) == ''") == FAIL)
10369 return FAIL;
10370 if (put_line(fd, " let s:wipebuf = bufnr('%')") == FAIL)
10371 return FAIL;
10372 if (put_line(fd, "endif") == FAIL)
10373 return FAIL;
10374
10375 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010376 * Now save the current files, current buffer first.
10377 */
10378 if (put_line(fd, "set shortmess=aoO") == FAIL)
10379 return FAIL;
10380
10381 /* Now put the other buffers into the buffer list */
10382 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
10383 {
10384 if (!(only_save_windows && buf->b_nwindows == 0)
10385 && !(buf->b_help && !(ssop_flags & SSOP_HELP))
10386 && buf->b_fname != NULL
10387 && buf->b_p_bl)
10388 {
10389 if (fprintf(fd, "badd +%ld ", buf->b_wininfo == NULL ? 1L
10390 : buf->b_wininfo->wi_fpos.lnum) < 0
10391 || ses_fname(fd, buf, &ssop_flags) == FAIL)
10392 return FAIL;
10393 }
10394 }
10395
10396 /* the global argument list */
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010010397 if (ses_arglist(fd, "argglobal", &global_alist.al_ga,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010398 !(ssop_flags & SSOP_CURDIR), &ssop_flags) == FAIL)
10399 return FAIL;
10400
10401 if (ssop_flags & SSOP_RESIZE)
10402 {
10403 /* Note: after the restore we still check it worked!*/
10404 if (fprintf(fd, "set lines=%ld columns=%ld" , Rows, Columns) < 0
10405 || put_eol(fd) == FAIL)
10406 return FAIL;
10407 }
10408
10409#ifdef FEAT_GUI
10410 if (gui.in_use && (ssop_flags & SSOP_WINPOS))
10411 {
10412 int x, y;
10413
10414 if (gui_mch_get_winpos(&x, &y) == OK)
10415 {
10416 /* Note: after the restore we still check it worked!*/
10417 if (fprintf(fd, "winpos %d %d", x, y) < 0 || put_eol(fd) == FAIL)
10418 return FAIL;
10419 }
10420 }
10421#endif
10422
10423 /*
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020010424 * When there are two or more tabpages and 'showtabline' is 1 the tabline
10425 * will be displayed when creating the next tab. That resizes the windows
10426 * in the first tab, which may cause problems. Set 'showtabline' to 2
10427 * temporarily to avoid that.
10428 */
10429 if (p_stal == 1 && first_tabpage->tp_next != NULL)
10430 {
10431 if (put_line(fd, "set stal=2") == FAIL)
10432 return FAIL;
10433 restore_stal = TRUE;
10434 }
10435
10436 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000010437 * May repeat putting Windows for each tab, when "tabpages" is in
10438 * 'sessionoptions'.
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010439 * Don't use goto_tabpage(), it may change directory and trigger
10440 * autocommands.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010441 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010442 tab_firstwin = firstwin; /* first window in tab page "tabnr" */
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010443 tab_topframe = topframe;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010444 for (tabnr = 1; ; ++tabnr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010445 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010446 int need_tabnew = FALSE;
10447
Bram Moolenaar18144c82006-04-12 21:52:12 +000010448 if ((ssop_flags & SSOP_TABPAGES))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010449 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010450 tabpage_T *tp = find_tabpage(tabnr);
10451
10452 if (tp == NULL)
10453 break; /* done all tab pages */
10454 if (tp == curtab)
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010455 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010456 tab_firstwin = firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010457 tab_topframe = topframe;
10458 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010459 else
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010460 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010461 tab_firstwin = tp->tp_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010462 tab_topframe = tp->tp_topframe;
10463 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010464 if (tabnr > 1)
10465 need_tabnew = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010466 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010467
Bram Moolenaar18144c82006-04-12 21:52:12 +000010468 /*
10469 * Before creating the window layout, try loading one file. If this
10470 * is aborted we don't end up with a number of useless windows.
10471 * This may have side effects! (e.g., compressed or network file).
10472 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010473 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010474 {
10475 if (ses_do_win(wp)
10476 && wp->w_buffer->b_ffname != NULL
10477 && !wp->w_buffer->b_help
10478#ifdef FEAT_QUICKFIX
10479 && !bt_nofile(wp->w_buffer)
10480#endif
10481 )
10482 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010483 if (fputs(need_tabnew ? "tabedit " : "edit ", fd) < 0
Bram Moolenaar18144c82006-04-12 21:52:12 +000010484 || ses_fname(fd, wp->w_buffer, &ssop_flags) == FAIL)
10485 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010486 need_tabnew = FALSE;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010487 if (!wp->w_arg_idx_invalid)
10488 edited_win = wp;
10489 break;
10490 }
10491 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010492
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010493 /* If no file got edited create an empty tab page. */
10494 if (need_tabnew && put_line(fd, "tabnew") == FAIL)
10495 return FAIL;
10496
Bram Moolenaar18144c82006-04-12 21:52:12 +000010497 /*
10498 * Save current window layout.
10499 */
10500 if (put_line(fd, "set splitbelow splitright") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010501 return FAIL;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010502 if (ses_win_rec(fd, tab_topframe) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010503 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010504 if (!p_sb && put_line(fd, "set nosplitbelow") == FAIL)
10505 return FAIL;
10506 if (!p_spr && put_line(fd, "set nosplitright") == FAIL)
10507 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010508
Bram Moolenaar18144c82006-04-12 21:52:12 +000010509 /*
10510 * Check if window sizes can be restored (no windows omitted).
10511 * Remember the window number of the current window after restoring.
10512 */
10513 nr = 0;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010514 for (wp = tab_firstwin; wp != NULL; wp = W_NEXT(wp))
Bram Moolenaar18144c82006-04-12 21:52:12 +000010515 {
10516 if (ses_do_win(wp))
10517 ++nr;
10518 else
10519 restore_size = FALSE;
10520 if (curwin == wp)
10521 cnr = nr;
10522 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010523
Bram Moolenaar18144c82006-04-12 21:52:12 +000010524 /* Go to the first window. */
10525 if (put_line(fd, "wincmd t") == FAIL)
10526 return FAIL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010527
Bram Moolenaar18144c82006-04-12 21:52:12 +000010528 /*
10529 * If more than one window, see if sizes can be restored.
10530 * First set 'winheight' and 'winwidth' to 1 to avoid the windows being
10531 * resized when moving between windows.
10532 * Do this before restoring the view, so that the topline and the
10533 * cursor can be set. This is done again below.
10534 */
10535 if (put_line(fd, "set winheight=1 winwidth=1") == FAIL)
10536 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010537 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010538 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010539
Bram Moolenaar18144c82006-04-12 21:52:12 +000010540 /*
10541 * Restore the view of the window (options, file, cursor, etc.).
10542 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010543 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010544 {
10545 if (!ses_do_win(wp))
10546 continue;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010547 if (put_view(fd, wp, wp != edited_win, &ssop_flags,
10548 cur_arg_idx) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010549 return FAIL;
10550 if (nr > 1 && put_line(fd, "wincmd w") == FAIL)
10551 return FAIL;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010552 next_arg_idx = wp->w_arg_idx;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010553 }
10554
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010555 /* The argument index in the first tab page is zero, need to set it in
10556 * each window. For further tab pages it's the window where we do
10557 * "tabedit". */
10558 cur_arg_idx = next_arg_idx;
10559
Bram Moolenaar18144c82006-04-12 21:52:12 +000010560 /*
10561 * Restore cursor to the current window if it's not the first one.
10562 */
10563 if (cnr > 1 && (fprintf(fd, "%dwincmd w", cnr) < 0
10564 || put_eol(fd) == FAIL))
10565 return FAIL;
10566
10567 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000010568 * Restore window sizes again after jumping around in windows, because
10569 * the current window has a minimum size while others may not.
10570 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010571 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010572 return FAIL;
10573
Bram Moolenaar18144c82006-04-12 21:52:12 +000010574 /* Don't continue in another tab page when doing only the current one
10575 * or when at the last tab page. */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010576 if (!(ssop_flags & SSOP_TABPAGES))
Bram Moolenaar18144c82006-04-12 21:52:12 +000010577 break;
10578 }
10579
10580 if (ssop_flags & SSOP_TABPAGES)
10581 {
Bram Moolenaar18144c82006-04-12 21:52:12 +000010582 if (fprintf(fd, "tabnext %d", tabpage_index(curtab)) < 0
10583 || put_eol(fd) == FAIL)
10584 return FAIL;
10585 }
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020010586 if (restore_stal && put_line(fd, "set stal=1") == FAIL)
10587 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010588
Bram Moolenaar9c102382006-05-03 21:26:49 +000010589 /*
10590 * Wipe out an empty unnamed buffer we started in.
10591 */
10592 if (put_line(fd, "if exists('s:wipebuf')") == FAIL)
10593 return FAIL;
Bram Moolenaarf3a67882006-05-05 21:09:41 +000010594 if (put_line(fd, " silent exe 'bwipe ' . s:wipebuf") == FAIL)
Bram Moolenaar9c102382006-05-03 21:26:49 +000010595 return FAIL;
10596 if (put_line(fd, "endif") == FAIL)
10597 return FAIL;
10598 if (put_line(fd, "unlet! s:wipebuf") == FAIL)
10599 return FAIL;
10600
10601 /* Re-apply 'winheight', 'winwidth' and 'shortmess'. */
10602 if (fprintf(fd, "set winheight=%ld winwidth=%ld shortmess=%s",
10603 p_wh, p_wiw, p_shm) < 0 || put_eol(fd) == FAIL)
10604 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010605
10606 /*
10607 * Lastly, execute the x.vim file if it exists.
10608 */
10609 if (put_line(fd, "let s:sx = expand(\"<sfile>:p:r\").\"x.vim\"") == FAIL
10610 || put_line(fd, "if file_readable(s:sx)") == FAIL
Bram Moolenaar42ba1262008-12-09 10:18:03 +000010611 || put_line(fd, " exe \"source \" . fnameescape(s:sx)") == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000010612 || put_line(fd, "endif") == FAIL)
10613 return FAIL;
10614
10615 return OK;
10616}
10617
10618 static int
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010619ses_winsizes(fd, restore_size, tab_firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010620 FILE *fd;
10621 int restore_size;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010622 win_T *tab_firstwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010623{
10624 int n = 0;
10625 win_T *wp;
10626
10627 if (restore_size && (ssop_flags & SSOP_WINSIZE))
10628 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010629 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010630 {
10631 if (!ses_do_win(wp))
10632 continue;
10633 ++n;
10634
10635 /* restore height when not full height */
10636 if (wp->w_height + wp->w_status_height < topframe->fr_height
10637 && (fprintf(fd,
10638 "exe '%dresize ' . ((&lines * %ld + %ld) / %ld)",
10639 n, (long)wp->w_height, Rows / 2, Rows) < 0
10640 || put_eol(fd) == FAIL))
10641 return FAIL;
10642
10643 /* restore width when not full width */
10644 if (wp->w_width < Columns && (fprintf(fd,
10645 "exe 'vert %dresize ' . ((&columns * %ld + %ld) / %ld)",
10646 n, (long)wp->w_width, Columns / 2, Columns) < 0
10647 || put_eol(fd) == FAIL))
10648 return FAIL;
10649 }
10650 }
10651 else
10652 {
10653 /* Just equalise window sizes */
10654 if (put_line(fd, "wincmd =") == FAIL)
10655 return FAIL;
10656 }
10657 return OK;
10658}
10659
10660/*
10661 * Write commands to "fd" to recursively create windows for frame "fr",
10662 * horizontally and vertically split.
10663 * After the commands the last window in the frame is the current window.
10664 * Returns FAIL when writing the commands to "fd" fails.
10665 */
10666 static int
10667ses_win_rec(fd, fr)
10668 FILE *fd;
10669 frame_T *fr;
10670{
10671 frame_T *frc;
10672 int count = 0;
10673
10674 if (fr->fr_layout != FR_LEAF)
10675 {
10676 /* Find first frame that's not skipped and then create a window for
10677 * each following one (first frame is already there). */
10678 frc = ses_skipframe(fr->fr_child);
10679 if (frc != NULL)
10680 while ((frc = ses_skipframe(frc->fr_next)) != NULL)
10681 {
10682 /* Make window as big as possible so that we have lots of room
10683 * to split. */
10684 if (put_line(fd, "wincmd _ | wincmd |") == FAIL
10685 || put_line(fd, fr->fr_layout == FR_COL
10686 ? "split" : "vsplit") == FAIL)
10687 return FAIL;
10688 ++count;
10689 }
10690
10691 /* Go back to the first window. */
10692 if (count > 0 && (fprintf(fd, fr->fr_layout == FR_COL
10693 ? "%dwincmd k" : "%dwincmd h", count) < 0
10694 || put_eol(fd) == FAIL))
10695 return FAIL;
10696
10697 /* Recursively create frames/windows in each window of this column or
10698 * row. */
10699 frc = ses_skipframe(fr->fr_child);
10700 while (frc != NULL)
10701 {
10702 ses_win_rec(fd, frc);
10703 frc = ses_skipframe(frc->fr_next);
10704 /* Go to next window. */
10705 if (frc != NULL && put_line(fd, "wincmd w") == FAIL)
10706 return FAIL;
10707 }
10708 }
10709 return OK;
10710}
10711
10712/*
10713 * Skip frames that don't contain windows we want to save in the Session.
10714 * Returns NULL when there none.
10715 */
10716 static frame_T *
10717ses_skipframe(fr)
10718 frame_T *fr;
10719{
10720 frame_T *frc;
10721
10722 for (frc = fr; frc != NULL; frc = frc->fr_next)
10723 if (ses_do_frame(frc))
10724 break;
10725 return frc;
10726}
10727
10728/*
10729 * Return TRUE if frame "fr" has a window somewhere that we want to save in
10730 * the Session.
10731 */
10732 static int
10733ses_do_frame(fr)
10734 frame_T *fr;
10735{
10736 frame_T *frc;
10737
10738 if (fr->fr_layout == FR_LEAF)
10739 return ses_do_win(fr->fr_win);
10740 for (frc = fr->fr_child; frc != NULL; frc = frc->fr_next)
10741 if (ses_do_frame(frc))
10742 return TRUE;
10743 return FALSE;
10744}
10745
10746/*
10747 * Return non-zero if window "wp" is to be stored in the Session.
10748 */
10749 static int
10750ses_do_win(wp)
10751 win_T *wp;
10752{
10753 if (wp->w_buffer->b_fname == NULL
10754#ifdef FEAT_QUICKFIX
10755 /* When 'buftype' is "nofile" can't restore the window contents. */
10756 || bt_nofile(wp->w_buffer)
10757#endif
10758 )
10759 return (ssop_flags & SSOP_BLANK);
10760 if (wp->w_buffer->b_help)
10761 return (ssop_flags & SSOP_HELP);
10762 return TRUE;
10763}
10764
10765/*
10766 * Write commands to "fd" to restore the view of a window.
10767 * Caller must make sure 'scrolloff' is zero.
10768 */
10769 static int
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010770put_view(fd, wp, add_edit, flagp, current_arg_idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010771 FILE *fd;
10772 win_T *wp;
10773 int add_edit; /* add ":edit" command to view */
10774 unsigned *flagp; /* vop_flags or ssop_flags */
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010775 int current_arg_idx; /* current argument index of the window, use
10776 * -1 if unknown */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010777{
10778 win_T *save_curwin;
10779 int f;
10780 int do_cursor;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010781 int did_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010782
10783 /* Always restore cursor position for ":mksession". For ":mkview" only
10784 * when 'viewoptions' contains "cursor". */
10785 do_cursor = (flagp == &ssop_flags || *flagp & SSOP_CURSOR);
10786
10787 /*
10788 * Local argument list.
10789 */
10790 if (wp->w_alist == &global_alist)
10791 {
10792 if (put_line(fd, "argglobal") == FAIL)
10793 return FAIL;
10794 }
10795 else
10796 {
10797 if (ses_arglist(fd, "arglocal", &wp->w_alist->al_ga,
10798 flagp == &vop_flags
10799 || !(*flagp & SSOP_CURDIR)
10800 || wp->w_localdir != NULL, flagp) == FAIL)
10801 return FAIL;
10802 }
10803
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010804 /* Only when part of a session: restore the argument index. Some
10805 * arguments may have been deleted, check if the index is valid. */
Bram Moolenaar51f53df2010-06-26 05:25:54 +020010806 if (wp->w_arg_idx != current_arg_idx && wp->w_arg_idx < WARGCOUNT(wp)
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010807 && flagp == &ssop_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010808 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010809 if (fprintf(fd, "%ldargu", (long)wp->w_arg_idx + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010810 || put_eol(fd) == FAIL)
10811 return FAIL;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010812 did_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010813 }
10814
10815 /* Edit the file. Skip this when ":next" already did it. */
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010816 if (add_edit && (!did_next || wp->w_arg_idx_invalid))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010817 {
10818 /*
10819 * Load the file.
10820 */
10821 if (wp->w_buffer->b_ffname != NULL
10822#ifdef FEAT_QUICKFIX
10823 && !bt_nofile(wp->w_buffer)
10824#endif
10825 )
10826 {
10827 /*
10828 * Editing a file in this buffer: use ":edit file".
10829 * This may have side effects! (e.g., compressed or network file).
10830 */
10831 if (fputs("edit ", fd) < 0
10832 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
10833 return FAIL;
10834 }
10835 else
10836 {
10837 /* No file in this buffer, just make it empty. */
10838 if (put_line(fd, "enew") == FAIL)
10839 return FAIL;
10840#ifdef FEAT_QUICKFIX
10841 if (wp->w_buffer->b_ffname != NULL)
10842 {
10843 /* The buffer does have a name, but it's not a file name. */
10844 if (fputs("file ", fd) < 0
10845 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
10846 return FAIL;
10847 }
10848#endif
10849 do_cursor = FALSE;
10850 }
10851 }
10852
10853 /*
10854 * Local mappings and abbreviations.
10855 */
10856 if ((*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
10857 && makemap(fd, wp->w_buffer) == FAIL)
10858 return FAIL;
10859
10860 /*
10861 * Local options. Need to go to the window temporarily.
10862 * Store only local values when using ":mkview" and when ":mksession" is
10863 * used and 'sessionoptions' doesn't include "options".
10864 * Some folding options are always stored when "folds" is included,
10865 * otherwise the folds would not be restored correctly.
10866 */
10867 save_curwin = curwin;
10868 curwin = wp;
10869 curbuf = curwin->w_buffer;
10870 if (*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
10871 f = makeset(fd, OPT_LOCAL,
10872 flagp == &vop_flags || !(*flagp & SSOP_OPTIONS));
10873#ifdef FEAT_FOLDING
10874 else if (*flagp & SSOP_FOLDS)
10875 f = makefoldset(fd);
10876#endif
10877 else
10878 f = OK;
10879 curwin = save_curwin;
10880 curbuf = curwin->w_buffer;
10881 if (f == FAIL)
10882 return FAIL;
10883
10884#ifdef FEAT_FOLDING
10885 /*
10886 * Save Folds when 'buftype' is empty and for help files.
10887 */
10888 if ((*flagp & SSOP_FOLDS)
10889 && wp->w_buffer->b_ffname != NULL
Bram Moolenaarb1b715d2006-01-21 22:09:43 +000010890# ifdef FEAT_QUICKFIX
10891 && (*wp->w_buffer->b_p_bt == NUL || wp->w_buffer->b_help)
10892# endif
10893 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000010894 {
10895 if (put_folds(fd, wp) == FAIL)
10896 return FAIL;
10897 }
10898#endif
10899
10900 /*
10901 * Set the cursor after creating folds, since that moves the cursor.
10902 */
10903 if (do_cursor)
10904 {
10905
10906 /* Restore the cursor line in the file and relatively in the
10907 * window. Don't use "G", it changes the jumplist. */
10908 if (fprintf(fd, "let s:l = %ld - ((%ld * winheight(0) + %ld) / %ld)",
10909 (long)wp->w_cursor.lnum,
10910 (long)(wp->w_cursor.lnum - wp->w_topline),
10911 (long)wp->w_height / 2, (long)wp->w_height) < 0
10912 || put_eol(fd) == FAIL
10913 || put_line(fd, "if s:l < 1 | let s:l = 1 | endif") == FAIL
10914 || put_line(fd, "exe s:l") == FAIL
10915 || put_line(fd, "normal! zt") == FAIL
10916 || fprintf(fd, "%ld", (long)wp->w_cursor.lnum) < 0
10917 || put_eol(fd) == FAIL)
10918 return FAIL;
10919 /* Restore the cursor column and left offset when not wrapping. */
10920 if (wp->w_cursor.col == 0)
10921 {
10922 if (put_line(fd, "normal! 0") == FAIL)
10923 return FAIL;
10924 }
10925 else
10926 {
10927 if (!wp->w_p_wrap && wp->w_leftcol > 0 && wp->w_width > 0)
10928 {
10929 if (fprintf(fd,
10930 "let s:c = %ld - ((%ld * winwidth(0) + %ld) / %ld)",
Bram Moolenaar558ddad2013-02-20 19:26:29 +010010931 (long)wp->w_virtcol + 1,
10932 (long)(wp->w_virtcol - wp->w_leftcol),
Bram Moolenaar071d4272004-06-13 20:20:40 +000010933 (long)wp->w_width / 2, (long)wp->w_width) < 0
10934 || put_eol(fd) == FAIL
10935 || put_line(fd, "if s:c > 0") == FAIL
10936 || fprintf(fd,
Bram Moolenaar558ddad2013-02-20 19:26:29 +010010937 " exe 'normal! ' . s:c . '|zs' . %ld . '|'",
10938 (long)wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010939 || put_eol(fd) == FAIL
10940 || put_line(fd, "else") == FAIL
Bram Moolenaarfdf447b2013-02-26 17:21:29 +010010941 || fprintf(fd, " normal! 0%d|", wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010942 || put_eol(fd) == FAIL
10943 || put_line(fd, "endif") == FAIL)
10944 return FAIL;
10945 }
10946 else
10947 {
Bram Moolenaar558ddad2013-02-20 19:26:29 +010010948 if (fprintf(fd, "normal! 0%d|", wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010949 || put_eol(fd) == FAIL)
10950 return FAIL;
10951 }
10952 }
10953 }
10954
10955 /*
10956 * Local directory.
10957 */
10958 if (wp->w_localdir != NULL)
10959 {
10960 if (fputs("lcd ", fd) < 0
10961 || ses_put_fname(fd, wp->w_localdir, flagp) == FAIL
10962 || put_eol(fd) == FAIL)
10963 return FAIL;
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010964 did_lcd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010965 }
10966
10967 return OK;
10968}
10969
10970/*
10971 * Write an argument list to the session file.
10972 * Returns FAIL if writing fails.
10973 */
10974 static int
10975ses_arglist(fd, cmd, gap, fullname, flagp)
10976 FILE *fd;
10977 char *cmd;
10978 garray_T *gap;
10979 int fullname; /* TRUE: use full path name */
10980 unsigned *flagp;
10981{
10982 int i;
Bram Moolenaard9462e32011-04-11 21:35:11 +020010983 char_u *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010984 char_u *s;
10985
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010010986 if (fputs(cmd, fd) < 0 || put_eol(fd) == FAIL)
10987 return FAIL;
10988 if (put_line(fd, "silent! argdel *") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010989 return FAIL;
10990 for (i = 0; i < gap->ga_len; ++i)
10991 {
10992 /* NULL file names are skipped (only happens when out of memory). */
10993 s = alist_name(&((aentry_T *)gap->ga_data)[i]);
10994 if (s != NULL)
10995 {
10996 if (fullname)
10997 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020010998 buf = alloc(MAXPATHL);
10999 if (buf != NULL)
11000 {
11001 (void)vim_FullName(s, buf, MAXPATHL, FALSE);
11002 s = buf;
11003 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011004 }
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011005 if (fputs("argadd ", fd) < 0
11006 || ses_put_fname(fd, s, flagp) == FAIL
11007 || put_eol(fd) == FAIL)
Bram Moolenaard9462e32011-04-11 21:35:11 +020011008 {
11009 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011010 return FAIL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011011 }
11012 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011013 }
11014 }
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011015 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011016}
11017
11018/*
11019 * Write a buffer name to the session file.
11020 * Also ends the line.
11021 * Returns FAIL if writing fails.
11022 */
11023 static int
11024ses_fname(fd, buf, flagp)
11025 FILE *fd;
11026 buf_T *buf;
11027 unsigned *flagp;
11028{
11029 char_u *name;
11030
11031 /* Use the short file name if the current directory is known at the time
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011032 * the session file will be sourced.
11033 * Don't do this for ":mkview", we don't know the current directory.
11034 * Don't do this after ":lcd", we don't keep track of what the current
11035 * directory is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011036 if (buf->b_sfname != NULL
11037 && flagp == &ssop_flags
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011038 && (ssop_flags & (SSOP_CURDIR | SSOP_SESDIR))
Bram Moolenaar8d594672009-07-01 18:18:57 +000011039#ifdef FEAT_AUTOCHDIR
11040 && !p_acd
11041#endif
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011042 && !did_lcd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011043 name = buf->b_sfname;
11044 else
11045 name = buf->b_ffname;
11046 if (ses_put_fname(fd, name, flagp) == FAIL || put_eol(fd) == FAIL)
11047 return FAIL;
11048 return OK;
11049}
11050
11051/*
11052 * Write a file name to the session file.
11053 * Takes care of the "slash" option in 'sessionoptions' and escapes special
11054 * characters.
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011055 * Returns FAIL if writing fails or out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011056 */
11057 static int
11058ses_put_fname(fd, name, flagp)
11059 FILE *fd;
11060 char_u *name;
11061 unsigned *flagp;
11062{
11063 char_u *sname;
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011064 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011065 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011066
11067 sname = home_replace_save(NULL, name);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011068 if (sname == NULL)
11069 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011070
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011071 if (*flagp & SSOP_SLASH)
11072 {
11073 /* change all backslashes to forward slashes */
11074 for (p = sname; *p != NUL; mb_ptr_adv(p))
11075 if (*p == '\\')
11076 *p = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +000011077 }
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011078
Bram Moolenaar84a05ac2013-05-06 04:24:17 +020011079 /* escape special characters */
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011080 p = vim_strsave_fnameescape(sname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011081 vim_free(sname);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011082 if (p == NULL)
11083 return FAIL;
11084
11085 /* write the result */
11086 if (fputs((char *)p, fd) < 0)
11087 retval = FAIL;
11088
11089 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011090 return retval;
11091}
11092
11093/*
11094 * ":loadview [nr]"
11095 */
11096 static void
11097ex_loadview(eap)
11098 exarg_T *eap;
11099{
11100 char_u *fname;
11101
11102 fname = get_view_file(*eap->arg);
11103 if (fname != NULL)
11104 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +000011105 do_source(fname, FALSE, DOSO_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011106 vim_free(fname);
11107 }
11108}
11109
11110/*
11111 * Get the name of the view file for the current buffer.
11112 */
11113 static char_u *
11114get_view_file(c)
11115 int c;
11116{
11117 int len = 0;
11118 char_u *p, *s;
11119 char_u *retval;
11120 char_u *sname;
11121
11122 if (curbuf->b_ffname == NULL)
11123 {
11124 EMSG(_(e_noname));
11125 return NULL;
11126 }
11127 sname = home_replace_save(NULL, curbuf->b_ffname);
11128 if (sname == NULL)
11129 return NULL;
11130
11131 /*
11132 * We want a file name without separators, because we're not going to make
11133 * a directory.
11134 * "normal" path separator -> "=+"
11135 * "=" -> "=="
11136 * ":" path separator -> "=-"
11137 */
11138 for (p = sname; *p; ++p)
11139 if (*p == '=' || vim_ispathsep(*p))
11140 ++len;
11141 retval = alloc((unsigned)(STRLEN(sname) + len + STRLEN(p_vdir) + 9));
11142 if (retval != NULL)
11143 {
11144 STRCPY(retval, p_vdir);
11145 add_pathsep(retval);
11146 s = retval + STRLEN(retval);
11147 for (p = sname; *p; ++p)
11148 {
11149 if (*p == '=')
11150 {
11151 *s++ = '=';
11152 *s++ = '=';
11153 }
11154 else if (vim_ispathsep(*p))
11155 {
11156 *s++ = '=';
Bram Moolenaare60acc12011-05-10 16:41:25 +020011157#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011158 if (*p == ':')
11159 *s++ = '-';
11160 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000011161#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000011162 *s++ = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +000011163 }
11164 else
11165 *s++ = *p;
11166 }
11167 *s++ = '=';
11168 *s++ = c;
11169 STRCPY(s, ".vim");
11170 }
11171
11172 vim_free(sname);
11173 return retval;
11174}
11175
11176#endif /* FEAT_SESSION */
11177
11178/*
11179 * Write end-of-line character(s) for ":mkexrc", ":mkvimrc" and ":mksession".
11180 * Return FAIL for a write error.
11181 */
11182 int
11183put_eol(fd)
11184 FILE *fd;
11185{
11186 if (
11187#ifdef USE_CRNL
11188 (
11189# ifdef MKSESSION_NL
11190 !mksession_nl &&
11191# endif
11192 (putc('\r', fd) < 0)) ||
11193#endif
11194 (putc('\n', fd) < 0))
11195 return FAIL;
11196 return OK;
11197}
11198
11199/*
11200 * Write a line to "fd".
11201 * Return FAIL for a write error.
11202 */
11203 int
11204put_line(fd, s)
11205 FILE *fd;
11206 char *s;
11207{
11208 if (fputs(s, fd) < 0 || put_eol(fd) == FAIL)
11209 return FAIL;
11210 return OK;
11211}
11212
11213#ifdef FEAT_VIMINFO
11214/*
11215 * ":rviminfo" and ":wviminfo".
11216 */
11217 static void
11218ex_viminfo(eap)
11219 exarg_T *eap;
11220{
11221 char_u *save_viminfo;
11222
11223 save_viminfo = p_viminfo;
11224 if (*p_viminfo == NUL)
11225 p_viminfo = (char_u *)"'100";
11226 if (eap->cmdidx == CMD_rviminfo)
11227 {
Bram Moolenaard812df62008-11-09 12:46:09 +000011228 if (read_viminfo(eap->arg, VIF_WANT_INFO | VIF_WANT_MARKS
11229 | (eap->forceit ? VIF_FORCEIT : 0)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011230 EMSG(_("E195: Cannot open viminfo file for reading"));
11231 }
11232 else
11233 write_viminfo(eap->arg, eap->forceit);
11234 p_viminfo = save_viminfo;
11235}
11236#endif
11237
11238#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +000011239/*
Bram Moolenaard9462e32011-04-11 21:35:11 +020011240 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +000011241 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +000011242 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011243 void
11244dialog_msg(buff, format, fname)
11245 char_u *buff;
11246 char *format;
11247 char_u *fname;
11248{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011249 if (fname == NULL)
11250 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +020011251 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011252}
11253#endif
11254
11255/*
11256 * ":behave {mswin,xterm}"
11257 */
11258 static void
11259ex_behave(eap)
11260 exarg_T *eap;
11261{
11262 if (STRCMP(eap->arg, "mswin") == 0)
11263 {
11264 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
11265 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
11266 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
11267 set_option_value((char_u *)"keymodel", 0L,
11268 (char_u *)"startsel,stopsel", 0);
11269 }
11270 else if (STRCMP(eap->arg, "xterm") == 0)
11271 {
11272 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
11273 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
11274 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
11275 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
11276 }
11277 else
11278 EMSG2(_(e_invarg2), eap->arg);
11279}
11280
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010011281#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
11282/*
11283 * Function given to ExpandGeneric() to obtain the possible arguments of the
11284 * ":behave {mswin,xterm}" command.
11285 */
11286 char_u *
11287get_behave_arg(xp, idx)
11288 expand_T *xp UNUSED;
11289 int idx;
11290{
11291 if (idx == 0)
11292 return (char_u *)"mswin";
11293 if (idx == 1)
11294 return (char_u *)"xterm";
11295 return NULL;
11296}
11297#endif
11298
Bram Moolenaar071d4272004-06-13 20:20:40 +000011299#ifdef FEAT_AUTOCMD
11300static int filetype_detect = FALSE;
11301static int filetype_plugin = FALSE;
11302static int filetype_indent = FALSE;
11303
11304/*
11305 * ":filetype [plugin] [indent] {on,off,detect}"
11306 * on: Load the filetype.vim file to install autocommands for file types.
11307 * off: Load the ftoff.vim file to remove all autocommands for file types.
11308 * plugin on: load filetype.vim and ftplugin.vim
11309 * plugin off: load ftplugof.vim
11310 * indent on: load filetype.vim and indent.vim
11311 * indent off: load indoff.vim
11312 */
11313 static void
11314ex_filetype(eap)
11315 exarg_T *eap;
11316{
11317 char_u *arg = eap->arg;
11318 int plugin = FALSE;
11319 int indent = FALSE;
11320
11321 if (*eap->arg == NUL)
11322 {
11323 /* Print current status. */
11324 smsg((char_u *)"filetype detection:%s plugin:%s indent:%s",
11325 filetype_detect ? "ON" : "OFF",
11326 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
11327 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
11328 return;
11329 }
11330
11331 /* Accept "plugin" and "indent" in any order. */
11332 for (;;)
11333 {
11334 if (STRNCMP(arg, "plugin", 6) == 0)
11335 {
11336 plugin = TRUE;
11337 arg = skipwhite(arg + 6);
11338 continue;
11339 }
11340 if (STRNCMP(arg, "indent", 6) == 0)
11341 {
11342 indent = TRUE;
11343 arg = skipwhite(arg + 6);
11344 continue;
11345 }
11346 break;
11347 }
11348 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
11349 {
11350 if (*arg == 'o' || !filetype_detect)
11351 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011352 source_runtime((char_u *)FILETYPE_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011353 filetype_detect = TRUE;
11354 if (plugin)
11355 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011356 source_runtime((char_u *)FTPLUGIN_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011357 filetype_plugin = TRUE;
11358 }
11359 if (indent)
11360 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011361 source_runtime((char_u *)INDENT_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011362 filetype_indent = TRUE;
11363 }
11364 }
11365 if (*arg == 'd')
11366 {
11367 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE);
Bram Moolenaara3227e22006-03-08 21:32:40 +000011368 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011369 }
11370 }
11371 else if (STRCMP(arg, "off") == 0)
11372 {
11373 if (plugin || indent)
11374 {
11375 if (plugin)
11376 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011377 source_runtime((char_u *)FTPLUGOF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011378 filetype_plugin = FALSE;
11379 }
11380 if (indent)
11381 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011382 source_runtime((char_u *)INDOFF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011383 filetype_indent = FALSE;
11384 }
11385 }
11386 else
11387 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011388 source_runtime((char_u *)FTOFF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011389 filetype_detect = FALSE;
11390 }
11391 }
11392 else
11393 EMSG2(_(e_invarg2), arg);
11394}
11395
11396/*
11397 * ":setfiletype {name}"
11398 */
11399 static void
11400ex_setfiletype(eap)
11401 exarg_T *eap;
11402{
11403 if (!did_filetype)
11404 set_option_value((char_u *)"filetype", 0L, eap->arg, OPT_LOCAL);
11405}
11406#endif
11407
Bram Moolenaar071d4272004-06-13 20:20:40 +000011408 static void
11409ex_digraphs(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +000011410 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011411{
11412#ifdef FEAT_DIGRAPHS
11413 if (*eap->arg != NUL)
11414 putdigraph(eap->arg);
11415 else
11416 listdigraphs();
11417#else
11418 EMSG(_("E196: No digraphs in this version"));
11419#endif
11420}
11421
11422 static void
11423ex_set(eap)
11424 exarg_T *eap;
11425{
11426 int flags = 0;
11427
11428 if (eap->cmdidx == CMD_setlocal)
11429 flags = OPT_LOCAL;
11430 else if (eap->cmdidx == CMD_setglobal)
11431 flags = OPT_GLOBAL;
11432#if defined(FEAT_EVAL) && defined(FEAT_AUTOCMD) && defined(FEAT_BROWSE)
11433 if (cmdmod.browse && flags == 0)
11434 ex_options(eap);
11435 else
11436#endif
11437 (void)do_set(eap->arg, flags);
11438}
11439
11440#ifdef FEAT_SEARCH_EXTRA
11441/*
11442 * ":nohlsearch"
11443 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011444 static void
11445ex_nohlsearch(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +000011446 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011447{
Bram Moolenaar8050efa2013-11-08 04:30:20 +010011448 SET_NO_HLSEARCH(TRUE);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +000011449 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011450}
11451
11452/*
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011453 * ":[N]match {group} {pattern}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000011454 * Sets nextcmd to the start of the next command, if any. Also called when
11455 * skipping commands to find the next command.
11456 */
11457 static void
11458ex_match(eap)
11459 exarg_T *eap;
11460{
11461 char_u *p;
Bram Moolenaar52d36c82007-08-11 14:00:30 +000011462 char_u *g = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011463 char_u *end;
11464 int c;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011465 int id;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011466
11467 if (eap->line2 <= 3)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011468 id = eap->line2;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011469 else
11470 {
11471 EMSG(e_invcmd);
11472 return;
11473 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011474
11475 /* First clear any old pattern. */
11476 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011477 match_delete(curwin, id, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011478
11479 if (ends_excmd(*eap->arg))
11480 end = eap->arg;
11481 else if ((STRNICMP(eap->arg, "none", 4) == 0
11482 && (vim_iswhite(eap->arg[4]) || ends_excmd(eap->arg[4]))))
11483 end = eap->arg + 4;
11484 else
11485 {
11486 p = skiptowhite(eap->arg);
11487 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011488 g = vim_strnsave(eap->arg, (int)(p - eap->arg));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011489 p = skipwhite(p);
11490 if (*p == NUL)
11491 {
11492 /* There must be two arguments. */
11493 EMSG2(_(e_invarg2), eap->arg);
11494 return;
11495 }
11496 end = skip_regexp(p + 1, *p, TRUE, NULL);
11497 if (!eap->skip)
11498 {
11499 if (*end != NUL && !ends_excmd(*skipwhite(end + 1)))
11500 {
11501 eap->errmsg = e_trailing;
11502 return;
11503 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000011504 if (*end != *p)
11505 {
11506 EMSG2(_(e_invarg2), p);
11507 return;
11508 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011509
11510 c = *end;
11511 *end = NUL;
Bram Moolenaarb3414592014-06-17 17:48:32 +020011512 match_add(curwin, g, p + 1, 10, id, NULL);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011513 vim_free(g);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000011514 *end = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011515 }
11516 }
11517 eap->nextcmd = find_nextcmd(end);
11518}
11519#endif
11520
11521#ifdef FEAT_CRYPT
11522/*
11523 * ":X": Get crypt key
11524 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011525 static void
11526ex_X(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +000011527 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011528{
Bram Moolenaar8f4ac012014-08-10 13:38:34 +020011529 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011530}
11531#endif
11532
11533#ifdef FEAT_FOLDING
11534 static void
11535ex_fold(eap)
11536 exarg_T *eap;
11537{
11538 if (foldManualAllowed(TRUE))
11539 foldCreate(eap->line1, eap->line2);
11540}
11541
11542 static void
11543ex_foldopen(eap)
11544 exarg_T *eap;
11545{
11546 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
11547 eap->forceit, FALSE);
11548}
11549
11550 static void
11551ex_folddo(eap)
11552 exarg_T *eap;
11553{
11554 linenr_T lnum;
11555
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020011556#ifdef FEAT_CLIPBOARD
11557 start_global_changes();
11558#endif
11559
Bram Moolenaar071d4272004-06-13 20:20:40 +000011560 /* First set the marks for all lines closed/open. */
11561 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
11562 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
11563 ml_setmarked(lnum);
11564
11565 /* Execute the command on the marked lines. */
11566 global_exe(eap->arg);
11567 ml_clearmarked(); /* clear rest of the marks */
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020011568#ifdef FEAT_CLIPBOARD
11569 end_global_changes();
11570#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011571}
11572#endif