blob: e1e5a83c4b87b9b94d33c557349dca76641e573e [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
16#ifdef HAVE_FCNTL_H
17# include <fcntl.h> /* for chdir() */
18#endif
19
20static int quitmore = 0;
21static int ex_pressedreturn = FALSE;
22#ifndef FEAT_PRINTER
23# define ex_hardcopy ex_ni
24#endif
25
26#ifdef FEAT_USR_CMDS
27typedef struct ucmd
28{
29 char_u *uc_name; /* The command name */
30 long_u uc_argt; /* The argument type */
31 char_u *uc_rep; /* The command's replacement string */
32 long uc_def; /* The default value for a range/count */
33 scid_T uc_scriptID; /* SID where the command was defined */
34 int uc_compl; /* completion type */
35# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
36 char_u *uc_compl_arg; /* completion argument if any */
37# endif
38} ucmd_T;
39
40#define UC_BUFFER 1 /* -buffer: local to current buffer */
41
Bram Moolenaar2c29bee2005-06-01 21:46:07 +000042static garray_T ucmds = {0, 0, sizeof(ucmd_T), 4, NULL};
Bram Moolenaar071d4272004-06-13 20:20:40 +000043
44#define USER_CMD(i) (&((ucmd_T *)(ucmds.ga_data))[i])
45#define USER_CMD_GA(gap, i) (&((ucmd_T *)((gap)->ga_data))[i])
46
47static void do_ucmd __ARGS((exarg_T *eap));
48static void ex_command __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +000049static void ex_delcommand __ARGS((exarg_T *eap));
50# ifdef FEAT_CMDL_COMPL
51static char_u *get_user_command_name __ARGS((int idx));
52# endif
53
54#else
55# define ex_command ex_ni
56# define ex_comclear ex_ni
57# define ex_delcommand ex_ni
58#endif
59
60#ifdef FEAT_EVAL
61static char_u *do_one_cmd __ARGS((char_u **, int, struct condstack *, char_u *(*getline)(int, void *, int), void *cookie));
62#else
63static char_u *do_one_cmd __ARGS((char_u **, int, char_u *(*getline)(int, void *, int), void *cookie));
64static int if_level = 0; /* depth in :if */
65#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000066static char_u *find_command __ARGS((exarg_T *eap, int *full));
67
68static void ex_abbreviate __ARGS((exarg_T *eap));
69static void ex_map __ARGS((exarg_T *eap));
70static void ex_unmap __ARGS((exarg_T *eap));
71static void ex_mapclear __ARGS((exarg_T *eap));
72static void ex_abclear __ARGS((exarg_T *eap));
73#ifndef FEAT_MENU
74# define ex_emenu ex_ni
75# define ex_menu ex_ni
76# define ex_menutranslate ex_ni
77#endif
78#ifdef FEAT_AUTOCMD
79static void ex_autocmd __ARGS((exarg_T *eap));
80static void ex_doautocmd __ARGS((exarg_T *eap));
81#else
82# define ex_autocmd ex_ni
83# define ex_doautocmd ex_ni
84# define ex_doautoall ex_ni
85#endif
86#ifdef FEAT_LISTCMDS
87static void ex_bunload __ARGS((exarg_T *eap));
88static void ex_buffer __ARGS((exarg_T *eap));
89static void ex_bmodified __ARGS((exarg_T *eap));
90static void ex_bnext __ARGS((exarg_T *eap));
91static void ex_bprevious __ARGS((exarg_T *eap));
92static void ex_brewind __ARGS((exarg_T *eap));
93static void ex_blast __ARGS((exarg_T *eap));
94#else
95# define ex_bunload ex_ni
96# define ex_buffer ex_ni
97# define ex_bmodified ex_ni
98# define ex_bnext ex_ni
99# define ex_bprevious ex_ni
100# define ex_brewind ex_ni
101# define ex_blast ex_ni
102# define buflist_list ex_ni
103# define ex_checktime ex_ni
104#endif
105#if !defined(FEAT_LISTCMDS) || !defined(FEAT_WINDOWS)
106# define ex_buffer_all ex_ni
107#endif
108static char_u *getargcmd __ARGS((char_u **));
109static char_u *skip_cmd_arg __ARGS((char_u *p, int rembs));
110static int getargopt __ARGS((exarg_T *eap));
111#ifndef FEAT_QUICKFIX
112# define ex_make ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +0000113# define ex_cbuffer ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000114# define ex_cc ex_ni
115# define ex_cnext ex_ni
116# define ex_cfile ex_ni
117# define qf_list ex_ni
118# define qf_age ex_ni
119# define ex_helpgrep ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +0000120# define ex_vimgrep ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000121#endif
122#if !defined(FEAT_QUICKFIX) || !defined(FEAT_WINDOWS)
123# define ex_cclose ex_ni
124# define ex_copen ex_ni
125# define ex_cwindow ex_ni
126#endif
Bram Moolenaar1e015462005-09-25 22:16:38 +0000127#if !defined(FEAT_QUICKFIX) || !defined(FEAT_EVAL)
128# define ex_cexpr ex_ni
129#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000130
131static int check_more __ARGS((int, int));
132static linenr_T get_address __ARGS((char_u **, int skip, int to_other_file));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000133static void get_flags __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000134#if !defined(FEAT_PERL) || !defined(FEAT_PYTHON) || !defined(FEAT_TCL) \
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000135 || !defined(FEAT_RUBY) || !defined(FEAT_MZSCHEME)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000136static void ex_script_ni __ARGS((exarg_T *eap));
137#endif
138static char_u *invalid_range __ARGS((exarg_T *eap));
139static void correct_range __ARGS((exarg_T *eap));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000140#ifdef FEAT_QUICKFIX
141static char_u *replace_makeprg __ARGS((exarg_T *eap, char_u *p, char_u **cmdlinep));
142#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000143static char_u *repl_cmdline __ARGS((exarg_T *eap, char_u *src, int srclen, char_u *repl, char_u **cmdlinep));
144static void ex_highlight __ARGS((exarg_T *eap));
145static void ex_colorscheme __ARGS((exarg_T *eap));
146static void ex_quit __ARGS((exarg_T *eap));
147static void ex_cquit __ARGS((exarg_T *eap));
148static void ex_quit_all __ARGS((exarg_T *eap));
149#ifdef FEAT_WINDOWS
150static void ex_close __ARGS((exarg_T *eap));
Bram Moolenaarf740b292006-02-16 22:11:02 +0000151static void ex_win_close __ARGS((int forceit, win_T *win, tabpage_T *tp));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000152static void ex_only __ARGS((exarg_T *eap));
153static void ex_all __ARGS((exarg_T *eap));
154static void ex_resize __ARGS((exarg_T *eap));
155static void ex_stag __ARGS((exarg_T *eap));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000156static void ex_tabclose __ARGS((exarg_T *eap));
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000157static void ex_tabonly __ARGS((exarg_T *eap));
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000158static void ex_tabnext __ARGS((exarg_T *eap));
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000159static void ex_tabmove __ARGS((exarg_T *eap));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000160static void ex_tabs __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000161#else
162# define ex_close ex_ni
163# define ex_only ex_ni
164# define ex_all ex_ni
165# define ex_resize ex_ni
166# define ex_splitview ex_ni
167# define ex_stag ex_ni
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000168# define ex_tabnext ex_ni
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000169# define ex_tabmove ex_ni
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000170# define ex_tabs ex_ni
171# define ex_tabclose ex_ni
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000172# define ex_tabonly ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000173#endif
174#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
175static void ex_pclose __ARGS((exarg_T *eap));
176static void ex_ptag __ARGS((exarg_T *eap));
177static void ex_pedit __ARGS((exarg_T *eap));
178#else
179# define ex_pclose ex_ni
180# define ex_ptag ex_ni
181# define ex_pedit ex_ni
182#endif
183static void ex_hide __ARGS((exarg_T *eap));
184static void ex_stop __ARGS((exarg_T *eap));
185static void ex_exit __ARGS((exarg_T *eap));
186static void ex_print __ARGS((exarg_T *eap));
187#ifdef FEAT_BYTEOFF
188static void ex_goto __ARGS((exarg_T *eap));
189#else
190# define ex_goto ex_ni
191#endif
192static void ex_shell __ARGS((exarg_T *eap));
193static void ex_preserve __ARGS((exarg_T *eap));
194static void ex_recover __ARGS((exarg_T *eap));
195#ifndef FEAT_LISTCMDS
196# define ex_argedit ex_ni
197# define ex_argadd ex_ni
198# define ex_argdelete ex_ni
199# define ex_listdo ex_ni
200#endif
201static void ex_mode __ARGS((exarg_T *eap));
202static void ex_wrongmodifier __ARGS((exarg_T *eap));
203static void ex_find __ARGS((exarg_T *eap));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000204static void ex_open __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000205static void ex_edit __ARGS((exarg_T *eap));
206#if !defined(FEAT_GUI) && !defined(FEAT_CLIENTSERVER)
207# define ex_drop ex_ni
208#endif
209#ifndef FEAT_GUI
210# define ex_gui ex_nogui
211static void ex_nogui __ARGS((exarg_T *eap));
212#endif
213#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
214static void ex_tearoff __ARGS((exarg_T *eap));
215#else
216# define ex_tearoff ex_ni
217#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000218#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000219static void ex_popup __ARGS((exarg_T *eap));
220#else
221# define ex_popup ex_ni
222#endif
223#ifndef FEAT_GUI_MSWIN
224# define ex_simalt ex_ni
225#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000226#if !defined(FEAT_GUI_MSWIN) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000227# define gui_mch_find_dialog ex_ni
228# define gui_mch_replace_dialog ex_ni
229#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000230#if !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000231# define ex_helpfind ex_ni
232#endif
233#ifndef FEAT_CSCOPE
234# define do_cscope ex_ni
235# define do_scscope ex_ni
236# define do_cstag ex_ni
237#endif
238#ifndef FEAT_SYN_HL
239# define ex_syntax ex_ni
Bram Moolenaarb765d632005-06-07 21:00:02 +0000240# define ex_spell ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000241# define ex_mkspell ex_ni
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000242# define ex_spelldump ex_ni
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000243# define ex_spellinfo ex_ni
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000244# define ex_spellrepall ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000245#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000246#ifndef FEAT_MZSCHEME
247# define ex_mzscheme ex_script_ni
248# define ex_mzfile ex_ni
249#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000250#ifndef FEAT_PERL
251# define ex_perl ex_script_ni
252# define ex_perldo ex_ni
253#endif
254#ifndef FEAT_PYTHON
255# define ex_python ex_script_ni
256# define ex_pyfile ex_ni
257#endif
258#ifndef FEAT_TCL
259# define ex_tcl ex_script_ni
260# define ex_tcldo ex_ni
261# define ex_tclfile ex_ni
262#endif
263#ifndef FEAT_RUBY
264# define ex_ruby ex_script_ni
265# define ex_rubydo ex_ni
266# define ex_rubyfile ex_ni
267#endif
268#ifndef FEAT_SNIFF
269# define ex_sniff ex_ni
270#endif
271#ifndef FEAT_KEYMAP
272# define ex_loadkeymap ex_ni
273#endif
274static void ex_swapname __ARGS((exarg_T *eap));
275static void ex_syncbind __ARGS((exarg_T *eap));
276static void ex_read __ARGS((exarg_T *eap));
277static void ex_cd __ARGS((exarg_T *eap));
278static void ex_pwd __ARGS((exarg_T *eap));
279static void ex_equal __ARGS((exarg_T *eap));
280static void ex_sleep __ARGS((exarg_T *eap));
281static void do_exmap __ARGS((exarg_T *eap, int isabbrev));
282static void ex_winsize __ARGS((exarg_T *eap));
283#ifdef FEAT_WINDOWS
284static void ex_wincmd __ARGS((exarg_T *eap));
285#else
286# define ex_wincmd ex_ni
287#endif
Bram Moolenaar843ee412004-06-30 16:16:41 +0000288#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000289static void ex_winpos __ARGS((exarg_T *eap));
290#else
291# define ex_winpos ex_ni
292#endif
293static void ex_operators __ARGS((exarg_T *eap));
294static void ex_put __ARGS((exarg_T *eap));
295static void ex_copymove __ARGS((exarg_T *eap));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000296static void ex_may_print __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000297static void ex_submagic __ARGS((exarg_T *eap));
298static void ex_join __ARGS((exarg_T *eap));
299static void ex_at __ARGS((exarg_T *eap));
300static void ex_bang __ARGS((exarg_T *eap));
301static void ex_undo __ARGS((exarg_T *eap));
302static void ex_redo __ARGS((exarg_T *eap));
303static void ex_redir __ARGS((exarg_T *eap));
304static void ex_redraw __ARGS((exarg_T *eap));
305static void ex_redrawstatus __ARGS((exarg_T *eap));
306static void close_redir __ARGS((void));
307static void ex_mkrc __ARGS((exarg_T *eap));
308static void ex_mark __ARGS((exarg_T *eap));
309#ifdef FEAT_USR_CMDS
310static char_u *uc_fun_cmd __ARGS((void));
Bram Moolenaar52b4b552005-03-07 23:00:57 +0000311static 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 +0000312#endif
313#ifdef FEAT_EX_EXTRA
314static void ex_normal __ARGS((exarg_T *eap));
315static void ex_startinsert __ARGS((exarg_T *eap));
316static void ex_stopinsert __ARGS((exarg_T *eap));
317#else
318# define ex_normal ex_ni
319# define ex_align ex_ni
320# define ex_retab ex_ni
321# define ex_startinsert ex_ni
322# define ex_stopinsert ex_ni
323# define ex_helptags ex_ni
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +0000324# define ex_sort ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000325#endif
326#ifdef FEAT_FIND_ID
327static void ex_checkpath __ARGS((exarg_T *eap));
328static void ex_findpat __ARGS((exarg_T *eap));
329#else
330# define ex_findpat ex_ni
331# define ex_checkpath ex_ni
332#endif
333#if defined(FEAT_FIND_ID) && defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
334static void ex_psearch __ARGS((exarg_T *eap));
335#else
336# define ex_psearch ex_ni
337#endif
338static void ex_tag __ARGS((exarg_T *eap));
339static void ex_tag_cmd __ARGS((exarg_T *eap, char_u *name));
340#ifndef FEAT_EVAL
341# define ex_scriptnames ex_ni
342# define ex_finish ex_ni
343# define ex_echo ex_ni
344# define ex_echohl ex_ni
345# define ex_execute ex_ni
346# define ex_call ex_ni
347# define ex_if ex_ni
348# define ex_endif ex_ni
349# define ex_else ex_ni
350# define ex_while ex_ni
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000351# define ex_for ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000352# define ex_continue ex_ni
353# define ex_break ex_ni
354# define ex_endwhile ex_ni
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000355# define ex_endfor ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000356# define ex_throw ex_ni
357# define ex_try ex_ni
358# define ex_catch ex_ni
359# define ex_finally ex_ni
360# define ex_endtry ex_ni
361# define ex_endfunction ex_ni
362# define ex_let ex_ni
363# define ex_unlet ex_ni
Bram Moolenaar65c1b012005-01-31 19:02:28 +0000364# define ex_lockvar ex_ni
365# define ex_unlockvar ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000366# define ex_function ex_ni
367# define ex_delfunction ex_ni
368# define ex_return ex_ni
369#endif
370static char_u *arg_all __ARGS((void));
371#ifdef FEAT_SESSION
372static int makeopens __ARGS((FILE *fd, char_u *dirnow));
373static int put_view __ARGS((FILE *fd, win_T *wp, int add_edit, unsigned *flagp));
374static void ex_loadview __ARGS((exarg_T *eap));
375static char_u *get_view_file __ARGS((int c));
376#else
377# define ex_loadview ex_ni
378#endif
379#ifndef FEAT_EVAL
380# define ex_compiler ex_ni
381#endif
382#ifdef FEAT_VIMINFO
383static void ex_viminfo __ARGS((exarg_T *eap));
384#else
385# define ex_viminfo ex_ni
386#endif
387static void ex_behave __ARGS((exarg_T *eap));
388#ifdef FEAT_AUTOCMD
389static void ex_filetype __ARGS((exarg_T *eap));
390static void ex_setfiletype __ARGS((exarg_T *eap));
391#else
392# define ex_filetype ex_ni
393# define ex_setfiletype ex_ni
394#endif
395#ifndef FEAT_DIFF
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000396# define ex_diffoff ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000397# define ex_diffpatch ex_ni
398# define ex_diffgetput ex_ni
399# define ex_diffsplit ex_ni
400# define ex_diffthis ex_ni
401# define ex_diffupdate ex_ni
402#endif
403static void ex_digraphs __ARGS((exarg_T *eap));
404static void ex_set __ARGS((exarg_T *eap));
405#if !defined(FEAT_EVAL) || !defined(FEAT_AUTOCMD)
406# define ex_options ex_ni
407#endif
408#ifdef FEAT_SEARCH_EXTRA
409static void ex_nohlsearch __ARGS((exarg_T *eap));
410static void ex_match __ARGS((exarg_T *eap));
411#else
412# define ex_nohlsearch ex_ni
413# define ex_match ex_ni
414#endif
415#ifdef FEAT_CRYPT
416static void ex_X __ARGS((exarg_T *eap));
417#else
418# define ex_X ex_ni
419#endif
420#ifdef FEAT_FOLDING
421static void ex_fold __ARGS((exarg_T *eap));
422static void ex_foldopen __ARGS((exarg_T *eap));
423static void ex_folddo __ARGS((exarg_T *eap));
424#else
425# define ex_fold ex_ni
426# define ex_foldopen ex_ni
427# define ex_folddo ex_ni
428#endif
429#if !((defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
430 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE)))
431# define ex_language ex_ni
432#endif
433#ifndef FEAT_SIGNS
434# define ex_sign ex_ni
435#endif
436#ifndef FEAT_SUN_WORKSHOP
437# define ex_wsverb ex_ni
438#endif
Bram Moolenaar009b2592004-10-24 19:18:58 +0000439#ifndef FEAT_NETBEANS_INTG
440# define ex_nbkey ex_ni
441#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000442
443#ifndef FEAT_EVAL
444# define ex_debug ex_ni
445# define ex_breakadd ex_ni
446# define ex_debuggreedy ex_ni
447# define ex_breakdel ex_ni
448# define ex_breaklist ex_ni
449#endif
450
451#ifndef FEAT_CMDHIST
452# define ex_history ex_ni
453#endif
454#ifndef FEAT_JUMPLIST
455# define ex_jumps ex_ni
456# define ex_changes ex_ni
457#endif
458
Bram Moolenaar05159a02005-02-26 23:04:13 +0000459#ifndef FEAT_PROFILE
460# define ex_profile ex_ni
461#endif
462
Bram Moolenaar071d4272004-06-13 20:20:40 +0000463/*
464 * Declare cmdnames[].
465 */
466#define DO_DECLARE_EXCMD
467#include "ex_cmds.h"
468
469/*
470 * Table used to quickly search for a command, based on its first character.
471 */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +0000472static cmdidx_T cmdidxs[27] =
Bram Moolenaar071d4272004-06-13 20:20:40 +0000473{
474 CMD_append,
475 CMD_buffer,
476 CMD_change,
477 CMD_delete,
478 CMD_edit,
479 CMD_file,
480 CMD_global,
481 CMD_help,
482 CMD_insert,
483 CMD_join,
484 CMD_k,
485 CMD_list,
486 CMD_move,
487 CMD_next,
488 CMD_open,
489 CMD_print,
490 CMD_quit,
491 CMD_read,
492 CMD_substitute,
493 CMD_t,
494 CMD_undo,
495 CMD_vglobal,
496 CMD_write,
497 CMD_xit,
498 CMD_yank,
499 CMD_z,
500 CMD_bang
501};
502
503static char_u dollar_command[2] = {'$', 0};
504
505
506#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000507/* Struct for storing a line inside a while/for loop */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000508typedef struct
509{
510 char_u *line; /* command line */
511 linenr_T lnum; /* sourcing_lnum of the line */
512} wcmd_T;
513
514/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000515 * Structure used to store info for line position in a while or for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000516 * This is required, because do_one_cmd() may invoke ex_function(), which
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000517 * reads more lines that may come from the while/for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000518 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000519struct loop_cookie
Bram Moolenaar071d4272004-06-13 20:20:40 +0000520{
521 garray_T *lines_gap; /* growarray with line info */
522 int current_line; /* last read line from growarray */
523 int repeating; /* TRUE when looping a second time */
524 /* When "repeating" is FALSE use "getline" and "cookie" to get lines */
525 char_u *(*getline) __ARGS((int, void *, int));
526 void *cookie;
527};
528
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000529static char_u *get_loop_line __ARGS((int c, void *cookie, int indent));
530static int store_loop_line __ARGS((garray_T *gap, char_u *line));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000531static void free_cmdlines __ARGS((garray_T *gap));
Bram Moolenaared203462004-06-16 11:19:22 +0000532
533/* Struct to save a few things while debugging. Used in do_cmdline() only. */
534struct dbg_stuff
535{
536 int trylevel;
537 int force_abort;
538 except_T *caught_stack;
539 char_u *vv_exception;
540 char_u *vv_throwpoint;
541 int did_emsg;
542 int got_int;
543 int did_throw;
544 int need_rethrow;
545 int check_cstack;
546 except_T *current_exception;
547};
548
549static void save_dbg_stuff __ARGS((struct dbg_stuff *dsp));
550static void restore_dbg_stuff __ARGS((struct dbg_stuff *dsp));
551
552 static void
553save_dbg_stuff(dsp)
554 struct dbg_stuff *dsp;
555{
556 dsp->trylevel = trylevel; trylevel = 0;
557 dsp->force_abort = force_abort; force_abort = FALSE;
558 dsp->caught_stack = caught_stack; caught_stack = NULL;
559 dsp->vv_exception = v_exception(NULL);
560 dsp->vv_throwpoint = v_throwpoint(NULL);
561
562 /* Necessary for debugging an inactive ":catch", ":finally", ":endtry" */
563 dsp->did_emsg = did_emsg; did_emsg = FALSE;
564 dsp->got_int = got_int; got_int = FALSE;
565 dsp->did_throw = did_throw; did_throw = FALSE;
566 dsp->need_rethrow = need_rethrow; need_rethrow = FALSE;
567 dsp->check_cstack = check_cstack; check_cstack = FALSE;
568 dsp->current_exception = current_exception; current_exception = NULL;
569}
570
571 static void
572restore_dbg_stuff(dsp)
573 struct dbg_stuff *dsp;
574{
575 suppress_errthrow = FALSE;
576 trylevel = dsp->trylevel;
577 force_abort = dsp->force_abort;
578 caught_stack = dsp->caught_stack;
579 (void)v_exception(dsp->vv_exception);
580 (void)v_throwpoint(dsp->vv_throwpoint);
581 did_emsg = dsp->did_emsg;
582 got_int = dsp->got_int;
583 did_throw = dsp->did_throw;
584 need_rethrow = dsp->need_rethrow;
585 check_cstack = dsp->check_cstack;
586 current_exception = dsp->current_exception;
587}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000588#endif
589
590
591/*
592 * do_exmode(): Repeatedly get commands for the "Ex" mode, until the ":vi"
593 * command is given.
594 */
595 void
596do_exmode(improved)
597 int improved; /* TRUE for "improved Ex" mode */
598{
599 int save_msg_scroll;
600 int prev_msg_row;
601 linenr_T prev_line;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000602 int changedtick;
603
604 if (improved)
605 exmode_active = EXMODE_VIM;
606 else
607 exmode_active = EXMODE_NORMAL;
608 State = NORMAL;
609
610 /* When using ":global /pat/ visual" and then "Q" we return to continue
611 * the :global command. */
612 if (global_busy)
613 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000614
615 save_msg_scroll = msg_scroll;
616 ++RedrawingDisabled; /* don't redisplay the window */
617 ++no_wait_return; /* don't wait for return */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000618#ifdef FEAT_GUI
619 /* Ignore scrollbar and mouse events in Ex mode */
620 ++hold_gui_events;
621#endif
622#ifdef FEAT_SNIFF
623 want_sniff_request = 0; /* No K_SNIFF wanted */
624#endif
625
626 MSG(_("Entering Ex mode. Type \"visual\" to go to Normal mode."));
627 while (exmode_active)
628 {
Bram Moolenaar7c626922005-02-07 22:01:03 +0000629#ifdef FEAT_EX_EXTRA
630 /* Check for a ":normal" command and no more characters left. */
631 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
632 {
633 exmode_active = FALSE;
634 break;
635 }
636#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000637 msg_scroll = TRUE;
638 need_wait_return = FALSE;
639 ex_pressedreturn = FALSE;
640 ex_no_reprint = FALSE;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000641 changedtick = curbuf->b_changedtick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642 prev_msg_row = msg_row;
643 prev_line = curwin->w_cursor.lnum;
644#ifdef FEAT_SNIFF
645 ProcessSniffRequests();
646#endif
647 if (improved)
648 {
649 cmdline_row = msg_row;
650 do_cmdline(NULL, getexline, NULL, 0);
651 }
652 else
653 do_cmdline(NULL, getexmodeline, NULL, DOCMD_NOWAIT);
654 lines_left = Rows - 1;
655
Bram Moolenaardf177f62005-02-22 08:39:57 +0000656 if ((prev_line != curwin->w_cursor.lnum
657 || changedtick != curbuf->b_changedtick) && !ex_no_reprint)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000658 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000659 if (curbuf->b_ml.ml_flags & ML_EMPTY)
660 EMSG(_(e_emptybuf));
661 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000662 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000663 if (ex_pressedreturn)
664 {
665 /* go up one line, to overwrite the ":<CR>" line, so the
666 * output doensn't contain empty lines. */
667 msg_row = prev_msg_row;
668 if (prev_msg_row == Rows - 1)
669 msg_row--;
670 }
671 msg_col = 0;
672 print_line_no_prefix(curwin->w_cursor.lnum, FALSE, FALSE);
673 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000674 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675 }
Bram Moolenaardf177f62005-02-22 08:39:57 +0000676 else if (ex_pressedreturn && !ex_no_reprint) /* must be at EOF */
677 {
678 if (curbuf->b_ml.ml_flags & ML_EMPTY)
679 EMSG(_(e_emptybuf));
680 else
681 EMSG(_("E501: At end-of-file"));
682 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000683 }
684
685#ifdef FEAT_GUI
686 --hold_gui_events;
687#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688 --RedrawingDisabled;
689 --no_wait_return;
690 update_screen(CLEAR);
691 need_wait_return = FALSE;
692 msg_scroll = save_msg_scroll;
693}
694
695/*
696 * Execute a simple command line. Used for translated commands like "*".
697 */
698 int
699do_cmdline_cmd(cmd)
700 char_u *cmd;
701{
702 return do_cmdline(cmd, NULL, NULL,
703 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
704}
705
706/*
707 * do_cmdline(): execute one Ex command line
708 *
709 * 1. Execute "cmdline" when it is not NULL.
710 * If "cmdline" is NULL, or more lines are needed, getline() is used.
711 * 2. Split up in parts separated with '|'.
712 *
713 * This function can be called recursively!
714 *
715 * flags:
716 * DOCMD_VERBOSE - The command will be included in the error message.
717 * DOCMD_NOWAIT - Don't call wait_return() and friends.
718 * DOCMD_REPEAT - Repeat execution until getline() returns NULL.
719 * DOCMD_KEYTYPED - Don't reset KeyTyped.
720 * DOCMD_EXCRESET - Reset the exception environment (used for debugging).
721 * DOCMD_KEEPLINE - Store first typed line (for repeating with ".").
722 *
723 * return FAIL if cmdline could not be executed, OK otherwise
724 */
725 int
726do_cmdline(cmdline, getline, cookie, flags)
727 char_u *cmdline;
728 char_u *(*getline) __ARGS((int, void *, int));
729 void *cookie; /* argument for getline() */
730 int flags;
731{
732 char_u *next_cmdline; /* next cmd to execute */
733 char_u *cmdline_copy = NULL; /* copy of cmd line */
734 int used_getline = FALSE; /* used "getline" to obtain command */
735 static int recursive = 0; /* recursive depth */
736 int msg_didout_before_start = 0;
737 int count = 0; /* line number count */
738 int did_inc = FALSE; /* incremented RedrawingDisabled */
739 int retval = OK;
740#ifdef FEAT_EVAL
741 struct condstack cstack; /* conditional stack */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000742 garray_T lines_ga; /* keep lines for ":while"/":for" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000743 int current_line = 0; /* active line in lines_ga */
744 char_u *fname = NULL; /* function or script name */
745 linenr_T *breakpoint = NULL; /* ptr to breakpoint field in cookie */
746 int *dbg_tick = NULL; /* ptr to dbg_tick field in cookie */
Bram Moolenaared203462004-06-16 11:19:22 +0000747 struct dbg_stuff debug_saved; /* saved things for debug mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000748 int initial_trylevel;
749 struct msglist **saved_msg_list = NULL;
750 struct msglist *private_msg_list;
751
752 /* "getline" and "cookie" passed to do_one_cmd() */
753 char_u *(*cmd_getline) __ARGS((int, void *, int));
754 void *cmd_cookie;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000755 struct loop_cookie cmd_loop_cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000756 void *real_cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000757 int getline_is_func;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000758#else
759# define cmd_getline getline
760# define cmd_cookie cookie
761#endif
762 static int call_depth = 0; /* recursiveness */
763
764#ifdef FEAT_EVAL
765 /* For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory
766 * location for storing error messages to be converted to an exception.
Bram Moolenaarcf3630f2005-01-08 16:04:29 +0000767 * This ensures that the do_errthrow() call in do_one_cmd() does not
768 * combine the messages stored by an earlier invocation of do_one_cmd()
769 * with the command name of the later one. This would happen when
770 * BufWritePost autocommands are executed after a write error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771 saved_msg_list = msg_list;
772 msg_list = &private_msg_list;
773 private_msg_list = NULL;
774#endif
775
776 /* It's possible to create an endless loop with ":execute", catch that
777 * here. The value of 200 allows nested function calls, ":source", etc. */
778 if (call_depth == 200)
779 {
780 EMSG(_("E169: Command too recursive"));
781#ifdef FEAT_EVAL
782 /* When converting to an exception, we do not include the command name
783 * since this is not an error of the specific command. */
784 do_errthrow((struct condstack *)NULL, (char_u *)NULL);
785 msg_list = saved_msg_list;
786#endif
787 return FAIL;
788 }
789 ++call_depth;
790
791#ifdef FEAT_EVAL
792 cstack.cs_idx = -1;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000793 cstack.cs_looplevel = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794 cstack.cs_trylevel = 0;
795 cstack.cs_emsg_silent_list = NULL;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000796 cstack.cs_lflags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000797 ga_init2(&lines_ga, (int)sizeof(wcmd_T), 10);
798
799 real_cookie = getline_cookie(getline, cookie);
800
801 /* Inside a function use a higher nesting level. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000802 getline_is_func = getline_equal(getline, cookie, get_func_line);
803 if (getline_is_func && ex_nesting_level == func_level(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804 ++ex_nesting_level;
805
806 /* Get the function or script name and the address where the next breakpoint
807 * line and the debug tick for a function or script are stored. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000808 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000809 {
810 fname = func_name(real_cookie);
811 breakpoint = func_breakpoint(real_cookie);
812 dbg_tick = func_dbg_tick(real_cookie);
813 }
814 else if (getline_equal(getline, cookie, getsourceline))
815 {
816 fname = sourcing_name;
817 breakpoint = source_breakpoint(real_cookie);
818 dbg_tick = source_dbg_tick(real_cookie);
819 }
820
821 /*
822 * Initialize "force_abort" and "suppress_errthrow" at the top level.
823 */
824 if (!recursive)
825 {
826 force_abort = FALSE;
827 suppress_errthrow = FALSE;
828 }
829
830 /*
831 * If requested, store and reset the global values controlling the
832 * exception handling (used when debugging).
833 */
834 else if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +0000835 save_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000836
837 initial_trylevel = trylevel;
838
839 /*
840 * "did_throw" will be set to TRUE when an exception is being thrown.
841 */
842 did_throw = FALSE;
843#endif
844 /*
845 * "did_emsg" will be set to TRUE when emsg() is used, in which case we
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000846 * cancel the whole command line, and any if/endif or loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000847 * If force_abort is set, we cancel everything.
848 */
849 did_emsg = FALSE;
850
851 /*
852 * KeyTyped is only set when calling vgetc(). Reset it here when not
853 * calling vgetc() (sourced command lines).
854 */
855 if (!(flags & DOCMD_KEYTYPED) && !getline_equal(getline, cookie, getexline))
856 KeyTyped = FALSE;
857
858 /*
859 * Continue executing command lines:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000860 * - when inside an ":if", ":while" or ":for"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000861 * - for multiple commands on one line, separated with '|'
862 * - when repeating until there are no more lines (for ":source")
863 */
864 next_cmdline = cmdline;
865 do
866 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000867#ifdef FEAT_EVAL
868 getline_is_func = getline_equal(getline, cookie, get_func_line);
869#endif
870
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000871 /* stop skipping cmds for an error msg after all endif/while/for */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000872 if (next_cmdline == NULL
873#ifdef FEAT_EVAL
874 && !force_abort
875 && cstack.cs_idx < 0
Bram Moolenaar05159a02005-02-26 23:04:13 +0000876 && !(getline_is_func && func_has_abort(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000877#endif
878 )
879 did_emsg = FALSE;
880
881 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000882 * 1. If repeating a line in a loop, get a line from lines_ga.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883 * 2. If no line given: Get an allocated line with getline().
884 * 3. If a line is given: Make a copy, so we can mess with it.
885 */
886
887#ifdef FEAT_EVAL
888 /* 1. If repeating, get a previous line from lines_ga. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000889 if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000890 {
891 /* Each '|' separated command is stored separately in lines_ga, to
892 * be able to jump to it. Don't use next_cmdline now. */
893 vim_free(cmdline_copy);
894 cmdline_copy = NULL;
895
896 /* Check if a function has returned or, unless it has an unclosed
897 * try conditional, aborted. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000898 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000900# ifdef FEAT_PROFILE
901 if (do_profiling)
902 func_line_end(real_cookie);
903# endif
904 if (func_has_ended(real_cookie))
905 {
906 retval = FAIL;
907 break;
908 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000909 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000910#ifdef FEAT_PROFILE
911 else if (do_profiling
912 && getline_equal(getline, cookie, getsourceline))
913 script_line_end();
914#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000915
916 /* Check if a sourced file hit a ":finish" command. */
917 if (source_finished(getline, cookie))
918 {
919 retval = FAIL;
920 break;
921 }
922
923 /* If breakpoints have been added/deleted need to check for it. */
924 if (breakpoint != NULL && dbg_tick != NULL
925 && *dbg_tick != debug_tick)
926 {
927 *breakpoint = dbg_find_breakpoint(
928 getline_equal(getline, cookie, getsourceline),
929 fname, sourcing_lnum);
930 *dbg_tick = debug_tick;
931 }
932
933 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
934 sourcing_lnum = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
935
936 /* Did we encounter a breakpoint? */
937 if (breakpoint != NULL && *breakpoint != 0
938 && *breakpoint <= sourcing_lnum)
939 {
940 dbg_breakpoint(fname, sourcing_lnum);
941 /* Find next breakpoint. */
942 *breakpoint = dbg_find_breakpoint(
943 getline_equal(getline, cookie, getsourceline),
944 fname, sourcing_lnum);
945 *dbg_tick = debug_tick;
946 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000947# ifdef FEAT_PROFILE
948 if (do_profiling)
949 {
950 if (getline_is_func)
951 func_line_start(real_cookie);
952 else if (getline_equal(getline, cookie, getsourceline))
953 script_line_start();
954 }
955# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956 }
957
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000958 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000959 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000960 /* Inside a while/for loop we need to store the lines and use them
Bram Moolenaar071d4272004-06-13 20:20:40 +0000961 * again. Pass a different "getline" function to do_one_cmd()
962 * below, so that it stores lines in or reads them from
963 * "lines_ga". Makes it possible to define a function inside a
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000964 * while/for loop. */
965 cmd_getline = get_loop_line;
966 cmd_cookie = (void *)&cmd_loop_cookie;
967 cmd_loop_cookie.lines_gap = &lines_ga;
968 cmd_loop_cookie.current_line = current_line;
969 cmd_loop_cookie.getline = getline;
970 cmd_loop_cookie.cookie = cookie;
971 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000972 }
973 else
974 {
975 cmd_getline = getline;
976 cmd_cookie = cookie;
977 }
978#endif
979
980 /* 2. If no line given, get an allocated line with getline(). */
981 if (next_cmdline == NULL)
982 {
983 /*
984 * Need to set msg_didout for the first line after an ":if",
985 * otherwise the ":if" will be overwritten.
986 */
987 if (count == 1 && getline_equal(getline, cookie, getexline))
988 msg_didout = TRUE;
989 if (getline == NULL || (next_cmdline = getline(':', cookie,
990#ifdef FEAT_EVAL
991 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
992#else
993 0
994#endif
995 )) == NULL)
996 {
997 /* Don't call wait_return for aborted command line. The NULL
998 * returned for the end of a sourced file or executed function
999 * doesn't do this. */
1000 if (KeyTyped && !(flags & DOCMD_REPEAT))
1001 need_wait_return = FALSE;
1002 retval = FAIL;
1003 break;
1004 }
1005 used_getline = TRUE;
1006
1007 /*
1008 * Keep the first typed line. Clear it when more lines are typed.
1009 */
1010 if (flags & DOCMD_KEEPLINE)
1011 {
1012 vim_free(repeat_cmdline);
1013 if (count == 0)
1014 repeat_cmdline = vim_strsave(next_cmdline);
1015 else
1016 repeat_cmdline = NULL;
1017 }
1018 }
1019
1020 /* 3. Make a copy of the command so we can mess with it. */
1021 else if (cmdline_copy == NULL)
1022 {
1023 next_cmdline = vim_strsave(next_cmdline);
1024 if (next_cmdline == NULL)
1025 {
1026 EMSG(_(e_outofmem));
1027 retval = FAIL;
1028 break;
1029 }
1030 }
1031 cmdline_copy = next_cmdline;
1032
1033#ifdef FEAT_EVAL
1034 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001035 * Save the current line when inside a ":while" or ":for", and when
1036 * the command looks like a ":while" or ":for", because we may need it
1037 * later. When there is a '|' and another command, it is stored
1038 * separately, because we need to be able to jump back to it from an
1039 * :endwhile/:endfor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001041 if (current_line == lines_ga.ga_len
1042 && (cstack.cs_looplevel || has_loop_cmd(next_cmdline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001044 if (store_loop_line(&lines_ga, next_cmdline) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045 {
1046 retval = FAIL;
1047 break;
1048 }
1049 }
1050 did_endif = FALSE;
1051#endif
1052
1053 if (count++ == 0)
1054 {
1055 /*
1056 * All output from the commands is put below each other, without
1057 * waiting for a return. Don't do this when executing commands
1058 * from a script or when being called recursive (e.g. for ":e
1059 * +command file").
1060 */
1061 if (!(flags & DOCMD_NOWAIT) && !recursive)
1062 {
1063 msg_didout_before_start = msg_didout;
1064 msg_didany = FALSE; /* no output yet */
1065 msg_start();
1066 msg_scroll = TRUE; /* put messages below each other */
1067 ++no_wait_return; /* dont wait for return until finished */
1068 ++RedrawingDisabled;
1069 did_inc = TRUE;
1070 }
1071 }
1072
1073 if (p_verbose >= 15 && sourcing_name != NULL)
1074 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001075 ++no_wait_return;
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001076 verbose_enter_scroll();
1077
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078 smsg((char_u *)_("line %ld: %s"),
1079 (long)sourcing_lnum, cmdline_copy);
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001080 if (msg_silent == 0)
1081 msg_puts((char_u *)"\n"); /* don't overwrite this */
1082
1083 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084 --no_wait_return;
1085 }
1086
1087 /*
1088 * 2. Execute one '|' separated command.
1089 * do_one_cmd() will return NULL if there is no trailing '|'.
1090 * "cmdline_copy" can change, e.g. for '%' and '#' expansion.
1091 */
1092 ++recursive;
1093 next_cmdline = do_one_cmd(&cmdline_copy, flags & DOCMD_VERBOSE,
1094#ifdef FEAT_EVAL
1095 &cstack,
1096#endif
1097 cmd_getline, cmd_cookie);
1098 --recursive;
1099
1100#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001101 if (cmd_cookie == (void *)&cmd_loop_cookie)
1102 /* Use "current_line" from "cmd_loop_cookie", it may have been
Bram Moolenaar071d4272004-06-13 20:20:40 +00001103 * incremented when defining a function. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001104 current_line = cmd_loop_cookie.current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105#endif
1106
1107 if (next_cmdline == NULL)
1108 {
1109 vim_free(cmdline_copy);
1110 cmdline_copy = NULL;
1111#ifdef FEAT_CMDHIST
1112 /*
1113 * If the command was typed, remember it for the ':' register.
1114 * Do this AFTER executing the command to make :@: work.
1115 */
1116 if (getline_equal(getline, cookie, getexline)
1117 && new_last_cmdline != NULL)
1118 {
1119 vim_free(last_cmdline);
1120 last_cmdline = new_last_cmdline;
1121 new_last_cmdline = NULL;
1122 }
1123#endif
1124 }
1125 else
1126 {
1127 /* need to copy the command after the '|' to cmdline_copy, for the
1128 * next do_one_cmd() */
1129 mch_memmove(cmdline_copy, next_cmdline, STRLEN(next_cmdline) + 1);
1130 next_cmdline = cmdline_copy;
1131 }
1132
1133
1134#ifdef FEAT_EVAL
1135 /* reset did_emsg for a function that is not aborted by an error */
1136 if (did_emsg && !force_abort
1137 && getline_equal(getline, cookie, get_func_line)
1138 && !func_has_abort(real_cookie))
1139 did_emsg = FALSE;
1140
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001141 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142 {
1143 ++current_line;
1144
1145 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001146 * An ":endwhile", ":endfor" and ":continue" is handled here.
1147 * If we were executing commands, jump back to the ":while" or
1148 * ":for".
1149 * If we were not executing commands, decrement cs_looplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001151 if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001153 cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001155 /* Jump back to the matching ":while" or ":for". Be careful
1156 * not to use a cs_line[] from an entry that isn't a ":while"
1157 * or ":for": It would make "current_line" invalid and can
1158 * cause a crash. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159 if (!did_emsg && !got_int && !did_throw
1160 && cstack.cs_idx >= 0
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001161 && (cstack.cs_flags[cstack.cs_idx]
1162 & (CSF_WHILE | CSF_FOR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001163 && cstack.cs_line[cstack.cs_idx] >= 0
1164 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
1165 {
1166 current_line = cstack.cs_line[cstack.cs_idx];
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001167 /* remember we jumped there */
1168 cstack.cs_lflags |= CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169 line_breakcheck(); /* check if CTRL-C typed */
1170
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001171 /* Check for the next breakpoint at or after the ":while"
1172 * or ":for". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173 if (breakpoint != NULL)
1174 {
1175 *breakpoint = dbg_find_breakpoint(
1176 getline_equal(getline, cookie, getsourceline),
1177 fname,
1178 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
1179 *dbg_tick = debug_tick;
1180 }
1181 }
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001182 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001184 /* can only get here with ":endwhile" or ":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185 if (cstack.cs_idx >= 0)
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001186 rewind_conditionals(&cstack, cstack.cs_idx - 1,
1187 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188 }
1189 }
1190
1191 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001192 * For a ":while" or ":for" we need to remember the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001194 else if (cstack.cs_lflags & CSL_HAD_LOOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001196 cstack.cs_lflags &= ~CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197 cstack.cs_line[cstack.cs_idx] = current_line - 1;
1198 }
1199 }
1200
1201 /*
1202 * When not inside any ":while" loop, clear remembered lines.
1203 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001204 if (cstack.cs_looplevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205 {
1206 if (lines_ga.ga_len > 0)
1207 {
1208 sourcing_lnum =
1209 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
1210 free_cmdlines(&lines_ga);
1211 }
1212 current_line = 0;
1213 }
1214
1215 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001216 * A ":finally" makes did_emsg, got_int, and did_throw pending for
1217 * being restored at the ":endtry". Reset them here and set the
1218 * ACTIVE and FINALLY flags, so that the finally clause gets executed.
1219 * This includes the case where a missing ":endif", ":endwhile" or
1220 * ":endfor" was detected by the ":finally" itself.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001222 if (cstack.cs_lflags & CSL_HAD_FINA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001224 cstack.cs_lflags &= ~CSL_HAD_FINA;
1225 report_make_pending(cstack.cs_pending[cstack.cs_idx]
1226 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001227 did_throw ? (void *)current_exception : NULL);
1228 did_emsg = got_int = did_throw = FALSE;
1229 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
1230 }
1231
1232 /* Update global "trylevel" for recursive calls to do_cmdline() from
1233 * within this loop. */
1234 trylevel = initial_trylevel + cstack.cs_trylevel;
1235
1236 /*
1237 * If the outermost try conditional (accross function calls and sourced
1238 * files) is aborted because of an error, an interrupt, or an uncaught
1239 * exception, cancel everything. If it is left normally, reset
1240 * force_abort to get the non-EH compatible abortion behavior for
1241 * the rest of the script.
1242 */
1243 if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
1244 force_abort = FALSE;
1245
1246 /* Convert an interrupt to an exception if appropriate. */
1247 (void)do_intthrow(&cstack);
1248#endif /* FEAT_EVAL */
1249
1250 }
1251 /*
1252 * Continue executing command lines when:
1253 * - no CTRL-C typed, no aborting error, no exception thrown or try
1254 * conditionals need to be checked for executing finally clauses or
1255 * catching an interrupt exception
1256 * - didn't get an error message or lines are not typed
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001257 * - there is a command after '|', inside a :if, :while, :for or :try, or
Bram Moolenaar071d4272004-06-13 20:20:40 +00001258 * looping for ":source" command or function call.
1259 */
1260 while (!((got_int
1261#ifdef FEAT_EVAL
1262 || (did_emsg && force_abort) || did_throw
1263#endif
1264 )
1265#ifdef FEAT_EVAL
1266 && cstack.cs_trylevel == 0
1267#endif
1268 )
1269 && !(did_emsg && used_getline
1270 && (getline_equal(getline, cookie, getexmodeline)
1271 || getline_equal(getline, cookie, getexline)))
1272 && (next_cmdline != NULL
1273#ifdef FEAT_EVAL
1274 || cstack.cs_idx >= 0
1275#endif
1276 || (flags & DOCMD_REPEAT)));
1277
1278 vim_free(cmdline_copy);
1279#ifdef FEAT_EVAL
1280 free_cmdlines(&lines_ga);
1281 ga_clear(&lines_ga);
1282
1283 if (cstack.cs_idx >= 0)
1284 {
1285 /*
1286 * If a sourced file or executed function ran to its end, report the
1287 * unclosed conditional.
1288 */
1289 if (!got_int && !did_throw
1290 && ((getline_equal(getline, cookie, getsourceline)
1291 && !source_finished(getline, cookie))
1292 || (getline_equal(getline, cookie, get_func_line)
1293 && !func_has_ended(real_cookie))))
1294 {
1295 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
1296 EMSG(_(e_endtry));
1297 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
1298 EMSG(_(e_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001299 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
1300 EMSG(_(e_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001301 else
1302 EMSG(_(e_endif));
1303 }
1304
1305 /*
1306 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1307 * ":endtry" in a sourced file or executed function. If the try
1308 * conditional is in its finally clause, ignore anything pending.
1309 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001310 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311 */
1312 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001313 {
1314 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1315
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001316 if (idx >= 0)
1317 --idx; /* remove try block not in its finally clause */
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001318 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1319 &cstack.cs_looplevel);
1320 }
1321 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322 trylevel = initial_trylevel;
1323 }
1324
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001325 /* If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1326 * lack was reported above and the error message is to be converted to an
Bram Moolenaar071d4272004-06-13 20:20:40 +00001327 * exception, do this now after rewinding the cstack. */
1328 do_errthrow(&cstack, getline_equal(getline, cookie, get_func_line)
1329 ? (char_u *)"endfunction" : (char_u *)NULL);
1330
1331 if (trylevel == 0)
1332 {
1333 /*
1334 * When an exception is being thrown out of the outermost try
1335 * conditional, discard the uncaught exception, disable the conversion
1336 * of interrupts or errors to exceptions, and ensure that no more
1337 * commands are executed.
1338 */
1339 if (did_throw)
1340 {
1341 void *p = NULL;
1342 char_u *saved_sourcing_name;
1343 int saved_sourcing_lnum;
1344 struct msglist *messages = NULL, *next;
1345
1346 /*
1347 * If the uncaught exception is a user exception, report it as an
1348 * error. If it is an error exception, display the saved error
1349 * message now. For an interrupt exception, do nothing; the
1350 * interrupt message is given elsewhere.
1351 */
1352 switch (current_exception->type)
1353 {
1354 case ET_USER:
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001355 vim_snprintf((char *)IObuff, IOSIZE,
1356 _("E605: Exception not caught: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357 current_exception->value);
1358 p = vim_strsave(IObuff);
1359 break;
1360 case ET_ERROR:
1361 messages = current_exception->messages;
1362 current_exception->messages = NULL;
1363 break;
1364 case ET_INTERRUPT:
1365 break;
1366 default:
1367 p = vim_strsave((char_u *)_(e_internal));
1368 }
1369
1370 saved_sourcing_name = sourcing_name;
1371 saved_sourcing_lnum = sourcing_lnum;
1372 sourcing_name = current_exception->throw_name;
1373 sourcing_lnum = current_exception->throw_lnum;
1374 current_exception->throw_name = NULL;
1375
1376 discard_current_exception(); /* uses IObuff if 'verbose' */
1377 suppress_errthrow = TRUE;
1378 force_abort = TRUE;
1379
1380 if (messages != NULL)
1381 {
1382 do
1383 {
1384 next = messages->next;
1385 emsg(messages->msg);
1386 vim_free(messages->msg);
1387 vim_free(messages);
1388 messages = next;
1389 }
1390 while (messages != NULL);
1391 }
1392 else if (p != NULL)
1393 {
1394 emsg(p);
1395 vim_free(p);
1396 }
1397 vim_free(sourcing_name);
1398 sourcing_name = saved_sourcing_name;
1399 sourcing_lnum = saved_sourcing_lnum;
1400 }
1401
1402 /*
1403 * On an interrupt or an aborting error not converted to an exception,
1404 * disable the conversion of errors to exceptions. (Interrupts are not
1405 * converted any more, here.) This enables also the interrupt message
1406 * when force_abort is set and did_emsg unset in case of an interrupt
1407 * from a finally clause after an error.
1408 */
1409 else if (got_int || (did_emsg && force_abort))
1410 suppress_errthrow = TRUE;
1411 }
1412
1413 /*
1414 * The current cstack will be freed when do_cmdline() returns. An uncaught
1415 * exception will have to be rethrown in the previous cstack. If a function
1416 * has just returned or a script file was just finished and the previous
1417 * cstack belongs to the same function or, respectively, script file, it
1418 * will have to be checked for finally clauses to be executed due to the
1419 * ":return" or ":finish". This is done in do_one_cmd().
1420 */
1421 if (did_throw)
1422 need_rethrow = TRUE;
1423 if ((getline_equal(getline, cookie, getsourceline)
1424 && ex_nesting_level > source_level(real_cookie))
1425 || (getline_equal(getline, cookie, get_func_line)
1426 && ex_nesting_level > func_level(real_cookie) + 1))
1427 {
1428 if (!did_throw)
1429 check_cstack = TRUE;
1430 }
1431 else
1432 {
1433 /* When leaving a function, reduce nesting level. */
1434 if (getline_equal(getline, cookie, get_func_line))
1435 --ex_nesting_level;
1436 /*
1437 * Go to debug mode when returning from a function in which we are
1438 * single-stepping.
1439 */
1440 if ((getline_equal(getline, cookie, getsourceline)
1441 || getline_equal(getline, cookie, get_func_line))
1442 && ex_nesting_level + 1 <= debug_break_level)
1443 do_debug(getline_equal(getline, cookie, getsourceline)
1444 ? (char_u *)_("End of sourced file")
1445 : (char_u *)_("End of function"));
1446 }
1447
1448 /*
1449 * Restore the exception environment (done after returning from the
1450 * debugger).
1451 */
1452 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001453 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001454
1455 msg_list = saved_msg_list;
1456#endif /* FEAT_EVAL */
1457
1458 /*
1459 * If there was too much output to fit on the command line, ask the user to
1460 * hit return before redrawing the screen. With the ":global" command we do
1461 * this only once after the command is finished.
1462 */
1463 if (did_inc)
1464 {
1465 --RedrawingDisabled;
1466 --no_wait_return;
1467 msg_scroll = FALSE;
1468
1469 /*
1470 * When just finished an ":if"-":else" which was typed, no need to
1471 * wait for hit-return. Also for an error situation.
1472 */
1473 if (retval == FAIL
1474#ifdef FEAT_EVAL
1475 || (did_endif && KeyTyped && !did_emsg)
1476#endif
1477 )
1478 {
1479 need_wait_return = FALSE;
1480 msg_didany = FALSE; /* don't wait when restarting edit */
1481 }
1482 else if (need_wait_return)
1483 {
1484 /*
1485 * The msg_start() above clears msg_didout. The wait_return we do
1486 * here should not overwrite the command that may be shown before
1487 * doing that.
1488 */
1489 msg_didout |= msg_didout_before_start;
1490 wait_return(FALSE);
1491 }
1492 }
1493
1494#ifndef FEAT_EVAL
1495 /*
1496 * Reset if_level, in case a sourced script file contains more ":if" than
1497 * ":endif" (could be ":if x | foo | endif").
1498 */
1499 if_level = 0;
1500#endif
1501
1502 --call_depth;
1503 return retval;
1504}
1505
1506#ifdef FEAT_EVAL
1507/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001508 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001509 */
1510 static char_u *
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001511get_loop_line(c, cookie, indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001512 int c;
1513 void *cookie;
1514 int indent;
1515{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001516 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517 wcmd_T *wp;
1518 char_u *line;
1519
1520 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1521 {
1522 if (cp->repeating)
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001523 return NULL; /* trying to read past ":endwhile"/":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001525 /* First time inside the ":while"/":for": get line normally. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001526 if (cp->getline == NULL)
1527 line = getcmdline(c, 0L, indent);
1528 else
1529 line = cp->getline(c, cp->cookie, indent);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001530 if (store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001531 ++cp->current_line;
1532
1533 return line;
1534 }
1535
1536 KeyTyped = FALSE;
1537 ++cp->current_line;
1538 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
1539 sourcing_lnum = wp->lnum;
1540 return vim_strsave(wp->line);
1541}
1542
1543/*
1544 * Store a line in "gap" so that a ":while" loop can execute it again.
1545 */
1546 static int
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001547store_loop_line(gap, line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001548 garray_T *gap;
1549 char_u *line;
1550{
1551 if (ga_grow(gap, 1) == FAIL)
1552 return FAIL;
1553 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
1554 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = sourcing_lnum;
1555 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001556 return OK;
1557}
1558
1559/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001560 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561 */
1562 static void
1563free_cmdlines(gap)
1564 garray_T *gap;
1565{
1566 while (gap->ga_len > 0)
1567 {
1568 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1569 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570 }
1571}
1572#endif
1573
1574/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001575 * If "getline" is get_loop_line(), return TRUE if the getline it uses equals
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576 * "func". * Otherwise return TRUE when "getline" equals "func".
1577 */
1578/*ARGSUSED*/
1579 int
1580getline_equal(getline, cookie, func)
1581 char_u *(*getline) __ARGS((int, void *, int));
1582 void *cookie; /* argument for getline() */
1583 char_u *(*func) __ARGS((int, void *, int));
1584{
1585#ifdef FEAT_EVAL
1586 char_u *(*gp) __ARGS((int, void *, int));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001587 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001588
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001589 /* When "getline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590 * function that's orignally used to obtain the lines. This may be nested
1591 * several levels. */
1592 gp = getline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001593 cp = (struct loop_cookie *)cookie;
1594 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595 {
1596 gp = cp->getline;
1597 cp = cp->cookie;
1598 }
1599 return gp == func;
1600#else
1601 return getline == func;
1602#endif
1603}
1604
1605#if defined(FEAT_EVAL) || defined(FEAT_MBYTE) || defined(PROTO)
1606/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001607 * If "getline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608 * getline function. Otherwise return "cookie".
1609 */
1610/*ARGSUSED*/
1611 void *
1612getline_cookie(getline, cookie)
1613 char_u *(*getline) __ARGS((int, void *, int));
1614 void *cookie; /* argument for getline() */
1615{
1616# ifdef FEAT_EVAL
1617 char_u *(*gp) __ARGS((int, void *, int));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001618 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001620 /* When "getline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621 * cookie that's orignally used to obtain the lines. This may be nested
1622 * several levels. */
1623 gp = getline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001624 cp = (struct loop_cookie *)cookie;
1625 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626 {
1627 gp = cp->getline;
1628 cp = cp->cookie;
1629 }
1630 return cp;
1631# else
1632 return cookie;
1633# endif
1634}
1635#endif
1636
1637/*
1638 * Execute one Ex command.
1639 *
1640 * If 'sourcing' is TRUE, the command will be included in the error message.
1641 *
1642 * 1. skip comment lines and leading space
1643 * 2. handle command modifiers
1644 * 3. parse range
1645 * 4. parse command
1646 * 5. parse arguments
1647 * 6. switch on command name
1648 *
1649 * Note: "getline" can be NULL.
1650 *
1651 * This function may be called recursively!
1652 */
1653#if (_MSC_VER == 1200)
1654/*
Bram Moolenaared203462004-06-16 11:19:22 +00001655 * Avoid optimisation bug in VC++ version 6.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656 */
Bram Moolenaar281bdce2005-01-25 21:53:18 +00001657 #pragma optimize( "g", off )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658#endif
1659 static char_u *
1660do_one_cmd(cmdlinep, sourcing,
1661#ifdef FEAT_EVAL
1662 cstack,
1663#endif
1664 getline, cookie)
1665 char_u **cmdlinep;
1666 int sourcing;
1667#ifdef FEAT_EVAL
1668 struct condstack *cstack;
1669#endif
1670 char_u *(*getline) __ARGS((int, void *, int));
1671 void *cookie; /* argument for getline() */
1672{
1673 char_u *p;
1674 linenr_T lnum;
1675 long n;
1676 char_u *errormsg = NULL; /* error message */
1677 exarg_T ea; /* Ex command arguments */
1678 long verbose_save = -1;
1679 int save_msg_scroll = 0;
1680 int did_silent = 0;
1681 int did_esilent = 0;
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001682#ifdef HAVE_SANDBOX
1683 int did_sandbox = FALSE;
1684#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001685 cmdmod_T save_cmdmod;
1686 int ni; /* set when Not Implemented */
1687
1688 vim_memset(&ea, 0, sizeof(ea));
1689 ea.line1 = 1;
1690 ea.line2 = 1;
1691#ifdef FEAT_EVAL
1692 ++ex_nesting_level;
1693#endif
1694
1695 /* when not editing the last file :q has to be typed twice */
1696 if (quitmore
1697#ifdef FEAT_EVAL
1698 /* avoid that a function call in 'statusline' does this */
1699 && !getline_equal(getline, cookie, get_func_line)
1700#endif
1701 )
1702 --quitmore;
1703
1704 /*
1705 * Reset browse, confirm, etc.. They are restored when returning, for
1706 * recursive calls.
1707 */
1708 save_cmdmod = cmdmod;
1709 vim_memset(&cmdmod, 0, sizeof(cmdmod));
1710
1711 /*
1712 * Repeat until no more command modifiers are found.
1713 */
1714 ea.cmd = *cmdlinep;
1715 for (;;)
1716 {
1717/*
1718 * 1. skip comment lines and leading white space and colons
1719 */
1720 while (*ea.cmd == ' ' || *ea.cmd == '\t' || *ea.cmd == ':')
1721 ++ea.cmd;
1722
1723 /* in ex mode, an empty line works like :+ */
1724 if (*ea.cmd == NUL && exmode_active
1725 && (getline_equal(getline, cookie, getexmodeline)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001726 || getline_equal(getline, cookie, getexline))
1727 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001728 {
1729 ea.cmd = (char_u *)"+";
1730 ex_pressedreturn = TRUE;
1731 }
1732
1733 /* ignore comment and empty lines */
1734 if (*ea.cmd == '"' || *ea.cmd == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001735 {
1736 ex_pressedreturn = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001737 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001738 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739
1740/*
1741 * 2. handle command modifiers.
1742 */
1743 p = ea.cmd;
1744 if (VIM_ISDIGIT(*ea.cmd))
1745 p = skipwhite(skipdigits(ea.cmd));
1746 switch (*p)
1747 {
1748 /* When adding an entry, also modify cmd_exists(). */
1749 case 'a': if (!checkforcmd(&ea.cmd, "aboveleft", 3))
1750 break;
1751#ifdef FEAT_WINDOWS
1752 cmdmod.split |= WSP_ABOVE;
1753#endif
1754 continue;
1755
1756 case 'b': if (checkforcmd(&ea.cmd, "belowright", 3))
1757 {
1758#ifdef FEAT_WINDOWS
1759 cmdmod.split |= WSP_BELOW;
1760#endif
1761 continue;
1762 }
1763 if (checkforcmd(&ea.cmd, "browse", 3))
1764 {
1765#ifdef FEAT_BROWSE
1766 cmdmod.browse = TRUE;
1767#endif
1768 continue;
1769 }
1770 if (!checkforcmd(&ea.cmd, "botright", 2))
1771 break;
1772#ifdef FEAT_WINDOWS
1773 cmdmod.split |= WSP_BOT;
1774#endif
1775 continue;
1776
1777 case 'c': if (!checkforcmd(&ea.cmd, "confirm", 4))
1778 break;
1779#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1780 cmdmod.confirm = TRUE;
1781#endif
1782 continue;
1783
1784 case 'k': if (checkforcmd(&ea.cmd, "keepmarks", 3))
1785 {
1786 cmdmod.keepmarks = TRUE;
1787 continue;
1788 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001789 if (checkforcmd(&ea.cmd, "keepalt", 5))
1790 {
1791 cmdmod.keepalt = TRUE;
1792 continue;
1793 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794 if (!checkforcmd(&ea.cmd, "keepjumps", 5))
1795 break;
1796 cmdmod.keepjumps = TRUE;
1797 continue;
1798
1799 /* ":hide" and ":hide | cmd" are not modifiers */
1800 case 'h': if (p != ea.cmd || !checkforcmd(&p, "hide", 3)
1801 || *p == NUL || ends_excmd(*p))
1802 break;
1803 ea.cmd = p;
1804 cmdmod.hide = TRUE;
1805 continue;
1806
1807 case 'l': if (checkforcmd(&ea.cmd, "lockmarks", 3))
1808 {
1809 cmdmod.lockmarks = TRUE;
1810 continue;
1811 }
1812
1813 if (!checkforcmd(&ea.cmd, "leftabove", 5))
1814 break;
1815#ifdef FEAT_WINDOWS
1816 cmdmod.split |= WSP_ABOVE;
1817#endif
1818 continue;
1819
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001820 case 'n': if (!checkforcmd(&ea.cmd, "noautocmd", 3))
1821 break;
1822#ifdef FEAT_AUTOCMD
1823 if (cmdmod.save_ei == NULL)
1824 {
1825 /* Set 'eventignore' to "all". Don't free the
1826 * existing option value, we restore it later. */
1827 cmdmod.save_ei = vim_strsave(p_ei);
1828 set_string_option_direct((char_u *)"ei", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001829 (char_u *)"all", OPT_FREE, SID_NONE);
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001830 }
1831#endif
1832 continue;
1833
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834 case 'r': if (!checkforcmd(&ea.cmd, "rightbelow", 6))
1835 break;
1836#ifdef FEAT_WINDOWS
1837 cmdmod.split |= WSP_BELOW;
1838#endif
1839 continue;
1840
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001841 case 's': if (checkforcmd(&ea.cmd, "sandbox", 3))
1842 {
1843#ifdef HAVE_SANDBOX
1844 if (!did_sandbox)
1845 ++sandbox;
1846 did_sandbox = TRUE;
1847#endif
1848 continue;
1849 }
1850 if (!checkforcmd(&ea.cmd, "silent", 3))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851 break;
1852 ++did_silent;
1853 ++msg_silent;
1854 save_msg_scroll = msg_scroll;
1855 if (*ea.cmd == '!' && !vim_iswhite(ea.cmd[-1]))
1856 {
1857 /* ":silent!", but not "silent !cmd" */
1858 ea.cmd = skipwhite(ea.cmd + 1);
1859 ++emsg_silent;
1860 ++did_esilent;
1861 }
1862 continue;
1863
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001864 case 't': if (checkforcmd(&p, "tab", 3))
1865 {
1866#ifdef FEAT_WINDOWS
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001867 if (vim_isdigit(*ea.cmd))
1868 cmdmod.tab = atoi((char *)ea.cmd) + 1;
1869 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001870 cmdmod.tab = tabpage_index(curtab) + 1;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001871 ea.cmd = p;
1872#endif
1873 continue;
1874 }
1875 if (!checkforcmd(&ea.cmd, "topleft", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001876 break;
1877#ifdef FEAT_WINDOWS
1878 cmdmod.split |= WSP_TOP;
1879#endif
1880 continue;
1881
1882 case 'v': if (checkforcmd(&ea.cmd, "vertical", 4))
1883 {
1884#ifdef FEAT_VERTSPLIT
1885 cmdmod.split |= WSP_VERT;
1886#endif
1887 continue;
1888 }
1889 if (!checkforcmd(&p, "verbose", 4))
1890 break;
1891 if (verbose_save < 0)
1892 verbose_save = p_verbose;
Bram Moolenaared203462004-06-16 11:19:22 +00001893 if (vim_isdigit(*ea.cmd))
1894 p_verbose = atoi((char *)ea.cmd);
1895 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896 p_verbose = 1;
1897 ea.cmd = p;
1898 continue;
1899 }
1900 break;
1901 }
1902
1903#ifdef FEAT_EVAL
1904 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
1905 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
1906#else
1907 ea.skip = (if_level > 0);
1908#endif
1909
1910#ifdef FEAT_EVAL
Bram Moolenaar05159a02005-02-26 23:04:13 +00001911# ifdef FEAT_PROFILE
1912 /* Count this line for profiling if ea.skip is FALSE. */
1913 if (do_profiling && !ea.skip)
1914 {
1915 if (getline_equal(getline, cookie, get_func_line))
1916 func_line_exec(getline_cookie(getline, cookie));
1917 else if (getline_equal(getline, cookie, getsourceline))
1918 script_line_exec();
1919 }
1920#endif
1921
Bram Moolenaar071d4272004-06-13 20:20:40 +00001922 /* May go to debug mode. If this happens and the ">quit" debug command is
1923 * used, throw an interrupt exception and skip the next command. */
1924 dbg_check_breakpoint(&ea);
1925 if (!ea.skip && got_int)
1926 {
1927 ea.skip = TRUE;
1928 (void)do_intthrow(cstack);
1929 }
1930#endif
1931
1932/*
1933 * 3. parse a range specifier of the form: addr [,addr] [;addr] ..
1934 *
1935 * where 'addr' is:
1936 *
1937 * % (entire file)
1938 * $ [+-NUM]
1939 * 'x [+-NUM] (where x denotes a currently defined mark)
1940 * . [+-NUM]
1941 * [+-NUM]..
1942 * NUM
1943 *
1944 * The ea.cmd pointer is updated to point to the first character following the
1945 * range spec. If an initial address is found, but no second, the upper bound
1946 * is equal to the lower.
1947 */
1948
1949 /* repeat for all ',' or ';' separated addresses */
1950 for (;;)
1951 {
1952 ea.line1 = ea.line2;
1953 ea.line2 = curwin->w_cursor.lnum; /* default is current line number */
1954 ea.cmd = skipwhite(ea.cmd);
1955 lnum = get_address(&ea.cmd, ea.skip, ea.addr_count == 0);
1956 if (ea.cmd == NULL) /* error detected */
1957 goto doend;
1958 if (lnum == MAXLNUM)
1959 {
1960 if (*ea.cmd == '%') /* '%' - all lines */
1961 {
1962 ++ea.cmd;
1963 ea.line1 = 1;
1964 ea.line2 = curbuf->b_ml.ml_line_count;
1965 ++ea.addr_count;
1966 }
1967 /* '*' - visual area */
1968 else if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
1969 {
1970 pos_T *fp;
1971
1972 ++ea.cmd;
1973 if (!ea.skip)
1974 {
1975 fp = getmark('<', FALSE);
1976 if (check_mark(fp) == FAIL)
1977 goto doend;
1978 ea.line1 = fp->lnum;
1979 fp = getmark('>', FALSE);
1980 if (check_mark(fp) == FAIL)
1981 goto doend;
1982 ea.line2 = fp->lnum;
1983 ++ea.addr_count;
1984 }
1985 }
1986 }
1987 else
1988 ea.line2 = lnum;
1989 ea.addr_count++;
1990
1991 if (*ea.cmd == ';')
1992 {
1993 if (!ea.skip)
1994 curwin->w_cursor.lnum = ea.line2;
1995 }
1996 else if (*ea.cmd != ',')
1997 break;
1998 ++ea.cmd;
1999 }
2000
2001 /* One address given: set start and end lines */
2002 if (ea.addr_count == 1)
2003 {
2004 ea.line1 = ea.line2;
2005 /* ... but only implicit: really no address given */
2006 if (lnum == MAXLNUM)
2007 ea.addr_count = 0;
2008 }
2009
2010 /* Don't leave the cursor on an illegal line (caused by ';') */
2011 check_cursor_lnum();
2012
2013/*
2014 * 4. parse command
2015 */
2016
2017 /*
2018 * Skip ':' and any white space
2019 */
2020 ea.cmd = skipwhite(ea.cmd);
2021 while (*ea.cmd == ':')
2022 ea.cmd = skipwhite(ea.cmd + 1);
2023
2024 /*
2025 * If we got a line, but no command, then go to the line.
2026 * If we find a '|' or '\n' we set ea.nextcmd.
2027 */
2028 if (*ea.cmd == NUL || *ea.cmd == '"' ||
2029 (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
2030 {
2031 /*
2032 * strange vi behaviour:
2033 * ":3" jumps to line 3
2034 * ":3|..." prints line 3
2035 * ":|" prints current line
2036 */
2037 if (ea.skip) /* skip this if inside :if */
2038 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00002039 if (*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002040 {
2041 ea.cmdidx = CMD_print;
2042 ea.argt = RANGE+COUNT+TRLBAR;
2043 if ((errormsg = invalid_range(&ea)) == NULL)
2044 {
2045 correct_range(&ea);
2046 ex_print(&ea);
2047 }
2048 }
2049 else if (ea.addr_count != 0)
2050 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002051 if (ea.line2 > curbuf->b_ml.ml_line_count)
2052 {
2053 /* With '-' in 'cpoptions' a line number past the file is an
2054 * error, otherwise put it at the end of the file. */
2055 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
2056 ea.line2 = -1;
2057 else
2058 ea.line2 = curbuf->b_ml.ml_line_count;
2059 }
2060
2061 if (ea.line2 < 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002062 errormsg = (char_u *)_(e_invrange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063 else
2064 {
2065 if (ea.line2 == 0)
2066 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002067 else
2068 curwin->w_cursor.lnum = ea.line2;
2069 beginline(BL_SOL | BL_FIX);
2070 }
2071 }
2072 goto doend;
2073 }
2074
2075 /* Find the command and let "p" point to after it. */
2076 p = find_command(&ea, NULL);
2077
2078#ifdef FEAT_USR_CMDS
2079 if (p == NULL)
2080 {
2081 if (!ea.skip)
2082 errormsg = (char_u *)_("E464: Ambiguous use of user-defined command");
2083 goto doend;
2084 }
2085 /* Check for wrong commands. */
2086 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78)
2087 {
2088 errormsg = uc_fun_cmd();
2089 goto doend;
2090 }
2091#endif
2092 if (ea.cmdidx == CMD_SIZE)
2093 {
2094 if (!ea.skip)
2095 {
2096 STRCPY(IObuff, _("E492: Not an editor command"));
2097 if (!sourcing)
2098 {
2099 STRCAT(IObuff, ": ");
2100 STRNCAT(IObuff, *cmdlinep, 40);
2101 }
2102 errormsg = IObuff;
2103 }
2104 goto doend;
2105 }
2106
2107 ni = (
2108#ifdef FEAT_USR_CMDS
2109 !USER_CMDIDX(ea.cmdidx) &&
2110#endif
2111 cmdnames[ea.cmdidx].cmd_func == ex_ni);
2112
2113#ifndef FEAT_EVAL
2114 /*
2115 * When the expression evaluation is disabled, recognize the ":if" and
2116 * ":endif" commands and ignore everything in between it.
2117 */
2118 if (ea.cmdidx == CMD_if)
2119 ++if_level;
2120 if (if_level)
2121 {
2122 if (ea.cmdidx == CMD_endif)
2123 --if_level;
2124 goto doend;
2125 }
2126
2127#endif
2128
2129 if (*p == '!' && ea.cmdidx != CMD_substitute) /* forced commands */
2130 {
2131 ++p;
2132 ea.forceit = TRUE;
2133 }
2134 else
2135 ea.forceit = FALSE;
2136
2137/*
2138 * 5. parse arguments
2139 */
2140#ifdef FEAT_USR_CMDS
2141 if (!USER_CMDIDX(ea.cmdidx))
2142#endif
2143 ea.argt = cmdnames[(int)ea.cmdidx].cmd_argt;
2144
2145 if (!ea.skip)
2146 {
2147#ifdef HAVE_SANDBOX
2148 if (sandbox != 0 && !(ea.argt & SBOXOK))
2149 {
2150 /* Command not allowed in sandbox. */
2151 errormsg = (char_u *)_(e_sandbox);
2152 goto doend;
2153 }
2154#endif
2155 if (!curbuf->b_p_ma && (ea.argt & MODIFY))
2156 {
2157 /* Command not allowed in non-'modifiable' buffer */
2158 errormsg = (char_u *)_(e_modifiable);
2159 goto doend;
2160 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002161
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002162 if (text_locked() && !(ea.argt & CMDWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163# ifdef FEAT_USR_CMDS
2164 && !USER_CMDIDX(ea.cmdidx)
2165# endif
2166 )
2167 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002168 /* Command not allowed when editing the command line. */
2169#ifdef FEAT_CMDWIN
2170 if (cmdwin_type != 0)
2171 errormsg = (char_u *)_(e_cmdwin);
2172 else
2173#endif
2174 errormsg = (char_u *)_(e_secure);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175 goto doend;
2176 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177
2178 if (!ni && !(ea.argt & RANGE) && ea.addr_count > 0)
2179 {
2180 /* no range allowed */
2181 errormsg = (char_u *)_(e_norange);
2182 goto doend;
2183 }
2184 }
2185
2186 if (!ni && !(ea.argt & BANG) && ea.forceit) /* no <!> allowed */
2187 {
2188 errormsg = (char_u *)_(e_nobang);
2189 goto doend;
2190 }
2191
2192 /*
2193 * Don't complain about the range if it is not used
2194 * (could happen if line_count is accidentally set to 0).
2195 */
2196 if (!ea.skip && !ni)
2197 {
2198 /*
2199 * If the range is backwards, ask for confirmation and, if given, swap
2200 * ea.line1 & ea.line2 so it's forwards again.
2201 * When global command is busy, don't ask, will fail below.
2202 */
2203 if (!global_busy && ea.line1 > ea.line2)
2204 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002205 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002206 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002207 if (sourcing || exmode_active)
2208 {
2209 errormsg = (char_u *)_("E493: Backwards range given");
2210 goto doend;
2211 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002212 if (ask_yesno((char_u *)
2213 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002214 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002215 }
2216 lnum = ea.line1;
2217 ea.line1 = ea.line2;
2218 ea.line2 = lnum;
2219 }
2220 if ((errormsg = invalid_range(&ea)) != NULL)
2221 goto doend;
2222 }
2223
2224 if ((ea.argt & NOTADR) && ea.addr_count == 0) /* default is 1, not cursor */
2225 ea.line2 = 1;
2226
2227 correct_range(&ea);
2228
2229#ifdef FEAT_FOLDING
2230 if (((ea.argt & WHOLEFOLD) || ea.addr_count >= 2) && !global_busy)
2231 {
2232 /* Put the first line at the start of a closed fold, put the last line
2233 * at the end of a closed fold. */
2234 (void)hasFolding(ea.line1, &ea.line1, NULL);
2235 (void)hasFolding(ea.line2, NULL, &ea.line2);
2236 }
2237#endif
2238
2239#ifdef FEAT_QUICKFIX
2240 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002241 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002242 * option here, so things like % get expanded.
2243 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002244 p = replace_makeprg(&ea, p, cmdlinep);
2245 if (p == NULL)
2246 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002247#endif
2248
2249 /*
2250 * Skip to start of argument.
2251 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2252 */
2253 if (ea.cmdidx == CMD_bang)
2254 ea.arg = p;
2255 else
2256 ea.arg = skipwhite(p);
2257
2258 /*
2259 * Check for "++opt=val" argument.
2260 * Must be first, allow ":w ++enc=utf8 !cmd"
2261 */
2262 if (ea.argt & ARGOPT)
2263 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2264 if (getargopt(&ea) == FAIL && !ni)
2265 {
2266 errormsg = (char_u *)_(e_invarg);
2267 goto doend;
2268 }
2269
2270 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2271 {
2272 if (*ea.arg == '>') /* append */
2273 {
2274 if (*++ea.arg != '>') /* typed wrong */
2275 {
2276 errormsg = (char_u *)_("E494: Use w or w>>");
2277 goto doend;
2278 }
2279 ea.arg = skipwhite(ea.arg + 1);
2280 ea.append = TRUE;
2281 }
2282 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
2283 {
2284 ++ea.arg;
2285 ea.usefilter = TRUE;
2286 }
2287 }
2288
2289 if (ea.cmdidx == CMD_read)
2290 {
2291 if (ea.forceit)
2292 {
2293 ea.usefilter = TRUE; /* :r! filter if ea.forceit */
2294 ea.forceit = FALSE;
2295 }
2296 else if (*ea.arg == '!') /* :r !filter */
2297 {
2298 ++ea.arg;
2299 ea.usefilter = TRUE;
2300 }
2301 }
2302
2303 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2304 {
2305 ea.amount = 1;
2306 while (*ea.arg == *ea.cmd) /* count number of '>' or '<' */
2307 {
2308 ++ea.arg;
2309 ++ea.amount;
2310 }
2311 ea.arg = skipwhite(ea.arg);
2312 }
2313
2314 /*
2315 * Check for "+command" argument, before checking for next command.
2316 * Don't do this for ":read !cmd" and ":write !cmd".
2317 */
2318 if ((ea.argt & EDITCMD) && !ea.usefilter)
2319 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2320
2321 /*
2322 * Check for '|' to separate commands and '"' to start comments.
2323 * Don't do this for ":read !cmd" and ":write !cmd".
2324 */
2325 if ((ea.argt & TRLBAR) && !ea.usefilter)
2326 separate_nextcmd(&ea);
2327
2328 /*
2329 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002330 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2331 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002332 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002333 else if (ea.cmdidx == CMD_bang
2334 || ea.cmdidx == CMD_global
2335 || ea.cmdidx == CMD_vglobal
2336 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002337 {
2338 for (p = ea.arg; *p; ++p)
2339 {
2340 /* Remove one backslash before a newline, so that it's possible to
2341 * pass a newline to the shell and also a newline that is preceded
2342 * with a backslash. This makes it impossible to end a shell
2343 * command in a backslash, but that doesn't appear useful.
2344 * Halving the number of backslashes is incompatible with previous
2345 * versions. */
2346 if (*p == '\\' && p[1] == '\n')
2347 mch_memmove(p, p + 1, STRLEN(p));
2348 else if (*p == '\n')
2349 {
2350 ea.nextcmd = p + 1;
2351 *p = NUL;
2352 break;
2353 }
2354 }
2355 }
2356
2357 if ((ea.argt & DFLALL) && ea.addr_count == 0)
2358 {
2359 ea.line1 = 1;
2360 ea.line2 = curbuf->b_ml.ml_line_count;
2361 }
2362
2363 /* accept numbered register only when no count allowed (:put) */
2364 if ( (ea.argt & REGSTR)
2365 && *ea.arg != NUL
2366#ifdef FEAT_USR_CMDS
2367 && valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2368 && USER_CMDIDX(ea.cmdidx)))
2369 /* Do not allow register = for user commands */
2370 && (!USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
2371#else
2372 && valid_yank_reg(*ea.arg, ea.cmdidx != CMD_put)
2373#endif
2374 && !((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)))
2375 {
2376 ea.regname = *ea.arg++;
2377#ifdef FEAT_EVAL
2378 /* for '=' register: accept the rest of the line as an expression */
2379 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2380 {
2381 set_expr_line(vim_strsave(ea.arg));
2382 ea.arg += STRLEN(ea.arg);
2383 }
2384#endif
2385 ea.arg = skipwhite(ea.arg);
2386 }
2387
2388 /*
2389 * Check for a count. When accepting a BUFNAME, don't use "123foo" as a
2390 * count, it's a buffer name.
2391 */
2392 if ((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)
2393 && (!(ea.argt & BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
2394 || vim_iswhite(*p)))
2395 {
2396 n = getdigits(&ea.arg);
2397 ea.arg = skipwhite(ea.arg);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002398 if (n <= 0 && !ni && (ea.argt & ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399 {
2400 errormsg = (char_u *)_(e_zerocount);
2401 goto doend;
2402 }
2403 if (ea.argt & NOTADR) /* e.g. :buffer 2, :sleep 3 */
2404 {
2405 ea.line2 = n;
2406 if (ea.addr_count == 0)
2407 ea.addr_count = 1;
2408 }
2409 else
2410 {
2411 ea.line1 = ea.line2;
2412 ea.line2 += n - 1;
2413 ++ea.addr_count;
2414 /*
2415 * Be vi compatible: no error message for out of range.
2416 */
2417 if (ea.line2 > curbuf->b_ml.ml_line_count)
2418 ea.line2 = curbuf->b_ml.ml_line_count;
2419 }
2420 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002421
2422 /*
2423 * Check for flags: 'l', 'p' and '#'.
2424 */
2425 if (ea.argt & EXFLAGS)
2426 get_flags(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002427 /* no arguments allowed */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002428 if (!ni && !(ea.argt & EXTRA) && *ea.arg != NUL
2429 && vim_strchr((char_u *)"|\"", *ea.arg) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430 {
2431 errormsg = (char_u *)_(e_trailing);
2432 goto doend;
2433 }
2434
2435 if (!ni && (ea.argt & NEEDARG) && *ea.arg == NUL)
2436 {
2437 errormsg = (char_u *)_(e_argreq);
2438 goto doend;
2439 }
2440
2441#ifdef FEAT_EVAL
2442 /*
2443 * Skip the command when it's not going to be executed.
2444 * The commands like :if, :endif, etc. always need to be executed.
2445 * Also make an exception for commands that handle a trailing command
2446 * themselves.
2447 */
2448 if (ea.skip)
2449 {
2450 switch (ea.cmdidx)
2451 {
2452 /* commands that need evaluation */
2453 case CMD_while:
2454 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002455 case CMD_for:
2456 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002457 case CMD_if:
2458 case CMD_elseif:
2459 case CMD_else:
2460 case CMD_endif:
2461 case CMD_try:
2462 case CMD_catch:
2463 case CMD_finally:
2464 case CMD_endtry:
2465 case CMD_function:
2466 break;
2467
2468 /* Commands that handle '|' themselves. Check: A command should
2469 * either have the TRLBAR flag, appear in this list or appear in
2470 * the list at ":help :bar". */
2471 case CMD_aboveleft:
2472 case CMD_and:
2473 case CMD_belowright:
2474 case CMD_botright:
2475 case CMD_browse:
2476 case CMD_call:
2477 case CMD_confirm:
2478 case CMD_delfunction:
2479 case CMD_djump:
2480 case CMD_dlist:
2481 case CMD_dsearch:
2482 case CMD_dsplit:
2483 case CMD_echo:
2484 case CMD_echoerr:
2485 case CMD_echomsg:
2486 case CMD_echon:
2487 case CMD_execute:
2488 case CMD_help:
2489 case CMD_hide:
2490 case CMD_ijump:
2491 case CMD_ilist:
2492 case CMD_isearch:
2493 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002494 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495 case CMD_keepjumps:
2496 case CMD_keepmarks:
2497 case CMD_leftabove:
2498 case CMD_let:
2499 case CMD_lockmarks:
2500 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002501 case CMD_mzscheme:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002502 case CMD_perl:
2503 case CMD_psearch:
2504 case CMD_python:
2505 case CMD_return:
2506 case CMD_rightbelow:
2507 case CMD_ruby:
2508 case CMD_silent:
2509 case CMD_smagic:
2510 case CMD_snomagic:
2511 case CMD_substitute:
2512 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002513 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514 case CMD_tcl:
2515 case CMD_throw:
2516 case CMD_tilde:
2517 case CMD_topleft:
2518 case CMD_unlet:
2519 case CMD_verbose:
2520 case CMD_vertical:
2521 break;
2522
2523 default: goto doend;
2524 }
2525 }
2526#endif
2527
2528 if (ea.argt & XFILE)
2529 {
2530 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2531 goto doend;
2532 }
2533
2534#ifdef FEAT_LISTCMDS
2535 /*
2536 * Accept buffer name. Cannot be used at the same time with a buffer
2537 * number. Don't do this for a user command.
2538 */
2539 if ((ea.argt & BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
2540# ifdef FEAT_USR_CMDS
2541 && !USER_CMDIDX(ea.cmdidx)
2542# endif
2543 )
2544 {
2545 /*
2546 * :bdelete, :bwipeout and :bunload take several arguments, separated
2547 * by spaces: find next space (skipping over escaped characters).
2548 * The others take one argument: ignore trailing spaces.
2549 */
2550 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2551 || ea.cmdidx == CMD_bunload)
2552 p = skiptowhite_esc(ea.arg);
2553 else
2554 {
2555 p = ea.arg + STRLEN(ea.arg);
2556 while (p > ea.arg && vim_iswhite(p[-1]))
2557 --p;
2558 }
2559 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & BUFUNL) != 0, FALSE);
2560 if (ea.line2 < 0) /* failed */
2561 goto doend;
2562 ea.addr_count = 1;
2563 ea.arg = skipwhite(p);
2564 }
2565#endif
2566
2567/*
2568 * 6. switch on command name
2569 *
2570 * The "ea" structure holds the arguments that can be used.
2571 */
2572 ea.cmdlinep = cmdlinep;
2573 ea.getline = getline;
2574 ea.cookie = cookie;
2575#ifdef FEAT_EVAL
2576 ea.cstack = cstack;
2577#endif
2578
2579#ifdef FEAT_USR_CMDS
2580 if (USER_CMDIDX(ea.cmdidx))
2581 {
2582 /*
2583 * Execute a user-defined command.
2584 */
2585 do_ucmd(&ea);
2586 }
2587 else
2588#endif
2589 {
2590 /*
2591 * Call the function to execute the command.
2592 */
2593 ea.errmsg = NULL;
2594 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2595 if (ea.errmsg != NULL)
2596 errormsg = (char_u *)_(ea.errmsg);
2597 }
2598
2599#ifdef FEAT_EVAL
2600 /*
2601 * If the command just executed called do_cmdline(), any throw or ":return"
2602 * or ":finish" encountered there must also check the cstack of the still
2603 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2604 * exception, or reanimate a returned function or finished script file and
2605 * return or finish it again.
2606 */
2607 if (need_rethrow)
2608 do_throw(cstack);
2609 else if (check_cstack)
2610 {
2611 if (source_finished(getline, cookie))
2612 do_finish(&ea, TRUE);
2613 else if (getline_equal(getline, cookie, get_func_line)
2614 && current_func_returned())
2615 do_return(&ea, TRUE, FALSE, NULL);
2616 }
2617 need_rethrow = check_cstack = FALSE;
2618#endif
2619
2620doend:
2621 if (curwin->w_cursor.lnum == 0) /* can happen with zero line number */
2622 curwin->w_cursor.lnum = 1;
2623
2624 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2625 {
2626 if (sourcing)
2627 {
2628 if (errormsg != IObuff)
2629 {
2630 STRCPY(IObuff, errormsg);
2631 errormsg = IObuff;
2632 }
2633 STRCAT(errormsg, ": ");
2634 STRNCAT(errormsg, *cmdlinep, IOSIZE - STRLEN(IObuff));
2635 }
2636 emsg(errormsg);
2637 }
2638#ifdef FEAT_EVAL
2639 do_errthrow(cstack,
2640 (ea.cmdidx != CMD_SIZE
2641# ifdef FEAT_USR_CMDS
2642 && !USER_CMDIDX(ea.cmdidx)
2643# endif
2644 ) ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
2645#endif
2646
2647 if (verbose_save >= 0)
2648 p_verbose = verbose_save;
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00002649#ifdef FEAT_AUTOCMD
2650 if (cmdmod.save_ei != NULL)
2651 {
2652 /* Restore 'eventignore' to the value before ":noautocmd". */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002653 set_string_option_direct((char_u *)"ei", -1, cmdmod.save_ei,
2654 OPT_FREE, SID_NONE);
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00002655 free_string_option(cmdmod.save_ei);
2656 }
2657#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658
2659 cmdmod = save_cmdmod;
2660
2661 if (did_silent > 0)
2662 {
2663 /* messages could be enabled for a serious error, need to check if the
2664 * counters don't become negative */
2665 msg_silent -= did_silent;
2666 if (msg_silent < 0)
2667 msg_silent = 0;
2668 emsg_silent -= did_esilent;
2669 if (emsg_silent < 0)
2670 emsg_silent = 0;
2671 /* Restore msg_scroll, it's set by file I/O commands, even when no
2672 * message is actually displayed. */
2673 msg_scroll = save_msg_scroll;
2674 }
2675
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002676#ifdef HAVE_SANDBOX
2677 if (did_sandbox)
2678 --sandbox;
2679#endif
2680
Bram Moolenaar071d4272004-06-13 20:20:40 +00002681 if (ea.nextcmd && *ea.nextcmd == NUL) /* not really a next command */
2682 ea.nextcmd = NULL;
2683
2684#ifdef FEAT_EVAL
2685 --ex_nesting_level;
2686#endif
2687
2688 return ea.nextcmd;
2689}
2690#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00002691 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002692#endif
2693
2694/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002695 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696 * If there is a match advance "pp" to the argument and return TRUE.
2697 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002698 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699checkforcmd(pp, cmd, len)
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002700 char_u **pp; /* start of command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701 char *cmd; /* name of command */
2702 int len; /* required length */
2703{
2704 int i;
2705
2706 for (i = 0; cmd[i] != NUL; ++i)
2707 if (cmd[i] != (*pp)[i])
2708 break;
2709 if (i >= len && !isalpha((*pp)[i]))
2710 {
2711 *pp = skipwhite(*pp + i);
2712 return TRUE;
2713 }
2714 return FALSE;
2715}
2716
2717/*
2718 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002719 * Start of the name can be found at eap->cmd.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720 * Returns pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002721 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002722 * Returns NULL for an ambiguous user command.
2723 */
2724/*ARGSUSED*/
2725 static char_u *
2726find_command(eap, full)
2727 exarg_T *eap;
2728 int *full;
2729{
2730 int len;
2731 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00002732 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002733
2734 /*
2735 * Isolate the command and search for it in the command table.
2736 * Exeptions:
2737 * - the 'k' command can directly be followed by any character.
2738 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
2739 * but :sre[wind] is another command, as are :scrip[tnames],
2740 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00002741 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002742 */
2743 p = eap->cmd;
2744 if (*p == 'k')
2745 {
2746 eap->cmdidx = CMD_k;
2747 ++p;
2748 }
2749 else if (p[0] == 's'
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002750 && ((p[1] == 'c' && p[2] != 's' && p[2] != 'r'
2751 && p[3] != 'i' && p[4] != 'p')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002752 || p[1] == 'g'
2753 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
2754 || p[1] == 'I'
2755 || (p[1] == 'r' && p[2] != 'e')))
2756 {
2757 eap->cmdidx = CMD_substitute;
2758 ++p;
2759 }
2760 else
2761 {
2762 while (ASCII_ISALPHA(*p))
2763 ++p;
2764 /* check for non-alpha command */
2765 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
2766 ++p;
2767 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00002768 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
2769 {
2770 /* Check for ":dl", ":dell", etc. to ":deletel": that's
2771 * :delete with the 'l' flag. Same for 'p'. */
2772 for (i = 0; i < len; ++i)
2773 if (eap->cmd[i] != "delete"[i])
2774 break;
2775 if (i == len - 1)
2776 {
2777 --len;
2778 if (p[-1] == 'l')
2779 eap->flags |= EXFLAG_LIST;
2780 else
2781 eap->flags |= EXFLAG_PRINT;
2782 }
2783 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002784
2785 if (ASCII_ISLOWER(*eap->cmd))
2786 eap->cmdidx = cmdidxs[CharOrdLow(*eap->cmd)];
2787 else
2788 eap->cmdidx = cmdidxs[26];
2789
2790 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
2791 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
2792 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
2793 (size_t)len) == 0)
2794 {
2795#ifdef FEAT_EVAL
2796 if (full != NULL
2797 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
2798 *full = TRUE;
2799#endif
2800 break;
2801 }
2802
2803#ifdef FEAT_USR_CMDS
2804 /* Look for a user defined command as a last resort */
2805 if (eap->cmdidx == CMD_SIZE && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
2806 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002807 /* User defined commands may contain digits. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002808 while (ASCII_ISALNUM(*p))
2809 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002810 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002811 }
2812#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002813 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002814 eap->cmdidx = CMD_SIZE;
2815 }
2816
2817 return p;
2818}
2819
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002820#ifdef FEAT_USR_CMDS
2821/*
2822 * Search for a user command that matches "eap->cmd".
2823 * Return cmdidx in "eap->cmdidx", flags in "eap->argt", idx in "eap->useridx".
2824 * Return a pointer to just after the command.
2825 * Return NULL if there is no matching command.
2826 */
2827 static char_u *
2828find_ucmd(eap, p, full, xp, compl)
2829 exarg_T *eap;
2830 char_u *p; /* end of the command (possibly including count) */
2831 int *full; /* set to TRUE for a full match */
2832 expand_T *xp; /* used for completion, NULL otherwise */
2833 int *compl; /* completion flags or NULL */
2834{
2835 int len = (int)(p - eap->cmd);
2836 int j, k, matchlen = 0;
2837 ucmd_T *uc;
2838 int found = FALSE;
2839 int possible = FALSE;
2840 char_u *cp, *np; /* Point into typed cmd and test name */
2841 garray_T *gap;
2842 int amb_local = FALSE; /* Found ambiguous buffer-local command,
2843 only full match global is accepted. */
2844
2845 /*
2846 * Look for buffer-local user commands first, then global ones.
2847 */
2848 gap = &curbuf->b_ucmds;
2849 for (;;)
2850 {
2851 for (j = 0; j < gap->ga_len; ++j)
2852 {
2853 uc = USER_CMD_GA(gap, j);
2854 cp = eap->cmd;
2855 np = uc->uc_name;
2856 k = 0;
2857 while (k < len && *np != NUL && *cp++ == *np++)
2858 k++;
2859 if (k == len || (*np == NUL && vim_isdigit(eap->cmd[k])))
2860 {
2861 /* If finding a second match, the command is ambiguous. But
2862 * not if a buffer-local command wasn't a full match and a
2863 * global command is a full match. */
2864 if (k == len && found && *np != NUL)
2865 {
2866 if (gap == &ucmds)
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002867 return NULL;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002868 amb_local = TRUE;
2869 }
2870
2871 if (!found || (k == len && *np == NUL))
2872 {
2873 /* If we matched up to a digit, then there could
2874 * be another command including the digit that we
2875 * should use instead.
2876 */
2877 if (k == len)
2878 found = TRUE;
2879 else
2880 possible = TRUE;
2881
2882 if (gap == &ucmds)
2883 eap->cmdidx = CMD_USER;
2884 else
2885 eap->cmdidx = CMD_USER_BUF;
2886 eap->argt = uc->uc_argt;
2887 eap->useridx = j;
2888
2889# ifdef FEAT_CMDL_COMPL
2890 if (compl != NULL)
2891 *compl = uc->uc_compl;
2892# ifdef FEAT_EVAL
2893 if (xp != NULL)
2894 {
2895 xp->xp_arg = uc->uc_compl_arg;
2896 xp->xp_scriptID = uc->uc_scriptID;
2897 }
2898# endif
2899# endif
2900 /* Do not search for further abbreviations
2901 * if this is an exact match. */
2902 matchlen = k;
2903 if (k == len && *np == NUL)
2904 {
2905 if (full != NULL)
2906 *full = TRUE;
2907 amb_local = FALSE;
2908 break;
2909 }
2910 }
2911 }
2912 }
2913
2914 /* Stop if we found a full match or searched all. */
2915 if (j < gap->ga_len || gap == &ucmds)
2916 break;
2917 gap = &ucmds;
2918 }
2919
2920 /* Only found ambiguous matches. */
2921 if (amb_local)
2922 {
2923 if (xp != NULL)
2924 xp->xp_context = EXPAND_UNSUCCESSFUL;
2925 return NULL;
2926 }
2927
2928 /* The match we found may be followed immediately by a number. Move "p"
2929 * back to point to it. */
2930 if (found || possible)
2931 return p + (matchlen - len);
2932 return p;
2933}
2934#endif
2935
Bram Moolenaar071d4272004-06-13 20:20:40 +00002936#if defined(FEAT_EVAL) || defined(PROTO)
2937/*
2938 * Return > 0 if an Ex command "name" exists.
2939 * Return 2 if there is an exact match.
2940 * Return 3 if there is an ambiguous match.
2941 */
2942 int
2943cmd_exists(name)
2944 char_u *name;
2945{
2946 exarg_T ea;
2947 int full = FALSE;
2948 int i;
2949 int j;
2950 static struct cmdmod
2951 {
2952 char *name;
2953 int minlen;
2954 } cmdmods[] = {
2955 {"aboveleft", 3},
2956 {"belowright", 3},
2957 {"botright", 2},
2958 {"browse", 3},
2959 {"confirm", 4},
2960 {"hide", 3},
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002961 {"keepalt", 5},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002962 {"keepjumps", 5},
2963 {"keepmarks", 3},
2964 {"leftabove", 5},
2965 {"lockmarks", 3},
2966 {"rightbelow", 6},
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002967 {"sandbox", 3},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002968 {"silent", 3},
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002969 {"tab", 3},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002970 {"topleft", 2},
2971 {"verbose", 4},
2972 {"vertical", 4},
2973 };
2974
2975 /* Check command modifiers. */
2976 for (i = 0; i < sizeof(cmdmods) / sizeof(struct cmdmod); ++i)
2977 {
2978 for (j = 0; name[j] != NUL; ++j)
2979 if (name[j] != cmdmods[i].name[j])
2980 break;
2981 if (name[j] == NUL && j >= cmdmods[i].minlen)
2982 return (cmdmods[i].name[j] == NUL ? 2 : 1);
2983 }
2984
2985 /* Check built-in commands and user defined commands. */
2986 ea.cmd = name;
2987 ea.cmdidx = (cmdidx_T)0;
2988 if (find_command(&ea, &full) == NULL)
2989 return 3;
2990 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
2991}
2992#endif
2993
2994/*
2995 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
2996 * we don't need/want deleted. Maybe this could be done better if we didn't
2997 * repeat all this stuff. The only problem is that they may not stay
2998 * perfectly compatible with each other, but then the command line syntax
2999 * probably won't change that much -- webb.
3000 */
3001 char_u *
3002set_one_cmd_context(xp, buff)
3003 expand_T *xp;
3004 char_u *buff; /* buffer for command string */
3005{
3006 char_u *p;
3007 char_u *cmd, *arg;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003008 int len = 0;
3009 exarg_T ea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003010#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3011 int compl = EXPAND_NOTHING;
3012#endif
3013#ifdef FEAT_CMDL_COMPL
3014 int delim;
3015#endif
3016 int forceit = FALSE;
3017 int usefilter = FALSE; /* filter instead of file name */
3018
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003019 ExpandInit(xp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003020 xp->xp_pattern = buff;
3021 xp->xp_context = EXPAND_COMMANDS; /* Default until we get past command */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003022 ea.argt = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003023
3024/*
3025 * 2. skip comment lines and leading space, colons or bars
3026 */
3027 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
3028 ;
3029 xp->xp_pattern = cmd;
3030
3031 if (*cmd == NUL)
3032 return NULL;
3033 if (*cmd == '"') /* ignore comment lines */
3034 {
3035 xp->xp_context = EXPAND_NOTHING;
3036 return NULL;
3037 }
3038
3039/*
3040 * 3. parse a range specifier of the form: addr [,addr] [;addr] ..
3041 */
3042 cmd = skip_range(cmd, &xp->xp_context);
3043
3044/*
3045 * 4. parse command
3046 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003047 xp->xp_pattern = cmd;
3048 if (*cmd == NUL)
3049 return NULL;
3050 if (*cmd == '"')
3051 {
3052 xp->xp_context = EXPAND_NOTHING;
3053 return NULL;
3054 }
3055
3056 if (*cmd == '|' || *cmd == '\n')
3057 return cmd + 1; /* There's another command */
3058
3059 /*
3060 * Isolate the command and search for it in the command table.
3061 * Exceptions:
3062 * - the 'k' command can directly be followed by any character, but
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003063 * do accept "keepmarks", "keepalt" and "keepjumps".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003064 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
3065 */
3066 if (*cmd == 'k' && cmd[1] != 'e')
3067 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003068 ea.cmdidx = CMD_k;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003069 p = cmd + 1;
3070 }
3071 else
3072 {
3073 p = cmd;
3074 while (ASCII_ISALPHA(*p) || *p == '*') /* Allow * wild card */
3075 ++p;
3076 /* check for non-alpha command */
3077 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3078 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003079 len = (int)(p - cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003080
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003081 if (len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003082 {
3083 xp->xp_context = EXPAND_UNSUCCESSFUL;
3084 return NULL;
3085 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003086 for (ea.cmdidx = (cmdidx_T)0; (int)ea.cmdidx < (int)CMD_SIZE;
3087 ea.cmdidx = (cmdidx_T)((int)ea.cmdidx + 1))
3088 if (STRNCMP(cmdnames[(int)ea.cmdidx].cmd_name, cmd, (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003089 break;
3090
3091#ifdef FEAT_USR_CMDS
3092 if (cmd[0] >= 'A' && cmd[0] <= 'Z')
3093 {
3094 while (ASCII_ISALNUM(*p) || *p == '*') /* Allow * wild card */
3095 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003096 len = (int)(p - cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003097 }
3098#endif
3099 }
3100
3101 /*
3102 * If the cursor is touching the command, and it ends in an alpha-numeric
3103 * character, complete the command name.
3104 */
3105 if (*p == NUL && ASCII_ISALNUM(p[-1]))
3106 return NULL;
3107
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003108 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109 {
3110 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
3111 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003112 ea.cmdidx = CMD_substitute;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113 p = cmd + 1;
3114 }
3115#ifdef FEAT_USR_CMDS
3116 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
3117 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003118 ea.cmd = cmd;
3119 p = find_ucmd(&ea, p, NULL, xp,
3120# if defined(FEAT_CMDL_COMPL)
3121 &compl
3122# else
3123 NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003124# endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003125 );
Bram Moolenaarebefac62005-12-28 22:39:57 +00003126 if (p == NULL)
3127 ea.cmdidx = CMD_SIZE; /* ambiguous user command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003128 }
3129#endif
3130 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003131 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003132 {
3133 /* Not still touching the command and it was an illegal one */
3134 xp->xp_context = EXPAND_UNSUCCESSFUL;
3135 return NULL;
3136 }
3137
3138 xp->xp_context = EXPAND_NOTHING; /* Default now that we're past command */
3139
3140 if (*p == '!') /* forced commands */
3141 {
3142 forceit = TRUE;
3143 ++p;
3144 }
3145
3146/*
3147 * 5. parse arguments
3148 */
3149#ifdef FEAT_USR_CMDS
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003150 if (!USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003151#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003152 ea.argt = cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003153
3154 arg = skipwhite(p);
3155
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003156 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003157 {
3158 if (*arg == '>') /* append */
3159 {
3160 if (*++arg == '>')
3161 ++arg;
3162 arg = skipwhite(arg);
3163 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003164 else if (*arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003165 {
3166 ++arg;
3167 usefilter = TRUE;
3168 }
3169 }
3170
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003171 if (ea.cmdidx == CMD_read)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003172 {
3173 usefilter = forceit; /* :r! filter if forced */
3174 if (*arg == '!') /* :r !filter */
3175 {
3176 ++arg;
3177 usefilter = TRUE;
3178 }
3179 }
3180
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003181 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003182 {
3183 while (*arg == *cmd) /* allow any number of '>' or '<' */
3184 ++arg;
3185 arg = skipwhite(arg);
3186 }
3187
3188 /* Does command allow "+command"? */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003189 if ((ea.argt & EDITCMD) && !usefilter && *arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190 {
3191 /* Check if we're in the +command */
3192 p = arg + 1;
3193 arg = skip_cmd_arg(arg, FALSE);
3194
3195 /* Still touching the command after '+'? */
3196 if (*arg == NUL)
3197 return p;
3198
3199 /* Skip space(s) after +command to get to the real argument */
3200 arg = skipwhite(arg);
3201 }
3202
3203 /*
3204 * Check for '|' to separate commands and '"' to start comments.
3205 * Don't do this for ":read !cmd" and ":write !cmd".
3206 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003207 if ((ea.argt & TRLBAR) && !usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208 {
3209 p = arg;
3210 /* ":redir @" is not the start of a comment */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003211 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003212 p += 2;
3213 while (*p)
3214 {
3215 if (*p == Ctrl_V)
3216 {
3217 if (p[1] != NUL)
3218 ++p;
3219 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003220 else if ( (*p == '"' && !(ea.argt & NOTRLCOM))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003221 || *p == '|' || *p == '\n')
3222 {
3223 if (*(p - 1) != '\\')
3224 {
3225 if (*p == '|' || *p == '\n')
3226 return p + 1;
3227 return NULL; /* It's a comment */
3228 }
3229 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003230 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003231 }
3232 }
3233
3234 /* no arguments allowed */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003235 if (!(ea.argt & EXTRA) && *arg != NUL &&
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236 vim_strchr((char_u *)"|\"", *arg) == NULL)
3237 return NULL;
3238
3239 /* Find start of last argument (argument just before cursor): */
3240 p = buff + STRLEN(buff);
3241 while (p != arg && *p != ' ' && *p != TAB)
3242 p--;
3243 if (*p == ' ' || *p == TAB)
3244 p++;
3245 xp->xp_pattern = p;
3246
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003247 if (ea.argt & XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003248 {
3249 int in_quote = FALSE;
3250 char_u *bow = NULL; /* Beginning of word */
3251
3252 /*
3253 * Allow spaces within back-quotes to count as part of the argument
3254 * being expanded.
3255 */
3256 xp->xp_pattern = skipwhite(arg);
3257 for (p = xp->xp_pattern; *p; )
3258 {
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003259 if (*p == '\\' && p[1] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003260 ++p;
3261#ifdef SPACE_IN_FILENAME
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003262 else if (vim_iswhite(*p) && (!(ea.argt & NOSPC) || usefilter))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003263#else
3264 else if (vim_iswhite(*p))
3265#endif
3266 {
3267 p = skipwhite(p);
3268 if (in_quote)
3269 bow = p;
3270 else
3271 xp->xp_pattern = p;
3272 --p;
3273 }
3274 else if (*p == '`')
3275 {
3276 if (!in_quote)
3277 {
3278 xp->xp_pattern = p;
3279 bow = p + 1;
3280 }
3281 in_quote = !in_quote;
3282 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003283 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003284 }
3285
3286 /*
3287 * If we are still inside the quotes, and we passed a space, just
3288 * expand from there.
3289 */
3290 if (bow != NULL && in_quote)
3291 xp->xp_pattern = bow;
3292 xp->xp_context = EXPAND_FILES;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003293
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003294#ifndef BACKSLASH_IN_FILENAME
3295 /* For a shell command more chars need to be escaped. */
3296 if (usefilter || ea.cmdidx == CMD_bang)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003297 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003298 xp->xp_shell = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003299
3300 /* When still after the command name expand executables. */
3301 if (xp->xp_pattern == skipwhite(arg))
3302 xp->xp_context = EXPAND_SHELLCMD;
3303 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003304#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305
3306 /* Check for environment variable */
3307 if (*xp->xp_pattern == '$'
3308#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
3309 || *xp->xp_pattern == '%'
3310#endif
3311 )
3312 {
3313 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
3314 if (!vim_isIDc(*p))
3315 break;
3316 if (*p == NUL)
3317 {
3318 xp->xp_context = EXPAND_ENV_VARS;
3319 ++xp->xp_pattern;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003320#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3321 /* Avoid that the assignment uses EXPAND_FILES again. */
Bram Moolenaara466c992005-07-09 21:03:22 +00003322 if (compl != EXPAND_USER_DEFINED && compl != EXPAND_USER_LIST)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003323 compl = EXPAND_ENV_VARS;
3324#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325 }
3326 }
3327 }
3328
3329/*
3330 * 6. switch on command name
3331 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003332 switch (ea.cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333 {
3334 case CMD_cd:
3335 case CMD_chdir:
3336 case CMD_lcd:
3337 case CMD_lchdir:
3338 if (xp->xp_context == EXPAND_FILES)
3339 xp->xp_context = EXPAND_DIRECTORIES;
3340 break;
3341 case CMD_help:
3342 xp->xp_context = EXPAND_HELP;
3343 xp->xp_pattern = arg;
3344 break;
3345
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003346 /* Command modifiers: return the argument.
3347 * Also for commands with an argument that is a command. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348 case CMD_aboveleft:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003349 case CMD_argdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350 case CMD_belowright:
3351 case CMD_botright:
3352 case CMD_browse:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003353 case CMD_bufdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354 case CMD_confirm:
Bram Moolenaardf177f62005-02-22 08:39:57 +00003355 case CMD_debug:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003356 case CMD_folddoclosed:
3357 case CMD_folddoopen:
3358 case CMD_hide:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003359 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360 case CMD_keepjumps:
3361 case CMD_keepmarks:
3362 case CMD_leftabove:
3363 case CMD_lockmarks:
3364 case CMD_rightbelow:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003365 case CMD_sandbox:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366 case CMD_silent:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003367 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368 case CMD_topleft:
3369 case CMD_verbose:
3370 case CMD_vertical:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003371 case CMD_windo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372 return arg;
3373
3374#ifdef FEAT_SEARCH_EXTRA
3375 case CMD_match:
3376 if (*arg == NUL || !ends_excmd(*arg))
3377 {
3378 /* Dummy call to clear variables. */
3379 set_context_in_highlight_cmd(xp, (char_u *)"link n");
3380 xp->xp_context = EXPAND_HIGHLIGHT;
3381 xp->xp_pattern = arg;
3382 arg = skipwhite(skiptowhite(arg));
3383 if (*arg != NUL)
3384 {
3385 xp->xp_context = EXPAND_NOTHING;
3386 arg = skip_regexp(arg + 1, *arg, p_magic, NULL);
3387 }
3388 }
3389 return find_nextcmd(arg);
3390#endif
3391
3392#ifdef FEAT_CMDL_COMPL
3393/*
3394 * All completion for the +cmdline_compl feature goes here.
3395 */
3396
3397# ifdef FEAT_USR_CMDS
3398 case CMD_command:
3399 /* Check for attributes */
3400 while (*arg == '-')
3401 {
3402 arg++; /* Skip "-" */
3403 p = skiptowhite(arg);
3404 if (*p == NUL)
3405 {
3406 /* Cursor is still in the attribute */
3407 p = vim_strchr(arg, '=');
3408 if (p == NULL)
3409 {
3410 /* No "=", so complete attribute names */
3411 xp->xp_context = EXPAND_USER_CMD_FLAGS;
3412 xp->xp_pattern = arg;
3413 return NULL;
3414 }
3415
3416 /* For the -complete and -nargs attributes, we complete
3417 * their arguments as well.
3418 */
3419 if (STRNICMP(arg, "complete", p - arg) == 0)
3420 {
3421 xp->xp_context = EXPAND_USER_COMPLETE;
3422 xp->xp_pattern = p + 1;
3423 return NULL;
3424 }
3425 else if (STRNICMP(arg, "nargs", p - arg) == 0)
3426 {
3427 xp->xp_context = EXPAND_USER_NARGS;
3428 xp->xp_pattern = p + 1;
3429 return NULL;
3430 }
3431 return NULL;
3432 }
3433 arg = skipwhite(p);
3434 }
3435
3436 /* After the attributes comes the new command name */
3437 p = skiptowhite(arg);
3438 if (*p == NUL)
3439 {
3440 xp->xp_context = EXPAND_USER_COMMANDS;
3441 xp->xp_pattern = arg;
3442 break;
3443 }
3444
3445 /* And finally comes a normal command */
3446 return skipwhite(p);
3447
3448 case CMD_delcommand:
3449 xp->xp_context = EXPAND_USER_COMMANDS;
3450 xp->xp_pattern = arg;
3451 break;
3452# endif
3453
3454 case CMD_global:
3455 case CMD_vglobal:
3456 delim = *arg; /* get the delimiter */
3457 if (delim)
3458 ++arg; /* skip delimiter if there is one */
3459
3460 while (arg[0] != NUL && arg[0] != delim)
3461 {
3462 if (arg[0] == '\\' && arg[1] != NUL)
3463 ++arg;
3464 ++arg;
3465 }
3466 if (arg[0] != NUL)
3467 return arg + 1;
3468 break;
3469 case CMD_and:
3470 case CMD_substitute:
3471 delim = *arg;
3472 if (delim)
3473 {
3474 /* skip "from" part */
3475 ++arg;
3476 arg = skip_regexp(arg, delim, p_magic, NULL);
3477 }
3478 /* skip "to" part */
3479 while (arg[0] != NUL && arg[0] != delim)
3480 {
3481 if (arg[0] == '\\' && arg[1] != NUL)
3482 ++arg;
3483 ++arg;
3484 }
3485 if (arg[0] != NUL) /* skip delimiter */
3486 ++arg;
3487 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
3488 ++arg;
3489 if (arg[0] != NUL)
3490 return arg;
3491 break;
3492 case CMD_isearch:
3493 case CMD_dsearch:
3494 case CMD_ilist:
3495 case CMD_dlist:
3496 case CMD_ijump:
3497 case CMD_psearch:
3498 case CMD_djump:
3499 case CMD_isplit:
3500 case CMD_dsplit:
3501 arg = skipwhite(skipdigits(arg)); /* skip count */
3502 if (*arg == '/') /* Match regexp, not just whole words */
3503 {
3504 for (++arg; *arg && *arg != '/'; arg++)
3505 if (*arg == '\\' && arg[1] != NUL)
3506 arg++;
3507 if (*arg)
3508 {
3509 arg = skipwhite(arg + 1);
3510
3511 /* Check for trailing illegal characters */
3512 if (*arg && vim_strchr((char_u *)"|\"\n", *arg) == NULL)
3513 xp->xp_context = EXPAND_NOTHING;
3514 else
3515 return arg;
3516 }
3517 }
3518 break;
3519#ifdef FEAT_AUTOCMD
3520 case CMD_autocmd:
3521 return set_context_in_autocmd(xp, arg, FALSE);
3522
3523 case CMD_doautocmd:
3524 return set_context_in_autocmd(xp, arg, TRUE);
3525#endif
3526 case CMD_set:
3527 set_context_in_set_cmd(xp, arg, 0);
3528 break;
3529 case CMD_setglobal:
3530 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
3531 break;
3532 case CMD_setlocal:
3533 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
3534 break;
3535 case CMD_tag:
3536 case CMD_stag:
3537 case CMD_ptag:
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00003538 case CMD_ltag:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003539 case CMD_tselect:
3540 case CMD_stselect:
3541 case CMD_ptselect:
3542 case CMD_tjump:
3543 case CMD_stjump:
3544 case CMD_ptjump:
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003545 if (*p_wop != NUL)
3546 xp->xp_context = EXPAND_TAGS_LISTFILES;
3547 else
3548 xp->xp_context = EXPAND_TAGS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549 xp->xp_pattern = arg;
3550 break;
3551 case CMD_augroup:
3552 xp->xp_context = EXPAND_AUGROUP;
3553 xp->xp_pattern = arg;
3554 break;
3555#ifdef FEAT_SYN_HL
3556 case CMD_syntax:
3557 set_context_in_syntax_cmd(xp, arg);
3558 break;
3559#endif
3560#ifdef FEAT_EVAL
3561 case CMD_let:
3562 case CMD_if:
3563 case CMD_elseif:
3564 case CMD_while:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00003565 case CMD_for:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566 case CMD_echo:
3567 case CMD_echon:
3568 case CMD_execute:
3569 case CMD_echomsg:
3570 case CMD_echoerr:
3571 case CMD_call:
3572 case CMD_return:
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003573 set_context_for_expression(xp, arg, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574 break;
3575
3576 case CMD_unlet:
3577 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3578 arg = xp->xp_pattern + 1;
3579 xp->xp_context = EXPAND_USER_VARS;
3580 xp->xp_pattern = arg;
3581 break;
3582
3583 case CMD_function:
3584 case CMD_delfunction:
3585 xp->xp_context = EXPAND_USER_FUNC;
3586 xp->xp_pattern = arg;
3587 break;
3588
3589 case CMD_echohl:
3590 xp->xp_context = EXPAND_HIGHLIGHT;
3591 xp->xp_pattern = arg;
3592 break;
3593#endif
3594 case CMD_highlight:
3595 set_context_in_highlight_cmd(xp, arg);
3596 break;
3597#ifdef FEAT_LISTCMDS
3598 case CMD_bdelete:
3599 case CMD_bwipeout:
3600 case CMD_bunload:
3601 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3602 arg = xp->xp_pattern + 1;
3603 /*FALLTHROUGH*/
3604 case CMD_buffer:
3605 case CMD_sbuffer:
3606 case CMD_checktime:
3607 xp->xp_context = EXPAND_BUFFERS;
3608 xp->xp_pattern = arg;
3609 break;
3610#endif
3611#ifdef FEAT_USR_CMDS
3612 case CMD_USER:
3613 case CMD_USER_BUF:
3614 if (compl != EXPAND_NOTHING)
3615 {
3616 /* XFILE: file names are handled above */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003617 if (!(ea.argt & XFILE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618 {
3619# ifdef FEAT_MENU
3620 if (compl == EXPAND_MENUS)
3621 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
3622# endif
3623 if (compl == EXPAND_COMMANDS)
3624 return arg;
3625 if (compl == EXPAND_MAPPINGS)
3626 return set_context_in_map_cmd(xp, (char_u *)"map",
3627 arg, forceit, FALSE, FALSE, CMD_map);
3628 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3629 arg = xp->xp_pattern + 1;
3630 xp->xp_pattern = arg;
3631 }
3632 xp->xp_context = compl;
3633 }
3634 break;
3635#endif
3636 case CMD_map: case CMD_noremap:
3637 case CMD_nmap: case CMD_nnoremap:
3638 case CMD_vmap: case CMD_vnoremap:
3639 case CMD_omap: case CMD_onoremap:
3640 case CMD_imap: case CMD_inoremap:
3641 case CMD_cmap: case CMD_cnoremap:
3642 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003643 FALSE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644 case CMD_unmap:
3645 case CMD_nunmap:
3646 case CMD_vunmap:
3647 case CMD_ounmap:
3648 case CMD_iunmap:
3649 case CMD_cunmap:
3650 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003651 FALSE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003652 case CMD_abbreviate: case CMD_noreabbrev:
3653 case CMD_cabbrev: case CMD_cnoreabbrev:
3654 case CMD_iabbrev: case CMD_inoreabbrev:
3655 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003656 TRUE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657 case CMD_unabbreviate:
3658 case CMD_cunabbrev:
3659 case CMD_iunabbrev:
3660 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003661 TRUE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662#ifdef FEAT_MENU
3663 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
3664 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
3665 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
3666 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
3667 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
3668 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
3669 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
3670 case CMD_tmenu: case CMD_tunmenu:
3671 case CMD_popup: case CMD_tearoff: case CMD_emenu:
3672 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
3673#endif
3674
3675 case CMD_colorscheme:
3676 xp->xp_context = EXPAND_COLORS;
3677 xp->xp_pattern = arg;
3678 break;
3679
3680 case CMD_compiler:
3681 xp->xp_context = EXPAND_COMPILER;
3682 xp->xp_pattern = arg;
3683 break;
3684
3685#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
3686 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
3687 case CMD_language:
3688 if (*skiptowhite(arg) == NUL)
3689 {
3690 xp->xp_context = EXPAND_LANGUAGE;
3691 xp->xp_pattern = arg;
3692 }
3693 else
3694 xp->xp_context = EXPAND_NOTHING;
3695 break;
3696#endif
3697
3698#endif /* FEAT_CMDL_COMPL */
3699
3700 default:
3701 break;
3702 }
3703 return NULL;
3704}
3705
3706/*
3707 * skip a range specifier of the form: addr [,addr] [;addr] ..
3708 *
3709 * Backslashed delimiters after / or ? will be skipped, and commands will
3710 * not be expanded between /'s and ?'s or after "'".
3711 *
Bram Moolenaardf177f62005-02-22 08:39:57 +00003712 * Also skip white space and ":" characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003713 * Returns the "cmd" pointer advanced to beyond the range.
3714 */
3715 char_u *
3716skip_range(cmd, ctx)
3717 char_u *cmd;
3718 int *ctx; /* pointer to xp_context or NULL */
3719{
3720 int delim;
3721
Bram Moolenaardf177f62005-02-22 08:39:57 +00003722 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723 {
3724 if (*cmd == '\'')
3725 {
3726 if (*++cmd == NUL && ctx != NULL)
3727 *ctx = EXPAND_NOTHING;
3728 }
3729 else if (*cmd == '/' || *cmd == '?')
3730 {
3731 delim = *cmd++;
3732 while (*cmd != NUL && *cmd != delim)
3733 if (*cmd++ == '\\' && *cmd != NUL)
3734 ++cmd;
3735 if (*cmd == NUL && ctx != NULL)
3736 *ctx = EXPAND_NOTHING;
3737 }
3738 if (*cmd != NUL)
3739 ++cmd;
3740 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00003741
3742 /* Skip ":" and white space. */
3743 while (*cmd == ':')
3744 cmd = skipwhite(cmd + 1);
3745
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746 return cmd;
3747}
3748
3749/*
3750 * get a single EX address
3751 *
3752 * Set ptr to the next character after the part that was interpreted.
3753 * Set ptr to NULL when an error is encountered.
3754 *
3755 * Return MAXLNUM when no Ex address was found.
3756 */
3757 static linenr_T
3758get_address(ptr, skip, to_other_file)
3759 char_u **ptr;
3760 int skip; /* only skip the address, don't use it */
3761 int to_other_file; /* flag: may jump to other file */
3762{
3763 int c;
3764 int i;
3765 long n;
3766 char_u *cmd;
3767 pos_T pos;
3768 pos_T *fp;
3769 linenr_T lnum;
3770
3771 cmd = skipwhite(*ptr);
3772 lnum = MAXLNUM;
3773 do
3774 {
3775 switch (*cmd)
3776 {
3777 case '.': /* '.' - Cursor position */
3778 ++cmd;
3779 lnum = curwin->w_cursor.lnum;
3780 break;
3781
3782 case '$': /* '$' - last line */
3783 ++cmd;
3784 lnum = curbuf->b_ml.ml_line_count;
3785 break;
3786
3787 case '\'': /* ''' - mark */
3788 if (*++cmd == NUL)
3789 {
3790 cmd = NULL;
3791 goto error;
3792 }
3793 if (skip)
3794 ++cmd;
3795 else
3796 {
3797 /* Only accept a mark in another file when it is
3798 * used by itself: ":'M". */
3799 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
3800 ++cmd;
3801 if (fp == (pos_T *)-1)
3802 /* Jumped to another file. */
3803 lnum = curwin->w_cursor.lnum;
3804 else
3805 {
3806 if (check_mark(fp) == FAIL)
3807 {
3808 cmd = NULL;
3809 goto error;
3810 }
3811 lnum = fp->lnum;
3812 }
3813 }
3814 break;
3815
3816 case '/':
3817 case '?': /* '/' or '?' - search */
3818 c = *cmd++;
3819 if (skip) /* skip "/pat/" */
3820 {
3821 cmd = skip_regexp(cmd, c, (int)p_magic, NULL);
3822 if (*cmd == c)
3823 ++cmd;
3824 }
3825 else
3826 {
3827 pos = curwin->w_cursor; /* save curwin->w_cursor */
3828 /*
3829 * When '/' or '?' follows another address, start
3830 * from there.
3831 */
3832 if (lnum != MAXLNUM)
3833 curwin->w_cursor.lnum = lnum;
3834 /*
3835 * Start a forward search at the end of the line.
3836 * Start a backward search at the start of the line.
3837 * This makes sure we never match in the current
3838 * line, and can match anywhere in the
3839 * next/previous line.
3840 */
3841 if (c == '/')
3842 curwin->w_cursor.col = MAXCOL;
3843 else
3844 curwin->w_cursor.col = 0;
3845 searchcmdlen = 0;
3846 if (!do_search(NULL, c, cmd, 1L,
3847 SEARCH_HIS + SEARCH_MSG + SEARCH_START))
3848 {
3849 curwin->w_cursor = pos;
3850 cmd = NULL;
3851 goto error;
3852 }
3853 lnum = curwin->w_cursor.lnum;
3854 curwin->w_cursor = pos;
3855 /* adjust command string pointer */
3856 cmd += searchcmdlen;
3857 }
3858 break;
3859
3860 case '\\': /* "\?", "\/" or "\&", repeat search */
3861 ++cmd;
3862 if (*cmd == '&')
3863 i = RE_SUBST;
3864 else if (*cmd == '?' || *cmd == '/')
3865 i = RE_SEARCH;
3866 else
3867 {
3868 EMSG(_(e_backslash));
3869 cmd = NULL;
3870 goto error;
3871 }
3872
3873 if (!skip)
3874 {
3875 /*
3876 * When search follows another address, start from
3877 * there.
3878 */
3879 if (lnum != MAXLNUM)
3880 pos.lnum = lnum;
3881 else
3882 pos.lnum = curwin->w_cursor.lnum;
3883
3884 /*
3885 * Start the search just like for the above
3886 * do_search().
3887 */
3888 if (*cmd != '?')
3889 pos.col = MAXCOL;
3890 else
3891 pos.col = 0;
3892 if (searchit(curwin, curbuf, &pos,
3893 *cmd == '?' ? BACKWARD : FORWARD,
3894 (char_u *)"", 1L,
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003895 SEARCH_MSG + SEARCH_START,
3896 i, (linenr_T)0) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003897 lnum = pos.lnum;
3898 else
3899 {
3900 cmd = NULL;
3901 goto error;
3902 }
3903 }
3904 ++cmd;
3905 break;
3906
3907 default:
3908 if (VIM_ISDIGIT(*cmd)) /* absolute line number */
3909 lnum = getdigits(&cmd);
3910 }
3911
3912 for (;;)
3913 {
3914 cmd = skipwhite(cmd);
3915 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
3916 break;
3917
3918 if (lnum == MAXLNUM)
3919 lnum = curwin->w_cursor.lnum; /* "+1" is same as ".+1" */
3920 if (VIM_ISDIGIT(*cmd))
3921 i = '+'; /* "number" is same as "+number" */
3922 else
3923 i = *cmd++;
3924 if (!VIM_ISDIGIT(*cmd)) /* '+' is '+1', but '+0' is not '+1' */
3925 n = 1;
3926 else
3927 n = getdigits(&cmd);
3928 if (i == '-')
3929 lnum -= n;
3930 else
3931 lnum += n;
3932 }
3933 } while (*cmd == '/' || *cmd == '?');
3934
3935error:
3936 *ptr = cmd;
3937 return lnum;
3938}
3939
3940/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00003941 * Get flags from an Ex command argument.
3942 */
3943 static void
3944get_flags(eap)
3945 exarg_T *eap;
3946{
3947 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
3948 {
3949 if (*eap->arg == 'l')
3950 eap->flags |= EXFLAG_LIST;
3951 else if (*eap->arg == 'p')
3952 eap->flags |= EXFLAG_PRINT;
3953 else
3954 eap->flags |= EXFLAG_NR;
3955 eap->arg = skipwhite(eap->arg + 1);
3956 }
3957}
3958
3959/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003960 * Function called for command which is Not Implemented. NI!
3961 */
3962 void
3963ex_ni(eap)
3964 exarg_T *eap;
3965{
3966 if (!eap->skip)
3967 eap->errmsg = (char_u *)N_("E319: Sorry, the command is not available in this version");
3968}
3969
3970#if !defined(FEAT_PERL) || !defined(FEAT_PYTHON) || !defined(FEAT_TCL) \
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003971 || !defined(FEAT_RUBY) || !defined(FEAT_MZSCHEME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003972/*
3973 * Function called for script command which is Not Implemented. NI!
3974 * Skips over ":perl <<EOF" constructs.
3975 */
3976 static void
3977ex_script_ni(eap)
3978 exarg_T *eap;
3979{
3980 if (!eap->skip)
3981 ex_ni(eap);
3982 else
3983 vim_free(script_get(eap, eap->arg));
3984}
3985#endif
3986
3987/*
3988 * Check range in Ex command for validity.
3989 * Return NULL when valid, error message when invalid.
3990 */
3991 static char_u *
3992invalid_range(eap)
3993 exarg_T *eap;
3994{
3995 if ( eap->line1 < 0
3996 || eap->line2 < 0
3997 || eap->line1 > eap->line2
3998 || ((eap->argt & RANGE)
3999 && !(eap->argt & NOTADR)
4000 && eap->line2 > curbuf->b_ml.ml_line_count
4001#ifdef FEAT_DIFF
4002 + (eap->cmdidx == CMD_diffget)
4003#endif
4004 ))
4005 return (char_u *)_(e_invrange);
4006 return NULL;
4007}
4008
4009/*
4010 * Correct the range for zero line number, if required.
4011 */
4012 static void
4013correct_range(eap)
4014 exarg_T *eap;
4015{
4016 if (!(eap->argt & ZEROR)) /* zero in range not allowed */
4017 {
4018 if (eap->line1 == 0)
4019 eap->line1 = 1;
4020 if (eap->line2 == 0)
4021 eap->line2 = 1;
4022 }
4023}
4024
Bram Moolenaar748bf032005-02-02 23:04:36 +00004025#ifdef FEAT_QUICKFIX
4026static char_u *skip_grep_pat __ARGS((exarg_T *eap));
4027
4028/*
4029 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4030 * pattern. Otherwise return eap->arg.
4031 */
4032 static char_u *
4033skip_grep_pat(eap)
4034 exarg_T *eap;
4035{
4036 char_u *p = eap->arg;
4037
Bram Moolenaara37420f2006-02-04 22:37:47 +00004038 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4039 || eap->cmdidx == CMD_vimgrepadd
4040 || eap->cmdidx == CMD_lvimgrepadd
4041 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004042 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004043 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004044 if (p == NULL)
4045 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004046 }
4047 return p;
4048}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004049
4050/*
4051 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4052 * in the command line, so that things like % get expanded.
4053 */
4054 static char_u *
4055replace_makeprg(eap, p, cmdlinep)
4056 exarg_T *eap;
4057 char_u *p;
4058 char_u **cmdlinep;
4059{
4060 char_u *new_cmdline;
4061 char_u *program;
4062 char_u *pos;
4063 char_u *ptr;
4064 int len;
4065 int i;
4066
4067 /*
4068 * Don't do it when ":vimgrep" is used for ":grep".
4069 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004070 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4071 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4072 || eap->cmdidx == CMD_grepadd
4073 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004074 && !grep_internal(eap->cmdidx))
4075 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004076 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4077 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004078 {
4079 if (*curbuf->b_p_gp == NUL)
4080 program = p_gp;
4081 else
4082 program = curbuf->b_p_gp;
4083 }
4084 else
4085 {
4086 if (*curbuf->b_p_mp == NUL)
4087 program = p_mp;
4088 else
4089 program = curbuf->b_p_mp;
4090 }
4091
4092 p = skipwhite(p);
4093
4094 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4095 {
4096 /* replace $* by given arguments */
4097 i = 1;
4098 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4099 ++i;
4100 len = (int)STRLEN(p);
4101 new_cmdline = alloc((int)(STRLEN(program) + i * (len - 2) + 1));
4102 if (new_cmdline == NULL)
4103 return NULL; /* out of memory */
4104 ptr = new_cmdline;
4105 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4106 {
4107 i = (int)(pos - program);
4108 STRNCPY(ptr, program, i);
4109 STRCPY(ptr += i, p);
4110 ptr += len;
4111 program = pos + 2;
4112 }
4113 STRCPY(ptr, program);
4114 }
4115 else
4116 {
4117 new_cmdline = alloc((int)(STRLEN(program) + STRLEN(p) + 2));
4118 if (new_cmdline == NULL)
4119 return NULL; /* out of memory */
4120 STRCPY(new_cmdline, program);
4121 STRCAT(new_cmdline, " ");
4122 STRCAT(new_cmdline, p);
4123 }
4124 msg_make(p);
4125
4126 /* 'eap->cmd' is not set here, because it is not used at CMD_make */
4127 vim_free(*cmdlinep);
4128 *cmdlinep = new_cmdline;
4129 p = new_cmdline;
4130 }
4131 return p;
4132}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004133#endif
4134
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135/*
4136 * Expand file name in Ex command argument.
4137 * Return FAIL for failure, OK otherwise.
4138 */
4139 int
4140expand_filename(eap, cmdlinep, errormsgp)
4141 exarg_T *eap;
4142 char_u **cmdlinep;
4143 char_u **errormsgp;
4144{
4145 int has_wildcards; /* need to expand wildcards */
4146 char_u *repl;
4147 int srclen;
4148 char_u *p;
4149 int n;
4150
Bram Moolenaar748bf032005-02-02 23:04:36 +00004151#ifdef FEAT_QUICKFIX
4152 /* Skip a regexp pattern for ":vimgrep[add] pat file..." */
4153 p = skip_grep_pat(eap);
4154#else
4155 p = eap->arg;
4156#endif
4157
Bram Moolenaar071d4272004-06-13 20:20:40 +00004158 /*
4159 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4160 * the file name contains a wildcard it should not cause expanding.
4161 * (it will be expanded anyway if there is a wildcard before replacing).
4162 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004163 has_wildcards = mch_has_wildcard(p);
4164 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004166#ifdef FEAT_EVAL
4167 /* Skip over `=expr`, wildcards in it are not expanded. */
4168 if (p[0] == '`' && p[1] == '=')
4169 {
4170 p += 2;
4171 (void)skip_expr(&p);
4172 if (*p == '`')
4173 ++p;
4174 continue;
4175 }
4176#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177 /*
4178 * Quick check if this cannot be the start of a special string.
4179 * Also removes backslash before '%', '#' and '<'.
4180 */
4181 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4182 {
4183 ++p;
4184 continue;
4185 }
4186
4187 /*
4188 * Try to find a match at this position.
4189 */
4190 repl = eval_vars(p, &srclen, &(eap->do_ecmd_lnum), errormsgp, eap->arg);
4191 if (*errormsgp != NULL) /* error detected */
4192 return FAIL;
4193 if (repl == NULL) /* no match found */
4194 {
4195 p += srclen;
4196 continue;
4197 }
4198
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004199 /* Wildcards won't be expanded below, the replacement is taken
4200 * literally. But do expand "~/file", "~user/file" and "$HOME/file". */
4201 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4202 {
4203 char_u *l = repl;
4204
4205 repl = expand_env_save(repl);
4206 vim_free(l);
4207 }
4208
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209 /* Need to escape white space et al. with a backslash. Don't do this
4210 * for shell commands (may have to use quotes instead). Don't do this
4211 * for non-unix systems when there is a single argument (spaces don't
4212 * separate arguments then). */
4213 if (!eap->usefilter
4214 && eap->cmdidx != CMD_bang
4215 && eap->cmdidx != CMD_make
Bram Moolenaara37420f2006-02-04 22:37:47 +00004216 && eap->cmdidx != CMD_lmake
Bram Moolenaar071d4272004-06-13 20:20:40 +00004217 && eap->cmdidx != CMD_grep
Bram Moolenaara37420f2006-02-04 22:37:47 +00004218 && eap->cmdidx != CMD_lgrep
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219 && eap->cmdidx != CMD_grepadd
Bram Moolenaara37420f2006-02-04 22:37:47 +00004220 && eap->cmdidx != CMD_lgrepadd
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221#ifndef UNIX
4222 && !(eap->argt & NOSPC)
4223#endif
4224 )
4225 {
4226 char_u *l;
4227#ifdef BACKSLASH_IN_FILENAME
4228 /* Don't escape a backslash here, because rem_backslash() doesn't
4229 * remove it later. */
4230 static char_u *nobslash = (char_u *)" \t\"|";
4231# define ESCAPE_CHARS nobslash
4232#else
4233# define ESCAPE_CHARS escape_chars
4234#endif
4235
4236 for (l = repl; *l; ++l)
4237 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
4238 {
4239 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
4240 if (l != NULL)
4241 {
4242 vim_free(repl);
4243 repl = l;
4244 }
4245 break;
4246 }
4247 }
4248
4249 /* For a shell command a '!' must be escaped. */
4250 if ((eap->usefilter || eap->cmdidx == CMD_bang)
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004251 && vim_strpbrk(repl, (char_u *)"!&;()<>") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004252 {
4253 char_u *l;
4254
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004255 l = vim_strsave_escaped(repl, (char_u *)"!&;()<>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256 if (l != NULL)
4257 {
4258 vim_free(repl);
4259 repl = l;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004260 /* For a sh-like shell escape "!" another time. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004261 if (strstr((char *)p_sh, "sh") != NULL)
4262 {
4263 l = vim_strsave_escaped(repl, (char_u *)"!");
4264 if (l != NULL)
4265 {
4266 vim_free(repl);
4267 repl = l;
4268 }
4269 }
4270 }
4271 }
4272
4273 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
4274 vim_free(repl);
4275 if (p == NULL)
4276 return FAIL;
4277 }
4278
4279 /*
4280 * One file argument: Expand wildcards.
4281 * Don't do this with ":r !command" or ":w !command".
4282 */
4283 if ((eap->argt & NOSPC) && !eap->usefilter)
4284 {
4285 /*
4286 * May do this twice:
4287 * 1. Replace environment variables.
4288 * 2. Replace any other wildcards, remove backslashes.
4289 */
4290 for (n = 1; n <= 2; ++n)
4291 {
4292 if (n == 2)
4293 {
4294#ifdef UNIX
4295 /*
4296 * Only for Unix we check for more than one file name.
4297 * For other systems spaces are considered to be part
4298 * of the file name.
4299 * Only check here if there is no wildcard, otherwise
4300 * ExpandOne() will check for errors. This allows
4301 * ":e `ls ve*.c`" on Unix.
4302 */
4303 if (!has_wildcards)
4304 for (p = eap->arg; *p; ++p)
4305 {
4306 /* skip escaped characters */
4307 if (p[1] && (*p == '\\' || *p == Ctrl_V))
4308 ++p;
4309 else if (vim_iswhite(*p))
4310 {
4311 *errormsgp = (char_u *)_("E172: Only one file name allowed");
4312 return FAIL;
4313 }
4314 }
4315#endif
4316
4317 /*
4318 * Halve the number of backslashes (this is Vi compatible).
4319 * For Unix and OS/2, when wildcards are expanded, this is
4320 * done by ExpandOne() below.
4321 */
4322#if defined(UNIX) || defined(OS2)
4323 if (!has_wildcards)
4324#endif
4325 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004326 }
4327
4328 if (has_wildcards)
4329 {
4330 if (n == 1)
4331 {
4332 /*
4333 * First loop: May expand environment variables. This
4334 * can be done much faster with expand_env() than with
4335 * something else (e.g., calling a shell).
4336 * After expanding environment variables, check again
4337 * if there are still wildcards present.
4338 */
4339 if (vim_strchr(eap->arg, '$') != NULL
4340 || vim_strchr(eap->arg, '~') != NULL)
4341 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00004342 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
4343 TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004344 has_wildcards = mch_has_wildcard(NameBuff);
4345 p = NameBuff;
4346 }
4347 else
4348 p = NULL;
4349 }
4350 else /* n == 2 */
4351 {
4352 expand_T xpc;
4353
4354 ExpandInit(&xpc);
4355 xpc.xp_context = EXPAND_FILES;
4356 p = ExpandOne(&xpc, eap->arg, NULL,
4357 WILD_LIST_NOTFOUND|WILD_ADD_SLASH,
4358 WILD_EXPAND_FREE);
4359 ExpandCleanup(&xpc);
4360 if (p == NULL)
4361 return FAIL;
4362 }
4363 if (p != NULL)
4364 {
4365 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
4366 p, cmdlinep);
4367 if (n == 2) /* p came from ExpandOne() */
4368 vim_free(p);
4369 }
4370 }
4371 }
4372 }
4373 return OK;
4374}
4375
4376/*
4377 * Replace part of the command line, keeping eap->cmd, eap->arg and
4378 * eap->nextcmd correct.
4379 * "src" points to the part that is to be replaced, of length "srclen".
4380 * "repl" is the replacement string.
4381 * Returns a pointer to the character after the replaced string.
4382 * Returns NULL for failure.
4383 */
4384 static char_u *
4385repl_cmdline(eap, src, srclen, repl, cmdlinep)
4386 exarg_T *eap;
4387 char_u *src;
4388 int srclen;
4389 char_u *repl;
4390 char_u **cmdlinep;
4391{
4392 int len;
4393 int i;
4394 char_u *new_cmdline;
4395
4396 /*
4397 * The new command line is build in new_cmdline[].
4398 * First allocate it.
4399 * Careful: a "+cmd" argument may have been NUL terminated.
4400 */
4401 len = (int)STRLEN(repl);
4402 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004403 if (eap->nextcmd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004404 i += (int)STRLEN(eap->nextcmd);/* add space for next command */
4405 if ((new_cmdline = alloc((unsigned)i)) == NULL)
4406 return NULL; /* out of memory! */
4407
4408 /*
4409 * Copy the stuff before the expanded part.
4410 * Copy the expanded stuff.
4411 * Copy what came after the expanded part.
4412 * Copy the next commands, if there are any.
4413 */
4414 i = (int)(src - *cmdlinep); /* length of part before match */
4415 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00004416
Bram Moolenaar071d4272004-06-13 20:20:40 +00004417 mch_memmove(new_cmdline + i, repl, (size_t)len);
4418 i += len; /* remember the end of the string */
4419 STRCPY(new_cmdline + i, src + srclen);
4420 src = new_cmdline + i; /* remember where to continue */
4421
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004422 if (eap->nextcmd != NULL) /* append next command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004423 {
4424 i = (int)STRLEN(new_cmdline) + 1;
4425 STRCPY(new_cmdline + i, eap->nextcmd);
4426 eap->nextcmd = new_cmdline + i;
4427 }
4428 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
4429 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
4430 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
4431 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
4432 vim_free(*cmdlinep);
4433 *cmdlinep = new_cmdline;
4434
4435 return src;
4436}
4437
4438/*
4439 * Check for '|' to separate commands and '"' to start comments.
4440 */
4441 void
4442separate_nextcmd(eap)
4443 exarg_T *eap;
4444{
4445 char_u *p;
4446
Bram Moolenaar86b68352004-12-27 21:59:20 +00004447#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004448 p = skip_grep_pat(eap);
4449#else
4450 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004451#endif
4452
4453 for ( ; *p; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004454 {
4455 if (*p == Ctrl_V)
4456 {
4457 if (eap->argt & (USECTRLV | XFILE))
4458 ++p; /* skip CTRL-V and next char */
4459 else
4460 STRCPY(p, p + 1); /* remove CTRL-V and skip next char */
4461 if (*p == NUL) /* stop at NUL after CTRL-V */
4462 break;
4463 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004464
4465#ifdef FEAT_EVAL
4466 /* Skip over `=expr` when wildcards are expanded. */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004467 else if (p[0] == '`' && p[1] == '=' && (eap->argt & XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004468 {
4469 p += 2;
4470 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004471 }
4472#endif
4473
Bram Moolenaar071d4272004-06-13 20:20:40 +00004474 /* Check for '"': start of comment or '|': next command */
4475 /* :@" and :*" do not start a comment!
4476 * :redir @" doesn't either. */
4477 else if ((*p == '"' && !(eap->argt & NOTRLCOM)
4478 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
4479 || p != eap->arg)
4480 && (eap->cmdidx != CMD_redir
4481 || p != eap->arg + 1 || p[-1] != '@'))
4482 || *p == '|' || *p == '\n')
4483 {
4484 /*
4485 * We remove the '\' before the '|', unless USECTRLV is used
4486 * AND 'b' is present in 'cpoptions'.
4487 */
4488 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
4489 || !(eap->argt & USECTRLV)) && *(p - 1) == '\\')
4490 {
4491 mch_memmove(p - 1, p, STRLEN(p) + 1); /* remove the '\' */
4492 --p;
4493 }
4494 else
4495 {
4496 eap->nextcmd = check_nextcmd(p);
4497 *p = NUL;
4498 break;
4499 }
4500 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004501 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004502
Bram Moolenaar071d4272004-06-13 20:20:40 +00004503 if (!(eap->argt & NOTRLCOM)) /* remove trailing spaces */
4504 del_trailing_spaces(eap->arg);
4505}
4506
4507/*
4508 * get + command from ex argument
4509 */
4510 static char_u *
4511getargcmd(argp)
4512 char_u **argp;
4513{
4514 char_u *arg = *argp;
4515 char_u *command = NULL;
4516
4517 if (*arg == '+') /* +[command] */
4518 {
4519 ++arg;
4520 if (vim_isspace(*arg))
4521 command = dollar_command;
4522 else
4523 {
4524 command = arg;
4525 arg = skip_cmd_arg(command, TRUE);
4526 if (*arg != NUL)
4527 *arg++ = NUL; /* terminate command with NUL */
4528 }
4529
4530 arg = skipwhite(arg); /* skip over spaces */
4531 *argp = arg;
4532 }
4533 return command;
4534}
4535
4536/*
4537 * Find end of "+command" argument. Skip over "\ " and "\\".
4538 */
4539 static char_u *
4540skip_cmd_arg(p, rembs)
4541 char_u *p;
4542 int rembs; /* TRUE to halve the number of backslashes */
4543{
4544 while (*p && !vim_isspace(*p))
4545 {
4546 if (*p == '\\' && p[1] != NUL)
4547 {
4548 if (rembs)
4549 mch_memmove(p, p + 1, STRLEN(p));
4550 else
4551 ++p;
4552 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004553 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004554 }
4555 return p;
4556}
4557
4558/*
4559 * Get "++opt=arg" argument.
4560 * Return FAIL or OK.
4561 */
4562 static int
4563getargopt(eap)
4564 exarg_T *eap;
4565{
4566 char_u *arg = eap->arg + 2;
4567 int *pp = NULL;
4568#ifdef FEAT_MBYTE
4569 char_u *p;
4570#endif
4571
4572 /* ":edit ++[no]bin[ary] file" */
4573 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
4574 {
4575 if (*arg == 'n')
4576 {
4577 arg += 2;
4578 eap->force_bin = FORCE_NOBIN;
4579 }
4580 else
4581 eap->force_bin = FORCE_BIN;
4582 if (!checkforcmd(&arg, "binary", 3))
4583 return FAIL;
4584 eap->arg = skipwhite(arg);
4585 return OK;
4586 }
4587
4588 if (STRNCMP(arg, "ff", 2) == 0)
4589 {
4590 arg += 2;
4591 pp = &eap->force_ff;
4592 }
4593 else if (STRNCMP(arg, "fileformat", 10) == 0)
4594 {
4595 arg += 10;
4596 pp = &eap->force_ff;
4597 }
4598#ifdef FEAT_MBYTE
4599 else if (STRNCMP(arg, "enc", 3) == 0)
4600 {
4601 arg += 3;
4602 pp = &eap->force_enc;
4603 }
4604 else if (STRNCMP(arg, "encoding", 8) == 0)
4605 {
4606 arg += 8;
4607 pp = &eap->force_enc;
4608 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004609 else if (STRNCMP(arg, "bad", 3) == 0)
4610 {
4611 arg += 3;
4612 pp = &eap->bad_char;
4613 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614#endif
4615
4616 if (pp == NULL || *arg != '=')
4617 return FAIL;
4618
4619 ++arg;
4620 *pp = (int)(arg - eap->cmd);
4621 arg = skip_cmd_arg(arg, FALSE);
4622 eap->arg = skipwhite(arg);
4623 *arg = NUL;
4624
4625#ifdef FEAT_MBYTE
4626 if (pp == &eap->force_ff)
4627 {
4628#endif
4629 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
4630 return FAIL;
4631#ifdef FEAT_MBYTE
4632 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004633 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634 {
4635 /* Make 'fileencoding' lower case. */
4636 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
4637 *p = TOLOWER_ASC(*p);
4638 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004639 else
4640 {
4641 /* Check ++bad= argument. Must be a single-byte character, "keep" or
4642 * "drop". */
4643 p = eap->cmd + eap->bad_char;
4644 if (STRICMP(p, "keep") == 0)
4645 eap->bad_char = BAD_KEEP;
4646 else if (STRICMP(p, "drop") == 0)
4647 eap->bad_char = BAD_DROP;
4648 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
4649 eap->bad_char = *p;
4650 else
4651 return FAIL;
4652 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004653#endif
4654
4655 return OK;
4656}
4657
4658/*
4659 * ":abbreviate" and friends.
4660 */
4661 static void
4662ex_abbreviate(eap)
4663 exarg_T *eap;
4664{
4665 do_exmap(eap, TRUE); /* almost the same as mapping */
4666}
4667
4668/*
4669 * ":map" and friends.
4670 */
4671 static void
4672ex_map(eap)
4673 exarg_T *eap;
4674{
4675 /*
4676 * If we are sourcing .exrc or .vimrc in current directory we
4677 * print the mappings for security reasons.
4678 */
4679 if (secure)
4680 {
4681 secure = 2;
4682 msg_outtrans(eap->cmd);
4683 msg_putchar('\n');
4684 }
4685 do_exmap(eap, FALSE);
4686}
4687
4688/*
4689 * ":unmap" and friends.
4690 */
4691 static void
4692ex_unmap(eap)
4693 exarg_T *eap;
4694{
4695 do_exmap(eap, FALSE);
4696}
4697
4698/*
4699 * ":mapclear" and friends.
4700 */
4701 static void
4702ex_mapclear(eap)
4703 exarg_T *eap;
4704{
4705 map_clear(eap->cmd, eap->arg, eap->forceit, FALSE);
4706}
4707
4708/*
4709 * ":abclear" and friends.
4710 */
4711 static void
4712ex_abclear(eap)
4713 exarg_T *eap;
4714{
4715 map_clear(eap->cmd, eap->arg, TRUE, TRUE);
4716}
4717
4718#ifdef FEAT_AUTOCMD
4719 static void
4720ex_autocmd(eap)
4721 exarg_T *eap;
4722{
4723 /*
4724 * Disallow auto commands from .exrc and .vimrc in current
4725 * directory for security reasons.
4726 */
4727 if (secure)
4728 {
4729 secure = 2;
4730 eap->errmsg = e_curdir;
4731 }
4732 else if (eap->cmdidx == CMD_autocmd)
4733 do_autocmd(eap->arg, eap->forceit);
4734 else
4735 do_augroup(eap->arg, eap->forceit);
4736}
4737
4738/*
4739 * ":doautocmd": Apply the automatic commands to the current buffer.
4740 */
4741 static void
4742ex_doautocmd(eap)
4743 exarg_T *eap;
4744{
4745 (void)do_doautocmd(eap->arg, TRUE);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00004746 do_modelines(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004747}
4748#endif
4749
4750#ifdef FEAT_LISTCMDS
4751/*
4752 * :[N]bunload[!] [N] [bufname] unload buffer
4753 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
4754 * :[N]bwipeout[!] [N] [bufname] delete buffer really
4755 */
4756 static void
4757ex_bunload(eap)
4758 exarg_T *eap;
4759{
4760 eap->errmsg = do_bufdel(
4761 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
4762 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
4763 : DOBUF_UNLOAD, eap->arg,
4764 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
4765}
4766
4767/*
4768 * :[N]buffer [N] to buffer N
4769 * :[N]sbuffer [N] to buffer N
4770 */
4771 static void
4772ex_buffer(eap)
4773 exarg_T *eap;
4774{
4775 if (*eap->arg)
4776 eap->errmsg = e_trailing;
4777 else
4778 {
4779 if (eap->addr_count == 0) /* default is current buffer */
4780 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
4781 else
4782 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
4783 }
4784}
4785
4786/*
4787 * :[N]bmodified [N] to next mod. buffer
4788 * :[N]sbmodified [N] to next mod. buffer
4789 */
4790 static void
4791ex_bmodified(eap)
4792 exarg_T *eap;
4793{
4794 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
4795}
4796
4797/*
4798 * :[N]bnext [N] to next buffer
4799 * :[N]sbnext [N] split and to next buffer
4800 */
4801 static void
4802ex_bnext(eap)
4803 exarg_T *eap;
4804{
4805 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
4806}
4807
4808/*
4809 * :[N]bNext [N] to previous buffer
4810 * :[N]bprevious [N] to previous buffer
4811 * :[N]sbNext [N] split and to previous buffer
4812 * :[N]sbprevious [N] split and to previous buffer
4813 */
4814 static void
4815ex_bprevious(eap)
4816 exarg_T *eap;
4817{
4818 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
4819}
4820
4821/*
4822 * :brewind to first buffer
4823 * :bfirst to first buffer
4824 * :sbrewind split and to first buffer
4825 * :sbfirst split and to first buffer
4826 */
4827 static void
4828ex_brewind(eap)
4829 exarg_T *eap;
4830{
4831 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
4832}
4833
4834/*
4835 * :blast to last buffer
4836 * :sblast split and to last buffer
4837 */
4838 static void
4839ex_blast(eap)
4840 exarg_T *eap;
4841{
4842 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
4843}
4844#endif
4845
4846 int
4847ends_excmd(c)
4848 int c;
4849{
4850 return (c == NUL || c == '|' || c == '"' || c == '\n');
4851}
4852
4853#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
4854 || defined(PROTO)
4855/*
4856 * Return the next command, after the first '|' or '\n'.
4857 * Return NULL if not found.
4858 */
4859 char_u *
4860find_nextcmd(p)
4861 char_u *p;
4862{
4863 while (*p != '|' && *p != '\n')
4864 {
4865 if (*p == NUL)
4866 return NULL;
4867 ++p;
4868 }
4869 return (p + 1);
4870}
4871#endif
4872
4873/*
4874 * Check if *p is a separator between Ex commands.
4875 * Return NULL if it isn't, (p + 1) if it is.
4876 */
4877 char_u *
4878check_nextcmd(p)
4879 char_u *p;
4880{
4881 p = skipwhite(p);
4882 if (*p == '|' || *p == '\n')
4883 return (p + 1);
4884 else
4885 return NULL;
4886}
4887
4888/*
4889 * - if there are more files to edit
4890 * - and this is the last window
4891 * - and forceit not used
4892 * - and not repeated twice on a row
4893 * return FAIL and give error message if 'message' TRUE
4894 * return OK otherwise
4895 */
4896 static int
4897check_more(message, forceit)
4898 int message; /* when FALSE check only, no messages */
4899 int forceit;
4900{
4901 int n = ARGCOUNT - curwin->w_arg_idx - 1;
4902
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00004903 if (!forceit && only_one_window()
4904 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004905 {
4906 if (message)
4907 {
4908#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
4909 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
4910 {
4911 char_u buff[IOSIZE];
4912
4913 if (n == 1)
4914 STRCPY(buff, _("1 more file to edit. Quit anyway?"));
4915 else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004916 vim_snprintf((char *)buff, IOSIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917 _("%d more files to edit. Quit anyway?"), n);
4918 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
4919 return OK;
4920 return FAIL;
4921 }
4922#endif
4923 if (n == 1)
4924 EMSG(_("E173: 1 more file to edit"));
4925 else
4926 EMSGN(_("E173: %ld more files to edit"), n);
4927 quitmore = 2; /* next try to quit is allowed */
4928 }
4929 return FAIL;
4930 }
4931 return OK;
4932}
4933
4934#ifdef FEAT_CMDL_COMPL
4935/*
4936 * Function given to ExpandGeneric() to obtain the list of command names.
4937 */
4938/*ARGSUSED*/
4939 char_u *
4940get_command_name(xp, idx)
4941 expand_T *xp;
4942 int idx;
4943{
4944 if (idx >= (int)CMD_SIZE)
4945# ifdef FEAT_USR_CMDS
4946 return get_user_command_name(idx);
4947# else
4948 return NULL;
4949# endif
4950 return cmdnames[idx].cmd_name;
4951}
4952#endif
4953
4954#if defined(FEAT_USR_CMDS) || defined(PROTO)
4955static 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));
4956static void uc_list __ARGS((char_u *name, size_t name_len));
4957static int uc_scan_attr __ARGS((char_u *attr, size_t len, long *argt, long *def, int *flags, int *compl, char_u **compl_arg));
4958static char_u *uc_split_args __ARGS((char_u *arg, size_t *lenp));
4959static 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));
4960
4961 static int
4962uc_add_command(name, name_len, rep, argt, def, flags, compl, compl_arg, force)
4963 char_u *name;
4964 size_t name_len;
4965 char_u *rep;
4966 long argt;
4967 long def;
4968 int flags;
4969 int compl;
4970 char_u *compl_arg;
4971 int force;
4972{
4973 ucmd_T *cmd = NULL;
4974 char_u *p;
4975 int i;
4976 int cmp = 1;
4977 char_u *rep_buf = NULL;
4978 garray_T *gap;
4979
4980 replace_termcodes(rep, &rep_buf, FALSE, FALSE);
4981 if (rep_buf == NULL)
4982 {
4983 /* Can't replace termcodes - try using the string as is */
4984 rep_buf = vim_strsave(rep);
4985
4986 /* Give up if out of memory */
4987 if (rep_buf == NULL)
4988 return FAIL;
4989 }
4990
4991 /* get address of growarray: global or in curbuf */
4992 if (flags & UC_BUFFER)
4993 {
4994 gap = &curbuf->b_ucmds;
4995 if (gap->ga_itemsize == 0)
4996 ga_init2(gap, (int)sizeof(ucmd_T), 4);
4997 }
4998 else
4999 gap = &ucmds;
5000
5001 /* Search for the command in the already defined commands. */
5002 for (i = 0; i < gap->ga_len; ++i)
5003 {
5004 size_t len;
5005
5006 cmd = USER_CMD_GA(gap, i);
5007 len = STRLEN(cmd->uc_name);
5008 cmp = STRNCMP(name, cmd->uc_name, name_len);
5009 if (cmp == 0)
5010 {
5011 if (name_len < len)
5012 cmp = -1;
5013 else if (name_len > len)
5014 cmp = 1;
5015 }
5016
5017 if (cmp == 0)
5018 {
5019 if (!force)
5020 {
5021 EMSG(_("E174: Command already exists: add ! to replace it"));
5022 goto fail;
5023 }
5024
5025 vim_free(cmd->uc_rep);
5026 cmd->uc_rep = 0;
5027 break;
5028 }
5029
5030 /* Stop as soon as we pass the name to add */
5031 if (cmp < 0)
5032 break;
5033 }
5034
5035 /* Extend the array unless we're replacing an existing command */
5036 if (cmp != 0)
5037 {
5038 if (ga_grow(gap, 1) != OK)
5039 goto fail;
5040 if ((p = vim_strnsave(name, (int)name_len)) == NULL)
5041 goto fail;
5042
5043 cmd = USER_CMD_GA(gap, i);
5044 mch_memmove(cmd + 1, cmd, (gap->ga_len - i) * sizeof(ucmd_T));
5045
5046 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047
5048 cmd->uc_name = p;
5049 }
5050
5051 cmd->uc_rep = rep_buf;
5052 cmd->uc_argt = argt;
5053 cmd->uc_def = def;
5054 cmd->uc_compl = compl;
5055#ifdef FEAT_EVAL
5056 cmd->uc_scriptID = current_SID;
5057# ifdef FEAT_CMDL_COMPL
5058 cmd->uc_compl_arg = compl_arg;
5059# endif
5060#endif
5061
5062 return OK;
5063
5064fail:
5065 vim_free(rep_buf);
5066#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5067 vim_free(compl_arg);
5068#endif
5069 return FAIL;
5070}
5071
5072/*
5073 * List of names for completion for ":command" with the EXPAND_ flag.
5074 * Must be alphabetical for completion.
5075 */
5076static struct
5077{
5078 int expand;
5079 char *name;
5080} command_complete[] =
5081{
5082 {EXPAND_AUGROUP, "augroup"},
5083 {EXPAND_BUFFERS, "buffer"},
5084 {EXPAND_COMMANDS, "command"},
5085#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5086 {EXPAND_USER_DEFINED, "custom"},
Bram Moolenaara466c992005-07-09 21:03:22 +00005087 {EXPAND_USER_LIST, "customlist"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005088#endif
5089 {EXPAND_DIRECTORIES, "dir"},
5090 {EXPAND_ENV_VARS, "environment"},
5091 {EXPAND_EVENTS, "event"},
5092 {EXPAND_EXPRESSION, "expression"},
5093 {EXPAND_FILES, "file"},
5094 {EXPAND_FUNCTIONS, "function"},
5095 {EXPAND_HELP, "help"},
5096 {EXPAND_HIGHLIGHT, "highlight"},
5097 {EXPAND_MAPPINGS, "mapping"},
5098 {EXPAND_MENUS, "menu"},
5099 {EXPAND_SETTINGS, "option"},
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005100 {EXPAND_SHELLCMD, "shellcmd"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005101 {EXPAND_TAGS, "tag"},
5102 {EXPAND_TAGS_LISTFILES, "tag_listfiles"},
5103 {EXPAND_USER_VARS, "var"},
5104 {0, NULL}
5105};
5106
5107 static void
5108uc_list(name, name_len)
5109 char_u *name;
5110 size_t name_len;
5111{
5112 int i, j;
5113 int found = FALSE;
5114 ucmd_T *cmd;
5115 int len;
5116 long a;
5117 garray_T *gap;
5118
5119 gap = &curbuf->b_ucmds;
5120 for (;;)
5121 {
5122 for (i = 0; i < gap->ga_len; ++i)
5123 {
5124 cmd = USER_CMD_GA(gap, i);
5125 a = cmd->uc_argt;
5126
5127 /* Skip commands which don't match the requested prefix */
5128 if (STRNCMP(name, cmd->uc_name, name_len) != 0)
5129 continue;
5130
5131 /* Put out the title first time */
5132 if (!found)
5133 MSG_PUTS_TITLE(_("\n Name Args Range Complete Definition"));
5134 found = TRUE;
5135 msg_putchar('\n');
5136 if (got_int)
5137 break;
5138
5139 /* Special cases */
5140 msg_putchar(a & BANG ? '!' : ' ');
5141 msg_putchar(a & REGSTR ? '"' : ' ');
5142 msg_putchar(gap != &ucmds ? 'b' : ' ');
5143 msg_putchar(' ');
5144
5145 msg_outtrans_attr(cmd->uc_name, hl_attr(HLF_D));
5146 len = (int)STRLEN(cmd->uc_name) + 4;
5147
5148 do {
5149 msg_putchar(' ');
5150 ++len;
5151 } while (len < 16);
5152
5153 len = 0;
5154
5155 /* Arguments */
5156 switch ((int)(a & (EXTRA|NOSPC|NEEDARG)))
5157 {
5158 case 0: IObuff[len++] = '0'; break;
5159 case (EXTRA): IObuff[len++] = '*'; break;
5160 case (EXTRA|NOSPC): IObuff[len++] = '?'; break;
5161 case (EXTRA|NEEDARG): IObuff[len++] = '+'; break;
5162 case (EXTRA|NOSPC|NEEDARG): IObuff[len++] = '1'; break;
5163 }
5164
5165 do {
5166 IObuff[len++] = ' ';
5167 } while (len < 5);
5168
5169 /* Range */
5170 if (a & (RANGE|COUNT))
5171 {
5172 if (a & COUNT)
5173 {
5174 /* -count=N */
5175 sprintf((char *)IObuff + len, "%ldc", cmd->uc_def);
5176 len += (int)STRLEN(IObuff + len);
5177 }
5178 else if (a & DFLALL)
5179 IObuff[len++] = '%';
5180 else if (cmd->uc_def >= 0)
5181 {
5182 /* -range=N */
5183 sprintf((char *)IObuff + len, "%ld", cmd->uc_def);
5184 len += (int)STRLEN(IObuff + len);
5185 }
5186 else
5187 IObuff[len++] = '.';
5188 }
5189
5190 do {
5191 IObuff[len++] = ' ';
5192 } while (len < 11);
5193
5194 /* Completion */
5195 for (j = 0; command_complete[j].expand != 0; ++j)
5196 if (command_complete[j].expand == cmd->uc_compl)
5197 {
5198 STRCPY(IObuff + len, command_complete[j].name);
5199 len += (int)STRLEN(IObuff + len);
5200 break;
5201 }
5202
5203 do {
5204 IObuff[len++] = ' ';
5205 } while (len < 21);
5206
5207 IObuff[len] = '\0';
5208 msg_outtrans(IObuff);
5209
5210 msg_outtrans_special(cmd->uc_rep, FALSE);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005211#ifdef FEAT_EVAL
5212 if (p_verbose > 0)
5213 last_set_msg(cmd->uc_scriptID);
5214#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005215 out_flush();
5216 ui_breakcheck();
5217 if (got_int)
5218 break;
5219 }
5220 if (gap == &ucmds || i < gap->ga_len)
5221 break;
5222 gap = &ucmds;
5223 }
5224
5225 if (!found)
5226 MSG(_("No user-defined commands found"));
5227}
5228
5229 static char_u *
5230uc_fun_cmd()
5231{
5232 static char_u fcmd[] = {0x84, 0xaf, 0x60, 0xb9, 0xaf, 0xb5, 0x60, 0xa4,
5233 0xa5, 0xad, 0xa1, 0xae, 0xa4, 0x60, 0xa1, 0x60,
5234 0xb3, 0xa8, 0xb2, 0xb5, 0xa2, 0xa2, 0xa5, 0xb2,
5235 0xb9, 0x7f, 0};
5236 int i;
5237
5238 for (i = 0; fcmd[i]; ++i)
5239 IObuff[i] = fcmd[i] - 0x40;
5240 IObuff[i] = 0;
5241 return IObuff;
5242}
5243
5244 static int
5245uc_scan_attr(attr, len, argt, def, flags, compl, compl_arg)
5246 char_u *attr;
5247 size_t len;
5248 long *argt;
5249 long *def;
5250 int *flags;
5251 int *compl;
5252 char_u **compl_arg;
5253{
5254 char_u *p;
5255
5256 if (len == 0)
5257 {
5258 EMSG(_("E175: No attribute specified"));
5259 return FAIL;
5260 }
5261
5262 /* First, try the simple attributes (no arguments) */
5263 if (STRNICMP(attr, "bang", len) == 0)
5264 *argt |= BANG;
5265 else if (STRNICMP(attr, "buffer", len) == 0)
5266 *flags |= UC_BUFFER;
5267 else if (STRNICMP(attr, "register", len) == 0)
5268 *argt |= REGSTR;
5269 else if (STRNICMP(attr, "bar", len) == 0)
5270 *argt |= TRLBAR;
5271 else
5272 {
5273 int i;
5274 char_u *val = NULL;
5275 size_t vallen = 0;
5276 size_t attrlen = len;
5277
5278 /* Look for the attribute name - which is the part before any '=' */
5279 for (i = 0; i < (int)len; ++i)
5280 {
5281 if (attr[i] == '=')
5282 {
5283 val = &attr[i + 1];
5284 vallen = len - i - 1;
5285 attrlen = i;
5286 break;
5287 }
5288 }
5289
5290 if (STRNICMP(attr, "nargs", attrlen) == 0)
5291 {
5292 if (vallen == 1)
5293 {
5294 if (*val == '0')
5295 /* Do nothing - this is the default */;
5296 else if (*val == '1')
5297 *argt |= (EXTRA | NOSPC | NEEDARG);
5298 else if (*val == '*')
5299 *argt |= EXTRA;
5300 else if (*val == '?')
5301 *argt |= (EXTRA | NOSPC);
5302 else if (*val == '+')
5303 *argt |= (EXTRA | NEEDARG);
5304 else
5305 goto wrong_nargs;
5306 }
5307 else
5308 {
5309wrong_nargs:
5310 EMSG(_("E176: Invalid number of arguments"));
5311 return FAIL;
5312 }
5313 }
5314 else if (STRNICMP(attr, "range", attrlen) == 0)
5315 {
5316 *argt |= RANGE;
5317 if (vallen == 1 && *val == '%')
5318 *argt |= DFLALL;
5319 else if (val != NULL)
5320 {
5321 p = val;
5322 if (*def >= 0)
5323 {
5324two_count:
5325 EMSG(_("E177: Count cannot be specified twice"));
5326 return FAIL;
5327 }
5328
5329 *def = getdigits(&p);
5330 *argt |= (ZEROR | NOTADR);
5331
5332 if (p != val + vallen || vallen == 0)
5333 {
5334invalid_count:
5335 EMSG(_("E178: Invalid default value for count"));
5336 return FAIL;
5337 }
5338 }
5339 }
5340 else if (STRNICMP(attr, "count", attrlen) == 0)
5341 {
Bram Moolenaar32e7b2d2005-02-27 22:36:47 +00005342 *argt |= (COUNT | ZEROR | RANGE | NOTADR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005343
5344 if (val != NULL)
5345 {
5346 p = val;
5347 if (*def >= 0)
5348 goto two_count;
5349
5350 *def = getdigits(&p);
5351
5352 if (p != val + vallen)
5353 goto invalid_count;
5354 }
5355
5356 if (*def < 0)
5357 *def = 0;
5358 }
5359 else if (STRNICMP(attr, "complete", attrlen) == 0)
5360 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005361 if (val == NULL)
5362 {
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005363 EMSG(_("E179: argument required for -complete"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005364 return FAIL;
5365 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005366
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005367 if (parse_compl_arg(val, (int)vallen, compl, argt, compl_arg)
5368 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005369 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005370 }
5371 else
5372 {
5373 char_u ch = attr[len];
5374 attr[len] = '\0';
5375 EMSG2(_("E181: Invalid attribute: %s"), attr);
5376 attr[len] = ch;
5377 return FAIL;
5378 }
5379 }
5380
5381 return OK;
5382}
5383
5384 static void
5385ex_command(eap)
5386 exarg_T *eap;
5387{
5388 char_u *name;
5389 char_u *end;
5390 char_u *p;
5391 long argt = 0;
5392 long def = -1;
5393 int flags = 0;
5394 int compl = EXPAND_NOTHING;
5395 char_u *compl_arg = NULL;
5396 int has_attr = (eap->arg[0] == '-');
5397
5398 p = eap->arg;
5399
5400 /* Check for attributes */
5401 while (*p == '-')
5402 {
5403 ++p;
5404 end = skiptowhite(p);
5405 if (uc_scan_attr(p, end - p, &argt, &def, &flags, &compl, &compl_arg)
5406 == FAIL)
5407 return;
5408 p = skipwhite(end);
5409 }
5410
5411 /* Get the name (if any) and skip to the following argument */
5412 name = p;
5413 if (ASCII_ISALPHA(*p))
5414 while (ASCII_ISALNUM(*p))
5415 ++p;
5416 if (!ends_excmd(*p) && !vim_iswhite(*p))
5417 {
5418 EMSG(_("E182: Invalid command name"));
5419 return;
5420 }
5421 end = p;
5422
5423 /* If there is nothing after the name, and no attributes were specified,
5424 * we are listing commands
5425 */
5426 p = skipwhite(end);
5427 if (!has_attr && ends_excmd(*p))
5428 {
5429 uc_list(name, end - name);
5430 }
5431 else if (!ASCII_ISUPPER(*name))
5432 {
5433 EMSG(_("E183: User defined commands must start with an uppercase letter"));
5434 return;
5435 }
5436 else
5437 uc_add_command(name, end - name, p, argt, def, flags, compl, compl_arg,
5438 eap->forceit);
5439}
5440
5441/*
5442 * ":comclear"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005443 * Clear all user commands, global and for current buffer.
5444 */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00005445/*ARGSUSED*/
5446 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005447ex_comclear(eap)
5448 exarg_T *eap;
5449{
5450 uc_clear(&ucmds);
5451 uc_clear(&curbuf->b_ucmds);
5452}
5453
5454/*
5455 * Clear all user commands for "gap".
5456 */
5457 void
5458uc_clear(gap)
5459 garray_T *gap;
5460{
5461 int i;
5462 ucmd_T *cmd;
5463
5464 for (i = 0; i < gap->ga_len; ++i)
5465 {
5466 cmd = USER_CMD_GA(gap, i);
5467 vim_free(cmd->uc_name);
5468 vim_free(cmd->uc_rep);
5469# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5470 vim_free(cmd->uc_compl_arg);
5471# endif
5472 }
5473 ga_clear(gap);
5474}
5475
5476 static void
5477ex_delcommand(eap)
5478 exarg_T *eap;
5479{
5480 int i = 0;
5481 ucmd_T *cmd = NULL;
5482 int cmp = -1;
5483 garray_T *gap;
5484
5485 gap = &curbuf->b_ucmds;
5486 for (;;)
5487 {
5488 for (i = 0; i < gap->ga_len; ++i)
5489 {
5490 cmd = USER_CMD_GA(gap, i);
5491 cmp = STRCMP(eap->arg, cmd->uc_name);
5492 if (cmp <= 0)
5493 break;
5494 }
5495 if (gap == &ucmds || cmp == 0)
5496 break;
5497 gap = &ucmds;
5498 }
5499
5500 if (cmp != 0)
5501 {
5502 EMSG2(_("E184: No such user-defined command: %s"), eap->arg);
5503 return;
5504 }
5505
5506 vim_free(cmd->uc_name);
5507 vim_free(cmd->uc_rep);
5508# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5509 vim_free(cmd->uc_compl_arg);
5510# endif
5511
5512 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005513
5514 if (i < gap->ga_len)
5515 mch_memmove(cmd, cmd + 1, (gap->ga_len - i) * sizeof(ucmd_T));
5516}
5517
5518 static char_u *
5519uc_split_args(arg, lenp)
5520 char_u *arg;
5521 size_t *lenp;
5522{
5523 char_u *buf;
5524 char_u *p;
5525 char_u *q;
5526 int len;
5527
5528 /* Precalculate length */
5529 p = arg;
5530 len = 2; /* Initial and final quotes */
5531
5532 while (*p)
5533 {
5534 if (p[0] == '\\' && vim_iswhite(p[1]))
5535 {
5536 len += 1;
5537 p += 2;
5538 }
5539 else if (*p == '\\' || *p == '"')
5540 {
5541 len += 2;
5542 p += 1;
5543 }
5544 else if (vim_iswhite(*p))
5545 {
5546 p = skipwhite(p);
5547 if (*p == NUL)
5548 break;
5549 len += 3; /* "," */
5550 }
5551 else
5552 {
5553 ++len;
5554 ++p;
5555 }
5556 }
5557
5558 buf = alloc(len + 1);
5559 if (buf == NULL)
5560 {
5561 *lenp = 0;
5562 return buf;
5563 }
5564
5565 p = arg;
5566 q = buf;
5567 *q++ = '"';
5568 while (*p)
5569 {
5570 if (p[0] == '\\' && vim_iswhite(p[1]))
5571 {
5572 *q++ = p[1];
5573 p += 2;
5574 }
5575 else if (*p == '\\' || *p == '"')
5576 {
5577 *q++ = '\\';
5578 *q++ = *p++;
5579 }
5580 else if (vim_iswhite(*p))
5581 {
5582 p = skipwhite(p);
5583 if (*p == NUL)
5584 break;
5585 *q++ = '"';
5586 *q++ = ',';
5587 *q++ = '"';
5588 }
5589 else
5590 {
5591 *q++ = *p++;
5592 }
5593 }
5594 *q++ = '"';
5595 *q = 0;
5596
5597 *lenp = len;
5598 return buf;
5599}
5600
5601/*
5602 * Check for a <> code in a user command.
5603 * "code" points to the '<'. "len" the length of the <> (inclusive).
5604 * "buf" is where the result is to be added.
5605 * "split_buf" points to a buffer used for splitting, caller should free it.
5606 * "split_len" is the length of what "split_buf" contains.
5607 * Returns the length of the replacement, which has been added to "buf".
5608 * Returns -1 if there was no match, and only the "<" has been copied.
5609 */
5610 static size_t
5611uc_check_code(code, len, buf, cmd, eap, split_buf, split_len)
5612 char_u *code;
5613 size_t len;
5614 char_u *buf;
5615 ucmd_T *cmd; /* the user command we're expanding */
5616 exarg_T *eap; /* ex arguments */
5617 char_u **split_buf;
5618 size_t *split_len;
5619{
5620 size_t result = 0;
5621 char_u *p = code + 1;
5622 size_t l = len - 2;
5623 int quote = 0;
5624 enum { ct_ARGS, ct_BANG, ct_COUNT, ct_LINE1, ct_LINE2, ct_REGISTER,
5625 ct_LT, ct_NONE } type = ct_NONE;
5626
5627 if ((vim_strchr((char_u *)"qQfF", *p) != NULL) && p[1] == '-')
5628 {
5629 quote = (*p == 'q' || *p == 'Q') ? 1 : 2;
5630 p += 2;
5631 l -= 2;
5632 }
5633
5634 if (l < 1)
5635 type = ct_NONE;
5636 else if (STRNICMP(p, "args", l) == 0)
5637 type = ct_ARGS;
5638 else if (STRNICMP(p, "bang", l) == 0)
5639 type = ct_BANG;
5640 else if (STRNICMP(p, "count", l) == 0)
5641 type = ct_COUNT;
5642 else if (STRNICMP(p, "line1", l) == 0)
5643 type = ct_LINE1;
5644 else if (STRNICMP(p, "line2", l) == 0)
5645 type = ct_LINE2;
5646 else if (STRNICMP(p, "lt", l) == 0)
5647 type = ct_LT;
5648 else if (STRNICMP(p, "register", l) == 0)
5649 type = ct_REGISTER;
5650
5651 switch (type)
5652 {
5653 case ct_ARGS:
5654 /* Simple case first */
5655 if (*eap->arg == NUL)
5656 {
5657 if (quote == 1)
5658 {
5659 result = 2;
5660 if (buf != NULL)
5661 STRCPY(buf, "''");
5662 }
5663 else
5664 result = 0;
5665 break;
5666 }
5667
5668 /* When specified there is a single argument don't split it.
5669 * Works for ":Cmd %" when % is "a b c". */
5670 if ((eap->argt & NOSPC) && quote == 2)
5671 quote = 1;
5672
5673 switch (quote)
5674 {
5675 case 0: /* No quoting, no splitting */
5676 result = STRLEN(eap->arg);
5677 if (buf != NULL)
5678 STRCPY(buf, eap->arg);
5679 break;
5680 case 1: /* Quote, but don't split */
5681 result = STRLEN(eap->arg) + 2;
5682 for (p = eap->arg; *p; ++p)
5683 {
5684 if (*p == '\\' || *p == '"')
5685 ++result;
5686 }
5687
5688 if (buf != NULL)
5689 {
5690 *buf++ = '"';
5691 for (p = eap->arg; *p; ++p)
5692 {
5693 if (*p == '\\' || *p == '"')
5694 *buf++ = '\\';
5695 *buf++ = *p;
5696 }
5697 *buf = '"';
5698 }
5699
5700 break;
5701 case 2: /* Quote and split */
5702 /* This is hard, so only do it once, and cache the result */
5703 if (*split_buf == NULL)
5704 *split_buf = uc_split_args(eap->arg, split_len);
5705
5706 result = *split_len;
5707 if (buf != NULL && result != 0)
5708 STRCPY(buf, *split_buf);
5709
5710 break;
5711 }
5712 break;
5713
5714 case ct_BANG:
5715 result = eap->forceit ? 1 : 0;
5716 if (quote)
5717 result += 2;
5718 if (buf != NULL)
5719 {
5720 if (quote)
5721 *buf++ = '"';
5722 if (eap->forceit)
5723 *buf++ = '!';
5724 if (quote)
5725 *buf = '"';
5726 }
5727 break;
5728
5729 case ct_LINE1:
5730 case ct_LINE2:
5731 case ct_COUNT:
5732 {
5733 char num_buf[20];
5734 long num = (type == ct_LINE1) ? eap->line1 :
5735 (type == ct_LINE2) ? eap->line2 :
5736 (eap->addr_count > 0) ? eap->line2 : cmd->uc_def;
5737 size_t num_len;
5738
5739 sprintf(num_buf, "%ld", num);
5740 num_len = STRLEN(num_buf);
5741 result = num_len;
5742
5743 if (quote)
5744 result += 2;
5745
5746 if (buf != NULL)
5747 {
5748 if (quote)
5749 *buf++ = '"';
5750 STRCPY(buf, num_buf);
5751 buf += num_len;
5752 if (quote)
5753 *buf = '"';
5754 }
5755
5756 break;
5757 }
5758
5759 case ct_REGISTER:
5760 result = eap->regname ? 1 : 0;
5761 if (quote)
5762 result += 2;
5763 if (buf != NULL)
5764 {
5765 if (quote)
5766 *buf++ = '\'';
5767 if (eap->regname)
5768 *buf++ = eap->regname;
5769 if (quote)
5770 *buf = '\'';
5771 }
5772 break;
5773
5774 case ct_LT:
5775 result = 1;
5776 if (buf != NULL)
5777 *buf = '<';
5778 break;
5779
5780 default:
5781 /* Not recognized: just copy the '<' and return -1. */
5782 result = (size_t)-1;
5783 if (buf != NULL)
5784 *buf = '<';
5785 break;
5786 }
5787
5788 return result;
5789}
5790
5791 static void
5792do_ucmd(eap)
5793 exarg_T *eap;
5794{
5795 char_u *buf;
5796 char_u *p;
5797 char_u *q;
5798
5799 char_u *start;
5800 char_u *end;
5801 size_t len, totlen;
5802
5803 size_t split_len = 0;
5804 char_u *split_buf = NULL;
5805 ucmd_T *cmd;
5806#ifdef FEAT_EVAL
5807 scid_T save_current_SID = current_SID;
5808#endif
5809
5810 if (eap->cmdidx == CMD_USER)
5811 cmd = USER_CMD(eap->useridx);
5812 else
5813 cmd = USER_CMD_GA(&curbuf->b_ucmds, eap->useridx);
5814
5815 /*
5816 * Replace <> in the command by the arguments.
5817 */
5818 buf = NULL;
5819 for (;;)
5820 {
5821 p = cmd->uc_rep;
5822 q = buf;
5823 totlen = 0;
5824 while ((start = vim_strchr(p, '<')) != NULL
5825 && (end = vim_strchr(start + 1, '>')) != NULL)
5826 {
5827 /* Include the '>' */
5828 ++end;
5829
5830 /* Take everything up to the '<' */
5831 len = start - p;
5832 if (buf == NULL)
5833 totlen += len;
5834 else
5835 {
5836 mch_memmove(q, p, len);
5837 q += len;
5838 }
5839
5840 len = uc_check_code(start, end - start, q, cmd, eap,
5841 &split_buf, &split_len);
5842 if (len == (size_t)-1)
5843 {
5844 /* no match, continue after '<' */
5845 p = start + 1;
5846 len = 1;
5847 }
5848 else
5849 p = end;
5850 if (buf == NULL)
5851 totlen += len;
5852 else
5853 q += len;
5854 }
5855 if (buf != NULL) /* second time here, finished */
5856 {
5857 STRCPY(q, p);
5858 break;
5859 }
5860
5861 totlen += STRLEN(p); /* Add on the trailing characters */
5862 buf = alloc((unsigned)(totlen + 1));
5863 if (buf == NULL)
5864 {
5865 vim_free(split_buf);
5866 return;
5867 }
5868 }
5869
5870#ifdef FEAT_EVAL
5871 current_SID = cmd->uc_scriptID;
5872#endif
5873 (void)do_cmdline(buf, eap->getline, eap->cookie,
5874 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
5875#ifdef FEAT_EVAL
5876 current_SID = save_current_SID;
5877#endif
5878 vim_free(buf);
5879 vim_free(split_buf);
5880}
5881
5882# if defined(FEAT_CMDL_COMPL) || defined(PROTO)
5883 static char_u *
5884get_user_command_name(idx)
5885 int idx;
5886{
5887 return get_user_commands(NULL, idx - (int)CMD_SIZE);
5888}
5889
5890/*
5891 * Function given to ExpandGeneric() to obtain the list of user command names.
5892 */
5893/*ARGSUSED*/
5894 char_u *
5895get_user_commands(xp, idx)
5896 expand_T *xp;
5897 int idx;
5898{
5899 if (idx < curbuf->b_ucmds.ga_len)
5900 return USER_CMD_GA(&curbuf->b_ucmds, idx)->uc_name;
5901 idx -= curbuf->b_ucmds.ga_len;
5902 if (idx < ucmds.ga_len)
5903 return USER_CMD(idx)->uc_name;
5904 return NULL;
5905}
5906
5907/*
5908 * Function given to ExpandGeneric() to obtain the list of user command
5909 * attributes.
5910 */
5911/*ARGSUSED*/
5912 char_u *
5913get_user_cmd_flags(xp, idx)
5914 expand_T *xp;
5915 int idx;
5916{
5917 static char *user_cmd_flags[] =
5918 {"bang", "bar", "buffer", "complete", "count",
5919 "nargs", "range", "register"};
5920
5921 if (idx >= sizeof(user_cmd_flags) / sizeof(user_cmd_flags[0]))
5922 return NULL;
5923 return (char_u *)user_cmd_flags[idx];
5924}
5925
5926/*
5927 * Function given to ExpandGeneric() to obtain the list of values for -nargs.
5928 */
5929/*ARGSUSED*/
5930 char_u *
5931get_user_cmd_nargs(xp, idx)
5932 expand_T *xp;
5933 int idx;
5934{
5935 static char *user_cmd_nargs[] = {"0", "1", "*", "?", "+"};
5936
5937 if (idx >= sizeof(user_cmd_nargs) / sizeof(user_cmd_nargs[0]))
5938 return NULL;
5939 return (char_u *)user_cmd_nargs[idx];
5940}
5941
5942/*
5943 * Function given to ExpandGeneric() to obtain the list of values for -complete.
5944 */
5945/*ARGSUSED*/
5946 char_u *
5947get_user_cmd_complete(xp, idx)
5948 expand_T *xp;
5949 int idx;
5950{
5951 return (char_u *)command_complete[idx].name;
5952}
5953# endif /* FEAT_CMDL_COMPL */
5954
5955#endif /* FEAT_USR_CMDS */
5956
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005957#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
5958/*
5959 * Parse a completion argument "value[vallen]".
5960 * The detected completion goes in "*complp", argument type in "*argt".
5961 * When there is an argument, for function and user defined completion, it's
5962 * copied to allocated memory and stored in "*compl_arg".
5963 * Returns FAIL if something is wrong.
5964 */
5965 int
5966parse_compl_arg(value, vallen, complp, argt, compl_arg)
5967 char_u *value;
5968 int vallen;
5969 int *complp;
5970 long *argt;
5971 char_u **compl_arg;
5972{
5973 char_u *arg = NULL;
5974 size_t arglen = 0;
5975 int i;
5976 int valend = vallen;
5977
5978 /* Look for any argument part - which is the part after any ',' */
5979 for (i = 0; i < vallen; ++i)
5980 {
5981 if (value[i] == ',')
5982 {
5983 arg = &value[i + 1];
5984 arglen = vallen - i - 1;
5985 valend = i;
5986 break;
5987 }
5988 }
5989
5990 for (i = 0; command_complete[i].expand != 0; ++i)
5991 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005992 if ((int)STRLEN(command_complete[i].name) == valend
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005993 && STRNCMP(value, command_complete[i].name, valend) == 0)
5994 {
5995 *complp = command_complete[i].expand;
5996 if (command_complete[i].expand == EXPAND_BUFFERS)
5997 *argt |= BUFNAME;
5998 else if (command_complete[i].expand == EXPAND_DIRECTORIES
5999 || command_complete[i].expand == EXPAND_FILES)
6000 *argt |= XFILE;
6001 break;
6002 }
6003 }
6004
6005 if (command_complete[i].expand == 0)
6006 {
6007 EMSG2(_("E180: Invalid complete value: %s"), value);
6008 return FAIL;
6009 }
6010
6011# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6012 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST
6013 && arg != NULL)
6014# else
6015 if (arg != NULL)
6016# endif
6017 {
6018 EMSG(_("E468: Completion argument only allowed for custom completion"));
6019 return FAIL;
6020 }
6021
6022# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6023 if ((*complp == EXPAND_USER_DEFINED || *complp == EXPAND_USER_LIST)
6024 && arg == NULL)
6025 {
6026 EMSG(_("E467: Custom completion requires a function argument"));
6027 return FAIL;
6028 }
6029
6030 if (arg != NULL)
6031 *compl_arg = vim_strnsave(arg, (int)arglen);
6032# endif
6033 return OK;
6034}
6035#endif
6036
Bram Moolenaar071d4272004-06-13 20:20:40 +00006037 static void
6038ex_colorscheme(eap)
6039 exarg_T *eap;
6040{
6041 if (load_colors(eap->arg) == FAIL)
6042 EMSG2(_("E185: Cannot find color scheme %s"), eap->arg);
6043}
6044
6045 static void
6046ex_highlight(eap)
6047 exarg_T *eap;
6048{
6049 if (*eap->arg == NUL && eap->cmd[2] == '!')
6050 MSG(_("Greetings, Vim user!"));
6051 do_highlight(eap->arg, eap->forceit, FALSE);
6052}
6053
6054
6055/*
6056 * Call this function if we thought we were going to exit, but we won't
6057 * (because of an error). May need to restore the terminal mode.
6058 */
6059 void
6060not_exiting()
6061{
6062 exiting = FALSE;
6063 settmode(TMODE_RAW);
6064}
6065
6066/*
6067 * ":quit": quit current window, quit Vim if closed the last window.
6068 */
6069 static void
6070ex_quit(eap)
6071 exarg_T *eap;
6072{
6073#ifdef FEAT_CMDWIN
6074 if (cmdwin_type != 0)
6075 {
6076 cmdwin_result = Ctrl_C;
6077 return;
6078 }
6079#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006080 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006081 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006082 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006083 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006084 return;
6085 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006086
6087#ifdef FEAT_NETBEANS_INTG
6088 netbeansForcedQuit = eap->forceit;
6089#endif
6090
6091 /*
6092 * If there are more files or windows we won't exit.
6093 */
6094 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6095 exiting = TRUE;
6096 if ((!P_HID(curbuf)
6097 && check_changed(curbuf, p_awa, FALSE, eap->forceit, FALSE))
6098 || check_more(TRUE, eap->forceit) == FAIL
6099 || (only_one_window() && check_changed_any(eap->forceit)))
6100 {
6101 not_exiting();
6102 }
6103 else
6104 {
6105#ifdef FEAT_WINDOWS
6106 if (only_one_window()) /* quit last window */
6107#endif
6108 getout(0);
6109#ifdef FEAT_WINDOWS
6110# ifdef FEAT_GUI
6111 need_mouse_correct = TRUE;
6112# endif
6113 /* close window; may free buffer */
6114 win_close(curwin, !P_HID(curwin->w_buffer) || eap->forceit);
6115#endif
6116 }
6117}
6118
6119/*
6120 * ":cquit".
6121 */
6122/*ARGSUSED*/
6123 static void
6124ex_cquit(eap)
6125 exarg_T *eap;
6126{
6127 getout(1); /* this does not always pass on the exit code to the Manx
6128 compiler. why? */
6129}
6130
6131/*
6132 * ":qall": try to quit all windows
6133 */
6134 static void
6135ex_quit_all(eap)
6136 exarg_T *eap;
6137{
6138# ifdef FEAT_CMDWIN
6139 if (cmdwin_type != 0)
6140 {
6141 if (eap->forceit)
6142 cmdwin_result = K_XF1; /* ex_window() takes care of this */
6143 else
6144 cmdwin_result = K_XF2;
6145 return;
6146 }
6147# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006148
6149 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006150 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006151 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006152 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006153 return;
6154 }
6155
Bram Moolenaar071d4272004-06-13 20:20:40 +00006156 exiting = TRUE;
6157 if (eap->forceit || !check_changed_any(FALSE))
6158 getout(0);
6159 not_exiting();
6160}
6161
6162#ifdef FEAT_WINDOWS
6163/*
6164 * ":close": close current window, unless it is the last one
6165 */
6166 static void
6167ex_close(eap)
6168 exarg_T *eap;
6169{
6170# ifdef FEAT_CMDWIN
6171 if (cmdwin_type != 0)
6172 cmdwin_result = K_IGNORE;
6173 else
6174# endif
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006175 if (!text_locked())
Bram Moolenaarf740b292006-02-16 22:11:02 +00006176 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006177}
6178
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006179#ifdef FEAT_QUICKFIX
6180/*
6181 * ":pclose": Close any preview window.
6182 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006183 static void
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006184ex_pclose(eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006185 exarg_T *eap;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006186{
6187 win_T *win;
6188
6189 for (win = firstwin; win != NULL; win = win->w_next)
6190 if (win->w_p_pvw)
6191 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006192 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006193 break;
6194 }
6195}
6196#endif
6197
Bram Moolenaarf740b292006-02-16 22:11:02 +00006198/*
6199 * Close window "win" and take care of handling closing the last window for a
6200 * modified buffer.
6201 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006202 static void
Bram Moolenaarf740b292006-02-16 22:11:02 +00006203ex_win_close(forceit, win, tp)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006204 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006205 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006206 tabpage_T *tp; /* NULL or the tab page "win" is in */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006207{
6208 int need_hide;
6209 buf_T *buf = win->w_buffer;
6210
6211 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006212 if (need_hide && !P_HID(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006213 {
6214#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
6215 if ((p_confirm || cmdmod.confirm) && p_write)
6216 {
6217 dialog_changed(buf, FALSE);
6218 if (buf_valid(buf) && bufIsChanged(buf))
6219 return;
6220 need_hide = FALSE;
6221 }
6222 else
6223#endif
6224 {
6225 EMSG(_(e_nowrtmsg));
6226 return;
6227 }
6228 }
6229
6230#ifdef FEAT_GUI
6231 need_mouse_correct = TRUE;
6232#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006233
Bram Moolenaar071d4272004-06-13 20:20:40 +00006234 /* free buffer when not hiding it or when it's a scratch buffer */
Bram Moolenaarf740b292006-02-16 22:11:02 +00006235 if (tp == NULL)
6236 win_close(win, !need_hide && !P_HID(buf));
6237 else
6238 win_close_othertab(win, !need_hide && !P_HID(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006239}
6240
Bram Moolenaar071d4272004-06-13 20:20:40 +00006241/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006242 * ":tabclose": close current tab page, unless it is the last one.
6243 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006244 */
6245 static void
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006246ex_tabclose(eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006247 exarg_T *eap;
6248{
Bram Moolenaarf740b292006-02-16 22:11:02 +00006249 tabpage_T *tp;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006250 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006251
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006252# ifdef FEAT_CMDWIN
6253 if (cmdwin_type != 0)
6254 cmdwin_result = K_IGNORE;
6255 else
6256# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006257 if (first_tabpage->tp_next == NULL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00006258 EMSG(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00006259 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006260 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006261 if (eap->addr_count > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006262 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006263 tp = find_tabpage((int)eap->line2);
6264 if (tp == NULL)
6265 {
6266 beep_flush();
6267 return;
6268 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00006269 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006270 {
6271 tabpage_close_other(tp, eap->forceit);
6272 return;
6273 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006274 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00006275 if (!text_locked())
6276 tabpage_close(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006277 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006278
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006279 if (h != tabline_height())
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006280 shell_new_rows();
6281}
6282
6283/*
6284 * ":tabonly": close all tab pages except the current one
6285 */
6286 static void
6287ex_tabonly(eap)
6288 exarg_T *eap;
6289{
6290 tabpage_T *tp;
6291 int done;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006292 int h = tabline_height();
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006293
6294# ifdef FEAT_CMDWIN
6295 if (cmdwin_type != 0)
6296 cmdwin_result = K_IGNORE;
6297 else
6298# endif
6299 if (first_tabpage->tp_next == NULL)
6300 MSG(_("Already only one tab page"));
6301 else
6302 {
6303 /* Repeat this up to a 1000 times, because autocommands may mess
6304 * up the lists. */
6305 for (done = 0; done < 1000; ++done)
6306 {
6307 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6308 if (tp->tp_topframe != topframe)
6309 {
6310 tabpage_close_other(tp, eap->forceit);
6311 /* if we failed to close it quit */
6312 if (valid_tabpage(tp))
6313 done = 1000;
6314 /* start over, "tp" is now invalid */
6315 break;
6316 }
6317 if (first_tabpage->tp_next == NULL)
6318 break;
6319 }
6320 }
6321
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006322 if (h != tabline_height())
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006323 shell_new_rows();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006324}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006325
6326/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006327 * Close the current tab page.
6328 */
6329 void
6330tabpage_close(forceit)
6331 int forceit;
6332{
6333 /* First close all the windows but the current one. If that worked then
6334 * close the last window in this tab, that will close it. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006335 if (lastwin != firstwin)
6336 close_others(TRUE, forceit);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006337 if (lastwin == firstwin)
6338 ex_win_close(forceit, curwin, NULL);
6339# ifdef FEAT_GUI
6340 need_mouse_correct = TRUE;
6341# endif
6342}
6343
6344/*
6345 * Close tab page "tp", which is not the current tab page.
6346 * Note that autocommands may make "tp" invalid.
6347 */
6348 void
6349tabpage_close_other(tp, forceit)
6350 tabpage_T *tp;
6351 int forceit;
6352{
6353 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006354 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006355
6356 /* Limit to 1000 windows, autocommands may add a window while we close
6357 * one. OK, so I'm paranoid... */
6358 while (++done < 1000)
6359 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006360 wp = tp->tp_firstwin;
6361 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006362
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006363 /* Autocommands may delete the tab page under our fingers and we may
6364 * fail to close a window with a modified buffer. */
6365 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006366 break;
6367 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006368 redraw_tabline = TRUE;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006369}
6370
6371/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006372 * ":only".
6373 */
6374 static void
6375ex_only(eap)
6376 exarg_T *eap;
6377{
6378# ifdef FEAT_GUI
6379 need_mouse_correct = TRUE;
6380# endif
6381 close_others(TRUE, eap->forceit);
6382}
6383
6384/*
6385 * ":all" and ":sall".
6386 */
6387 static void
6388ex_all(eap)
6389 exarg_T *eap;
6390{
6391 if (eap->addr_count == 0)
6392 eap->line2 = 9999;
6393 do_arg_all((int)eap->line2, eap->forceit);
6394}
6395#endif /* FEAT_WINDOWS */
6396
6397 static void
6398ex_hide(eap)
6399 exarg_T *eap;
6400{
6401 if (*eap->arg != NUL && check_nextcmd(eap->arg) == NULL)
6402 eap->errmsg = e_invarg;
6403 else
6404 {
6405 /* ":hide" or ":hide | cmd": hide current window */
6406 eap->nextcmd = check_nextcmd(eap->arg);
6407#ifdef FEAT_WINDOWS
6408 if (!eap->skip)
6409 {
6410# ifdef FEAT_GUI
6411 need_mouse_correct = TRUE;
6412# endif
6413 win_close(curwin, FALSE); /* don't free buffer */
6414 }
6415#endif
6416 }
6417}
6418
6419/*
6420 * ":stop" and ":suspend": Suspend Vim.
6421 */
6422 static void
6423ex_stop(eap)
6424 exarg_T *eap;
6425{
6426 /*
6427 * Disallow suspending for "rvim".
6428 */
6429 if (!check_restricted()
6430#ifdef WIN3264
6431 /*
6432 * Check if external commands are allowed now.
6433 */
6434 && can_end_termcap_mode(TRUE)
6435#endif
6436 )
6437 {
6438 if (!eap->forceit)
6439 autowrite_all();
6440 windgoto((int)Rows - 1, 0);
6441 out_char('\n');
6442 out_flush();
6443 stoptermcap();
6444 out_flush(); /* needed for SUN to restore xterm buffer */
6445#ifdef FEAT_TITLE
6446 mch_restore_title(3); /* restore window titles */
6447#endif
6448 ui_suspend(); /* call machine specific function */
6449#ifdef FEAT_TITLE
6450 maketitle();
6451 resettitle(); /* force updating the title */
6452#endif
6453 starttermcap();
6454 scroll_start(); /* scroll screen before redrawing */
6455 redraw_later_clear();
6456 shell_resized(); /* may have resized window */
6457 }
6458}
6459
6460/*
6461 * ":exit", ":xit" and ":wq": Write file and exit Vim.
6462 */
6463 static void
6464ex_exit(eap)
6465 exarg_T *eap;
6466{
6467#ifdef FEAT_CMDWIN
6468 if (cmdwin_type != 0)
6469 {
6470 cmdwin_result = Ctrl_C;
6471 return;
6472 }
6473#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006474 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006475 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006476 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006477 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006478 return;
6479 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006480
6481 /*
6482 * if more files or windows we won't exit
6483 */
6484 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6485 exiting = TRUE;
6486 if ( ((eap->cmdidx == CMD_wq
6487 || curbufIsChanged())
6488 && do_write(eap) == FAIL)
6489 || check_more(TRUE, eap->forceit) == FAIL
6490 || (only_one_window() && check_changed_any(eap->forceit)))
6491 {
6492 not_exiting();
6493 }
6494 else
6495 {
6496#ifdef FEAT_WINDOWS
6497 if (only_one_window()) /* quit last window, exit Vim */
6498#endif
6499 getout(0);
6500#ifdef FEAT_WINDOWS
6501# ifdef FEAT_GUI
6502 need_mouse_correct = TRUE;
6503# endif
6504 /* quit current window, may free buffer */
6505 win_close(curwin, !P_HID(curwin->w_buffer));
6506#endif
6507 }
6508}
6509
6510/*
6511 * ":print", ":list", ":number".
6512 */
6513 static void
6514ex_print(eap)
6515 exarg_T *eap;
6516{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006517 if (curbuf->b_ml.ml_flags & ML_EMPTY)
6518 EMSG(_(e_emptybuf));
6519 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006520 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00006521 for ( ;!got_int; ui_breakcheck())
6522 {
6523 print_line(eap->line1,
6524 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
6525 || (eap->flags & EXFLAG_NR)),
6526 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
6527 if (++eap->line1 > eap->line2)
6528 break;
6529 out_flush(); /* show one line at a time */
6530 }
6531 setpcmark();
6532 /* put cursor at last line */
6533 curwin->w_cursor.lnum = eap->line2;
6534 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006535 }
6536
Bram Moolenaar071d4272004-06-13 20:20:40 +00006537 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006538}
6539
6540#ifdef FEAT_BYTEOFF
6541 static void
6542ex_goto(eap)
6543 exarg_T *eap;
6544{
6545 goto_byte(eap->line2);
6546}
6547#endif
6548
6549/*
6550 * ":shell".
6551 */
6552/*ARGSUSED*/
6553 static void
6554ex_shell(eap)
6555 exarg_T *eap;
6556{
6557 do_shell(NULL, 0);
6558}
6559
6560#if (defined(FEAT_WINDOWS) && defined(HAVE_DROP_FILE)) \
6561 || (defined(FEAT_GUI_GTK) && defined(FEAT_DND)) \
Bram Moolenaar371baa92005-12-29 22:43:53 +00006562 || defined(FEAT_GUI_MSWIN) \
6563 || defined(FEAT_GUI_MAC) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00006564 || defined(PROTO)
6565
6566/*
6567 * Handle a file drop. The code is here because a drop is *nearly* like an
6568 * :args command, but not quite (we have a list of exact filenames, so we
6569 * don't want to (a) parse a command line, or (b) expand wildcards. So the
6570 * code is very similar to :args and hence needs access to a lot of the static
6571 * functions in this file.
6572 *
6573 * The list should be allocated using alloc(), as should each item in the
6574 * list. This function takes over responsibility for freeing the list.
6575 *
6576 * XXX The list is made into the arggument list. This is freed using
6577 * FreeWild(), which does a series of vim_free() calls, unless the two defines
6578 * __EMX__ and __ALWAYS_HAS_TRAILING_NUL_POINTER are set. In this case, a
6579 * routine _fnexplodefree() is used. This may cause problems, but as the drop
6580 * file functionality is (currently) not in EMX this is not presently a
6581 * problem.
6582 */
6583 void
6584handle_drop(filec, filev, split)
6585 int filec; /* the number of files dropped */
6586 char_u **filev; /* the list of files dropped */
6587 int split; /* force splitting the window */
6588{
6589 exarg_T ea;
6590 int save_msg_scroll = msg_scroll;
6591
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006592 /* Postpone this while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006593 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006594 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006595
6596 /* Check whether the current buffer is changed. If so, we will need
6597 * to split the current window or data could be lost.
6598 * We don't need to check if the 'hidden' option is set, as in this
6599 * case the buffer won't be lost.
6600 */
6601 if (!P_HID(curbuf) && !split)
6602 {
6603 ++emsg_off;
6604 split = check_changed(curbuf, TRUE, FALSE, FALSE, FALSE);
6605 --emsg_off;
6606 }
6607 if (split)
6608 {
6609# ifdef FEAT_WINDOWS
6610 if (win_split(0, 0) == FAIL)
6611 return;
6612# ifdef FEAT_SCROLLBIND
6613 curwin->w_p_scb = FALSE;
6614# endif
6615
6616 /* When splitting the window, create a new alist. Otherwise the
6617 * existing one is overwritten. */
6618 alist_unlink(curwin->w_alist);
6619 alist_new();
6620# else
6621 return; /* can't split, always fail */
6622# endif
6623 }
6624
6625 /*
6626 * Set up the new argument list.
6627 */
Bram Moolenaar86b68352004-12-27 21:59:20 +00006628 alist_set(ALIST(curwin), filec, filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006629
6630 /*
6631 * Move to the first file.
6632 */
6633 /* Fake up a minimal "next" command for do_argfile() */
6634 vim_memset(&ea, 0, sizeof(ea));
6635 ea.cmd = (char_u *)"next";
6636 do_argfile(&ea, 0);
6637
6638 /* do_ecmd() may set need_start_insertmode, but since we never left Insert
6639 * mode that is not needed here. */
6640 need_start_insertmode = FALSE;
6641
6642 /* Restore msg_scroll, otherwise a following command may cause scrolling
6643 * unexpectedly. The screen will be redrawn by the caller, thus
6644 * msg_scroll being set by displaying a message is irrelevant. */
6645 msg_scroll = save_msg_scroll;
6646}
6647#endif
6648
Bram Moolenaar071d4272004-06-13 20:20:40 +00006649/*
6650 * Clear an argument list: free all file names and reset it to zero entries.
6651 */
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00006652 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00006653alist_clear(al)
6654 alist_T *al;
6655{
6656 while (--al->al_ga.ga_len >= 0)
6657 vim_free(AARGLIST(al)[al->al_ga.ga_len].ae_fname);
6658 ga_clear(&al->al_ga);
6659}
6660
6661/*
6662 * Init an argument list.
6663 */
6664 void
6665alist_init(al)
6666 alist_T *al;
6667{
6668 ga_init2(&al->al_ga, (int)sizeof(aentry_T), 5);
6669}
6670
6671#if defined(FEAT_WINDOWS) || defined(PROTO)
6672
6673/*
6674 * Remove a reference from an argument list.
6675 * Ignored when the argument list is the global one.
6676 * If the argument list is no longer used by any window, free it.
6677 */
6678 void
6679alist_unlink(al)
6680 alist_T *al;
6681{
6682 if (al != &global_alist && --al->al_refcount <= 0)
6683 {
6684 alist_clear(al);
6685 vim_free(al);
6686 }
6687}
6688
6689# if defined(FEAT_LISTCMDS) || defined(HAVE_DROP_FILE) || defined(PROTO)
6690/*
6691 * Create a new argument list and use it for the current window.
6692 */
6693 void
6694alist_new()
6695{
6696 curwin->w_alist = (alist_T *)alloc((unsigned)sizeof(alist_T));
6697 if (curwin->w_alist == NULL)
6698 {
6699 curwin->w_alist = &global_alist;
6700 ++global_alist.al_refcount;
6701 }
6702 else
6703 {
6704 curwin->w_alist->al_refcount = 1;
6705 alist_init(curwin->w_alist);
6706 }
6707}
6708# endif
6709#endif
6710
6711#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE) || defined(PROTO)
6712/*
6713 * Expand the file names in the global argument list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00006714 * If "fnum_list" is not NULL, use "fnum_list[fnum_len]" as a list of buffer
6715 * numbers to be re-used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006716 */
6717 void
Bram Moolenaar86b68352004-12-27 21:59:20 +00006718alist_expand(fnum_list, fnum_len)
6719 int *fnum_list;
6720 int fnum_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006721{
6722 char_u **old_arg_files;
Bram Moolenaar86b68352004-12-27 21:59:20 +00006723 int old_arg_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006724 char_u **new_arg_files;
6725 int new_arg_file_count;
6726 char_u *save_p_su = p_su;
6727 int i;
6728
6729 /* Don't use 'suffixes' here. This should work like the shell did the
6730 * expansion. Also, the vimrc file isn't read yet, thus the user
6731 * can't set the options. */
6732 p_su = empty_option;
6733 old_arg_files = (char_u **)alloc((unsigned)(sizeof(char_u *) * GARGCOUNT));
6734 if (old_arg_files != NULL)
6735 {
6736 for (i = 0; i < GARGCOUNT; ++i)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006737 old_arg_files[i] = vim_strsave(GARGLIST[i].ae_fname);
6738 old_arg_count = GARGCOUNT;
6739 if (expand_wildcards(old_arg_count, old_arg_files,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006740 &new_arg_file_count, &new_arg_files,
6741 EW_FILE|EW_NOTFOUND|EW_ADDSLASH) == OK
6742 && new_arg_file_count > 0)
6743 {
Bram Moolenaar86b68352004-12-27 21:59:20 +00006744 alist_set(&global_alist, new_arg_file_count, new_arg_files,
6745 TRUE, fnum_list, fnum_len);
6746 FreeWild(old_arg_count, old_arg_files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006747 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006748 }
6749 p_su = save_p_su;
6750}
6751#endif
6752
6753/*
6754 * Set the argument list for the current window.
6755 * Takes over the allocated files[] and the allocated fnames in it.
6756 */
6757 void
Bram Moolenaar86b68352004-12-27 21:59:20 +00006758alist_set(al, count, files, use_curbuf, fnum_list, fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006759 alist_T *al;
6760 int count;
6761 char_u **files;
6762 int use_curbuf;
Bram Moolenaar86b68352004-12-27 21:59:20 +00006763 int *fnum_list;
6764 int fnum_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006765{
6766 int i;
6767
6768 alist_clear(al);
6769 if (ga_grow(&al->al_ga, count) == OK)
6770 {
6771 for (i = 0; i < count; ++i)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00006772 {
6773 if (got_int)
6774 {
6775 /* When adding many buffers this can take a long time. Allow
6776 * interrupting here. */
6777 while (i < count)
6778 vim_free(files[i++]);
6779 break;
6780 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00006781
6782 /* May set buffer name of a buffer previously used for the
6783 * argument list, so that it's re-used by alist_add. */
6784 if (fnum_list != NULL && i < fnum_len)
6785 buf_set_name(fnum_list[i], files[i]);
6786
Bram Moolenaar071d4272004-06-13 20:20:40 +00006787 alist_add(al, files[i], use_curbuf ? 2 : 1);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00006788 ui_breakcheck();
6789 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006790 vim_free(files);
6791 }
6792 else
6793 FreeWild(count, files);
6794#ifdef FEAT_WINDOWS
6795 if (al == &global_alist)
6796#endif
6797 arg_had_last = FALSE;
6798}
6799
6800/*
6801 * Add file "fname" to argument list "al".
6802 * "fname" must have been allocated and "al" must have been checked for room.
6803 */
6804 void
6805alist_add(al, fname, set_fnum)
6806 alist_T *al;
6807 char_u *fname;
6808 int set_fnum; /* 1: set buffer number; 2: re-use curbuf */
6809{
6810 if (fname == NULL) /* don't add NULL file names */
6811 return;
6812#ifdef BACKSLASH_IN_FILENAME
6813 slash_adjust(fname);
6814#endif
6815 AARGLIST(al)[al->al_ga.ga_len].ae_fname = fname;
6816 if (set_fnum > 0)
6817 AARGLIST(al)[al->al_ga.ga_len].ae_fnum =
6818 buflist_add(fname, BLN_LISTED | (set_fnum == 2 ? BLN_CURBUF : 0));
6819 ++al->al_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006820}
6821
6822#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
6823/*
6824 * Adjust slashes in file names. Called after 'shellslash' was set.
6825 */
6826 void
6827alist_slash_adjust()
6828{
6829 int i;
6830# ifdef FEAT_WINDOWS
6831 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006832 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006833# endif
6834
6835 for (i = 0; i < GARGCOUNT; ++i)
6836 if (GARGLIST[i].ae_fname != NULL)
6837 slash_adjust(GARGLIST[i].ae_fname);
6838# ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00006839 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006840 if (wp->w_alist != &global_alist)
6841 for (i = 0; i < WARGCOUNT(wp); ++i)
6842 if (WARGLIST(wp)[i].ae_fname != NULL)
6843 slash_adjust(WARGLIST(wp)[i].ae_fname);
6844# endif
6845}
6846#endif
6847
6848/*
6849 * ":preserve".
6850 */
6851/*ARGSUSED*/
6852 static void
6853ex_preserve(eap)
6854 exarg_T *eap;
6855{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00006856 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006857 ml_preserve(curbuf, TRUE);
6858}
6859
6860/*
6861 * ":recover".
6862 */
6863 static void
6864ex_recover(eap)
6865 exarg_T *eap;
6866{
6867 /* Set recoverymode right away to avoid the ATTENTION prompt. */
6868 recoverymode = TRUE;
6869 if (!check_changed(curbuf, p_awa, TRUE, eap->forceit, FALSE)
6870 && (*eap->arg == NUL
6871 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
6872 ml_recover();
6873 recoverymode = FALSE;
6874}
6875
6876/*
6877 * Command modifier used in a wrong way.
6878 */
6879 static void
6880ex_wrongmodifier(eap)
6881 exarg_T *eap;
6882{
6883 eap->errmsg = e_invcmd;
6884}
6885
6886#ifdef FEAT_WINDOWS
6887/*
6888 * :sview [+command] file split window with new file, read-only
6889 * :split [[+command] file] split window with current or new file
6890 * :vsplit [[+command] file] split window vertically with current or new file
6891 * :new [[+command] file] split window with no or new file
6892 * :vnew [[+command] file] split vertically window with no or new file
6893 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006894 *
6895 * :tabedit open new Tab page with empty window
6896 * :tabedit [+command] file open new Tab page and edit "file"
6897 * :tabnew [[+command] file] just like :tabedit
6898 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006899 */
6900 void
6901ex_splitview(eap)
6902 exarg_T *eap;
6903{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006904 win_T *old_curwin = curwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006905# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006906 char_u *fname = NULL;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006907# endif
6908# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006909 int browse_flag = cmdmod.browse;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006910# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006911
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006912# ifndef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006913 if (eap->cmdidx == CMD_vsplit || eap->cmdidx == CMD_vnew)
6914 {
6915 ex_ni(eap);
6916 return;
6917 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006918# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006919
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006920# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006921 need_mouse_correct = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006922# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006923
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006924# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00006925 /* A ":split" in the quickfix window works like ":new". Don't want two
6926 * quickfix windows. */
6927 if (bt_quickfix(curbuf))
6928 {
6929 if (eap->cmdidx == CMD_split)
6930 eap->cmdidx = CMD_new;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006931# ifdef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006932 if (eap->cmdidx == CMD_vsplit)
6933 eap->cmdidx = CMD_vnew;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006934# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006935 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006936# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006937
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006938# ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006939 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006940 {
6941 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
6942 FNAME_MESS, TRUE, curbuf->b_ffname);
6943 if (fname == NULL)
6944 goto theend;
6945 eap->arg = fname;
6946 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006947# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006948 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006949# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006950# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006951# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006952 if (cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006953# ifdef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006954 && eap->cmdidx != CMD_vnew
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006955# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006956 && eap->cmdidx != CMD_new)
6957 {
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006958 if (
6959# ifdef FEAT_GUI
6960 !gui.in_use &&
6961# endif
6962 au_has_group((char_u *)"FileExplorer"))
6963 {
6964 /* No browsing supported but we do have the file explorer:
6965 * Edit the directory. */
6966 if (*eap->arg == NUL || !mch_isdir(eap->arg))
6967 eap->arg = (char_u *)".";
6968 }
6969 else
6970 {
6971 fname = do_browse(0, (char_u *)_("Edit File in new window"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00006972 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006973 if (fname == NULL)
6974 goto theend;
6975 eap->arg = fname;
6976 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006977 }
6978 cmdmod.browse = FALSE; /* Don't browse again in do_ecmd(). */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006979# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006980
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006981 /*
6982 * Either open new tab page or split the window.
6983 */
6984 if (eap->cmdidx == CMD_tabedit
6985 || eap->cmdidx == CMD_tabfind
6986 || eap->cmdidx == CMD_tabnew)
6987 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006988 if (win_new_tabpage(cmdmod.tab) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006989 {
6990 do_exedit(eap, NULL);
6991
6992 /* set the alternate buffer for the window we came from */
6993 if (curwin != old_curwin
6994 && win_valid(old_curwin)
6995 && old_curwin->w_buffer != curbuf
6996 && !cmdmod.keepalt)
6997 old_curwin->w_alt_fnum = curbuf->b_fnum;
6998 }
6999 }
7000 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007001 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
7002 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007003# ifdef FEAT_SCROLLBIND
Bram Moolenaar071d4272004-06-13 20:20:40 +00007004 /* Reset 'scrollbind' when editing another file, but keep it when
7005 * doing ":split" without arguments. */
7006 if (*eap->arg != NUL
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007007# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007008 || cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007009# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007010 )
7011 curwin->w_p_scb = FALSE;
7012 else
7013 do_check_scrollbind(FALSE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007014# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007015 do_exedit(eap, old_curwin);
7016 }
7017
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007018# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007019 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007020# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007021
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007022# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007023theend:
7024 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007025# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007026}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007027
7028/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007029 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007030 */
7031 void
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007032tabpage_new()
7033{
7034 exarg_T ea;
7035
7036 vim_memset(&ea, 0, sizeof(ea));
7037 ea.cmdidx = CMD_tabnew;
7038 ea.cmd = (char_u *)"tabn";
7039 ea.arg = (char_u *)"";
7040 ex_splitview(&ea);
7041}
7042
7043/*
7044 * :tabnext command
7045 */
7046 static void
7047ex_tabnext(eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007048 exarg_T *eap;
7049{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007050 switch (eap->cmdidx)
7051 {
7052 case CMD_tabfirst:
7053 case CMD_tabrewind:
7054 goto_tabpage(1);
7055 break;
7056 case CMD_tablast:
7057 goto_tabpage(9999);
7058 break;
7059 case CMD_tabprevious:
7060 case CMD_tabNext:
7061 goto_tabpage(eap->addr_count == 0 ? -1 : -(int)eap->line2);
7062 break;
7063 default: /* CMD_tabnext */
7064 goto_tabpage(eap->addr_count == 0 ? 0 : (int)eap->line2);
7065 break;
7066 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007067}
7068
7069/*
7070 * :tabmove command
7071 */
7072 static void
7073ex_tabmove(eap)
7074 exarg_T *eap;
7075{
7076 tabpage_move(eap->addr_count == 0 ? 9999 : (int)eap->line2);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007077}
7078
7079/*
7080 * :tabs command: List tabs and their contents.
7081 */
7082/*ARGSUSED*/
7083 static void
7084ex_tabs(eap)
7085 exarg_T *eap;
7086{
7087 tabpage_T *tp;
7088 win_T *wp;
7089 int tabcount = 1;
7090
7091 msg_start();
7092 msg_scroll = TRUE;
7093 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
7094 {
7095 msg_putchar('\n');
7096 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
7097 msg_outtrans_attr(IObuff, hl_attr(HLF_T));
7098 out_flush(); /* output one line at a time */
7099 ui_breakcheck();
7100
Bram Moolenaar030f0df2006-02-21 22:02:53 +00007101 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007102 wp = firstwin;
7103 else
7104 wp = tp->tp_firstwin;
7105 for ( ; wp != NULL && !got_int; wp = wp->w_next)
7106 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007107 msg_putchar('\n');
7108 msg_putchar(wp == curwin ? '>' : ' ');
7109 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007110 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
7111 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007112 if (buf_spname(wp->w_buffer) != NULL)
7113 STRCPY(IObuff, buf_spname(wp->w_buffer));
7114 else
7115 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
7116 IObuff, IOSIZE, TRUE);
7117 msg_outtrans(IObuff);
7118 out_flush(); /* output one line at a time */
7119 ui_breakcheck();
7120 }
7121 }
7122}
7123
7124#endif /* FEAT_WINDOWS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007125
7126/*
7127 * ":mode": Set screen mode.
7128 * If no argument given, just get the screen size and redraw.
7129 */
7130 static void
7131ex_mode(eap)
7132 exarg_T *eap;
7133{
7134 if (*eap->arg == NUL)
7135 shell_resized();
7136 else
7137 mch_screenmode(eap->arg);
7138}
7139
7140#ifdef FEAT_WINDOWS
7141/*
7142 * ":resize".
7143 * set, increment or decrement current window height
7144 */
7145 static void
7146ex_resize(eap)
7147 exarg_T *eap;
7148{
7149 int n;
7150 win_T *wp = curwin;
7151
7152 if (eap->addr_count > 0)
7153 {
7154 n = eap->line2;
7155 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
7156 ;
7157 }
7158
7159#ifdef FEAT_GUI
7160 need_mouse_correct = TRUE;
7161#endif
7162 n = atol((char *)eap->arg);
7163#ifdef FEAT_VERTSPLIT
7164 if (cmdmod.split & WSP_VERT)
7165 {
7166 if (*eap->arg == '-' || *eap->arg == '+')
7167 n += W_WIDTH(curwin);
7168 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
7169 n = 9999;
7170 win_setwidth_win((int)n, wp);
7171 }
7172 else
7173#endif
7174 {
7175 if (*eap->arg == '-' || *eap->arg == '+')
7176 n += curwin->w_height;
7177 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
7178 n = 9999;
7179 win_setheight_win((int)n, wp);
7180 }
7181}
7182#endif
7183
7184/*
7185 * ":find [+command] <file>" command.
7186 */
7187 static void
7188ex_find(eap)
7189 exarg_T *eap;
7190{
7191#ifdef FEAT_SEARCHPATH
7192 char_u *fname;
7193 int count;
7194
7195 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
7196 TRUE, curbuf->b_ffname);
7197 if (eap->addr_count > 0)
7198 {
7199 /* Repeat finding the file "count" times. This matters when it
7200 * appears several times in the path. */
7201 count = eap->line2;
7202 while (fname != NULL && --count > 0)
7203 {
7204 vim_free(fname);
7205 fname = find_file_in_path(NULL, 0, FNAME_MESS,
7206 FALSE, curbuf->b_ffname);
7207 }
7208 }
7209
7210 if (fname != NULL)
7211 {
7212 eap->arg = fname;
7213#endif
7214 do_exedit(eap, NULL);
7215#ifdef FEAT_SEARCHPATH
7216 vim_free(fname);
7217 }
7218#endif
7219}
7220
7221/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00007222 * ":open" simulation: for now just work like ":visual".
7223 */
7224 static void
7225ex_open(eap)
7226 exarg_T *eap;
7227{
7228 regmatch_T regmatch;
7229 char_u *p;
7230
7231 curwin->w_cursor.lnum = eap->line2;
7232 beginline(BL_SOL | BL_FIX);
7233 if (*eap->arg == '/')
7234 {
7235 /* ":open /pattern/": put cursor in column found with pattern */
7236 ++eap->arg;
7237 p = skip_regexp(eap->arg, '/', p_magic, NULL);
7238 *p = NUL;
7239 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
7240 if (regmatch.regprog != NULL)
7241 {
7242 regmatch.rm_ic = p_ic;
7243 p = ml_get_curline();
7244 if (vim_regexec(&regmatch, p, (colnr_T)0))
7245 curwin->w_cursor.col = regmatch.startp[0] - p;
7246 else
7247 EMSG(_(e_nomatch));
7248 vim_free(regmatch.regprog);
7249 }
7250 /* Move to the NUL, ignore any other arguments. */
7251 eap->arg += STRLEN(eap->arg);
7252 }
7253 check_cursor();
7254
7255 eap->cmdidx = CMD_visual;
7256 do_exedit(eap, NULL);
7257}
7258
7259/*
7260 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007261 */
7262 static void
7263ex_edit(eap)
7264 exarg_T *eap;
7265{
7266 do_exedit(eap, NULL);
7267}
7268
7269/*
7270 * ":edit <file>" command and alikes.
7271 */
7272/*ARGSUSED*/
7273 void
7274do_exedit(eap, old_curwin)
7275 exarg_T *eap;
7276 win_T *old_curwin; /* curwin before doing a split or NULL */
7277{
7278 int n;
7279#ifdef FEAT_WINDOWS
7280 int need_hide;
7281#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00007282 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007283
7284 /*
7285 * ":vi" command ends Ex mode.
7286 */
7287 if (exmode_active && (eap->cmdidx == CMD_visual
7288 || eap->cmdidx == CMD_view))
7289 {
7290 exmode_active = FALSE;
7291 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007292 {
7293 /* Special case: ":global/pat/visual\NLvi-commands" */
7294 if (global_busy)
7295 {
7296 int rd = RedrawingDisabled;
7297 int nwr = no_wait_return;
7298 int ms = msg_scroll;
7299#ifdef FEAT_GUI
7300 int he = hold_gui_events;
7301#endif
7302
7303 if (eap->nextcmd != NULL)
7304 {
7305 stuffReadbuff(eap->nextcmd);
7306 eap->nextcmd = NULL;
7307 }
7308
7309 if (exmode_was != EXMODE_VIM)
7310 settmode(TMODE_RAW);
7311 RedrawingDisabled = 0;
7312 no_wait_return = 0;
7313 need_wait_return = FALSE;
7314 msg_scroll = 0;
7315#ifdef FEAT_GUI
7316 hold_gui_events = 0;
7317#endif
7318 must_redraw = CLEAR;
7319
7320 main_loop(FALSE, TRUE);
7321
7322 RedrawingDisabled = rd;
7323 no_wait_return = nwr;
7324 msg_scroll = ms;
7325#ifdef FEAT_GUI
7326 hold_gui_events = he;
7327#endif
7328 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007329 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007330 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007331 }
7332
7333 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007334 || eap->cmdidx == CMD_tabnew
7335 || eap->cmdidx == CMD_tabedit
Bram Moolenaar071d4272004-06-13 20:20:40 +00007336#ifdef FEAT_VERTSPLIT
7337 || eap->cmdidx == CMD_vnew
7338#endif
7339 ) && *eap->arg == NUL)
7340 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007341 /* ":new" or ":tabnew" without argument: edit an new empty buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007342 setpcmark();
7343 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
7344 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0));
7345 }
7346 else if ((eap->cmdidx != CMD_split
7347#ifdef FEAT_VERTSPLIT
7348 && eap->cmdidx != CMD_vsplit
7349#endif
7350 )
7351 || *eap->arg != NUL
7352#ifdef FEAT_BROWSE
7353 || cmdmod.browse
7354#endif
7355 )
7356 {
7357 n = readonlymode;
7358 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
7359 readonlymode = TRUE;
7360 else if (eap->cmdidx == CMD_enew)
7361 readonlymode = FALSE; /* 'readonly' doesn't make sense in an
7362 empty buffer */
7363 setpcmark();
7364 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
7365 NULL, eap,
7366 /* ":edit" goes to first line if Vi compatible */
7367 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
7368 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
7369 ? ECMD_ONE : eap->do_ecmd_lnum,
7370 (P_HID(curbuf) ? ECMD_HIDE : 0)
7371 + (eap->forceit ? ECMD_FORCEIT : 0)
7372#ifdef FEAT_LISTCMDS
7373 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
7374#endif
7375 ) == FAIL)
7376 {
7377 /* Editing the file failed. If the window was split, close it. */
7378#ifdef FEAT_WINDOWS
7379 if (old_curwin != NULL)
7380 {
7381 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
7382 if (!need_hide || P_HID(curbuf))
7383 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007384# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7385 cleanup_T cs;
7386
7387 /* Reset the error/interrupt/exception state here so that
7388 * aborting() returns FALSE when closing a window. */
7389 enter_cleanup(&cs);
7390# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007391# ifdef FEAT_GUI
7392 need_mouse_correct = TRUE;
7393# endif
7394 win_close(curwin, !need_hide && !P_HID(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007395
7396# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7397 /* Restore the error/interrupt/exception state if not
7398 * discarded by a new aborting error, interrupt, or
7399 * uncaught exception. */
7400 leave_cleanup(&cs);
7401# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007402 }
7403 }
7404#endif
7405 }
7406 else if (readonlymode && curbuf->b_nwindows == 1)
7407 {
7408 /* When editing an already visited buffer, 'readonly' won't be set
7409 * but the previous value is kept. With ":view" and ":sview" we
7410 * want the file to be readonly, except when another window is
7411 * editing the same buffer. */
7412 curbuf->b_p_ro = TRUE;
7413 }
7414 readonlymode = n;
7415 }
7416 else
7417 {
7418 if (eap->do_ecmd_cmd != NULL)
7419 do_cmdline_cmd(eap->do_ecmd_cmd);
7420#ifdef FEAT_TITLE
7421 n = curwin->w_arg_idx_invalid;
7422#endif
7423 check_arg_idx(curwin);
7424#ifdef FEAT_TITLE
7425 if (n != curwin->w_arg_idx_invalid)
7426 maketitle();
7427#endif
7428 }
7429
7430#ifdef FEAT_WINDOWS
7431 /*
7432 * if ":split file" worked, set alternate file name in old window to new
7433 * file
7434 */
7435 if (old_curwin != NULL
7436 && *eap->arg != NUL
7437 && curwin != old_curwin
7438 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00007439 && old_curwin->w_buffer != curbuf
7440 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007441 old_curwin->w_alt_fnum = curbuf->b_fnum;
7442#endif
7443
7444 ex_no_reprint = TRUE;
7445}
7446
7447#ifndef FEAT_GUI
7448/*
7449 * ":gui" and ":gvim" when there is no GUI.
7450 */
7451 static void
7452ex_nogui(eap)
7453 exarg_T *eap;
7454{
7455 eap->errmsg = e_nogvim;
7456}
7457#endif
7458
7459#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
7460 static void
7461ex_tearoff(eap)
7462 exarg_T *eap;
7463{
7464 gui_make_tearoff(eap->arg);
7465}
7466#endif
7467
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007468#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007469 static void
7470ex_popup(eap)
7471 exarg_T *eap;
7472{
Bram Moolenaar97409f12005-07-08 22:17:29 +00007473 gui_make_popup(eap->arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007474}
7475#endif
7476
7477/*ARGSUSED*/
7478 static void
7479ex_swapname(eap)
7480 exarg_T *eap;
7481{
7482 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
7483 MSG(_("No swap file"));
7484 else
7485 msg(curbuf->b_ml.ml_mfp->mf_fname);
7486}
7487
7488/*
7489 * ":syncbind" forces all 'scrollbind' windows to have the same relative
7490 * offset.
7491 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
7492 */
7493/*ARGSUSED*/
7494 static void
7495ex_syncbind(eap)
7496 exarg_T *eap;
7497{
7498#ifdef FEAT_SCROLLBIND
7499 win_T *wp;
7500 long topline;
7501 long y;
7502 linenr_T old_linenr = curwin->w_cursor.lnum;
7503
7504 setpcmark();
7505
7506 /*
7507 * determine max topline
7508 */
7509 if (curwin->w_p_scb)
7510 {
7511 topline = curwin->w_topline;
7512 for (wp = firstwin; wp; wp = wp->w_next)
7513 {
7514 if (wp->w_p_scb && wp->w_buffer)
7515 {
7516 y = wp->w_buffer->b_ml.ml_line_count - p_so;
7517 if (topline > y)
7518 topline = y;
7519 }
7520 }
7521 if (topline < 1)
7522 topline = 1;
7523 }
7524 else
7525 {
7526 topline = 1;
7527 }
7528
7529
7530 /*
7531 * set all scrollbind windows to the same topline
7532 */
7533 wp = curwin;
7534 for (curwin = firstwin; curwin; curwin = curwin->w_next)
7535 {
7536 if (curwin->w_p_scb)
7537 {
7538 y = topline - curwin->w_topline;
7539 if (y > 0)
7540 scrollup(y, TRUE);
7541 else
7542 scrolldown(-y, TRUE);
7543 curwin->w_scbind_pos = topline;
7544 redraw_later(VALID);
7545 cursor_correct();
7546#ifdef FEAT_WINDOWS
7547 curwin->w_redr_status = TRUE;
7548#endif
7549 }
7550 }
7551 curwin = wp;
7552 if (curwin->w_p_scb)
7553 {
7554 did_syncbind = TRUE;
7555 checkpcmark();
7556 if (old_linenr != curwin->w_cursor.lnum)
7557 {
7558 char_u ctrl_o[2];
7559
7560 ctrl_o[0] = Ctrl_O;
7561 ctrl_o[1] = 0;
7562 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
7563 }
7564 }
7565#endif
7566}
7567
7568
7569 static void
7570ex_read(eap)
7571 exarg_T *eap;
7572{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007573 int i;
7574 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
7575 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007576
7577 if (eap->usefilter) /* :r!cmd */
7578 do_bang(1, eap, FALSE, FALSE, TRUE);
7579 else
7580 {
7581 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
7582 return;
7583
7584#ifdef FEAT_BROWSE
7585 if (cmdmod.browse)
7586 {
7587 char_u *browseFile;
7588
Bram Moolenaar7171abe2004-10-11 10:06:20 +00007589 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007590 NULL, NULL, NULL, curbuf);
7591 if (browseFile != NULL)
7592 {
7593 i = readfile(browseFile, NULL,
7594 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7595 vim_free(browseFile);
7596 }
7597 else
7598 i = OK;
7599 }
7600 else
7601#endif
7602 if (*eap->arg == NUL)
7603 {
7604 if (check_fname() == FAIL) /* check for no file name */
7605 return;
7606 i = readfile(curbuf->b_ffname, curbuf->b_fname,
7607 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7608 }
7609 else
7610 {
7611 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
7612 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
7613 i = readfile(eap->arg, NULL,
7614 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7615
7616 }
7617 if (i == FAIL)
7618 {
7619#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7620 if (!aborting())
7621#endif
7622 EMSG2(_(e_notopen), eap->arg);
7623 }
7624 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00007625 {
7626 if (empty && exmode_active)
7627 {
7628 /* Delete the empty line that remains. Historically ex does
7629 * this but vi doesn't. */
7630 if (eap->line2 == 0)
7631 lnum = curbuf->b_ml.ml_line_count;
7632 else
7633 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007634 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007635 {
7636 ml_delete(lnum, FALSE);
7637 deleted_lines_mark(lnum, 1L);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007638 if (curwin->w_cursor.lnum > 1
7639 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007640 --curwin->w_cursor.lnum;
7641 }
7642 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007643 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007644 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007645 }
7646}
7647
Bram Moolenaarea408852005-06-25 22:49:46 +00007648static char_u *prev_dir = NULL;
7649
7650#if defined(EXITFREE) || defined(PROTO)
7651 void
7652free_cd_dir()
7653{
7654 vim_free(prev_dir);
7655}
7656#endif
7657
7658
Bram Moolenaar071d4272004-06-13 20:20:40 +00007659/*
7660 * ":cd", ":lcd", ":chdir" and ":lchdir".
7661 */
7662 static void
7663ex_cd(eap)
7664 exarg_T *eap;
7665{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007666 char_u *new_dir;
7667 char_u *tofree;
7668
7669 new_dir = eap->arg;
7670#if !defined(UNIX) && !defined(VMS)
7671 /* for non-UNIX ":cd" means: print current directory */
7672 if (*new_dir == NUL)
7673 ex_pwd(NULL);
7674 else
7675#endif
7676 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007677 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged()
7678 && !eap->forceit)
7679 {
7680 EMSG(_("E747: Cannot change directory, buffer is modifed (add ! to override)"));
7681 return;
7682 }
7683
Bram Moolenaar071d4272004-06-13 20:20:40 +00007684 /* ":cd -": Change to previous directory */
7685 if (STRCMP(new_dir, "-") == 0)
7686 {
7687 if (prev_dir == NULL)
7688 {
7689 EMSG(_("E186: No previous directory"));
7690 return;
7691 }
7692 new_dir = prev_dir;
7693 }
7694
7695 /* Save current directory for next ":cd -" */
7696 tofree = prev_dir;
7697 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7698 prev_dir = vim_strsave(NameBuff);
7699 else
7700 prev_dir = NULL;
7701
7702#if defined(UNIX) || defined(VMS)
7703 /* for UNIX ":cd" means: go to home directory */
7704 if (*new_dir == NUL)
7705 {
7706 /* use NameBuff for home directory name */
7707# ifdef VMS
7708 char_u *p;
7709
7710 p = mch_getenv((char_u *)"SYS$LOGIN");
7711 if (p == NULL || *p == NUL) /* empty is the same as not set */
7712 NameBuff[0] = NUL;
7713 else
Bram Moolenaarb6356332005-07-18 21:40:44 +00007714 vim_strncpy(NameBuff, p, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007715# else
7716 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
7717# endif
7718 new_dir = NameBuff;
7719 }
7720#endif
7721 if (new_dir == NULL || vim_chdir(new_dir))
7722 EMSG(_(e_failed));
7723 else
7724 {
7725 vim_free(curwin->w_localdir);
7726 if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir)
7727 {
7728 /* If still in global directory, need to remember current
7729 * directory as global directory. */
7730 if (globaldir == NULL && prev_dir != NULL)
7731 globaldir = vim_strsave(prev_dir);
7732 /* Remember this local directory for the window. */
7733 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7734 curwin->w_localdir = vim_strsave(NameBuff);
7735 }
7736 else
7737 {
7738 /* We are now in the global directory, no need to remember its
7739 * name. */
7740 vim_free(globaldir);
7741 globaldir = NULL;
7742 curwin->w_localdir = NULL;
7743 }
7744
7745 shorten_fnames(TRUE);
7746
7747 /* Echo the new current directory if the command was typed. */
7748 if (KeyTyped)
7749 ex_pwd(eap);
7750 }
7751 vim_free(tofree);
7752 }
7753}
7754
7755/*
7756 * ":pwd".
7757 */
7758/*ARGSUSED*/
7759 static void
7760ex_pwd(eap)
7761 exarg_T *eap;
7762{
7763 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7764 {
7765#ifdef BACKSLASH_IN_FILENAME
7766 slash_adjust(NameBuff);
7767#endif
7768 msg(NameBuff);
7769 }
7770 else
7771 EMSG(_("E187: Unknown"));
7772}
7773
7774/*
7775 * ":=".
7776 */
7777 static void
7778ex_equal(eap)
7779 exarg_T *eap;
7780{
7781 smsg((char_u *)"%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007782 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007783}
7784
7785 static void
7786ex_sleep(eap)
7787 exarg_T *eap;
7788{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007789 int n;
7790 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007791
7792 if (cursor_valid())
7793 {
7794 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
7795 if (n >= 0)
7796 windgoto((int)n, curwin->w_wcol);
7797 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007798
7799 len = eap->line2;
7800 switch (*eap->arg)
7801 {
7802 case 'm': break;
7803 case NUL: len *= 1000L; break;
7804 default: EMSG2(_(e_invarg2), eap->arg); return;
7805 }
7806 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007807}
7808
7809/*
7810 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
7811 */
7812 void
7813do_sleep(msec)
7814 long msec;
7815{
7816 long done;
7817
7818 cursor_on();
7819 out_flush();
7820 for (done = 0; !got_int && done < msec; done += 1000L)
7821 {
7822 ui_delay(msec - done > 1000L ? 1000L : msec - done, TRUE);
7823 ui_breakcheck();
7824 }
7825}
7826
7827 static void
7828do_exmap(eap, isabbrev)
7829 exarg_T *eap;
7830 int isabbrev;
7831{
7832 int mode;
7833 char_u *cmdp;
7834
7835 cmdp = eap->cmd;
7836 mode = get_map_mode(&cmdp, eap->forceit || isabbrev);
7837
7838 switch (do_map((*cmdp == 'n') ? 2 : (*cmdp == 'u'),
7839 eap->arg, mode, isabbrev))
7840 {
7841 case 1: EMSG(_(e_invarg));
7842 break;
7843 case 2: EMSG(isabbrev ? _(e_noabbr) : _(e_nomap));
7844 break;
7845 }
7846}
7847
7848/*
7849 * ":winsize" command (obsolete).
7850 */
7851 static void
7852ex_winsize(eap)
7853 exarg_T *eap;
7854{
7855 int w, h;
7856 char_u *arg = eap->arg;
7857 char_u *p;
7858
7859 w = getdigits(&arg);
7860 arg = skipwhite(arg);
7861 p = arg;
7862 h = getdigits(&arg);
7863 if (*p != NUL && *arg == NUL)
7864 set_shellsize(w, h, TRUE);
7865 else
7866 EMSG(_("E465: :winsize requires two number arguments"));
7867}
7868
7869#ifdef FEAT_WINDOWS
7870 static void
7871ex_wincmd(eap)
7872 exarg_T *eap;
7873{
7874 int xchar = NUL;
7875 char_u *p;
7876
7877 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
7878 {
7879 /* CTRL-W g and CTRL-W CTRL-G have an extra command character */
7880 if (eap->arg[1] == NUL)
7881 {
7882 EMSG(_(e_invarg));
7883 return;
7884 }
7885 xchar = eap->arg[1];
7886 p = eap->arg + 2;
7887 }
7888 else
7889 p = eap->arg + 1;
7890
7891 eap->nextcmd = check_nextcmd(p);
7892 p = skipwhite(p);
7893 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
7894 EMSG(_(e_invarg));
7895 else
7896 {
7897 /* Pass flags on for ":vertical wincmd ]". */
7898 postponed_split_flags = cmdmod.split;
7899 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
7900 postponed_split_flags = 0;
7901 }
7902}
7903#endif
7904
Bram Moolenaar843ee412004-06-30 16:16:41 +00007905#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007906/*
7907 * ":winpos".
7908 */
7909 static void
7910ex_winpos(eap)
7911 exarg_T *eap;
7912{
7913 int x, y;
7914 char_u *arg = eap->arg;
7915 char_u *p;
7916
7917 if (*arg == NUL)
7918 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00007919# if defined(FEAT_GUI) || defined(MSWIN)
7920# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007921 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00007922# else
7923 if (mch_get_winpos(&x, &y) != FAIL)
7924# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007925 {
7926 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
7927 msg(IObuff);
7928 }
7929 else
7930# endif
7931 EMSG(_("E188: Obtaining window position not implemented for this platform"));
7932 }
7933 else
7934 {
7935 x = getdigits(&arg);
7936 arg = skipwhite(arg);
7937 p = arg;
7938 y = getdigits(&arg);
7939 if (*p == NUL || *arg != NUL)
7940 {
7941 EMSG(_("E466: :winpos requires two number arguments"));
7942 return;
7943 }
7944# ifdef FEAT_GUI
7945 if (gui.in_use)
7946 gui_mch_set_winpos(x, y);
7947 else if (gui.starting)
7948 {
7949 /* Remember the coordinates for when the window is opened. */
7950 gui_win_x = x;
7951 gui_win_y = y;
7952 }
7953# ifdef HAVE_TGETENT
7954 else
7955# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00007956# else
7957# ifdef MSWIN
7958 mch_set_winpos(x, y);
7959# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007960# endif
7961# ifdef HAVE_TGETENT
7962 if (*T_CWP)
7963 term_set_winpos(x, y);
7964# endif
7965 }
7966}
7967#endif
7968
7969/*
7970 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
7971 */
7972 static void
7973ex_operators(eap)
7974 exarg_T *eap;
7975{
7976 oparg_T oa;
7977
7978 clear_oparg(&oa);
7979 oa.regname = eap->regname;
7980 oa.start.lnum = eap->line1;
7981 oa.end.lnum = eap->line2;
7982 oa.line_count = eap->line2 - eap->line1 + 1;
7983 oa.motion_type = MLINE;
7984#ifdef FEAT_VIRTUALEDIT
7985 virtual_op = FALSE;
7986#endif
7987 if (eap->cmdidx != CMD_yank) /* position cursor for undo */
7988 {
7989 setpcmark();
7990 curwin->w_cursor.lnum = eap->line1;
7991 beginline(BL_SOL | BL_FIX);
7992 }
7993
7994 switch (eap->cmdidx)
7995 {
7996 case CMD_delete:
7997 oa.op_type = OP_DELETE;
7998 op_delete(&oa);
7999 break;
8000
8001 case CMD_yank:
8002 oa.op_type = OP_YANK;
8003 (void)op_yank(&oa, FALSE, TRUE);
8004 break;
8005
8006 default: /* CMD_rshift or CMD_lshift */
8007 if ((eap->cmdidx == CMD_rshift)
8008#ifdef FEAT_RIGHTLEFT
8009 ^ curwin->w_p_rl
8010#endif
8011 )
8012 oa.op_type = OP_RSHIFT;
8013 else
8014 oa.op_type = OP_LSHIFT;
8015 op_shift(&oa, FALSE, eap->amount);
8016 break;
8017 }
8018#ifdef FEAT_VIRTUALEDIT
8019 virtual_op = MAYBE;
8020#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00008021 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008022}
8023
8024/*
8025 * ":put".
8026 */
8027 static void
8028ex_put(eap)
8029 exarg_T *eap;
8030{
8031 /* ":0put" works like ":1put!". */
8032 if (eap->line2 == 0)
8033 {
8034 eap->line2 = 1;
8035 eap->forceit = TRUE;
8036 }
8037 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008038 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
8039 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008040}
8041
8042/*
8043 * Handle ":copy" and ":move".
8044 */
8045 static void
8046ex_copymove(eap)
8047 exarg_T *eap;
8048{
8049 long n;
8050
8051 n = get_address(&eap->arg, FALSE, FALSE);
8052 if (eap->arg == NULL) /* error detected */
8053 {
8054 eap->nextcmd = NULL;
8055 return;
8056 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008057 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008058
8059 /*
8060 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
8061 */
8062 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
8063 {
8064 EMSG(_(e_invaddr));
8065 return;
8066 }
8067
8068 if (eap->cmdidx == CMD_move)
8069 {
8070 if (do_move(eap->line1, eap->line2, n) == FAIL)
8071 return;
8072 }
8073 else
8074 ex_copy(eap->line1, eap->line2, n);
8075 u_clearline();
8076 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008077 ex_may_print(eap);
8078}
8079
8080/*
8081 * Print the current line if flags were given to the Ex command.
8082 */
8083 static void
8084ex_may_print(eap)
8085 exarg_T *eap;
8086{
8087 if (eap->flags != 0)
8088 {
8089 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
8090 (eap->flags & EXFLAG_LIST));
8091 ex_no_reprint = TRUE;
8092 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008093}
8094
8095/*
8096 * ":smagic" and ":snomagic".
8097 */
8098 static void
8099ex_submagic(eap)
8100 exarg_T *eap;
8101{
8102 int magic_save = p_magic;
8103
8104 p_magic = (eap->cmdidx == CMD_smagic);
8105 do_sub(eap);
8106 p_magic = magic_save;
8107}
8108
8109/*
8110 * ":join".
8111 */
8112 static void
8113ex_join(eap)
8114 exarg_T *eap;
8115{
8116 curwin->w_cursor.lnum = eap->line1;
8117 if (eap->line1 == eap->line2)
8118 {
8119 if (eap->addr_count >= 2) /* :2,2join does nothing */
8120 return;
8121 if (eap->line2 == curbuf->b_ml.ml_line_count)
8122 {
8123 beep_flush();
8124 return;
8125 }
8126 ++eap->line2;
8127 }
8128 do_do_join(eap->line2 - eap->line1 + 1, !eap->forceit);
8129 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008130 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008131}
8132
8133/*
8134 * ":[addr]@r" or ":[addr]*r": execute register
8135 */
8136 static void
8137ex_at(eap)
8138 exarg_T *eap;
8139{
8140 int c;
8141
8142 curwin->w_cursor.lnum = eap->line2;
8143
8144#ifdef USE_ON_FLY_SCROLL
8145 dont_scroll = TRUE; /* disallow scrolling here */
8146#endif
8147
8148 /* get the register name. No name means to use the previous one */
8149 c = *eap->arg;
8150 if (c == NUL || (c == '*' && *eap->cmd == '*'))
8151 c = '@';
8152 /* put the register in mapbuf */
8153 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL) == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008154 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008155 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00008156 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008157 else
8158 {
8159 int save_efr = exec_from_reg;
8160
8161 exec_from_reg = TRUE;
8162
8163 /*
8164 * Execute from the typeahead buffer.
8165 * Originally this didn't check for the typeahead buffer to be empty,
8166 * thus could read more Ex commands from stdin. It's not clear why,
8167 * it is certainly unexpected.
8168 */
8169 while ((!stuff_empty() || typebuf.tb_len > 0) && vpeekc() == ':')
8170 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
8171
8172 exec_from_reg = save_efr;
8173 }
8174}
8175
8176/*
8177 * ":!".
8178 */
8179 static void
8180ex_bang(eap)
8181 exarg_T *eap;
8182{
8183 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
8184}
8185
8186/*
8187 * ":undo".
8188 */
8189/*ARGSUSED*/
8190 static void
8191ex_undo(eap)
8192 exarg_T *eap;
8193{
8194 u_undo(1);
8195}
8196
8197/*
8198 * ":redo".
8199 */
8200/*ARGSUSED*/
8201 static void
8202ex_redo(eap)
8203 exarg_T *eap;
8204{
8205 u_redo(1);
8206}
8207
8208/*
8209 * ":redir": start/stop redirection.
8210 */
8211 static void
8212ex_redir(eap)
8213 exarg_T *eap;
8214{
8215 char *mode;
8216 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00008217 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008218
8219 if (STRICMP(eap->arg, "END") == 0)
8220 close_redir();
8221 else
8222 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008223 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008224 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008225 ++arg;
8226 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008227 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008228 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008229 mode = "a";
8230 }
8231 else
8232 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00008233 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008234
8235 close_redir();
8236
8237 /* Expand environment variables and "~/". */
Bram Moolenaarca472992005-01-21 11:46:23 +00008238 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008239 if (fname == NULL)
8240 return;
8241#ifdef FEAT_BROWSE
8242 if (cmdmod.browse)
8243 {
8244 char_u *browseFile;
8245
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008246 browseFile = do_browse(BROWSE_SAVE,
8247 (char_u *)_("Save Redirection"),
8248 fname, NULL, NULL, BROWSE_FILTER_ALL_FILES, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008249 if (browseFile == NULL)
8250 return; /* operation cancelled */
8251 vim_free(fname);
8252 fname = browseFile;
8253 eap->forceit = TRUE; /* since dialog already asked */
8254 }
8255#endif
8256
8257 redir_fd = open_exfile(fname, eap->forceit, mode);
8258 vim_free(fname);
8259 }
8260#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00008261 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008262 {
8263 /* redirect to a register a-z (resp. A-Z for appending) */
8264 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00008265 ++arg;
8266 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008267# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00008268 || *arg == '*'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008269# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00008270 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008271 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008272 redir_reg = *arg++;
Bram Moolenaard9d30582005-05-18 22:10:28 +00008273 if (*arg == '>' && arg[1] == '>')
8274 arg += 2;
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00008275 else if ((*arg == NUL || (*arg == '>' && arg[1] == NUL)) &&
8276 (islower(redir_reg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008277# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00008278 || redir_reg == '*'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008279# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00008280 || redir_reg == '"'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008281 {
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00008282 if (*arg == '>')
8283 arg++;
8284
Bram Moolenaar071d4272004-06-13 20:20:40 +00008285 /* make register empty */
8286 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
8287 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008288 }
8289 if (*arg != NUL)
8290 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00008291 redir_reg = 0;
Bram Moolenaard9d30582005-05-18 22:10:28 +00008292 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008293 }
8294 }
8295 else if (*arg == '=' && arg[1] == '>')
8296 {
8297 int append;
8298
8299 /* redirect to a variable */
8300 close_redir();
8301 arg += 2;
8302
8303 if (*arg == '>')
8304 {
8305 ++arg;
8306 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008307 }
8308 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008309 append = FALSE;
8310
8311 if (var_redir_start(skipwhite(arg), append) == OK)
8312 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008313 }
8314#endif
8315
8316 /* TODO: redirect to a buffer */
8317
Bram Moolenaar071d4272004-06-13 20:20:40 +00008318 else
Bram Moolenaarca472992005-01-21 11:46:23 +00008319 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008320 }
8321}
8322
8323/*
8324 * ":redraw": force redraw
8325 */
8326 static void
8327ex_redraw(eap)
8328 exarg_T *eap;
8329{
8330 int r = RedrawingDisabled;
8331 int p = p_lz;
8332
8333 RedrawingDisabled = 0;
8334 p_lz = FALSE;
8335 update_topline();
8336 update_screen(eap->forceit ? CLEAR :
8337#ifdef FEAT_VISUAL
8338 VIsual_active ? INVERTED :
8339#endif
8340 0);
8341#ifdef FEAT_TITLE
8342 if (need_maketitle)
8343 maketitle();
8344#endif
8345 RedrawingDisabled = r;
8346 p_lz = p;
8347
8348 /* Reset msg_didout, so that a message that's there is overwritten. */
8349 msg_didout = FALSE;
8350 msg_col = 0;
8351
8352 out_flush();
8353}
8354
8355/*
8356 * ":redrawstatus": force redraw of status line(s)
8357 */
8358/*ARGSUSED*/
8359 static void
8360ex_redrawstatus(eap)
8361 exarg_T *eap;
8362{
8363#if defined(FEAT_WINDOWS)
8364 int r = RedrawingDisabled;
8365 int p = p_lz;
8366
8367 RedrawingDisabled = 0;
8368 p_lz = FALSE;
8369 if (eap->forceit)
8370 status_redraw_all();
8371 else
8372 status_redraw_curbuf();
8373 update_screen(
8374# ifdef FEAT_VISUAL
8375 VIsual_active ? INVERTED :
8376# endif
8377 0);
8378 RedrawingDisabled = r;
8379 p_lz = p;
8380 out_flush();
8381#endif
8382}
8383
8384 static void
8385close_redir()
8386{
8387 if (redir_fd != NULL)
8388 {
8389 fclose(redir_fd);
8390 redir_fd = NULL;
8391 }
8392#ifdef FEAT_EVAL
8393 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008394 if (redir_vname)
8395 {
8396 var_redir_stop();
8397 redir_vname = 0;
8398 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008399#endif
8400}
8401
8402#if defined(FEAT_SESSION) && defined(USE_CRNL)
8403# define MKSESSION_NL
8404static int mksession_nl = FALSE; /* use NL only in put_eol() */
8405#endif
8406
8407/*
8408 * ":mkexrc", ":mkvimrc", ":mkview" and ":mksession".
8409 */
8410 static void
8411ex_mkrc(eap)
8412 exarg_T *eap;
8413{
8414 FILE *fd;
8415 int failed = FALSE;
8416 char_u *fname;
8417#ifdef FEAT_BROWSE
8418 char_u *browseFile = NULL;
8419#endif
8420#ifdef FEAT_SESSION
8421 int view_session = FALSE;
8422 int using_vdir = FALSE; /* using 'viewdir'? */
8423 char_u *viewFile = NULL;
8424 unsigned *flagp;
8425#endif
8426
8427 if (eap->cmdidx == CMD_mksession || eap->cmdidx == CMD_mkview)
8428 {
8429#ifdef FEAT_SESSION
8430 view_session = TRUE;
8431#else
8432 ex_ni(eap);
8433 return;
8434#endif
8435 }
8436
8437#ifdef FEAT_SESSION
8438 /* ":mkview" or ":mkview 9": generate file name with 'viewdir' */
8439 if (eap->cmdidx == CMD_mkview
8440 && (*eap->arg == NUL
8441 || (vim_isdigit(*eap->arg) && eap->arg[1] == NUL)))
8442 {
8443 eap->forceit = TRUE;
8444 fname = get_view_file(*eap->arg);
8445 if (fname == NULL)
8446 return;
8447 viewFile = fname;
8448 using_vdir = TRUE;
8449 }
8450 else
8451#endif
8452 if (*eap->arg != NUL)
8453 fname = eap->arg;
8454 else if (eap->cmdidx == CMD_mkvimrc)
8455 fname = (char_u *)VIMRC_FILE;
8456#ifdef FEAT_SESSION
8457 else if (eap->cmdidx == CMD_mksession)
8458 fname = (char_u *)SESSION_FILE;
8459#endif
8460 else
8461 fname = (char_u *)EXRC_FILE;
8462
8463#ifdef FEAT_BROWSE
8464 if (cmdmod.browse)
8465 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008466 browseFile = do_browse(BROWSE_SAVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008467# ifdef FEAT_SESSION
8468 eap->cmdidx == CMD_mkview ? (char_u *)_("Save View") :
8469 eap->cmdidx == CMD_mksession ? (char_u *)_("Save Session") :
8470# endif
8471 (char_u *)_("Save Setup"),
8472 fname, (char_u *)"vim", NULL, BROWSE_FILTER_MACROS, NULL);
8473 if (browseFile == NULL)
8474 goto theend;
8475 fname = browseFile;
8476 eap->forceit = TRUE; /* since dialog already asked */
8477 }
8478#endif
8479
8480#if defined(FEAT_SESSION) && defined(vim_mkdir)
8481 /* When using 'viewdir' may have to create the directory. */
8482 if (using_vdir && !mch_isdir(p_vdir))
Bram Moolenaardf177f62005-02-22 08:39:57 +00008483 vim_mkdir_emsg(p_vdir, 0755);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008484#endif
8485
8486 fd = open_exfile(fname, eap->forceit, WRITEBIN);
8487 if (fd != NULL)
8488 {
8489#ifdef FEAT_SESSION
8490 if (eap->cmdidx == CMD_mkview)
8491 flagp = &vop_flags;
8492 else
8493 flagp = &ssop_flags;
8494#endif
8495
8496#ifdef MKSESSION_NL
8497 /* "unix" in 'sessionoptions': use NL line separator */
8498 if (view_session && (*flagp & SSOP_UNIX))
8499 mksession_nl = TRUE;
8500#endif
8501
8502 /* Write the version command for :mkvimrc */
8503 if (eap->cmdidx == CMD_mkvimrc)
8504 (void)put_line(fd, "version 6.0");
8505
8506#ifdef FEAT_SESSION
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008507 if (eap->cmdidx == CMD_mksession)
8508 {
8509 if (put_line(fd, "let SessionLoad = 1") == FAIL)
8510 failed = TRUE;
8511 }
8512
Bram Moolenaar071d4272004-06-13 20:20:40 +00008513 if (eap->cmdidx != CMD_mkview)
8514#endif
8515 {
8516 /* Write setting 'compatible' first, because it has side effects.
8517 * For that same reason only do it when needed. */
8518 if (p_cp)
8519 (void)put_line(fd, "if !&cp | set cp | endif");
8520 else
8521 (void)put_line(fd, "if &cp | set nocp | endif");
8522 }
8523
8524#ifdef FEAT_SESSION
8525 if (!view_session
8526 || (eap->cmdidx == CMD_mksession
8527 && (*flagp & SSOP_OPTIONS)))
8528#endif
8529 failed |= (makemap(fd, NULL) == FAIL
8530 || makeset(fd, OPT_GLOBAL, FALSE) == FAIL);
8531
8532#ifdef FEAT_SESSION
8533 if (!failed && view_session)
8534 {
8535 if (put_line(fd, "let s:so_save = &so | let s:siso_save = &siso | set so=0 siso=0") == FAIL)
8536 failed = TRUE;
8537 if (eap->cmdidx == CMD_mksession)
8538 {
8539 char_u dirnow[MAXPATHL]; /* current directory */
8540
8541 /*
8542 * Change to session file's dir.
8543 */
8544 if (mch_dirname(dirnow, MAXPATHL) == FAIL
8545 || mch_chdir((char *)dirnow) != 0)
8546 *dirnow = NUL;
8547 if (*dirnow != NUL && (ssop_flags & SSOP_SESDIR))
8548 {
8549 if (vim_chdirfile(fname) == OK)
8550 shorten_fnames(TRUE);
8551 }
8552 else if (*dirnow != NUL
8553 && (ssop_flags & SSOP_CURDIR) && globaldir != NULL)
8554 {
8555 (void)mch_chdir((char *)globaldir);
8556 shorten_fnames(TRUE);
8557 }
8558
8559 failed |= (makeopens(fd, dirnow) == FAIL);
8560
8561 /* restore original dir */
8562 if (*dirnow != NUL && ((ssop_flags & SSOP_SESDIR)
8563 || ((ssop_flags & SSOP_CURDIR) && globaldir != NULL)))
8564 {
8565 if (mch_chdir((char *)dirnow) != 0)
8566 EMSG(_(e_prev_dir));
8567 shorten_fnames(TRUE);
8568 }
8569 }
8570 else
8571 {
8572 failed |= (put_view(fd, curwin, !using_vdir, flagp) == FAIL);
8573 }
8574 if (put_line(fd, "let &so = s:so_save | let &siso = s:siso_save")
8575 == FAIL)
8576 failed = TRUE;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008577 if (put_line(fd, "doautoall SessionLoadPost") == FAIL)
8578 failed = TRUE;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008579 if (eap->cmdidx == CMD_mksession)
8580 {
8581 if (put_line(fd, "unlet SessionLoad") == FAIL)
8582 failed = TRUE;
8583 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008584 }
8585#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008586 if (put_line(fd, "\" vim: set ft=vim :") == FAIL)
8587 failed = TRUE;
8588
Bram Moolenaar071d4272004-06-13 20:20:40 +00008589 failed |= fclose(fd);
8590
8591 if (failed)
8592 EMSG(_(e_write));
8593#if defined(FEAT_EVAL) && defined(FEAT_SESSION)
8594 else if (eap->cmdidx == CMD_mksession)
8595 {
8596 /* successful session write - set this_session var */
8597 char_u tbuf[MAXPATHL];
8598
8599 if (vim_FullName(fname, tbuf, MAXPATHL, FALSE) == OK)
8600 set_vim_var_string(VV_THIS_SESSION, tbuf, -1);
8601 }
8602#endif
8603#ifdef MKSESSION_NL
8604 mksession_nl = FALSE;
8605#endif
8606 }
8607
8608#ifdef FEAT_BROWSE
8609theend:
8610 vim_free(browseFile);
8611#endif
8612#ifdef FEAT_SESSION
8613 vim_free(viewFile);
8614#endif
8615}
8616
Bram Moolenaardf177f62005-02-22 08:39:57 +00008617#if ((defined(FEAT_SESSION) || defined(FEAT_EVAL)) && defined(vim_mkdir)) \
8618 || defined(PROTO)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008619/*ARGSUSED*/
Bram Moolenaardf177f62005-02-22 08:39:57 +00008620 int
8621vim_mkdir_emsg(name, prot)
8622 char_u *name;
8623 int prot;
8624{
8625 if (vim_mkdir(name, prot) != 0)
8626 {
8627 EMSG2(_("E739: Cannot create directory: %s"), name);
8628 return FAIL;
8629 }
8630 return OK;
8631}
8632#endif
8633
Bram Moolenaar071d4272004-06-13 20:20:40 +00008634/*
8635 * Open a file for writing for an Ex command, with some checks.
8636 * Return file descriptor, or NULL on failure.
8637 */
8638 FILE *
8639open_exfile(fname, forceit, mode)
8640 char_u *fname;
8641 int forceit;
8642 char *mode; /* "w" for create new file or "a" for append */
8643{
8644 FILE *fd;
8645
8646#ifdef UNIX
8647 /* with Unix it is possible to open a directory */
8648 if (mch_isdir(fname))
8649 {
8650 EMSG2(_(e_isadir2), fname);
8651 return NULL;
8652 }
8653#endif
8654 if (!forceit && *mode != 'a' && vim_fexists(fname))
8655 {
8656 EMSG2(_("E189: \"%s\" exists (add ! to override)"), fname);
8657 return NULL;
8658 }
8659
8660 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
8661 EMSG2(_("E190: Cannot open \"%s\" for writing"), fname);
8662
8663 return fd;
8664}
8665
8666/*
8667 * ":mark" and ":k".
8668 */
8669 static void
8670ex_mark(eap)
8671 exarg_T *eap;
8672{
8673 pos_T pos;
8674
8675 if (*eap->arg == NUL) /* No argument? */
8676 EMSG(_(e_argreq));
8677 else if (eap->arg[1] != NUL) /* more than one character? */
8678 EMSG(_(e_trailing));
8679 else
8680 {
8681 pos = curwin->w_cursor; /* save curwin->w_cursor */
8682 curwin->w_cursor.lnum = eap->line2;
8683 beginline(BL_WHITE | BL_FIX);
8684 if (setmark(*eap->arg) == FAIL) /* set mark */
8685 EMSG(_("E191: Argument must be a letter or forward/backward quote"));
8686 curwin->w_cursor = pos; /* restore curwin->w_cursor */
8687 }
8688}
8689
8690/*
8691 * Update w_topline, w_leftcol and the cursor position.
8692 */
8693 void
8694update_topline_cursor()
8695{
8696 check_cursor(); /* put cursor on valid line */
8697 update_topline();
8698 if (!curwin->w_p_wrap)
8699 validate_cursor();
8700 update_curswant();
8701}
8702
8703#ifdef FEAT_EX_EXTRA
8704/*
8705 * ":normal[!] {commands}": Execute normal mode commands.
8706 */
8707 static void
8708ex_normal(eap)
8709 exarg_T *eap;
8710{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008711 int save_msg_scroll = msg_scroll;
8712 int save_restart_edit = restart_edit;
8713 int save_msg_didout = msg_didout;
8714 int save_State = State;
8715 tasave_T tabuf;
8716 int save_insertmode = p_im;
8717 int save_finish_op = finish_op;
8718#ifdef FEAT_MBYTE
8719 char_u *arg = NULL;
8720 int l;
8721 char_u *p;
8722#endif
8723
8724 if (ex_normal_busy >= p_mmd)
8725 {
8726 EMSG(_("E192: Recursive use of :normal too deep"));
8727 return;
8728 }
8729 ++ex_normal_busy;
8730
8731 msg_scroll = FALSE; /* no msg scrolling in Normal mode */
8732 restart_edit = 0; /* don't go to Insert mode */
8733 p_im = FALSE; /* don't use 'insertmode' */
8734
8735#ifdef FEAT_MBYTE
8736 /*
8737 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
8738 * this for the K_SPECIAL leading byte, otherwise special keys will not
8739 * work.
8740 */
8741 if (has_mbyte)
8742 {
8743 int len = 0;
8744
8745 /* Count the number of characters to be escaped. */
8746 for (p = eap->arg; *p != NUL; ++p)
8747 {
8748# ifdef FEAT_GUI
8749 if (*p == CSI) /* leadbyte CSI */
8750 len += 2;
8751# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008752 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008753 if (*++p == K_SPECIAL /* trailbyte K_SPECIAL or CSI */
8754# ifdef FEAT_GUI
8755 || *p == CSI
8756# endif
8757 )
8758 len += 2;
8759 }
8760 if (len > 0)
8761 {
8762 arg = alloc((unsigned)(STRLEN(eap->arg) + len + 1));
8763 if (arg != NULL)
8764 {
8765 len = 0;
8766 for (p = eap->arg; *p != NUL; ++p)
8767 {
8768 arg[len++] = *p;
8769# ifdef FEAT_GUI
8770 if (*p == CSI)
8771 {
8772 arg[len++] = KS_EXTRA;
8773 arg[len++] = (int)KE_CSI;
8774 }
8775# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008776 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008777 {
8778 arg[len++] = *++p;
8779 if (*p == K_SPECIAL)
8780 {
8781 arg[len++] = KS_SPECIAL;
8782 arg[len++] = KE_FILLER;
8783 }
8784# ifdef FEAT_GUI
8785 else if (*p == CSI)
8786 {
8787 arg[len++] = KS_EXTRA;
8788 arg[len++] = (int)KE_CSI;
8789 }
8790# endif
8791 }
8792 arg[len] = NUL;
8793 }
8794 }
8795 }
8796 }
8797#endif
8798
8799 /*
8800 * Save the current typeahead. This is required to allow using ":normal"
8801 * from an event handler and makes sure we don't hang when the argument
8802 * ends with half a command.
8803 */
8804 save_typeahead(&tabuf);
8805 if (tabuf.typebuf_valid)
8806 {
8807 /*
8808 * Repeat the :normal command for each line in the range. When no
8809 * range given, execute it just once, without positioning the cursor
8810 * first.
8811 */
8812 do
8813 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008814 if (eap->addr_count != 0)
8815 {
8816 curwin->w_cursor.lnum = eap->line1++;
8817 curwin->w_cursor.col = 0;
8818 }
8819
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008820 exec_normal_cmd(
Bram Moolenaar071d4272004-06-13 20:20:40 +00008821#ifdef FEAT_MBYTE
8822 arg != NULL ? arg :
8823#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008824 eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008825 }
8826 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
8827 }
8828
8829 /* Might not return to the main loop when in an event handler. */
8830 update_topline_cursor();
8831
8832 /* Restore the previous typeahead. */
8833 restore_typeahead(&tabuf);
8834
8835 --ex_normal_busy;
8836 msg_scroll = save_msg_scroll;
8837 restart_edit = save_restart_edit;
8838 p_im = save_insertmode;
8839 finish_op = save_finish_op;
8840 msg_didout |= save_msg_didout; /* don't reset msg_didout now */
8841
8842 /* Restore the state (needed when called from a function executed for
8843 * 'indentexpr'). */
8844 State = save_State;
8845#ifdef FEAT_MBYTE
8846 vim_free(arg);
8847#endif
8848}
8849
8850/*
Bram Moolenaar64969662005-12-14 21:59:55 +00008851 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008852 */
8853 static void
8854ex_startinsert(eap)
8855 exarg_T *eap;
8856{
Bram Moolenaarfd371682005-01-14 21:42:54 +00008857 if (eap->forceit)
8858 {
8859 coladvance((colnr_T)MAXCOL);
8860 curwin->w_curswant = MAXCOL;
8861 curwin->w_set_curswant = FALSE;
8862 }
8863
Bram Moolenaara40c5002005-01-09 21:16:21 +00008864 /* Ignore the command when already in Insert mode. Inserting an
8865 * expression register that invokes a function can do this. */
8866 if (State & INSERT)
8867 return;
8868
Bram Moolenaar64969662005-12-14 21:59:55 +00008869 if (eap->cmdidx == CMD_startinsert)
8870 restart_edit = 'a';
8871 else if (eap->cmdidx == CMD_startreplace)
8872 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008873 else
Bram Moolenaar64969662005-12-14 21:59:55 +00008874 restart_edit = 'V';
8875
8876 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008877 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00008878 if (eap->cmdidx == CMD_startinsert)
8879 restart_edit = 'i';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008880 curwin->w_curswant = 0; /* avoid MAXCOL */
8881 }
8882}
8883
8884/*
8885 * ":stopinsert"
8886 */
8887/*ARGSUSED*/
8888 static void
8889ex_stopinsert(eap)
8890 exarg_T *eap;
8891{
8892 restart_edit = 0;
8893 stop_insert_mode = TRUE;
8894}
8895#endif
8896
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008897#if defined(FEAT_EX_EXTRA) || defined(FEAT_MENU) || defined(PROTO)
8898/*
8899 * Execute normal mode command "cmd".
8900 * "remap" can be REMAP_NONE or REMAP_YES.
8901 */
8902 void
8903exec_normal_cmd(cmd, remap, silent)
8904 char_u *cmd;
8905 int remap;
8906 int silent;
8907{
8908 oparg_T oa;
8909
8910 /*
8911 * Stuff the argument into the typeahead buffer.
8912 * Execute normal_cmd() until there is no typeahead left.
8913 */
8914 clear_oparg(&oa);
8915 finish_op = FALSE;
8916 ins_typebuf(cmd, remap, 0, TRUE, silent);
8917 while ((!stuff_empty() || (!typebuf_typed() && typebuf.tb_len > 0))
8918 && !got_int)
8919 {
8920 update_topline_cursor();
8921 normal_cmd(&oa, FALSE); /* execute a Normal mode cmd */
8922 }
8923}
8924#endif
8925
Bram Moolenaar071d4272004-06-13 20:20:40 +00008926#ifdef FEAT_FIND_ID
8927 static void
8928ex_checkpath(eap)
8929 exarg_T *eap;
8930{
8931 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
8932 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
8933 (linenr_T)1, (linenr_T)MAXLNUM);
8934}
8935
8936#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
8937/*
8938 * ":psearch"
8939 */
8940 static void
8941ex_psearch(eap)
8942 exarg_T *eap;
8943{
8944 g_do_tagpreview = p_pvh;
8945 ex_findpat(eap);
8946 g_do_tagpreview = 0;
8947}
8948#endif
8949
8950 static void
8951ex_findpat(eap)
8952 exarg_T *eap;
8953{
8954 int whole = TRUE;
8955 long n;
8956 char_u *p;
8957 int action;
8958
8959 switch (cmdnames[eap->cmdidx].cmd_name[2])
8960 {
8961 case 'e': /* ":psearch", ":isearch" and ":dsearch" */
8962 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
8963 action = ACTION_GOTO;
8964 else
8965 action = ACTION_SHOW;
8966 break;
8967 case 'i': /* ":ilist" and ":dlist" */
8968 action = ACTION_SHOW_ALL;
8969 break;
8970 case 'u': /* ":ijump" and ":djump" */
8971 action = ACTION_GOTO;
8972 break;
8973 default: /* ":isplit" and ":dsplit" */
8974 action = ACTION_SPLIT;
8975 break;
8976 }
8977
8978 n = 1;
8979 if (vim_isdigit(*eap->arg)) /* get count */
8980 {
8981 n = getdigits(&eap->arg);
8982 eap->arg = skipwhite(eap->arg);
8983 }
8984 if (*eap->arg == '/') /* Match regexp, not just whole words */
8985 {
8986 whole = FALSE;
8987 ++eap->arg;
8988 p = skip_regexp(eap->arg, '/', p_magic, NULL);
8989 if (*p)
8990 {
8991 *p++ = NUL;
8992 p = skipwhite(p);
8993
8994 /* Check for trailing illegal characters */
8995 if (!ends_excmd(*p))
8996 eap->errmsg = e_trailing;
8997 else
8998 eap->nextcmd = check_nextcmd(p);
8999 }
9000 }
9001 if (!eap->skip)
9002 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
9003 whole, !eap->forceit,
9004 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
9005 n, action, eap->line1, eap->line2);
9006}
9007#endif
9008
9009#ifdef FEAT_WINDOWS
9010
9011# ifdef FEAT_QUICKFIX
9012/*
9013 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
9014 */
9015 static void
9016ex_ptag(eap)
9017 exarg_T *eap;
9018{
9019 g_do_tagpreview = p_pvh;
9020 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9021}
9022
9023/*
9024 * ":pedit"
9025 */
9026 static void
9027ex_pedit(eap)
9028 exarg_T *eap;
9029{
9030 win_T *curwin_save = curwin;
9031
9032 g_do_tagpreview = p_pvh;
9033 prepare_tagpreview();
9034 keep_help_flag = curwin_save->w_buffer->b_help;
9035 do_exedit(eap, NULL);
9036 keep_help_flag = FALSE;
9037 if (curwin != curwin_save && win_valid(curwin_save))
9038 {
9039 /* Return cursor to where we were */
9040 validate_cursor();
9041 redraw_later(VALID);
9042 win_enter(curwin_save, TRUE);
9043 }
9044 g_do_tagpreview = 0;
9045}
9046# endif
9047
9048/*
9049 * ":stag", ":stselect" and ":stjump".
9050 */
9051 static void
9052ex_stag(eap)
9053 exarg_T *eap;
9054{
9055 postponed_split = -1;
9056 postponed_split_flags = cmdmod.split;
9057 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9058 postponed_split_flags = 0;
9059}
9060#endif
9061
9062/*
9063 * ":tag", ":tselect", ":tjump", ":tnext", etc.
9064 */
9065 static void
9066ex_tag(eap)
9067 exarg_T *eap;
9068{
9069 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
9070}
9071
9072 static void
9073ex_tag_cmd(eap, name)
9074 exarg_T *eap;
9075 char_u *name;
9076{
9077 int cmd;
9078
9079 switch (name[1])
9080 {
9081 case 'j': cmd = DT_JUMP; /* ":tjump" */
9082 break;
9083 case 's': cmd = DT_SELECT; /* ":tselect" */
9084 break;
9085 case 'p': cmd = DT_PREV; /* ":tprevious" */
9086 break;
9087 case 'N': cmd = DT_PREV; /* ":tNext" */
9088 break;
9089 case 'n': cmd = DT_NEXT; /* ":tnext" */
9090 break;
9091 case 'o': cmd = DT_POP; /* ":pop" */
9092 break;
9093 case 'f': /* ":tfirst" */
9094 case 'r': cmd = DT_FIRST; /* ":trewind" */
9095 break;
9096 case 'l': cmd = DT_LAST; /* ":tlast" */
9097 break;
9098 default: /* ":tag" */
9099#ifdef FEAT_CSCOPE
9100 if (p_cst)
9101 {
9102 do_cstag(eap);
9103 return;
9104 }
9105#endif
9106 cmd = DT_TAG;
9107 break;
9108 }
9109
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00009110 if (name[0] == 'l')
9111 {
9112#ifndef FEAT_QUICKFIX
9113 ex_ni(eap);
9114 return;
9115#else
9116 cmd = DT_LTAG;
9117#endif
9118 }
9119
Bram Moolenaar071d4272004-06-13 20:20:40 +00009120 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
9121 eap->forceit, TRUE);
9122}
9123
9124/*
9125 * Evaluate cmdline variables.
9126 *
9127 * change '%' to curbuf->b_ffname
9128 * '#' to curwin->w_altfile
9129 * '<cword>' to word under the cursor
9130 * '<cWORD>' to WORD under the cursor
9131 * '<cfile>' to path name under the cursor
9132 * '<sfile>' to sourced file name
9133 * '<afile>' to file name for autocommand
9134 * '<abuf>' to buffer number for autocommand
9135 * '<amatch>' to matching name for autocommand
9136 *
9137 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
9138 * "" for error without a message) and NULL is returned.
9139 * Returns an allocated string if a valid match was found.
9140 * Returns NULL if no match was found. "usedlen" then still contains the
9141 * number of characters to skip.
9142 */
9143 char_u *
9144eval_vars(src, usedlen, lnump, errormsg, srcstart)
9145 char_u *src; /* pointer into commandline */
9146 int *usedlen; /* characters after src that are used */
9147 linenr_T *lnump; /* line number for :e command, or NULL */
9148 char_u **errormsg; /* pointer to error message */
9149 char_u *srcstart; /* beginning of valid memory for src */
9150{
9151 int i;
9152 char_u *s;
9153 char_u *result;
9154 char_u *resultbuf = NULL;
9155 int resultlen;
9156 buf_T *buf;
9157 int valid = VALID_HEAD + VALID_PATH; /* assume valid result */
9158 int spec_idx;
9159#ifdef FEAT_MODIFY_FNAME
9160 int skip_mod = FALSE;
9161#endif
9162 static char *(spec_str[]) =
9163 {
9164 "%",
9165#define SPEC_PERC 0
9166 "#",
9167#define SPEC_HASH 1
9168 "<cword>", /* cursor word */
9169#define SPEC_CWORD 2
9170 "<cWORD>", /* cursor WORD */
9171#define SPEC_CCWORD 3
9172 "<cfile>", /* cursor path name */
9173#define SPEC_CFILE 4
9174 "<sfile>", /* ":so" file name */
9175#define SPEC_SFILE 5
9176#ifdef FEAT_AUTOCMD
9177 "<afile>", /* autocommand file name */
9178# define SPEC_AFILE 6
9179 "<abuf>", /* autocommand buffer number */
9180# define SPEC_ABUF 7
9181 "<amatch>", /* autocommand match name */
9182# define SPEC_AMATCH 8
9183#endif
9184#ifdef FEAT_CLIENTSERVER
9185 "<client>"
9186# define SPEC_CLIENT 9
9187#endif
9188 };
9189#define SPEC_COUNT (sizeof(spec_str) / sizeof(char *))
9190
9191#if defined(FEAT_AUTOCMD) || defined(FEAT_CLIENTSERVER)
9192 char_u strbuf[30];
9193#endif
9194
9195 *errormsg = NULL;
9196
9197 /*
9198 * Check if there is something to do.
9199 */
9200 for (spec_idx = 0; spec_idx < SPEC_COUNT; ++spec_idx)
9201 {
9202 *usedlen = (int)STRLEN(spec_str[spec_idx]);
9203 if (STRNCMP(src, spec_str[spec_idx], *usedlen) == 0)
9204 break;
9205 }
9206 if (spec_idx == SPEC_COUNT) /* no match */
9207 {
9208 *usedlen = 1;
9209 return NULL;
9210 }
9211
9212 /*
9213 * Skip when preceded with a backslash "\%" and "\#".
9214 * Note: In "\\%" the % is also not recognized!
9215 */
9216 if (src > srcstart && src[-1] == '\\')
9217 {
9218 *usedlen = 0;
9219 STRCPY(src - 1, src); /* remove backslash */
9220 return NULL;
9221 }
9222
9223 /*
9224 * word or WORD under cursor
9225 */
9226 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD)
9227 {
9228 resultlen = find_ident_under_cursor(&result, spec_idx == SPEC_CWORD ?
9229 (FIND_IDENT|FIND_STRING) : FIND_STRING);
9230 if (resultlen == 0)
9231 {
9232 *errormsg = (char_u *)"";
9233 return NULL;
9234 }
9235 }
9236
9237 /*
9238 * '#': Alternate file name
9239 * '%': Current file name
9240 * File name under the cursor
9241 * File name for autocommand
9242 * and following modifiers
9243 */
9244 else
9245 {
9246 switch (spec_idx)
9247 {
9248 case SPEC_PERC: /* '%': current file */
9249 if (curbuf->b_fname == NULL)
9250 {
9251 result = (char_u *)"";
9252 valid = 0; /* Must have ":p:h" to be valid */
9253 }
9254 else
9255#ifdef RISCOS
9256 /* Always use the full path for RISC OS if possible. */
9257 result = curbuf->b_ffname;
9258 if (result == NULL)
9259 result = curbuf->b_fname;
9260#else
9261 result = curbuf->b_fname;
9262#endif
9263 break;
9264
9265 case SPEC_HASH: /* '#' or "#99": alternate file */
9266 if (src[1] == '#') /* "##": the argument list */
9267 {
9268 result = arg_all();
9269 resultbuf = result;
9270 *usedlen = 2;
9271#ifdef FEAT_MODIFY_FNAME
9272 skip_mod = TRUE;
9273#endif
9274 break;
9275 }
9276 s = src + 1;
9277 i = (int)getdigits(&s);
9278 *usedlen = (int)(s - src); /* length of what we expand */
9279
9280 buf = buflist_findnr(i);
9281 if (buf == NULL)
9282 {
9283 *errormsg = (char_u *)_("E194: No alternate file name to substitute for '#'");
9284 return NULL;
9285 }
9286 if (lnump != NULL)
9287 *lnump = ECMD_LAST;
9288 if (buf->b_fname == NULL)
9289 {
9290 result = (char_u *)"";
9291 valid = 0; /* Must have ":p:h" to be valid */
9292 }
9293 else
9294 result = buf->b_fname;
9295 break;
9296
9297#ifdef FEAT_SEARCHPATH
9298 case SPEC_CFILE: /* file name under cursor */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009299 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009300 if (result == NULL)
9301 {
9302 *errormsg = (char_u *)"";
9303 return NULL;
9304 }
9305 resultbuf = result; /* remember allocated string */
9306 break;
9307#endif
9308
9309#ifdef FEAT_AUTOCMD
9310 case SPEC_AFILE: /* file name for autocommand */
9311 result = autocmd_fname;
9312 if (result == NULL)
9313 {
9314 *errormsg = (char_u *)_("E495: no autocommand file name to substitute for \"<afile>\"");
9315 return NULL;
9316 }
9317 break;
9318
9319 case SPEC_ABUF: /* buffer number for autocommand */
9320 if (autocmd_bufnr <= 0)
9321 {
9322 *errormsg = (char_u *)_("E496: no autocommand buffer number to substitute for \"<abuf>\"");
9323 return NULL;
9324 }
9325 sprintf((char *)strbuf, "%d", autocmd_bufnr);
9326 result = strbuf;
9327 break;
9328
9329 case SPEC_AMATCH: /* match name for autocommand */
9330 result = autocmd_match;
9331 if (result == NULL)
9332 {
9333 *errormsg = (char_u *)_("E497: no autocommand match name to substitute for \"<amatch>\"");
9334 return NULL;
9335 }
9336 break;
9337
9338#endif
9339 case SPEC_SFILE: /* file name for ":so" command */
9340 result = sourcing_name;
9341 if (result == NULL)
9342 {
9343 *errormsg = (char_u *)_("E498: no :source file name to substitute for \"<sfile>\"");
9344 return NULL;
9345 }
9346 break;
9347#if defined(FEAT_CLIENTSERVER)
9348 case SPEC_CLIENT: /* Source of last submitted input */
9349 sprintf((char *)strbuf, "0x%x", (unsigned int)clientWindow);
9350 result = strbuf;
9351 break;
9352#endif
9353 }
9354
9355 resultlen = (int)STRLEN(result); /* length of new string */
9356 if (src[*usedlen] == '<') /* remove the file name extension */
9357 {
9358 ++*usedlen;
9359#ifdef RISCOS
9360 if ((s = vim_strrchr(result, '/')) != NULL && s >= gettail(result))
9361#else
9362 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
9363#endif
9364 resultlen = (int)(s - result);
9365 }
9366#ifdef FEAT_MODIFY_FNAME
9367 else if (!skip_mod)
9368 {
9369 valid |= modify_fname(src, usedlen, &result, &resultbuf,
9370 &resultlen);
9371 if (result == NULL)
9372 {
9373 *errormsg = (char_u *)"";
9374 return NULL;
9375 }
9376 }
9377#endif
9378 }
9379
9380 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
9381 {
9382 if (valid != VALID_HEAD + VALID_PATH)
9383 /* xgettext:no-c-format */
9384 *errormsg = (char_u *)_("E499: Empty file name for '%' or '#', only works with \":p:h\"");
9385 else
9386 *errormsg = (char_u *)_("E500: Evaluates to an empty string");
9387 result = NULL;
9388 }
9389 else
9390 result = vim_strnsave(result, resultlen);
9391 vim_free(resultbuf);
9392 return result;
9393}
9394
9395/*
9396 * Concatenate all files in the argument list, separated by spaces, and return
9397 * it in one allocated string.
9398 * Spaces and backslashes in the file names are escaped with a backslash.
9399 * Returns NULL when out of memory.
9400 */
9401 static char_u *
9402arg_all()
9403{
9404 int len;
9405 int idx;
9406 char_u *retval = NULL;
9407 char_u *p;
9408
9409 /*
9410 * Do this loop two times:
9411 * first time: compute the total length
9412 * second time: concatenate the names
9413 */
9414 for (;;)
9415 {
9416 len = 0;
9417 for (idx = 0; idx < ARGCOUNT; ++idx)
9418 {
9419 p = alist_name(&ARGLIST[idx]);
9420 if (p != NULL)
9421 {
9422 if (len > 0)
9423 {
9424 /* insert a space in between names */
9425 if (retval != NULL)
9426 retval[len] = ' ';
9427 ++len;
9428 }
9429 for ( ; *p != NUL; ++p)
9430 {
9431 if (*p == ' ' || *p == '\\')
9432 {
9433 /* insert a backslash */
9434 if (retval != NULL)
9435 retval[len] = '\\';
9436 ++len;
9437 }
9438 if (retval != NULL)
9439 retval[len] = *p;
9440 ++len;
9441 }
9442 }
9443 }
9444
9445 /* second time: break here */
9446 if (retval != NULL)
9447 {
9448 retval[len] = NUL;
9449 break;
9450 }
9451
9452 /* allocate memory */
9453 retval = alloc(len + 1);
9454 if (retval == NULL)
9455 break;
9456 }
9457
9458 return retval;
9459}
9460
9461#if defined(FEAT_AUTOCMD) || defined(PROTO)
9462/*
9463 * Expand the <sfile> string in "arg".
9464 *
9465 * Returns an allocated string, or NULL for any error.
9466 */
9467 char_u *
9468expand_sfile(arg)
9469 char_u *arg;
9470{
9471 char_u *errormsg;
9472 int len;
9473 char_u *result;
9474 char_u *newres;
9475 char_u *repl;
9476 int srclen;
9477 char_u *p;
9478
9479 result = vim_strsave(arg);
9480 if (result == NULL)
9481 return NULL;
9482
9483 for (p = result; *p; )
9484 {
9485 if (STRNCMP(p, "<sfile>", 7) != 0)
9486 ++p;
9487 else
9488 {
9489 /* replace "<sfile>" with the sourced file name, and do ":" stuff */
9490 repl = eval_vars(p, &srclen, NULL, &errormsg, result);
9491 if (errormsg != NULL)
9492 {
9493 if (*errormsg)
9494 emsg(errormsg);
9495 vim_free(result);
9496 return NULL;
9497 }
9498 if (repl == NULL) /* no match (cannot happen) */
9499 {
9500 p += srclen;
9501 continue;
9502 }
9503 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
9504 newres = alloc(len);
9505 if (newres == NULL)
9506 {
9507 vim_free(repl);
9508 vim_free(result);
9509 return NULL;
9510 }
9511 mch_memmove(newres, result, (size_t)(p - result));
9512 STRCPY(newres + (p - result), repl);
9513 len = (int)STRLEN(newres);
9514 STRCAT(newres, p + srclen);
9515 vim_free(repl);
9516 vim_free(result);
9517 result = newres;
9518 p = newres + len; /* continue after the match */
9519 }
9520 }
9521
9522 return result;
9523}
9524#endif
9525
9526#ifdef FEAT_SESSION
9527static int ses_winsizes __ARGS((FILE *fd, int restore_size));
9528static int ses_win_rec __ARGS((FILE *fd, frame_T *fr));
9529static frame_T *ses_skipframe __ARGS((frame_T *fr));
9530static int ses_do_frame __ARGS((frame_T *fr));
9531static int ses_do_win __ARGS((win_T *wp));
9532static int ses_arglist __ARGS((FILE *fd, char *cmd, garray_T *gap, int fullname, unsigned *flagp));
9533static int ses_put_fname __ARGS((FILE *fd, char_u *name, unsigned *flagp));
9534static int ses_fname __ARGS((FILE *fd, buf_T *buf, unsigned *flagp));
9535
9536/*
9537 * Write openfile commands for the current buffers to an .exrc file.
9538 * Return FAIL on error, OK otherwise.
9539 */
9540 static int
9541makeopens(fd, dirnow)
9542 FILE *fd;
9543 char_u *dirnow; /* Current directory name */
9544{
9545 buf_T *buf;
9546 int only_save_windows = TRUE;
9547 int nr;
9548 int cnr = 1;
9549 int restore_size = TRUE;
9550 win_T *wp;
9551 char_u *sname;
9552 win_T *edited_win = NULL;
9553
9554 if (ssop_flags & SSOP_BUFFERS)
9555 only_save_windows = FALSE; /* Save ALL buffers */
9556
9557 /*
9558 * Begin by setting the this_session variable, and then other
9559 * sessionable variables.
9560 */
9561#ifdef FEAT_EVAL
9562 if (put_line(fd, "let v:this_session=expand(\"<sfile>:p\")") == FAIL)
9563 return FAIL;
9564 if (ssop_flags & SSOP_GLOBALS)
9565 if (store_session_globals(fd) == FAIL)
9566 return FAIL;
9567#endif
9568
9569 /*
9570 * Close all windows but one.
9571 */
9572 if (put_line(fd, "silent only") == FAIL)
9573 return FAIL;
9574
9575 /*
9576 * Now a :cd command to the session directory or the current directory
9577 */
9578 if (ssop_flags & SSOP_SESDIR)
9579 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009580 if (put_line(fd, "exe \"cd \" . escape(expand(\"<sfile>:p:h\"), ' ')")
9581 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009582 return FAIL;
9583 }
9584 else if (ssop_flags & SSOP_CURDIR)
9585 {
9586 sname = home_replace_save(NULL, globaldir != NULL ? globaldir : dirnow);
9587 if (sname == NULL
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009588 || fputs("cd ", fd) < 0
9589 || ses_put_fname(fd, sname, &ssop_flags) == FAIL
9590 || put_eol(fd) == FAIL)
9591 {
9592 vim_free(sname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009593 return FAIL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009594 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009595 vim_free(sname);
9596 }
9597
9598 /*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009599 * If there is an empty, unnamed buffer we will wipe it out later.
9600 * Remember the buffer number.
9601 */
9602 if (put_line(fd, "if expand('%') == '' && !&modified && line('$') <= 1 && getline(1) == ''") == FAIL)
9603 return FAIL;
9604 if (put_line(fd, " let s:wipebuf = bufnr('%')") == FAIL)
9605 return FAIL;
9606 if (put_line(fd, "endif") == FAIL)
9607 return FAIL;
9608
9609 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009610 * Now save the current files, current buffer first.
9611 */
9612 if (put_line(fd, "set shortmess=aoO") == FAIL)
9613 return FAIL;
9614
9615 /* Now put the other buffers into the buffer list */
9616 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9617 {
9618 if (!(only_save_windows && buf->b_nwindows == 0)
9619 && !(buf->b_help && !(ssop_flags & SSOP_HELP))
9620 && buf->b_fname != NULL
9621 && buf->b_p_bl)
9622 {
9623 if (fprintf(fd, "badd +%ld ", buf->b_wininfo == NULL ? 1L
9624 : buf->b_wininfo->wi_fpos.lnum) < 0
9625 || ses_fname(fd, buf, &ssop_flags) == FAIL)
9626 return FAIL;
9627 }
9628 }
9629
9630 /* the global argument list */
9631 if (ses_arglist(fd, "args", &global_alist.al_ga,
9632 !(ssop_flags & SSOP_CURDIR), &ssop_flags) == FAIL)
9633 return FAIL;
9634
9635 if (ssop_flags & SSOP_RESIZE)
9636 {
9637 /* Note: after the restore we still check it worked!*/
9638 if (fprintf(fd, "set lines=%ld columns=%ld" , Rows, Columns) < 0
9639 || put_eol(fd) == FAIL)
9640 return FAIL;
9641 }
9642
9643#ifdef FEAT_GUI
9644 if (gui.in_use && (ssop_flags & SSOP_WINPOS))
9645 {
9646 int x, y;
9647
9648 if (gui_mch_get_winpos(&x, &y) == OK)
9649 {
9650 /* Note: after the restore we still check it worked!*/
9651 if (fprintf(fd, "winpos %d %d", x, y) < 0 || put_eol(fd) == FAIL)
9652 return FAIL;
9653 }
9654 }
9655#endif
9656
9657 /*
9658 * Before creating the window layout, try loading one file. If this is
9659 * aborted we don't end up with a number of useless windows.
9660 * This may have side effects! (e.g., compressed or network file).
9661 */
9662 for (wp = firstwin; wp != NULL; wp = wp->w_next)
9663 {
9664 if (ses_do_win(wp)
9665 && wp->w_buffer->b_ffname != NULL
9666 && !wp->w_buffer->b_help
9667#ifdef FEAT_QUICKFIX
9668 && !bt_nofile(wp->w_buffer)
9669#endif
9670 )
9671 {
9672 if (fputs("edit ", fd) < 0
9673 || ses_fname(fd, wp->w_buffer, &ssop_flags) == FAIL)
9674 return FAIL;
9675 if (!wp->w_arg_idx_invalid)
9676 edited_win = wp;
9677 break;
9678 }
9679 }
9680
9681 /*
9682 * Save current window layout.
9683 */
9684 if (put_line(fd, "set splitbelow splitright") == FAIL)
9685 return FAIL;
9686 if (ses_win_rec(fd, topframe) == FAIL)
9687 return FAIL;
9688 if (!p_sb && put_line(fd, "set nosplitbelow") == FAIL)
9689 return FAIL;
9690 if (!p_spr && put_line(fd, "set nosplitright") == FAIL)
9691 return FAIL;
9692
9693 /*
9694 * Check if window sizes can be restored (no windows omitted).
9695 * Remember the window number of the current window after restoring.
9696 */
9697 nr = 0;
9698 for (wp = firstwin; wp != NULL; wp = W_NEXT(wp))
9699 {
9700 if (ses_do_win(wp))
9701 ++nr;
9702 else
9703 restore_size = FALSE;
9704 if (curwin == wp)
9705 cnr = nr;
9706 }
9707
9708 /* Go to the first window. */
9709 if (put_line(fd, "wincmd t") == FAIL)
9710 return FAIL;
9711
9712 /*
9713 * If more than one window, see if sizes can be restored.
9714 * First set 'winheight' and 'winwidth' to 1 to avoid the windows being
9715 * resized when moving between windows.
9716 * Do this before restoring the view, so that the topline and the cursor
9717 * can be set. This is done again below.
9718 */
9719 if (put_line(fd, "set winheight=1 winwidth=1") == FAIL)
9720 return FAIL;
9721 if (nr > 1 && ses_winsizes(fd, restore_size) == FAIL)
9722 return FAIL;
9723
9724 /*
9725 * Restore the view of the window (options, file, cursor, etc.).
9726 */
9727 for (wp = firstwin; wp != NULL; wp = wp->w_next)
9728 {
9729 if (!ses_do_win(wp))
9730 continue;
9731 if (put_view(fd, wp, wp != edited_win, &ssop_flags) == FAIL)
9732 return FAIL;
9733 if (nr > 1 && put_line(fd, "wincmd w") == FAIL)
9734 return FAIL;
9735 }
9736
9737 /*
9738 * Restore cursor to the current window if it's not the first one.
9739 */
9740 if (cnr > 1 && (fprintf(fd, "%dwincmd w", cnr) < 0 || put_eol(fd) == FAIL))
9741 return FAIL;
9742
9743 /*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009744 * Wipe out an empty unnamed buffer we started in.
9745 */
9746 if (put_line(fd, "if exists('s:wipebuf')") == FAIL)
9747 return FAIL;
9748 if (put_line(fd, " exe 'bwipe ' . s:wipebuf") == FAIL)
9749 return FAIL;
9750 if (put_line(fd, "endif") == FAIL)
9751 return FAIL;
9752 if (put_line(fd, "unlet! s:wipebuf") == FAIL)
9753 return FAIL;
9754
9755 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009756 * Restore window sizes again after jumping around in windows, because the
9757 * current window has a minimum size while others may not.
9758 */
9759 if (nr > 1 && ses_winsizes(fd, restore_size) == FAIL)
9760 return FAIL;
9761
9762 /* Re-apply 'winheight', 'winwidth' and 'shortmess'. */
9763 if (fprintf(fd, "set winheight=%ld winwidth=%ld shortmess=%s",
9764 p_wh, p_wiw, p_shm) < 0 || put_eol(fd) == FAIL)
9765 return FAIL;
9766
9767 /*
9768 * Lastly, execute the x.vim file if it exists.
9769 */
9770 if (put_line(fd, "let s:sx = expand(\"<sfile>:p:r\").\"x.vim\"") == FAIL
9771 || put_line(fd, "if file_readable(s:sx)") == FAIL
9772 || put_line(fd, " exe \"source \" . s:sx") == FAIL
9773 || put_line(fd, "endif") == FAIL)
9774 return FAIL;
9775
9776 return OK;
9777}
9778
9779 static int
9780ses_winsizes(fd, restore_size)
9781 FILE *fd;
9782 int restore_size;
9783{
9784 int n = 0;
9785 win_T *wp;
9786
9787 if (restore_size && (ssop_flags & SSOP_WINSIZE))
9788 {
9789 for (wp = firstwin; wp != NULL; wp = wp->w_next)
9790 {
9791 if (!ses_do_win(wp))
9792 continue;
9793 ++n;
9794
9795 /* restore height when not full height */
9796 if (wp->w_height + wp->w_status_height < topframe->fr_height
9797 && (fprintf(fd,
9798 "exe '%dresize ' . ((&lines * %ld + %ld) / %ld)",
9799 n, (long)wp->w_height, Rows / 2, Rows) < 0
9800 || put_eol(fd) == FAIL))
9801 return FAIL;
9802
9803 /* restore width when not full width */
9804 if (wp->w_width < Columns && (fprintf(fd,
9805 "exe 'vert %dresize ' . ((&columns * %ld + %ld) / %ld)",
9806 n, (long)wp->w_width, Columns / 2, Columns) < 0
9807 || put_eol(fd) == FAIL))
9808 return FAIL;
9809 }
9810 }
9811 else
9812 {
9813 /* Just equalise window sizes */
9814 if (put_line(fd, "wincmd =") == FAIL)
9815 return FAIL;
9816 }
9817 return OK;
9818}
9819
9820/*
9821 * Write commands to "fd" to recursively create windows for frame "fr",
9822 * horizontally and vertically split.
9823 * After the commands the last window in the frame is the current window.
9824 * Returns FAIL when writing the commands to "fd" fails.
9825 */
9826 static int
9827ses_win_rec(fd, fr)
9828 FILE *fd;
9829 frame_T *fr;
9830{
9831 frame_T *frc;
9832 int count = 0;
9833
9834 if (fr->fr_layout != FR_LEAF)
9835 {
9836 /* Find first frame that's not skipped and then create a window for
9837 * each following one (first frame is already there). */
9838 frc = ses_skipframe(fr->fr_child);
9839 if (frc != NULL)
9840 while ((frc = ses_skipframe(frc->fr_next)) != NULL)
9841 {
9842 /* Make window as big as possible so that we have lots of room
9843 * to split. */
9844 if (put_line(fd, "wincmd _ | wincmd |") == FAIL
9845 || put_line(fd, fr->fr_layout == FR_COL
9846 ? "split" : "vsplit") == FAIL)
9847 return FAIL;
9848 ++count;
9849 }
9850
9851 /* Go back to the first window. */
9852 if (count > 0 && (fprintf(fd, fr->fr_layout == FR_COL
9853 ? "%dwincmd k" : "%dwincmd h", count) < 0
9854 || put_eol(fd) == FAIL))
9855 return FAIL;
9856
9857 /* Recursively create frames/windows in each window of this column or
9858 * row. */
9859 frc = ses_skipframe(fr->fr_child);
9860 while (frc != NULL)
9861 {
9862 ses_win_rec(fd, frc);
9863 frc = ses_skipframe(frc->fr_next);
9864 /* Go to next window. */
9865 if (frc != NULL && put_line(fd, "wincmd w") == FAIL)
9866 return FAIL;
9867 }
9868 }
9869 return OK;
9870}
9871
9872/*
9873 * Skip frames that don't contain windows we want to save in the Session.
9874 * Returns NULL when there none.
9875 */
9876 static frame_T *
9877ses_skipframe(fr)
9878 frame_T *fr;
9879{
9880 frame_T *frc;
9881
9882 for (frc = fr; frc != NULL; frc = frc->fr_next)
9883 if (ses_do_frame(frc))
9884 break;
9885 return frc;
9886}
9887
9888/*
9889 * Return TRUE if frame "fr" has a window somewhere that we want to save in
9890 * the Session.
9891 */
9892 static int
9893ses_do_frame(fr)
9894 frame_T *fr;
9895{
9896 frame_T *frc;
9897
9898 if (fr->fr_layout == FR_LEAF)
9899 return ses_do_win(fr->fr_win);
9900 for (frc = fr->fr_child; frc != NULL; frc = frc->fr_next)
9901 if (ses_do_frame(frc))
9902 return TRUE;
9903 return FALSE;
9904}
9905
9906/*
9907 * Return non-zero if window "wp" is to be stored in the Session.
9908 */
9909 static int
9910ses_do_win(wp)
9911 win_T *wp;
9912{
9913 if (wp->w_buffer->b_fname == NULL
9914#ifdef FEAT_QUICKFIX
9915 /* When 'buftype' is "nofile" can't restore the window contents. */
9916 || bt_nofile(wp->w_buffer)
9917#endif
9918 )
9919 return (ssop_flags & SSOP_BLANK);
9920 if (wp->w_buffer->b_help)
9921 return (ssop_flags & SSOP_HELP);
9922 return TRUE;
9923}
9924
9925/*
9926 * Write commands to "fd" to restore the view of a window.
9927 * Caller must make sure 'scrolloff' is zero.
9928 */
9929 static int
9930put_view(fd, wp, add_edit, flagp)
9931 FILE *fd;
9932 win_T *wp;
9933 int add_edit; /* add ":edit" command to view */
9934 unsigned *flagp; /* vop_flags or ssop_flags */
9935{
9936 win_T *save_curwin;
9937 int f;
9938 int do_cursor;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00009939 int did_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009940
9941 /* Always restore cursor position for ":mksession". For ":mkview" only
9942 * when 'viewoptions' contains "cursor". */
9943 do_cursor = (flagp == &ssop_flags || *flagp & SSOP_CURSOR);
9944
9945 /*
9946 * Local argument list.
9947 */
9948 if (wp->w_alist == &global_alist)
9949 {
9950 if (put_line(fd, "argglobal") == FAIL)
9951 return FAIL;
9952 }
9953 else
9954 {
9955 if (ses_arglist(fd, "arglocal", &wp->w_alist->al_ga,
9956 flagp == &vop_flags
9957 || !(*flagp & SSOP_CURDIR)
9958 || wp->w_localdir != NULL, flagp) == FAIL)
9959 return FAIL;
9960 }
9961
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00009962 /* Only when part of a session: restore the argument index. Some
9963 * arguments may have been deleted, check if the index is valid. */
9964 if (wp->w_arg_idx != 0 && wp->w_arg_idx <= WARGCOUNT(wp)
9965 && flagp == &ssop_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009966 {
9967 if (fprintf(fd, "%ldnext", (long)wp->w_arg_idx) < 0
9968 || put_eol(fd) == FAIL)
9969 return FAIL;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00009970 did_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009971 }
9972
9973 /* Edit the file. Skip this when ":next" already did it. */
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00009974 if (add_edit && (!did_next || wp->w_arg_idx_invalid))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009975 {
9976 /*
9977 * Load the file.
9978 */
9979 if (wp->w_buffer->b_ffname != NULL
9980#ifdef FEAT_QUICKFIX
9981 && !bt_nofile(wp->w_buffer)
9982#endif
9983 )
9984 {
9985 /*
9986 * Editing a file in this buffer: use ":edit file".
9987 * This may have side effects! (e.g., compressed or network file).
9988 */
9989 if (fputs("edit ", fd) < 0
9990 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
9991 return FAIL;
9992 }
9993 else
9994 {
9995 /* No file in this buffer, just make it empty. */
9996 if (put_line(fd, "enew") == FAIL)
9997 return FAIL;
9998#ifdef FEAT_QUICKFIX
9999 if (wp->w_buffer->b_ffname != NULL)
10000 {
10001 /* The buffer does have a name, but it's not a file name. */
10002 if (fputs("file ", fd) < 0
10003 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
10004 return FAIL;
10005 }
10006#endif
10007 do_cursor = FALSE;
10008 }
10009 }
10010
10011 /*
10012 * Local mappings and abbreviations.
10013 */
10014 if ((*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
10015 && makemap(fd, wp->w_buffer) == FAIL)
10016 return FAIL;
10017
10018 /*
10019 * Local options. Need to go to the window temporarily.
10020 * Store only local values when using ":mkview" and when ":mksession" is
10021 * used and 'sessionoptions' doesn't include "options".
10022 * Some folding options are always stored when "folds" is included,
10023 * otherwise the folds would not be restored correctly.
10024 */
10025 save_curwin = curwin;
10026 curwin = wp;
10027 curbuf = curwin->w_buffer;
10028 if (*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
10029 f = makeset(fd, OPT_LOCAL,
10030 flagp == &vop_flags || !(*flagp & SSOP_OPTIONS));
10031#ifdef FEAT_FOLDING
10032 else if (*flagp & SSOP_FOLDS)
10033 f = makefoldset(fd);
10034#endif
10035 else
10036 f = OK;
10037 curwin = save_curwin;
10038 curbuf = curwin->w_buffer;
10039 if (f == FAIL)
10040 return FAIL;
10041
10042#ifdef FEAT_FOLDING
10043 /*
10044 * Save Folds when 'buftype' is empty and for help files.
10045 */
10046 if ((*flagp & SSOP_FOLDS)
10047 && wp->w_buffer->b_ffname != NULL
Bram Moolenaarb1b715d2006-01-21 22:09:43 +000010048# ifdef FEAT_QUICKFIX
10049 && (*wp->w_buffer->b_p_bt == NUL || wp->w_buffer->b_help)
10050# endif
10051 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000010052 {
10053 if (put_folds(fd, wp) == FAIL)
10054 return FAIL;
10055 }
10056#endif
10057
10058 /*
10059 * Set the cursor after creating folds, since that moves the cursor.
10060 */
10061 if (do_cursor)
10062 {
10063
10064 /* Restore the cursor line in the file and relatively in the
10065 * window. Don't use "G", it changes the jumplist. */
10066 if (fprintf(fd, "let s:l = %ld - ((%ld * winheight(0) + %ld) / %ld)",
10067 (long)wp->w_cursor.lnum,
10068 (long)(wp->w_cursor.lnum - wp->w_topline),
10069 (long)wp->w_height / 2, (long)wp->w_height) < 0
10070 || put_eol(fd) == FAIL
10071 || put_line(fd, "if s:l < 1 | let s:l = 1 | endif") == FAIL
10072 || put_line(fd, "exe s:l") == FAIL
10073 || put_line(fd, "normal! zt") == FAIL
10074 || fprintf(fd, "%ld", (long)wp->w_cursor.lnum) < 0
10075 || put_eol(fd) == FAIL)
10076 return FAIL;
10077 /* Restore the cursor column and left offset when not wrapping. */
10078 if (wp->w_cursor.col == 0)
10079 {
10080 if (put_line(fd, "normal! 0") == FAIL)
10081 return FAIL;
10082 }
10083 else
10084 {
10085 if (!wp->w_p_wrap && wp->w_leftcol > 0 && wp->w_width > 0)
10086 {
10087 if (fprintf(fd,
10088 "let s:c = %ld - ((%ld * winwidth(0) + %ld) / %ld)",
10089 (long)wp->w_cursor.col,
10090 (long)(wp->w_cursor.col - wp->w_leftcol),
10091 (long)wp->w_width / 2, (long)wp->w_width) < 0
10092 || put_eol(fd) == FAIL
10093 || put_line(fd, "if s:c > 0") == FAIL
10094 || fprintf(fd,
10095 " exe 'normal! 0' . s:c . 'lzs' . (%ld - s:c) . 'l'",
10096 (long)wp->w_cursor.col) < 0
10097 || put_eol(fd) == FAIL
10098 || put_line(fd, "else") == FAIL
10099 || fprintf(fd, " normal! 0%dl", wp->w_cursor.col) < 0
10100 || put_eol(fd) == FAIL
10101 || put_line(fd, "endif") == FAIL)
10102 return FAIL;
10103 }
10104 else
10105 {
10106 if (fprintf(fd, "normal! 0%dl", wp->w_cursor.col) < 0
10107 || put_eol(fd) == FAIL)
10108 return FAIL;
10109 }
10110 }
10111 }
10112
10113 /*
10114 * Local directory.
10115 */
10116 if (wp->w_localdir != NULL)
10117 {
10118 if (fputs("lcd ", fd) < 0
10119 || ses_put_fname(fd, wp->w_localdir, flagp) == FAIL
10120 || put_eol(fd) == FAIL)
10121 return FAIL;
10122 }
10123
10124 return OK;
10125}
10126
10127/*
10128 * Write an argument list to the session file.
10129 * Returns FAIL if writing fails.
10130 */
10131 static int
10132ses_arglist(fd, cmd, gap, fullname, flagp)
10133 FILE *fd;
10134 char *cmd;
10135 garray_T *gap;
10136 int fullname; /* TRUE: use full path name */
10137 unsigned *flagp;
10138{
10139 int i;
10140 char_u buf[MAXPATHL];
10141 char_u *s;
10142
10143 if (gap->ga_len == 0)
10144 return put_line(fd, "silent! argdel *");
10145 if (fputs(cmd, fd) < 0)
10146 return FAIL;
10147 for (i = 0; i < gap->ga_len; ++i)
10148 {
10149 /* NULL file names are skipped (only happens when out of memory). */
10150 s = alist_name(&((aentry_T *)gap->ga_data)[i]);
10151 if (s != NULL)
10152 {
10153 if (fullname)
10154 {
10155 (void)vim_FullName(s, buf, MAXPATHL, FALSE);
10156 s = buf;
10157 }
10158 if (fputs(" ", fd) < 0 || ses_put_fname(fd, s, flagp) == FAIL)
10159 return FAIL;
10160 }
10161 }
10162 return put_eol(fd);
10163}
10164
10165/*
10166 * Write a buffer name to the session file.
10167 * Also ends the line.
10168 * Returns FAIL if writing fails.
10169 */
10170 static int
10171ses_fname(fd, buf, flagp)
10172 FILE *fd;
10173 buf_T *buf;
10174 unsigned *flagp;
10175{
10176 char_u *name;
10177
10178 /* Use the short file name if the current directory is known at the time
10179 * the session file will be sourced. Don't do this for ":mkview", we
10180 * don't know the current directory. */
10181 if (buf->b_sfname != NULL
10182 && flagp == &ssop_flags
10183 && (ssop_flags & (SSOP_CURDIR | SSOP_SESDIR)))
10184 name = buf->b_sfname;
10185 else
10186 name = buf->b_ffname;
10187 if (ses_put_fname(fd, name, flagp) == FAIL || put_eol(fd) == FAIL)
10188 return FAIL;
10189 return OK;
10190}
10191
10192/*
10193 * Write a file name to the session file.
10194 * Takes care of the "slash" option in 'sessionoptions' and escapes special
10195 * characters.
10196 * Returns FAIL if writing fails.
10197 */
10198 static int
10199ses_put_fname(fd, name, flagp)
10200 FILE *fd;
10201 char_u *name;
10202 unsigned *flagp;
10203{
10204 char_u *sname;
10205 int retval = OK;
10206 int c;
10207
10208 sname = home_replace_save(NULL, name);
10209 if (sname != NULL)
10210 name = sname;
10211 while (*name != NUL)
10212 {
10213#ifdef FEAT_MBYTE
10214 {
10215 int l;
10216
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010217 if (has_mbyte && (l = (*mb_ptr2len)(name)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010218 {
10219 /* copy a multibyte char */
10220 while (--l >= 0)
10221 {
10222 if (putc(*name, fd) != *name)
10223 retval = FAIL;
10224 ++name;
10225 }
10226 continue;
10227 }
10228 }
10229#endif
10230 c = *name++;
10231 if (c == '\\' && (*flagp & SSOP_SLASH))
10232 /* change a backslash to a forward slash */
10233 c = '/';
10234 else if ((vim_strchr(escape_chars, c) != NULL
10235#ifdef BACKSLASH_IN_FILENAME
10236 && c != '\\'
10237#endif
10238 ) || c == '#' || c == '%')
10239 {
10240 /* escape a special character with a backslash */
10241 if (putc('\\', fd) != '\\')
10242 retval = FAIL;
10243 }
10244 if (putc(c, fd) != c)
10245 retval = FAIL;
10246 }
10247 vim_free(sname);
10248 return retval;
10249}
10250
10251/*
10252 * ":loadview [nr]"
10253 */
10254 static void
10255ex_loadview(eap)
10256 exarg_T *eap;
10257{
10258 char_u *fname;
10259
10260 fname = get_view_file(*eap->arg);
10261 if (fname != NULL)
10262 {
10263 do_source(fname, FALSE, FALSE);
10264 vim_free(fname);
10265 }
10266}
10267
10268/*
10269 * Get the name of the view file for the current buffer.
10270 */
10271 static char_u *
10272get_view_file(c)
10273 int c;
10274{
10275 int len = 0;
10276 char_u *p, *s;
10277 char_u *retval;
10278 char_u *sname;
10279
10280 if (curbuf->b_ffname == NULL)
10281 {
10282 EMSG(_(e_noname));
10283 return NULL;
10284 }
10285 sname = home_replace_save(NULL, curbuf->b_ffname);
10286 if (sname == NULL)
10287 return NULL;
10288
10289 /*
10290 * We want a file name without separators, because we're not going to make
10291 * a directory.
10292 * "normal" path separator -> "=+"
10293 * "=" -> "=="
10294 * ":" path separator -> "=-"
10295 */
10296 for (p = sname; *p; ++p)
10297 if (*p == '=' || vim_ispathsep(*p))
10298 ++len;
10299 retval = alloc((unsigned)(STRLEN(sname) + len + STRLEN(p_vdir) + 9));
10300 if (retval != NULL)
10301 {
10302 STRCPY(retval, p_vdir);
10303 add_pathsep(retval);
10304 s = retval + STRLEN(retval);
10305 for (p = sname; *p; ++p)
10306 {
10307 if (*p == '=')
10308 {
10309 *s++ = '=';
10310 *s++ = '=';
10311 }
10312 else if (vim_ispathsep(*p))
10313 {
10314 *s++ = '=';
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000010315#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA) || defined(RISCOS) \
Bram Moolenaar071d4272004-06-13 20:20:40 +000010316 || defined(VMS)
10317 if (*p == ':')
10318 *s++ = '-';
10319 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010320#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000010321 *s++ = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010322 }
10323 else
10324 *s++ = *p;
10325 }
10326 *s++ = '=';
10327 *s++ = c;
10328 STRCPY(s, ".vim");
10329 }
10330
10331 vim_free(sname);
10332 return retval;
10333}
10334
10335#endif /* FEAT_SESSION */
10336
10337/*
10338 * Write end-of-line character(s) for ":mkexrc", ":mkvimrc" and ":mksession".
10339 * Return FAIL for a write error.
10340 */
10341 int
10342put_eol(fd)
10343 FILE *fd;
10344{
10345 if (
10346#ifdef USE_CRNL
10347 (
10348# ifdef MKSESSION_NL
10349 !mksession_nl &&
10350# endif
10351 (putc('\r', fd) < 0)) ||
10352#endif
10353 (putc('\n', fd) < 0))
10354 return FAIL;
10355 return OK;
10356}
10357
10358/*
10359 * Write a line to "fd".
10360 * Return FAIL for a write error.
10361 */
10362 int
10363put_line(fd, s)
10364 FILE *fd;
10365 char *s;
10366{
10367 if (fputs(s, fd) < 0 || put_eol(fd) == FAIL)
10368 return FAIL;
10369 return OK;
10370}
10371
10372#ifdef FEAT_VIMINFO
10373/*
10374 * ":rviminfo" and ":wviminfo".
10375 */
10376 static void
10377ex_viminfo(eap)
10378 exarg_T *eap;
10379{
10380 char_u *save_viminfo;
10381
10382 save_viminfo = p_viminfo;
10383 if (*p_viminfo == NUL)
10384 p_viminfo = (char_u *)"'100";
10385 if (eap->cmdidx == CMD_rviminfo)
10386 {
10387 if (read_viminfo(eap->arg, TRUE, TRUE, eap->forceit) == FAIL)
10388 EMSG(_("E195: Cannot open viminfo file for reading"));
10389 }
10390 else
10391 write_viminfo(eap->arg, eap->forceit);
10392 p_viminfo = save_viminfo;
10393}
10394#endif
10395
10396#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +000010397/*
10398 * Make a dialog message in "buff[IOSIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +000010399 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +000010400 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010401 void
10402dialog_msg(buff, format, fname)
10403 char_u *buff;
10404 char *format;
10405 char_u *fname;
10406{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010407 if (fname == NULL)
10408 fname = (char_u *)_("Untitled");
Bram Moolenaarb765d632005-06-07 21:00:02 +000010409 vim_snprintf((char *)buff, IOSIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010410}
10411#endif
10412
10413/*
10414 * ":behave {mswin,xterm}"
10415 */
10416 static void
10417ex_behave(eap)
10418 exarg_T *eap;
10419{
10420 if (STRCMP(eap->arg, "mswin") == 0)
10421 {
10422 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
10423 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
10424 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
10425 set_option_value((char_u *)"keymodel", 0L,
10426 (char_u *)"startsel,stopsel", 0);
10427 }
10428 else if (STRCMP(eap->arg, "xterm") == 0)
10429 {
10430 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
10431 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
10432 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
10433 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
10434 }
10435 else
10436 EMSG2(_(e_invarg2), eap->arg);
10437}
10438
10439#ifdef FEAT_AUTOCMD
10440static int filetype_detect = FALSE;
10441static int filetype_plugin = FALSE;
10442static int filetype_indent = FALSE;
10443
10444/*
10445 * ":filetype [plugin] [indent] {on,off,detect}"
10446 * on: Load the filetype.vim file to install autocommands for file types.
10447 * off: Load the ftoff.vim file to remove all autocommands for file types.
10448 * plugin on: load filetype.vim and ftplugin.vim
10449 * plugin off: load ftplugof.vim
10450 * indent on: load filetype.vim and indent.vim
10451 * indent off: load indoff.vim
10452 */
10453 static void
10454ex_filetype(eap)
10455 exarg_T *eap;
10456{
10457 char_u *arg = eap->arg;
10458 int plugin = FALSE;
10459 int indent = FALSE;
10460
10461 if (*eap->arg == NUL)
10462 {
10463 /* Print current status. */
10464 smsg((char_u *)"filetype detection:%s plugin:%s indent:%s",
10465 filetype_detect ? "ON" : "OFF",
10466 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
10467 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
10468 return;
10469 }
10470
10471 /* Accept "plugin" and "indent" in any order. */
10472 for (;;)
10473 {
10474 if (STRNCMP(arg, "plugin", 6) == 0)
10475 {
10476 plugin = TRUE;
10477 arg = skipwhite(arg + 6);
10478 continue;
10479 }
10480 if (STRNCMP(arg, "indent", 6) == 0)
10481 {
10482 indent = TRUE;
10483 arg = skipwhite(arg + 6);
10484 continue;
10485 }
10486 break;
10487 }
10488 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
10489 {
10490 if (*arg == 'o' || !filetype_detect)
10491 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000010492 source_runtime((char_u *)FILETYPE_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010493 filetype_detect = TRUE;
10494 if (plugin)
10495 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000010496 source_runtime((char_u *)FTPLUGIN_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010497 filetype_plugin = TRUE;
10498 }
10499 if (indent)
10500 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000010501 source_runtime((char_u *)INDENT_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010502 filetype_indent = TRUE;
10503 }
10504 }
10505 if (*arg == 'd')
10506 {
10507 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +000010508 do_modelines(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010509 }
10510 }
10511 else if (STRCMP(arg, "off") == 0)
10512 {
10513 if (plugin || indent)
10514 {
10515 if (plugin)
10516 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000010517 source_runtime((char_u *)FTPLUGOF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010518 filetype_plugin = FALSE;
10519 }
10520 if (indent)
10521 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000010522 source_runtime((char_u *)INDOFF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010523 filetype_indent = FALSE;
10524 }
10525 }
10526 else
10527 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000010528 source_runtime((char_u *)FTOFF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010529 filetype_detect = FALSE;
10530 }
10531 }
10532 else
10533 EMSG2(_(e_invarg2), arg);
10534}
10535
10536/*
10537 * ":setfiletype {name}"
10538 */
10539 static void
10540ex_setfiletype(eap)
10541 exarg_T *eap;
10542{
10543 if (!did_filetype)
10544 set_option_value((char_u *)"filetype", 0L, eap->arg, OPT_LOCAL);
10545}
10546#endif
10547
10548/*ARGSUSED*/
10549 static void
10550ex_digraphs(eap)
10551 exarg_T *eap;
10552{
10553#ifdef FEAT_DIGRAPHS
10554 if (*eap->arg != NUL)
10555 putdigraph(eap->arg);
10556 else
10557 listdigraphs();
10558#else
10559 EMSG(_("E196: No digraphs in this version"));
10560#endif
10561}
10562
10563 static void
10564ex_set(eap)
10565 exarg_T *eap;
10566{
10567 int flags = 0;
10568
10569 if (eap->cmdidx == CMD_setlocal)
10570 flags = OPT_LOCAL;
10571 else if (eap->cmdidx == CMD_setglobal)
10572 flags = OPT_GLOBAL;
10573#if defined(FEAT_EVAL) && defined(FEAT_AUTOCMD) && defined(FEAT_BROWSE)
10574 if (cmdmod.browse && flags == 0)
10575 ex_options(eap);
10576 else
10577#endif
10578 (void)do_set(eap->arg, flags);
10579}
10580
10581#ifdef FEAT_SEARCH_EXTRA
10582/*
10583 * ":nohlsearch"
10584 */
10585/*ARGSUSED*/
10586 static void
10587ex_nohlsearch(eap)
10588 exarg_T *eap;
10589{
10590 no_hlsearch = TRUE;
10591 redraw_all_later(NOT_VALID);
10592}
10593
10594/*
Bram Moolenaare1438bb2006-03-01 22:01:55 +000010595 * ":[N]match {group} {pattern}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010596 * Sets nextcmd to the start of the next command, if any. Also called when
10597 * skipping commands to find the next command.
10598 */
10599 static void
10600ex_match(eap)
10601 exarg_T *eap;
10602{
10603 char_u *p;
10604 char_u *end;
10605 int c;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000010606 int mi;
10607
10608 if (eap->line2 <= 3)
10609 mi = eap->line2 - 1;
10610 else
10611 {
10612 EMSG(e_invcmd);
10613 return;
10614 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010615
10616 /* First clear any old pattern. */
10617 if (!eap->skip)
10618 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +000010619 vim_free(curwin->w_match[mi].regprog);
10620 curwin->w_match[mi].regprog = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010621 redraw_later(NOT_VALID); /* always need a redraw */
10622 }
10623
10624 if (ends_excmd(*eap->arg))
10625 end = eap->arg;
10626 else if ((STRNICMP(eap->arg, "none", 4) == 0
10627 && (vim_iswhite(eap->arg[4]) || ends_excmd(eap->arg[4]))))
10628 end = eap->arg + 4;
10629 else
10630 {
10631 p = skiptowhite(eap->arg);
10632 if (!eap->skip)
10633 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +000010634 curwin->w_match_id[mi] = syn_namen2id(eap->arg,
10635 (int)(p - eap->arg));
10636 if (curwin->w_match_id[mi] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010637 {
10638 EMSG2(_(e_nogroup), eap->arg);
10639 return;
10640 }
10641 }
10642 p = skipwhite(p);
10643 if (*p == NUL)
10644 {
10645 /* There must be two arguments. */
10646 EMSG2(_(e_invarg2), eap->arg);
10647 return;
10648 }
10649 end = skip_regexp(p + 1, *p, TRUE, NULL);
10650 if (!eap->skip)
10651 {
10652 if (*end != NUL && !ends_excmd(*skipwhite(end + 1)))
10653 {
10654 eap->errmsg = e_trailing;
10655 return;
10656 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000010657 if (*end != *p)
10658 {
10659 EMSG2(_(e_invarg2), p);
10660 return;
10661 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010662
10663 c = *end;
10664 *end = NUL;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000010665 curwin->w_match[mi].regprog = vim_regcomp(p + 1, RE_MAGIC);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010666 *end = c;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000010667 if (curwin->w_match[mi].regprog == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010668 {
10669 EMSG2(_(e_invarg2), p);
10670 return;
10671 }
10672 }
10673 }
10674 eap->nextcmd = find_nextcmd(end);
10675}
10676#endif
10677
10678#ifdef FEAT_CRYPT
10679/*
10680 * ":X": Get crypt key
10681 */
10682/*ARGSUSED*/
10683 static void
10684ex_X(eap)
10685 exarg_T *eap;
10686{
10687 (void)get_crypt_key(TRUE, TRUE);
10688}
10689#endif
10690
10691#ifdef FEAT_FOLDING
10692 static void
10693ex_fold(eap)
10694 exarg_T *eap;
10695{
10696 if (foldManualAllowed(TRUE))
10697 foldCreate(eap->line1, eap->line2);
10698}
10699
10700 static void
10701ex_foldopen(eap)
10702 exarg_T *eap;
10703{
10704 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
10705 eap->forceit, FALSE);
10706}
10707
10708 static void
10709ex_folddo(eap)
10710 exarg_T *eap;
10711{
10712 linenr_T lnum;
10713
10714 /* First set the marks for all lines closed/open. */
10715 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
10716 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
10717 ml_setmarked(lnum);
10718
10719 /* Execute the command on the marked lines. */
10720 global_exe(eap->arg);
10721 ml_clearmarked(); /* clear rest of the marks */
10722}
10723#endif