blob: 8323ca83ee938b25ab424dda3e58286e2ed21d7d [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 Moolenaarf71a3db2006-03-12 21:50:18 +0000239#endif
240#ifndef FEAT_SPELL
Bram Moolenaarb765d632005-06-07 21:00:02 +0000241# define ex_spell ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000242# define ex_mkspell ex_ni
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000243# define ex_spelldump ex_ni
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000244# define ex_spellinfo ex_ni
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000245# define ex_spellrepall ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000246#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000247#ifndef FEAT_MZSCHEME
248# define ex_mzscheme ex_script_ni
249# define ex_mzfile ex_ni
250#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000251#ifndef FEAT_PERL
252# define ex_perl ex_script_ni
253# define ex_perldo ex_ni
254#endif
255#ifndef FEAT_PYTHON
256# define ex_python ex_script_ni
257# define ex_pyfile ex_ni
258#endif
259#ifndef FEAT_TCL
260# define ex_tcl ex_script_ni
261# define ex_tcldo ex_ni
262# define ex_tclfile ex_ni
263#endif
264#ifndef FEAT_RUBY
265# define ex_ruby ex_script_ni
266# define ex_rubydo ex_ni
267# define ex_rubyfile ex_ni
268#endif
269#ifndef FEAT_SNIFF
270# define ex_sniff ex_ni
271#endif
272#ifndef FEAT_KEYMAP
273# define ex_loadkeymap ex_ni
274#endif
275static void ex_swapname __ARGS((exarg_T *eap));
276static void ex_syncbind __ARGS((exarg_T *eap));
277static void ex_read __ARGS((exarg_T *eap));
278static void ex_cd __ARGS((exarg_T *eap));
279static void ex_pwd __ARGS((exarg_T *eap));
280static void ex_equal __ARGS((exarg_T *eap));
281static void ex_sleep __ARGS((exarg_T *eap));
282static void do_exmap __ARGS((exarg_T *eap, int isabbrev));
283static void ex_winsize __ARGS((exarg_T *eap));
284#ifdef FEAT_WINDOWS
285static void ex_wincmd __ARGS((exarg_T *eap));
286#else
287# define ex_wincmd ex_ni
288#endif
Bram Moolenaar843ee412004-06-30 16:16:41 +0000289#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000290static void ex_winpos __ARGS((exarg_T *eap));
291#else
292# define ex_winpos ex_ni
293#endif
294static void ex_operators __ARGS((exarg_T *eap));
295static void ex_put __ARGS((exarg_T *eap));
296static void ex_copymove __ARGS((exarg_T *eap));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000297static void ex_may_print __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000298static void ex_submagic __ARGS((exarg_T *eap));
299static void ex_join __ARGS((exarg_T *eap));
300static void ex_at __ARGS((exarg_T *eap));
301static void ex_bang __ARGS((exarg_T *eap));
302static void ex_undo __ARGS((exarg_T *eap));
303static void ex_redo __ARGS((exarg_T *eap));
Bram Moolenaarc7d89352006-03-14 22:53:34 +0000304static void ex_later __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000305static void ex_redir __ARGS((exarg_T *eap));
306static void ex_redraw __ARGS((exarg_T *eap));
307static void ex_redrawstatus __ARGS((exarg_T *eap));
308static void close_redir __ARGS((void));
309static void ex_mkrc __ARGS((exarg_T *eap));
310static void ex_mark __ARGS((exarg_T *eap));
311#ifdef FEAT_USR_CMDS
312static char_u *uc_fun_cmd __ARGS((void));
Bram Moolenaar52b4b552005-03-07 23:00:57 +0000313static 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 +0000314#endif
315#ifdef FEAT_EX_EXTRA
316static void ex_normal __ARGS((exarg_T *eap));
317static void ex_startinsert __ARGS((exarg_T *eap));
318static void ex_stopinsert __ARGS((exarg_T *eap));
319#else
320# define ex_normal ex_ni
321# define ex_align ex_ni
322# define ex_retab ex_ni
323# define ex_startinsert ex_ni
324# define ex_stopinsert ex_ni
325# define ex_helptags ex_ni
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +0000326# define ex_sort ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000327#endif
328#ifdef FEAT_FIND_ID
329static void ex_checkpath __ARGS((exarg_T *eap));
330static void ex_findpat __ARGS((exarg_T *eap));
331#else
332# define ex_findpat ex_ni
333# define ex_checkpath ex_ni
334#endif
335#if defined(FEAT_FIND_ID) && defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
336static void ex_psearch __ARGS((exarg_T *eap));
337#else
338# define ex_psearch ex_ni
339#endif
340static void ex_tag __ARGS((exarg_T *eap));
341static void ex_tag_cmd __ARGS((exarg_T *eap, char_u *name));
342#ifndef FEAT_EVAL
343# define ex_scriptnames ex_ni
344# define ex_finish ex_ni
345# define ex_echo ex_ni
346# define ex_echohl ex_ni
347# define ex_execute ex_ni
348# define ex_call ex_ni
349# define ex_if ex_ni
350# define ex_endif ex_ni
351# define ex_else ex_ni
352# define ex_while ex_ni
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000353# define ex_for ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000354# define ex_continue ex_ni
355# define ex_break ex_ni
356# define ex_endwhile ex_ni
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000357# define ex_endfor ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000358# define ex_throw ex_ni
359# define ex_try ex_ni
360# define ex_catch ex_ni
361# define ex_finally ex_ni
362# define ex_endtry ex_ni
363# define ex_endfunction ex_ni
364# define ex_let ex_ni
365# define ex_unlet ex_ni
Bram Moolenaar65c1b012005-01-31 19:02:28 +0000366# define ex_lockvar ex_ni
367# define ex_unlockvar ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000368# define ex_function ex_ni
369# define ex_delfunction ex_ni
370# define ex_return ex_ni
371#endif
372static char_u *arg_all __ARGS((void));
373#ifdef FEAT_SESSION
374static int makeopens __ARGS((FILE *fd, char_u *dirnow));
375static int put_view __ARGS((FILE *fd, win_T *wp, int add_edit, unsigned *flagp));
376static void ex_loadview __ARGS((exarg_T *eap));
377static char_u *get_view_file __ARGS((int c));
378#else
379# define ex_loadview ex_ni
380#endif
381#ifndef FEAT_EVAL
382# define ex_compiler ex_ni
383#endif
384#ifdef FEAT_VIMINFO
385static void ex_viminfo __ARGS((exarg_T *eap));
386#else
387# define ex_viminfo ex_ni
388#endif
389static void ex_behave __ARGS((exarg_T *eap));
390#ifdef FEAT_AUTOCMD
391static void ex_filetype __ARGS((exarg_T *eap));
392static void ex_setfiletype __ARGS((exarg_T *eap));
393#else
394# define ex_filetype ex_ni
395# define ex_setfiletype ex_ni
396#endif
397#ifndef FEAT_DIFF
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000398# define ex_diffoff ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000399# define ex_diffpatch ex_ni
400# define ex_diffgetput ex_ni
401# define ex_diffsplit ex_ni
402# define ex_diffthis ex_ni
403# define ex_diffupdate ex_ni
404#endif
405static void ex_digraphs __ARGS((exarg_T *eap));
406static void ex_set __ARGS((exarg_T *eap));
407#if !defined(FEAT_EVAL) || !defined(FEAT_AUTOCMD)
408# define ex_options ex_ni
409#endif
410#ifdef FEAT_SEARCH_EXTRA
411static void ex_nohlsearch __ARGS((exarg_T *eap));
412static void ex_match __ARGS((exarg_T *eap));
413#else
414# define ex_nohlsearch ex_ni
415# define ex_match ex_ni
416#endif
417#ifdef FEAT_CRYPT
418static void ex_X __ARGS((exarg_T *eap));
419#else
420# define ex_X ex_ni
421#endif
422#ifdef FEAT_FOLDING
423static void ex_fold __ARGS((exarg_T *eap));
424static void ex_foldopen __ARGS((exarg_T *eap));
425static void ex_folddo __ARGS((exarg_T *eap));
426#else
427# define ex_fold ex_ni
428# define ex_foldopen ex_ni
429# define ex_folddo ex_ni
430#endif
431#if !((defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
432 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE)))
433# define ex_language ex_ni
434#endif
435#ifndef FEAT_SIGNS
436# define ex_sign ex_ni
437#endif
438#ifndef FEAT_SUN_WORKSHOP
439# define ex_wsverb ex_ni
440#endif
Bram Moolenaar009b2592004-10-24 19:18:58 +0000441#ifndef FEAT_NETBEANS_INTG
442# define ex_nbkey ex_ni
443#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000444
445#ifndef FEAT_EVAL
446# define ex_debug ex_ni
447# define ex_breakadd ex_ni
448# define ex_debuggreedy ex_ni
449# define ex_breakdel ex_ni
450# define ex_breaklist ex_ni
451#endif
452
453#ifndef FEAT_CMDHIST
454# define ex_history ex_ni
455#endif
456#ifndef FEAT_JUMPLIST
457# define ex_jumps ex_ni
458# define ex_changes ex_ni
459#endif
460
Bram Moolenaar05159a02005-02-26 23:04:13 +0000461#ifndef FEAT_PROFILE
462# define ex_profile ex_ni
463#endif
464
Bram Moolenaar071d4272004-06-13 20:20:40 +0000465/*
466 * Declare cmdnames[].
467 */
468#define DO_DECLARE_EXCMD
469#include "ex_cmds.h"
470
471/*
472 * Table used to quickly search for a command, based on its first character.
473 */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +0000474static cmdidx_T cmdidxs[27] =
Bram Moolenaar071d4272004-06-13 20:20:40 +0000475{
476 CMD_append,
477 CMD_buffer,
478 CMD_change,
479 CMD_delete,
480 CMD_edit,
481 CMD_file,
482 CMD_global,
483 CMD_help,
484 CMD_insert,
485 CMD_join,
486 CMD_k,
487 CMD_list,
488 CMD_move,
489 CMD_next,
490 CMD_open,
491 CMD_print,
492 CMD_quit,
493 CMD_read,
494 CMD_substitute,
495 CMD_t,
496 CMD_undo,
497 CMD_vglobal,
498 CMD_write,
499 CMD_xit,
500 CMD_yank,
501 CMD_z,
502 CMD_bang
503};
504
505static char_u dollar_command[2] = {'$', 0};
506
507
508#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000509/* Struct for storing a line inside a while/for loop */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000510typedef struct
511{
512 char_u *line; /* command line */
513 linenr_T lnum; /* sourcing_lnum of the line */
514} wcmd_T;
515
516/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000517 * Structure used to store info for line position in a while or for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000518 * This is required, because do_one_cmd() may invoke ex_function(), which
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000519 * reads more lines that may come from the while/for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000520 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000521struct loop_cookie
Bram Moolenaar071d4272004-06-13 20:20:40 +0000522{
523 garray_T *lines_gap; /* growarray with line info */
524 int current_line; /* last read line from growarray */
525 int repeating; /* TRUE when looping a second time */
526 /* When "repeating" is FALSE use "getline" and "cookie" to get lines */
527 char_u *(*getline) __ARGS((int, void *, int));
528 void *cookie;
529};
530
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000531static char_u *get_loop_line __ARGS((int c, void *cookie, int indent));
532static int store_loop_line __ARGS((garray_T *gap, char_u *line));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000533static void free_cmdlines __ARGS((garray_T *gap));
Bram Moolenaared203462004-06-16 11:19:22 +0000534
535/* Struct to save a few things while debugging. Used in do_cmdline() only. */
536struct dbg_stuff
537{
538 int trylevel;
539 int force_abort;
540 except_T *caught_stack;
541 char_u *vv_exception;
542 char_u *vv_throwpoint;
543 int did_emsg;
544 int got_int;
545 int did_throw;
546 int need_rethrow;
547 int check_cstack;
548 except_T *current_exception;
549};
550
551static void save_dbg_stuff __ARGS((struct dbg_stuff *dsp));
552static void restore_dbg_stuff __ARGS((struct dbg_stuff *dsp));
553
554 static void
555save_dbg_stuff(dsp)
556 struct dbg_stuff *dsp;
557{
558 dsp->trylevel = trylevel; trylevel = 0;
559 dsp->force_abort = force_abort; force_abort = FALSE;
560 dsp->caught_stack = caught_stack; caught_stack = NULL;
561 dsp->vv_exception = v_exception(NULL);
562 dsp->vv_throwpoint = v_throwpoint(NULL);
563
564 /* Necessary for debugging an inactive ":catch", ":finally", ":endtry" */
565 dsp->did_emsg = did_emsg; did_emsg = FALSE;
566 dsp->got_int = got_int; got_int = FALSE;
567 dsp->did_throw = did_throw; did_throw = FALSE;
568 dsp->need_rethrow = need_rethrow; need_rethrow = FALSE;
569 dsp->check_cstack = check_cstack; check_cstack = FALSE;
570 dsp->current_exception = current_exception; current_exception = NULL;
571}
572
573 static void
574restore_dbg_stuff(dsp)
575 struct dbg_stuff *dsp;
576{
577 suppress_errthrow = FALSE;
578 trylevel = dsp->trylevel;
579 force_abort = dsp->force_abort;
580 caught_stack = dsp->caught_stack;
581 (void)v_exception(dsp->vv_exception);
582 (void)v_throwpoint(dsp->vv_throwpoint);
583 did_emsg = dsp->did_emsg;
584 got_int = dsp->got_int;
585 did_throw = dsp->did_throw;
586 need_rethrow = dsp->need_rethrow;
587 check_cstack = dsp->check_cstack;
588 current_exception = dsp->current_exception;
589}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000590#endif
591
592
593/*
594 * do_exmode(): Repeatedly get commands for the "Ex" mode, until the ":vi"
595 * command is given.
596 */
597 void
598do_exmode(improved)
599 int improved; /* TRUE for "improved Ex" mode */
600{
601 int save_msg_scroll;
602 int prev_msg_row;
603 linenr_T prev_line;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000604 int changedtick;
605
606 if (improved)
607 exmode_active = EXMODE_VIM;
608 else
609 exmode_active = EXMODE_NORMAL;
610 State = NORMAL;
611
612 /* When using ":global /pat/ visual" and then "Q" we return to continue
613 * the :global command. */
614 if (global_busy)
615 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000616
617 save_msg_scroll = msg_scroll;
618 ++RedrawingDisabled; /* don't redisplay the window */
619 ++no_wait_return; /* don't wait for return */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620#ifdef FEAT_GUI
621 /* Ignore scrollbar and mouse events in Ex mode */
622 ++hold_gui_events;
623#endif
624#ifdef FEAT_SNIFF
625 want_sniff_request = 0; /* No K_SNIFF wanted */
626#endif
627
628 MSG(_("Entering Ex mode. Type \"visual\" to go to Normal mode."));
629 while (exmode_active)
630 {
Bram Moolenaar7c626922005-02-07 22:01:03 +0000631#ifdef FEAT_EX_EXTRA
632 /* Check for a ":normal" command and no more characters left. */
633 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
634 {
635 exmode_active = FALSE;
636 break;
637 }
638#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000639 msg_scroll = TRUE;
640 need_wait_return = FALSE;
641 ex_pressedreturn = FALSE;
642 ex_no_reprint = FALSE;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000643 changedtick = curbuf->b_changedtick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000644 prev_msg_row = msg_row;
645 prev_line = curwin->w_cursor.lnum;
646#ifdef FEAT_SNIFF
647 ProcessSniffRequests();
648#endif
649 if (improved)
650 {
651 cmdline_row = msg_row;
652 do_cmdline(NULL, getexline, NULL, 0);
653 }
654 else
655 do_cmdline(NULL, getexmodeline, NULL, DOCMD_NOWAIT);
656 lines_left = Rows - 1;
657
Bram Moolenaardf177f62005-02-22 08:39:57 +0000658 if ((prev_line != curwin->w_cursor.lnum
659 || changedtick != curbuf->b_changedtick) && !ex_no_reprint)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000660 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000661 if (curbuf->b_ml.ml_flags & ML_EMPTY)
662 EMSG(_(e_emptybuf));
663 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000664 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000665 if (ex_pressedreturn)
666 {
667 /* go up one line, to overwrite the ":<CR>" line, so the
668 * output doensn't contain empty lines. */
669 msg_row = prev_msg_row;
670 if (prev_msg_row == Rows - 1)
671 msg_row--;
672 }
673 msg_col = 0;
674 print_line_no_prefix(curwin->w_cursor.lnum, FALSE, FALSE);
675 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000676 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677 }
Bram Moolenaardf177f62005-02-22 08:39:57 +0000678 else if (ex_pressedreturn && !ex_no_reprint) /* must be at EOF */
679 {
680 if (curbuf->b_ml.ml_flags & ML_EMPTY)
681 EMSG(_(e_emptybuf));
682 else
683 EMSG(_("E501: At end-of-file"));
684 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000685 }
686
687#ifdef FEAT_GUI
688 --hold_gui_events;
689#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690 --RedrawingDisabled;
691 --no_wait_return;
692 update_screen(CLEAR);
693 need_wait_return = FALSE;
694 msg_scroll = save_msg_scroll;
695}
696
697/*
698 * Execute a simple command line. Used for translated commands like "*".
699 */
700 int
701do_cmdline_cmd(cmd)
702 char_u *cmd;
703{
704 return do_cmdline(cmd, NULL, NULL,
705 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
706}
707
708/*
709 * do_cmdline(): execute one Ex command line
710 *
711 * 1. Execute "cmdline" when it is not NULL.
712 * If "cmdline" is NULL, or more lines are needed, getline() is used.
713 * 2. Split up in parts separated with '|'.
714 *
715 * This function can be called recursively!
716 *
717 * flags:
718 * DOCMD_VERBOSE - The command will be included in the error message.
719 * DOCMD_NOWAIT - Don't call wait_return() and friends.
720 * DOCMD_REPEAT - Repeat execution until getline() returns NULL.
721 * DOCMD_KEYTYPED - Don't reset KeyTyped.
722 * DOCMD_EXCRESET - Reset the exception environment (used for debugging).
723 * DOCMD_KEEPLINE - Store first typed line (for repeating with ".").
724 *
725 * return FAIL if cmdline could not be executed, OK otherwise
726 */
727 int
728do_cmdline(cmdline, getline, cookie, flags)
729 char_u *cmdline;
730 char_u *(*getline) __ARGS((int, void *, int));
731 void *cookie; /* argument for getline() */
732 int flags;
733{
734 char_u *next_cmdline; /* next cmd to execute */
735 char_u *cmdline_copy = NULL; /* copy of cmd line */
736 int used_getline = FALSE; /* used "getline" to obtain command */
737 static int recursive = 0; /* recursive depth */
738 int msg_didout_before_start = 0;
739 int count = 0; /* line number count */
740 int did_inc = FALSE; /* incremented RedrawingDisabled */
741 int retval = OK;
742#ifdef FEAT_EVAL
743 struct condstack cstack; /* conditional stack */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000744 garray_T lines_ga; /* keep lines for ":while"/":for" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000745 int current_line = 0; /* active line in lines_ga */
746 char_u *fname = NULL; /* function or script name */
747 linenr_T *breakpoint = NULL; /* ptr to breakpoint field in cookie */
748 int *dbg_tick = NULL; /* ptr to dbg_tick field in cookie */
Bram Moolenaared203462004-06-16 11:19:22 +0000749 struct dbg_stuff debug_saved; /* saved things for debug mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750 int initial_trylevel;
751 struct msglist **saved_msg_list = NULL;
752 struct msglist *private_msg_list;
753
754 /* "getline" and "cookie" passed to do_one_cmd() */
755 char_u *(*cmd_getline) __ARGS((int, void *, int));
756 void *cmd_cookie;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000757 struct loop_cookie cmd_loop_cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000758 void *real_cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000759 int getline_is_func;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000760#else
761# define cmd_getline getline
762# define cmd_cookie cookie
763#endif
764 static int call_depth = 0; /* recursiveness */
765
766#ifdef FEAT_EVAL
767 /* For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory
768 * location for storing error messages to be converted to an exception.
Bram Moolenaarcf3630f2005-01-08 16:04:29 +0000769 * This ensures that the do_errthrow() call in do_one_cmd() does not
770 * combine the messages stored by an earlier invocation of do_one_cmd()
771 * with the command name of the later one. This would happen when
772 * BufWritePost autocommands are executed after a write error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773 saved_msg_list = msg_list;
774 msg_list = &private_msg_list;
775 private_msg_list = NULL;
776#endif
777
778 /* It's possible to create an endless loop with ":execute", catch that
779 * here. The value of 200 allows nested function calls, ":source", etc. */
780 if (call_depth == 200)
781 {
782 EMSG(_("E169: Command too recursive"));
783#ifdef FEAT_EVAL
784 /* When converting to an exception, we do not include the command name
785 * since this is not an error of the specific command. */
786 do_errthrow((struct condstack *)NULL, (char_u *)NULL);
787 msg_list = saved_msg_list;
788#endif
789 return FAIL;
790 }
791 ++call_depth;
792
793#ifdef FEAT_EVAL
794 cstack.cs_idx = -1;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000795 cstack.cs_looplevel = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796 cstack.cs_trylevel = 0;
797 cstack.cs_emsg_silent_list = NULL;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000798 cstack.cs_lflags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799 ga_init2(&lines_ga, (int)sizeof(wcmd_T), 10);
800
801 real_cookie = getline_cookie(getline, cookie);
802
803 /* Inside a function use a higher nesting level. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000804 getline_is_func = getline_equal(getline, cookie, get_func_line);
805 if (getline_is_func && ex_nesting_level == func_level(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806 ++ex_nesting_level;
807
808 /* Get the function or script name and the address where the next breakpoint
809 * line and the debug tick for a function or script are stored. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000810 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811 {
812 fname = func_name(real_cookie);
813 breakpoint = func_breakpoint(real_cookie);
814 dbg_tick = func_dbg_tick(real_cookie);
815 }
816 else if (getline_equal(getline, cookie, getsourceline))
817 {
818 fname = sourcing_name;
819 breakpoint = source_breakpoint(real_cookie);
820 dbg_tick = source_dbg_tick(real_cookie);
821 }
822
823 /*
824 * Initialize "force_abort" and "suppress_errthrow" at the top level.
825 */
826 if (!recursive)
827 {
828 force_abort = FALSE;
829 suppress_errthrow = FALSE;
830 }
831
832 /*
833 * If requested, store and reset the global values controlling the
834 * exception handling (used when debugging).
835 */
836 else if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +0000837 save_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000838
839 initial_trylevel = trylevel;
840
841 /*
842 * "did_throw" will be set to TRUE when an exception is being thrown.
843 */
844 did_throw = FALSE;
845#endif
846 /*
847 * "did_emsg" will be set to TRUE when emsg() is used, in which case we
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000848 * cancel the whole command line, and any if/endif or loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849 * If force_abort is set, we cancel everything.
850 */
851 did_emsg = FALSE;
852
853 /*
854 * KeyTyped is only set when calling vgetc(). Reset it here when not
855 * calling vgetc() (sourced command lines).
856 */
857 if (!(flags & DOCMD_KEYTYPED) && !getline_equal(getline, cookie, getexline))
858 KeyTyped = FALSE;
859
860 /*
861 * Continue executing command lines:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000862 * - when inside an ":if", ":while" or ":for"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000863 * - for multiple commands on one line, separated with '|'
864 * - when repeating until there are no more lines (for ":source")
865 */
866 next_cmdline = cmdline;
867 do
868 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000869#ifdef FEAT_EVAL
870 getline_is_func = getline_equal(getline, cookie, get_func_line);
871#endif
872
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000873 /* stop skipping cmds for an error msg after all endif/while/for */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874 if (next_cmdline == NULL
875#ifdef FEAT_EVAL
876 && !force_abort
877 && cstack.cs_idx < 0
Bram Moolenaar05159a02005-02-26 23:04:13 +0000878 && !(getline_is_func && func_has_abort(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879#endif
880 )
881 did_emsg = FALSE;
882
883 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000884 * 1. If repeating a line in a loop, get a line from lines_ga.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885 * 2. If no line given: Get an allocated line with getline().
886 * 3. If a line is given: Make a copy, so we can mess with it.
887 */
888
889#ifdef FEAT_EVAL
890 /* 1. If repeating, get a previous line from lines_ga. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000891 if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892 {
893 /* Each '|' separated command is stored separately in lines_ga, to
894 * be able to jump to it. Don't use next_cmdline now. */
895 vim_free(cmdline_copy);
896 cmdline_copy = NULL;
897
898 /* Check if a function has returned or, unless it has an unclosed
899 * try conditional, aborted. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000900 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000902# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000903 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000904 func_line_end(real_cookie);
905# endif
906 if (func_has_ended(real_cookie))
907 {
908 retval = FAIL;
909 break;
910 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000911 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000912#ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000913 else if (do_profiling == PROF_YES
Bram Moolenaar05159a02005-02-26 23:04:13 +0000914 && getline_equal(getline, cookie, getsourceline))
915 script_line_end();
916#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000917
918 /* Check if a sourced file hit a ":finish" command. */
919 if (source_finished(getline, cookie))
920 {
921 retval = FAIL;
922 break;
923 }
924
925 /* If breakpoints have been added/deleted need to check for it. */
926 if (breakpoint != NULL && dbg_tick != NULL
927 && *dbg_tick != debug_tick)
928 {
929 *breakpoint = dbg_find_breakpoint(
930 getline_equal(getline, cookie, getsourceline),
931 fname, sourcing_lnum);
932 *dbg_tick = debug_tick;
933 }
934
935 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
936 sourcing_lnum = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
937
938 /* Did we encounter a breakpoint? */
939 if (breakpoint != NULL && *breakpoint != 0
940 && *breakpoint <= sourcing_lnum)
941 {
942 dbg_breakpoint(fname, sourcing_lnum);
943 /* Find next breakpoint. */
944 *breakpoint = dbg_find_breakpoint(
945 getline_equal(getline, cookie, getsourceline),
946 fname, sourcing_lnum);
947 *dbg_tick = debug_tick;
948 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000949# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000950 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000951 {
952 if (getline_is_func)
953 func_line_start(real_cookie);
954 else if (getline_equal(getline, cookie, getsourceline))
955 script_line_start();
956 }
957# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000958 }
959
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000960 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000961 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000962 /* Inside a while/for loop we need to store the lines and use them
Bram Moolenaar071d4272004-06-13 20:20:40 +0000963 * again. Pass a different "getline" function to do_one_cmd()
964 * below, so that it stores lines in or reads them from
965 * "lines_ga". Makes it possible to define a function inside a
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000966 * while/for loop. */
967 cmd_getline = get_loop_line;
968 cmd_cookie = (void *)&cmd_loop_cookie;
969 cmd_loop_cookie.lines_gap = &lines_ga;
970 cmd_loop_cookie.current_line = current_line;
971 cmd_loop_cookie.getline = getline;
972 cmd_loop_cookie.cookie = cookie;
973 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000974 }
975 else
976 {
977 cmd_getline = getline;
978 cmd_cookie = cookie;
979 }
980#endif
981
982 /* 2. If no line given, get an allocated line with getline(). */
983 if (next_cmdline == NULL)
984 {
985 /*
986 * Need to set msg_didout for the first line after an ":if",
987 * otherwise the ":if" will be overwritten.
988 */
989 if (count == 1 && getline_equal(getline, cookie, getexline))
990 msg_didout = TRUE;
991 if (getline == NULL || (next_cmdline = getline(':', cookie,
992#ifdef FEAT_EVAL
993 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
994#else
995 0
996#endif
997 )) == NULL)
998 {
999 /* Don't call wait_return for aborted command line. The NULL
1000 * returned for the end of a sourced file or executed function
1001 * doesn't do this. */
1002 if (KeyTyped && !(flags & DOCMD_REPEAT))
1003 need_wait_return = FALSE;
1004 retval = FAIL;
1005 break;
1006 }
1007 used_getline = TRUE;
1008
1009 /*
1010 * Keep the first typed line. Clear it when more lines are typed.
1011 */
1012 if (flags & DOCMD_KEEPLINE)
1013 {
1014 vim_free(repeat_cmdline);
1015 if (count == 0)
1016 repeat_cmdline = vim_strsave(next_cmdline);
1017 else
1018 repeat_cmdline = NULL;
1019 }
1020 }
1021
1022 /* 3. Make a copy of the command so we can mess with it. */
1023 else if (cmdline_copy == NULL)
1024 {
1025 next_cmdline = vim_strsave(next_cmdline);
1026 if (next_cmdline == NULL)
1027 {
1028 EMSG(_(e_outofmem));
1029 retval = FAIL;
1030 break;
1031 }
1032 }
1033 cmdline_copy = next_cmdline;
1034
1035#ifdef FEAT_EVAL
1036 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001037 * Save the current line when inside a ":while" or ":for", and when
1038 * the command looks like a ":while" or ":for", because we may need it
1039 * later. When there is a '|' and another command, it is stored
1040 * separately, because we need to be able to jump back to it from an
1041 * :endwhile/:endfor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001043 if (current_line == lines_ga.ga_len
1044 && (cstack.cs_looplevel || has_loop_cmd(next_cmdline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001046 if (store_loop_line(&lines_ga, next_cmdline) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047 {
1048 retval = FAIL;
1049 break;
1050 }
1051 }
1052 did_endif = FALSE;
1053#endif
1054
1055 if (count++ == 0)
1056 {
1057 /*
1058 * All output from the commands is put below each other, without
1059 * waiting for a return. Don't do this when executing commands
1060 * from a script or when being called recursive (e.g. for ":e
1061 * +command file").
1062 */
1063 if (!(flags & DOCMD_NOWAIT) && !recursive)
1064 {
1065 msg_didout_before_start = msg_didout;
1066 msg_didany = FALSE; /* no output yet */
1067 msg_start();
1068 msg_scroll = TRUE; /* put messages below each other */
1069 ++no_wait_return; /* dont wait for return until finished */
1070 ++RedrawingDisabled;
1071 did_inc = TRUE;
1072 }
1073 }
1074
1075 if (p_verbose >= 15 && sourcing_name != NULL)
1076 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077 ++no_wait_return;
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001078 verbose_enter_scroll();
1079
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080 smsg((char_u *)_("line %ld: %s"),
1081 (long)sourcing_lnum, cmdline_copy);
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001082 if (msg_silent == 0)
1083 msg_puts((char_u *)"\n"); /* don't overwrite this */
1084
1085 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001086 --no_wait_return;
1087 }
1088
1089 /*
1090 * 2. Execute one '|' separated command.
1091 * do_one_cmd() will return NULL if there is no trailing '|'.
1092 * "cmdline_copy" can change, e.g. for '%' and '#' expansion.
1093 */
1094 ++recursive;
1095 next_cmdline = do_one_cmd(&cmdline_copy, flags & DOCMD_VERBOSE,
1096#ifdef FEAT_EVAL
1097 &cstack,
1098#endif
1099 cmd_getline, cmd_cookie);
1100 --recursive;
1101
1102#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001103 if (cmd_cookie == (void *)&cmd_loop_cookie)
1104 /* Use "current_line" from "cmd_loop_cookie", it may have been
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105 * incremented when defining a function. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001106 current_line = cmd_loop_cookie.current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001107#endif
1108
1109 if (next_cmdline == NULL)
1110 {
1111 vim_free(cmdline_copy);
1112 cmdline_copy = NULL;
1113#ifdef FEAT_CMDHIST
1114 /*
1115 * If the command was typed, remember it for the ':' register.
1116 * Do this AFTER executing the command to make :@: work.
1117 */
1118 if (getline_equal(getline, cookie, getexline)
1119 && new_last_cmdline != NULL)
1120 {
1121 vim_free(last_cmdline);
1122 last_cmdline = new_last_cmdline;
1123 new_last_cmdline = NULL;
1124 }
1125#endif
1126 }
1127 else
1128 {
1129 /* need to copy the command after the '|' to cmdline_copy, for the
1130 * next do_one_cmd() */
1131 mch_memmove(cmdline_copy, next_cmdline, STRLEN(next_cmdline) + 1);
1132 next_cmdline = cmdline_copy;
1133 }
1134
1135
1136#ifdef FEAT_EVAL
1137 /* reset did_emsg for a function that is not aborted by an error */
1138 if (did_emsg && !force_abort
1139 && getline_equal(getline, cookie, get_func_line)
1140 && !func_has_abort(real_cookie))
1141 did_emsg = FALSE;
1142
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001143 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144 {
1145 ++current_line;
1146
1147 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001148 * An ":endwhile", ":endfor" and ":continue" is handled here.
1149 * If we were executing commands, jump back to the ":while" or
1150 * ":for".
1151 * If we were not executing commands, decrement cs_looplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001153 if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001155 cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001157 /* Jump back to the matching ":while" or ":for". Be careful
1158 * not to use a cs_line[] from an entry that isn't a ":while"
1159 * or ":for": It would make "current_line" invalid and can
1160 * cause a crash. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001161 if (!did_emsg && !got_int && !did_throw
1162 && cstack.cs_idx >= 0
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001163 && (cstack.cs_flags[cstack.cs_idx]
1164 & (CSF_WHILE | CSF_FOR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165 && cstack.cs_line[cstack.cs_idx] >= 0
1166 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
1167 {
1168 current_line = cstack.cs_line[cstack.cs_idx];
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001169 /* remember we jumped there */
1170 cstack.cs_lflags |= CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171 line_breakcheck(); /* check if CTRL-C typed */
1172
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001173 /* Check for the next breakpoint at or after the ":while"
1174 * or ":for". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175 if (breakpoint != NULL)
1176 {
1177 *breakpoint = dbg_find_breakpoint(
1178 getline_equal(getline, cookie, getsourceline),
1179 fname,
1180 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
1181 *dbg_tick = debug_tick;
1182 }
1183 }
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001184 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001186 /* can only get here with ":endwhile" or ":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001187 if (cstack.cs_idx >= 0)
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001188 rewind_conditionals(&cstack, cstack.cs_idx - 1,
1189 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190 }
1191 }
1192
1193 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001194 * For a ":while" or ":for" we need to remember the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001196 else if (cstack.cs_lflags & CSL_HAD_LOOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001198 cstack.cs_lflags &= ~CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199 cstack.cs_line[cstack.cs_idx] = current_line - 1;
1200 }
1201 }
1202
1203 /*
1204 * When not inside any ":while" loop, clear remembered lines.
1205 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001206 if (cstack.cs_looplevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207 {
1208 if (lines_ga.ga_len > 0)
1209 {
1210 sourcing_lnum =
1211 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
1212 free_cmdlines(&lines_ga);
1213 }
1214 current_line = 0;
1215 }
1216
1217 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001218 * A ":finally" makes did_emsg, got_int, and did_throw pending for
1219 * being restored at the ":endtry". Reset them here and set the
1220 * ACTIVE and FINALLY flags, so that the finally clause gets executed.
1221 * This includes the case where a missing ":endif", ":endwhile" or
1222 * ":endfor" was detected by the ":finally" itself.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001224 if (cstack.cs_lflags & CSL_HAD_FINA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001226 cstack.cs_lflags &= ~CSL_HAD_FINA;
1227 report_make_pending(cstack.cs_pending[cstack.cs_idx]
1228 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001229 did_throw ? (void *)current_exception : NULL);
1230 did_emsg = got_int = did_throw = FALSE;
1231 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
1232 }
1233
1234 /* Update global "trylevel" for recursive calls to do_cmdline() from
1235 * within this loop. */
1236 trylevel = initial_trylevel + cstack.cs_trylevel;
1237
1238 /*
1239 * If the outermost try conditional (accross function calls and sourced
1240 * files) is aborted because of an error, an interrupt, or an uncaught
1241 * exception, cancel everything. If it is left normally, reset
1242 * force_abort to get the non-EH compatible abortion behavior for
1243 * the rest of the script.
1244 */
1245 if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
1246 force_abort = FALSE;
1247
1248 /* Convert an interrupt to an exception if appropriate. */
1249 (void)do_intthrow(&cstack);
1250#endif /* FEAT_EVAL */
1251
1252 }
1253 /*
1254 * Continue executing command lines when:
1255 * - no CTRL-C typed, no aborting error, no exception thrown or try
1256 * conditionals need to be checked for executing finally clauses or
1257 * catching an interrupt exception
1258 * - didn't get an error message or lines are not typed
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001259 * - there is a command after '|', inside a :if, :while, :for or :try, or
Bram Moolenaar071d4272004-06-13 20:20:40 +00001260 * looping for ":source" command or function call.
1261 */
1262 while (!((got_int
1263#ifdef FEAT_EVAL
1264 || (did_emsg && force_abort) || did_throw
1265#endif
1266 )
1267#ifdef FEAT_EVAL
1268 && cstack.cs_trylevel == 0
1269#endif
1270 )
1271 && !(did_emsg && used_getline
1272 && (getline_equal(getline, cookie, getexmodeline)
1273 || getline_equal(getline, cookie, getexline)))
1274 && (next_cmdline != NULL
1275#ifdef FEAT_EVAL
1276 || cstack.cs_idx >= 0
1277#endif
1278 || (flags & DOCMD_REPEAT)));
1279
1280 vim_free(cmdline_copy);
1281#ifdef FEAT_EVAL
1282 free_cmdlines(&lines_ga);
1283 ga_clear(&lines_ga);
1284
1285 if (cstack.cs_idx >= 0)
1286 {
1287 /*
1288 * If a sourced file or executed function ran to its end, report the
1289 * unclosed conditional.
1290 */
1291 if (!got_int && !did_throw
1292 && ((getline_equal(getline, cookie, getsourceline)
1293 && !source_finished(getline, cookie))
1294 || (getline_equal(getline, cookie, get_func_line)
1295 && !func_has_ended(real_cookie))))
1296 {
1297 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
1298 EMSG(_(e_endtry));
1299 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
1300 EMSG(_(e_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001301 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
1302 EMSG(_(e_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303 else
1304 EMSG(_(e_endif));
1305 }
1306
1307 /*
1308 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1309 * ":endtry" in a sourced file or executed function. If the try
1310 * conditional is in its finally clause, ignore anything pending.
1311 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001312 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313 */
1314 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001315 {
1316 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1317
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001318 if (idx >= 0)
1319 --idx; /* remove try block not in its finally clause */
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001320 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1321 &cstack.cs_looplevel);
1322 }
1323 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001324 trylevel = initial_trylevel;
1325 }
1326
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001327 /* If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1328 * lack was reported above and the error message is to be converted to an
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329 * exception, do this now after rewinding the cstack. */
1330 do_errthrow(&cstack, getline_equal(getline, cookie, get_func_line)
1331 ? (char_u *)"endfunction" : (char_u *)NULL);
1332
1333 if (trylevel == 0)
1334 {
1335 /*
1336 * When an exception is being thrown out of the outermost try
1337 * conditional, discard the uncaught exception, disable the conversion
1338 * of interrupts or errors to exceptions, and ensure that no more
1339 * commands are executed.
1340 */
1341 if (did_throw)
1342 {
1343 void *p = NULL;
1344 char_u *saved_sourcing_name;
1345 int saved_sourcing_lnum;
1346 struct msglist *messages = NULL, *next;
1347
1348 /*
1349 * If the uncaught exception is a user exception, report it as an
1350 * error. If it is an error exception, display the saved error
1351 * message now. For an interrupt exception, do nothing; the
1352 * interrupt message is given elsewhere.
1353 */
1354 switch (current_exception->type)
1355 {
1356 case ET_USER:
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001357 vim_snprintf((char *)IObuff, IOSIZE,
1358 _("E605: Exception not caught: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359 current_exception->value);
1360 p = vim_strsave(IObuff);
1361 break;
1362 case ET_ERROR:
1363 messages = current_exception->messages;
1364 current_exception->messages = NULL;
1365 break;
1366 case ET_INTERRUPT:
1367 break;
1368 default:
1369 p = vim_strsave((char_u *)_(e_internal));
1370 }
1371
1372 saved_sourcing_name = sourcing_name;
1373 saved_sourcing_lnum = sourcing_lnum;
1374 sourcing_name = current_exception->throw_name;
1375 sourcing_lnum = current_exception->throw_lnum;
1376 current_exception->throw_name = NULL;
1377
1378 discard_current_exception(); /* uses IObuff if 'verbose' */
1379 suppress_errthrow = TRUE;
1380 force_abort = TRUE;
1381
1382 if (messages != NULL)
1383 {
1384 do
1385 {
1386 next = messages->next;
1387 emsg(messages->msg);
1388 vim_free(messages->msg);
1389 vim_free(messages);
1390 messages = next;
1391 }
1392 while (messages != NULL);
1393 }
1394 else if (p != NULL)
1395 {
1396 emsg(p);
1397 vim_free(p);
1398 }
1399 vim_free(sourcing_name);
1400 sourcing_name = saved_sourcing_name;
1401 sourcing_lnum = saved_sourcing_lnum;
1402 }
1403
1404 /*
1405 * On an interrupt or an aborting error not converted to an exception,
1406 * disable the conversion of errors to exceptions. (Interrupts are not
1407 * converted any more, here.) This enables also the interrupt message
1408 * when force_abort is set and did_emsg unset in case of an interrupt
1409 * from a finally clause after an error.
1410 */
1411 else if (got_int || (did_emsg && force_abort))
1412 suppress_errthrow = TRUE;
1413 }
1414
1415 /*
1416 * The current cstack will be freed when do_cmdline() returns. An uncaught
1417 * exception will have to be rethrown in the previous cstack. If a function
1418 * has just returned or a script file was just finished and the previous
1419 * cstack belongs to the same function or, respectively, script file, it
1420 * will have to be checked for finally clauses to be executed due to the
1421 * ":return" or ":finish". This is done in do_one_cmd().
1422 */
1423 if (did_throw)
1424 need_rethrow = TRUE;
1425 if ((getline_equal(getline, cookie, getsourceline)
1426 && ex_nesting_level > source_level(real_cookie))
1427 || (getline_equal(getline, cookie, get_func_line)
1428 && ex_nesting_level > func_level(real_cookie) + 1))
1429 {
1430 if (!did_throw)
1431 check_cstack = TRUE;
1432 }
1433 else
1434 {
1435 /* When leaving a function, reduce nesting level. */
1436 if (getline_equal(getline, cookie, get_func_line))
1437 --ex_nesting_level;
1438 /*
1439 * Go to debug mode when returning from a function in which we are
1440 * single-stepping.
1441 */
1442 if ((getline_equal(getline, cookie, getsourceline)
1443 || getline_equal(getline, cookie, get_func_line))
1444 && ex_nesting_level + 1 <= debug_break_level)
1445 do_debug(getline_equal(getline, cookie, getsourceline)
1446 ? (char_u *)_("End of sourced file")
1447 : (char_u *)_("End of function"));
1448 }
1449
1450 /*
1451 * Restore the exception environment (done after returning from the
1452 * debugger).
1453 */
1454 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001455 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001456
1457 msg_list = saved_msg_list;
1458#endif /* FEAT_EVAL */
1459
1460 /*
1461 * If there was too much output to fit on the command line, ask the user to
1462 * hit return before redrawing the screen. With the ":global" command we do
1463 * this only once after the command is finished.
1464 */
1465 if (did_inc)
1466 {
1467 --RedrawingDisabled;
1468 --no_wait_return;
1469 msg_scroll = FALSE;
1470
1471 /*
1472 * When just finished an ":if"-":else" which was typed, no need to
1473 * wait for hit-return. Also for an error situation.
1474 */
1475 if (retval == FAIL
1476#ifdef FEAT_EVAL
1477 || (did_endif && KeyTyped && !did_emsg)
1478#endif
1479 )
1480 {
1481 need_wait_return = FALSE;
1482 msg_didany = FALSE; /* don't wait when restarting edit */
1483 }
1484 else if (need_wait_return)
1485 {
1486 /*
1487 * The msg_start() above clears msg_didout. The wait_return we do
1488 * here should not overwrite the command that may be shown before
1489 * doing that.
1490 */
1491 msg_didout |= msg_didout_before_start;
1492 wait_return(FALSE);
1493 }
1494 }
1495
1496#ifndef FEAT_EVAL
1497 /*
1498 * Reset if_level, in case a sourced script file contains more ":if" than
1499 * ":endif" (could be ":if x | foo | endif").
1500 */
1501 if_level = 0;
1502#endif
1503
1504 --call_depth;
1505 return retval;
1506}
1507
1508#ifdef FEAT_EVAL
1509/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001510 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511 */
1512 static char_u *
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001513get_loop_line(c, cookie, indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001514 int c;
1515 void *cookie;
1516 int indent;
1517{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001518 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001519 wcmd_T *wp;
1520 char_u *line;
1521
1522 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1523 {
1524 if (cp->repeating)
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001525 return NULL; /* trying to read past ":endwhile"/":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001526
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001527 /* First time inside the ":while"/":for": get line normally. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001528 if (cp->getline == NULL)
1529 line = getcmdline(c, 0L, indent);
1530 else
1531 line = cp->getline(c, cp->cookie, indent);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001532 if (store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001533 ++cp->current_line;
1534
1535 return line;
1536 }
1537
1538 KeyTyped = FALSE;
1539 ++cp->current_line;
1540 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
1541 sourcing_lnum = wp->lnum;
1542 return vim_strsave(wp->line);
1543}
1544
1545/*
1546 * Store a line in "gap" so that a ":while" loop can execute it again.
1547 */
1548 static int
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001549store_loop_line(gap, line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001550 garray_T *gap;
1551 char_u *line;
1552{
1553 if (ga_grow(gap, 1) == FAIL)
1554 return FAIL;
1555 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
1556 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = sourcing_lnum;
1557 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001558 return OK;
1559}
1560
1561/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001562 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563 */
1564 static void
1565free_cmdlines(gap)
1566 garray_T *gap;
1567{
1568 while (gap->ga_len > 0)
1569 {
1570 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1571 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001572 }
1573}
1574#endif
1575
1576/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001577 * If "getline" is get_loop_line(), return TRUE if the getline it uses equals
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578 * "func". * Otherwise return TRUE when "getline" equals "func".
1579 */
1580/*ARGSUSED*/
1581 int
1582getline_equal(getline, cookie, func)
1583 char_u *(*getline) __ARGS((int, void *, int));
1584 void *cookie; /* argument for getline() */
1585 char_u *(*func) __ARGS((int, void *, int));
1586{
1587#ifdef FEAT_EVAL
1588 char_u *(*gp) __ARGS((int, void *, int));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001589 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001591 /* When "getline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592 * function that's orignally used to obtain the lines. This may be nested
1593 * several levels. */
1594 gp = getline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001595 cp = (struct loop_cookie *)cookie;
1596 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597 {
1598 gp = cp->getline;
1599 cp = cp->cookie;
1600 }
1601 return gp == func;
1602#else
1603 return getline == func;
1604#endif
1605}
1606
1607#if defined(FEAT_EVAL) || defined(FEAT_MBYTE) || defined(PROTO)
1608/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001609 * If "getline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610 * getline function. Otherwise return "cookie".
1611 */
1612/*ARGSUSED*/
1613 void *
1614getline_cookie(getline, cookie)
1615 char_u *(*getline) __ARGS((int, void *, int));
1616 void *cookie; /* argument for getline() */
1617{
1618# ifdef FEAT_EVAL
1619 char_u *(*gp) __ARGS((int, void *, int));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001620 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001622 /* When "getline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623 * cookie that's orignally used to obtain the lines. This may be nested
1624 * several levels. */
1625 gp = getline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001626 cp = (struct loop_cookie *)cookie;
1627 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628 {
1629 gp = cp->getline;
1630 cp = cp->cookie;
1631 }
1632 return cp;
1633# else
1634 return cookie;
1635# endif
1636}
1637#endif
1638
1639/*
1640 * Execute one Ex command.
1641 *
1642 * If 'sourcing' is TRUE, the command will be included in the error message.
1643 *
1644 * 1. skip comment lines and leading space
1645 * 2. handle command modifiers
1646 * 3. parse range
1647 * 4. parse command
1648 * 5. parse arguments
1649 * 6. switch on command name
1650 *
1651 * Note: "getline" can be NULL.
1652 *
1653 * This function may be called recursively!
1654 */
1655#if (_MSC_VER == 1200)
1656/*
Bram Moolenaared203462004-06-16 11:19:22 +00001657 * Avoid optimisation bug in VC++ version 6.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658 */
Bram Moolenaar281bdce2005-01-25 21:53:18 +00001659 #pragma optimize( "g", off )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001660#endif
1661 static char_u *
1662do_one_cmd(cmdlinep, sourcing,
1663#ifdef FEAT_EVAL
1664 cstack,
1665#endif
1666 getline, cookie)
1667 char_u **cmdlinep;
1668 int sourcing;
1669#ifdef FEAT_EVAL
1670 struct condstack *cstack;
1671#endif
1672 char_u *(*getline) __ARGS((int, void *, int));
1673 void *cookie; /* argument for getline() */
1674{
1675 char_u *p;
1676 linenr_T lnum;
1677 long n;
1678 char_u *errormsg = NULL; /* error message */
1679 exarg_T ea; /* Ex command arguments */
1680 long verbose_save = -1;
1681 int save_msg_scroll = 0;
1682 int did_silent = 0;
1683 int did_esilent = 0;
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001684#ifdef HAVE_SANDBOX
1685 int did_sandbox = FALSE;
1686#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687 cmdmod_T save_cmdmod;
1688 int ni; /* set when Not Implemented */
1689
1690 vim_memset(&ea, 0, sizeof(ea));
1691 ea.line1 = 1;
1692 ea.line2 = 1;
1693#ifdef FEAT_EVAL
1694 ++ex_nesting_level;
1695#endif
1696
1697 /* when not editing the last file :q has to be typed twice */
1698 if (quitmore
1699#ifdef FEAT_EVAL
1700 /* avoid that a function call in 'statusline' does this */
1701 && !getline_equal(getline, cookie, get_func_line)
1702#endif
1703 )
1704 --quitmore;
1705
1706 /*
1707 * Reset browse, confirm, etc.. They are restored when returning, for
1708 * recursive calls.
1709 */
1710 save_cmdmod = cmdmod;
1711 vim_memset(&cmdmod, 0, sizeof(cmdmod));
1712
1713 /*
1714 * Repeat until no more command modifiers are found.
1715 */
1716 ea.cmd = *cmdlinep;
1717 for (;;)
1718 {
1719/*
1720 * 1. skip comment lines and leading white space and colons
1721 */
1722 while (*ea.cmd == ' ' || *ea.cmd == '\t' || *ea.cmd == ':')
1723 ++ea.cmd;
1724
1725 /* in ex mode, an empty line works like :+ */
1726 if (*ea.cmd == NUL && exmode_active
1727 && (getline_equal(getline, cookie, getexmodeline)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001728 || getline_equal(getline, cookie, getexline))
1729 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730 {
1731 ea.cmd = (char_u *)"+";
1732 ex_pressedreturn = TRUE;
1733 }
1734
1735 /* ignore comment and empty lines */
1736 if (*ea.cmd == '"' || *ea.cmd == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001737 {
1738 ex_pressedreturn = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001740 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001741
1742/*
1743 * 2. handle command modifiers.
1744 */
1745 p = ea.cmd;
1746 if (VIM_ISDIGIT(*ea.cmd))
1747 p = skipwhite(skipdigits(ea.cmd));
1748 switch (*p)
1749 {
1750 /* When adding an entry, also modify cmd_exists(). */
1751 case 'a': if (!checkforcmd(&ea.cmd, "aboveleft", 3))
1752 break;
1753#ifdef FEAT_WINDOWS
1754 cmdmod.split |= WSP_ABOVE;
1755#endif
1756 continue;
1757
1758 case 'b': if (checkforcmd(&ea.cmd, "belowright", 3))
1759 {
1760#ifdef FEAT_WINDOWS
1761 cmdmod.split |= WSP_BELOW;
1762#endif
1763 continue;
1764 }
1765 if (checkforcmd(&ea.cmd, "browse", 3))
1766 {
1767#ifdef FEAT_BROWSE
1768 cmdmod.browse = TRUE;
1769#endif
1770 continue;
1771 }
1772 if (!checkforcmd(&ea.cmd, "botright", 2))
1773 break;
1774#ifdef FEAT_WINDOWS
1775 cmdmod.split |= WSP_BOT;
1776#endif
1777 continue;
1778
1779 case 'c': if (!checkforcmd(&ea.cmd, "confirm", 4))
1780 break;
1781#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1782 cmdmod.confirm = TRUE;
1783#endif
1784 continue;
1785
1786 case 'k': if (checkforcmd(&ea.cmd, "keepmarks", 3))
1787 {
1788 cmdmod.keepmarks = TRUE;
1789 continue;
1790 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001791 if (checkforcmd(&ea.cmd, "keepalt", 5))
1792 {
1793 cmdmod.keepalt = TRUE;
1794 continue;
1795 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796 if (!checkforcmd(&ea.cmd, "keepjumps", 5))
1797 break;
1798 cmdmod.keepjumps = TRUE;
1799 continue;
1800
1801 /* ":hide" and ":hide | cmd" are not modifiers */
1802 case 'h': if (p != ea.cmd || !checkforcmd(&p, "hide", 3)
1803 || *p == NUL || ends_excmd(*p))
1804 break;
1805 ea.cmd = p;
1806 cmdmod.hide = TRUE;
1807 continue;
1808
1809 case 'l': if (checkforcmd(&ea.cmd, "lockmarks", 3))
1810 {
1811 cmdmod.lockmarks = TRUE;
1812 continue;
1813 }
1814
1815 if (!checkforcmd(&ea.cmd, "leftabove", 5))
1816 break;
1817#ifdef FEAT_WINDOWS
1818 cmdmod.split |= WSP_ABOVE;
1819#endif
1820 continue;
1821
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001822 case 'n': if (!checkforcmd(&ea.cmd, "noautocmd", 3))
1823 break;
1824#ifdef FEAT_AUTOCMD
1825 if (cmdmod.save_ei == NULL)
1826 {
1827 /* Set 'eventignore' to "all". Don't free the
1828 * existing option value, we restore it later. */
1829 cmdmod.save_ei = vim_strsave(p_ei);
1830 set_string_option_direct((char_u *)"ei", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001831 (char_u *)"all", OPT_FREE, SID_NONE);
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001832 }
1833#endif
1834 continue;
1835
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836 case 'r': if (!checkforcmd(&ea.cmd, "rightbelow", 6))
1837 break;
1838#ifdef FEAT_WINDOWS
1839 cmdmod.split |= WSP_BELOW;
1840#endif
1841 continue;
1842
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001843 case 's': if (checkforcmd(&ea.cmd, "sandbox", 3))
1844 {
1845#ifdef HAVE_SANDBOX
1846 if (!did_sandbox)
1847 ++sandbox;
1848 did_sandbox = TRUE;
1849#endif
1850 continue;
1851 }
1852 if (!checkforcmd(&ea.cmd, "silent", 3))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853 break;
1854 ++did_silent;
1855 ++msg_silent;
1856 save_msg_scroll = msg_scroll;
1857 if (*ea.cmd == '!' && !vim_iswhite(ea.cmd[-1]))
1858 {
1859 /* ":silent!", but not "silent !cmd" */
1860 ea.cmd = skipwhite(ea.cmd + 1);
1861 ++emsg_silent;
1862 ++did_esilent;
1863 }
1864 continue;
1865
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001866 case 't': if (checkforcmd(&p, "tab", 3))
1867 {
1868#ifdef FEAT_WINDOWS
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001869 if (vim_isdigit(*ea.cmd))
1870 cmdmod.tab = atoi((char *)ea.cmd) + 1;
1871 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001872 cmdmod.tab = tabpage_index(curtab) + 1;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001873 ea.cmd = p;
1874#endif
1875 continue;
1876 }
1877 if (!checkforcmd(&ea.cmd, "topleft", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001878 break;
1879#ifdef FEAT_WINDOWS
1880 cmdmod.split |= WSP_TOP;
1881#endif
1882 continue;
1883
1884 case 'v': if (checkforcmd(&ea.cmd, "vertical", 4))
1885 {
1886#ifdef FEAT_VERTSPLIT
1887 cmdmod.split |= WSP_VERT;
1888#endif
1889 continue;
1890 }
1891 if (!checkforcmd(&p, "verbose", 4))
1892 break;
1893 if (verbose_save < 0)
1894 verbose_save = p_verbose;
Bram Moolenaared203462004-06-16 11:19:22 +00001895 if (vim_isdigit(*ea.cmd))
1896 p_verbose = atoi((char *)ea.cmd);
1897 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898 p_verbose = 1;
1899 ea.cmd = p;
1900 continue;
1901 }
1902 break;
1903 }
1904
1905#ifdef FEAT_EVAL
1906 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
1907 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
1908#else
1909 ea.skip = (if_level > 0);
1910#endif
1911
1912#ifdef FEAT_EVAL
Bram Moolenaar05159a02005-02-26 23:04:13 +00001913# ifdef FEAT_PROFILE
1914 /* Count this line for profiling if ea.skip is FALSE. */
Bram Moolenaar371d5402006-03-20 21:47:49 +00001915 if (do_profiling == PROF_YES && !ea.skip)
Bram Moolenaar05159a02005-02-26 23:04:13 +00001916 {
1917 if (getline_equal(getline, cookie, get_func_line))
1918 func_line_exec(getline_cookie(getline, cookie));
1919 else if (getline_equal(getline, cookie, getsourceline))
1920 script_line_exec();
1921 }
1922#endif
1923
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924 /* May go to debug mode. If this happens and the ">quit" debug command is
1925 * used, throw an interrupt exception and skip the next command. */
1926 dbg_check_breakpoint(&ea);
1927 if (!ea.skip && got_int)
1928 {
1929 ea.skip = TRUE;
1930 (void)do_intthrow(cstack);
1931 }
1932#endif
1933
1934/*
1935 * 3. parse a range specifier of the form: addr [,addr] [;addr] ..
1936 *
1937 * where 'addr' is:
1938 *
1939 * % (entire file)
1940 * $ [+-NUM]
1941 * 'x [+-NUM] (where x denotes a currently defined mark)
1942 * . [+-NUM]
1943 * [+-NUM]..
1944 * NUM
1945 *
1946 * The ea.cmd pointer is updated to point to the first character following the
1947 * range spec. If an initial address is found, but no second, the upper bound
1948 * is equal to the lower.
1949 */
1950
1951 /* repeat for all ',' or ';' separated addresses */
1952 for (;;)
1953 {
1954 ea.line1 = ea.line2;
1955 ea.line2 = curwin->w_cursor.lnum; /* default is current line number */
1956 ea.cmd = skipwhite(ea.cmd);
1957 lnum = get_address(&ea.cmd, ea.skip, ea.addr_count == 0);
1958 if (ea.cmd == NULL) /* error detected */
1959 goto doend;
1960 if (lnum == MAXLNUM)
1961 {
1962 if (*ea.cmd == '%') /* '%' - all lines */
1963 {
1964 ++ea.cmd;
1965 ea.line1 = 1;
1966 ea.line2 = curbuf->b_ml.ml_line_count;
1967 ++ea.addr_count;
1968 }
1969 /* '*' - visual area */
1970 else if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
1971 {
1972 pos_T *fp;
1973
1974 ++ea.cmd;
1975 if (!ea.skip)
1976 {
1977 fp = getmark('<', FALSE);
1978 if (check_mark(fp) == FAIL)
1979 goto doend;
1980 ea.line1 = fp->lnum;
1981 fp = getmark('>', FALSE);
1982 if (check_mark(fp) == FAIL)
1983 goto doend;
1984 ea.line2 = fp->lnum;
1985 ++ea.addr_count;
1986 }
1987 }
1988 }
1989 else
1990 ea.line2 = lnum;
1991 ea.addr_count++;
1992
1993 if (*ea.cmd == ';')
1994 {
1995 if (!ea.skip)
1996 curwin->w_cursor.lnum = ea.line2;
1997 }
1998 else if (*ea.cmd != ',')
1999 break;
2000 ++ea.cmd;
2001 }
2002
2003 /* One address given: set start and end lines */
2004 if (ea.addr_count == 1)
2005 {
2006 ea.line1 = ea.line2;
2007 /* ... but only implicit: really no address given */
2008 if (lnum == MAXLNUM)
2009 ea.addr_count = 0;
2010 }
2011
2012 /* Don't leave the cursor on an illegal line (caused by ';') */
2013 check_cursor_lnum();
2014
2015/*
2016 * 4. parse command
2017 */
2018
2019 /*
2020 * Skip ':' and any white space
2021 */
2022 ea.cmd = skipwhite(ea.cmd);
2023 while (*ea.cmd == ':')
2024 ea.cmd = skipwhite(ea.cmd + 1);
2025
2026 /*
2027 * If we got a line, but no command, then go to the line.
2028 * If we find a '|' or '\n' we set ea.nextcmd.
2029 */
2030 if (*ea.cmd == NUL || *ea.cmd == '"' ||
2031 (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
2032 {
2033 /*
2034 * strange vi behaviour:
2035 * ":3" jumps to line 3
2036 * ":3|..." prints line 3
2037 * ":|" prints current line
2038 */
2039 if (ea.skip) /* skip this if inside :if */
2040 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00002041 if (*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002042 {
2043 ea.cmdidx = CMD_print;
2044 ea.argt = RANGE+COUNT+TRLBAR;
2045 if ((errormsg = invalid_range(&ea)) == NULL)
2046 {
2047 correct_range(&ea);
2048 ex_print(&ea);
2049 }
2050 }
2051 else if (ea.addr_count != 0)
2052 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002053 if (ea.line2 > curbuf->b_ml.ml_line_count)
2054 {
2055 /* With '-' in 'cpoptions' a line number past the file is an
2056 * error, otherwise put it at the end of the file. */
2057 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
2058 ea.line2 = -1;
2059 else
2060 ea.line2 = curbuf->b_ml.ml_line_count;
2061 }
2062
2063 if (ea.line2 < 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002064 errormsg = (char_u *)_(e_invrange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002065 else
2066 {
2067 if (ea.line2 == 0)
2068 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069 else
2070 curwin->w_cursor.lnum = ea.line2;
2071 beginline(BL_SOL | BL_FIX);
2072 }
2073 }
2074 goto doend;
2075 }
2076
2077 /* Find the command and let "p" point to after it. */
2078 p = find_command(&ea, NULL);
2079
2080#ifdef FEAT_USR_CMDS
2081 if (p == NULL)
2082 {
2083 if (!ea.skip)
2084 errormsg = (char_u *)_("E464: Ambiguous use of user-defined command");
2085 goto doend;
2086 }
2087 /* Check for wrong commands. */
2088 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78)
2089 {
2090 errormsg = uc_fun_cmd();
2091 goto doend;
2092 }
2093#endif
2094 if (ea.cmdidx == CMD_SIZE)
2095 {
2096 if (!ea.skip)
2097 {
2098 STRCPY(IObuff, _("E492: Not an editor command"));
2099 if (!sourcing)
2100 {
2101 STRCAT(IObuff, ": ");
2102 STRNCAT(IObuff, *cmdlinep, 40);
2103 }
2104 errormsg = IObuff;
2105 }
2106 goto doend;
2107 }
2108
2109 ni = (
2110#ifdef FEAT_USR_CMDS
2111 !USER_CMDIDX(ea.cmdidx) &&
2112#endif
2113 cmdnames[ea.cmdidx].cmd_func == ex_ni);
2114
2115#ifndef FEAT_EVAL
2116 /*
2117 * When the expression evaluation is disabled, recognize the ":if" and
2118 * ":endif" commands and ignore everything in between it.
2119 */
2120 if (ea.cmdidx == CMD_if)
2121 ++if_level;
2122 if (if_level)
2123 {
2124 if (ea.cmdidx == CMD_endif)
2125 --if_level;
2126 goto doend;
2127 }
2128
2129#endif
2130
2131 if (*p == '!' && ea.cmdidx != CMD_substitute) /* forced commands */
2132 {
2133 ++p;
2134 ea.forceit = TRUE;
2135 }
2136 else
2137 ea.forceit = FALSE;
2138
2139/*
2140 * 5. parse arguments
2141 */
2142#ifdef FEAT_USR_CMDS
2143 if (!USER_CMDIDX(ea.cmdidx))
2144#endif
2145 ea.argt = cmdnames[(int)ea.cmdidx].cmd_argt;
2146
2147 if (!ea.skip)
2148 {
2149#ifdef HAVE_SANDBOX
2150 if (sandbox != 0 && !(ea.argt & SBOXOK))
2151 {
2152 /* Command not allowed in sandbox. */
2153 errormsg = (char_u *)_(e_sandbox);
2154 goto doend;
2155 }
2156#endif
2157 if (!curbuf->b_p_ma && (ea.argt & MODIFY))
2158 {
2159 /* Command not allowed in non-'modifiable' buffer */
2160 errormsg = (char_u *)_(e_modifiable);
2161 goto doend;
2162 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002163
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002164 if (text_locked() && !(ea.argt & CMDWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165# ifdef FEAT_USR_CMDS
2166 && !USER_CMDIDX(ea.cmdidx)
2167# endif
2168 )
2169 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002170 /* Command not allowed when editing the command line. */
2171#ifdef FEAT_CMDWIN
2172 if (cmdwin_type != 0)
2173 errormsg = (char_u *)_(e_cmdwin);
2174 else
2175#endif
2176 errormsg = (char_u *)_(e_secure);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177 goto doend;
2178 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002179#ifdef FEAT_AUTOCMD
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002180 /* Disallow editing another buffer when "curbuf_lock" is set.
2181 * Do allow ":edit" (check for argument later).
2182 * Do allow ":checktime" (it's postponed). */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002183 if (!(ea.argt & CMDWIN)
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002184 && ea.cmdidx != CMD_edit
2185 && ea.cmdidx != CMD_checktime
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002186# ifdef FEAT_USR_CMDS
2187 && !USER_CMDIDX(ea.cmdidx)
2188# endif
2189 && curbuf_locked())
2190 goto doend;
2191#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002192
2193 if (!ni && !(ea.argt & RANGE) && ea.addr_count > 0)
2194 {
2195 /* no range allowed */
2196 errormsg = (char_u *)_(e_norange);
2197 goto doend;
2198 }
2199 }
2200
2201 if (!ni && !(ea.argt & BANG) && ea.forceit) /* no <!> allowed */
2202 {
2203 errormsg = (char_u *)_(e_nobang);
2204 goto doend;
2205 }
2206
2207 /*
2208 * Don't complain about the range if it is not used
2209 * (could happen if line_count is accidentally set to 0).
2210 */
2211 if (!ea.skip && !ni)
2212 {
2213 /*
2214 * If the range is backwards, ask for confirmation and, if given, swap
2215 * ea.line1 & ea.line2 so it's forwards again.
2216 * When global command is busy, don't ask, will fail below.
2217 */
2218 if (!global_busy && ea.line1 > ea.line2)
2219 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002220 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002222 if (sourcing || exmode_active)
2223 {
2224 errormsg = (char_u *)_("E493: Backwards range given");
2225 goto doend;
2226 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002227 if (ask_yesno((char_u *)
2228 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002229 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002230 }
2231 lnum = ea.line1;
2232 ea.line1 = ea.line2;
2233 ea.line2 = lnum;
2234 }
2235 if ((errormsg = invalid_range(&ea)) != NULL)
2236 goto doend;
2237 }
2238
2239 if ((ea.argt & NOTADR) && ea.addr_count == 0) /* default is 1, not cursor */
2240 ea.line2 = 1;
2241
2242 correct_range(&ea);
2243
2244#ifdef FEAT_FOLDING
2245 if (((ea.argt & WHOLEFOLD) || ea.addr_count >= 2) && !global_busy)
2246 {
2247 /* Put the first line at the start of a closed fold, put the last line
2248 * at the end of a closed fold. */
2249 (void)hasFolding(ea.line1, &ea.line1, NULL);
2250 (void)hasFolding(ea.line2, NULL, &ea.line2);
2251 }
2252#endif
2253
2254#ifdef FEAT_QUICKFIX
2255 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002256 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002257 * option here, so things like % get expanded.
2258 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002259 p = replace_makeprg(&ea, p, cmdlinep);
2260 if (p == NULL)
2261 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002262#endif
2263
2264 /*
2265 * Skip to start of argument.
2266 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2267 */
2268 if (ea.cmdidx == CMD_bang)
2269 ea.arg = p;
2270 else
2271 ea.arg = skipwhite(p);
2272
2273 /*
2274 * Check for "++opt=val" argument.
2275 * Must be first, allow ":w ++enc=utf8 !cmd"
2276 */
2277 if (ea.argt & ARGOPT)
2278 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2279 if (getargopt(&ea) == FAIL && !ni)
2280 {
2281 errormsg = (char_u *)_(e_invarg);
2282 goto doend;
2283 }
2284
2285 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2286 {
2287 if (*ea.arg == '>') /* append */
2288 {
2289 if (*++ea.arg != '>') /* typed wrong */
2290 {
2291 errormsg = (char_u *)_("E494: Use w or w>>");
2292 goto doend;
2293 }
2294 ea.arg = skipwhite(ea.arg + 1);
2295 ea.append = TRUE;
2296 }
2297 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
2298 {
2299 ++ea.arg;
2300 ea.usefilter = TRUE;
2301 }
2302 }
2303
2304 if (ea.cmdidx == CMD_read)
2305 {
2306 if (ea.forceit)
2307 {
2308 ea.usefilter = TRUE; /* :r! filter if ea.forceit */
2309 ea.forceit = FALSE;
2310 }
2311 else if (*ea.arg == '!') /* :r !filter */
2312 {
2313 ++ea.arg;
2314 ea.usefilter = TRUE;
2315 }
2316 }
2317
2318 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2319 {
2320 ea.amount = 1;
2321 while (*ea.arg == *ea.cmd) /* count number of '>' or '<' */
2322 {
2323 ++ea.arg;
2324 ++ea.amount;
2325 }
2326 ea.arg = skipwhite(ea.arg);
2327 }
2328
2329 /*
2330 * Check for "+command" argument, before checking for next command.
2331 * Don't do this for ":read !cmd" and ":write !cmd".
2332 */
2333 if ((ea.argt & EDITCMD) && !ea.usefilter)
2334 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2335
2336 /*
2337 * Check for '|' to separate commands and '"' to start comments.
2338 * Don't do this for ":read !cmd" and ":write !cmd".
2339 */
2340 if ((ea.argt & TRLBAR) && !ea.usefilter)
2341 separate_nextcmd(&ea);
2342
2343 /*
2344 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002345 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2346 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002348 else if (ea.cmdidx == CMD_bang
2349 || ea.cmdidx == CMD_global
2350 || ea.cmdidx == CMD_vglobal
2351 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352 {
2353 for (p = ea.arg; *p; ++p)
2354 {
2355 /* Remove one backslash before a newline, so that it's possible to
2356 * pass a newline to the shell and also a newline that is preceded
2357 * with a backslash. This makes it impossible to end a shell
2358 * command in a backslash, but that doesn't appear useful.
2359 * Halving the number of backslashes is incompatible with previous
2360 * versions. */
2361 if (*p == '\\' && p[1] == '\n')
2362 mch_memmove(p, p + 1, STRLEN(p));
2363 else if (*p == '\n')
2364 {
2365 ea.nextcmd = p + 1;
2366 *p = NUL;
2367 break;
2368 }
2369 }
2370 }
2371
2372 if ((ea.argt & DFLALL) && ea.addr_count == 0)
2373 {
2374 ea.line1 = 1;
2375 ea.line2 = curbuf->b_ml.ml_line_count;
2376 }
2377
2378 /* accept numbered register only when no count allowed (:put) */
2379 if ( (ea.argt & REGSTR)
2380 && *ea.arg != NUL
2381#ifdef FEAT_USR_CMDS
2382 && valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2383 && USER_CMDIDX(ea.cmdidx)))
2384 /* Do not allow register = for user commands */
2385 && (!USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
2386#else
2387 && valid_yank_reg(*ea.arg, ea.cmdidx != CMD_put)
2388#endif
2389 && !((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)))
2390 {
2391 ea.regname = *ea.arg++;
2392#ifdef FEAT_EVAL
2393 /* for '=' register: accept the rest of the line as an expression */
2394 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2395 {
2396 set_expr_line(vim_strsave(ea.arg));
2397 ea.arg += STRLEN(ea.arg);
2398 }
2399#endif
2400 ea.arg = skipwhite(ea.arg);
2401 }
2402
2403 /*
2404 * Check for a count. When accepting a BUFNAME, don't use "123foo" as a
2405 * count, it's a buffer name.
2406 */
2407 if ((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)
2408 && (!(ea.argt & BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
2409 || vim_iswhite(*p)))
2410 {
2411 n = getdigits(&ea.arg);
2412 ea.arg = skipwhite(ea.arg);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002413 if (n <= 0 && !ni && (ea.argt & ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414 {
2415 errormsg = (char_u *)_(e_zerocount);
2416 goto doend;
2417 }
2418 if (ea.argt & NOTADR) /* e.g. :buffer 2, :sleep 3 */
2419 {
2420 ea.line2 = n;
2421 if (ea.addr_count == 0)
2422 ea.addr_count = 1;
2423 }
2424 else
2425 {
2426 ea.line1 = ea.line2;
2427 ea.line2 += n - 1;
2428 ++ea.addr_count;
2429 /*
2430 * Be vi compatible: no error message for out of range.
2431 */
2432 if (ea.line2 > curbuf->b_ml.ml_line_count)
2433 ea.line2 = curbuf->b_ml.ml_line_count;
2434 }
2435 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002436
2437 /*
2438 * Check for flags: 'l', 'p' and '#'.
2439 */
2440 if (ea.argt & EXFLAGS)
2441 get_flags(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002442 /* no arguments allowed */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002443 if (!ni && !(ea.argt & EXTRA) && *ea.arg != NUL
Bram Moolenaara2031822006-03-07 22:29:51 +00002444 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002445 {
2446 errormsg = (char_u *)_(e_trailing);
2447 goto doend;
2448 }
2449
2450 if (!ni && (ea.argt & NEEDARG) && *ea.arg == NUL)
2451 {
2452 errormsg = (char_u *)_(e_argreq);
2453 goto doend;
2454 }
2455
2456#ifdef FEAT_EVAL
2457 /*
2458 * Skip the command when it's not going to be executed.
2459 * The commands like :if, :endif, etc. always need to be executed.
2460 * Also make an exception for commands that handle a trailing command
2461 * themselves.
2462 */
2463 if (ea.skip)
2464 {
2465 switch (ea.cmdidx)
2466 {
2467 /* commands that need evaluation */
2468 case CMD_while:
2469 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002470 case CMD_for:
2471 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472 case CMD_if:
2473 case CMD_elseif:
2474 case CMD_else:
2475 case CMD_endif:
2476 case CMD_try:
2477 case CMD_catch:
2478 case CMD_finally:
2479 case CMD_endtry:
2480 case CMD_function:
2481 break;
2482
2483 /* Commands that handle '|' themselves. Check: A command should
2484 * either have the TRLBAR flag, appear in this list or appear in
2485 * the list at ":help :bar". */
2486 case CMD_aboveleft:
2487 case CMD_and:
2488 case CMD_belowright:
2489 case CMD_botright:
2490 case CMD_browse:
2491 case CMD_call:
2492 case CMD_confirm:
2493 case CMD_delfunction:
2494 case CMD_djump:
2495 case CMD_dlist:
2496 case CMD_dsearch:
2497 case CMD_dsplit:
2498 case CMD_echo:
2499 case CMD_echoerr:
2500 case CMD_echomsg:
2501 case CMD_echon:
2502 case CMD_execute:
2503 case CMD_help:
2504 case CMD_hide:
2505 case CMD_ijump:
2506 case CMD_ilist:
2507 case CMD_isearch:
2508 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002509 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002510 case CMD_keepjumps:
2511 case CMD_keepmarks:
2512 case CMD_leftabove:
2513 case CMD_let:
2514 case CMD_lockmarks:
2515 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002516 case CMD_mzscheme:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002517 case CMD_perl:
2518 case CMD_psearch:
2519 case CMD_python:
2520 case CMD_return:
2521 case CMD_rightbelow:
2522 case CMD_ruby:
2523 case CMD_silent:
2524 case CMD_smagic:
2525 case CMD_snomagic:
2526 case CMD_substitute:
2527 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002528 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002529 case CMD_tcl:
2530 case CMD_throw:
2531 case CMD_tilde:
2532 case CMD_topleft:
2533 case CMD_unlet:
2534 case CMD_verbose:
2535 case CMD_vertical:
2536 break;
2537
2538 default: goto doend;
2539 }
2540 }
2541#endif
2542
2543 if (ea.argt & XFILE)
2544 {
2545 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2546 goto doend;
2547 }
2548
2549#ifdef FEAT_LISTCMDS
2550 /*
2551 * Accept buffer name. Cannot be used at the same time with a buffer
2552 * number. Don't do this for a user command.
2553 */
2554 if ((ea.argt & BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
2555# ifdef FEAT_USR_CMDS
2556 && !USER_CMDIDX(ea.cmdidx)
2557# endif
2558 )
2559 {
2560 /*
2561 * :bdelete, :bwipeout and :bunload take several arguments, separated
2562 * by spaces: find next space (skipping over escaped characters).
2563 * The others take one argument: ignore trailing spaces.
2564 */
2565 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2566 || ea.cmdidx == CMD_bunload)
2567 p = skiptowhite_esc(ea.arg);
2568 else
2569 {
2570 p = ea.arg + STRLEN(ea.arg);
2571 while (p > ea.arg && vim_iswhite(p[-1]))
2572 --p;
2573 }
2574 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & BUFUNL) != 0, FALSE);
2575 if (ea.line2 < 0) /* failed */
2576 goto doend;
2577 ea.addr_count = 1;
2578 ea.arg = skipwhite(p);
2579 }
2580#endif
2581
2582/*
2583 * 6. switch on command name
2584 *
2585 * The "ea" structure holds the arguments that can be used.
2586 */
2587 ea.cmdlinep = cmdlinep;
2588 ea.getline = getline;
2589 ea.cookie = cookie;
2590#ifdef FEAT_EVAL
2591 ea.cstack = cstack;
2592#endif
2593
2594#ifdef FEAT_USR_CMDS
2595 if (USER_CMDIDX(ea.cmdidx))
2596 {
2597 /*
2598 * Execute a user-defined command.
2599 */
2600 do_ucmd(&ea);
2601 }
2602 else
2603#endif
2604 {
2605 /*
2606 * Call the function to execute the command.
2607 */
2608 ea.errmsg = NULL;
2609 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2610 if (ea.errmsg != NULL)
2611 errormsg = (char_u *)_(ea.errmsg);
2612 }
2613
2614#ifdef FEAT_EVAL
2615 /*
2616 * If the command just executed called do_cmdline(), any throw or ":return"
2617 * or ":finish" encountered there must also check the cstack of the still
2618 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2619 * exception, or reanimate a returned function or finished script file and
2620 * return or finish it again.
2621 */
2622 if (need_rethrow)
2623 do_throw(cstack);
2624 else if (check_cstack)
2625 {
2626 if (source_finished(getline, cookie))
2627 do_finish(&ea, TRUE);
2628 else if (getline_equal(getline, cookie, get_func_line)
2629 && current_func_returned())
2630 do_return(&ea, TRUE, FALSE, NULL);
2631 }
2632 need_rethrow = check_cstack = FALSE;
2633#endif
2634
2635doend:
2636 if (curwin->w_cursor.lnum == 0) /* can happen with zero line number */
2637 curwin->w_cursor.lnum = 1;
2638
2639 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2640 {
2641 if (sourcing)
2642 {
2643 if (errormsg != IObuff)
2644 {
2645 STRCPY(IObuff, errormsg);
2646 errormsg = IObuff;
2647 }
2648 STRCAT(errormsg, ": ");
2649 STRNCAT(errormsg, *cmdlinep, IOSIZE - STRLEN(IObuff));
2650 }
2651 emsg(errormsg);
2652 }
2653#ifdef FEAT_EVAL
2654 do_errthrow(cstack,
2655 (ea.cmdidx != CMD_SIZE
2656# ifdef FEAT_USR_CMDS
2657 && !USER_CMDIDX(ea.cmdidx)
2658# endif
2659 ) ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
2660#endif
2661
2662 if (verbose_save >= 0)
2663 p_verbose = verbose_save;
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00002664#ifdef FEAT_AUTOCMD
2665 if (cmdmod.save_ei != NULL)
2666 {
2667 /* Restore 'eventignore' to the value before ":noautocmd". */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002668 set_string_option_direct((char_u *)"ei", -1, cmdmod.save_ei,
2669 OPT_FREE, SID_NONE);
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00002670 free_string_option(cmdmod.save_ei);
2671 }
2672#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673
2674 cmdmod = save_cmdmod;
2675
2676 if (did_silent > 0)
2677 {
2678 /* messages could be enabled for a serious error, need to check if the
2679 * counters don't become negative */
2680 msg_silent -= did_silent;
2681 if (msg_silent < 0)
2682 msg_silent = 0;
2683 emsg_silent -= did_esilent;
2684 if (emsg_silent < 0)
2685 emsg_silent = 0;
2686 /* Restore msg_scroll, it's set by file I/O commands, even when no
2687 * message is actually displayed. */
2688 msg_scroll = save_msg_scroll;
2689 }
2690
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002691#ifdef HAVE_SANDBOX
2692 if (did_sandbox)
2693 --sandbox;
2694#endif
2695
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696 if (ea.nextcmd && *ea.nextcmd == NUL) /* not really a next command */
2697 ea.nextcmd = NULL;
2698
2699#ifdef FEAT_EVAL
2700 --ex_nesting_level;
2701#endif
2702
2703 return ea.nextcmd;
2704}
2705#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00002706 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707#endif
2708
2709/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002710 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002711 * If there is a match advance "pp" to the argument and return TRUE.
2712 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002713 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00002714checkforcmd(pp, cmd, len)
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002715 char_u **pp; /* start of command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002716 char *cmd; /* name of command */
2717 int len; /* required length */
2718{
2719 int i;
2720
2721 for (i = 0; cmd[i] != NUL; ++i)
2722 if (cmd[i] != (*pp)[i])
2723 break;
2724 if (i >= len && !isalpha((*pp)[i]))
2725 {
2726 *pp = skipwhite(*pp + i);
2727 return TRUE;
2728 }
2729 return FALSE;
2730}
2731
2732/*
2733 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002734 * Start of the name can be found at eap->cmd.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002735 * Returns pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002736 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002737 * Returns NULL for an ambiguous user command.
2738 */
2739/*ARGSUSED*/
2740 static char_u *
2741find_command(eap, full)
2742 exarg_T *eap;
2743 int *full;
2744{
2745 int len;
2746 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00002747 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002748
2749 /*
2750 * Isolate the command and search for it in the command table.
2751 * Exeptions:
2752 * - the 'k' command can directly be followed by any character.
2753 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
2754 * but :sre[wind] is another command, as are :scrip[tnames],
2755 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00002756 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002757 */
2758 p = eap->cmd;
2759 if (*p == 'k')
2760 {
2761 eap->cmdidx = CMD_k;
2762 ++p;
2763 }
2764 else if (p[0] == 's'
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002765 && ((p[1] == 'c' && p[2] != 's' && p[2] != 'r'
2766 && p[3] != 'i' && p[4] != 'p')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002767 || p[1] == 'g'
2768 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
2769 || p[1] == 'I'
2770 || (p[1] == 'r' && p[2] != 'e')))
2771 {
2772 eap->cmdidx = CMD_substitute;
2773 ++p;
2774 }
2775 else
2776 {
2777 while (ASCII_ISALPHA(*p))
2778 ++p;
2779 /* check for non-alpha command */
2780 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
2781 ++p;
2782 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00002783 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
2784 {
2785 /* Check for ":dl", ":dell", etc. to ":deletel": that's
2786 * :delete with the 'l' flag. Same for 'p'. */
2787 for (i = 0; i < len; ++i)
2788 if (eap->cmd[i] != "delete"[i])
2789 break;
2790 if (i == len - 1)
2791 {
2792 --len;
2793 if (p[-1] == 'l')
2794 eap->flags |= EXFLAG_LIST;
2795 else
2796 eap->flags |= EXFLAG_PRINT;
2797 }
2798 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799
2800 if (ASCII_ISLOWER(*eap->cmd))
2801 eap->cmdidx = cmdidxs[CharOrdLow(*eap->cmd)];
2802 else
2803 eap->cmdidx = cmdidxs[26];
2804
2805 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
2806 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
2807 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
2808 (size_t)len) == 0)
2809 {
2810#ifdef FEAT_EVAL
2811 if (full != NULL
2812 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
2813 *full = TRUE;
2814#endif
2815 break;
2816 }
2817
2818#ifdef FEAT_USR_CMDS
2819 /* Look for a user defined command as a last resort */
2820 if (eap->cmdidx == CMD_SIZE && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
2821 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002822 /* User defined commands may contain digits. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002823 while (ASCII_ISALNUM(*p))
2824 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002825 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002826 }
2827#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002828 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002829 eap->cmdidx = CMD_SIZE;
2830 }
2831
2832 return p;
2833}
2834
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002835#ifdef FEAT_USR_CMDS
2836/*
2837 * Search for a user command that matches "eap->cmd".
2838 * Return cmdidx in "eap->cmdidx", flags in "eap->argt", idx in "eap->useridx".
2839 * Return a pointer to just after the command.
2840 * Return NULL if there is no matching command.
2841 */
2842 static char_u *
2843find_ucmd(eap, p, full, xp, compl)
2844 exarg_T *eap;
2845 char_u *p; /* end of the command (possibly including count) */
2846 int *full; /* set to TRUE for a full match */
2847 expand_T *xp; /* used for completion, NULL otherwise */
2848 int *compl; /* completion flags or NULL */
2849{
2850 int len = (int)(p - eap->cmd);
2851 int j, k, matchlen = 0;
2852 ucmd_T *uc;
2853 int found = FALSE;
2854 int possible = FALSE;
2855 char_u *cp, *np; /* Point into typed cmd and test name */
2856 garray_T *gap;
2857 int amb_local = FALSE; /* Found ambiguous buffer-local command,
2858 only full match global is accepted. */
2859
2860 /*
2861 * Look for buffer-local user commands first, then global ones.
2862 */
2863 gap = &curbuf->b_ucmds;
2864 for (;;)
2865 {
2866 for (j = 0; j < gap->ga_len; ++j)
2867 {
2868 uc = USER_CMD_GA(gap, j);
2869 cp = eap->cmd;
2870 np = uc->uc_name;
2871 k = 0;
2872 while (k < len && *np != NUL && *cp++ == *np++)
2873 k++;
2874 if (k == len || (*np == NUL && vim_isdigit(eap->cmd[k])))
2875 {
2876 /* If finding a second match, the command is ambiguous. But
2877 * not if a buffer-local command wasn't a full match and a
2878 * global command is a full match. */
2879 if (k == len && found && *np != NUL)
2880 {
2881 if (gap == &ucmds)
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002882 return NULL;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002883 amb_local = TRUE;
2884 }
2885
2886 if (!found || (k == len && *np == NUL))
2887 {
2888 /* If we matched up to a digit, then there could
2889 * be another command including the digit that we
2890 * should use instead.
2891 */
2892 if (k == len)
2893 found = TRUE;
2894 else
2895 possible = TRUE;
2896
2897 if (gap == &ucmds)
2898 eap->cmdidx = CMD_USER;
2899 else
2900 eap->cmdidx = CMD_USER_BUF;
2901 eap->argt = uc->uc_argt;
2902 eap->useridx = j;
2903
2904# ifdef FEAT_CMDL_COMPL
2905 if (compl != NULL)
2906 *compl = uc->uc_compl;
2907# ifdef FEAT_EVAL
2908 if (xp != NULL)
2909 {
2910 xp->xp_arg = uc->uc_compl_arg;
2911 xp->xp_scriptID = uc->uc_scriptID;
2912 }
2913# endif
2914# endif
2915 /* Do not search for further abbreviations
2916 * if this is an exact match. */
2917 matchlen = k;
2918 if (k == len && *np == NUL)
2919 {
2920 if (full != NULL)
2921 *full = TRUE;
2922 amb_local = FALSE;
2923 break;
2924 }
2925 }
2926 }
2927 }
2928
2929 /* Stop if we found a full match or searched all. */
2930 if (j < gap->ga_len || gap == &ucmds)
2931 break;
2932 gap = &ucmds;
2933 }
2934
2935 /* Only found ambiguous matches. */
2936 if (amb_local)
2937 {
2938 if (xp != NULL)
2939 xp->xp_context = EXPAND_UNSUCCESSFUL;
2940 return NULL;
2941 }
2942
2943 /* The match we found may be followed immediately by a number. Move "p"
2944 * back to point to it. */
2945 if (found || possible)
2946 return p + (matchlen - len);
2947 return p;
2948}
2949#endif
2950
Bram Moolenaar071d4272004-06-13 20:20:40 +00002951#if defined(FEAT_EVAL) || defined(PROTO)
2952/*
2953 * Return > 0 if an Ex command "name" exists.
2954 * Return 2 if there is an exact match.
2955 * Return 3 if there is an ambiguous match.
2956 */
2957 int
2958cmd_exists(name)
2959 char_u *name;
2960{
2961 exarg_T ea;
2962 int full = FALSE;
2963 int i;
2964 int j;
2965 static struct cmdmod
2966 {
2967 char *name;
2968 int minlen;
2969 } cmdmods[] = {
2970 {"aboveleft", 3},
2971 {"belowright", 3},
2972 {"botright", 2},
2973 {"browse", 3},
2974 {"confirm", 4},
2975 {"hide", 3},
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002976 {"keepalt", 5},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002977 {"keepjumps", 5},
2978 {"keepmarks", 3},
2979 {"leftabove", 5},
2980 {"lockmarks", 3},
2981 {"rightbelow", 6},
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002982 {"sandbox", 3},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002983 {"silent", 3},
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002984 {"tab", 3},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002985 {"topleft", 2},
2986 {"verbose", 4},
2987 {"vertical", 4},
2988 };
2989
2990 /* Check command modifiers. */
2991 for (i = 0; i < sizeof(cmdmods) / sizeof(struct cmdmod); ++i)
2992 {
2993 for (j = 0; name[j] != NUL; ++j)
2994 if (name[j] != cmdmods[i].name[j])
2995 break;
2996 if (name[j] == NUL && j >= cmdmods[i].minlen)
2997 return (cmdmods[i].name[j] == NUL ? 2 : 1);
2998 }
2999
3000 /* Check built-in commands and user defined commands. */
3001 ea.cmd = name;
3002 ea.cmdidx = (cmdidx_T)0;
3003 if (find_command(&ea, &full) == NULL)
3004 return 3;
3005 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3006}
3007#endif
3008
3009/*
3010 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
3011 * we don't need/want deleted. Maybe this could be done better if we didn't
3012 * repeat all this stuff. The only problem is that they may not stay
3013 * perfectly compatible with each other, but then the command line syntax
3014 * probably won't change that much -- webb.
3015 */
3016 char_u *
3017set_one_cmd_context(xp, buff)
3018 expand_T *xp;
3019 char_u *buff; /* buffer for command string */
3020{
3021 char_u *p;
3022 char_u *cmd, *arg;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003023 int len = 0;
3024 exarg_T ea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003025#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3026 int compl = EXPAND_NOTHING;
3027#endif
3028#ifdef FEAT_CMDL_COMPL
3029 int delim;
3030#endif
3031 int forceit = FALSE;
3032 int usefilter = FALSE; /* filter instead of file name */
3033
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003034 ExpandInit(xp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003035 xp->xp_pattern = buff;
3036 xp->xp_context = EXPAND_COMMANDS; /* Default until we get past command */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003037 ea.argt = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003038
3039/*
3040 * 2. skip comment lines and leading space, colons or bars
3041 */
3042 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
3043 ;
3044 xp->xp_pattern = cmd;
3045
3046 if (*cmd == NUL)
3047 return NULL;
3048 if (*cmd == '"') /* ignore comment lines */
3049 {
3050 xp->xp_context = EXPAND_NOTHING;
3051 return NULL;
3052 }
3053
3054/*
3055 * 3. parse a range specifier of the form: addr [,addr] [;addr] ..
3056 */
3057 cmd = skip_range(cmd, &xp->xp_context);
3058
3059/*
3060 * 4. parse command
3061 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003062 xp->xp_pattern = cmd;
3063 if (*cmd == NUL)
3064 return NULL;
3065 if (*cmd == '"')
3066 {
3067 xp->xp_context = EXPAND_NOTHING;
3068 return NULL;
3069 }
3070
3071 if (*cmd == '|' || *cmd == '\n')
3072 return cmd + 1; /* There's another command */
3073
3074 /*
3075 * Isolate the command and search for it in the command table.
3076 * Exceptions:
3077 * - the 'k' command can directly be followed by any character, but
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003078 * do accept "keepmarks", "keepalt" and "keepjumps".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003079 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
3080 */
3081 if (*cmd == 'k' && cmd[1] != 'e')
3082 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003083 ea.cmdidx = CMD_k;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003084 p = cmd + 1;
3085 }
3086 else
3087 {
3088 p = cmd;
3089 while (ASCII_ISALPHA(*p) || *p == '*') /* Allow * wild card */
3090 ++p;
3091 /* check for non-alpha command */
3092 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3093 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003094 len = (int)(p - cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003095
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003096 if (len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003097 {
3098 xp->xp_context = EXPAND_UNSUCCESSFUL;
3099 return NULL;
3100 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003101 for (ea.cmdidx = (cmdidx_T)0; (int)ea.cmdidx < (int)CMD_SIZE;
3102 ea.cmdidx = (cmdidx_T)((int)ea.cmdidx + 1))
3103 if (STRNCMP(cmdnames[(int)ea.cmdidx].cmd_name, cmd, (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003104 break;
3105
3106#ifdef FEAT_USR_CMDS
3107 if (cmd[0] >= 'A' && cmd[0] <= 'Z')
3108 {
3109 while (ASCII_ISALNUM(*p) || *p == '*') /* Allow * wild card */
3110 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003111 len = (int)(p - cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003112 }
3113#endif
3114 }
3115
3116 /*
3117 * If the cursor is touching the command, and it ends in an alpha-numeric
3118 * character, complete the command name.
3119 */
3120 if (*p == NUL && ASCII_ISALNUM(p[-1]))
3121 return NULL;
3122
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003123 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003124 {
3125 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
3126 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003127 ea.cmdidx = CMD_substitute;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003128 p = cmd + 1;
3129 }
3130#ifdef FEAT_USR_CMDS
3131 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
3132 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003133 ea.cmd = cmd;
3134 p = find_ucmd(&ea, p, NULL, xp,
3135# if defined(FEAT_CMDL_COMPL)
3136 &compl
3137# else
3138 NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003139# endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003140 );
Bram Moolenaarebefac62005-12-28 22:39:57 +00003141 if (p == NULL)
3142 ea.cmdidx = CMD_SIZE; /* ambiguous user command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143 }
3144#endif
3145 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003146 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003147 {
3148 /* Not still touching the command and it was an illegal one */
3149 xp->xp_context = EXPAND_UNSUCCESSFUL;
3150 return NULL;
3151 }
3152
3153 xp->xp_context = EXPAND_NOTHING; /* Default now that we're past command */
3154
3155 if (*p == '!') /* forced commands */
3156 {
3157 forceit = TRUE;
3158 ++p;
3159 }
3160
3161/*
3162 * 5. parse arguments
3163 */
3164#ifdef FEAT_USR_CMDS
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003165 if (!USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003166#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003167 ea.argt = cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003168
3169 arg = skipwhite(p);
3170
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003171 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003172 {
3173 if (*arg == '>') /* append */
3174 {
3175 if (*++arg == '>')
3176 ++arg;
3177 arg = skipwhite(arg);
3178 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003179 else if (*arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003180 {
3181 ++arg;
3182 usefilter = TRUE;
3183 }
3184 }
3185
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003186 if (ea.cmdidx == CMD_read)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187 {
3188 usefilter = forceit; /* :r! filter if forced */
3189 if (*arg == '!') /* :r !filter */
3190 {
3191 ++arg;
3192 usefilter = TRUE;
3193 }
3194 }
3195
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003196 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003197 {
3198 while (*arg == *cmd) /* allow any number of '>' or '<' */
3199 ++arg;
3200 arg = skipwhite(arg);
3201 }
3202
3203 /* Does command allow "+command"? */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003204 if ((ea.argt & EDITCMD) && !usefilter && *arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003205 {
3206 /* Check if we're in the +command */
3207 p = arg + 1;
3208 arg = skip_cmd_arg(arg, FALSE);
3209
3210 /* Still touching the command after '+'? */
3211 if (*arg == NUL)
3212 return p;
3213
3214 /* Skip space(s) after +command to get to the real argument */
3215 arg = skipwhite(arg);
3216 }
3217
3218 /*
3219 * Check for '|' to separate commands and '"' to start comments.
3220 * Don't do this for ":read !cmd" and ":write !cmd".
3221 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003222 if ((ea.argt & TRLBAR) && !usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223 {
3224 p = arg;
3225 /* ":redir @" is not the start of a comment */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003226 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003227 p += 2;
3228 while (*p)
3229 {
3230 if (*p == Ctrl_V)
3231 {
3232 if (p[1] != NUL)
3233 ++p;
3234 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003235 else if ( (*p == '"' && !(ea.argt & NOTRLCOM))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236 || *p == '|' || *p == '\n')
3237 {
3238 if (*(p - 1) != '\\')
3239 {
3240 if (*p == '|' || *p == '\n')
3241 return p + 1;
3242 return NULL; /* It's a comment */
3243 }
3244 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003245 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246 }
3247 }
3248
3249 /* no arguments allowed */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003250 if (!(ea.argt & EXTRA) && *arg != NUL &&
Bram Moolenaar071d4272004-06-13 20:20:40 +00003251 vim_strchr((char_u *)"|\"", *arg) == NULL)
3252 return NULL;
3253
3254 /* Find start of last argument (argument just before cursor): */
3255 p = buff + STRLEN(buff);
3256 while (p != arg && *p != ' ' && *p != TAB)
3257 p--;
3258 if (*p == ' ' || *p == TAB)
3259 p++;
3260 xp->xp_pattern = p;
3261
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003262 if (ea.argt & XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003263 {
3264 int in_quote = FALSE;
3265 char_u *bow = NULL; /* Beginning of word */
3266
3267 /*
3268 * Allow spaces within back-quotes to count as part of the argument
3269 * being expanded.
3270 */
3271 xp->xp_pattern = skipwhite(arg);
3272 for (p = xp->xp_pattern; *p; )
3273 {
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003274 if (*p == '\\' && p[1] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275 ++p;
3276#ifdef SPACE_IN_FILENAME
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003277 else if (vim_iswhite(*p) && (!(ea.argt & NOSPC) || usefilter))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278#else
3279 else if (vim_iswhite(*p))
3280#endif
3281 {
3282 p = skipwhite(p);
3283 if (in_quote)
3284 bow = p;
3285 else
3286 xp->xp_pattern = p;
3287 --p;
3288 }
3289 else if (*p == '`')
3290 {
3291 if (!in_quote)
3292 {
3293 xp->xp_pattern = p;
3294 bow = p + 1;
3295 }
3296 in_quote = !in_quote;
3297 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003298 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003299 }
3300
3301 /*
3302 * If we are still inside the quotes, and we passed a space, just
3303 * expand from there.
3304 */
3305 if (bow != NULL && in_quote)
3306 xp->xp_pattern = bow;
3307 xp->xp_context = EXPAND_FILES;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003308
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003309#ifndef BACKSLASH_IN_FILENAME
3310 /* For a shell command more chars need to be escaped. */
3311 if (usefilter || ea.cmdidx == CMD_bang)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003312 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003313 xp->xp_shell = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003314
3315 /* When still after the command name expand executables. */
3316 if (xp->xp_pattern == skipwhite(arg))
3317 xp->xp_context = EXPAND_SHELLCMD;
3318 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003319#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320
3321 /* Check for environment variable */
3322 if (*xp->xp_pattern == '$'
3323#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
3324 || *xp->xp_pattern == '%'
3325#endif
3326 )
3327 {
3328 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
3329 if (!vim_isIDc(*p))
3330 break;
3331 if (*p == NUL)
3332 {
3333 xp->xp_context = EXPAND_ENV_VARS;
3334 ++xp->xp_pattern;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003335#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3336 /* Avoid that the assignment uses EXPAND_FILES again. */
Bram Moolenaara466c992005-07-09 21:03:22 +00003337 if (compl != EXPAND_USER_DEFINED && compl != EXPAND_USER_LIST)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003338 compl = EXPAND_ENV_VARS;
3339#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340 }
3341 }
3342 }
3343
3344/*
3345 * 6. switch on command name
3346 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003347 switch (ea.cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348 {
3349 case CMD_cd:
3350 case CMD_chdir:
3351 case CMD_lcd:
3352 case CMD_lchdir:
3353 if (xp->xp_context == EXPAND_FILES)
3354 xp->xp_context = EXPAND_DIRECTORIES;
3355 break;
3356 case CMD_help:
3357 xp->xp_context = EXPAND_HELP;
3358 xp->xp_pattern = arg;
3359 break;
3360
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003361 /* Command modifiers: return the argument.
3362 * Also for commands with an argument that is a command. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363 case CMD_aboveleft:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003364 case CMD_argdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365 case CMD_belowright:
3366 case CMD_botright:
3367 case CMD_browse:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003368 case CMD_bufdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003369 case CMD_confirm:
Bram Moolenaardf177f62005-02-22 08:39:57 +00003370 case CMD_debug:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371 case CMD_folddoclosed:
3372 case CMD_folddoopen:
3373 case CMD_hide:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003374 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375 case CMD_keepjumps:
3376 case CMD_keepmarks:
3377 case CMD_leftabove:
3378 case CMD_lockmarks:
3379 case CMD_rightbelow:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003380 case CMD_sandbox:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381 case CMD_silent:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003382 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383 case CMD_topleft:
3384 case CMD_verbose:
3385 case CMD_vertical:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003386 case CMD_windo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387 return arg;
3388
3389#ifdef FEAT_SEARCH_EXTRA
3390 case CMD_match:
3391 if (*arg == NUL || !ends_excmd(*arg))
3392 {
3393 /* Dummy call to clear variables. */
3394 set_context_in_highlight_cmd(xp, (char_u *)"link n");
3395 xp->xp_context = EXPAND_HIGHLIGHT;
3396 xp->xp_pattern = arg;
3397 arg = skipwhite(skiptowhite(arg));
3398 if (*arg != NUL)
3399 {
3400 xp->xp_context = EXPAND_NOTHING;
3401 arg = skip_regexp(arg + 1, *arg, p_magic, NULL);
3402 }
3403 }
3404 return find_nextcmd(arg);
3405#endif
3406
3407#ifdef FEAT_CMDL_COMPL
3408/*
3409 * All completion for the +cmdline_compl feature goes here.
3410 */
3411
3412# ifdef FEAT_USR_CMDS
3413 case CMD_command:
3414 /* Check for attributes */
3415 while (*arg == '-')
3416 {
3417 arg++; /* Skip "-" */
3418 p = skiptowhite(arg);
3419 if (*p == NUL)
3420 {
3421 /* Cursor is still in the attribute */
3422 p = vim_strchr(arg, '=');
3423 if (p == NULL)
3424 {
3425 /* No "=", so complete attribute names */
3426 xp->xp_context = EXPAND_USER_CMD_FLAGS;
3427 xp->xp_pattern = arg;
3428 return NULL;
3429 }
3430
3431 /* For the -complete and -nargs attributes, we complete
3432 * their arguments as well.
3433 */
3434 if (STRNICMP(arg, "complete", p - arg) == 0)
3435 {
3436 xp->xp_context = EXPAND_USER_COMPLETE;
3437 xp->xp_pattern = p + 1;
3438 return NULL;
3439 }
3440 else if (STRNICMP(arg, "nargs", p - arg) == 0)
3441 {
3442 xp->xp_context = EXPAND_USER_NARGS;
3443 xp->xp_pattern = p + 1;
3444 return NULL;
3445 }
3446 return NULL;
3447 }
3448 arg = skipwhite(p);
3449 }
3450
3451 /* After the attributes comes the new command name */
3452 p = skiptowhite(arg);
3453 if (*p == NUL)
3454 {
3455 xp->xp_context = EXPAND_USER_COMMANDS;
3456 xp->xp_pattern = arg;
3457 break;
3458 }
3459
3460 /* And finally comes a normal command */
3461 return skipwhite(p);
3462
3463 case CMD_delcommand:
3464 xp->xp_context = EXPAND_USER_COMMANDS;
3465 xp->xp_pattern = arg;
3466 break;
3467# endif
3468
3469 case CMD_global:
3470 case CMD_vglobal:
3471 delim = *arg; /* get the delimiter */
3472 if (delim)
3473 ++arg; /* skip delimiter if there is one */
3474
3475 while (arg[0] != NUL && arg[0] != delim)
3476 {
3477 if (arg[0] == '\\' && arg[1] != NUL)
3478 ++arg;
3479 ++arg;
3480 }
3481 if (arg[0] != NUL)
3482 return arg + 1;
3483 break;
3484 case CMD_and:
3485 case CMD_substitute:
3486 delim = *arg;
3487 if (delim)
3488 {
3489 /* skip "from" part */
3490 ++arg;
3491 arg = skip_regexp(arg, delim, p_magic, NULL);
3492 }
3493 /* skip "to" part */
3494 while (arg[0] != NUL && arg[0] != delim)
3495 {
3496 if (arg[0] == '\\' && arg[1] != NUL)
3497 ++arg;
3498 ++arg;
3499 }
3500 if (arg[0] != NUL) /* skip delimiter */
3501 ++arg;
3502 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
3503 ++arg;
3504 if (arg[0] != NUL)
3505 return arg;
3506 break;
3507 case CMD_isearch:
3508 case CMD_dsearch:
3509 case CMD_ilist:
3510 case CMD_dlist:
3511 case CMD_ijump:
3512 case CMD_psearch:
3513 case CMD_djump:
3514 case CMD_isplit:
3515 case CMD_dsplit:
3516 arg = skipwhite(skipdigits(arg)); /* skip count */
3517 if (*arg == '/') /* Match regexp, not just whole words */
3518 {
3519 for (++arg; *arg && *arg != '/'; arg++)
3520 if (*arg == '\\' && arg[1] != NUL)
3521 arg++;
3522 if (*arg)
3523 {
3524 arg = skipwhite(arg + 1);
3525
3526 /* Check for trailing illegal characters */
3527 if (*arg && vim_strchr((char_u *)"|\"\n", *arg) == NULL)
3528 xp->xp_context = EXPAND_NOTHING;
3529 else
3530 return arg;
3531 }
3532 }
3533 break;
3534#ifdef FEAT_AUTOCMD
3535 case CMD_autocmd:
3536 return set_context_in_autocmd(xp, arg, FALSE);
3537
3538 case CMD_doautocmd:
3539 return set_context_in_autocmd(xp, arg, TRUE);
3540#endif
3541 case CMD_set:
3542 set_context_in_set_cmd(xp, arg, 0);
3543 break;
3544 case CMD_setglobal:
3545 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
3546 break;
3547 case CMD_setlocal:
3548 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
3549 break;
3550 case CMD_tag:
3551 case CMD_stag:
3552 case CMD_ptag:
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00003553 case CMD_ltag:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554 case CMD_tselect:
3555 case CMD_stselect:
3556 case CMD_ptselect:
3557 case CMD_tjump:
3558 case CMD_stjump:
3559 case CMD_ptjump:
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003560 if (*p_wop != NUL)
3561 xp->xp_context = EXPAND_TAGS_LISTFILES;
3562 else
3563 xp->xp_context = EXPAND_TAGS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564 xp->xp_pattern = arg;
3565 break;
3566 case CMD_augroup:
3567 xp->xp_context = EXPAND_AUGROUP;
3568 xp->xp_pattern = arg;
3569 break;
3570#ifdef FEAT_SYN_HL
3571 case CMD_syntax:
3572 set_context_in_syntax_cmd(xp, arg);
3573 break;
3574#endif
3575#ifdef FEAT_EVAL
3576 case CMD_let:
3577 case CMD_if:
3578 case CMD_elseif:
3579 case CMD_while:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00003580 case CMD_for:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581 case CMD_echo:
3582 case CMD_echon:
3583 case CMD_execute:
3584 case CMD_echomsg:
3585 case CMD_echoerr:
3586 case CMD_call:
3587 case CMD_return:
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003588 set_context_for_expression(xp, arg, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589 break;
3590
3591 case CMD_unlet:
3592 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3593 arg = xp->xp_pattern + 1;
3594 xp->xp_context = EXPAND_USER_VARS;
3595 xp->xp_pattern = arg;
3596 break;
3597
3598 case CMD_function:
3599 case CMD_delfunction:
3600 xp->xp_context = EXPAND_USER_FUNC;
3601 xp->xp_pattern = arg;
3602 break;
3603
3604 case CMD_echohl:
3605 xp->xp_context = EXPAND_HIGHLIGHT;
3606 xp->xp_pattern = arg;
3607 break;
3608#endif
3609 case CMD_highlight:
3610 set_context_in_highlight_cmd(xp, arg);
3611 break;
3612#ifdef FEAT_LISTCMDS
3613 case CMD_bdelete:
3614 case CMD_bwipeout:
3615 case CMD_bunload:
3616 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3617 arg = xp->xp_pattern + 1;
3618 /*FALLTHROUGH*/
3619 case CMD_buffer:
3620 case CMD_sbuffer:
3621 case CMD_checktime:
3622 xp->xp_context = EXPAND_BUFFERS;
3623 xp->xp_pattern = arg;
3624 break;
3625#endif
3626#ifdef FEAT_USR_CMDS
3627 case CMD_USER:
3628 case CMD_USER_BUF:
3629 if (compl != EXPAND_NOTHING)
3630 {
3631 /* XFILE: file names are handled above */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003632 if (!(ea.argt & XFILE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633 {
3634# ifdef FEAT_MENU
3635 if (compl == EXPAND_MENUS)
3636 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
3637# endif
3638 if (compl == EXPAND_COMMANDS)
3639 return arg;
3640 if (compl == EXPAND_MAPPINGS)
3641 return set_context_in_map_cmd(xp, (char_u *)"map",
3642 arg, forceit, FALSE, FALSE, CMD_map);
3643 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3644 arg = xp->xp_pattern + 1;
3645 xp->xp_pattern = arg;
3646 }
3647 xp->xp_context = compl;
3648 }
3649 break;
3650#endif
3651 case CMD_map: case CMD_noremap:
3652 case CMD_nmap: case CMD_nnoremap:
3653 case CMD_vmap: case CMD_vnoremap:
3654 case CMD_omap: case CMD_onoremap:
3655 case CMD_imap: case CMD_inoremap:
3656 case CMD_cmap: case CMD_cnoremap:
3657 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003658 FALSE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659 case CMD_unmap:
3660 case CMD_nunmap:
3661 case CMD_vunmap:
3662 case CMD_ounmap:
3663 case CMD_iunmap:
3664 case CMD_cunmap:
3665 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003666 FALSE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667 case CMD_abbreviate: case CMD_noreabbrev:
3668 case CMD_cabbrev: case CMD_cnoreabbrev:
3669 case CMD_iabbrev: case CMD_inoreabbrev:
3670 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003671 TRUE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003672 case CMD_unabbreviate:
3673 case CMD_cunabbrev:
3674 case CMD_iunabbrev:
3675 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003676 TRUE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003677#ifdef FEAT_MENU
3678 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
3679 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
3680 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
3681 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
3682 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
3683 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
3684 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
3685 case CMD_tmenu: case CMD_tunmenu:
3686 case CMD_popup: case CMD_tearoff: case CMD_emenu:
3687 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
3688#endif
3689
3690 case CMD_colorscheme:
3691 xp->xp_context = EXPAND_COLORS;
3692 xp->xp_pattern = arg;
3693 break;
3694
3695 case CMD_compiler:
3696 xp->xp_context = EXPAND_COMPILER;
3697 xp->xp_pattern = arg;
3698 break;
3699
3700#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
3701 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
3702 case CMD_language:
3703 if (*skiptowhite(arg) == NUL)
3704 {
3705 xp->xp_context = EXPAND_LANGUAGE;
3706 xp->xp_pattern = arg;
3707 }
3708 else
3709 xp->xp_context = EXPAND_NOTHING;
3710 break;
3711#endif
3712
3713#endif /* FEAT_CMDL_COMPL */
3714
3715 default:
3716 break;
3717 }
3718 return NULL;
3719}
3720
3721/*
3722 * skip a range specifier of the form: addr [,addr] [;addr] ..
3723 *
3724 * Backslashed delimiters after / or ? will be skipped, and commands will
3725 * not be expanded between /'s and ?'s or after "'".
3726 *
Bram Moolenaardf177f62005-02-22 08:39:57 +00003727 * Also skip white space and ":" characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728 * Returns the "cmd" pointer advanced to beyond the range.
3729 */
3730 char_u *
3731skip_range(cmd, ctx)
3732 char_u *cmd;
3733 int *ctx; /* pointer to xp_context or NULL */
3734{
3735 int delim;
3736
Bram Moolenaardf177f62005-02-22 08:39:57 +00003737 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003738 {
3739 if (*cmd == '\'')
3740 {
3741 if (*++cmd == NUL && ctx != NULL)
3742 *ctx = EXPAND_NOTHING;
3743 }
3744 else if (*cmd == '/' || *cmd == '?')
3745 {
3746 delim = *cmd++;
3747 while (*cmd != NUL && *cmd != delim)
3748 if (*cmd++ == '\\' && *cmd != NUL)
3749 ++cmd;
3750 if (*cmd == NUL && ctx != NULL)
3751 *ctx = EXPAND_NOTHING;
3752 }
3753 if (*cmd != NUL)
3754 ++cmd;
3755 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00003756
3757 /* Skip ":" and white space. */
3758 while (*cmd == ':')
3759 cmd = skipwhite(cmd + 1);
3760
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761 return cmd;
3762}
3763
3764/*
3765 * get a single EX address
3766 *
3767 * Set ptr to the next character after the part that was interpreted.
3768 * Set ptr to NULL when an error is encountered.
3769 *
3770 * Return MAXLNUM when no Ex address was found.
3771 */
3772 static linenr_T
3773get_address(ptr, skip, to_other_file)
3774 char_u **ptr;
3775 int skip; /* only skip the address, don't use it */
3776 int to_other_file; /* flag: may jump to other file */
3777{
3778 int c;
3779 int i;
3780 long n;
3781 char_u *cmd;
3782 pos_T pos;
3783 pos_T *fp;
3784 linenr_T lnum;
3785
3786 cmd = skipwhite(*ptr);
3787 lnum = MAXLNUM;
3788 do
3789 {
3790 switch (*cmd)
3791 {
3792 case '.': /* '.' - Cursor position */
3793 ++cmd;
3794 lnum = curwin->w_cursor.lnum;
3795 break;
3796
3797 case '$': /* '$' - last line */
3798 ++cmd;
3799 lnum = curbuf->b_ml.ml_line_count;
3800 break;
3801
3802 case '\'': /* ''' - mark */
3803 if (*++cmd == NUL)
3804 {
3805 cmd = NULL;
3806 goto error;
3807 }
3808 if (skip)
3809 ++cmd;
3810 else
3811 {
3812 /* Only accept a mark in another file when it is
3813 * used by itself: ":'M". */
3814 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
3815 ++cmd;
3816 if (fp == (pos_T *)-1)
3817 /* Jumped to another file. */
3818 lnum = curwin->w_cursor.lnum;
3819 else
3820 {
3821 if (check_mark(fp) == FAIL)
3822 {
3823 cmd = NULL;
3824 goto error;
3825 }
3826 lnum = fp->lnum;
3827 }
3828 }
3829 break;
3830
3831 case '/':
3832 case '?': /* '/' or '?' - search */
3833 c = *cmd++;
3834 if (skip) /* skip "/pat/" */
3835 {
3836 cmd = skip_regexp(cmd, c, (int)p_magic, NULL);
3837 if (*cmd == c)
3838 ++cmd;
3839 }
3840 else
3841 {
3842 pos = curwin->w_cursor; /* save curwin->w_cursor */
3843 /*
3844 * When '/' or '?' follows another address, start
3845 * from there.
3846 */
3847 if (lnum != MAXLNUM)
3848 curwin->w_cursor.lnum = lnum;
3849 /*
3850 * Start a forward search at the end of the line.
3851 * Start a backward search at the start of the line.
3852 * This makes sure we never match in the current
3853 * line, and can match anywhere in the
3854 * next/previous line.
3855 */
3856 if (c == '/')
3857 curwin->w_cursor.col = MAXCOL;
3858 else
3859 curwin->w_cursor.col = 0;
3860 searchcmdlen = 0;
3861 if (!do_search(NULL, c, cmd, 1L,
3862 SEARCH_HIS + SEARCH_MSG + SEARCH_START))
3863 {
3864 curwin->w_cursor = pos;
3865 cmd = NULL;
3866 goto error;
3867 }
3868 lnum = curwin->w_cursor.lnum;
3869 curwin->w_cursor = pos;
3870 /* adjust command string pointer */
3871 cmd += searchcmdlen;
3872 }
3873 break;
3874
3875 case '\\': /* "\?", "\/" or "\&", repeat search */
3876 ++cmd;
3877 if (*cmd == '&')
3878 i = RE_SUBST;
3879 else if (*cmd == '?' || *cmd == '/')
3880 i = RE_SEARCH;
3881 else
3882 {
3883 EMSG(_(e_backslash));
3884 cmd = NULL;
3885 goto error;
3886 }
3887
3888 if (!skip)
3889 {
3890 /*
3891 * When search follows another address, start from
3892 * there.
3893 */
3894 if (lnum != MAXLNUM)
3895 pos.lnum = lnum;
3896 else
3897 pos.lnum = curwin->w_cursor.lnum;
3898
3899 /*
3900 * Start the search just like for the above
3901 * do_search().
3902 */
3903 if (*cmd != '?')
3904 pos.col = MAXCOL;
3905 else
3906 pos.col = 0;
3907 if (searchit(curwin, curbuf, &pos,
3908 *cmd == '?' ? BACKWARD : FORWARD,
3909 (char_u *)"", 1L,
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003910 SEARCH_MSG + SEARCH_START,
3911 i, (linenr_T)0) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003912 lnum = pos.lnum;
3913 else
3914 {
3915 cmd = NULL;
3916 goto error;
3917 }
3918 }
3919 ++cmd;
3920 break;
3921
3922 default:
3923 if (VIM_ISDIGIT(*cmd)) /* absolute line number */
3924 lnum = getdigits(&cmd);
3925 }
3926
3927 for (;;)
3928 {
3929 cmd = skipwhite(cmd);
3930 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
3931 break;
3932
3933 if (lnum == MAXLNUM)
3934 lnum = curwin->w_cursor.lnum; /* "+1" is same as ".+1" */
3935 if (VIM_ISDIGIT(*cmd))
3936 i = '+'; /* "number" is same as "+number" */
3937 else
3938 i = *cmd++;
3939 if (!VIM_ISDIGIT(*cmd)) /* '+' is '+1', but '+0' is not '+1' */
3940 n = 1;
3941 else
3942 n = getdigits(&cmd);
3943 if (i == '-')
3944 lnum -= n;
3945 else
3946 lnum += n;
3947 }
3948 } while (*cmd == '/' || *cmd == '?');
3949
3950error:
3951 *ptr = cmd;
3952 return lnum;
3953}
3954
3955/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00003956 * Get flags from an Ex command argument.
3957 */
3958 static void
3959get_flags(eap)
3960 exarg_T *eap;
3961{
3962 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
3963 {
3964 if (*eap->arg == 'l')
3965 eap->flags |= EXFLAG_LIST;
3966 else if (*eap->arg == 'p')
3967 eap->flags |= EXFLAG_PRINT;
3968 else
3969 eap->flags |= EXFLAG_NR;
3970 eap->arg = skipwhite(eap->arg + 1);
3971 }
3972}
3973
3974/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975 * Function called for command which is Not Implemented. NI!
3976 */
3977 void
3978ex_ni(eap)
3979 exarg_T *eap;
3980{
3981 if (!eap->skip)
3982 eap->errmsg = (char_u *)N_("E319: Sorry, the command is not available in this version");
3983}
3984
3985#if !defined(FEAT_PERL) || !defined(FEAT_PYTHON) || !defined(FEAT_TCL) \
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003986 || !defined(FEAT_RUBY) || !defined(FEAT_MZSCHEME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003987/*
3988 * Function called for script command which is Not Implemented. NI!
3989 * Skips over ":perl <<EOF" constructs.
3990 */
3991 static void
3992ex_script_ni(eap)
3993 exarg_T *eap;
3994{
3995 if (!eap->skip)
3996 ex_ni(eap);
3997 else
3998 vim_free(script_get(eap, eap->arg));
3999}
4000#endif
4001
4002/*
4003 * Check range in Ex command for validity.
4004 * Return NULL when valid, error message when invalid.
4005 */
4006 static char_u *
4007invalid_range(eap)
4008 exarg_T *eap;
4009{
4010 if ( eap->line1 < 0
4011 || eap->line2 < 0
4012 || eap->line1 > eap->line2
4013 || ((eap->argt & RANGE)
4014 && !(eap->argt & NOTADR)
4015 && eap->line2 > curbuf->b_ml.ml_line_count
4016#ifdef FEAT_DIFF
4017 + (eap->cmdidx == CMD_diffget)
4018#endif
4019 ))
4020 return (char_u *)_(e_invrange);
4021 return NULL;
4022}
4023
4024/*
4025 * Correct the range for zero line number, if required.
4026 */
4027 static void
4028correct_range(eap)
4029 exarg_T *eap;
4030{
4031 if (!(eap->argt & ZEROR)) /* zero in range not allowed */
4032 {
4033 if (eap->line1 == 0)
4034 eap->line1 = 1;
4035 if (eap->line2 == 0)
4036 eap->line2 = 1;
4037 }
4038}
4039
Bram Moolenaar748bf032005-02-02 23:04:36 +00004040#ifdef FEAT_QUICKFIX
4041static char_u *skip_grep_pat __ARGS((exarg_T *eap));
4042
4043/*
4044 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4045 * pattern. Otherwise return eap->arg.
4046 */
4047 static char_u *
4048skip_grep_pat(eap)
4049 exarg_T *eap;
4050{
4051 char_u *p = eap->arg;
4052
Bram Moolenaara37420f2006-02-04 22:37:47 +00004053 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4054 || eap->cmdidx == CMD_vimgrepadd
4055 || eap->cmdidx == CMD_lvimgrepadd
4056 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004057 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004058 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004059 if (p == NULL)
4060 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004061 }
4062 return p;
4063}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004064
4065/*
4066 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4067 * in the command line, so that things like % get expanded.
4068 */
4069 static char_u *
4070replace_makeprg(eap, p, cmdlinep)
4071 exarg_T *eap;
4072 char_u *p;
4073 char_u **cmdlinep;
4074{
4075 char_u *new_cmdline;
4076 char_u *program;
4077 char_u *pos;
4078 char_u *ptr;
4079 int len;
4080 int i;
4081
4082 /*
4083 * Don't do it when ":vimgrep" is used for ":grep".
4084 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004085 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4086 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4087 || eap->cmdidx == CMD_grepadd
4088 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004089 && !grep_internal(eap->cmdidx))
4090 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004091 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4092 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004093 {
4094 if (*curbuf->b_p_gp == NUL)
4095 program = p_gp;
4096 else
4097 program = curbuf->b_p_gp;
4098 }
4099 else
4100 {
4101 if (*curbuf->b_p_mp == NUL)
4102 program = p_mp;
4103 else
4104 program = curbuf->b_p_mp;
4105 }
4106
4107 p = skipwhite(p);
4108
4109 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4110 {
4111 /* replace $* by given arguments */
4112 i = 1;
4113 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4114 ++i;
4115 len = (int)STRLEN(p);
4116 new_cmdline = alloc((int)(STRLEN(program) + i * (len - 2) + 1));
4117 if (new_cmdline == NULL)
4118 return NULL; /* out of memory */
4119 ptr = new_cmdline;
4120 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4121 {
4122 i = (int)(pos - program);
4123 STRNCPY(ptr, program, i);
4124 STRCPY(ptr += i, p);
4125 ptr += len;
4126 program = pos + 2;
4127 }
4128 STRCPY(ptr, program);
4129 }
4130 else
4131 {
4132 new_cmdline = alloc((int)(STRLEN(program) + STRLEN(p) + 2));
4133 if (new_cmdline == NULL)
4134 return NULL; /* out of memory */
4135 STRCPY(new_cmdline, program);
4136 STRCAT(new_cmdline, " ");
4137 STRCAT(new_cmdline, p);
4138 }
4139 msg_make(p);
4140
4141 /* 'eap->cmd' is not set here, because it is not used at CMD_make */
4142 vim_free(*cmdlinep);
4143 *cmdlinep = new_cmdline;
4144 p = new_cmdline;
4145 }
4146 return p;
4147}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004148#endif
4149
Bram Moolenaar071d4272004-06-13 20:20:40 +00004150/*
4151 * Expand file name in Ex command argument.
4152 * Return FAIL for failure, OK otherwise.
4153 */
4154 int
4155expand_filename(eap, cmdlinep, errormsgp)
4156 exarg_T *eap;
4157 char_u **cmdlinep;
4158 char_u **errormsgp;
4159{
4160 int has_wildcards; /* need to expand wildcards */
4161 char_u *repl;
4162 int srclen;
4163 char_u *p;
4164 int n;
4165
Bram Moolenaar748bf032005-02-02 23:04:36 +00004166#ifdef FEAT_QUICKFIX
4167 /* Skip a regexp pattern for ":vimgrep[add] pat file..." */
4168 p = skip_grep_pat(eap);
4169#else
4170 p = eap->arg;
4171#endif
4172
Bram Moolenaar071d4272004-06-13 20:20:40 +00004173 /*
4174 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4175 * the file name contains a wildcard it should not cause expanding.
4176 * (it will be expanded anyway if there is a wildcard before replacing).
4177 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004178 has_wildcards = mch_has_wildcard(p);
4179 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004181#ifdef FEAT_EVAL
4182 /* Skip over `=expr`, wildcards in it are not expanded. */
4183 if (p[0] == '`' && p[1] == '=')
4184 {
4185 p += 2;
4186 (void)skip_expr(&p);
4187 if (*p == '`')
4188 ++p;
4189 continue;
4190 }
4191#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192 /*
4193 * Quick check if this cannot be the start of a special string.
4194 * Also removes backslash before '%', '#' and '<'.
4195 */
4196 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4197 {
4198 ++p;
4199 continue;
4200 }
4201
4202 /*
4203 * Try to find a match at this position.
4204 */
4205 repl = eval_vars(p, &srclen, &(eap->do_ecmd_lnum), errormsgp, eap->arg);
4206 if (*errormsgp != NULL) /* error detected */
4207 return FAIL;
4208 if (repl == NULL) /* no match found */
4209 {
4210 p += srclen;
4211 continue;
4212 }
4213
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004214 /* Wildcards won't be expanded below, the replacement is taken
4215 * literally. But do expand "~/file", "~user/file" and "$HOME/file". */
4216 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4217 {
4218 char_u *l = repl;
4219
4220 repl = expand_env_save(repl);
4221 vim_free(l);
4222 }
4223
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224 /* Need to escape white space et al. with a backslash. Don't do this
4225 * for shell commands (may have to use quotes instead). Don't do this
4226 * for non-unix systems when there is a single argument (spaces don't
4227 * separate arguments then). */
4228 if (!eap->usefilter
4229 && eap->cmdidx != CMD_bang
4230 && eap->cmdidx != CMD_make
Bram Moolenaara37420f2006-02-04 22:37:47 +00004231 && eap->cmdidx != CMD_lmake
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232 && eap->cmdidx != CMD_grep
Bram Moolenaara37420f2006-02-04 22:37:47 +00004233 && eap->cmdidx != CMD_lgrep
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234 && eap->cmdidx != CMD_grepadd
Bram Moolenaara37420f2006-02-04 22:37:47 +00004235 && eap->cmdidx != CMD_lgrepadd
Bram Moolenaar071d4272004-06-13 20:20:40 +00004236#ifndef UNIX
4237 && !(eap->argt & NOSPC)
4238#endif
4239 )
4240 {
4241 char_u *l;
4242#ifdef BACKSLASH_IN_FILENAME
4243 /* Don't escape a backslash here, because rem_backslash() doesn't
4244 * remove it later. */
4245 static char_u *nobslash = (char_u *)" \t\"|";
4246# define ESCAPE_CHARS nobslash
4247#else
4248# define ESCAPE_CHARS escape_chars
4249#endif
4250
4251 for (l = repl; *l; ++l)
4252 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
4253 {
4254 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
4255 if (l != NULL)
4256 {
4257 vim_free(repl);
4258 repl = l;
4259 }
4260 break;
4261 }
4262 }
4263
4264 /* For a shell command a '!' must be escaped. */
4265 if ((eap->usefilter || eap->cmdidx == CMD_bang)
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004266 && vim_strpbrk(repl, (char_u *)"!&;()<>") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004267 {
4268 char_u *l;
4269
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004270 l = vim_strsave_escaped(repl, (char_u *)"!&;()<>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271 if (l != NULL)
4272 {
4273 vim_free(repl);
4274 repl = l;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004275 /* For a sh-like shell escape "!" another time. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004276 if (strstr((char *)p_sh, "sh") != NULL)
4277 {
4278 l = vim_strsave_escaped(repl, (char_u *)"!");
4279 if (l != NULL)
4280 {
4281 vim_free(repl);
4282 repl = l;
4283 }
4284 }
4285 }
4286 }
4287
4288 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
4289 vim_free(repl);
4290 if (p == NULL)
4291 return FAIL;
4292 }
4293
4294 /*
4295 * One file argument: Expand wildcards.
4296 * Don't do this with ":r !command" or ":w !command".
4297 */
4298 if ((eap->argt & NOSPC) && !eap->usefilter)
4299 {
4300 /*
4301 * May do this twice:
4302 * 1. Replace environment variables.
4303 * 2. Replace any other wildcards, remove backslashes.
4304 */
4305 for (n = 1; n <= 2; ++n)
4306 {
4307 if (n == 2)
4308 {
4309#ifdef UNIX
4310 /*
4311 * Only for Unix we check for more than one file name.
4312 * For other systems spaces are considered to be part
4313 * of the file name.
4314 * Only check here if there is no wildcard, otherwise
4315 * ExpandOne() will check for errors. This allows
4316 * ":e `ls ve*.c`" on Unix.
4317 */
4318 if (!has_wildcards)
4319 for (p = eap->arg; *p; ++p)
4320 {
4321 /* skip escaped characters */
4322 if (p[1] && (*p == '\\' || *p == Ctrl_V))
4323 ++p;
4324 else if (vim_iswhite(*p))
4325 {
4326 *errormsgp = (char_u *)_("E172: Only one file name allowed");
4327 return FAIL;
4328 }
4329 }
4330#endif
4331
4332 /*
4333 * Halve the number of backslashes (this is Vi compatible).
4334 * For Unix and OS/2, when wildcards are expanded, this is
4335 * done by ExpandOne() below.
4336 */
4337#if defined(UNIX) || defined(OS2)
4338 if (!has_wildcards)
4339#endif
4340 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004341 }
4342
4343 if (has_wildcards)
4344 {
4345 if (n == 1)
4346 {
4347 /*
4348 * First loop: May expand environment variables. This
4349 * can be done much faster with expand_env() than with
4350 * something else (e.g., calling a shell).
4351 * After expanding environment variables, check again
4352 * if there are still wildcards present.
4353 */
4354 if (vim_strchr(eap->arg, '$') != NULL
4355 || vim_strchr(eap->arg, '~') != NULL)
4356 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00004357 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
4358 TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004359 has_wildcards = mch_has_wildcard(NameBuff);
4360 p = NameBuff;
4361 }
4362 else
4363 p = NULL;
4364 }
4365 else /* n == 2 */
4366 {
4367 expand_T xpc;
4368
4369 ExpandInit(&xpc);
4370 xpc.xp_context = EXPAND_FILES;
4371 p = ExpandOne(&xpc, eap->arg, NULL,
4372 WILD_LIST_NOTFOUND|WILD_ADD_SLASH,
4373 WILD_EXPAND_FREE);
4374 ExpandCleanup(&xpc);
4375 if (p == NULL)
4376 return FAIL;
4377 }
4378 if (p != NULL)
4379 {
4380 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
4381 p, cmdlinep);
4382 if (n == 2) /* p came from ExpandOne() */
4383 vim_free(p);
4384 }
4385 }
4386 }
4387 }
4388 return OK;
4389}
4390
4391/*
4392 * Replace part of the command line, keeping eap->cmd, eap->arg and
4393 * eap->nextcmd correct.
4394 * "src" points to the part that is to be replaced, of length "srclen".
4395 * "repl" is the replacement string.
4396 * Returns a pointer to the character after the replaced string.
4397 * Returns NULL for failure.
4398 */
4399 static char_u *
4400repl_cmdline(eap, src, srclen, repl, cmdlinep)
4401 exarg_T *eap;
4402 char_u *src;
4403 int srclen;
4404 char_u *repl;
4405 char_u **cmdlinep;
4406{
4407 int len;
4408 int i;
4409 char_u *new_cmdline;
4410
4411 /*
4412 * The new command line is build in new_cmdline[].
4413 * First allocate it.
4414 * Careful: a "+cmd" argument may have been NUL terminated.
4415 */
4416 len = (int)STRLEN(repl);
4417 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004418 if (eap->nextcmd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004419 i += (int)STRLEN(eap->nextcmd);/* add space for next command */
4420 if ((new_cmdline = alloc((unsigned)i)) == NULL)
4421 return NULL; /* out of memory! */
4422
4423 /*
4424 * Copy the stuff before the expanded part.
4425 * Copy the expanded stuff.
4426 * Copy what came after the expanded part.
4427 * Copy the next commands, if there are any.
4428 */
4429 i = (int)(src - *cmdlinep); /* length of part before match */
4430 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00004431
Bram Moolenaar071d4272004-06-13 20:20:40 +00004432 mch_memmove(new_cmdline + i, repl, (size_t)len);
4433 i += len; /* remember the end of the string */
4434 STRCPY(new_cmdline + i, src + srclen);
4435 src = new_cmdline + i; /* remember where to continue */
4436
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004437 if (eap->nextcmd != NULL) /* append next command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004438 {
4439 i = (int)STRLEN(new_cmdline) + 1;
4440 STRCPY(new_cmdline + i, eap->nextcmd);
4441 eap->nextcmd = new_cmdline + i;
4442 }
4443 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
4444 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
4445 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
4446 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
4447 vim_free(*cmdlinep);
4448 *cmdlinep = new_cmdline;
4449
4450 return src;
4451}
4452
4453/*
4454 * Check for '|' to separate commands and '"' to start comments.
4455 */
4456 void
4457separate_nextcmd(eap)
4458 exarg_T *eap;
4459{
4460 char_u *p;
4461
Bram Moolenaar86b68352004-12-27 21:59:20 +00004462#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004463 p = skip_grep_pat(eap);
4464#else
4465 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004466#endif
4467
4468 for ( ; *p; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004469 {
4470 if (*p == Ctrl_V)
4471 {
4472 if (eap->argt & (USECTRLV | XFILE))
4473 ++p; /* skip CTRL-V and next char */
4474 else
4475 STRCPY(p, p + 1); /* remove CTRL-V and skip next char */
4476 if (*p == NUL) /* stop at NUL after CTRL-V */
4477 break;
4478 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004479
4480#ifdef FEAT_EVAL
4481 /* Skip over `=expr` when wildcards are expanded. */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004482 else if (p[0] == '`' && p[1] == '=' && (eap->argt & XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004483 {
4484 p += 2;
4485 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004486 }
4487#endif
4488
Bram Moolenaar071d4272004-06-13 20:20:40 +00004489 /* Check for '"': start of comment or '|': next command */
4490 /* :@" and :*" do not start a comment!
4491 * :redir @" doesn't either. */
4492 else if ((*p == '"' && !(eap->argt & NOTRLCOM)
4493 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
4494 || p != eap->arg)
4495 && (eap->cmdidx != CMD_redir
4496 || p != eap->arg + 1 || p[-1] != '@'))
4497 || *p == '|' || *p == '\n')
4498 {
4499 /*
4500 * We remove the '\' before the '|', unless USECTRLV is used
4501 * AND 'b' is present in 'cpoptions'.
4502 */
4503 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
4504 || !(eap->argt & USECTRLV)) && *(p - 1) == '\\')
4505 {
4506 mch_memmove(p - 1, p, STRLEN(p) + 1); /* remove the '\' */
4507 --p;
4508 }
4509 else
4510 {
4511 eap->nextcmd = check_nextcmd(p);
4512 *p = NUL;
4513 break;
4514 }
4515 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004516 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004517
Bram Moolenaar071d4272004-06-13 20:20:40 +00004518 if (!(eap->argt & NOTRLCOM)) /* remove trailing spaces */
4519 del_trailing_spaces(eap->arg);
4520}
4521
4522/*
4523 * get + command from ex argument
4524 */
4525 static char_u *
4526getargcmd(argp)
4527 char_u **argp;
4528{
4529 char_u *arg = *argp;
4530 char_u *command = NULL;
4531
4532 if (*arg == '+') /* +[command] */
4533 {
4534 ++arg;
4535 if (vim_isspace(*arg))
4536 command = dollar_command;
4537 else
4538 {
4539 command = arg;
4540 arg = skip_cmd_arg(command, TRUE);
4541 if (*arg != NUL)
4542 *arg++ = NUL; /* terminate command with NUL */
4543 }
4544
4545 arg = skipwhite(arg); /* skip over spaces */
4546 *argp = arg;
4547 }
4548 return command;
4549}
4550
4551/*
4552 * Find end of "+command" argument. Skip over "\ " and "\\".
4553 */
4554 static char_u *
4555skip_cmd_arg(p, rembs)
4556 char_u *p;
4557 int rembs; /* TRUE to halve the number of backslashes */
4558{
4559 while (*p && !vim_isspace(*p))
4560 {
4561 if (*p == '\\' && p[1] != NUL)
4562 {
4563 if (rembs)
4564 mch_memmove(p, p + 1, STRLEN(p));
4565 else
4566 ++p;
4567 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004568 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004569 }
4570 return p;
4571}
4572
4573/*
4574 * Get "++opt=arg" argument.
4575 * Return FAIL or OK.
4576 */
4577 static int
4578getargopt(eap)
4579 exarg_T *eap;
4580{
4581 char_u *arg = eap->arg + 2;
4582 int *pp = NULL;
4583#ifdef FEAT_MBYTE
4584 char_u *p;
4585#endif
4586
4587 /* ":edit ++[no]bin[ary] file" */
4588 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
4589 {
4590 if (*arg == 'n')
4591 {
4592 arg += 2;
4593 eap->force_bin = FORCE_NOBIN;
4594 }
4595 else
4596 eap->force_bin = FORCE_BIN;
4597 if (!checkforcmd(&arg, "binary", 3))
4598 return FAIL;
4599 eap->arg = skipwhite(arg);
4600 return OK;
4601 }
4602
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004603 /* ":read ++edit file" */
4604 if (STRNCMP(arg, "edit", 4) == 0)
4605 {
4606 eap->read_edit = TRUE;
4607 eap->arg = skipwhite(arg + 4);
4608 return OK;
4609 }
4610
Bram Moolenaar071d4272004-06-13 20:20:40 +00004611 if (STRNCMP(arg, "ff", 2) == 0)
4612 {
4613 arg += 2;
4614 pp = &eap->force_ff;
4615 }
4616 else if (STRNCMP(arg, "fileformat", 10) == 0)
4617 {
4618 arg += 10;
4619 pp = &eap->force_ff;
4620 }
4621#ifdef FEAT_MBYTE
4622 else if (STRNCMP(arg, "enc", 3) == 0)
4623 {
4624 arg += 3;
4625 pp = &eap->force_enc;
4626 }
4627 else if (STRNCMP(arg, "encoding", 8) == 0)
4628 {
4629 arg += 8;
4630 pp = &eap->force_enc;
4631 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004632 else if (STRNCMP(arg, "bad", 3) == 0)
4633 {
4634 arg += 3;
4635 pp = &eap->bad_char;
4636 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004637#endif
4638
4639 if (pp == NULL || *arg != '=')
4640 return FAIL;
4641
4642 ++arg;
4643 *pp = (int)(arg - eap->cmd);
4644 arg = skip_cmd_arg(arg, FALSE);
4645 eap->arg = skipwhite(arg);
4646 *arg = NUL;
4647
4648#ifdef FEAT_MBYTE
4649 if (pp == &eap->force_ff)
4650 {
4651#endif
4652 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
4653 return FAIL;
4654#ifdef FEAT_MBYTE
4655 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004656 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004657 {
4658 /* Make 'fileencoding' lower case. */
4659 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
4660 *p = TOLOWER_ASC(*p);
4661 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004662 else
4663 {
4664 /* Check ++bad= argument. Must be a single-byte character, "keep" or
4665 * "drop". */
4666 p = eap->cmd + eap->bad_char;
4667 if (STRICMP(p, "keep") == 0)
4668 eap->bad_char = BAD_KEEP;
4669 else if (STRICMP(p, "drop") == 0)
4670 eap->bad_char = BAD_DROP;
4671 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
4672 eap->bad_char = *p;
4673 else
4674 return FAIL;
4675 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004676#endif
4677
4678 return OK;
4679}
4680
4681/*
4682 * ":abbreviate" and friends.
4683 */
4684 static void
4685ex_abbreviate(eap)
4686 exarg_T *eap;
4687{
4688 do_exmap(eap, TRUE); /* almost the same as mapping */
4689}
4690
4691/*
4692 * ":map" and friends.
4693 */
4694 static void
4695ex_map(eap)
4696 exarg_T *eap;
4697{
4698 /*
4699 * If we are sourcing .exrc or .vimrc in current directory we
4700 * print the mappings for security reasons.
4701 */
4702 if (secure)
4703 {
4704 secure = 2;
4705 msg_outtrans(eap->cmd);
4706 msg_putchar('\n');
4707 }
4708 do_exmap(eap, FALSE);
4709}
4710
4711/*
4712 * ":unmap" and friends.
4713 */
4714 static void
4715ex_unmap(eap)
4716 exarg_T *eap;
4717{
4718 do_exmap(eap, FALSE);
4719}
4720
4721/*
4722 * ":mapclear" and friends.
4723 */
4724 static void
4725ex_mapclear(eap)
4726 exarg_T *eap;
4727{
4728 map_clear(eap->cmd, eap->arg, eap->forceit, FALSE);
4729}
4730
4731/*
4732 * ":abclear" and friends.
4733 */
4734 static void
4735ex_abclear(eap)
4736 exarg_T *eap;
4737{
4738 map_clear(eap->cmd, eap->arg, TRUE, TRUE);
4739}
4740
4741#ifdef FEAT_AUTOCMD
4742 static void
4743ex_autocmd(eap)
4744 exarg_T *eap;
4745{
4746 /*
4747 * Disallow auto commands from .exrc and .vimrc in current
4748 * directory for security reasons.
4749 */
4750 if (secure)
4751 {
4752 secure = 2;
4753 eap->errmsg = e_curdir;
4754 }
4755 else if (eap->cmdidx == CMD_autocmd)
4756 do_autocmd(eap->arg, eap->forceit);
4757 else
4758 do_augroup(eap->arg, eap->forceit);
4759}
4760
4761/*
4762 * ":doautocmd": Apply the automatic commands to the current buffer.
4763 */
4764 static void
4765ex_doautocmd(eap)
4766 exarg_T *eap;
4767{
4768 (void)do_doautocmd(eap->arg, TRUE);
Bram Moolenaara3227e22006-03-08 21:32:40 +00004769 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770}
4771#endif
4772
4773#ifdef FEAT_LISTCMDS
4774/*
4775 * :[N]bunload[!] [N] [bufname] unload buffer
4776 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
4777 * :[N]bwipeout[!] [N] [bufname] delete buffer really
4778 */
4779 static void
4780ex_bunload(eap)
4781 exarg_T *eap;
4782{
4783 eap->errmsg = do_bufdel(
4784 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
4785 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
4786 : DOBUF_UNLOAD, eap->arg,
4787 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
4788}
4789
4790/*
4791 * :[N]buffer [N] to buffer N
4792 * :[N]sbuffer [N] to buffer N
4793 */
4794 static void
4795ex_buffer(eap)
4796 exarg_T *eap;
4797{
4798 if (*eap->arg)
4799 eap->errmsg = e_trailing;
4800 else
4801 {
4802 if (eap->addr_count == 0) /* default is current buffer */
4803 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
4804 else
4805 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
4806 }
4807}
4808
4809/*
4810 * :[N]bmodified [N] to next mod. buffer
4811 * :[N]sbmodified [N] to next mod. buffer
4812 */
4813 static void
4814ex_bmodified(eap)
4815 exarg_T *eap;
4816{
4817 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
4818}
4819
4820/*
4821 * :[N]bnext [N] to next buffer
4822 * :[N]sbnext [N] split and to next buffer
4823 */
4824 static void
4825ex_bnext(eap)
4826 exarg_T *eap;
4827{
4828 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
4829}
4830
4831/*
4832 * :[N]bNext [N] to previous buffer
4833 * :[N]bprevious [N] to previous buffer
4834 * :[N]sbNext [N] split and to previous buffer
4835 * :[N]sbprevious [N] split and to previous buffer
4836 */
4837 static void
4838ex_bprevious(eap)
4839 exarg_T *eap;
4840{
4841 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
4842}
4843
4844/*
4845 * :brewind to first buffer
4846 * :bfirst to first buffer
4847 * :sbrewind split and to first buffer
4848 * :sbfirst split and to first buffer
4849 */
4850 static void
4851ex_brewind(eap)
4852 exarg_T *eap;
4853{
4854 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
4855}
4856
4857/*
4858 * :blast to last buffer
4859 * :sblast split and to last buffer
4860 */
4861 static void
4862ex_blast(eap)
4863 exarg_T *eap;
4864{
4865 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
4866}
4867#endif
4868
4869 int
4870ends_excmd(c)
4871 int c;
4872{
4873 return (c == NUL || c == '|' || c == '"' || c == '\n');
4874}
4875
4876#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
4877 || defined(PROTO)
4878/*
4879 * Return the next command, after the first '|' or '\n'.
4880 * Return NULL if not found.
4881 */
4882 char_u *
4883find_nextcmd(p)
4884 char_u *p;
4885{
4886 while (*p != '|' && *p != '\n')
4887 {
4888 if (*p == NUL)
4889 return NULL;
4890 ++p;
4891 }
4892 return (p + 1);
4893}
4894#endif
4895
4896/*
4897 * Check if *p is a separator between Ex commands.
4898 * Return NULL if it isn't, (p + 1) if it is.
4899 */
4900 char_u *
4901check_nextcmd(p)
4902 char_u *p;
4903{
4904 p = skipwhite(p);
4905 if (*p == '|' || *p == '\n')
4906 return (p + 1);
4907 else
4908 return NULL;
4909}
4910
4911/*
4912 * - if there are more files to edit
4913 * - and this is the last window
4914 * - and forceit not used
4915 * - and not repeated twice on a row
4916 * return FAIL and give error message if 'message' TRUE
4917 * return OK otherwise
4918 */
4919 static int
4920check_more(message, forceit)
4921 int message; /* when FALSE check only, no messages */
4922 int forceit;
4923{
4924 int n = ARGCOUNT - curwin->w_arg_idx - 1;
4925
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00004926 if (!forceit && only_one_window()
4927 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004928 {
4929 if (message)
4930 {
4931#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
4932 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
4933 {
4934 char_u buff[IOSIZE];
4935
4936 if (n == 1)
4937 STRCPY(buff, _("1 more file to edit. Quit anyway?"));
4938 else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004939 vim_snprintf((char *)buff, IOSIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004940 _("%d more files to edit. Quit anyway?"), n);
4941 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
4942 return OK;
4943 return FAIL;
4944 }
4945#endif
4946 if (n == 1)
4947 EMSG(_("E173: 1 more file to edit"));
4948 else
4949 EMSGN(_("E173: %ld more files to edit"), n);
4950 quitmore = 2; /* next try to quit is allowed */
4951 }
4952 return FAIL;
4953 }
4954 return OK;
4955}
4956
4957#ifdef FEAT_CMDL_COMPL
4958/*
4959 * Function given to ExpandGeneric() to obtain the list of command names.
4960 */
4961/*ARGSUSED*/
4962 char_u *
4963get_command_name(xp, idx)
4964 expand_T *xp;
4965 int idx;
4966{
4967 if (idx >= (int)CMD_SIZE)
4968# ifdef FEAT_USR_CMDS
4969 return get_user_command_name(idx);
4970# else
4971 return NULL;
4972# endif
4973 return cmdnames[idx].cmd_name;
4974}
4975#endif
4976
4977#if defined(FEAT_USR_CMDS) || defined(PROTO)
4978static 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));
4979static void uc_list __ARGS((char_u *name, size_t name_len));
4980static int uc_scan_attr __ARGS((char_u *attr, size_t len, long *argt, long *def, int *flags, int *compl, char_u **compl_arg));
4981static char_u *uc_split_args __ARGS((char_u *arg, size_t *lenp));
4982static 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));
4983
4984 static int
4985uc_add_command(name, name_len, rep, argt, def, flags, compl, compl_arg, force)
4986 char_u *name;
4987 size_t name_len;
4988 char_u *rep;
4989 long argt;
4990 long def;
4991 int flags;
4992 int compl;
4993 char_u *compl_arg;
4994 int force;
4995{
4996 ucmd_T *cmd = NULL;
4997 char_u *p;
4998 int i;
4999 int cmp = 1;
5000 char_u *rep_buf = NULL;
5001 garray_T *gap;
5002
5003 replace_termcodes(rep, &rep_buf, FALSE, FALSE);
5004 if (rep_buf == NULL)
5005 {
5006 /* Can't replace termcodes - try using the string as is */
5007 rep_buf = vim_strsave(rep);
5008
5009 /* Give up if out of memory */
5010 if (rep_buf == NULL)
5011 return FAIL;
5012 }
5013
5014 /* get address of growarray: global or in curbuf */
5015 if (flags & UC_BUFFER)
5016 {
5017 gap = &curbuf->b_ucmds;
5018 if (gap->ga_itemsize == 0)
5019 ga_init2(gap, (int)sizeof(ucmd_T), 4);
5020 }
5021 else
5022 gap = &ucmds;
5023
5024 /* Search for the command in the already defined commands. */
5025 for (i = 0; i < gap->ga_len; ++i)
5026 {
5027 size_t len;
5028
5029 cmd = USER_CMD_GA(gap, i);
5030 len = STRLEN(cmd->uc_name);
5031 cmp = STRNCMP(name, cmd->uc_name, name_len);
5032 if (cmp == 0)
5033 {
5034 if (name_len < len)
5035 cmp = -1;
5036 else if (name_len > len)
5037 cmp = 1;
5038 }
5039
5040 if (cmp == 0)
5041 {
5042 if (!force)
5043 {
5044 EMSG(_("E174: Command already exists: add ! to replace it"));
5045 goto fail;
5046 }
5047
5048 vim_free(cmd->uc_rep);
5049 cmd->uc_rep = 0;
5050 break;
5051 }
5052
5053 /* Stop as soon as we pass the name to add */
5054 if (cmp < 0)
5055 break;
5056 }
5057
5058 /* Extend the array unless we're replacing an existing command */
5059 if (cmp != 0)
5060 {
5061 if (ga_grow(gap, 1) != OK)
5062 goto fail;
5063 if ((p = vim_strnsave(name, (int)name_len)) == NULL)
5064 goto fail;
5065
5066 cmd = USER_CMD_GA(gap, i);
5067 mch_memmove(cmd + 1, cmd, (gap->ga_len - i) * sizeof(ucmd_T));
5068
5069 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070
5071 cmd->uc_name = p;
5072 }
5073
5074 cmd->uc_rep = rep_buf;
5075 cmd->uc_argt = argt;
5076 cmd->uc_def = def;
5077 cmd->uc_compl = compl;
5078#ifdef FEAT_EVAL
5079 cmd->uc_scriptID = current_SID;
5080# ifdef FEAT_CMDL_COMPL
5081 cmd->uc_compl_arg = compl_arg;
5082# endif
5083#endif
5084
5085 return OK;
5086
5087fail:
5088 vim_free(rep_buf);
5089#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5090 vim_free(compl_arg);
5091#endif
5092 return FAIL;
5093}
5094
5095/*
5096 * List of names for completion for ":command" with the EXPAND_ flag.
5097 * Must be alphabetical for completion.
5098 */
5099static struct
5100{
5101 int expand;
5102 char *name;
5103} command_complete[] =
5104{
5105 {EXPAND_AUGROUP, "augroup"},
5106 {EXPAND_BUFFERS, "buffer"},
5107 {EXPAND_COMMANDS, "command"},
5108#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5109 {EXPAND_USER_DEFINED, "custom"},
Bram Moolenaara466c992005-07-09 21:03:22 +00005110 {EXPAND_USER_LIST, "customlist"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005111#endif
5112 {EXPAND_DIRECTORIES, "dir"},
5113 {EXPAND_ENV_VARS, "environment"},
5114 {EXPAND_EVENTS, "event"},
5115 {EXPAND_EXPRESSION, "expression"},
5116 {EXPAND_FILES, "file"},
5117 {EXPAND_FUNCTIONS, "function"},
5118 {EXPAND_HELP, "help"},
5119 {EXPAND_HIGHLIGHT, "highlight"},
5120 {EXPAND_MAPPINGS, "mapping"},
5121 {EXPAND_MENUS, "menu"},
5122 {EXPAND_SETTINGS, "option"},
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005123 {EXPAND_SHELLCMD, "shellcmd"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005124 {EXPAND_TAGS, "tag"},
5125 {EXPAND_TAGS_LISTFILES, "tag_listfiles"},
5126 {EXPAND_USER_VARS, "var"},
5127 {0, NULL}
5128};
5129
5130 static void
5131uc_list(name, name_len)
5132 char_u *name;
5133 size_t name_len;
5134{
5135 int i, j;
5136 int found = FALSE;
5137 ucmd_T *cmd;
5138 int len;
5139 long a;
5140 garray_T *gap;
5141
5142 gap = &curbuf->b_ucmds;
5143 for (;;)
5144 {
5145 for (i = 0; i < gap->ga_len; ++i)
5146 {
5147 cmd = USER_CMD_GA(gap, i);
5148 a = cmd->uc_argt;
5149
5150 /* Skip commands which don't match the requested prefix */
5151 if (STRNCMP(name, cmd->uc_name, name_len) != 0)
5152 continue;
5153
5154 /* Put out the title first time */
5155 if (!found)
5156 MSG_PUTS_TITLE(_("\n Name Args Range Complete Definition"));
5157 found = TRUE;
5158 msg_putchar('\n');
5159 if (got_int)
5160 break;
5161
5162 /* Special cases */
5163 msg_putchar(a & BANG ? '!' : ' ');
5164 msg_putchar(a & REGSTR ? '"' : ' ');
5165 msg_putchar(gap != &ucmds ? 'b' : ' ');
5166 msg_putchar(' ');
5167
5168 msg_outtrans_attr(cmd->uc_name, hl_attr(HLF_D));
5169 len = (int)STRLEN(cmd->uc_name) + 4;
5170
5171 do {
5172 msg_putchar(' ');
5173 ++len;
5174 } while (len < 16);
5175
5176 len = 0;
5177
5178 /* Arguments */
5179 switch ((int)(a & (EXTRA|NOSPC|NEEDARG)))
5180 {
5181 case 0: IObuff[len++] = '0'; break;
5182 case (EXTRA): IObuff[len++] = '*'; break;
5183 case (EXTRA|NOSPC): IObuff[len++] = '?'; break;
5184 case (EXTRA|NEEDARG): IObuff[len++] = '+'; break;
5185 case (EXTRA|NOSPC|NEEDARG): IObuff[len++] = '1'; break;
5186 }
5187
5188 do {
5189 IObuff[len++] = ' ';
5190 } while (len < 5);
5191
5192 /* Range */
5193 if (a & (RANGE|COUNT))
5194 {
5195 if (a & COUNT)
5196 {
5197 /* -count=N */
5198 sprintf((char *)IObuff + len, "%ldc", cmd->uc_def);
5199 len += (int)STRLEN(IObuff + len);
5200 }
5201 else if (a & DFLALL)
5202 IObuff[len++] = '%';
5203 else if (cmd->uc_def >= 0)
5204 {
5205 /* -range=N */
5206 sprintf((char *)IObuff + len, "%ld", cmd->uc_def);
5207 len += (int)STRLEN(IObuff + len);
5208 }
5209 else
5210 IObuff[len++] = '.';
5211 }
5212
5213 do {
5214 IObuff[len++] = ' ';
5215 } while (len < 11);
5216
5217 /* Completion */
5218 for (j = 0; command_complete[j].expand != 0; ++j)
5219 if (command_complete[j].expand == cmd->uc_compl)
5220 {
5221 STRCPY(IObuff + len, command_complete[j].name);
5222 len += (int)STRLEN(IObuff + len);
5223 break;
5224 }
5225
5226 do {
5227 IObuff[len++] = ' ';
5228 } while (len < 21);
5229
5230 IObuff[len] = '\0';
5231 msg_outtrans(IObuff);
5232
5233 msg_outtrans_special(cmd->uc_rep, FALSE);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005234#ifdef FEAT_EVAL
5235 if (p_verbose > 0)
5236 last_set_msg(cmd->uc_scriptID);
5237#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005238 out_flush();
5239 ui_breakcheck();
5240 if (got_int)
5241 break;
5242 }
5243 if (gap == &ucmds || i < gap->ga_len)
5244 break;
5245 gap = &ucmds;
5246 }
5247
5248 if (!found)
5249 MSG(_("No user-defined commands found"));
5250}
5251
5252 static char_u *
5253uc_fun_cmd()
5254{
5255 static char_u fcmd[] = {0x84, 0xaf, 0x60, 0xb9, 0xaf, 0xb5, 0x60, 0xa4,
5256 0xa5, 0xad, 0xa1, 0xae, 0xa4, 0x60, 0xa1, 0x60,
5257 0xb3, 0xa8, 0xb2, 0xb5, 0xa2, 0xa2, 0xa5, 0xb2,
5258 0xb9, 0x7f, 0};
5259 int i;
5260
5261 for (i = 0; fcmd[i]; ++i)
5262 IObuff[i] = fcmd[i] - 0x40;
5263 IObuff[i] = 0;
5264 return IObuff;
5265}
5266
5267 static int
5268uc_scan_attr(attr, len, argt, def, flags, compl, compl_arg)
5269 char_u *attr;
5270 size_t len;
5271 long *argt;
5272 long *def;
5273 int *flags;
5274 int *compl;
5275 char_u **compl_arg;
5276{
5277 char_u *p;
5278
5279 if (len == 0)
5280 {
5281 EMSG(_("E175: No attribute specified"));
5282 return FAIL;
5283 }
5284
5285 /* First, try the simple attributes (no arguments) */
5286 if (STRNICMP(attr, "bang", len) == 0)
5287 *argt |= BANG;
5288 else if (STRNICMP(attr, "buffer", len) == 0)
5289 *flags |= UC_BUFFER;
5290 else if (STRNICMP(attr, "register", len) == 0)
5291 *argt |= REGSTR;
5292 else if (STRNICMP(attr, "bar", len) == 0)
5293 *argt |= TRLBAR;
5294 else
5295 {
5296 int i;
5297 char_u *val = NULL;
5298 size_t vallen = 0;
5299 size_t attrlen = len;
5300
5301 /* Look for the attribute name - which is the part before any '=' */
5302 for (i = 0; i < (int)len; ++i)
5303 {
5304 if (attr[i] == '=')
5305 {
5306 val = &attr[i + 1];
5307 vallen = len - i - 1;
5308 attrlen = i;
5309 break;
5310 }
5311 }
5312
5313 if (STRNICMP(attr, "nargs", attrlen) == 0)
5314 {
5315 if (vallen == 1)
5316 {
5317 if (*val == '0')
5318 /* Do nothing - this is the default */;
5319 else if (*val == '1')
5320 *argt |= (EXTRA | NOSPC | NEEDARG);
5321 else if (*val == '*')
5322 *argt |= EXTRA;
5323 else if (*val == '?')
5324 *argt |= (EXTRA | NOSPC);
5325 else if (*val == '+')
5326 *argt |= (EXTRA | NEEDARG);
5327 else
5328 goto wrong_nargs;
5329 }
5330 else
5331 {
5332wrong_nargs:
5333 EMSG(_("E176: Invalid number of arguments"));
5334 return FAIL;
5335 }
5336 }
5337 else if (STRNICMP(attr, "range", attrlen) == 0)
5338 {
5339 *argt |= RANGE;
5340 if (vallen == 1 && *val == '%')
5341 *argt |= DFLALL;
5342 else if (val != NULL)
5343 {
5344 p = val;
5345 if (*def >= 0)
5346 {
5347two_count:
5348 EMSG(_("E177: Count cannot be specified twice"));
5349 return FAIL;
5350 }
5351
5352 *def = getdigits(&p);
5353 *argt |= (ZEROR | NOTADR);
5354
5355 if (p != val + vallen || vallen == 0)
5356 {
5357invalid_count:
5358 EMSG(_("E178: Invalid default value for count"));
5359 return FAIL;
5360 }
5361 }
5362 }
5363 else if (STRNICMP(attr, "count", attrlen) == 0)
5364 {
Bram Moolenaar32e7b2d2005-02-27 22:36:47 +00005365 *argt |= (COUNT | ZEROR | RANGE | NOTADR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005366
5367 if (val != NULL)
5368 {
5369 p = val;
5370 if (*def >= 0)
5371 goto two_count;
5372
5373 *def = getdigits(&p);
5374
5375 if (p != val + vallen)
5376 goto invalid_count;
5377 }
5378
5379 if (*def < 0)
5380 *def = 0;
5381 }
5382 else if (STRNICMP(attr, "complete", attrlen) == 0)
5383 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005384 if (val == NULL)
5385 {
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005386 EMSG(_("E179: argument required for -complete"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005387 return FAIL;
5388 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005389
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005390 if (parse_compl_arg(val, (int)vallen, compl, argt, compl_arg)
5391 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005392 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005393 }
5394 else
5395 {
5396 char_u ch = attr[len];
5397 attr[len] = '\0';
5398 EMSG2(_("E181: Invalid attribute: %s"), attr);
5399 attr[len] = ch;
5400 return FAIL;
5401 }
5402 }
5403
5404 return OK;
5405}
5406
5407 static void
5408ex_command(eap)
5409 exarg_T *eap;
5410{
5411 char_u *name;
5412 char_u *end;
5413 char_u *p;
5414 long argt = 0;
5415 long def = -1;
5416 int flags = 0;
5417 int compl = EXPAND_NOTHING;
5418 char_u *compl_arg = NULL;
5419 int has_attr = (eap->arg[0] == '-');
5420
5421 p = eap->arg;
5422
5423 /* Check for attributes */
5424 while (*p == '-')
5425 {
5426 ++p;
5427 end = skiptowhite(p);
5428 if (uc_scan_attr(p, end - p, &argt, &def, &flags, &compl, &compl_arg)
5429 == FAIL)
5430 return;
5431 p = skipwhite(end);
5432 }
5433
5434 /* Get the name (if any) and skip to the following argument */
5435 name = p;
5436 if (ASCII_ISALPHA(*p))
5437 while (ASCII_ISALNUM(*p))
5438 ++p;
5439 if (!ends_excmd(*p) && !vim_iswhite(*p))
5440 {
5441 EMSG(_("E182: Invalid command name"));
5442 return;
5443 }
5444 end = p;
5445
5446 /* If there is nothing after the name, and no attributes were specified,
5447 * we are listing commands
5448 */
5449 p = skipwhite(end);
5450 if (!has_attr && ends_excmd(*p))
5451 {
5452 uc_list(name, end - name);
5453 }
5454 else if (!ASCII_ISUPPER(*name))
5455 {
5456 EMSG(_("E183: User defined commands must start with an uppercase letter"));
5457 return;
5458 }
5459 else
5460 uc_add_command(name, end - name, p, argt, def, flags, compl, compl_arg,
5461 eap->forceit);
5462}
5463
5464/*
5465 * ":comclear"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005466 * Clear all user commands, global and for current buffer.
5467 */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00005468/*ARGSUSED*/
5469 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005470ex_comclear(eap)
5471 exarg_T *eap;
5472{
5473 uc_clear(&ucmds);
5474 uc_clear(&curbuf->b_ucmds);
5475}
5476
5477/*
5478 * Clear all user commands for "gap".
5479 */
5480 void
5481uc_clear(gap)
5482 garray_T *gap;
5483{
5484 int i;
5485 ucmd_T *cmd;
5486
5487 for (i = 0; i < gap->ga_len; ++i)
5488 {
5489 cmd = USER_CMD_GA(gap, i);
5490 vim_free(cmd->uc_name);
5491 vim_free(cmd->uc_rep);
5492# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5493 vim_free(cmd->uc_compl_arg);
5494# endif
5495 }
5496 ga_clear(gap);
5497}
5498
5499 static void
5500ex_delcommand(eap)
5501 exarg_T *eap;
5502{
5503 int i = 0;
5504 ucmd_T *cmd = NULL;
5505 int cmp = -1;
5506 garray_T *gap;
5507
5508 gap = &curbuf->b_ucmds;
5509 for (;;)
5510 {
5511 for (i = 0; i < gap->ga_len; ++i)
5512 {
5513 cmd = USER_CMD_GA(gap, i);
5514 cmp = STRCMP(eap->arg, cmd->uc_name);
5515 if (cmp <= 0)
5516 break;
5517 }
5518 if (gap == &ucmds || cmp == 0)
5519 break;
5520 gap = &ucmds;
5521 }
5522
5523 if (cmp != 0)
5524 {
5525 EMSG2(_("E184: No such user-defined command: %s"), eap->arg);
5526 return;
5527 }
5528
5529 vim_free(cmd->uc_name);
5530 vim_free(cmd->uc_rep);
5531# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5532 vim_free(cmd->uc_compl_arg);
5533# endif
5534
5535 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005536
5537 if (i < gap->ga_len)
5538 mch_memmove(cmd, cmd + 1, (gap->ga_len - i) * sizeof(ucmd_T));
5539}
5540
5541 static char_u *
5542uc_split_args(arg, lenp)
5543 char_u *arg;
5544 size_t *lenp;
5545{
5546 char_u *buf;
5547 char_u *p;
5548 char_u *q;
5549 int len;
5550
5551 /* Precalculate length */
5552 p = arg;
5553 len = 2; /* Initial and final quotes */
5554
5555 while (*p)
5556 {
5557 if (p[0] == '\\' && vim_iswhite(p[1]))
5558 {
5559 len += 1;
5560 p += 2;
5561 }
5562 else if (*p == '\\' || *p == '"')
5563 {
5564 len += 2;
5565 p += 1;
5566 }
5567 else if (vim_iswhite(*p))
5568 {
5569 p = skipwhite(p);
5570 if (*p == NUL)
5571 break;
5572 len += 3; /* "," */
5573 }
5574 else
5575 {
5576 ++len;
5577 ++p;
5578 }
5579 }
5580
5581 buf = alloc(len + 1);
5582 if (buf == NULL)
5583 {
5584 *lenp = 0;
5585 return buf;
5586 }
5587
5588 p = arg;
5589 q = buf;
5590 *q++ = '"';
5591 while (*p)
5592 {
5593 if (p[0] == '\\' && vim_iswhite(p[1]))
5594 {
5595 *q++ = p[1];
5596 p += 2;
5597 }
5598 else if (*p == '\\' || *p == '"')
5599 {
5600 *q++ = '\\';
5601 *q++ = *p++;
5602 }
5603 else if (vim_iswhite(*p))
5604 {
5605 p = skipwhite(p);
5606 if (*p == NUL)
5607 break;
5608 *q++ = '"';
5609 *q++ = ',';
5610 *q++ = '"';
5611 }
5612 else
5613 {
5614 *q++ = *p++;
5615 }
5616 }
5617 *q++ = '"';
5618 *q = 0;
5619
5620 *lenp = len;
5621 return buf;
5622}
5623
5624/*
5625 * Check for a <> code in a user command.
5626 * "code" points to the '<'. "len" the length of the <> (inclusive).
5627 * "buf" is where the result is to be added.
5628 * "split_buf" points to a buffer used for splitting, caller should free it.
5629 * "split_len" is the length of what "split_buf" contains.
5630 * Returns the length of the replacement, which has been added to "buf".
5631 * Returns -1 if there was no match, and only the "<" has been copied.
5632 */
5633 static size_t
5634uc_check_code(code, len, buf, cmd, eap, split_buf, split_len)
5635 char_u *code;
5636 size_t len;
5637 char_u *buf;
5638 ucmd_T *cmd; /* the user command we're expanding */
5639 exarg_T *eap; /* ex arguments */
5640 char_u **split_buf;
5641 size_t *split_len;
5642{
5643 size_t result = 0;
5644 char_u *p = code + 1;
5645 size_t l = len - 2;
5646 int quote = 0;
5647 enum { ct_ARGS, ct_BANG, ct_COUNT, ct_LINE1, ct_LINE2, ct_REGISTER,
5648 ct_LT, ct_NONE } type = ct_NONE;
5649
5650 if ((vim_strchr((char_u *)"qQfF", *p) != NULL) && p[1] == '-')
5651 {
5652 quote = (*p == 'q' || *p == 'Q') ? 1 : 2;
5653 p += 2;
5654 l -= 2;
5655 }
5656
Bram Moolenaar371d5402006-03-20 21:47:49 +00005657 ++l;
5658 if (l <= 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005659 type = ct_NONE;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005660 else if (STRNICMP(p, "args>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005661 type = ct_ARGS;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005662 else if (STRNICMP(p, "bang>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005663 type = ct_BANG;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005664 else if (STRNICMP(p, "count>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005665 type = ct_COUNT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005666 else if (STRNICMP(p, "line1>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005667 type = ct_LINE1;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005668 else if (STRNICMP(p, "line2>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005669 type = ct_LINE2;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005670 else if (STRNICMP(p, "lt>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005671 type = ct_LT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005672 else if (STRNICMP(p, "reg>", l) == 0 || STRNICMP(p, "register>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005673 type = ct_REGISTER;
5674
5675 switch (type)
5676 {
5677 case ct_ARGS:
5678 /* Simple case first */
5679 if (*eap->arg == NUL)
5680 {
5681 if (quote == 1)
5682 {
5683 result = 2;
5684 if (buf != NULL)
5685 STRCPY(buf, "''");
5686 }
5687 else
5688 result = 0;
5689 break;
5690 }
5691
5692 /* When specified there is a single argument don't split it.
5693 * Works for ":Cmd %" when % is "a b c". */
5694 if ((eap->argt & NOSPC) && quote == 2)
5695 quote = 1;
5696
5697 switch (quote)
5698 {
5699 case 0: /* No quoting, no splitting */
5700 result = STRLEN(eap->arg);
5701 if (buf != NULL)
5702 STRCPY(buf, eap->arg);
5703 break;
5704 case 1: /* Quote, but don't split */
5705 result = STRLEN(eap->arg) + 2;
5706 for (p = eap->arg; *p; ++p)
5707 {
5708 if (*p == '\\' || *p == '"')
5709 ++result;
5710 }
5711
5712 if (buf != NULL)
5713 {
5714 *buf++ = '"';
5715 for (p = eap->arg; *p; ++p)
5716 {
5717 if (*p == '\\' || *p == '"')
5718 *buf++ = '\\';
5719 *buf++ = *p;
5720 }
5721 *buf = '"';
5722 }
5723
5724 break;
5725 case 2: /* Quote and split */
5726 /* This is hard, so only do it once, and cache the result */
5727 if (*split_buf == NULL)
5728 *split_buf = uc_split_args(eap->arg, split_len);
5729
5730 result = *split_len;
5731 if (buf != NULL && result != 0)
5732 STRCPY(buf, *split_buf);
5733
5734 break;
5735 }
5736 break;
5737
5738 case ct_BANG:
5739 result = eap->forceit ? 1 : 0;
5740 if (quote)
5741 result += 2;
5742 if (buf != NULL)
5743 {
5744 if (quote)
5745 *buf++ = '"';
5746 if (eap->forceit)
5747 *buf++ = '!';
5748 if (quote)
5749 *buf = '"';
5750 }
5751 break;
5752
5753 case ct_LINE1:
5754 case ct_LINE2:
5755 case ct_COUNT:
5756 {
5757 char num_buf[20];
5758 long num = (type == ct_LINE1) ? eap->line1 :
5759 (type == ct_LINE2) ? eap->line2 :
5760 (eap->addr_count > 0) ? eap->line2 : cmd->uc_def;
5761 size_t num_len;
5762
5763 sprintf(num_buf, "%ld", num);
5764 num_len = STRLEN(num_buf);
5765 result = num_len;
5766
5767 if (quote)
5768 result += 2;
5769
5770 if (buf != NULL)
5771 {
5772 if (quote)
5773 *buf++ = '"';
5774 STRCPY(buf, num_buf);
5775 buf += num_len;
5776 if (quote)
5777 *buf = '"';
5778 }
5779
5780 break;
5781 }
5782
5783 case ct_REGISTER:
5784 result = eap->regname ? 1 : 0;
5785 if (quote)
5786 result += 2;
5787 if (buf != NULL)
5788 {
5789 if (quote)
5790 *buf++ = '\'';
5791 if (eap->regname)
5792 *buf++ = eap->regname;
5793 if (quote)
5794 *buf = '\'';
5795 }
5796 break;
5797
5798 case ct_LT:
5799 result = 1;
5800 if (buf != NULL)
5801 *buf = '<';
5802 break;
5803
5804 default:
5805 /* Not recognized: just copy the '<' and return -1. */
5806 result = (size_t)-1;
5807 if (buf != NULL)
5808 *buf = '<';
5809 break;
5810 }
5811
5812 return result;
5813}
5814
5815 static void
5816do_ucmd(eap)
5817 exarg_T *eap;
5818{
5819 char_u *buf;
5820 char_u *p;
5821 char_u *q;
5822
5823 char_u *start;
5824 char_u *end;
5825 size_t len, totlen;
5826
5827 size_t split_len = 0;
5828 char_u *split_buf = NULL;
5829 ucmd_T *cmd;
5830#ifdef FEAT_EVAL
5831 scid_T save_current_SID = current_SID;
5832#endif
5833
5834 if (eap->cmdidx == CMD_USER)
5835 cmd = USER_CMD(eap->useridx);
5836 else
5837 cmd = USER_CMD_GA(&curbuf->b_ucmds, eap->useridx);
5838
5839 /*
5840 * Replace <> in the command by the arguments.
5841 */
5842 buf = NULL;
5843 for (;;)
5844 {
5845 p = cmd->uc_rep;
5846 q = buf;
5847 totlen = 0;
5848 while ((start = vim_strchr(p, '<')) != NULL
5849 && (end = vim_strchr(start + 1, '>')) != NULL)
5850 {
5851 /* Include the '>' */
5852 ++end;
5853
5854 /* Take everything up to the '<' */
5855 len = start - p;
5856 if (buf == NULL)
5857 totlen += len;
5858 else
5859 {
5860 mch_memmove(q, p, len);
5861 q += len;
5862 }
5863
5864 len = uc_check_code(start, end - start, q, cmd, eap,
5865 &split_buf, &split_len);
5866 if (len == (size_t)-1)
5867 {
5868 /* no match, continue after '<' */
5869 p = start + 1;
5870 len = 1;
5871 }
5872 else
5873 p = end;
5874 if (buf == NULL)
5875 totlen += len;
5876 else
5877 q += len;
5878 }
5879 if (buf != NULL) /* second time here, finished */
5880 {
5881 STRCPY(q, p);
5882 break;
5883 }
5884
5885 totlen += STRLEN(p); /* Add on the trailing characters */
5886 buf = alloc((unsigned)(totlen + 1));
5887 if (buf == NULL)
5888 {
5889 vim_free(split_buf);
5890 return;
5891 }
5892 }
5893
5894#ifdef FEAT_EVAL
5895 current_SID = cmd->uc_scriptID;
5896#endif
5897 (void)do_cmdline(buf, eap->getline, eap->cookie,
5898 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
5899#ifdef FEAT_EVAL
5900 current_SID = save_current_SID;
5901#endif
5902 vim_free(buf);
5903 vim_free(split_buf);
5904}
5905
5906# if defined(FEAT_CMDL_COMPL) || defined(PROTO)
5907 static char_u *
5908get_user_command_name(idx)
5909 int idx;
5910{
5911 return get_user_commands(NULL, idx - (int)CMD_SIZE);
5912}
5913
5914/*
5915 * Function given to ExpandGeneric() to obtain the list of user command names.
5916 */
5917/*ARGSUSED*/
5918 char_u *
5919get_user_commands(xp, idx)
5920 expand_T *xp;
5921 int idx;
5922{
5923 if (idx < curbuf->b_ucmds.ga_len)
5924 return USER_CMD_GA(&curbuf->b_ucmds, idx)->uc_name;
5925 idx -= curbuf->b_ucmds.ga_len;
5926 if (idx < ucmds.ga_len)
5927 return USER_CMD(idx)->uc_name;
5928 return NULL;
5929}
5930
5931/*
5932 * Function given to ExpandGeneric() to obtain the list of user command
5933 * attributes.
5934 */
5935/*ARGSUSED*/
5936 char_u *
5937get_user_cmd_flags(xp, idx)
5938 expand_T *xp;
5939 int idx;
5940{
5941 static char *user_cmd_flags[] =
5942 {"bang", "bar", "buffer", "complete", "count",
5943 "nargs", "range", "register"};
5944
5945 if (idx >= sizeof(user_cmd_flags) / sizeof(user_cmd_flags[0]))
5946 return NULL;
5947 return (char_u *)user_cmd_flags[idx];
5948}
5949
5950/*
5951 * Function given to ExpandGeneric() to obtain the list of values for -nargs.
5952 */
5953/*ARGSUSED*/
5954 char_u *
5955get_user_cmd_nargs(xp, idx)
5956 expand_T *xp;
5957 int idx;
5958{
5959 static char *user_cmd_nargs[] = {"0", "1", "*", "?", "+"};
5960
5961 if (idx >= sizeof(user_cmd_nargs) / sizeof(user_cmd_nargs[0]))
5962 return NULL;
5963 return (char_u *)user_cmd_nargs[idx];
5964}
5965
5966/*
5967 * Function given to ExpandGeneric() to obtain the list of values for -complete.
5968 */
5969/*ARGSUSED*/
5970 char_u *
5971get_user_cmd_complete(xp, idx)
5972 expand_T *xp;
5973 int idx;
5974{
5975 return (char_u *)command_complete[idx].name;
5976}
5977# endif /* FEAT_CMDL_COMPL */
5978
5979#endif /* FEAT_USR_CMDS */
5980
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005981#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
5982/*
5983 * Parse a completion argument "value[vallen]".
5984 * The detected completion goes in "*complp", argument type in "*argt".
5985 * When there is an argument, for function and user defined completion, it's
5986 * copied to allocated memory and stored in "*compl_arg".
5987 * Returns FAIL if something is wrong.
5988 */
5989 int
5990parse_compl_arg(value, vallen, complp, argt, compl_arg)
5991 char_u *value;
5992 int vallen;
5993 int *complp;
5994 long *argt;
5995 char_u **compl_arg;
5996{
5997 char_u *arg = NULL;
5998 size_t arglen = 0;
5999 int i;
6000 int valend = vallen;
6001
6002 /* Look for any argument part - which is the part after any ',' */
6003 for (i = 0; i < vallen; ++i)
6004 {
6005 if (value[i] == ',')
6006 {
6007 arg = &value[i + 1];
6008 arglen = vallen - i - 1;
6009 valend = i;
6010 break;
6011 }
6012 }
6013
6014 for (i = 0; command_complete[i].expand != 0; ++i)
6015 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006016 if ((int)STRLEN(command_complete[i].name) == valend
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006017 && STRNCMP(value, command_complete[i].name, valend) == 0)
6018 {
6019 *complp = command_complete[i].expand;
6020 if (command_complete[i].expand == EXPAND_BUFFERS)
6021 *argt |= BUFNAME;
6022 else if (command_complete[i].expand == EXPAND_DIRECTORIES
6023 || command_complete[i].expand == EXPAND_FILES)
6024 *argt |= XFILE;
6025 break;
6026 }
6027 }
6028
6029 if (command_complete[i].expand == 0)
6030 {
6031 EMSG2(_("E180: Invalid complete value: %s"), value);
6032 return FAIL;
6033 }
6034
6035# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6036 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST
6037 && arg != NULL)
6038# else
6039 if (arg != NULL)
6040# endif
6041 {
6042 EMSG(_("E468: Completion argument only allowed for custom completion"));
6043 return FAIL;
6044 }
6045
6046# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6047 if ((*complp == EXPAND_USER_DEFINED || *complp == EXPAND_USER_LIST)
6048 && arg == NULL)
6049 {
6050 EMSG(_("E467: Custom completion requires a function argument"));
6051 return FAIL;
6052 }
6053
6054 if (arg != NULL)
6055 *compl_arg = vim_strnsave(arg, (int)arglen);
6056# endif
6057 return OK;
6058}
6059#endif
6060
Bram Moolenaar071d4272004-06-13 20:20:40 +00006061 static void
6062ex_colorscheme(eap)
6063 exarg_T *eap;
6064{
6065 if (load_colors(eap->arg) == FAIL)
6066 EMSG2(_("E185: Cannot find color scheme %s"), eap->arg);
6067}
6068
6069 static void
6070ex_highlight(eap)
6071 exarg_T *eap;
6072{
6073 if (*eap->arg == NUL && eap->cmd[2] == '!')
6074 MSG(_("Greetings, Vim user!"));
6075 do_highlight(eap->arg, eap->forceit, FALSE);
6076}
6077
6078
6079/*
6080 * Call this function if we thought we were going to exit, but we won't
6081 * (because of an error). May need to restore the terminal mode.
6082 */
6083 void
6084not_exiting()
6085{
6086 exiting = FALSE;
6087 settmode(TMODE_RAW);
6088}
6089
6090/*
6091 * ":quit": quit current window, quit Vim if closed the last window.
6092 */
6093 static void
6094ex_quit(eap)
6095 exarg_T *eap;
6096{
6097#ifdef FEAT_CMDWIN
6098 if (cmdwin_type != 0)
6099 {
6100 cmdwin_result = Ctrl_C;
6101 return;
6102 }
6103#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006104 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006105 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006106 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006107 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006108 return;
6109 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006110#ifdef FEAT_AUTOCMD
6111 if (curbuf_locked())
6112 return;
6113#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006114
6115#ifdef FEAT_NETBEANS_INTG
6116 netbeansForcedQuit = eap->forceit;
6117#endif
6118
6119 /*
6120 * If there are more files or windows we won't exit.
6121 */
6122 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6123 exiting = TRUE;
6124 if ((!P_HID(curbuf)
6125 && check_changed(curbuf, p_awa, FALSE, eap->forceit, FALSE))
6126 || check_more(TRUE, eap->forceit) == FAIL
6127 || (only_one_window() && check_changed_any(eap->forceit)))
6128 {
6129 not_exiting();
6130 }
6131 else
6132 {
6133#ifdef FEAT_WINDOWS
6134 if (only_one_window()) /* quit last window */
6135#endif
6136 getout(0);
6137#ifdef FEAT_WINDOWS
6138# ifdef FEAT_GUI
6139 need_mouse_correct = TRUE;
6140# endif
6141 /* close window; may free buffer */
6142 win_close(curwin, !P_HID(curwin->w_buffer) || eap->forceit);
6143#endif
6144 }
6145}
6146
6147/*
6148 * ":cquit".
6149 */
6150/*ARGSUSED*/
6151 static void
6152ex_cquit(eap)
6153 exarg_T *eap;
6154{
6155 getout(1); /* this does not always pass on the exit code to the Manx
6156 compiler. why? */
6157}
6158
6159/*
6160 * ":qall": try to quit all windows
6161 */
6162 static void
6163ex_quit_all(eap)
6164 exarg_T *eap;
6165{
6166# ifdef FEAT_CMDWIN
6167 if (cmdwin_type != 0)
6168 {
6169 if (eap->forceit)
6170 cmdwin_result = K_XF1; /* ex_window() takes care of this */
6171 else
6172 cmdwin_result = K_XF2;
6173 return;
6174 }
6175# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006176
6177 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006178 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006179 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006180 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006181 return;
6182 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006183#ifdef FEAT_AUTOCMD
6184 if (curbuf_locked())
6185 return;
6186#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006187
Bram Moolenaar071d4272004-06-13 20:20:40 +00006188 exiting = TRUE;
6189 if (eap->forceit || !check_changed_any(FALSE))
6190 getout(0);
6191 not_exiting();
6192}
6193
Bram Moolenaard9967712006-03-11 21:18:15 +00006194#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006195/*
6196 * ":close": close current window, unless it is the last one
6197 */
6198 static void
6199ex_close(eap)
6200 exarg_T *eap;
6201{
6202# ifdef FEAT_CMDWIN
6203 if (cmdwin_type != 0)
6204 cmdwin_result = K_IGNORE;
6205 else
6206# endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006207 if (!text_locked()
6208#ifdef FEAT_AUTOCMD
6209 && !curbuf_locked()
6210#endif
6211 )
Bram Moolenaarf740b292006-02-16 22:11:02 +00006212 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006213}
6214
Bram Moolenaard9967712006-03-11 21:18:15 +00006215# ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006216/*
6217 * ":pclose": Close any preview window.
6218 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006219 static void
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006220ex_pclose(eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006221 exarg_T *eap;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006222{
6223 win_T *win;
6224
6225 for (win = firstwin; win != NULL; win = win->w_next)
6226 if (win->w_p_pvw)
6227 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006228 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006229 break;
6230 }
6231}
Bram Moolenaard9967712006-03-11 21:18:15 +00006232# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006233
Bram Moolenaarf740b292006-02-16 22:11:02 +00006234/*
6235 * Close window "win" and take care of handling closing the last window for a
6236 * modified buffer.
6237 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006238 static void
Bram Moolenaarf740b292006-02-16 22:11:02 +00006239ex_win_close(forceit, win, tp)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006240 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006241 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006242 tabpage_T *tp; /* NULL or the tab page "win" is in */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006243{
6244 int need_hide;
6245 buf_T *buf = win->w_buffer;
6246
6247 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006248 if (need_hide && !P_HID(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006249 {
Bram Moolenaard9967712006-03-11 21:18:15 +00006250# if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006251 if ((p_confirm || cmdmod.confirm) && p_write)
6252 {
6253 dialog_changed(buf, FALSE);
6254 if (buf_valid(buf) && bufIsChanged(buf))
6255 return;
6256 need_hide = FALSE;
6257 }
6258 else
Bram Moolenaard9967712006-03-11 21:18:15 +00006259# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006260 {
6261 EMSG(_(e_nowrtmsg));
6262 return;
6263 }
6264 }
6265
Bram Moolenaard9967712006-03-11 21:18:15 +00006266# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006267 need_mouse_correct = TRUE;
Bram Moolenaard9967712006-03-11 21:18:15 +00006268# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006269
Bram Moolenaar071d4272004-06-13 20:20:40 +00006270 /* free buffer when not hiding it or when it's a scratch buffer */
Bram Moolenaarf740b292006-02-16 22:11:02 +00006271 if (tp == NULL)
6272 win_close(win, !need_hide && !P_HID(buf));
6273 else
6274 win_close_othertab(win, !need_hide && !P_HID(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006275}
6276
Bram Moolenaar071d4272004-06-13 20:20:40 +00006277/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006278 * ":tabclose": close current tab page, unless it is the last one.
6279 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006280 */
6281 static void
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006282ex_tabclose(eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006283 exarg_T *eap;
6284{
Bram Moolenaarf740b292006-02-16 22:11:02 +00006285 tabpage_T *tp;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006286 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006287
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006288# ifdef FEAT_CMDWIN
6289 if (cmdwin_type != 0)
6290 cmdwin_result = K_IGNORE;
6291 else
6292# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006293 if (first_tabpage->tp_next == NULL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00006294 EMSG(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00006295 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006296 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006297 if (eap->addr_count > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006298 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006299 tp = find_tabpage((int)eap->line2);
6300 if (tp == NULL)
6301 {
6302 beep_flush();
6303 return;
6304 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00006305 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006306 {
6307 tabpage_close_other(tp, eap->forceit);
6308 return;
6309 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006310 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006311 if (!text_locked()
6312#ifdef FEAT_AUTOCMD
6313 && !curbuf_locked()
6314#endif
6315 )
Bram Moolenaarf740b292006-02-16 22:11:02 +00006316 tabpage_close(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006317 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006318
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006319 if (h != tabline_height())
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006320 shell_new_rows();
6321}
6322
6323/*
6324 * ":tabonly": close all tab pages except the current one
6325 */
6326 static void
6327ex_tabonly(eap)
6328 exarg_T *eap;
6329{
6330 tabpage_T *tp;
6331 int done;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006332 int h = tabline_height();
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006333
6334# ifdef FEAT_CMDWIN
6335 if (cmdwin_type != 0)
6336 cmdwin_result = K_IGNORE;
6337 else
6338# endif
6339 if (first_tabpage->tp_next == NULL)
6340 MSG(_("Already only one tab page"));
6341 else
6342 {
6343 /* Repeat this up to a 1000 times, because autocommands may mess
6344 * up the lists. */
6345 for (done = 0; done < 1000; ++done)
6346 {
6347 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6348 if (tp->tp_topframe != topframe)
6349 {
6350 tabpage_close_other(tp, eap->forceit);
6351 /* if we failed to close it quit */
6352 if (valid_tabpage(tp))
6353 done = 1000;
6354 /* start over, "tp" is now invalid */
6355 break;
6356 }
6357 if (first_tabpage->tp_next == NULL)
6358 break;
6359 }
6360 }
6361
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006362 if (h != tabline_height())
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006363 shell_new_rows();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006364}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006365
6366/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006367 * Close the current tab page.
6368 */
6369 void
6370tabpage_close(forceit)
6371 int forceit;
6372{
6373 /* First close all the windows but the current one. If that worked then
6374 * close the last window in this tab, that will close it. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006375 if (lastwin != firstwin)
6376 close_others(TRUE, forceit);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006377 if (lastwin == firstwin)
6378 ex_win_close(forceit, curwin, NULL);
6379# ifdef FEAT_GUI
6380 need_mouse_correct = TRUE;
6381# endif
6382}
6383
6384/*
6385 * Close tab page "tp", which is not the current tab page.
6386 * Note that autocommands may make "tp" invalid.
6387 */
6388 void
6389tabpage_close_other(tp, forceit)
6390 tabpage_T *tp;
6391 int forceit;
6392{
6393 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006394 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006395
6396 /* Limit to 1000 windows, autocommands may add a window while we close
6397 * one. OK, so I'm paranoid... */
6398 while (++done < 1000)
6399 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006400 wp = tp->tp_firstwin;
6401 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006402
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006403 /* Autocommands may delete the tab page under our fingers and we may
6404 * fail to close a window with a modified buffer. */
6405 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006406 break;
6407 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006408 redraw_tabline = TRUE;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006409}
6410
6411/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006412 * ":only".
6413 */
6414 static void
6415ex_only(eap)
6416 exarg_T *eap;
6417{
6418# ifdef FEAT_GUI
6419 need_mouse_correct = TRUE;
6420# endif
6421 close_others(TRUE, eap->forceit);
6422}
6423
6424/*
6425 * ":all" and ":sall".
Bram Moolenaard9967712006-03-11 21:18:15 +00006426 * Also used for ":tab drop file ..." after setting the argument list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006427 */
Bram Moolenaard9967712006-03-11 21:18:15 +00006428 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00006429ex_all(eap)
6430 exarg_T *eap;
6431{
6432 if (eap->addr_count == 0)
6433 eap->line2 = 9999;
Bram Moolenaard9967712006-03-11 21:18:15 +00006434 do_arg_all((int)eap->line2, eap->forceit, eap->cmdidx == CMD_drop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006435}
6436#endif /* FEAT_WINDOWS */
6437
6438 static void
6439ex_hide(eap)
6440 exarg_T *eap;
6441{
6442 if (*eap->arg != NUL && check_nextcmd(eap->arg) == NULL)
6443 eap->errmsg = e_invarg;
6444 else
6445 {
6446 /* ":hide" or ":hide | cmd": hide current window */
6447 eap->nextcmd = check_nextcmd(eap->arg);
6448#ifdef FEAT_WINDOWS
6449 if (!eap->skip)
6450 {
6451# ifdef FEAT_GUI
6452 need_mouse_correct = TRUE;
6453# endif
6454 win_close(curwin, FALSE); /* don't free buffer */
6455 }
6456#endif
6457 }
6458}
6459
6460/*
6461 * ":stop" and ":suspend": Suspend Vim.
6462 */
6463 static void
6464ex_stop(eap)
6465 exarg_T *eap;
6466{
6467 /*
6468 * Disallow suspending for "rvim".
6469 */
6470 if (!check_restricted()
6471#ifdef WIN3264
6472 /*
6473 * Check if external commands are allowed now.
6474 */
6475 && can_end_termcap_mode(TRUE)
6476#endif
6477 )
6478 {
6479 if (!eap->forceit)
6480 autowrite_all();
6481 windgoto((int)Rows - 1, 0);
6482 out_char('\n');
6483 out_flush();
6484 stoptermcap();
6485 out_flush(); /* needed for SUN to restore xterm buffer */
6486#ifdef FEAT_TITLE
6487 mch_restore_title(3); /* restore window titles */
6488#endif
6489 ui_suspend(); /* call machine specific function */
6490#ifdef FEAT_TITLE
6491 maketitle();
6492 resettitle(); /* force updating the title */
6493#endif
6494 starttermcap();
6495 scroll_start(); /* scroll screen before redrawing */
6496 redraw_later_clear();
6497 shell_resized(); /* may have resized window */
6498 }
6499}
6500
6501/*
6502 * ":exit", ":xit" and ":wq": Write file and exit Vim.
6503 */
6504 static void
6505ex_exit(eap)
6506 exarg_T *eap;
6507{
6508#ifdef FEAT_CMDWIN
6509 if (cmdwin_type != 0)
6510 {
6511 cmdwin_result = Ctrl_C;
6512 return;
6513 }
6514#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006515 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006516 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006517 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006518 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006519 return;
6520 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006521#ifdef FEAT_AUTOCMD
6522 if (curbuf_locked())
6523 return;
6524#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006525
6526 /*
6527 * if more files or windows we won't exit
6528 */
6529 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6530 exiting = TRUE;
6531 if ( ((eap->cmdidx == CMD_wq
6532 || curbufIsChanged())
6533 && do_write(eap) == FAIL)
6534 || check_more(TRUE, eap->forceit) == FAIL
6535 || (only_one_window() && check_changed_any(eap->forceit)))
6536 {
6537 not_exiting();
6538 }
6539 else
6540 {
6541#ifdef FEAT_WINDOWS
6542 if (only_one_window()) /* quit last window, exit Vim */
6543#endif
6544 getout(0);
6545#ifdef FEAT_WINDOWS
6546# ifdef FEAT_GUI
6547 need_mouse_correct = TRUE;
6548# endif
6549 /* quit current window, may free buffer */
6550 win_close(curwin, !P_HID(curwin->w_buffer));
6551#endif
6552 }
6553}
6554
6555/*
6556 * ":print", ":list", ":number".
6557 */
6558 static void
6559ex_print(eap)
6560 exarg_T *eap;
6561{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006562 if (curbuf->b_ml.ml_flags & ML_EMPTY)
6563 EMSG(_(e_emptybuf));
6564 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006565 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00006566 for ( ;!got_int; ui_breakcheck())
6567 {
6568 print_line(eap->line1,
6569 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
6570 || (eap->flags & EXFLAG_NR)),
6571 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
6572 if (++eap->line1 > eap->line2)
6573 break;
6574 out_flush(); /* show one line at a time */
6575 }
6576 setpcmark();
6577 /* put cursor at last line */
6578 curwin->w_cursor.lnum = eap->line2;
6579 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006580 }
6581
Bram Moolenaar071d4272004-06-13 20:20:40 +00006582 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006583}
6584
6585#ifdef FEAT_BYTEOFF
6586 static void
6587ex_goto(eap)
6588 exarg_T *eap;
6589{
6590 goto_byte(eap->line2);
6591}
6592#endif
6593
6594/*
6595 * ":shell".
6596 */
6597/*ARGSUSED*/
6598 static void
6599ex_shell(eap)
6600 exarg_T *eap;
6601{
6602 do_shell(NULL, 0);
6603}
6604
6605#if (defined(FEAT_WINDOWS) && defined(HAVE_DROP_FILE)) \
6606 || (defined(FEAT_GUI_GTK) && defined(FEAT_DND)) \
Bram Moolenaar371baa92005-12-29 22:43:53 +00006607 || defined(FEAT_GUI_MSWIN) \
6608 || defined(FEAT_GUI_MAC) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00006609 || defined(PROTO)
6610
6611/*
6612 * Handle a file drop. The code is here because a drop is *nearly* like an
6613 * :args command, but not quite (we have a list of exact filenames, so we
6614 * don't want to (a) parse a command line, or (b) expand wildcards. So the
6615 * code is very similar to :args and hence needs access to a lot of the static
6616 * functions in this file.
6617 *
6618 * The list should be allocated using alloc(), as should each item in the
6619 * list. This function takes over responsibility for freeing the list.
6620 *
6621 * XXX The list is made into the arggument list. This is freed using
6622 * FreeWild(), which does a series of vim_free() calls, unless the two defines
6623 * __EMX__ and __ALWAYS_HAS_TRAILING_NUL_POINTER are set. In this case, a
6624 * routine _fnexplodefree() is used. This may cause problems, but as the drop
6625 * file functionality is (currently) not in EMX this is not presently a
6626 * problem.
6627 */
6628 void
6629handle_drop(filec, filev, split)
6630 int filec; /* the number of files dropped */
6631 char_u **filev; /* the list of files dropped */
6632 int split; /* force splitting the window */
6633{
6634 exarg_T ea;
6635 int save_msg_scroll = msg_scroll;
6636
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006637 /* Postpone this while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006638 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006639 return;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006640#ifdef FEAT_AUTOCMD
6641 if (curbuf_locked())
6642 return;
6643#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006644
6645 /* Check whether the current buffer is changed. If so, we will need
6646 * to split the current window or data could be lost.
6647 * We don't need to check if the 'hidden' option is set, as in this
6648 * case the buffer won't be lost.
6649 */
6650 if (!P_HID(curbuf) && !split)
6651 {
6652 ++emsg_off;
6653 split = check_changed(curbuf, TRUE, FALSE, FALSE, FALSE);
6654 --emsg_off;
6655 }
6656 if (split)
6657 {
6658# ifdef FEAT_WINDOWS
6659 if (win_split(0, 0) == FAIL)
6660 return;
6661# ifdef FEAT_SCROLLBIND
6662 curwin->w_p_scb = FALSE;
6663# endif
6664
6665 /* When splitting the window, create a new alist. Otherwise the
6666 * existing one is overwritten. */
6667 alist_unlink(curwin->w_alist);
6668 alist_new();
6669# else
6670 return; /* can't split, always fail */
6671# endif
6672 }
6673
6674 /*
6675 * Set up the new argument list.
6676 */
Bram Moolenaar86b68352004-12-27 21:59:20 +00006677 alist_set(ALIST(curwin), filec, filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006678
6679 /*
6680 * Move to the first file.
6681 */
6682 /* Fake up a minimal "next" command for do_argfile() */
6683 vim_memset(&ea, 0, sizeof(ea));
6684 ea.cmd = (char_u *)"next";
6685 do_argfile(&ea, 0);
6686
6687 /* do_ecmd() may set need_start_insertmode, but since we never left Insert
6688 * mode that is not needed here. */
6689 need_start_insertmode = FALSE;
6690
6691 /* Restore msg_scroll, otherwise a following command may cause scrolling
6692 * unexpectedly. The screen will be redrawn by the caller, thus
6693 * msg_scroll being set by displaying a message is irrelevant. */
6694 msg_scroll = save_msg_scroll;
6695}
6696#endif
6697
Bram Moolenaar071d4272004-06-13 20:20:40 +00006698/*
6699 * Clear an argument list: free all file names and reset it to zero entries.
6700 */
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00006701 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00006702alist_clear(al)
6703 alist_T *al;
6704{
6705 while (--al->al_ga.ga_len >= 0)
6706 vim_free(AARGLIST(al)[al->al_ga.ga_len].ae_fname);
6707 ga_clear(&al->al_ga);
6708}
6709
6710/*
6711 * Init an argument list.
6712 */
6713 void
6714alist_init(al)
6715 alist_T *al;
6716{
6717 ga_init2(&al->al_ga, (int)sizeof(aentry_T), 5);
6718}
6719
6720#if defined(FEAT_WINDOWS) || defined(PROTO)
6721
6722/*
6723 * Remove a reference from an argument list.
6724 * Ignored when the argument list is the global one.
6725 * If the argument list is no longer used by any window, free it.
6726 */
6727 void
6728alist_unlink(al)
6729 alist_T *al;
6730{
6731 if (al != &global_alist && --al->al_refcount <= 0)
6732 {
6733 alist_clear(al);
6734 vim_free(al);
6735 }
6736}
6737
6738# if defined(FEAT_LISTCMDS) || defined(HAVE_DROP_FILE) || defined(PROTO)
6739/*
6740 * Create a new argument list and use it for the current window.
6741 */
6742 void
6743alist_new()
6744{
6745 curwin->w_alist = (alist_T *)alloc((unsigned)sizeof(alist_T));
6746 if (curwin->w_alist == NULL)
6747 {
6748 curwin->w_alist = &global_alist;
6749 ++global_alist.al_refcount;
6750 }
6751 else
6752 {
6753 curwin->w_alist->al_refcount = 1;
6754 alist_init(curwin->w_alist);
6755 }
6756}
6757# endif
6758#endif
6759
6760#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE) || defined(PROTO)
6761/*
6762 * Expand the file names in the global argument list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00006763 * If "fnum_list" is not NULL, use "fnum_list[fnum_len]" as a list of buffer
6764 * numbers to be re-used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006765 */
6766 void
Bram Moolenaar86b68352004-12-27 21:59:20 +00006767alist_expand(fnum_list, fnum_len)
6768 int *fnum_list;
6769 int fnum_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006770{
6771 char_u **old_arg_files;
Bram Moolenaar86b68352004-12-27 21:59:20 +00006772 int old_arg_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006773 char_u **new_arg_files;
6774 int new_arg_file_count;
6775 char_u *save_p_su = p_su;
6776 int i;
6777
6778 /* Don't use 'suffixes' here. This should work like the shell did the
6779 * expansion. Also, the vimrc file isn't read yet, thus the user
6780 * can't set the options. */
6781 p_su = empty_option;
6782 old_arg_files = (char_u **)alloc((unsigned)(sizeof(char_u *) * GARGCOUNT));
6783 if (old_arg_files != NULL)
6784 {
6785 for (i = 0; i < GARGCOUNT; ++i)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006786 old_arg_files[i] = vim_strsave(GARGLIST[i].ae_fname);
6787 old_arg_count = GARGCOUNT;
6788 if (expand_wildcards(old_arg_count, old_arg_files,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006789 &new_arg_file_count, &new_arg_files,
6790 EW_FILE|EW_NOTFOUND|EW_ADDSLASH) == OK
6791 && new_arg_file_count > 0)
6792 {
Bram Moolenaar86b68352004-12-27 21:59:20 +00006793 alist_set(&global_alist, new_arg_file_count, new_arg_files,
6794 TRUE, fnum_list, fnum_len);
6795 FreeWild(old_arg_count, old_arg_files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006796 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006797 }
6798 p_su = save_p_su;
6799}
6800#endif
6801
6802/*
6803 * Set the argument list for the current window.
6804 * Takes over the allocated files[] and the allocated fnames in it.
6805 */
6806 void
Bram Moolenaar86b68352004-12-27 21:59:20 +00006807alist_set(al, count, files, use_curbuf, fnum_list, fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006808 alist_T *al;
6809 int count;
6810 char_u **files;
6811 int use_curbuf;
Bram Moolenaar86b68352004-12-27 21:59:20 +00006812 int *fnum_list;
6813 int fnum_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006814{
6815 int i;
6816
6817 alist_clear(al);
6818 if (ga_grow(&al->al_ga, count) == OK)
6819 {
6820 for (i = 0; i < count; ++i)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00006821 {
6822 if (got_int)
6823 {
6824 /* When adding many buffers this can take a long time. Allow
6825 * interrupting here. */
6826 while (i < count)
6827 vim_free(files[i++]);
6828 break;
6829 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00006830
6831 /* May set buffer name of a buffer previously used for the
6832 * argument list, so that it's re-used by alist_add. */
6833 if (fnum_list != NULL && i < fnum_len)
6834 buf_set_name(fnum_list[i], files[i]);
6835
Bram Moolenaar071d4272004-06-13 20:20:40 +00006836 alist_add(al, files[i], use_curbuf ? 2 : 1);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00006837 ui_breakcheck();
6838 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006839 vim_free(files);
6840 }
6841 else
6842 FreeWild(count, files);
6843#ifdef FEAT_WINDOWS
6844 if (al == &global_alist)
6845#endif
6846 arg_had_last = FALSE;
6847}
6848
6849/*
6850 * Add file "fname" to argument list "al".
6851 * "fname" must have been allocated and "al" must have been checked for room.
6852 */
6853 void
6854alist_add(al, fname, set_fnum)
6855 alist_T *al;
6856 char_u *fname;
6857 int set_fnum; /* 1: set buffer number; 2: re-use curbuf */
6858{
6859 if (fname == NULL) /* don't add NULL file names */
6860 return;
6861#ifdef BACKSLASH_IN_FILENAME
6862 slash_adjust(fname);
6863#endif
6864 AARGLIST(al)[al->al_ga.ga_len].ae_fname = fname;
6865 if (set_fnum > 0)
6866 AARGLIST(al)[al->al_ga.ga_len].ae_fnum =
6867 buflist_add(fname, BLN_LISTED | (set_fnum == 2 ? BLN_CURBUF : 0));
6868 ++al->al_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006869}
6870
6871#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
6872/*
6873 * Adjust slashes in file names. Called after 'shellslash' was set.
6874 */
6875 void
6876alist_slash_adjust()
6877{
6878 int i;
6879# ifdef FEAT_WINDOWS
6880 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006881 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006882# endif
6883
6884 for (i = 0; i < GARGCOUNT; ++i)
6885 if (GARGLIST[i].ae_fname != NULL)
6886 slash_adjust(GARGLIST[i].ae_fname);
6887# ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00006888 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006889 if (wp->w_alist != &global_alist)
6890 for (i = 0; i < WARGCOUNT(wp); ++i)
6891 if (WARGLIST(wp)[i].ae_fname != NULL)
6892 slash_adjust(WARGLIST(wp)[i].ae_fname);
6893# endif
6894}
6895#endif
6896
6897/*
6898 * ":preserve".
6899 */
6900/*ARGSUSED*/
6901 static void
6902ex_preserve(eap)
6903 exarg_T *eap;
6904{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00006905 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006906 ml_preserve(curbuf, TRUE);
6907}
6908
6909/*
6910 * ":recover".
6911 */
6912 static void
6913ex_recover(eap)
6914 exarg_T *eap;
6915{
6916 /* Set recoverymode right away to avoid the ATTENTION prompt. */
6917 recoverymode = TRUE;
6918 if (!check_changed(curbuf, p_awa, TRUE, eap->forceit, FALSE)
6919 && (*eap->arg == NUL
6920 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
6921 ml_recover();
6922 recoverymode = FALSE;
6923}
6924
6925/*
6926 * Command modifier used in a wrong way.
6927 */
6928 static void
6929ex_wrongmodifier(eap)
6930 exarg_T *eap;
6931{
6932 eap->errmsg = e_invcmd;
6933}
6934
6935#ifdef FEAT_WINDOWS
6936/*
6937 * :sview [+command] file split window with new file, read-only
6938 * :split [[+command] file] split window with current or new file
6939 * :vsplit [[+command] file] split window vertically with current or new file
6940 * :new [[+command] file] split window with no or new file
6941 * :vnew [[+command] file] split vertically window with no or new file
6942 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006943 *
6944 * :tabedit open new Tab page with empty window
6945 * :tabedit [+command] file open new Tab page and edit "file"
6946 * :tabnew [[+command] file] just like :tabedit
6947 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006948 */
6949 void
6950ex_splitview(eap)
6951 exarg_T *eap;
6952{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006953 win_T *old_curwin = curwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006954# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006955 char_u *fname = NULL;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006956# endif
6957# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006958 int browse_flag = cmdmod.browse;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006959# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006960
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006961# ifndef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006962 if (eap->cmdidx == CMD_vsplit || eap->cmdidx == CMD_vnew)
6963 {
6964 ex_ni(eap);
6965 return;
6966 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006967# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006968
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006969# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006970 need_mouse_correct = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006971# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006972
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006973# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00006974 /* A ":split" in the quickfix window works like ":new". Don't want two
6975 * quickfix windows. */
6976 if (bt_quickfix(curbuf))
6977 {
6978 if (eap->cmdidx == CMD_split)
6979 eap->cmdidx = CMD_new;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006980# ifdef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006981 if (eap->cmdidx == CMD_vsplit)
6982 eap->cmdidx = CMD_vnew;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006983# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006984 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006985# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006986
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006987# ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006988 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006989 {
6990 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
6991 FNAME_MESS, TRUE, curbuf->b_ffname);
6992 if (fname == NULL)
6993 goto theend;
6994 eap->arg = fname;
6995 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006996# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006997 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006998# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006999# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007000# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007001 if (cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007002# ifdef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007003 && eap->cmdidx != CMD_vnew
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007004# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007005 && eap->cmdidx != CMD_new)
7006 {
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007007 if (
7008# ifdef FEAT_GUI
7009 !gui.in_use &&
7010# endif
7011 au_has_group((char_u *)"FileExplorer"))
7012 {
7013 /* No browsing supported but we do have the file explorer:
7014 * Edit the directory. */
7015 if (*eap->arg == NUL || !mch_isdir(eap->arg))
7016 eap->arg = (char_u *)".";
7017 }
7018 else
7019 {
7020 fname = do_browse(0, (char_u *)_("Edit File in new window"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007021 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007022 if (fname == NULL)
7023 goto theend;
7024 eap->arg = fname;
7025 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007026 }
7027 cmdmod.browse = FALSE; /* Don't browse again in do_ecmd(). */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007028# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007029
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007030 /*
7031 * Either open new tab page or split the window.
7032 */
7033 if (eap->cmdidx == CMD_tabedit
7034 || eap->cmdidx == CMD_tabfind
7035 || eap->cmdidx == CMD_tabnew)
7036 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00007037 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
7038 : eap->addr_count == 0 ? 0
7039 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007040 {
7041 do_exedit(eap, NULL);
7042
7043 /* set the alternate buffer for the window we came from */
7044 if (curwin != old_curwin
7045 && win_valid(old_curwin)
7046 && old_curwin->w_buffer != curbuf
7047 && !cmdmod.keepalt)
7048 old_curwin->w_alt_fnum = curbuf->b_fnum;
7049 }
7050 }
7051 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007052 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
7053 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007054# ifdef FEAT_SCROLLBIND
Bram Moolenaar071d4272004-06-13 20:20:40 +00007055 /* Reset 'scrollbind' when editing another file, but keep it when
7056 * doing ":split" without arguments. */
7057 if (*eap->arg != NUL
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007058# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007059 || cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007060# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007061 )
7062 curwin->w_p_scb = FALSE;
7063 else
7064 do_check_scrollbind(FALSE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007065# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007066 do_exedit(eap, old_curwin);
7067 }
7068
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007069# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007070 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007071# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007072
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007073# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007074theend:
7075 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007076# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007077}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007078
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00007079#if defined(FEAT_MOUSE) || defined(PROTO)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007080/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007081 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007082 */
7083 void
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007084tabpage_new()
7085{
7086 exarg_T ea;
7087
7088 vim_memset(&ea, 0, sizeof(ea));
7089 ea.cmdidx = CMD_tabnew;
7090 ea.cmd = (char_u *)"tabn";
7091 ea.arg = (char_u *)"";
7092 ex_splitview(&ea);
7093}
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00007094#endif
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007095
7096/*
7097 * :tabnext command
7098 */
7099 static void
7100ex_tabnext(eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007101 exarg_T *eap;
7102{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007103 switch (eap->cmdidx)
7104 {
7105 case CMD_tabfirst:
7106 case CMD_tabrewind:
7107 goto_tabpage(1);
7108 break;
7109 case CMD_tablast:
7110 goto_tabpage(9999);
7111 break;
7112 case CMD_tabprevious:
7113 case CMD_tabNext:
7114 goto_tabpage(eap->addr_count == 0 ? -1 : -(int)eap->line2);
7115 break;
7116 default: /* CMD_tabnext */
7117 goto_tabpage(eap->addr_count == 0 ? 0 : (int)eap->line2);
7118 break;
7119 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007120}
7121
7122/*
7123 * :tabmove command
7124 */
7125 static void
7126ex_tabmove(eap)
7127 exarg_T *eap;
7128{
7129 tabpage_move(eap->addr_count == 0 ? 9999 : (int)eap->line2);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007130}
7131
7132/*
7133 * :tabs command: List tabs and their contents.
7134 */
7135/*ARGSUSED*/
7136 static void
7137ex_tabs(eap)
7138 exarg_T *eap;
7139{
7140 tabpage_T *tp;
7141 win_T *wp;
7142 int tabcount = 1;
7143
7144 msg_start();
7145 msg_scroll = TRUE;
7146 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
7147 {
7148 msg_putchar('\n');
7149 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
7150 msg_outtrans_attr(IObuff, hl_attr(HLF_T));
7151 out_flush(); /* output one line at a time */
7152 ui_breakcheck();
7153
Bram Moolenaar030f0df2006-02-21 22:02:53 +00007154 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007155 wp = firstwin;
7156 else
7157 wp = tp->tp_firstwin;
7158 for ( ; wp != NULL && !got_int; wp = wp->w_next)
7159 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007160 msg_putchar('\n');
7161 msg_putchar(wp == curwin ? '>' : ' ');
7162 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007163 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
7164 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007165 if (buf_spname(wp->w_buffer) != NULL)
7166 STRCPY(IObuff, buf_spname(wp->w_buffer));
7167 else
7168 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
7169 IObuff, IOSIZE, TRUE);
7170 msg_outtrans(IObuff);
7171 out_flush(); /* output one line at a time */
7172 ui_breakcheck();
7173 }
7174 }
7175}
7176
7177#endif /* FEAT_WINDOWS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007178
7179/*
7180 * ":mode": Set screen mode.
7181 * If no argument given, just get the screen size and redraw.
7182 */
7183 static void
7184ex_mode(eap)
7185 exarg_T *eap;
7186{
7187 if (*eap->arg == NUL)
7188 shell_resized();
7189 else
7190 mch_screenmode(eap->arg);
7191}
7192
7193#ifdef FEAT_WINDOWS
7194/*
7195 * ":resize".
7196 * set, increment or decrement current window height
7197 */
7198 static void
7199ex_resize(eap)
7200 exarg_T *eap;
7201{
7202 int n;
7203 win_T *wp = curwin;
7204
7205 if (eap->addr_count > 0)
7206 {
7207 n = eap->line2;
7208 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
7209 ;
7210 }
7211
7212#ifdef FEAT_GUI
7213 need_mouse_correct = TRUE;
7214#endif
7215 n = atol((char *)eap->arg);
7216#ifdef FEAT_VERTSPLIT
7217 if (cmdmod.split & WSP_VERT)
7218 {
7219 if (*eap->arg == '-' || *eap->arg == '+')
7220 n += W_WIDTH(curwin);
7221 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
7222 n = 9999;
7223 win_setwidth_win((int)n, wp);
7224 }
7225 else
7226#endif
7227 {
7228 if (*eap->arg == '-' || *eap->arg == '+')
7229 n += curwin->w_height;
7230 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
7231 n = 9999;
7232 win_setheight_win((int)n, wp);
7233 }
7234}
7235#endif
7236
7237/*
7238 * ":find [+command] <file>" command.
7239 */
7240 static void
7241ex_find(eap)
7242 exarg_T *eap;
7243{
7244#ifdef FEAT_SEARCHPATH
7245 char_u *fname;
7246 int count;
7247
7248 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
7249 TRUE, curbuf->b_ffname);
7250 if (eap->addr_count > 0)
7251 {
7252 /* Repeat finding the file "count" times. This matters when it
7253 * appears several times in the path. */
7254 count = eap->line2;
7255 while (fname != NULL && --count > 0)
7256 {
7257 vim_free(fname);
7258 fname = find_file_in_path(NULL, 0, FNAME_MESS,
7259 FALSE, curbuf->b_ffname);
7260 }
7261 }
7262
7263 if (fname != NULL)
7264 {
7265 eap->arg = fname;
7266#endif
7267 do_exedit(eap, NULL);
7268#ifdef FEAT_SEARCHPATH
7269 vim_free(fname);
7270 }
7271#endif
7272}
7273
7274/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00007275 * ":open" simulation: for now just work like ":visual".
7276 */
7277 static void
7278ex_open(eap)
7279 exarg_T *eap;
7280{
7281 regmatch_T regmatch;
7282 char_u *p;
7283
7284 curwin->w_cursor.lnum = eap->line2;
7285 beginline(BL_SOL | BL_FIX);
7286 if (*eap->arg == '/')
7287 {
7288 /* ":open /pattern/": put cursor in column found with pattern */
7289 ++eap->arg;
7290 p = skip_regexp(eap->arg, '/', p_magic, NULL);
7291 *p = NUL;
7292 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
7293 if (regmatch.regprog != NULL)
7294 {
7295 regmatch.rm_ic = p_ic;
7296 p = ml_get_curline();
7297 if (vim_regexec(&regmatch, p, (colnr_T)0))
7298 curwin->w_cursor.col = regmatch.startp[0] - p;
7299 else
7300 EMSG(_(e_nomatch));
7301 vim_free(regmatch.regprog);
7302 }
7303 /* Move to the NUL, ignore any other arguments. */
7304 eap->arg += STRLEN(eap->arg);
7305 }
7306 check_cursor();
7307
7308 eap->cmdidx = CMD_visual;
7309 do_exedit(eap, NULL);
7310}
7311
7312/*
7313 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007314 */
7315 static void
7316ex_edit(eap)
7317 exarg_T *eap;
7318{
7319 do_exedit(eap, NULL);
7320}
7321
7322/*
7323 * ":edit <file>" command and alikes.
7324 */
7325/*ARGSUSED*/
7326 void
7327do_exedit(eap, old_curwin)
7328 exarg_T *eap;
7329 win_T *old_curwin; /* curwin before doing a split or NULL */
7330{
7331 int n;
7332#ifdef FEAT_WINDOWS
7333 int need_hide;
7334#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00007335 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007336
7337 /*
7338 * ":vi" command ends Ex mode.
7339 */
7340 if (exmode_active && (eap->cmdidx == CMD_visual
7341 || eap->cmdidx == CMD_view))
7342 {
7343 exmode_active = FALSE;
7344 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007345 {
7346 /* Special case: ":global/pat/visual\NLvi-commands" */
7347 if (global_busy)
7348 {
7349 int rd = RedrawingDisabled;
7350 int nwr = no_wait_return;
7351 int ms = msg_scroll;
7352#ifdef FEAT_GUI
7353 int he = hold_gui_events;
7354#endif
7355
7356 if (eap->nextcmd != NULL)
7357 {
7358 stuffReadbuff(eap->nextcmd);
7359 eap->nextcmd = NULL;
7360 }
7361
7362 if (exmode_was != EXMODE_VIM)
7363 settmode(TMODE_RAW);
7364 RedrawingDisabled = 0;
7365 no_wait_return = 0;
7366 need_wait_return = FALSE;
7367 msg_scroll = 0;
7368#ifdef FEAT_GUI
7369 hold_gui_events = 0;
7370#endif
7371 must_redraw = CLEAR;
7372
7373 main_loop(FALSE, TRUE);
7374
7375 RedrawingDisabled = rd;
7376 no_wait_return = nwr;
7377 msg_scroll = ms;
7378#ifdef FEAT_GUI
7379 hold_gui_events = he;
7380#endif
7381 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007382 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007383 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007384 }
7385
7386 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007387 || eap->cmdidx == CMD_tabnew
7388 || eap->cmdidx == CMD_tabedit
Bram Moolenaar071d4272004-06-13 20:20:40 +00007389#ifdef FEAT_VERTSPLIT
7390 || eap->cmdidx == CMD_vnew
7391#endif
7392 ) && *eap->arg == NUL)
7393 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007394 /* ":new" or ":tabnew" without argument: edit an new empty buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007395 setpcmark();
7396 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
7397 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0));
7398 }
7399 else if ((eap->cmdidx != CMD_split
7400#ifdef FEAT_VERTSPLIT
7401 && eap->cmdidx != CMD_vsplit
7402#endif
7403 )
7404 || *eap->arg != NUL
7405#ifdef FEAT_BROWSE
7406 || cmdmod.browse
7407#endif
7408 )
7409 {
Bram Moolenaar5555acc2006-04-07 21:33:12 +00007410#ifdef FEAT_AUTOCMD
7411 /* Can't edit another file when "curbuf_lock" is set. Only ":edit"
7412 * can bring us here, others are stopped earlier. */
7413 if (*eap->arg != NUL && curbuf_locked())
7414 return;
7415#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007416 n = readonlymode;
7417 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
7418 readonlymode = TRUE;
7419 else if (eap->cmdidx == CMD_enew)
7420 readonlymode = FALSE; /* 'readonly' doesn't make sense in an
7421 empty buffer */
7422 setpcmark();
7423 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
7424 NULL, eap,
7425 /* ":edit" goes to first line if Vi compatible */
7426 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
7427 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
7428 ? ECMD_ONE : eap->do_ecmd_lnum,
7429 (P_HID(curbuf) ? ECMD_HIDE : 0)
7430 + (eap->forceit ? ECMD_FORCEIT : 0)
7431#ifdef FEAT_LISTCMDS
7432 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
7433#endif
7434 ) == FAIL)
7435 {
7436 /* Editing the file failed. If the window was split, close it. */
7437#ifdef FEAT_WINDOWS
7438 if (old_curwin != NULL)
7439 {
7440 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
7441 if (!need_hide || P_HID(curbuf))
7442 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007443# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7444 cleanup_T cs;
7445
7446 /* Reset the error/interrupt/exception state here so that
7447 * aborting() returns FALSE when closing a window. */
7448 enter_cleanup(&cs);
7449# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007450# ifdef FEAT_GUI
7451 need_mouse_correct = TRUE;
7452# endif
7453 win_close(curwin, !need_hide && !P_HID(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007454
7455# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7456 /* Restore the error/interrupt/exception state if not
7457 * discarded by a new aborting error, interrupt, or
7458 * uncaught exception. */
7459 leave_cleanup(&cs);
7460# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007461 }
7462 }
7463#endif
7464 }
7465 else if (readonlymode && curbuf->b_nwindows == 1)
7466 {
7467 /* When editing an already visited buffer, 'readonly' won't be set
7468 * but the previous value is kept. With ":view" and ":sview" we
7469 * want the file to be readonly, except when another window is
7470 * editing the same buffer. */
7471 curbuf->b_p_ro = TRUE;
7472 }
7473 readonlymode = n;
7474 }
7475 else
7476 {
7477 if (eap->do_ecmd_cmd != NULL)
7478 do_cmdline_cmd(eap->do_ecmd_cmd);
7479#ifdef FEAT_TITLE
7480 n = curwin->w_arg_idx_invalid;
7481#endif
7482 check_arg_idx(curwin);
7483#ifdef FEAT_TITLE
7484 if (n != curwin->w_arg_idx_invalid)
7485 maketitle();
7486#endif
7487 }
7488
7489#ifdef FEAT_WINDOWS
7490 /*
7491 * if ":split file" worked, set alternate file name in old window to new
7492 * file
7493 */
7494 if (old_curwin != NULL
7495 && *eap->arg != NUL
7496 && curwin != old_curwin
7497 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00007498 && old_curwin->w_buffer != curbuf
7499 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007500 old_curwin->w_alt_fnum = curbuf->b_fnum;
7501#endif
7502
7503 ex_no_reprint = TRUE;
7504}
7505
7506#ifndef FEAT_GUI
7507/*
7508 * ":gui" and ":gvim" when there is no GUI.
7509 */
7510 static void
7511ex_nogui(eap)
7512 exarg_T *eap;
7513{
7514 eap->errmsg = e_nogvim;
7515}
7516#endif
7517
7518#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
7519 static void
7520ex_tearoff(eap)
7521 exarg_T *eap;
7522{
7523 gui_make_tearoff(eap->arg);
7524}
7525#endif
7526
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007527#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007528 static void
7529ex_popup(eap)
7530 exarg_T *eap;
7531{
Bram Moolenaar97409f12005-07-08 22:17:29 +00007532 gui_make_popup(eap->arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007533}
7534#endif
7535
7536/*ARGSUSED*/
7537 static void
7538ex_swapname(eap)
7539 exarg_T *eap;
7540{
7541 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
7542 MSG(_("No swap file"));
7543 else
7544 msg(curbuf->b_ml.ml_mfp->mf_fname);
7545}
7546
7547/*
7548 * ":syncbind" forces all 'scrollbind' windows to have the same relative
7549 * offset.
7550 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
7551 */
7552/*ARGSUSED*/
7553 static void
7554ex_syncbind(eap)
7555 exarg_T *eap;
7556{
7557#ifdef FEAT_SCROLLBIND
7558 win_T *wp;
7559 long topline;
7560 long y;
7561 linenr_T old_linenr = curwin->w_cursor.lnum;
7562
7563 setpcmark();
7564
7565 /*
7566 * determine max topline
7567 */
7568 if (curwin->w_p_scb)
7569 {
7570 topline = curwin->w_topline;
7571 for (wp = firstwin; wp; wp = wp->w_next)
7572 {
7573 if (wp->w_p_scb && wp->w_buffer)
7574 {
7575 y = wp->w_buffer->b_ml.ml_line_count - p_so;
7576 if (topline > y)
7577 topline = y;
7578 }
7579 }
7580 if (topline < 1)
7581 topline = 1;
7582 }
7583 else
7584 {
7585 topline = 1;
7586 }
7587
7588
7589 /*
7590 * set all scrollbind windows to the same topline
7591 */
7592 wp = curwin;
7593 for (curwin = firstwin; curwin; curwin = curwin->w_next)
7594 {
7595 if (curwin->w_p_scb)
7596 {
7597 y = topline - curwin->w_topline;
7598 if (y > 0)
7599 scrollup(y, TRUE);
7600 else
7601 scrolldown(-y, TRUE);
7602 curwin->w_scbind_pos = topline;
7603 redraw_later(VALID);
7604 cursor_correct();
7605#ifdef FEAT_WINDOWS
7606 curwin->w_redr_status = TRUE;
7607#endif
7608 }
7609 }
7610 curwin = wp;
7611 if (curwin->w_p_scb)
7612 {
7613 did_syncbind = TRUE;
7614 checkpcmark();
7615 if (old_linenr != curwin->w_cursor.lnum)
7616 {
7617 char_u ctrl_o[2];
7618
7619 ctrl_o[0] = Ctrl_O;
7620 ctrl_o[1] = 0;
7621 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
7622 }
7623 }
7624#endif
7625}
7626
7627
7628 static void
7629ex_read(eap)
7630 exarg_T *eap;
7631{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007632 int i;
7633 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
7634 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007635
7636 if (eap->usefilter) /* :r!cmd */
7637 do_bang(1, eap, FALSE, FALSE, TRUE);
7638 else
7639 {
7640 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
7641 return;
7642
7643#ifdef FEAT_BROWSE
7644 if (cmdmod.browse)
7645 {
7646 char_u *browseFile;
7647
Bram Moolenaar7171abe2004-10-11 10:06:20 +00007648 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007649 NULL, NULL, NULL, curbuf);
7650 if (browseFile != NULL)
7651 {
7652 i = readfile(browseFile, NULL,
7653 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7654 vim_free(browseFile);
7655 }
7656 else
7657 i = OK;
7658 }
7659 else
7660#endif
7661 if (*eap->arg == NUL)
7662 {
7663 if (check_fname() == FAIL) /* check for no file name */
7664 return;
7665 i = readfile(curbuf->b_ffname, curbuf->b_fname,
7666 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7667 }
7668 else
7669 {
7670 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
7671 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
7672 i = readfile(eap->arg, NULL,
7673 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7674
7675 }
7676 if (i == FAIL)
7677 {
7678#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7679 if (!aborting())
7680#endif
7681 EMSG2(_(e_notopen), eap->arg);
7682 }
7683 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00007684 {
7685 if (empty && exmode_active)
7686 {
7687 /* Delete the empty line that remains. Historically ex does
7688 * this but vi doesn't. */
7689 if (eap->line2 == 0)
7690 lnum = curbuf->b_ml.ml_line_count;
7691 else
7692 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007693 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007694 {
7695 ml_delete(lnum, FALSE);
7696 deleted_lines_mark(lnum, 1L);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007697 if (curwin->w_cursor.lnum > 1
7698 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007699 --curwin->w_cursor.lnum;
7700 }
7701 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007702 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007703 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007704 }
7705}
7706
Bram Moolenaarea408852005-06-25 22:49:46 +00007707static char_u *prev_dir = NULL;
7708
7709#if defined(EXITFREE) || defined(PROTO)
7710 void
7711free_cd_dir()
7712{
7713 vim_free(prev_dir);
7714}
7715#endif
7716
7717
Bram Moolenaar071d4272004-06-13 20:20:40 +00007718/*
7719 * ":cd", ":lcd", ":chdir" and ":lchdir".
7720 */
7721 static void
7722ex_cd(eap)
7723 exarg_T *eap;
7724{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007725 char_u *new_dir;
7726 char_u *tofree;
7727
7728 new_dir = eap->arg;
7729#if !defined(UNIX) && !defined(VMS)
7730 /* for non-UNIX ":cd" means: print current directory */
7731 if (*new_dir == NUL)
7732 ex_pwd(NULL);
7733 else
7734#endif
7735 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007736 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged()
7737 && !eap->forceit)
7738 {
7739 EMSG(_("E747: Cannot change directory, buffer is modifed (add ! to override)"));
7740 return;
7741 }
7742
Bram Moolenaar071d4272004-06-13 20:20:40 +00007743 /* ":cd -": Change to previous directory */
7744 if (STRCMP(new_dir, "-") == 0)
7745 {
7746 if (prev_dir == NULL)
7747 {
7748 EMSG(_("E186: No previous directory"));
7749 return;
7750 }
7751 new_dir = prev_dir;
7752 }
7753
7754 /* Save current directory for next ":cd -" */
7755 tofree = prev_dir;
7756 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7757 prev_dir = vim_strsave(NameBuff);
7758 else
7759 prev_dir = NULL;
7760
7761#if defined(UNIX) || defined(VMS)
7762 /* for UNIX ":cd" means: go to home directory */
7763 if (*new_dir == NUL)
7764 {
7765 /* use NameBuff for home directory name */
7766# ifdef VMS
7767 char_u *p;
7768
7769 p = mch_getenv((char_u *)"SYS$LOGIN");
7770 if (p == NULL || *p == NUL) /* empty is the same as not set */
7771 NameBuff[0] = NUL;
7772 else
Bram Moolenaarb6356332005-07-18 21:40:44 +00007773 vim_strncpy(NameBuff, p, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007774# else
7775 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
7776# endif
7777 new_dir = NameBuff;
7778 }
7779#endif
7780 if (new_dir == NULL || vim_chdir(new_dir))
7781 EMSG(_(e_failed));
7782 else
7783 {
7784 vim_free(curwin->w_localdir);
7785 if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir)
7786 {
7787 /* If still in global directory, need to remember current
7788 * directory as global directory. */
7789 if (globaldir == NULL && prev_dir != NULL)
7790 globaldir = vim_strsave(prev_dir);
7791 /* Remember this local directory for the window. */
7792 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7793 curwin->w_localdir = vim_strsave(NameBuff);
7794 }
7795 else
7796 {
7797 /* We are now in the global directory, no need to remember its
7798 * name. */
7799 vim_free(globaldir);
7800 globaldir = NULL;
7801 curwin->w_localdir = NULL;
7802 }
7803
7804 shorten_fnames(TRUE);
7805
7806 /* Echo the new current directory if the command was typed. */
7807 if (KeyTyped)
7808 ex_pwd(eap);
7809 }
7810 vim_free(tofree);
7811 }
7812}
7813
7814/*
7815 * ":pwd".
7816 */
7817/*ARGSUSED*/
7818 static void
7819ex_pwd(eap)
7820 exarg_T *eap;
7821{
7822 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7823 {
7824#ifdef BACKSLASH_IN_FILENAME
7825 slash_adjust(NameBuff);
7826#endif
7827 msg(NameBuff);
7828 }
7829 else
7830 EMSG(_("E187: Unknown"));
7831}
7832
7833/*
7834 * ":=".
7835 */
7836 static void
7837ex_equal(eap)
7838 exarg_T *eap;
7839{
7840 smsg((char_u *)"%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007841 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007842}
7843
7844 static void
7845ex_sleep(eap)
7846 exarg_T *eap;
7847{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007848 int n;
7849 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007850
7851 if (cursor_valid())
7852 {
7853 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
7854 if (n >= 0)
7855 windgoto((int)n, curwin->w_wcol);
7856 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007857
7858 len = eap->line2;
7859 switch (*eap->arg)
7860 {
7861 case 'm': break;
7862 case NUL: len *= 1000L; break;
7863 default: EMSG2(_(e_invarg2), eap->arg); return;
7864 }
7865 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007866}
7867
7868/*
7869 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
7870 */
7871 void
7872do_sleep(msec)
7873 long msec;
7874{
7875 long done;
7876
7877 cursor_on();
7878 out_flush();
7879 for (done = 0; !got_int && done < msec; done += 1000L)
7880 {
7881 ui_delay(msec - done > 1000L ? 1000L : msec - done, TRUE);
7882 ui_breakcheck();
7883 }
7884}
7885
7886 static void
7887do_exmap(eap, isabbrev)
7888 exarg_T *eap;
7889 int isabbrev;
7890{
7891 int mode;
7892 char_u *cmdp;
7893
7894 cmdp = eap->cmd;
7895 mode = get_map_mode(&cmdp, eap->forceit || isabbrev);
7896
7897 switch (do_map((*cmdp == 'n') ? 2 : (*cmdp == 'u'),
7898 eap->arg, mode, isabbrev))
7899 {
7900 case 1: EMSG(_(e_invarg));
7901 break;
7902 case 2: EMSG(isabbrev ? _(e_noabbr) : _(e_nomap));
7903 break;
7904 }
7905}
7906
7907/*
7908 * ":winsize" command (obsolete).
7909 */
7910 static void
7911ex_winsize(eap)
7912 exarg_T *eap;
7913{
7914 int w, h;
7915 char_u *arg = eap->arg;
7916 char_u *p;
7917
7918 w = getdigits(&arg);
7919 arg = skipwhite(arg);
7920 p = arg;
7921 h = getdigits(&arg);
7922 if (*p != NUL && *arg == NUL)
7923 set_shellsize(w, h, TRUE);
7924 else
7925 EMSG(_("E465: :winsize requires two number arguments"));
7926}
7927
7928#ifdef FEAT_WINDOWS
7929 static void
7930ex_wincmd(eap)
7931 exarg_T *eap;
7932{
7933 int xchar = NUL;
7934 char_u *p;
7935
7936 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
7937 {
7938 /* CTRL-W g and CTRL-W CTRL-G have an extra command character */
7939 if (eap->arg[1] == NUL)
7940 {
7941 EMSG(_(e_invarg));
7942 return;
7943 }
7944 xchar = eap->arg[1];
7945 p = eap->arg + 2;
7946 }
7947 else
7948 p = eap->arg + 1;
7949
7950 eap->nextcmd = check_nextcmd(p);
7951 p = skipwhite(p);
7952 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
7953 EMSG(_(e_invarg));
7954 else
7955 {
7956 /* Pass flags on for ":vertical wincmd ]". */
7957 postponed_split_flags = cmdmod.split;
7958 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
7959 postponed_split_flags = 0;
7960 }
7961}
7962#endif
7963
Bram Moolenaar843ee412004-06-30 16:16:41 +00007964#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007965/*
7966 * ":winpos".
7967 */
7968 static void
7969ex_winpos(eap)
7970 exarg_T *eap;
7971{
7972 int x, y;
7973 char_u *arg = eap->arg;
7974 char_u *p;
7975
7976 if (*arg == NUL)
7977 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00007978# if defined(FEAT_GUI) || defined(MSWIN)
7979# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007980 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00007981# else
7982 if (mch_get_winpos(&x, &y) != FAIL)
7983# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007984 {
7985 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
7986 msg(IObuff);
7987 }
7988 else
7989# endif
7990 EMSG(_("E188: Obtaining window position not implemented for this platform"));
7991 }
7992 else
7993 {
7994 x = getdigits(&arg);
7995 arg = skipwhite(arg);
7996 p = arg;
7997 y = getdigits(&arg);
7998 if (*p == NUL || *arg != NUL)
7999 {
8000 EMSG(_("E466: :winpos requires two number arguments"));
8001 return;
8002 }
8003# ifdef FEAT_GUI
8004 if (gui.in_use)
8005 gui_mch_set_winpos(x, y);
8006 else if (gui.starting)
8007 {
8008 /* Remember the coordinates for when the window is opened. */
8009 gui_win_x = x;
8010 gui_win_y = y;
8011 }
8012# ifdef HAVE_TGETENT
8013 else
8014# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00008015# else
8016# ifdef MSWIN
8017 mch_set_winpos(x, y);
8018# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008019# endif
8020# ifdef HAVE_TGETENT
8021 if (*T_CWP)
8022 term_set_winpos(x, y);
8023# endif
8024 }
8025}
8026#endif
8027
8028/*
8029 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
8030 */
8031 static void
8032ex_operators(eap)
8033 exarg_T *eap;
8034{
8035 oparg_T oa;
8036
8037 clear_oparg(&oa);
8038 oa.regname = eap->regname;
8039 oa.start.lnum = eap->line1;
8040 oa.end.lnum = eap->line2;
8041 oa.line_count = eap->line2 - eap->line1 + 1;
8042 oa.motion_type = MLINE;
8043#ifdef FEAT_VIRTUALEDIT
8044 virtual_op = FALSE;
8045#endif
8046 if (eap->cmdidx != CMD_yank) /* position cursor for undo */
8047 {
8048 setpcmark();
8049 curwin->w_cursor.lnum = eap->line1;
8050 beginline(BL_SOL | BL_FIX);
8051 }
8052
8053 switch (eap->cmdidx)
8054 {
8055 case CMD_delete:
8056 oa.op_type = OP_DELETE;
8057 op_delete(&oa);
8058 break;
8059
8060 case CMD_yank:
8061 oa.op_type = OP_YANK;
8062 (void)op_yank(&oa, FALSE, TRUE);
8063 break;
8064
8065 default: /* CMD_rshift or CMD_lshift */
8066 if ((eap->cmdidx == CMD_rshift)
8067#ifdef FEAT_RIGHTLEFT
8068 ^ curwin->w_p_rl
8069#endif
8070 )
8071 oa.op_type = OP_RSHIFT;
8072 else
8073 oa.op_type = OP_LSHIFT;
8074 op_shift(&oa, FALSE, eap->amount);
8075 break;
8076 }
8077#ifdef FEAT_VIRTUALEDIT
8078 virtual_op = MAYBE;
8079#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00008080 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008081}
8082
8083/*
8084 * ":put".
8085 */
8086 static void
8087ex_put(eap)
8088 exarg_T *eap;
8089{
8090 /* ":0put" works like ":1put!". */
8091 if (eap->line2 == 0)
8092 {
8093 eap->line2 = 1;
8094 eap->forceit = TRUE;
8095 }
8096 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008097 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
8098 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008099}
8100
8101/*
8102 * Handle ":copy" and ":move".
8103 */
8104 static void
8105ex_copymove(eap)
8106 exarg_T *eap;
8107{
8108 long n;
8109
8110 n = get_address(&eap->arg, FALSE, FALSE);
8111 if (eap->arg == NULL) /* error detected */
8112 {
8113 eap->nextcmd = NULL;
8114 return;
8115 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008116 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008117
8118 /*
8119 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
8120 */
8121 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
8122 {
8123 EMSG(_(e_invaddr));
8124 return;
8125 }
8126
8127 if (eap->cmdidx == CMD_move)
8128 {
8129 if (do_move(eap->line1, eap->line2, n) == FAIL)
8130 return;
8131 }
8132 else
8133 ex_copy(eap->line1, eap->line2, n);
8134 u_clearline();
8135 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008136 ex_may_print(eap);
8137}
8138
8139/*
8140 * Print the current line if flags were given to the Ex command.
8141 */
8142 static void
8143ex_may_print(eap)
8144 exarg_T *eap;
8145{
8146 if (eap->flags != 0)
8147 {
8148 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
8149 (eap->flags & EXFLAG_LIST));
8150 ex_no_reprint = TRUE;
8151 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008152}
8153
8154/*
8155 * ":smagic" and ":snomagic".
8156 */
8157 static void
8158ex_submagic(eap)
8159 exarg_T *eap;
8160{
8161 int magic_save = p_magic;
8162
8163 p_magic = (eap->cmdidx == CMD_smagic);
8164 do_sub(eap);
8165 p_magic = magic_save;
8166}
8167
8168/*
8169 * ":join".
8170 */
8171 static void
8172ex_join(eap)
8173 exarg_T *eap;
8174{
8175 curwin->w_cursor.lnum = eap->line1;
8176 if (eap->line1 == eap->line2)
8177 {
8178 if (eap->addr_count >= 2) /* :2,2join does nothing */
8179 return;
8180 if (eap->line2 == curbuf->b_ml.ml_line_count)
8181 {
8182 beep_flush();
8183 return;
8184 }
8185 ++eap->line2;
8186 }
8187 do_do_join(eap->line2 - eap->line1 + 1, !eap->forceit);
8188 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008189 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008190}
8191
8192/*
8193 * ":[addr]@r" or ":[addr]*r": execute register
8194 */
8195 static void
8196ex_at(eap)
8197 exarg_T *eap;
8198{
8199 int c;
8200
8201 curwin->w_cursor.lnum = eap->line2;
8202
8203#ifdef USE_ON_FLY_SCROLL
8204 dont_scroll = TRUE; /* disallow scrolling here */
8205#endif
8206
8207 /* get the register name. No name means to use the previous one */
8208 c = *eap->arg;
8209 if (c == NUL || (c == '*' && *eap->cmd == '*'))
8210 c = '@';
8211 /* put the register in mapbuf */
8212 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL) == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008213 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008214 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00008215 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008216 else
8217 {
8218 int save_efr = exec_from_reg;
8219
8220 exec_from_reg = TRUE;
8221
8222 /*
8223 * Execute from the typeahead buffer.
8224 * Originally this didn't check for the typeahead buffer to be empty,
8225 * thus could read more Ex commands from stdin. It's not clear why,
8226 * it is certainly unexpected.
8227 */
8228 while ((!stuff_empty() || typebuf.tb_len > 0) && vpeekc() == ':')
8229 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
8230
8231 exec_from_reg = save_efr;
8232 }
8233}
8234
8235/*
8236 * ":!".
8237 */
8238 static void
8239ex_bang(eap)
8240 exarg_T *eap;
8241{
8242 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
8243}
8244
8245/*
8246 * ":undo".
8247 */
8248/*ARGSUSED*/
8249 static void
8250ex_undo(eap)
8251 exarg_T *eap;
8252{
Bram Moolenaard3667a22006-03-16 21:35:52 +00008253 if (eap->addr_count == 1) /* :undo 123 */
8254 undo_time(eap->line2, FALSE, TRUE);
8255 else
8256 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008257}
8258
8259/*
8260 * ":redo".
8261 */
8262/*ARGSUSED*/
8263 static void
8264ex_redo(eap)
8265 exarg_T *eap;
8266{
8267 u_redo(1);
8268}
8269
8270/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008271 * ":earlier" and ":later".
8272 */
8273/*ARGSUSED*/
8274 static void
8275ex_later(eap)
8276 exarg_T *eap;
8277{
8278 long count = 0;
8279 int sec = FALSE;
8280 char_u *p = eap->arg;
8281
8282 if (*p == NUL)
8283 count = 1;
8284 else if (isdigit(*p))
8285 {
8286 count = getdigits(&p);
8287 switch (*p)
8288 {
8289 case 's': ++p; sec = TRUE; break;
8290 case 'm': ++p; sec = TRUE; count *= 60; break;
8291 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
8292 }
8293 }
8294
8295 if (*p != NUL)
8296 EMSG2(_(e_invarg2), eap->arg);
8297 else
Bram Moolenaard3667a22006-03-16 21:35:52 +00008298 undo_time(eap->cmdidx == CMD_earlier ? -count : count, sec, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008299}
8300
8301/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008302 * ":redir": start/stop redirection.
8303 */
8304 static void
8305ex_redir(eap)
8306 exarg_T *eap;
8307{
8308 char *mode;
8309 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00008310 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008311
8312 if (STRICMP(eap->arg, "END") == 0)
8313 close_redir();
8314 else
8315 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008316 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008317 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008318 ++arg;
8319 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008320 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008321 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008322 mode = "a";
8323 }
8324 else
8325 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00008326 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008327
8328 close_redir();
8329
8330 /* Expand environment variables and "~/". */
Bram Moolenaarca472992005-01-21 11:46:23 +00008331 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008332 if (fname == NULL)
8333 return;
8334#ifdef FEAT_BROWSE
8335 if (cmdmod.browse)
8336 {
8337 char_u *browseFile;
8338
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008339 browseFile = do_browse(BROWSE_SAVE,
8340 (char_u *)_("Save Redirection"),
8341 fname, NULL, NULL, BROWSE_FILTER_ALL_FILES, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008342 if (browseFile == NULL)
8343 return; /* operation cancelled */
8344 vim_free(fname);
8345 fname = browseFile;
8346 eap->forceit = TRUE; /* since dialog already asked */
8347 }
8348#endif
8349
8350 redir_fd = open_exfile(fname, eap->forceit, mode);
8351 vim_free(fname);
8352 }
8353#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00008354 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008355 {
8356 /* redirect to a register a-z (resp. A-Z for appending) */
8357 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00008358 ++arg;
8359 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008360# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00008361 || *arg == '*'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008362# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00008363 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008364 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008365 redir_reg = *arg++;
Bram Moolenaard9d30582005-05-18 22:10:28 +00008366 if (*arg == '>' && arg[1] == '>')
8367 arg += 2;
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00008368 else if ((*arg == NUL || (*arg == '>' && arg[1] == NUL)) &&
8369 (islower(redir_reg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008370# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00008371 || redir_reg == '*'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008372# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00008373 || redir_reg == '"'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008374 {
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00008375 if (*arg == '>')
8376 arg++;
8377
Bram Moolenaar071d4272004-06-13 20:20:40 +00008378 /* make register empty */
8379 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
8380 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008381 }
8382 if (*arg != NUL)
8383 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00008384 redir_reg = 0;
Bram Moolenaard9d30582005-05-18 22:10:28 +00008385 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008386 }
8387 }
8388 else if (*arg == '=' && arg[1] == '>')
8389 {
8390 int append;
8391
8392 /* redirect to a variable */
8393 close_redir();
8394 arg += 2;
8395
8396 if (*arg == '>')
8397 {
8398 ++arg;
8399 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008400 }
8401 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008402 append = FALSE;
8403
8404 if (var_redir_start(skipwhite(arg), append) == OK)
8405 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008406 }
8407#endif
8408
8409 /* TODO: redirect to a buffer */
8410
Bram Moolenaar071d4272004-06-13 20:20:40 +00008411 else
Bram Moolenaarca472992005-01-21 11:46:23 +00008412 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008413 }
8414}
8415
8416/*
8417 * ":redraw": force redraw
8418 */
8419 static void
8420ex_redraw(eap)
8421 exarg_T *eap;
8422{
8423 int r = RedrawingDisabled;
8424 int p = p_lz;
8425
8426 RedrawingDisabled = 0;
8427 p_lz = FALSE;
8428 update_topline();
8429 update_screen(eap->forceit ? CLEAR :
8430#ifdef FEAT_VISUAL
8431 VIsual_active ? INVERTED :
8432#endif
8433 0);
8434#ifdef FEAT_TITLE
8435 if (need_maketitle)
8436 maketitle();
8437#endif
8438 RedrawingDisabled = r;
8439 p_lz = p;
8440
8441 /* Reset msg_didout, so that a message that's there is overwritten. */
8442 msg_didout = FALSE;
8443 msg_col = 0;
8444
8445 out_flush();
8446}
8447
8448/*
8449 * ":redrawstatus": force redraw of status line(s)
8450 */
8451/*ARGSUSED*/
8452 static void
8453ex_redrawstatus(eap)
8454 exarg_T *eap;
8455{
8456#if defined(FEAT_WINDOWS)
8457 int r = RedrawingDisabled;
8458 int p = p_lz;
8459
8460 RedrawingDisabled = 0;
8461 p_lz = FALSE;
8462 if (eap->forceit)
8463 status_redraw_all();
8464 else
8465 status_redraw_curbuf();
8466 update_screen(
8467# ifdef FEAT_VISUAL
8468 VIsual_active ? INVERTED :
8469# endif
8470 0);
8471 RedrawingDisabled = r;
8472 p_lz = p;
8473 out_flush();
8474#endif
8475}
8476
8477 static void
8478close_redir()
8479{
8480 if (redir_fd != NULL)
8481 {
8482 fclose(redir_fd);
8483 redir_fd = NULL;
8484 }
8485#ifdef FEAT_EVAL
8486 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008487 if (redir_vname)
8488 {
8489 var_redir_stop();
8490 redir_vname = 0;
8491 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008492#endif
8493}
8494
8495#if defined(FEAT_SESSION) && defined(USE_CRNL)
8496# define MKSESSION_NL
8497static int mksession_nl = FALSE; /* use NL only in put_eol() */
8498#endif
8499
8500/*
8501 * ":mkexrc", ":mkvimrc", ":mkview" and ":mksession".
8502 */
8503 static void
8504ex_mkrc(eap)
8505 exarg_T *eap;
8506{
8507 FILE *fd;
8508 int failed = FALSE;
8509 char_u *fname;
8510#ifdef FEAT_BROWSE
8511 char_u *browseFile = NULL;
8512#endif
8513#ifdef FEAT_SESSION
8514 int view_session = FALSE;
8515 int using_vdir = FALSE; /* using 'viewdir'? */
8516 char_u *viewFile = NULL;
8517 unsigned *flagp;
8518#endif
8519
8520 if (eap->cmdidx == CMD_mksession || eap->cmdidx == CMD_mkview)
8521 {
8522#ifdef FEAT_SESSION
8523 view_session = TRUE;
8524#else
8525 ex_ni(eap);
8526 return;
8527#endif
8528 }
8529
8530#ifdef FEAT_SESSION
8531 /* ":mkview" or ":mkview 9": generate file name with 'viewdir' */
8532 if (eap->cmdidx == CMD_mkview
8533 && (*eap->arg == NUL
8534 || (vim_isdigit(*eap->arg) && eap->arg[1] == NUL)))
8535 {
8536 eap->forceit = TRUE;
8537 fname = get_view_file(*eap->arg);
8538 if (fname == NULL)
8539 return;
8540 viewFile = fname;
8541 using_vdir = TRUE;
8542 }
8543 else
8544#endif
8545 if (*eap->arg != NUL)
8546 fname = eap->arg;
8547 else if (eap->cmdidx == CMD_mkvimrc)
8548 fname = (char_u *)VIMRC_FILE;
8549#ifdef FEAT_SESSION
8550 else if (eap->cmdidx == CMD_mksession)
8551 fname = (char_u *)SESSION_FILE;
8552#endif
8553 else
8554 fname = (char_u *)EXRC_FILE;
8555
8556#ifdef FEAT_BROWSE
8557 if (cmdmod.browse)
8558 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008559 browseFile = do_browse(BROWSE_SAVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008560# ifdef FEAT_SESSION
8561 eap->cmdidx == CMD_mkview ? (char_u *)_("Save View") :
8562 eap->cmdidx == CMD_mksession ? (char_u *)_("Save Session") :
8563# endif
8564 (char_u *)_("Save Setup"),
8565 fname, (char_u *)"vim", NULL, BROWSE_FILTER_MACROS, NULL);
8566 if (browseFile == NULL)
8567 goto theend;
8568 fname = browseFile;
8569 eap->forceit = TRUE; /* since dialog already asked */
8570 }
8571#endif
8572
8573#if defined(FEAT_SESSION) && defined(vim_mkdir)
8574 /* When using 'viewdir' may have to create the directory. */
8575 if (using_vdir && !mch_isdir(p_vdir))
Bram Moolenaardf177f62005-02-22 08:39:57 +00008576 vim_mkdir_emsg(p_vdir, 0755);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008577#endif
8578
8579 fd = open_exfile(fname, eap->forceit, WRITEBIN);
8580 if (fd != NULL)
8581 {
8582#ifdef FEAT_SESSION
8583 if (eap->cmdidx == CMD_mkview)
8584 flagp = &vop_flags;
8585 else
8586 flagp = &ssop_flags;
8587#endif
8588
8589#ifdef MKSESSION_NL
8590 /* "unix" in 'sessionoptions': use NL line separator */
8591 if (view_session && (*flagp & SSOP_UNIX))
8592 mksession_nl = TRUE;
8593#endif
8594
8595 /* Write the version command for :mkvimrc */
8596 if (eap->cmdidx == CMD_mkvimrc)
8597 (void)put_line(fd, "version 6.0");
8598
8599#ifdef FEAT_SESSION
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008600 if (eap->cmdidx == CMD_mksession)
8601 {
8602 if (put_line(fd, "let SessionLoad = 1") == FAIL)
8603 failed = TRUE;
8604 }
8605
Bram Moolenaar071d4272004-06-13 20:20:40 +00008606 if (eap->cmdidx != CMD_mkview)
8607#endif
8608 {
8609 /* Write setting 'compatible' first, because it has side effects.
8610 * For that same reason only do it when needed. */
8611 if (p_cp)
8612 (void)put_line(fd, "if !&cp | set cp | endif");
8613 else
8614 (void)put_line(fd, "if &cp | set nocp | endif");
8615 }
8616
8617#ifdef FEAT_SESSION
8618 if (!view_session
8619 || (eap->cmdidx == CMD_mksession
8620 && (*flagp & SSOP_OPTIONS)))
8621#endif
8622 failed |= (makemap(fd, NULL) == FAIL
8623 || makeset(fd, OPT_GLOBAL, FALSE) == FAIL);
8624
8625#ifdef FEAT_SESSION
8626 if (!failed && view_session)
8627 {
8628 if (put_line(fd, "let s:so_save = &so | let s:siso_save = &siso | set so=0 siso=0") == FAIL)
8629 failed = TRUE;
8630 if (eap->cmdidx == CMD_mksession)
8631 {
8632 char_u dirnow[MAXPATHL]; /* current directory */
8633
8634 /*
8635 * Change to session file's dir.
8636 */
8637 if (mch_dirname(dirnow, MAXPATHL) == FAIL
8638 || mch_chdir((char *)dirnow) != 0)
8639 *dirnow = NUL;
8640 if (*dirnow != NUL && (ssop_flags & SSOP_SESDIR))
8641 {
8642 if (vim_chdirfile(fname) == OK)
8643 shorten_fnames(TRUE);
8644 }
8645 else if (*dirnow != NUL
8646 && (ssop_flags & SSOP_CURDIR) && globaldir != NULL)
8647 {
8648 (void)mch_chdir((char *)globaldir);
8649 shorten_fnames(TRUE);
8650 }
8651
8652 failed |= (makeopens(fd, dirnow) == FAIL);
8653
8654 /* restore original dir */
8655 if (*dirnow != NUL && ((ssop_flags & SSOP_SESDIR)
8656 || ((ssop_flags & SSOP_CURDIR) && globaldir != NULL)))
8657 {
8658 if (mch_chdir((char *)dirnow) != 0)
8659 EMSG(_(e_prev_dir));
8660 shorten_fnames(TRUE);
8661 }
8662 }
8663 else
8664 {
8665 failed |= (put_view(fd, curwin, !using_vdir, flagp) == FAIL);
8666 }
8667 if (put_line(fd, "let &so = s:so_save | let &siso = s:siso_save")
8668 == FAIL)
8669 failed = TRUE;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008670 if (put_line(fd, "doautoall SessionLoadPost") == FAIL)
8671 failed = TRUE;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008672 if (eap->cmdidx == CMD_mksession)
8673 {
8674 if (put_line(fd, "unlet SessionLoad") == FAIL)
8675 failed = TRUE;
8676 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008677 }
8678#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008679 if (put_line(fd, "\" vim: set ft=vim :") == FAIL)
8680 failed = TRUE;
8681
Bram Moolenaar071d4272004-06-13 20:20:40 +00008682 failed |= fclose(fd);
8683
8684 if (failed)
8685 EMSG(_(e_write));
8686#if defined(FEAT_EVAL) && defined(FEAT_SESSION)
8687 else if (eap->cmdidx == CMD_mksession)
8688 {
8689 /* successful session write - set this_session var */
8690 char_u tbuf[MAXPATHL];
8691
8692 if (vim_FullName(fname, tbuf, MAXPATHL, FALSE) == OK)
8693 set_vim_var_string(VV_THIS_SESSION, tbuf, -1);
8694 }
8695#endif
8696#ifdef MKSESSION_NL
8697 mksession_nl = FALSE;
8698#endif
8699 }
8700
8701#ifdef FEAT_BROWSE
8702theend:
8703 vim_free(browseFile);
8704#endif
8705#ifdef FEAT_SESSION
8706 vim_free(viewFile);
8707#endif
8708}
8709
Bram Moolenaardf177f62005-02-22 08:39:57 +00008710#if ((defined(FEAT_SESSION) || defined(FEAT_EVAL)) && defined(vim_mkdir)) \
8711 || defined(PROTO)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008712/*ARGSUSED*/
Bram Moolenaardf177f62005-02-22 08:39:57 +00008713 int
8714vim_mkdir_emsg(name, prot)
8715 char_u *name;
8716 int prot;
8717{
8718 if (vim_mkdir(name, prot) != 0)
8719 {
8720 EMSG2(_("E739: Cannot create directory: %s"), name);
8721 return FAIL;
8722 }
8723 return OK;
8724}
8725#endif
8726
Bram Moolenaar071d4272004-06-13 20:20:40 +00008727/*
8728 * Open a file for writing for an Ex command, with some checks.
8729 * Return file descriptor, or NULL on failure.
8730 */
8731 FILE *
8732open_exfile(fname, forceit, mode)
8733 char_u *fname;
8734 int forceit;
8735 char *mode; /* "w" for create new file or "a" for append */
8736{
8737 FILE *fd;
8738
8739#ifdef UNIX
8740 /* with Unix it is possible to open a directory */
8741 if (mch_isdir(fname))
8742 {
8743 EMSG2(_(e_isadir2), fname);
8744 return NULL;
8745 }
8746#endif
8747 if (!forceit && *mode != 'a' && vim_fexists(fname))
8748 {
8749 EMSG2(_("E189: \"%s\" exists (add ! to override)"), fname);
8750 return NULL;
8751 }
8752
8753 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
8754 EMSG2(_("E190: Cannot open \"%s\" for writing"), fname);
8755
8756 return fd;
8757}
8758
8759/*
8760 * ":mark" and ":k".
8761 */
8762 static void
8763ex_mark(eap)
8764 exarg_T *eap;
8765{
8766 pos_T pos;
8767
8768 if (*eap->arg == NUL) /* No argument? */
8769 EMSG(_(e_argreq));
8770 else if (eap->arg[1] != NUL) /* more than one character? */
8771 EMSG(_(e_trailing));
8772 else
8773 {
8774 pos = curwin->w_cursor; /* save curwin->w_cursor */
8775 curwin->w_cursor.lnum = eap->line2;
8776 beginline(BL_WHITE | BL_FIX);
8777 if (setmark(*eap->arg) == FAIL) /* set mark */
8778 EMSG(_("E191: Argument must be a letter or forward/backward quote"));
8779 curwin->w_cursor = pos; /* restore curwin->w_cursor */
8780 }
8781}
8782
8783/*
8784 * Update w_topline, w_leftcol and the cursor position.
8785 */
8786 void
8787update_topline_cursor()
8788{
8789 check_cursor(); /* put cursor on valid line */
8790 update_topline();
8791 if (!curwin->w_p_wrap)
8792 validate_cursor();
8793 update_curswant();
8794}
8795
8796#ifdef FEAT_EX_EXTRA
8797/*
8798 * ":normal[!] {commands}": Execute normal mode commands.
8799 */
8800 static void
8801ex_normal(eap)
8802 exarg_T *eap;
8803{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008804 int save_msg_scroll = msg_scroll;
8805 int save_restart_edit = restart_edit;
8806 int save_msg_didout = msg_didout;
8807 int save_State = State;
8808 tasave_T tabuf;
8809 int save_insertmode = p_im;
8810 int save_finish_op = finish_op;
8811#ifdef FEAT_MBYTE
8812 char_u *arg = NULL;
8813 int l;
8814 char_u *p;
8815#endif
8816
8817 if (ex_normal_busy >= p_mmd)
8818 {
8819 EMSG(_("E192: Recursive use of :normal too deep"));
8820 return;
8821 }
8822 ++ex_normal_busy;
8823
8824 msg_scroll = FALSE; /* no msg scrolling in Normal mode */
8825 restart_edit = 0; /* don't go to Insert mode */
8826 p_im = FALSE; /* don't use 'insertmode' */
8827
8828#ifdef FEAT_MBYTE
8829 /*
8830 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
8831 * this for the K_SPECIAL leading byte, otherwise special keys will not
8832 * work.
8833 */
8834 if (has_mbyte)
8835 {
8836 int len = 0;
8837
8838 /* Count the number of characters to be escaped. */
8839 for (p = eap->arg; *p != NUL; ++p)
8840 {
8841# ifdef FEAT_GUI
8842 if (*p == CSI) /* leadbyte CSI */
8843 len += 2;
8844# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008845 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008846 if (*++p == K_SPECIAL /* trailbyte K_SPECIAL or CSI */
8847# ifdef FEAT_GUI
8848 || *p == CSI
8849# endif
8850 )
8851 len += 2;
8852 }
8853 if (len > 0)
8854 {
8855 arg = alloc((unsigned)(STRLEN(eap->arg) + len + 1));
8856 if (arg != NULL)
8857 {
8858 len = 0;
8859 for (p = eap->arg; *p != NUL; ++p)
8860 {
8861 arg[len++] = *p;
8862# ifdef FEAT_GUI
8863 if (*p == CSI)
8864 {
8865 arg[len++] = KS_EXTRA;
8866 arg[len++] = (int)KE_CSI;
8867 }
8868# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008869 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008870 {
8871 arg[len++] = *++p;
8872 if (*p == K_SPECIAL)
8873 {
8874 arg[len++] = KS_SPECIAL;
8875 arg[len++] = KE_FILLER;
8876 }
8877# ifdef FEAT_GUI
8878 else if (*p == CSI)
8879 {
8880 arg[len++] = KS_EXTRA;
8881 arg[len++] = (int)KE_CSI;
8882 }
8883# endif
8884 }
8885 arg[len] = NUL;
8886 }
8887 }
8888 }
8889 }
8890#endif
8891
8892 /*
8893 * Save the current typeahead. This is required to allow using ":normal"
8894 * from an event handler and makes sure we don't hang when the argument
8895 * ends with half a command.
8896 */
8897 save_typeahead(&tabuf);
8898 if (tabuf.typebuf_valid)
8899 {
8900 /*
8901 * Repeat the :normal command for each line in the range. When no
8902 * range given, execute it just once, without positioning the cursor
8903 * first.
8904 */
8905 do
8906 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008907 if (eap->addr_count != 0)
8908 {
8909 curwin->w_cursor.lnum = eap->line1++;
8910 curwin->w_cursor.col = 0;
8911 }
8912
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008913 exec_normal_cmd(
Bram Moolenaar071d4272004-06-13 20:20:40 +00008914#ifdef FEAT_MBYTE
8915 arg != NULL ? arg :
8916#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008917 eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008918 }
8919 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
8920 }
8921
8922 /* Might not return to the main loop when in an event handler. */
8923 update_topline_cursor();
8924
8925 /* Restore the previous typeahead. */
8926 restore_typeahead(&tabuf);
8927
8928 --ex_normal_busy;
8929 msg_scroll = save_msg_scroll;
8930 restart_edit = save_restart_edit;
8931 p_im = save_insertmode;
8932 finish_op = save_finish_op;
8933 msg_didout |= save_msg_didout; /* don't reset msg_didout now */
8934
8935 /* Restore the state (needed when called from a function executed for
8936 * 'indentexpr'). */
8937 State = save_State;
8938#ifdef FEAT_MBYTE
8939 vim_free(arg);
8940#endif
8941}
8942
8943/*
Bram Moolenaar64969662005-12-14 21:59:55 +00008944 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008945 */
8946 static void
8947ex_startinsert(eap)
8948 exarg_T *eap;
8949{
Bram Moolenaarfd371682005-01-14 21:42:54 +00008950 if (eap->forceit)
8951 {
8952 coladvance((colnr_T)MAXCOL);
8953 curwin->w_curswant = MAXCOL;
8954 curwin->w_set_curswant = FALSE;
8955 }
8956
Bram Moolenaara40c5002005-01-09 21:16:21 +00008957 /* Ignore the command when already in Insert mode. Inserting an
8958 * expression register that invokes a function can do this. */
8959 if (State & INSERT)
8960 return;
8961
Bram Moolenaar64969662005-12-14 21:59:55 +00008962 if (eap->cmdidx == CMD_startinsert)
8963 restart_edit = 'a';
8964 else if (eap->cmdidx == CMD_startreplace)
8965 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008966 else
Bram Moolenaar64969662005-12-14 21:59:55 +00008967 restart_edit = 'V';
8968
8969 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008970 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00008971 if (eap->cmdidx == CMD_startinsert)
8972 restart_edit = 'i';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008973 curwin->w_curswant = 0; /* avoid MAXCOL */
8974 }
8975}
8976
8977/*
8978 * ":stopinsert"
8979 */
8980/*ARGSUSED*/
8981 static void
8982ex_stopinsert(eap)
8983 exarg_T *eap;
8984{
8985 restart_edit = 0;
8986 stop_insert_mode = TRUE;
8987}
8988#endif
8989
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008990#if defined(FEAT_EX_EXTRA) || defined(FEAT_MENU) || defined(PROTO)
8991/*
8992 * Execute normal mode command "cmd".
8993 * "remap" can be REMAP_NONE or REMAP_YES.
8994 */
8995 void
8996exec_normal_cmd(cmd, remap, silent)
8997 char_u *cmd;
8998 int remap;
8999 int silent;
9000{
9001 oparg_T oa;
9002
9003 /*
9004 * Stuff the argument into the typeahead buffer.
9005 * Execute normal_cmd() until there is no typeahead left.
9006 */
9007 clear_oparg(&oa);
9008 finish_op = FALSE;
9009 ins_typebuf(cmd, remap, 0, TRUE, silent);
9010 while ((!stuff_empty() || (!typebuf_typed() && typebuf.tb_len > 0))
9011 && !got_int)
9012 {
9013 update_topline_cursor();
9014 normal_cmd(&oa, FALSE); /* execute a Normal mode cmd */
9015 }
9016}
9017#endif
9018
Bram Moolenaar071d4272004-06-13 20:20:40 +00009019#ifdef FEAT_FIND_ID
9020 static void
9021ex_checkpath(eap)
9022 exarg_T *eap;
9023{
9024 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
9025 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
9026 (linenr_T)1, (linenr_T)MAXLNUM);
9027}
9028
9029#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
9030/*
9031 * ":psearch"
9032 */
9033 static void
9034ex_psearch(eap)
9035 exarg_T *eap;
9036{
9037 g_do_tagpreview = p_pvh;
9038 ex_findpat(eap);
9039 g_do_tagpreview = 0;
9040}
9041#endif
9042
9043 static void
9044ex_findpat(eap)
9045 exarg_T *eap;
9046{
9047 int whole = TRUE;
9048 long n;
9049 char_u *p;
9050 int action;
9051
9052 switch (cmdnames[eap->cmdidx].cmd_name[2])
9053 {
9054 case 'e': /* ":psearch", ":isearch" and ":dsearch" */
9055 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
9056 action = ACTION_GOTO;
9057 else
9058 action = ACTION_SHOW;
9059 break;
9060 case 'i': /* ":ilist" and ":dlist" */
9061 action = ACTION_SHOW_ALL;
9062 break;
9063 case 'u': /* ":ijump" and ":djump" */
9064 action = ACTION_GOTO;
9065 break;
9066 default: /* ":isplit" and ":dsplit" */
9067 action = ACTION_SPLIT;
9068 break;
9069 }
9070
9071 n = 1;
9072 if (vim_isdigit(*eap->arg)) /* get count */
9073 {
9074 n = getdigits(&eap->arg);
9075 eap->arg = skipwhite(eap->arg);
9076 }
9077 if (*eap->arg == '/') /* Match regexp, not just whole words */
9078 {
9079 whole = FALSE;
9080 ++eap->arg;
9081 p = skip_regexp(eap->arg, '/', p_magic, NULL);
9082 if (*p)
9083 {
9084 *p++ = NUL;
9085 p = skipwhite(p);
9086
9087 /* Check for trailing illegal characters */
9088 if (!ends_excmd(*p))
9089 eap->errmsg = e_trailing;
9090 else
9091 eap->nextcmd = check_nextcmd(p);
9092 }
9093 }
9094 if (!eap->skip)
9095 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
9096 whole, !eap->forceit,
9097 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
9098 n, action, eap->line1, eap->line2);
9099}
9100#endif
9101
9102#ifdef FEAT_WINDOWS
9103
9104# ifdef FEAT_QUICKFIX
9105/*
9106 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
9107 */
9108 static void
9109ex_ptag(eap)
9110 exarg_T *eap;
9111{
9112 g_do_tagpreview = p_pvh;
9113 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9114}
9115
9116/*
9117 * ":pedit"
9118 */
9119 static void
9120ex_pedit(eap)
9121 exarg_T *eap;
9122{
9123 win_T *curwin_save = curwin;
9124
9125 g_do_tagpreview = p_pvh;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00009126 prepare_tagpreview(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009127 keep_help_flag = curwin_save->w_buffer->b_help;
9128 do_exedit(eap, NULL);
9129 keep_help_flag = FALSE;
9130 if (curwin != curwin_save && win_valid(curwin_save))
9131 {
9132 /* Return cursor to where we were */
9133 validate_cursor();
9134 redraw_later(VALID);
9135 win_enter(curwin_save, TRUE);
9136 }
9137 g_do_tagpreview = 0;
9138}
9139# endif
9140
9141/*
9142 * ":stag", ":stselect" and ":stjump".
9143 */
9144 static void
9145ex_stag(eap)
9146 exarg_T *eap;
9147{
9148 postponed_split = -1;
9149 postponed_split_flags = cmdmod.split;
9150 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9151 postponed_split_flags = 0;
9152}
9153#endif
9154
9155/*
9156 * ":tag", ":tselect", ":tjump", ":tnext", etc.
9157 */
9158 static void
9159ex_tag(eap)
9160 exarg_T *eap;
9161{
9162 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
9163}
9164
9165 static void
9166ex_tag_cmd(eap, name)
9167 exarg_T *eap;
9168 char_u *name;
9169{
9170 int cmd;
9171
9172 switch (name[1])
9173 {
9174 case 'j': cmd = DT_JUMP; /* ":tjump" */
9175 break;
9176 case 's': cmd = DT_SELECT; /* ":tselect" */
9177 break;
9178 case 'p': cmd = DT_PREV; /* ":tprevious" */
9179 break;
9180 case 'N': cmd = DT_PREV; /* ":tNext" */
9181 break;
9182 case 'n': cmd = DT_NEXT; /* ":tnext" */
9183 break;
9184 case 'o': cmd = DT_POP; /* ":pop" */
9185 break;
9186 case 'f': /* ":tfirst" */
9187 case 'r': cmd = DT_FIRST; /* ":trewind" */
9188 break;
9189 case 'l': cmd = DT_LAST; /* ":tlast" */
9190 break;
9191 default: /* ":tag" */
9192#ifdef FEAT_CSCOPE
9193 if (p_cst)
9194 {
9195 do_cstag(eap);
9196 return;
9197 }
9198#endif
9199 cmd = DT_TAG;
9200 break;
9201 }
9202
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00009203 if (name[0] == 'l')
9204 {
9205#ifndef FEAT_QUICKFIX
9206 ex_ni(eap);
9207 return;
9208#else
9209 cmd = DT_LTAG;
9210#endif
9211 }
9212
Bram Moolenaar071d4272004-06-13 20:20:40 +00009213 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
9214 eap->forceit, TRUE);
9215}
9216
9217/*
9218 * Evaluate cmdline variables.
9219 *
9220 * change '%' to curbuf->b_ffname
9221 * '#' to curwin->w_altfile
9222 * '<cword>' to word under the cursor
9223 * '<cWORD>' to WORD under the cursor
9224 * '<cfile>' to path name under the cursor
9225 * '<sfile>' to sourced file name
9226 * '<afile>' to file name for autocommand
9227 * '<abuf>' to buffer number for autocommand
9228 * '<amatch>' to matching name for autocommand
9229 *
9230 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
9231 * "" for error without a message) and NULL is returned.
9232 * Returns an allocated string if a valid match was found.
9233 * Returns NULL if no match was found. "usedlen" then still contains the
9234 * number of characters to skip.
9235 */
9236 char_u *
9237eval_vars(src, usedlen, lnump, errormsg, srcstart)
9238 char_u *src; /* pointer into commandline */
9239 int *usedlen; /* characters after src that are used */
9240 linenr_T *lnump; /* line number for :e command, or NULL */
9241 char_u **errormsg; /* pointer to error message */
9242 char_u *srcstart; /* beginning of valid memory for src */
9243{
9244 int i;
9245 char_u *s;
9246 char_u *result;
9247 char_u *resultbuf = NULL;
9248 int resultlen;
9249 buf_T *buf;
9250 int valid = VALID_HEAD + VALID_PATH; /* assume valid result */
9251 int spec_idx;
9252#ifdef FEAT_MODIFY_FNAME
9253 int skip_mod = FALSE;
9254#endif
9255 static char *(spec_str[]) =
9256 {
9257 "%",
9258#define SPEC_PERC 0
9259 "#",
9260#define SPEC_HASH 1
9261 "<cword>", /* cursor word */
9262#define SPEC_CWORD 2
9263 "<cWORD>", /* cursor WORD */
9264#define SPEC_CCWORD 3
9265 "<cfile>", /* cursor path name */
9266#define SPEC_CFILE 4
9267 "<sfile>", /* ":so" file name */
9268#define SPEC_SFILE 5
9269#ifdef FEAT_AUTOCMD
9270 "<afile>", /* autocommand file name */
9271# define SPEC_AFILE 6
9272 "<abuf>", /* autocommand buffer number */
9273# define SPEC_ABUF 7
9274 "<amatch>", /* autocommand match name */
9275# define SPEC_AMATCH 8
9276#endif
9277#ifdef FEAT_CLIENTSERVER
9278 "<client>"
9279# define SPEC_CLIENT 9
9280#endif
9281 };
9282#define SPEC_COUNT (sizeof(spec_str) / sizeof(char *))
9283
9284#if defined(FEAT_AUTOCMD) || defined(FEAT_CLIENTSERVER)
9285 char_u strbuf[30];
9286#endif
9287
9288 *errormsg = NULL;
9289
9290 /*
9291 * Check if there is something to do.
9292 */
9293 for (spec_idx = 0; spec_idx < SPEC_COUNT; ++spec_idx)
9294 {
9295 *usedlen = (int)STRLEN(spec_str[spec_idx]);
9296 if (STRNCMP(src, spec_str[spec_idx], *usedlen) == 0)
9297 break;
9298 }
9299 if (spec_idx == SPEC_COUNT) /* no match */
9300 {
9301 *usedlen = 1;
9302 return NULL;
9303 }
9304
9305 /*
9306 * Skip when preceded with a backslash "\%" and "\#".
9307 * Note: In "\\%" the % is also not recognized!
9308 */
9309 if (src > srcstart && src[-1] == '\\')
9310 {
9311 *usedlen = 0;
9312 STRCPY(src - 1, src); /* remove backslash */
9313 return NULL;
9314 }
9315
9316 /*
9317 * word or WORD under cursor
9318 */
9319 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD)
9320 {
9321 resultlen = find_ident_under_cursor(&result, spec_idx == SPEC_CWORD ?
9322 (FIND_IDENT|FIND_STRING) : FIND_STRING);
9323 if (resultlen == 0)
9324 {
9325 *errormsg = (char_u *)"";
9326 return NULL;
9327 }
9328 }
9329
9330 /*
9331 * '#': Alternate file name
9332 * '%': Current file name
9333 * File name under the cursor
9334 * File name for autocommand
9335 * and following modifiers
9336 */
9337 else
9338 {
9339 switch (spec_idx)
9340 {
9341 case SPEC_PERC: /* '%': current file */
9342 if (curbuf->b_fname == NULL)
9343 {
9344 result = (char_u *)"";
9345 valid = 0; /* Must have ":p:h" to be valid */
9346 }
9347 else
9348#ifdef RISCOS
9349 /* Always use the full path for RISC OS if possible. */
9350 result = curbuf->b_ffname;
9351 if (result == NULL)
9352 result = curbuf->b_fname;
9353#else
9354 result = curbuf->b_fname;
9355#endif
9356 break;
9357
9358 case SPEC_HASH: /* '#' or "#99": alternate file */
9359 if (src[1] == '#') /* "##": the argument list */
9360 {
9361 result = arg_all();
9362 resultbuf = result;
9363 *usedlen = 2;
9364#ifdef FEAT_MODIFY_FNAME
9365 skip_mod = TRUE;
9366#endif
9367 break;
9368 }
9369 s = src + 1;
9370 i = (int)getdigits(&s);
9371 *usedlen = (int)(s - src); /* length of what we expand */
9372
9373 buf = buflist_findnr(i);
9374 if (buf == NULL)
9375 {
9376 *errormsg = (char_u *)_("E194: No alternate file name to substitute for '#'");
9377 return NULL;
9378 }
9379 if (lnump != NULL)
9380 *lnump = ECMD_LAST;
9381 if (buf->b_fname == NULL)
9382 {
9383 result = (char_u *)"";
9384 valid = 0; /* Must have ":p:h" to be valid */
9385 }
9386 else
9387 result = buf->b_fname;
9388 break;
9389
9390#ifdef FEAT_SEARCHPATH
9391 case SPEC_CFILE: /* file name under cursor */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009392 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009393 if (result == NULL)
9394 {
9395 *errormsg = (char_u *)"";
9396 return NULL;
9397 }
9398 resultbuf = result; /* remember allocated string */
9399 break;
9400#endif
9401
9402#ifdef FEAT_AUTOCMD
9403 case SPEC_AFILE: /* file name for autocommand */
9404 result = autocmd_fname;
9405 if (result == NULL)
9406 {
9407 *errormsg = (char_u *)_("E495: no autocommand file name to substitute for \"<afile>\"");
9408 return NULL;
9409 }
9410 break;
9411
9412 case SPEC_ABUF: /* buffer number for autocommand */
9413 if (autocmd_bufnr <= 0)
9414 {
9415 *errormsg = (char_u *)_("E496: no autocommand buffer number to substitute for \"<abuf>\"");
9416 return NULL;
9417 }
9418 sprintf((char *)strbuf, "%d", autocmd_bufnr);
9419 result = strbuf;
9420 break;
9421
9422 case SPEC_AMATCH: /* match name for autocommand */
9423 result = autocmd_match;
9424 if (result == NULL)
9425 {
9426 *errormsg = (char_u *)_("E497: no autocommand match name to substitute for \"<amatch>\"");
9427 return NULL;
9428 }
9429 break;
9430
9431#endif
9432 case SPEC_SFILE: /* file name for ":so" command */
9433 result = sourcing_name;
9434 if (result == NULL)
9435 {
9436 *errormsg = (char_u *)_("E498: no :source file name to substitute for \"<sfile>\"");
9437 return NULL;
9438 }
9439 break;
9440#if defined(FEAT_CLIENTSERVER)
9441 case SPEC_CLIENT: /* Source of last submitted input */
9442 sprintf((char *)strbuf, "0x%x", (unsigned int)clientWindow);
9443 result = strbuf;
9444 break;
9445#endif
9446 }
9447
9448 resultlen = (int)STRLEN(result); /* length of new string */
9449 if (src[*usedlen] == '<') /* remove the file name extension */
9450 {
9451 ++*usedlen;
9452#ifdef RISCOS
9453 if ((s = vim_strrchr(result, '/')) != NULL && s >= gettail(result))
9454#else
9455 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
9456#endif
9457 resultlen = (int)(s - result);
9458 }
9459#ifdef FEAT_MODIFY_FNAME
9460 else if (!skip_mod)
9461 {
9462 valid |= modify_fname(src, usedlen, &result, &resultbuf,
9463 &resultlen);
9464 if (result == NULL)
9465 {
9466 *errormsg = (char_u *)"";
9467 return NULL;
9468 }
9469 }
9470#endif
9471 }
9472
9473 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
9474 {
9475 if (valid != VALID_HEAD + VALID_PATH)
9476 /* xgettext:no-c-format */
9477 *errormsg = (char_u *)_("E499: Empty file name for '%' or '#', only works with \":p:h\"");
9478 else
9479 *errormsg = (char_u *)_("E500: Evaluates to an empty string");
9480 result = NULL;
9481 }
9482 else
9483 result = vim_strnsave(result, resultlen);
9484 vim_free(resultbuf);
9485 return result;
9486}
9487
9488/*
9489 * Concatenate all files in the argument list, separated by spaces, and return
9490 * it in one allocated string.
9491 * Spaces and backslashes in the file names are escaped with a backslash.
9492 * Returns NULL when out of memory.
9493 */
9494 static char_u *
9495arg_all()
9496{
9497 int len;
9498 int idx;
9499 char_u *retval = NULL;
9500 char_u *p;
9501
9502 /*
9503 * Do this loop two times:
9504 * first time: compute the total length
9505 * second time: concatenate the names
9506 */
9507 for (;;)
9508 {
9509 len = 0;
9510 for (idx = 0; idx < ARGCOUNT; ++idx)
9511 {
9512 p = alist_name(&ARGLIST[idx]);
9513 if (p != NULL)
9514 {
9515 if (len > 0)
9516 {
9517 /* insert a space in between names */
9518 if (retval != NULL)
9519 retval[len] = ' ';
9520 ++len;
9521 }
9522 for ( ; *p != NUL; ++p)
9523 {
9524 if (*p == ' ' || *p == '\\')
9525 {
9526 /* insert a backslash */
9527 if (retval != NULL)
9528 retval[len] = '\\';
9529 ++len;
9530 }
9531 if (retval != NULL)
9532 retval[len] = *p;
9533 ++len;
9534 }
9535 }
9536 }
9537
9538 /* second time: break here */
9539 if (retval != NULL)
9540 {
9541 retval[len] = NUL;
9542 break;
9543 }
9544
9545 /* allocate memory */
9546 retval = alloc(len + 1);
9547 if (retval == NULL)
9548 break;
9549 }
9550
9551 return retval;
9552}
9553
9554#if defined(FEAT_AUTOCMD) || defined(PROTO)
9555/*
9556 * Expand the <sfile> string in "arg".
9557 *
9558 * Returns an allocated string, or NULL for any error.
9559 */
9560 char_u *
9561expand_sfile(arg)
9562 char_u *arg;
9563{
9564 char_u *errormsg;
9565 int len;
9566 char_u *result;
9567 char_u *newres;
9568 char_u *repl;
9569 int srclen;
9570 char_u *p;
9571
9572 result = vim_strsave(arg);
9573 if (result == NULL)
9574 return NULL;
9575
9576 for (p = result; *p; )
9577 {
9578 if (STRNCMP(p, "<sfile>", 7) != 0)
9579 ++p;
9580 else
9581 {
9582 /* replace "<sfile>" with the sourced file name, and do ":" stuff */
9583 repl = eval_vars(p, &srclen, NULL, &errormsg, result);
9584 if (errormsg != NULL)
9585 {
9586 if (*errormsg)
9587 emsg(errormsg);
9588 vim_free(result);
9589 return NULL;
9590 }
9591 if (repl == NULL) /* no match (cannot happen) */
9592 {
9593 p += srclen;
9594 continue;
9595 }
9596 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
9597 newres = alloc(len);
9598 if (newres == NULL)
9599 {
9600 vim_free(repl);
9601 vim_free(result);
9602 return NULL;
9603 }
9604 mch_memmove(newres, result, (size_t)(p - result));
9605 STRCPY(newres + (p - result), repl);
9606 len = (int)STRLEN(newres);
9607 STRCAT(newres, p + srclen);
9608 vim_free(repl);
9609 vim_free(result);
9610 result = newres;
9611 p = newres + len; /* continue after the match */
9612 }
9613 }
9614
9615 return result;
9616}
9617#endif
9618
9619#ifdef FEAT_SESSION
9620static int ses_winsizes __ARGS((FILE *fd, int restore_size));
9621static int ses_win_rec __ARGS((FILE *fd, frame_T *fr));
9622static frame_T *ses_skipframe __ARGS((frame_T *fr));
9623static int ses_do_frame __ARGS((frame_T *fr));
9624static int ses_do_win __ARGS((win_T *wp));
9625static int ses_arglist __ARGS((FILE *fd, char *cmd, garray_T *gap, int fullname, unsigned *flagp));
9626static int ses_put_fname __ARGS((FILE *fd, char_u *name, unsigned *flagp));
9627static int ses_fname __ARGS((FILE *fd, buf_T *buf, unsigned *flagp));
9628
9629/*
9630 * Write openfile commands for the current buffers to an .exrc file.
9631 * Return FAIL on error, OK otherwise.
9632 */
9633 static int
9634makeopens(fd, dirnow)
9635 FILE *fd;
9636 char_u *dirnow; /* Current directory name */
9637{
9638 buf_T *buf;
9639 int only_save_windows = TRUE;
9640 int nr;
9641 int cnr = 1;
9642 int restore_size = TRUE;
9643 win_T *wp;
9644 char_u *sname;
9645 win_T *edited_win = NULL;
Bram Moolenaar18144c82006-04-12 21:52:12 +00009646 tabpage_T *old_curtab = curtab;
9647 int tabnr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009648
9649 if (ssop_flags & SSOP_BUFFERS)
9650 only_save_windows = FALSE; /* Save ALL buffers */
9651
9652 /*
9653 * Begin by setting the this_session variable, and then other
9654 * sessionable variables.
9655 */
9656#ifdef FEAT_EVAL
9657 if (put_line(fd, "let v:this_session=expand(\"<sfile>:p\")") == FAIL)
9658 return FAIL;
9659 if (ssop_flags & SSOP_GLOBALS)
9660 if (store_session_globals(fd) == FAIL)
9661 return FAIL;
9662#endif
9663
9664 /*
9665 * Close all windows but one.
9666 */
9667 if (put_line(fd, "silent only") == FAIL)
9668 return FAIL;
9669
9670 /*
9671 * Now a :cd command to the session directory or the current directory
9672 */
9673 if (ssop_flags & SSOP_SESDIR)
9674 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009675 if (put_line(fd, "exe \"cd \" . escape(expand(\"<sfile>:p:h\"), ' ')")
9676 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009677 return FAIL;
9678 }
9679 else if (ssop_flags & SSOP_CURDIR)
9680 {
9681 sname = home_replace_save(NULL, globaldir != NULL ? globaldir : dirnow);
9682 if (sname == NULL
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009683 || fputs("cd ", fd) < 0
9684 || ses_put_fname(fd, sname, &ssop_flags) == FAIL
9685 || put_eol(fd) == FAIL)
9686 {
9687 vim_free(sname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009688 return FAIL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009689 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009690 vim_free(sname);
9691 }
9692
9693 /*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009694 * If there is an empty, unnamed buffer we will wipe it out later.
9695 * Remember the buffer number.
9696 */
9697 if (put_line(fd, "if expand('%') == '' && !&modified && line('$') <= 1 && getline(1) == ''") == FAIL)
9698 return FAIL;
9699 if (put_line(fd, " let s:wipebuf = bufnr('%')") == FAIL)
9700 return FAIL;
9701 if (put_line(fd, "endif") == FAIL)
9702 return FAIL;
9703
9704 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009705 * Now save the current files, current buffer first.
9706 */
9707 if (put_line(fd, "set shortmess=aoO") == FAIL)
9708 return FAIL;
9709
9710 /* Now put the other buffers into the buffer list */
9711 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9712 {
9713 if (!(only_save_windows && buf->b_nwindows == 0)
9714 && !(buf->b_help && !(ssop_flags & SSOP_HELP))
9715 && buf->b_fname != NULL
9716 && buf->b_p_bl)
9717 {
9718 if (fprintf(fd, "badd +%ld ", buf->b_wininfo == NULL ? 1L
9719 : buf->b_wininfo->wi_fpos.lnum) < 0
9720 || ses_fname(fd, buf, &ssop_flags) == FAIL)
9721 return FAIL;
9722 }
9723 }
9724
9725 /* the global argument list */
9726 if (ses_arglist(fd, "args", &global_alist.al_ga,
9727 !(ssop_flags & SSOP_CURDIR), &ssop_flags) == FAIL)
9728 return FAIL;
9729
9730 if (ssop_flags & SSOP_RESIZE)
9731 {
9732 /* Note: after the restore we still check it worked!*/
9733 if (fprintf(fd, "set lines=%ld columns=%ld" , Rows, Columns) < 0
9734 || put_eol(fd) == FAIL)
9735 return FAIL;
9736 }
9737
9738#ifdef FEAT_GUI
9739 if (gui.in_use && (ssop_flags & SSOP_WINPOS))
9740 {
9741 int x, y;
9742
9743 if (gui_mch_get_winpos(&x, &y) == OK)
9744 {
9745 /* Note: after the restore we still check it worked!*/
9746 if (fprintf(fd, "winpos %d %d", x, y) < 0 || put_eol(fd) == FAIL)
9747 return FAIL;
9748 }
9749 }
9750#endif
9751
9752 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +00009753 * May repeat putting Windows for each tab, when "tabpages" is in
9754 * 'sessionoptions'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009755 */
Bram Moolenaar18144c82006-04-12 21:52:12 +00009756 for (tabnr = 1; ; ++tabnr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009757 {
Bram Moolenaar18144c82006-04-12 21:52:12 +00009758 if ((ssop_flags & SSOP_TABPAGES))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009759 {
Bram Moolenaar18144c82006-04-12 21:52:12 +00009760 goto_tabpage(tabnr);
9761 if (tabnr > 1 && put_line(fd, "tabnew") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009762 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009763 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009764
Bram Moolenaar18144c82006-04-12 21:52:12 +00009765 /*
9766 * Before creating the window layout, try loading one file. If this
9767 * is aborted we don't end up with a number of useless windows.
9768 * This may have side effects! (e.g., compressed or network file).
9769 */
9770 for (wp = firstwin; wp != NULL; wp = wp->w_next)
9771 {
9772 if (ses_do_win(wp)
9773 && wp->w_buffer->b_ffname != NULL
9774 && !wp->w_buffer->b_help
9775#ifdef FEAT_QUICKFIX
9776 && !bt_nofile(wp->w_buffer)
9777#endif
9778 )
9779 {
9780 if (fputs("edit ", fd) < 0
9781 || ses_fname(fd, wp->w_buffer, &ssop_flags) == FAIL)
9782 return FAIL;
9783 if (!wp->w_arg_idx_invalid)
9784 edited_win = wp;
9785 break;
9786 }
9787 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009788
Bram Moolenaar18144c82006-04-12 21:52:12 +00009789 /*
9790 * Save current window layout.
9791 */
9792 if (put_line(fd, "set splitbelow splitright") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009793 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +00009794 if (ses_win_rec(fd, topframe) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009795 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +00009796 if (!p_sb && put_line(fd, "set nosplitbelow") == FAIL)
9797 return FAIL;
9798 if (!p_spr && put_line(fd, "set nosplitright") == FAIL)
9799 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009800
Bram Moolenaar18144c82006-04-12 21:52:12 +00009801 /*
9802 * Check if window sizes can be restored (no windows omitted).
9803 * Remember the window number of the current window after restoring.
9804 */
9805 nr = 0;
9806 for (wp = firstwin; wp != NULL; wp = W_NEXT(wp))
9807 {
9808 if (ses_do_win(wp))
9809 ++nr;
9810 else
9811 restore_size = FALSE;
9812 if (curwin == wp)
9813 cnr = nr;
9814 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009815
Bram Moolenaar18144c82006-04-12 21:52:12 +00009816 /* Go to the first window. */
9817 if (put_line(fd, "wincmd t") == FAIL)
9818 return FAIL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009819
Bram Moolenaar18144c82006-04-12 21:52:12 +00009820 /*
9821 * If more than one window, see if sizes can be restored.
9822 * First set 'winheight' and 'winwidth' to 1 to avoid the windows being
9823 * resized when moving between windows.
9824 * Do this before restoring the view, so that the topline and the
9825 * cursor can be set. This is done again below.
9826 */
9827 if (put_line(fd, "set winheight=1 winwidth=1") == FAIL)
9828 return FAIL;
9829 if (nr > 1 && ses_winsizes(fd, restore_size) == FAIL)
9830 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009831
Bram Moolenaar18144c82006-04-12 21:52:12 +00009832 /*
9833 * Restore the view of the window (options, file, cursor, etc.).
9834 */
9835 for (wp = firstwin; wp != NULL; wp = wp->w_next)
9836 {
9837 if (!ses_do_win(wp))
9838 continue;
9839 if (put_view(fd, wp, wp != edited_win, &ssop_flags) == FAIL)
9840 return FAIL;
9841 if (nr > 1 && put_line(fd, "wincmd w") == FAIL)
9842 return FAIL;
9843 }
9844
9845 /*
9846 * Restore cursor to the current window if it's not the first one.
9847 */
9848 if (cnr > 1 && (fprintf(fd, "%dwincmd w", cnr) < 0
9849 || put_eol(fd) == FAIL))
9850 return FAIL;
9851
9852 /*
9853 * Wipe out an empty unnamed buffer we started in.
9854 */
9855 if (put_line(fd, "if exists('s:wipebuf')") == FAIL)
9856 return FAIL;
9857 if (put_line(fd, " exe 'bwipe ' . s:wipebuf") == FAIL)
9858 return FAIL;
9859 if (put_line(fd, "endif") == FAIL)
9860 return FAIL;
9861 if (put_line(fd, "unlet! s:wipebuf") == FAIL)
9862 return FAIL;
9863
9864 /*
9865 * Restore window sizes again after jumping around in windows, because
9866 * the current window has a minimum size while others may not.
9867 */
9868 if (nr > 1 && ses_winsizes(fd, restore_size) == FAIL)
9869 return FAIL;
9870
9871 /* Re-apply 'winheight', 'winwidth' and 'shortmess'. */
9872 if (fprintf(fd, "set winheight=%ld winwidth=%ld shortmess=%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00009873 p_wh, p_wiw, p_shm) < 0 || put_eol(fd) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +00009874 return FAIL;
9875
9876 /* Don't continue in another tab page when doing only the current one
9877 * or when at the last tab page. */
9878 if (!(ssop_flags & SSOP_TABPAGES) || curtab->tp_next == NULL)
9879 break;
9880 }
9881
9882 if (ssop_flags & SSOP_TABPAGES)
9883 {
9884 if (valid_tabpage(old_curtab))
9885 goto_tabpage_tp(old_curtab);
9886 if (fprintf(fd, "tabnext %d", tabpage_index(curtab)) < 0
9887 || put_eol(fd) == FAIL)
9888 return FAIL;
9889 }
9890
Bram Moolenaar071d4272004-06-13 20:20:40 +00009891
9892 /*
9893 * Lastly, execute the x.vim file if it exists.
9894 */
9895 if (put_line(fd, "let s:sx = expand(\"<sfile>:p:r\").\"x.vim\"") == FAIL
9896 || put_line(fd, "if file_readable(s:sx)") == FAIL
9897 || put_line(fd, " exe \"source \" . s:sx") == FAIL
9898 || put_line(fd, "endif") == FAIL)
9899 return FAIL;
9900
9901 return OK;
9902}
9903
9904 static int
9905ses_winsizes(fd, restore_size)
9906 FILE *fd;
9907 int restore_size;
9908{
9909 int n = 0;
9910 win_T *wp;
9911
9912 if (restore_size && (ssop_flags & SSOP_WINSIZE))
9913 {
9914 for (wp = firstwin; wp != NULL; wp = wp->w_next)
9915 {
9916 if (!ses_do_win(wp))
9917 continue;
9918 ++n;
9919
9920 /* restore height when not full height */
9921 if (wp->w_height + wp->w_status_height < topframe->fr_height
9922 && (fprintf(fd,
9923 "exe '%dresize ' . ((&lines * %ld + %ld) / %ld)",
9924 n, (long)wp->w_height, Rows / 2, Rows) < 0
9925 || put_eol(fd) == FAIL))
9926 return FAIL;
9927
9928 /* restore width when not full width */
9929 if (wp->w_width < Columns && (fprintf(fd,
9930 "exe 'vert %dresize ' . ((&columns * %ld + %ld) / %ld)",
9931 n, (long)wp->w_width, Columns / 2, Columns) < 0
9932 || put_eol(fd) == FAIL))
9933 return FAIL;
9934 }
9935 }
9936 else
9937 {
9938 /* Just equalise window sizes */
9939 if (put_line(fd, "wincmd =") == FAIL)
9940 return FAIL;
9941 }
9942 return OK;
9943}
9944
9945/*
9946 * Write commands to "fd" to recursively create windows for frame "fr",
9947 * horizontally and vertically split.
9948 * After the commands the last window in the frame is the current window.
9949 * Returns FAIL when writing the commands to "fd" fails.
9950 */
9951 static int
9952ses_win_rec(fd, fr)
9953 FILE *fd;
9954 frame_T *fr;
9955{
9956 frame_T *frc;
9957 int count = 0;
9958
9959 if (fr->fr_layout != FR_LEAF)
9960 {
9961 /* Find first frame that's not skipped and then create a window for
9962 * each following one (first frame is already there). */
9963 frc = ses_skipframe(fr->fr_child);
9964 if (frc != NULL)
9965 while ((frc = ses_skipframe(frc->fr_next)) != NULL)
9966 {
9967 /* Make window as big as possible so that we have lots of room
9968 * to split. */
9969 if (put_line(fd, "wincmd _ | wincmd |") == FAIL
9970 || put_line(fd, fr->fr_layout == FR_COL
9971 ? "split" : "vsplit") == FAIL)
9972 return FAIL;
9973 ++count;
9974 }
9975
9976 /* Go back to the first window. */
9977 if (count > 0 && (fprintf(fd, fr->fr_layout == FR_COL
9978 ? "%dwincmd k" : "%dwincmd h", count) < 0
9979 || put_eol(fd) == FAIL))
9980 return FAIL;
9981
9982 /* Recursively create frames/windows in each window of this column or
9983 * row. */
9984 frc = ses_skipframe(fr->fr_child);
9985 while (frc != NULL)
9986 {
9987 ses_win_rec(fd, frc);
9988 frc = ses_skipframe(frc->fr_next);
9989 /* Go to next window. */
9990 if (frc != NULL && put_line(fd, "wincmd w") == FAIL)
9991 return FAIL;
9992 }
9993 }
9994 return OK;
9995}
9996
9997/*
9998 * Skip frames that don't contain windows we want to save in the Session.
9999 * Returns NULL when there none.
10000 */
10001 static frame_T *
10002ses_skipframe(fr)
10003 frame_T *fr;
10004{
10005 frame_T *frc;
10006
10007 for (frc = fr; frc != NULL; frc = frc->fr_next)
10008 if (ses_do_frame(frc))
10009 break;
10010 return frc;
10011}
10012
10013/*
10014 * Return TRUE if frame "fr" has a window somewhere that we want to save in
10015 * the Session.
10016 */
10017 static int
10018ses_do_frame(fr)
10019 frame_T *fr;
10020{
10021 frame_T *frc;
10022
10023 if (fr->fr_layout == FR_LEAF)
10024 return ses_do_win(fr->fr_win);
10025 for (frc = fr->fr_child; frc != NULL; frc = frc->fr_next)
10026 if (ses_do_frame(frc))
10027 return TRUE;
10028 return FALSE;
10029}
10030
10031/*
10032 * Return non-zero if window "wp" is to be stored in the Session.
10033 */
10034 static int
10035ses_do_win(wp)
10036 win_T *wp;
10037{
10038 if (wp->w_buffer->b_fname == NULL
10039#ifdef FEAT_QUICKFIX
10040 /* When 'buftype' is "nofile" can't restore the window contents. */
10041 || bt_nofile(wp->w_buffer)
10042#endif
10043 )
10044 return (ssop_flags & SSOP_BLANK);
10045 if (wp->w_buffer->b_help)
10046 return (ssop_flags & SSOP_HELP);
10047 return TRUE;
10048}
10049
10050/*
10051 * Write commands to "fd" to restore the view of a window.
10052 * Caller must make sure 'scrolloff' is zero.
10053 */
10054 static int
10055put_view(fd, wp, add_edit, flagp)
10056 FILE *fd;
10057 win_T *wp;
10058 int add_edit; /* add ":edit" command to view */
10059 unsigned *flagp; /* vop_flags or ssop_flags */
10060{
10061 win_T *save_curwin;
10062 int f;
10063 int do_cursor;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010064 int did_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010065
10066 /* Always restore cursor position for ":mksession". For ":mkview" only
10067 * when 'viewoptions' contains "cursor". */
10068 do_cursor = (flagp == &ssop_flags || *flagp & SSOP_CURSOR);
10069
10070 /*
10071 * Local argument list.
10072 */
10073 if (wp->w_alist == &global_alist)
10074 {
10075 if (put_line(fd, "argglobal") == FAIL)
10076 return FAIL;
10077 }
10078 else
10079 {
10080 if (ses_arglist(fd, "arglocal", &wp->w_alist->al_ga,
10081 flagp == &vop_flags
10082 || !(*flagp & SSOP_CURDIR)
10083 || wp->w_localdir != NULL, flagp) == FAIL)
10084 return FAIL;
10085 }
10086
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010087 /* Only when part of a session: restore the argument index. Some
10088 * arguments may have been deleted, check if the index is valid. */
10089 if (wp->w_arg_idx != 0 && wp->w_arg_idx <= WARGCOUNT(wp)
10090 && flagp == &ssop_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010091 {
10092 if (fprintf(fd, "%ldnext", (long)wp->w_arg_idx) < 0
10093 || put_eol(fd) == FAIL)
10094 return FAIL;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010095 did_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010096 }
10097
10098 /* Edit the file. Skip this when ":next" already did it. */
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010099 if (add_edit && (!did_next || wp->w_arg_idx_invalid))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010100 {
10101 /*
10102 * Load the file.
10103 */
10104 if (wp->w_buffer->b_ffname != NULL
10105#ifdef FEAT_QUICKFIX
10106 && !bt_nofile(wp->w_buffer)
10107#endif
10108 )
10109 {
10110 /*
10111 * Editing a file in this buffer: use ":edit file".
10112 * This may have side effects! (e.g., compressed or network file).
10113 */
10114 if (fputs("edit ", fd) < 0
10115 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
10116 return FAIL;
10117 }
10118 else
10119 {
10120 /* No file in this buffer, just make it empty. */
10121 if (put_line(fd, "enew") == FAIL)
10122 return FAIL;
10123#ifdef FEAT_QUICKFIX
10124 if (wp->w_buffer->b_ffname != NULL)
10125 {
10126 /* The buffer does have a name, but it's not a file name. */
10127 if (fputs("file ", fd) < 0
10128 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
10129 return FAIL;
10130 }
10131#endif
10132 do_cursor = FALSE;
10133 }
10134 }
10135
10136 /*
10137 * Local mappings and abbreviations.
10138 */
10139 if ((*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
10140 && makemap(fd, wp->w_buffer) == FAIL)
10141 return FAIL;
10142
10143 /*
10144 * Local options. Need to go to the window temporarily.
10145 * Store only local values when using ":mkview" and when ":mksession" is
10146 * used and 'sessionoptions' doesn't include "options".
10147 * Some folding options are always stored when "folds" is included,
10148 * otherwise the folds would not be restored correctly.
10149 */
10150 save_curwin = curwin;
10151 curwin = wp;
10152 curbuf = curwin->w_buffer;
10153 if (*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
10154 f = makeset(fd, OPT_LOCAL,
10155 flagp == &vop_flags || !(*flagp & SSOP_OPTIONS));
10156#ifdef FEAT_FOLDING
10157 else if (*flagp & SSOP_FOLDS)
10158 f = makefoldset(fd);
10159#endif
10160 else
10161 f = OK;
10162 curwin = save_curwin;
10163 curbuf = curwin->w_buffer;
10164 if (f == FAIL)
10165 return FAIL;
10166
10167#ifdef FEAT_FOLDING
10168 /*
10169 * Save Folds when 'buftype' is empty and for help files.
10170 */
10171 if ((*flagp & SSOP_FOLDS)
10172 && wp->w_buffer->b_ffname != NULL
Bram Moolenaarb1b715d2006-01-21 22:09:43 +000010173# ifdef FEAT_QUICKFIX
10174 && (*wp->w_buffer->b_p_bt == NUL || wp->w_buffer->b_help)
10175# endif
10176 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000010177 {
10178 if (put_folds(fd, wp) == FAIL)
10179 return FAIL;
10180 }
10181#endif
10182
10183 /*
10184 * Set the cursor after creating folds, since that moves the cursor.
10185 */
10186 if (do_cursor)
10187 {
10188
10189 /* Restore the cursor line in the file and relatively in the
10190 * window. Don't use "G", it changes the jumplist. */
10191 if (fprintf(fd, "let s:l = %ld - ((%ld * winheight(0) + %ld) / %ld)",
10192 (long)wp->w_cursor.lnum,
10193 (long)(wp->w_cursor.lnum - wp->w_topline),
10194 (long)wp->w_height / 2, (long)wp->w_height) < 0
10195 || put_eol(fd) == FAIL
10196 || put_line(fd, "if s:l < 1 | let s:l = 1 | endif") == FAIL
10197 || put_line(fd, "exe s:l") == FAIL
10198 || put_line(fd, "normal! zt") == FAIL
10199 || fprintf(fd, "%ld", (long)wp->w_cursor.lnum) < 0
10200 || put_eol(fd) == FAIL)
10201 return FAIL;
10202 /* Restore the cursor column and left offset when not wrapping. */
10203 if (wp->w_cursor.col == 0)
10204 {
10205 if (put_line(fd, "normal! 0") == FAIL)
10206 return FAIL;
10207 }
10208 else
10209 {
10210 if (!wp->w_p_wrap && wp->w_leftcol > 0 && wp->w_width > 0)
10211 {
10212 if (fprintf(fd,
10213 "let s:c = %ld - ((%ld * winwidth(0) + %ld) / %ld)",
10214 (long)wp->w_cursor.col,
10215 (long)(wp->w_cursor.col - wp->w_leftcol),
10216 (long)wp->w_width / 2, (long)wp->w_width) < 0
10217 || put_eol(fd) == FAIL
10218 || put_line(fd, "if s:c > 0") == FAIL
10219 || fprintf(fd,
10220 " exe 'normal! 0' . s:c . 'lzs' . (%ld - s:c) . 'l'",
10221 (long)wp->w_cursor.col) < 0
10222 || put_eol(fd) == FAIL
10223 || put_line(fd, "else") == FAIL
10224 || fprintf(fd, " normal! 0%dl", wp->w_cursor.col) < 0
10225 || put_eol(fd) == FAIL
10226 || put_line(fd, "endif") == FAIL)
10227 return FAIL;
10228 }
10229 else
10230 {
10231 if (fprintf(fd, "normal! 0%dl", wp->w_cursor.col) < 0
10232 || put_eol(fd) == FAIL)
10233 return FAIL;
10234 }
10235 }
10236 }
10237
10238 /*
10239 * Local directory.
10240 */
10241 if (wp->w_localdir != NULL)
10242 {
10243 if (fputs("lcd ", fd) < 0
10244 || ses_put_fname(fd, wp->w_localdir, flagp) == FAIL
10245 || put_eol(fd) == FAIL)
10246 return FAIL;
10247 }
10248
10249 return OK;
10250}
10251
10252/*
10253 * Write an argument list to the session file.
10254 * Returns FAIL if writing fails.
10255 */
10256 static int
10257ses_arglist(fd, cmd, gap, fullname, flagp)
10258 FILE *fd;
10259 char *cmd;
10260 garray_T *gap;
10261 int fullname; /* TRUE: use full path name */
10262 unsigned *flagp;
10263{
10264 int i;
10265 char_u buf[MAXPATHL];
10266 char_u *s;
10267
10268 if (gap->ga_len == 0)
10269 return put_line(fd, "silent! argdel *");
10270 if (fputs(cmd, fd) < 0)
10271 return FAIL;
10272 for (i = 0; i < gap->ga_len; ++i)
10273 {
10274 /* NULL file names are skipped (only happens when out of memory). */
10275 s = alist_name(&((aentry_T *)gap->ga_data)[i]);
10276 if (s != NULL)
10277 {
10278 if (fullname)
10279 {
10280 (void)vim_FullName(s, buf, MAXPATHL, FALSE);
10281 s = buf;
10282 }
10283 if (fputs(" ", fd) < 0 || ses_put_fname(fd, s, flagp) == FAIL)
10284 return FAIL;
10285 }
10286 }
10287 return put_eol(fd);
10288}
10289
10290/*
10291 * Write a buffer name to the session file.
10292 * Also ends the line.
10293 * Returns FAIL if writing fails.
10294 */
10295 static int
10296ses_fname(fd, buf, flagp)
10297 FILE *fd;
10298 buf_T *buf;
10299 unsigned *flagp;
10300{
10301 char_u *name;
10302
10303 /* Use the short file name if the current directory is known at the time
10304 * the session file will be sourced. Don't do this for ":mkview", we
10305 * don't know the current directory. */
10306 if (buf->b_sfname != NULL
10307 && flagp == &ssop_flags
10308 && (ssop_flags & (SSOP_CURDIR | SSOP_SESDIR)))
10309 name = buf->b_sfname;
10310 else
10311 name = buf->b_ffname;
10312 if (ses_put_fname(fd, name, flagp) == FAIL || put_eol(fd) == FAIL)
10313 return FAIL;
10314 return OK;
10315}
10316
10317/*
10318 * Write a file name to the session file.
10319 * Takes care of the "slash" option in 'sessionoptions' and escapes special
10320 * characters.
10321 * Returns FAIL if writing fails.
10322 */
10323 static int
10324ses_put_fname(fd, name, flagp)
10325 FILE *fd;
10326 char_u *name;
10327 unsigned *flagp;
10328{
10329 char_u *sname;
10330 int retval = OK;
10331 int c;
10332
10333 sname = home_replace_save(NULL, name);
10334 if (sname != NULL)
10335 name = sname;
10336 while (*name != NUL)
10337 {
10338#ifdef FEAT_MBYTE
10339 {
10340 int l;
10341
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010342 if (has_mbyte && (l = (*mb_ptr2len)(name)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010343 {
10344 /* copy a multibyte char */
10345 while (--l >= 0)
10346 {
10347 if (putc(*name, fd) != *name)
10348 retval = FAIL;
10349 ++name;
10350 }
10351 continue;
10352 }
10353 }
10354#endif
10355 c = *name++;
10356 if (c == '\\' && (*flagp & SSOP_SLASH))
10357 /* change a backslash to a forward slash */
10358 c = '/';
10359 else if ((vim_strchr(escape_chars, c) != NULL
10360#ifdef BACKSLASH_IN_FILENAME
10361 && c != '\\'
10362#endif
10363 ) || c == '#' || c == '%')
10364 {
10365 /* escape a special character with a backslash */
10366 if (putc('\\', fd) != '\\')
10367 retval = FAIL;
10368 }
10369 if (putc(c, fd) != c)
10370 retval = FAIL;
10371 }
10372 vim_free(sname);
10373 return retval;
10374}
10375
10376/*
10377 * ":loadview [nr]"
10378 */
10379 static void
10380ex_loadview(eap)
10381 exarg_T *eap;
10382{
10383 char_u *fname;
10384
10385 fname = get_view_file(*eap->arg);
10386 if (fname != NULL)
10387 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010388 do_source(fname, FALSE, DOSO_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010389 vim_free(fname);
10390 }
10391}
10392
10393/*
10394 * Get the name of the view file for the current buffer.
10395 */
10396 static char_u *
10397get_view_file(c)
10398 int c;
10399{
10400 int len = 0;
10401 char_u *p, *s;
10402 char_u *retval;
10403 char_u *sname;
10404
10405 if (curbuf->b_ffname == NULL)
10406 {
10407 EMSG(_(e_noname));
10408 return NULL;
10409 }
10410 sname = home_replace_save(NULL, curbuf->b_ffname);
10411 if (sname == NULL)
10412 return NULL;
10413
10414 /*
10415 * We want a file name without separators, because we're not going to make
10416 * a directory.
10417 * "normal" path separator -> "=+"
10418 * "=" -> "=="
10419 * ":" path separator -> "=-"
10420 */
10421 for (p = sname; *p; ++p)
10422 if (*p == '=' || vim_ispathsep(*p))
10423 ++len;
10424 retval = alloc((unsigned)(STRLEN(sname) + len + STRLEN(p_vdir) + 9));
10425 if (retval != NULL)
10426 {
10427 STRCPY(retval, p_vdir);
10428 add_pathsep(retval);
10429 s = retval + STRLEN(retval);
10430 for (p = sname; *p; ++p)
10431 {
10432 if (*p == '=')
10433 {
10434 *s++ = '=';
10435 *s++ = '=';
10436 }
10437 else if (vim_ispathsep(*p))
10438 {
10439 *s++ = '=';
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000010440#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA) || defined(RISCOS) \
Bram Moolenaar071d4272004-06-13 20:20:40 +000010441 || defined(VMS)
10442 if (*p == ':')
10443 *s++ = '-';
10444 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010445#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000010446 *s++ = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010447 }
10448 else
10449 *s++ = *p;
10450 }
10451 *s++ = '=';
10452 *s++ = c;
10453 STRCPY(s, ".vim");
10454 }
10455
10456 vim_free(sname);
10457 return retval;
10458}
10459
10460#endif /* FEAT_SESSION */
10461
10462/*
10463 * Write end-of-line character(s) for ":mkexrc", ":mkvimrc" and ":mksession".
10464 * Return FAIL for a write error.
10465 */
10466 int
10467put_eol(fd)
10468 FILE *fd;
10469{
10470 if (
10471#ifdef USE_CRNL
10472 (
10473# ifdef MKSESSION_NL
10474 !mksession_nl &&
10475# endif
10476 (putc('\r', fd) < 0)) ||
10477#endif
10478 (putc('\n', fd) < 0))
10479 return FAIL;
10480 return OK;
10481}
10482
10483/*
10484 * Write a line to "fd".
10485 * Return FAIL for a write error.
10486 */
10487 int
10488put_line(fd, s)
10489 FILE *fd;
10490 char *s;
10491{
10492 if (fputs(s, fd) < 0 || put_eol(fd) == FAIL)
10493 return FAIL;
10494 return OK;
10495}
10496
10497#ifdef FEAT_VIMINFO
10498/*
10499 * ":rviminfo" and ":wviminfo".
10500 */
10501 static void
10502ex_viminfo(eap)
10503 exarg_T *eap;
10504{
10505 char_u *save_viminfo;
10506
10507 save_viminfo = p_viminfo;
10508 if (*p_viminfo == NUL)
10509 p_viminfo = (char_u *)"'100";
10510 if (eap->cmdidx == CMD_rviminfo)
10511 {
10512 if (read_viminfo(eap->arg, TRUE, TRUE, eap->forceit) == FAIL)
10513 EMSG(_("E195: Cannot open viminfo file for reading"));
10514 }
10515 else
10516 write_viminfo(eap->arg, eap->forceit);
10517 p_viminfo = save_viminfo;
10518}
10519#endif
10520
10521#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +000010522/*
10523 * Make a dialog message in "buff[IOSIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +000010524 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +000010525 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010526 void
10527dialog_msg(buff, format, fname)
10528 char_u *buff;
10529 char *format;
10530 char_u *fname;
10531{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010532 if (fname == NULL)
10533 fname = (char_u *)_("Untitled");
Bram Moolenaarb765d632005-06-07 21:00:02 +000010534 vim_snprintf((char *)buff, IOSIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010535}
10536#endif
10537
10538/*
10539 * ":behave {mswin,xterm}"
10540 */
10541 static void
10542ex_behave(eap)
10543 exarg_T *eap;
10544{
10545 if (STRCMP(eap->arg, "mswin") == 0)
10546 {
10547 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
10548 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
10549 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
10550 set_option_value((char_u *)"keymodel", 0L,
10551 (char_u *)"startsel,stopsel", 0);
10552 }
10553 else if (STRCMP(eap->arg, "xterm") == 0)
10554 {
10555 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
10556 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
10557 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
10558 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
10559 }
10560 else
10561 EMSG2(_(e_invarg2), eap->arg);
10562}
10563
10564#ifdef FEAT_AUTOCMD
10565static int filetype_detect = FALSE;
10566static int filetype_plugin = FALSE;
10567static int filetype_indent = FALSE;
10568
10569/*
10570 * ":filetype [plugin] [indent] {on,off,detect}"
10571 * on: Load the filetype.vim file to install autocommands for file types.
10572 * off: Load the ftoff.vim file to remove all autocommands for file types.
10573 * plugin on: load filetype.vim and ftplugin.vim
10574 * plugin off: load ftplugof.vim
10575 * indent on: load filetype.vim and indent.vim
10576 * indent off: load indoff.vim
10577 */
10578 static void
10579ex_filetype(eap)
10580 exarg_T *eap;
10581{
10582 char_u *arg = eap->arg;
10583 int plugin = FALSE;
10584 int indent = FALSE;
10585
10586 if (*eap->arg == NUL)
10587 {
10588 /* Print current status. */
10589 smsg((char_u *)"filetype detection:%s plugin:%s indent:%s",
10590 filetype_detect ? "ON" : "OFF",
10591 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
10592 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
10593 return;
10594 }
10595
10596 /* Accept "plugin" and "indent" in any order. */
10597 for (;;)
10598 {
10599 if (STRNCMP(arg, "plugin", 6) == 0)
10600 {
10601 plugin = TRUE;
10602 arg = skipwhite(arg + 6);
10603 continue;
10604 }
10605 if (STRNCMP(arg, "indent", 6) == 0)
10606 {
10607 indent = TRUE;
10608 arg = skipwhite(arg + 6);
10609 continue;
10610 }
10611 break;
10612 }
10613 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
10614 {
10615 if (*arg == 'o' || !filetype_detect)
10616 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000010617 source_runtime((char_u *)FILETYPE_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010618 filetype_detect = TRUE;
10619 if (plugin)
10620 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000010621 source_runtime((char_u *)FTPLUGIN_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010622 filetype_plugin = TRUE;
10623 }
10624 if (indent)
10625 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000010626 source_runtime((char_u *)INDENT_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010627 filetype_indent = TRUE;
10628 }
10629 }
10630 if (*arg == 'd')
10631 {
10632 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE);
Bram Moolenaara3227e22006-03-08 21:32:40 +000010633 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010634 }
10635 }
10636 else if (STRCMP(arg, "off") == 0)
10637 {
10638 if (plugin || indent)
10639 {
10640 if (plugin)
10641 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000010642 source_runtime((char_u *)FTPLUGOF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010643 filetype_plugin = FALSE;
10644 }
10645 if (indent)
10646 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000010647 source_runtime((char_u *)INDOFF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010648 filetype_indent = FALSE;
10649 }
10650 }
10651 else
10652 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000010653 source_runtime((char_u *)FTOFF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010654 filetype_detect = FALSE;
10655 }
10656 }
10657 else
10658 EMSG2(_(e_invarg2), arg);
10659}
10660
10661/*
10662 * ":setfiletype {name}"
10663 */
10664 static void
10665ex_setfiletype(eap)
10666 exarg_T *eap;
10667{
10668 if (!did_filetype)
10669 set_option_value((char_u *)"filetype", 0L, eap->arg, OPT_LOCAL);
10670}
10671#endif
10672
10673/*ARGSUSED*/
10674 static void
10675ex_digraphs(eap)
10676 exarg_T *eap;
10677{
10678#ifdef FEAT_DIGRAPHS
10679 if (*eap->arg != NUL)
10680 putdigraph(eap->arg);
10681 else
10682 listdigraphs();
10683#else
10684 EMSG(_("E196: No digraphs in this version"));
10685#endif
10686}
10687
10688 static void
10689ex_set(eap)
10690 exarg_T *eap;
10691{
10692 int flags = 0;
10693
10694 if (eap->cmdidx == CMD_setlocal)
10695 flags = OPT_LOCAL;
10696 else if (eap->cmdidx == CMD_setglobal)
10697 flags = OPT_GLOBAL;
10698#if defined(FEAT_EVAL) && defined(FEAT_AUTOCMD) && defined(FEAT_BROWSE)
10699 if (cmdmod.browse && flags == 0)
10700 ex_options(eap);
10701 else
10702#endif
10703 (void)do_set(eap->arg, flags);
10704}
10705
10706#ifdef FEAT_SEARCH_EXTRA
10707/*
10708 * ":nohlsearch"
10709 */
10710/*ARGSUSED*/
10711 static void
10712ex_nohlsearch(eap)
10713 exarg_T *eap;
10714{
10715 no_hlsearch = TRUE;
Bram Moolenaarf71a3db2006-03-12 21:50:18 +000010716 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010717}
10718
10719/*
Bram Moolenaare1438bb2006-03-01 22:01:55 +000010720 * ":[N]match {group} {pattern}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010721 * Sets nextcmd to the start of the next command, if any. Also called when
10722 * skipping commands to find the next command.
10723 */
10724 static void
10725ex_match(eap)
10726 exarg_T *eap;
10727{
10728 char_u *p;
10729 char_u *end;
10730 int c;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000010731 int mi;
10732
10733 if (eap->line2 <= 3)
10734 mi = eap->line2 - 1;
10735 else
10736 {
10737 EMSG(e_invcmd);
10738 return;
10739 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010740
10741 /* First clear any old pattern. */
10742 if (!eap->skip)
10743 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +000010744 vim_free(curwin->w_match[mi].regprog);
10745 curwin->w_match[mi].regprog = NULL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010746 vim_free(curwin->w_match_pat[mi]);
10747 curwin->w_match_pat[mi] = NULL;
Bram Moolenaarf71a3db2006-03-12 21:50:18 +000010748 redraw_later(SOME_VALID); /* always need a redraw */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010749 }
10750
10751 if (ends_excmd(*eap->arg))
10752 end = eap->arg;
10753 else if ((STRNICMP(eap->arg, "none", 4) == 0
10754 && (vim_iswhite(eap->arg[4]) || ends_excmd(eap->arg[4]))))
10755 end = eap->arg + 4;
10756 else
10757 {
10758 p = skiptowhite(eap->arg);
10759 if (!eap->skip)
10760 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +000010761 curwin->w_match_id[mi] = syn_namen2id(eap->arg,
10762 (int)(p - eap->arg));
10763 if (curwin->w_match_id[mi] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010764 {
10765 EMSG2(_(e_nogroup), eap->arg);
10766 return;
10767 }
10768 }
10769 p = skipwhite(p);
10770 if (*p == NUL)
10771 {
10772 /* There must be two arguments. */
10773 EMSG2(_(e_invarg2), eap->arg);
10774 return;
10775 }
10776 end = skip_regexp(p + 1, *p, TRUE, NULL);
10777 if (!eap->skip)
10778 {
10779 if (*end != NUL && !ends_excmd(*skipwhite(end + 1)))
10780 {
10781 eap->errmsg = e_trailing;
10782 return;
10783 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000010784 if (*end != *p)
10785 {
10786 EMSG2(_(e_invarg2), p);
10787 return;
10788 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010789
10790 c = *end;
10791 *end = NUL;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000010792 curwin->w_match[mi].regprog = vim_regcomp(p + 1, RE_MAGIC);
Bram Moolenaare1438bb2006-03-01 22:01:55 +000010793 if (curwin->w_match[mi].regprog == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010794 {
10795 EMSG2(_(e_invarg2), p);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010796 *end = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010797 return;
10798 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010799 curwin->w_match_pat[mi] = vim_strsave(p + 1);
10800 *end = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010801 }
10802 }
10803 eap->nextcmd = find_nextcmd(end);
10804}
10805#endif
10806
10807#ifdef FEAT_CRYPT
10808/*
10809 * ":X": Get crypt key
10810 */
10811/*ARGSUSED*/
10812 static void
10813ex_X(eap)
10814 exarg_T *eap;
10815{
10816 (void)get_crypt_key(TRUE, TRUE);
10817}
10818#endif
10819
10820#ifdef FEAT_FOLDING
10821 static void
10822ex_fold(eap)
10823 exarg_T *eap;
10824{
10825 if (foldManualAllowed(TRUE))
10826 foldCreate(eap->line1, eap->line2);
10827}
10828
10829 static void
10830ex_foldopen(eap)
10831 exarg_T *eap;
10832{
10833 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
10834 eap->forceit, FALSE);
10835}
10836
10837 static void
10838ex_folddo(eap)
10839 exarg_T *eap;
10840{
10841 linenr_T lnum;
10842
10843 /* First set the marks for all lines closed/open. */
10844 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
10845 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
10846 ml_setmarked(lnum);
10847
10848 /* Execute the command on the marked lines. */
10849 global_exe(eap->arg);
10850 ml_clearmarked(); /* clear rest of the marks */
10851}
10852#endif