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