blob: 7e5e3b17560303f0265e5cce61dbc466da3d1368 [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));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000153static void ex_resize __ARGS((exarg_T *eap));
154static void ex_stag __ARGS((exarg_T *eap));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000155static void ex_tabclose __ARGS((exarg_T *eap));
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000156static void ex_tabonly __ARGS((exarg_T *eap));
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000157static void ex_tabnext __ARGS((exarg_T *eap));
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000158static void ex_tabmove __ARGS((exarg_T *eap));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000159static void ex_tabs __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000160#else
161# define ex_close ex_ni
162# define ex_only ex_ni
163# define ex_all ex_ni
164# define ex_resize ex_ni
165# define ex_splitview ex_ni
166# define ex_stag ex_ni
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000167# define ex_tabnext ex_ni
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000168# define ex_tabmove ex_ni
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000169# define ex_tabs ex_ni
170# define ex_tabclose ex_ni
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000171# define ex_tabonly ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000172#endif
173#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
174static void ex_pclose __ARGS((exarg_T *eap));
175static void ex_ptag __ARGS((exarg_T *eap));
176static void ex_pedit __ARGS((exarg_T *eap));
177#else
178# define ex_pclose ex_ni
179# define ex_ptag ex_ni
180# define ex_pedit ex_ni
181#endif
182static void ex_hide __ARGS((exarg_T *eap));
183static void ex_stop __ARGS((exarg_T *eap));
184static void ex_exit __ARGS((exarg_T *eap));
185static void ex_print __ARGS((exarg_T *eap));
186#ifdef FEAT_BYTEOFF
187static void ex_goto __ARGS((exarg_T *eap));
188#else
189# define ex_goto ex_ni
190#endif
191static void ex_shell __ARGS((exarg_T *eap));
192static void ex_preserve __ARGS((exarg_T *eap));
193static void ex_recover __ARGS((exarg_T *eap));
194#ifndef FEAT_LISTCMDS
195# define ex_argedit ex_ni
196# define ex_argadd ex_ni
197# define ex_argdelete ex_ni
198# define ex_listdo ex_ni
199#endif
200static void ex_mode __ARGS((exarg_T *eap));
201static void ex_wrongmodifier __ARGS((exarg_T *eap));
202static void ex_find __ARGS((exarg_T *eap));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000203static void ex_open __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000204static void ex_edit __ARGS((exarg_T *eap));
205#if !defined(FEAT_GUI) && !defined(FEAT_CLIENTSERVER)
206# define ex_drop ex_ni
207#endif
208#ifndef FEAT_GUI
209# define ex_gui ex_nogui
210static void ex_nogui __ARGS((exarg_T *eap));
211#endif
212#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
213static void ex_tearoff __ARGS((exarg_T *eap));
214#else
215# define ex_tearoff ex_ni
216#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000217#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000218static void ex_popup __ARGS((exarg_T *eap));
219#else
220# define ex_popup ex_ni
221#endif
222#ifndef FEAT_GUI_MSWIN
223# define ex_simalt ex_ni
224#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000225#if !defined(FEAT_GUI_MSWIN) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000226# define gui_mch_find_dialog ex_ni
227# define gui_mch_replace_dialog ex_ni
228#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000229#if !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000230# define ex_helpfind ex_ni
231#endif
232#ifndef FEAT_CSCOPE
233# define do_cscope ex_ni
234# define do_scscope ex_ni
235# define do_cstag ex_ni
236#endif
237#ifndef FEAT_SYN_HL
238# define ex_syntax ex_ni
Bram Moolenaarb765d632005-06-07 21:00:02 +0000239# define ex_spell ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000240# define ex_mkspell ex_ni
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000241# define ex_spelldump ex_ni
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000242# define ex_spellinfo 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
Bram Moolenaara2031822006-03-07 22:29:51 +00002428 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & TRLBAR) == 0))
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 Moolenaar362e1a32006-03-06 23:29:24 +00003292
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003293#ifndef BACKSLASH_IN_FILENAME
3294 /* For a shell command more chars need to be escaped. */
3295 if (usefilter || ea.cmdidx == CMD_bang)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003296 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003297 xp->xp_shell = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003298
3299 /* When still after the command name expand executables. */
3300 if (xp->xp_pattern == skipwhite(arg))
3301 xp->xp_context = EXPAND_SHELLCMD;
3302 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003303#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304
3305 /* Check for environment variable */
3306 if (*xp->xp_pattern == '$'
3307#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
3308 || *xp->xp_pattern == '%'
3309#endif
3310 )
3311 {
3312 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
3313 if (!vim_isIDc(*p))
3314 break;
3315 if (*p == NUL)
3316 {
3317 xp->xp_context = EXPAND_ENV_VARS;
3318 ++xp->xp_pattern;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003319#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3320 /* Avoid that the assignment uses EXPAND_FILES again. */
Bram Moolenaara466c992005-07-09 21:03:22 +00003321 if (compl != EXPAND_USER_DEFINED && compl != EXPAND_USER_LIST)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003322 compl = EXPAND_ENV_VARS;
3323#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324 }
3325 }
3326 }
3327
3328/*
3329 * 6. switch on command name
3330 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003331 switch (ea.cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003332 {
3333 case CMD_cd:
3334 case CMD_chdir:
3335 case CMD_lcd:
3336 case CMD_lchdir:
3337 if (xp->xp_context == EXPAND_FILES)
3338 xp->xp_context = EXPAND_DIRECTORIES;
3339 break;
3340 case CMD_help:
3341 xp->xp_context = EXPAND_HELP;
3342 xp->xp_pattern = arg;
3343 break;
3344
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003345 /* Command modifiers: return the argument.
3346 * Also for commands with an argument that is a command. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003347 case CMD_aboveleft:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003348 case CMD_argdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349 case CMD_belowright:
3350 case CMD_botright:
3351 case CMD_browse:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003352 case CMD_bufdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353 case CMD_confirm:
Bram Moolenaardf177f62005-02-22 08:39:57 +00003354 case CMD_debug:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355 case CMD_folddoclosed:
3356 case CMD_folddoopen:
3357 case CMD_hide:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003358 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359 case CMD_keepjumps:
3360 case CMD_keepmarks:
3361 case CMD_leftabove:
3362 case CMD_lockmarks:
3363 case CMD_rightbelow:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003364 case CMD_sandbox:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365 case CMD_silent:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003366 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367 case CMD_topleft:
3368 case CMD_verbose:
3369 case CMD_vertical:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003370 case CMD_windo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371 return arg;
3372
3373#ifdef FEAT_SEARCH_EXTRA
3374 case CMD_match:
3375 if (*arg == NUL || !ends_excmd(*arg))
3376 {
3377 /* Dummy call to clear variables. */
3378 set_context_in_highlight_cmd(xp, (char_u *)"link n");
3379 xp->xp_context = EXPAND_HIGHLIGHT;
3380 xp->xp_pattern = arg;
3381 arg = skipwhite(skiptowhite(arg));
3382 if (*arg != NUL)
3383 {
3384 xp->xp_context = EXPAND_NOTHING;
3385 arg = skip_regexp(arg + 1, *arg, p_magic, NULL);
3386 }
3387 }
3388 return find_nextcmd(arg);
3389#endif
3390
3391#ifdef FEAT_CMDL_COMPL
3392/*
3393 * All completion for the +cmdline_compl feature goes here.
3394 */
3395
3396# ifdef FEAT_USR_CMDS
3397 case CMD_command:
3398 /* Check for attributes */
3399 while (*arg == '-')
3400 {
3401 arg++; /* Skip "-" */
3402 p = skiptowhite(arg);
3403 if (*p == NUL)
3404 {
3405 /* Cursor is still in the attribute */
3406 p = vim_strchr(arg, '=');
3407 if (p == NULL)
3408 {
3409 /* No "=", so complete attribute names */
3410 xp->xp_context = EXPAND_USER_CMD_FLAGS;
3411 xp->xp_pattern = arg;
3412 return NULL;
3413 }
3414
3415 /* For the -complete and -nargs attributes, we complete
3416 * their arguments as well.
3417 */
3418 if (STRNICMP(arg, "complete", p - arg) == 0)
3419 {
3420 xp->xp_context = EXPAND_USER_COMPLETE;
3421 xp->xp_pattern = p + 1;
3422 return NULL;
3423 }
3424 else if (STRNICMP(arg, "nargs", p - arg) == 0)
3425 {
3426 xp->xp_context = EXPAND_USER_NARGS;
3427 xp->xp_pattern = p + 1;
3428 return NULL;
3429 }
3430 return NULL;
3431 }
3432 arg = skipwhite(p);
3433 }
3434
3435 /* After the attributes comes the new command name */
3436 p = skiptowhite(arg);
3437 if (*p == NUL)
3438 {
3439 xp->xp_context = EXPAND_USER_COMMANDS;
3440 xp->xp_pattern = arg;
3441 break;
3442 }
3443
3444 /* And finally comes a normal command */
3445 return skipwhite(p);
3446
3447 case CMD_delcommand:
3448 xp->xp_context = EXPAND_USER_COMMANDS;
3449 xp->xp_pattern = arg;
3450 break;
3451# endif
3452
3453 case CMD_global:
3454 case CMD_vglobal:
3455 delim = *arg; /* get the delimiter */
3456 if (delim)
3457 ++arg; /* skip delimiter if there is one */
3458
3459 while (arg[0] != NUL && arg[0] != delim)
3460 {
3461 if (arg[0] == '\\' && arg[1] != NUL)
3462 ++arg;
3463 ++arg;
3464 }
3465 if (arg[0] != NUL)
3466 return arg + 1;
3467 break;
3468 case CMD_and:
3469 case CMD_substitute:
3470 delim = *arg;
3471 if (delim)
3472 {
3473 /* skip "from" part */
3474 ++arg;
3475 arg = skip_regexp(arg, delim, p_magic, NULL);
3476 }
3477 /* skip "to" part */
3478 while (arg[0] != NUL && arg[0] != delim)
3479 {
3480 if (arg[0] == '\\' && arg[1] != NUL)
3481 ++arg;
3482 ++arg;
3483 }
3484 if (arg[0] != NUL) /* skip delimiter */
3485 ++arg;
3486 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
3487 ++arg;
3488 if (arg[0] != NUL)
3489 return arg;
3490 break;
3491 case CMD_isearch:
3492 case CMD_dsearch:
3493 case CMD_ilist:
3494 case CMD_dlist:
3495 case CMD_ijump:
3496 case CMD_psearch:
3497 case CMD_djump:
3498 case CMD_isplit:
3499 case CMD_dsplit:
3500 arg = skipwhite(skipdigits(arg)); /* skip count */
3501 if (*arg == '/') /* Match regexp, not just whole words */
3502 {
3503 for (++arg; *arg && *arg != '/'; arg++)
3504 if (*arg == '\\' && arg[1] != NUL)
3505 arg++;
3506 if (*arg)
3507 {
3508 arg = skipwhite(arg + 1);
3509
3510 /* Check for trailing illegal characters */
3511 if (*arg && vim_strchr((char_u *)"|\"\n", *arg) == NULL)
3512 xp->xp_context = EXPAND_NOTHING;
3513 else
3514 return arg;
3515 }
3516 }
3517 break;
3518#ifdef FEAT_AUTOCMD
3519 case CMD_autocmd:
3520 return set_context_in_autocmd(xp, arg, FALSE);
3521
3522 case CMD_doautocmd:
3523 return set_context_in_autocmd(xp, arg, TRUE);
3524#endif
3525 case CMD_set:
3526 set_context_in_set_cmd(xp, arg, 0);
3527 break;
3528 case CMD_setglobal:
3529 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
3530 break;
3531 case CMD_setlocal:
3532 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
3533 break;
3534 case CMD_tag:
3535 case CMD_stag:
3536 case CMD_ptag:
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00003537 case CMD_ltag:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538 case CMD_tselect:
3539 case CMD_stselect:
3540 case CMD_ptselect:
3541 case CMD_tjump:
3542 case CMD_stjump:
3543 case CMD_ptjump:
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003544 if (*p_wop != NUL)
3545 xp->xp_context = EXPAND_TAGS_LISTFILES;
3546 else
3547 xp->xp_context = EXPAND_TAGS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548 xp->xp_pattern = arg;
3549 break;
3550 case CMD_augroup:
3551 xp->xp_context = EXPAND_AUGROUP;
3552 xp->xp_pattern = arg;
3553 break;
3554#ifdef FEAT_SYN_HL
3555 case CMD_syntax:
3556 set_context_in_syntax_cmd(xp, arg);
3557 break;
3558#endif
3559#ifdef FEAT_EVAL
3560 case CMD_let:
3561 case CMD_if:
3562 case CMD_elseif:
3563 case CMD_while:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00003564 case CMD_for:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565 case CMD_echo:
3566 case CMD_echon:
3567 case CMD_execute:
3568 case CMD_echomsg:
3569 case CMD_echoerr:
3570 case CMD_call:
3571 case CMD_return:
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003572 set_context_for_expression(xp, arg, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003573 break;
3574
3575 case CMD_unlet:
3576 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3577 arg = xp->xp_pattern + 1;
3578 xp->xp_context = EXPAND_USER_VARS;
3579 xp->xp_pattern = arg;
3580 break;
3581
3582 case CMD_function:
3583 case CMD_delfunction:
3584 xp->xp_context = EXPAND_USER_FUNC;
3585 xp->xp_pattern = arg;
3586 break;
3587
3588 case CMD_echohl:
3589 xp->xp_context = EXPAND_HIGHLIGHT;
3590 xp->xp_pattern = arg;
3591 break;
3592#endif
3593 case CMD_highlight:
3594 set_context_in_highlight_cmd(xp, arg);
3595 break;
3596#ifdef FEAT_LISTCMDS
3597 case CMD_bdelete:
3598 case CMD_bwipeout:
3599 case CMD_bunload:
3600 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3601 arg = xp->xp_pattern + 1;
3602 /*FALLTHROUGH*/
3603 case CMD_buffer:
3604 case CMD_sbuffer:
3605 case CMD_checktime:
3606 xp->xp_context = EXPAND_BUFFERS;
3607 xp->xp_pattern = arg;
3608 break;
3609#endif
3610#ifdef FEAT_USR_CMDS
3611 case CMD_USER:
3612 case CMD_USER_BUF:
3613 if (compl != EXPAND_NOTHING)
3614 {
3615 /* XFILE: file names are handled above */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003616 if (!(ea.argt & XFILE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003617 {
3618# ifdef FEAT_MENU
3619 if (compl == EXPAND_MENUS)
3620 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
3621# endif
3622 if (compl == EXPAND_COMMANDS)
3623 return arg;
3624 if (compl == EXPAND_MAPPINGS)
3625 return set_context_in_map_cmd(xp, (char_u *)"map",
3626 arg, forceit, FALSE, FALSE, CMD_map);
3627 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3628 arg = xp->xp_pattern + 1;
3629 xp->xp_pattern = arg;
3630 }
3631 xp->xp_context = compl;
3632 }
3633 break;
3634#endif
3635 case CMD_map: case CMD_noremap:
3636 case CMD_nmap: case CMD_nnoremap:
3637 case CMD_vmap: case CMD_vnoremap:
3638 case CMD_omap: case CMD_onoremap:
3639 case CMD_imap: case CMD_inoremap:
3640 case CMD_cmap: case CMD_cnoremap:
3641 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003642 FALSE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643 case CMD_unmap:
3644 case CMD_nunmap:
3645 case CMD_vunmap:
3646 case CMD_ounmap:
3647 case CMD_iunmap:
3648 case CMD_cunmap:
3649 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003650 FALSE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651 case CMD_abbreviate: case CMD_noreabbrev:
3652 case CMD_cabbrev: case CMD_cnoreabbrev:
3653 case CMD_iabbrev: case CMD_inoreabbrev:
3654 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003655 TRUE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003656 case CMD_unabbreviate:
3657 case CMD_cunabbrev:
3658 case CMD_iunabbrev:
3659 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003660 TRUE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003661#ifdef FEAT_MENU
3662 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
3663 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
3664 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
3665 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
3666 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
3667 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
3668 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
3669 case CMD_tmenu: case CMD_tunmenu:
3670 case CMD_popup: case CMD_tearoff: case CMD_emenu:
3671 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
3672#endif
3673
3674 case CMD_colorscheme:
3675 xp->xp_context = EXPAND_COLORS;
3676 xp->xp_pattern = arg;
3677 break;
3678
3679 case CMD_compiler:
3680 xp->xp_context = EXPAND_COMPILER;
3681 xp->xp_pattern = arg;
3682 break;
3683
3684#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
3685 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
3686 case CMD_language:
3687 if (*skiptowhite(arg) == NUL)
3688 {
3689 xp->xp_context = EXPAND_LANGUAGE;
3690 xp->xp_pattern = arg;
3691 }
3692 else
3693 xp->xp_context = EXPAND_NOTHING;
3694 break;
3695#endif
3696
3697#endif /* FEAT_CMDL_COMPL */
3698
3699 default:
3700 break;
3701 }
3702 return NULL;
3703}
3704
3705/*
3706 * skip a range specifier of the form: addr [,addr] [;addr] ..
3707 *
3708 * Backslashed delimiters after / or ? will be skipped, and commands will
3709 * not be expanded between /'s and ?'s or after "'".
3710 *
Bram Moolenaardf177f62005-02-22 08:39:57 +00003711 * Also skip white space and ":" characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003712 * Returns the "cmd" pointer advanced to beyond the range.
3713 */
3714 char_u *
3715skip_range(cmd, ctx)
3716 char_u *cmd;
3717 int *ctx; /* pointer to xp_context or NULL */
3718{
3719 int delim;
3720
Bram Moolenaardf177f62005-02-22 08:39:57 +00003721 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722 {
3723 if (*cmd == '\'')
3724 {
3725 if (*++cmd == NUL && ctx != NULL)
3726 *ctx = EXPAND_NOTHING;
3727 }
3728 else if (*cmd == '/' || *cmd == '?')
3729 {
3730 delim = *cmd++;
3731 while (*cmd != NUL && *cmd != delim)
3732 if (*cmd++ == '\\' && *cmd != NUL)
3733 ++cmd;
3734 if (*cmd == NUL && ctx != NULL)
3735 *ctx = EXPAND_NOTHING;
3736 }
3737 if (*cmd != NUL)
3738 ++cmd;
3739 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00003740
3741 /* Skip ":" and white space. */
3742 while (*cmd == ':')
3743 cmd = skipwhite(cmd + 1);
3744
Bram Moolenaar071d4272004-06-13 20:20:40 +00003745 return cmd;
3746}
3747
3748/*
3749 * get a single EX address
3750 *
3751 * Set ptr to the next character after the part that was interpreted.
3752 * Set ptr to NULL when an error is encountered.
3753 *
3754 * Return MAXLNUM when no Ex address was found.
3755 */
3756 static linenr_T
3757get_address(ptr, skip, to_other_file)
3758 char_u **ptr;
3759 int skip; /* only skip the address, don't use it */
3760 int to_other_file; /* flag: may jump to other file */
3761{
3762 int c;
3763 int i;
3764 long n;
3765 char_u *cmd;
3766 pos_T pos;
3767 pos_T *fp;
3768 linenr_T lnum;
3769
3770 cmd = skipwhite(*ptr);
3771 lnum = MAXLNUM;
3772 do
3773 {
3774 switch (*cmd)
3775 {
3776 case '.': /* '.' - Cursor position */
3777 ++cmd;
3778 lnum = curwin->w_cursor.lnum;
3779 break;
3780
3781 case '$': /* '$' - last line */
3782 ++cmd;
3783 lnum = curbuf->b_ml.ml_line_count;
3784 break;
3785
3786 case '\'': /* ''' - mark */
3787 if (*++cmd == NUL)
3788 {
3789 cmd = NULL;
3790 goto error;
3791 }
3792 if (skip)
3793 ++cmd;
3794 else
3795 {
3796 /* Only accept a mark in another file when it is
3797 * used by itself: ":'M". */
3798 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
3799 ++cmd;
3800 if (fp == (pos_T *)-1)
3801 /* Jumped to another file. */
3802 lnum = curwin->w_cursor.lnum;
3803 else
3804 {
3805 if (check_mark(fp) == FAIL)
3806 {
3807 cmd = NULL;
3808 goto error;
3809 }
3810 lnum = fp->lnum;
3811 }
3812 }
3813 break;
3814
3815 case '/':
3816 case '?': /* '/' or '?' - search */
3817 c = *cmd++;
3818 if (skip) /* skip "/pat/" */
3819 {
3820 cmd = skip_regexp(cmd, c, (int)p_magic, NULL);
3821 if (*cmd == c)
3822 ++cmd;
3823 }
3824 else
3825 {
3826 pos = curwin->w_cursor; /* save curwin->w_cursor */
3827 /*
3828 * When '/' or '?' follows another address, start
3829 * from there.
3830 */
3831 if (lnum != MAXLNUM)
3832 curwin->w_cursor.lnum = lnum;
3833 /*
3834 * Start a forward search at the end of the line.
3835 * Start a backward search at the start of the line.
3836 * This makes sure we never match in the current
3837 * line, and can match anywhere in the
3838 * next/previous line.
3839 */
3840 if (c == '/')
3841 curwin->w_cursor.col = MAXCOL;
3842 else
3843 curwin->w_cursor.col = 0;
3844 searchcmdlen = 0;
3845 if (!do_search(NULL, c, cmd, 1L,
3846 SEARCH_HIS + SEARCH_MSG + SEARCH_START))
3847 {
3848 curwin->w_cursor = pos;
3849 cmd = NULL;
3850 goto error;
3851 }
3852 lnum = curwin->w_cursor.lnum;
3853 curwin->w_cursor = pos;
3854 /* adjust command string pointer */
3855 cmd += searchcmdlen;
3856 }
3857 break;
3858
3859 case '\\': /* "\?", "\/" or "\&", repeat search */
3860 ++cmd;
3861 if (*cmd == '&')
3862 i = RE_SUBST;
3863 else if (*cmd == '?' || *cmd == '/')
3864 i = RE_SEARCH;
3865 else
3866 {
3867 EMSG(_(e_backslash));
3868 cmd = NULL;
3869 goto error;
3870 }
3871
3872 if (!skip)
3873 {
3874 /*
3875 * When search follows another address, start from
3876 * there.
3877 */
3878 if (lnum != MAXLNUM)
3879 pos.lnum = lnum;
3880 else
3881 pos.lnum = curwin->w_cursor.lnum;
3882
3883 /*
3884 * Start the search just like for the above
3885 * do_search().
3886 */
3887 if (*cmd != '?')
3888 pos.col = MAXCOL;
3889 else
3890 pos.col = 0;
3891 if (searchit(curwin, curbuf, &pos,
3892 *cmd == '?' ? BACKWARD : FORWARD,
3893 (char_u *)"", 1L,
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003894 SEARCH_MSG + SEARCH_START,
3895 i, (linenr_T)0) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896 lnum = pos.lnum;
3897 else
3898 {
3899 cmd = NULL;
3900 goto error;
3901 }
3902 }
3903 ++cmd;
3904 break;
3905
3906 default:
3907 if (VIM_ISDIGIT(*cmd)) /* absolute line number */
3908 lnum = getdigits(&cmd);
3909 }
3910
3911 for (;;)
3912 {
3913 cmd = skipwhite(cmd);
3914 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
3915 break;
3916
3917 if (lnum == MAXLNUM)
3918 lnum = curwin->w_cursor.lnum; /* "+1" is same as ".+1" */
3919 if (VIM_ISDIGIT(*cmd))
3920 i = '+'; /* "number" is same as "+number" */
3921 else
3922 i = *cmd++;
3923 if (!VIM_ISDIGIT(*cmd)) /* '+' is '+1', but '+0' is not '+1' */
3924 n = 1;
3925 else
3926 n = getdigits(&cmd);
3927 if (i == '-')
3928 lnum -= n;
3929 else
3930 lnum += n;
3931 }
3932 } while (*cmd == '/' || *cmd == '?');
3933
3934error:
3935 *ptr = cmd;
3936 return lnum;
3937}
3938
3939/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00003940 * Get flags from an Ex command argument.
3941 */
3942 static void
3943get_flags(eap)
3944 exarg_T *eap;
3945{
3946 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
3947 {
3948 if (*eap->arg == 'l')
3949 eap->flags |= EXFLAG_LIST;
3950 else if (*eap->arg == 'p')
3951 eap->flags |= EXFLAG_PRINT;
3952 else
3953 eap->flags |= EXFLAG_NR;
3954 eap->arg = skipwhite(eap->arg + 1);
3955 }
3956}
3957
3958/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003959 * Function called for command which is Not Implemented. NI!
3960 */
3961 void
3962ex_ni(eap)
3963 exarg_T *eap;
3964{
3965 if (!eap->skip)
3966 eap->errmsg = (char_u *)N_("E319: Sorry, the command is not available in this version");
3967}
3968
3969#if !defined(FEAT_PERL) || !defined(FEAT_PYTHON) || !defined(FEAT_TCL) \
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003970 || !defined(FEAT_RUBY) || !defined(FEAT_MZSCHEME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003971/*
3972 * Function called for script command which is Not Implemented. NI!
3973 * Skips over ":perl <<EOF" constructs.
3974 */
3975 static void
3976ex_script_ni(eap)
3977 exarg_T *eap;
3978{
3979 if (!eap->skip)
3980 ex_ni(eap);
3981 else
3982 vim_free(script_get(eap, eap->arg));
3983}
3984#endif
3985
3986/*
3987 * Check range in Ex command for validity.
3988 * Return NULL when valid, error message when invalid.
3989 */
3990 static char_u *
3991invalid_range(eap)
3992 exarg_T *eap;
3993{
3994 if ( eap->line1 < 0
3995 || eap->line2 < 0
3996 || eap->line1 > eap->line2
3997 || ((eap->argt & RANGE)
3998 && !(eap->argt & NOTADR)
3999 && eap->line2 > curbuf->b_ml.ml_line_count
4000#ifdef FEAT_DIFF
4001 + (eap->cmdidx == CMD_diffget)
4002#endif
4003 ))
4004 return (char_u *)_(e_invrange);
4005 return NULL;
4006}
4007
4008/*
4009 * Correct the range for zero line number, if required.
4010 */
4011 static void
4012correct_range(eap)
4013 exarg_T *eap;
4014{
4015 if (!(eap->argt & ZEROR)) /* zero in range not allowed */
4016 {
4017 if (eap->line1 == 0)
4018 eap->line1 = 1;
4019 if (eap->line2 == 0)
4020 eap->line2 = 1;
4021 }
4022}
4023
Bram Moolenaar748bf032005-02-02 23:04:36 +00004024#ifdef FEAT_QUICKFIX
4025static char_u *skip_grep_pat __ARGS((exarg_T *eap));
4026
4027/*
4028 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4029 * pattern. Otherwise return eap->arg.
4030 */
4031 static char_u *
4032skip_grep_pat(eap)
4033 exarg_T *eap;
4034{
4035 char_u *p = eap->arg;
4036
Bram Moolenaara37420f2006-02-04 22:37:47 +00004037 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4038 || eap->cmdidx == CMD_vimgrepadd
4039 || eap->cmdidx == CMD_lvimgrepadd
4040 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004041 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004042 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004043 if (p == NULL)
4044 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004045 }
4046 return p;
4047}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004048
4049/*
4050 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4051 * in the command line, so that things like % get expanded.
4052 */
4053 static char_u *
4054replace_makeprg(eap, p, cmdlinep)
4055 exarg_T *eap;
4056 char_u *p;
4057 char_u **cmdlinep;
4058{
4059 char_u *new_cmdline;
4060 char_u *program;
4061 char_u *pos;
4062 char_u *ptr;
4063 int len;
4064 int i;
4065
4066 /*
4067 * Don't do it when ":vimgrep" is used for ":grep".
4068 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004069 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4070 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4071 || eap->cmdidx == CMD_grepadd
4072 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004073 && !grep_internal(eap->cmdidx))
4074 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004075 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4076 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004077 {
4078 if (*curbuf->b_p_gp == NUL)
4079 program = p_gp;
4080 else
4081 program = curbuf->b_p_gp;
4082 }
4083 else
4084 {
4085 if (*curbuf->b_p_mp == NUL)
4086 program = p_mp;
4087 else
4088 program = curbuf->b_p_mp;
4089 }
4090
4091 p = skipwhite(p);
4092
4093 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4094 {
4095 /* replace $* by given arguments */
4096 i = 1;
4097 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4098 ++i;
4099 len = (int)STRLEN(p);
4100 new_cmdline = alloc((int)(STRLEN(program) + i * (len - 2) + 1));
4101 if (new_cmdline == NULL)
4102 return NULL; /* out of memory */
4103 ptr = new_cmdline;
4104 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4105 {
4106 i = (int)(pos - program);
4107 STRNCPY(ptr, program, i);
4108 STRCPY(ptr += i, p);
4109 ptr += len;
4110 program = pos + 2;
4111 }
4112 STRCPY(ptr, program);
4113 }
4114 else
4115 {
4116 new_cmdline = alloc((int)(STRLEN(program) + STRLEN(p) + 2));
4117 if (new_cmdline == NULL)
4118 return NULL; /* out of memory */
4119 STRCPY(new_cmdline, program);
4120 STRCAT(new_cmdline, " ");
4121 STRCAT(new_cmdline, p);
4122 }
4123 msg_make(p);
4124
4125 /* 'eap->cmd' is not set here, because it is not used at CMD_make */
4126 vim_free(*cmdlinep);
4127 *cmdlinep = new_cmdline;
4128 p = new_cmdline;
4129 }
4130 return p;
4131}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004132#endif
4133
Bram Moolenaar071d4272004-06-13 20:20:40 +00004134/*
4135 * Expand file name in Ex command argument.
4136 * Return FAIL for failure, OK otherwise.
4137 */
4138 int
4139expand_filename(eap, cmdlinep, errormsgp)
4140 exarg_T *eap;
4141 char_u **cmdlinep;
4142 char_u **errormsgp;
4143{
4144 int has_wildcards; /* need to expand wildcards */
4145 char_u *repl;
4146 int srclen;
4147 char_u *p;
4148 int n;
4149
Bram Moolenaar748bf032005-02-02 23:04:36 +00004150#ifdef FEAT_QUICKFIX
4151 /* Skip a regexp pattern for ":vimgrep[add] pat file..." */
4152 p = skip_grep_pat(eap);
4153#else
4154 p = eap->arg;
4155#endif
4156
Bram Moolenaar071d4272004-06-13 20:20:40 +00004157 /*
4158 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4159 * the file name contains a wildcard it should not cause expanding.
4160 * (it will be expanded anyway if there is a wildcard before replacing).
4161 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004162 has_wildcards = mch_has_wildcard(p);
4163 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004164 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004165#ifdef FEAT_EVAL
4166 /* Skip over `=expr`, wildcards in it are not expanded. */
4167 if (p[0] == '`' && p[1] == '=')
4168 {
4169 p += 2;
4170 (void)skip_expr(&p);
4171 if (*p == '`')
4172 ++p;
4173 continue;
4174 }
4175#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176 /*
4177 * Quick check if this cannot be the start of a special string.
4178 * Also removes backslash before '%', '#' and '<'.
4179 */
4180 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4181 {
4182 ++p;
4183 continue;
4184 }
4185
4186 /*
4187 * Try to find a match at this position.
4188 */
4189 repl = eval_vars(p, &srclen, &(eap->do_ecmd_lnum), errormsgp, eap->arg);
4190 if (*errormsgp != NULL) /* error detected */
4191 return FAIL;
4192 if (repl == NULL) /* no match found */
4193 {
4194 p += srclen;
4195 continue;
4196 }
4197
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004198 /* Wildcards won't be expanded below, the replacement is taken
4199 * literally. But do expand "~/file", "~user/file" and "$HOME/file". */
4200 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4201 {
4202 char_u *l = repl;
4203
4204 repl = expand_env_save(repl);
4205 vim_free(l);
4206 }
4207
Bram Moolenaar071d4272004-06-13 20:20:40 +00004208 /* Need to escape white space et al. with a backslash. Don't do this
4209 * for shell commands (may have to use quotes instead). Don't do this
4210 * for non-unix systems when there is a single argument (spaces don't
4211 * separate arguments then). */
4212 if (!eap->usefilter
4213 && eap->cmdidx != CMD_bang
4214 && eap->cmdidx != CMD_make
Bram Moolenaara37420f2006-02-04 22:37:47 +00004215 && eap->cmdidx != CMD_lmake
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216 && eap->cmdidx != CMD_grep
Bram Moolenaara37420f2006-02-04 22:37:47 +00004217 && eap->cmdidx != CMD_lgrep
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218 && eap->cmdidx != CMD_grepadd
Bram Moolenaara37420f2006-02-04 22:37:47 +00004219 && eap->cmdidx != CMD_lgrepadd
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220#ifndef UNIX
4221 && !(eap->argt & NOSPC)
4222#endif
4223 )
4224 {
4225 char_u *l;
4226#ifdef BACKSLASH_IN_FILENAME
4227 /* Don't escape a backslash here, because rem_backslash() doesn't
4228 * remove it later. */
4229 static char_u *nobslash = (char_u *)" \t\"|";
4230# define ESCAPE_CHARS nobslash
4231#else
4232# define ESCAPE_CHARS escape_chars
4233#endif
4234
4235 for (l = repl; *l; ++l)
4236 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
4237 {
4238 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
4239 if (l != NULL)
4240 {
4241 vim_free(repl);
4242 repl = l;
4243 }
4244 break;
4245 }
4246 }
4247
4248 /* For a shell command a '!' must be escaped. */
4249 if ((eap->usefilter || eap->cmdidx == CMD_bang)
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004250 && vim_strpbrk(repl, (char_u *)"!&;()<>") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004251 {
4252 char_u *l;
4253
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004254 l = vim_strsave_escaped(repl, (char_u *)"!&;()<>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004255 if (l != NULL)
4256 {
4257 vim_free(repl);
4258 repl = l;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004259 /* For a sh-like shell escape "!" another time. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004260 if (strstr((char *)p_sh, "sh") != NULL)
4261 {
4262 l = vim_strsave_escaped(repl, (char_u *)"!");
4263 if (l != NULL)
4264 {
4265 vim_free(repl);
4266 repl = l;
4267 }
4268 }
4269 }
4270 }
4271
4272 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
4273 vim_free(repl);
4274 if (p == NULL)
4275 return FAIL;
4276 }
4277
4278 /*
4279 * One file argument: Expand wildcards.
4280 * Don't do this with ":r !command" or ":w !command".
4281 */
4282 if ((eap->argt & NOSPC) && !eap->usefilter)
4283 {
4284 /*
4285 * May do this twice:
4286 * 1. Replace environment variables.
4287 * 2. Replace any other wildcards, remove backslashes.
4288 */
4289 for (n = 1; n <= 2; ++n)
4290 {
4291 if (n == 2)
4292 {
4293#ifdef UNIX
4294 /*
4295 * Only for Unix we check for more than one file name.
4296 * For other systems spaces are considered to be part
4297 * of the file name.
4298 * Only check here if there is no wildcard, otherwise
4299 * ExpandOne() will check for errors. This allows
4300 * ":e `ls ve*.c`" on Unix.
4301 */
4302 if (!has_wildcards)
4303 for (p = eap->arg; *p; ++p)
4304 {
4305 /* skip escaped characters */
4306 if (p[1] && (*p == '\\' || *p == Ctrl_V))
4307 ++p;
4308 else if (vim_iswhite(*p))
4309 {
4310 *errormsgp = (char_u *)_("E172: Only one file name allowed");
4311 return FAIL;
4312 }
4313 }
4314#endif
4315
4316 /*
4317 * Halve the number of backslashes (this is Vi compatible).
4318 * For Unix and OS/2, when wildcards are expanded, this is
4319 * done by ExpandOne() below.
4320 */
4321#if defined(UNIX) || defined(OS2)
4322 if (!has_wildcards)
4323#endif
4324 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004325 }
4326
4327 if (has_wildcards)
4328 {
4329 if (n == 1)
4330 {
4331 /*
4332 * First loop: May expand environment variables. This
4333 * can be done much faster with expand_env() than with
4334 * something else (e.g., calling a shell).
4335 * After expanding environment variables, check again
4336 * if there are still wildcards present.
4337 */
4338 if (vim_strchr(eap->arg, '$') != NULL
4339 || vim_strchr(eap->arg, '~') != NULL)
4340 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00004341 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
4342 TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004343 has_wildcards = mch_has_wildcard(NameBuff);
4344 p = NameBuff;
4345 }
4346 else
4347 p = NULL;
4348 }
4349 else /* n == 2 */
4350 {
4351 expand_T xpc;
4352
4353 ExpandInit(&xpc);
4354 xpc.xp_context = EXPAND_FILES;
4355 p = ExpandOne(&xpc, eap->arg, NULL,
4356 WILD_LIST_NOTFOUND|WILD_ADD_SLASH,
4357 WILD_EXPAND_FREE);
4358 ExpandCleanup(&xpc);
4359 if (p == NULL)
4360 return FAIL;
4361 }
4362 if (p != NULL)
4363 {
4364 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
4365 p, cmdlinep);
4366 if (n == 2) /* p came from ExpandOne() */
4367 vim_free(p);
4368 }
4369 }
4370 }
4371 }
4372 return OK;
4373}
4374
4375/*
4376 * Replace part of the command line, keeping eap->cmd, eap->arg and
4377 * eap->nextcmd correct.
4378 * "src" points to the part that is to be replaced, of length "srclen".
4379 * "repl" is the replacement string.
4380 * Returns a pointer to the character after the replaced string.
4381 * Returns NULL for failure.
4382 */
4383 static char_u *
4384repl_cmdline(eap, src, srclen, repl, cmdlinep)
4385 exarg_T *eap;
4386 char_u *src;
4387 int srclen;
4388 char_u *repl;
4389 char_u **cmdlinep;
4390{
4391 int len;
4392 int i;
4393 char_u *new_cmdline;
4394
4395 /*
4396 * The new command line is build in new_cmdline[].
4397 * First allocate it.
4398 * Careful: a "+cmd" argument may have been NUL terminated.
4399 */
4400 len = (int)STRLEN(repl);
4401 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004402 if (eap->nextcmd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004403 i += (int)STRLEN(eap->nextcmd);/* add space for next command */
4404 if ((new_cmdline = alloc((unsigned)i)) == NULL)
4405 return NULL; /* out of memory! */
4406
4407 /*
4408 * Copy the stuff before the expanded part.
4409 * Copy the expanded stuff.
4410 * Copy what came after the expanded part.
4411 * Copy the next commands, if there are any.
4412 */
4413 i = (int)(src - *cmdlinep); /* length of part before match */
4414 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00004415
Bram Moolenaar071d4272004-06-13 20:20:40 +00004416 mch_memmove(new_cmdline + i, repl, (size_t)len);
4417 i += len; /* remember the end of the string */
4418 STRCPY(new_cmdline + i, src + srclen);
4419 src = new_cmdline + i; /* remember where to continue */
4420
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004421 if (eap->nextcmd != NULL) /* append next command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004422 {
4423 i = (int)STRLEN(new_cmdline) + 1;
4424 STRCPY(new_cmdline + i, eap->nextcmd);
4425 eap->nextcmd = new_cmdline + i;
4426 }
4427 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
4428 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
4429 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
4430 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
4431 vim_free(*cmdlinep);
4432 *cmdlinep = new_cmdline;
4433
4434 return src;
4435}
4436
4437/*
4438 * Check for '|' to separate commands and '"' to start comments.
4439 */
4440 void
4441separate_nextcmd(eap)
4442 exarg_T *eap;
4443{
4444 char_u *p;
4445
Bram Moolenaar86b68352004-12-27 21:59:20 +00004446#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004447 p = skip_grep_pat(eap);
4448#else
4449 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004450#endif
4451
4452 for ( ; *p; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004453 {
4454 if (*p == Ctrl_V)
4455 {
4456 if (eap->argt & (USECTRLV | XFILE))
4457 ++p; /* skip CTRL-V and next char */
4458 else
4459 STRCPY(p, p + 1); /* remove CTRL-V and skip next char */
4460 if (*p == NUL) /* stop at NUL after CTRL-V */
4461 break;
4462 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004463
4464#ifdef FEAT_EVAL
4465 /* Skip over `=expr` when wildcards are expanded. */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004466 else if (p[0] == '`' && p[1] == '=' && (eap->argt & XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004467 {
4468 p += 2;
4469 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004470 }
4471#endif
4472
Bram Moolenaar071d4272004-06-13 20:20:40 +00004473 /* Check for '"': start of comment or '|': next command */
4474 /* :@" and :*" do not start a comment!
4475 * :redir @" doesn't either. */
4476 else if ((*p == '"' && !(eap->argt & NOTRLCOM)
4477 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
4478 || p != eap->arg)
4479 && (eap->cmdidx != CMD_redir
4480 || p != eap->arg + 1 || p[-1] != '@'))
4481 || *p == '|' || *p == '\n')
4482 {
4483 /*
4484 * We remove the '\' before the '|', unless USECTRLV is used
4485 * AND 'b' is present in 'cpoptions'.
4486 */
4487 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
4488 || !(eap->argt & USECTRLV)) && *(p - 1) == '\\')
4489 {
4490 mch_memmove(p - 1, p, STRLEN(p) + 1); /* remove the '\' */
4491 --p;
4492 }
4493 else
4494 {
4495 eap->nextcmd = check_nextcmd(p);
4496 *p = NUL;
4497 break;
4498 }
4499 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004500 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004501
Bram Moolenaar071d4272004-06-13 20:20:40 +00004502 if (!(eap->argt & NOTRLCOM)) /* remove trailing spaces */
4503 del_trailing_spaces(eap->arg);
4504}
4505
4506/*
4507 * get + command from ex argument
4508 */
4509 static char_u *
4510getargcmd(argp)
4511 char_u **argp;
4512{
4513 char_u *arg = *argp;
4514 char_u *command = NULL;
4515
4516 if (*arg == '+') /* +[command] */
4517 {
4518 ++arg;
4519 if (vim_isspace(*arg))
4520 command = dollar_command;
4521 else
4522 {
4523 command = arg;
4524 arg = skip_cmd_arg(command, TRUE);
4525 if (*arg != NUL)
4526 *arg++ = NUL; /* terminate command with NUL */
4527 }
4528
4529 arg = skipwhite(arg); /* skip over spaces */
4530 *argp = arg;
4531 }
4532 return command;
4533}
4534
4535/*
4536 * Find end of "+command" argument. Skip over "\ " and "\\".
4537 */
4538 static char_u *
4539skip_cmd_arg(p, rembs)
4540 char_u *p;
4541 int rembs; /* TRUE to halve the number of backslashes */
4542{
4543 while (*p && !vim_isspace(*p))
4544 {
4545 if (*p == '\\' && p[1] != NUL)
4546 {
4547 if (rembs)
4548 mch_memmove(p, p + 1, STRLEN(p));
4549 else
4550 ++p;
4551 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004552 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004553 }
4554 return p;
4555}
4556
4557/*
4558 * Get "++opt=arg" argument.
4559 * Return FAIL or OK.
4560 */
4561 static int
4562getargopt(eap)
4563 exarg_T *eap;
4564{
4565 char_u *arg = eap->arg + 2;
4566 int *pp = NULL;
4567#ifdef FEAT_MBYTE
4568 char_u *p;
4569#endif
4570
4571 /* ":edit ++[no]bin[ary] file" */
4572 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
4573 {
4574 if (*arg == 'n')
4575 {
4576 arg += 2;
4577 eap->force_bin = FORCE_NOBIN;
4578 }
4579 else
4580 eap->force_bin = FORCE_BIN;
4581 if (!checkforcmd(&arg, "binary", 3))
4582 return FAIL;
4583 eap->arg = skipwhite(arg);
4584 return OK;
4585 }
4586
4587 if (STRNCMP(arg, "ff", 2) == 0)
4588 {
4589 arg += 2;
4590 pp = &eap->force_ff;
4591 }
4592 else if (STRNCMP(arg, "fileformat", 10) == 0)
4593 {
4594 arg += 10;
4595 pp = &eap->force_ff;
4596 }
4597#ifdef FEAT_MBYTE
4598 else if (STRNCMP(arg, "enc", 3) == 0)
4599 {
4600 arg += 3;
4601 pp = &eap->force_enc;
4602 }
4603 else if (STRNCMP(arg, "encoding", 8) == 0)
4604 {
4605 arg += 8;
4606 pp = &eap->force_enc;
4607 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004608 else if (STRNCMP(arg, "bad", 3) == 0)
4609 {
4610 arg += 3;
4611 pp = &eap->bad_char;
4612 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004613#endif
4614
4615 if (pp == NULL || *arg != '=')
4616 return FAIL;
4617
4618 ++arg;
4619 *pp = (int)(arg - eap->cmd);
4620 arg = skip_cmd_arg(arg, FALSE);
4621 eap->arg = skipwhite(arg);
4622 *arg = NUL;
4623
4624#ifdef FEAT_MBYTE
4625 if (pp == &eap->force_ff)
4626 {
4627#endif
4628 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
4629 return FAIL;
4630#ifdef FEAT_MBYTE
4631 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004632 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004633 {
4634 /* Make 'fileencoding' lower case. */
4635 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
4636 *p = TOLOWER_ASC(*p);
4637 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004638 else
4639 {
4640 /* Check ++bad= argument. Must be a single-byte character, "keep" or
4641 * "drop". */
4642 p = eap->cmd + eap->bad_char;
4643 if (STRICMP(p, "keep") == 0)
4644 eap->bad_char = BAD_KEEP;
4645 else if (STRICMP(p, "drop") == 0)
4646 eap->bad_char = BAD_DROP;
4647 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
4648 eap->bad_char = *p;
4649 else
4650 return FAIL;
4651 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004652#endif
4653
4654 return OK;
4655}
4656
4657/*
4658 * ":abbreviate" and friends.
4659 */
4660 static void
4661ex_abbreviate(eap)
4662 exarg_T *eap;
4663{
4664 do_exmap(eap, TRUE); /* almost the same as mapping */
4665}
4666
4667/*
4668 * ":map" and friends.
4669 */
4670 static void
4671ex_map(eap)
4672 exarg_T *eap;
4673{
4674 /*
4675 * If we are sourcing .exrc or .vimrc in current directory we
4676 * print the mappings for security reasons.
4677 */
4678 if (secure)
4679 {
4680 secure = 2;
4681 msg_outtrans(eap->cmd);
4682 msg_putchar('\n');
4683 }
4684 do_exmap(eap, FALSE);
4685}
4686
4687/*
4688 * ":unmap" and friends.
4689 */
4690 static void
4691ex_unmap(eap)
4692 exarg_T *eap;
4693{
4694 do_exmap(eap, FALSE);
4695}
4696
4697/*
4698 * ":mapclear" and friends.
4699 */
4700 static void
4701ex_mapclear(eap)
4702 exarg_T *eap;
4703{
4704 map_clear(eap->cmd, eap->arg, eap->forceit, FALSE);
4705}
4706
4707/*
4708 * ":abclear" and friends.
4709 */
4710 static void
4711ex_abclear(eap)
4712 exarg_T *eap;
4713{
4714 map_clear(eap->cmd, eap->arg, TRUE, TRUE);
4715}
4716
4717#ifdef FEAT_AUTOCMD
4718 static void
4719ex_autocmd(eap)
4720 exarg_T *eap;
4721{
4722 /*
4723 * Disallow auto commands from .exrc and .vimrc in current
4724 * directory for security reasons.
4725 */
4726 if (secure)
4727 {
4728 secure = 2;
4729 eap->errmsg = e_curdir;
4730 }
4731 else if (eap->cmdidx == CMD_autocmd)
4732 do_autocmd(eap->arg, eap->forceit);
4733 else
4734 do_augroup(eap->arg, eap->forceit);
4735}
4736
4737/*
4738 * ":doautocmd": Apply the automatic commands to the current buffer.
4739 */
4740 static void
4741ex_doautocmd(eap)
4742 exarg_T *eap;
4743{
4744 (void)do_doautocmd(eap->arg, TRUE);
Bram Moolenaara3227e22006-03-08 21:32:40 +00004745 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004746}
4747#endif
4748
4749#ifdef FEAT_LISTCMDS
4750/*
4751 * :[N]bunload[!] [N] [bufname] unload buffer
4752 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
4753 * :[N]bwipeout[!] [N] [bufname] delete buffer really
4754 */
4755 static void
4756ex_bunload(eap)
4757 exarg_T *eap;
4758{
4759 eap->errmsg = do_bufdel(
4760 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
4761 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
4762 : DOBUF_UNLOAD, eap->arg,
4763 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
4764}
4765
4766/*
4767 * :[N]buffer [N] to buffer N
4768 * :[N]sbuffer [N] to buffer N
4769 */
4770 static void
4771ex_buffer(eap)
4772 exarg_T *eap;
4773{
4774 if (*eap->arg)
4775 eap->errmsg = e_trailing;
4776 else
4777 {
4778 if (eap->addr_count == 0) /* default is current buffer */
4779 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
4780 else
4781 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
4782 }
4783}
4784
4785/*
4786 * :[N]bmodified [N] to next mod. buffer
4787 * :[N]sbmodified [N] to next mod. buffer
4788 */
4789 static void
4790ex_bmodified(eap)
4791 exarg_T *eap;
4792{
4793 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
4794}
4795
4796/*
4797 * :[N]bnext [N] to next buffer
4798 * :[N]sbnext [N] split and to next buffer
4799 */
4800 static void
4801ex_bnext(eap)
4802 exarg_T *eap;
4803{
4804 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
4805}
4806
4807/*
4808 * :[N]bNext [N] to previous buffer
4809 * :[N]bprevious [N] to previous buffer
4810 * :[N]sbNext [N] split and to previous buffer
4811 * :[N]sbprevious [N] split and to previous buffer
4812 */
4813 static void
4814ex_bprevious(eap)
4815 exarg_T *eap;
4816{
4817 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
4818}
4819
4820/*
4821 * :brewind to first buffer
4822 * :bfirst to first buffer
4823 * :sbrewind split and to first buffer
4824 * :sbfirst split and to first buffer
4825 */
4826 static void
4827ex_brewind(eap)
4828 exarg_T *eap;
4829{
4830 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
4831}
4832
4833/*
4834 * :blast to last buffer
4835 * :sblast split and to last buffer
4836 */
4837 static void
4838ex_blast(eap)
4839 exarg_T *eap;
4840{
4841 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
4842}
4843#endif
4844
4845 int
4846ends_excmd(c)
4847 int c;
4848{
4849 return (c == NUL || c == '|' || c == '"' || c == '\n');
4850}
4851
4852#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
4853 || defined(PROTO)
4854/*
4855 * Return the next command, after the first '|' or '\n'.
4856 * Return NULL if not found.
4857 */
4858 char_u *
4859find_nextcmd(p)
4860 char_u *p;
4861{
4862 while (*p != '|' && *p != '\n')
4863 {
4864 if (*p == NUL)
4865 return NULL;
4866 ++p;
4867 }
4868 return (p + 1);
4869}
4870#endif
4871
4872/*
4873 * Check if *p is a separator between Ex commands.
4874 * Return NULL if it isn't, (p + 1) if it is.
4875 */
4876 char_u *
4877check_nextcmd(p)
4878 char_u *p;
4879{
4880 p = skipwhite(p);
4881 if (*p == '|' || *p == '\n')
4882 return (p + 1);
4883 else
4884 return NULL;
4885}
4886
4887/*
4888 * - if there are more files to edit
4889 * - and this is the last window
4890 * - and forceit not used
4891 * - and not repeated twice on a row
4892 * return FAIL and give error message if 'message' TRUE
4893 * return OK otherwise
4894 */
4895 static int
4896check_more(message, forceit)
4897 int message; /* when FALSE check only, no messages */
4898 int forceit;
4899{
4900 int n = ARGCOUNT - curwin->w_arg_idx - 1;
4901
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00004902 if (!forceit && only_one_window()
4903 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004904 {
4905 if (message)
4906 {
4907#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
4908 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
4909 {
4910 char_u buff[IOSIZE];
4911
4912 if (n == 1)
4913 STRCPY(buff, _("1 more file to edit. Quit anyway?"));
4914 else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004915 vim_snprintf((char *)buff, IOSIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916 _("%d more files to edit. Quit anyway?"), n);
4917 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
4918 return OK;
4919 return FAIL;
4920 }
4921#endif
4922 if (n == 1)
4923 EMSG(_("E173: 1 more file to edit"));
4924 else
4925 EMSGN(_("E173: %ld more files to edit"), n);
4926 quitmore = 2; /* next try to quit is allowed */
4927 }
4928 return FAIL;
4929 }
4930 return OK;
4931}
4932
4933#ifdef FEAT_CMDL_COMPL
4934/*
4935 * Function given to ExpandGeneric() to obtain the list of command names.
4936 */
4937/*ARGSUSED*/
4938 char_u *
4939get_command_name(xp, idx)
4940 expand_T *xp;
4941 int idx;
4942{
4943 if (idx >= (int)CMD_SIZE)
4944# ifdef FEAT_USR_CMDS
4945 return get_user_command_name(idx);
4946# else
4947 return NULL;
4948# endif
4949 return cmdnames[idx].cmd_name;
4950}
4951#endif
4952
4953#if defined(FEAT_USR_CMDS) || defined(PROTO)
4954static 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));
4955static void uc_list __ARGS((char_u *name, size_t name_len));
4956static int uc_scan_attr __ARGS((char_u *attr, size_t len, long *argt, long *def, int *flags, int *compl, char_u **compl_arg));
4957static char_u *uc_split_args __ARGS((char_u *arg, size_t *lenp));
4958static 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));
4959
4960 static int
4961uc_add_command(name, name_len, rep, argt, def, flags, compl, compl_arg, force)
4962 char_u *name;
4963 size_t name_len;
4964 char_u *rep;
4965 long argt;
4966 long def;
4967 int flags;
4968 int compl;
4969 char_u *compl_arg;
4970 int force;
4971{
4972 ucmd_T *cmd = NULL;
4973 char_u *p;
4974 int i;
4975 int cmp = 1;
4976 char_u *rep_buf = NULL;
4977 garray_T *gap;
4978
4979 replace_termcodes(rep, &rep_buf, FALSE, FALSE);
4980 if (rep_buf == NULL)
4981 {
4982 /* Can't replace termcodes - try using the string as is */
4983 rep_buf = vim_strsave(rep);
4984
4985 /* Give up if out of memory */
4986 if (rep_buf == NULL)
4987 return FAIL;
4988 }
4989
4990 /* get address of growarray: global or in curbuf */
4991 if (flags & UC_BUFFER)
4992 {
4993 gap = &curbuf->b_ucmds;
4994 if (gap->ga_itemsize == 0)
4995 ga_init2(gap, (int)sizeof(ucmd_T), 4);
4996 }
4997 else
4998 gap = &ucmds;
4999
5000 /* Search for the command in the already defined commands. */
5001 for (i = 0; i < gap->ga_len; ++i)
5002 {
5003 size_t len;
5004
5005 cmd = USER_CMD_GA(gap, i);
5006 len = STRLEN(cmd->uc_name);
5007 cmp = STRNCMP(name, cmd->uc_name, name_len);
5008 if (cmp == 0)
5009 {
5010 if (name_len < len)
5011 cmp = -1;
5012 else if (name_len > len)
5013 cmp = 1;
5014 }
5015
5016 if (cmp == 0)
5017 {
5018 if (!force)
5019 {
5020 EMSG(_("E174: Command already exists: add ! to replace it"));
5021 goto fail;
5022 }
5023
5024 vim_free(cmd->uc_rep);
5025 cmd->uc_rep = 0;
5026 break;
5027 }
5028
5029 /* Stop as soon as we pass the name to add */
5030 if (cmp < 0)
5031 break;
5032 }
5033
5034 /* Extend the array unless we're replacing an existing command */
5035 if (cmp != 0)
5036 {
5037 if (ga_grow(gap, 1) != OK)
5038 goto fail;
5039 if ((p = vim_strnsave(name, (int)name_len)) == NULL)
5040 goto fail;
5041
5042 cmd = USER_CMD_GA(gap, i);
5043 mch_memmove(cmd + 1, cmd, (gap->ga_len - i) * sizeof(ucmd_T));
5044
5045 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005046
5047 cmd->uc_name = p;
5048 }
5049
5050 cmd->uc_rep = rep_buf;
5051 cmd->uc_argt = argt;
5052 cmd->uc_def = def;
5053 cmd->uc_compl = compl;
5054#ifdef FEAT_EVAL
5055 cmd->uc_scriptID = current_SID;
5056# ifdef FEAT_CMDL_COMPL
5057 cmd->uc_compl_arg = compl_arg;
5058# endif
5059#endif
5060
5061 return OK;
5062
5063fail:
5064 vim_free(rep_buf);
5065#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5066 vim_free(compl_arg);
5067#endif
5068 return FAIL;
5069}
5070
5071/*
5072 * List of names for completion for ":command" with the EXPAND_ flag.
5073 * Must be alphabetical for completion.
5074 */
5075static struct
5076{
5077 int expand;
5078 char *name;
5079} command_complete[] =
5080{
5081 {EXPAND_AUGROUP, "augroup"},
5082 {EXPAND_BUFFERS, "buffer"},
5083 {EXPAND_COMMANDS, "command"},
5084#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5085 {EXPAND_USER_DEFINED, "custom"},
Bram Moolenaara466c992005-07-09 21:03:22 +00005086 {EXPAND_USER_LIST, "customlist"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005087#endif
5088 {EXPAND_DIRECTORIES, "dir"},
5089 {EXPAND_ENV_VARS, "environment"},
5090 {EXPAND_EVENTS, "event"},
5091 {EXPAND_EXPRESSION, "expression"},
5092 {EXPAND_FILES, "file"},
5093 {EXPAND_FUNCTIONS, "function"},
5094 {EXPAND_HELP, "help"},
5095 {EXPAND_HIGHLIGHT, "highlight"},
5096 {EXPAND_MAPPINGS, "mapping"},
5097 {EXPAND_MENUS, "menu"},
5098 {EXPAND_SETTINGS, "option"},
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005099 {EXPAND_SHELLCMD, "shellcmd"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005100 {EXPAND_TAGS, "tag"},
5101 {EXPAND_TAGS_LISTFILES, "tag_listfiles"},
5102 {EXPAND_USER_VARS, "var"},
5103 {0, NULL}
5104};
5105
5106 static void
5107uc_list(name, name_len)
5108 char_u *name;
5109 size_t name_len;
5110{
5111 int i, j;
5112 int found = FALSE;
5113 ucmd_T *cmd;
5114 int len;
5115 long a;
5116 garray_T *gap;
5117
5118 gap = &curbuf->b_ucmds;
5119 for (;;)
5120 {
5121 for (i = 0; i < gap->ga_len; ++i)
5122 {
5123 cmd = USER_CMD_GA(gap, i);
5124 a = cmd->uc_argt;
5125
5126 /* Skip commands which don't match the requested prefix */
5127 if (STRNCMP(name, cmd->uc_name, name_len) != 0)
5128 continue;
5129
5130 /* Put out the title first time */
5131 if (!found)
5132 MSG_PUTS_TITLE(_("\n Name Args Range Complete Definition"));
5133 found = TRUE;
5134 msg_putchar('\n');
5135 if (got_int)
5136 break;
5137
5138 /* Special cases */
5139 msg_putchar(a & BANG ? '!' : ' ');
5140 msg_putchar(a & REGSTR ? '"' : ' ');
5141 msg_putchar(gap != &ucmds ? 'b' : ' ');
5142 msg_putchar(' ');
5143
5144 msg_outtrans_attr(cmd->uc_name, hl_attr(HLF_D));
5145 len = (int)STRLEN(cmd->uc_name) + 4;
5146
5147 do {
5148 msg_putchar(' ');
5149 ++len;
5150 } while (len < 16);
5151
5152 len = 0;
5153
5154 /* Arguments */
5155 switch ((int)(a & (EXTRA|NOSPC|NEEDARG)))
5156 {
5157 case 0: IObuff[len++] = '0'; break;
5158 case (EXTRA): IObuff[len++] = '*'; break;
5159 case (EXTRA|NOSPC): IObuff[len++] = '?'; break;
5160 case (EXTRA|NEEDARG): IObuff[len++] = '+'; break;
5161 case (EXTRA|NOSPC|NEEDARG): IObuff[len++] = '1'; break;
5162 }
5163
5164 do {
5165 IObuff[len++] = ' ';
5166 } while (len < 5);
5167
5168 /* Range */
5169 if (a & (RANGE|COUNT))
5170 {
5171 if (a & COUNT)
5172 {
5173 /* -count=N */
5174 sprintf((char *)IObuff + len, "%ldc", cmd->uc_def);
5175 len += (int)STRLEN(IObuff + len);
5176 }
5177 else if (a & DFLALL)
5178 IObuff[len++] = '%';
5179 else if (cmd->uc_def >= 0)
5180 {
5181 /* -range=N */
5182 sprintf((char *)IObuff + len, "%ld", cmd->uc_def);
5183 len += (int)STRLEN(IObuff + len);
5184 }
5185 else
5186 IObuff[len++] = '.';
5187 }
5188
5189 do {
5190 IObuff[len++] = ' ';
5191 } while (len < 11);
5192
5193 /* Completion */
5194 for (j = 0; command_complete[j].expand != 0; ++j)
5195 if (command_complete[j].expand == cmd->uc_compl)
5196 {
5197 STRCPY(IObuff + len, command_complete[j].name);
5198 len += (int)STRLEN(IObuff + len);
5199 break;
5200 }
5201
5202 do {
5203 IObuff[len++] = ' ';
5204 } while (len < 21);
5205
5206 IObuff[len] = '\0';
5207 msg_outtrans(IObuff);
5208
5209 msg_outtrans_special(cmd->uc_rep, FALSE);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005210#ifdef FEAT_EVAL
5211 if (p_verbose > 0)
5212 last_set_msg(cmd->uc_scriptID);
5213#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214 out_flush();
5215 ui_breakcheck();
5216 if (got_int)
5217 break;
5218 }
5219 if (gap == &ucmds || i < gap->ga_len)
5220 break;
5221 gap = &ucmds;
5222 }
5223
5224 if (!found)
5225 MSG(_("No user-defined commands found"));
5226}
5227
5228 static char_u *
5229uc_fun_cmd()
5230{
5231 static char_u fcmd[] = {0x84, 0xaf, 0x60, 0xb9, 0xaf, 0xb5, 0x60, 0xa4,
5232 0xa5, 0xad, 0xa1, 0xae, 0xa4, 0x60, 0xa1, 0x60,
5233 0xb3, 0xa8, 0xb2, 0xb5, 0xa2, 0xa2, 0xa5, 0xb2,
5234 0xb9, 0x7f, 0};
5235 int i;
5236
5237 for (i = 0; fcmd[i]; ++i)
5238 IObuff[i] = fcmd[i] - 0x40;
5239 IObuff[i] = 0;
5240 return IObuff;
5241}
5242
5243 static int
5244uc_scan_attr(attr, len, argt, def, flags, compl, compl_arg)
5245 char_u *attr;
5246 size_t len;
5247 long *argt;
5248 long *def;
5249 int *flags;
5250 int *compl;
5251 char_u **compl_arg;
5252{
5253 char_u *p;
5254
5255 if (len == 0)
5256 {
5257 EMSG(_("E175: No attribute specified"));
5258 return FAIL;
5259 }
5260
5261 /* First, try the simple attributes (no arguments) */
5262 if (STRNICMP(attr, "bang", len) == 0)
5263 *argt |= BANG;
5264 else if (STRNICMP(attr, "buffer", len) == 0)
5265 *flags |= UC_BUFFER;
5266 else if (STRNICMP(attr, "register", len) == 0)
5267 *argt |= REGSTR;
5268 else if (STRNICMP(attr, "bar", len) == 0)
5269 *argt |= TRLBAR;
5270 else
5271 {
5272 int i;
5273 char_u *val = NULL;
5274 size_t vallen = 0;
5275 size_t attrlen = len;
5276
5277 /* Look for the attribute name - which is the part before any '=' */
5278 for (i = 0; i < (int)len; ++i)
5279 {
5280 if (attr[i] == '=')
5281 {
5282 val = &attr[i + 1];
5283 vallen = len - i - 1;
5284 attrlen = i;
5285 break;
5286 }
5287 }
5288
5289 if (STRNICMP(attr, "nargs", attrlen) == 0)
5290 {
5291 if (vallen == 1)
5292 {
5293 if (*val == '0')
5294 /* Do nothing - this is the default */;
5295 else if (*val == '1')
5296 *argt |= (EXTRA | NOSPC | NEEDARG);
5297 else if (*val == '*')
5298 *argt |= EXTRA;
5299 else if (*val == '?')
5300 *argt |= (EXTRA | NOSPC);
5301 else if (*val == '+')
5302 *argt |= (EXTRA | NEEDARG);
5303 else
5304 goto wrong_nargs;
5305 }
5306 else
5307 {
5308wrong_nargs:
5309 EMSG(_("E176: Invalid number of arguments"));
5310 return FAIL;
5311 }
5312 }
5313 else if (STRNICMP(attr, "range", attrlen) == 0)
5314 {
5315 *argt |= RANGE;
5316 if (vallen == 1 && *val == '%')
5317 *argt |= DFLALL;
5318 else if (val != NULL)
5319 {
5320 p = val;
5321 if (*def >= 0)
5322 {
5323two_count:
5324 EMSG(_("E177: Count cannot be specified twice"));
5325 return FAIL;
5326 }
5327
5328 *def = getdigits(&p);
5329 *argt |= (ZEROR | NOTADR);
5330
5331 if (p != val + vallen || vallen == 0)
5332 {
5333invalid_count:
5334 EMSG(_("E178: Invalid default value for count"));
5335 return FAIL;
5336 }
5337 }
5338 }
5339 else if (STRNICMP(attr, "count", attrlen) == 0)
5340 {
Bram Moolenaar32e7b2d2005-02-27 22:36:47 +00005341 *argt |= (COUNT | ZEROR | RANGE | NOTADR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005342
5343 if (val != NULL)
5344 {
5345 p = val;
5346 if (*def >= 0)
5347 goto two_count;
5348
5349 *def = getdigits(&p);
5350
5351 if (p != val + vallen)
5352 goto invalid_count;
5353 }
5354
5355 if (*def < 0)
5356 *def = 0;
5357 }
5358 else if (STRNICMP(attr, "complete", attrlen) == 0)
5359 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005360 if (val == NULL)
5361 {
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005362 EMSG(_("E179: argument required for -complete"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005363 return FAIL;
5364 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005365
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005366 if (parse_compl_arg(val, (int)vallen, compl, argt, compl_arg)
5367 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005368 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005369 }
5370 else
5371 {
5372 char_u ch = attr[len];
5373 attr[len] = '\0';
5374 EMSG2(_("E181: Invalid attribute: %s"), attr);
5375 attr[len] = ch;
5376 return FAIL;
5377 }
5378 }
5379
5380 return OK;
5381}
5382
5383 static void
5384ex_command(eap)
5385 exarg_T *eap;
5386{
5387 char_u *name;
5388 char_u *end;
5389 char_u *p;
5390 long argt = 0;
5391 long def = -1;
5392 int flags = 0;
5393 int compl = EXPAND_NOTHING;
5394 char_u *compl_arg = NULL;
5395 int has_attr = (eap->arg[0] == '-');
5396
5397 p = eap->arg;
5398
5399 /* Check for attributes */
5400 while (*p == '-')
5401 {
5402 ++p;
5403 end = skiptowhite(p);
5404 if (uc_scan_attr(p, end - p, &argt, &def, &flags, &compl, &compl_arg)
5405 == FAIL)
5406 return;
5407 p = skipwhite(end);
5408 }
5409
5410 /* Get the name (if any) and skip to the following argument */
5411 name = p;
5412 if (ASCII_ISALPHA(*p))
5413 while (ASCII_ISALNUM(*p))
5414 ++p;
5415 if (!ends_excmd(*p) && !vim_iswhite(*p))
5416 {
5417 EMSG(_("E182: Invalid command name"));
5418 return;
5419 }
5420 end = p;
5421
5422 /* If there is nothing after the name, and no attributes were specified,
5423 * we are listing commands
5424 */
5425 p = skipwhite(end);
5426 if (!has_attr && ends_excmd(*p))
5427 {
5428 uc_list(name, end - name);
5429 }
5430 else if (!ASCII_ISUPPER(*name))
5431 {
5432 EMSG(_("E183: User defined commands must start with an uppercase letter"));
5433 return;
5434 }
5435 else
5436 uc_add_command(name, end - name, p, argt, def, flags, compl, compl_arg,
5437 eap->forceit);
5438}
5439
5440/*
5441 * ":comclear"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005442 * Clear all user commands, global and for current buffer.
5443 */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00005444/*ARGSUSED*/
5445 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005446ex_comclear(eap)
5447 exarg_T *eap;
5448{
5449 uc_clear(&ucmds);
5450 uc_clear(&curbuf->b_ucmds);
5451}
5452
5453/*
5454 * Clear all user commands for "gap".
5455 */
5456 void
5457uc_clear(gap)
5458 garray_T *gap;
5459{
5460 int i;
5461 ucmd_T *cmd;
5462
5463 for (i = 0; i < gap->ga_len; ++i)
5464 {
5465 cmd = USER_CMD_GA(gap, i);
5466 vim_free(cmd->uc_name);
5467 vim_free(cmd->uc_rep);
5468# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5469 vim_free(cmd->uc_compl_arg);
5470# endif
5471 }
5472 ga_clear(gap);
5473}
5474
5475 static void
5476ex_delcommand(eap)
5477 exarg_T *eap;
5478{
5479 int i = 0;
5480 ucmd_T *cmd = NULL;
5481 int cmp = -1;
5482 garray_T *gap;
5483
5484 gap = &curbuf->b_ucmds;
5485 for (;;)
5486 {
5487 for (i = 0; i < gap->ga_len; ++i)
5488 {
5489 cmd = USER_CMD_GA(gap, i);
5490 cmp = STRCMP(eap->arg, cmd->uc_name);
5491 if (cmp <= 0)
5492 break;
5493 }
5494 if (gap == &ucmds || cmp == 0)
5495 break;
5496 gap = &ucmds;
5497 }
5498
5499 if (cmp != 0)
5500 {
5501 EMSG2(_("E184: No such user-defined command: %s"), eap->arg);
5502 return;
5503 }
5504
5505 vim_free(cmd->uc_name);
5506 vim_free(cmd->uc_rep);
5507# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5508 vim_free(cmd->uc_compl_arg);
5509# endif
5510
5511 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005512
5513 if (i < gap->ga_len)
5514 mch_memmove(cmd, cmd + 1, (gap->ga_len - i) * sizeof(ucmd_T));
5515}
5516
5517 static char_u *
5518uc_split_args(arg, lenp)
5519 char_u *arg;
5520 size_t *lenp;
5521{
5522 char_u *buf;
5523 char_u *p;
5524 char_u *q;
5525 int len;
5526
5527 /* Precalculate length */
5528 p = arg;
5529 len = 2; /* Initial and final quotes */
5530
5531 while (*p)
5532 {
5533 if (p[0] == '\\' && vim_iswhite(p[1]))
5534 {
5535 len += 1;
5536 p += 2;
5537 }
5538 else if (*p == '\\' || *p == '"')
5539 {
5540 len += 2;
5541 p += 1;
5542 }
5543 else if (vim_iswhite(*p))
5544 {
5545 p = skipwhite(p);
5546 if (*p == NUL)
5547 break;
5548 len += 3; /* "," */
5549 }
5550 else
5551 {
5552 ++len;
5553 ++p;
5554 }
5555 }
5556
5557 buf = alloc(len + 1);
5558 if (buf == NULL)
5559 {
5560 *lenp = 0;
5561 return buf;
5562 }
5563
5564 p = arg;
5565 q = buf;
5566 *q++ = '"';
5567 while (*p)
5568 {
5569 if (p[0] == '\\' && vim_iswhite(p[1]))
5570 {
5571 *q++ = p[1];
5572 p += 2;
5573 }
5574 else if (*p == '\\' || *p == '"')
5575 {
5576 *q++ = '\\';
5577 *q++ = *p++;
5578 }
5579 else if (vim_iswhite(*p))
5580 {
5581 p = skipwhite(p);
5582 if (*p == NUL)
5583 break;
5584 *q++ = '"';
5585 *q++ = ',';
5586 *q++ = '"';
5587 }
5588 else
5589 {
5590 *q++ = *p++;
5591 }
5592 }
5593 *q++ = '"';
5594 *q = 0;
5595
5596 *lenp = len;
5597 return buf;
5598}
5599
5600/*
5601 * Check for a <> code in a user command.
5602 * "code" points to the '<'. "len" the length of the <> (inclusive).
5603 * "buf" is where the result is to be added.
5604 * "split_buf" points to a buffer used for splitting, caller should free it.
5605 * "split_len" is the length of what "split_buf" contains.
5606 * Returns the length of the replacement, which has been added to "buf".
5607 * Returns -1 if there was no match, and only the "<" has been copied.
5608 */
5609 static size_t
5610uc_check_code(code, len, buf, cmd, eap, split_buf, split_len)
5611 char_u *code;
5612 size_t len;
5613 char_u *buf;
5614 ucmd_T *cmd; /* the user command we're expanding */
5615 exarg_T *eap; /* ex arguments */
5616 char_u **split_buf;
5617 size_t *split_len;
5618{
5619 size_t result = 0;
5620 char_u *p = code + 1;
5621 size_t l = len - 2;
5622 int quote = 0;
5623 enum { ct_ARGS, ct_BANG, ct_COUNT, ct_LINE1, ct_LINE2, ct_REGISTER,
5624 ct_LT, ct_NONE } type = ct_NONE;
5625
5626 if ((vim_strchr((char_u *)"qQfF", *p) != NULL) && p[1] == '-')
5627 {
5628 quote = (*p == 'q' || *p == 'Q') ? 1 : 2;
5629 p += 2;
5630 l -= 2;
5631 }
5632
5633 if (l < 1)
5634 type = ct_NONE;
5635 else if (STRNICMP(p, "args", l) == 0)
5636 type = ct_ARGS;
5637 else if (STRNICMP(p, "bang", l) == 0)
5638 type = ct_BANG;
5639 else if (STRNICMP(p, "count", l) == 0)
5640 type = ct_COUNT;
5641 else if (STRNICMP(p, "line1", l) == 0)
5642 type = ct_LINE1;
5643 else if (STRNICMP(p, "line2", l) == 0)
5644 type = ct_LINE2;
5645 else if (STRNICMP(p, "lt", l) == 0)
5646 type = ct_LT;
5647 else if (STRNICMP(p, "register", l) == 0)
5648 type = ct_REGISTER;
5649
5650 switch (type)
5651 {
5652 case ct_ARGS:
5653 /* Simple case first */
5654 if (*eap->arg == NUL)
5655 {
5656 if (quote == 1)
5657 {
5658 result = 2;
5659 if (buf != NULL)
5660 STRCPY(buf, "''");
5661 }
5662 else
5663 result = 0;
5664 break;
5665 }
5666
5667 /* When specified there is a single argument don't split it.
5668 * Works for ":Cmd %" when % is "a b c". */
5669 if ((eap->argt & NOSPC) && quote == 2)
5670 quote = 1;
5671
5672 switch (quote)
5673 {
5674 case 0: /* No quoting, no splitting */
5675 result = STRLEN(eap->arg);
5676 if (buf != NULL)
5677 STRCPY(buf, eap->arg);
5678 break;
5679 case 1: /* Quote, but don't split */
5680 result = STRLEN(eap->arg) + 2;
5681 for (p = eap->arg; *p; ++p)
5682 {
5683 if (*p == '\\' || *p == '"')
5684 ++result;
5685 }
5686
5687 if (buf != NULL)
5688 {
5689 *buf++ = '"';
5690 for (p = eap->arg; *p; ++p)
5691 {
5692 if (*p == '\\' || *p == '"')
5693 *buf++ = '\\';
5694 *buf++ = *p;
5695 }
5696 *buf = '"';
5697 }
5698
5699 break;
5700 case 2: /* Quote and split */
5701 /* This is hard, so only do it once, and cache the result */
5702 if (*split_buf == NULL)
5703 *split_buf = uc_split_args(eap->arg, split_len);
5704
5705 result = *split_len;
5706 if (buf != NULL && result != 0)
5707 STRCPY(buf, *split_buf);
5708
5709 break;
5710 }
5711 break;
5712
5713 case ct_BANG:
5714 result = eap->forceit ? 1 : 0;
5715 if (quote)
5716 result += 2;
5717 if (buf != NULL)
5718 {
5719 if (quote)
5720 *buf++ = '"';
5721 if (eap->forceit)
5722 *buf++ = '!';
5723 if (quote)
5724 *buf = '"';
5725 }
5726 break;
5727
5728 case ct_LINE1:
5729 case ct_LINE2:
5730 case ct_COUNT:
5731 {
5732 char num_buf[20];
5733 long num = (type == ct_LINE1) ? eap->line1 :
5734 (type == ct_LINE2) ? eap->line2 :
5735 (eap->addr_count > 0) ? eap->line2 : cmd->uc_def;
5736 size_t num_len;
5737
5738 sprintf(num_buf, "%ld", num);
5739 num_len = STRLEN(num_buf);
5740 result = num_len;
5741
5742 if (quote)
5743 result += 2;
5744
5745 if (buf != NULL)
5746 {
5747 if (quote)
5748 *buf++ = '"';
5749 STRCPY(buf, num_buf);
5750 buf += num_len;
5751 if (quote)
5752 *buf = '"';
5753 }
5754
5755 break;
5756 }
5757
5758 case ct_REGISTER:
5759 result = eap->regname ? 1 : 0;
5760 if (quote)
5761 result += 2;
5762 if (buf != NULL)
5763 {
5764 if (quote)
5765 *buf++ = '\'';
5766 if (eap->regname)
5767 *buf++ = eap->regname;
5768 if (quote)
5769 *buf = '\'';
5770 }
5771 break;
5772
5773 case ct_LT:
5774 result = 1;
5775 if (buf != NULL)
5776 *buf = '<';
5777 break;
5778
5779 default:
5780 /* Not recognized: just copy the '<' and return -1. */
5781 result = (size_t)-1;
5782 if (buf != NULL)
5783 *buf = '<';
5784 break;
5785 }
5786
5787 return result;
5788}
5789
5790 static void
5791do_ucmd(eap)
5792 exarg_T *eap;
5793{
5794 char_u *buf;
5795 char_u *p;
5796 char_u *q;
5797
5798 char_u *start;
5799 char_u *end;
5800 size_t len, totlen;
5801
5802 size_t split_len = 0;
5803 char_u *split_buf = NULL;
5804 ucmd_T *cmd;
5805#ifdef FEAT_EVAL
5806 scid_T save_current_SID = current_SID;
5807#endif
5808
5809 if (eap->cmdidx == CMD_USER)
5810 cmd = USER_CMD(eap->useridx);
5811 else
5812 cmd = USER_CMD_GA(&curbuf->b_ucmds, eap->useridx);
5813
5814 /*
5815 * Replace <> in the command by the arguments.
5816 */
5817 buf = NULL;
5818 for (;;)
5819 {
5820 p = cmd->uc_rep;
5821 q = buf;
5822 totlen = 0;
5823 while ((start = vim_strchr(p, '<')) != NULL
5824 && (end = vim_strchr(start + 1, '>')) != NULL)
5825 {
5826 /* Include the '>' */
5827 ++end;
5828
5829 /* Take everything up to the '<' */
5830 len = start - p;
5831 if (buf == NULL)
5832 totlen += len;
5833 else
5834 {
5835 mch_memmove(q, p, len);
5836 q += len;
5837 }
5838
5839 len = uc_check_code(start, end - start, q, cmd, eap,
5840 &split_buf, &split_len);
5841 if (len == (size_t)-1)
5842 {
5843 /* no match, continue after '<' */
5844 p = start + 1;
5845 len = 1;
5846 }
5847 else
5848 p = end;
5849 if (buf == NULL)
5850 totlen += len;
5851 else
5852 q += len;
5853 }
5854 if (buf != NULL) /* second time here, finished */
5855 {
5856 STRCPY(q, p);
5857 break;
5858 }
5859
5860 totlen += STRLEN(p); /* Add on the trailing characters */
5861 buf = alloc((unsigned)(totlen + 1));
5862 if (buf == NULL)
5863 {
5864 vim_free(split_buf);
5865 return;
5866 }
5867 }
5868
5869#ifdef FEAT_EVAL
5870 current_SID = cmd->uc_scriptID;
5871#endif
5872 (void)do_cmdline(buf, eap->getline, eap->cookie,
5873 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
5874#ifdef FEAT_EVAL
5875 current_SID = save_current_SID;
5876#endif
5877 vim_free(buf);
5878 vim_free(split_buf);
5879}
5880
5881# if defined(FEAT_CMDL_COMPL) || defined(PROTO)
5882 static char_u *
5883get_user_command_name(idx)
5884 int idx;
5885{
5886 return get_user_commands(NULL, idx - (int)CMD_SIZE);
5887}
5888
5889/*
5890 * Function given to ExpandGeneric() to obtain the list of user command names.
5891 */
5892/*ARGSUSED*/
5893 char_u *
5894get_user_commands(xp, idx)
5895 expand_T *xp;
5896 int idx;
5897{
5898 if (idx < curbuf->b_ucmds.ga_len)
5899 return USER_CMD_GA(&curbuf->b_ucmds, idx)->uc_name;
5900 idx -= curbuf->b_ucmds.ga_len;
5901 if (idx < ucmds.ga_len)
5902 return USER_CMD(idx)->uc_name;
5903 return NULL;
5904}
5905
5906/*
5907 * Function given to ExpandGeneric() to obtain the list of user command
5908 * attributes.
5909 */
5910/*ARGSUSED*/
5911 char_u *
5912get_user_cmd_flags(xp, idx)
5913 expand_T *xp;
5914 int idx;
5915{
5916 static char *user_cmd_flags[] =
5917 {"bang", "bar", "buffer", "complete", "count",
5918 "nargs", "range", "register"};
5919
5920 if (idx >= sizeof(user_cmd_flags) / sizeof(user_cmd_flags[0]))
5921 return NULL;
5922 return (char_u *)user_cmd_flags[idx];
5923}
5924
5925/*
5926 * Function given to ExpandGeneric() to obtain the list of values for -nargs.
5927 */
5928/*ARGSUSED*/
5929 char_u *
5930get_user_cmd_nargs(xp, idx)
5931 expand_T *xp;
5932 int idx;
5933{
5934 static char *user_cmd_nargs[] = {"0", "1", "*", "?", "+"};
5935
5936 if (idx >= sizeof(user_cmd_nargs) / sizeof(user_cmd_nargs[0]))
5937 return NULL;
5938 return (char_u *)user_cmd_nargs[idx];
5939}
5940
5941/*
5942 * Function given to ExpandGeneric() to obtain the list of values for -complete.
5943 */
5944/*ARGSUSED*/
5945 char_u *
5946get_user_cmd_complete(xp, idx)
5947 expand_T *xp;
5948 int idx;
5949{
5950 return (char_u *)command_complete[idx].name;
5951}
5952# endif /* FEAT_CMDL_COMPL */
5953
5954#endif /* FEAT_USR_CMDS */
5955
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005956#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
5957/*
5958 * Parse a completion argument "value[vallen]".
5959 * The detected completion goes in "*complp", argument type in "*argt".
5960 * When there is an argument, for function and user defined completion, it's
5961 * copied to allocated memory and stored in "*compl_arg".
5962 * Returns FAIL if something is wrong.
5963 */
5964 int
5965parse_compl_arg(value, vallen, complp, argt, compl_arg)
5966 char_u *value;
5967 int vallen;
5968 int *complp;
5969 long *argt;
5970 char_u **compl_arg;
5971{
5972 char_u *arg = NULL;
5973 size_t arglen = 0;
5974 int i;
5975 int valend = vallen;
5976
5977 /* Look for any argument part - which is the part after any ',' */
5978 for (i = 0; i < vallen; ++i)
5979 {
5980 if (value[i] == ',')
5981 {
5982 arg = &value[i + 1];
5983 arglen = vallen - i - 1;
5984 valend = i;
5985 break;
5986 }
5987 }
5988
5989 for (i = 0; command_complete[i].expand != 0; ++i)
5990 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005991 if ((int)STRLEN(command_complete[i].name) == valend
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005992 && STRNCMP(value, command_complete[i].name, valend) == 0)
5993 {
5994 *complp = command_complete[i].expand;
5995 if (command_complete[i].expand == EXPAND_BUFFERS)
5996 *argt |= BUFNAME;
5997 else if (command_complete[i].expand == EXPAND_DIRECTORIES
5998 || command_complete[i].expand == EXPAND_FILES)
5999 *argt |= XFILE;
6000 break;
6001 }
6002 }
6003
6004 if (command_complete[i].expand == 0)
6005 {
6006 EMSG2(_("E180: Invalid complete value: %s"), value);
6007 return FAIL;
6008 }
6009
6010# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6011 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST
6012 && arg != NULL)
6013# else
6014 if (arg != NULL)
6015# endif
6016 {
6017 EMSG(_("E468: Completion argument only allowed for custom completion"));
6018 return FAIL;
6019 }
6020
6021# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6022 if ((*complp == EXPAND_USER_DEFINED || *complp == EXPAND_USER_LIST)
6023 && arg == NULL)
6024 {
6025 EMSG(_("E467: Custom completion requires a function argument"));
6026 return FAIL;
6027 }
6028
6029 if (arg != NULL)
6030 *compl_arg = vim_strnsave(arg, (int)arglen);
6031# endif
6032 return OK;
6033}
6034#endif
6035
Bram Moolenaar071d4272004-06-13 20:20:40 +00006036 static void
6037ex_colorscheme(eap)
6038 exarg_T *eap;
6039{
6040 if (load_colors(eap->arg) == FAIL)
6041 EMSG2(_("E185: Cannot find color scheme %s"), eap->arg);
6042}
6043
6044 static void
6045ex_highlight(eap)
6046 exarg_T *eap;
6047{
6048 if (*eap->arg == NUL && eap->cmd[2] == '!')
6049 MSG(_("Greetings, Vim user!"));
6050 do_highlight(eap->arg, eap->forceit, FALSE);
6051}
6052
6053
6054/*
6055 * Call this function if we thought we were going to exit, but we won't
6056 * (because of an error). May need to restore the terminal mode.
6057 */
6058 void
6059not_exiting()
6060{
6061 exiting = FALSE;
6062 settmode(TMODE_RAW);
6063}
6064
6065/*
6066 * ":quit": quit current window, quit Vim if closed the last window.
6067 */
6068 static void
6069ex_quit(eap)
6070 exarg_T *eap;
6071{
6072#ifdef FEAT_CMDWIN
6073 if (cmdwin_type != 0)
6074 {
6075 cmdwin_result = Ctrl_C;
6076 return;
6077 }
6078#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006079 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006080 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006081 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006082 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006083 return;
6084 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006085
6086#ifdef FEAT_NETBEANS_INTG
6087 netbeansForcedQuit = eap->forceit;
6088#endif
6089
6090 /*
6091 * If there are more files or windows we won't exit.
6092 */
6093 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6094 exiting = TRUE;
6095 if ((!P_HID(curbuf)
6096 && check_changed(curbuf, p_awa, FALSE, eap->forceit, FALSE))
6097 || check_more(TRUE, eap->forceit) == FAIL
6098 || (only_one_window() && check_changed_any(eap->forceit)))
6099 {
6100 not_exiting();
6101 }
6102 else
6103 {
6104#ifdef FEAT_WINDOWS
6105 if (only_one_window()) /* quit last window */
6106#endif
6107 getout(0);
6108#ifdef FEAT_WINDOWS
6109# ifdef FEAT_GUI
6110 need_mouse_correct = TRUE;
6111# endif
6112 /* close window; may free buffer */
6113 win_close(curwin, !P_HID(curwin->w_buffer) || eap->forceit);
6114#endif
6115 }
6116}
6117
6118/*
6119 * ":cquit".
6120 */
6121/*ARGSUSED*/
6122 static void
6123ex_cquit(eap)
6124 exarg_T *eap;
6125{
6126 getout(1); /* this does not always pass on the exit code to the Manx
6127 compiler. why? */
6128}
6129
6130/*
6131 * ":qall": try to quit all windows
6132 */
6133 static void
6134ex_quit_all(eap)
6135 exarg_T *eap;
6136{
6137# ifdef FEAT_CMDWIN
6138 if (cmdwin_type != 0)
6139 {
6140 if (eap->forceit)
6141 cmdwin_result = K_XF1; /* ex_window() takes care of this */
6142 else
6143 cmdwin_result = K_XF2;
6144 return;
6145 }
6146# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006147
6148 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006149 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006150 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006151 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006152 return;
6153 }
6154
Bram Moolenaar071d4272004-06-13 20:20:40 +00006155 exiting = TRUE;
6156 if (eap->forceit || !check_changed_any(FALSE))
6157 getout(0);
6158 not_exiting();
6159}
6160
Bram Moolenaard9967712006-03-11 21:18:15 +00006161#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006162/*
6163 * ":close": close current window, unless it is the last one
6164 */
6165 static void
6166ex_close(eap)
6167 exarg_T *eap;
6168{
6169# ifdef FEAT_CMDWIN
6170 if (cmdwin_type != 0)
6171 cmdwin_result = K_IGNORE;
6172 else
6173# endif
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006174 if (!text_locked())
Bram Moolenaarf740b292006-02-16 22:11:02 +00006175 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006176}
6177
Bram Moolenaard9967712006-03-11 21:18:15 +00006178# ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006179/*
6180 * ":pclose": Close any preview window.
6181 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006182 static void
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006183ex_pclose(eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006184 exarg_T *eap;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006185{
6186 win_T *win;
6187
6188 for (win = firstwin; win != NULL; win = win->w_next)
6189 if (win->w_p_pvw)
6190 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006191 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006192 break;
6193 }
6194}
Bram Moolenaard9967712006-03-11 21:18:15 +00006195# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006196
Bram Moolenaarf740b292006-02-16 22:11:02 +00006197/*
6198 * Close window "win" and take care of handling closing the last window for a
6199 * modified buffer.
6200 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006201 static void
Bram Moolenaarf740b292006-02-16 22:11:02 +00006202ex_win_close(forceit, win, tp)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006203 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006204 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006205 tabpage_T *tp; /* NULL or the tab page "win" is in */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006206{
6207 int need_hide;
6208 buf_T *buf = win->w_buffer;
6209
6210 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006211 if (need_hide && !P_HID(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006212 {
Bram Moolenaard9967712006-03-11 21:18:15 +00006213# if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006214 if ((p_confirm || cmdmod.confirm) && p_write)
6215 {
6216 dialog_changed(buf, FALSE);
6217 if (buf_valid(buf) && bufIsChanged(buf))
6218 return;
6219 need_hide = FALSE;
6220 }
6221 else
Bram Moolenaard9967712006-03-11 21:18:15 +00006222# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006223 {
6224 EMSG(_(e_nowrtmsg));
6225 return;
6226 }
6227 }
6228
Bram Moolenaard9967712006-03-11 21:18:15 +00006229# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006230 need_mouse_correct = TRUE;
Bram Moolenaard9967712006-03-11 21:18:15 +00006231# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006232
Bram Moolenaar071d4272004-06-13 20:20:40 +00006233 /* free buffer when not hiding it or when it's a scratch buffer */
Bram Moolenaarf740b292006-02-16 22:11:02 +00006234 if (tp == NULL)
6235 win_close(win, !need_hide && !P_HID(buf));
6236 else
6237 win_close_othertab(win, !need_hide && !P_HID(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006238}
6239
Bram Moolenaar071d4272004-06-13 20:20:40 +00006240/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006241 * ":tabclose": close current tab page, unless it is the last one.
6242 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006243 */
6244 static void
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006245ex_tabclose(eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006246 exarg_T *eap;
6247{
Bram Moolenaarf740b292006-02-16 22:11:02 +00006248 tabpage_T *tp;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006249 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006250
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006251# ifdef FEAT_CMDWIN
6252 if (cmdwin_type != 0)
6253 cmdwin_result = K_IGNORE;
6254 else
6255# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006256 if (first_tabpage->tp_next == NULL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00006257 EMSG(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00006258 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006259 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006260 if (eap->addr_count > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006261 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006262 tp = find_tabpage((int)eap->line2);
6263 if (tp == NULL)
6264 {
6265 beep_flush();
6266 return;
6267 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00006268 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006269 {
6270 tabpage_close_other(tp, eap->forceit);
6271 return;
6272 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006273 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00006274 if (!text_locked())
6275 tabpage_close(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006276 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006277
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006278 if (h != tabline_height())
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006279 shell_new_rows();
6280}
6281
6282/*
6283 * ":tabonly": close all tab pages except the current one
6284 */
6285 static void
6286ex_tabonly(eap)
6287 exarg_T *eap;
6288{
6289 tabpage_T *tp;
6290 int done;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006291 int h = tabline_height();
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006292
6293# ifdef FEAT_CMDWIN
6294 if (cmdwin_type != 0)
6295 cmdwin_result = K_IGNORE;
6296 else
6297# endif
6298 if (first_tabpage->tp_next == NULL)
6299 MSG(_("Already only one tab page"));
6300 else
6301 {
6302 /* Repeat this up to a 1000 times, because autocommands may mess
6303 * up the lists. */
6304 for (done = 0; done < 1000; ++done)
6305 {
6306 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6307 if (tp->tp_topframe != topframe)
6308 {
6309 tabpage_close_other(tp, eap->forceit);
6310 /* if we failed to close it quit */
6311 if (valid_tabpage(tp))
6312 done = 1000;
6313 /* start over, "tp" is now invalid */
6314 break;
6315 }
6316 if (first_tabpage->tp_next == NULL)
6317 break;
6318 }
6319 }
6320
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006321 if (h != tabline_height())
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006322 shell_new_rows();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006323}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006324
6325/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006326 * Close the current tab page.
6327 */
6328 void
6329tabpage_close(forceit)
6330 int forceit;
6331{
6332 /* First close all the windows but the current one. If that worked then
6333 * close the last window in this tab, that will close it. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006334 if (lastwin != firstwin)
6335 close_others(TRUE, forceit);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006336 if (lastwin == firstwin)
6337 ex_win_close(forceit, curwin, NULL);
6338# ifdef FEAT_GUI
6339 need_mouse_correct = TRUE;
6340# endif
6341}
6342
6343/*
6344 * Close tab page "tp", which is not the current tab page.
6345 * Note that autocommands may make "tp" invalid.
6346 */
6347 void
6348tabpage_close_other(tp, forceit)
6349 tabpage_T *tp;
6350 int forceit;
6351{
6352 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006353 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006354
6355 /* Limit to 1000 windows, autocommands may add a window while we close
6356 * one. OK, so I'm paranoid... */
6357 while (++done < 1000)
6358 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006359 wp = tp->tp_firstwin;
6360 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006361
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006362 /* Autocommands may delete the tab page under our fingers and we may
6363 * fail to close a window with a modified buffer. */
6364 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006365 break;
6366 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006367 redraw_tabline = TRUE;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006368}
6369
6370/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006371 * ":only".
6372 */
6373 static void
6374ex_only(eap)
6375 exarg_T *eap;
6376{
6377# ifdef FEAT_GUI
6378 need_mouse_correct = TRUE;
6379# endif
6380 close_others(TRUE, eap->forceit);
6381}
6382
6383/*
6384 * ":all" and ":sall".
Bram Moolenaard9967712006-03-11 21:18:15 +00006385 * Also used for ":tab drop file ..." after setting the argument list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006386 */
Bram Moolenaard9967712006-03-11 21:18:15 +00006387 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00006388ex_all(eap)
6389 exarg_T *eap;
6390{
6391 if (eap->addr_count == 0)
6392 eap->line2 = 9999;
Bram Moolenaard9967712006-03-11 21:18:15 +00006393 do_arg_all((int)eap->line2, eap->forceit, eap->cmdidx == CMD_drop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006394}
6395#endif /* FEAT_WINDOWS */
6396
6397 static void
6398ex_hide(eap)
6399 exarg_T *eap;
6400{
6401 if (*eap->arg != NUL && check_nextcmd(eap->arg) == NULL)
6402 eap->errmsg = e_invarg;
6403 else
6404 {
6405 /* ":hide" or ":hide | cmd": hide current window */
6406 eap->nextcmd = check_nextcmd(eap->arg);
6407#ifdef FEAT_WINDOWS
6408 if (!eap->skip)
6409 {
6410# ifdef FEAT_GUI
6411 need_mouse_correct = TRUE;
6412# endif
6413 win_close(curwin, FALSE); /* don't free buffer */
6414 }
6415#endif
6416 }
6417}
6418
6419/*
6420 * ":stop" and ":suspend": Suspend Vim.
6421 */
6422 static void
6423ex_stop(eap)
6424 exarg_T *eap;
6425{
6426 /*
6427 * Disallow suspending for "rvim".
6428 */
6429 if (!check_restricted()
6430#ifdef WIN3264
6431 /*
6432 * Check if external commands are allowed now.
6433 */
6434 && can_end_termcap_mode(TRUE)
6435#endif
6436 )
6437 {
6438 if (!eap->forceit)
6439 autowrite_all();
6440 windgoto((int)Rows - 1, 0);
6441 out_char('\n');
6442 out_flush();
6443 stoptermcap();
6444 out_flush(); /* needed for SUN to restore xterm buffer */
6445#ifdef FEAT_TITLE
6446 mch_restore_title(3); /* restore window titles */
6447#endif
6448 ui_suspend(); /* call machine specific function */
6449#ifdef FEAT_TITLE
6450 maketitle();
6451 resettitle(); /* force updating the title */
6452#endif
6453 starttermcap();
6454 scroll_start(); /* scroll screen before redrawing */
6455 redraw_later_clear();
6456 shell_resized(); /* may have resized window */
6457 }
6458}
6459
6460/*
6461 * ":exit", ":xit" and ":wq": Write file and exit Vim.
6462 */
6463 static void
6464ex_exit(eap)
6465 exarg_T *eap;
6466{
6467#ifdef FEAT_CMDWIN
6468 if (cmdwin_type != 0)
6469 {
6470 cmdwin_result = Ctrl_C;
6471 return;
6472 }
6473#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006474 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006475 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006476 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006477 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006478 return;
6479 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006480
6481 /*
6482 * if more files or windows we won't exit
6483 */
6484 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6485 exiting = TRUE;
6486 if ( ((eap->cmdidx == CMD_wq
6487 || curbufIsChanged())
6488 && do_write(eap) == FAIL)
6489 || check_more(TRUE, eap->forceit) == FAIL
6490 || (only_one_window() && check_changed_any(eap->forceit)))
6491 {
6492 not_exiting();
6493 }
6494 else
6495 {
6496#ifdef FEAT_WINDOWS
6497 if (only_one_window()) /* quit last window, exit Vim */
6498#endif
6499 getout(0);
6500#ifdef FEAT_WINDOWS
6501# ifdef FEAT_GUI
6502 need_mouse_correct = TRUE;
6503# endif
6504 /* quit current window, may free buffer */
6505 win_close(curwin, !P_HID(curwin->w_buffer));
6506#endif
6507 }
6508}
6509
6510/*
6511 * ":print", ":list", ":number".
6512 */
6513 static void
6514ex_print(eap)
6515 exarg_T *eap;
6516{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006517 if (curbuf->b_ml.ml_flags & ML_EMPTY)
6518 EMSG(_(e_emptybuf));
6519 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006520 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00006521 for ( ;!got_int; ui_breakcheck())
6522 {
6523 print_line(eap->line1,
6524 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
6525 || (eap->flags & EXFLAG_NR)),
6526 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
6527 if (++eap->line1 > eap->line2)
6528 break;
6529 out_flush(); /* show one line at a time */
6530 }
6531 setpcmark();
6532 /* put cursor at last line */
6533 curwin->w_cursor.lnum = eap->line2;
6534 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006535 }
6536
Bram Moolenaar071d4272004-06-13 20:20:40 +00006537 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006538}
6539
6540#ifdef FEAT_BYTEOFF
6541 static void
6542ex_goto(eap)
6543 exarg_T *eap;
6544{
6545 goto_byte(eap->line2);
6546}
6547#endif
6548
6549/*
6550 * ":shell".
6551 */
6552/*ARGSUSED*/
6553 static void
6554ex_shell(eap)
6555 exarg_T *eap;
6556{
6557 do_shell(NULL, 0);
6558}
6559
6560#if (defined(FEAT_WINDOWS) && defined(HAVE_DROP_FILE)) \
6561 || (defined(FEAT_GUI_GTK) && defined(FEAT_DND)) \
Bram Moolenaar371baa92005-12-29 22:43:53 +00006562 || defined(FEAT_GUI_MSWIN) \
6563 || defined(FEAT_GUI_MAC) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00006564 || defined(PROTO)
6565
6566/*
6567 * Handle a file drop. The code is here because a drop is *nearly* like an
6568 * :args command, but not quite (we have a list of exact filenames, so we
6569 * don't want to (a) parse a command line, or (b) expand wildcards. So the
6570 * code is very similar to :args and hence needs access to a lot of the static
6571 * functions in this file.
6572 *
6573 * The list should be allocated using alloc(), as should each item in the
6574 * list. This function takes over responsibility for freeing the list.
6575 *
6576 * XXX The list is made into the arggument list. This is freed using
6577 * FreeWild(), which does a series of vim_free() calls, unless the two defines
6578 * __EMX__ and __ALWAYS_HAS_TRAILING_NUL_POINTER are set. In this case, a
6579 * routine _fnexplodefree() is used. This may cause problems, but as the drop
6580 * file functionality is (currently) not in EMX this is not presently a
6581 * problem.
6582 */
6583 void
6584handle_drop(filec, filev, split)
6585 int filec; /* the number of files dropped */
6586 char_u **filev; /* the list of files dropped */
6587 int split; /* force splitting the window */
6588{
6589 exarg_T ea;
6590 int save_msg_scroll = msg_scroll;
6591
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006592 /* Postpone this while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006593 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006594 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006595
6596 /* Check whether the current buffer is changed. If so, we will need
6597 * to split the current window or data could be lost.
6598 * We don't need to check if the 'hidden' option is set, as in this
6599 * case the buffer won't be lost.
6600 */
6601 if (!P_HID(curbuf) && !split)
6602 {
6603 ++emsg_off;
6604 split = check_changed(curbuf, TRUE, FALSE, FALSE, FALSE);
6605 --emsg_off;
6606 }
6607 if (split)
6608 {
6609# ifdef FEAT_WINDOWS
6610 if (win_split(0, 0) == FAIL)
6611 return;
6612# ifdef FEAT_SCROLLBIND
6613 curwin->w_p_scb = FALSE;
6614# endif
6615
6616 /* When splitting the window, create a new alist. Otherwise the
6617 * existing one is overwritten. */
6618 alist_unlink(curwin->w_alist);
6619 alist_new();
6620# else
6621 return; /* can't split, always fail */
6622# endif
6623 }
6624
6625 /*
6626 * Set up the new argument list.
6627 */
Bram Moolenaar86b68352004-12-27 21:59:20 +00006628 alist_set(ALIST(curwin), filec, filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006629
6630 /*
6631 * Move to the first file.
6632 */
6633 /* Fake up a minimal "next" command for do_argfile() */
6634 vim_memset(&ea, 0, sizeof(ea));
6635 ea.cmd = (char_u *)"next";
6636 do_argfile(&ea, 0);
6637
6638 /* do_ecmd() may set need_start_insertmode, but since we never left Insert
6639 * mode that is not needed here. */
6640 need_start_insertmode = FALSE;
6641
6642 /* Restore msg_scroll, otherwise a following command may cause scrolling
6643 * unexpectedly. The screen will be redrawn by the caller, thus
6644 * msg_scroll being set by displaying a message is irrelevant. */
6645 msg_scroll = save_msg_scroll;
6646}
6647#endif
6648
Bram Moolenaar071d4272004-06-13 20:20:40 +00006649/*
6650 * Clear an argument list: free all file names and reset it to zero entries.
6651 */
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00006652 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00006653alist_clear(al)
6654 alist_T *al;
6655{
6656 while (--al->al_ga.ga_len >= 0)
6657 vim_free(AARGLIST(al)[al->al_ga.ga_len].ae_fname);
6658 ga_clear(&al->al_ga);
6659}
6660
6661/*
6662 * Init an argument list.
6663 */
6664 void
6665alist_init(al)
6666 alist_T *al;
6667{
6668 ga_init2(&al->al_ga, (int)sizeof(aentry_T), 5);
6669}
6670
6671#if defined(FEAT_WINDOWS) || defined(PROTO)
6672
6673/*
6674 * Remove a reference from an argument list.
6675 * Ignored when the argument list is the global one.
6676 * If the argument list is no longer used by any window, free it.
6677 */
6678 void
6679alist_unlink(al)
6680 alist_T *al;
6681{
6682 if (al != &global_alist && --al->al_refcount <= 0)
6683 {
6684 alist_clear(al);
6685 vim_free(al);
6686 }
6687}
6688
6689# if defined(FEAT_LISTCMDS) || defined(HAVE_DROP_FILE) || defined(PROTO)
6690/*
6691 * Create a new argument list and use it for the current window.
6692 */
6693 void
6694alist_new()
6695{
6696 curwin->w_alist = (alist_T *)alloc((unsigned)sizeof(alist_T));
6697 if (curwin->w_alist == NULL)
6698 {
6699 curwin->w_alist = &global_alist;
6700 ++global_alist.al_refcount;
6701 }
6702 else
6703 {
6704 curwin->w_alist->al_refcount = 1;
6705 alist_init(curwin->w_alist);
6706 }
6707}
6708# endif
6709#endif
6710
6711#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE) || defined(PROTO)
6712/*
6713 * Expand the file names in the global argument list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00006714 * If "fnum_list" is not NULL, use "fnum_list[fnum_len]" as a list of buffer
6715 * numbers to be re-used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006716 */
6717 void
Bram Moolenaar86b68352004-12-27 21:59:20 +00006718alist_expand(fnum_list, fnum_len)
6719 int *fnum_list;
6720 int fnum_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006721{
6722 char_u **old_arg_files;
Bram Moolenaar86b68352004-12-27 21:59:20 +00006723 int old_arg_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006724 char_u **new_arg_files;
6725 int new_arg_file_count;
6726 char_u *save_p_su = p_su;
6727 int i;
6728
6729 /* Don't use 'suffixes' here. This should work like the shell did the
6730 * expansion. Also, the vimrc file isn't read yet, thus the user
6731 * can't set the options. */
6732 p_su = empty_option;
6733 old_arg_files = (char_u **)alloc((unsigned)(sizeof(char_u *) * GARGCOUNT));
6734 if (old_arg_files != NULL)
6735 {
6736 for (i = 0; i < GARGCOUNT; ++i)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006737 old_arg_files[i] = vim_strsave(GARGLIST[i].ae_fname);
6738 old_arg_count = GARGCOUNT;
6739 if (expand_wildcards(old_arg_count, old_arg_files,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006740 &new_arg_file_count, &new_arg_files,
6741 EW_FILE|EW_NOTFOUND|EW_ADDSLASH) == OK
6742 && new_arg_file_count > 0)
6743 {
Bram Moolenaar86b68352004-12-27 21:59:20 +00006744 alist_set(&global_alist, new_arg_file_count, new_arg_files,
6745 TRUE, fnum_list, fnum_len);
6746 FreeWild(old_arg_count, old_arg_files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006747 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006748 }
6749 p_su = save_p_su;
6750}
6751#endif
6752
6753/*
6754 * Set the argument list for the current window.
6755 * Takes over the allocated files[] and the allocated fnames in it.
6756 */
6757 void
Bram Moolenaar86b68352004-12-27 21:59:20 +00006758alist_set(al, count, files, use_curbuf, fnum_list, fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006759 alist_T *al;
6760 int count;
6761 char_u **files;
6762 int use_curbuf;
Bram Moolenaar86b68352004-12-27 21:59:20 +00006763 int *fnum_list;
6764 int fnum_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006765{
6766 int i;
6767
6768 alist_clear(al);
6769 if (ga_grow(&al->al_ga, count) == OK)
6770 {
6771 for (i = 0; i < count; ++i)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00006772 {
6773 if (got_int)
6774 {
6775 /* When adding many buffers this can take a long time. Allow
6776 * interrupting here. */
6777 while (i < count)
6778 vim_free(files[i++]);
6779 break;
6780 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00006781
6782 /* May set buffer name of a buffer previously used for the
6783 * argument list, so that it's re-used by alist_add. */
6784 if (fnum_list != NULL && i < fnum_len)
6785 buf_set_name(fnum_list[i], files[i]);
6786
Bram Moolenaar071d4272004-06-13 20:20:40 +00006787 alist_add(al, files[i], use_curbuf ? 2 : 1);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00006788 ui_breakcheck();
6789 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006790 vim_free(files);
6791 }
6792 else
6793 FreeWild(count, files);
6794#ifdef FEAT_WINDOWS
6795 if (al == &global_alist)
6796#endif
6797 arg_had_last = FALSE;
6798}
6799
6800/*
6801 * Add file "fname" to argument list "al".
6802 * "fname" must have been allocated and "al" must have been checked for room.
6803 */
6804 void
6805alist_add(al, fname, set_fnum)
6806 alist_T *al;
6807 char_u *fname;
6808 int set_fnum; /* 1: set buffer number; 2: re-use curbuf */
6809{
6810 if (fname == NULL) /* don't add NULL file names */
6811 return;
6812#ifdef BACKSLASH_IN_FILENAME
6813 slash_adjust(fname);
6814#endif
6815 AARGLIST(al)[al->al_ga.ga_len].ae_fname = fname;
6816 if (set_fnum > 0)
6817 AARGLIST(al)[al->al_ga.ga_len].ae_fnum =
6818 buflist_add(fname, BLN_LISTED | (set_fnum == 2 ? BLN_CURBUF : 0));
6819 ++al->al_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006820}
6821
6822#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
6823/*
6824 * Adjust slashes in file names. Called after 'shellslash' was set.
6825 */
6826 void
6827alist_slash_adjust()
6828{
6829 int i;
6830# ifdef FEAT_WINDOWS
6831 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006832 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006833# endif
6834
6835 for (i = 0; i < GARGCOUNT; ++i)
6836 if (GARGLIST[i].ae_fname != NULL)
6837 slash_adjust(GARGLIST[i].ae_fname);
6838# ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00006839 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006840 if (wp->w_alist != &global_alist)
6841 for (i = 0; i < WARGCOUNT(wp); ++i)
6842 if (WARGLIST(wp)[i].ae_fname != NULL)
6843 slash_adjust(WARGLIST(wp)[i].ae_fname);
6844# endif
6845}
6846#endif
6847
6848/*
6849 * ":preserve".
6850 */
6851/*ARGSUSED*/
6852 static void
6853ex_preserve(eap)
6854 exarg_T *eap;
6855{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00006856 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006857 ml_preserve(curbuf, TRUE);
6858}
6859
6860/*
6861 * ":recover".
6862 */
6863 static void
6864ex_recover(eap)
6865 exarg_T *eap;
6866{
6867 /* Set recoverymode right away to avoid the ATTENTION prompt. */
6868 recoverymode = TRUE;
6869 if (!check_changed(curbuf, p_awa, TRUE, eap->forceit, FALSE)
6870 && (*eap->arg == NUL
6871 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
6872 ml_recover();
6873 recoverymode = FALSE;
6874}
6875
6876/*
6877 * Command modifier used in a wrong way.
6878 */
6879 static void
6880ex_wrongmodifier(eap)
6881 exarg_T *eap;
6882{
6883 eap->errmsg = e_invcmd;
6884}
6885
6886#ifdef FEAT_WINDOWS
6887/*
6888 * :sview [+command] file split window with new file, read-only
6889 * :split [[+command] file] split window with current or new file
6890 * :vsplit [[+command] file] split window vertically with current or new file
6891 * :new [[+command] file] split window with no or new file
6892 * :vnew [[+command] file] split vertically window with no or new file
6893 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006894 *
6895 * :tabedit open new Tab page with empty window
6896 * :tabedit [+command] file open new Tab page and edit "file"
6897 * :tabnew [[+command] file] just like :tabedit
6898 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006899 */
6900 void
6901ex_splitview(eap)
6902 exarg_T *eap;
6903{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006904 win_T *old_curwin = curwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006905# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006906 char_u *fname = NULL;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006907# endif
6908# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006909 int browse_flag = cmdmod.browse;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006910# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006911
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006912# ifndef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006913 if (eap->cmdidx == CMD_vsplit || eap->cmdidx == CMD_vnew)
6914 {
6915 ex_ni(eap);
6916 return;
6917 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006918# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006919
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006920# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006921 need_mouse_correct = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006922# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006923
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006924# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00006925 /* A ":split" in the quickfix window works like ":new". Don't want two
6926 * quickfix windows. */
6927 if (bt_quickfix(curbuf))
6928 {
6929 if (eap->cmdidx == CMD_split)
6930 eap->cmdidx = CMD_new;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006931# ifdef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006932 if (eap->cmdidx == CMD_vsplit)
6933 eap->cmdidx = CMD_vnew;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006934# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006935 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006936# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006937
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006938# ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006939 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006940 {
6941 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
6942 FNAME_MESS, TRUE, curbuf->b_ffname);
6943 if (fname == NULL)
6944 goto theend;
6945 eap->arg = fname;
6946 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006947# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006948 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006949# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006950# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006951# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006952 if (cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006953# ifdef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006954 && eap->cmdidx != CMD_vnew
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006955# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006956 && eap->cmdidx != CMD_new)
6957 {
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006958 if (
6959# ifdef FEAT_GUI
6960 !gui.in_use &&
6961# endif
6962 au_has_group((char_u *)"FileExplorer"))
6963 {
6964 /* No browsing supported but we do have the file explorer:
6965 * Edit the directory. */
6966 if (*eap->arg == NUL || !mch_isdir(eap->arg))
6967 eap->arg = (char_u *)".";
6968 }
6969 else
6970 {
6971 fname = do_browse(0, (char_u *)_("Edit File in new window"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00006972 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006973 if (fname == NULL)
6974 goto theend;
6975 eap->arg = fname;
6976 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006977 }
6978 cmdmod.browse = FALSE; /* Don't browse again in do_ecmd(). */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006979# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006980
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006981 /*
6982 * Either open new tab page or split the window.
6983 */
6984 if (eap->cmdidx == CMD_tabedit
6985 || eap->cmdidx == CMD_tabfind
6986 || eap->cmdidx == CMD_tabnew)
6987 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006988 if (win_new_tabpage(cmdmod.tab) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006989 {
6990 do_exedit(eap, NULL);
6991
6992 /* set the alternate buffer for the window we came from */
6993 if (curwin != old_curwin
6994 && win_valid(old_curwin)
6995 && old_curwin->w_buffer != curbuf
6996 && !cmdmod.keepalt)
6997 old_curwin->w_alt_fnum = curbuf->b_fnum;
6998 }
6999 }
7000 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007001 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
7002 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007003# ifdef FEAT_SCROLLBIND
Bram Moolenaar071d4272004-06-13 20:20:40 +00007004 /* Reset 'scrollbind' when editing another file, but keep it when
7005 * doing ":split" without arguments. */
7006 if (*eap->arg != NUL
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007007# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007008 || cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007009# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007010 )
7011 curwin->w_p_scb = FALSE;
7012 else
7013 do_check_scrollbind(FALSE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007014# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007015 do_exedit(eap, old_curwin);
7016 }
7017
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007018# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007019 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007020# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007021
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007022# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007023theend:
7024 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007025# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007026}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007027
7028/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007029 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007030 */
7031 void
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007032tabpage_new()
7033{
7034 exarg_T ea;
7035
7036 vim_memset(&ea, 0, sizeof(ea));
7037 ea.cmdidx = CMD_tabnew;
7038 ea.cmd = (char_u *)"tabn";
7039 ea.arg = (char_u *)"";
7040 ex_splitview(&ea);
7041}
7042
7043/*
7044 * :tabnext command
7045 */
7046 static void
7047ex_tabnext(eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007048 exarg_T *eap;
7049{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007050 switch (eap->cmdidx)
7051 {
7052 case CMD_tabfirst:
7053 case CMD_tabrewind:
7054 goto_tabpage(1);
7055 break;
7056 case CMD_tablast:
7057 goto_tabpage(9999);
7058 break;
7059 case CMD_tabprevious:
7060 case CMD_tabNext:
7061 goto_tabpage(eap->addr_count == 0 ? -1 : -(int)eap->line2);
7062 break;
7063 default: /* CMD_tabnext */
7064 goto_tabpage(eap->addr_count == 0 ? 0 : (int)eap->line2);
7065 break;
7066 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007067}
7068
7069/*
7070 * :tabmove command
7071 */
7072 static void
7073ex_tabmove(eap)
7074 exarg_T *eap;
7075{
7076 tabpage_move(eap->addr_count == 0 ? 9999 : (int)eap->line2);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007077}
7078
7079/*
7080 * :tabs command: List tabs and their contents.
7081 */
7082/*ARGSUSED*/
7083 static void
7084ex_tabs(eap)
7085 exarg_T *eap;
7086{
7087 tabpage_T *tp;
7088 win_T *wp;
7089 int tabcount = 1;
7090
7091 msg_start();
7092 msg_scroll = TRUE;
7093 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
7094 {
7095 msg_putchar('\n');
7096 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
7097 msg_outtrans_attr(IObuff, hl_attr(HLF_T));
7098 out_flush(); /* output one line at a time */
7099 ui_breakcheck();
7100
Bram Moolenaar030f0df2006-02-21 22:02:53 +00007101 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007102 wp = firstwin;
7103 else
7104 wp = tp->tp_firstwin;
7105 for ( ; wp != NULL && !got_int; wp = wp->w_next)
7106 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007107 msg_putchar('\n');
7108 msg_putchar(wp == curwin ? '>' : ' ');
7109 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007110 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
7111 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007112 if (buf_spname(wp->w_buffer) != NULL)
7113 STRCPY(IObuff, buf_spname(wp->w_buffer));
7114 else
7115 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
7116 IObuff, IOSIZE, TRUE);
7117 msg_outtrans(IObuff);
7118 out_flush(); /* output one line at a time */
7119 ui_breakcheck();
7120 }
7121 }
7122}
7123
7124#endif /* FEAT_WINDOWS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007125
7126/*
7127 * ":mode": Set screen mode.
7128 * If no argument given, just get the screen size and redraw.
7129 */
7130 static void
7131ex_mode(eap)
7132 exarg_T *eap;
7133{
7134 if (*eap->arg == NUL)
7135 shell_resized();
7136 else
7137 mch_screenmode(eap->arg);
7138}
7139
7140#ifdef FEAT_WINDOWS
7141/*
7142 * ":resize".
7143 * set, increment or decrement current window height
7144 */
7145 static void
7146ex_resize(eap)
7147 exarg_T *eap;
7148{
7149 int n;
7150 win_T *wp = curwin;
7151
7152 if (eap->addr_count > 0)
7153 {
7154 n = eap->line2;
7155 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
7156 ;
7157 }
7158
7159#ifdef FEAT_GUI
7160 need_mouse_correct = TRUE;
7161#endif
7162 n = atol((char *)eap->arg);
7163#ifdef FEAT_VERTSPLIT
7164 if (cmdmod.split & WSP_VERT)
7165 {
7166 if (*eap->arg == '-' || *eap->arg == '+')
7167 n += W_WIDTH(curwin);
7168 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
7169 n = 9999;
7170 win_setwidth_win((int)n, wp);
7171 }
7172 else
7173#endif
7174 {
7175 if (*eap->arg == '-' || *eap->arg == '+')
7176 n += curwin->w_height;
7177 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
7178 n = 9999;
7179 win_setheight_win((int)n, wp);
7180 }
7181}
7182#endif
7183
7184/*
7185 * ":find [+command] <file>" command.
7186 */
7187 static void
7188ex_find(eap)
7189 exarg_T *eap;
7190{
7191#ifdef FEAT_SEARCHPATH
7192 char_u *fname;
7193 int count;
7194
7195 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
7196 TRUE, curbuf->b_ffname);
7197 if (eap->addr_count > 0)
7198 {
7199 /* Repeat finding the file "count" times. This matters when it
7200 * appears several times in the path. */
7201 count = eap->line2;
7202 while (fname != NULL && --count > 0)
7203 {
7204 vim_free(fname);
7205 fname = find_file_in_path(NULL, 0, FNAME_MESS,
7206 FALSE, curbuf->b_ffname);
7207 }
7208 }
7209
7210 if (fname != NULL)
7211 {
7212 eap->arg = fname;
7213#endif
7214 do_exedit(eap, NULL);
7215#ifdef FEAT_SEARCHPATH
7216 vim_free(fname);
7217 }
7218#endif
7219}
7220
7221/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00007222 * ":open" simulation: for now just work like ":visual".
7223 */
7224 static void
7225ex_open(eap)
7226 exarg_T *eap;
7227{
7228 regmatch_T regmatch;
7229 char_u *p;
7230
7231 curwin->w_cursor.lnum = eap->line2;
7232 beginline(BL_SOL | BL_FIX);
7233 if (*eap->arg == '/')
7234 {
7235 /* ":open /pattern/": put cursor in column found with pattern */
7236 ++eap->arg;
7237 p = skip_regexp(eap->arg, '/', p_magic, NULL);
7238 *p = NUL;
7239 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
7240 if (regmatch.regprog != NULL)
7241 {
7242 regmatch.rm_ic = p_ic;
7243 p = ml_get_curline();
7244 if (vim_regexec(&regmatch, p, (colnr_T)0))
7245 curwin->w_cursor.col = regmatch.startp[0] - p;
7246 else
7247 EMSG(_(e_nomatch));
7248 vim_free(regmatch.regprog);
7249 }
7250 /* Move to the NUL, ignore any other arguments. */
7251 eap->arg += STRLEN(eap->arg);
7252 }
7253 check_cursor();
7254
7255 eap->cmdidx = CMD_visual;
7256 do_exedit(eap, NULL);
7257}
7258
7259/*
7260 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007261 */
7262 static void
7263ex_edit(eap)
7264 exarg_T *eap;
7265{
7266 do_exedit(eap, NULL);
7267}
7268
7269/*
7270 * ":edit <file>" command and alikes.
7271 */
7272/*ARGSUSED*/
7273 void
7274do_exedit(eap, old_curwin)
7275 exarg_T *eap;
7276 win_T *old_curwin; /* curwin before doing a split or NULL */
7277{
7278 int n;
7279#ifdef FEAT_WINDOWS
7280 int need_hide;
7281#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00007282 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007283
7284 /*
7285 * ":vi" command ends Ex mode.
7286 */
7287 if (exmode_active && (eap->cmdidx == CMD_visual
7288 || eap->cmdidx == CMD_view))
7289 {
7290 exmode_active = FALSE;
7291 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007292 {
7293 /* Special case: ":global/pat/visual\NLvi-commands" */
7294 if (global_busy)
7295 {
7296 int rd = RedrawingDisabled;
7297 int nwr = no_wait_return;
7298 int ms = msg_scroll;
7299#ifdef FEAT_GUI
7300 int he = hold_gui_events;
7301#endif
7302
7303 if (eap->nextcmd != NULL)
7304 {
7305 stuffReadbuff(eap->nextcmd);
7306 eap->nextcmd = NULL;
7307 }
7308
7309 if (exmode_was != EXMODE_VIM)
7310 settmode(TMODE_RAW);
7311 RedrawingDisabled = 0;
7312 no_wait_return = 0;
7313 need_wait_return = FALSE;
7314 msg_scroll = 0;
7315#ifdef FEAT_GUI
7316 hold_gui_events = 0;
7317#endif
7318 must_redraw = CLEAR;
7319
7320 main_loop(FALSE, TRUE);
7321
7322 RedrawingDisabled = rd;
7323 no_wait_return = nwr;
7324 msg_scroll = ms;
7325#ifdef FEAT_GUI
7326 hold_gui_events = he;
7327#endif
7328 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007329 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007330 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007331 }
7332
7333 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007334 || eap->cmdidx == CMD_tabnew
7335 || eap->cmdidx == CMD_tabedit
Bram Moolenaar071d4272004-06-13 20:20:40 +00007336#ifdef FEAT_VERTSPLIT
7337 || eap->cmdidx == CMD_vnew
7338#endif
7339 ) && *eap->arg == NUL)
7340 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007341 /* ":new" or ":tabnew" without argument: edit an new empty buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007342 setpcmark();
7343 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
7344 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0));
7345 }
7346 else if ((eap->cmdidx != CMD_split
7347#ifdef FEAT_VERTSPLIT
7348 && eap->cmdidx != CMD_vsplit
7349#endif
7350 )
7351 || *eap->arg != NUL
7352#ifdef FEAT_BROWSE
7353 || cmdmod.browse
7354#endif
7355 )
7356 {
7357 n = readonlymode;
7358 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
7359 readonlymode = TRUE;
7360 else if (eap->cmdidx == CMD_enew)
7361 readonlymode = FALSE; /* 'readonly' doesn't make sense in an
7362 empty buffer */
7363 setpcmark();
7364 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
7365 NULL, eap,
7366 /* ":edit" goes to first line if Vi compatible */
7367 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
7368 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
7369 ? ECMD_ONE : eap->do_ecmd_lnum,
7370 (P_HID(curbuf) ? ECMD_HIDE : 0)
7371 + (eap->forceit ? ECMD_FORCEIT : 0)
7372#ifdef FEAT_LISTCMDS
7373 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
7374#endif
7375 ) == FAIL)
7376 {
7377 /* Editing the file failed. If the window was split, close it. */
7378#ifdef FEAT_WINDOWS
7379 if (old_curwin != NULL)
7380 {
7381 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
7382 if (!need_hide || P_HID(curbuf))
7383 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007384# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7385 cleanup_T cs;
7386
7387 /* Reset the error/interrupt/exception state here so that
7388 * aborting() returns FALSE when closing a window. */
7389 enter_cleanup(&cs);
7390# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007391# ifdef FEAT_GUI
7392 need_mouse_correct = TRUE;
7393# endif
7394 win_close(curwin, !need_hide && !P_HID(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007395
7396# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7397 /* Restore the error/interrupt/exception state if not
7398 * discarded by a new aborting error, interrupt, or
7399 * uncaught exception. */
7400 leave_cleanup(&cs);
7401# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007402 }
7403 }
7404#endif
7405 }
7406 else if (readonlymode && curbuf->b_nwindows == 1)
7407 {
7408 /* When editing an already visited buffer, 'readonly' won't be set
7409 * but the previous value is kept. With ":view" and ":sview" we
7410 * want the file to be readonly, except when another window is
7411 * editing the same buffer. */
7412 curbuf->b_p_ro = TRUE;
7413 }
7414 readonlymode = n;
7415 }
7416 else
7417 {
7418 if (eap->do_ecmd_cmd != NULL)
7419 do_cmdline_cmd(eap->do_ecmd_cmd);
7420#ifdef FEAT_TITLE
7421 n = curwin->w_arg_idx_invalid;
7422#endif
7423 check_arg_idx(curwin);
7424#ifdef FEAT_TITLE
7425 if (n != curwin->w_arg_idx_invalid)
7426 maketitle();
7427#endif
7428 }
7429
7430#ifdef FEAT_WINDOWS
7431 /*
7432 * if ":split file" worked, set alternate file name in old window to new
7433 * file
7434 */
7435 if (old_curwin != NULL
7436 && *eap->arg != NUL
7437 && curwin != old_curwin
7438 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00007439 && old_curwin->w_buffer != curbuf
7440 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007441 old_curwin->w_alt_fnum = curbuf->b_fnum;
7442#endif
7443
7444 ex_no_reprint = TRUE;
7445}
7446
7447#ifndef FEAT_GUI
7448/*
7449 * ":gui" and ":gvim" when there is no GUI.
7450 */
7451 static void
7452ex_nogui(eap)
7453 exarg_T *eap;
7454{
7455 eap->errmsg = e_nogvim;
7456}
7457#endif
7458
7459#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
7460 static void
7461ex_tearoff(eap)
7462 exarg_T *eap;
7463{
7464 gui_make_tearoff(eap->arg);
7465}
7466#endif
7467
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007468#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007469 static void
7470ex_popup(eap)
7471 exarg_T *eap;
7472{
Bram Moolenaar97409f12005-07-08 22:17:29 +00007473 gui_make_popup(eap->arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007474}
7475#endif
7476
7477/*ARGSUSED*/
7478 static void
7479ex_swapname(eap)
7480 exarg_T *eap;
7481{
7482 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
7483 MSG(_("No swap file"));
7484 else
7485 msg(curbuf->b_ml.ml_mfp->mf_fname);
7486}
7487
7488/*
7489 * ":syncbind" forces all 'scrollbind' windows to have the same relative
7490 * offset.
7491 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
7492 */
7493/*ARGSUSED*/
7494 static void
7495ex_syncbind(eap)
7496 exarg_T *eap;
7497{
7498#ifdef FEAT_SCROLLBIND
7499 win_T *wp;
7500 long topline;
7501 long y;
7502 linenr_T old_linenr = curwin->w_cursor.lnum;
7503
7504 setpcmark();
7505
7506 /*
7507 * determine max topline
7508 */
7509 if (curwin->w_p_scb)
7510 {
7511 topline = curwin->w_topline;
7512 for (wp = firstwin; wp; wp = wp->w_next)
7513 {
7514 if (wp->w_p_scb && wp->w_buffer)
7515 {
7516 y = wp->w_buffer->b_ml.ml_line_count - p_so;
7517 if (topline > y)
7518 topline = y;
7519 }
7520 }
7521 if (topline < 1)
7522 topline = 1;
7523 }
7524 else
7525 {
7526 topline = 1;
7527 }
7528
7529
7530 /*
7531 * set all scrollbind windows to the same topline
7532 */
7533 wp = curwin;
7534 for (curwin = firstwin; curwin; curwin = curwin->w_next)
7535 {
7536 if (curwin->w_p_scb)
7537 {
7538 y = topline - curwin->w_topline;
7539 if (y > 0)
7540 scrollup(y, TRUE);
7541 else
7542 scrolldown(-y, TRUE);
7543 curwin->w_scbind_pos = topline;
7544 redraw_later(VALID);
7545 cursor_correct();
7546#ifdef FEAT_WINDOWS
7547 curwin->w_redr_status = TRUE;
7548#endif
7549 }
7550 }
7551 curwin = wp;
7552 if (curwin->w_p_scb)
7553 {
7554 did_syncbind = TRUE;
7555 checkpcmark();
7556 if (old_linenr != curwin->w_cursor.lnum)
7557 {
7558 char_u ctrl_o[2];
7559
7560 ctrl_o[0] = Ctrl_O;
7561 ctrl_o[1] = 0;
7562 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
7563 }
7564 }
7565#endif
7566}
7567
7568
7569 static void
7570ex_read(eap)
7571 exarg_T *eap;
7572{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007573 int i;
7574 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
7575 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007576
7577 if (eap->usefilter) /* :r!cmd */
7578 do_bang(1, eap, FALSE, FALSE, TRUE);
7579 else
7580 {
7581 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
7582 return;
7583
7584#ifdef FEAT_BROWSE
7585 if (cmdmod.browse)
7586 {
7587 char_u *browseFile;
7588
Bram Moolenaar7171abe2004-10-11 10:06:20 +00007589 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007590 NULL, NULL, NULL, curbuf);
7591 if (browseFile != NULL)
7592 {
7593 i = readfile(browseFile, NULL,
7594 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7595 vim_free(browseFile);
7596 }
7597 else
7598 i = OK;
7599 }
7600 else
7601#endif
7602 if (*eap->arg == NUL)
7603 {
7604 if (check_fname() == FAIL) /* check for no file name */
7605 return;
7606 i = readfile(curbuf->b_ffname, curbuf->b_fname,
7607 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7608 }
7609 else
7610 {
7611 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
7612 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
7613 i = readfile(eap->arg, NULL,
7614 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7615
7616 }
7617 if (i == FAIL)
7618 {
7619#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7620 if (!aborting())
7621#endif
7622 EMSG2(_(e_notopen), eap->arg);
7623 }
7624 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00007625 {
7626 if (empty && exmode_active)
7627 {
7628 /* Delete the empty line that remains. Historically ex does
7629 * this but vi doesn't. */
7630 if (eap->line2 == 0)
7631 lnum = curbuf->b_ml.ml_line_count;
7632 else
7633 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007634 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007635 {
7636 ml_delete(lnum, FALSE);
7637 deleted_lines_mark(lnum, 1L);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007638 if (curwin->w_cursor.lnum > 1
7639 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007640 --curwin->w_cursor.lnum;
7641 }
7642 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007643 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007644 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007645 }
7646}
7647
Bram Moolenaarea408852005-06-25 22:49:46 +00007648static char_u *prev_dir = NULL;
7649
7650#if defined(EXITFREE) || defined(PROTO)
7651 void
7652free_cd_dir()
7653{
7654 vim_free(prev_dir);
7655}
7656#endif
7657
7658
Bram Moolenaar071d4272004-06-13 20:20:40 +00007659/*
7660 * ":cd", ":lcd", ":chdir" and ":lchdir".
7661 */
7662 static void
7663ex_cd(eap)
7664 exarg_T *eap;
7665{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007666 char_u *new_dir;
7667 char_u *tofree;
7668
7669 new_dir = eap->arg;
7670#if !defined(UNIX) && !defined(VMS)
7671 /* for non-UNIX ":cd" means: print current directory */
7672 if (*new_dir == NUL)
7673 ex_pwd(NULL);
7674 else
7675#endif
7676 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007677 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged()
7678 && !eap->forceit)
7679 {
7680 EMSG(_("E747: Cannot change directory, buffer is modifed (add ! to override)"));
7681 return;
7682 }
7683
Bram Moolenaar071d4272004-06-13 20:20:40 +00007684 /* ":cd -": Change to previous directory */
7685 if (STRCMP(new_dir, "-") == 0)
7686 {
7687 if (prev_dir == NULL)
7688 {
7689 EMSG(_("E186: No previous directory"));
7690 return;
7691 }
7692 new_dir = prev_dir;
7693 }
7694
7695 /* Save current directory for next ":cd -" */
7696 tofree = prev_dir;
7697 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7698 prev_dir = vim_strsave(NameBuff);
7699 else
7700 prev_dir = NULL;
7701
7702#if defined(UNIX) || defined(VMS)
7703 /* for UNIX ":cd" means: go to home directory */
7704 if (*new_dir == NUL)
7705 {
7706 /* use NameBuff for home directory name */
7707# ifdef VMS
7708 char_u *p;
7709
7710 p = mch_getenv((char_u *)"SYS$LOGIN");
7711 if (p == NULL || *p == NUL) /* empty is the same as not set */
7712 NameBuff[0] = NUL;
7713 else
Bram Moolenaarb6356332005-07-18 21:40:44 +00007714 vim_strncpy(NameBuff, p, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007715# else
7716 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
7717# endif
7718 new_dir = NameBuff;
7719 }
7720#endif
7721 if (new_dir == NULL || vim_chdir(new_dir))
7722 EMSG(_(e_failed));
7723 else
7724 {
7725 vim_free(curwin->w_localdir);
7726 if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir)
7727 {
7728 /* If still in global directory, need to remember current
7729 * directory as global directory. */
7730 if (globaldir == NULL && prev_dir != NULL)
7731 globaldir = vim_strsave(prev_dir);
7732 /* Remember this local directory for the window. */
7733 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7734 curwin->w_localdir = vim_strsave(NameBuff);
7735 }
7736 else
7737 {
7738 /* We are now in the global directory, no need to remember its
7739 * name. */
7740 vim_free(globaldir);
7741 globaldir = NULL;
7742 curwin->w_localdir = NULL;
7743 }
7744
7745 shorten_fnames(TRUE);
7746
7747 /* Echo the new current directory if the command was typed. */
7748 if (KeyTyped)
7749 ex_pwd(eap);
7750 }
7751 vim_free(tofree);
7752 }
7753}
7754
7755/*
7756 * ":pwd".
7757 */
7758/*ARGSUSED*/
7759 static void
7760ex_pwd(eap)
7761 exarg_T *eap;
7762{
7763 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7764 {
7765#ifdef BACKSLASH_IN_FILENAME
7766 slash_adjust(NameBuff);
7767#endif
7768 msg(NameBuff);
7769 }
7770 else
7771 EMSG(_("E187: Unknown"));
7772}
7773
7774/*
7775 * ":=".
7776 */
7777 static void
7778ex_equal(eap)
7779 exarg_T *eap;
7780{
7781 smsg((char_u *)"%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007782 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007783}
7784
7785 static void
7786ex_sleep(eap)
7787 exarg_T *eap;
7788{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007789 int n;
7790 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007791
7792 if (cursor_valid())
7793 {
7794 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
7795 if (n >= 0)
7796 windgoto((int)n, curwin->w_wcol);
7797 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007798
7799 len = eap->line2;
7800 switch (*eap->arg)
7801 {
7802 case 'm': break;
7803 case NUL: len *= 1000L; break;
7804 default: EMSG2(_(e_invarg2), eap->arg); return;
7805 }
7806 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007807}
7808
7809/*
7810 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
7811 */
7812 void
7813do_sleep(msec)
7814 long msec;
7815{
7816 long done;
7817
7818 cursor_on();
7819 out_flush();
7820 for (done = 0; !got_int && done < msec; done += 1000L)
7821 {
7822 ui_delay(msec - done > 1000L ? 1000L : msec - done, TRUE);
7823 ui_breakcheck();
7824 }
7825}
7826
7827 static void
7828do_exmap(eap, isabbrev)
7829 exarg_T *eap;
7830 int isabbrev;
7831{
7832 int mode;
7833 char_u *cmdp;
7834
7835 cmdp = eap->cmd;
7836 mode = get_map_mode(&cmdp, eap->forceit || isabbrev);
7837
7838 switch (do_map((*cmdp == 'n') ? 2 : (*cmdp == 'u'),
7839 eap->arg, mode, isabbrev))
7840 {
7841 case 1: EMSG(_(e_invarg));
7842 break;
7843 case 2: EMSG(isabbrev ? _(e_noabbr) : _(e_nomap));
7844 break;
7845 }
7846}
7847
7848/*
7849 * ":winsize" command (obsolete).
7850 */
7851 static void
7852ex_winsize(eap)
7853 exarg_T *eap;
7854{
7855 int w, h;
7856 char_u *arg = eap->arg;
7857 char_u *p;
7858
7859 w = getdigits(&arg);
7860 arg = skipwhite(arg);
7861 p = arg;
7862 h = getdigits(&arg);
7863 if (*p != NUL && *arg == NUL)
7864 set_shellsize(w, h, TRUE);
7865 else
7866 EMSG(_("E465: :winsize requires two number arguments"));
7867}
7868
7869#ifdef FEAT_WINDOWS
7870 static void
7871ex_wincmd(eap)
7872 exarg_T *eap;
7873{
7874 int xchar = NUL;
7875 char_u *p;
7876
7877 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
7878 {
7879 /* CTRL-W g and CTRL-W CTRL-G have an extra command character */
7880 if (eap->arg[1] == NUL)
7881 {
7882 EMSG(_(e_invarg));
7883 return;
7884 }
7885 xchar = eap->arg[1];
7886 p = eap->arg + 2;
7887 }
7888 else
7889 p = eap->arg + 1;
7890
7891 eap->nextcmd = check_nextcmd(p);
7892 p = skipwhite(p);
7893 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
7894 EMSG(_(e_invarg));
7895 else
7896 {
7897 /* Pass flags on for ":vertical wincmd ]". */
7898 postponed_split_flags = cmdmod.split;
7899 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
7900 postponed_split_flags = 0;
7901 }
7902}
7903#endif
7904
Bram Moolenaar843ee412004-06-30 16:16:41 +00007905#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007906/*
7907 * ":winpos".
7908 */
7909 static void
7910ex_winpos(eap)
7911 exarg_T *eap;
7912{
7913 int x, y;
7914 char_u *arg = eap->arg;
7915 char_u *p;
7916
7917 if (*arg == NUL)
7918 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00007919# if defined(FEAT_GUI) || defined(MSWIN)
7920# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007921 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00007922# else
7923 if (mch_get_winpos(&x, &y) != FAIL)
7924# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007925 {
7926 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
7927 msg(IObuff);
7928 }
7929 else
7930# endif
7931 EMSG(_("E188: Obtaining window position not implemented for this platform"));
7932 }
7933 else
7934 {
7935 x = getdigits(&arg);
7936 arg = skipwhite(arg);
7937 p = arg;
7938 y = getdigits(&arg);
7939 if (*p == NUL || *arg != NUL)
7940 {
7941 EMSG(_("E466: :winpos requires two number arguments"));
7942 return;
7943 }
7944# ifdef FEAT_GUI
7945 if (gui.in_use)
7946 gui_mch_set_winpos(x, y);
7947 else if (gui.starting)
7948 {
7949 /* Remember the coordinates for when the window is opened. */
7950 gui_win_x = x;
7951 gui_win_y = y;
7952 }
7953# ifdef HAVE_TGETENT
7954 else
7955# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00007956# else
7957# ifdef MSWIN
7958 mch_set_winpos(x, y);
7959# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007960# endif
7961# ifdef HAVE_TGETENT
7962 if (*T_CWP)
7963 term_set_winpos(x, y);
7964# endif
7965 }
7966}
7967#endif
7968
7969/*
7970 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
7971 */
7972 static void
7973ex_operators(eap)
7974 exarg_T *eap;
7975{
7976 oparg_T oa;
7977
7978 clear_oparg(&oa);
7979 oa.regname = eap->regname;
7980 oa.start.lnum = eap->line1;
7981 oa.end.lnum = eap->line2;
7982 oa.line_count = eap->line2 - eap->line1 + 1;
7983 oa.motion_type = MLINE;
7984#ifdef FEAT_VIRTUALEDIT
7985 virtual_op = FALSE;
7986#endif
7987 if (eap->cmdidx != CMD_yank) /* position cursor for undo */
7988 {
7989 setpcmark();
7990 curwin->w_cursor.lnum = eap->line1;
7991 beginline(BL_SOL | BL_FIX);
7992 }
7993
7994 switch (eap->cmdidx)
7995 {
7996 case CMD_delete:
7997 oa.op_type = OP_DELETE;
7998 op_delete(&oa);
7999 break;
8000
8001 case CMD_yank:
8002 oa.op_type = OP_YANK;
8003 (void)op_yank(&oa, FALSE, TRUE);
8004 break;
8005
8006 default: /* CMD_rshift or CMD_lshift */
8007 if ((eap->cmdidx == CMD_rshift)
8008#ifdef FEAT_RIGHTLEFT
8009 ^ curwin->w_p_rl
8010#endif
8011 )
8012 oa.op_type = OP_RSHIFT;
8013 else
8014 oa.op_type = OP_LSHIFT;
8015 op_shift(&oa, FALSE, eap->amount);
8016 break;
8017 }
8018#ifdef FEAT_VIRTUALEDIT
8019 virtual_op = MAYBE;
8020#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00008021 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008022}
8023
8024/*
8025 * ":put".
8026 */
8027 static void
8028ex_put(eap)
8029 exarg_T *eap;
8030{
8031 /* ":0put" works like ":1put!". */
8032 if (eap->line2 == 0)
8033 {
8034 eap->line2 = 1;
8035 eap->forceit = TRUE;
8036 }
8037 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008038 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
8039 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008040}
8041
8042/*
8043 * Handle ":copy" and ":move".
8044 */
8045 static void
8046ex_copymove(eap)
8047 exarg_T *eap;
8048{
8049 long n;
8050
8051 n = get_address(&eap->arg, FALSE, FALSE);
8052 if (eap->arg == NULL) /* error detected */
8053 {
8054 eap->nextcmd = NULL;
8055 return;
8056 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008057 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008058
8059 /*
8060 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
8061 */
8062 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
8063 {
8064 EMSG(_(e_invaddr));
8065 return;
8066 }
8067
8068 if (eap->cmdidx == CMD_move)
8069 {
8070 if (do_move(eap->line1, eap->line2, n) == FAIL)
8071 return;
8072 }
8073 else
8074 ex_copy(eap->line1, eap->line2, n);
8075 u_clearline();
8076 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008077 ex_may_print(eap);
8078}
8079
8080/*
8081 * Print the current line if flags were given to the Ex command.
8082 */
8083 static void
8084ex_may_print(eap)
8085 exarg_T *eap;
8086{
8087 if (eap->flags != 0)
8088 {
8089 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
8090 (eap->flags & EXFLAG_LIST));
8091 ex_no_reprint = TRUE;
8092 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008093}
8094
8095/*
8096 * ":smagic" and ":snomagic".
8097 */
8098 static void
8099ex_submagic(eap)
8100 exarg_T *eap;
8101{
8102 int magic_save = p_magic;
8103
8104 p_magic = (eap->cmdidx == CMD_smagic);
8105 do_sub(eap);
8106 p_magic = magic_save;
8107}
8108
8109/*
8110 * ":join".
8111 */
8112 static void
8113ex_join(eap)
8114 exarg_T *eap;
8115{
8116 curwin->w_cursor.lnum = eap->line1;
8117 if (eap->line1 == eap->line2)
8118 {
8119 if (eap->addr_count >= 2) /* :2,2join does nothing */
8120 return;
8121 if (eap->line2 == curbuf->b_ml.ml_line_count)
8122 {
8123 beep_flush();
8124 return;
8125 }
8126 ++eap->line2;
8127 }
8128 do_do_join(eap->line2 - eap->line1 + 1, !eap->forceit);
8129 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008130 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008131}
8132
8133/*
8134 * ":[addr]@r" or ":[addr]*r": execute register
8135 */
8136 static void
8137ex_at(eap)
8138 exarg_T *eap;
8139{
8140 int c;
8141
8142 curwin->w_cursor.lnum = eap->line2;
8143
8144#ifdef USE_ON_FLY_SCROLL
8145 dont_scroll = TRUE; /* disallow scrolling here */
8146#endif
8147
8148 /* get the register name. No name means to use the previous one */
8149 c = *eap->arg;
8150 if (c == NUL || (c == '*' && *eap->cmd == '*'))
8151 c = '@';
8152 /* put the register in mapbuf */
8153 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL) == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008154 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008155 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00008156 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008157 else
8158 {
8159 int save_efr = exec_from_reg;
8160
8161 exec_from_reg = TRUE;
8162
8163 /*
8164 * Execute from the typeahead buffer.
8165 * Originally this didn't check for the typeahead buffer to be empty,
8166 * thus could read more Ex commands from stdin. It's not clear why,
8167 * it is certainly unexpected.
8168 */
8169 while ((!stuff_empty() || typebuf.tb_len > 0) && vpeekc() == ':')
8170 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
8171
8172 exec_from_reg = save_efr;
8173 }
8174}
8175
8176/*
8177 * ":!".
8178 */
8179 static void
8180ex_bang(eap)
8181 exarg_T *eap;
8182{
8183 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
8184}
8185
8186/*
8187 * ":undo".
8188 */
8189/*ARGSUSED*/
8190 static void
8191ex_undo(eap)
8192 exarg_T *eap;
8193{
8194 u_undo(1);
8195}
8196
8197/*
8198 * ":redo".
8199 */
8200/*ARGSUSED*/
8201 static void
8202ex_redo(eap)
8203 exarg_T *eap;
8204{
8205 u_redo(1);
8206}
8207
8208/*
8209 * ":redir": start/stop redirection.
8210 */
8211 static void
8212ex_redir(eap)
8213 exarg_T *eap;
8214{
8215 char *mode;
8216 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00008217 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008218
8219 if (STRICMP(eap->arg, "END") == 0)
8220 close_redir();
8221 else
8222 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008223 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008224 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008225 ++arg;
8226 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008227 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008228 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008229 mode = "a";
8230 }
8231 else
8232 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00008233 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008234
8235 close_redir();
8236
8237 /* Expand environment variables and "~/". */
Bram Moolenaarca472992005-01-21 11:46:23 +00008238 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008239 if (fname == NULL)
8240 return;
8241#ifdef FEAT_BROWSE
8242 if (cmdmod.browse)
8243 {
8244 char_u *browseFile;
8245
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008246 browseFile = do_browse(BROWSE_SAVE,
8247 (char_u *)_("Save Redirection"),
8248 fname, NULL, NULL, BROWSE_FILTER_ALL_FILES, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008249 if (browseFile == NULL)
8250 return; /* operation cancelled */
8251 vim_free(fname);
8252 fname = browseFile;
8253 eap->forceit = TRUE; /* since dialog already asked */
8254 }
8255#endif
8256
8257 redir_fd = open_exfile(fname, eap->forceit, mode);
8258 vim_free(fname);
8259 }
8260#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00008261 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008262 {
8263 /* redirect to a register a-z (resp. A-Z for appending) */
8264 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00008265 ++arg;
8266 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008267# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00008268 || *arg == '*'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008269# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00008270 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008271 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008272 redir_reg = *arg++;
Bram Moolenaard9d30582005-05-18 22:10:28 +00008273 if (*arg == '>' && arg[1] == '>')
8274 arg += 2;
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00008275 else if ((*arg == NUL || (*arg == '>' && arg[1] == NUL)) &&
8276 (islower(redir_reg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008277# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00008278 || redir_reg == '*'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008279# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00008280 || redir_reg == '"'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008281 {
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00008282 if (*arg == '>')
8283 arg++;
8284
Bram Moolenaar071d4272004-06-13 20:20:40 +00008285 /* make register empty */
8286 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
8287 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008288 }
8289 if (*arg != NUL)
8290 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00008291 redir_reg = 0;
Bram Moolenaard9d30582005-05-18 22:10:28 +00008292 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008293 }
8294 }
8295 else if (*arg == '=' && arg[1] == '>')
8296 {
8297 int append;
8298
8299 /* redirect to a variable */
8300 close_redir();
8301 arg += 2;
8302
8303 if (*arg == '>')
8304 {
8305 ++arg;
8306 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008307 }
8308 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008309 append = FALSE;
8310
8311 if (var_redir_start(skipwhite(arg), append) == OK)
8312 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008313 }
8314#endif
8315
8316 /* TODO: redirect to a buffer */
8317
Bram Moolenaar071d4272004-06-13 20:20:40 +00008318 else
Bram Moolenaarca472992005-01-21 11:46:23 +00008319 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008320 }
8321}
8322
8323/*
8324 * ":redraw": force redraw
8325 */
8326 static void
8327ex_redraw(eap)
8328 exarg_T *eap;
8329{
8330 int r = RedrawingDisabled;
8331 int p = p_lz;
8332
8333 RedrawingDisabled = 0;
8334 p_lz = FALSE;
8335 update_topline();
8336 update_screen(eap->forceit ? CLEAR :
8337#ifdef FEAT_VISUAL
8338 VIsual_active ? INVERTED :
8339#endif
8340 0);
8341#ifdef FEAT_TITLE
8342 if (need_maketitle)
8343 maketitle();
8344#endif
8345 RedrawingDisabled = r;
8346 p_lz = p;
8347
8348 /* Reset msg_didout, so that a message that's there is overwritten. */
8349 msg_didout = FALSE;
8350 msg_col = 0;
8351
8352 out_flush();
8353}
8354
8355/*
8356 * ":redrawstatus": force redraw of status line(s)
8357 */
8358/*ARGSUSED*/
8359 static void
8360ex_redrawstatus(eap)
8361 exarg_T *eap;
8362{
8363#if defined(FEAT_WINDOWS)
8364 int r = RedrawingDisabled;
8365 int p = p_lz;
8366
8367 RedrawingDisabled = 0;
8368 p_lz = FALSE;
8369 if (eap->forceit)
8370 status_redraw_all();
8371 else
8372 status_redraw_curbuf();
8373 update_screen(
8374# ifdef FEAT_VISUAL
8375 VIsual_active ? INVERTED :
8376# endif
8377 0);
8378 RedrawingDisabled = r;
8379 p_lz = p;
8380 out_flush();
8381#endif
8382}
8383
8384 static void
8385close_redir()
8386{
8387 if (redir_fd != NULL)
8388 {
8389 fclose(redir_fd);
8390 redir_fd = NULL;
8391 }
8392#ifdef FEAT_EVAL
8393 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008394 if (redir_vname)
8395 {
8396 var_redir_stop();
8397 redir_vname = 0;
8398 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008399#endif
8400}
8401
8402#if defined(FEAT_SESSION) && defined(USE_CRNL)
8403# define MKSESSION_NL
8404static int mksession_nl = FALSE; /* use NL only in put_eol() */
8405#endif
8406
8407/*
8408 * ":mkexrc", ":mkvimrc", ":mkview" and ":mksession".
8409 */
8410 static void
8411ex_mkrc(eap)
8412 exarg_T *eap;
8413{
8414 FILE *fd;
8415 int failed = FALSE;
8416 char_u *fname;
8417#ifdef FEAT_BROWSE
8418 char_u *browseFile = NULL;
8419#endif
8420#ifdef FEAT_SESSION
8421 int view_session = FALSE;
8422 int using_vdir = FALSE; /* using 'viewdir'? */
8423 char_u *viewFile = NULL;
8424 unsigned *flagp;
8425#endif
8426
8427 if (eap->cmdidx == CMD_mksession || eap->cmdidx == CMD_mkview)
8428 {
8429#ifdef FEAT_SESSION
8430 view_session = TRUE;
8431#else
8432 ex_ni(eap);
8433 return;
8434#endif
8435 }
8436
8437#ifdef FEAT_SESSION
8438 /* ":mkview" or ":mkview 9": generate file name with 'viewdir' */
8439 if (eap->cmdidx == CMD_mkview
8440 && (*eap->arg == NUL
8441 || (vim_isdigit(*eap->arg) && eap->arg[1] == NUL)))
8442 {
8443 eap->forceit = TRUE;
8444 fname = get_view_file(*eap->arg);
8445 if (fname == NULL)
8446 return;
8447 viewFile = fname;
8448 using_vdir = TRUE;
8449 }
8450 else
8451#endif
8452 if (*eap->arg != NUL)
8453 fname = eap->arg;
8454 else if (eap->cmdidx == CMD_mkvimrc)
8455 fname = (char_u *)VIMRC_FILE;
8456#ifdef FEAT_SESSION
8457 else if (eap->cmdidx == CMD_mksession)
8458 fname = (char_u *)SESSION_FILE;
8459#endif
8460 else
8461 fname = (char_u *)EXRC_FILE;
8462
8463#ifdef FEAT_BROWSE
8464 if (cmdmod.browse)
8465 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008466 browseFile = do_browse(BROWSE_SAVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008467# ifdef FEAT_SESSION
8468 eap->cmdidx == CMD_mkview ? (char_u *)_("Save View") :
8469 eap->cmdidx == CMD_mksession ? (char_u *)_("Save Session") :
8470# endif
8471 (char_u *)_("Save Setup"),
8472 fname, (char_u *)"vim", NULL, BROWSE_FILTER_MACROS, NULL);
8473 if (browseFile == NULL)
8474 goto theend;
8475 fname = browseFile;
8476 eap->forceit = TRUE; /* since dialog already asked */
8477 }
8478#endif
8479
8480#if defined(FEAT_SESSION) && defined(vim_mkdir)
8481 /* When using 'viewdir' may have to create the directory. */
8482 if (using_vdir && !mch_isdir(p_vdir))
Bram Moolenaardf177f62005-02-22 08:39:57 +00008483 vim_mkdir_emsg(p_vdir, 0755);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008484#endif
8485
8486 fd = open_exfile(fname, eap->forceit, WRITEBIN);
8487 if (fd != NULL)
8488 {
8489#ifdef FEAT_SESSION
8490 if (eap->cmdidx == CMD_mkview)
8491 flagp = &vop_flags;
8492 else
8493 flagp = &ssop_flags;
8494#endif
8495
8496#ifdef MKSESSION_NL
8497 /* "unix" in 'sessionoptions': use NL line separator */
8498 if (view_session && (*flagp & SSOP_UNIX))
8499 mksession_nl = TRUE;
8500#endif
8501
8502 /* Write the version command for :mkvimrc */
8503 if (eap->cmdidx == CMD_mkvimrc)
8504 (void)put_line(fd, "version 6.0");
8505
8506#ifdef FEAT_SESSION
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008507 if (eap->cmdidx == CMD_mksession)
8508 {
8509 if (put_line(fd, "let SessionLoad = 1") == FAIL)
8510 failed = TRUE;
8511 }
8512
Bram Moolenaar071d4272004-06-13 20:20:40 +00008513 if (eap->cmdidx != CMD_mkview)
8514#endif
8515 {
8516 /* Write setting 'compatible' first, because it has side effects.
8517 * For that same reason only do it when needed. */
8518 if (p_cp)
8519 (void)put_line(fd, "if !&cp | set cp | endif");
8520 else
8521 (void)put_line(fd, "if &cp | set nocp | endif");
8522 }
8523
8524#ifdef FEAT_SESSION
8525 if (!view_session
8526 || (eap->cmdidx == CMD_mksession
8527 && (*flagp & SSOP_OPTIONS)))
8528#endif
8529 failed |= (makemap(fd, NULL) == FAIL
8530 || makeset(fd, OPT_GLOBAL, FALSE) == FAIL);
8531
8532#ifdef FEAT_SESSION
8533 if (!failed && view_session)
8534 {
8535 if (put_line(fd, "let s:so_save = &so | let s:siso_save = &siso | set so=0 siso=0") == FAIL)
8536 failed = TRUE;
8537 if (eap->cmdidx == CMD_mksession)
8538 {
8539 char_u dirnow[MAXPATHL]; /* current directory */
8540
8541 /*
8542 * Change to session file's dir.
8543 */
8544 if (mch_dirname(dirnow, MAXPATHL) == FAIL
8545 || mch_chdir((char *)dirnow) != 0)
8546 *dirnow = NUL;
8547 if (*dirnow != NUL && (ssop_flags & SSOP_SESDIR))
8548 {
8549 if (vim_chdirfile(fname) == OK)
8550 shorten_fnames(TRUE);
8551 }
8552 else if (*dirnow != NUL
8553 && (ssop_flags & SSOP_CURDIR) && globaldir != NULL)
8554 {
8555 (void)mch_chdir((char *)globaldir);
8556 shorten_fnames(TRUE);
8557 }
8558
8559 failed |= (makeopens(fd, dirnow) == FAIL);
8560
8561 /* restore original dir */
8562 if (*dirnow != NUL && ((ssop_flags & SSOP_SESDIR)
8563 || ((ssop_flags & SSOP_CURDIR) && globaldir != NULL)))
8564 {
8565 if (mch_chdir((char *)dirnow) != 0)
8566 EMSG(_(e_prev_dir));
8567 shorten_fnames(TRUE);
8568 }
8569 }
8570 else
8571 {
8572 failed |= (put_view(fd, curwin, !using_vdir, flagp) == FAIL);
8573 }
8574 if (put_line(fd, "let &so = s:so_save | let &siso = s:siso_save")
8575 == FAIL)
8576 failed = TRUE;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008577 if (put_line(fd, "doautoall SessionLoadPost") == FAIL)
8578 failed = TRUE;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008579 if (eap->cmdidx == CMD_mksession)
8580 {
8581 if (put_line(fd, "unlet SessionLoad") == FAIL)
8582 failed = TRUE;
8583 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008584 }
8585#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008586 if (put_line(fd, "\" vim: set ft=vim :") == FAIL)
8587 failed = TRUE;
8588
Bram Moolenaar071d4272004-06-13 20:20:40 +00008589 failed |= fclose(fd);
8590
8591 if (failed)
8592 EMSG(_(e_write));
8593#if defined(FEAT_EVAL) && defined(FEAT_SESSION)
8594 else if (eap->cmdidx == CMD_mksession)
8595 {
8596 /* successful session write - set this_session var */
8597 char_u tbuf[MAXPATHL];
8598
8599 if (vim_FullName(fname, tbuf, MAXPATHL, FALSE) == OK)
8600 set_vim_var_string(VV_THIS_SESSION, tbuf, -1);
8601 }
8602#endif
8603#ifdef MKSESSION_NL
8604 mksession_nl = FALSE;
8605#endif
8606 }
8607
8608#ifdef FEAT_BROWSE
8609theend:
8610 vim_free(browseFile);
8611#endif
8612#ifdef FEAT_SESSION
8613 vim_free(viewFile);
8614#endif
8615}
8616
Bram Moolenaardf177f62005-02-22 08:39:57 +00008617#if ((defined(FEAT_SESSION) || defined(FEAT_EVAL)) && defined(vim_mkdir)) \
8618 || defined(PROTO)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008619/*ARGSUSED*/
Bram Moolenaardf177f62005-02-22 08:39:57 +00008620 int
8621vim_mkdir_emsg(name, prot)
8622 char_u *name;
8623 int prot;
8624{
8625 if (vim_mkdir(name, prot) != 0)
8626 {
8627 EMSG2(_("E739: Cannot create directory: %s"), name);
8628 return FAIL;
8629 }
8630 return OK;
8631}
8632#endif
8633
Bram Moolenaar071d4272004-06-13 20:20:40 +00008634/*
8635 * Open a file for writing for an Ex command, with some checks.
8636 * Return file descriptor, or NULL on failure.
8637 */
8638 FILE *
8639open_exfile(fname, forceit, mode)
8640 char_u *fname;
8641 int forceit;
8642 char *mode; /* "w" for create new file or "a" for append */
8643{
8644 FILE *fd;
8645
8646#ifdef UNIX
8647 /* with Unix it is possible to open a directory */
8648 if (mch_isdir(fname))
8649 {
8650 EMSG2(_(e_isadir2), fname);
8651 return NULL;
8652 }
8653#endif
8654 if (!forceit && *mode != 'a' && vim_fexists(fname))
8655 {
8656 EMSG2(_("E189: \"%s\" exists (add ! to override)"), fname);
8657 return NULL;
8658 }
8659
8660 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
8661 EMSG2(_("E190: Cannot open \"%s\" for writing"), fname);
8662
8663 return fd;
8664}
8665
8666/*
8667 * ":mark" and ":k".
8668 */
8669 static void
8670ex_mark(eap)
8671 exarg_T *eap;
8672{
8673 pos_T pos;
8674
8675 if (*eap->arg == NUL) /* No argument? */
8676 EMSG(_(e_argreq));
8677 else if (eap->arg[1] != NUL) /* more than one character? */
8678 EMSG(_(e_trailing));
8679 else
8680 {
8681 pos = curwin->w_cursor; /* save curwin->w_cursor */
8682 curwin->w_cursor.lnum = eap->line2;
8683 beginline(BL_WHITE | BL_FIX);
8684 if (setmark(*eap->arg) == FAIL) /* set mark */
8685 EMSG(_("E191: Argument must be a letter or forward/backward quote"));
8686 curwin->w_cursor = pos; /* restore curwin->w_cursor */
8687 }
8688}
8689
8690/*
8691 * Update w_topline, w_leftcol and the cursor position.
8692 */
8693 void
8694update_topline_cursor()
8695{
8696 check_cursor(); /* put cursor on valid line */
8697 update_topline();
8698 if (!curwin->w_p_wrap)
8699 validate_cursor();
8700 update_curswant();
8701}
8702
8703#ifdef FEAT_EX_EXTRA
8704/*
8705 * ":normal[!] {commands}": Execute normal mode commands.
8706 */
8707 static void
8708ex_normal(eap)
8709 exarg_T *eap;
8710{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008711 int save_msg_scroll = msg_scroll;
8712 int save_restart_edit = restart_edit;
8713 int save_msg_didout = msg_didout;
8714 int save_State = State;
8715 tasave_T tabuf;
8716 int save_insertmode = p_im;
8717 int save_finish_op = finish_op;
8718#ifdef FEAT_MBYTE
8719 char_u *arg = NULL;
8720 int l;
8721 char_u *p;
8722#endif
8723
8724 if (ex_normal_busy >= p_mmd)
8725 {
8726 EMSG(_("E192: Recursive use of :normal too deep"));
8727 return;
8728 }
8729 ++ex_normal_busy;
8730
8731 msg_scroll = FALSE; /* no msg scrolling in Normal mode */
8732 restart_edit = 0; /* don't go to Insert mode */
8733 p_im = FALSE; /* don't use 'insertmode' */
8734
8735#ifdef FEAT_MBYTE
8736 /*
8737 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
8738 * this for the K_SPECIAL leading byte, otherwise special keys will not
8739 * work.
8740 */
8741 if (has_mbyte)
8742 {
8743 int len = 0;
8744
8745 /* Count the number of characters to be escaped. */
8746 for (p = eap->arg; *p != NUL; ++p)
8747 {
8748# ifdef FEAT_GUI
8749 if (*p == CSI) /* leadbyte CSI */
8750 len += 2;
8751# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008752 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008753 if (*++p == K_SPECIAL /* trailbyte K_SPECIAL or CSI */
8754# ifdef FEAT_GUI
8755 || *p == CSI
8756# endif
8757 )
8758 len += 2;
8759 }
8760 if (len > 0)
8761 {
8762 arg = alloc((unsigned)(STRLEN(eap->arg) + len + 1));
8763 if (arg != NULL)
8764 {
8765 len = 0;
8766 for (p = eap->arg; *p != NUL; ++p)
8767 {
8768 arg[len++] = *p;
8769# ifdef FEAT_GUI
8770 if (*p == CSI)
8771 {
8772 arg[len++] = KS_EXTRA;
8773 arg[len++] = (int)KE_CSI;
8774 }
8775# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008776 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008777 {
8778 arg[len++] = *++p;
8779 if (*p == K_SPECIAL)
8780 {
8781 arg[len++] = KS_SPECIAL;
8782 arg[len++] = KE_FILLER;
8783 }
8784# ifdef FEAT_GUI
8785 else if (*p == CSI)
8786 {
8787 arg[len++] = KS_EXTRA;
8788 arg[len++] = (int)KE_CSI;
8789 }
8790# endif
8791 }
8792 arg[len] = NUL;
8793 }
8794 }
8795 }
8796 }
8797#endif
8798
8799 /*
8800 * Save the current typeahead. This is required to allow using ":normal"
8801 * from an event handler and makes sure we don't hang when the argument
8802 * ends with half a command.
8803 */
8804 save_typeahead(&tabuf);
8805 if (tabuf.typebuf_valid)
8806 {
8807 /*
8808 * Repeat the :normal command for each line in the range. When no
8809 * range given, execute it just once, without positioning the cursor
8810 * first.
8811 */
8812 do
8813 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008814 if (eap->addr_count != 0)
8815 {
8816 curwin->w_cursor.lnum = eap->line1++;
8817 curwin->w_cursor.col = 0;
8818 }
8819
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008820 exec_normal_cmd(
Bram Moolenaar071d4272004-06-13 20:20:40 +00008821#ifdef FEAT_MBYTE
8822 arg != NULL ? arg :
8823#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008824 eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008825 }
8826 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
8827 }
8828
8829 /* Might not return to the main loop when in an event handler. */
8830 update_topline_cursor();
8831
8832 /* Restore the previous typeahead. */
8833 restore_typeahead(&tabuf);
8834
8835 --ex_normal_busy;
8836 msg_scroll = save_msg_scroll;
8837 restart_edit = save_restart_edit;
8838 p_im = save_insertmode;
8839 finish_op = save_finish_op;
8840 msg_didout |= save_msg_didout; /* don't reset msg_didout now */
8841
8842 /* Restore the state (needed when called from a function executed for
8843 * 'indentexpr'). */
8844 State = save_State;
8845#ifdef FEAT_MBYTE
8846 vim_free(arg);
8847#endif
8848}
8849
8850/*
Bram Moolenaar64969662005-12-14 21:59:55 +00008851 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008852 */
8853 static void
8854ex_startinsert(eap)
8855 exarg_T *eap;
8856{
Bram Moolenaarfd371682005-01-14 21:42:54 +00008857 if (eap->forceit)
8858 {
8859 coladvance((colnr_T)MAXCOL);
8860 curwin->w_curswant = MAXCOL;
8861 curwin->w_set_curswant = FALSE;
8862 }
8863
Bram Moolenaara40c5002005-01-09 21:16:21 +00008864 /* Ignore the command when already in Insert mode. Inserting an
8865 * expression register that invokes a function can do this. */
8866 if (State & INSERT)
8867 return;
8868
Bram Moolenaar64969662005-12-14 21:59:55 +00008869 if (eap->cmdidx == CMD_startinsert)
8870 restart_edit = 'a';
8871 else if (eap->cmdidx == CMD_startreplace)
8872 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008873 else
Bram Moolenaar64969662005-12-14 21:59:55 +00008874 restart_edit = 'V';
8875
8876 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008877 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00008878 if (eap->cmdidx == CMD_startinsert)
8879 restart_edit = 'i';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008880 curwin->w_curswant = 0; /* avoid MAXCOL */
8881 }
8882}
8883
8884/*
8885 * ":stopinsert"
8886 */
8887/*ARGSUSED*/
8888 static void
8889ex_stopinsert(eap)
8890 exarg_T *eap;
8891{
8892 restart_edit = 0;
8893 stop_insert_mode = TRUE;
8894}
8895#endif
8896
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008897#if defined(FEAT_EX_EXTRA) || defined(FEAT_MENU) || defined(PROTO)
8898/*
8899 * Execute normal mode command "cmd".
8900 * "remap" can be REMAP_NONE or REMAP_YES.
8901 */
8902 void
8903exec_normal_cmd(cmd, remap, silent)
8904 char_u *cmd;
8905 int remap;
8906 int silent;
8907{
8908 oparg_T oa;
8909
8910 /*
8911 * Stuff the argument into the typeahead buffer.
8912 * Execute normal_cmd() until there is no typeahead left.
8913 */
8914 clear_oparg(&oa);
8915 finish_op = FALSE;
8916 ins_typebuf(cmd, remap, 0, TRUE, silent);
8917 while ((!stuff_empty() || (!typebuf_typed() && typebuf.tb_len > 0))
8918 && !got_int)
8919 {
8920 update_topline_cursor();
8921 normal_cmd(&oa, FALSE); /* execute a Normal mode cmd */
8922 }
8923}
8924#endif
8925
Bram Moolenaar071d4272004-06-13 20:20:40 +00008926#ifdef FEAT_FIND_ID
8927 static void
8928ex_checkpath(eap)
8929 exarg_T *eap;
8930{
8931 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
8932 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
8933 (linenr_T)1, (linenr_T)MAXLNUM);
8934}
8935
8936#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
8937/*
8938 * ":psearch"
8939 */
8940 static void
8941ex_psearch(eap)
8942 exarg_T *eap;
8943{
8944 g_do_tagpreview = p_pvh;
8945 ex_findpat(eap);
8946 g_do_tagpreview = 0;
8947}
8948#endif
8949
8950 static void
8951ex_findpat(eap)
8952 exarg_T *eap;
8953{
8954 int whole = TRUE;
8955 long n;
8956 char_u *p;
8957 int action;
8958
8959 switch (cmdnames[eap->cmdidx].cmd_name[2])
8960 {
8961 case 'e': /* ":psearch", ":isearch" and ":dsearch" */
8962 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
8963 action = ACTION_GOTO;
8964 else
8965 action = ACTION_SHOW;
8966 break;
8967 case 'i': /* ":ilist" and ":dlist" */
8968 action = ACTION_SHOW_ALL;
8969 break;
8970 case 'u': /* ":ijump" and ":djump" */
8971 action = ACTION_GOTO;
8972 break;
8973 default: /* ":isplit" and ":dsplit" */
8974 action = ACTION_SPLIT;
8975 break;
8976 }
8977
8978 n = 1;
8979 if (vim_isdigit(*eap->arg)) /* get count */
8980 {
8981 n = getdigits(&eap->arg);
8982 eap->arg = skipwhite(eap->arg);
8983 }
8984 if (*eap->arg == '/') /* Match regexp, not just whole words */
8985 {
8986 whole = FALSE;
8987 ++eap->arg;
8988 p = skip_regexp(eap->arg, '/', p_magic, NULL);
8989 if (*p)
8990 {
8991 *p++ = NUL;
8992 p = skipwhite(p);
8993
8994 /* Check for trailing illegal characters */
8995 if (!ends_excmd(*p))
8996 eap->errmsg = e_trailing;
8997 else
8998 eap->nextcmd = check_nextcmd(p);
8999 }
9000 }
9001 if (!eap->skip)
9002 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
9003 whole, !eap->forceit,
9004 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
9005 n, action, eap->line1, eap->line2);
9006}
9007#endif
9008
9009#ifdef FEAT_WINDOWS
9010
9011# ifdef FEAT_QUICKFIX
9012/*
9013 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
9014 */
9015 static void
9016ex_ptag(eap)
9017 exarg_T *eap;
9018{
9019 g_do_tagpreview = p_pvh;
9020 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9021}
9022
9023/*
9024 * ":pedit"
9025 */
9026 static void
9027ex_pedit(eap)
9028 exarg_T *eap;
9029{
9030 win_T *curwin_save = curwin;
9031
9032 g_do_tagpreview = p_pvh;
9033 prepare_tagpreview();
9034 keep_help_flag = curwin_save->w_buffer->b_help;
9035 do_exedit(eap, NULL);
9036 keep_help_flag = FALSE;
9037 if (curwin != curwin_save && win_valid(curwin_save))
9038 {
9039 /* Return cursor to where we were */
9040 validate_cursor();
9041 redraw_later(VALID);
9042 win_enter(curwin_save, TRUE);
9043 }
9044 g_do_tagpreview = 0;
9045}
9046# endif
9047
9048/*
9049 * ":stag", ":stselect" and ":stjump".
9050 */
9051 static void
9052ex_stag(eap)
9053 exarg_T *eap;
9054{
9055 postponed_split = -1;
9056 postponed_split_flags = cmdmod.split;
9057 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9058 postponed_split_flags = 0;
9059}
9060#endif
9061
9062/*
9063 * ":tag", ":tselect", ":tjump", ":tnext", etc.
9064 */
9065 static void
9066ex_tag(eap)
9067 exarg_T *eap;
9068{
9069 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
9070}
9071
9072 static void
9073ex_tag_cmd(eap, name)
9074 exarg_T *eap;
9075 char_u *name;
9076{
9077 int cmd;
9078
9079 switch (name[1])
9080 {
9081 case 'j': cmd = DT_JUMP; /* ":tjump" */
9082 break;
9083 case 's': cmd = DT_SELECT; /* ":tselect" */
9084 break;
9085 case 'p': cmd = DT_PREV; /* ":tprevious" */
9086 break;
9087 case 'N': cmd = DT_PREV; /* ":tNext" */
9088 break;
9089 case 'n': cmd = DT_NEXT; /* ":tnext" */
9090 break;
9091 case 'o': cmd = DT_POP; /* ":pop" */
9092 break;
9093 case 'f': /* ":tfirst" */
9094 case 'r': cmd = DT_FIRST; /* ":trewind" */
9095 break;
9096 case 'l': cmd = DT_LAST; /* ":tlast" */
9097 break;
9098 default: /* ":tag" */
9099#ifdef FEAT_CSCOPE
9100 if (p_cst)
9101 {
9102 do_cstag(eap);
9103 return;
9104 }
9105#endif
9106 cmd = DT_TAG;
9107 break;
9108 }
9109
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00009110 if (name[0] == 'l')
9111 {
9112#ifndef FEAT_QUICKFIX
9113 ex_ni(eap);
9114 return;
9115#else
9116 cmd = DT_LTAG;
9117#endif
9118 }
9119
Bram Moolenaar071d4272004-06-13 20:20:40 +00009120 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
9121 eap->forceit, TRUE);
9122}
9123
9124/*
9125 * Evaluate cmdline variables.
9126 *
9127 * change '%' to curbuf->b_ffname
9128 * '#' to curwin->w_altfile
9129 * '<cword>' to word under the cursor
9130 * '<cWORD>' to WORD under the cursor
9131 * '<cfile>' to path name under the cursor
9132 * '<sfile>' to sourced file name
9133 * '<afile>' to file name for autocommand
9134 * '<abuf>' to buffer number for autocommand
9135 * '<amatch>' to matching name for autocommand
9136 *
9137 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
9138 * "" for error without a message) and NULL is returned.
9139 * Returns an allocated string if a valid match was found.
9140 * Returns NULL if no match was found. "usedlen" then still contains the
9141 * number of characters to skip.
9142 */
9143 char_u *
9144eval_vars(src, usedlen, lnump, errormsg, srcstart)
9145 char_u *src; /* pointer into commandline */
9146 int *usedlen; /* characters after src that are used */
9147 linenr_T *lnump; /* line number for :e command, or NULL */
9148 char_u **errormsg; /* pointer to error message */
9149 char_u *srcstart; /* beginning of valid memory for src */
9150{
9151 int i;
9152 char_u *s;
9153 char_u *result;
9154 char_u *resultbuf = NULL;
9155 int resultlen;
9156 buf_T *buf;
9157 int valid = VALID_HEAD + VALID_PATH; /* assume valid result */
9158 int spec_idx;
9159#ifdef FEAT_MODIFY_FNAME
9160 int skip_mod = FALSE;
9161#endif
9162 static char *(spec_str[]) =
9163 {
9164 "%",
9165#define SPEC_PERC 0
9166 "#",
9167#define SPEC_HASH 1
9168 "<cword>", /* cursor word */
9169#define SPEC_CWORD 2
9170 "<cWORD>", /* cursor WORD */
9171#define SPEC_CCWORD 3
9172 "<cfile>", /* cursor path name */
9173#define SPEC_CFILE 4
9174 "<sfile>", /* ":so" file name */
9175#define SPEC_SFILE 5
9176#ifdef FEAT_AUTOCMD
9177 "<afile>", /* autocommand file name */
9178# define SPEC_AFILE 6
9179 "<abuf>", /* autocommand buffer number */
9180# define SPEC_ABUF 7
9181 "<amatch>", /* autocommand match name */
9182# define SPEC_AMATCH 8
9183#endif
9184#ifdef FEAT_CLIENTSERVER
9185 "<client>"
9186# define SPEC_CLIENT 9
9187#endif
9188 };
9189#define SPEC_COUNT (sizeof(spec_str) / sizeof(char *))
9190
9191#if defined(FEAT_AUTOCMD) || defined(FEAT_CLIENTSERVER)
9192 char_u strbuf[30];
9193#endif
9194
9195 *errormsg = NULL;
9196
9197 /*
9198 * Check if there is something to do.
9199 */
9200 for (spec_idx = 0; spec_idx < SPEC_COUNT; ++spec_idx)
9201 {
9202 *usedlen = (int)STRLEN(spec_str[spec_idx]);
9203 if (STRNCMP(src, spec_str[spec_idx], *usedlen) == 0)
9204 break;
9205 }
9206 if (spec_idx == SPEC_COUNT) /* no match */
9207 {
9208 *usedlen = 1;
9209 return NULL;
9210 }
9211
9212 /*
9213 * Skip when preceded with a backslash "\%" and "\#".
9214 * Note: In "\\%" the % is also not recognized!
9215 */
9216 if (src > srcstart && src[-1] == '\\')
9217 {
9218 *usedlen = 0;
9219 STRCPY(src - 1, src); /* remove backslash */
9220 return NULL;
9221 }
9222
9223 /*
9224 * word or WORD under cursor
9225 */
9226 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD)
9227 {
9228 resultlen = find_ident_under_cursor(&result, spec_idx == SPEC_CWORD ?
9229 (FIND_IDENT|FIND_STRING) : FIND_STRING);
9230 if (resultlen == 0)
9231 {
9232 *errormsg = (char_u *)"";
9233 return NULL;
9234 }
9235 }
9236
9237 /*
9238 * '#': Alternate file name
9239 * '%': Current file name
9240 * File name under the cursor
9241 * File name for autocommand
9242 * and following modifiers
9243 */
9244 else
9245 {
9246 switch (spec_idx)
9247 {
9248 case SPEC_PERC: /* '%': current file */
9249 if (curbuf->b_fname == NULL)
9250 {
9251 result = (char_u *)"";
9252 valid = 0; /* Must have ":p:h" to be valid */
9253 }
9254 else
9255#ifdef RISCOS
9256 /* Always use the full path for RISC OS if possible. */
9257 result = curbuf->b_ffname;
9258 if (result == NULL)
9259 result = curbuf->b_fname;
9260#else
9261 result = curbuf->b_fname;
9262#endif
9263 break;
9264
9265 case SPEC_HASH: /* '#' or "#99": alternate file */
9266 if (src[1] == '#') /* "##": the argument list */
9267 {
9268 result = arg_all();
9269 resultbuf = result;
9270 *usedlen = 2;
9271#ifdef FEAT_MODIFY_FNAME
9272 skip_mod = TRUE;
9273#endif
9274 break;
9275 }
9276 s = src + 1;
9277 i = (int)getdigits(&s);
9278 *usedlen = (int)(s - src); /* length of what we expand */
9279
9280 buf = buflist_findnr(i);
9281 if (buf == NULL)
9282 {
9283 *errormsg = (char_u *)_("E194: No alternate file name to substitute for '#'");
9284 return NULL;
9285 }
9286 if (lnump != NULL)
9287 *lnump = ECMD_LAST;
9288 if (buf->b_fname == NULL)
9289 {
9290 result = (char_u *)"";
9291 valid = 0; /* Must have ":p:h" to be valid */
9292 }
9293 else
9294 result = buf->b_fname;
9295 break;
9296
9297#ifdef FEAT_SEARCHPATH
9298 case SPEC_CFILE: /* file name under cursor */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009299 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009300 if (result == NULL)
9301 {
9302 *errormsg = (char_u *)"";
9303 return NULL;
9304 }
9305 resultbuf = result; /* remember allocated string */
9306 break;
9307#endif
9308
9309#ifdef FEAT_AUTOCMD
9310 case SPEC_AFILE: /* file name for autocommand */
9311 result = autocmd_fname;
9312 if (result == NULL)
9313 {
9314 *errormsg = (char_u *)_("E495: no autocommand file name to substitute for \"<afile>\"");
9315 return NULL;
9316 }
9317 break;
9318
9319 case SPEC_ABUF: /* buffer number for autocommand */
9320 if (autocmd_bufnr <= 0)
9321 {
9322 *errormsg = (char_u *)_("E496: no autocommand buffer number to substitute for \"<abuf>\"");
9323 return NULL;
9324 }
9325 sprintf((char *)strbuf, "%d", autocmd_bufnr);
9326 result = strbuf;
9327 break;
9328
9329 case SPEC_AMATCH: /* match name for autocommand */
9330 result = autocmd_match;
9331 if (result == NULL)
9332 {
9333 *errormsg = (char_u *)_("E497: no autocommand match name to substitute for \"<amatch>\"");
9334 return NULL;
9335 }
9336 break;
9337
9338#endif
9339 case SPEC_SFILE: /* file name for ":so" command */
9340 result = sourcing_name;
9341 if (result == NULL)
9342 {
9343 *errormsg = (char_u *)_("E498: no :source file name to substitute for \"<sfile>\"");
9344 return NULL;
9345 }
9346 break;
9347#if defined(FEAT_CLIENTSERVER)
9348 case SPEC_CLIENT: /* Source of last submitted input */
9349 sprintf((char *)strbuf, "0x%x", (unsigned int)clientWindow);
9350 result = strbuf;
9351 break;
9352#endif
9353 }
9354
9355 resultlen = (int)STRLEN(result); /* length of new string */
9356 if (src[*usedlen] == '<') /* remove the file name extension */
9357 {
9358 ++*usedlen;
9359#ifdef RISCOS
9360 if ((s = vim_strrchr(result, '/')) != NULL && s >= gettail(result))
9361#else
9362 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
9363#endif
9364 resultlen = (int)(s - result);
9365 }
9366#ifdef FEAT_MODIFY_FNAME
9367 else if (!skip_mod)
9368 {
9369 valid |= modify_fname(src, usedlen, &result, &resultbuf,
9370 &resultlen);
9371 if (result == NULL)
9372 {
9373 *errormsg = (char_u *)"";
9374 return NULL;
9375 }
9376 }
9377#endif
9378 }
9379
9380 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
9381 {
9382 if (valid != VALID_HEAD + VALID_PATH)
9383 /* xgettext:no-c-format */
9384 *errormsg = (char_u *)_("E499: Empty file name for '%' or '#', only works with \":p:h\"");
9385 else
9386 *errormsg = (char_u *)_("E500: Evaluates to an empty string");
9387 result = NULL;
9388 }
9389 else
9390 result = vim_strnsave(result, resultlen);
9391 vim_free(resultbuf);
9392 return result;
9393}
9394
9395/*
9396 * Concatenate all files in the argument list, separated by spaces, and return
9397 * it in one allocated string.
9398 * Spaces and backslashes in the file names are escaped with a backslash.
9399 * Returns NULL when out of memory.
9400 */
9401 static char_u *
9402arg_all()
9403{
9404 int len;
9405 int idx;
9406 char_u *retval = NULL;
9407 char_u *p;
9408
9409 /*
9410 * Do this loop two times:
9411 * first time: compute the total length
9412 * second time: concatenate the names
9413 */
9414 for (;;)
9415 {
9416 len = 0;
9417 for (idx = 0; idx < ARGCOUNT; ++idx)
9418 {
9419 p = alist_name(&ARGLIST[idx]);
9420 if (p != NULL)
9421 {
9422 if (len > 0)
9423 {
9424 /* insert a space in between names */
9425 if (retval != NULL)
9426 retval[len] = ' ';
9427 ++len;
9428 }
9429 for ( ; *p != NUL; ++p)
9430 {
9431 if (*p == ' ' || *p == '\\')
9432 {
9433 /* insert a backslash */
9434 if (retval != NULL)
9435 retval[len] = '\\';
9436 ++len;
9437 }
9438 if (retval != NULL)
9439 retval[len] = *p;
9440 ++len;
9441 }
9442 }
9443 }
9444
9445 /* second time: break here */
9446 if (retval != NULL)
9447 {
9448 retval[len] = NUL;
9449 break;
9450 }
9451
9452 /* allocate memory */
9453 retval = alloc(len + 1);
9454 if (retval == NULL)
9455 break;
9456 }
9457
9458 return retval;
9459}
9460
9461#if defined(FEAT_AUTOCMD) || defined(PROTO)
9462/*
9463 * Expand the <sfile> string in "arg".
9464 *
9465 * Returns an allocated string, or NULL for any error.
9466 */
9467 char_u *
9468expand_sfile(arg)
9469 char_u *arg;
9470{
9471 char_u *errormsg;
9472 int len;
9473 char_u *result;
9474 char_u *newres;
9475 char_u *repl;
9476 int srclen;
9477 char_u *p;
9478
9479 result = vim_strsave(arg);
9480 if (result == NULL)
9481 return NULL;
9482
9483 for (p = result; *p; )
9484 {
9485 if (STRNCMP(p, "<sfile>", 7) != 0)
9486 ++p;
9487 else
9488 {
9489 /* replace "<sfile>" with the sourced file name, and do ":" stuff */
9490 repl = eval_vars(p, &srclen, NULL, &errormsg, result);
9491 if (errormsg != NULL)
9492 {
9493 if (*errormsg)
9494 emsg(errormsg);
9495 vim_free(result);
9496 return NULL;
9497 }
9498 if (repl == NULL) /* no match (cannot happen) */
9499 {
9500 p += srclen;
9501 continue;
9502 }
9503 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
9504 newres = alloc(len);
9505 if (newres == NULL)
9506 {
9507 vim_free(repl);
9508 vim_free(result);
9509 return NULL;
9510 }
9511 mch_memmove(newres, result, (size_t)(p - result));
9512 STRCPY(newres + (p - result), repl);
9513 len = (int)STRLEN(newres);
9514 STRCAT(newres, p + srclen);
9515 vim_free(repl);
9516 vim_free(result);
9517 result = newres;
9518 p = newres + len; /* continue after the match */
9519 }
9520 }
9521
9522 return result;
9523}
9524#endif
9525
9526#ifdef FEAT_SESSION
9527static int ses_winsizes __ARGS((FILE *fd, int restore_size));
9528static int ses_win_rec __ARGS((FILE *fd, frame_T *fr));
9529static frame_T *ses_skipframe __ARGS((frame_T *fr));
9530static int ses_do_frame __ARGS((frame_T *fr));
9531static int ses_do_win __ARGS((win_T *wp));
9532static int ses_arglist __ARGS((FILE *fd, char *cmd, garray_T *gap, int fullname, unsigned *flagp));
9533static int ses_put_fname __ARGS((FILE *fd, char_u *name, unsigned *flagp));
9534static int ses_fname __ARGS((FILE *fd, buf_T *buf, unsigned *flagp));
9535
9536/*
9537 * Write openfile commands for the current buffers to an .exrc file.
9538 * Return FAIL on error, OK otherwise.
9539 */
9540 static int
9541makeopens(fd, dirnow)
9542 FILE *fd;
9543 char_u *dirnow; /* Current directory name */
9544{
9545 buf_T *buf;
9546 int only_save_windows = TRUE;
9547 int nr;
9548 int cnr = 1;
9549 int restore_size = TRUE;
9550 win_T *wp;
9551 char_u *sname;
9552 win_T *edited_win = NULL;
9553
9554 if (ssop_flags & SSOP_BUFFERS)
9555 only_save_windows = FALSE; /* Save ALL buffers */
9556
9557 /*
9558 * Begin by setting the this_session variable, and then other
9559 * sessionable variables.
9560 */
9561#ifdef FEAT_EVAL
9562 if (put_line(fd, "let v:this_session=expand(\"<sfile>:p\")") == FAIL)
9563 return FAIL;
9564 if (ssop_flags & SSOP_GLOBALS)
9565 if (store_session_globals(fd) == FAIL)
9566 return FAIL;
9567#endif
9568
9569 /*
9570 * Close all windows but one.
9571 */
9572 if (put_line(fd, "silent only") == FAIL)
9573 return FAIL;
9574
9575 /*
9576 * Now a :cd command to the session directory or the current directory
9577 */
9578 if (ssop_flags & SSOP_SESDIR)
9579 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009580 if (put_line(fd, "exe \"cd \" . escape(expand(\"<sfile>:p:h\"), ' ')")
9581 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009582 return FAIL;
9583 }
9584 else if (ssop_flags & SSOP_CURDIR)
9585 {
9586 sname = home_replace_save(NULL, globaldir != NULL ? globaldir : dirnow);
9587 if (sname == NULL
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009588 || fputs("cd ", fd) < 0
9589 || ses_put_fname(fd, sname, &ssop_flags) == FAIL
9590 || put_eol(fd) == FAIL)
9591 {
9592 vim_free(sname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009593 return FAIL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009594 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009595 vim_free(sname);
9596 }
9597
9598 /*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009599 * If there is an empty, unnamed buffer we will wipe it out later.
9600 * Remember the buffer number.
9601 */
9602 if (put_line(fd, "if expand('%') == '' && !&modified && line('$') <= 1 && getline(1) == ''") == FAIL)
9603 return FAIL;
9604 if (put_line(fd, " let s:wipebuf = bufnr('%')") == FAIL)
9605 return FAIL;
9606 if (put_line(fd, "endif") == FAIL)
9607 return FAIL;
9608
9609 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009610 * Now save the current files, current buffer first.
9611 */
9612 if (put_line(fd, "set shortmess=aoO") == FAIL)
9613 return FAIL;
9614
9615 /* Now put the other buffers into the buffer list */
9616 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9617 {
9618 if (!(only_save_windows && buf->b_nwindows == 0)
9619 && !(buf->b_help && !(ssop_flags & SSOP_HELP))
9620 && buf->b_fname != NULL
9621 && buf->b_p_bl)
9622 {
9623 if (fprintf(fd, "badd +%ld ", buf->b_wininfo == NULL ? 1L
9624 : buf->b_wininfo->wi_fpos.lnum) < 0
9625 || ses_fname(fd, buf, &ssop_flags) == FAIL)
9626 return FAIL;
9627 }
9628 }
9629
9630 /* the global argument list */
9631 if (ses_arglist(fd, "args", &global_alist.al_ga,
9632 !(ssop_flags & SSOP_CURDIR), &ssop_flags) == FAIL)
9633 return FAIL;
9634
9635 if (ssop_flags & SSOP_RESIZE)
9636 {
9637 /* Note: after the restore we still check it worked!*/
9638 if (fprintf(fd, "set lines=%ld columns=%ld" , Rows, Columns) < 0
9639 || put_eol(fd) == FAIL)
9640 return FAIL;
9641 }
9642
9643#ifdef FEAT_GUI
9644 if (gui.in_use && (ssop_flags & SSOP_WINPOS))
9645 {
9646 int x, y;
9647
9648 if (gui_mch_get_winpos(&x, &y) == OK)
9649 {
9650 /* Note: after the restore we still check it worked!*/
9651 if (fprintf(fd, "winpos %d %d", x, y) < 0 || put_eol(fd) == FAIL)
9652 return FAIL;
9653 }
9654 }
9655#endif
9656
9657 /*
9658 * Before creating the window layout, try loading one file. If this is
9659 * aborted we don't end up with a number of useless windows.
9660 * This may have side effects! (e.g., compressed or network file).
9661 */
9662 for (wp = firstwin; wp != NULL; wp = wp->w_next)
9663 {
9664 if (ses_do_win(wp)
9665 && wp->w_buffer->b_ffname != NULL
9666 && !wp->w_buffer->b_help
9667#ifdef FEAT_QUICKFIX
9668 && !bt_nofile(wp->w_buffer)
9669#endif
9670 )
9671 {
9672 if (fputs("edit ", fd) < 0
9673 || ses_fname(fd, wp->w_buffer, &ssop_flags) == FAIL)
9674 return FAIL;
9675 if (!wp->w_arg_idx_invalid)
9676 edited_win = wp;
9677 break;
9678 }
9679 }
9680
9681 /*
9682 * Save current window layout.
9683 */
9684 if (put_line(fd, "set splitbelow splitright") == FAIL)
9685 return FAIL;
9686 if (ses_win_rec(fd, topframe) == FAIL)
9687 return FAIL;
9688 if (!p_sb && put_line(fd, "set nosplitbelow") == FAIL)
9689 return FAIL;
9690 if (!p_spr && put_line(fd, "set nosplitright") == FAIL)
9691 return FAIL;
9692
9693 /*
9694 * Check if window sizes can be restored (no windows omitted).
9695 * Remember the window number of the current window after restoring.
9696 */
9697 nr = 0;
9698 for (wp = firstwin; wp != NULL; wp = W_NEXT(wp))
9699 {
9700 if (ses_do_win(wp))
9701 ++nr;
9702 else
9703 restore_size = FALSE;
9704 if (curwin == wp)
9705 cnr = nr;
9706 }
9707
9708 /* Go to the first window. */
9709 if (put_line(fd, "wincmd t") == FAIL)
9710 return FAIL;
9711
9712 /*
9713 * If more than one window, see if sizes can be restored.
9714 * First set 'winheight' and 'winwidth' to 1 to avoid the windows being
9715 * resized when moving between windows.
9716 * Do this before restoring the view, so that the topline and the cursor
9717 * can be set. This is done again below.
9718 */
9719 if (put_line(fd, "set winheight=1 winwidth=1") == FAIL)
9720 return FAIL;
9721 if (nr > 1 && ses_winsizes(fd, restore_size) == FAIL)
9722 return FAIL;
9723
9724 /*
9725 * Restore the view of the window (options, file, cursor, etc.).
9726 */
9727 for (wp = firstwin; wp != NULL; wp = wp->w_next)
9728 {
9729 if (!ses_do_win(wp))
9730 continue;
9731 if (put_view(fd, wp, wp != edited_win, &ssop_flags) == FAIL)
9732 return FAIL;
9733 if (nr > 1 && put_line(fd, "wincmd w") == FAIL)
9734 return FAIL;
9735 }
9736
9737 /*
9738 * Restore cursor to the current window if it's not the first one.
9739 */
9740 if (cnr > 1 && (fprintf(fd, "%dwincmd w", cnr) < 0 || put_eol(fd) == FAIL))
9741 return FAIL;
9742
9743 /*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009744 * Wipe out an empty unnamed buffer we started in.
9745 */
9746 if (put_line(fd, "if exists('s:wipebuf')") == FAIL)
9747 return FAIL;
9748 if (put_line(fd, " exe 'bwipe ' . s:wipebuf") == FAIL)
9749 return FAIL;
9750 if (put_line(fd, "endif") == FAIL)
9751 return FAIL;
9752 if (put_line(fd, "unlet! s:wipebuf") == FAIL)
9753 return FAIL;
9754
9755 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009756 * Restore window sizes again after jumping around in windows, because the
9757 * current window has a minimum size while others may not.
9758 */
9759 if (nr > 1 && ses_winsizes(fd, restore_size) == FAIL)
9760 return FAIL;
9761
9762 /* Re-apply 'winheight', 'winwidth' and 'shortmess'. */
9763 if (fprintf(fd, "set winheight=%ld winwidth=%ld shortmess=%s",
9764 p_wh, p_wiw, p_shm) < 0 || put_eol(fd) == FAIL)
9765 return FAIL;
9766
9767 /*
9768 * Lastly, execute the x.vim file if it exists.
9769 */
9770 if (put_line(fd, "let s:sx = expand(\"<sfile>:p:r\").\"x.vim\"") == FAIL
9771 || put_line(fd, "if file_readable(s:sx)") == FAIL
9772 || put_line(fd, " exe \"source \" . s:sx") == FAIL
9773 || put_line(fd, "endif") == FAIL)
9774 return FAIL;
9775
9776 return OK;
9777}
9778
9779 static int
9780ses_winsizes(fd, restore_size)
9781 FILE *fd;
9782 int restore_size;
9783{
9784 int n = 0;
9785 win_T *wp;
9786
9787 if (restore_size && (ssop_flags & SSOP_WINSIZE))
9788 {
9789 for (wp = firstwin; wp != NULL; wp = wp->w_next)
9790 {
9791 if (!ses_do_win(wp))
9792 continue;
9793 ++n;
9794
9795 /* restore height when not full height */
9796 if (wp->w_height + wp->w_status_height < topframe->fr_height
9797 && (fprintf(fd,
9798 "exe '%dresize ' . ((&lines * %ld + %ld) / %ld)",
9799 n, (long)wp->w_height, Rows / 2, Rows) < 0
9800 || put_eol(fd) == FAIL))
9801 return FAIL;
9802
9803 /* restore width when not full width */
9804 if (wp->w_width < Columns && (fprintf(fd,
9805 "exe 'vert %dresize ' . ((&columns * %ld + %ld) / %ld)",
9806 n, (long)wp->w_width, Columns / 2, Columns) < 0
9807 || put_eol(fd) == FAIL))
9808 return FAIL;
9809 }
9810 }
9811 else
9812 {
9813 /* Just equalise window sizes */
9814 if (put_line(fd, "wincmd =") == FAIL)
9815 return FAIL;
9816 }
9817 return OK;
9818}
9819
9820/*
9821 * Write commands to "fd" to recursively create windows for frame "fr",
9822 * horizontally and vertically split.
9823 * After the commands the last window in the frame is the current window.
9824 * Returns FAIL when writing the commands to "fd" fails.
9825 */
9826 static int
9827ses_win_rec(fd, fr)
9828 FILE *fd;
9829 frame_T *fr;
9830{
9831 frame_T *frc;
9832 int count = 0;
9833
9834 if (fr->fr_layout != FR_LEAF)
9835 {
9836 /* Find first frame that's not skipped and then create a window for
9837 * each following one (first frame is already there). */
9838 frc = ses_skipframe(fr->fr_child);
9839 if (frc != NULL)
9840 while ((frc = ses_skipframe(frc->fr_next)) != NULL)
9841 {
9842 /* Make window as big as possible so that we have lots of room
9843 * to split. */
9844 if (put_line(fd, "wincmd _ | wincmd |") == FAIL
9845 || put_line(fd, fr->fr_layout == FR_COL
9846 ? "split" : "vsplit") == FAIL)
9847 return FAIL;
9848 ++count;
9849 }
9850
9851 /* Go back to the first window. */
9852 if (count > 0 && (fprintf(fd, fr->fr_layout == FR_COL
9853 ? "%dwincmd k" : "%dwincmd h", count) < 0
9854 || put_eol(fd) == FAIL))
9855 return FAIL;
9856
9857 /* Recursively create frames/windows in each window of this column or
9858 * row. */
9859 frc = ses_skipframe(fr->fr_child);
9860 while (frc != NULL)
9861 {
9862 ses_win_rec(fd, frc);
9863 frc = ses_skipframe(frc->fr_next);
9864 /* Go to next window. */
9865 if (frc != NULL && put_line(fd, "wincmd w") == FAIL)
9866 return FAIL;
9867 }
9868 }
9869 return OK;
9870}
9871
9872/*
9873 * Skip frames that don't contain windows we want to save in the Session.
9874 * Returns NULL when there none.
9875 */
9876 static frame_T *
9877ses_skipframe(fr)
9878 frame_T *fr;
9879{
9880 frame_T *frc;
9881
9882 for (frc = fr; frc != NULL; frc = frc->fr_next)
9883 if (ses_do_frame(frc))
9884 break;
9885 return frc;
9886}
9887
9888/*
9889 * Return TRUE if frame "fr" has a window somewhere that we want to save in
9890 * the Session.
9891 */
9892 static int
9893ses_do_frame(fr)
9894 frame_T *fr;
9895{
9896 frame_T *frc;
9897
9898 if (fr->fr_layout == FR_LEAF)
9899 return ses_do_win(fr->fr_win);
9900 for (frc = fr->fr_child; frc != NULL; frc = frc->fr_next)
9901 if (ses_do_frame(frc))
9902 return TRUE;
9903 return FALSE;
9904}
9905
9906/*
9907 * Return non-zero if window "wp" is to be stored in the Session.
9908 */
9909 static int
9910ses_do_win(wp)
9911 win_T *wp;
9912{
9913 if (wp->w_buffer->b_fname == NULL
9914#ifdef FEAT_QUICKFIX
9915 /* When 'buftype' is "nofile" can't restore the window contents. */
9916 || bt_nofile(wp->w_buffer)
9917#endif
9918 )
9919 return (ssop_flags & SSOP_BLANK);
9920 if (wp->w_buffer->b_help)
9921 return (ssop_flags & SSOP_HELP);
9922 return TRUE;
9923}
9924
9925/*
9926 * Write commands to "fd" to restore the view of a window.
9927 * Caller must make sure 'scrolloff' is zero.
9928 */
9929 static int
9930put_view(fd, wp, add_edit, flagp)
9931 FILE *fd;
9932 win_T *wp;
9933 int add_edit; /* add ":edit" command to view */
9934 unsigned *flagp; /* vop_flags or ssop_flags */
9935{
9936 win_T *save_curwin;
9937 int f;
9938 int do_cursor;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00009939 int did_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009940
9941 /* Always restore cursor position for ":mksession". For ":mkview" only
9942 * when 'viewoptions' contains "cursor". */
9943 do_cursor = (flagp == &ssop_flags || *flagp & SSOP_CURSOR);
9944
9945 /*
9946 * Local argument list.
9947 */
9948 if (wp->w_alist == &global_alist)
9949 {
9950 if (put_line(fd, "argglobal") == FAIL)
9951 return FAIL;
9952 }
9953 else
9954 {
9955 if (ses_arglist(fd, "arglocal", &wp->w_alist->al_ga,
9956 flagp == &vop_flags
9957 || !(*flagp & SSOP_CURDIR)
9958 || wp->w_localdir != NULL, flagp) == FAIL)
9959 return FAIL;
9960 }
9961
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00009962 /* Only when part of a session: restore the argument index. Some
9963 * arguments may have been deleted, check if the index is valid. */
9964 if (wp->w_arg_idx != 0 && wp->w_arg_idx <= WARGCOUNT(wp)
9965 && flagp == &ssop_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009966 {
9967 if (fprintf(fd, "%ldnext", (long)wp->w_arg_idx) < 0
9968 || put_eol(fd) == FAIL)
9969 return FAIL;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00009970 did_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009971 }
9972
9973 /* Edit the file. Skip this when ":next" already did it. */
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00009974 if (add_edit && (!did_next || wp->w_arg_idx_invalid))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009975 {
9976 /*
9977 * Load the file.
9978 */
9979 if (wp->w_buffer->b_ffname != NULL
9980#ifdef FEAT_QUICKFIX
9981 && !bt_nofile(wp->w_buffer)
9982#endif
9983 )
9984 {
9985 /*
9986 * Editing a file in this buffer: use ":edit file".
9987 * This may have side effects! (e.g., compressed or network file).
9988 */
9989 if (fputs("edit ", fd) < 0
9990 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
9991 return FAIL;
9992 }
9993 else
9994 {
9995 /* No file in this buffer, just make it empty. */
9996 if (put_line(fd, "enew") == FAIL)
9997 return FAIL;
9998#ifdef FEAT_QUICKFIX
9999 if (wp->w_buffer->b_ffname != NULL)
10000 {
10001 /* The buffer does have a name, but it's not a file name. */
10002 if (fputs("file ", fd) < 0
10003 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
10004 return FAIL;
10005 }
10006#endif
10007 do_cursor = FALSE;
10008 }
10009 }
10010
10011 /*
10012 * Local mappings and abbreviations.
10013 */
10014 if ((*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
10015 && makemap(fd, wp->w_buffer) == FAIL)
10016 return FAIL;
10017
10018 /*
10019 * Local options. Need to go to the window temporarily.
10020 * Store only local values when using ":mkview" and when ":mksession" is
10021 * used and 'sessionoptions' doesn't include "options".
10022 * Some folding options are always stored when "folds" is included,
10023 * otherwise the folds would not be restored correctly.
10024 */
10025 save_curwin = curwin;
10026 curwin = wp;
10027 curbuf = curwin->w_buffer;
10028 if (*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
10029 f = makeset(fd, OPT_LOCAL,
10030 flagp == &vop_flags || !(*flagp & SSOP_OPTIONS));
10031#ifdef FEAT_FOLDING
10032 else if (*flagp & SSOP_FOLDS)
10033 f = makefoldset(fd);
10034#endif
10035 else
10036 f = OK;
10037 curwin = save_curwin;
10038 curbuf = curwin->w_buffer;
10039 if (f == FAIL)
10040 return FAIL;
10041
10042#ifdef FEAT_FOLDING
10043 /*
10044 * Save Folds when 'buftype' is empty and for help files.
10045 */
10046 if ((*flagp & SSOP_FOLDS)
10047 && wp->w_buffer->b_ffname != NULL
Bram Moolenaarb1b715d2006-01-21 22:09:43 +000010048# ifdef FEAT_QUICKFIX
10049 && (*wp->w_buffer->b_p_bt == NUL || wp->w_buffer->b_help)
10050# endif
10051 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000010052 {
10053 if (put_folds(fd, wp) == FAIL)
10054 return FAIL;
10055 }
10056#endif
10057
10058 /*
10059 * Set the cursor after creating folds, since that moves the cursor.
10060 */
10061 if (do_cursor)
10062 {
10063
10064 /* Restore the cursor line in the file and relatively in the
10065 * window. Don't use "G", it changes the jumplist. */
10066 if (fprintf(fd, "let s:l = %ld - ((%ld * winheight(0) + %ld) / %ld)",
10067 (long)wp->w_cursor.lnum,
10068 (long)(wp->w_cursor.lnum - wp->w_topline),
10069 (long)wp->w_height / 2, (long)wp->w_height) < 0
10070 || put_eol(fd) == FAIL
10071 || put_line(fd, "if s:l < 1 | let s:l = 1 | endif") == FAIL
10072 || put_line(fd, "exe s:l") == FAIL
10073 || put_line(fd, "normal! zt") == FAIL
10074 || fprintf(fd, "%ld", (long)wp->w_cursor.lnum) < 0
10075 || put_eol(fd) == FAIL)
10076 return FAIL;
10077 /* Restore the cursor column and left offset when not wrapping. */
10078 if (wp->w_cursor.col == 0)
10079 {
10080 if (put_line(fd, "normal! 0") == FAIL)
10081 return FAIL;
10082 }
10083 else
10084 {
10085 if (!wp->w_p_wrap && wp->w_leftcol > 0 && wp->w_width > 0)
10086 {
10087 if (fprintf(fd,
10088 "let s:c = %ld - ((%ld * winwidth(0) + %ld) / %ld)",
10089 (long)wp->w_cursor.col,
10090 (long)(wp->w_cursor.col - wp->w_leftcol),
10091 (long)wp->w_width / 2, (long)wp->w_width) < 0
10092 || put_eol(fd) == FAIL
10093 || put_line(fd, "if s:c > 0") == FAIL
10094 || fprintf(fd,
10095 " exe 'normal! 0' . s:c . 'lzs' . (%ld - s:c) . 'l'",
10096 (long)wp->w_cursor.col) < 0
10097 || put_eol(fd) == FAIL
10098 || put_line(fd, "else") == FAIL
10099 || fprintf(fd, " normal! 0%dl", wp->w_cursor.col) < 0
10100 || put_eol(fd) == FAIL
10101 || put_line(fd, "endif") == FAIL)
10102 return FAIL;
10103 }
10104 else
10105 {
10106 if (fprintf(fd, "normal! 0%dl", wp->w_cursor.col) < 0
10107 || put_eol(fd) == FAIL)
10108 return FAIL;
10109 }
10110 }
10111 }
10112
10113 /*
10114 * Local directory.
10115 */
10116 if (wp->w_localdir != NULL)
10117 {
10118 if (fputs("lcd ", fd) < 0
10119 || ses_put_fname(fd, wp->w_localdir, flagp) == FAIL
10120 || put_eol(fd) == FAIL)
10121 return FAIL;
10122 }
10123
10124 return OK;
10125}
10126
10127/*
10128 * Write an argument list to the session file.
10129 * Returns FAIL if writing fails.
10130 */
10131 static int
10132ses_arglist(fd, cmd, gap, fullname, flagp)
10133 FILE *fd;
10134 char *cmd;
10135 garray_T *gap;
10136 int fullname; /* TRUE: use full path name */
10137 unsigned *flagp;
10138{
10139 int i;
10140 char_u buf[MAXPATHL];
10141 char_u *s;
10142
10143 if (gap->ga_len == 0)
10144 return put_line(fd, "silent! argdel *");
10145 if (fputs(cmd, fd) < 0)
10146 return FAIL;
10147 for (i = 0; i < gap->ga_len; ++i)
10148 {
10149 /* NULL file names are skipped (only happens when out of memory). */
10150 s = alist_name(&((aentry_T *)gap->ga_data)[i]);
10151 if (s != NULL)
10152 {
10153 if (fullname)
10154 {
10155 (void)vim_FullName(s, buf, MAXPATHL, FALSE);
10156 s = buf;
10157 }
10158 if (fputs(" ", fd) < 0 || ses_put_fname(fd, s, flagp) == FAIL)
10159 return FAIL;
10160 }
10161 }
10162 return put_eol(fd);
10163}
10164
10165/*
10166 * Write a buffer name to the session file.
10167 * Also ends the line.
10168 * Returns FAIL if writing fails.
10169 */
10170 static int
10171ses_fname(fd, buf, flagp)
10172 FILE *fd;
10173 buf_T *buf;
10174 unsigned *flagp;
10175{
10176 char_u *name;
10177
10178 /* Use the short file name if the current directory is known at the time
10179 * the session file will be sourced. Don't do this for ":mkview", we
10180 * don't know the current directory. */
10181 if (buf->b_sfname != NULL
10182 && flagp == &ssop_flags
10183 && (ssop_flags & (SSOP_CURDIR | SSOP_SESDIR)))
10184 name = buf->b_sfname;
10185 else
10186 name = buf->b_ffname;
10187 if (ses_put_fname(fd, name, flagp) == FAIL || put_eol(fd) == FAIL)
10188 return FAIL;
10189 return OK;
10190}
10191
10192/*
10193 * Write a file name to the session file.
10194 * Takes care of the "slash" option in 'sessionoptions' and escapes special
10195 * characters.
10196 * Returns FAIL if writing fails.
10197 */
10198 static int
10199ses_put_fname(fd, name, flagp)
10200 FILE *fd;
10201 char_u *name;
10202 unsigned *flagp;
10203{
10204 char_u *sname;
10205 int retval = OK;
10206 int c;
10207
10208 sname = home_replace_save(NULL, name);
10209 if (sname != NULL)
10210 name = sname;
10211 while (*name != NUL)
10212 {
10213#ifdef FEAT_MBYTE
10214 {
10215 int l;
10216
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010217 if (has_mbyte && (l = (*mb_ptr2len)(name)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010218 {
10219 /* copy a multibyte char */
10220 while (--l >= 0)
10221 {
10222 if (putc(*name, fd) != *name)
10223 retval = FAIL;
10224 ++name;
10225 }
10226 continue;
10227 }
10228 }
10229#endif
10230 c = *name++;
10231 if (c == '\\' && (*flagp & SSOP_SLASH))
10232 /* change a backslash to a forward slash */
10233 c = '/';
10234 else if ((vim_strchr(escape_chars, c) != NULL
10235#ifdef BACKSLASH_IN_FILENAME
10236 && c != '\\'
10237#endif
10238 ) || c == '#' || c == '%')
10239 {
10240 /* escape a special character with a backslash */
10241 if (putc('\\', fd) != '\\')
10242 retval = FAIL;
10243 }
10244 if (putc(c, fd) != c)
10245 retval = FAIL;
10246 }
10247 vim_free(sname);
10248 return retval;
10249}
10250
10251/*
10252 * ":loadview [nr]"
10253 */
10254 static void
10255ex_loadview(eap)
10256 exarg_T *eap;
10257{
10258 char_u *fname;
10259
10260 fname = get_view_file(*eap->arg);
10261 if (fname != NULL)
10262 {
10263 do_source(fname, FALSE, FALSE);
10264 vim_free(fname);
10265 }
10266}
10267
10268/*
10269 * Get the name of the view file for the current buffer.
10270 */
10271 static char_u *
10272get_view_file(c)
10273 int c;
10274{
10275 int len = 0;
10276 char_u *p, *s;
10277 char_u *retval;
10278 char_u *sname;
10279
10280 if (curbuf->b_ffname == NULL)
10281 {
10282 EMSG(_(e_noname));
10283 return NULL;
10284 }
10285 sname = home_replace_save(NULL, curbuf->b_ffname);
10286 if (sname == NULL)
10287 return NULL;
10288
10289 /*
10290 * We want a file name without separators, because we're not going to make
10291 * a directory.
10292 * "normal" path separator -> "=+"
10293 * "=" -> "=="
10294 * ":" path separator -> "=-"
10295 */
10296 for (p = sname; *p; ++p)
10297 if (*p == '=' || vim_ispathsep(*p))
10298 ++len;
10299 retval = alloc((unsigned)(STRLEN(sname) + len + STRLEN(p_vdir) + 9));
10300 if (retval != NULL)
10301 {
10302 STRCPY(retval, p_vdir);
10303 add_pathsep(retval);
10304 s = retval + STRLEN(retval);
10305 for (p = sname; *p; ++p)
10306 {
10307 if (*p == '=')
10308 {
10309 *s++ = '=';
10310 *s++ = '=';
10311 }
10312 else if (vim_ispathsep(*p))
10313 {
10314 *s++ = '=';
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000010315#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA) || defined(RISCOS) \
Bram Moolenaar071d4272004-06-13 20:20:40 +000010316 || defined(VMS)
10317 if (*p == ':')
10318 *s++ = '-';
10319 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010320#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000010321 *s++ = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010322 }
10323 else
10324 *s++ = *p;
10325 }
10326 *s++ = '=';
10327 *s++ = c;
10328 STRCPY(s, ".vim");
10329 }
10330
10331 vim_free(sname);
10332 return retval;
10333}
10334
10335#endif /* FEAT_SESSION */
10336
10337/*
10338 * Write end-of-line character(s) for ":mkexrc", ":mkvimrc" and ":mksession".
10339 * Return FAIL for a write error.
10340 */
10341 int
10342put_eol(fd)
10343 FILE *fd;
10344{
10345 if (
10346#ifdef USE_CRNL
10347 (
10348# ifdef MKSESSION_NL
10349 !mksession_nl &&
10350# endif
10351 (putc('\r', fd) < 0)) ||
10352#endif
10353 (putc('\n', fd) < 0))
10354 return FAIL;
10355 return OK;
10356}
10357
10358/*
10359 * Write a line to "fd".
10360 * Return FAIL for a write error.
10361 */
10362 int
10363put_line(fd, s)
10364 FILE *fd;
10365 char *s;
10366{
10367 if (fputs(s, fd) < 0 || put_eol(fd) == FAIL)
10368 return FAIL;
10369 return OK;
10370}
10371
10372#ifdef FEAT_VIMINFO
10373/*
10374 * ":rviminfo" and ":wviminfo".
10375 */
10376 static void
10377ex_viminfo(eap)
10378 exarg_T *eap;
10379{
10380 char_u *save_viminfo;
10381
10382 save_viminfo = p_viminfo;
10383 if (*p_viminfo == NUL)
10384 p_viminfo = (char_u *)"'100";
10385 if (eap->cmdidx == CMD_rviminfo)
10386 {
10387 if (read_viminfo(eap->arg, TRUE, TRUE, eap->forceit) == FAIL)
10388 EMSG(_("E195: Cannot open viminfo file for reading"));
10389 }
10390 else
10391 write_viminfo(eap->arg, eap->forceit);
10392 p_viminfo = save_viminfo;
10393}
10394#endif
10395
10396#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +000010397/*
10398 * Make a dialog message in "buff[IOSIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +000010399 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +000010400 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010401 void
10402dialog_msg(buff, format, fname)
10403 char_u *buff;
10404 char *format;
10405 char_u *fname;
10406{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010407 if (fname == NULL)
10408 fname = (char_u *)_("Untitled");
Bram Moolenaarb765d632005-06-07 21:00:02 +000010409 vim_snprintf((char *)buff, IOSIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010410}
10411#endif
10412
10413/*
10414 * ":behave {mswin,xterm}"
10415 */
10416 static void
10417ex_behave(eap)
10418 exarg_T *eap;
10419{
10420 if (STRCMP(eap->arg, "mswin") == 0)
10421 {
10422 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
10423 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
10424 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
10425 set_option_value((char_u *)"keymodel", 0L,
10426 (char_u *)"startsel,stopsel", 0);
10427 }
10428 else if (STRCMP(eap->arg, "xterm") == 0)
10429 {
10430 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
10431 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
10432 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
10433 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
10434 }
10435 else
10436 EMSG2(_(e_invarg2), eap->arg);
10437}
10438
10439#ifdef FEAT_AUTOCMD
10440static int filetype_detect = FALSE;
10441static int filetype_plugin = FALSE;
10442static int filetype_indent = FALSE;
10443
10444/*
10445 * ":filetype [plugin] [indent] {on,off,detect}"
10446 * on: Load the filetype.vim file to install autocommands for file types.
10447 * off: Load the ftoff.vim file to remove all autocommands for file types.
10448 * plugin on: load filetype.vim and ftplugin.vim
10449 * plugin off: load ftplugof.vim
10450 * indent on: load filetype.vim and indent.vim
10451 * indent off: load indoff.vim
10452 */
10453 static void
10454ex_filetype(eap)
10455 exarg_T *eap;
10456{
10457 char_u *arg = eap->arg;
10458 int plugin = FALSE;
10459 int indent = FALSE;
10460
10461 if (*eap->arg == NUL)
10462 {
10463 /* Print current status. */
10464 smsg((char_u *)"filetype detection:%s plugin:%s indent:%s",
10465 filetype_detect ? "ON" : "OFF",
10466 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
10467 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
10468 return;
10469 }
10470
10471 /* Accept "plugin" and "indent" in any order. */
10472 for (;;)
10473 {
10474 if (STRNCMP(arg, "plugin", 6) == 0)
10475 {
10476 plugin = TRUE;
10477 arg = skipwhite(arg + 6);
10478 continue;
10479 }
10480 if (STRNCMP(arg, "indent", 6) == 0)
10481 {
10482 indent = TRUE;
10483 arg = skipwhite(arg + 6);
10484 continue;
10485 }
10486 break;
10487 }
10488 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
10489 {
10490 if (*arg == 'o' || !filetype_detect)
10491 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000010492 source_runtime((char_u *)FILETYPE_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010493 filetype_detect = TRUE;
10494 if (plugin)
10495 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000010496 source_runtime((char_u *)FTPLUGIN_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010497 filetype_plugin = TRUE;
10498 }
10499 if (indent)
10500 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000010501 source_runtime((char_u *)INDENT_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010502 filetype_indent = TRUE;
10503 }
10504 }
10505 if (*arg == 'd')
10506 {
10507 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE);
Bram Moolenaara3227e22006-03-08 21:32:40 +000010508 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010509 }
10510 }
10511 else if (STRCMP(arg, "off") == 0)
10512 {
10513 if (plugin || indent)
10514 {
10515 if (plugin)
10516 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000010517 source_runtime((char_u *)FTPLUGOF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010518 filetype_plugin = FALSE;
10519 }
10520 if (indent)
10521 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000010522 source_runtime((char_u *)INDOFF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010523 filetype_indent = FALSE;
10524 }
10525 }
10526 else
10527 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000010528 source_runtime((char_u *)FTOFF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010529 filetype_detect = FALSE;
10530 }
10531 }
10532 else
10533 EMSG2(_(e_invarg2), arg);
10534}
10535
10536/*
10537 * ":setfiletype {name}"
10538 */
10539 static void
10540ex_setfiletype(eap)
10541 exarg_T *eap;
10542{
10543 if (!did_filetype)
10544 set_option_value((char_u *)"filetype", 0L, eap->arg, OPT_LOCAL);
10545}
10546#endif
10547
10548/*ARGSUSED*/
10549 static void
10550ex_digraphs(eap)
10551 exarg_T *eap;
10552{
10553#ifdef FEAT_DIGRAPHS
10554 if (*eap->arg != NUL)
10555 putdigraph(eap->arg);
10556 else
10557 listdigraphs();
10558#else
10559 EMSG(_("E196: No digraphs in this version"));
10560#endif
10561}
10562
10563 static void
10564ex_set(eap)
10565 exarg_T *eap;
10566{
10567 int flags = 0;
10568
10569 if (eap->cmdidx == CMD_setlocal)
10570 flags = OPT_LOCAL;
10571 else if (eap->cmdidx == CMD_setglobal)
10572 flags = OPT_GLOBAL;
10573#if defined(FEAT_EVAL) && defined(FEAT_AUTOCMD) && defined(FEAT_BROWSE)
10574 if (cmdmod.browse && flags == 0)
10575 ex_options(eap);
10576 else
10577#endif
10578 (void)do_set(eap->arg, flags);
10579}
10580
10581#ifdef FEAT_SEARCH_EXTRA
10582/*
10583 * ":nohlsearch"
10584 */
10585/*ARGSUSED*/
10586 static void
10587ex_nohlsearch(eap)
10588 exarg_T *eap;
10589{
10590 no_hlsearch = TRUE;
10591 redraw_all_later(NOT_VALID);
10592}
10593
10594/*
Bram Moolenaare1438bb2006-03-01 22:01:55 +000010595 * ":[N]match {group} {pattern}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010596 * Sets nextcmd to the start of the next command, if any. Also called when
10597 * skipping commands to find the next command.
10598 */
10599 static void
10600ex_match(eap)
10601 exarg_T *eap;
10602{
10603 char_u *p;
10604 char_u *end;
10605 int c;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000010606 int mi;
10607
10608 if (eap->line2 <= 3)
10609 mi = eap->line2 - 1;
10610 else
10611 {
10612 EMSG(e_invcmd);
10613 return;
10614 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010615
10616 /* First clear any old pattern. */
10617 if (!eap->skip)
10618 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +000010619 vim_free(curwin->w_match[mi].regprog);
10620 curwin->w_match[mi].regprog = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010621 redraw_later(NOT_VALID); /* always need a redraw */
10622 }
10623
10624 if (ends_excmd(*eap->arg))
10625 end = eap->arg;
10626 else if ((STRNICMP(eap->arg, "none", 4) == 0
10627 && (vim_iswhite(eap->arg[4]) || ends_excmd(eap->arg[4]))))
10628 end = eap->arg + 4;
10629 else
10630 {
10631 p = skiptowhite(eap->arg);
10632 if (!eap->skip)
10633 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +000010634 curwin->w_match_id[mi] = syn_namen2id(eap->arg,
10635 (int)(p - eap->arg));
10636 if (curwin->w_match_id[mi] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010637 {
10638 EMSG2(_(e_nogroup), eap->arg);
10639 return;
10640 }
10641 }
10642 p = skipwhite(p);
10643 if (*p == NUL)
10644 {
10645 /* There must be two arguments. */
10646 EMSG2(_(e_invarg2), eap->arg);
10647 return;
10648 }
10649 end = skip_regexp(p + 1, *p, TRUE, NULL);
10650 if (!eap->skip)
10651 {
10652 if (*end != NUL && !ends_excmd(*skipwhite(end + 1)))
10653 {
10654 eap->errmsg = e_trailing;
10655 return;
10656 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000010657 if (*end != *p)
10658 {
10659 EMSG2(_(e_invarg2), p);
10660 return;
10661 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010662
10663 c = *end;
10664 *end = NUL;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000010665 curwin->w_match[mi].regprog = vim_regcomp(p + 1, RE_MAGIC);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010666 *end = c;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000010667 if (curwin->w_match[mi].regprog == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010668 {
10669 EMSG2(_(e_invarg2), p);
10670 return;
10671 }
10672 }
10673 }
10674 eap->nextcmd = find_nextcmd(end);
10675}
10676#endif
10677
10678#ifdef FEAT_CRYPT
10679/*
10680 * ":X": Get crypt key
10681 */
10682/*ARGSUSED*/
10683 static void
10684ex_X(eap)
10685 exarg_T *eap;
10686{
10687 (void)get_crypt_key(TRUE, TRUE);
10688}
10689#endif
10690
10691#ifdef FEAT_FOLDING
10692 static void
10693ex_fold(eap)
10694 exarg_T *eap;
10695{
10696 if (foldManualAllowed(TRUE))
10697 foldCreate(eap->line1, eap->line2);
10698}
10699
10700 static void
10701ex_foldopen(eap)
10702 exarg_T *eap;
10703{
10704 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
10705 eap->forceit, FALSE);
10706}
10707
10708 static void
10709ex_folddo(eap)
10710 exarg_T *eap;
10711{
10712 linenr_T lnum;
10713
10714 /* First set the marks for all lines closed/open. */
10715 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
10716 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
10717 ml_setmarked(lnum);
10718
10719 /* Execute the command on the marked lines. */
10720 global_exe(eap->arg);
10721 ml_clearmarked(); /* clear rest of the marks */
10722}
10723#endif