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