blob: 97cb91c414a7226cc8c322dff6f8ea33f549753f [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,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001828 (char_u *)"all", OPT_FREE, SID_NONE);
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001829 }
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". */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002652 set_string_option_direct((char_u *)"ei", -1, cmdmod.save_ei,
2653 OPT_FREE, SID_NONE);
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00002654 free_string_option(cmdmod.save_ei);
2655 }
2656#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657
2658 cmdmod = save_cmdmod;
2659
2660 if (did_silent > 0)
2661 {
2662 /* messages could be enabled for a serious error, need to check if the
2663 * counters don't become negative */
2664 msg_silent -= did_silent;
2665 if (msg_silent < 0)
2666 msg_silent = 0;
2667 emsg_silent -= did_esilent;
2668 if (emsg_silent < 0)
2669 emsg_silent = 0;
2670 /* Restore msg_scroll, it's set by file I/O commands, even when no
2671 * message is actually displayed. */
2672 msg_scroll = save_msg_scroll;
2673 }
2674
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002675#ifdef HAVE_SANDBOX
2676 if (did_sandbox)
2677 --sandbox;
2678#endif
2679
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680 if (ea.nextcmd && *ea.nextcmd == NUL) /* not really a next command */
2681 ea.nextcmd = NULL;
2682
2683#ifdef FEAT_EVAL
2684 --ex_nesting_level;
2685#endif
2686
2687 return ea.nextcmd;
2688}
2689#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00002690 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691#endif
2692
2693/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002694 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695 * If there is a match advance "pp" to the argument and return TRUE.
2696 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002697 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698checkforcmd(pp, cmd, len)
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002699 char_u **pp; /* start of command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700 char *cmd; /* name of command */
2701 int len; /* required length */
2702{
2703 int i;
2704
2705 for (i = 0; cmd[i] != NUL; ++i)
2706 if (cmd[i] != (*pp)[i])
2707 break;
2708 if (i >= len && !isalpha((*pp)[i]))
2709 {
2710 *pp = skipwhite(*pp + i);
2711 return TRUE;
2712 }
2713 return FALSE;
2714}
2715
2716/*
2717 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002718 * Start of the name can be found at eap->cmd.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002719 * Returns pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002720 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002721 * Returns NULL for an ambiguous user command.
2722 */
2723/*ARGSUSED*/
2724 static char_u *
2725find_command(eap, full)
2726 exarg_T *eap;
2727 int *full;
2728{
2729 int len;
2730 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00002731 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002732
2733 /*
2734 * Isolate the command and search for it in the command table.
2735 * Exeptions:
2736 * - the 'k' command can directly be followed by any character.
2737 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
2738 * but :sre[wind] is another command, as are :scrip[tnames],
2739 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00002740 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002741 */
2742 p = eap->cmd;
2743 if (*p == 'k')
2744 {
2745 eap->cmdidx = CMD_k;
2746 ++p;
2747 }
2748 else if (p[0] == 's'
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002749 && ((p[1] == 'c' && p[2] != 's' && p[2] != 'r'
2750 && p[3] != 'i' && p[4] != 'p')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751 || p[1] == 'g'
2752 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
2753 || p[1] == 'I'
2754 || (p[1] == 'r' && p[2] != 'e')))
2755 {
2756 eap->cmdidx = CMD_substitute;
2757 ++p;
2758 }
2759 else
2760 {
2761 while (ASCII_ISALPHA(*p))
2762 ++p;
2763 /* check for non-alpha command */
2764 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
2765 ++p;
2766 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00002767 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
2768 {
2769 /* Check for ":dl", ":dell", etc. to ":deletel": that's
2770 * :delete with the 'l' flag. Same for 'p'. */
2771 for (i = 0; i < len; ++i)
2772 if (eap->cmd[i] != "delete"[i])
2773 break;
2774 if (i == len - 1)
2775 {
2776 --len;
2777 if (p[-1] == 'l')
2778 eap->flags |= EXFLAG_LIST;
2779 else
2780 eap->flags |= EXFLAG_PRINT;
2781 }
2782 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002783
2784 if (ASCII_ISLOWER(*eap->cmd))
2785 eap->cmdidx = cmdidxs[CharOrdLow(*eap->cmd)];
2786 else
2787 eap->cmdidx = cmdidxs[26];
2788
2789 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
2790 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
2791 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
2792 (size_t)len) == 0)
2793 {
2794#ifdef FEAT_EVAL
2795 if (full != NULL
2796 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
2797 *full = TRUE;
2798#endif
2799 break;
2800 }
2801
2802#ifdef FEAT_USR_CMDS
2803 /* Look for a user defined command as a last resort */
2804 if (eap->cmdidx == CMD_SIZE && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
2805 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002806 /* User defined commands may contain digits. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002807 while (ASCII_ISALNUM(*p))
2808 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002809 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002810 }
2811#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002812 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002813 eap->cmdidx = CMD_SIZE;
2814 }
2815
2816 return p;
2817}
2818
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002819#ifdef FEAT_USR_CMDS
2820/*
2821 * Search for a user command that matches "eap->cmd".
2822 * Return cmdidx in "eap->cmdidx", flags in "eap->argt", idx in "eap->useridx".
2823 * Return a pointer to just after the command.
2824 * Return NULL if there is no matching command.
2825 */
2826 static char_u *
2827find_ucmd(eap, p, full, xp, compl)
2828 exarg_T *eap;
2829 char_u *p; /* end of the command (possibly including count) */
2830 int *full; /* set to TRUE for a full match */
2831 expand_T *xp; /* used for completion, NULL otherwise */
2832 int *compl; /* completion flags or NULL */
2833{
2834 int len = (int)(p - eap->cmd);
2835 int j, k, matchlen = 0;
2836 ucmd_T *uc;
2837 int found = FALSE;
2838 int possible = FALSE;
2839 char_u *cp, *np; /* Point into typed cmd and test name */
2840 garray_T *gap;
2841 int amb_local = FALSE; /* Found ambiguous buffer-local command,
2842 only full match global is accepted. */
2843
2844 /*
2845 * Look for buffer-local user commands first, then global ones.
2846 */
2847 gap = &curbuf->b_ucmds;
2848 for (;;)
2849 {
2850 for (j = 0; j < gap->ga_len; ++j)
2851 {
2852 uc = USER_CMD_GA(gap, j);
2853 cp = eap->cmd;
2854 np = uc->uc_name;
2855 k = 0;
2856 while (k < len && *np != NUL && *cp++ == *np++)
2857 k++;
2858 if (k == len || (*np == NUL && vim_isdigit(eap->cmd[k])))
2859 {
2860 /* If finding a second match, the command is ambiguous. But
2861 * not if a buffer-local command wasn't a full match and a
2862 * global command is a full match. */
2863 if (k == len && found && *np != NUL)
2864 {
2865 if (gap == &ucmds)
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002866 return NULL;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002867 amb_local = TRUE;
2868 }
2869
2870 if (!found || (k == len && *np == NUL))
2871 {
2872 /* If we matched up to a digit, then there could
2873 * be another command including the digit that we
2874 * should use instead.
2875 */
2876 if (k == len)
2877 found = TRUE;
2878 else
2879 possible = TRUE;
2880
2881 if (gap == &ucmds)
2882 eap->cmdidx = CMD_USER;
2883 else
2884 eap->cmdidx = CMD_USER_BUF;
2885 eap->argt = uc->uc_argt;
2886 eap->useridx = j;
2887
2888# ifdef FEAT_CMDL_COMPL
2889 if (compl != NULL)
2890 *compl = uc->uc_compl;
2891# ifdef FEAT_EVAL
2892 if (xp != NULL)
2893 {
2894 xp->xp_arg = uc->uc_compl_arg;
2895 xp->xp_scriptID = uc->uc_scriptID;
2896 }
2897# endif
2898# endif
2899 /* Do not search for further abbreviations
2900 * if this is an exact match. */
2901 matchlen = k;
2902 if (k == len && *np == NUL)
2903 {
2904 if (full != NULL)
2905 *full = TRUE;
2906 amb_local = FALSE;
2907 break;
2908 }
2909 }
2910 }
2911 }
2912
2913 /* Stop if we found a full match or searched all. */
2914 if (j < gap->ga_len || gap == &ucmds)
2915 break;
2916 gap = &ucmds;
2917 }
2918
2919 /* Only found ambiguous matches. */
2920 if (amb_local)
2921 {
2922 if (xp != NULL)
2923 xp->xp_context = EXPAND_UNSUCCESSFUL;
2924 return NULL;
2925 }
2926
2927 /* The match we found may be followed immediately by a number. Move "p"
2928 * back to point to it. */
2929 if (found || possible)
2930 return p + (matchlen - len);
2931 return p;
2932}
2933#endif
2934
Bram Moolenaar071d4272004-06-13 20:20:40 +00002935#if defined(FEAT_EVAL) || defined(PROTO)
2936/*
2937 * Return > 0 if an Ex command "name" exists.
2938 * Return 2 if there is an exact match.
2939 * Return 3 if there is an ambiguous match.
2940 */
2941 int
2942cmd_exists(name)
2943 char_u *name;
2944{
2945 exarg_T ea;
2946 int full = FALSE;
2947 int i;
2948 int j;
2949 static struct cmdmod
2950 {
2951 char *name;
2952 int minlen;
2953 } cmdmods[] = {
2954 {"aboveleft", 3},
2955 {"belowright", 3},
2956 {"botright", 2},
2957 {"browse", 3},
2958 {"confirm", 4},
2959 {"hide", 3},
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002960 {"keepalt", 5},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002961 {"keepjumps", 5},
2962 {"keepmarks", 3},
2963 {"leftabove", 5},
2964 {"lockmarks", 3},
2965 {"rightbelow", 6},
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002966 {"sandbox", 3},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002967 {"silent", 3},
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002968 {"tab", 3},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002969 {"topleft", 2},
2970 {"verbose", 4},
2971 {"vertical", 4},
2972 };
2973
2974 /* Check command modifiers. */
2975 for (i = 0; i < sizeof(cmdmods) / sizeof(struct cmdmod); ++i)
2976 {
2977 for (j = 0; name[j] != NUL; ++j)
2978 if (name[j] != cmdmods[i].name[j])
2979 break;
2980 if (name[j] == NUL && j >= cmdmods[i].minlen)
2981 return (cmdmods[i].name[j] == NUL ? 2 : 1);
2982 }
2983
2984 /* Check built-in commands and user defined commands. */
2985 ea.cmd = name;
2986 ea.cmdidx = (cmdidx_T)0;
2987 if (find_command(&ea, &full) == NULL)
2988 return 3;
2989 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
2990}
2991#endif
2992
2993/*
2994 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
2995 * we don't need/want deleted. Maybe this could be done better if we didn't
2996 * repeat all this stuff. The only problem is that they may not stay
2997 * perfectly compatible with each other, but then the command line syntax
2998 * probably won't change that much -- webb.
2999 */
3000 char_u *
3001set_one_cmd_context(xp, buff)
3002 expand_T *xp;
3003 char_u *buff; /* buffer for command string */
3004{
3005 char_u *p;
3006 char_u *cmd, *arg;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003007 int len = 0;
3008 exarg_T ea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003009#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3010 int compl = EXPAND_NOTHING;
3011#endif
3012#ifdef FEAT_CMDL_COMPL
3013 int delim;
3014#endif
3015 int forceit = FALSE;
3016 int usefilter = FALSE; /* filter instead of file name */
3017
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003018 ExpandInit(xp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003019 xp->xp_pattern = buff;
3020 xp->xp_context = EXPAND_COMMANDS; /* Default until we get past command */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003021 ea.argt = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003022
3023/*
3024 * 2. skip comment lines and leading space, colons or bars
3025 */
3026 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
3027 ;
3028 xp->xp_pattern = cmd;
3029
3030 if (*cmd == NUL)
3031 return NULL;
3032 if (*cmd == '"') /* ignore comment lines */
3033 {
3034 xp->xp_context = EXPAND_NOTHING;
3035 return NULL;
3036 }
3037
3038/*
3039 * 3. parse a range specifier of the form: addr [,addr] [;addr] ..
3040 */
3041 cmd = skip_range(cmd, &xp->xp_context);
3042
3043/*
3044 * 4. parse command
3045 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003046 xp->xp_pattern = cmd;
3047 if (*cmd == NUL)
3048 return NULL;
3049 if (*cmd == '"')
3050 {
3051 xp->xp_context = EXPAND_NOTHING;
3052 return NULL;
3053 }
3054
3055 if (*cmd == '|' || *cmd == '\n')
3056 return cmd + 1; /* There's another command */
3057
3058 /*
3059 * Isolate the command and search for it in the command table.
3060 * Exceptions:
3061 * - the 'k' command can directly be followed by any character, but
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003062 * do accept "keepmarks", "keepalt" and "keepjumps".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003063 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
3064 */
3065 if (*cmd == 'k' && cmd[1] != 'e')
3066 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003067 ea.cmdidx = CMD_k;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003068 p = cmd + 1;
3069 }
3070 else
3071 {
3072 p = cmd;
3073 while (ASCII_ISALPHA(*p) || *p == '*') /* Allow * wild card */
3074 ++p;
3075 /* check for non-alpha command */
3076 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3077 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003078 len = (int)(p - cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003079
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003080 if (len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003081 {
3082 xp->xp_context = EXPAND_UNSUCCESSFUL;
3083 return NULL;
3084 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003085 for (ea.cmdidx = (cmdidx_T)0; (int)ea.cmdidx < (int)CMD_SIZE;
3086 ea.cmdidx = (cmdidx_T)((int)ea.cmdidx + 1))
3087 if (STRNCMP(cmdnames[(int)ea.cmdidx].cmd_name, cmd, (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003088 break;
3089
3090#ifdef FEAT_USR_CMDS
3091 if (cmd[0] >= 'A' && cmd[0] <= 'Z')
3092 {
3093 while (ASCII_ISALNUM(*p) || *p == '*') /* Allow * wild card */
3094 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003095 len = (int)(p - cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003096 }
3097#endif
3098 }
3099
3100 /*
3101 * If the cursor is touching the command, and it ends in an alpha-numeric
3102 * character, complete the command name.
3103 */
3104 if (*p == NUL && ASCII_ISALNUM(p[-1]))
3105 return NULL;
3106
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003107 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003108 {
3109 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
3110 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003111 ea.cmdidx = CMD_substitute;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003112 p = cmd + 1;
3113 }
3114#ifdef FEAT_USR_CMDS
3115 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
3116 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003117 ea.cmd = cmd;
3118 p = find_ucmd(&ea, p, NULL, xp,
3119# if defined(FEAT_CMDL_COMPL)
3120 &compl
3121# else
3122 NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003123# endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003124 );
Bram Moolenaarebefac62005-12-28 22:39:57 +00003125 if (p == NULL)
3126 ea.cmdidx = CMD_SIZE; /* ambiguous user command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127 }
3128#endif
3129 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003130 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003131 {
3132 /* Not still touching the command and it was an illegal one */
3133 xp->xp_context = EXPAND_UNSUCCESSFUL;
3134 return NULL;
3135 }
3136
3137 xp->xp_context = EXPAND_NOTHING; /* Default now that we're past command */
3138
3139 if (*p == '!') /* forced commands */
3140 {
3141 forceit = TRUE;
3142 ++p;
3143 }
3144
3145/*
3146 * 5. parse arguments
3147 */
3148#ifdef FEAT_USR_CMDS
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003149 if (!USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003151 ea.argt = cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152
3153 arg = skipwhite(p);
3154
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003155 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156 {
3157 if (*arg == '>') /* append */
3158 {
3159 if (*++arg == '>')
3160 ++arg;
3161 arg = skipwhite(arg);
3162 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003163 else if (*arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003164 {
3165 ++arg;
3166 usefilter = TRUE;
3167 }
3168 }
3169
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003170 if (ea.cmdidx == CMD_read)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003171 {
3172 usefilter = forceit; /* :r! filter if forced */
3173 if (*arg == '!') /* :r !filter */
3174 {
3175 ++arg;
3176 usefilter = TRUE;
3177 }
3178 }
3179
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003180 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003181 {
3182 while (*arg == *cmd) /* allow any number of '>' or '<' */
3183 ++arg;
3184 arg = skipwhite(arg);
3185 }
3186
3187 /* Does command allow "+command"? */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003188 if ((ea.argt & EDITCMD) && !usefilter && *arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003189 {
3190 /* Check if we're in the +command */
3191 p = arg + 1;
3192 arg = skip_cmd_arg(arg, FALSE);
3193
3194 /* Still touching the command after '+'? */
3195 if (*arg == NUL)
3196 return p;
3197
3198 /* Skip space(s) after +command to get to the real argument */
3199 arg = skipwhite(arg);
3200 }
3201
3202 /*
3203 * Check for '|' to separate commands and '"' to start comments.
3204 * Don't do this for ":read !cmd" and ":write !cmd".
3205 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003206 if ((ea.argt & TRLBAR) && !usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003207 {
3208 p = arg;
3209 /* ":redir @" is not the start of a comment */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003210 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211 p += 2;
3212 while (*p)
3213 {
3214 if (*p == Ctrl_V)
3215 {
3216 if (p[1] != NUL)
3217 ++p;
3218 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003219 else if ( (*p == '"' && !(ea.argt & NOTRLCOM))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003220 || *p == '|' || *p == '\n')
3221 {
3222 if (*(p - 1) != '\\')
3223 {
3224 if (*p == '|' || *p == '\n')
3225 return p + 1;
3226 return NULL; /* It's a comment */
3227 }
3228 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003229 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230 }
3231 }
3232
3233 /* no arguments allowed */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003234 if (!(ea.argt & EXTRA) && *arg != NUL &&
Bram Moolenaar071d4272004-06-13 20:20:40 +00003235 vim_strchr((char_u *)"|\"", *arg) == NULL)
3236 return NULL;
3237
3238 /* Find start of last argument (argument just before cursor): */
3239 p = buff + STRLEN(buff);
3240 while (p != arg && *p != ' ' && *p != TAB)
3241 p--;
3242 if (*p == ' ' || *p == TAB)
3243 p++;
3244 xp->xp_pattern = p;
3245
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003246 if (ea.argt & XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003247 {
3248 int in_quote = FALSE;
3249 char_u *bow = NULL; /* Beginning of word */
3250
3251 /*
3252 * Allow spaces within back-quotes to count as part of the argument
3253 * being expanded.
3254 */
3255 xp->xp_pattern = skipwhite(arg);
3256 for (p = xp->xp_pattern; *p; )
3257 {
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003258 if (*p == '\\' && p[1] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003259 ++p;
3260#ifdef SPACE_IN_FILENAME
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003261 else if (vim_iswhite(*p) && (!(ea.argt & NOSPC) || usefilter))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003262#else
3263 else if (vim_iswhite(*p))
3264#endif
3265 {
3266 p = skipwhite(p);
3267 if (in_quote)
3268 bow = p;
3269 else
3270 xp->xp_pattern = p;
3271 --p;
3272 }
3273 else if (*p == '`')
3274 {
3275 if (!in_quote)
3276 {
3277 xp->xp_pattern = p;
3278 bow = p + 1;
3279 }
3280 in_quote = !in_quote;
3281 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003282 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283 }
3284
3285 /*
3286 * If we are still inside the quotes, and we passed a space, just
3287 * expand from there.
3288 */
3289 if (bow != NULL && in_quote)
3290 xp->xp_pattern = bow;
3291 xp->xp_context = EXPAND_FILES;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003292#ifndef BACKSLASH_IN_FILENAME
3293 /* For a shell command more chars need to be escaped. */
3294 if (usefilter || ea.cmdidx == CMD_bang)
3295 xp->xp_shell = TRUE;
3296#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297
3298 /* Check for environment variable */
3299 if (*xp->xp_pattern == '$'
3300#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
3301 || *xp->xp_pattern == '%'
3302#endif
3303 )
3304 {
3305 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
3306 if (!vim_isIDc(*p))
3307 break;
3308 if (*p == NUL)
3309 {
3310 xp->xp_context = EXPAND_ENV_VARS;
3311 ++xp->xp_pattern;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003312#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3313 /* Avoid that the assignment uses EXPAND_FILES again. */
Bram Moolenaara466c992005-07-09 21:03:22 +00003314 if (compl != EXPAND_USER_DEFINED && compl != EXPAND_USER_LIST)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003315 compl = EXPAND_ENV_VARS;
3316#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317 }
3318 }
3319 }
3320
3321/*
3322 * 6. switch on command name
3323 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003324 switch (ea.cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325 {
3326 case CMD_cd:
3327 case CMD_chdir:
3328 case CMD_lcd:
3329 case CMD_lchdir:
3330 if (xp->xp_context == EXPAND_FILES)
3331 xp->xp_context = EXPAND_DIRECTORIES;
3332 break;
3333 case CMD_help:
3334 xp->xp_context = EXPAND_HELP;
3335 xp->xp_pattern = arg;
3336 break;
3337
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003338 /* Command modifiers: return the argument.
3339 * Also for commands with an argument that is a command. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340 case CMD_aboveleft:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003341 case CMD_argdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342 case CMD_belowright:
3343 case CMD_botright:
3344 case CMD_browse:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003345 case CMD_bufdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346 case CMD_confirm:
Bram Moolenaardf177f62005-02-22 08:39:57 +00003347 case CMD_debug:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348 case CMD_folddoclosed:
3349 case CMD_folddoopen:
3350 case CMD_hide:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003351 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352 case CMD_keepjumps:
3353 case CMD_keepmarks:
3354 case CMD_leftabove:
3355 case CMD_lockmarks:
3356 case CMD_rightbelow:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003357 case CMD_sandbox:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358 case CMD_silent:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003359 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360 case CMD_topleft:
3361 case CMD_verbose:
3362 case CMD_vertical:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003363 case CMD_windo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364 return arg;
3365
3366#ifdef FEAT_SEARCH_EXTRA
3367 case CMD_match:
3368 if (*arg == NUL || !ends_excmd(*arg))
3369 {
3370 /* Dummy call to clear variables. */
3371 set_context_in_highlight_cmd(xp, (char_u *)"link n");
3372 xp->xp_context = EXPAND_HIGHLIGHT;
3373 xp->xp_pattern = arg;
3374 arg = skipwhite(skiptowhite(arg));
3375 if (*arg != NUL)
3376 {
3377 xp->xp_context = EXPAND_NOTHING;
3378 arg = skip_regexp(arg + 1, *arg, p_magic, NULL);
3379 }
3380 }
3381 return find_nextcmd(arg);
3382#endif
3383
3384#ifdef FEAT_CMDL_COMPL
3385/*
3386 * All completion for the +cmdline_compl feature goes here.
3387 */
3388
3389# ifdef FEAT_USR_CMDS
3390 case CMD_command:
3391 /* Check for attributes */
3392 while (*arg == '-')
3393 {
3394 arg++; /* Skip "-" */
3395 p = skiptowhite(arg);
3396 if (*p == NUL)
3397 {
3398 /* Cursor is still in the attribute */
3399 p = vim_strchr(arg, '=');
3400 if (p == NULL)
3401 {
3402 /* No "=", so complete attribute names */
3403 xp->xp_context = EXPAND_USER_CMD_FLAGS;
3404 xp->xp_pattern = arg;
3405 return NULL;
3406 }
3407
3408 /* For the -complete and -nargs attributes, we complete
3409 * their arguments as well.
3410 */
3411 if (STRNICMP(arg, "complete", p - arg) == 0)
3412 {
3413 xp->xp_context = EXPAND_USER_COMPLETE;
3414 xp->xp_pattern = p + 1;
3415 return NULL;
3416 }
3417 else if (STRNICMP(arg, "nargs", p - arg) == 0)
3418 {
3419 xp->xp_context = EXPAND_USER_NARGS;
3420 xp->xp_pattern = p + 1;
3421 return NULL;
3422 }
3423 return NULL;
3424 }
3425 arg = skipwhite(p);
3426 }
3427
3428 /* After the attributes comes the new command name */
3429 p = skiptowhite(arg);
3430 if (*p == NUL)
3431 {
3432 xp->xp_context = EXPAND_USER_COMMANDS;
3433 xp->xp_pattern = arg;
3434 break;
3435 }
3436
3437 /* And finally comes a normal command */
3438 return skipwhite(p);
3439
3440 case CMD_delcommand:
3441 xp->xp_context = EXPAND_USER_COMMANDS;
3442 xp->xp_pattern = arg;
3443 break;
3444# endif
3445
3446 case CMD_global:
3447 case CMD_vglobal:
3448 delim = *arg; /* get the delimiter */
3449 if (delim)
3450 ++arg; /* skip delimiter if there is one */
3451
3452 while (arg[0] != NUL && arg[0] != delim)
3453 {
3454 if (arg[0] == '\\' && arg[1] != NUL)
3455 ++arg;
3456 ++arg;
3457 }
3458 if (arg[0] != NUL)
3459 return arg + 1;
3460 break;
3461 case CMD_and:
3462 case CMD_substitute:
3463 delim = *arg;
3464 if (delim)
3465 {
3466 /* skip "from" part */
3467 ++arg;
3468 arg = skip_regexp(arg, delim, p_magic, NULL);
3469 }
3470 /* skip "to" part */
3471 while (arg[0] != NUL && arg[0] != delim)
3472 {
3473 if (arg[0] == '\\' && arg[1] != NUL)
3474 ++arg;
3475 ++arg;
3476 }
3477 if (arg[0] != NUL) /* skip delimiter */
3478 ++arg;
3479 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
3480 ++arg;
3481 if (arg[0] != NUL)
3482 return arg;
3483 break;
3484 case CMD_isearch:
3485 case CMD_dsearch:
3486 case CMD_ilist:
3487 case CMD_dlist:
3488 case CMD_ijump:
3489 case CMD_psearch:
3490 case CMD_djump:
3491 case CMD_isplit:
3492 case CMD_dsplit:
3493 arg = skipwhite(skipdigits(arg)); /* skip count */
3494 if (*arg == '/') /* Match regexp, not just whole words */
3495 {
3496 for (++arg; *arg && *arg != '/'; arg++)
3497 if (*arg == '\\' && arg[1] != NUL)
3498 arg++;
3499 if (*arg)
3500 {
3501 arg = skipwhite(arg + 1);
3502
3503 /* Check for trailing illegal characters */
3504 if (*arg && vim_strchr((char_u *)"|\"\n", *arg) == NULL)
3505 xp->xp_context = EXPAND_NOTHING;
3506 else
3507 return arg;
3508 }
3509 }
3510 break;
3511#ifdef FEAT_AUTOCMD
3512 case CMD_autocmd:
3513 return set_context_in_autocmd(xp, arg, FALSE);
3514
3515 case CMD_doautocmd:
3516 return set_context_in_autocmd(xp, arg, TRUE);
3517#endif
3518 case CMD_set:
3519 set_context_in_set_cmd(xp, arg, 0);
3520 break;
3521 case CMD_setglobal:
3522 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
3523 break;
3524 case CMD_setlocal:
3525 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
3526 break;
3527 case CMD_tag:
3528 case CMD_stag:
3529 case CMD_ptag:
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00003530 case CMD_ltag:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531 case CMD_tselect:
3532 case CMD_stselect:
3533 case CMD_ptselect:
3534 case CMD_tjump:
3535 case CMD_stjump:
3536 case CMD_ptjump:
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003537 if (*p_wop != NUL)
3538 xp->xp_context = EXPAND_TAGS_LISTFILES;
3539 else
3540 xp->xp_context = EXPAND_TAGS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541 xp->xp_pattern = arg;
3542 break;
3543 case CMD_augroup:
3544 xp->xp_context = EXPAND_AUGROUP;
3545 xp->xp_pattern = arg;
3546 break;
3547#ifdef FEAT_SYN_HL
3548 case CMD_syntax:
3549 set_context_in_syntax_cmd(xp, arg);
3550 break;
3551#endif
3552#ifdef FEAT_EVAL
3553 case CMD_let:
3554 case CMD_if:
3555 case CMD_elseif:
3556 case CMD_while:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00003557 case CMD_for:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558 case CMD_echo:
3559 case CMD_echon:
3560 case CMD_execute:
3561 case CMD_echomsg:
3562 case CMD_echoerr:
3563 case CMD_call:
3564 case CMD_return:
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003565 set_context_for_expression(xp, arg, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566 break;
3567
3568 case CMD_unlet:
3569 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3570 arg = xp->xp_pattern + 1;
3571 xp->xp_context = EXPAND_USER_VARS;
3572 xp->xp_pattern = arg;
3573 break;
3574
3575 case CMD_function:
3576 case CMD_delfunction:
3577 xp->xp_context = EXPAND_USER_FUNC;
3578 xp->xp_pattern = arg;
3579 break;
3580
3581 case CMD_echohl:
3582 xp->xp_context = EXPAND_HIGHLIGHT;
3583 xp->xp_pattern = arg;
3584 break;
3585#endif
3586 case CMD_highlight:
3587 set_context_in_highlight_cmd(xp, arg);
3588 break;
3589#ifdef FEAT_LISTCMDS
3590 case CMD_bdelete:
3591 case CMD_bwipeout:
3592 case CMD_bunload:
3593 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3594 arg = xp->xp_pattern + 1;
3595 /*FALLTHROUGH*/
3596 case CMD_buffer:
3597 case CMD_sbuffer:
3598 case CMD_checktime:
3599 xp->xp_context = EXPAND_BUFFERS;
3600 xp->xp_pattern = arg;
3601 break;
3602#endif
3603#ifdef FEAT_USR_CMDS
3604 case CMD_USER:
3605 case CMD_USER_BUF:
3606 if (compl != EXPAND_NOTHING)
3607 {
3608 /* XFILE: file names are handled above */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003609 if (!(ea.argt & XFILE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610 {
3611# ifdef FEAT_MENU
3612 if (compl == EXPAND_MENUS)
3613 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
3614# endif
3615 if (compl == EXPAND_COMMANDS)
3616 return arg;
3617 if (compl == EXPAND_MAPPINGS)
3618 return set_context_in_map_cmd(xp, (char_u *)"map",
3619 arg, forceit, FALSE, FALSE, CMD_map);
3620 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3621 arg = xp->xp_pattern + 1;
3622 xp->xp_pattern = arg;
3623 }
3624 xp->xp_context = compl;
3625 }
3626 break;
3627#endif
3628 case CMD_map: case CMD_noremap:
3629 case CMD_nmap: case CMD_nnoremap:
3630 case CMD_vmap: case CMD_vnoremap:
3631 case CMD_omap: case CMD_onoremap:
3632 case CMD_imap: case CMD_inoremap:
3633 case CMD_cmap: case CMD_cnoremap:
3634 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003635 FALSE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636 case CMD_unmap:
3637 case CMD_nunmap:
3638 case CMD_vunmap:
3639 case CMD_ounmap:
3640 case CMD_iunmap:
3641 case CMD_cunmap:
3642 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003643 FALSE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644 case CMD_abbreviate: case CMD_noreabbrev:
3645 case CMD_cabbrev: case CMD_cnoreabbrev:
3646 case CMD_iabbrev: case CMD_inoreabbrev:
3647 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003648 TRUE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003649 case CMD_unabbreviate:
3650 case CMD_cunabbrev:
3651 case CMD_iunabbrev:
3652 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003653 TRUE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003654#ifdef FEAT_MENU
3655 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
3656 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
3657 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
3658 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
3659 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
3660 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
3661 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
3662 case CMD_tmenu: case CMD_tunmenu:
3663 case CMD_popup: case CMD_tearoff: case CMD_emenu:
3664 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
3665#endif
3666
3667 case CMD_colorscheme:
3668 xp->xp_context = EXPAND_COLORS;
3669 xp->xp_pattern = arg;
3670 break;
3671
3672 case CMD_compiler:
3673 xp->xp_context = EXPAND_COMPILER;
3674 xp->xp_pattern = arg;
3675 break;
3676
3677#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
3678 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
3679 case CMD_language:
3680 if (*skiptowhite(arg) == NUL)
3681 {
3682 xp->xp_context = EXPAND_LANGUAGE;
3683 xp->xp_pattern = arg;
3684 }
3685 else
3686 xp->xp_context = EXPAND_NOTHING;
3687 break;
3688#endif
3689
3690#endif /* FEAT_CMDL_COMPL */
3691
3692 default:
3693 break;
3694 }
3695 return NULL;
3696}
3697
3698/*
3699 * skip a range specifier of the form: addr [,addr] [;addr] ..
3700 *
3701 * Backslashed delimiters after / or ? will be skipped, and commands will
3702 * not be expanded between /'s and ?'s or after "'".
3703 *
Bram Moolenaardf177f62005-02-22 08:39:57 +00003704 * Also skip white space and ":" characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003705 * Returns the "cmd" pointer advanced to beyond the range.
3706 */
3707 char_u *
3708skip_range(cmd, ctx)
3709 char_u *cmd;
3710 int *ctx; /* pointer to xp_context or NULL */
3711{
3712 int delim;
3713
Bram Moolenaardf177f62005-02-22 08:39:57 +00003714 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003715 {
3716 if (*cmd == '\'')
3717 {
3718 if (*++cmd == NUL && ctx != NULL)
3719 *ctx = EXPAND_NOTHING;
3720 }
3721 else if (*cmd == '/' || *cmd == '?')
3722 {
3723 delim = *cmd++;
3724 while (*cmd != NUL && *cmd != delim)
3725 if (*cmd++ == '\\' && *cmd != NUL)
3726 ++cmd;
3727 if (*cmd == NUL && ctx != NULL)
3728 *ctx = EXPAND_NOTHING;
3729 }
3730 if (*cmd != NUL)
3731 ++cmd;
3732 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00003733
3734 /* Skip ":" and white space. */
3735 while (*cmd == ':')
3736 cmd = skipwhite(cmd + 1);
3737
Bram Moolenaar071d4272004-06-13 20:20:40 +00003738 return cmd;
3739}
3740
3741/*
3742 * get a single EX address
3743 *
3744 * Set ptr to the next character after the part that was interpreted.
3745 * Set ptr to NULL when an error is encountered.
3746 *
3747 * Return MAXLNUM when no Ex address was found.
3748 */
3749 static linenr_T
3750get_address(ptr, skip, to_other_file)
3751 char_u **ptr;
3752 int skip; /* only skip the address, don't use it */
3753 int to_other_file; /* flag: may jump to other file */
3754{
3755 int c;
3756 int i;
3757 long n;
3758 char_u *cmd;
3759 pos_T pos;
3760 pos_T *fp;
3761 linenr_T lnum;
3762
3763 cmd = skipwhite(*ptr);
3764 lnum = MAXLNUM;
3765 do
3766 {
3767 switch (*cmd)
3768 {
3769 case '.': /* '.' - Cursor position */
3770 ++cmd;
3771 lnum = curwin->w_cursor.lnum;
3772 break;
3773
3774 case '$': /* '$' - last line */
3775 ++cmd;
3776 lnum = curbuf->b_ml.ml_line_count;
3777 break;
3778
3779 case '\'': /* ''' - mark */
3780 if (*++cmd == NUL)
3781 {
3782 cmd = NULL;
3783 goto error;
3784 }
3785 if (skip)
3786 ++cmd;
3787 else
3788 {
3789 /* Only accept a mark in another file when it is
3790 * used by itself: ":'M". */
3791 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
3792 ++cmd;
3793 if (fp == (pos_T *)-1)
3794 /* Jumped to another file. */
3795 lnum = curwin->w_cursor.lnum;
3796 else
3797 {
3798 if (check_mark(fp) == FAIL)
3799 {
3800 cmd = NULL;
3801 goto error;
3802 }
3803 lnum = fp->lnum;
3804 }
3805 }
3806 break;
3807
3808 case '/':
3809 case '?': /* '/' or '?' - search */
3810 c = *cmd++;
3811 if (skip) /* skip "/pat/" */
3812 {
3813 cmd = skip_regexp(cmd, c, (int)p_magic, NULL);
3814 if (*cmd == c)
3815 ++cmd;
3816 }
3817 else
3818 {
3819 pos = curwin->w_cursor; /* save curwin->w_cursor */
3820 /*
3821 * When '/' or '?' follows another address, start
3822 * from there.
3823 */
3824 if (lnum != MAXLNUM)
3825 curwin->w_cursor.lnum = lnum;
3826 /*
3827 * Start a forward search at the end of the line.
3828 * Start a backward search at the start of the line.
3829 * This makes sure we never match in the current
3830 * line, and can match anywhere in the
3831 * next/previous line.
3832 */
3833 if (c == '/')
3834 curwin->w_cursor.col = MAXCOL;
3835 else
3836 curwin->w_cursor.col = 0;
3837 searchcmdlen = 0;
3838 if (!do_search(NULL, c, cmd, 1L,
3839 SEARCH_HIS + SEARCH_MSG + SEARCH_START))
3840 {
3841 curwin->w_cursor = pos;
3842 cmd = NULL;
3843 goto error;
3844 }
3845 lnum = curwin->w_cursor.lnum;
3846 curwin->w_cursor = pos;
3847 /* adjust command string pointer */
3848 cmd += searchcmdlen;
3849 }
3850 break;
3851
3852 case '\\': /* "\?", "\/" or "\&", repeat search */
3853 ++cmd;
3854 if (*cmd == '&')
3855 i = RE_SUBST;
3856 else if (*cmd == '?' || *cmd == '/')
3857 i = RE_SEARCH;
3858 else
3859 {
3860 EMSG(_(e_backslash));
3861 cmd = NULL;
3862 goto error;
3863 }
3864
3865 if (!skip)
3866 {
3867 /*
3868 * When search follows another address, start from
3869 * there.
3870 */
3871 if (lnum != MAXLNUM)
3872 pos.lnum = lnum;
3873 else
3874 pos.lnum = curwin->w_cursor.lnum;
3875
3876 /*
3877 * Start the search just like for the above
3878 * do_search().
3879 */
3880 if (*cmd != '?')
3881 pos.col = MAXCOL;
3882 else
3883 pos.col = 0;
3884 if (searchit(curwin, curbuf, &pos,
3885 *cmd == '?' ? BACKWARD : FORWARD,
3886 (char_u *)"", 1L,
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003887 SEARCH_MSG + SEARCH_START,
3888 i, (linenr_T)0) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889 lnum = pos.lnum;
3890 else
3891 {
3892 cmd = NULL;
3893 goto error;
3894 }
3895 }
3896 ++cmd;
3897 break;
3898
3899 default:
3900 if (VIM_ISDIGIT(*cmd)) /* absolute line number */
3901 lnum = getdigits(&cmd);
3902 }
3903
3904 for (;;)
3905 {
3906 cmd = skipwhite(cmd);
3907 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
3908 break;
3909
3910 if (lnum == MAXLNUM)
3911 lnum = curwin->w_cursor.lnum; /* "+1" is same as ".+1" */
3912 if (VIM_ISDIGIT(*cmd))
3913 i = '+'; /* "number" is same as "+number" */
3914 else
3915 i = *cmd++;
3916 if (!VIM_ISDIGIT(*cmd)) /* '+' is '+1', but '+0' is not '+1' */
3917 n = 1;
3918 else
3919 n = getdigits(&cmd);
3920 if (i == '-')
3921 lnum -= n;
3922 else
3923 lnum += n;
3924 }
3925 } while (*cmd == '/' || *cmd == '?');
3926
3927error:
3928 *ptr = cmd;
3929 return lnum;
3930}
3931
3932/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00003933 * Get flags from an Ex command argument.
3934 */
3935 static void
3936get_flags(eap)
3937 exarg_T *eap;
3938{
3939 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
3940 {
3941 if (*eap->arg == 'l')
3942 eap->flags |= EXFLAG_LIST;
3943 else if (*eap->arg == 'p')
3944 eap->flags |= EXFLAG_PRINT;
3945 else
3946 eap->flags |= EXFLAG_NR;
3947 eap->arg = skipwhite(eap->arg + 1);
3948 }
3949}
3950
3951/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003952 * Function called for command which is Not Implemented. NI!
3953 */
3954 void
3955ex_ni(eap)
3956 exarg_T *eap;
3957{
3958 if (!eap->skip)
3959 eap->errmsg = (char_u *)N_("E319: Sorry, the command is not available in this version");
3960}
3961
3962#if !defined(FEAT_PERL) || !defined(FEAT_PYTHON) || !defined(FEAT_TCL) \
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003963 || !defined(FEAT_RUBY) || !defined(FEAT_MZSCHEME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003964/*
3965 * Function called for script command which is Not Implemented. NI!
3966 * Skips over ":perl <<EOF" constructs.
3967 */
3968 static void
3969ex_script_ni(eap)
3970 exarg_T *eap;
3971{
3972 if (!eap->skip)
3973 ex_ni(eap);
3974 else
3975 vim_free(script_get(eap, eap->arg));
3976}
3977#endif
3978
3979/*
3980 * Check range in Ex command for validity.
3981 * Return NULL when valid, error message when invalid.
3982 */
3983 static char_u *
3984invalid_range(eap)
3985 exarg_T *eap;
3986{
3987 if ( eap->line1 < 0
3988 || eap->line2 < 0
3989 || eap->line1 > eap->line2
3990 || ((eap->argt & RANGE)
3991 && !(eap->argt & NOTADR)
3992 && eap->line2 > curbuf->b_ml.ml_line_count
3993#ifdef FEAT_DIFF
3994 + (eap->cmdidx == CMD_diffget)
3995#endif
3996 ))
3997 return (char_u *)_(e_invrange);
3998 return NULL;
3999}
4000
4001/*
4002 * Correct the range for zero line number, if required.
4003 */
4004 static void
4005correct_range(eap)
4006 exarg_T *eap;
4007{
4008 if (!(eap->argt & ZEROR)) /* zero in range not allowed */
4009 {
4010 if (eap->line1 == 0)
4011 eap->line1 = 1;
4012 if (eap->line2 == 0)
4013 eap->line2 = 1;
4014 }
4015}
4016
Bram Moolenaar748bf032005-02-02 23:04:36 +00004017#ifdef FEAT_QUICKFIX
4018static char_u *skip_grep_pat __ARGS((exarg_T *eap));
4019
4020/*
4021 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4022 * pattern. Otherwise return eap->arg.
4023 */
4024 static char_u *
4025skip_grep_pat(eap)
4026 exarg_T *eap;
4027{
4028 char_u *p = eap->arg;
4029
Bram Moolenaara37420f2006-02-04 22:37:47 +00004030 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4031 || eap->cmdidx == CMD_vimgrepadd
4032 || eap->cmdidx == CMD_lvimgrepadd
4033 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004034 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004035 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004036 if (p == NULL)
4037 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004038 }
4039 return p;
4040}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004041
4042/*
4043 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4044 * in the command line, so that things like % get expanded.
4045 */
4046 static char_u *
4047replace_makeprg(eap, p, cmdlinep)
4048 exarg_T *eap;
4049 char_u *p;
4050 char_u **cmdlinep;
4051{
4052 char_u *new_cmdline;
4053 char_u *program;
4054 char_u *pos;
4055 char_u *ptr;
4056 int len;
4057 int i;
4058
4059 /*
4060 * Don't do it when ":vimgrep" is used for ":grep".
4061 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004062 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4063 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4064 || eap->cmdidx == CMD_grepadd
4065 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004066 && !grep_internal(eap->cmdidx))
4067 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004068 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4069 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004070 {
4071 if (*curbuf->b_p_gp == NUL)
4072 program = p_gp;
4073 else
4074 program = curbuf->b_p_gp;
4075 }
4076 else
4077 {
4078 if (*curbuf->b_p_mp == NUL)
4079 program = p_mp;
4080 else
4081 program = curbuf->b_p_mp;
4082 }
4083
4084 p = skipwhite(p);
4085
4086 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4087 {
4088 /* replace $* by given arguments */
4089 i = 1;
4090 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4091 ++i;
4092 len = (int)STRLEN(p);
4093 new_cmdline = alloc((int)(STRLEN(program) + i * (len - 2) + 1));
4094 if (new_cmdline == NULL)
4095 return NULL; /* out of memory */
4096 ptr = new_cmdline;
4097 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4098 {
4099 i = (int)(pos - program);
4100 STRNCPY(ptr, program, i);
4101 STRCPY(ptr += i, p);
4102 ptr += len;
4103 program = pos + 2;
4104 }
4105 STRCPY(ptr, program);
4106 }
4107 else
4108 {
4109 new_cmdline = alloc((int)(STRLEN(program) + STRLEN(p) + 2));
4110 if (new_cmdline == NULL)
4111 return NULL; /* out of memory */
4112 STRCPY(new_cmdline, program);
4113 STRCAT(new_cmdline, " ");
4114 STRCAT(new_cmdline, p);
4115 }
4116 msg_make(p);
4117
4118 /* 'eap->cmd' is not set here, because it is not used at CMD_make */
4119 vim_free(*cmdlinep);
4120 *cmdlinep = new_cmdline;
4121 p = new_cmdline;
4122 }
4123 return p;
4124}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004125#endif
4126
Bram Moolenaar071d4272004-06-13 20:20:40 +00004127/*
4128 * Expand file name in Ex command argument.
4129 * Return FAIL for failure, OK otherwise.
4130 */
4131 int
4132expand_filename(eap, cmdlinep, errormsgp)
4133 exarg_T *eap;
4134 char_u **cmdlinep;
4135 char_u **errormsgp;
4136{
4137 int has_wildcards; /* need to expand wildcards */
4138 char_u *repl;
4139 int srclen;
4140 char_u *p;
4141 int n;
4142
Bram Moolenaar748bf032005-02-02 23:04:36 +00004143#ifdef FEAT_QUICKFIX
4144 /* Skip a regexp pattern for ":vimgrep[add] pat file..." */
4145 p = skip_grep_pat(eap);
4146#else
4147 p = eap->arg;
4148#endif
4149
Bram Moolenaar071d4272004-06-13 20:20:40 +00004150 /*
4151 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4152 * the file name contains a wildcard it should not cause expanding.
4153 * (it will be expanded anyway if there is a wildcard before replacing).
4154 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004155 has_wildcards = mch_has_wildcard(p);
4156 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004157 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004158#ifdef FEAT_EVAL
4159 /* Skip over `=expr`, wildcards in it are not expanded. */
4160 if (p[0] == '`' && p[1] == '=')
4161 {
4162 p += 2;
4163 (void)skip_expr(&p);
4164 if (*p == '`')
4165 ++p;
4166 continue;
4167 }
4168#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169 /*
4170 * Quick check if this cannot be the start of a special string.
4171 * Also removes backslash before '%', '#' and '<'.
4172 */
4173 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4174 {
4175 ++p;
4176 continue;
4177 }
4178
4179 /*
4180 * Try to find a match at this position.
4181 */
4182 repl = eval_vars(p, &srclen, &(eap->do_ecmd_lnum), errormsgp, eap->arg);
4183 if (*errormsgp != NULL) /* error detected */
4184 return FAIL;
4185 if (repl == NULL) /* no match found */
4186 {
4187 p += srclen;
4188 continue;
4189 }
4190
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004191 /* Wildcards won't be expanded below, the replacement is taken
4192 * literally. But do expand "~/file", "~user/file" and "$HOME/file". */
4193 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4194 {
4195 char_u *l = repl;
4196
4197 repl = expand_env_save(repl);
4198 vim_free(l);
4199 }
4200
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201 /* Need to escape white space et al. with a backslash. Don't do this
4202 * for shell commands (may have to use quotes instead). Don't do this
4203 * for non-unix systems when there is a single argument (spaces don't
4204 * separate arguments then). */
4205 if (!eap->usefilter
4206 && eap->cmdidx != CMD_bang
4207 && eap->cmdidx != CMD_make
Bram Moolenaara37420f2006-02-04 22:37:47 +00004208 && eap->cmdidx != CMD_lmake
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209 && eap->cmdidx != CMD_grep
Bram Moolenaara37420f2006-02-04 22:37:47 +00004210 && eap->cmdidx != CMD_lgrep
Bram Moolenaar071d4272004-06-13 20:20:40 +00004211 && eap->cmdidx != CMD_grepadd
Bram Moolenaara37420f2006-02-04 22:37:47 +00004212 && eap->cmdidx != CMD_lgrepadd
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213#ifndef UNIX
4214 && !(eap->argt & NOSPC)
4215#endif
4216 )
4217 {
4218 char_u *l;
4219#ifdef BACKSLASH_IN_FILENAME
4220 /* Don't escape a backslash here, because rem_backslash() doesn't
4221 * remove it later. */
4222 static char_u *nobslash = (char_u *)" \t\"|";
4223# define ESCAPE_CHARS nobslash
4224#else
4225# define ESCAPE_CHARS escape_chars
4226#endif
4227
4228 for (l = repl; *l; ++l)
4229 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
4230 {
4231 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
4232 if (l != NULL)
4233 {
4234 vim_free(repl);
4235 repl = l;
4236 }
4237 break;
4238 }
4239 }
4240
4241 /* For a shell command a '!' must be escaped. */
4242 if ((eap->usefilter || eap->cmdidx == CMD_bang)
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004243 && vim_strpbrk(repl, (char_u *)"!&;()<>") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244 {
4245 char_u *l;
4246
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004247 l = vim_strsave_escaped(repl, (char_u *)"!&;()<>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248 if (l != NULL)
4249 {
4250 vim_free(repl);
4251 repl = l;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004252 /* For a sh-like shell escape "!" another time. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253 if (strstr((char *)p_sh, "sh") != NULL)
4254 {
4255 l = vim_strsave_escaped(repl, (char_u *)"!");
4256 if (l != NULL)
4257 {
4258 vim_free(repl);
4259 repl = l;
4260 }
4261 }
4262 }
4263 }
4264
4265 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
4266 vim_free(repl);
4267 if (p == NULL)
4268 return FAIL;
4269 }
4270
4271 /*
4272 * One file argument: Expand wildcards.
4273 * Don't do this with ":r !command" or ":w !command".
4274 */
4275 if ((eap->argt & NOSPC) && !eap->usefilter)
4276 {
4277 /*
4278 * May do this twice:
4279 * 1. Replace environment variables.
4280 * 2. Replace any other wildcards, remove backslashes.
4281 */
4282 for (n = 1; n <= 2; ++n)
4283 {
4284 if (n == 2)
4285 {
4286#ifdef UNIX
4287 /*
4288 * Only for Unix we check for more than one file name.
4289 * For other systems spaces are considered to be part
4290 * of the file name.
4291 * Only check here if there is no wildcard, otherwise
4292 * ExpandOne() will check for errors. This allows
4293 * ":e `ls ve*.c`" on Unix.
4294 */
4295 if (!has_wildcards)
4296 for (p = eap->arg; *p; ++p)
4297 {
4298 /* skip escaped characters */
4299 if (p[1] && (*p == '\\' || *p == Ctrl_V))
4300 ++p;
4301 else if (vim_iswhite(*p))
4302 {
4303 *errormsgp = (char_u *)_("E172: Only one file name allowed");
4304 return FAIL;
4305 }
4306 }
4307#endif
4308
4309 /*
4310 * Halve the number of backslashes (this is Vi compatible).
4311 * For Unix and OS/2, when wildcards are expanded, this is
4312 * done by ExpandOne() below.
4313 */
4314#if defined(UNIX) || defined(OS2)
4315 if (!has_wildcards)
4316#endif
4317 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004318 }
4319
4320 if (has_wildcards)
4321 {
4322 if (n == 1)
4323 {
4324 /*
4325 * First loop: May expand environment variables. This
4326 * can be done much faster with expand_env() than with
4327 * something else (e.g., calling a shell).
4328 * After expanding environment variables, check again
4329 * if there are still wildcards present.
4330 */
4331 if (vim_strchr(eap->arg, '$') != NULL
4332 || vim_strchr(eap->arg, '~') != NULL)
4333 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00004334 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
4335 TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004336 has_wildcards = mch_has_wildcard(NameBuff);
4337 p = NameBuff;
4338 }
4339 else
4340 p = NULL;
4341 }
4342 else /* n == 2 */
4343 {
4344 expand_T xpc;
4345
4346 ExpandInit(&xpc);
4347 xpc.xp_context = EXPAND_FILES;
4348 p = ExpandOne(&xpc, eap->arg, NULL,
4349 WILD_LIST_NOTFOUND|WILD_ADD_SLASH,
4350 WILD_EXPAND_FREE);
4351 ExpandCleanup(&xpc);
4352 if (p == NULL)
4353 return FAIL;
4354 }
4355 if (p != NULL)
4356 {
4357 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
4358 p, cmdlinep);
4359 if (n == 2) /* p came from ExpandOne() */
4360 vim_free(p);
4361 }
4362 }
4363 }
4364 }
4365 return OK;
4366}
4367
4368/*
4369 * Replace part of the command line, keeping eap->cmd, eap->arg and
4370 * eap->nextcmd correct.
4371 * "src" points to the part that is to be replaced, of length "srclen".
4372 * "repl" is the replacement string.
4373 * Returns a pointer to the character after the replaced string.
4374 * Returns NULL for failure.
4375 */
4376 static char_u *
4377repl_cmdline(eap, src, srclen, repl, cmdlinep)
4378 exarg_T *eap;
4379 char_u *src;
4380 int srclen;
4381 char_u *repl;
4382 char_u **cmdlinep;
4383{
4384 int len;
4385 int i;
4386 char_u *new_cmdline;
4387
4388 /*
4389 * The new command line is build in new_cmdline[].
4390 * First allocate it.
4391 * Careful: a "+cmd" argument may have been NUL terminated.
4392 */
4393 len = (int)STRLEN(repl);
4394 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
4395 if (eap->nextcmd)
4396 i += (int)STRLEN(eap->nextcmd);/* add space for next command */
4397 if ((new_cmdline = alloc((unsigned)i)) == NULL)
4398 return NULL; /* out of memory! */
4399
4400 /*
4401 * Copy the stuff before the expanded part.
4402 * Copy the expanded stuff.
4403 * Copy what came after the expanded part.
4404 * Copy the next commands, if there are any.
4405 */
4406 i = (int)(src - *cmdlinep); /* length of part before match */
4407 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00004408
Bram Moolenaar071d4272004-06-13 20:20:40 +00004409 mch_memmove(new_cmdline + i, repl, (size_t)len);
4410 i += len; /* remember the end of the string */
4411 STRCPY(new_cmdline + i, src + srclen);
4412 src = new_cmdline + i; /* remember where to continue */
4413
4414 if (eap->nextcmd) /* append next command */
4415 {
4416 i = (int)STRLEN(new_cmdline) + 1;
4417 STRCPY(new_cmdline + i, eap->nextcmd);
4418 eap->nextcmd = new_cmdline + i;
4419 }
4420 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
4421 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
4422 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
4423 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
4424 vim_free(*cmdlinep);
4425 *cmdlinep = new_cmdline;
4426
4427 return src;
4428}
4429
4430/*
4431 * Check for '|' to separate commands and '"' to start comments.
4432 */
4433 void
4434separate_nextcmd(eap)
4435 exarg_T *eap;
4436{
4437 char_u *p;
4438
Bram Moolenaar86b68352004-12-27 21:59:20 +00004439#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004440 p = skip_grep_pat(eap);
4441#else
4442 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004443#endif
4444
4445 for ( ; *p; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004446 {
4447 if (*p == Ctrl_V)
4448 {
4449 if (eap->argt & (USECTRLV | XFILE))
4450 ++p; /* skip CTRL-V and next char */
4451 else
4452 STRCPY(p, p + 1); /* remove CTRL-V and skip next char */
4453 if (*p == NUL) /* stop at NUL after CTRL-V */
4454 break;
4455 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004456
4457#ifdef FEAT_EVAL
4458 /* Skip over `=expr` when wildcards are expanded. */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004459 else if (p[0] == '`' && p[1] == '=' && (eap->argt & XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004460 {
4461 p += 2;
4462 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004463 }
4464#endif
4465
Bram Moolenaar071d4272004-06-13 20:20:40 +00004466 /* Check for '"': start of comment or '|': next command */
4467 /* :@" and :*" do not start a comment!
4468 * :redir @" doesn't either. */
4469 else if ((*p == '"' && !(eap->argt & NOTRLCOM)
4470 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
4471 || p != eap->arg)
4472 && (eap->cmdidx != CMD_redir
4473 || p != eap->arg + 1 || p[-1] != '@'))
4474 || *p == '|' || *p == '\n')
4475 {
4476 /*
4477 * We remove the '\' before the '|', unless USECTRLV is used
4478 * AND 'b' is present in 'cpoptions'.
4479 */
4480 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
4481 || !(eap->argt & USECTRLV)) && *(p - 1) == '\\')
4482 {
4483 mch_memmove(p - 1, p, STRLEN(p) + 1); /* remove the '\' */
4484 --p;
4485 }
4486 else
4487 {
4488 eap->nextcmd = check_nextcmd(p);
4489 *p = NUL;
4490 break;
4491 }
4492 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004494
Bram Moolenaar071d4272004-06-13 20:20:40 +00004495 if (!(eap->argt & NOTRLCOM)) /* remove trailing spaces */
4496 del_trailing_spaces(eap->arg);
4497}
4498
4499/*
4500 * get + command from ex argument
4501 */
4502 static char_u *
4503getargcmd(argp)
4504 char_u **argp;
4505{
4506 char_u *arg = *argp;
4507 char_u *command = NULL;
4508
4509 if (*arg == '+') /* +[command] */
4510 {
4511 ++arg;
4512 if (vim_isspace(*arg))
4513 command = dollar_command;
4514 else
4515 {
4516 command = arg;
4517 arg = skip_cmd_arg(command, TRUE);
4518 if (*arg != NUL)
4519 *arg++ = NUL; /* terminate command with NUL */
4520 }
4521
4522 arg = skipwhite(arg); /* skip over spaces */
4523 *argp = arg;
4524 }
4525 return command;
4526}
4527
4528/*
4529 * Find end of "+command" argument. Skip over "\ " and "\\".
4530 */
4531 static char_u *
4532skip_cmd_arg(p, rembs)
4533 char_u *p;
4534 int rembs; /* TRUE to halve the number of backslashes */
4535{
4536 while (*p && !vim_isspace(*p))
4537 {
4538 if (*p == '\\' && p[1] != NUL)
4539 {
4540 if (rembs)
4541 mch_memmove(p, p + 1, STRLEN(p));
4542 else
4543 ++p;
4544 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004545 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004546 }
4547 return p;
4548}
4549
4550/*
4551 * Get "++opt=arg" argument.
4552 * Return FAIL or OK.
4553 */
4554 static int
4555getargopt(eap)
4556 exarg_T *eap;
4557{
4558 char_u *arg = eap->arg + 2;
4559 int *pp = NULL;
4560#ifdef FEAT_MBYTE
4561 char_u *p;
4562#endif
4563
4564 /* ":edit ++[no]bin[ary] file" */
4565 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
4566 {
4567 if (*arg == 'n')
4568 {
4569 arg += 2;
4570 eap->force_bin = FORCE_NOBIN;
4571 }
4572 else
4573 eap->force_bin = FORCE_BIN;
4574 if (!checkforcmd(&arg, "binary", 3))
4575 return FAIL;
4576 eap->arg = skipwhite(arg);
4577 return OK;
4578 }
4579
4580 if (STRNCMP(arg, "ff", 2) == 0)
4581 {
4582 arg += 2;
4583 pp = &eap->force_ff;
4584 }
4585 else if (STRNCMP(arg, "fileformat", 10) == 0)
4586 {
4587 arg += 10;
4588 pp = &eap->force_ff;
4589 }
4590#ifdef FEAT_MBYTE
4591 else if (STRNCMP(arg, "enc", 3) == 0)
4592 {
4593 arg += 3;
4594 pp = &eap->force_enc;
4595 }
4596 else if (STRNCMP(arg, "encoding", 8) == 0)
4597 {
4598 arg += 8;
4599 pp = &eap->force_enc;
4600 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004601 else if (STRNCMP(arg, "bad", 3) == 0)
4602 {
4603 arg += 3;
4604 pp = &eap->bad_char;
4605 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004606#endif
4607
4608 if (pp == NULL || *arg != '=')
4609 return FAIL;
4610
4611 ++arg;
4612 *pp = (int)(arg - eap->cmd);
4613 arg = skip_cmd_arg(arg, FALSE);
4614 eap->arg = skipwhite(arg);
4615 *arg = NUL;
4616
4617#ifdef FEAT_MBYTE
4618 if (pp == &eap->force_ff)
4619 {
4620#endif
4621 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
4622 return FAIL;
4623#ifdef FEAT_MBYTE
4624 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004625 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004626 {
4627 /* Make 'fileencoding' lower case. */
4628 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
4629 *p = TOLOWER_ASC(*p);
4630 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004631 else
4632 {
4633 /* Check ++bad= argument. Must be a single-byte character, "keep" or
4634 * "drop". */
4635 p = eap->cmd + eap->bad_char;
4636 if (STRICMP(p, "keep") == 0)
4637 eap->bad_char = BAD_KEEP;
4638 else if (STRICMP(p, "drop") == 0)
4639 eap->bad_char = BAD_DROP;
4640 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
4641 eap->bad_char = *p;
4642 else
4643 return FAIL;
4644 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004645#endif
4646
4647 return OK;
4648}
4649
4650/*
4651 * ":abbreviate" and friends.
4652 */
4653 static void
4654ex_abbreviate(eap)
4655 exarg_T *eap;
4656{
4657 do_exmap(eap, TRUE); /* almost the same as mapping */
4658}
4659
4660/*
4661 * ":map" and friends.
4662 */
4663 static void
4664ex_map(eap)
4665 exarg_T *eap;
4666{
4667 /*
4668 * If we are sourcing .exrc or .vimrc in current directory we
4669 * print the mappings for security reasons.
4670 */
4671 if (secure)
4672 {
4673 secure = 2;
4674 msg_outtrans(eap->cmd);
4675 msg_putchar('\n');
4676 }
4677 do_exmap(eap, FALSE);
4678}
4679
4680/*
4681 * ":unmap" and friends.
4682 */
4683 static void
4684ex_unmap(eap)
4685 exarg_T *eap;
4686{
4687 do_exmap(eap, FALSE);
4688}
4689
4690/*
4691 * ":mapclear" and friends.
4692 */
4693 static void
4694ex_mapclear(eap)
4695 exarg_T *eap;
4696{
4697 map_clear(eap->cmd, eap->arg, eap->forceit, FALSE);
4698}
4699
4700/*
4701 * ":abclear" and friends.
4702 */
4703 static void
4704ex_abclear(eap)
4705 exarg_T *eap;
4706{
4707 map_clear(eap->cmd, eap->arg, TRUE, TRUE);
4708}
4709
4710#ifdef FEAT_AUTOCMD
4711 static void
4712ex_autocmd(eap)
4713 exarg_T *eap;
4714{
4715 /*
4716 * Disallow auto commands from .exrc and .vimrc in current
4717 * directory for security reasons.
4718 */
4719 if (secure)
4720 {
4721 secure = 2;
4722 eap->errmsg = e_curdir;
4723 }
4724 else if (eap->cmdidx == CMD_autocmd)
4725 do_autocmd(eap->arg, eap->forceit);
4726 else
4727 do_augroup(eap->arg, eap->forceit);
4728}
4729
4730/*
4731 * ":doautocmd": Apply the automatic commands to the current buffer.
4732 */
4733 static void
4734ex_doautocmd(eap)
4735 exarg_T *eap;
4736{
4737 (void)do_doautocmd(eap->arg, TRUE);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00004738 do_modelines(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004739}
4740#endif
4741
4742#ifdef FEAT_LISTCMDS
4743/*
4744 * :[N]bunload[!] [N] [bufname] unload buffer
4745 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
4746 * :[N]bwipeout[!] [N] [bufname] delete buffer really
4747 */
4748 static void
4749ex_bunload(eap)
4750 exarg_T *eap;
4751{
4752 eap->errmsg = do_bufdel(
4753 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
4754 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
4755 : DOBUF_UNLOAD, eap->arg,
4756 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
4757}
4758
4759/*
4760 * :[N]buffer [N] to buffer N
4761 * :[N]sbuffer [N] to buffer N
4762 */
4763 static void
4764ex_buffer(eap)
4765 exarg_T *eap;
4766{
4767 if (*eap->arg)
4768 eap->errmsg = e_trailing;
4769 else
4770 {
4771 if (eap->addr_count == 0) /* default is current buffer */
4772 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
4773 else
4774 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
4775 }
4776}
4777
4778/*
4779 * :[N]bmodified [N] to next mod. buffer
4780 * :[N]sbmodified [N] to next mod. buffer
4781 */
4782 static void
4783ex_bmodified(eap)
4784 exarg_T *eap;
4785{
4786 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
4787}
4788
4789/*
4790 * :[N]bnext [N] to next buffer
4791 * :[N]sbnext [N] split and to next buffer
4792 */
4793 static void
4794ex_bnext(eap)
4795 exarg_T *eap;
4796{
4797 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
4798}
4799
4800/*
4801 * :[N]bNext [N] to previous buffer
4802 * :[N]bprevious [N] to previous buffer
4803 * :[N]sbNext [N] split and to previous buffer
4804 * :[N]sbprevious [N] split and to previous buffer
4805 */
4806 static void
4807ex_bprevious(eap)
4808 exarg_T *eap;
4809{
4810 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
4811}
4812
4813/*
4814 * :brewind to first buffer
4815 * :bfirst to first buffer
4816 * :sbrewind split and to first buffer
4817 * :sbfirst split and to first buffer
4818 */
4819 static void
4820ex_brewind(eap)
4821 exarg_T *eap;
4822{
4823 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
4824}
4825
4826/*
4827 * :blast to last buffer
4828 * :sblast split and to last buffer
4829 */
4830 static void
4831ex_blast(eap)
4832 exarg_T *eap;
4833{
4834 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
4835}
4836#endif
4837
4838 int
4839ends_excmd(c)
4840 int c;
4841{
4842 return (c == NUL || c == '|' || c == '"' || c == '\n');
4843}
4844
4845#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
4846 || defined(PROTO)
4847/*
4848 * Return the next command, after the first '|' or '\n'.
4849 * Return NULL if not found.
4850 */
4851 char_u *
4852find_nextcmd(p)
4853 char_u *p;
4854{
4855 while (*p != '|' && *p != '\n')
4856 {
4857 if (*p == NUL)
4858 return NULL;
4859 ++p;
4860 }
4861 return (p + 1);
4862}
4863#endif
4864
4865/*
4866 * Check if *p is a separator between Ex commands.
4867 * Return NULL if it isn't, (p + 1) if it is.
4868 */
4869 char_u *
4870check_nextcmd(p)
4871 char_u *p;
4872{
4873 p = skipwhite(p);
4874 if (*p == '|' || *p == '\n')
4875 return (p + 1);
4876 else
4877 return NULL;
4878}
4879
4880/*
4881 * - if there are more files to edit
4882 * - and this is the last window
4883 * - and forceit not used
4884 * - and not repeated twice on a row
4885 * return FAIL and give error message if 'message' TRUE
4886 * return OK otherwise
4887 */
4888 static int
4889check_more(message, forceit)
4890 int message; /* when FALSE check only, no messages */
4891 int forceit;
4892{
4893 int n = ARGCOUNT - curwin->w_arg_idx - 1;
4894
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00004895 if (!forceit && only_one_window()
4896 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004897 {
4898 if (message)
4899 {
4900#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
4901 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
4902 {
4903 char_u buff[IOSIZE];
4904
4905 if (n == 1)
4906 STRCPY(buff, _("1 more file to edit. Quit anyway?"));
4907 else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004908 vim_snprintf((char *)buff, IOSIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004909 _("%d more files to edit. Quit anyway?"), n);
4910 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
4911 return OK;
4912 return FAIL;
4913 }
4914#endif
4915 if (n == 1)
4916 EMSG(_("E173: 1 more file to edit"));
4917 else
4918 EMSGN(_("E173: %ld more files to edit"), n);
4919 quitmore = 2; /* next try to quit is allowed */
4920 }
4921 return FAIL;
4922 }
4923 return OK;
4924}
4925
4926#ifdef FEAT_CMDL_COMPL
4927/*
4928 * Function given to ExpandGeneric() to obtain the list of command names.
4929 */
4930/*ARGSUSED*/
4931 char_u *
4932get_command_name(xp, idx)
4933 expand_T *xp;
4934 int idx;
4935{
4936 if (idx >= (int)CMD_SIZE)
4937# ifdef FEAT_USR_CMDS
4938 return get_user_command_name(idx);
4939# else
4940 return NULL;
4941# endif
4942 return cmdnames[idx].cmd_name;
4943}
4944#endif
4945
4946#if defined(FEAT_USR_CMDS) || defined(PROTO)
4947static 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));
4948static void uc_list __ARGS((char_u *name, size_t name_len));
4949static int uc_scan_attr __ARGS((char_u *attr, size_t len, long *argt, long *def, int *flags, int *compl, char_u **compl_arg));
4950static char_u *uc_split_args __ARGS((char_u *arg, size_t *lenp));
4951static 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));
4952
4953 static int
4954uc_add_command(name, name_len, rep, argt, def, flags, compl, compl_arg, force)
4955 char_u *name;
4956 size_t name_len;
4957 char_u *rep;
4958 long argt;
4959 long def;
4960 int flags;
4961 int compl;
4962 char_u *compl_arg;
4963 int force;
4964{
4965 ucmd_T *cmd = NULL;
4966 char_u *p;
4967 int i;
4968 int cmp = 1;
4969 char_u *rep_buf = NULL;
4970 garray_T *gap;
4971
4972 replace_termcodes(rep, &rep_buf, FALSE, FALSE);
4973 if (rep_buf == NULL)
4974 {
4975 /* Can't replace termcodes - try using the string as is */
4976 rep_buf = vim_strsave(rep);
4977
4978 /* Give up if out of memory */
4979 if (rep_buf == NULL)
4980 return FAIL;
4981 }
4982
4983 /* get address of growarray: global or in curbuf */
4984 if (flags & UC_BUFFER)
4985 {
4986 gap = &curbuf->b_ucmds;
4987 if (gap->ga_itemsize == 0)
4988 ga_init2(gap, (int)sizeof(ucmd_T), 4);
4989 }
4990 else
4991 gap = &ucmds;
4992
4993 /* Search for the command in the already defined commands. */
4994 for (i = 0; i < gap->ga_len; ++i)
4995 {
4996 size_t len;
4997
4998 cmd = USER_CMD_GA(gap, i);
4999 len = STRLEN(cmd->uc_name);
5000 cmp = STRNCMP(name, cmd->uc_name, name_len);
5001 if (cmp == 0)
5002 {
5003 if (name_len < len)
5004 cmp = -1;
5005 else if (name_len > len)
5006 cmp = 1;
5007 }
5008
5009 if (cmp == 0)
5010 {
5011 if (!force)
5012 {
5013 EMSG(_("E174: Command already exists: add ! to replace it"));
5014 goto fail;
5015 }
5016
5017 vim_free(cmd->uc_rep);
5018 cmd->uc_rep = 0;
5019 break;
5020 }
5021
5022 /* Stop as soon as we pass the name to add */
5023 if (cmp < 0)
5024 break;
5025 }
5026
5027 /* Extend the array unless we're replacing an existing command */
5028 if (cmp != 0)
5029 {
5030 if (ga_grow(gap, 1) != OK)
5031 goto fail;
5032 if ((p = vim_strnsave(name, (int)name_len)) == NULL)
5033 goto fail;
5034
5035 cmd = USER_CMD_GA(gap, i);
5036 mch_memmove(cmd + 1, cmd, (gap->ga_len - i) * sizeof(ucmd_T));
5037
5038 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005039
5040 cmd->uc_name = p;
5041 }
5042
5043 cmd->uc_rep = rep_buf;
5044 cmd->uc_argt = argt;
5045 cmd->uc_def = def;
5046 cmd->uc_compl = compl;
5047#ifdef FEAT_EVAL
5048 cmd->uc_scriptID = current_SID;
5049# ifdef FEAT_CMDL_COMPL
5050 cmd->uc_compl_arg = compl_arg;
5051# endif
5052#endif
5053
5054 return OK;
5055
5056fail:
5057 vim_free(rep_buf);
5058#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5059 vim_free(compl_arg);
5060#endif
5061 return FAIL;
5062}
5063
5064/*
5065 * List of names for completion for ":command" with the EXPAND_ flag.
5066 * Must be alphabetical for completion.
5067 */
5068static struct
5069{
5070 int expand;
5071 char *name;
5072} command_complete[] =
5073{
5074 {EXPAND_AUGROUP, "augroup"},
5075 {EXPAND_BUFFERS, "buffer"},
5076 {EXPAND_COMMANDS, "command"},
5077#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5078 {EXPAND_USER_DEFINED, "custom"},
Bram Moolenaara466c992005-07-09 21:03:22 +00005079 {EXPAND_USER_LIST, "customlist"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005080#endif
5081 {EXPAND_DIRECTORIES, "dir"},
5082 {EXPAND_ENV_VARS, "environment"},
5083 {EXPAND_EVENTS, "event"},
5084 {EXPAND_EXPRESSION, "expression"},
5085 {EXPAND_FILES, "file"},
5086 {EXPAND_FUNCTIONS, "function"},
5087 {EXPAND_HELP, "help"},
5088 {EXPAND_HIGHLIGHT, "highlight"},
5089 {EXPAND_MAPPINGS, "mapping"},
5090 {EXPAND_MENUS, "menu"},
5091 {EXPAND_SETTINGS, "option"},
5092 {EXPAND_TAGS, "tag"},
5093 {EXPAND_TAGS_LISTFILES, "tag_listfiles"},
5094 {EXPAND_USER_VARS, "var"},
5095 {0, NULL}
5096};
5097
5098 static void
5099uc_list(name, name_len)
5100 char_u *name;
5101 size_t name_len;
5102{
5103 int i, j;
5104 int found = FALSE;
5105 ucmd_T *cmd;
5106 int len;
5107 long a;
5108 garray_T *gap;
5109
5110 gap = &curbuf->b_ucmds;
5111 for (;;)
5112 {
5113 for (i = 0; i < gap->ga_len; ++i)
5114 {
5115 cmd = USER_CMD_GA(gap, i);
5116 a = cmd->uc_argt;
5117
5118 /* Skip commands which don't match the requested prefix */
5119 if (STRNCMP(name, cmd->uc_name, name_len) != 0)
5120 continue;
5121
5122 /* Put out the title first time */
5123 if (!found)
5124 MSG_PUTS_TITLE(_("\n Name Args Range Complete Definition"));
5125 found = TRUE;
5126 msg_putchar('\n');
5127 if (got_int)
5128 break;
5129
5130 /* Special cases */
5131 msg_putchar(a & BANG ? '!' : ' ');
5132 msg_putchar(a & REGSTR ? '"' : ' ');
5133 msg_putchar(gap != &ucmds ? 'b' : ' ');
5134 msg_putchar(' ');
5135
5136 msg_outtrans_attr(cmd->uc_name, hl_attr(HLF_D));
5137 len = (int)STRLEN(cmd->uc_name) + 4;
5138
5139 do {
5140 msg_putchar(' ');
5141 ++len;
5142 } while (len < 16);
5143
5144 len = 0;
5145
5146 /* Arguments */
5147 switch ((int)(a & (EXTRA|NOSPC|NEEDARG)))
5148 {
5149 case 0: IObuff[len++] = '0'; break;
5150 case (EXTRA): IObuff[len++] = '*'; break;
5151 case (EXTRA|NOSPC): IObuff[len++] = '?'; break;
5152 case (EXTRA|NEEDARG): IObuff[len++] = '+'; break;
5153 case (EXTRA|NOSPC|NEEDARG): IObuff[len++] = '1'; break;
5154 }
5155
5156 do {
5157 IObuff[len++] = ' ';
5158 } while (len < 5);
5159
5160 /* Range */
5161 if (a & (RANGE|COUNT))
5162 {
5163 if (a & COUNT)
5164 {
5165 /* -count=N */
5166 sprintf((char *)IObuff + len, "%ldc", cmd->uc_def);
5167 len += (int)STRLEN(IObuff + len);
5168 }
5169 else if (a & DFLALL)
5170 IObuff[len++] = '%';
5171 else if (cmd->uc_def >= 0)
5172 {
5173 /* -range=N */
5174 sprintf((char *)IObuff + len, "%ld", cmd->uc_def);
5175 len += (int)STRLEN(IObuff + len);
5176 }
5177 else
5178 IObuff[len++] = '.';
5179 }
5180
5181 do {
5182 IObuff[len++] = ' ';
5183 } while (len < 11);
5184
5185 /* Completion */
5186 for (j = 0; command_complete[j].expand != 0; ++j)
5187 if (command_complete[j].expand == cmd->uc_compl)
5188 {
5189 STRCPY(IObuff + len, command_complete[j].name);
5190 len += (int)STRLEN(IObuff + len);
5191 break;
5192 }
5193
5194 do {
5195 IObuff[len++] = ' ';
5196 } while (len < 21);
5197
5198 IObuff[len] = '\0';
5199 msg_outtrans(IObuff);
5200
5201 msg_outtrans_special(cmd->uc_rep, FALSE);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005202#ifdef FEAT_EVAL
5203 if (p_verbose > 0)
5204 last_set_msg(cmd->uc_scriptID);
5205#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005206 out_flush();
5207 ui_breakcheck();
5208 if (got_int)
5209 break;
5210 }
5211 if (gap == &ucmds || i < gap->ga_len)
5212 break;
5213 gap = &ucmds;
5214 }
5215
5216 if (!found)
5217 MSG(_("No user-defined commands found"));
5218}
5219
5220 static char_u *
5221uc_fun_cmd()
5222{
5223 static char_u fcmd[] = {0x84, 0xaf, 0x60, 0xb9, 0xaf, 0xb5, 0x60, 0xa4,
5224 0xa5, 0xad, 0xa1, 0xae, 0xa4, 0x60, 0xa1, 0x60,
5225 0xb3, 0xa8, 0xb2, 0xb5, 0xa2, 0xa2, 0xa5, 0xb2,
5226 0xb9, 0x7f, 0};
5227 int i;
5228
5229 for (i = 0; fcmd[i]; ++i)
5230 IObuff[i] = fcmd[i] - 0x40;
5231 IObuff[i] = 0;
5232 return IObuff;
5233}
5234
5235 static int
5236uc_scan_attr(attr, len, argt, def, flags, compl, compl_arg)
5237 char_u *attr;
5238 size_t len;
5239 long *argt;
5240 long *def;
5241 int *flags;
5242 int *compl;
5243 char_u **compl_arg;
5244{
5245 char_u *p;
5246
5247 if (len == 0)
5248 {
5249 EMSG(_("E175: No attribute specified"));
5250 return FAIL;
5251 }
5252
5253 /* First, try the simple attributes (no arguments) */
5254 if (STRNICMP(attr, "bang", len) == 0)
5255 *argt |= BANG;
5256 else if (STRNICMP(attr, "buffer", len) == 0)
5257 *flags |= UC_BUFFER;
5258 else if (STRNICMP(attr, "register", len) == 0)
5259 *argt |= REGSTR;
5260 else if (STRNICMP(attr, "bar", len) == 0)
5261 *argt |= TRLBAR;
5262 else
5263 {
5264 int i;
5265 char_u *val = NULL;
5266 size_t vallen = 0;
5267 size_t attrlen = len;
5268
5269 /* Look for the attribute name - which is the part before any '=' */
5270 for (i = 0; i < (int)len; ++i)
5271 {
5272 if (attr[i] == '=')
5273 {
5274 val = &attr[i + 1];
5275 vallen = len - i - 1;
5276 attrlen = i;
5277 break;
5278 }
5279 }
5280
5281 if (STRNICMP(attr, "nargs", attrlen) == 0)
5282 {
5283 if (vallen == 1)
5284 {
5285 if (*val == '0')
5286 /* Do nothing - this is the default */;
5287 else if (*val == '1')
5288 *argt |= (EXTRA | NOSPC | NEEDARG);
5289 else if (*val == '*')
5290 *argt |= EXTRA;
5291 else if (*val == '?')
5292 *argt |= (EXTRA | NOSPC);
5293 else if (*val == '+')
5294 *argt |= (EXTRA | NEEDARG);
5295 else
5296 goto wrong_nargs;
5297 }
5298 else
5299 {
5300wrong_nargs:
5301 EMSG(_("E176: Invalid number of arguments"));
5302 return FAIL;
5303 }
5304 }
5305 else if (STRNICMP(attr, "range", attrlen) == 0)
5306 {
5307 *argt |= RANGE;
5308 if (vallen == 1 && *val == '%')
5309 *argt |= DFLALL;
5310 else if (val != NULL)
5311 {
5312 p = val;
5313 if (*def >= 0)
5314 {
5315two_count:
5316 EMSG(_("E177: Count cannot be specified twice"));
5317 return FAIL;
5318 }
5319
5320 *def = getdigits(&p);
5321 *argt |= (ZEROR | NOTADR);
5322
5323 if (p != val + vallen || vallen == 0)
5324 {
5325invalid_count:
5326 EMSG(_("E178: Invalid default value for count"));
5327 return FAIL;
5328 }
5329 }
5330 }
5331 else if (STRNICMP(attr, "count", attrlen) == 0)
5332 {
Bram Moolenaar32e7b2d2005-02-27 22:36:47 +00005333 *argt |= (COUNT | ZEROR | RANGE | NOTADR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005334
5335 if (val != NULL)
5336 {
5337 p = val;
5338 if (*def >= 0)
5339 goto two_count;
5340
5341 *def = getdigits(&p);
5342
5343 if (p != val + vallen)
5344 goto invalid_count;
5345 }
5346
5347 if (*def < 0)
5348 *def = 0;
5349 }
5350 else if (STRNICMP(attr, "complete", attrlen) == 0)
5351 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005352 if (val == NULL)
5353 {
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005354 EMSG(_("E179: argument required for -complete"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005355 return FAIL;
5356 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005357
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005358 if (parse_compl_arg(val, (int)vallen, compl, argt, compl_arg)
5359 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005360 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005361 }
5362 else
5363 {
5364 char_u ch = attr[len];
5365 attr[len] = '\0';
5366 EMSG2(_("E181: Invalid attribute: %s"), attr);
5367 attr[len] = ch;
5368 return FAIL;
5369 }
5370 }
5371
5372 return OK;
5373}
5374
5375 static void
5376ex_command(eap)
5377 exarg_T *eap;
5378{
5379 char_u *name;
5380 char_u *end;
5381 char_u *p;
5382 long argt = 0;
5383 long def = -1;
5384 int flags = 0;
5385 int compl = EXPAND_NOTHING;
5386 char_u *compl_arg = NULL;
5387 int has_attr = (eap->arg[0] == '-');
5388
5389 p = eap->arg;
5390
5391 /* Check for attributes */
5392 while (*p == '-')
5393 {
5394 ++p;
5395 end = skiptowhite(p);
5396 if (uc_scan_attr(p, end - p, &argt, &def, &flags, &compl, &compl_arg)
5397 == FAIL)
5398 return;
5399 p = skipwhite(end);
5400 }
5401
5402 /* Get the name (if any) and skip to the following argument */
5403 name = p;
5404 if (ASCII_ISALPHA(*p))
5405 while (ASCII_ISALNUM(*p))
5406 ++p;
5407 if (!ends_excmd(*p) && !vim_iswhite(*p))
5408 {
5409 EMSG(_("E182: Invalid command name"));
5410 return;
5411 }
5412 end = p;
5413
5414 /* If there is nothing after the name, and no attributes were specified,
5415 * we are listing commands
5416 */
5417 p = skipwhite(end);
5418 if (!has_attr && ends_excmd(*p))
5419 {
5420 uc_list(name, end - name);
5421 }
5422 else if (!ASCII_ISUPPER(*name))
5423 {
5424 EMSG(_("E183: User defined commands must start with an uppercase letter"));
5425 return;
5426 }
5427 else
5428 uc_add_command(name, end - name, p, argt, def, flags, compl, compl_arg,
5429 eap->forceit);
5430}
5431
5432/*
5433 * ":comclear"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005434 * Clear all user commands, global and for current buffer.
5435 */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00005436/*ARGSUSED*/
5437 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005438ex_comclear(eap)
5439 exarg_T *eap;
5440{
5441 uc_clear(&ucmds);
5442 uc_clear(&curbuf->b_ucmds);
5443}
5444
5445/*
5446 * Clear all user commands for "gap".
5447 */
5448 void
5449uc_clear(gap)
5450 garray_T *gap;
5451{
5452 int i;
5453 ucmd_T *cmd;
5454
5455 for (i = 0; i < gap->ga_len; ++i)
5456 {
5457 cmd = USER_CMD_GA(gap, i);
5458 vim_free(cmd->uc_name);
5459 vim_free(cmd->uc_rep);
5460# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5461 vim_free(cmd->uc_compl_arg);
5462# endif
5463 }
5464 ga_clear(gap);
5465}
5466
5467 static void
5468ex_delcommand(eap)
5469 exarg_T *eap;
5470{
5471 int i = 0;
5472 ucmd_T *cmd = NULL;
5473 int cmp = -1;
5474 garray_T *gap;
5475
5476 gap = &curbuf->b_ucmds;
5477 for (;;)
5478 {
5479 for (i = 0; i < gap->ga_len; ++i)
5480 {
5481 cmd = USER_CMD_GA(gap, i);
5482 cmp = STRCMP(eap->arg, cmd->uc_name);
5483 if (cmp <= 0)
5484 break;
5485 }
5486 if (gap == &ucmds || cmp == 0)
5487 break;
5488 gap = &ucmds;
5489 }
5490
5491 if (cmp != 0)
5492 {
5493 EMSG2(_("E184: No such user-defined command: %s"), eap->arg);
5494 return;
5495 }
5496
5497 vim_free(cmd->uc_name);
5498 vim_free(cmd->uc_rep);
5499# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5500 vim_free(cmd->uc_compl_arg);
5501# endif
5502
5503 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005504
5505 if (i < gap->ga_len)
5506 mch_memmove(cmd, cmd + 1, (gap->ga_len - i) * sizeof(ucmd_T));
5507}
5508
5509 static char_u *
5510uc_split_args(arg, lenp)
5511 char_u *arg;
5512 size_t *lenp;
5513{
5514 char_u *buf;
5515 char_u *p;
5516 char_u *q;
5517 int len;
5518
5519 /* Precalculate length */
5520 p = arg;
5521 len = 2; /* Initial and final quotes */
5522
5523 while (*p)
5524 {
5525 if (p[0] == '\\' && vim_iswhite(p[1]))
5526 {
5527 len += 1;
5528 p += 2;
5529 }
5530 else if (*p == '\\' || *p == '"')
5531 {
5532 len += 2;
5533 p += 1;
5534 }
5535 else if (vim_iswhite(*p))
5536 {
5537 p = skipwhite(p);
5538 if (*p == NUL)
5539 break;
5540 len += 3; /* "," */
5541 }
5542 else
5543 {
5544 ++len;
5545 ++p;
5546 }
5547 }
5548
5549 buf = alloc(len + 1);
5550 if (buf == NULL)
5551 {
5552 *lenp = 0;
5553 return buf;
5554 }
5555
5556 p = arg;
5557 q = buf;
5558 *q++ = '"';
5559 while (*p)
5560 {
5561 if (p[0] == '\\' && vim_iswhite(p[1]))
5562 {
5563 *q++ = p[1];
5564 p += 2;
5565 }
5566 else if (*p == '\\' || *p == '"')
5567 {
5568 *q++ = '\\';
5569 *q++ = *p++;
5570 }
5571 else if (vim_iswhite(*p))
5572 {
5573 p = skipwhite(p);
5574 if (*p == NUL)
5575 break;
5576 *q++ = '"';
5577 *q++ = ',';
5578 *q++ = '"';
5579 }
5580 else
5581 {
5582 *q++ = *p++;
5583 }
5584 }
5585 *q++ = '"';
5586 *q = 0;
5587
5588 *lenp = len;
5589 return buf;
5590}
5591
5592/*
5593 * Check for a <> code in a user command.
5594 * "code" points to the '<'. "len" the length of the <> (inclusive).
5595 * "buf" is where the result is to be added.
5596 * "split_buf" points to a buffer used for splitting, caller should free it.
5597 * "split_len" is the length of what "split_buf" contains.
5598 * Returns the length of the replacement, which has been added to "buf".
5599 * Returns -1 if there was no match, and only the "<" has been copied.
5600 */
5601 static size_t
5602uc_check_code(code, len, buf, cmd, eap, split_buf, split_len)
5603 char_u *code;
5604 size_t len;
5605 char_u *buf;
5606 ucmd_T *cmd; /* the user command we're expanding */
5607 exarg_T *eap; /* ex arguments */
5608 char_u **split_buf;
5609 size_t *split_len;
5610{
5611 size_t result = 0;
5612 char_u *p = code + 1;
5613 size_t l = len - 2;
5614 int quote = 0;
5615 enum { ct_ARGS, ct_BANG, ct_COUNT, ct_LINE1, ct_LINE2, ct_REGISTER,
5616 ct_LT, ct_NONE } type = ct_NONE;
5617
5618 if ((vim_strchr((char_u *)"qQfF", *p) != NULL) && p[1] == '-')
5619 {
5620 quote = (*p == 'q' || *p == 'Q') ? 1 : 2;
5621 p += 2;
5622 l -= 2;
5623 }
5624
5625 if (l < 1)
5626 type = ct_NONE;
5627 else if (STRNICMP(p, "args", l) == 0)
5628 type = ct_ARGS;
5629 else if (STRNICMP(p, "bang", l) == 0)
5630 type = ct_BANG;
5631 else if (STRNICMP(p, "count", l) == 0)
5632 type = ct_COUNT;
5633 else if (STRNICMP(p, "line1", l) == 0)
5634 type = ct_LINE1;
5635 else if (STRNICMP(p, "line2", l) == 0)
5636 type = ct_LINE2;
5637 else if (STRNICMP(p, "lt", l) == 0)
5638 type = ct_LT;
5639 else if (STRNICMP(p, "register", l) == 0)
5640 type = ct_REGISTER;
5641
5642 switch (type)
5643 {
5644 case ct_ARGS:
5645 /* Simple case first */
5646 if (*eap->arg == NUL)
5647 {
5648 if (quote == 1)
5649 {
5650 result = 2;
5651 if (buf != NULL)
5652 STRCPY(buf, "''");
5653 }
5654 else
5655 result = 0;
5656 break;
5657 }
5658
5659 /* When specified there is a single argument don't split it.
5660 * Works for ":Cmd %" when % is "a b c". */
5661 if ((eap->argt & NOSPC) && quote == 2)
5662 quote = 1;
5663
5664 switch (quote)
5665 {
5666 case 0: /* No quoting, no splitting */
5667 result = STRLEN(eap->arg);
5668 if (buf != NULL)
5669 STRCPY(buf, eap->arg);
5670 break;
5671 case 1: /* Quote, but don't split */
5672 result = STRLEN(eap->arg) + 2;
5673 for (p = eap->arg; *p; ++p)
5674 {
5675 if (*p == '\\' || *p == '"')
5676 ++result;
5677 }
5678
5679 if (buf != NULL)
5680 {
5681 *buf++ = '"';
5682 for (p = eap->arg; *p; ++p)
5683 {
5684 if (*p == '\\' || *p == '"')
5685 *buf++ = '\\';
5686 *buf++ = *p;
5687 }
5688 *buf = '"';
5689 }
5690
5691 break;
5692 case 2: /* Quote and split */
5693 /* This is hard, so only do it once, and cache the result */
5694 if (*split_buf == NULL)
5695 *split_buf = uc_split_args(eap->arg, split_len);
5696
5697 result = *split_len;
5698 if (buf != NULL && result != 0)
5699 STRCPY(buf, *split_buf);
5700
5701 break;
5702 }
5703 break;
5704
5705 case ct_BANG:
5706 result = eap->forceit ? 1 : 0;
5707 if (quote)
5708 result += 2;
5709 if (buf != NULL)
5710 {
5711 if (quote)
5712 *buf++ = '"';
5713 if (eap->forceit)
5714 *buf++ = '!';
5715 if (quote)
5716 *buf = '"';
5717 }
5718 break;
5719
5720 case ct_LINE1:
5721 case ct_LINE2:
5722 case ct_COUNT:
5723 {
5724 char num_buf[20];
5725 long num = (type == ct_LINE1) ? eap->line1 :
5726 (type == ct_LINE2) ? eap->line2 :
5727 (eap->addr_count > 0) ? eap->line2 : cmd->uc_def;
5728 size_t num_len;
5729
5730 sprintf(num_buf, "%ld", num);
5731 num_len = STRLEN(num_buf);
5732 result = num_len;
5733
5734 if (quote)
5735 result += 2;
5736
5737 if (buf != NULL)
5738 {
5739 if (quote)
5740 *buf++ = '"';
5741 STRCPY(buf, num_buf);
5742 buf += num_len;
5743 if (quote)
5744 *buf = '"';
5745 }
5746
5747 break;
5748 }
5749
5750 case ct_REGISTER:
5751 result = eap->regname ? 1 : 0;
5752 if (quote)
5753 result += 2;
5754 if (buf != NULL)
5755 {
5756 if (quote)
5757 *buf++ = '\'';
5758 if (eap->regname)
5759 *buf++ = eap->regname;
5760 if (quote)
5761 *buf = '\'';
5762 }
5763 break;
5764
5765 case ct_LT:
5766 result = 1;
5767 if (buf != NULL)
5768 *buf = '<';
5769 break;
5770
5771 default:
5772 /* Not recognized: just copy the '<' and return -1. */
5773 result = (size_t)-1;
5774 if (buf != NULL)
5775 *buf = '<';
5776 break;
5777 }
5778
5779 return result;
5780}
5781
5782 static void
5783do_ucmd(eap)
5784 exarg_T *eap;
5785{
5786 char_u *buf;
5787 char_u *p;
5788 char_u *q;
5789
5790 char_u *start;
5791 char_u *end;
5792 size_t len, totlen;
5793
5794 size_t split_len = 0;
5795 char_u *split_buf = NULL;
5796 ucmd_T *cmd;
5797#ifdef FEAT_EVAL
5798 scid_T save_current_SID = current_SID;
5799#endif
5800
5801 if (eap->cmdidx == CMD_USER)
5802 cmd = USER_CMD(eap->useridx);
5803 else
5804 cmd = USER_CMD_GA(&curbuf->b_ucmds, eap->useridx);
5805
5806 /*
5807 * Replace <> in the command by the arguments.
5808 */
5809 buf = NULL;
5810 for (;;)
5811 {
5812 p = cmd->uc_rep;
5813 q = buf;
5814 totlen = 0;
5815 while ((start = vim_strchr(p, '<')) != NULL
5816 && (end = vim_strchr(start + 1, '>')) != NULL)
5817 {
5818 /* Include the '>' */
5819 ++end;
5820
5821 /* Take everything up to the '<' */
5822 len = start - p;
5823 if (buf == NULL)
5824 totlen += len;
5825 else
5826 {
5827 mch_memmove(q, p, len);
5828 q += len;
5829 }
5830
5831 len = uc_check_code(start, end - start, q, cmd, eap,
5832 &split_buf, &split_len);
5833 if (len == (size_t)-1)
5834 {
5835 /* no match, continue after '<' */
5836 p = start + 1;
5837 len = 1;
5838 }
5839 else
5840 p = end;
5841 if (buf == NULL)
5842 totlen += len;
5843 else
5844 q += len;
5845 }
5846 if (buf != NULL) /* second time here, finished */
5847 {
5848 STRCPY(q, p);
5849 break;
5850 }
5851
5852 totlen += STRLEN(p); /* Add on the trailing characters */
5853 buf = alloc((unsigned)(totlen + 1));
5854 if (buf == NULL)
5855 {
5856 vim_free(split_buf);
5857 return;
5858 }
5859 }
5860
5861#ifdef FEAT_EVAL
5862 current_SID = cmd->uc_scriptID;
5863#endif
5864 (void)do_cmdline(buf, eap->getline, eap->cookie,
5865 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
5866#ifdef FEAT_EVAL
5867 current_SID = save_current_SID;
5868#endif
5869 vim_free(buf);
5870 vim_free(split_buf);
5871}
5872
5873# if defined(FEAT_CMDL_COMPL) || defined(PROTO)
5874 static char_u *
5875get_user_command_name(idx)
5876 int idx;
5877{
5878 return get_user_commands(NULL, idx - (int)CMD_SIZE);
5879}
5880
5881/*
5882 * Function given to ExpandGeneric() to obtain the list of user command names.
5883 */
5884/*ARGSUSED*/
5885 char_u *
5886get_user_commands(xp, idx)
5887 expand_T *xp;
5888 int idx;
5889{
5890 if (idx < curbuf->b_ucmds.ga_len)
5891 return USER_CMD_GA(&curbuf->b_ucmds, idx)->uc_name;
5892 idx -= curbuf->b_ucmds.ga_len;
5893 if (idx < ucmds.ga_len)
5894 return USER_CMD(idx)->uc_name;
5895 return NULL;
5896}
5897
5898/*
5899 * Function given to ExpandGeneric() to obtain the list of user command
5900 * attributes.
5901 */
5902/*ARGSUSED*/
5903 char_u *
5904get_user_cmd_flags(xp, idx)
5905 expand_T *xp;
5906 int idx;
5907{
5908 static char *user_cmd_flags[] =
5909 {"bang", "bar", "buffer", "complete", "count",
5910 "nargs", "range", "register"};
5911
5912 if (idx >= sizeof(user_cmd_flags) / sizeof(user_cmd_flags[0]))
5913 return NULL;
5914 return (char_u *)user_cmd_flags[idx];
5915}
5916
5917/*
5918 * Function given to ExpandGeneric() to obtain the list of values for -nargs.
5919 */
5920/*ARGSUSED*/
5921 char_u *
5922get_user_cmd_nargs(xp, idx)
5923 expand_T *xp;
5924 int idx;
5925{
5926 static char *user_cmd_nargs[] = {"0", "1", "*", "?", "+"};
5927
5928 if (idx >= sizeof(user_cmd_nargs) / sizeof(user_cmd_nargs[0]))
5929 return NULL;
5930 return (char_u *)user_cmd_nargs[idx];
5931}
5932
5933/*
5934 * Function given to ExpandGeneric() to obtain the list of values for -complete.
5935 */
5936/*ARGSUSED*/
5937 char_u *
5938get_user_cmd_complete(xp, idx)
5939 expand_T *xp;
5940 int idx;
5941{
5942 return (char_u *)command_complete[idx].name;
5943}
5944# endif /* FEAT_CMDL_COMPL */
5945
5946#endif /* FEAT_USR_CMDS */
5947
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005948#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
5949/*
5950 * Parse a completion argument "value[vallen]".
5951 * The detected completion goes in "*complp", argument type in "*argt".
5952 * When there is an argument, for function and user defined completion, it's
5953 * copied to allocated memory and stored in "*compl_arg".
5954 * Returns FAIL if something is wrong.
5955 */
5956 int
5957parse_compl_arg(value, vallen, complp, argt, compl_arg)
5958 char_u *value;
5959 int vallen;
5960 int *complp;
5961 long *argt;
5962 char_u **compl_arg;
5963{
5964 char_u *arg = NULL;
5965 size_t arglen = 0;
5966 int i;
5967 int valend = vallen;
5968
5969 /* Look for any argument part - which is the part after any ',' */
5970 for (i = 0; i < vallen; ++i)
5971 {
5972 if (value[i] == ',')
5973 {
5974 arg = &value[i + 1];
5975 arglen = vallen - i - 1;
5976 valend = i;
5977 break;
5978 }
5979 }
5980
5981 for (i = 0; command_complete[i].expand != 0; ++i)
5982 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005983 if ((int)STRLEN(command_complete[i].name) == valend
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005984 && STRNCMP(value, command_complete[i].name, valend) == 0)
5985 {
5986 *complp = command_complete[i].expand;
5987 if (command_complete[i].expand == EXPAND_BUFFERS)
5988 *argt |= BUFNAME;
5989 else if (command_complete[i].expand == EXPAND_DIRECTORIES
5990 || command_complete[i].expand == EXPAND_FILES)
5991 *argt |= XFILE;
5992 break;
5993 }
5994 }
5995
5996 if (command_complete[i].expand == 0)
5997 {
5998 EMSG2(_("E180: Invalid complete value: %s"), value);
5999 return FAIL;
6000 }
6001
6002# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6003 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST
6004 && arg != NULL)
6005# else
6006 if (arg != NULL)
6007# endif
6008 {
6009 EMSG(_("E468: Completion argument only allowed for custom completion"));
6010 return FAIL;
6011 }
6012
6013# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6014 if ((*complp == EXPAND_USER_DEFINED || *complp == EXPAND_USER_LIST)
6015 && arg == NULL)
6016 {
6017 EMSG(_("E467: Custom completion requires a function argument"));
6018 return FAIL;
6019 }
6020
6021 if (arg != NULL)
6022 *compl_arg = vim_strnsave(arg, (int)arglen);
6023# endif
6024 return OK;
6025}
6026#endif
6027
Bram Moolenaar071d4272004-06-13 20:20:40 +00006028 static void
6029ex_colorscheme(eap)
6030 exarg_T *eap;
6031{
6032 if (load_colors(eap->arg) == FAIL)
6033 EMSG2(_("E185: Cannot find color scheme %s"), eap->arg);
6034}
6035
6036 static void
6037ex_highlight(eap)
6038 exarg_T *eap;
6039{
6040 if (*eap->arg == NUL && eap->cmd[2] == '!')
6041 MSG(_("Greetings, Vim user!"));
6042 do_highlight(eap->arg, eap->forceit, FALSE);
6043}
6044
6045
6046/*
6047 * Call this function if we thought we were going to exit, but we won't
6048 * (because of an error). May need to restore the terminal mode.
6049 */
6050 void
6051not_exiting()
6052{
6053 exiting = FALSE;
6054 settmode(TMODE_RAW);
6055}
6056
6057/*
6058 * ":quit": quit current window, quit Vim if closed the last window.
6059 */
6060 static void
6061ex_quit(eap)
6062 exarg_T *eap;
6063{
6064#ifdef FEAT_CMDWIN
6065 if (cmdwin_type != 0)
6066 {
6067 cmdwin_result = Ctrl_C;
6068 return;
6069 }
6070#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006071 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006072 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006073 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006074 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006075 return;
6076 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006077
6078#ifdef FEAT_NETBEANS_INTG
6079 netbeansForcedQuit = eap->forceit;
6080#endif
6081
6082 /*
6083 * If there are more files or windows we won't exit.
6084 */
6085 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6086 exiting = TRUE;
6087 if ((!P_HID(curbuf)
6088 && check_changed(curbuf, p_awa, FALSE, eap->forceit, FALSE))
6089 || check_more(TRUE, eap->forceit) == FAIL
6090 || (only_one_window() && check_changed_any(eap->forceit)))
6091 {
6092 not_exiting();
6093 }
6094 else
6095 {
6096#ifdef FEAT_WINDOWS
6097 if (only_one_window()) /* quit last window */
6098#endif
6099 getout(0);
6100#ifdef FEAT_WINDOWS
6101# ifdef FEAT_GUI
6102 need_mouse_correct = TRUE;
6103# endif
6104 /* close window; may free buffer */
6105 win_close(curwin, !P_HID(curwin->w_buffer) || eap->forceit);
6106#endif
6107 }
6108}
6109
6110/*
6111 * ":cquit".
6112 */
6113/*ARGSUSED*/
6114 static void
6115ex_cquit(eap)
6116 exarg_T *eap;
6117{
6118 getout(1); /* this does not always pass on the exit code to the Manx
6119 compiler. why? */
6120}
6121
6122/*
6123 * ":qall": try to quit all windows
6124 */
6125 static void
6126ex_quit_all(eap)
6127 exarg_T *eap;
6128{
6129# ifdef FEAT_CMDWIN
6130 if (cmdwin_type != 0)
6131 {
6132 if (eap->forceit)
6133 cmdwin_result = K_XF1; /* ex_window() takes care of this */
6134 else
6135 cmdwin_result = K_XF2;
6136 return;
6137 }
6138# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006139
6140 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006141 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006142 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006143 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006144 return;
6145 }
6146
Bram Moolenaar071d4272004-06-13 20:20:40 +00006147 exiting = TRUE;
6148 if (eap->forceit || !check_changed_any(FALSE))
6149 getout(0);
6150 not_exiting();
6151}
6152
6153#ifdef FEAT_WINDOWS
6154/*
6155 * ":close": close current window, unless it is the last one
6156 */
6157 static void
6158ex_close(eap)
6159 exarg_T *eap;
6160{
6161# ifdef FEAT_CMDWIN
6162 if (cmdwin_type != 0)
6163 cmdwin_result = K_IGNORE;
6164 else
6165# endif
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006166 if (!text_locked())
Bram Moolenaarf740b292006-02-16 22:11:02 +00006167 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006168}
6169
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006170#ifdef FEAT_QUICKFIX
6171/*
6172 * ":pclose": Close any preview window.
6173 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006174 static void
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006175ex_pclose(eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006176 exarg_T *eap;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006177{
6178 win_T *win;
6179
6180 for (win = firstwin; win != NULL; win = win->w_next)
6181 if (win->w_p_pvw)
6182 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006183 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006184 break;
6185 }
6186}
6187#endif
6188
Bram Moolenaarf740b292006-02-16 22:11:02 +00006189/*
6190 * Close window "win" and take care of handling closing the last window for a
6191 * modified buffer.
6192 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006193 static void
Bram Moolenaarf740b292006-02-16 22:11:02 +00006194ex_win_close(forceit, win, tp)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006195 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006196 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006197 tabpage_T *tp; /* NULL or the tab page "win" is in */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006198{
6199 int need_hide;
6200 buf_T *buf = win->w_buffer;
6201
6202 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006203 if (need_hide && !P_HID(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006204 {
6205#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
6206 if ((p_confirm || cmdmod.confirm) && p_write)
6207 {
6208 dialog_changed(buf, FALSE);
6209 if (buf_valid(buf) && bufIsChanged(buf))
6210 return;
6211 need_hide = FALSE;
6212 }
6213 else
6214#endif
6215 {
6216 EMSG(_(e_nowrtmsg));
6217 return;
6218 }
6219 }
6220
6221#ifdef FEAT_GUI
6222 need_mouse_correct = TRUE;
6223#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006224
Bram Moolenaar071d4272004-06-13 20:20:40 +00006225 /* free buffer when not hiding it or when it's a scratch buffer */
Bram Moolenaarf740b292006-02-16 22:11:02 +00006226 if (tp == NULL)
6227 win_close(win, !need_hide && !P_HID(buf));
6228 else
6229 win_close_othertab(win, !need_hide && !P_HID(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006230}
6231
Bram Moolenaar071d4272004-06-13 20:20:40 +00006232/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006233 * ":tabclose": close current tab page, unless it is the last one.
6234 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006235 */
6236 static void
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006237ex_tabclose(eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006238 exarg_T *eap;
6239{
Bram Moolenaarf740b292006-02-16 22:11:02 +00006240 tabpage_T *tp;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006241 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006242
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006243# ifdef FEAT_CMDWIN
6244 if (cmdwin_type != 0)
6245 cmdwin_result = K_IGNORE;
6246 else
6247# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006248 if (first_tabpage->tp_next == NULL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00006249 EMSG(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00006250 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006251 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006252 if (eap->addr_count > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006253 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006254 tp = find_tabpage((int)eap->line2);
6255 if (tp == NULL)
6256 {
6257 beep_flush();
6258 return;
6259 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00006260 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006261 {
6262 tabpage_close_other(tp, eap->forceit);
6263 return;
6264 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006265 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00006266 if (!text_locked())
6267 tabpage_close(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006268 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006269
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006270 if (h != tabline_height())
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006271 shell_new_rows();
6272}
6273
6274/*
6275 * ":tabonly": close all tab pages except the current one
6276 */
6277 static void
6278ex_tabonly(eap)
6279 exarg_T *eap;
6280{
6281 tabpage_T *tp;
6282 int done;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006283 int h = tabline_height();
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006284
6285# ifdef FEAT_CMDWIN
6286 if (cmdwin_type != 0)
6287 cmdwin_result = K_IGNORE;
6288 else
6289# endif
6290 if (first_tabpage->tp_next == NULL)
6291 MSG(_("Already only one tab page"));
6292 else
6293 {
6294 /* Repeat this up to a 1000 times, because autocommands may mess
6295 * up the lists. */
6296 for (done = 0; done < 1000; ++done)
6297 {
6298 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6299 if (tp->tp_topframe != topframe)
6300 {
6301 tabpage_close_other(tp, eap->forceit);
6302 /* if we failed to close it quit */
6303 if (valid_tabpage(tp))
6304 done = 1000;
6305 /* start over, "tp" is now invalid */
6306 break;
6307 }
6308 if (first_tabpage->tp_next == NULL)
6309 break;
6310 }
6311 }
6312
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006313 if (h != tabline_height())
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006314 shell_new_rows();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006315}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006316
6317/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006318 * Close the current tab page.
6319 */
6320 void
6321tabpage_close(forceit)
6322 int forceit;
6323{
6324 /* First close all the windows but the current one. If that worked then
6325 * close the last window in this tab, that will close it. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006326 if (lastwin != firstwin)
6327 close_others(TRUE, forceit);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006328 if (lastwin == firstwin)
6329 ex_win_close(forceit, curwin, NULL);
6330# ifdef FEAT_GUI
6331 need_mouse_correct = TRUE;
6332# endif
6333}
6334
6335/*
6336 * Close tab page "tp", which is not the current tab page.
6337 * Note that autocommands may make "tp" invalid.
6338 */
6339 void
6340tabpage_close_other(tp, forceit)
6341 tabpage_T *tp;
6342 int forceit;
6343{
6344 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006345 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006346
6347 /* Limit to 1000 windows, autocommands may add a window while we close
6348 * one. OK, so I'm paranoid... */
6349 while (++done < 1000)
6350 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006351 wp = tp->tp_firstwin;
6352 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006353
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006354 /* Autocommands may delete the tab page under our fingers and we may
6355 * fail to close a window with a modified buffer. */
6356 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006357 break;
6358 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006359 redraw_tabline = TRUE;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006360}
6361
6362/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006363 * ":only".
6364 */
6365 static void
6366ex_only(eap)
6367 exarg_T *eap;
6368{
6369# ifdef FEAT_GUI
6370 need_mouse_correct = TRUE;
6371# endif
6372 close_others(TRUE, eap->forceit);
6373}
6374
6375/*
6376 * ":all" and ":sall".
6377 */
6378 static void
6379ex_all(eap)
6380 exarg_T *eap;
6381{
6382 if (eap->addr_count == 0)
6383 eap->line2 = 9999;
6384 do_arg_all((int)eap->line2, eap->forceit);
6385}
6386#endif /* FEAT_WINDOWS */
6387
6388 static void
6389ex_hide(eap)
6390 exarg_T *eap;
6391{
6392 if (*eap->arg != NUL && check_nextcmd(eap->arg) == NULL)
6393 eap->errmsg = e_invarg;
6394 else
6395 {
6396 /* ":hide" or ":hide | cmd": hide current window */
6397 eap->nextcmd = check_nextcmd(eap->arg);
6398#ifdef FEAT_WINDOWS
6399 if (!eap->skip)
6400 {
6401# ifdef FEAT_GUI
6402 need_mouse_correct = TRUE;
6403# endif
6404 win_close(curwin, FALSE); /* don't free buffer */
6405 }
6406#endif
6407 }
6408}
6409
6410/*
6411 * ":stop" and ":suspend": Suspend Vim.
6412 */
6413 static void
6414ex_stop(eap)
6415 exarg_T *eap;
6416{
6417 /*
6418 * Disallow suspending for "rvim".
6419 */
6420 if (!check_restricted()
6421#ifdef WIN3264
6422 /*
6423 * Check if external commands are allowed now.
6424 */
6425 && can_end_termcap_mode(TRUE)
6426#endif
6427 )
6428 {
6429 if (!eap->forceit)
6430 autowrite_all();
6431 windgoto((int)Rows - 1, 0);
6432 out_char('\n');
6433 out_flush();
6434 stoptermcap();
6435 out_flush(); /* needed for SUN to restore xterm buffer */
6436#ifdef FEAT_TITLE
6437 mch_restore_title(3); /* restore window titles */
6438#endif
6439 ui_suspend(); /* call machine specific function */
6440#ifdef FEAT_TITLE
6441 maketitle();
6442 resettitle(); /* force updating the title */
6443#endif
6444 starttermcap();
6445 scroll_start(); /* scroll screen before redrawing */
6446 redraw_later_clear();
6447 shell_resized(); /* may have resized window */
6448 }
6449}
6450
6451/*
6452 * ":exit", ":xit" and ":wq": Write file and exit Vim.
6453 */
6454 static void
6455ex_exit(eap)
6456 exarg_T *eap;
6457{
6458#ifdef FEAT_CMDWIN
6459 if (cmdwin_type != 0)
6460 {
6461 cmdwin_result = Ctrl_C;
6462 return;
6463 }
6464#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006465 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006466 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006467 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006468 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006469 return;
6470 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006471
6472 /*
6473 * if more files or windows we won't exit
6474 */
6475 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6476 exiting = TRUE;
6477 if ( ((eap->cmdidx == CMD_wq
6478 || curbufIsChanged())
6479 && do_write(eap) == FAIL)
6480 || check_more(TRUE, eap->forceit) == FAIL
6481 || (only_one_window() && check_changed_any(eap->forceit)))
6482 {
6483 not_exiting();
6484 }
6485 else
6486 {
6487#ifdef FEAT_WINDOWS
6488 if (only_one_window()) /* quit last window, exit Vim */
6489#endif
6490 getout(0);
6491#ifdef FEAT_WINDOWS
6492# ifdef FEAT_GUI
6493 need_mouse_correct = TRUE;
6494# endif
6495 /* quit current window, may free buffer */
6496 win_close(curwin, !P_HID(curwin->w_buffer));
6497#endif
6498 }
6499}
6500
6501/*
6502 * ":print", ":list", ":number".
6503 */
6504 static void
6505ex_print(eap)
6506 exarg_T *eap;
6507{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006508 if (curbuf->b_ml.ml_flags & ML_EMPTY)
6509 EMSG(_(e_emptybuf));
6510 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006511 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00006512 for ( ;!got_int; ui_breakcheck())
6513 {
6514 print_line(eap->line1,
6515 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
6516 || (eap->flags & EXFLAG_NR)),
6517 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
6518 if (++eap->line1 > eap->line2)
6519 break;
6520 out_flush(); /* show one line at a time */
6521 }
6522 setpcmark();
6523 /* put cursor at last line */
6524 curwin->w_cursor.lnum = eap->line2;
6525 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006526 }
6527
Bram Moolenaar071d4272004-06-13 20:20:40 +00006528 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006529}
6530
6531#ifdef FEAT_BYTEOFF
6532 static void
6533ex_goto(eap)
6534 exarg_T *eap;
6535{
6536 goto_byte(eap->line2);
6537}
6538#endif
6539
6540/*
6541 * ":shell".
6542 */
6543/*ARGSUSED*/
6544 static void
6545ex_shell(eap)
6546 exarg_T *eap;
6547{
6548 do_shell(NULL, 0);
6549}
6550
6551#if (defined(FEAT_WINDOWS) && defined(HAVE_DROP_FILE)) \
6552 || (defined(FEAT_GUI_GTK) && defined(FEAT_DND)) \
Bram Moolenaar371baa92005-12-29 22:43:53 +00006553 || defined(FEAT_GUI_MSWIN) \
6554 || defined(FEAT_GUI_MAC) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00006555 || defined(PROTO)
6556
6557/*
6558 * Handle a file drop. The code is here because a drop is *nearly* like an
6559 * :args command, but not quite (we have a list of exact filenames, so we
6560 * don't want to (a) parse a command line, or (b) expand wildcards. So the
6561 * code is very similar to :args and hence needs access to a lot of the static
6562 * functions in this file.
6563 *
6564 * The list should be allocated using alloc(), as should each item in the
6565 * list. This function takes over responsibility for freeing the list.
6566 *
6567 * XXX The list is made into the arggument list. This is freed using
6568 * FreeWild(), which does a series of vim_free() calls, unless the two defines
6569 * __EMX__ and __ALWAYS_HAS_TRAILING_NUL_POINTER are set. In this case, a
6570 * routine _fnexplodefree() is used. This may cause problems, but as the drop
6571 * file functionality is (currently) not in EMX this is not presently a
6572 * problem.
6573 */
6574 void
6575handle_drop(filec, filev, split)
6576 int filec; /* the number of files dropped */
6577 char_u **filev; /* the list of files dropped */
6578 int split; /* force splitting the window */
6579{
6580 exarg_T ea;
6581 int save_msg_scroll = msg_scroll;
6582
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006583 /* Postpone this while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006584 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006585 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006586
6587 /* Check whether the current buffer is changed. If so, we will need
6588 * to split the current window or data could be lost.
6589 * We don't need to check if the 'hidden' option is set, as in this
6590 * case the buffer won't be lost.
6591 */
6592 if (!P_HID(curbuf) && !split)
6593 {
6594 ++emsg_off;
6595 split = check_changed(curbuf, TRUE, FALSE, FALSE, FALSE);
6596 --emsg_off;
6597 }
6598 if (split)
6599 {
6600# ifdef FEAT_WINDOWS
6601 if (win_split(0, 0) == FAIL)
6602 return;
6603# ifdef FEAT_SCROLLBIND
6604 curwin->w_p_scb = FALSE;
6605# endif
6606
6607 /* When splitting the window, create a new alist. Otherwise the
6608 * existing one is overwritten. */
6609 alist_unlink(curwin->w_alist);
6610 alist_new();
6611# else
6612 return; /* can't split, always fail */
6613# endif
6614 }
6615
6616 /*
6617 * Set up the new argument list.
6618 */
Bram Moolenaar86b68352004-12-27 21:59:20 +00006619 alist_set(ALIST(curwin), filec, filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006620
6621 /*
6622 * Move to the first file.
6623 */
6624 /* Fake up a minimal "next" command for do_argfile() */
6625 vim_memset(&ea, 0, sizeof(ea));
6626 ea.cmd = (char_u *)"next";
6627 do_argfile(&ea, 0);
6628
6629 /* do_ecmd() may set need_start_insertmode, but since we never left Insert
6630 * mode that is not needed here. */
6631 need_start_insertmode = FALSE;
6632
6633 /* Restore msg_scroll, otherwise a following command may cause scrolling
6634 * unexpectedly. The screen will be redrawn by the caller, thus
6635 * msg_scroll being set by displaying a message is irrelevant. */
6636 msg_scroll = save_msg_scroll;
6637}
6638#endif
6639
Bram Moolenaar071d4272004-06-13 20:20:40 +00006640/*
6641 * Clear an argument list: free all file names and reset it to zero entries.
6642 */
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00006643 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00006644alist_clear(al)
6645 alist_T *al;
6646{
6647 while (--al->al_ga.ga_len >= 0)
6648 vim_free(AARGLIST(al)[al->al_ga.ga_len].ae_fname);
6649 ga_clear(&al->al_ga);
6650}
6651
6652/*
6653 * Init an argument list.
6654 */
6655 void
6656alist_init(al)
6657 alist_T *al;
6658{
6659 ga_init2(&al->al_ga, (int)sizeof(aentry_T), 5);
6660}
6661
6662#if defined(FEAT_WINDOWS) || defined(PROTO)
6663
6664/*
6665 * Remove a reference from an argument list.
6666 * Ignored when the argument list is the global one.
6667 * If the argument list is no longer used by any window, free it.
6668 */
6669 void
6670alist_unlink(al)
6671 alist_T *al;
6672{
6673 if (al != &global_alist && --al->al_refcount <= 0)
6674 {
6675 alist_clear(al);
6676 vim_free(al);
6677 }
6678}
6679
6680# if defined(FEAT_LISTCMDS) || defined(HAVE_DROP_FILE) || defined(PROTO)
6681/*
6682 * Create a new argument list and use it for the current window.
6683 */
6684 void
6685alist_new()
6686{
6687 curwin->w_alist = (alist_T *)alloc((unsigned)sizeof(alist_T));
6688 if (curwin->w_alist == NULL)
6689 {
6690 curwin->w_alist = &global_alist;
6691 ++global_alist.al_refcount;
6692 }
6693 else
6694 {
6695 curwin->w_alist->al_refcount = 1;
6696 alist_init(curwin->w_alist);
6697 }
6698}
6699# endif
6700#endif
6701
6702#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE) || defined(PROTO)
6703/*
6704 * Expand the file names in the global argument list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00006705 * If "fnum_list" is not NULL, use "fnum_list[fnum_len]" as a list of buffer
6706 * numbers to be re-used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006707 */
6708 void
Bram Moolenaar86b68352004-12-27 21:59:20 +00006709alist_expand(fnum_list, fnum_len)
6710 int *fnum_list;
6711 int fnum_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006712{
6713 char_u **old_arg_files;
Bram Moolenaar86b68352004-12-27 21:59:20 +00006714 int old_arg_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006715 char_u **new_arg_files;
6716 int new_arg_file_count;
6717 char_u *save_p_su = p_su;
6718 int i;
6719
6720 /* Don't use 'suffixes' here. This should work like the shell did the
6721 * expansion. Also, the vimrc file isn't read yet, thus the user
6722 * can't set the options. */
6723 p_su = empty_option;
6724 old_arg_files = (char_u **)alloc((unsigned)(sizeof(char_u *) * GARGCOUNT));
6725 if (old_arg_files != NULL)
6726 {
6727 for (i = 0; i < GARGCOUNT; ++i)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006728 old_arg_files[i] = vim_strsave(GARGLIST[i].ae_fname);
6729 old_arg_count = GARGCOUNT;
6730 if (expand_wildcards(old_arg_count, old_arg_files,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006731 &new_arg_file_count, &new_arg_files,
6732 EW_FILE|EW_NOTFOUND|EW_ADDSLASH) == OK
6733 && new_arg_file_count > 0)
6734 {
Bram Moolenaar86b68352004-12-27 21:59:20 +00006735 alist_set(&global_alist, new_arg_file_count, new_arg_files,
6736 TRUE, fnum_list, fnum_len);
6737 FreeWild(old_arg_count, old_arg_files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006738 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006739 }
6740 p_su = save_p_su;
6741}
6742#endif
6743
6744/*
6745 * Set the argument list for the current window.
6746 * Takes over the allocated files[] and the allocated fnames in it.
6747 */
6748 void
Bram Moolenaar86b68352004-12-27 21:59:20 +00006749alist_set(al, count, files, use_curbuf, fnum_list, fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006750 alist_T *al;
6751 int count;
6752 char_u **files;
6753 int use_curbuf;
Bram Moolenaar86b68352004-12-27 21:59:20 +00006754 int *fnum_list;
6755 int fnum_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006756{
6757 int i;
6758
6759 alist_clear(al);
6760 if (ga_grow(&al->al_ga, count) == OK)
6761 {
6762 for (i = 0; i < count; ++i)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00006763 {
6764 if (got_int)
6765 {
6766 /* When adding many buffers this can take a long time. Allow
6767 * interrupting here. */
6768 while (i < count)
6769 vim_free(files[i++]);
6770 break;
6771 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00006772
6773 /* May set buffer name of a buffer previously used for the
6774 * argument list, so that it's re-used by alist_add. */
6775 if (fnum_list != NULL && i < fnum_len)
6776 buf_set_name(fnum_list[i], files[i]);
6777
Bram Moolenaar071d4272004-06-13 20:20:40 +00006778 alist_add(al, files[i], use_curbuf ? 2 : 1);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00006779 ui_breakcheck();
6780 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006781 vim_free(files);
6782 }
6783 else
6784 FreeWild(count, files);
6785#ifdef FEAT_WINDOWS
6786 if (al == &global_alist)
6787#endif
6788 arg_had_last = FALSE;
6789}
6790
6791/*
6792 * Add file "fname" to argument list "al".
6793 * "fname" must have been allocated and "al" must have been checked for room.
6794 */
6795 void
6796alist_add(al, fname, set_fnum)
6797 alist_T *al;
6798 char_u *fname;
6799 int set_fnum; /* 1: set buffer number; 2: re-use curbuf */
6800{
6801 if (fname == NULL) /* don't add NULL file names */
6802 return;
6803#ifdef BACKSLASH_IN_FILENAME
6804 slash_adjust(fname);
6805#endif
6806 AARGLIST(al)[al->al_ga.ga_len].ae_fname = fname;
6807 if (set_fnum > 0)
6808 AARGLIST(al)[al->al_ga.ga_len].ae_fnum =
6809 buflist_add(fname, BLN_LISTED | (set_fnum == 2 ? BLN_CURBUF : 0));
6810 ++al->al_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006811}
6812
6813#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
6814/*
6815 * Adjust slashes in file names. Called after 'shellslash' was set.
6816 */
6817 void
6818alist_slash_adjust()
6819{
6820 int i;
6821# ifdef FEAT_WINDOWS
6822 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006823 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006824# endif
6825
6826 for (i = 0; i < GARGCOUNT; ++i)
6827 if (GARGLIST[i].ae_fname != NULL)
6828 slash_adjust(GARGLIST[i].ae_fname);
6829# ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00006830 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006831 if (wp->w_alist != &global_alist)
6832 for (i = 0; i < WARGCOUNT(wp); ++i)
6833 if (WARGLIST(wp)[i].ae_fname != NULL)
6834 slash_adjust(WARGLIST(wp)[i].ae_fname);
6835# endif
6836}
6837#endif
6838
6839/*
6840 * ":preserve".
6841 */
6842/*ARGSUSED*/
6843 static void
6844ex_preserve(eap)
6845 exarg_T *eap;
6846{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00006847 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006848 ml_preserve(curbuf, TRUE);
6849}
6850
6851/*
6852 * ":recover".
6853 */
6854 static void
6855ex_recover(eap)
6856 exarg_T *eap;
6857{
6858 /* Set recoverymode right away to avoid the ATTENTION prompt. */
6859 recoverymode = TRUE;
6860 if (!check_changed(curbuf, p_awa, TRUE, eap->forceit, FALSE)
6861 && (*eap->arg == NUL
6862 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
6863 ml_recover();
6864 recoverymode = FALSE;
6865}
6866
6867/*
6868 * Command modifier used in a wrong way.
6869 */
6870 static void
6871ex_wrongmodifier(eap)
6872 exarg_T *eap;
6873{
6874 eap->errmsg = e_invcmd;
6875}
6876
6877#ifdef FEAT_WINDOWS
6878/*
6879 * :sview [+command] file split window with new file, read-only
6880 * :split [[+command] file] split window with current or new file
6881 * :vsplit [[+command] file] split window vertically with current or new file
6882 * :new [[+command] file] split window with no or new file
6883 * :vnew [[+command] file] split vertically window with no or new file
6884 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006885 *
6886 * :tabedit open new Tab page with empty window
6887 * :tabedit [+command] file open new Tab page and edit "file"
6888 * :tabnew [[+command] file] just like :tabedit
6889 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006890 */
6891 void
6892ex_splitview(eap)
6893 exarg_T *eap;
6894{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006895 win_T *old_curwin = curwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006896# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006897 char_u *fname = NULL;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006898# endif
6899# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006900 int browse_flag = cmdmod.browse;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006901# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006902
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006903# ifndef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006904 if (eap->cmdidx == CMD_vsplit || eap->cmdidx == CMD_vnew)
6905 {
6906 ex_ni(eap);
6907 return;
6908 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006909# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006910
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006911# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006912 need_mouse_correct = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006913# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006914
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006915# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00006916 /* A ":split" in the quickfix window works like ":new". Don't want two
6917 * quickfix windows. */
6918 if (bt_quickfix(curbuf))
6919 {
6920 if (eap->cmdidx == CMD_split)
6921 eap->cmdidx = CMD_new;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006922# ifdef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006923 if (eap->cmdidx == CMD_vsplit)
6924 eap->cmdidx = CMD_vnew;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006925# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006926 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006927# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006928
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006929# ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006930 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006931 {
6932 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
6933 FNAME_MESS, TRUE, curbuf->b_ffname);
6934 if (fname == NULL)
6935 goto theend;
6936 eap->arg = fname;
6937 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006938# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006939 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006940# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006941# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006942# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006943 if (cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006944# ifdef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006945 && eap->cmdidx != CMD_vnew
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006946# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006947 && eap->cmdidx != CMD_new)
6948 {
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006949 if (
6950# ifdef FEAT_GUI
6951 !gui.in_use &&
6952# endif
6953 au_has_group((char_u *)"FileExplorer"))
6954 {
6955 /* No browsing supported but we do have the file explorer:
6956 * Edit the directory. */
6957 if (*eap->arg == NUL || !mch_isdir(eap->arg))
6958 eap->arg = (char_u *)".";
6959 }
6960 else
6961 {
6962 fname = do_browse(0, (char_u *)_("Edit File in new window"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00006963 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006964 if (fname == NULL)
6965 goto theend;
6966 eap->arg = fname;
6967 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006968 }
6969 cmdmod.browse = FALSE; /* Don't browse again in do_ecmd(). */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006970# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006971
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006972 /*
6973 * Either open new tab page or split the window.
6974 */
6975 if (eap->cmdidx == CMD_tabedit
6976 || eap->cmdidx == CMD_tabfind
6977 || eap->cmdidx == CMD_tabnew)
6978 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006979 if (win_new_tabpage(cmdmod.tab) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006980 {
6981 do_exedit(eap, NULL);
6982
6983 /* set the alternate buffer for the window we came from */
6984 if (curwin != old_curwin
6985 && win_valid(old_curwin)
6986 && old_curwin->w_buffer != curbuf
6987 && !cmdmod.keepalt)
6988 old_curwin->w_alt_fnum = curbuf->b_fnum;
6989 }
6990 }
6991 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006992 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
6993 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006994# ifdef FEAT_SCROLLBIND
Bram Moolenaar071d4272004-06-13 20:20:40 +00006995 /* Reset 'scrollbind' when editing another file, but keep it when
6996 * doing ":split" without arguments. */
6997 if (*eap->arg != NUL
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006998# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006999 || cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007000# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007001 )
7002 curwin->w_p_scb = FALSE;
7003 else
7004 do_check_scrollbind(FALSE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007005# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007006 do_exedit(eap, old_curwin);
7007 }
7008
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007009# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007010 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007011# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007012
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007013# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007014theend:
7015 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007016# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007017}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007018
7019/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007020 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007021 */
7022 void
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007023tabpage_new()
7024{
7025 exarg_T ea;
7026
7027 vim_memset(&ea, 0, sizeof(ea));
7028 ea.cmdidx = CMD_tabnew;
7029 ea.cmd = (char_u *)"tabn";
7030 ea.arg = (char_u *)"";
7031 ex_splitview(&ea);
7032}
7033
7034/*
7035 * :tabnext command
7036 */
7037 static void
7038ex_tabnext(eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007039 exarg_T *eap;
7040{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007041 switch (eap->cmdidx)
7042 {
7043 case CMD_tabfirst:
7044 case CMD_tabrewind:
7045 goto_tabpage(1);
7046 break;
7047 case CMD_tablast:
7048 goto_tabpage(9999);
7049 break;
7050 case CMD_tabprevious:
7051 case CMD_tabNext:
7052 goto_tabpage(eap->addr_count == 0 ? -1 : -(int)eap->line2);
7053 break;
7054 default: /* CMD_tabnext */
7055 goto_tabpage(eap->addr_count == 0 ? 0 : (int)eap->line2);
7056 break;
7057 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007058}
7059
7060/*
7061 * :tabmove command
7062 */
7063 static void
7064ex_tabmove(eap)
7065 exarg_T *eap;
7066{
7067 tabpage_move(eap->addr_count == 0 ? 9999 : (int)eap->line2);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007068}
7069
7070/*
7071 * :tabs command: List tabs and their contents.
7072 */
7073/*ARGSUSED*/
7074 static void
7075ex_tabs(eap)
7076 exarg_T *eap;
7077{
7078 tabpage_T *tp;
7079 win_T *wp;
7080 int tabcount = 1;
7081
7082 msg_start();
7083 msg_scroll = TRUE;
7084 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
7085 {
7086 msg_putchar('\n');
7087 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
7088 msg_outtrans_attr(IObuff, hl_attr(HLF_T));
7089 out_flush(); /* output one line at a time */
7090 ui_breakcheck();
7091
Bram Moolenaar030f0df2006-02-21 22:02:53 +00007092 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007093 wp = firstwin;
7094 else
7095 wp = tp->tp_firstwin;
7096 for ( ; wp != NULL && !got_int; wp = wp->w_next)
7097 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007098 msg_putchar('\n');
7099 msg_putchar(wp == curwin ? '>' : ' ');
7100 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007101 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
7102 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007103 if (buf_spname(wp->w_buffer) != NULL)
7104 STRCPY(IObuff, buf_spname(wp->w_buffer));
7105 else
7106 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
7107 IObuff, IOSIZE, TRUE);
7108 msg_outtrans(IObuff);
7109 out_flush(); /* output one line at a time */
7110 ui_breakcheck();
7111 }
7112 }
7113}
7114
7115#endif /* FEAT_WINDOWS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007116
7117/*
7118 * ":mode": Set screen mode.
7119 * If no argument given, just get the screen size and redraw.
7120 */
7121 static void
7122ex_mode(eap)
7123 exarg_T *eap;
7124{
7125 if (*eap->arg == NUL)
7126 shell_resized();
7127 else
7128 mch_screenmode(eap->arg);
7129}
7130
7131#ifdef FEAT_WINDOWS
7132/*
7133 * ":resize".
7134 * set, increment or decrement current window height
7135 */
7136 static void
7137ex_resize(eap)
7138 exarg_T *eap;
7139{
7140 int n;
7141 win_T *wp = curwin;
7142
7143 if (eap->addr_count > 0)
7144 {
7145 n = eap->line2;
7146 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
7147 ;
7148 }
7149
7150#ifdef FEAT_GUI
7151 need_mouse_correct = TRUE;
7152#endif
7153 n = atol((char *)eap->arg);
7154#ifdef FEAT_VERTSPLIT
7155 if (cmdmod.split & WSP_VERT)
7156 {
7157 if (*eap->arg == '-' || *eap->arg == '+')
7158 n += W_WIDTH(curwin);
7159 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
7160 n = 9999;
7161 win_setwidth_win((int)n, wp);
7162 }
7163 else
7164#endif
7165 {
7166 if (*eap->arg == '-' || *eap->arg == '+')
7167 n += curwin->w_height;
7168 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
7169 n = 9999;
7170 win_setheight_win((int)n, wp);
7171 }
7172}
7173#endif
7174
7175/*
7176 * ":find [+command] <file>" command.
7177 */
7178 static void
7179ex_find(eap)
7180 exarg_T *eap;
7181{
7182#ifdef FEAT_SEARCHPATH
7183 char_u *fname;
7184 int count;
7185
7186 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
7187 TRUE, curbuf->b_ffname);
7188 if (eap->addr_count > 0)
7189 {
7190 /* Repeat finding the file "count" times. This matters when it
7191 * appears several times in the path. */
7192 count = eap->line2;
7193 while (fname != NULL && --count > 0)
7194 {
7195 vim_free(fname);
7196 fname = find_file_in_path(NULL, 0, FNAME_MESS,
7197 FALSE, curbuf->b_ffname);
7198 }
7199 }
7200
7201 if (fname != NULL)
7202 {
7203 eap->arg = fname;
7204#endif
7205 do_exedit(eap, NULL);
7206#ifdef FEAT_SEARCHPATH
7207 vim_free(fname);
7208 }
7209#endif
7210}
7211
7212/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00007213 * ":open" simulation: for now just work like ":visual".
7214 */
7215 static void
7216ex_open(eap)
7217 exarg_T *eap;
7218{
7219 regmatch_T regmatch;
7220 char_u *p;
7221
7222 curwin->w_cursor.lnum = eap->line2;
7223 beginline(BL_SOL | BL_FIX);
7224 if (*eap->arg == '/')
7225 {
7226 /* ":open /pattern/": put cursor in column found with pattern */
7227 ++eap->arg;
7228 p = skip_regexp(eap->arg, '/', p_magic, NULL);
7229 *p = NUL;
7230 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
7231 if (regmatch.regprog != NULL)
7232 {
7233 regmatch.rm_ic = p_ic;
7234 p = ml_get_curline();
7235 if (vim_regexec(&regmatch, p, (colnr_T)0))
7236 curwin->w_cursor.col = regmatch.startp[0] - p;
7237 else
7238 EMSG(_(e_nomatch));
7239 vim_free(regmatch.regprog);
7240 }
7241 /* Move to the NUL, ignore any other arguments. */
7242 eap->arg += STRLEN(eap->arg);
7243 }
7244 check_cursor();
7245
7246 eap->cmdidx = CMD_visual;
7247 do_exedit(eap, NULL);
7248}
7249
7250/*
7251 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007252 */
7253 static void
7254ex_edit(eap)
7255 exarg_T *eap;
7256{
7257 do_exedit(eap, NULL);
7258}
7259
7260/*
7261 * ":edit <file>" command and alikes.
7262 */
7263/*ARGSUSED*/
7264 void
7265do_exedit(eap, old_curwin)
7266 exarg_T *eap;
7267 win_T *old_curwin; /* curwin before doing a split or NULL */
7268{
7269 int n;
7270#ifdef FEAT_WINDOWS
7271 int need_hide;
7272#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00007273 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007274
7275 /*
7276 * ":vi" command ends Ex mode.
7277 */
7278 if (exmode_active && (eap->cmdidx == CMD_visual
7279 || eap->cmdidx == CMD_view))
7280 {
7281 exmode_active = FALSE;
7282 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007283 {
7284 /* Special case: ":global/pat/visual\NLvi-commands" */
7285 if (global_busy)
7286 {
7287 int rd = RedrawingDisabled;
7288 int nwr = no_wait_return;
7289 int ms = msg_scroll;
7290#ifdef FEAT_GUI
7291 int he = hold_gui_events;
7292#endif
7293
7294 if (eap->nextcmd != NULL)
7295 {
7296 stuffReadbuff(eap->nextcmd);
7297 eap->nextcmd = NULL;
7298 }
7299
7300 if (exmode_was != EXMODE_VIM)
7301 settmode(TMODE_RAW);
7302 RedrawingDisabled = 0;
7303 no_wait_return = 0;
7304 need_wait_return = FALSE;
7305 msg_scroll = 0;
7306#ifdef FEAT_GUI
7307 hold_gui_events = 0;
7308#endif
7309 must_redraw = CLEAR;
7310
7311 main_loop(FALSE, TRUE);
7312
7313 RedrawingDisabled = rd;
7314 no_wait_return = nwr;
7315 msg_scroll = ms;
7316#ifdef FEAT_GUI
7317 hold_gui_events = he;
7318#endif
7319 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007320 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007321 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007322 }
7323
7324 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007325 || eap->cmdidx == CMD_tabnew
7326 || eap->cmdidx == CMD_tabedit
Bram Moolenaar071d4272004-06-13 20:20:40 +00007327#ifdef FEAT_VERTSPLIT
7328 || eap->cmdidx == CMD_vnew
7329#endif
7330 ) && *eap->arg == NUL)
7331 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007332 /* ":new" or ":tabnew" without argument: edit an new empty buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007333 setpcmark();
7334 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
7335 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0));
7336 }
7337 else if ((eap->cmdidx != CMD_split
7338#ifdef FEAT_VERTSPLIT
7339 && eap->cmdidx != CMD_vsplit
7340#endif
7341 )
7342 || *eap->arg != NUL
7343#ifdef FEAT_BROWSE
7344 || cmdmod.browse
7345#endif
7346 )
7347 {
7348 n = readonlymode;
7349 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
7350 readonlymode = TRUE;
7351 else if (eap->cmdidx == CMD_enew)
7352 readonlymode = FALSE; /* 'readonly' doesn't make sense in an
7353 empty buffer */
7354 setpcmark();
7355 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
7356 NULL, eap,
7357 /* ":edit" goes to first line if Vi compatible */
7358 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
7359 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
7360 ? ECMD_ONE : eap->do_ecmd_lnum,
7361 (P_HID(curbuf) ? ECMD_HIDE : 0)
7362 + (eap->forceit ? ECMD_FORCEIT : 0)
7363#ifdef FEAT_LISTCMDS
7364 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
7365#endif
7366 ) == FAIL)
7367 {
7368 /* Editing the file failed. If the window was split, close it. */
7369#ifdef FEAT_WINDOWS
7370 if (old_curwin != NULL)
7371 {
7372 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
7373 if (!need_hide || P_HID(curbuf))
7374 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007375# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7376 cleanup_T cs;
7377
7378 /* Reset the error/interrupt/exception state here so that
7379 * aborting() returns FALSE when closing a window. */
7380 enter_cleanup(&cs);
7381# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007382# ifdef FEAT_GUI
7383 need_mouse_correct = TRUE;
7384# endif
7385 win_close(curwin, !need_hide && !P_HID(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007386
7387# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7388 /* Restore the error/interrupt/exception state if not
7389 * discarded by a new aborting error, interrupt, or
7390 * uncaught exception. */
7391 leave_cleanup(&cs);
7392# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007393 }
7394 }
7395#endif
7396 }
7397 else if (readonlymode && curbuf->b_nwindows == 1)
7398 {
7399 /* When editing an already visited buffer, 'readonly' won't be set
7400 * but the previous value is kept. With ":view" and ":sview" we
7401 * want the file to be readonly, except when another window is
7402 * editing the same buffer. */
7403 curbuf->b_p_ro = TRUE;
7404 }
7405 readonlymode = n;
7406 }
7407 else
7408 {
7409 if (eap->do_ecmd_cmd != NULL)
7410 do_cmdline_cmd(eap->do_ecmd_cmd);
7411#ifdef FEAT_TITLE
7412 n = curwin->w_arg_idx_invalid;
7413#endif
7414 check_arg_idx(curwin);
7415#ifdef FEAT_TITLE
7416 if (n != curwin->w_arg_idx_invalid)
7417 maketitle();
7418#endif
7419 }
7420
7421#ifdef FEAT_WINDOWS
7422 /*
7423 * if ":split file" worked, set alternate file name in old window to new
7424 * file
7425 */
7426 if (old_curwin != NULL
7427 && *eap->arg != NUL
7428 && curwin != old_curwin
7429 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00007430 && old_curwin->w_buffer != curbuf
7431 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007432 old_curwin->w_alt_fnum = curbuf->b_fnum;
7433#endif
7434
7435 ex_no_reprint = TRUE;
7436}
7437
7438#ifndef FEAT_GUI
7439/*
7440 * ":gui" and ":gvim" when there is no GUI.
7441 */
7442 static void
7443ex_nogui(eap)
7444 exarg_T *eap;
7445{
7446 eap->errmsg = e_nogvim;
7447}
7448#endif
7449
7450#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
7451 static void
7452ex_tearoff(eap)
7453 exarg_T *eap;
7454{
7455 gui_make_tearoff(eap->arg);
7456}
7457#endif
7458
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007459#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007460 static void
7461ex_popup(eap)
7462 exarg_T *eap;
7463{
Bram Moolenaar97409f12005-07-08 22:17:29 +00007464 gui_make_popup(eap->arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007465}
7466#endif
7467
7468/*ARGSUSED*/
7469 static void
7470ex_swapname(eap)
7471 exarg_T *eap;
7472{
7473 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
7474 MSG(_("No swap file"));
7475 else
7476 msg(curbuf->b_ml.ml_mfp->mf_fname);
7477}
7478
7479/*
7480 * ":syncbind" forces all 'scrollbind' windows to have the same relative
7481 * offset.
7482 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
7483 */
7484/*ARGSUSED*/
7485 static void
7486ex_syncbind(eap)
7487 exarg_T *eap;
7488{
7489#ifdef FEAT_SCROLLBIND
7490 win_T *wp;
7491 long topline;
7492 long y;
7493 linenr_T old_linenr = curwin->w_cursor.lnum;
7494
7495 setpcmark();
7496
7497 /*
7498 * determine max topline
7499 */
7500 if (curwin->w_p_scb)
7501 {
7502 topline = curwin->w_topline;
7503 for (wp = firstwin; wp; wp = wp->w_next)
7504 {
7505 if (wp->w_p_scb && wp->w_buffer)
7506 {
7507 y = wp->w_buffer->b_ml.ml_line_count - p_so;
7508 if (topline > y)
7509 topline = y;
7510 }
7511 }
7512 if (topline < 1)
7513 topline = 1;
7514 }
7515 else
7516 {
7517 topline = 1;
7518 }
7519
7520
7521 /*
7522 * set all scrollbind windows to the same topline
7523 */
7524 wp = curwin;
7525 for (curwin = firstwin; curwin; curwin = curwin->w_next)
7526 {
7527 if (curwin->w_p_scb)
7528 {
7529 y = topline - curwin->w_topline;
7530 if (y > 0)
7531 scrollup(y, TRUE);
7532 else
7533 scrolldown(-y, TRUE);
7534 curwin->w_scbind_pos = topline;
7535 redraw_later(VALID);
7536 cursor_correct();
7537#ifdef FEAT_WINDOWS
7538 curwin->w_redr_status = TRUE;
7539#endif
7540 }
7541 }
7542 curwin = wp;
7543 if (curwin->w_p_scb)
7544 {
7545 did_syncbind = TRUE;
7546 checkpcmark();
7547 if (old_linenr != curwin->w_cursor.lnum)
7548 {
7549 char_u ctrl_o[2];
7550
7551 ctrl_o[0] = Ctrl_O;
7552 ctrl_o[1] = 0;
7553 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
7554 }
7555 }
7556#endif
7557}
7558
7559
7560 static void
7561ex_read(eap)
7562 exarg_T *eap;
7563{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007564 int i;
7565 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
7566 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007567
7568 if (eap->usefilter) /* :r!cmd */
7569 do_bang(1, eap, FALSE, FALSE, TRUE);
7570 else
7571 {
7572 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
7573 return;
7574
7575#ifdef FEAT_BROWSE
7576 if (cmdmod.browse)
7577 {
7578 char_u *browseFile;
7579
Bram Moolenaar7171abe2004-10-11 10:06:20 +00007580 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007581 NULL, NULL, NULL, curbuf);
7582 if (browseFile != NULL)
7583 {
7584 i = readfile(browseFile, NULL,
7585 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7586 vim_free(browseFile);
7587 }
7588 else
7589 i = OK;
7590 }
7591 else
7592#endif
7593 if (*eap->arg == NUL)
7594 {
7595 if (check_fname() == FAIL) /* check for no file name */
7596 return;
7597 i = readfile(curbuf->b_ffname, curbuf->b_fname,
7598 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7599 }
7600 else
7601 {
7602 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
7603 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
7604 i = readfile(eap->arg, NULL,
7605 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7606
7607 }
7608 if (i == FAIL)
7609 {
7610#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7611 if (!aborting())
7612#endif
7613 EMSG2(_(e_notopen), eap->arg);
7614 }
7615 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00007616 {
7617 if (empty && exmode_active)
7618 {
7619 /* Delete the empty line that remains. Historically ex does
7620 * this but vi doesn't. */
7621 if (eap->line2 == 0)
7622 lnum = curbuf->b_ml.ml_line_count;
7623 else
7624 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007625 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007626 {
7627 ml_delete(lnum, FALSE);
7628 deleted_lines_mark(lnum, 1L);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007629 if (curwin->w_cursor.lnum > 1
7630 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007631 --curwin->w_cursor.lnum;
7632 }
7633 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007634 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007635 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007636 }
7637}
7638
Bram Moolenaarea408852005-06-25 22:49:46 +00007639static char_u *prev_dir = NULL;
7640
7641#if defined(EXITFREE) || defined(PROTO)
7642 void
7643free_cd_dir()
7644{
7645 vim_free(prev_dir);
7646}
7647#endif
7648
7649
Bram Moolenaar071d4272004-06-13 20:20:40 +00007650/*
7651 * ":cd", ":lcd", ":chdir" and ":lchdir".
7652 */
7653 static void
7654ex_cd(eap)
7655 exarg_T *eap;
7656{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007657 char_u *new_dir;
7658 char_u *tofree;
7659
7660 new_dir = eap->arg;
7661#if !defined(UNIX) && !defined(VMS)
7662 /* for non-UNIX ":cd" means: print current directory */
7663 if (*new_dir == NUL)
7664 ex_pwd(NULL);
7665 else
7666#endif
7667 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007668 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged()
7669 && !eap->forceit)
7670 {
7671 EMSG(_("E747: Cannot change directory, buffer is modifed (add ! to override)"));
7672 return;
7673 }
7674
Bram Moolenaar071d4272004-06-13 20:20:40 +00007675 /* ":cd -": Change to previous directory */
7676 if (STRCMP(new_dir, "-") == 0)
7677 {
7678 if (prev_dir == NULL)
7679 {
7680 EMSG(_("E186: No previous directory"));
7681 return;
7682 }
7683 new_dir = prev_dir;
7684 }
7685
7686 /* Save current directory for next ":cd -" */
7687 tofree = prev_dir;
7688 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7689 prev_dir = vim_strsave(NameBuff);
7690 else
7691 prev_dir = NULL;
7692
7693#if defined(UNIX) || defined(VMS)
7694 /* for UNIX ":cd" means: go to home directory */
7695 if (*new_dir == NUL)
7696 {
7697 /* use NameBuff for home directory name */
7698# ifdef VMS
7699 char_u *p;
7700
7701 p = mch_getenv((char_u *)"SYS$LOGIN");
7702 if (p == NULL || *p == NUL) /* empty is the same as not set */
7703 NameBuff[0] = NUL;
7704 else
Bram Moolenaarb6356332005-07-18 21:40:44 +00007705 vim_strncpy(NameBuff, p, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007706# else
7707 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
7708# endif
7709 new_dir = NameBuff;
7710 }
7711#endif
7712 if (new_dir == NULL || vim_chdir(new_dir))
7713 EMSG(_(e_failed));
7714 else
7715 {
7716 vim_free(curwin->w_localdir);
7717 if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir)
7718 {
7719 /* If still in global directory, need to remember current
7720 * directory as global directory. */
7721 if (globaldir == NULL && prev_dir != NULL)
7722 globaldir = vim_strsave(prev_dir);
7723 /* Remember this local directory for the window. */
7724 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7725 curwin->w_localdir = vim_strsave(NameBuff);
7726 }
7727 else
7728 {
7729 /* We are now in the global directory, no need to remember its
7730 * name. */
7731 vim_free(globaldir);
7732 globaldir = NULL;
7733 curwin->w_localdir = NULL;
7734 }
7735
7736 shorten_fnames(TRUE);
7737
7738 /* Echo the new current directory if the command was typed. */
7739 if (KeyTyped)
7740 ex_pwd(eap);
7741 }
7742 vim_free(tofree);
7743 }
7744}
7745
7746/*
7747 * ":pwd".
7748 */
7749/*ARGSUSED*/
7750 static void
7751ex_pwd(eap)
7752 exarg_T *eap;
7753{
7754 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7755 {
7756#ifdef BACKSLASH_IN_FILENAME
7757 slash_adjust(NameBuff);
7758#endif
7759 msg(NameBuff);
7760 }
7761 else
7762 EMSG(_("E187: Unknown"));
7763}
7764
7765/*
7766 * ":=".
7767 */
7768 static void
7769ex_equal(eap)
7770 exarg_T *eap;
7771{
7772 smsg((char_u *)"%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007773 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007774}
7775
7776 static void
7777ex_sleep(eap)
7778 exarg_T *eap;
7779{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007780 int n;
7781 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007782
7783 if (cursor_valid())
7784 {
7785 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
7786 if (n >= 0)
7787 windgoto((int)n, curwin->w_wcol);
7788 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007789
7790 len = eap->line2;
7791 switch (*eap->arg)
7792 {
7793 case 'm': break;
7794 case NUL: len *= 1000L; break;
7795 default: EMSG2(_(e_invarg2), eap->arg); return;
7796 }
7797 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007798}
7799
7800/*
7801 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
7802 */
7803 void
7804do_sleep(msec)
7805 long msec;
7806{
7807 long done;
7808
7809 cursor_on();
7810 out_flush();
7811 for (done = 0; !got_int && done < msec; done += 1000L)
7812 {
7813 ui_delay(msec - done > 1000L ? 1000L : msec - done, TRUE);
7814 ui_breakcheck();
7815 }
7816}
7817
7818 static void
7819do_exmap(eap, isabbrev)
7820 exarg_T *eap;
7821 int isabbrev;
7822{
7823 int mode;
7824 char_u *cmdp;
7825
7826 cmdp = eap->cmd;
7827 mode = get_map_mode(&cmdp, eap->forceit || isabbrev);
7828
7829 switch (do_map((*cmdp == 'n') ? 2 : (*cmdp == 'u'),
7830 eap->arg, mode, isabbrev))
7831 {
7832 case 1: EMSG(_(e_invarg));
7833 break;
7834 case 2: EMSG(isabbrev ? _(e_noabbr) : _(e_nomap));
7835 break;
7836 }
7837}
7838
7839/*
7840 * ":winsize" command (obsolete).
7841 */
7842 static void
7843ex_winsize(eap)
7844 exarg_T *eap;
7845{
7846 int w, h;
7847 char_u *arg = eap->arg;
7848 char_u *p;
7849
7850 w = getdigits(&arg);
7851 arg = skipwhite(arg);
7852 p = arg;
7853 h = getdigits(&arg);
7854 if (*p != NUL && *arg == NUL)
7855 set_shellsize(w, h, TRUE);
7856 else
7857 EMSG(_("E465: :winsize requires two number arguments"));
7858}
7859
7860#ifdef FEAT_WINDOWS
7861 static void
7862ex_wincmd(eap)
7863 exarg_T *eap;
7864{
7865 int xchar = NUL;
7866 char_u *p;
7867
7868 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
7869 {
7870 /* CTRL-W g and CTRL-W CTRL-G have an extra command character */
7871 if (eap->arg[1] == NUL)
7872 {
7873 EMSG(_(e_invarg));
7874 return;
7875 }
7876 xchar = eap->arg[1];
7877 p = eap->arg + 2;
7878 }
7879 else
7880 p = eap->arg + 1;
7881
7882 eap->nextcmd = check_nextcmd(p);
7883 p = skipwhite(p);
7884 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
7885 EMSG(_(e_invarg));
7886 else
7887 {
7888 /* Pass flags on for ":vertical wincmd ]". */
7889 postponed_split_flags = cmdmod.split;
7890 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
7891 postponed_split_flags = 0;
7892 }
7893}
7894#endif
7895
Bram Moolenaar843ee412004-06-30 16:16:41 +00007896#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007897/*
7898 * ":winpos".
7899 */
7900 static void
7901ex_winpos(eap)
7902 exarg_T *eap;
7903{
7904 int x, y;
7905 char_u *arg = eap->arg;
7906 char_u *p;
7907
7908 if (*arg == NUL)
7909 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00007910# if defined(FEAT_GUI) || defined(MSWIN)
7911# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007912 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00007913# else
7914 if (mch_get_winpos(&x, &y) != FAIL)
7915# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007916 {
7917 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
7918 msg(IObuff);
7919 }
7920 else
7921# endif
7922 EMSG(_("E188: Obtaining window position not implemented for this platform"));
7923 }
7924 else
7925 {
7926 x = getdigits(&arg);
7927 arg = skipwhite(arg);
7928 p = arg;
7929 y = getdigits(&arg);
7930 if (*p == NUL || *arg != NUL)
7931 {
7932 EMSG(_("E466: :winpos requires two number arguments"));
7933 return;
7934 }
7935# ifdef FEAT_GUI
7936 if (gui.in_use)
7937 gui_mch_set_winpos(x, y);
7938 else if (gui.starting)
7939 {
7940 /* Remember the coordinates for when the window is opened. */
7941 gui_win_x = x;
7942 gui_win_y = y;
7943 }
7944# ifdef HAVE_TGETENT
7945 else
7946# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00007947# else
7948# ifdef MSWIN
7949 mch_set_winpos(x, y);
7950# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007951# endif
7952# ifdef HAVE_TGETENT
7953 if (*T_CWP)
7954 term_set_winpos(x, y);
7955# endif
7956 }
7957}
7958#endif
7959
7960/*
7961 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
7962 */
7963 static void
7964ex_operators(eap)
7965 exarg_T *eap;
7966{
7967 oparg_T oa;
7968
7969 clear_oparg(&oa);
7970 oa.regname = eap->regname;
7971 oa.start.lnum = eap->line1;
7972 oa.end.lnum = eap->line2;
7973 oa.line_count = eap->line2 - eap->line1 + 1;
7974 oa.motion_type = MLINE;
7975#ifdef FEAT_VIRTUALEDIT
7976 virtual_op = FALSE;
7977#endif
7978 if (eap->cmdidx != CMD_yank) /* position cursor for undo */
7979 {
7980 setpcmark();
7981 curwin->w_cursor.lnum = eap->line1;
7982 beginline(BL_SOL | BL_FIX);
7983 }
7984
7985 switch (eap->cmdidx)
7986 {
7987 case CMD_delete:
7988 oa.op_type = OP_DELETE;
7989 op_delete(&oa);
7990 break;
7991
7992 case CMD_yank:
7993 oa.op_type = OP_YANK;
7994 (void)op_yank(&oa, FALSE, TRUE);
7995 break;
7996
7997 default: /* CMD_rshift or CMD_lshift */
7998 if ((eap->cmdidx == CMD_rshift)
7999#ifdef FEAT_RIGHTLEFT
8000 ^ curwin->w_p_rl
8001#endif
8002 )
8003 oa.op_type = OP_RSHIFT;
8004 else
8005 oa.op_type = OP_LSHIFT;
8006 op_shift(&oa, FALSE, eap->amount);
8007 break;
8008 }
8009#ifdef FEAT_VIRTUALEDIT
8010 virtual_op = MAYBE;
8011#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00008012 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008013}
8014
8015/*
8016 * ":put".
8017 */
8018 static void
8019ex_put(eap)
8020 exarg_T *eap;
8021{
8022 /* ":0put" works like ":1put!". */
8023 if (eap->line2 == 0)
8024 {
8025 eap->line2 = 1;
8026 eap->forceit = TRUE;
8027 }
8028 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008029 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
8030 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008031}
8032
8033/*
8034 * Handle ":copy" and ":move".
8035 */
8036 static void
8037ex_copymove(eap)
8038 exarg_T *eap;
8039{
8040 long n;
8041
8042 n = get_address(&eap->arg, FALSE, FALSE);
8043 if (eap->arg == NULL) /* error detected */
8044 {
8045 eap->nextcmd = NULL;
8046 return;
8047 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008048 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008049
8050 /*
8051 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
8052 */
8053 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
8054 {
8055 EMSG(_(e_invaddr));
8056 return;
8057 }
8058
8059 if (eap->cmdidx == CMD_move)
8060 {
8061 if (do_move(eap->line1, eap->line2, n) == FAIL)
8062 return;
8063 }
8064 else
8065 ex_copy(eap->line1, eap->line2, n);
8066 u_clearline();
8067 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008068 ex_may_print(eap);
8069}
8070
8071/*
8072 * Print the current line if flags were given to the Ex command.
8073 */
8074 static void
8075ex_may_print(eap)
8076 exarg_T *eap;
8077{
8078 if (eap->flags != 0)
8079 {
8080 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
8081 (eap->flags & EXFLAG_LIST));
8082 ex_no_reprint = TRUE;
8083 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008084}
8085
8086/*
8087 * ":smagic" and ":snomagic".
8088 */
8089 static void
8090ex_submagic(eap)
8091 exarg_T *eap;
8092{
8093 int magic_save = p_magic;
8094
8095 p_magic = (eap->cmdidx == CMD_smagic);
8096 do_sub(eap);
8097 p_magic = magic_save;
8098}
8099
8100/*
8101 * ":join".
8102 */
8103 static void
8104ex_join(eap)
8105 exarg_T *eap;
8106{
8107 curwin->w_cursor.lnum = eap->line1;
8108 if (eap->line1 == eap->line2)
8109 {
8110 if (eap->addr_count >= 2) /* :2,2join does nothing */
8111 return;
8112 if (eap->line2 == curbuf->b_ml.ml_line_count)
8113 {
8114 beep_flush();
8115 return;
8116 }
8117 ++eap->line2;
8118 }
8119 do_do_join(eap->line2 - eap->line1 + 1, !eap->forceit);
8120 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008121 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008122}
8123
8124/*
8125 * ":[addr]@r" or ":[addr]*r": execute register
8126 */
8127 static void
8128ex_at(eap)
8129 exarg_T *eap;
8130{
8131 int c;
8132
8133 curwin->w_cursor.lnum = eap->line2;
8134
8135#ifdef USE_ON_FLY_SCROLL
8136 dont_scroll = TRUE; /* disallow scrolling here */
8137#endif
8138
8139 /* get the register name. No name means to use the previous one */
8140 c = *eap->arg;
8141 if (c == NUL || (c == '*' && *eap->cmd == '*'))
8142 c = '@';
8143 /* put the register in mapbuf */
8144 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL) == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008145 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008146 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00008147 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008148 else
8149 {
8150 int save_efr = exec_from_reg;
8151
8152 exec_from_reg = TRUE;
8153
8154 /*
8155 * Execute from the typeahead buffer.
8156 * Originally this didn't check for the typeahead buffer to be empty,
8157 * thus could read more Ex commands from stdin. It's not clear why,
8158 * it is certainly unexpected.
8159 */
8160 while ((!stuff_empty() || typebuf.tb_len > 0) && vpeekc() == ':')
8161 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
8162
8163 exec_from_reg = save_efr;
8164 }
8165}
8166
8167/*
8168 * ":!".
8169 */
8170 static void
8171ex_bang(eap)
8172 exarg_T *eap;
8173{
8174 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
8175}
8176
8177/*
8178 * ":undo".
8179 */
8180/*ARGSUSED*/
8181 static void
8182ex_undo(eap)
8183 exarg_T *eap;
8184{
8185 u_undo(1);
8186}
8187
8188/*
8189 * ":redo".
8190 */
8191/*ARGSUSED*/
8192 static void
8193ex_redo(eap)
8194 exarg_T *eap;
8195{
8196 u_redo(1);
8197}
8198
8199/*
8200 * ":redir": start/stop redirection.
8201 */
8202 static void
8203ex_redir(eap)
8204 exarg_T *eap;
8205{
8206 char *mode;
8207 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00008208 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008209
8210 if (STRICMP(eap->arg, "END") == 0)
8211 close_redir();
8212 else
8213 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008214 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008215 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008216 ++arg;
8217 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008218 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008219 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008220 mode = "a";
8221 }
8222 else
8223 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00008224 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008225
8226 close_redir();
8227
8228 /* Expand environment variables and "~/". */
Bram Moolenaarca472992005-01-21 11:46:23 +00008229 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008230 if (fname == NULL)
8231 return;
8232#ifdef FEAT_BROWSE
8233 if (cmdmod.browse)
8234 {
8235 char_u *browseFile;
8236
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008237 browseFile = do_browse(BROWSE_SAVE,
8238 (char_u *)_("Save Redirection"),
8239 fname, NULL, NULL, BROWSE_FILTER_ALL_FILES, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008240 if (browseFile == NULL)
8241 return; /* operation cancelled */
8242 vim_free(fname);
8243 fname = browseFile;
8244 eap->forceit = TRUE; /* since dialog already asked */
8245 }
8246#endif
8247
8248 redir_fd = open_exfile(fname, eap->forceit, mode);
8249 vim_free(fname);
8250 }
8251#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00008252 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008253 {
8254 /* redirect to a register a-z (resp. A-Z for appending) */
8255 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00008256 ++arg;
8257 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008258# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00008259 || *arg == '*'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008260# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00008261 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008262 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008263 redir_reg = *arg++;
Bram Moolenaard9d30582005-05-18 22:10:28 +00008264 if (*arg == '>' && arg[1] == '>')
8265 arg += 2;
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00008266 else if ((*arg == NUL || (*arg == '>' && arg[1] == NUL)) &&
8267 (islower(redir_reg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008268# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00008269 || redir_reg == '*'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008270# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00008271 || redir_reg == '"'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008272 {
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00008273 if (*arg == '>')
8274 arg++;
8275
Bram Moolenaar071d4272004-06-13 20:20:40 +00008276 /* make register empty */
8277 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
8278 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008279 }
8280 if (*arg != NUL)
8281 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00008282 redir_reg = 0;
Bram Moolenaard9d30582005-05-18 22:10:28 +00008283 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008284 }
8285 }
8286 else if (*arg == '=' && arg[1] == '>')
8287 {
8288 int append;
8289
8290 /* redirect to a variable */
8291 close_redir();
8292 arg += 2;
8293
8294 if (*arg == '>')
8295 {
8296 ++arg;
8297 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008298 }
8299 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008300 append = FALSE;
8301
8302 if (var_redir_start(skipwhite(arg), append) == OK)
8303 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008304 }
8305#endif
8306
8307 /* TODO: redirect to a buffer */
8308
Bram Moolenaar071d4272004-06-13 20:20:40 +00008309 else
Bram Moolenaarca472992005-01-21 11:46:23 +00008310 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008311 }
8312}
8313
8314/*
8315 * ":redraw": force redraw
8316 */
8317 static void
8318ex_redraw(eap)
8319 exarg_T *eap;
8320{
8321 int r = RedrawingDisabled;
8322 int p = p_lz;
8323
8324 RedrawingDisabled = 0;
8325 p_lz = FALSE;
8326 update_topline();
8327 update_screen(eap->forceit ? CLEAR :
8328#ifdef FEAT_VISUAL
8329 VIsual_active ? INVERTED :
8330#endif
8331 0);
8332#ifdef FEAT_TITLE
8333 if (need_maketitle)
8334 maketitle();
8335#endif
8336 RedrawingDisabled = r;
8337 p_lz = p;
8338
8339 /* Reset msg_didout, so that a message that's there is overwritten. */
8340 msg_didout = FALSE;
8341 msg_col = 0;
8342
8343 out_flush();
8344}
8345
8346/*
8347 * ":redrawstatus": force redraw of status line(s)
8348 */
8349/*ARGSUSED*/
8350 static void
8351ex_redrawstatus(eap)
8352 exarg_T *eap;
8353{
8354#if defined(FEAT_WINDOWS)
8355 int r = RedrawingDisabled;
8356 int p = p_lz;
8357
8358 RedrawingDisabled = 0;
8359 p_lz = FALSE;
8360 if (eap->forceit)
8361 status_redraw_all();
8362 else
8363 status_redraw_curbuf();
8364 update_screen(
8365# ifdef FEAT_VISUAL
8366 VIsual_active ? INVERTED :
8367# endif
8368 0);
8369 RedrawingDisabled = r;
8370 p_lz = p;
8371 out_flush();
8372#endif
8373}
8374
8375 static void
8376close_redir()
8377{
8378 if (redir_fd != NULL)
8379 {
8380 fclose(redir_fd);
8381 redir_fd = NULL;
8382 }
8383#ifdef FEAT_EVAL
8384 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008385 if (redir_vname)
8386 {
8387 var_redir_stop();
8388 redir_vname = 0;
8389 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008390#endif
8391}
8392
8393#if defined(FEAT_SESSION) && defined(USE_CRNL)
8394# define MKSESSION_NL
8395static int mksession_nl = FALSE; /* use NL only in put_eol() */
8396#endif
8397
8398/*
8399 * ":mkexrc", ":mkvimrc", ":mkview" and ":mksession".
8400 */
8401 static void
8402ex_mkrc(eap)
8403 exarg_T *eap;
8404{
8405 FILE *fd;
8406 int failed = FALSE;
8407 char_u *fname;
8408#ifdef FEAT_BROWSE
8409 char_u *browseFile = NULL;
8410#endif
8411#ifdef FEAT_SESSION
8412 int view_session = FALSE;
8413 int using_vdir = FALSE; /* using 'viewdir'? */
8414 char_u *viewFile = NULL;
8415 unsigned *flagp;
8416#endif
8417
8418 if (eap->cmdidx == CMD_mksession || eap->cmdidx == CMD_mkview)
8419 {
8420#ifdef FEAT_SESSION
8421 view_session = TRUE;
8422#else
8423 ex_ni(eap);
8424 return;
8425#endif
8426 }
8427
8428#ifdef FEAT_SESSION
8429 /* ":mkview" or ":mkview 9": generate file name with 'viewdir' */
8430 if (eap->cmdidx == CMD_mkview
8431 && (*eap->arg == NUL
8432 || (vim_isdigit(*eap->arg) && eap->arg[1] == NUL)))
8433 {
8434 eap->forceit = TRUE;
8435 fname = get_view_file(*eap->arg);
8436 if (fname == NULL)
8437 return;
8438 viewFile = fname;
8439 using_vdir = TRUE;
8440 }
8441 else
8442#endif
8443 if (*eap->arg != NUL)
8444 fname = eap->arg;
8445 else if (eap->cmdidx == CMD_mkvimrc)
8446 fname = (char_u *)VIMRC_FILE;
8447#ifdef FEAT_SESSION
8448 else if (eap->cmdidx == CMD_mksession)
8449 fname = (char_u *)SESSION_FILE;
8450#endif
8451 else
8452 fname = (char_u *)EXRC_FILE;
8453
8454#ifdef FEAT_BROWSE
8455 if (cmdmod.browse)
8456 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008457 browseFile = do_browse(BROWSE_SAVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008458# ifdef FEAT_SESSION
8459 eap->cmdidx == CMD_mkview ? (char_u *)_("Save View") :
8460 eap->cmdidx == CMD_mksession ? (char_u *)_("Save Session") :
8461# endif
8462 (char_u *)_("Save Setup"),
8463 fname, (char_u *)"vim", NULL, BROWSE_FILTER_MACROS, NULL);
8464 if (browseFile == NULL)
8465 goto theend;
8466 fname = browseFile;
8467 eap->forceit = TRUE; /* since dialog already asked */
8468 }
8469#endif
8470
8471#if defined(FEAT_SESSION) && defined(vim_mkdir)
8472 /* When using 'viewdir' may have to create the directory. */
8473 if (using_vdir && !mch_isdir(p_vdir))
Bram Moolenaardf177f62005-02-22 08:39:57 +00008474 vim_mkdir_emsg(p_vdir, 0755);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008475#endif
8476
8477 fd = open_exfile(fname, eap->forceit, WRITEBIN);
8478 if (fd != NULL)
8479 {
8480#ifdef FEAT_SESSION
8481 if (eap->cmdidx == CMD_mkview)
8482 flagp = &vop_flags;
8483 else
8484 flagp = &ssop_flags;
8485#endif
8486
8487#ifdef MKSESSION_NL
8488 /* "unix" in 'sessionoptions': use NL line separator */
8489 if (view_session && (*flagp & SSOP_UNIX))
8490 mksession_nl = TRUE;
8491#endif
8492
8493 /* Write the version command for :mkvimrc */
8494 if (eap->cmdidx == CMD_mkvimrc)
8495 (void)put_line(fd, "version 6.0");
8496
8497#ifdef FEAT_SESSION
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008498 if (eap->cmdidx == CMD_mksession)
8499 {
8500 if (put_line(fd, "let SessionLoad = 1") == FAIL)
8501 failed = TRUE;
8502 }
8503
Bram Moolenaar071d4272004-06-13 20:20:40 +00008504 if (eap->cmdidx != CMD_mkview)
8505#endif
8506 {
8507 /* Write setting 'compatible' first, because it has side effects.
8508 * For that same reason only do it when needed. */
8509 if (p_cp)
8510 (void)put_line(fd, "if !&cp | set cp | endif");
8511 else
8512 (void)put_line(fd, "if &cp | set nocp | endif");
8513 }
8514
8515#ifdef FEAT_SESSION
8516 if (!view_session
8517 || (eap->cmdidx == CMD_mksession
8518 && (*flagp & SSOP_OPTIONS)))
8519#endif
8520 failed |= (makemap(fd, NULL) == FAIL
8521 || makeset(fd, OPT_GLOBAL, FALSE) == FAIL);
8522
8523#ifdef FEAT_SESSION
8524 if (!failed && view_session)
8525 {
8526 if (put_line(fd, "let s:so_save = &so | let s:siso_save = &siso | set so=0 siso=0") == FAIL)
8527 failed = TRUE;
8528 if (eap->cmdidx == CMD_mksession)
8529 {
8530 char_u dirnow[MAXPATHL]; /* current directory */
8531
8532 /*
8533 * Change to session file's dir.
8534 */
8535 if (mch_dirname(dirnow, MAXPATHL) == FAIL
8536 || mch_chdir((char *)dirnow) != 0)
8537 *dirnow = NUL;
8538 if (*dirnow != NUL && (ssop_flags & SSOP_SESDIR))
8539 {
8540 if (vim_chdirfile(fname) == OK)
8541 shorten_fnames(TRUE);
8542 }
8543 else if (*dirnow != NUL
8544 && (ssop_flags & SSOP_CURDIR) && globaldir != NULL)
8545 {
8546 (void)mch_chdir((char *)globaldir);
8547 shorten_fnames(TRUE);
8548 }
8549
8550 failed |= (makeopens(fd, dirnow) == FAIL);
8551
8552 /* restore original dir */
8553 if (*dirnow != NUL && ((ssop_flags & SSOP_SESDIR)
8554 || ((ssop_flags & SSOP_CURDIR) && globaldir != NULL)))
8555 {
8556 if (mch_chdir((char *)dirnow) != 0)
8557 EMSG(_(e_prev_dir));
8558 shorten_fnames(TRUE);
8559 }
8560 }
8561 else
8562 {
8563 failed |= (put_view(fd, curwin, !using_vdir, flagp) == FAIL);
8564 }
8565 if (put_line(fd, "let &so = s:so_save | let &siso = s:siso_save")
8566 == FAIL)
8567 failed = TRUE;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008568 if (put_line(fd, "doautoall SessionLoadPost") == FAIL)
8569 failed = TRUE;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008570 if (eap->cmdidx == CMD_mksession)
8571 {
8572 if (put_line(fd, "unlet SessionLoad") == FAIL)
8573 failed = TRUE;
8574 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008575 }
8576#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008577 if (put_line(fd, "\" vim: set ft=vim :") == FAIL)
8578 failed = TRUE;
8579
Bram Moolenaar071d4272004-06-13 20:20:40 +00008580 failed |= fclose(fd);
8581
8582 if (failed)
8583 EMSG(_(e_write));
8584#if defined(FEAT_EVAL) && defined(FEAT_SESSION)
8585 else if (eap->cmdidx == CMD_mksession)
8586 {
8587 /* successful session write - set this_session var */
8588 char_u tbuf[MAXPATHL];
8589
8590 if (vim_FullName(fname, tbuf, MAXPATHL, FALSE) == OK)
8591 set_vim_var_string(VV_THIS_SESSION, tbuf, -1);
8592 }
8593#endif
8594#ifdef MKSESSION_NL
8595 mksession_nl = FALSE;
8596#endif
8597 }
8598
8599#ifdef FEAT_BROWSE
8600theend:
8601 vim_free(browseFile);
8602#endif
8603#ifdef FEAT_SESSION
8604 vim_free(viewFile);
8605#endif
8606}
8607
Bram Moolenaardf177f62005-02-22 08:39:57 +00008608#if ((defined(FEAT_SESSION) || defined(FEAT_EVAL)) && defined(vim_mkdir)) \
8609 || defined(PROTO)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008610/*ARGSUSED*/
Bram Moolenaardf177f62005-02-22 08:39:57 +00008611 int
8612vim_mkdir_emsg(name, prot)
8613 char_u *name;
8614 int prot;
8615{
8616 if (vim_mkdir(name, prot) != 0)
8617 {
8618 EMSG2(_("E739: Cannot create directory: %s"), name);
8619 return FAIL;
8620 }
8621 return OK;
8622}
8623#endif
8624
Bram Moolenaar071d4272004-06-13 20:20:40 +00008625/*
8626 * Open a file for writing for an Ex command, with some checks.
8627 * Return file descriptor, or NULL on failure.
8628 */
8629 FILE *
8630open_exfile(fname, forceit, mode)
8631 char_u *fname;
8632 int forceit;
8633 char *mode; /* "w" for create new file or "a" for append */
8634{
8635 FILE *fd;
8636
8637#ifdef UNIX
8638 /* with Unix it is possible to open a directory */
8639 if (mch_isdir(fname))
8640 {
8641 EMSG2(_(e_isadir2), fname);
8642 return NULL;
8643 }
8644#endif
8645 if (!forceit && *mode != 'a' && vim_fexists(fname))
8646 {
8647 EMSG2(_("E189: \"%s\" exists (add ! to override)"), fname);
8648 return NULL;
8649 }
8650
8651 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
8652 EMSG2(_("E190: Cannot open \"%s\" for writing"), fname);
8653
8654 return fd;
8655}
8656
8657/*
8658 * ":mark" and ":k".
8659 */
8660 static void
8661ex_mark(eap)
8662 exarg_T *eap;
8663{
8664 pos_T pos;
8665
8666 if (*eap->arg == NUL) /* No argument? */
8667 EMSG(_(e_argreq));
8668 else if (eap->arg[1] != NUL) /* more than one character? */
8669 EMSG(_(e_trailing));
8670 else
8671 {
8672 pos = curwin->w_cursor; /* save curwin->w_cursor */
8673 curwin->w_cursor.lnum = eap->line2;
8674 beginline(BL_WHITE | BL_FIX);
8675 if (setmark(*eap->arg) == FAIL) /* set mark */
8676 EMSG(_("E191: Argument must be a letter or forward/backward quote"));
8677 curwin->w_cursor = pos; /* restore curwin->w_cursor */
8678 }
8679}
8680
8681/*
8682 * Update w_topline, w_leftcol and the cursor position.
8683 */
8684 void
8685update_topline_cursor()
8686{
8687 check_cursor(); /* put cursor on valid line */
8688 update_topline();
8689 if (!curwin->w_p_wrap)
8690 validate_cursor();
8691 update_curswant();
8692}
8693
8694#ifdef FEAT_EX_EXTRA
8695/*
8696 * ":normal[!] {commands}": Execute normal mode commands.
8697 */
8698 static void
8699ex_normal(eap)
8700 exarg_T *eap;
8701{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008702 int save_msg_scroll = msg_scroll;
8703 int save_restart_edit = restart_edit;
8704 int save_msg_didout = msg_didout;
8705 int save_State = State;
8706 tasave_T tabuf;
8707 int save_insertmode = p_im;
8708 int save_finish_op = finish_op;
8709#ifdef FEAT_MBYTE
8710 char_u *arg = NULL;
8711 int l;
8712 char_u *p;
8713#endif
8714
8715 if (ex_normal_busy >= p_mmd)
8716 {
8717 EMSG(_("E192: Recursive use of :normal too deep"));
8718 return;
8719 }
8720 ++ex_normal_busy;
8721
8722 msg_scroll = FALSE; /* no msg scrolling in Normal mode */
8723 restart_edit = 0; /* don't go to Insert mode */
8724 p_im = FALSE; /* don't use 'insertmode' */
8725
8726#ifdef FEAT_MBYTE
8727 /*
8728 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
8729 * this for the K_SPECIAL leading byte, otherwise special keys will not
8730 * work.
8731 */
8732 if (has_mbyte)
8733 {
8734 int len = 0;
8735
8736 /* Count the number of characters to be escaped. */
8737 for (p = eap->arg; *p != NUL; ++p)
8738 {
8739# ifdef FEAT_GUI
8740 if (*p == CSI) /* leadbyte CSI */
8741 len += 2;
8742# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008743 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008744 if (*++p == K_SPECIAL /* trailbyte K_SPECIAL or CSI */
8745# ifdef FEAT_GUI
8746 || *p == CSI
8747# endif
8748 )
8749 len += 2;
8750 }
8751 if (len > 0)
8752 {
8753 arg = alloc((unsigned)(STRLEN(eap->arg) + len + 1));
8754 if (arg != NULL)
8755 {
8756 len = 0;
8757 for (p = eap->arg; *p != NUL; ++p)
8758 {
8759 arg[len++] = *p;
8760# ifdef FEAT_GUI
8761 if (*p == CSI)
8762 {
8763 arg[len++] = KS_EXTRA;
8764 arg[len++] = (int)KE_CSI;
8765 }
8766# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008767 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008768 {
8769 arg[len++] = *++p;
8770 if (*p == K_SPECIAL)
8771 {
8772 arg[len++] = KS_SPECIAL;
8773 arg[len++] = KE_FILLER;
8774 }
8775# ifdef FEAT_GUI
8776 else if (*p == CSI)
8777 {
8778 arg[len++] = KS_EXTRA;
8779 arg[len++] = (int)KE_CSI;
8780 }
8781# endif
8782 }
8783 arg[len] = NUL;
8784 }
8785 }
8786 }
8787 }
8788#endif
8789
8790 /*
8791 * Save the current typeahead. This is required to allow using ":normal"
8792 * from an event handler and makes sure we don't hang when the argument
8793 * ends with half a command.
8794 */
8795 save_typeahead(&tabuf);
8796 if (tabuf.typebuf_valid)
8797 {
8798 /*
8799 * Repeat the :normal command for each line in the range. When no
8800 * range given, execute it just once, without positioning the cursor
8801 * first.
8802 */
8803 do
8804 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008805 if (eap->addr_count != 0)
8806 {
8807 curwin->w_cursor.lnum = eap->line1++;
8808 curwin->w_cursor.col = 0;
8809 }
8810
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008811 exec_normal_cmd(
Bram Moolenaar071d4272004-06-13 20:20:40 +00008812#ifdef FEAT_MBYTE
8813 arg != NULL ? arg :
8814#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008815 eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008816 }
8817 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
8818 }
8819
8820 /* Might not return to the main loop when in an event handler. */
8821 update_topline_cursor();
8822
8823 /* Restore the previous typeahead. */
8824 restore_typeahead(&tabuf);
8825
8826 --ex_normal_busy;
8827 msg_scroll = save_msg_scroll;
8828 restart_edit = save_restart_edit;
8829 p_im = save_insertmode;
8830 finish_op = save_finish_op;
8831 msg_didout |= save_msg_didout; /* don't reset msg_didout now */
8832
8833 /* Restore the state (needed when called from a function executed for
8834 * 'indentexpr'). */
8835 State = save_State;
8836#ifdef FEAT_MBYTE
8837 vim_free(arg);
8838#endif
8839}
8840
8841/*
Bram Moolenaar64969662005-12-14 21:59:55 +00008842 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008843 */
8844 static void
8845ex_startinsert(eap)
8846 exarg_T *eap;
8847{
Bram Moolenaarfd371682005-01-14 21:42:54 +00008848 if (eap->forceit)
8849 {
8850 coladvance((colnr_T)MAXCOL);
8851 curwin->w_curswant = MAXCOL;
8852 curwin->w_set_curswant = FALSE;
8853 }
8854
Bram Moolenaara40c5002005-01-09 21:16:21 +00008855 /* Ignore the command when already in Insert mode. Inserting an
8856 * expression register that invokes a function can do this. */
8857 if (State & INSERT)
8858 return;
8859
Bram Moolenaar64969662005-12-14 21:59:55 +00008860 if (eap->cmdidx == CMD_startinsert)
8861 restart_edit = 'a';
8862 else if (eap->cmdidx == CMD_startreplace)
8863 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008864 else
Bram Moolenaar64969662005-12-14 21:59:55 +00008865 restart_edit = 'V';
8866
8867 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008868 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00008869 if (eap->cmdidx == CMD_startinsert)
8870 restart_edit = 'i';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008871 curwin->w_curswant = 0; /* avoid MAXCOL */
8872 }
8873}
8874
8875/*
8876 * ":stopinsert"
8877 */
8878/*ARGSUSED*/
8879 static void
8880ex_stopinsert(eap)
8881 exarg_T *eap;
8882{
8883 restart_edit = 0;
8884 stop_insert_mode = TRUE;
8885}
8886#endif
8887
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008888#if defined(FEAT_EX_EXTRA) || defined(FEAT_MENU) || defined(PROTO)
8889/*
8890 * Execute normal mode command "cmd".
8891 * "remap" can be REMAP_NONE or REMAP_YES.
8892 */
8893 void
8894exec_normal_cmd(cmd, remap, silent)
8895 char_u *cmd;
8896 int remap;
8897 int silent;
8898{
8899 oparg_T oa;
8900
8901 /*
8902 * Stuff the argument into the typeahead buffer.
8903 * Execute normal_cmd() until there is no typeahead left.
8904 */
8905 clear_oparg(&oa);
8906 finish_op = FALSE;
8907 ins_typebuf(cmd, remap, 0, TRUE, silent);
8908 while ((!stuff_empty() || (!typebuf_typed() && typebuf.tb_len > 0))
8909 && !got_int)
8910 {
8911 update_topline_cursor();
8912 normal_cmd(&oa, FALSE); /* execute a Normal mode cmd */
8913 }
8914}
8915#endif
8916
Bram Moolenaar071d4272004-06-13 20:20:40 +00008917#ifdef FEAT_FIND_ID
8918 static void
8919ex_checkpath(eap)
8920 exarg_T *eap;
8921{
8922 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
8923 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
8924 (linenr_T)1, (linenr_T)MAXLNUM);
8925}
8926
8927#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
8928/*
8929 * ":psearch"
8930 */
8931 static void
8932ex_psearch(eap)
8933 exarg_T *eap;
8934{
8935 g_do_tagpreview = p_pvh;
8936 ex_findpat(eap);
8937 g_do_tagpreview = 0;
8938}
8939#endif
8940
8941 static void
8942ex_findpat(eap)
8943 exarg_T *eap;
8944{
8945 int whole = TRUE;
8946 long n;
8947 char_u *p;
8948 int action;
8949
8950 switch (cmdnames[eap->cmdidx].cmd_name[2])
8951 {
8952 case 'e': /* ":psearch", ":isearch" and ":dsearch" */
8953 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
8954 action = ACTION_GOTO;
8955 else
8956 action = ACTION_SHOW;
8957 break;
8958 case 'i': /* ":ilist" and ":dlist" */
8959 action = ACTION_SHOW_ALL;
8960 break;
8961 case 'u': /* ":ijump" and ":djump" */
8962 action = ACTION_GOTO;
8963 break;
8964 default: /* ":isplit" and ":dsplit" */
8965 action = ACTION_SPLIT;
8966 break;
8967 }
8968
8969 n = 1;
8970 if (vim_isdigit(*eap->arg)) /* get count */
8971 {
8972 n = getdigits(&eap->arg);
8973 eap->arg = skipwhite(eap->arg);
8974 }
8975 if (*eap->arg == '/') /* Match regexp, not just whole words */
8976 {
8977 whole = FALSE;
8978 ++eap->arg;
8979 p = skip_regexp(eap->arg, '/', p_magic, NULL);
8980 if (*p)
8981 {
8982 *p++ = NUL;
8983 p = skipwhite(p);
8984
8985 /* Check for trailing illegal characters */
8986 if (!ends_excmd(*p))
8987 eap->errmsg = e_trailing;
8988 else
8989 eap->nextcmd = check_nextcmd(p);
8990 }
8991 }
8992 if (!eap->skip)
8993 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
8994 whole, !eap->forceit,
8995 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
8996 n, action, eap->line1, eap->line2);
8997}
8998#endif
8999
9000#ifdef FEAT_WINDOWS
9001
9002# ifdef FEAT_QUICKFIX
9003/*
9004 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
9005 */
9006 static void
9007ex_ptag(eap)
9008 exarg_T *eap;
9009{
9010 g_do_tagpreview = p_pvh;
9011 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9012}
9013
9014/*
9015 * ":pedit"
9016 */
9017 static void
9018ex_pedit(eap)
9019 exarg_T *eap;
9020{
9021 win_T *curwin_save = curwin;
9022
9023 g_do_tagpreview = p_pvh;
9024 prepare_tagpreview();
9025 keep_help_flag = curwin_save->w_buffer->b_help;
9026 do_exedit(eap, NULL);
9027 keep_help_flag = FALSE;
9028 if (curwin != curwin_save && win_valid(curwin_save))
9029 {
9030 /* Return cursor to where we were */
9031 validate_cursor();
9032 redraw_later(VALID);
9033 win_enter(curwin_save, TRUE);
9034 }
9035 g_do_tagpreview = 0;
9036}
9037# endif
9038
9039/*
9040 * ":stag", ":stselect" and ":stjump".
9041 */
9042 static void
9043ex_stag(eap)
9044 exarg_T *eap;
9045{
9046 postponed_split = -1;
9047 postponed_split_flags = cmdmod.split;
9048 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9049 postponed_split_flags = 0;
9050}
9051#endif
9052
9053/*
9054 * ":tag", ":tselect", ":tjump", ":tnext", etc.
9055 */
9056 static void
9057ex_tag(eap)
9058 exarg_T *eap;
9059{
9060 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
9061}
9062
9063 static void
9064ex_tag_cmd(eap, name)
9065 exarg_T *eap;
9066 char_u *name;
9067{
9068 int cmd;
9069
9070 switch (name[1])
9071 {
9072 case 'j': cmd = DT_JUMP; /* ":tjump" */
9073 break;
9074 case 's': cmd = DT_SELECT; /* ":tselect" */
9075 break;
9076 case 'p': cmd = DT_PREV; /* ":tprevious" */
9077 break;
9078 case 'N': cmd = DT_PREV; /* ":tNext" */
9079 break;
9080 case 'n': cmd = DT_NEXT; /* ":tnext" */
9081 break;
9082 case 'o': cmd = DT_POP; /* ":pop" */
9083 break;
9084 case 'f': /* ":tfirst" */
9085 case 'r': cmd = DT_FIRST; /* ":trewind" */
9086 break;
9087 case 'l': cmd = DT_LAST; /* ":tlast" */
9088 break;
9089 default: /* ":tag" */
9090#ifdef FEAT_CSCOPE
9091 if (p_cst)
9092 {
9093 do_cstag(eap);
9094 return;
9095 }
9096#endif
9097 cmd = DT_TAG;
9098 break;
9099 }
9100
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00009101 if (name[0] == 'l')
9102 {
9103#ifndef FEAT_QUICKFIX
9104 ex_ni(eap);
9105 return;
9106#else
9107 cmd = DT_LTAG;
9108#endif
9109 }
9110
Bram Moolenaar071d4272004-06-13 20:20:40 +00009111 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
9112 eap->forceit, TRUE);
9113}
9114
9115/*
9116 * Evaluate cmdline variables.
9117 *
9118 * change '%' to curbuf->b_ffname
9119 * '#' to curwin->w_altfile
9120 * '<cword>' to word under the cursor
9121 * '<cWORD>' to WORD under the cursor
9122 * '<cfile>' to path name under the cursor
9123 * '<sfile>' to sourced file name
9124 * '<afile>' to file name for autocommand
9125 * '<abuf>' to buffer number for autocommand
9126 * '<amatch>' to matching name for autocommand
9127 *
9128 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
9129 * "" for error without a message) and NULL is returned.
9130 * Returns an allocated string if a valid match was found.
9131 * Returns NULL if no match was found. "usedlen" then still contains the
9132 * number of characters to skip.
9133 */
9134 char_u *
9135eval_vars(src, usedlen, lnump, errormsg, srcstart)
9136 char_u *src; /* pointer into commandline */
9137 int *usedlen; /* characters after src that are used */
9138 linenr_T *lnump; /* line number for :e command, or NULL */
9139 char_u **errormsg; /* pointer to error message */
9140 char_u *srcstart; /* beginning of valid memory for src */
9141{
9142 int i;
9143 char_u *s;
9144 char_u *result;
9145 char_u *resultbuf = NULL;
9146 int resultlen;
9147 buf_T *buf;
9148 int valid = VALID_HEAD + VALID_PATH; /* assume valid result */
9149 int spec_idx;
9150#ifdef FEAT_MODIFY_FNAME
9151 int skip_mod = FALSE;
9152#endif
9153 static char *(spec_str[]) =
9154 {
9155 "%",
9156#define SPEC_PERC 0
9157 "#",
9158#define SPEC_HASH 1
9159 "<cword>", /* cursor word */
9160#define SPEC_CWORD 2
9161 "<cWORD>", /* cursor WORD */
9162#define SPEC_CCWORD 3
9163 "<cfile>", /* cursor path name */
9164#define SPEC_CFILE 4
9165 "<sfile>", /* ":so" file name */
9166#define SPEC_SFILE 5
9167#ifdef FEAT_AUTOCMD
9168 "<afile>", /* autocommand file name */
9169# define SPEC_AFILE 6
9170 "<abuf>", /* autocommand buffer number */
9171# define SPEC_ABUF 7
9172 "<amatch>", /* autocommand match name */
9173# define SPEC_AMATCH 8
9174#endif
9175#ifdef FEAT_CLIENTSERVER
9176 "<client>"
9177# define SPEC_CLIENT 9
9178#endif
9179 };
9180#define SPEC_COUNT (sizeof(spec_str) / sizeof(char *))
9181
9182#if defined(FEAT_AUTOCMD) || defined(FEAT_CLIENTSERVER)
9183 char_u strbuf[30];
9184#endif
9185
9186 *errormsg = NULL;
9187
9188 /*
9189 * Check if there is something to do.
9190 */
9191 for (spec_idx = 0; spec_idx < SPEC_COUNT; ++spec_idx)
9192 {
9193 *usedlen = (int)STRLEN(spec_str[spec_idx]);
9194 if (STRNCMP(src, spec_str[spec_idx], *usedlen) == 0)
9195 break;
9196 }
9197 if (spec_idx == SPEC_COUNT) /* no match */
9198 {
9199 *usedlen = 1;
9200 return NULL;
9201 }
9202
9203 /*
9204 * Skip when preceded with a backslash "\%" and "\#".
9205 * Note: In "\\%" the % is also not recognized!
9206 */
9207 if (src > srcstart && src[-1] == '\\')
9208 {
9209 *usedlen = 0;
9210 STRCPY(src - 1, src); /* remove backslash */
9211 return NULL;
9212 }
9213
9214 /*
9215 * word or WORD under cursor
9216 */
9217 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD)
9218 {
9219 resultlen = find_ident_under_cursor(&result, spec_idx == SPEC_CWORD ?
9220 (FIND_IDENT|FIND_STRING) : FIND_STRING);
9221 if (resultlen == 0)
9222 {
9223 *errormsg = (char_u *)"";
9224 return NULL;
9225 }
9226 }
9227
9228 /*
9229 * '#': Alternate file name
9230 * '%': Current file name
9231 * File name under the cursor
9232 * File name for autocommand
9233 * and following modifiers
9234 */
9235 else
9236 {
9237 switch (spec_idx)
9238 {
9239 case SPEC_PERC: /* '%': current file */
9240 if (curbuf->b_fname == NULL)
9241 {
9242 result = (char_u *)"";
9243 valid = 0; /* Must have ":p:h" to be valid */
9244 }
9245 else
9246#ifdef RISCOS
9247 /* Always use the full path for RISC OS if possible. */
9248 result = curbuf->b_ffname;
9249 if (result == NULL)
9250 result = curbuf->b_fname;
9251#else
9252 result = curbuf->b_fname;
9253#endif
9254 break;
9255
9256 case SPEC_HASH: /* '#' or "#99": alternate file */
9257 if (src[1] == '#') /* "##": the argument list */
9258 {
9259 result = arg_all();
9260 resultbuf = result;
9261 *usedlen = 2;
9262#ifdef FEAT_MODIFY_FNAME
9263 skip_mod = TRUE;
9264#endif
9265 break;
9266 }
9267 s = src + 1;
9268 i = (int)getdigits(&s);
9269 *usedlen = (int)(s - src); /* length of what we expand */
9270
9271 buf = buflist_findnr(i);
9272 if (buf == NULL)
9273 {
9274 *errormsg = (char_u *)_("E194: No alternate file name to substitute for '#'");
9275 return NULL;
9276 }
9277 if (lnump != NULL)
9278 *lnump = ECMD_LAST;
9279 if (buf->b_fname == NULL)
9280 {
9281 result = (char_u *)"";
9282 valid = 0; /* Must have ":p:h" to be valid */
9283 }
9284 else
9285 result = buf->b_fname;
9286 break;
9287
9288#ifdef FEAT_SEARCHPATH
9289 case SPEC_CFILE: /* file name under cursor */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009290 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009291 if (result == NULL)
9292 {
9293 *errormsg = (char_u *)"";
9294 return NULL;
9295 }
9296 resultbuf = result; /* remember allocated string */
9297 break;
9298#endif
9299
9300#ifdef FEAT_AUTOCMD
9301 case SPEC_AFILE: /* file name for autocommand */
9302 result = autocmd_fname;
9303 if (result == NULL)
9304 {
9305 *errormsg = (char_u *)_("E495: no autocommand file name to substitute for \"<afile>\"");
9306 return NULL;
9307 }
9308 break;
9309
9310 case SPEC_ABUF: /* buffer number for autocommand */
9311 if (autocmd_bufnr <= 0)
9312 {
9313 *errormsg = (char_u *)_("E496: no autocommand buffer number to substitute for \"<abuf>\"");
9314 return NULL;
9315 }
9316 sprintf((char *)strbuf, "%d", autocmd_bufnr);
9317 result = strbuf;
9318 break;
9319
9320 case SPEC_AMATCH: /* match name for autocommand */
9321 result = autocmd_match;
9322 if (result == NULL)
9323 {
9324 *errormsg = (char_u *)_("E497: no autocommand match name to substitute for \"<amatch>\"");
9325 return NULL;
9326 }
9327 break;
9328
9329#endif
9330 case SPEC_SFILE: /* file name for ":so" command */
9331 result = sourcing_name;
9332 if (result == NULL)
9333 {
9334 *errormsg = (char_u *)_("E498: no :source file name to substitute for \"<sfile>\"");
9335 return NULL;
9336 }
9337 break;
9338#if defined(FEAT_CLIENTSERVER)
9339 case SPEC_CLIENT: /* Source of last submitted input */
9340 sprintf((char *)strbuf, "0x%x", (unsigned int)clientWindow);
9341 result = strbuf;
9342 break;
9343#endif
9344 }
9345
9346 resultlen = (int)STRLEN(result); /* length of new string */
9347 if (src[*usedlen] == '<') /* remove the file name extension */
9348 {
9349 ++*usedlen;
9350#ifdef RISCOS
9351 if ((s = vim_strrchr(result, '/')) != NULL && s >= gettail(result))
9352#else
9353 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
9354#endif
9355 resultlen = (int)(s - result);
9356 }
9357#ifdef FEAT_MODIFY_FNAME
9358 else if (!skip_mod)
9359 {
9360 valid |= modify_fname(src, usedlen, &result, &resultbuf,
9361 &resultlen);
9362 if (result == NULL)
9363 {
9364 *errormsg = (char_u *)"";
9365 return NULL;
9366 }
9367 }
9368#endif
9369 }
9370
9371 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
9372 {
9373 if (valid != VALID_HEAD + VALID_PATH)
9374 /* xgettext:no-c-format */
9375 *errormsg = (char_u *)_("E499: Empty file name for '%' or '#', only works with \":p:h\"");
9376 else
9377 *errormsg = (char_u *)_("E500: Evaluates to an empty string");
9378 result = NULL;
9379 }
9380 else
9381 result = vim_strnsave(result, resultlen);
9382 vim_free(resultbuf);
9383 return result;
9384}
9385
9386/*
9387 * Concatenate all files in the argument list, separated by spaces, and return
9388 * it in one allocated string.
9389 * Spaces and backslashes in the file names are escaped with a backslash.
9390 * Returns NULL when out of memory.
9391 */
9392 static char_u *
9393arg_all()
9394{
9395 int len;
9396 int idx;
9397 char_u *retval = NULL;
9398 char_u *p;
9399
9400 /*
9401 * Do this loop two times:
9402 * first time: compute the total length
9403 * second time: concatenate the names
9404 */
9405 for (;;)
9406 {
9407 len = 0;
9408 for (idx = 0; idx < ARGCOUNT; ++idx)
9409 {
9410 p = alist_name(&ARGLIST[idx]);
9411 if (p != NULL)
9412 {
9413 if (len > 0)
9414 {
9415 /* insert a space in between names */
9416 if (retval != NULL)
9417 retval[len] = ' ';
9418 ++len;
9419 }
9420 for ( ; *p != NUL; ++p)
9421 {
9422 if (*p == ' ' || *p == '\\')
9423 {
9424 /* insert a backslash */
9425 if (retval != NULL)
9426 retval[len] = '\\';
9427 ++len;
9428 }
9429 if (retval != NULL)
9430 retval[len] = *p;
9431 ++len;
9432 }
9433 }
9434 }
9435
9436 /* second time: break here */
9437 if (retval != NULL)
9438 {
9439 retval[len] = NUL;
9440 break;
9441 }
9442
9443 /* allocate memory */
9444 retval = alloc(len + 1);
9445 if (retval == NULL)
9446 break;
9447 }
9448
9449 return retval;
9450}
9451
9452#if defined(FEAT_AUTOCMD) || defined(PROTO)
9453/*
9454 * Expand the <sfile> string in "arg".
9455 *
9456 * Returns an allocated string, or NULL for any error.
9457 */
9458 char_u *
9459expand_sfile(arg)
9460 char_u *arg;
9461{
9462 char_u *errormsg;
9463 int len;
9464 char_u *result;
9465 char_u *newres;
9466 char_u *repl;
9467 int srclen;
9468 char_u *p;
9469
9470 result = vim_strsave(arg);
9471 if (result == NULL)
9472 return NULL;
9473
9474 for (p = result; *p; )
9475 {
9476 if (STRNCMP(p, "<sfile>", 7) != 0)
9477 ++p;
9478 else
9479 {
9480 /* replace "<sfile>" with the sourced file name, and do ":" stuff */
9481 repl = eval_vars(p, &srclen, NULL, &errormsg, result);
9482 if (errormsg != NULL)
9483 {
9484 if (*errormsg)
9485 emsg(errormsg);
9486 vim_free(result);
9487 return NULL;
9488 }
9489 if (repl == NULL) /* no match (cannot happen) */
9490 {
9491 p += srclen;
9492 continue;
9493 }
9494 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
9495 newres = alloc(len);
9496 if (newres == NULL)
9497 {
9498 vim_free(repl);
9499 vim_free(result);
9500 return NULL;
9501 }
9502 mch_memmove(newres, result, (size_t)(p - result));
9503 STRCPY(newres + (p - result), repl);
9504 len = (int)STRLEN(newres);
9505 STRCAT(newres, p + srclen);
9506 vim_free(repl);
9507 vim_free(result);
9508 result = newres;
9509 p = newres + len; /* continue after the match */
9510 }
9511 }
9512
9513 return result;
9514}
9515#endif
9516
9517#ifdef FEAT_SESSION
9518static int ses_winsizes __ARGS((FILE *fd, int restore_size));
9519static int ses_win_rec __ARGS((FILE *fd, frame_T *fr));
9520static frame_T *ses_skipframe __ARGS((frame_T *fr));
9521static int ses_do_frame __ARGS((frame_T *fr));
9522static int ses_do_win __ARGS((win_T *wp));
9523static int ses_arglist __ARGS((FILE *fd, char *cmd, garray_T *gap, int fullname, unsigned *flagp));
9524static int ses_put_fname __ARGS((FILE *fd, char_u *name, unsigned *flagp));
9525static int ses_fname __ARGS((FILE *fd, buf_T *buf, unsigned *flagp));
9526
9527/*
9528 * Write openfile commands for the current buffers to an .exrc file.
9529 * Return FAIL on error, OK otherwise.
9530 */
9531 static int
9532makeopens(fd, dirnow)
9533 FILE *fd;
9534 char_u *dirnow; /* Current directory name */
9535{
9536 buf_T *buf;
9537 int only_save_windows = TRUE;
9538 int nr;
9539 int cnr = 1;
9540 int restore_size = TRUE;
9541 win_T *wp;
9542 char_u *sname;
9543 win_T *edited_win = NULL;
9544
9545 if (ssop_flags & SSOP_BUFFERS)
9546 only_save_windows = FALSE; /* Save ALL buffers */
9547
9548 /*
9549 * Begin by setting the this_session variable, and then other
9550 * sessionable variables.
9551 */
9552#ifdef FEAT_EVAL
9553 if (put_line(fd, "let v:this_session=expand(\"<sfile>:p\")") == FAIL)
9554 return FAIL;
9555 if (ssop_flags & SSOP_GLOBALS)
9556 if (store_session_globals(fd) == FAIL)
9557 return FAIL;
9558#endif
9559
9560 /*
9561 * Close all windows but one.
9562 */
9563 if (put_line(fd, "silent only") == FAIL)
9564 return FAIL;
9565
9566 /*
9567 * Now a :cd command to the session directory or the current directory
9568 */
9569 if (ssop_flags & SSOP_SESDIR)
9570 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009571 if (put_line(fd, "exe \"cd \" . escape(expand(\"<sfile>:p:h\"), ' ')")
9572 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009573 return FAIL;
9574 }
9575 else if (ssop_flags & SSOP_CURDIR)
9576 {
9577 sname = home_replace_save(NULL, globaldir != NULL ? globaldir : dirnow);
9578 if (sname == NULL
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009579 || fputs("cd ", fd) < 0
9580 || ses_put_fname(fd, sname, &ssop_flags) == FAIL
9581 || put_eol(fd) == FAIL)
9582 {
9583 vim_free(sname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009584 return FAIL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009585 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009586 vim_free(sname);
9587 }
9588
9589 /*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009590 * If there is an empty, unnamed buffer we will wipe it out later.
9591 * Remember the buffer number.
9592 */
9593 if (put_line(fd, "if expand('%') == '' && !&modified && line('$') <= 1 && getline(1) == ''") == FAIL)
9594 return FAIL;
9595 if (put_line(fd, " let s:wipebuf = bufnr('%')") == FAIL)
9596 return FAIL;
9597 if (put_line(fd, "endif") == FAIL)
9598 return FAIL;
9599
9600 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009601 * Now save the current files, current buffer first.
9602 */
9603 if (put_line(fd, "set shortmess=aoO") == FAIL)
9604 return FAIL;
9605
9606 /* Now put the other buffers into the buffer list */
9607 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9608 {
9609 if (!(only_save_windows && buf->b_nwindows == 0)
9610 && !(buf->b_help && !(ssop_flags & SSOP_HELP))
9611 && buf->b_fname != NULL
9612 && buf->b_p_bl)
9613 {
9614 if (fprintf(fd, "badd +%ld ", buf->b_wininfo == NULL ? 1L
9615 : buf->b_wininfo->wi_fpos.lnum) < 0
9616 || ses_fname(fd, buf, &ssop_flags) == FAIL)
9617 return FAIL;
9618 }
9619 }
9620
9621 /* the global argument list */
9622 if (ses_arglist(fd, "args", &global_alist.al_ga,
9623 !(ssop_flags & SSOP_CURDIR), &ssop_flags) == FAIL)
9624 return FAIL;
9625
9626 if (ssop_flags & SSOP_RESIZE)
9627 {
9628 /* Note: after the restore we still check it worked!*/
9629 if (fprintf(fd, "set lines=%ld columns=%ld" , Rows, Columns) < 0
9630 || put_eol(fd) == FAIL)
9631 return FAIL;
9632 }
9633
9634#ifdef FEAT_GUI
9635 if (gui.in_use && (ssop_flags & SSOP_WINPOS))
9636 {
9637 int x, y;
9638
9639 if (gui_mch_get_winpos(&x, &y) == OK)
9640 {
9641 /* Note: after the restore we still check it worked!*/
9642 if (fprintf(fd, "winpos %d %d", x, y) < 0 || put_eol(fd) == FAIL)
9643 return FAIL;
9644 }
9645 }
9646#endif
9647
9648 /*
9649 * Before creating the window layout, try loading one file. If this is
9650 * aborted we don't end up with a number of useless windows.
9651 * This may have side effects! (e.g., compressed or network file).
9652 */
9653 for (wp = firstwin; wp != NULL; wp = wp->w_next)
9654 {
9655 if (ses_do_win(wp)
9656 && wp->w_buffer->b_ffname != NULL
9657 && !wp->w_buffer->b_help
9658#ifdef FEAT_QUICKFIX
9659 && !bt_nofile(wp->w_buffer)
9660#endif
9661 )
9662 {
9663 if (fputs("edit ", fd) < 0
9664 || ses_fname(fd, wp->w_buffer, &ssop_flags) == FAIL)
9665 return FAIL;
9666 if (!wp->w_arg_idx_invalid)
9667 edited_win = wp;
9668 break;
9669 }
9670 }
9671
9672 /*
9673 * Save current window layout.
9674 */
9675 if (put_line(fd, "set splitbelow splitright") == FAIL)
9676 return FAIL;
9677 if (ses_win_rec(fd, topframe) == FAIL)
9678 return FAIL;
9679 if (!p_sb && put_line(fd, "set nosplitbelow") == FAIL)
9680 return FAIL;
9681 if (!p_spr && put_line(fd, "set nosplitright") == FAIL)
9682 return FAIL;
9683
9684 /*
9685 * Check if window sizes can be restored (no windows omitted).
9686 * Remember the window number of the current window after restoring.
9687 */
9688 nr = 0;
9689 for (wp = firstwin; wp != NULL; wp = W_NEXT(wp))
9690 {
9691 if (ses_do_win(wp))
9692 ++nr;
9693 else
9694 restore_size = FALSE;
9695 if (curwin == wp)
9696 cnr = nr;
9697 }
9698
9699 /* Go to the first window. */
9700 if (put_line(fd, "wincmd t") == FAIL)
9701 return FAIL;
9702
9703 /*
9704 * If more than one window, see if sizes can be restored.
9705 * First set 'winheight' and 'winwidth' to 1 to avoid the windows being
9706 * resized when moving between windows.
9707 * Do this before restoring the view, so that the topline and the cursor
9708 * can be set. This is done again below.
9709 */
9710 if (put_line(fd, "set winheight=1 winwidth=1") == FAIL)
9711 return FAIL;
9712 if (nr > 1 && ses_winsizes(fd, restore_size) == FAIL)
9713 return FAIL;
9714
9715 /*
9716 * Restore the view of the window (options, file, cursor, etc.).
9717 */
9718 for (wp = firstwin; wp != NULL; wp = wp->w_next)
9719 {
9720 if (!ses_do_win(wp))
9721 continue;
9722 if (put_view(fd, wp, wp != edited_win, &ssop_flags) == FAIL)
9723 return FAIL;
9724 if (nr > 1 && put_line(fd, "wincmd w") == FAIL)
9725 return FAIL;
9726 }
9727
9728 /*
9729 * Restore cursor to the current window if it's not the first one.
9730 */
9731 if (cnr > 1 && (fprintf(fd, "%dwincmd w", cnr) < 0 || put_eol(fd) == FAIL))
9732 return FAIL;
9733
9734 /*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009735 * Wipe out an empty unnamed buffer we started in.
9736 */
9737 if (put_line(fd, "if exists('s:wipebuf')") == FAIL)
9738 return FAIL;
9739 if (put_line(fd, " exe 'bwipe ' . s:wipebuf") == FAIL)
9740 return FAIL;
9741 if (put_line(fd, "endif") == FAIL)
9742 return FAIL;
9743 if (put_line(fd, "unlet! s:wipebuf") == FAIL)
9744 return FAIL;
9745
9746 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009747 * Restore window sizes again after jumping around in windows, because the
9748 * current window has a minimum size while others may not.
9749 */
9750 if (nr > 1 && ses_winsizes(fd, restore_size) == FAIL)
9751 return FAIL;
9752
9753 /* Re-apply 'winheight', 'winwidth' and 'shortmess'. */
9754 if (fprintf(fd, "set winheight=%ld winwidth=%ld shortmess=%s",
9755 p_wh, p_wiw, p_shm) < 0 || put_eol(fd) == FAIL)
9756 return FAIL;
9757
9758 /*
9759 * Lastly, execute the x.vim file if it exists.
9760 */
9761 if (put_line(fd, "let s:sx = expand(\"<sfile>:p:r\").\"x.vim\"") == FAIL
9762 || put_line(fd, "if file_readable(s:sx)") == FAIL
9763 || put_line(fd, " exe \"source \" . s:sx") == FAIL
9764 || put_line(fd, "endif") == FAIL)
9765 return FAIL;
9766
9767 return OK;
9768}
9769
9770 static int
9771ses_winsizes(fd, restore_size)
9772 FILE *fd;
9773 int restore_size;
9774{
9775 int n = 0;
9776 win_T *wp;
9777
9778 if (restore_size && (ssop_flags & SSOP_WINSIZE))
9779 {
9780 for (wp = firstwin; wp != NULL; wp = wp->w_next)
9781 {
9782 if (!ses_do_win(wp))
9783 continue;
9784 ++n;
9785
9786 /* restore height when not full height */
9787 if (wp->w_height + wp->w_status_height < topframe->fr_height
9788 && (fprintf(fd,
9789 "exe '%dresize ' . ((&lines * %ld + %ld) / %ld)",
9790 n, (long)wp->w_height, Rows / 2, Rows) < 0
9791 || put_eol(fd) == FAIL))
9792 return FAIL;
9793
9794 /* restore width when not full width */
9795 if (wp->w_width < Columns && (fprintf(fd,
9796 "exe 'vert %dresize ' . ((&columns * %ld + %ld) / %ld)",
9797 n, (long)wp->w_width, Columns / 2, Columns) < 0
9798 || put_eol(fd) == FAIL))
9799 return FAIL;
9800 }
9801 }
9802 else
9803 {
9804 /* Just equalise window sizes */
9805 if (put_line(fd, "wincmd =") == FAIL)
9806 return FAIL;
9807 }
9808 return OK;
9809}
9810
9811/*
9812 * Write commands to "fd" to recursively create windows for frame "fr",
9813 * horizontally and vertically split.
9814 * After the commands the last window in the frame is the current window.
9815 * Returns FAIL when writing the commands to "fd" fails.
9816 */
9817 static int
9818ses_win_rec(fd, fr)
9819 FILE *fd;
9820 frame_T *fr;
9821{
9822 frame_T *frc;
9823 int count = 0;
9824
9825 if (fr->fr_layout != FR_LEAF)
9826 {
9827 /* Find first frame that's not skipped and then create a window for
9828 * each following one (first frame is already there). */
9829 frc = ses_skipframe(fr->fr_child);
9830 if (frc != NULL)
9831 while ((frc = ses_skipframe(frc->fr_next)) != NULL)
9832 {
9833 /* Make window as big as possible so that we have lots of room
9834 * to split. */
9835 if (put_line(fd, "wincmd _ | wincmd |") == FAIL
9836 || put_line(fd, fr->fr_layout == FR_COL
9837 ? "split" : "vsplit") == FAIL)
9838 return FAIL;
9839 ++count;
9840 }
9841
9842 /* Go back to the first window. */
9843 if (count > 0 && (fprintf(fd, fr->fr_layout == FR_COL
9844 ? "%dwincmd k" : "%dwincmd h", count) < 0
9845 || put_eol(fd) == FAIL))
9846 return FAIL;
9847
9848 /* Recursively create frames/windows in each window of this column or
9849 * row. */
9850 frc = ses_skipframe(fr->fr_child);
9851 while (frc != NULL)
9852 {
9853 ses_win_rec(fd, frc);
9854 frc = ses_skipframe(frc->fr_next);
9855 /* Go to next window. */
9856 if (frc != NULL && put_line(fd, "wincmd w") == FAIL)
9857 return FAIL;
9858 }
9859 }
9860 return OK;
9861}
9862
9863/*
9864 * Skip frames that don't contain windows we want to save in the Session.
9865 * Returns NULL when there none.
9866 */
9867 static frame_T *
9868ses_skipframe(fr)
9869 frame_T *fr;
9870{
9871 frame_T *frc;
9872
9873 for (frc = fr; frc != NULL; frc = frc->fr_next)
9874 if (ses_do_frame(frc))
9875 break;
9876 return frc;
9877}
9878
9879/*
9880 * Return TRUE if frame "fr" has a window somewhere that we want to save in
9881 * the Session.
9882 */
9883 static int
9884ses_do_frame(fr)
9885 frame_T *fr;
9886{
9887 frame_T *frc;
9888
9889 if (fr->fr_layout == FR_LEAF)
9890 return ses_do_win(fr->fr_win);
9891 for (frc = fr->fr_child; frc != NULL; frc = frc->fr_next)
9892 if (ses_do_frame(frc))
9893 return TRUE;
9894 return FALSE;
9895}
9896
9897/*
9898 * Return non-zero if window "wp" is to be stored in the Session.
9899 */
9900 static int
9901ses_do_win(wp)
9902 win_T *wp;
9903{
9904 if (wp->w_buffer->b_fname == NULL
9905#ifdef FEAT_QUICKFIX
9906 /* When 'buftype' is "nofile" can't restore the window contents. */
9907 || bt_nofile(wp->w_buffer)
9908#endif
9909 )
9910 return (ssop_flags & SSOP_BLANK);
9911 if (wp->w_buffer->b_help)
9912 return (ssop_flags & SSOP_HELP);
9913 return TRUE;
9914}
9915
9916/*
9917 * Write commands to "fd" to restore the view of a window.
9918 * Caller must make sure 'scrolloff' is zero.
9919 */
9920 static int
9921put_view(fd, wp, add_edit, flagp)
9922 FILE *fd;
9923 win_T *wp;
9924 int add_edit; /* add ":edit" command to view */
9925 unsigned *flagp; /* vop_flags or ssop_flags */
9926{
9927 win_T *save_curwin;
9928 int f;
9929 int do_cursor;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00009930 int did_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009931
9932 /* Always restore cursor position for ":mksession". For ":mkview" only
9933 * when 'viewoptions' contains "cursor". */
9934 do_cursor = (flagp == &ssop_flags || *flagp & SSOP_CURSOR);
9935
9936 /*
9937 * Local argument list.
9938 */
9939 if (wp->w_alist == &global_alist)
9940 {
9941 if (put_line(fd, "argglobal") == FAIL)
9942 return FAIL;
9943 }
9944 else
9945 {
9946 if (ses_arglist(fd, "arglocal", &wp->w_alist->al_ga,
9947 flagp == &vop_flags
9948 || !(*flagp & SSOP_CURDIR)
9949 || wp->w_localdir != NULL, flagp) == FAIL)
9950 return FAIL;
9951 }
9952
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00009953 /* Only when part of a session: restore the argument index. Some
9954 * arguments may have been deleted, check if the index is valid. */
9955 if (wp->w_arg_idx != 0 && wp->w_arg_idx <= WARGCOUNT(wp)
9956 && flagp == &ssop_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009957 {
9958 if (fprintf(fd, "%ldnext", (long)wp->w_arg_idx) < 0
9959 || put_eol(fd) == FAIL)
9960 return FAIL;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00009961 did_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009962 }
9963
9964 /* Edit the file. Skip this when ":next" already did it. */
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00009965 if (add_edit && (!did_next || wp->w_arg_idx_invalid))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009966 {
9967 /*
9968 * Load the file.
9969 */
9970 if (wp->w_buffer->b_ffname != NULL
9971#ifdef FEAT_QUICKFIX
9972 && !bt_nofile(wp->w_buffer)
9973#endif
9974 )
9975 {
9976 /*
9977 * Editing a file in this buffer: use ":edit file".
9978 * This may have side effects! (e.g., compressed or network file).
9979 */
9980 if (fputs("edit ", fd) < 0
9981 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
9982 return FAIL;
9983 }
9984 else
9985 {
9986 /* No file in this buffer, just make it empty. */
9987 if (put_line(fd, "enew") == FAIL)
9988 return FAIL;
9989#ifdef FEAT_QUICKFIX
9990 if (wp->w_buffer->b_ffname != NULL)
9991 {
9992 /* The buffer does have a name, but it's not a file name. */
9993 if (fputs("file ", fd) < 0
9994 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
9995 return FAIL;
9996 }
9997#endif
9998 do_cursor = FALSE;
9999 }
10000 }
10001
10002 /*
10003 * Local mappings and abbreviations.
10004 */
10005 if ((*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
10006 && makemap(fd, wp->w_buffer) == FAIL)
10007 return FAIL;
10008
10009 /*
10010 * Local options. Need to go to the window temporarily.
10011 * Store only local values when using ":mkview" and when ":mksession" is
10012 * used and 'sessionoptions' doesn't include "options".
10013 * Some folding options are always stored when "folds" is included,
10014 * otherwise the folds would not be restored correctly.
10015 */
10016 save_curwin = curwin;
10017 curwin = wp;
10018 curbuf = curwin->w_buffer;
10019 if (*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
10020 f = makeset(fd, OPT_LOCAL,
10021 flagp == &vop_flags || !(*flagp & SSOP_OPTIONS));
10022#ifdef FEAT_FOLDING
10023 else if (*flagp & SSOP_FOLDS)
10024 f = makefoldset(fd);
10025#endif
10026 else
10027 f = OK;
10028 curwin = save_curwin;
10029 curbuf = curwin->w_buffer;
10030 if (f == FAIL)
10031 return FAIL;
10032
10033#ifdef FEAT_FOLDING
10034 /*
10035 * Save Folds when 'buftype' is empty and for help files.
10036 */
10037 if ((*flagp & SSOP_FOLDS)
10038 && wp->w_buffer->b_ffname != NULL
Bram Moolenaarb1b715d2006-01-21 22:09:43 +000010039# ifdef FEAT_QUICKFIX
10040 && (*wp->w_buffer->b_p_bt == NUL || wp->w_buffer->b_help)
10041# endif
10042 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000010043 {
10044 if (put_folds(fd, wp) == FAIL)
10045 return FAIL;
10046 }
10047#endif
10048
10049 /*
10050 * Set the cursor after creating folds, since that moves the cursor.
10051 */
10052 if (do_cursor)
10053 {
10054
10055 /* Restore the cursor line in the file and relatively in the
10056 * window. Don't use "G", it changes the jumplist. */
10057 if (fprintf(fd, "let s:l = %ld - ((%ld * winheight(0) + %ld) / %ld)",
10058 (long)wp->w_cursor.lnum,
10059 (long)(wp->w_cursor.lnum - wp->w_topline),
10060 (long)wp->w_height / 2, (long)wp->w_height) < 0
10061 || put_eol(fd) == FAIL
10062 || put_line(fd, "if s:l < 1 | let s:l = 1 | endif") == FAIL
10063 || put_line(fd, "exe s:l") == FAIL
10064 || put_line(fd, "normal! zt") == FAIL
10065 || fprintf(fd, "%ld", (long)wp->w_cursor.lnum) < 0
10066 || put_eol(fd) == FAIL)
10067 return FAIL;
10068 /* Restore the cursor column and left offset when not wrapping. */
10069 if (wp->w_cursor.col == 0)
10070 {
10071 if (put_line(fd, "normal! 0") == FAIL)
10072 return FAIL;
10073 }
10074 else
10075 {
10076 if (!wp->w_p_wrap && wp->w_leftcol > 0 && wp->w_width > 0)
10077 {
10078 if (fprintf(fd,
10079 "let s:c = %ld - ((%ld * winwidth(0) + %ld) / %ld)",
10080 (long)wp->w_cursor.col,
10081 (long)(wp->w_cursor.col - wp->w_leftcol),
10082 (long)wp->w_width / 2, (long)wp->w_width) < 0
10083 || put_eol(fd) == FAIL
10084 || put_line(fd, "if s:c > 0") == FAIL
10085 || fprintf(fd,
10086 " exe 'normal! 0' . s:c . 'lzs' . (%ld - s:c) . 'l'",
10087 (long)wp->w_cursor.col) < 0
10088 || put_eol(fd) == FAIL
10089 || put_line(fd, "else") == FAIL
10090 || fprintf(fd, " normal! 0%dl", wp->w_cursor.col) < 0
10091 || put_eol(fd) == FAIL
10092 || put_line(fd, "endif") == FAIL)
10093 return FAIL;
10094 }
10095 else
10096 {
10097 if (fprintf(fd, "normal! 0%dl", wp->w_cursor.col) < 0
10098 || put_eol(fd) == FAIL)
10099 return FAIL;
10100 }
10101 }
10102 }
10103
10104 /*
10105 * Local directory.
10106 */
10107 if (wp->w_localdir != NULL)
10108 {
10109 if (fputs("lcd ", fd) < 0
10110 || ses_put_fname(fd, wp->w_localdir, flagp) == FAIL
10111 || put_eol(fd) == FAIL)
10112 return FAIL;
10113 }
10114
10115 return OK;
10116}
10117
10118/*
10119 * Write an argument list to the session file.
10120 * Returns FAIL if writing fails.
10121 */
10122 static int
10123ses_arglist(fd, cmd, gap, fullname, flagp)
10124 FILE *fd;
10125 char *cmd;
10126 garray_T *gap;
10127 int fullname; /* TRUE: use full path name */
10128 unsigned *flagp;
10129{
10130 int i;
10131 char_u buf[MAXPATHL];
10132 char_u *s;
10133
10134 if (gap->ga_len == 0)
10135 return put_line(fd, "silent! argdel *");
10136 if (fputs(cmd, fd) < 0)
10137 return FAIL;
10138 for (i = 0; i < gap->ga_len; ++i)
10139 {
10140 /* NULL file names are skipped (only happens when out of memory). */
10141 s = alist_name(&((aentry_T *)gap->ga_data)[i]);
10142 if (s != NULL)
10143 {
10144 if (fullname)
10145 {
10146 (void)vim_FullName(s, buf, MAXPATHL, FALSE);
10147 s = buf;
10148 }
10149 if (fputs(" ", fd) < 0 || ses_put_fname(fd, s, flagp) == FAIL)
10150 return FAIL;
10151 }
10152 }
10153 return put_eol(fd);
10154}
10155
10156/*
10157 * Write a buffer name to the session file.
10158 * Also ends the line.
10159 * Returns FAIL if writing fails.
10160 */
10161 static int
10162ses_fname(fd, buf, flagp)
10163 FILE *fd;
10164 buf_T *buf;
10165 unsigned *flagp;
10166{
10167 char_u *name;
10168
10169 /* Use the short file name if the current directory is known at the time
10170 * the session file will be sourced. Don't do this for ":mkview", we
10171 * don't know the current directory. */
10172 if (buf->b_sfname != NULL
10173 && flagp == &ssop_flags
10174 && (ssop_flags & (SSOP_CURDIR | SSOP_SESDIR)))
10175 name = buf->b_sfname;
10176 else
10177 name = buf->b_ffname;
10178 if (ses_put_fname(fd, name, flagp) == FAIL || put_eol(fd) == FAIL)
10179 return FAIL;
10180 return OK;
10181}
10182
10183/*
10184 * Write a file name to the session file.
10185 * Takes care of the "slash" option in 'sessionoptions' and escapes special
10186 * characters.
10187 * Returns FAIL if writing fails.
10188 */
10189 static int
10190ses_put_fname(fd, name, flagp)
10191 FILE *fd;
10192 char_u *name;
10193 unsigned *flagp;
10194{
10195 char_u *sname;
10196 int retval = OK;
10197 int c;
10198
10199 sname = home_replace_save(NULL, name);
10200 if (sname != NULL)
10201 name = sname;
10202 while (*name != NUL)
10203 {
10204#ifdef FEAT_MBYTE
10205 {
10206 int l;
10207
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010208 if (has_mbyte && (l = (*mb_ptr2len)(name)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010209 {
10210 /* copy a multibyte char */
10211 while (--l >= 0)
10212 {
10213 if (putc(*name, fd) != *name)
10214 retval = FAIL;
10215 ++name;
10216 }
10217 continue;
10218 }
10219 }
10220#endif
10221 c = *name++;
10222 if (c == '\\' && (*flagp & SSOP_SLASH))
10223 /* change a backslash to a forward slash */
10224 c = '/';
10225 else if ((vim_strchr(escape_chars, c) != NULL
10226#ifdef BACKSLASH_IN_FILENAME
10227 && c != '\\'
10228#endif
10229 ) || c == '#' || c == '%')
10230 {
10231 /* escape a special character with a backslash */
10232 if (putc('\\', fd) != '\\')
10233 retval = FAIL;
10234 }
10235 if (putc(c, fd) != c)
10236 retval = FAIL;
10237 }
10238 vim_free(sname);
10239 return retval;
10240}
10241
10242/*
10243 * ":loadview [nr]"
10244 */
10245 static void
10246ex_loadview(eap)
10247 exarg_T *eap;
10248{
10249 char_u *fname;
10250
10251 fname = get_view_file(*eap->arg);
10252 if (fname != NULL)
10253 {
10254 do_source(fname, FALSE, FALSE);
10255 vim_free(fname);
10256 }
10257}
10258
10259/*
10260 * Get the name of the view file for the current buffer.
10261 */
10262 static char_u *
10263get_view_file(c)
10264 int c;
10265{
10266 int len = 0;
10267 char_u *p, *s;
10268 char_u *retval;
10269 char_u *sname;
10270
10271 if (curbuf->b_ffname == NULL)
10272 {
10273 EMSG(_(e_noname));
10274 return NULL;
10275 }
10276 sname = home_replace_save(NULL, curbuf->b_ffname);
10277 if (sname == NULL)
10278 return NULL;
10279
10280 /*
10281 * We want a file name without separators, because we're not going to make
10282 * a directory.
10283 * "normal" path separator -> "=+"
10284 * "=" -> "=="
10285 * ":" path separator -> "=-"
10286 */
10287 for (p = sname; *p; ++p)
10288 if (*p == '=' || vim_ispathsep(*p))
10289 ++len;
10290 retval = alloc((unsigned)(STRLEN(sname) + len + STRLEN(p_vdir) + 9));
10291 if (retval != NULL)
10292 {
10293 STRCPY(retval, p_vdir);
10294 add_pathsep(retval);
10295 s = retval + STRLEN(retval);
10296 for (p = sname; *p; ++p)
10297 {
10298 if (*p == '=')
10299 {
10300 *s++ = '=';
10301 *s++ = '=';
10302 }
10303 else if (vim_ispathsep(*p))
10304 {
10305 *s++ = '=';
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000010306#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA) || defined(RISCOS) \
Bram Moolenaar071d4272004-06-13 20:20:40 +000010307 || defined(VMS)
10308 if (*p == ':')
10309 *s++ = '-';
10310 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010311#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000010312 *s++ = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010313 }
10314 else
10315 *s++ = *p;
10316 }
10317 *s++ = '=';
10318 *s++ = c;
10319 STRCPY(s, ".vim");
10320 }
10321
10322 vim_free(sname);
10323 return retval;
10324}
10325
10326#endif /* FEAT_SESSION */
10327
10328/*
10329 * Write end-of-line character(s) for ":mkexrc", ":mkvimrc" and ":mksession".
10330 * Return FAIL for a write error.
10331 */
10332 int
10333put_eol(fd)
10334 FILE *fd;
10335{
10336 if (
10337#ifdef USE_CRNL
10338 (
10339# ifdef MKSESSION_NL
10340 !mksession_nl &&
10341# endif
10342 (putc('\r', fd) < 0)) ||
10343#endif
10344 (putc('\n', fd) < 0))
10345 return FAIL;
10346 return OK;
10347}
10348
10349/*
10350 * Write a line to "fd".
10351 * Return FAIL for a write error.
10352 */
10353 int
10354put_line(fd, s)
10355 FILE *fd;
10356 char *s;
10357{
10358 if (fputs(s, fd) < 0 || put_eol(fd) == FAIL)
10359 return FAIL;
10360 return OK;
10361}
10362
10363#ifdef FEAT_VIMINFO
10364/*
10365 * ":rviminfo" and ":wviminfo".
10366 */
10367 static void
10368ex_viminfo(eap)
10369 exarg_T *eap;
10370{
10371 char_u *save_viminfo;
10372
10373 save_viminfo = p_viminfo;
10374 if (*p_viminfo == NUL)
10375 p_viminfo = (char_u *)"'100";
10376 if (eap->cmdidx == CMD_rviminfo)
10377 {
10378 if (read_viminfo(eap->arg, TRUE, TRUE, eap->forceit) == FAIL)
10379 EMSG(_("E195: Cannot open viminfo file for reading"));
10380 }
10381 else
10382 write_viminfo(eap->arg, eap->forceit);
10383 p_viminfo = save_viminfo;
10384}
10385#endif
10386
10387#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +000010388/*
10389 * Make a dialog message in "buff[IOSIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +000010390 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +000010391 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010392 void
10393dialog_msg(buff, format, fname)
10394 char_u *buff;
10395 char *format;
10396 char_u *fname;
10397{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010398 if (fname == NULL)
10399 fname = (char_u *)_("Untitled");
Bram Moolenaarb765d632005-06-07 21:00:02 +000010400 vim_snprintf((char *)buff, IOSIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010401}
10402#endif
10403
10404/*
10405 * ":behave {mswin,xterm}"
10406 */
10407 static void
10408ex_behave(eap)
10409 exarg_T *eap;
10410{
10411 if (STRCMP(eap->arg, "mswin") == 0)
10412 {
10413 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
10414 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
10415 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
10416 set_option_value((char_u *)"keymodel", 0L,
10417 (char_u *)"startsel,stopsel", 0);
10418 }
10419 else if (STRCMP(eap->arg, "xterm") == 0)
10420 {
10421 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
10422 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
10423 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
10424 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
10425 }
10426 else
10427 EMSG2(_(e_invarg2), eap->arg);
10428}
10429
10430#ifdef FEAT_AUTOCMD
10431static int filetype_detect = FALSE;
10432static int filetype_plugin = FALSE;
10433static int filetype_indent = FALSE;
10434
10435/*
10436 * ":filetype [plugin] [indent] {on,off,detect}"
10437 * on: Load the filetype.vim file to install autocommands for file types.
10438 * off: Load the ftoff.vim file to remove all autocommands for file types.
10439 * plugin on: load filetype.vim and ftplugin.vim
10440 * plugin off: load ftplugof.vim
10441 * indent on: load filetype.vim and indent.vim
10442 * indent off: load indoff.vim
10443 */
10444 static void
10445ex_filetype(eap)
10446 exarg_T *eap;
10447{
10448 char_u *arg = eap->arg;
10449 int plugin = FALSE;
10450 int indent = FALSE;
10451
10452 if (*eap->arg == NUL)
10453 {
10454 /* Print current status. */
10455 smsg((char_u *)"filetype detection:%s plugin:%s indent:%s",
10456 filetype_detect ? "ON" : "OFF",
10457 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
10458 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
10459 return;
10460 }
10461
10462 /* Accept "plugin" and "indent" in any order. */
10463 for (;;)
10464 {
10465 if (STRNCMP(arg, "plugin", 6) == 0)
10466 {
10467 plugin = TRUE;
10468 arg = skipwhite(arg + 6);
10469 continue;
10470 }
10471 if (STRNCMP(arg, "indent", 6) == 0)
10472 {
10473 indent = TRUE;
10474 arg = skipwhite(arg + 6);
10475 continue;
10476 }
10477 break;
10478 }
10479 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
10480 {
10481 if (*arg == 'o' || !filetype_detect)
10482 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000010483 source_runtime((char_u *)FILETYPE_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010484 filetype_detect = TRUE;
10485 if (plugin)
10486 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000010487 source_runtime((char_u *)FTPLUGIN_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010488 filetype_plugin = TRUE;
10489 }
10490 if (indent)
10491 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000010492 source_runtime((char_u *)INDENT_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010493 filetype_indent = TRUE;
10494 }
10495 }
10496 if (*arg == 'd')
10497 {
10498 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +000010499 do_modelines(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010500 }
10501 }
10502 else if (STRCMP(arg, "off") == 0)
10503 {
10504 if (plugin || indent)
10505 {
10506 if (plugin)
10507 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000010508 source_runtime((char_u *)FTPLUGOF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010509 filetype_plugin = FALSE;
10510 }
10511 if (indent)
10512 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000010513 source_runtime((char_u *)INDOFF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010514 filetype_indent = FALSE;
10515 }
10516 }
10517 else
10518 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000010519 source_runtime((char_u *)FTOFF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010520 filetype_detect = FALSE;
10521 }
10522 }
10523 else
10524 EMSG2(_(e_invarg2), arg);
10525}
10526
10527/*
10528 * ":setfiletype {name}"
10529 */
10530 static void
10531ex_setfiletype(eap)
10532 exarg_T *eap;
10533{
10534 if (!did_filetype)
10535 set_option_value((char_u *)"filetype", 0L, eap->arg, OPT_LOCAL);
10536}
10537#endif
10538
10539/*ARGSUSED*/
10540 static void
10541ex_digraphs(eap)
10542 exarg_T *eap;
10543{
10544#ifdef FEAT_DIGRAPHS
10545 if (*eap->arg != NUL)
10546 putdigraph(eap->arg);
10547 else
10548 listdigraphs();
10549#else
10550 EMSG(_("E196: No digraphs in this version"));
10551#endif
10552}
10553
10554 static void
10555ex_set(eap)
10556 exarg_T *eap;
10557{
10558 int flags = 0;
10559
10560 if (eap->cmdidx == CMD_setlocal)
10561 flags = OPT_LOCAL;
10562 else if (eap->cmdidx == CMD_setglobal)
10563 flags = OPT_GLOBAL;
10564#if defined(FEAT_EVAL) && defined(FEAT_AUTOCMD) && defined(FEAT_BROWSE)
10565 if (cmdmod.browse && flags == 0)
10566 ex_options(eap);
10567 else
10568#endif
10569 (void)do_set(eap->arg, flags);
10570}
10571
10572#ifdef FEAT_SEARCH_EXTRA
10573/*
10574 * ":nohlsearch"
10575 */
10576/*ARGSUSED*/
10577 static void
10578ex_nohlsearch(eap)
10579 exarg_T *eap;
10580{
10581 no_hlsearch = TRUE;
10582 redraw_all_later(NOT_VALID);
10583}
10584
10585/*
10586 * ":match {group} {pattern}"
10587 * Sets nextcmd to the start of the next command, if any. Also called when
10588 * skipping commands to find the next command.
10589 */
10590 static void
10591ex_match(eap)
10592 exarg_T *eap;
10593{
10594 char_u *p;
10595 char_u *end;
10596 int c;
10597
10598 /* First clear any old pattern. */
10599 if (!eap->skip)
10600 {
10601 vim_free(curwin->w_match.regprog);
10602 curwin->w_match.regprog = NULL;
10603 redraw_later(NOT_VALID); /* always need a redraw */
10604 }
10605
10606 if (ends_excmd(*eap->arg))
10607 end = eap->arg;
10608 else if ((STRNICMP(eap->arg, "none", 4) == 0
10609 && (vim_iswhite(eap->arg[4]) || ends_excmd(eap->arg[4]))))
10610 end = eap->arg + 4;
10611 else
10612 {
10613 p = skiptowhite(eap->arg);
10614 if (!eap->skip)
10615 {
10616 curwin->w_match_id = syn_namen2id(eap->arg, (int)(p - eap->arg));
10617 if (curwin->w_match_id == 0)
10618 {
10619 EMSG2(_(e_nogroup), eap->arg);
10620 return;
10621 }
10622 }
10623 p = skipwhite(p);
10624 if (*p == NUL)
10625 {
10626 /* There must be two arguments. */
10627 EMSG2(_(e_invarg2), eap->arg);
10628 return;
10629 }
10630 end = skip_regexp(p + 1, *p, TRUE, NULL);
10631 if (!eap->skip)
10632 {
10633 if (*end != NUL && !ends_excmd(*skipwhite(end + 1)))
10634 {
10635 eap->errmsg = e_trailing;
10636 return;
10637 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000010638 if (*end != *p)
10639 {
10640 EMSG2(_(e_invarg2), p);
10641 return;
10642 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010643
10644 c = *end;
10645 *end = NUL;
10646 curwin->w_match.regprog = vim_regcomp(p + 1, RE_MAGIC);
10647 *end = c;
10648 if (curwin->w_match.regprog == NULL)
10649 {
10650 EMSG2(_(e_invarg2), p);
10651 return;
10652 }
10653 }
10654 }
10655 eap->nextcmd = find_nextcmd(end);
10656}
10657#endif
10658
10659#ifdef FEAT_CRYPT
10660/*
10661 * ":X": Get crypt key
10662 */
10663/*ARGSUSED*/
10664 static void
10665ex_X(eap)
10666 exarg_T *eap;
10667{
10668 (void)get_crypt_key(TRUE, TRUE);
10669}
10670#endif
10671
10672#ifdef FEAT_FOLDING
10673 static void
10674ex_fold(eap)
10675 exarg_T *eap;
10676{
10677 if (foldManualAllowed(TRUE))
10678 foldCreate(eap->line1, eap->line2);
10679}
10680
10681 static void
10682ex_foldopen(eap)
10683 exarg_T *eap;
10684{
10685 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
10686 eap->forceit, FALSE);
10687}
10688
10689 static void
10690ex_folddo(eap)
10691 exarg_T *eap;
10692{
10693 linenr_T lnum;
10694
10695 /* First set the marks for all lines closed/open. */
10696 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
10697 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
10698 ml_setmarked(lnum);
10699
10700 /* Execute the command on the marked lines. */
10701 global_exe(eap->arg);
10702 ml_clearmarked(); /* clear rest of the marks */
10703}
10704#endif